Debian Codegraph @

clang/test/CIR/CodeGen/aapcs-volatile-bitfields.c

raw
1 // RUN: %clang_cc1 -triple aarch64-unknown-linux-gnu -fclangir -emit-cir -fdump-record-layouts %s -o %t.cir > %t.cirlayout
2 // RUN: FileCheck --input-file=%t.cirlayout %s --check-prefix=CIR-LAYOUT
3 // RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR
4
5 // RUN: %clang_cc1 -triple aarch64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
6 // RUN: FileCheck --input-file=%t-cir.ll %s --check-prefix=LLVM
7
8 // RUN: %clang_cc1 -triple aarch64-unknown-linux-gnu -emit-llvm -fdump-record-layouts %s -o %t.ll > %t.ogcglayout
9 // RUN: FileCheck --input-file=%t.ogcglayout %s --check-prefix=OGCG-LAYOUT
10 // RUN: FileCheck --input-file=%t.ll %s --check-prefix=OGCG
11
12 typedef struct {
13 unsigned int a : 9;
14 volatile unsigned int b : 1;
15 unsigned int c : 1;
16 } st1;
17
18 // CIR-LAYOUT: BitFields:[
19 // CIR-LAYOUT-NEXT: <CIRBitFieldInfo name:a offset:0 size:9 isSigned:0 storageSize:16 storageOffset:0 volatileOffset:0 volatileStorageSize:32 volatileStorageOffset:0>
20 // CIR-LAYOUT-NEXT: <CIRBitFieldInfo name:b offset:9 size:1 isSigned:0 storageSize:16 storageOffset:0 volatileOffset:9 volatileStorageSize:32 volatileStorageOffset:0>
21 // CIR-LAYOUT-NEXT: <CIRBitFieldInfo name:c offset:10 size:1 isSigned:0 storageSize:16 storageOffset:0 volatileOffset:10 volatileStorageSize:32 volatileStorageOffset:0>
22
23 // OGCG-LAYOUT: BitFields:[
24 // OGCG-LAYOUT-NEXT: <CGBitFieldInfo Offset:0 Size:9 IsSigned:0 StorageSize:16 StorageOffset:0 VolatileOffset:0 VolatileStorageSize:32 VolatileStorageOffset:0>
25 // OGCG-LAYOUT-NEXT: <CGBitFieldInfo Offset:9 Size:1 IsSigned:0 StorageSize:16 StorageOffset:0 VolatileOffset:9 VolatileStorageSize:32 VolatileStorageOffset:0>
26 // OGCG-LAYOUT-NEXT: <CGBitFieldInfo Offset:10 Size:1 IsSigned:0 StorageSize:16 StorageOffset:0 VolatileOffset:10 VolatileStorageSize:32 VolatileStorageOffset:0>
27
28 // different base types
29 typedef struct{
30 volatile short a : 3;
31 volatile int b: 13;
32 volatile long c : 5;
33 } st2;
34
35 // CIR-LAYOUT: BitFields:[
36 // CIR-LAYOUT-NEXT: <CIRBitFieldInfo name:a offset:0 size:3 isSigned:1 storageSize:32 storageOffset:0 volatileOffset:0 volatileStorageSize:16 volatileStorageOffset:0>
37 // CIR-LAYOUT-NEXT: <CIRBitFieldInfo name:b offset:3 size:13 isSigned:1 storageSize:32 storageOffset:0 volatileOffset:3 volatileStorageSize:32 volatileStorageOffset:0>
38 // CIR-LAYOUT-NEXT: <CIRBitFieldInfo name:c offset:16 size:5 isSigned:1 storageSize:32 storageOffset:0 volatileOffset:16 volatileStorageSize:64 volatileStorageOffset:0>
39
40 // OGCG-LAYOUT: BitFields:[
41 // OGCG-LAYOUT-NEXT: <CGBitFieldInfo Offset:0 Size:3 IsSigned:1 StorageSize:32 StorageOffset:0 VolatileOffset:0 VolatileStorageSize:16 VolatileStorageOffset:0>
42 // OGCG-LAYOUT-NEXT: <CGBitFieldInfo Offset:3 Size:13 IsSigned:1 StorageSize:32 StorageOffset:0 VolatileOffset:3 VolatileStorageSize:32 VolatileStorageOffset:0>
43 // OGCG-LAYOUT-NEXT: <CGBitFieldInfo Offset:16 Size:5 IsSigned:1 StorageSize:32 StorageOffset:0 VolatileOffset:16 VolatileStorageSize:64 VolatileStorageOffset:0>
44
45 typedef struct{
46 volatile unsigned int a : 3;
47 unsigned int : 0; // zero-length bit-field force next field to aligned int boundary
48 volatile unsigned int b : 5;
49 } st3;
50
51 // CIR-LAYOUT: BitFields:[
52 // CIR-LAYOUT-NEXT: <CIRBitFieldInfo name:a offset:0 size:3 isSigned:0 storageSize:8 storageOffset:0 volatileOffset:0 volatileStorageSize:32 volatileStorageOffset:0>
53 // CIR-LAYOUT-NEXT: <CIRBitFieldInfo name:b offset:0 size:5 isSigned:0 storageSize:8 storageOffset:4 volatileOffset:0 volatileStorageSize:0 volatileStorageOffset:0>
54
55 // OGCG-LAYOUT: BitFields:[
56 // OGCG-LAYOUT-NEXT: <CGBitFieldInfo Offset:0 Size:3 IsSigned:0 StorageSize:8 StorageOffset:0 VolatileOffset:0 VolatileStorageSize:32 VolatileStorageOffset:0>
57 // OGCG-LAYOUT-NEXT: <CGBitFieldInfo Offset:0 Size:5 IsSigned:0 StorageSize:8 StorageOffset:4 VolatileOffset:0 VolatileStorageSize:0 VolatileStorageOffset:0>
58
59 typedef struct{
60 volatile unsigned int a : 3;
61 unsigned int z;
62 volatile unsigned long b : 16;
63 } st4;
64
65 // CIR-LAYOUT: BitFields:[
66 // CIR-LAYOUT-NEXT: <CIRBitFieldInfo name:a offset:0 size:3 isSigned:0 storageSize:8 storageOffset:0 volatileOffset:0 volatileStorageSize:32 volatileStorageOffset:0>
67 // CIR-LAYOUT-NEXT: <CIRBitFieldInfo name:b offset:0 size:16 isSigned:0 storageSize:16 storageOffset:8 volatileOffset:0 volatileStorageSize:64 volatileStorageOffset:1>
68
69 // OGCG-LAYOUT: BitFields:[
70 // OGCG-LAYOUT-NEXT: <CGBitFieldInfo Offset:0 Size:3 IsSigned:0 StorageSize:8 StorageOffset:0 VolatileOffset:0 VolatileStorageSize:32 VolatileStorageOffset:0>
71 // OGCG-LAYOUT-NEXT: <CGBitFieldInfo Offset:0 Size:16 IsSigned:0 StorageSize:16 StorageOffset:8 VolatileOffset:0 VolatileStorageSize:64 VolatileStorageOffset:1>
72
73
74 void def () {
75 st1 s1;
76 st2 s2;
77 st3 s3;
78 st4 s4;
79 }
80
81 int check_load(st1 *s1) {
82 return s1->b;
83 }
84
85 // CIR: cir.func {{.*}} @check_load
86 // CIR: [[LOAD:%.*]] = cir.load align(8) {{.*}} : !cir.ptr<!cir.ptr<!rec_st1>>, !cir.ptr<!rec_st1>
87 // CIR: [[MEMBER:%.*]] = cir.get_member [[LOAD]][0] {name = "b"} : !cir.ptr<!rec_st1> -> !cir.ptr<!u16i>
88 // CIR: [[BITFI:%.*]] = cir.get_bitfield align(4) (#bfi_b, [[MEMBER]] {is_volatile} : !cir.ptr<!u16i>) -> !u32i
89 // CIR: [[CAST:%.*]] = cir.cast integral [[BITFI]] : !u32i -> !s32i
90 // CIR: cir.store [[CAST]], [[RETVAL:%.*]] : !s32i, !cir.ptr<!s32i>
91 // CIR: [[RET:%.*]] = cir.load [[RETVAL]] : !cir.ptr<!s32i>, !s32i
92 // CIR: cir.return [[RET]] : !s32i
93
94 // LLVM:define dso_local i32 @check_load
95 // LLVM: [[LOAD:%.*]] = load ptr, ptr {{.*}}, align 8
96 // LLVM: [[MEMBER:%.*]] = getelementptr %struct.st1, ptr [[LOAD]], i32 0, i32 0
97 // LLVM: [[LOADVOL:%.*]] = load volatile i32, ptr [[MEMBER]], align 4
98 // LLVM: [[LSHR:%.*]] = lshr i32 [[LOADVOL]], 9
99 // LLVM: [[CLEAR:%.*]] = and i32 [[LSHR]], 1
100 // LLVM: store i32 [[CLEAR]], ptr [[RETVAL:%.*]], align 4
101 // LLVM: [[RET:%.*]] = load i32, ptr [[RETVAL]], align 4
102 // LLVM: ret i32 [[RET]]
103
104 // OGCG: define dso_local i32 @check_load
105 // OGCG: [[LOAD:%.*]] = load ptr, ptr {{.*}}, align 8
106 // OGCG: [[LOADVOL:%.*]] = load volatile i32, ptr [[LOAD]], align 4
107 // OGCG: [[LSHR:%.*]] = lshr i32 [[LOADVOL]], 9
108 // OGCG: [[CLEAR:%.*]] = and i32 [[LSHR]], 1
109 // OGCG: ret i32 [[CLEAR]]
110
111 // this volatile bit-field container overlaps with a zero-length bit-field,
112 // so it may be accessed without using the container's width.
113 int check_load_exception(st3 *s3) {
114 return s3->b;
115 }
116
117 // CIR: cir.func {{.*}} @check_load_exception
118 // CIR: [[LOAD:%.*]] = cir.load align(8) {{.*}} : !cir.ptr<!cir.ptr<!rec_st3>>, !cir.ptr<!rec_st3>
119 // CIR: [[MEMBER:%.*]] = cir.get_member [[LOAD]][2] {name = "b"} : !cir.ptr<!rec_st3> -> !cir.ptr<!u8i>
120 // CIR: [[BITFI:%.*]] = cir.get_bitfield align(4) (#bfi_b1, [[MEMBER]] {is_volatile} : !cir.ptr<!u8i>) -> !u32i
121 // CIR: [[CAST:%.*]] = cir.cast integral [[BITFI]] : !u32i -> !s32i
122 // CIR: cir.store [[CAST]], [[RETVAL:%.*]] : !s32i, !cir.ptr<!s32i>
123 // CIR: [[RET:%.*]] = cir.load [[RETVAL]] : !cir.ptr<!s32i>, !s32i
124 // CIR: cir.return [[RET]] : !s32i
125
126 // LLVM:define dso_local i32 @check_load_exception
127 // LLVM: [[LOAD:%.*]] = load ptr, ptr {{.*}}, align 8
128 // LLVM: [[MEMBER:%.*]] = getelementptr %struct.st3, ptr [[LOAD]], i32 0, i32 2
129 // LLVM: [[LOADVOL:%.*]] = load volatile i8, ptr [[MEMBER]], align 4
130 // LLVM: [[CLEAR:%.*]] = and i8 [[LOADVOL]], 31
131 // LLVM: [[CAST:%.*]] = zext i8 [[CLEAR]] to i32
132 // LLVM: store i32 [[CAST]], ptr [[RETVAL:%.*]], align 4
133 // LLVM: [[RET:%.*]] = load i32, ptr [[RETVAL]], align 4
134 // LLVM: ret i32 [[RET]]
135
136 // OGCG: define dso_local i32 @check_load_exception
137 // OGCG: [[LOAD:%.*]] = load ptr, ptr {{.*}}, align 8
138 // OGCG: [[MEMBER:%.*]] = getelementptr inbounds nuw %struct.st3, ptr [[LOAD]], i32 0, i32 2
139 // OGCG: [[LOADVOL:%.*]] = load volatile i8, ptr [[MEMBER]], align 4
140 // OGCG: [[CLEAR:%.*]] = and i8 [[LOADVOL]], 31
141 // OGCG: [[CAST:%.*]] = zext i8 [[CLEAR]] to i32
142 // OGCG: ret i32 [[CAST]]
143
144 typedef struct {
145 volatile int a : 24;
146 char b;
147 volatile int c: 30;
148 } clip;
149
150 int clip_load_exception2(clip *c) {
151 return c->a;
152 }
153
154 // CIR: cir.func {{.*}} @clip_load_exception2
155 // CIR: [[LOAD:%.*]] = cir.load align(8) {{.*}} : !cir.ptr<!cir.ptr<!rec_clip>>, !cir.ptr<!rec_clip>
156 // CIR: [[MEMBER:%.*]] = cir.get_member [[LOAD]][0] {name = "a"} : !cir.ptr<!rec_clip> -> !cir.ptr<!cir.array<!u8i x 3>>
157 // CIR: [[BITFI:%.*]] = cir.get_bitfield align(4) (#bfi_a1, [[MEMBER]] {is_volatile} : !cir.ptr<!cir.array<!u8i x 3>>) -> !s32i
158 // CIR: cir.store [[BITFI]], [[RETVAL:%.*]] : !s32i, !cir.ptr<!s32i>
159 // CIR: [[RET:%.*]] = cir.load [[RETVAL]] : !cir.ptr<!s32i>, !s32i
160 // CIR: cir.return [[RET]] : !s32i
161
162 // LLVM:define dso_local i32 @clip_load_exception2
163 // LLVM: [[LOAD:%.*]] = load ptr, ptr {{.*}}, align 8
164 // LLVM: [[MEMBER:%.*]] = getelementptr %struct.clip, ptr [[LOAD]], i32 0, i32 0
165 // LLVM: [[LOADVOL:%.*]] = load volatile i24, ptr [[MEMBER]], align 4
166 // LLVM: [[CAST:%.*]] = sext i24 [[LOADVOL]] to i32
167 // LLVM: store i32 [[CAST]], ptr [[RETVAL:%.*]], align 4
168 // LLVM: [[RET:%.*]] = load i32, ptr [[RETVAL]], align 4
169 // LLVM: ret i32 [[RET]]
170
171 // OGCG: define dso_local i32 @clip_load_exception2
172 // OGCG: [[LOAD:%.*]] = load ptr, ptr {{.*}}, align 8
173 // OGCG: [[LOADVOL:%.*]] = load volatile i24, ptr [[LOAD]], align 4
174 // OGCG: [[CAST:%.*]] = sext i24 [[LOADVOL]] to i32
175 // OGCG: ret i32 [[CAST]]
176
177 void check_store(st2 *s2) {
178 s2->a = 1;
179 }
180
181 // CIR: cir.func {{.*}} @check_store
182 // CIR: [[CONST:%.*]] = cir.const #cir.int<1> : !s16i
183 // CIR: [[LOAD:%.*]] = cir.load align(8) {{.*}} : !cir.ptr<!cir.ptr<!rec_st2>>, !cir.ptr<!rec_st2>
184 // CIR: [[MEMBER:%.*]] = cir.get_member [[LOAD]][0] {name = "a"} : !cir.ptr<!rec_st2> -> !cir.ptr<!u32i>
185 // CIR: [[SETBF:%.*]] = cir.set_bitfield align(8) (#bfi_a, [[MEMBER]] : !cir.ptr<!u32i>, [[CONST]] : !s16i) {is_volatile} -> !s16i
186 // CIR: cir.return
187
188 // LLVM:define dso_local void @check_store
189 // LLVM: [[LOAD:%.*]] = load ptr, ptr {{.*}}, align 8
190 // LLVM: [[MEMBER:%.*]] = getelementptr %struct.st2, ptr [[LOAD]], i32 0, i32 0
191 // LLVM: [[LOADVOL:%.*]] = load volatile i16, ptr [[MEMBER]], align 8
192 // LLVM: [[CLEAR:%.*]] = and i16 [[LOADVOL]], -8
193 // LLVM: [[SET:%.*]] = or i16 [[CLEAR]], 1
194 // LLVM: store volatile i16 [[SET]], ptr [[MEMBER]], align 8
195 // LLVM: ret void
196
197 // OGCG: define dso_local void @check_store
198 // OGCG: [[LOAD:%.*]] = load ptr, ptr {{.*}}, align 8
199 // OGCG: [[LOADVOL:%.*]] = load volatile i16, ptr [[LOAD]], align 8
200 // OGCG: [[CLEAR:%.*]] = and i16 [[LOADVOL]], -8
201 // OGCG: [[SET:%.*]] = or i16 [[CLEAR]], 1
202 // OGCG: store volatile i16 [[SET]], ptr [[LOAD]], align 8
203 // OGCG: ret void
204
205 // this volatile bit-field container overlaps with a zero-length bit-field,
206 // so it may be accessed without using the container's width.
207 void check_store_exception(st3 *s3) {
208 s3->b = 2;
209 }
210
211 // CIR: cir.func {{.*}} @check_store_exception
212 // CIR: [[CONST:%.*]] = cir.const #cir.int<2> : !u32i
213 // CIR: [[LOAD:%.*]] = cir.load align(8) {{.*}} : !cir.ptr<!cir.ptr<!rec_st3>>, !cir.ptr<!rec_st3>
214 // CIR: [[MEMBER:%.*]] = cir.get_member [[LOAD]][2] {name = "b"} : !cir.ptr<!rec_st3> -> !cir.ptr<!u8i>
215 // CIR: [[SETBF:%.*]] = cir.set_bitfield align(4) (#bfi_b1, [[MEMBER]] : !cir.ptr<!u8i>, [[CONST]] : !u32i) {is_volatile} -> !u32i
216 // CIR: cir.return
217
218 // LLVM:define dso_local void @check_store_exception
219 // LLVM: [[LOAD:%.*]] = load ptr, ptr {{.*}}, align 8
220 // LLVM: [[MEMBER:%.*]] = getelementptr %struct.st3, ptr [[LOAD]], i32 0, i32 2
221 // LLVM: [[LOADVOL:%.*]] = load volatile i8, ptr [[MEMBER]], align 4
222 // LLVM: [[CLEAR:%.*]] = and i8 [[LOADVOL]], -32
223 // LLVM: [[SET:%.*]] = or i8 [[CLEAR]], 2
224 // LLVM: store volatile i8 [[SET]], ptr [[MEMBER]], align 4
225 // LLVM: ret void
226
227 // OGCG: define dso_local void @check_store_exception
228 // OGCG: [[LOAD:%.*]] = load ptr, ptr {{.*}}, align 8
229 // OGCG: [[MEMBER:%.*]] = getelementptr inbounds nuw %struct.st3, ptr [[LOAD]], i32 0, i32 2
230 // OGCG: [[LOADVOL:%.*]] = load volatile i8, ptr [[MEMBER]], align 4
231 // OGCG: [[CLEAR:%.*]] = and i8 [[LOADVOL]], -32
232 // OGCG: [[SET:%.*]] = or i8 [[CLEAR]], 2
233 // OGCG: store volatile i8 [[SET]], ptr [[MEMBER]], align 4
234 // OGCG: ret void
235
236 void clip_store_exception2(clip *c) {
237 c->a = 3;
238 }
239
240 // CIR: cir.func {{.*}} @clip_store_exception2
241 // CIR: [[CONST:%.*]] = cir.const #cir.int<3> : !s32i
242 // CIR: [[LOAD:%.*]] = cir.load align(8) {{.*}} : !cir.ptr<!cir.ptr<!rec_clip>>, !cir.ptr<!rec_clip>
243 // CIR: [[MEMBER:%.*]] = cir.get_member [[LOAD]][0] {name = "a"} : !cir.ptr<!rec_clip> -> !cir.ptr<!cir.array<!u8i x 3>>
244 // CIR: [[SETBF:%.*]] = cir.set_bitfield align(4) (#bfi_a1, [[MEMBER]] : !cir.ptr<!cir.array<!u8i x 3>>, [[CONST]] : !s32i) {is_volatile} -> !s32i
245 // CIR: cir.return
246
247 // LLVM:define dso_local void @clip_store_exception2
248 // LLVM: [[LOAD:%.*]] = load ptr, ptr {{.*}}, align 8
249 // LLVM: [[MEMBER:%.*]] = getelementptr %struct.clip, ptr [[LOAD]], i32 0, i32 0
250 // LLVM: store volatile i24 3, ptr [[MEMBER]], align 4
251 // LLVM: ret void
252
253 // OGCG: define dso_local void @clip_store_exception2
254 // OGCG: [[LOAD:%.*]] = load ptr, ptr {{.*}}, align 8
255 // OGCG: store volatile i24 3, ptr [[LOAD]], align 4
256 // OGCG: ret void
257
258 void check_store_second_member (st4 *s4) {
259 s4->b = 1;
260 }
261
262 // CIR: cir.func {{.*}} @check_store_second_member
263 // CIR: [[ONE:%.*]] = cir.const #cir.int<1> : !u64i
264 // CIR: [[LOAD:%.*]] = cir.load align(8) {{.*}} : !cir.ptr<!cir.ptr<!rec_st4>>, !cir.ptr<!rec_st4>
265 // CIR: [[MEMBER:%.*]] = cir.get_member [[LOAD]][2] {name = "b"} : !cir.ptr<!rec_st4> -> !cir.ptr<!u16i>
266 // CIR: cir.set_bitfield align(8) (#bfi_b2, [[MEMBER]] : !cir.ptr<!u16i>, [[ONE]] : !u64i) {is_volatile} -> !u64i
267
268 // LLVM: define dso_local void @check_store_second_member
269 // LLVM: [[LOAD:%.*]] = load ptr, ptr {{.*}}, align 8
270 // LLVM: [[MEMBER:%.*]] = getelementptr %struct.st4, ptr [[LOAD]], i32 0, i32 2
271 // LLVM: [[VAL:%.*]] = load volatile i64, ptr [[MEMBER]], align 8
272 // LLVM: [[CLEAR:%.*]] = and i64 [[VAL]], -65536
273 // LLVM: [[SET:%.*]] = or i64 [[CLEAR]], 1
274 // LLVM: store volatile i64 [[SET]], ptr [[MEMBER]], align 8
275
276 // OGCG: define dso_local void @check_store_second_member
277 // OGCG: [[LOAD:%.*]] = load ptr, ptr {{.*}}, align 8
278 // OGCG: [[MEMBER:%.*]] = getelementptr inbounds i64, ptr [[LOAD]], i64 1
279 // OGCG: [[LOADBF:%.*]] = load volatile i64, ptr [[MEMBER]], align 8
280 // OGCG: [[CLR:%.*]] = and i64 [[LOADBF]], -65536
281 // OGCG: [[SET:%.*]] = or i64 [[CLR]], 1
282 // OGCG: store volatile i64 [[SET]], ptr [[MEMBER]], align 8