condition.cpp
6.55 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -o - %s | FileCheck %s
void *f();
template <typename T> T* g() {
if (T* t = f())
return t;
return 0;
}
void h() {
void *a = g<void>();
}
struct X {
X();
X(const X&);
~X();
operator bool();
};
struct Y {
Y();
~Y();
};
X getX();
// CHECK-LABEL: define void @_Z11if_destructi(
void if_destruct(int z) {
// Verify that the condition variable is destroyed at the end of the
// "if" statement.
// CHECK: call void @_ZN1XC1Ev
// CHECK: call zeroext i1 @_ZN1XcvbEv
if (X x = X()) {
// CHECK: store i32 18
z = 18;
}
// CHECK: call void @_ZN1XD1Ev
// CHECK: store i32 17
z = 17;
// CHECK: call void @_ZN1XC1Ev
if (X x = X())
Y y;
// CHECK: br
// CHECK: call void @_ZN1YC1Ev
// CHECK: call void @_ZN1YD1Ev
// CHECK: br
// CHECK: call void @_ZN1XD1Ev
// CHECK: call void @_Z4getXv
// CHECK: call zeroext i1 @_ZN1XcvbEv
// CHECK: call void @_ZN1XD1Ev
// CHECK: br
if (getX()) { }
// CHECK: ret
}
struct ConvertibleToInt {
ConvertibleToInt();
~ConvertibleToInt();
operator int();
};
ConvertibleToInt getConvToInt();
void switch_destruct(int z) {
// CHECK: call void @_ZN16ConvertibleToIntC1Ev
switch (ConvertibleToInt conv = ConvertibleToInt()) {
case 0:
break;
default:
// CHECK: store i32 19
z = 19;
break;
}
// CHECK: call void @_ZN16ConvertibleToIntD1Ev
// CHECK: store i32 20
z = 20;
// CHECK: call void @_Z12getConvToIntv
// CHECK: call i32 @_ZN16ConvertibleToIntcviEv
// CHECK: call void @_ZN16ConvertibleToIntD1Ev
switch(getConvToInt()) {
case 0:
break;
}
// CHECK: store i32 27
z = 27;
// CHECK: ret
}
int foo();
// CHECK-LABEL: define void @_Z14while_destructi
void while_destruct(int z) {
// CHECK: [[Z:%.*]] = alloca i32
// CHECK: [[CLEANUPDEST:%.*]] = alloca i32
while (X x = X()) {
// CHECK: call void @_ZN1XC1Ev
// CHECK-NEXT: [[COND:%.*]] = call zeroext i1 @_ZN1XcvbEv
// CHECK-NEXT: br i1 [[COND]]
// Loop-exit staging block.
// CHECK: store i32 3, i32* [[CLEANUPDEST]]
// CHECK-NEXT: br
// While body.
// CHECK: store i32 21, i32* [[Z]]
// CHECK: store i32 0, i32* [[CLEANUPDEST]]
// CHECK-NEXT: br
z = 21;
// Cleanup.
// CHECK: call void @_ZN1XD1Ev
// CHECK-NEXT: [[DEST:%.*]] = load i32, i32* [[CLEANUPDEST]]
// CHECK-NEXT: switch i32 [[DEST]]
}
// CHECK: store i32 22, i32* [[Z]]
z = 22;
// CHECK: call void @_Z4getXv
// CHECK-NEXT: call zeroext i1 @_ZN1XcvbEv
// CHECK-NEXT: call void @_ZN1XD1Ev
// CHECK-NEXT: br
while(getX()) { }
// CHECK: store i32 25, i32* [[Z]]
z = 25;
// CHECK: ret
}
// CHECK-LABEL: define void @_Z12for_destructi(
void for_destruct(int z) {
// CHECK: [[Z:%.*]] = alloca i32
// CHECK: [[CLEANUPDEST:%.*]] = alloca i32
// CHECK: [[I:%.*]] = alloca i32
// CHECK: call void @_ZN1YC1Ev
// CHECK-NEXT: br
// -> %for.cond
for(Y y = Y(); X x = X(); ++z) {
// %for.cond: The loop condition.
// CHECK: call void @_ZN1XC1Ev
// CHECK-NEXT: [[COND:%.*]] = call zeroext i1 @_ZN1XcvbEv(
// CHECK-NEXT: br i1 [[COND]]
// -> %for.body, %for.cond.cleanup
// %for.cond.cleanup: Exit cleanup staging.
// CHECK: store i32 2, i32* [[CLEANUPDEST]]
// CHECK-NEXT: br
// -> %cleanup
// %for.body:
// CHECK: store i32 23, i32* [[Z]]
// CHECK-NEXT: br
// -> %for.inc
z = 23;
// %for.inc:
// CHECK: [[TMP:%.*]] = load i32, i32* [[Z]]
// CHECK-NEXT: [[INC:%.*]] = add nsw i32 [[TMP]], 1
// CHECK-NEXT: store i32 [[INC]], i32* [[Z]]
// CHECK-NEXT: store i32 0, i32* [[CLEANUPDEST]]
// CHECK-NEXT: br
// -> %cleanup
// %cleanup: Destroys X.
// CHECK: call void @_ZN1XD1Ev
// CHECK-NEXT: [[YDESTTMP:%.*]] = load i32, i32* [[CLEANUPDEST]]
// CHECK-NEXT: switch i32 [[YDESTTMP]]
// 0 -> %cleanup.cont, default -> %cleanup1
// %cleanup.cont: (eliminable)
// CHECK: br
// -> %for.cond
// %cleanup1: Destroys Y.
// CHECK: call void @_ZN1YD1Ev(
// CHECK-NEXT: br
// -> %for.end
}
// %for.end:
// CHECK: store i32 24
z = 24;
// CHECK-NEXT: store i32 0, i32* [[I]]
// CHECK-NEXT: br
// -> %for.cond6
// %for.cond6:
// CHECK: call void @_Z4getXv
// CHECK-NEXT: call zeroext i1 @_ZN1XcvbEv
// CHECK-NEXT: call void @_ZN1XD1Ev
// CHECK-NEXT: br
// -> %for.body10, %for.end16
// %for.body10:
// CHECK: br
// -> %for.inc11
// %for.inc11:
// CHECK: call void @_Z4getXv
// CHECK-NEXT: load i32, i32* [[I]]
// CHECK-NEXT: add
// CHECK-NEXT: store
// CHECK-NEXT: call void @_ZN1XD1Ev
// CHECK-NEXT: br
// -> %for.cond6
int i = 0;
for(; getX(); getX(), ++i) { }
// %for.end16
// CHECK: store i32 26
z = 26;
// CHECK-NEXT: ret void
}
void do_destruct(int z) {
// CHECK-LABEL: define void @_Z11do_destruct
do {
// CHECK: store i32 77
z = 77;
// CHECK: call void @_Z4getXv
// CHECK: call zeroext i1 @_ZN1XcvbEv
// CHECK: call void @_ZN1XD1Ev
// CHECK: br
} while (getX());
// CHECK: store i32 99
z = 99;
// CHECK: ret
}
int f(X);
template<typename T>
int instantiated(T x) {
int result;
// CHECK: call void @_ZN1XC1ERKS_
// CHECK: call i32 @_Z1f1X
// CHECK: call void @_ZN1XD1Ev
// CHECK: br
// CHECK: store i32 2
// CHECK: br
// CHECK: store i32 3
if (f(x)) { result = 2; } else { result = 3; }
// CHECK: call void @_ZN1XC1ERKS_
// CHECK: call i32 @_Z1f1X
// CHECK: call void @_ZN1XD1Ev
// CHECK: br
// CHECK: store i32 4
// CHECK: br
while (f(x)) { result = 4; }
// CHECK: call void @_ZN1XC1ERKS_
// CHECK: call i32 @_Z1f1X
// CHECK: call void @_ZN1XD1Ev
// CHECK: br
// CHECK: store i32 6
// CHECK: br
// CHECK: call void @_ZN1XC1ERKS_
// CHECK: call i32 @_Z1f1X
// CHECK: store i32 5
// CHECK: call void @_ZN1XD1Ev
// CHECK: br
for (; f(x); f(x), result = 5) {
result = 6;
}
// CHECK: call void @_ZN1XC1ERKS_
// CHECK: call i32 @_Z1f1X
// CHECK: call void @_ZN1XD1Ev
// CHECK: switch i32
// CHECK: store i32 7
// CHECK: store i32 8
switch (f(x)) {
case 0:
result = 7;
break;
case 1:
result = 8;
}
// CHECK: store i32 9
// CHECK: br
// CHECK: call void @_ZN1XC1ERKS_
// CHECK: call i32 @_Z1f1X
// CHECK: call void @_ZN1XD1Ev
// CHECK: br
do {
result = 9;
} while (f(x));
// CHECK: store i32 10
// CHECK: call void @_ZN1XC1ERKS_
// CHECK: call zeroext i1 @_ZN1XcvbEv
// CHECK: call void @_ZN1XD1Ev
// CHECK: br
do {
result = 10;
} while (X(x));
// CHECK: ret i32
return result;
}
template int instantiated(X);