blob: d0b96a9ea16e623d670f67f2752578e01b2bc83f [file] [log] [blame]
Diego Novillo92aa8c22014-03-10 22:41:28 +00001; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/discriminator.prof | opt -analyze -branch-prob | FileCheck %s
Xinliang David Lid38392e2016-05-27 23:20:16 +00002; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/discriminator.prof | opt -analyze -branch-prob | FileCheck %s
Diego Novillo92aa8c22014-03-10 22:41:28 +00003
4; Original code
5;
6; 1 int foo(int i) {
7; 2 int x = 0;
8; 3 while (i < 100) {
9; 4 if (i < 5) x--;
10; 5 i++;
11; 6 }
12; 7 return x;
13; 8 }
14;
15; In this test, if the loop is executed 100 times, the decrement operation
16; at line 4 should only execute 5 times. This is reflected in the profile
17; data for line offset 3. In Inputs/discriminator.prof, we have:
18;
19; 3: 100
20; 3.1: 5
21;
22; This means that the predicate 'i < 5' (line 3) is executed 100 times,
23; but the then branch (line 3.1) is only executed 5 times.
24
Peter Collingbourned4bff302015-11-05 22:03:56 +000025define i32 @foo(i32 %i) #0 !dbg !4 {
Diego Novillo92aa8c22014-03-10 22:41:28 +000026; CHECK: Printing analysis 'Branch Probability Analysis' for function 'foo':
27entry:
28 %i.addr = alloca i32, align 4
29 %x = alloca i32, align 4
30 store i32 %i, i32* %i.addr, align 4
31 store i32 0, i32* %x, align 4, !dbg !10
32 br label %while.cond, !dbg !11
33
34while.cond: ; preds = %if.end, %entry
David Blaikiea79ac142015-02-27 21:17:42 +000035 %0 = load i32, i32* %i.addr, align 4, !dbg !12
Diego Novillo92aa8c22014-03-10 22:41:28 +000036 %cmp = icmp slt i32 %0, 100, !dbg !12
37 br i1 %cmp, label %while.body, label %while.end, !dbg !12
Dehao Chenc0a1e432016-08-12 16:22:12 +000038; CHECK: edge while.cond -> while.body probability is 0x7d83ba68 / 0x80000000 = 98.06% [HOT edge]
39; CHECK: edge while.cond -> while.end probability is 0x027c4598 / 0x80000000 = 1.94%
Diego Novillo92aa8c22014-03-10 22:41:28 +000040
41while.body: ; preds = %while.cond
David Blaikiea79ac142015-02-27 21:17:42 +000042 %1 = load i32, i32* %i.addr, align 4, !dbg !14
Diego Novillo92aa8c22014-03-10 22:41:28 +000043 %cmp1 = icmp slt i32 %1, 50, !dbg !14
44 br i1 %cmp1, label %if.then, label %if.end, !dbg !14
Dehao Chenc0a1e432016-08-12 16:22:12 +000045; CHECK: edge while.body -> if.then probability is 0x07878788 / 0x80000000 = 5.88%
46; CHECK: edge while.body -> if.end probability is 0x78787878 / 0x80000000 = 94.12% [HOT edge]
Diego Novillo92aa8c22014-03-10 22:41:28 +000047
48if.then: ; preds = %while.body
David Blaikiea79ac142015-02-27 21:17:42 +000049 %2 = load i32, i32* %x, align 4, !dbg !17
Diego Novillo92aa8c22014-03-10 22:41:28 +000050 %dec = add nsw i32 %2, -1, !dbg !17
51 store i32 %dec, i32* %x, align 4, !dbg !17
52 br label %if.end, !dbg !17
53
54if.end: ; preds = %if.then, %while.body
David Blaikiea79ac142015-02-27 21:17:42 +000055 %3 = load i32, i32* %i.addr, align 4, !dbg !19
Diego Novillo92aa8c22014-03-10 22:41:28 +000056 %inc = add nsw i32 %3, 1, !dbg !19
57 store i32 %inc, i32* %i.addr, align 4, !dbg !19
58 br label %while.cond, !dbg !20
59
60while.end: ; preds = %while.cond
David Blaikiea79ac142015-02-27 21:17:42 +000061 %4 = load i32, i32* %x, align 4, !dbg !21
Diego Novillo92aa8c22014-03-10 22:41:28 +000062 ret i32 %4, !dbg !21
63}
64
65
66!llvm.dbg.cu = !{!0}
67!llvm.module.flags = !{!7, !8}
68!llvm.ident = !{!9}
69
Adrian Prantl75819ae2016-04-15 15:57:41 +000070!0 = distinct !DICompileUnit(language: DW_LANG_C99, producer: "clang version 3.5 ", isOptimized: false, emissionKind: NoDebug, file: !1, enums: !2, retainedTypes: !2, globals: !2, imports: !2)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000071!1 = !DIFile(filename: "discriminator.c", directory: ".")
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +000072!2 = !{}
Adrian Prantl75819ae2016-04-15 15:57:41 +000073!4 = distinct !DISubprogram(name: "foo", line: 1, isLocal: false, isDefinition: true, virtualIndex: 6, flags: DIFlagPrototyped, isOptimized: false, unit: !0, scopeLine: 1, file: !1, scope: !5, type: !6, variables: !2)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000074!5 = !DIFile(filename: "discriminator.c", directory: ".")
75!6 = !DISubroutineType(types: !2)
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +000076!7 = !{i32 2, !"Dwarf Version", i32 4}
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +000077!8 = !{i32 1, !"Debug Info Version", i32 3}
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +000078!9 = !{!"clang version 3.5 "}
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000079!10 = !DILocation(line: 2, scope: !4)
80!11 = !DILocation(line: 3, scope: !4)
81!12 = !DILocation(line: 3, scope: !13)
82!13 = !DILexicalBlockFile(discriminator: 1, file: !1, scope: !4)
83!14 = !DILocation(line: 4, scope: !15)
84!15 = distinct !DILexicalBlock(line: 4, column: 0, file: !1, scope: !16)
85!16 = distinct !DILexicalBlock(line: 3, column: 0, file: !1, scope: !4)
86!17 = !DILocation(line: 4, scope: !18)
87!18 = !DILexicalBlockFile(discriminator: 1, file: !1, scope: !15)
88!19 = !DILocation(line: 5, scope: !16)
89!20 = !DILocation(line: 6, scope: !16)
90!21 = !DILocation(line: 7, scope: !4)