Debian Codegraph @

clang/test/CIR/CodeGen/union.c

raw
1 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
2 // RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
3 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
4 // RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s
5 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll
6 // RUN: FileCheck --check-prefix=OGCG --input-file=%t.ll %s
7
8 union U1 {
9 int n;
10 char c;
11 };
12
13 // CIR: !rec_U1 = !cir.record<union "U1" {!s32i, !s8i}>
14 // LLVM: %union.U1 = type { i32 }
15 // OGCG: %union.U1 = type { i32 }
16
17 union U2 {
18 char b;
19 short s;
20 int i;
21 float f;
22 double d;
23 };
24
25 // CIR: !rec_U2 = !cir.record<union "U2" {!s8i, !s16i, !s32i, !cir.float, !cir.double}>
26 // LLVM: %union.U2 = type { double }
27 // OGCG: %union.U2 = type { double }
28
29 union U3 {
30 char c[5];
31 int i;
32 } __attribute__((packed));
33
34 // CIR: !rec_U3 = !cir.record<union "U3" packed padded {!cir.array<!s8i x 5>, !s32i, !u8i}>
35 // LLVM: %union.U3 = type <{ i32, i8 }>
36 // OGCG: %union.U3 = type <{ i32, i8 }>
37
38 union U4 {
39 char c[5];
40 int i;
41 };
42
43 // CIR: !rec_U4 = !cir.record<union "U4" padded {!cir.array<!s8i x 5>, !s32i, !cir.array<!u8i x 4>}>
44 // LLVM: %union.U4 = type { i32, [4 x i8] }
45 // OGCG: %union.U4 = type { i32, [4 x i8] }
46
47 union IncompleteU *p;
48
49 // CIR: cir.global external @p = #cir.ptr<null> : !cir.ptr<!rec_IncompleteU>
50 // LLVM: @p = global ptr null
51 // OGCG: @p = global ptr null, align 8
52
53 void f1(void) {
54 union IncompleteU *p;
55 }
56
57 // CIR: cir.func{{.*}} @f1()
58 // CIR-NEXT: cir.alloca !cir.ptr<!rec_IncompleteU>, !cir.ptr<!cir.ptr<!rec_IncompleteU>>, ["p"]
59 // CIR-NEXT: cir.return
60
61 // LLVM: define{{.*}} void @f1()
62 // LLVM-NEXT: %[[P:.*]] = alloca ptr, i64 1, align 8
63 // LLVM-NEXT: ret void
64
65 // OGCG: define{{.*}} void @f1()
66 // OGCG-NEXT: entry:
67 // OGCG-NEXT: %[[P:.*]] = alloca ptr, align 8
68 // OGCG-NEXT: ret void
69
70 int f2(void) {
71 union U1 u;
72 u.n = 42;
73 return u.n;
74 }
75
76 // CIR: cir.func{{.*}} @f2() -> !s32i
77 // CIR-NEXT: %[[RETVAL_ADDR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["__retval"] {alignment = 4 : i64}
78 // CIR-NEXT: %[[U:.*]] = cir.alloca !rec_U1, !cir.ptr<!rec_U1>, ["u"] {alignment = 4 : i64}
79 // CIR-NEXT: %[[I:.*]] = cir.const #cir.int<42> : !s32i
80 // CIR-NEXT: %[[N:.*]] = cir.get_member %[[U]][0] {name = "n"} : !cir.ptr<!rec_U1> -> !cir.ptr<!s32i>
81 // CIR-NEXT: cir.store{{.*}} %[[I]], %[[N]] : !s32i, !cir.ptr<!s32i>
82 // CIR-NEXT: %[[N2:.*]] = cir.get_member %[[U]][0] {name = "n"} : !cir.ptr<!rec_U1> -> !cir.ptr<!s32i>
83 // CIR-NEXT: %[[VAL:.*]] = cir.load{{.*}} %[[N2]] : !cir.ptr<!s32i>, !s32i
84 // CIR-NEXT: cir.store{{.*}} %[[VAL]], %[[RETVAL_ADDR]] : !s32i, !cir.ptr<!s32i>
85 // CIR-NEXT: %[[RET:.*]] = cir.load{{.*}} %[[RETVAL_ADDR]] : !cir.ptr<!s32i>, !s32i
86 // CIR-NEXT: cir.return %[[RET]] : !s32i
87
88 // LLVM: define{{.*}} i32 @f2()
89 // LLVM-NEXT: %[[RETVAL:.*]] = alloca i32, i64 1, align 4
90 // LLVM-NEXT: %[[U:.*]] = alloca %union.U1, i64 1, align 4
91 // LLVM-NEXT: store i32 42, ptr %[[U]], align 4
92 // LLVM-NEXT: %[[N_VAL:.*]] = load i32, ptr %[[U]], align 4
93 // LLVM-NEXT: store i32 %[[N_VAL]], ptr %[[RETVAL]], align 4
94 // LLVM-NEXT: %[[RET:.*]] = load i32, ptr %[[RETVAL]], align 4
95 // LLVM-NEXT: ret i32 %[[RET]]
96
97 // OGCG: define{{.*}} i32 @f2()
98 // OGCG-NEXT: entry:
99 // OGCG-NEXT: %[[U:.*]] = alloca %union.U1, align 4
100 // OGCG-NEXT: store i32 42, ptr %[[U]], align 4
101 // OGCG-NEXT: %[[N_VAL:.*]] = load i32, ptr %[[U]], align 4
102 // OGCG-NEXT: ret i32 %[[N_VAL]]
103
104 void shouldGenerateUnionAccess(union U2 u) {
105 u.b = 0;
106 u.b;
107 u.i = 1;
108 u.i;
109 u.f = 0.1F;
110 u.f;
111 u.d = 0.1;
112 u.d;
113 }
114
115 // CIR: cir.func{{.*}} @shouldGenerateUnionAccess(%[[ARG:.*]]: !rec_U2
116 // CIR-NEXT: %[[U:.*]] = cir.alloca !rec_U2, !cir.ptr<!rec_U2>, ["u", init] {alignment = 8 : i64}
117 // CIR-NEXT: cir.store{{.*}} %[[ARG]], %[[U]] : !rec_U2, !cir.ptr<!rec_U2>
118 // CIR-NEXT: %[[ZERO:.*]] = cir.const #cir.int<0> : !s8i
119 // CIR-NEXT: %[[B_PTR:.*]] = cir.get_member %[[U]][0] {name = "b"} : !cir.ptr<!rec_U2> -> !cir.ptr<!s8i>
120 // CIR-NEXT: cir.store{{.*}} %[[ZERO]], %[[B_PTR]] : !s8i, !cir.ptr<!s8i>
121 // CIR-NEXT: %[[B_PTR2:.*]] = cir.get_member %[[U]][0] {name = "b"} : !cir.ptr<!rec_U2> -> !cir.ptr<!s8i>
122 // CIR-NEXT: %[[B_VAL:.*]] = cir.load{{.*}} %[[B_PTR2]] : !cir.ptr<!s8i>, !s8i
123 // CIR-NEXT: %[[ONE:.*]] = cir.const #cir.int<1> : !s32i
124 // CIR-NEXT: %[[I_PTR:.*]] = cir.get_member %[[U]][2] {name = "i"} : !cir.ptr<!rec_U2> -> !cir.ptr<!s32i>
125 // CIR-NEXT: cir.store{{.*}} %[[ONE]], %[[I_PTR]] : !s32i, !cir.ptr<!s32i>
126 // CIR-NEXT: %[[I_PTR2:.*]] = cir.get_member %[[U]][2] {name = "i"} : !cir.ptr<!rec_U2> -> !cir.ptr<!s32i>
127 // CIR-NEXT: %[[I_VAL:.*]] = cir.load{{.*}} %[[I_PTR2]] : !cir.ptr<!s32i>, !s32i
128 // CIR-NEXT: %[[FLOAT_VAL:.*]] = cir.const #cir.fp<1.000000e-01> : !cir.float
129 // CIR-NEXT: %[[F_PTR:.*]] = cir.get_member %[[U]][3] {name = "f"} : !cir.ptr<!rec_U2> -> !cir.ptr<!cir.float>
130 // CIR-NEXT: cir.store{{.*}} %[[FLOAT_VAL]], %[[F_PTR]] : !cir.float, !cir.ptr<!cir.float>
131 // CIR-NEXT: %[[F_PTR2:.*]] = cir.get_member %[[U]][3] {name = "f"} : !cir.ptr<!rec_U2> -> !cir.ptr<!cir.float>
132 // CIR-NEXT: %[[F_VAL:.*]] = cir.load{{.*}} %[[F_PTR2]] : !cir.ptr<!cir.float>, !cir.float
133 // CIR-NEXT: %[[DOUBLE_VAL:.*]] = cir.const #cir.fp<1.000000e-01> : !cir.double
134 // CIR-NEXT: %[[D_PTR:.*]] = cir.get_member %[[U]][4] {name = "d"} : !cir.ptr<!rec_U2> -> !cir.ptr<!cir.double>
135 // CIR-NEXT: cir.store{{.*}} %[[DOUBLE_VAL]], %[[D_PTR]] : !cir.double, !cir.ptr<!cir.double>
136 // CIR-NEXT: %[[D_PTR2:.*]] = cir.get_member %[[U]][4] {name = "d"} : !cir.ptr<!rec_U2> -> !cir.ptr<!cir.double>
137 // CIR-NEXT: %[[D_VAL:.*]] = cir.load{{.*}} %[[D_PTR2]] : !cir.ptr<!cir.double>, !cir.double
138 // CIR-NEXT: cir.return
139
140 // LLVM: define{{.*}} void @shouldGenerateUnionAccess(%union.U2 %[[ARG:.*]])
141 // LLVM-NEXT: %[[U:.*]] = alloca %union.U2, i64 1, align 8
142 // LLVM-NEXT: store %union.U2 %[[ARG]], ptr %[[U]], align 8
143 // LLVM-NEXT: store i8 0, ptr %[[U]], align 8
144 // LLVM-NEXT: %[[B_VAL:.*]] = load i8, ptr %[[U]], align 8
145 // LLVM-NEXT: store i32 1, ptr %[[U]], align 8
146 // LLVM-NEXT: %[[I_VAL:.*]] = load i32, ptr %[[U]], align 8
147 // LLVM-NEXT: store float 0x3FB99999A0000000, ptr %[[U]], align 8
148 // LLVM-NEXT: %[[F_VAL:.*]] = load float, ptr %[[U]], align 8
149 // LLVM-NEXT: store double 1.000000e-01, ptr %[[U]], align 8
150 // LLVM-NEXT: %[[D_VAL:.*]] = load double, ptr %[[U]], align 8
151 // LLVM-NEXT: ret void
152
153 // OGCG: define{{.*}} void @shouldGenerateUnionAccess(i64 %[[ARG:.*]])
154 // OGCG-NEXT: entry:
155 // OGCG-NEXT: %[[U:.*]] = alloca %union.U2, align 8
156 // OGCG-NEXT: %[[COERCE_DIVE:.*]] = getelementptr inbounds nuw %union.U2, ptr %[[U]], i32 0, i32 0
157 // OGCG-NEXT: store i64 %[[ARG]], ptr %[[COERCE_DIVE]], align 8
158 // OGCG-NEXT: store i8 0, ptr %[[U]], align 8
159 // OGCG-NEXT: %[[B_VAL:.*]] = load i8, ptr %[[U]], align 8
160 // OGCG-NEXT: store i32 1, ptr %[[U]], align 8
161 // OGCG-NEXT: %[[I_VAL:.*]] = load i32, ptr %[[U]], align 8
162 // OGCG-NEXT: store float 0x3FB99999A0000000, ptr %[[U]], align 8
163 // OGCG-NEXT: %[[F_VAL:.*]] = load float, ptr %[[U]], align 8
164 // OGCG-NEXT: store double 1.000000e-01, ptr %[[U]], align 8
165 // OGCG-NEXT: %[[D_VAL:.*]] = load double, ptr %[[U]], align 8
166 // OGCG-NEXT: ret void
167
168 void f3(union U3 u) {
169 u.c[2] = 0;
170 }
171
172 // CIR: cir.func{{.*}} @f3(%[[ARG:.*]]: !rec_U3
173 // CIR-NEXT: %[[U:.*]] = cir.alloca !rec_U3, !cir.ptr<!rec_U3>, ["u", init] {alignment = 1 : i64}
174 // CIR-NEXT: cir.store{{.*}} %[[ARG]], %[[U]] : !rec_U3, !cir.ptr<!rec_U3>
175 // CIR-NEXT: %[[ZERO:.*]] = cir.const #cir.int<0> : !s8i
176 // CIR-NEXT: %[[IDX:.*]] = cir.const #cir.int<2> : !s32i
177 // CIR-NEXT: %[[C_PTR:.*]] = cir.get_member %[[U]][0] {name = "c"} : !cir.ptr<!rec_U3> -> !cir.ptr<!cir.array<!s8i x 5>>
178 // CIR-NEXT: %[[ELEM_PTR:.*]] = cir.get_element %[[C_PTR]][%[[IDX]] : !s32i] : !cir.ptr<!cir.array<!s8i x 5>> -> !cir.ptr<!s8i>
179 // CIR-NEXT: cir.store{{.*}} %[[ZERO]], %[[ELEM_PTR]] : !s8i, !cir.ptr<!s8i>
180 // CIR-NEXT: cir.return
181
182 // LLVM: define{{.*}} void @f3(%union.U3 %[[ARG:.*]])
183 // LLVM-NEXT: %[[U:.*]] = alloca %union.U3, i64 1, align 1
184 // LLVM-NEXT: store %union.U3 %[[ARG]], ptr %[[U]], align 1
185 // LLVM-NEXT: %[[ELEM_PTR:.*]] = getelementptr [5 x i8], ptr %[[U]], i32 0, i64 2
186 // LLVM-NEXT: store i8 0, ptr %[[ELEM_PTR]], align 1
187 // LLVM-NEXT: ret void
188
189 // OGCG: define{{.*}} void @f3(i40 %[[ARG:.*]])
190 // OGCG-NEXT: entry:
191 // OGCG-NEXT: %[[U:.*]] = alloca %union.U3, align 1
192 // OGCG-NEXT: store i40 %[[ARG]], ptr %[[U]], align 1
193 // OGCG-NEXT: %[[ARRAYIDX:.*]] = getelementptr inbounds [5 x i8], ptr %[[U]], i64 0, i64 2
194 // OGCG-NEXT: store i8 0, ptr %[[ARRAYIDX]], align 1
195 // OGCG-NEXT: ret void
196
197 void f5(union U4 u) {
198 u.c[4] = 65;
199 }
200
201 // CIR: cir.func{{.*}} @f5(%[[ARG:.*]]: !rec_U4
202 // CIR-NEXT: %[[U:.*]] = cir.alloca !rec_U4, !cir.ptr<!rec_U4>, ["u", init] {alignment = 4 : i64}
203 // CIR-NEXT: cir.store{{.*}} %[[ARG]], %[[U]] : !rec_U4, !cir.ptr<!rec_U4>
204 // CIR-NEXT: %[[CHAR_VAL:.*]] = cir.const #cir.int<65> : !s8i
205 // CIR-NEXT: %[[IDX:.*]] = cir.const #cir.int<4> : !s32i
206 // CIR-NEXT: %[[C_PTR:.*]] = cir.get_member %[[U]][0] {name = "c"} : !cir.ptr<!rec_U4> -> !cir.ptr<!cir.array<!s8i x 5>>
207 // CIR-NEXT: %[[ELEM_PTR:.*]] = cir.get_element %[[C_PTR]][%[[IDX]] : !s32i] : !cir.ptr<!cir.array<!s8i x 5>> -> !cir.ptr<!s8i>
208 // CIR-NEXT: cir.store{{.*}} %[[CHAR_VAL]], %[[ELEM_PTR]] : !s8i, !cir.ptr<!s8i>
209 // CIR-NEXT: cir.return
210
211 // LLVM: define{{.*}} void @f5(%union.U4 %[[ARG:.*]])
212 // LLVM-NEXT: %[[U:.*]] = alloca %union.U4, i64 1, align 4
213 // LLVM-NEXT: store %union.U4 %[[ARG]], ptr %[[U]], align 4
214 // LLVM-NEXT: %[[ELEM_PTR:.*]] = getelementptr [5 x i8], ptr %[[U]], i32 0, i64 4
215 // LLVM-NEXT: store i8 65, ptr %[[ELEM_PTR]], align 4
216 // LLVM-NEXT: ret void
217
218 // OGCG: define{{.*}} void @f5(i64 %[[ARG:.*]])
219 // OGCG-NEXT: entry:
220 // OGCG-NEXT: %[[U:.*]] = alloca %union.U4, align 4
221 // OGCG-NEXT: store i64 %[[ARG]], ptr %[[U]], align 4
222 // OGCG-NEXT: %[[ARRAYIDX:.*]] = getelementptr inbounds [5 x i8], ptr %[[U]], i64 0, i64 4
223 // OGCG-NEXT: store i8 65, ptr %[[ARRAYIDX]], align 4
224 // OGCG-NEXT: ret void