aboutsummaryrefslogtreecommitdiff
path: root/clang/test/CIR/CodeGen/struct-init.cpp
blob: 63e13dd70839214f48e73e4312d6bf3f7ab28d59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
// RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll
// RUN: FileCheck --check-prefix=OGCG --input-file=%t.ll %s

struct BitfieldStruct {
  unsigned int a:4;
  unsigned int b:14;
  unsigned int c:14;
};

BitfieldStruct overlapping_init = { 3, 2, 1 };

// This is unintuitive. The bitfields are initialized using a struct of constants
// that maps to the bitfields but splits the value into bytes.

// 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
// LLVM: @overlapping_init = global { i8, i8, i8, i8 } { i8 35, i8 0, i8 4, i8 0 }
// OGCG: @overlapping_init = global { i8, i8, i8, i8 } { i8 35, i8 0, i8 4, i8 0 }

struct S {
  int a, b, c;
};

S partial_init = { 1 };

// CIR: cir.global external @partial_init = #cir.const_record<{#cir.int<1> : !s32i, #cir.int<0> : !s32i, #cir.int<0> : !s32i}> : !rec_S
// LLVM: @partial_init = global %struct.S { i32 1, i32 0, i32 0 }
// OGCG: @partial_init = global %struct.S { i32 1, i32 0, i32 0 }

struct StructWithDefaultInit {
  int a = 2;
};

StructWithDefaultInit swdi = {};

// CIR: cir.global external @swdi = #cir.const_record<{#cir.int<2> : !s32i}> : !rec_StructWithDefaultInit
// LLVM: @swdi = global %struct.StructWithDefaultInit { i32 2 }, align 4
// OGCG: @swdi = global %struct.StructWithDefaultInit { i32 2 }, align 4

void init() {
  S s1 = {1, 2, 3};
  S s2 = {4, 5};
}

// CIR: cir.func{{.*}} @_Z4initv()
// CIR:   %[[S1:.*]] = cir.alloca !rec_S, !cir.ptr<!rec_S>, ["s1", init]
// CIR:   %[[S2:.*]] = cir.alloca !rec_S, !cir.ptr<!rec_S>, ["s2", init]
// CIR:   %[[S1_A:.*]] = cir.get_member %[[S1]][0] {name = "a"}
// CIR:   %[[ONE:.*]] = cir.const #cir.int<1>
// CIR:   cir.store{{.*}} %[[ONE]], %[[S1_A]]
// CIR:   %[[S1_B:.*]] = cir.get_member %[[S1]][1] {name = "b"}
// CIR:   %[[TWO:.*]] = cir.const #cir.int<2>
// CIR:   cir.store{{.*}} %[[TWO]], %[[S1_B]]
// CIR:   %[[S1_C:.*]] = cir.get_member %[[S1]][2] {name = "c"}
// CIR:   %[[THREE:.*]] = cir.const #cir.int<3>
// CIR:   cir.store{{.*}} %[[THREE]], %[[S1_C]]
// CIR:   %[[S2_A:.*]] = cir.get_member %[[S2]][0] {name = "a"}
// CIR:   %[[FOUR:.*]] = cir.const #cir.int<4>
// CIR:   cir.store{{.*}} %[[FOUR]], %[[S2_A]]
// CIR:   %[[S2_B:.*]] = cir.get_member %[[S2]][1] {name = "b"}
// CIR:   %[[FIVE:.*]] = cir.const #cir.int<5>
// CIR:   cir.store{{.*}} %[[FIVE]], %[[S2_B]]
// CIR:   %[[S2_C:.*]] = cir.get_member %[[S2]][2] {name = "c"}
// CIR:   %[[ZERO:.*]] = cir.const #cir.int<0>
// CIR:   cir.store{{.*}} %[[ZERO]], %[[S2_C]]
// CIR:   cir.return

// LLVM: define{{.*}} void @_Z4initv()
// LLVM:   %[[S1:.*]] = alloca %struct.S
// LLVM:   %[[S2:.*]] = alloca %struct.S
// LLVM:   %[[S1_A:.*]] = getelementptr %struct.S, ptr %[[S1]], i32 0, i32 0
// LLVM:   store i32 1, ptr %[[S1_A]]
// LLVM:   %[[S1_B:.*]] = getelementptr %struct.S, ptr %[[S1]], i32 0, i32 1
// LLVM:   store i32 2, ptr %[[S1_B]]
// LLVM:   %[[S1_C:.*]] = getelementptr %struct.S, ptr %[[S1]], i32 0, i32 2
// LLVM:   store i32 3, ptr %[[S1_C]]
// LLVM:   %[[S2_A:.*]] = getelementptr %struct.S, ptr %[[S2]], i32 0, i32 0
// LLVM:   store i32 4, ptr %[[S2_A]]
// LLVM:   %[[S2_B:.*]] = getelementptr %struct.S, ptr %[[S2]], i32 0, i32 1
// LLVM:   store i32 5, ptr %[[S2_B]]
// LLVM:   %[[S2_C:.*]] = getelementptr %struct.S, ptr %[[S2]], i32 0, i32 2
// LLVM:   store i32 0, ptr %[[S2_C]]

// OGCG: @__const._Z4initv.s1 = private unnamed_addr constant %struct.S { i32 1, i32 2, i32 3 }
// OGCG: @__const._Z4initv.s2 = private unnamed_addr constant %struct.S { i32 4, i32 5, i32 0 }

// OGCG: define{{.*}} void @_Z4initv()
// OGCG:   %[[S1:.*]] = alloca %struct.S
// OGCG:   %[[S2:.*]] = alloca %struct.S
// OGCG:   call void @llvm.memcpy.p0.p0.i64(ptr{{.*}} %[[S1]], ptr{{.*}} @__const._Z4initv.s1, i64 12, i1 false)
// OGCG:   call void @llvm.memcpy.p0.p0.i64(ptr{{.*}} %[[S2]], ptr{{.*}} @__const._Z4initv.s2, i64 12, i1 false)

void init_var(int a, int b) {
  S s = {a, b};
}

// CIR: cir.func{{.*}} @_Z8init_varii(%[[A_ARG:.*]]: !s32i {{.*}}, %[[B_ARG:.*]]: !s32i {{.*}})
// CIR:   %[[A_PTR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["a", init]
// CIR:   %[[B_PTR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["b", init]
// CIR:   %[[S:.*]] = cir.alloca !rec_S, !cir.ptr<!rec_S>, ["s", init]
// CIR:   cir.store{{.*}} %[[A_ARG]], %[[A_PTR]]
// CIR:   cir.store{{.*}} %[[B_ARG]], %[[B_PTR]]
// CIR:   %[[S_A:.*]] = cir.get_member %[[S]][0] {name = "a"}
// CIR:   %[[A:.*]] = cir.load{{.*}} %[[A_PTR]]
// CIR:   cir.store{{.*}} %[[A]], %[[S_A]]
// CIR:   %[[S_B:.*]] = cir.get_member %[[S]][1] {name = "b"}
// CIR:   %[[B:.*]] = cir.load{{.*}} %[[B_PTR]]
// CIR:   cir.store{{.*}} %[[B]], %[[S_B]]
// CIR:   cir.return

// LLVM: define{{.*}} void @_Z8init_varii(i32 %[[A_ARG:.*]], i32 %[[B_ARG:.*]])
// LLVM:   %[[A_PTR:.*]] = alloca i32
// LLVM:   %[[B_PTR:.*]] = alloca i32
// LLVM:   %[[S:.*]] = alloca %struct.S
// LLVM:   store i32 %[[A_ARG]], ptr %[[A_PTR]]
// LLVM:   store i32 %[[B_ARG]], ptr %[[B_PTR]]
// LLVM:   %[[S_A:.*]] = getelementptr %struct.S, ptr %[[S]], i32 0, i32 0
// LLVM:   %[[A:.*]] = load i32, ptr %[[A_PTR]] 
// LLVM:   store i32 %[[A]], ptr %[[S_A]]
// LLVM:   %[[S_B:.*]] = getelementptr %struct.S, ptr %[[S]], i32 0, i32 1
// LLVM:   %[[B:.*]] = load i32, ptr %[[B_PTR]]
// LLVM:   store i32 %[[B]], ptr %[[S_B]]
// LLVM:   ret void

// OGCG: define{{.*}} void @_Z8init_varii(i32 {{.*}} %[[A_ARG:.*]], i32 {{.*}} %[[B_ARG:.*]])
// OGCG:   %[[A_PTR:.*]] = alloca i32
// OGCG:   %[[B_PTR:.*]] = alloca i32
// OGCG:   %[[S:.*]] = alloca %struct.S
// OGCG:   store i32 %[[A_ARG]], ptr %[[A_PTR]]
// OGCG:   store i32 %[[B_ARG]], ptr %[[B_PTR]]
// OGCG:   %[[S_A:.*]] = getelementptr {{.*}} %struct.S, ptr %[[S]], i32 0, i32 0
// OGCG:   %[[A:.*]] = load i32, ptr %[[A_PTR]] 
// OGCG:   store i32 %[[A]], ptr %[[S_A]]
// OGCG:   %[[S_B:.*]] = getelementptr {{.*}} %struct.S, ptr %[[S]], i32 0, i32 1
// OGCG:   %[[B:.*]] = load i32, ptr %[[B_PTR]]
// OGCG:   store i32 %[[B]], ptr %[[S_B]]
// OGCG:   %[[S_C:.*]] = getelementptr {{.*}} %struct.S, ptr %[[S]], i32 0, i32 2
// OGCG:   store i32 0, ptr %[[S_C]]
// OGCG:   ret void

void init_expr(int a, int b, int c) {
  S s = {a + 1, b + 2, c + 3};
}

// CIR: cir.func{{.*}} @_Z9init_expriii(%[[A_ARG:.*]]: !s32i {{.*}}, %[[B_ARG:.*]]: !s32i {{.*}}, %[[C_ARG:.*]]: !s32i {{.*}})
// CIR:   %[[A_PTR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["a", init]
// CIR:   %[[B_PTR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["b", init]
// CIR:   %[[C_PTR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["c", init]
// CIR:   %[[S:.*]] = cir.alloca !rec_S, !cir.ptr<!rec_S>, ["s", init]
// CIR:   cir.store{{.*}} %[[A_ARG]], %[[A_PTR]]
// CIR:   cir.store{{.*}} %[[B_ARG]], %[[B_PTR]]
// CIR:   cir.store{{.*}} %[[C_ARG]], %[[C_PTR]]
// CIR:   %[[S_A:.*]] = cir.get_member %[[S]][0] {name = "a"}
// CIR:   %[[A:.*]] = cir.load{{.*}} %[[A_PTR]]
// CIR:   %[[ONE:.*]] = cir.const #cir.int<1>
// CIR:   %[[A_PLUS_ONE:.*]] = cir.binop(add, %[[A]], %[[ONE]])
// CIR:   cir.store{{.*}} %[[A_PLUS_ONE]], %[[S_A]]
// CIR:   %[[S_B:.*]] = cir.get_member %[[S]][1] {name = "b"}
// CIR:   %[[B:.*]] = cir.load{{.*}} %[[B_PTR]]
// CIR:   %[[TWO:.*]] = cir.const #cir.int<2>
// CIR:   %[[B_PLUS_TWO:.*]] = cir.binop(add, %[[B]], %[[TWO]]) nsw : !s32i
// CIR:   cir.store{{.*}} %[[B_PLUS_TWO]], %[[S_B]]
// CIR:   %[[S_C:.*]] = cir.get_member %[[S]][2] {name = "c"}
// CIR:   %[[C:.*]] = cir.load{{.*}} %[[C_PTR]]
// CIR:   %[[THREE:.*]] = cir.const #cir.int<3>
// CIR:   %[[C_PLUS_THREE:.*]] = cir.binop(add, %[[C]], %[[THREE]]) nsw : !s32i
// CIR:   cir.store{{.*}} %[[C_PLUS_THREE]], %[[S_C]]
// CIR:   cir.return

// LLVM: define{{.*}} void @_Z9init_expriii(i32 %[[A_ARG:.*]], i32 %[[B_ARG:.*]], i32 %[[C_ARG:.*]])
// LLVM:   %[[A_PTR:.*]] = alloca i32
// LLVM:   %[[B_PTR:.*]] = alloca i32
// LLVM:   %[[C_PTR:.*]] = alloca i32
// LLVM:   %[[S:.*]] = alloca %struct.S
// LLVM:   store i32 %[[A_ARG]], ptr %[[A_PTR]]
// LLVM:   store i32 %[[B_ARG]], ptr %[[B_PTR]]
// LLVM:   store i32 %[[C_ARG]], ptr %[[C_PTR]]
// LLVM:   %[[S_A:.*]] = getelementptr %struct.S, ptr %[[S]], i32 0, i32 0
// LLVM:   %[[A:.*]] = load i32, ptr %[[A_PTR]] 
// LLVM:   %[[A_PLUS_ONE:.*]] = add nsw i32 %[[A]], 1
// LLVM:   store i32 %[[A_PLUS_ONE]], ptr %[[S_A]]
// LLVM:   %[[S_B:.*]] = getelementptr %struct.S, ptr %[[S]], i32 0, i32 1
// LLVM:   %[[B:.*]] = load i32, ptr %[[B_PTR]]
// LLVM:   %[[B_PLUS_TWO:.*]] = add nsw i32 %[[B]], 2
// LLVM:   store i32 %[[B_PLUS_TWO]], ptr %[[S_B]]
// LLVM:   %[[S_C:.*]] = getelementptr %struct.S, ptr %[[S]], i32 0, i32 2
// LLVM:   %[[C:.*]] = load i32, ptr %[[C_PTR]]
// LLVM:   %[[C_PLUS_THREE:.*]] = add nsw i32 %[[C]], 3
// LLVM:   store i32 %[[C_PLUS_THREE]], ptr %[[S_C]]
// LLVM:   ret void

// OGCG: define{{.*}} void @_Z9init_expriii(i32 {{.*}} %[[A_ARG:.*]], i32 {{.*}} %[[B_ARG:.*]], i32 {{.*}} %[[C_ARG:.*]])
// OGCG:   %[[A_PTR:.*]] = alloca i32
// OGCG:   %[[B_PTR:.*]] = alloca i32
// OGCG:   %[[C_PTR:.*]] = alloca i32
// OGCG:   %[[S:.*]] = alloca %struct.S
// OGCG:   store i32 %[[A_ARG]], ptr %[[A_PTR]]
// OGCG:   store i32 %[[B_ARG]], ptr %[[B_PTR]]
// OGCG:   store i32 %[[C_ARG]], ptr %[[C_PTR]]
// OGCG:   %[[S_A:.*]] = getelementptr {{.*}} %struct.S, ptr %[[S]], i32 0, i32 0
// OGCG:   %[[A:.*]] = load i32, ptr %[[A_PTR]] 
// OGCG:   %[[A_PLUS_ONE:.*]] = add nsw i32 %[[A]], 1
// OGCG:   store i32 %[[A_PLUS_ONE]], ptr %[[S_A]]
// OGCG:   %[[S_B:.*]] = getelementptr {{.*}} %struct.S, ptr %[[S]], i32 0, i32 1
// OGCG:   %[[B:.*]] = load i32, ptr %[[B_PTR]]
// OGCG:   %[[B_PLUS_TWO:.*]] = add nsw i32 %[[B]], 2
// OGCG:   store i32 %[[B_PLUS_TWO]], ptr %[[S_B]]
// OGCG:   %[[S_C:.*]] = getelementptr {{.*}} %struct.S, ptr %[[S]], i32 0, i32 2
// OGCG:   %[[C:.*]] = load i32, ptr %[[C_PTR]]
// OGCG:   %[[C_PLUS_THREE:.*]] = add nsw i32 %[[C]], 3
// OGCG:   store i32 %[[C_PLUS_THREE]], ptr %[[S_C]]
// OGCG:   ret void