blob: 7965dbe93f9a0e40708ce77a6a86bdc52784bbe9 [file] [log] [blame]
Joerg Sonnenberger529f4ed2019-10-13 22:33:461// 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
Bill Wendling2a81f662018-12-01 08:29:363
4int a = 42;
5
Bill Wendling2a81f662018-12-01 08:29:366/* --- Compound literals */
7
8struct foo { int x, y; };
9
10int y;
11struct foo f = (struct foo){ __builtin_constant_p(y), 42 };
12
13struct foo test0(int expr) {
Joerg Sonnenberger529f4ed2019-10-13 22:33:4614 // CHECK-LABEL: test0
15 // CHECK: call i1 @llvm.is.constant.i32
Bill Wendling2a81f662018-12-01 08:29:3616 struct foo f = (struct foo){ __builtin_constant_p(expr), 42 };
17 return f;
18}
19
20/* --- Pointer types */
21
Bill Wendling2a81f662018-12-01 08:29:3622int test1() {
Joerg Sonnenberger529f4ed2019-10-13 22:33:4623 // CHECK-LABEL: test1
Bill Wendling2a81f662018-12-01 08:29:3624 // CHECK: ret i32 0
25 return __builtin_constant_p(&a - 13);
26}
27
Bill Wendling2a81f662018-12-01 08:29:3628/* --- Aggregate types */
29
30int b[] = {1, 2, 3};
31
Joerg Sonnenberger529f4ed2019-10-13 22:33:4632int test2() {
33 // CHECK-LABEL: test2
Bill Wendling2a81f662018-12-01 08:29:3634 // CHECK: ret i32 0
35 return __builtin_constant_p(b);
36}
37
Joerg Sonnenberger529f4ed2019-10-13 22:33:4638const char test3_c[] = {1, 2, 3, 0};
Bill Wendling2a81f662018-12-01 08:29:3639
Joerg Sonnenberger529f4ed2019-10-13 22:33:4640int test3() {
41 // CHECK-LABEL: test3
Bill Wendling2a81f662018-12-01 08:29:3642 // CHECK: ret i32 0
Joerg Sonnenberger529f4ed2019-10-13 22:33:4643 return __builtin_constant_p(test3_c);
Bill Wendling2a81f662018-12-01 08:29:3644}
45
Joerg Sonnenberger529f4ed2019-10-13 22:33:4646inline char test4_i(const char *x) {
Bill Wendling2a81f662018-12-01 08:29:3647 return x[1];
48}
49
Joerg Sonnenberger529f4ed2019-10-13 22:33:4650int test4() {
Fangrui Songdbc96b52020-02-03 18:09:3951 // CHECK: define i32 @test4
Bill Wendling2a81f662018-12-01 08:29:3652 // CHECK: ret i32 0
Joerg Sonnenberger529f4ed2019-10-13 22:33:4653 return __builtin_constant_p(test4_i(test3_c));
Bill Wendling2a81f662018-12-01 08:29:3654}
55
56/* --- Constant global variables */
57
58const int c = 42;
59
Joerg Sonnenberger529f4ed2019-10-13 22:33:4660int test5() {
61 // CHECK-LABEL: test5
Bill Wendling2a81f662018-12-01 08:29:3662 // CHECK: ret i32 1
Joerg Sonnenberger529f4ed2019-10-13 22:33:4663 return __builtin_constant_p(c);
Bill Wendling2a81f662018-12-01 08:29:3664}
65
66/* --- Array types */
67
68int arr[] = { 1, 2, 3 };
69const int c_arr[] = { 1, 2, 3 };
70
Joerg Sonnenberger529f4ed2019-10-13 22:33:4671int test6() {
72 // CHECK-LABEL: test6
73 // CHECK: call i1 @llvm.is.constant.i32
Bill Wendling2a81f662018-12-01 08:29:3674 return __builtin_constant_p(arr[2]);
75}
76
Joerg Sonnenberger529f4ed2019-10-13 22:33:4677int test7() {
78 // CHECK-LABEL: test7
79 // CHECK: call i1 @llvm.is.constant.i32
Bill Wendling2a81f662018-12-01 08:29:3680 return __builtin_constant_p(c_arr[2]);
81}
82
Joerg Sonnenberger529f4ed2019-10-13 22:33:4683int test8() {
84 // CHECK-LABEL: test8
Bill Wendling2a81f662018-12-01 08:29:3685 // CHECK: ret i32 0
86 return __builtin_constant_p(c_arr);
87}
88
89/* --- Function pointers */
90
Joerg Sonnenberger529f4ed2019-10-13 22:33:4691int test9() {
92 // CHECK-LABEL: test9
Bill Wendling2a81f662018-12-01 08:29:3693 // CHECK: ret i32 0
Joerg Sonnenberger529f4ed2019-10-13 22:33:4694 return __builtin_constant_p(&test9);
Bill Wendling2a81f662018-12-01 08:29:3695}
96
Joerg Sonnenberger529f4ed2019-10-13 22:33:4697int test10() {
98 // CHECK-LABEL: test10
Bill Wendling2a81f662018-12-01 08:29:3699 // CHECK: ret i32 1
100 return __builtin_constant_p(&test10 != 0);
101}
102
103typedef unsigned long uintptr_t;
104#define assign(p, v) ({ \
105 uintptr_t _r_a_p__v = (uintptr_t)(v); \
106 if (__builtin_constant_p(v) && _r_a_p__v == (uintptr_t)0) { \
107 union { \
108 uintptr_t __val; \
109 char __c[1]; \
110 } __u = { \
111 .__val = (uintptr_t)_r_a_p__v \
112 }; \
113 *(volatile unsigned int*)&p = *(unsigned int*)(__u.__c); \
114 __u.__val; \
115 } \
116 _r_a_p__v; \
117})
118
119typedef void fn_p(void);
120extern fn_p *dest_p;
121
122static void src_fn(void) {
123}
124
Joerg Sonnenberger529f4ed2019-10-13 22:33:46125void test11() {
Bill Wendling2a81f662018-12-01 08:29:36126 assign(dest_p, src_fn);
127}
128
Joerg Sonnenberger529f4ed2019-10-13 22:33:46129extern int test12_v;
Bill Wendling2a81f662018-12-01 08:29:36130
Joerg Sonnenberger529f4ed2019-10-13 22:33:46131struct { const char *t; int a; } test12[] = {
132 { "tag", __builtin_constant_p(test12_v) && !test12_v ? 1 : 0 }
Bill Wendling2a81f662018-12-01 08:29:36133};
134
Joerg Sonnenberger529f4ed2019-10-13 22:33:46135extern char test13_v;
136struct { int a; } test13 = { __builtin_constant_p(test13_v) };
Bill Wendlingaa775132018-12-18 22:54:03137
Joerg Sonnenberger529f4ed2019-10-13 22:33:46138extern unsigned long long test14_v;
Bill Wendlingaa775132018-12-18 22:54:03139
Joerg Sonnenberger529f4ed2019-10-13 22:33:46140void test14() {
141 // CHECK-LABEL: test14
Bill Wendlingaa775132018-12-18 22:54:03142 // CHECK: call void asm sideeffect "", {{.*}}(i32 -1)
Joerg Sonnenberger529f4ed2019-10-13 22:33:46143 __asm__ __volatile__("" :: "n"( (__builtin_constant_p(test14_v) || 0) ? 1 : -1));
Bill Wendlingaa775132018-12-18 22:54:03144}
Richard Smith31cfb312019-04-27 02:58:17145
Joerg Sonnenberger529f4ed2019-10-13 22:33:46146int test15_f();
Fangrui Songdbc96b52020-02-03 18:09:39147// CHECK-LABEL: define void @test15
Joerg Sonnenberger529f4ed2019-10-13 22:33:46148// CHECK-NOT: call {{.*}}test15_f
149void test15() {
Richard Smith31cfb312019-04-27 02:58:17150 int a, b;
Joerg Sonnenberger529f4ed2019-10-13 22:33:46151 (void)__builtin_constant_p((a = b, test15_f()));
Richard Smith31cfb312019-04-27 02:58:17152}