1 ; RUN: opt < %s -analyze -branch-prob | FileCheck %s 2 ; RUN: opt < %s -passes='print<branch-prob>' -disable-output 2>&1 | FileCheck %s 3 4 define i32 @test1(i32 %i, i32* %a) { 5 ; CHECK: Printing analysis {{.*}} for function 'test1' 6 entry: 7 br label %body 8 ; CHECK: edge entry -> body probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 9 10 body: 11 %iv = phi i32 [ 0, %entry ], [ %next, %body ] 12 %base = phi i32 [ 0, %entry ], [ %sum, %body ] 13 %arrayidx = getelementptr inbounds i32, i32* %a, i32 %iv 14 %0 = load i32, i32* %arrayidx 15 %sum = add nsw i32 %0, %base 16 %next = add i32 %iv, 1 17 %exitcond = icmp eq i32 %next, %i 18 br i1 %exitcond, label %exit, label %body 19 ; CHECK: edge body -> exit probability is 0x04000000 / 0x80000000 = 3.12% 20 ; CHECK: edge body -> body probability is 0x7c000000 / 0x80000000 = 96.88% [HOT edge] 21 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 entry: 29 %cond = icmp ult i32 %i, 42 30 br i1 %cond, label %then, label %else, !prof !0 31 ; CHECK: edge entry -> then probability is 0x78787878 / 0x80000000 = 94.12% [HOT edge] 32 ; CHECK: edge entry -> else probability is 0x07878788 / 0x80000000 = 5.88% 33 34 then: 35 br label %exit 36 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 37 38 else: 39 br label %exit 40 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 41 42 exit: 43 %result = phi i32 [ %a, %then ], [ %b, %else ] 44 ret i32 %result 45 } 46 47 !0 = !{!"branch_weights", i32 64, i32 4} 48 49 define i32 @test3(i32 %i, i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) { 50 ; CHECK: Printing analysis {{.*}} for function 'test3' 51 entry: 52 switch i32 %i, label %case_a [ i32 1, label %case_b 53 i32 2, label %case_c 54 i32 3, label %case_d 55 i32 4, label %case_e ], !prof !1 56 ; CHECK: edge entry -> case_a probability is 0x06666666 / 0x80000000 = 5.00% 57 ; CHECK: edge entry -> case_b probability is 0x06666666 / 0x80000000 = 5.00% 58 ; CHECK: edge entry -> case_c probability is 0x66666666 / 0x80000000 = 80.00% 59 ; CHECK: edge entry -> case_d probability is 0x06666666 / 0x80000000 = 5.00% 60 ; CHECK: edge entry -> case_e probability is 0x06666666 / 0x80000000 = 5.00% 61 62 case_a: 63 br label %exit 64 ; CHECK: edge case_a -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 65 66 case_b: 67 br label %exit 68 ; CHECK: edge case_b -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 69 70 case_c: 71 br label %exit 72 ; CHECK: edge case_c -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 73 74 case_d: 75 br label %exit 76 ; CHECK: edge case_d -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 77 78 case_e: 79 br label %exit 80 ; CHECK: edge case_e -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 81 82 exit: 83 %result = phi i32 [ %a, %case_a ], 84 [ %b, %case_b ], 85 [ %c, %case_c ], 86 [ %d, %case_d ], 87 [ %e, %case_e ] 88 ret i32 %result 89 } 90 91 !1 = !{!"branch_weights", i32 4, i32 4, i32 64, i32 4, i32 4} 92 93 define i32 @test4(i32 %x) nounwind uwtable readnone ssp { 94 ; CHECK: Printing analysis {{.*}} for function 'test4' 95 entry: 96 %conv = sext i32 %x to i64 97 switch i64 %conv, label %return [ 98 i64 0, label %sw.bb 99 i64 1, label %sw.bb 100 i64 2, label %sw.bb 101 i64 5, label %sw.bb1 102 ], !prof !2 103 ; CHECK: edge entry -> return probability is 0x0a8a8a8b / 0x80000000 = 8.24% 104 ; CHECK: edge entry -> sw.bb probability is 0x15151515 / 0x80000000 = 16.47% 105 ; CHECK: edge entry -> sw.bb1 probability is 0x60606060 / 0x80000000 = 75.29% 106 107 sw.bb: 108 br label %return 109 110 sw.bb1: 111 br label %return 112 113 return: 114 %retval.0 = phi i32 [ 5, %sw.bb1 ], [ 1, %sw.bb ], [ 0, %entry ] 115 ret i32 %retval.0 116 } 117 118 !2 = !{!"branch_weights", i32 7, i32 6, i32 4, i32 4, i32 64} 119 120 declare void @coldfunc() cold 121 122 define i32 @test5(i32 %a, i32 %b, i1 %flag) { 123 ; CHECK: Printing analysis {{.*}} for function 'test5' 124 entry: 125 br i1 %flag, label %then, label %else 126 ; CHECK: edge entry -> then probability is 0x07878788 / 0x80000000 = 5.88% 127 ; CHECK: edge entry -> else probability is 0x78787878 / 0x80000000 = 94.12% [HOT edge] 128 129 then: 130 call void @coldfunc() 131 br label %exit 132 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 133 134 else: 135 br label %exit 136 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 137 138 exit: 139 %result = phi i32 [ %a, %then ], [ %b, %else ] 140 ret i32 %result 141 } 142 143 declare i32 @regular_function(i32 %i) 144 145 define i32 @test_cold_call_sites(i32* %a) { 146 ; Test that edges to blocks post-dominated by cold call sites 147 ; are marked as not expected to be taken. 148 ; TODO(dnovillo) The calls to regular_function should not be merged, but 149 ; they are currently being merged. Convert this into a code generation test 150 ; after that is fixed. 151 152 ; CHECK: Printing analysis {{.*}} for function 'test_cold_call_sites' 153 ; CHECK: edge entry -> then probability is 0x07878788 / 0x80000000 = 5.88% 154 ; CHECK: edge entry -> else probability is 0x78787878 / 0x80000000 = 94.12% [HOT edge] 155 156 entry: 157 %gep1 = getelementptr i32, i32* %a, i32 1 158 %val1 = load i32, i32* %gep1 159 %cond1 = icmp ugt i32 %val1, 1 160 br i1 %cond1, label %then, label %else 161 162 then: 163 ; This function is not declared cold, but this call site is. 164 %val4 = call i32 @regular_function(i32 %val1) cold 165 br label %exit 166 167 else: 168 %gep2 = getelementptr i32, i32* %a, i32 2 169 %val2 = load i32, i32* %gep2 170 %val3 = call i32 @regular_function(i32 %val2) 171 br label %exit 172 173 exit: 174 %ret = phi i32 [ %val4, %then ], [ %val3, %else ] 175 ret i32 %ret 176 } 177 178 define i32 @zero1(i32 %i, i32 %a, i32 %b) { 179 ; CHECK: Printing analysis {{.*}} for function 'zero1' 180 entry: 181 %cond = icmp eq i32 %i, 0 182 br i1 %cond, label %then, label %else 183 ; CHECK: edge entry -> then probability is 0x30000000 / 0x80000000 = 37.50% 184 ; CHECK: edge entry -> else probability is 0x50000000 / 0x80000000 = 62.50% 185 186 then: 187 br label %exit 188 189 else: 190 br label %exit 191 192 exit: 193 %result = phi i32 [ %a, %then ], [ %b, %else ] 194 ret i32 %result 195 } 196 197 define i32 @zero2(i32 %i, i32 %a, i32 %b) { 198 ; CHECK: Printing analysis {{.*}} for function 'zero2' 199 entry: 200 %cond = icmp ne i32 %i, -1 201 br i1 %cond, label %then, label %else 202 ; CHECK: edge entry -> then probability is 0x50000000 / 0x80000000 = 62.50% 203 ; CHECK: edge entry -> else probability is 0x30000000 / 0x80000000 = 37.50% 204 205 then: 206 br label %exit 207 208 else: 209 br label %exit 210 211 exit: 212 %result = phi i32 [ %a, %then ], [ %b, %else ] 213 ret i32 %result 214 } 215 216 define i32 @zero3(i32 %i, i32 %a, i32 %b) { 217 ; CHECK: Printing analysis {{.*}} for function 'zero3' 218 entry: 219 ; AND'ing with a single bit bitmask essentially leads to a bool comparison, 220 ; meaning we don't have probability information. 221 %and = and i32 %i, 2 222 %tobool = icmp eq i32 %and, 0 223 br i1 %tobool, label %then, label %else 224 ; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00% 225 ; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00% 226 227 then: 228 ; AND'ing with other bitmask might be something else, so we still assume the 229 ; usual probabilities. 230 %and2 = and i32 %i, 5 231 %tobool2 = icmp eq i32 %and2, 0 232 br i1 %tobool2, label %else, label %exit 233 ; CHECK: edge then -> else probability is 0x30000000 / 0x80000000 = 37.50% 234 ; CHECK: edge then -> exit probability is 0x50000000 / 0x80000000 = 62.50% 235 236 else: 237 br label %exit 238 239 exit: 240 %result = phi i32 [ %a, %then ], [ %b, %else ] 241 ret i32 %result 242 } 243 244