Repository: llvm/llvm-project
File: clang/test/CIR/CodeGen/pred-info-builtins.c (C)

Code before:
// RUN: %clang_cc1 -O0 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o - | FileCheck %s --check-prefix=CIR-O0
// RUN: %clang_cc1 -O2 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o - | FileCheck %s --check-prefix=CIR-O2
// RUN: %clang_cc1 -O0 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o - | FileCheck %s --check-prefix=LLVM
// RUN: %clang_cc1 -O0 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o - | FileCheck %s --check-prefix=OGCG

extern void __attribute__((noinline)) bar(void);

void expect(int x) {
  if (__builtin_expect(x, 0))
    bar();
}
// CIR-O0: cir.func dso_local @expect
// CIR-O0:   cir.if {{%.*}} {
// CIR-O0:     cir.call @bar() : () -> ()

// CIR-O2: cir.func dso_local @expect
// CIR-O2:   [[EXPECT:%.*]] = cir.expect({{.*}}, {{.*}}) : !s64i
// CIR-O2:   [[EXPECT_BOOL:%.*]] = cir.cast int_to_bool [[EXPECT]] : !s64i -> !cir.bool
// CIR-O2:   cir.if [[EXPECT_BOOL]]
// CIR-O2:     cir.call @bar() : () -> ()

// LLVM-LABEL: @expect
// LLVM: br i1 {{.*}}, label %[[THEN:.*]], label %[[END:.*]]
// LLVM: [[THEN]]:
// LLVM: call void @bar()

// OGCG-LABEL: @expect
// OGCG: br i1 {{.*}}, label %[[THEN:.*]], label %[[END:.*]]
// OGCG: [[THEN]]:
// OGCG: call void @bar()

void expect_with_probability(int x) {
  if (__builtin_expect_with_probability(x, 1, 0.8))
    bar();
}
// CIR-O0: cir.func dso_local @expect_with_probability
// CIR-O0:   cir.if {{%.*}} {
// CIR-O0:     cir.call @bar() : () -> ()


Review comment (suggestion):
It looks like these were already implemented and are tested in `clang/test/CIR/CodeGenBuiltins/builtin_call.cpp`

Code after:
// Test __builtin_expect, __builtin_expect_with_probability, and __builtin_unpredictable.
// Focus: O0 vs O2 CIR output (no cir.expect at O0), and LLVM/OGCG with -O2 -disable-llvm-passes.
// Builtin call lowering is also covered by builtin_call.cpp.
//
// RUN: %clang_cc1 -O0 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o - | FileCheck %s --check-prefix=CIR-O0
// CIR-O0-NOT: cir.expect
// RUN: %clang_cc1 -O2 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o - | FileCheck %s --check-prefix=CIR-O2
// RUN: %clang_cc1 -O2 -disable-llvm-passes -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o - | FileCheck %s --check-prefix=LLVM
// RUN: %clang_cc1 -O2 -disable-llvm-passes -triple x86_64-unknown-linux-gnu -emit-llvm %s -o - | FileCheck %s --check-prefix=OGCG

extern void __attribute__((noinline)) bar(void);

void expect(int x) {
  if (__builtin_expect(x, 0))
    bar();
}
// CIR-O0: cir.func {{.*}} @expect
// CIR-O0:   cir.if {{%.*}} {
// CIR-O0:     cir.call @bar() : () -> ()

// CIR-O2: cir.func {{.*}} @expect
// CIR-O2:   [[EXPECT:%.*]] = cir.expect({{.*}}, {{.*}}) : !s64i
// CIR-O2:   [[EXPECT_BOOL:%.*]] = cir.cast int_to_bool [[EXPECT]] : !s64i -> !cir.bool
// CIR-O2:   cir.if [[EXPECT_BOOL]]
// CIR-O2:     cir.call @bar() : () -> ()

// LLVM-LABEL: @expect
// LLVM: br i1 {{.*}}, label %[[THEN:.*]], label %[[END:.*]]
// LLVM: [[THEN]]:
// LLVM: call void @bar()

// OGCG-LABEL: @expect
// OGCG: br i1 {{.*}}, label %[[THEN:.*]], label %[[END:.*]]
// OGCG: [[THEN]]:
// OGCG: call void @bar()

void expect_with_probability(int x) {
  if (__builtin_expect_with_probability(x, 1, 0.8))
    bar();