102 lines · cpp
1// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -o - %s -O1 -disable-llvm-passes | FileCheck %s --check-prefix=ALL --check-prefix=O12// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -o - %s -O0 | FileCheck %s --check-prefix=ALL --check-prefix=O03extern int global;4 5int expect_taken(int x) {6// ALL-LABEL: expect_taken7// O1: call i64 @llvm.expect.with.probability.i64(i64 {{%.*}}, i64 1, double 9.000000e-01)8// O0-NOT: @llvm.expect.with.probability9 10 if (__builtin_expect_with_probability(x == 100, 1, 0.9)) {11 return 0;12 }13 return x;14}15 16int expect_not_taken(int x) {17// ALL-LABEL: expect_not_taken18// O1: call i64 @llvm.expect.with.probability.i64(i64 {{%.*}}, i64 0, double 9.000000e-01)19// O0-NOT: @llvm.expect.with.probability20 21 if (__builtin_expect_with_probability(x == 100, 0, 0.9)) {22 return 0;23 }24 return x;25}26 27struct S {28 static constexpr int prob = 1;29};30 31template<typename T>32int expect_taken_template(int x) {33// ALL-LABEL: expect_taken_template34// O1: call i64 @llvm.expect.with.probability.i64(i64 {{%.*}}, i64 1, double 1.000000e+00)35// O0-NOT: @llvm.expect.with.probability36 37 if (__builtin_expect_with_probability (x == 100, 1, T::prob)) {38 return 0;39 }40 return x;41}42 43int f() {44 return expect_taken_template<S>(global);45}46 47int x;48extern "C" {49 int y(void);50}51void foo();52 53void expect_value_side_effects() {54// ALL-LABEL: expect_value_side_effects55// ALL: [[CALL:%.*]] = call i32 @y56// O1: [[SEXT:%.*]] = sext i32 [[CALL]] to i6457// O1: call i64 @llvm.expect.with.probability.i64(i64 {{%.*}}, i64 [[SEXT]], double 6.000000e-01)58// O0-NOT: @llvm.expect.with.probability59 60 if (__builtin_expect_with_probability(x, y(), 0.6))61 foo();62}63 64int switch_cond(int x) {65// ALL-LABEL: switch_cond66// O1: call i64 @llvm.expect.with.probability.i64(i64 {{%.*}}, i64 1, double 8.000000e-01)67// O0-NOT: @llvm.expect.with.probability68 69 switch (__builtin_expect_with_probability(x, 1, 0.8)) {70 case 0:71 x = x + 0;72 case 1:73 x = x + 1;74 case 2:75 x = x + 2;76 case 5:77 x = x + 5;78 default:79 x = x + 6;80 }81 return x;82}83 84constexpr double prob = 0.8;85 86int variable_expected(int stuff) {87// ALL-LABEL: variable_expected88// O1: call i64 @llvm.expect.with.probability.i64(i64 {{%.*}}, i64 {{%.*}}, double 8.000000e-01)89// O0-NOT: @llvm.expect.with.probability90 91 int res = 0;92 93 switch(__builtin_expect_with_probability(stuff, stuff, prob)) {94 case 0:95 res = 1;96 break;97 default:98 break;99 }100 return res;101}102