Revert "[llvm] Improve llvm.objectsize computation by computing GEP, alloca and mallo...
[llvm-project.git] / clang / test / CodeGen / volatile-1.c
blob7116a4e1d03da9c21283fd70f060b7a5c59d0422
1 // XFAIL: target=aarch64-pc-windows-msvc
2 // RUN: %clang_cc1 -Wno-return-type -Wno-unused-value -emit-llvm %s -w -o - | FileCheck %s
4 // CHECK: @i = {{(dso_local )?}}global [[INT:i[0-9]+]] 0
5 volatile int i, j, k;
6 volatile int ar[5];
7 volatile char c;
8 // CHECK: @ci = {{(dso_local )?}}global [[CINT:.*]] zeroinitializer
9 volatile _Complex int ci;
10 volatile struct S {
11 #ifdef __cplusplus
12 void operator =(volatile struct S&o) volatile;
13 #endif
14 int i;
15 } a, b;
17 //void operator =(volatile struct S&o1, volatile struct S&o2) volatile;
18 int printf(const char *, ...);
21 // Note that these test results are very much specific to C!
22 // Assignments in C++ yield l-values, not r-values, and the situations
23 // that do implicit lvalue-to-rvalue conversion are substantially
24 // reduced.
26 // CHECK-LABEL: define {{.*}}void @test()
27 void test(void) {
28 // CHECK: load volatile [[INT]], ptr @i
30 // CHECK-NEXT: load volatile [[INT]], ptr @ci, align 4
31 // CHECK-NEXT: load volatile [[INT]], ptr getelementptr inbounds nuw ([[CINT]], ptr @ci, i32 0, i32 1), align 4
32 // CHECK-NEXT: sitofp [[INT]]
33 (float)(ci);
34 // CHECK-NEXT: load volatile [[INT]], ptr @ci, align 4
35 // CHECK-NEXT: load volatile [[INT]], ptr getelementptr inbounds nuw ([[CINT]], ptr @ci, i32 0, i32 1), align 4
36 (void)ci;
37 // CHECK-NEXT: memcpy
38 (void)a;
39 // CHECK-NEXT: [[R:%.*]] = load volatile [[INT]], ptr @ci, align 4
40 // CHECK-NEXT: [[I:%.*]] = load volatile [[INT]], ptr getelementptr inbounds nuw ([[CINT]], ptr @ci, i32 0, i32 1), align 4
41 // CHECK-NEXT: store volatile [[INT]] [[R]], ptr @ci, align 4
42 // CHECK-NEXT: store volatile [[INT]] [[I]], ptr getelementptr inbounds nuw ([[CINT]], ptr @ci, i32 0, i32 1), align 4
43 (void)(ci=ci);
44 // CHECK-NEXT: [[T:%.*]] = load volatile [[INT]], ptr @j
45 // CHECK-NEXT: store volatile [[INT]] [[T]], ptr @i
46 (void)(i=j);
47 // CHECK-NEXT: [[R1:%.*]] = load volatile [[INT]], ptr @ci, align 4
48 // CHECK-NEXT: [[I1:%.*]] = load volatile [[INT]], ptr getelementptr inbounds nuw ([[CINT]], ptr @ci, i32 0, i32 1), align 4
49 // CHECK-NEXT: [[R2:%.*]] = load volatile [[INT]], ptr @ci, align 4
50 // CHECK-NEXT: [[I2:%.*]] = load volatile [[INT]], ptr getelementptr inbounds nuw ([[CINT]], ptr @ci, i32 0, i32 1), align 4
51 // Not sure why they're ordered this way.
52 // CHECK-NEXT: [[R:%.*]] = add [[INT]] [[R2]], [[R1]]
53 // CHECK-NEXT: [[I:%.*]] = add [[INT]] [[I2]], [[I1]]
54 // CHECK-NEXT: store volatile [[INT]] [[R]], ptr @ci, align 4
55 // CHECK-NEXT: store volatile [[INT]] [[I]], ptr getelementptr inbounds nuw ([[CINT]], ptr @ci, i32 0, i32 1), align 4
56 ci+=ci;
58 // CHECK-NEXT: [[R1:%.*]] = load volatile [[INT]], ptr @ci, align 4
59 // CHECK-NEXT: [[I1:%.*]] = load volatile [[INT]], ptr getelementptr inbounds nuw ([[CINT]], ptr @ci, i32 0, i32 1), align 4
60 // CHECK-NEXT: [[R2:%.*]] = load volatile [[INT]], ptr @ci, align 4
61 // CHECK-NEXT: [[I2:%.*]] = load volatile [[INT]], ptr getelementptr inbounds nuw ([[CINT]], ptr @ci, i32 0, i32 1), align 4
62 // CHECK-NEXT: [[R:%.*]] = add [[INT]] [[R2]], [[R1]]
63 // CHECK-NEXT: [[I:%.*]] = add [[INT]] [[I2]], [[I1]]
64 // CHECK-NEXT: store volatile [[INT]] [[R]], ptr @ci, align 4
65 // CHECK-NEXT: store volatile [[INT]] [[I]], ptr getelementptr inbounds nuw ([[CINT]], ptr @ci, i32 0, i32 1), align 4
66 // CHECK-NEXT: [[R2:%.*]] = load volatile [[INT]], ptr @ci, align 4
67 // CHECK-NEXT: [[I2:%.*]] = load volatile [[INT]], ptr getelementptr inbounds nuw ([[CINT]], ptr @ci, i32 0, i32 1), align 4
68 // These additions can be elided
69 // CHECK-NEXT: add [[INT]] [[R]], [[R2]]
70 // CHECK-NEXT: add [[INT]] [[I]], [[I2]]
71 (ci += ci) + ci;
72 // CHECK-NEXT: call void asm
73 asm("nop");
74 // CHECK-NEXT: load volatile
75 // CHECK-NEXT: load volatile
76 // CHECK-NEXT: add nsw [[INT]]
77 // CHECK-NEXT: store volatile
78 // CHECK-NEXT: load volatile
79 // CHECK-NEXT: add nsw [[INT]]
80 (i += j) + k;
81 // CHECK-NEXT: call void asm
82 asm("nop");
83 // CHECK-NEXT: load volatile
84 // CHECK-NEXT: load volatile
85 // CHECK-NEXT: add nsw [[INT]]
86 // CHECK-NEXT: store volatile
87 // CHECK-NEXT: add nsw [[INT]]
88 (i += j) + 1;
89 // CHECK-NEXT: call void asm
90 asm("nop");
91 // CHECK-NEXT: load volatile
92 // CHECK-NEXT: load volatile
93 // CHECK-NEXT: load volatile
94 // CHECK-NEXT: load volatile
95 // CHECK-NEXT: add [[INT]]
96 // CHECK-NEXT: add [[INT]]
97 ci+ci;
99 // CHECK-NEXT: load volatile
100 __real i;
101 // CHECK-NEXT: load volatile
102 // CHECK-NEXT: load volatile
103 +ci;
104 // CHECK-NEXT: call void asm
105 asm("nop");
106 // CHECK-NEXT: load volatile
107 // CHECK-NEXT: store volatile
108 (void)(i=i);
109 // CHECK-NEXT: load volatile
110 // CHECK-NEXT: store volatile
111 // CHECK-NEXT: sitofp
112 (float)(i=i);
113 // CHECK-NEXT: load volatile
114 (void)i;
115 // CHECK-NEXT: load volatile
116 // CHECK-NEXT: store volatile
117 i=i;
118 // CHECK-NEXT: load volatile
119 // CHECK-NEXT: store volatile
120 // CHECK-NEXT: store volatile
121 i=i=i;
122 #ifndef __cplusplus
123 // CHECK-NEXT: load volatile
124 // CHECK-NEXT: store volatile
125 (void)__builtin_choose_expr(0, i=i, j=j);
126 #endif
127 // CHECK-NEXT: load volatile
128 // CHECK-NEXT: icmp
129 // CHECK-NEXT: br i1
130 // CHECK: load volatile
131 // CHECK-NEXT: store volatile
132 // CHECK-NEXT: br label
133 // CHECK: load volatile
134 // CHECK-NEXT: store volatile
135 // CHECK-NEXT: br label
136 k ? (i=i) : (j=j);
137 // CHECK: phi
138 // CHECK-NEXT: load volatile
139 // CHECK-NEXT: load volatile
140 // CHECK-NEXT: store volatile
141 (void)(i,(i=i));
142 // CHECK-NEXT: load volatile
143 // CHECK-NEXT: store volatile
144 // CHECK-NEXT: load volatile
145 i=i,i;
146 // CHECK-NEXT: load volatile
147 // CHECK-NEXT: store volatile
148 // CHECK-NEXT: load volatile
149 // CHECK-NEXT: store volatile
150 (i=j,k=j);
151 // CHECK-NEXT: load volatile
152 // CHECK-NEXT: store volatile
153 // CHECK-NEXT: load volatile
154 (i=j,k);
155 // CHECK-NEXT: load volatile
156 // CHECK-NEXT: load volatile
157 (i,j);
158 // CHECK-NEXT: load volatile
159 // CHECK-NEXT: trunc
160 // CHECK-NEXT: store volatile
161 // CHECK-NEXT: sext
162 // CHECK-NEXT: store volatile
163 i=c=k;
164 // CHECK-NEXT: load volatile
165 // CHECK-NEXT: load volatile
166 // CHECK-NEXT: add nsw [[INT]]
167 // CHECK-NEXT: store volatile
168 i+=k;
169 // CHECK-NEXT: load volatile
170 // CHECK-NEXT: load volatile
172 #ifndef __cplusplus
173 // CHECK-NEXT: load volatile
174 // CHECK-NEXT: load volatile
175 (int)ci;
176 // CHECK-NEXT: load volatile
177 // CHECK-NEXT: load volatile
178 // CHECK-NEXT: icmp ne
179 // CHECK-NEXT: icmp ne
180 // CHECK-NEXT: or i1
181 (_Bool)ci;
182 #endif
183 // CHECK-NEXT: load volatile
184 // CHECK-NEXT: load volatile
185 // CHECK-NEXT: store volatile
186 // CHECK-NEXT: store volatile
187 ci=ci;
188 // CHECK-NEXT: load volatile
189 // CHECK-NEXT: load volatile
190 // CHECK-NEXT: store volatile
191 // CHECK-NEXT: store volatile
192 // CHECK-NEXT: store volatile
193 // CHECK-NEXT: store volatile
194 ci=ci=ci;
195 // CHECK-NEXT: [[T:%.*]] = load volatile [[INT]], ptr getelementptr inbounds nuw ([[CINT]], ptr @ci, i32 0, i32 1)
196 // CHECK-NEXT: store volatile [[INT]] [[T]], ptr getelementptr inbounds nuw ([[CINT]], ptr @ci, i32 0, i32 1)
197 // CHECK-NEXT: store volatile [[INT]] [[T]], ptr getelementptr inbounds nuw ([[CINT]], ptr @ci, i32 0, i32 1)
198 __imag ci = __imag ci = __imag ci;
199 // CHECK-NEXT: load volatile
200 // CHECK-NEXT: store volatile
201 __real (i = j);
202 // CHECK-NEXT: load volatile
203 __imag i;
205 // ============================================================
206 // FIXME: Test cases we get wrong.
208 // A use. We load all of a into a copy of a, then load i. gcc forgets to do
209 // the assignment.
210 // (a = a).i;
212 // ============================================================
213 // Test cases where we intentionally differ from gcc, due to suspected bugs in
214 // gcc.
216 // Not a use. gcc forgets to do the assignment.
217 // CHECK-NEXT: call void @llvm.memcpy{{.*}}, i1 true
218 // CHECK-NEXT: call void @llvm.memcpy{{.*}}, i1 true
219 ((a=a),a);
221 // Not a use. gcc gets this wrong, it doesn't emit the copy!
222 // (void)(a=a);
224 // Not a use. gcc got this wrong in 4.2 and omitted the side effects
225 // entirely, but it is fixed in 4.4.0.
226 // CHECK-NEXT: load volatile
227 // CHECK-NEXT: store volatile
228 __imag (i = j);
230 #ifndef __cplusplus
231 // A use of the real part
232 // CHECK-NEXT: load volatile
233 // CHECK-NEXT: load volatile
234 // CHECK-NEXT: store volatile
235 // CHECK-NEXT: store volatile
236 // CHECK-NEXT: sitofp
237 (float)(ci=ci);
238 // Not a use, bug? gcc treats this as not a use, that's probably a bug due to
239 // tree folding ignoring volatile.
240 // CHECK-NEXT: load volatile
241 // CHECK-NEXT: load volatile
242 // CHECK-NEXT: store volatile
243 // CHECK-NEXT: store volatile
244 (int)(ci=ci);
245 #endif
247 // A use.
248 // CHECK-NEXT: load volatile
249 // CHECK-NEXT: store volatile
250 // CHECK-NEXT: sitofp
251 (float)(i=i);
252 // A use. gcc treats this as not a use, that's probably a bug due to tree
253 // folding ignoring volatile.
254 // CHECK-NEXT: load volatile
255 // CHECK-NEXT: store volatile
256 (int)(i=i);
258 // A use.
259 // CHECK-NEXT: load volatile
260 // CHECK-NEXT: store volatile
261 // CHECK-NEXT: sub
262 -(i=j);
263 // A use. gcc treats this a not a use, that's probably a bug due to tree
264 // folding ignoring volatile.
265 // CHECK-NEXT: load volatile
266 // CHECK-NEXT: store volatile
267 +(i=k);
269 // A use. gcc treats this a not a use, that's probably a bug due to tree
270 // folding ignoring volatile.
271 // CHECK-NEXT: load volatile
272 // CHECK-NEXT: load volatile
273 // CHECK-NEXT: store volatile
274 // CHECK-NEXT: store volatile
275 __real (ci=ci);
277 // A use.
278 // CHECK-NEXT: load volatile
279 // CHECK-NEXT: add
280 i + 0;
281 // A use.
282 // CHECK-NEXT: load volatile
283 // CHECK-NEXT: store volatile
284 // CHECK-NEXT: load volatile
285 // CHECK-NEXT: add
286 (i=j) + i;
287 // A use. gcc treats this as not a use, that's probably a bug due to tree
288 // folding ignoring volatile.
289 // CHECK-NEXT: load volatile
290 // CHECK-NEXT: store volatile
291 // CHECK-NEXT: add
292 (i=j) + 0;
294 #ifdef __cplusplus
295 (i,j)=k;
296 (j=k,i)=i;
297 struct { int x; } s, s1;
298 printf("s is at %p\n", &s);
299 printf("s is at %p\n", &(s = s1));
300 printf("s.x is at %p\n", &((s = s1).x));
301 #endif
304 extern volatile enum X x;
305 // CHECK-LABEL: define {{.*}}void @test1()
306 void test1(void) {
307 extern void test1_helper(void);
308 test1_helper();
309 // CHECK: call {{.*}}void @test1_helper()
310 // CHECK-NEXT: ret void
312 (void) x;
313 return x;
316 // CHECK: define {{.*}} @test2()
317 int test2(void) {
318 // CHECK: load volatile i32, ptr
319 // CHECK-NEXT: load volatile i32, ptr
320 // CHECK-NEXT: load volatile i32, ptr
321 // CHECK-NEXT: add i32
322 // CHECK-NEXT: add i32
323 // CHECK-NEXT: store volatile i32
324 // CHECK-NEXT: ret i32
325 return i += ci;