1; RUN: opt < %s -analyze -branch-prob | FileCheck %s 2 3define i32 @test1(i32 %i, i32* %a) { 4; CHECK: Printing analysis {{.*}} for function 'test1' 5entry: 6 br label %body 7; CHECK: edge entry -> body probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 8 9body: 10 %iv = phi i32 [ 0, %entry ], [ %next, %body ] 11 %base = phi i32 [ 0, %entry ], [ %sum, %body ] 12 %arrayidx = getelementptr inbounds i32, i32* %a, i32 %iv 13 %0 = load i32, 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 0x04000000 / 0x80000000 = 3.12% 19; CHECK: edge body -> body probability is 0x7c000000 / 0x80000000 = 96.88% [HOT edge] 20 21exit: 22 ret i32 %sum 23} 24 25define i32 @test2(i32 %i, i32 %a, i32 %b) { 26; CHECK: Printing analysis {{.*}} for function 'test2' 27entry: 28 %cond = icmp ult i32 %i, 42 29 br i1 %cond, label %then, label %else, !prof !0 30; CHECK: edge entry -> then probability is 0x78787878 / 0x80000000 = 94.12% [HOT edge] 31; CHECK: edge entry -> else probability is 0x07878788 / 0x80000000 = 5.88% 32 33then: 34 br label %exit 35; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 36 37else: 38 br label %exit 39; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 40 41exit: 42 %result = phi i32 [ %a, %then ], [ %b, %else ] 43 ret i32 %result 44} 45 46!0 = !{!"branch_weights", i32 64, i32 4} 47 48define i32 @test3(i32 %i, i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) { 49; CHECK: Printing analysis {{.*}} for function 'test3' 50entry: 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 0x06666666 / 0x80000000 = 5.00% 56; CHECK: edge entry -> case_b probability is 0x06666666 / 0x80000000 = 5.00% 57; CHECK: edge entry -> case_c probability is 0x66666666 / 0x80000000 = 80.00% 58; CHECK: edge entry -> case_d probability is 0x06666666 / 0x80000000 = 5.00% 59; CHECK: edge entry -> case_e probability is 0x06666666 / 0x80000000 = 5.00% 60 61case_a: 62 br label %exit 63; CHECK: edge case_a -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 64 65case_b: 66 br label %exit 67; CHECK: edge case_b -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 68 69case_c: 70 br label %exit 71; CHECK: edge case_c -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 72 73case_d: 74 br label %exit 75; CHECK: edge case_d -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 76 77case_e: 78 br label %exit 79; CHECK: edge case_e -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 80 81exit: 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 = !{!"branch_weights", i32 4, i32 4, i32 64, i32 4, i32 4} 91 92define i32 @test4(i32 %x) nounwind uwtable readnone ssp { 93; CHECK: Printing analysis {{.*}} for function 'test4' 94entry: 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 0x0a8a8a8b / 0x80000000 = 8.24% 103; CHECK: edge entry -> sw.bb probability is 0x15151515 / 0x80000000 = 16.47% 104; CHECK: edge entry -> sw.bb1 probability is 0x60606060 / 0x80000000 = 75.29% 105 106sw.bb: 107 br label %return 108 109sw.bb1: 110 br label %return 111 112return: 113 %retval.0 = phi i32 [ 5, %sw.bb1 ], [ 1, %sw.bb ], [ 0, %entry ] 114 ret i32 %retval.0 115} 116 117!2 = !{!"branch_weights", i32 7, i32 6, i32 4, i32 4, i32 64} 118 119declare void @coldfunc() cold 120 121define i32 @test5(i32 %a, i32 %b, i1 %flag) { 122; CHECK: Printing analysis {{.*}} for function 'test5' 123entry: 124 br i1 %flag, label %then, label %else 125; CHECK: edge entry -> then probability is 0x07878788 / 0x80000000 = 5.88% 126; CHECK: edge entry -> else probability is 0x78787878 / 0x80000000 = 94.12% [HOT edge] 127 128then: 129 call void @coldfunc() 130 br label %exit 131; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 132 133else: 134 br label %exit 135; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 136 137exit: 138 %result = phi i32 [ %a, %then ], [ %b, %else ] 139 ret i32 %result 140} 141 142declare i32 @regular_function(i32 %i) 143 144define 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 0x07878788 / 0x80000000 = 5.88% 153; CHECK: edge entry -> else probability is 0x78787878 / 0x80000000 = 94.12% [HOT edge] 154 155entry: 156 %gep1 = getelementptr i32, i32* %a, i32 1 157 %val1 = load i32, i32* %gep1 158 %cond1 = icmp ugt i32 %val1, 1 159 br i1 %cond1, label %then, label %else 160 161then: 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 166else: 167 %gep2 = getelementptr i32, i32* %a, i32 2 168 %val2 = load i32, i32* %gep2 169 %val3 = call i32 @regular_function(i32 %val2) 170 br label %exit 171 172exit: 173 %ret = phi i32 [ %val4, %then ], [ %val3, %else ] 174 ret i32 %ret 175} 176 177define i32 @zero1(i32 %i, i32 %a, i32 %b) { 178; CHECK: Printing analysis {{.*}} for function 'zero1' 179entry: 180 %cond = icmp eq i32 %i, 0 181 br i1 %cond, label %then, label %else 182; CHECK: edge entry -> then probability is 0x30000000 / 0x80000000 = 37.50% 183; CHECK: edge entry -> else probability is 0x50000000 / 0x80000000 = 62.50% 184 185then: 186 br label %exit 187 188else: 189 br label %exit 190 191exit: 192 %result = phi i32 [ %a, %then ], [ %b, %else ] 193 ret i32 %result 194} 195 196define i32 @zero2(i32 %i, i32 %a, i32 %b) { 197; CHECK: Printing analysis {{.*}} for function 'zero2' 198entry: 199 %cond = icmp ne i32 %i, -1 200 br i1 %cond, label %then, label %else 201; CHECK: edge entry -> then probability is 0x50000000 / 0x80000000 = 62.50% 202; CHECK: edge entry -> else probability is 0x30000000 / 0x80000000 = 37.50% 203 204then: 205 br label %exit 206 207else: 208 br label %exit 209 210exit: 211 %result = phi i32 [ %a, %then ], [ %b, %else ] 212 ret i32 %result 213} 214 215define i32 @zero3(i32 %i, i32 %a, i32 %b) { 216; CHECK: Printing analysis {{.*}} for function 'zero3' 217entry: 218; AND'ing with a single bit bitmask essentially leads to a bool comparison, 219; meaning we don't have probability information. 220 %and = and i32 %i, 2 221 %tobool = icmp eq i32 %and, 0 222 br i1 %tobool, label %then, label %else 223; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00% 224; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00% 225 226then: 227; AND'ing with other bitmask might be something else, so we still assume the 228; usual probabilities. 229 %and2 = and i32 %i, 5 230 %tobool2 = icmp eq i32 %and2, 0 231 br i1 %tobool2, label %else, label %exit 232; CHECK: edge then -> else probability is 0x30000000 / 0x80000000 = 37.50% 233; CHECK: edge then -> exit probability is 0x50000000 / 0x80000000 = 62.50% 234 235else: 236 br label %exit 237 238exit: 239 %result = phi i32 [ %a, %then ], [ %b, %else ] 240 ret i32 %result 241} 242 243