Debian Codegraph @

clang/test/CIR/CodeGenBuiltins/builtin-constant-p.c

raw
1 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-cir %s -o %t.cir
2 // RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
3 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
4 // RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM
5 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll
6 // RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG
7
8 int a = 42;
9
10 /* --- Compound literals */
11
12 struct foo { int x, y; };
13
14 int y;
15 struct foo f = (struct foo){ __builtin_constant_p(y), 42 };
16
17 // CIR: cir.global external @f = #cir.const_record<{#cir.int<0> : !s32i, #cir.int<42> : !s32i}> : !rec_foo
18 // LLVM: @f = global %struct.foo { i32 0, i32 42 }
19 // OGCG: @f = global %struct.foo { i32 0, i32 42 }
20
21 struct foo test0(int expr) {
22 struct foo f = (struct foo){ __builtin_constant_p(expr), 42 };
23 return f;
24 }
25
26 // CIR: cir.func {{.*}} @test0(%[[ARG0:.*]]: !s32i {{.*}}) -> !rec_foo
27 // CIR: %[[EXPR_ADDR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["expr", init]
28 // CIR: cir.store %[[ARG0]], %[[EXPR_ADDR]]
29 // CIR: %[[EXPR:.*]] = cir.load{{.*}} %[[EXPR_ADDR]]
30 // CIR: %[[IS_CONSTANT:.*]] = cir.is_constant %[[EXPR]] : !s32i -> !cir.bool
31
32 // LLVM: define{{.*}} %struct.foo @test0(i32 %[[ARG0:.*]])
33 // LLVM: %[[EXPR_ADDR:.*]] = alloca i32
34 // LLVM: store i32 %[[ARG0]], ptr %[[EXPR_ADDR]]
35 // LLVM: %[[EXPR:.*]] = load i32, ptr %[[EXPR_ADDR]]
36 // LLVM: %[[IS_CONSTANT:.*]] = call i1 @llvm.is.constant.i32(i32 %[[EXPR]])
37
38 // OGCG: define{{.*}} i64 @test0(i32 {{.*}} %[[ARG0:.*]])
39 // OGCG: %[[EXPR_ADDR:.*]] = alloca i32
40 // OGCG: store i32 %[[ARG0]], ptr %[[EXPR_ADDR]]
41 // OGCG: %[[EXPR:.*]] = load i32, ptr %[[EXPR_ADDR]]
42 // OGCG: %[[IS_CONSTANT:.*]] = call i1 @llvm.is.constant.i32(i32 %[[EXPR]])
43
44 /* --- Pointer types */
45
46 int test1(void) {
47 return __builtin_constant_p(&a - 13);
48 }
49
50 // CIR: cir.func {{.*}} @test1() -> !s32i
51 // CIR: %[[TMP1:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["__retval"]
52 // CIR: %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i
53 // CIR: cir.store %[[ZERO]], %[[TMP1]] : !s32i, !cir.ptr<!s32i>
54 // CIR: %[[TMP2:.*]] = cir.load %[[TMP1]] : !cir.ptr<!s32i>, !s32i
55 // CIR: cir.return %[[TMP2]] : !s32i
56
57 // LLVM: define{{.*}} i32 @test1()
58 // LLVM: %[[TMP1:.*]] = alloca i32
59 // LLVM: store i32 0, ptr %[[TMP1]]
60 // LLVM: %[[TMP2:.*]] = load i32, ptr %[[TMP1]]
61 // LLVM: ret i32 %[[TMP2]]
62
63 // OGCG: define{{.*}} i32 @test1()
64 // OGCG: ret i32 0
65
66 /* --- Aggregate types */
67
68 int b[] = {1, 2, 3};
69
70 int test2(void) {
71 return __builtin_constant_p(b);
72 }
73
74 // CIR: cir.func {{.*}} @test2() -> !s32i
75 // CIR: %[[TMP1:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["__retval"]
76 // CIR: %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i
77 // CIR: cir.store %[[ZERO]], %[[TMP1]] : !s32i, !cir.ptr<!s32i>
78 // CIR: %[[TMP2:.*]] = cir.load %[[TMP1]] : !cir.ptr<!s32i>, !s32i
79 // CIR: cir.return %[[TMP2]] : !s32i
80
81 // LLVM: define{{.*}} i32 @test2()
82 // LLVM: %[[TMP1:.*]] = alloca i32
83 // LLVM: store i32 0, ptr %[[TMP1]]
84 // LLVM: %[[TMP2:.*]] = load i32, ptr %[[TMP1]]
85 // LLVM: ret i32 %[[TMP2]]
86
87 // OGCG: define{{.*}} i32 @test2()
88 // OGCG: ret i32 0
89
90 const char test3_c[] = {1, 2, 3, 0};
91
92 int test3(void) {
93 return __builtin_constant_p(test3_c);
94 }
95
96 // CIR: cir.func {{.*}} @test3() -> !s32i
97 // CIR: %[[TMP1:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["__retval"]
98 // CIR: %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i
99 // CIR: cir.store %[[ZERO]], %[[TMP1]] : !s32i, !cir.ptr<!s32i>
100 // CIR: %[[TMP2:.*]] = cir.load %[[TMP1]] : !cir.ptr<!s32i>, !s32i
101 // CIR: cir.return %[[TMP2]] : !s32i
102
103 // LLVM: define{{.*}} i32 @test3()
104 // LLVM: %[[TMP1:.*]] = alloca i32
105 // LLVM: store i32 0, ptr %[[TMP1]]
106 // LLVM: %[[TMP2:.*]] = load i32, ptr %[[TMP1]]
107 // LLVM: ret i32 %[[TMP2]]
108
109 // OGCG: define{{.*}} i32 @test3()
110 // OGCG: ret i32 0
111
112 inline char test4_i(const char *x) {
113 return x[1];
114 }
115
116 int test4(void) {
117 return __builtin_constant_p(test4_i(test3_c));
118 }
119
120 // CIR: cir.func {{.*}} @test4() -> !s32i
121 // CIR: %[[TMP1:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["__retval"]
122 // CIR: %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i
123 // CIR: cir.store %[[ZERO]], %[[TMP1]] : !s32i, !cir.ptr<!s32i>
124 // CIR: %[[TMP2:.*]] = cir.load %[[TMP1]] : !cir.ptr<!s32i>, !s32i
125 // CIR: cir.return %[[TMP2]] : !s32i
126
127 // LLVM: define{{.*}} i32 @test4()
128 // LLVM: %[[TMP1:.*]] = alloca i32
129 // LLVM: store i32 0, ptr %[[TMP1]]
130 // LLVM: %[[TMP2:.*]] = load i32, ptr %[[TMP1]]
131 // LLVM: ret i32 %[[TMP2]]
132
133 // OGCG: define{{.*}} i32 @test4()
134 // OGCG: ret i32 0
135
136 /* --- Constant global variables */
137
138 const int c = 42;
139
140 int test5(void) {
141 return __builtin_constant_p(c);
142 }
143
144 // CIR: cir.func {{.*}} @test5() -> !s32i
145 // CIR: %[[TMP1:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["__retval"]
146 // CIR: %[[ONE:.*]] = cir.const #cir.int<1> : !s32i
147 // CIR: cir.store %[[ONE]], %[[TMP1]] : !s32i, !cir.ptr<!s32i>
148 // CIR: %[[TMP2:.*]] = cir.load %[[TMP1]] : !cir.ptr<!s32i>, !s32i
149 // CIR: cir.return %[[TMP2]] : !s32i
150
151 // LLVM: define{{.*}} i32 @test5()
152 // LLVM: %[[TMP1:.*]] = alloca i32
153 // LLVM: store i32 1, ptr %[[TMP1]]
154 // LLVM: %[[TMP2:.*]] = load i32, ptr %[[TMP1]]
155 // LLVM: ret i32 %[[TMP2]]
156
157 // OGCG: define{{.*}} i32 @test5()
158 // OGCG: ret i32 1
159
160 /* --- Array types */
161
162 int arr[] = { 1, 2, 3 };
163
164 int test6(void) {
165 return __builtin_constant_p(arr[2]);
166 }
167
168 // CIR: cir.func {{.*}} @test6() -> !s32i
169 // CIR: %[[TWO:.*]] = cir.const #cir.int<2> : !s32i
170 // CIR: %[[ARR:.*]] = cir.get_global @arr : !cir.ptr<!cir.array<!s32i x 3>>
171 // CIR: %[[ELE_PTR:.*]] = cir.get_element %[[ARR]][%[[TWO]] : !s32i] : !cir.ptr<!cir.array<!s32i x 3>> -> !cir.ptr<!s32i>
172 // CIR: %[[ELE:.*]] = cir.load{{.*}} %[[ELE_PTR]] : !cir.ptr<!s32i>, !s32i
173 // CIR: %[[IS_CONSTANT:.*]] = cir.is_constant %[[ELE]] : !s32i -> !cir.bool
174
175 // LLVM: define {{.*}} i32 @test6()
176 // LLVM: %[[TMP1:.*]] = load i32, ptr getelementptr inbounds nuw (i8, ptr @arr, i64 8)
177 // LLVM: %[[TMP2:.*]] = call i1 @llvm.is.constant.i32(i32 %[[TMP1]])
178
179 // OGCG: define {{.*}} i32 @test6()
180 // OGCG: %[[TMP1:.*]] = load i32, ptr getelementptr inbounds ([3 x i32], ptr @arr, i64 0, i64 2)
181 // OGCG: %[[TMP2:.*]] = call i1 @llvm.is.constant.i32(i32 %[[TMP1]])
182
183 const int c_arr[] = { 1, 2, 3 };
184
185 int test7(void) {
186 return __builtin_constant_p(c_arr[2]);
187 }
188
189 // CIR: cir.func {{.*}} @test7() -> !s32i
190 // CIR: %[[TMP1:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["__retval"]
191 // CIR: %[[ONE:.*]] = cir.const #cir.int<1> : !s32i
192 // CIR: cir.store %[[ONE]], %[[TMP1]] : !s32i, !cir.ptr<!s32i>
193 // CIR: %[[TMP2:.*]] = cir.load %[[TMP1]] : !cir.ptr<!s32i>, !s32i
194 // CIR: cir.return %[[TMP2]] : !s32i
195
196 // LLVM: define{{.*}} i32 @test7()
197 // LLVM: %[[TMP1:.*]] = alloca i32
198 // LLVM: store i32 1, ptr %[[TMP1]]
199 // LLVM: %[[TMP2:.*]] = load i32, ptr %[[TMP1]]
200 // LLVM: ret i32 %[[TMP2]]
201
202 // OGCG: define{{.*}} i32 @test7()
203 // OGCG: ret i32 1
204
205 int test8(void) {
206 return __builtin_constant_p(c_arr);
207 }
208
209 // CIR: cir.func {{.*}} @test8() -> !s32i
210 // CIR: %[[TMP1:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["__retval"]
211 // CIR: %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i
212 // CIR: cir.store %[[ZERO]], %[[TMP1]] : !s32i, !cir.ptr<!s32i>
213 // CIR: %[[TMP2:.*]] = cir.load %[[TMP1]] : !cir.ptr<!s32i>, !s32i
214 // CIR: cir.return %[[TMP2]] : !s32i
215
216 // LLVM: define{{.*}} i32 @test8()
217 // LLVM: %[[TMP1:.*]] = alloca i32
218 // LLVM: store i32 0, ptr %[[TMP1]]
219 // LLVM: %[[TMP2:.*]] = load i32, ptr %[[TMP1]]
220 // LLVM: ret i32 %[[TMP2]]
221
222 // OGCG: define{{.*}} i32 @test8()
223 // OGCG: ret i32 0
224
225 /* --- Function pointers */
226
227 int test9(void) {
228 return __builtin_constant_p(&test9);
229 }
230
231 // CIR: cir.func {{.*}} @test9() -> !s32i
232 // CIR: %[[TMP1:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["__retval"]
233 // CIR: %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i
234 // CIR: cir.store %[[ZERO]], %[[TMP1]] : !s32i, !cir.ptr<!s32i>
235 // CIR: %[[TMP2:.*]] = cir.load %[[TMP1]] : !cir.ptr<!s32i>, !s32i
236 // CIR: cir.return %[[TMP2]] : !s32i
237
238 // LLVM: define{{.*}} i32 @test9()
239 // LLVM: %[[TMP1:.*]] = alloca i32
240 // LLVM: store i32 0, ptr %[[TMP1]]
241 // LLVM: %[[TMP2:.*]] = load i32, ptr %[[TMP1]]
242 // LLVM: ret i32 %[[TMP2]]
243
244 // OGCG: define{{.*}} i32 @test9()
245 // OGCG: ret i32 0
246
247 int test10(void) {
248 return __builtin_constant_p(&test10 != 0);
249 }
250
251 // CIR: cir.func {{.*}} @test10() -> !s32i
252 // CIR: %[[TMP1:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["__retval"]
253 // CIR: %[[ONE:.*]] = cir.const #cir.int<1> : !s32i
254 // CIR: cir.store %[[ONE]], %[[TMP1]] : !s32i, !cir.ptr<!s32i>
255 // CIR: %[[TMP2:.*]] = cir.load %[[TMP1]] : !cir.ptr<!s32i>, !s32i
256 // CIR: cir.return %[[TMP2]] : !s32i
257
258 // LLVM: define{{.*}} i32 @test10()
259 // LLVM: %[[TMP1:.*]] = alloca i32
260 // LLVM: store i32 1, ptr %[[TMP1]]
261 // LLVM: %[[TMP2:.*]] = load i32, ptr %[[TMP1]]
262 // LLVM: ret i32 %[[TMP2]]
263
264 // OGCG: define{{.*}} i32 @test10()
265 // OGCG: ret i32 1
266
267 int test11_f(void);
268 void test11(void) {
269 int a, b;
270 (void)__builtin_constant_p((a = b, test11_f()));
271 }
272
273 // CIR: cir.func {{.*}} @test11()
274 // CIR-NOT: call {{.*}}test11_f
275
276 // LLVM: define{{.*}} void @test11()
277 // LLVM-NOT: call {{.*}}test11_f
278
279 // OGCG: define{{.*}} void @test11()
280 // OGCG-NOT: call {{.*}}test11_f