Debian Codegraph @

clang/test/CodeGen/builtin-constant-p.c

raw
1 // RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -disable-llvm-optzns -o - %s -O2 | FileCheck %s
2 // RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -disable-llvm-optzns -o - %s -O0 | FileCheck %s
3 // RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -disable-llvm-optzns -o - %s -O2 -fexperimental-new-constant-interpreter | FileCheck %s
4 // RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -disable-llvm-optzns -o - %s -O0 -fexperimental-new-constant-interpreter | FileCheck %s
5
6
7 int a = 42;
8
9 /* --- Compound literals */
10
11 struct foo { int x, y; };
12
13 int y;
14 struct foo f = (struct foo){ __builtin_constant_p(y), 42 };
15
16 struct foo test0(int expr) {
17 // CHECK-LABEL: test0
18 // CHECK: call i1 @llvm.is.constant.i32
19 struct foo f = (struct foo){ __builtin_constant_p(expr), 42 };
20 return f;
21 }
22
23 /* --- Pointer types */
24
25 int test1(void) {
26 // CHECK-LABEL: test1
27 // CHECK: ret i32 0
28 return __builtin_constant_p(&a - 13);
29 }
30
31 /* --- Aggregate types */
32
33 int b[] = {1, 2, 3};
34
35 int test2(void) {
36 // CHECK-LABEL: test2
37 // CHECK: ret i32 0
38 return __builtin_constant_p(b);
39 }
40
41 const char test3_c[] = {1, 2, 3, 0};
42
43 int test3(void) {
44 // CHECK-LABEL: test3
45 // CHECK: ret i32 0
46 return __builtin_constant_p(test3_c);
47 }
48
49 inline char test4_i(const char *x) {
50 return x[1];
51 }
52
53 int test4(void) {
54 // CHECK: define{{.*}} i32 @test4
55 // CHECK: ret i32 0
56 return __builtin_constant_p(test4_i(test3_c));
57 }
58
59 /* --- Constant global variables */
60
61 const int c = 42;
62
63 int test5(void) {
64 // CHECK-LABEL: test5
65 // CHECK: ret i32 1
66 return __builtin_constant_p(c);
67 }
68
69 /* --- Array types */
70
71 int arr[] = { 1, 2, 3 };
72 const int c_arr[] = { 1, 2, 3 };
73
74 int test6(void) {
75 // CHECK-LABEL: test6
76 // CHECK: call i1 @llvm.is.constant.i32
77 return __builtin_constant_p(arr[2]);
78 }
79
80 int test7(void) {
81 // CHECK-LABEL: test7
82 // CHECK: ret i32 1
83 return __builtin_constant_p(c_arr[2]);
84 }
85
86 int test8(void) {
87 // CHECK-LABEL: test8
88 // CHECK: ret i32 0
89 return __builtin_constant_p(c_arr);
90 }
91
92 /* --- Function pointers */
93
94 int test9(void) {
95 // CHECK-LABEL: test9
96 // CHECK: ret i32 0
97 return __builtin_constant_p(&test9);
98 }
99
100 int test10(void) {
101 // CHECK-LABEL: test10
102 // CHECK: ret i32 1
103 return __builtin_constant_p(&test10 != 0);
104 }
105
106 typedef 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
122 typedef void fn_p(void);
123 extern fn_p *dest_p;
124
125 static void src_fn(void) {
126 }
127
128 void test11(void) {
129 assign(dest_p, src_fn);
130 }
131
132 extern int test12_v;
133
134 struct { const char *t; int a; } test12[] = {
135 { "tag", __builtin_constant_p(test12_v) && !test12_v ? 1 : 0 }
136 };
137
138 extern char test13_v;
139 struct { int a; } test13 = { __builtin_constant_p(test13_v) };
140
141 extern unsigned long long test14_v;
142
143 void test14(void) {
144 // CHECK-LABEL: test14
145 // CHECK: call void asm sideeffect "", {{.*}}(i32 -1)
146 __asm__ __volatile__("" :: "n"( (__builtin_constant_p(test14_v) || 0) ? 1 : -1));
147 }
148
149 int test15_f(void);
150 // CHECK-LABEL: define{{.*}} void @test15
151 // CHECK-NOT: call {{.*}}test15_f
152 void test15(void) {
153 int a, b;
154 (void)__builtin_constant_p((a = b, test15_f()));
155 }