156 lines · c
1// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -disable-llvm-optzns -o - %s -O2 | FileCheck %s2// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -disable-llvm-optzns -o - %s -O0 | FileCheck %s3// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -disable-llvm-optzns -o - %s -O2 -fexperimental-new-constant-interpreter | FileCheck %s4// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -disable-llvm-optzns -o - %s -O0 -fexperimental-new-constant-interpreter | FileCheck %s5 6 7int a = 42;8 9/* --- Compound literals */10 11struct foo { int x, y; };12 13int y;14struct foo f = (struct foo){ __builtin_constant_p(y), 42 };15 16struct foo test0(int expr) {17 // CHECK-LABEL: test018 // CHECK: call i1 @llvm.is.constant.i3219 struct foo f = (struct foo){ __builtin_constant_p(expr), 42 };20 return f;21}22 23/* --- Pointer types */24 25int test1(void) {26 // CHECK-LABEL: test127 // CHECK: ret i32 028 return __builtin_constant_p(&a - 13);29}30 31/* --- Aggregate types */32 33int b[] = {1, 2, 3};34 35int test2(void) {36 // CHECK-LABEL: test237 // CHECK: ret i32 038 return __builtin_constant_p(b);39}40 41const char test3_c[] = {1, 2, 3, 0};42 43int test3(void) {44 // CHECK-LABEL: test345 // CHECK: ret i32 046 return __builtin_constant_p(test3_c);47}48 49inline char test4_i(const char *x) {50 return x[1];51}52 53int test4(void) {54 // CHECK: define{{.*}} i32 @test455 // CHECK: ret i32 056 return __builtin_constant_p(test4_i(test3_c));57}58 59/* --- Constant global variables */60 61const int c = 42;62 63int test5(void) {64 // CHECK-LABEL: test565 // CHECK: ret i32 166 return __builtin_constant_p(c);67}68 69/* --- Array types */70 71int arr[] = { 1, 2, 3 };72const int c_arr[] = { 1, 2, 3 };73 74int test6(void) {75 // CHECK-LABEL: test676 // CHECK: call i1 @llvm.is.constant.i3277 return __builtin_constant_p(arr[2]);78}79 80int test7(void) {81 // CHECK-LABEL: test782 // CHECK: ret i32 183 return __builtin_constant_p(c_arr[2]);84}85 86int test8(void) {87 // CHECK-LABEL: test888 // CHECK: ret i32 089 return __builtin_constant_p(c_arr);90}91 92/* --- Function pointers */93 94int test9(void) {95 // CHECK-LABEL: test996 // CHECK: ret i32 097 return __builtin_constant_p(&test9);98}99 100int test10(void) {101 // CHECK-LABEL: test10102 // CHECK: ret i32 1103 return __builtin_constant_p(&test10 != 0);104}105 106typedef unsigned long uintptr_t;107#define assign(p, v) ({ \108 uintptr_t _r_a_p__v = (uintptr_t)(v); \109 if (__builtin_constant_p(v) && _r_a_p__v == (uintptr_t)0) { \110 union { \111 uintptr_t __val; \112 char __c[1]; \113 } __u = { \114 .__val = (uintptr_t)_r_a_p__v \115 }; \116 *(volatile unsigned int*)&p = *(unsigned int*)(__u.__c); \117 __u.__val; \118 } \119 _r_a_p__v; \120})121 122typedef void fn_p(void);123extern fn_p *dest_p;124 125static void src_fn(void) {126}127 128void test11(void) {129 assign(dest_p, src_fn);130}131 132extern int test12_v;133 134struct { const char *t; int a; } test12[] = {135 { "tag", __builtin_constant_p(test12_v) && !test12_v ? 1 : 0 }136};137 138extern char test13_v;139struct { int a; } test13 = { __builtin_constant_p(test13_v) };140 141extern unsigned long long test14_v;142 143void test14(void) {144 // CHECK-LABEL: test14145 // CHECK: call void asm sideeffect "", {{.*}}(i32 -1) 146 __asm__ __volatile__("" :: "n"( (__builtin_constant_p(test14_v) || 0) ? 1 : -1));147}148 149int test15_f(void);150// CHECK-LABEL: define{{.*}} void @test15151// CHECK-NOT: call {{.*}}test15_f152void test15(void) {153 int a, b;154 (void)__builtin_constant_p((a = b, test15_f()));155}156