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 16 / 16 = 100% 8 9body: 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 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 64 / 68 31; CHECK: edge entry -> else probability is 4 / 68 32 33then: 34 br label %exit 35; CHECK: edge then -> exit probability is 16 / 16 = 100% 36 37else: 38 br label %exit 39; CHECK: edge else -> exit probability is 16 / 16 = 100% 40 41exit: 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 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 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 61case_a: 62 br label %exit 63; CHECK: edge case_a -> exit probability is 16 / 16 = 100% 64 65case_b: 66 br label %exit 67; CHECK: edge case_b -> exit probability is 16 / 16 = 100% 68 69case_c: 70 br label %exit 71; CHECK: edge case_c -> exit probability is 16 / 16 = 100% 72 73case_d: 74 br label %exit 75; CHECK: edge case_d -> exit probability is 16 / 16 = 100% 76 77case_e: 78 br label %exit 79; CHECK: edge case_e -> exit probability is 16 / 16 = 100% 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 = metadata !{metadata !"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 7 / 85 103; CHECK: edge entry -> sw.bb probability is 14 / 85 104; CHECK: edge entry -> sw.bb1 probability is 64 / 85 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 = metadata !{metadata !"branch_weights", i32 7, i32 6, i32 4, i32 4, i32 64} 118