xref: /aosp_15_r20/external/llvm/test/Transforms/SampleProfile/calls.ll (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker; RUN: opt < %s -instcombine -sample-profile -sample-profile-file=%S/Inputs/calls.prof | opt -analyze -branch-prob | FileCheck %s
2*9880d681SAndroid Build Coastguard Worker; RUN: opt < %s -passes="function(instcombine),sample-profile" -sample-profile-file=%S/Inputs/calls.prof | opt -analyze -branch-prob | FileCheck %s
3*9880d681SAndroid Build Coastguard Worker
4*9880d681SAndroid Build Coastguard Worker; Original C++ test case
5*9880d681SAndroid Build Coastguard Worker;
6*9880d681SAndroid Build Coastguard Worker; #include <stdio.h>
7*9880d681SAndroid Build Coastguard Worker;
8*9880d681SAndroid Build Coastguard Worker; int sum(int x, int y) {
9*9880d681SAndroid Build Coastguard Worker;   return x + y;
10*9880d681SAndroid Build Coastguard Worker; }
11*9880d681SAndroid Build Coastguard Worker;
12*9880d681SAndroid Build Coastguard Worker; int main() {
13*9880d681SAndroid Build Coastguard Worker;   int s, i = 0;
14*9880d681SAndroid Build Coastguard Worker;   while (i++ < 20000 * 20000)
15*9880d681SAndroid Build Coastguard Worker;     if (i != 100) s = sum(i, s); else s = 30;
16*9880d681SAndroid Build Coastguard Worker;   printf("sum is %d\n", s);
17*9880d681SAndroid Build Coastguard Worker;   return 0;
18*9880d681SAndroid Build Coastguard Worker; }
19*9880d681SAndroid Build Coastguard Worker;
20*9880d681SAndroid Build Coastguard Worker@.str = private unnamed_addr constant [11 x i8] c"sum is %d\0A\00", align 1
21*9880d681SAndroid Build Coastguard Worker
22*9880d681SAndroid Build Coastguard Worker; Function Attrs: nounwind uwtable
23*9880d681SAndroid Build Coastguard Workerdefine i32 @_Z3sumii(i32 %x, i32 %y) !dbg !4 {
24*9880d681SAndroid Build Coastguard Workerentry:
25*9880d681SAndroid Build Coastguard Worker  %x.addr = alloca i32, align 4
26*9880d681SAndroid Build Coastguard Worker  %y.addr = alloca i32, align 4
27*9880d681SAndroid Build Coastguard Worker  store i32 %x, i32* %x.addr, align 4
28*9880d681SAndroid Build Coastguard Worker  store i32 %y, i32* %y.addr, align 4
29*9880d681SAndroid Build Coastguard Worker  %0 = load i32, i32* %x.addr, align 4, !dbg !11
30*9880d681SAndroid Build Coastguard Worker  %1 = load i32, i32* %y.addr, align 4, !dbg !11
31*9880d681SAndroid Build Coastguard Worker  %add = add nsw i32 %0, %1, !dbg !11
32*9880d681SAndroid Build Coastguard Worker  ret i32 %add, !dbg !11
33*9880d681SAndroid Build Coastguard Worker}
34*9880d681SAndroid Build Coastguard Worker
35*9880d681SAndroid Build Coastguard Worker; Function Attrs: uwtable
36*9880d681SAndroid Build Coastguard Workerdefine i32 @main() !dbg !7 {
37*9880d681SAndroid Build Coastguard Workerentry:
38*9880d681SAndroid Build Coastguard Worker  %retval = alloca i32, align 4
39*9880d681SAndroid Build Coastguard Worker  %s = alloca i32, align 4
40*9880d681SAndroid Build Coastguard Worker  %i = alloca i32, align 4
41*9880d681SAndroid Build Coastguard Worker  store i32 0, i32* %retval
42*9880d681SAndroid Build Coastguard Worker  store i32 0, i32* %i, align 4, !dbg !12
43*9880d681SAndroid Build Coastguard Worker  br label %while.cond, !dbg !13
44*9880d681SAndroid Build Coastguard Worker
45*9880d681SAndroid Build Coastguard Workerwhile.cond:                                       ; preds = %if.end, %entry
46*9880d681SAndroid Build Coastguard Worker  %0 = load i32, i32* %i, align 4, !dbg !14
47*9880d681SAndroid Build Coastguard Worker  %inc = add nsw i32 %0, 1, !dbg !14
48*9880d681SAndroid Build Coastguard Worker  store i32 %inc, i32* %i, align 4, !dbg !14
49*9880d681SAndroid Build Coastguard Worker  %cmp = icmp slt i32 %0, 400000000, !dbg !14
50*9880d681SAndroid Build Coastguard Worker  br i1 %cmp, label %while.body, label %while.end, !dbg !14
51*9880d681SAndroid Build Coastguard Worker; CHECK: edge while.cond -> while.body probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
52*9880d681SAndroid Build Coastguard Worker; CHECK: edge while.cond -> while.end probability is 0x00000000 / 0x80000000 = 0.00%
53*9880d681SAndroid Build Coastguard Worker
54*9880d681SAndroid Build Coastguard Workerwhile.body:                                       ; preds = %while.cond
55*9880d681SAndroid Build Coastguard Worker  %1 = load i32, i32* %i, align 4, !dbg !16
56*9880d681SAndroid Build Coastguard Worker  %cmp1 = icmp ne i32 %1, 100, !dbg !16
57*9880d681SAndroid Build Coastguard Worker  br i1 %cmp1, label %if.then, label %if.else, !dbg !16
58*9880d681SAndroid Build Coastguard Worker; Without discriminator information, the profiler used to think that
59*9880d681SAndroid Build Coastguard Worker; both branches out of while.body had the same weight. In reality,
60*9880d681SAndroid Build Coastguard Worker; the edge while.body->if.then is taken most of the time.
61*9880d681SAndroid Build Coastguard Worker;
62*9880d681SAndroid Build Coastguard Worker; CHECK: edge while.body -> if.else probability is 0x00000000 / 0x80000000 = 0.00%
63*9880d681SAndroid Build Coastguard Worker; CHECK: edge while.body -> if.then probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
64*9880d681SAndroid Build Coastguard Worker
65*9880d681SAndroid Build Coastguard Worker
66*9880d681SAndroid Build Coastguard Workerif.then:                                          ; preds = %while.body
67*9880d681SAndroid Build Coastguard Worker  %2 = load i32, i32* %i, align 4, !dbg !18
68*9880d681SAndroid Build Coastguard Worker  %3 = load i32, i32* %s, align 4, !dbg !18
69*9880d681SAndroid Build Coastguard Worker  %call = call i32 @_Z3sumii(i32 %2, i32 %3), !dbg !18
70*9880d681SAndroid Build Coastguard Worker  store i32 %call, i32* %s, align 4, !dbg !18
71*9880d681SAndroid Build Coastguard Worker  br label %if.end, !dbg !18
72*9880d681SAndroid Build Coastguard Worker
73*9880d681SAndroid Build Coastguard Workerif.else:                                          ; preds = %while.body
74*9880d681SAndroid Build Coastguard Worker  store i32 30, i32* %s, align 4, !dbg !20
75*9880d681SAndroid Build Coastguard Worker  br label %if.end
76*9880d681SAndroid Build Coastguard Worker
77*9880d681SAndroid Build Coastguard Workerif.end:                                           ; preds = %if.else, %if.then
78*9880d681SAndroid Build Coastguard Worker  br label %while.cond, !dbg !22
79*9880d681SAndroid Build Coastguard Worker
80*9880d681SAndroid Build Coastguard Workerwhile.end:                                        ; preds = %while.cond
81*9880d681SAndroid Build Coastguard Worker  %4 = load i32, i32* %s, align 4, !dbg !24
82*9880d681SAndroid Build Coastguard Worker  %call2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i32 0, i32 0), i32 %4), !dbg !24
83*9880d681SAndroid Build Coastguard Worker  ret i32 0, !dbg !25
84*9880d681SAndroid Build Coastguard Worker}
85*9880d681SAndroid Build Coastguard Worker
86*9880d681SAndroid Build Coastguard Workerdeclare i32 @printf(i8*, ...) #2
87*9880d681SAndroid Build Coastguard Worker
88*9880d681SAndroid Build Coastguard Worker!llvm.dbg.cu = !{!0}
89*9880d681SAndroid Build Coastguard Worker!llvm.module.flags = !{!8, !9}
90*9880d681SAndroid Build Coastguard Worker!llvm.ident = !{!10}
91*9880d681SAndroid Build Coastguard Worker
92*9880d681SAndroid Build Coastguard Worker!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, producer: "clang version 3.5 ", isOptimized: false, emissionKind: NoDebug, file: !1, enums: !2, retainedTypes: !2, globals: !2, imports: !2)
93*9880d681SAndroid Build Coastguard Worker!1 = !DIFile(filename: "calls.cc", directory: ".")
94*9880d681SAndroid Build Coastguard Worker!2 = !{}
95*9880d681SAndroid Build Coastguard Worker!4 = distinct !DISubprogram(name: "sum", line: 3, isLocal: false, isDefinition: true, virtualIndex: 6, flags: DIFlagPrototyped, isOptimized: false, unit: !0, scopeLine: 3, file: !1, scope: !5, type: !6, variables: !2)
96*9880d681SAndroid Build Coastguard Worker!5 = !DIFile(filename: "calls.cc", directory: ".")
97*9880d681SAndroid Build Coastguard Worker!6 = !DISubroutineType(types: !2)
98*9880d681SAndroid Build Coastguard Worker!7 = distinct !DISubprogram(name: "main", line: 7, isLocal: false, isDefinition: true, virtualIndex: 6, flags: DIFlagPrototyped, isOptimized: false, unit: !0, scopeLine: 7, file: !1, scope: !5, type: !6, variables: !2)
99*9880d681SAndroid Build Coastguard Worker!8 = !{i32 2, !"Dwarf Version", i32 4}
100*9880d681SAndroid Build Coastguard Worker!9 = !{i32 1, !"Debug Info Version", i32 3}
101*9880d681SAndroid Build Coastguard Worker!10 = !{!"clang version 3.5 "}
102*9880d681SAndroid Build Coastguard Worker!11 = !DILocation(line: 4, scope: !4)
103*9880d681SAndroid Build Coastguard Worker!12 = !DILocation(line: 8, scope: !7)
104*9880d681SAndroid Build Coastguard Worker!13 = !DILocation(line: 9, scope: !7)
105*9880d681SAndroid Build Coastguard Worker!14 = !DILocation(line: 9, scope: !15)
106*9880d681SAndroid Build Coastguard Worker!15 = !DILexicalBlockFile(discriminator: 1, file: !1, scope: !7)
107*9880d681SAndroid Build Coastguard Worker!16 = !DILocation(line: 10, scope: !17)
108*9880d681SAndroid Build Coastguard Worker!17 = distinct !DILexicalBlock(line: 10, column: 0, file: !1, scope: !7)
109*9880d681SAndroid Build Coastguard Worker!18 = !DILocation(line: 10, scope: !19)
110*9880d681SAndroid Build Coastguard Worker!19 = !DILexicalBlockFile(discriminator: 1, file: !1, scope: !17)
111*9880d681SAndroid Build Coastguard Worker!20 = !DILocation(line: 10, scope: !21)
112*9880d681SAndroid Build Coastguard Worker!21 = !DILexicalBlockFile(discriminator: 2, file: !1, scope: !17)
113*9880d681SAndroid Build Coastguard Worker!22 = !DILocation(line: 10, scope: !23)
114*9880d681SAndroid Build Coastguard Worker!23 = !DILexicalBlockFile(discriminator: 3, file: !1, scope: !17)
115*9880d681SAndroid Build Coastguard Worker!24 = !DILocation(line: 11, scope: !7)
116*9880d681SAndroid Build Coastguard Worker!25 = !DILocation(line: 12, scope: !7)
117