1 ; RUN: opt < %s -analyze -branch-prob | FileCheck %s 2 3 define i32 @test1(i32 %i, i32* %a) { 4 ; CHECK: Printing analysis {{.*}} for function 'test1' 5 entry: 6 br label %body 7 ; CHECK: edge entry -> body probability is 16 / 16 = 100% 8 9 body: 10 %iv = phi i32 [ 0, %entry ], [ %next, %body ] 11 %base = phi i32 [ 0, %entry ], [ %sum, %body ] 12 %arrayidx = getelementptr inbounds i32* %a, i32 %iv 13 %0 = load i32* %arrayidx 14 %sum = add nsw i32 %0, %base 15 %next = add i32 %iv, 1 16 %exitcond = icmp eq i32 %next, %i 17 br i1 %exitcond, label %exit, label %body 18 ; CHECK: edge body -> exit probability is 4 / 128 19 ; CHECK: edge body -> body probability is 124 / 128 20 21 exit: 22 ret i32 %sum 23 } 24 25 define i32 @test2(i32 %i, i32 %a, i32 %b) { 26 ; CHECK: Printing analysis {{.*}} for function 'test2' 27 entry: 28 %cond = icmp ult i32 %i, 42 29 br i1 %cond, label %then, label %else, !prof !0 30 ; CHECK: edge entry -> then probability is 64 / 68 31 ; CHECK: edge entry -> else probability is 4 / 68 32 33 then: 34 br label %exit 35 ; CHECK: edge then -> exit probability is 16 / 16 = 100% 36 37 else: 38 br label %exit 39 ; CHECK: edge else -> exit probability is 16 / 16 = 100% 40 41 exit: 42 %result = phi i32 [ %a, %then ], [ %b, %else ] 43 ret i32 %result 44 } 45 46 !0 = metadata !{metadata !"branch_weights", i32 64, i32 4} 47 48 define i32 @test3(i32 %i, i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) { 49 ; CHECK: Printing analysis {{.*}} for function 'test3' 50 entry: 51 switch i32 %i, label %case_a [ i32 1, label %case_b 52 i32 2, label %case_c 53 i32 3, label %case_d 54 i32 4, label %case_e ], !prof !1 55 ; CHECK: edge entry -> case_a probability is 4 / 80 56 ; CHECK: edge entry -> case_b probability is 4 / 80 57 ; CHECK: edge entry -> case_c probability is 64 / 80 58 ; CHECK: edge entry -> case_d probability is 4 / 80 59 ; CHECK: edge entry -> case_e probability is 4 / 80 60 61 case_a: 62 br label %exit 63 ; CHECK: edge case_a -> exit probability is 16 / 16 = 100% 64 65 case_b: 66 br label %exit 67 ; CHECK: edge case_b -> exit probability is 16 / 16 = 100% 68 69 case_c: 70 br label %exit 71 ; CHECK: edge case_c -> exit probability is 16 / 16 = 100% 72 73 case_d: 74 br label %exit 75 ; CHECK: edge case_d -> exit probability is 16 / 16 = 100% 76 77 case_e: 78 br label %exit 79 ; CHECK: edge case_e -> exit probability is 16 / 16 = 100% 80 81 exit: 82 %result = phi i32 [ %a, %case_a ], 83 [ %b, %case_b ], 84 [ %c, %case_c ], 85 [ %d, %case_d ], 86 [ %e, %case_e ] 87 ret i32 %result 88 } 89 90 !1 = metadata !{metadata !"branch_weights", i32 4, i32 4, i32 64, i32 4, i32 4} 91 92 define i32 @test4(i32 %x) nounwind uwtable readnone ssp { 93 ; CHECK: Printing analysis {{.*}} for function 'test4' 94 entry: 95 %conv = sext i32 %x to i64 96 switch i64 %conv, label %return [ 97 i64 0, label %sw.bb 98 i64 1, label %sw.bb 99 i64 2, label %sw.bb 100 i64 5, label %sw.bb1 101 ], !prof !2 102 ; CHECK: edge entry -> return probability is 7 / 85 103 ; CHECK: edge entry -> sw.bb probability is 14 / 85 104 ; CHECK: edge entry -> sw.bb1 probability is 64 / 85 105 106 sw.bb: 107 br label %return 108 109 sw.bb1: 110 br label %return 111 112 return: 113 %retval.0 = phi i32 [ 5, %sw.bb1 ], [ 1, %sw.bb ], [ 0, %entry ] 114 ret i32 %retval.0 115 } 116 117 !2 = metadata !{metadata !"branch_weights", i32 7, i32 6, i32 4, i32 4, i32 64} 118 119 declare void @coldfunc() cold 120 121 define i32 @test5(i32 %a, i32 %b, i1 %flag) { 122 ; CHECK: Printing analysis {{.*}} for function 'test5' 123 entry: 124 br i1 %flag, label %then, label %else 125 ; CHECK: edge entry -> then probability is 4 / 68 126 ; CHECK: edge entry -> else probability is 64 / 68 127 128 then: 129 call void @coldfunc() 130 br label %exit 131 ; CHECK: edge then -> exit probability is 16 / 16 = 100% 132 133 else: 134 br label %exit 135 ; CHECK: edge else -> exit probability is 16 / 16 = 100% 136 137 exit: 138 %result = phi i32 [ %a, %then ], [ %b, %else ] 139 ret i32 %result 140 } 141 142 declare i32 @regular_function(i32 %i) 143 144 define i32 @test_cold_call_sites(i32* %a) { 145 ; Test that edges to blocks post-dominated by cold call sites 146 ; are marked as not expected to be taken. 147 ; TODO(dnovillo) The calls to regular_function should not be merged, but 148 ; they are currently being merged. Convert this into a code generation test 149 ; after that is fixed. 150 151 ; CHECK: Printing analysis {{.*}} for function 'test_cold_call_sites' 152 ; CHECK: edge entry -> then probability is 4 / 68 = 5.88235% 153 ; CHECK: edge entry -> else probability is 64 / 68 = 94.1176% [HOT edge] 154 155 entry: 156 %gep1 = getelementptr i32* %a, i32 1 157 %val1 = load i32* %gep1 158 %cond1 = icmp ugt i32 %val1, 1 159 br i1 %cond1, label %then, label %else 160 161 then: 162 ; This function is not declared cold, but this call site is. 163 %val4 = call i32 @regular_function(i32 %val1) cold 164 br label %exit 165 166 else: 167 %gep2 = getelementptr i32* %a, i32 2 168 %val2 = load i32* %gep2 169 %val3 = call i32 @regular_function(i32 %val2) 170 br label %exit 171 172 exit: 173 %ret = phi i32 [ %val4, %then ], [ %val3, %else ] 174 ret i32 %ret 175 } 176