Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 1 | ; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/discriminator.prof | opt -analyze -branch-prob | FileCheck %s |
| 2 | |
| 3 | ; Original code |
| 4 | ; |
| 5 | ; 1 int foo(int i) { |
| 6 | ; 2 int x = 0; |
| 7 | ; 3 while (i < 100) { |
| 8 | ; 4 if (i < 5) x--; |
| 9 | ; 5 i++; |
| 10 | ; 6 } |
| 11 | ; 7 return x; |
| 12 | ; 8 } |
| 13 | ; |
| 14 | ; In this test, if the loop is executed 100 times, the decrement operation |
| 15 | ; at line 4 should only execute 5 times. This is reflected in the profile |
| 16 | ; data for line offset 3. In Inputs/discriminator.prof, we have: |
| 17 | ; |
| 18 | ; 3: 100 |
| 19 | ; 3.1: 5 |
| 20 | ; |
| 21 | ; This means that the predicate 'i < 5' (line 3) is executed 100 times, |
| 22 | ; but the then branch (line 3.1) is only executed 5 times. |
| 23 | |
| 24 | define i32 @foo(i32 %i) #0 { |
| 25 | ; CHECK: Printing analysis 'Branch Probability Analysis' for function 'foo': |
| 26 | entry: |
| 27 | %i.addr = alloca i32, align 4 |
| 28 | %x = alloca i32, align 4 |
| 29 | store i32 %i, i32* %i.addr, align 4 |
| 30 | store i32 0, i32* %x, align 4, !dbg !10 |
| 31 | br label %while.cond, !dbg !11 |
| 32 | |
| 33 | while.cond: ; preds = %if.end, %entry |
David Blaikie | a79ac14 | 2015-02-27 21:17:42 +0000 | [diff] [blame] | 34 | %0 = load i32, i32* %i.addr, align 4, !dbg !12 |
Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 35 | %cmp = icmp slt i32 %0, 100, !dbg !12 |
| 36 | br i1 %cmp, label %while.body, label %while.end, !dbg !12 |
| 37 | ; CHECK: edge while.cond -> while.body probability is 100 / 101 = 99.0099% [HOT edge] |
| 38 | ; CHECK: edge while.cond -> while.end probability is 1 / 101 = 0.990099% |
| 39 | |
| 40 | while.body: ; preds = %while.cond |
David Blaikie | a79ac14 | 2015-02-27 21:17:42 +0000 | [diff] [blame] | 41 | %1 = load i32, i32* %i.addr, align 4, !dbg !14 |
Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 42 | %cmp1 = icmp slt i32 %1, 50, !dbg !14 |
| 43 | br i1 %cmp1, label %if.then, label %if.end, !dbg !14 |
| 44 | ; CHECK: edge while.body -> if.then probability is 5 / 100 = 5% |
| 45 | ; CHECK: edge while.body -> if.end probability is 95 / 100 = 95% [HOT edge] |
| 46 | |
| 47 | if.then: ; preds = %while.body |
David Blaikie | a79ac14 | 2015-02-27 21:17:42 +0000 | [diff] [blame] | 48 | %2 = load i32, i32* %x, align 4, !dbg !17 |
Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 49 | %dec = add nsw i32 %2, -1, !dbg !17 |
| 50 | store i32 %dec, i32* %x, align 4, !dbg !17 |
| 51 | br label %if.end, !dbg !17 |
| 52 | |
| 53 | if.end: ; preds = %if.then, %while.body |
David Blaikie | a79ac14 | 2015-02-27 21:17:42 +0000 | [diff] [blame] | 54 | %3 = load i32, i32* %i.addr, align 4, !dbg !19 |
Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 55 | %inc = add nsw i32 %3, 1, !dbg !19 |
| 56 | store i32 %inc, i32* %i.addr, align 4, !dbg !19 |
| 57 | br label %while.cond, !dbg !20 |
| 58 | |
| 59 | while.end: ; preds = %while.cond |
David Blaikie | a79ac14 | 2015-02-27 21:17:42 +0000 | [diff] [blame] | 60 | %4 = load i32, i32* %x, align 4, !dbg !21 |
Diego Novillo | 92aa8c2 | 2014-03-10 22:41:28 +0000 | [diff] [blame] | 61 | ret i32 %4, !dbg !21 |
| 62 | } |
| 63 | |
| 64 | |
| 65 | !llvm.dbg.cu = !{!0} |
| 66 | !llvm.module.flags = !{!7, !8} |
| 67 | !llvm.ident = !{!9} |
| 68 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame^] | 69 | !0 = !MDCompileUnit(language: DW_LANG_C99, producer: "clang version 3.5 ", isOptimized: false, emissionKind: 0, file: !1, enums: !2, retainedTypes: !2, subprograms: !3, globals: !2, imports: !2) |
| 70 | !1 = !MDFile(filename: "discriminator.c", directory: ".") |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 71 | !2 = !{} |
| 72 | !3 = !{!4} |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame^] | 73 | !4 = !MDSubprogram(name: "foo", line: 1, isLocal: false, isDefinition: true, virtualIndex: 6, flags: DIFlagPrototyped, isOptimized: false, scopeLine: 1, file: !1, scope: !5, type: !6, function: i32 (i32)* @foo, variables: !2) |
| 74 | !5 = !MDFile(filename: "discriminator.c", directory: ".") |
| 75 | !6 = !MDSubroutineType(types: !2) |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 76 | !7 = !{i32 2, !"Dwarf Version", i32 4} |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame^] | 77 | !8 = !{i32 1, !"Debug Info Version", i32 3} |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 78 | !9 = !{!"clang version 3.5 "} |
Duncan P. N. Exon Smith | 9885469 | 2015-01-14 22:27:36 +0000 | [diff] [blame] | 79 | !10 = !MDLocation(line: 2, scope: !4) |
| 80 | !11 = !MDLocation(line: 3, scope: !4) |
| 81 | !12 = !MDLocation(line: 3, scope: !13) |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame^] | 82 | !13 = !MDLexicalBlockFile(discriminator: 1, file: !1, scope: !4) |
Duncan P. N. Exon Smith | 9885469 | 2015-01-14 22:27:36 +0000 | [diff] [blame] | 83 | !14 = !MDLocation(line: 4, scope: !15) |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame^] | 84 | !15 = distinct !MDLexicalBlock(line: 4, column: 0, file: !1, scope: !16) |
| 85 | !16 = distinct !MDLexicalBlock(line: 3, column: 0, file: !1, scope: !4) |
Duncan P. N. Exon Smith | 9885469 | 2015-01-14 22:27:36 +0000 | [diff] [blame] | 86 | !17 = !MDLocation(line: 4, scope: !18) |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame^] | 87 | !18 = !MDLexicalBlockFile(discriminator: 1, file: !1, scope: !15) |
Duncan P. N. Exon Smith | 9885469 | 2015-01-14 22:27:36 +0000 | [diff] [blame] | 88 | !19 = !MDLocation(line: 5, scope: !16) |
| 89 | !20 = !MDLocation(line: 6, scope: !16) |
| 90 | !21 = !MDLocation(line: 7, scope: !4) |