1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm %s -std=c++2a -o %t.ll
// RUN: FileCheck -check-prefix=CHECK-FN-CG -input-file=%t.ll %s
// RUN: FileCheck -check-prefix=CHECK-STATIC -input-file=%t.ll %s
// RUN: FileCheck -check-prefix=CHECK-DYN -input-file=%t.ll %s
// RUN: FileCheck -check-prefix=CHECK-ARR -input-file=%t.ll %s
// RUN: FileCheck -check-prefix=CHECK-FOLD -input-file=%t.ll %s
using size_t = decltype(sizeof(int));
#define CONSTINIT __attribute__((require_constant_initialization))
extern "C" [[noreturn]] void BOOM();
extern "C" void OK();
extern "C" size_t RANDU();
namespace std {
inline constexpr bool is_constant_evaluated() noexcept {
return __builtin_is_constant_evaluated();
}
} // namespace std
// CHECK-FN-CG-LABEL: define zeroext i1 @_Z3foov()
// CHECK-FN-CG: ret i1 false
bool foo() {
return __builtin_is_constant_evaluated();
}
// CHECK-FN-CG-LABEL: define linkonce_odr i32 @_Z1fv()
constexpr int f() {
// CHECK-FN-CG: store i32 13, i32* %n, align 4
// CHECK-FN-CG: store i32 17, i32* %m, align 4
// CHECK-FN-CG: %1 = load i32, i32* %m, align 4
// CHECK-FN-CG: %add = add nsw i32 %1, 13
// CHECK-FN-CG: ret i32 %add
const int n = __builtin_is_constant_evaluated() && std::is_constant_evaluated() ? 13 : 17; // n == 13
int m = __builtin_is_constant_evaluated() ? 13 : 17; // m might be 13 or 17 (see below)
char arr[n] = {}; // char[13]
return m + int(sizeof(arr));
}
// CHECK-STATIC-DAG: @p = global i32 26,
CONSTINIT int p = f(); // f().m == 13; initialized to 26
// CHECK-STATIC-DAG: @p2 = global i32 26,
int p2 = f(); // same result without CONSTINIT
// CHECK-DYN-LABEL: define internal void @__cxx_global_var_init()
// CHECK-DYN: %0 = load i32, i32* @p, align 4
// CHECK-DYN-NEXT: %call = call i32 @_Z1fv()
// CHECK-DYN-NEXT: %add = add nsw i32 %0, %call
// CHECK-DYN-NEXT: store i32 %add, i32* @q, align 4
// CHECK-DYN-NEXT: ret void
int q = p + f(); // m == 17 for this call; initialized to 56
int y;
// CHECK-STATIC-DAG: @b = global i32 2,
CONSTINIT int b = __builtin_is_constant_evaluated() ? 2 : y; // static initialization to 2
// CHECK-DYN-LABEL: define internal void @__cxx_global_var_init.1()
// CHECK-DYN: %0 = load i32, i32* @y, align 4
// CHECK-DYN: %1 = load i32, i32* @y, align 4
// CHECK-DYN-NEXT: %add = add
// CHECK-DYN-NEXT: store i32 %add, i32* @c,
int c = y + (__builtin_is_constant_evaluated() ? 2 : y); // dynamic initialization to y+y
// CHECK-DYN-LABEL: define internal void @__cxx_global_var_init.2()
// CHECK-DYN: store i32 1, i32* @_ZL1a, align 4
// CHECK-DYN-NEXT: ret void
const int a = __builtin_is_constant_evaluated() ? y : 1; // dynamic initialization to 1
const int *a_sink = &a;
// CHECK-ARR-LABEL: define void @_Z13test_arr_exprv
void test_arr_expr() {
// CHECK-ARR: %x1 = alloca [101 x i8],
char x1[std::is_constant_evaluated() && __builtin_is_constant_evaluated() ? 101 : 1];
// CHECK-ARR: %x2 = alloca [42 x i8],
char x2[std::is_constant_evaluated() && __builtin_is_constant_evaluated() ? 42 : RANDU()];
// CHECK-ARR: call i8* @llvm.stacksave()
// CHECK-ARR: %vla = alloca i8, i64 13,
char x3[std::is_constant_evaluated() || __builtin_is_constant_evaluated() ? RANDU() : 13];
}
// CHECK-ARR-LABEL: define void @_Z17test_new_arr_exprv
void test_new_arr_expr() {
// CHECK-ARR: call i8* @_Znam(i64 17)
new char[std::is_constant_evaluated() || __builtin_is_constant_evaluated() ? 1 : 17];
}
// CHECK-FOLD-LABEL: @_Z31test_constant_initialized_locali(
bool test_constant_initialized_local(int k) {
// CHECK-FOLD: store i8 1, i8* %n,
// CHECK-FOLD: store volatile i8* %n, i8** %p,
const bool n = __builtin_is_constant_evaluated() && std::is_constant_evaluated();
const bool *volatile p = &n;
return *p;
}
// CHECK-FOLD-LABEL: define void @_Z21test_ir_constant_foldv()
void test_ir_constant_fold() {
// CHECK-FOLD-NEXT: entry:
// CHECK-FOLD-NEXT: call void @OK()
// CHECK-FOLD-NEXT: call void @OK()
// CHECK-FOLD-NEXT: ret void
if (std::is_constant_evaluated()) {
BOOM();
} else {
OK();
}
std::is_constant_evaluated() ? BOOM() : OK();
}
// CHECK-STATIC-DAG: @ir = constant i32* @i_constant,
int i_constant;
int i_not_constant;
int &ir = __builtin_is_constant_evaluated() ? i_constant : i_not_constant;
// CHECK-FOLD-LABEL: @_Z35test_ref_initialization_local_scopev()
void test_ref_initialization_local_scope() {
const int i_constant = 42;
const int i_non_constant = 101;
// CHECK-FOLD: store i32* %i_non_constant, i32** %r,
const int &r = __builtin_is_constant_evaluated() ? i_constant : i_non_constant;
}
// CHECK-FOLD-LABEL: @_Z22test_ref_to_static_varv()
void test_ref_to_static_var() {
static int i_constant = 42;
static int i_non_constant = 101;
// CHECK-FOLD: store i32* @_ZZ22test_ref_to_static_varvE10i_constant, i32** %r,
int &r = __builtin_is_constant_evaluated() ? i_constant : i_non_constant;
}
|