Debian Codegraph @

clang/test/CIR/CodeGen/struct-init.cpp

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 struct BitfieldStruct {
9 unsigned int a:4;
10 unsigned int b:14;
11 unsigned int c:14;
12 };
13
14 BitfieldStruct overlapping_init = { 3, 2, 1 };
15
16 // This is unintuitive. The bitfields are initialized using a struct of constants
17 // that maps to the bitfields but splits the value into bytes.
18
19 // CIR: cir.global external @overlapping_init = #cir.const_record<{#cir.int<35> : !u8i, #cir.int<0> : !u8i, #cir.int<4> : !u8i, #cir.int<0> : !u8i}> : !rec_anon_struct
20 // LLVM: @overlapping_init = global { i8, i8, i8, i8 } { i8 35, i8 0, i8 4, i8 0 }
21 // OGCG: @overlapping_init = global { i8, i8, i8, i8 } { i8 35, i8 0, i8 4, i8 0 }
22
23 struct S {
24 int a, b, c;
25 };
26
27 S partial_init = { 1 };
28
29 // CIR: cir.global external @partial_init = #cir.const_record<{#cir.int<1> : !s32i, #cir.int<0> : !s32i, #cir.int<0> : !s32i}> : !rec_S
30 // LLVM: @partial_init = global %struct.S { i32 1, i32 0, i32 0 }
31 // OGCG: @partial_init = global %struct.S { i32 1, i32 0, i32 0 }
32
33 struct StructWithDefaultInit {
34 int a = 2;
35 };
36
37 StructWithDefaultInit swdi = {};
38
39 // CIR: cir.global external @swdi = #cir.const_record<{#cir.int<2> : !s32i}> : !rec_StructWithDefaultInit
40 // LLVM: @swdi = global %struct.StructWithDefaultInit { i32 2 }, align 4
41 // OGCG: @swdi = global %struct.StructWithDefaultInit { i32 2 }, align 4
42
43 struct StructWithFieldInitFromConst {
44 int a : 10;
45 int b = a;
46 };
47
48 StructWithFieldInitFromConst swfifc = {};
49
50 // CIR: cir.global external @swfifc = #cir.zero : !rec_anon_struct
51 // LLVM: @swfifc = global { i8, i8, i32 } zeroinitializer, align 4
52 // OGCG: @swfifc = global { i8, i8, i32 } zeroinitializer, align 4
53
54 StructWithFieldInitFromConst swfifc2 = { 2 };
55
56 // CIR: cir.global external @swfifc2 = #cir.const_record<{#cir.int<2> : !u8i, #cir.int<0> : !u8i, #cir.int<2> : !s32i}> : !rec_anon_struct
57 // LLVM: @swfifc2 = global { i8, i8, i32 } { i8 2, i8 0, i32 2 }, align 4
58 // OGCG: @swfifc2 = global { i8, i8, i32 } { i8 2, i8 0, i32 2 }, align 4
59
60 void init() {
61 S s1 = {1, 2, 3};
62 S s2 = {4, 5};
63 }
64
65 // CIR: cir.func{{.*}} @_Z4initv()
66 // CIR: %[[S1:.*]] = cir.alloca !rec_S, !cir.ptr<!rec_S>, ["s1", init]
67 // CIR: %[[S2:.*]] = cir.alloca !rec_S, !cir.ptr<!rec_S>, ["s2", init]
68 // CIR: %[[CONST_1:.*]] = cir.const #cir.const_record<{#cir.int<1> : !s32i, #cir.int<2> : !s32i, #cir.int<3> : !s32i}> : !rec_S
69 // CIR: cir.store{{.*}} %[[CONST_1]], %[[S1]]
70 // CIR: %[[CONST_2:.*]] = cir.const #cir.const_record<{#cir.int<4> : !s32i, #cir.int<5> : !s32i, #cir.int<0> : !s32i}> : !rec_S
71 // CIR: cir.store{{.*}} %[[CONST_2]], %[[S2]]
72
73 // LLVM: define{{.*}} void @_Z4initv()
74 // LLVM: %[[S1:.*]] = alloca %struct.S
75 // LLVM: %[[S2:.*]] = alloca %struct.S
76 // LLVM: store %struct.S { i32 1, i32 2, i32 3 }, ptr %[[S1]], align 4
77 // LLVM: store %struct.S { i32 4, i32 5, i32 0 }, ptr %[[S2]], align 4
78
79 // OGCG: @__const._Z4initv.s1 = private unnamed_addr constant %struct.S { i32 1, i32 2, i32 3 }
80 // OGCG: @__const._Z4initv.s2 = private unnamed_addr constant %struct.S { i32 4, i32 5, i32 0 }
81
82 // OGCG: define{{.*}} void @_Z4initv()
83 // OGCG: %[[S1:.*]] = alloca %struct.S
84 // OGCG: %[[S2:.*]] = alloca %struct.S
85 // OGCG: call void @llvm.memcpy.p0.p0.i64(ptr{{.*}} %[[S1]], ptr{{.*}} @__const._Z4initv.s1, i64 12, i1 false)
86 // OGCG: call void @llvm.memcpy.p0.p0.i64(ptr{{.*}} %[[S2]], ptr{{.*}} @__const._Z4initv.s2, i64 12, i1 false)
87
88 void init_var(int a, int b) {
89 S s = {a, b};
90 }
91
92 // CIR: cir.func{{.*}} @_Z8init_varii(%[[A_ARG:.*]]: !s32i {{.*}}, %[[B_ARG:.*]]: !s32i {{.*}})
93 // CIR: %[[A_PTR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["a", init]
94 // CIR: %[[B_PTR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["b", init]
95 // CIR: %[[S:.*]] = cir.alloca !rec_S, !cir.ptr<!rec_S>, ["s", init]
96 // CIR: cir.store{{.*}} %[[A_ARG]], %[[A_PTR]]
97 // CIR: cir.store{{.*}} %[[B_ARG]], %[[B_PTR]]
98 // CIR: %[[S_A:.*]] = cir.get_member %[[S]][0] {name = "a"}
99 // CIR: %[[A:.*]] = cir.load{{.*}} %[[A_PTR]]
100 // CIR: cir.store{{.*}} %[[A]], %[[S_A]]
101 // CIR: %[[S_B:.*]] = cir.get_member %[[S]][1] {name = "b"}
102 // CIR: %[[B:.*]] = cir.load{{.*}} %[[B_PTR]]
103 // CIR: cir.store{{.*}} %[[B]], %[[S_B]]
104 // CIR: cir.return
105
106 // LLVM: define{{.*}} void @_Z8init_varii(i32 %[[A_ARG:.*]], i32 %[[B_ARG:.*]])
107 // LLVM: %[[A_PTR:.*]] = alloca i32
108 // LLVM: %[[B_PTR:.*]] = alloca i32
109 // LLVM: %[[S:.*]] = alloca %struct.S
110 // LLVM: store i32 %[[A_ARG]], ptr %[[A_PTR]]
111 // LLVM: store i32 %[[B_ARG]], ptr %[[B_PTR]]
112 // LLVM: %[[S_A:.*]] = getelementptr %struct.S, ptr %[[S]], i32 0, i32 0
113 // LLVM: %[[A:.*]] = load i32, ptr %[[A_PTR]]
114 // LLVM: store i32 %[[A]], ptr %[[S_A]]
115 // LLVM: %[[S_B:.*]] = getelementptr %struct.S, ptr %[[S]], i32 0, i32 1
116 // LLVM: %[[B:.*]] = load i32, ptr %[[B_PTR]]
117 // LLVM: store i32 %[[B]], ptr %[[S_B]]
118 // LLVM: ret void
119
120 // OGCG: define{{.*}} void @_Z8init_varii(i32 {{.*}} %[[A_ARG:.*]], i32 {{.*}} %[[B_ARG:.*]])
121 // OGCG: %[[A_PTR:.*]] = alloca i32
122 // OGCG: %[[B_PTR:.*]] = alloca i32
123 // OGCG: %[[S:.*]] = alloca %struct.S
124 // OGCG: store i32 %[[A_ARG]], ptr %[[A_PTR]]
125 // OGCG: store i32 %[[B_ARG]], ptr %[[B_PTR]]
126 // OGCG: %[[S_A:.*]] = getelementptr {{.*}} %struct.S, ptr %[[S]], i32 0, i32 0
127 // OGCG: %[[A:.*]] = load i32, ptr %[[A_PTR]]
128 // OGCG: store i32 %[[A]], ptr %[[S_A]]
129 // OGCG: %[[S_B:.*]] = getelementptr {{.*}} %struct.S, ptr %[[S]], i32 0, i32 1
130 // OGCG: %[[B:.*]] = load i32, ptr %[[B_PTR]]
131 // OGCG: store i32 %[[B]], ptr %[[S_B]]
132 // OGCG: %[[S_C:.*]] = getelementptr {{.*}} %struct.S, ptr %[[S]], i32 0, i32 2
133 // OGCG: store i32 0, ptr %[[S_C]]
134 // OGCG: ret void
135
136 void init_expr(int a, int b, int c) {
137 S s = {a + 1, b + 2, c + 3};
138 }
139
140 // CIR: cir.func{{.*}} @_Z9init_expriii(%[[A_ARG:.*]]: !s32i {{.*}}, %[[B_ARG:.*]]: !s32i {{.*}}, %[[C_ARG:.*]]: !s32i {{.*}})
141 // CIR: %[[A_PTR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["a", init]
142 // CIR: %[[B_PTR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["b", init]
143 // CIR: %[[C_PTR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["c", init]
144 // CIR: %[[S:.*]] = cir.alloca !rec_S, !cir.ptr<!rec_S>, ["s", init]
145 // CIR: cir.store{{.*}} %[[A_ARG]], %[[A_PTR]]
146 // CIR: cir.store{{.*}} %[[B_ARG]], %[[B_PTR]]
147 // CIR: cir.store{{.*}} %[[C_ARG]], %[[C_PTR]]
148 // CIR: %[[S_A:.*]] = cir.get_member %[[S]][0] {name = "a"}
149 // CIR: %[[A:.*]] = cir.load{{.*}} %[[A_PTR]]
150 // CIR: %[[ONE:.*]] = cir.const #cir.int<1>
151 // CIR: %[[A_PLUS_ONE:.*]] = cir.binop(add, %[[A]], %[[ONE]])
152 // CIR: cir.store{{.*}} %[[A_PLUS_ONE]], %[[S_A]]
153 // CIR: %[[S_B:.*]] = cir.get_member %[[S]][1] {name = "b"}
154 // CIR: %[[B:.*]] = cir.load{{.*}} %[[B_PTR]]
155 // CIR: %[[TWO:.*]] = cir.const #cir.int<2>
156 // CIR: %[[B_PLUS_TWO:.*]] = cir.binop(add, %[[B]], %[[TWO]]) nsw : !s32i
157 // CIR: cir.store{{.*}} %[[B_PLUS_TWO]], %[[S_B]]
158 // CIR: %[[S_C:.*]] = cir.get_member %[[S]][2] {name = "c"}
159 // CIR: %[[C:.*]] = cir.load{{.*}} %[[C_PTR]]
160 // CIR: %[[THREE:.*]] = cir.const #cir.int<3>
161 // CIR: %[[C_PLUS_THREE:.*]] = cir.binop(add, %[[C]], %[[THREE]]) nsw : !s32i
162 // CIR: cir.store{{.*}} %[[C_PLUS_THREE]], %[[S_C]]
163 // CIR: cir.return
164
165 // LLVM: define{{.*}} void @_Z9init_expriii(i32 %[[A_ARG:.*]], i32 %[[B_ARG:.*]], i32 %[[C_ARG:.*]])
166 // LLVM: %[[A_PTR:.*]] = alloca i32
167 // LLVM: %[[B_PTR:.*]] = alloca i32
168 // LLVM: %[[C_PTR:.*]] = alloca i32
169 // LLVM: %[[S:.*]] = alloca %struct.S
170 // LLVM: store i32 %[[A_ARG]], ptr %[[A_PTR]]
171 // LLVM: store i32 %[[B_ARG]], ptr %[[B_PTR]]
172 // LLVM: store i32 %[[C_ARG]], ptr %[[C_PTR]]
173 // LLVM: %[[S_A:.*]] = getelementptr %struct.S, ptr %[[S]], i32 0, i32 0
174 // LLVM: %[[A:.*]] = load i32, ptr %[[A_PTR]]
175 // LLVM: %[[A_PLUS_ONE:.*]] = add nsw i32 %[[A]], 1
176 // LLVM: store i32 %[[A_PLUS_ONE]], ptr %[[S_A]]
177 // LLVM: %[[S_B:.*]] = getelementptr %struct.S, ptr %[[S]], i32 0, i32 1
178 // LLVM: %[[B:.*]] = load i32, ptr %[[B_PTR]]
179 // LLVM: %[[B_PLUS_TWO:.*]] = add nsw i32 %[[B]], 2
180 // LLVM: store i32 %[[B_PLUS_TWO]], ptr %[[S_B]]
181 // LLVM: %[[S_C:.*]] = getelementptr %struct.S, ptr %[[S]], i32 0, i32 2
182 // LLVM: %[[C:.*]] = load i32, ptr %[[C_PTR]]
183 // LLVM: %[[C_PLUS_THREE:.*]] = add nsw i32 %[[C]], 3
184 // LLVM: store i32 %[[C_PLUS_THREE]], ptr %[[S_C]]
185 // LLVM: ret void
186
187 // OGCG: define{{.*}} void @_Z9init_expriii(i32 {{.*}} %[[A_ARG:.*]], i32 {{.*}} %[[B_ARG:.*]], i32 {{.*}} %[[C_ARG:.*]])
188 // OGCG: %[[A_PTR:.*]] = alloca i32
189 // OGCG: %[[B_PTR:.*]] = alloca i32
190 // OGCG: %[[C_PTR:.*]] = alloca i32
191 // OGCG: %[[S:.*]] = alloca %struct.S
192 // OGCG: store i32 %[[A_ARG]], ptr %[[A_PTR]]
193 // OGCG: store i32 %[[B_ARG]], ptr %[[B_PTR]]
194 // OGCG: store i32 %[[C_ARG]], ptr %[[C_PTR]]
195 // OGCG: %[[S_A:.*]] = getelementptr {{.*}} %struct.S, ptr %[[S]], i32 0, i32 0
196 // OGCG: %[[A:.*]] = load i32, ptr %[[A_PTR]]
197 // OGCG: %[[A_PLUS_ONE:.*]] = add nsw i32 %[[A]], 1
198 // OGCG: store i32 %[[A_PLUS_ONE]], ptr %[[S_A]]
199 // OGCG: %[[S_B:.*]] = getelementptr {{.*}} %struct.S, ptr %[[S]], i32 0, i32 1
200 // OGCG: %[[B:.*]] = load i32, ptr %[[B_PTR]]
201 // OGCG: %[[B_PLUS_TWO:.*]] = add nsw i32 %[[B]], 2
202 // OGCG: store i32 %[[B_PLUS_TWO]], ptr %[[S_B]]
203 // OGCG: %[[S_C:.*]] = getelementptr {{.*}} %struct.S, ptr %[[S]], i32 0, i32 2
204 // OGCG: %[[C:.*]] = load i32, ptr %[[C_PTR]]
205 // OGCG: %[[C_PLUS_THREE:.*]] = add nsw i32 %[[C]], 3
206 // OGCG: store i32 %[[C_PLUS_THREE]], ptr %[[S_C]]
207 // OGCG: ret void
208
209 void cxx_default_init_with_struct_field() {
210 struct Parent {
211 int getA();
212 int a = getA();
213 };
214 Parent p = Parent{};
215 }
216
217 // CIR: %[[P_ADDR:.*]] = cir.alloca !rec_Parent, !cir.ptr<!rec_Parent>, ["p", init]
218 // CIR: %[[P_ELEM_0_PTR:.*]] = cir.get_member %[[P_ADDR]][0] {name = "a"} : !cir.ptr<!rec_Parent> -> !cir.ptr<!s32i>
219 // CIR: %[[METHOD_CALL:.*]] = cir.call @_ZZ34cxx_default_init_with_struct_fieldvEN6Parent4getAEv(%[[P_ADDR]]) : (!cir.ptr<!rec_Parent>) -> !s32i
220 // CIR: cir.store{{.*}} %[[METHOD_CALL]], %[[P_ELEM_0_PTR]] : !s32i, !cir.ptr<!s32i>
221
222 // LLVM: %[[P_ADDR:.*]] = alloca %struct.Parent, i64 1, align 4
223 // LLVM: %[[P_ELEM_0_PTR:.*]] = getelementptr %struct.Parent, ptr %[[P_ADDR]], i32 0, i32 0
224 // LLVM: %[[METHOD_CALL:.*]] = call i32 @_ZZ34cxx_default_init_with_struct_fieldvEN6Parent4getAEv(ptr %[[P_ADDR]])
225 // LLVM: store i32 %[[METHOD_CALL]], ptr %[[P_ELEM_0_PTR]], align 4
226
227 // OGCG: %[[P_ADDR:.*]] = alloca %struct.Parent, align 4
228 // OGCG: %[[P_ELEM_0_PTR:.*]] = getelementptr inbounds nuw %struct.Parent, ptr %[[P_ADDR]], i32 0, i32 0
229 // OGCG: %[[METHOD_CALL:.*]] = call noundef i32 @_ZZ34cxx_default_init_with_struct_fieldvEN6Parent4getAEv(ptr {{.*}} %[[P_ADDR]])
230 // OGCG: store i32 %[[METHOD_CALL]], ptr %[[P_ELEM_0_PTR]], align 4