stmtexpr.cpp
7.1 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
// RUN: %clang_cc1 -Wno-unused-value -triple i686-linux-gnu -emit-llvm -o - %s | FileCheck %s
// rdar: //8540501
extern "C" int printf(...);
extern "C" void abort();
struct A
{
int i;
A (int j) : i(j) {printf("this = %p A(%d)\n", this, j);}
A (const A &j) : i(j.i) {printf("this = %p const A&(%d)\n", this, i);}
A& operator= (const A &j) { i = j.i; abort(); return *this; }
~A() { printf("this = %p ~A(%d)\n", this, i); }
};
struct B
{
int i;
B (const A& a) { i = a.i; }
B() {printf("this = %p B()\n", this);}
B (const B &j) : i(j.i) {printf("this = %p const B&(%d)\n", this, i);}
~B() { printf("this = %p ~B(%d)\n", this, i); }
};
A foo(int j)
{
return ({ j ? A(1) : A(0); });
}
void foo2()
{
A b = ({ A a(1); A a1(2); A a2(3); a1; a2; a; });
if (b.i != 1)
abort();
A c = ({ A a(1); A a1(2); A a2(3); a1; a2; a; A a3(4); a2; a3; });
if (c.i != 4)
abort();
}
void foo3()
{
const A &b = ({ A a(1); a; });
if (b.i != 1)
abort();
}
void foo4()
{
// CHECK: call {{.*}} @_ZN1AC1Ei
// CHECK: call {{.*}} @_ZN1AC1ERKS_
// CHECK: call {{.*}} @_ZN1AD1Ev
// CHECK: call {{.*}} @_ZN1BC1ERK1A
// CHECK: call {{.*}} @_ZN1AD1Ev
const B &b = ({ A a(1); a; });
if (b.i != 1)
abort();
}
int main()
{
foo2();
foo3();
foo4();
return foo(1).i-1;
}
// rdar: // 8600553
int a[128];
int* foo5() {
// CHECK-NOT: memcpy
// Check that array-to-pointer conversion occurs in a
// statement-expression.
return (({ a; }));
}
// <rdar://problem/14074868>
// Make sure this doesn't crash.
int foo5(bool b) {
int y = 0;
y = ({ A a(1); if (b) goto G; a.i; });
G: return y;
}
// When we emit a full expression with cleanups that contains branches out of
// the full expression, the result of the inner expression (the call to
// call_with_cleanups in this case) may not dominate the fallthrough destination
// of the shared cleanup block.
//
// In this case the CFG will be a sequence of two diamonds, but the only
// dynamically possible execution paths are both left hand branches and both
// right hand branches. The first diamond LHS will call bar, and the second
// diamond LHS will assign the result to v, but the call to bar does not
// dominate the assignment.
int bar(A, int);
extern "C" int cleanup_exit_scalar(bool b) {
int v = bar(A(1), ({ if (b) return 42; 13; }));
return v;
}
// CHECK-LABEL: define{{.*}} i32 @cleanup_exit_scalar({{.*}})
// CHECK: call {{.*}} @_ZN1AC1Ei
// Spill after bar.
// CHECK: %[[v:[^ ]*]] = call{{.*}} i32 @_Z3bar1Ai({{.*}})
// CHECK-NEXT: store i32 %[[v]], i32* %[[tmp:[^, ]*]]
// Do cleanup.
// CHECK: call {{.*}} @_ZN1AD1Ev
// CHECK: switch
// Reload before v assignment.
// CHECK: %[[v:[^ ]*]] = load i32, i32* %[[tmp]]
// CHECK-NEXT: store i32 %[[v]], i32* %v
// No need to spill when the expression result is a constant, constants don't
// have dominance problems.
extern "C" int cleanup_exit_scalar_constant(bool b) {
int v = (A(1), (void)({ if (b) return 42; 0; }), 13);
return v;
}
// CHECK-LABEL: define{{.*}} i32 @cleanup_exit_scalar_constant({{.*}})
// CHECK: store i32 13, i32* %v
// Check for the same bug for lvalue expression evaluation kind.
// FIXME: What about non-reference lvalues, like bitfield lvalues and vector
// lvalues?
int &getref();
extern "C" int cleanup_exit_lvalue(bool cond) {
int &r = (A(1), ({ if (cond) return 0; (void)0; }), getref());
return r;
}
// CHECK-LABEL: define{{.*}} i32 @cleanup_exit_lvalue({{.*}})
// CHECK: call {{.*}} @_ZN1AC1Ei
// Spill after bar.
// CHECK: %[[v:[^ ]*]] = call dereferenceable(4) i32* @_Z6getrefv({{.*}})
// CHECK-NEXT: store i32* %[[v]], i32** %[[tmp:[^, ]*]]
// Do cleanup.
// CHECK: call {{.*}} @_ZN1AD1Ev
// CHECK: switch
// Reload before v assignment.
// CHECK: %[[v:[^ ]*]] = load i32*, i32** %[[tmp]]
// CHECK-NEXT: store i32* %[[v]], i32** %r
// Bind the reference to a byval argument. It is not an instruction or Constant,
// so it's a bit of a corner case.
struct ByVal { int x[3]; };
extern "C" int cleanup_exit_lvalue_byval(bool cond, ByVal arg) {
ByVal &r = (A(1), ({ if (cond) return 0; (void)ByVal(); }), arg);
return r.x[0];
}
// CHECK-LABEL: define{{.*}} i32 @cleanup_exit_lvalue_byval({{.*}}, %struct.ByVal* byval(%struct.ByVal) align 4 %arg)
// CHECK: call {{.*}} @_ZN1AC1Ei
// CHECK: call {{.*}} @_ZN1AD1Ev
// CHECK: switch
// CHECK: store %struct.ByVal* %arg, %struct.ByVal** %r
// Bind the reference to a local variable. We don't need to spill it. Binding a
// reference to it doesn't generate any instructions.
extern "C" int cleanup_exit_lvalue_local(bool cond) {
int local = 42;
int &r = (A(1), ({ if (cond) return 0; (void)0; }), local);
return r;
}
// CHECK-LABEL: define{{.*}} i32 @cleanup_exit_lvalue_local({{.*}})
// CHECK: %local = alloca i32
// CHECK: store i32 42, i32* %local
// CHECK: call {{.*}} @_ZN1AC1Ei
// CHECK-NOT: store i32* %local
// CHECK: call {{.*}} @_ZN1AD1Ev
// CHECK: switch
// CHECK: store i32* %local, i32** %r, align 4
// We handle ExprWithCleanups for complex evaluation type separately, and it had
// the same bug.
_Complex float bar_complex(A, int);
extern "C" int cleanup_exit_complex(bool b) {
_Complex float v = bar_complex(A(1), ({ if (b) return 42; 13; }));
return (float)v;
}
// CHECK-LABEL: define{{.*}} i32 @cleanup_exit_complex({{.*}})
// CHECK: call {{.*}} @_ZN1AC1Ei
// Spill after bar.
// CHECK: call {{.*}} @_Z11bar_complex1Ai({{.*}})
// CHECK: store float %{{.*}}, float* %[[tmp1:[^, ]*]]
// CHECK: store float %{{.*}}, float* %[[tmp2:[^, ]*]]
// Do cleanup.
// CHECK: call {{.*}} @_ZN1AD1Ev
// CHECK: switch
// Reload before v assignment.
// CHECK: %[[v1:[^ ]*]] = load float, float* %[[tmp1]]
// CHECK: %[[v2:[^ ]*]] = load float, float* %[[tmp2]]
// CHECK: store float %[[v1]], float* %v.realp
// CHECK: store float %[[v2]], float* %v.imagp
extern "C" void then(int);
// CHECK-LABEL: @{{.*}}volatile_load
void volatile_load() {
volatile int n;
// CHECK-NOT: load volatile
// CHECK: load volatile
// CHECK-NOT: load volatile
({n;});
// CHECK-LABEL: @then(i32 1)
then(1);
// CHECK-NOT: load volatile
// CHECK: load volatile
// CHECK-NOT: load volatile
({goto lab; lab: n;});
// CHECK-LABEL: @then(i32 2)
then(2);
// CHECK-NOT: load volatile
// CHECK: load volatile
// CHECK-NOT: load volatile
({[[gsl::suppress("foo")]] n;});
// CHECK-LABEL: @then(i32 3)
then(3);
// CHECK-NOT: load volatile
// CHECK: load volatile
// CHECK-NOT: load volatile
({if (true) n;});
// CHECK: }
}
// CHECK-LABEL: @{{.*}}volatile_load_template
template<typename T>
void volatile_load_template() {
volatile T n;
// CHECK-NOT: load volatile
// CHECK: load volatile
// CHECK-NOT: load volatile
({n;});
// CHECK-LABEL: @then(i32 1)
then(1);
// CHECK-NOT: load volatile
// CHECK: load volatile
// CHECK-NOT: load volatile
({goto lab; lab: n;});
// CHECK-LABEL: @then(i32 2)
then(2);
// CHECK-NOT: load volatile
// CHECK: load volatile
// CHECK-NOT: load volatile
({[[gsl::suppress("foo")]] n;});
// CHECK-LABEL: @then(i32 3)
then(3);
// CHECK-NOT: load volatile
// CHECK: load volatile
// CHECK-NOT: load volatile
({if (true) n;});
// CHECK: }
}
template void volatile_load_template<int>();