1 ; RUN: opt < %s -analyze -block-freq | FileCheck %s 2 3 define i32 @test1(i32 %i, i32* %a) { 4 ; CHECK: Printing analysis {{.*}} for function 'test1' 5 ; CHECK: entry = 1.0 6 entry: 7 br label %body 8 9 ; Loop backedges are weighted and thus their bodies have a greater frequency. 10 ; CHECK: body = 32.0 11 body: 12 %iv = phi i32 [ 0, %entry ], [ %next, %body ] 13 %base = phi i32 [ 0, %entry ], [ %sum, %body ] 14 %arrayidx = getelementptr inbounds i32* %a, i32 %iv 15 %0 = load i32* %arrayidx 16 %sum = add nsw i32 %0, %base 17 %next = add i32 %iv, 1 18 %exitcond = icmp eq i32 %next, %i 19 br i1 %exitcond, label %exit, label %body 20 21 ; CHECK: exit = 1.0 22 exit: 23 ret i32 %sum 24 } 25 26 define i32 @test2(i32 %i, i32 %a, i32 %b) { 27 ; CHECK: Printing analysis {{.*}} for function 'test2' 28 ; CHECK: entry = 1.0 29 entry: 30 %cond = icmp ult i32 %i, 42 31 br i1 %cond, label %then, label %else, !prof !0 32 33 ; The 'then' branch is predicted more likely via branch weight metadata. 34 ; CHECK: then = 0.94116 35 then: 36 br label %exit 37 38 ; CHECK: else = 0.05877 39 else: 40 br label %exit 41 42 ; FIXME: It may be a bug that we don't sum back to 1.0. 43 ; CHECK: exit = 0.99993 44 exit: 45 %result = phi i32 [ %a, %then ], [ %b, %else ] 46 ret i32 %result 47 } 48 49 !0 = metadata !{metadata !"branch_weights", i32 64, i32 4} 50 51 define i32 @test3(i32 %i, i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) { 52 ; CHECK: Printing analysis {{.*}} for function 'test3' 53 ; CHECK: entry = 1.0 54 entry: 55 switch i32 %i, label %case_a [ i32 1, label %case_b 56 i32 2, label %case_c 57 i32 3, label %case_d 58 i32 4, label %case_e ], !prof !1 59 60 ; CHECK: case_a = 0.04998 61 case_a: 62 br label %exit 63 64 ; CHECK: case_b = 0.04998 65 case_b: 66 br label %exit 67 68 ; The 'case_c' branch is predicted more likely via branch weight metadata. 69 ; CHECK: case_c = 0.79998 70 case_c: 71 br label %exit 72 73 ; CHECK: case_d = 0.04998 74 case_d: 75 br label %exit 76 77 ; CHECK: case_e = 0.04998 78 case_e: 79 br label %exit 80 81 ; FIXME: It may be a bug that we don't sum back to 1.0. 82 ; CHECK: exit = 0.99993 83 exit: 84 %result = phi i32 [ %a, %case_a ], 85 [ %b, %case_b ], 86 [ %c, %case_c ], 87 [ %d, %case_d ], 88 [ %e, %case_e ] 89 ret i32 %result 90 } 91 92 !1 = metadata !{metadata !"branch_weights", i32 4, i32 4, i32 64, i32 4, i32 4} 93 94 ; CHECK: Printing analysis {{.*}} for function 'nested_loops' 95 ; CHECK: entry = 1.0 96 ; This test doesn't seem to be assigning sensible frequencies to nested loops. 97 define void @nested_loops(i32 %a) { 98 entry: 99 br label %for.cond1.preheader 100 101 for.cond1.preheader: 102 %x.024 = phi i32 [ 0, %entry ], [ %inc12, %for.inc11 ] 103 br label %for.cond4.preheader 104 105 for.cond4.preheader: 106 %y.023 = phi i32 [ 0, %for.cond1.preheader ], [ %inc9, %for.inc8 ] 107 %add = add i32 %y.023, %x.024 108 br label %for.body6 109 110 for.body6: 111 %z.022 = phi i32 [ 0, %for.cond4.preheader ], [ %inc, %for.body6 ] 112 %add7 = add i32 %add, %z.022 113 tail call void @g(i32 %add7) #2 114 %inc = add i32 %z.022, 1 115 %cmp5 = icmp ugt i32 %inc, %a 116 br i1 %cmp5, label %for.inc8, label %for.body6, !prof !2 117 118 for.inc8: 119 %inc9 = add i32 %y.023, 1 120 %cmp2 = icmp ugt i32 %inc9, %a 121 br i1 %cmp2, label %for.inc11, label %for.cond4.preheader, !prof !2 122 123 for.inc11: 124 %inc12 = add i32 %x.024, 1 125 %cmp = icmp ugt i32 %inc12, %a 126 br i1 %cmp, label %for.end13, label %for.cond1.preheader, !prof !2 127 128 for.end13: 129 ret void 130 } 131 132 declare void @g(i32) #1 133 134 !2 = metadata !{metadata !"branch_weights", i32 1, i32 4000} 135