File: microsoft-abi-eh-cleanups.cpp

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (361 lines) | stat: -rw-r--r-- 13,614 bytes parent folder | download
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// RUN: %clang_cc1 -no-opaque-pointers -std=c++11 -emit-llvm %s -o - -triple=i386-pc-win32 -mconstructor-aliases -fexceptions -fcxx-exceptions -fno-rtti | FileCheck -check-prefix WIN32 -check-prefix WIN32-O0 %s
// RUN: %clang_cc1 -no-opaque-pointers -std=c++11 -emit-llvm -O3 -disable-llvm-passes %s -o - -triple=i386-pc-win32 -mconstructor-aliases -fexceptions -fcxx-exceptions -fno-rtti | FileCheck -check-prefix WIN32 -check-prefix WIN32-O3 -check-prefix WIN32-LIFETIME %s

struct A {
  A();
  ~A();
  int a;
};

A getA();

int TakesTwo(A a, A b);
void HasEHCleanup() {
  TakesTwo(getA(), getA());
}

// With exceptions, we need to clean up at least one of these temporaries.
// WIN32-LABEL: define dso_local void @"?HasEHCleanup@@YAXXZ"() {{.*}} {
// WIN32:   %[[base:.*]] = call i8* @llvm.stacksave()
//    If this call throws, we have to restore the stack.
// WIN32:   call void @"?getA@@YA?AUA@@XZ"(%struct.A* sret(%struct.A) align 4 %{{.*}})
//    If this call throws, we have to cleanup the first temporary.
// WIN32:   invoke void @"?getA@@YA?AUA@@XZ"(%struct.A* sret(%struct.A) align 4 %{{.*}})
//    If this call throws, we have to cleanup the stacksave.
// WIN32:   call noundef i32 @"?TakesTwo@@YAHUA@@0@Z"
// WIN32:   call void @llvm.stackrestore
// WIN32:   ret void
//
//    There should be one dtor call for unwinding from the second getA.
// WIN32:   cleanuppad
// WIN32:   call x86_thiscallcc void @"??1A@@QAE@XZ"({{.*}})
// WIN32-NOT: @"??1A@@QAE@XZ"
// WIN32: }


// This test verifies the fix for a crash that occurred after
// fa87fa97fb79.
void HasEHCleanupNoexcept() noexcept {
  TakesTwo(getA(), getA());
}

// With exceptions, we need to clean up at least one of these temporaries.
// WIN32-LABEL: define dso_local void @"?HasEHCleanupNoexcept@@YAXXZ"() {{.*}} {
// WIN32:   %[[base:.*]] = call i8* @llvm.stacksave()
// WIN32:   invoke void @"?getA@@YA?AUA@@XZ"(%struct.A* sret(%struct.A) align 4 %{{.*}})
// WIN32:   invoke void @"?getA@@YA?AUA@@XZ"(%struct.A* sret(%struct.A) align 4 %{{.*}})
// WIN32:   invoke noundef i32 @"?TakesTwo@@YAHUA@@0@Z"
// WIN32:   call void @llvm.stackrestore
// WIN32:   ret void
//
//    Since all the calls terminate, there should be no dtors on the unwind
// WIN32:   cleanuppad
// WIN32-NOT: @"??1A@@QAE@XZ"
// WIN32: }


void TakeRef(const A &a);
int HasDeactivatedCleanups() {
  return TakesTwo((TakeRef(A()), A()), (TakeRef(A()), A()));
}

// WIN32-LABEL: define dso_local noundef i32 @"?HasDeactivatedCleanups@@YAHXZ"() {{.*}} {
// WIN32:   %[[isactive:.*]] = alloca i1
// WIN32:   call i8* @llvm.stacksave()
// WIN32:   %[[argmem:.*]] = alloca inalloca [[argmem_ty:<{ %struct.A, %struct.A }>]]
// WIN32:   %[[arg1:.*]] = getelementptr inbounds [[argmem_ty]], [[argmem_ty]]* %[[argmem]], i32 0, i32 1
// WIN32:   call x86_thiscallcc noundef %struct.A* @"??0A@@QAE@XZ"
// WIN32:   invoke void @"?TakeRef@@YAXABUA@@@Z"
//
// WIN32:   invoke x86_thiscallcc noundef %struct.A* @"??0A@@QAE@XZ"(%struct.A* {{[^,]*}} %[[arg1]])
// WIN32:   store i1 true, i1* %[[isactive]]
//
// WIN32:   %[[arg0:.*]] = getelementptr inbounds [[argmem_ty]], [[argmem_ty]]* %[[argmem]], i32 0, i32 0
// WIN32:   invoke x86_thiscallcc noundef %struct.A* @"??0A@@QAE@XZ"
// WIN32:   invoke void @"?TakeRef@@YAXABUA@@@Z"
// WIN32:   invoke x86_thiscallcc noundef %struct.A* @"??0A@@QAE@XZ"
// WIN32:   store i1 false, i1* %[[isactive]]
//
// WIN32:   invoke noundef i32 @"?TakesTwo@@YAHUA@@0@Z"([[argmem_ty]]* inalloca([[argmem_ty]]) %[[argmem]])
//        Destroy the two const ref temporaries.
// WIN32:   call x86_thiscallcc void @"??1A@@QAE@XZ"({{.*}})
// WIN32:   call x86_thiscallcc void @"??1A@@QAE@XZ"({{.*}})
// WIN32:   ret i32
//
//        Conditionally destroy arg1.
// WIN32:   %[[cond:.*]] = load i1, i1* %[[isactive]]
// WIN32:   br i1 %[[cond]]
// WIN32:   call x86_thiscallcc void @"??1A@@QAE@XZ"(%struct.A* {{[^,]*}} %[[arg1]])
// WIN32: }

// Test putting the cleanups inside a conditional.
int CouldThrow();
int HasConditionalCleanup(bool cond) {
  return (cond ? TakesTwo(A(), A()) : CouldThrow());
}

// WIN32-LABEL: define dso_local noundef i32 @"?HasConditionalCleanup@@YAH_N@Z"(i1 noundef zeroext %{{.*}}) {{.*}} {
// WIN32:   store i1 false
// WIN32:   br i1
// WIN32:   call i8* @llvm.stacksave()
// WIN32:   call x86_thiscallcc noundef %struct.A* @"??0A@@QAE@XZ"(%struct.A* {{[^,]*}} %{{.*}})
// WIN32:   store i1 true
// WIN32:   invoke x86_thiscallcc noundef %struct.A* @"??0A@@QAE@XZ"(%struct.A* {{[^,]*}} %{{.*}})
// WIN32:   call noundef i32 @"?TakesTwo@@YAHUA@@0@Z"
//
// WIN32:   call void @llvm.stackrestore
//
// WIN32:   call noundef i32 @"?CouldThrow@@YAHXZ"()
//
//        Only one dtor in the invoke for arg1
// WIN32:   call x86_thiscallcc void @"??1A@@QAE@XZ"({{.*}})
// WIN32-NOT: invoke x86_thiscallcc void @"??1A@@QAE@XZ"
// WIN32: }

// Now test both.
int HasConditionalDeactivatedCleanups(bool cond) {
  return (cond ? TakesTwo((TakeRef(A()), A()), (TakeRef(A()), A())) : CouldThrow());
}

// WIN32-O0-LABEL: define dso_local noundef i32 @"?HasConditionalDeactivatedCleanups@@YAH_N@Z"{{.*}} {
// WIN32-O0:   alloca i1
// WIN32-O0:   %[[arg1_cond:.*]] = alloca i1
//        Start all four cleanups as deactivated.
// WIN32-O0:   store i1 false
// WIN32-O0:   store i1 false
// WIN32-O0:   store i1 false
// WIN32-O0:   store i1 false
// WIN32-O0:   br i1
//        True condition.
// WIN32-O0:   call x86_thiscallcc noundef %struct.A* @"??0A@@QAE@XZ"
// WIN32-O0:   store i1 true
// WIN32-O0:   invoke void @"?TakeRef@@YAXABUA@@@Z"
// WIN32-O0:   invoke x86_thiscallcc noundef %struct.A* @"??0A@@QAE@XZ"
// WIN32-O0:   store i1 true, i1* %[[arg1_cond]]
// WIN32-O0:   invoke x86_thiscallcc noundef %struct.A* @"??0A@@QAE@XZ"
// WIN32-O0:   store i1 true
// WIN32-O0:   invoke void @"?TakeRef@@YAXABUA@@@Z"
// WIN32-O0:   invoke x86_thiscallcc noundef %struct.A* @"??0A@@QAE@XZ"
// WIN32-O0:   store i1 true
// WIN32-O0:   store i1 false, i1* %[[arg1_cond]]
// WIN32-O0:   invoke noundef i32 @"?TakesTwo@@YAHUA@@0@Z"
//        False condition.
// WIN32-O0:   invoke noundef i32 @"?CouldThrow@@YAHXZ"()
//        Two normal cleanups for TakeRef args.
// WIN32-O0:   call x86_thiscallcc void @"??1A@@QAE@XZ"({{.*}})
// WIN32-O0-NOT:   invoke x86_thiscallcc void @"??1A@@QAE@XZ"
// WIN32-O0:   ret i32
//
//        Somewhere in the landing pad soup, we conditionally destroy arg1.
// WIN32-O0:   %[[isactive:.*]] = load i1, i1* %[[arg1_cond]]
// WIN32-O0:   br i1 %[[isactive]]
// WIN32-O0:   call x86_thiscallcc void @"??1A@@QAE@XZ"({{.*}})
// WIN32-O0: }

// WIN32-O3-LABEL: define dso_local noundef i32 @"?HasConditionalDeactivatedCleanups@@YAH_N@Z"{{.*}} {
// WIN32-O3:   alloca i1
// WIN32-O3:   alloca i1
// WIN32-O3:   %[[arg1_cond:.*]] = alloca i1
//        Start all four cleanups as deactivated.
// WIN32-O3:   store i1 false
// WIN32-O3:   store i1 false
// WIN32-O3:   store i1 false
// WIN32-O3:   store i1 false
// WIN32-O3:   store i1 false
// WIN32-O3:   store i1 false
// WIN32-O3:   br i1
//        True condition.
// WIN32-O3:   call x86_thiscallcc noundef %struct.A* @"??0A@@QAE@XZ"
// WIN32-O3:   store i1 true
// WIN32-O3:   invoke void @"?TakeRef@@YAXABUA@@@Z"
// WIN32-O3:   invoke x86_thiscallcc noundef %struct.A* @"??0A@@QAE@XZ"
// WIN32-O3:   store i1 true, i1* %[[arg1_cond]]
// WIN32-O3:   invoke x86_thiscallcc noundef %struct.A* @"??0A@@QAE@XZ"
// WIN32-O3:   store i1 true
// WIN32-O3:   invoke void @"?TakeRef@@YAXABUA@@@Z"
// WIN32-O3:   invoke x86_thiscallcc noundef %struct.A* @"??0A@@QAE@XZ"
// WIN32-O3:   store i1 true
// WIN32-O3:   store i1 false, i1* %[[arg1_cond]]
// WIN32-O3:   invoke noundef i32 @"?TakesTwo@@YAHUA@@0@Z"
//        False condition.
// WIN32-O3:   invoke noundef i32 @"?CouldThrow@@YAHXZ"()
//        Two normal cleanups for TakeRef args.
// WIN32-O3:   call x86_thiscallcc void @"??1A@@QAE@XZ"({{.*}})
// WIN32-O3-NOT:   invoke x86_thiscallcc void @"??1A@@QAE@XZ"
// WIN32-O3:   ret i32
//
//        Somewhere in the landing pad soup, we conditionally destroy arg1.
// WIN32-O3:   %[[isactive:.*]] = load i1, i1* %[[arg1_cond]]
// WIN32-O3:   br i1 %[[isactive]]
// WIN32-O3:   call x86_thiscallcc void @"??1A@@QAE@XZ"({{.*}})
// WIN32-O3: }

namespace crash_on_partial_destroy {
struct A {
  virtual ~A();
};

struct B : virtual A {
  // Has an implicit destructor.
};

struct C : B {
  C();
};

void foo();
// We used to crash when emitting this.
C::C() { foo(); }

// Verify that we don't bother with a vbtable lookup when adjusting the this
// pointer to call a base destructor from a constructor while unwinding.
// WIN32-LABEL: define dso_local {{.*}} @"??0C@crash_on_partial_destroy@@QAE@XZ"{{.*}} {
// WIN32:      cleanuppad
//
//        We shouldn't do any vbptr loads, just constant GEPs.
// WIN32-NOT:  load
// WIN32:      getelementptr i8, i8* %{{.*}}, i32 4
// WIN32-NOT:  load
// WIN32:      bitcast i8* %{{.*}} to %"struct.crash_on_partial_destroy::B"*
// WIN32:      call x86_thiscallcc void @"??1B@crash_on_partial_destroy@@UAE@XZ"
//
// WIN32-NOT:  load
// WIN32:      bitcast %"struct.crash_on_partial_destroy::C"* %{{.*}} to i8*
// WIN32-NOT:  load
// WIN32:      getelementptr inbounds i8, i8* %{{.*}}, i32 4
// WIN32-NOT:  load
// WIN32:      bitcast i8* %{{.*}} to %"struct.crash_on_partial_destroy::A"*
// WIN32:      call x86_thiscallcc void @"??1A@crash_on_partial_destroy@@UAE@XZ"({{.*}})
// WIN32: }
}

namespace dont_call_terminate {
struct C {
  ~C();
};
void g();
void f() {
  C c;
  g();
}

// WIN32-LABEL: define dso_local void @"?f@dont_call_terminate@@YAXXZ"()
// WIN32: invoke void @"?g@dont_call_terminate@@YAXXZ"()
// WIN32-NEXT: to label %[[cont:[^ ]*]] unwind label %[[lpad:[^ ]*]]
//
// WIN32: [[cont]]
// WIN32: call x86_thiscallcc void @"??1C@dont_call_terminate@@QAE@XZ"({{.*}})
//
// WIN32: [[lpad]]
// WIN32-NEXT: cleanuppad
// WIN32: call x86_thiscallcc void @"??1C@dont_call_terminate@@QAE@XZ"({{.*}})
}

namespace noexcept_false_dtor {
struct D {
  ~D() noexcept(false);
};
void f() {
  D d;
  CouldThrow();
}
}

// WIN32-LABEL: define dso_local void @"?f@noexcept_false_dtor@@YAXXZ"()
// WIN32: invoke noundef i32 @"?CouldThrow@@YAHXZ"()
// WIN32: call x86_thiscallcc void @"??1D@noexcept_false_dtor@@QAE@XZ"(%"struct.noexcept_false_dtor::D"* {{[^,]*}} %{{.*}})
// WIN32: cleanuppad
// WIN32: call x86_thiscallcc void @"??1D@noexcept_false_dtor@@QAE@XZ"(%"struct.noexcept_false_dtor::D"* {{[^,]*}} %{{.*}})
// WIN32: cleanupret

namespace lifetime_marker {
struct C {
  ~C();
};
void g();
void f() {
  C c;
  g();
}

// WIN32-LIFETIME-LABEL: define dso_local void @"?f@lifetime_marker@@YAXXZ"()
// WIN32-LIFETIME: %[[c:.*]] = alloca %"struct.lifetime_marker::C"
// WIN32-LIFETIME: %[[bc0:.*]] = bitcast %"struct.lifetime_marker::C"* %c to i8*
// WIN32-LIFETIME: call void @llvm.lifetime.start.p0i8(i64 1, i8* %[[bc0]])
// WIN32-LIFETIME: invoke void @"?g@lifetime_marker@@YAXXZ"()
// WIN32-LIFETIME-NEXT: to label %[[cont:[^ ]*]] unwind label %[[lpad0:[^ ]*]]
//
// WIN32-LIFETIME: [[cont]]
// WIN32-LIFETIME: call x86_thiscallcc void @"??1C@lifetime_marker@@QAE@XZ"({{.*}})
// WIN32-LIFETIME: %[[bc1:.*]] = bitcast %"struct.lifetime_marker::C"* %[[c]] to i8*
// WIN32-LIFETIME: call void @llvm.lifetime.end.p0i8(i64 1, i8* %[[bc1]])
//
// WIN32-LIFETIME: [[lpad0]]
// WIN32-LIFETIME-NEXT: cleanuppad
// WIN32-LIFETIME: call x86_thiscallcc void @"??1C@lifetime_marker@@QAE@XZ"({{.*}})
// WIN32-LIFETIME: cleanupret {{.*}} unwind label %[[lpad1:[^ ]*]]
//
// WIN32-LIFETIME: [[lpad1]]
// WIN32-LIFETIME-NEXT: cleanuppad
// WIN32-LIFETIME: %[[bc2:.*]] = bitcast %"struct.lifetime_marker::C"* %[[c]] to i8*
// WIN32-LIFETIME: call void @llvm.lifetime.end.p0i8(i64 1, i8* %[[bc2]])
}

struct class_2 {
  class_2();
  virtual ~class_2();
};
struct class_1 : virtual class_2 {
  class_1(){throw "Unhandled exception";}
  virtual ~class_1() {}
};
struct class_0 : class_1 {
  class_0() ;
  virtual ~class_0() {}
};

class_0::class_0() {
  // WIN32: define dso_local x86_thiscallcc noundef %struct.class_0* @"??0class_0@@QAE@XZ"(%struct.class_0* {{[^,]*}} returned align 4 dereferenceable(4) %this, i32 noundef %is_most_derived)
  // WIN32: store i32 %is_most_derived, i32* %[[IS_MOST_DERIVED_VAR:.*]], align 4
  // WIN32: %[[IS_MOST_DERIVED_VAL:.*]] = load i32, i32* %[[IS_MOST_DERIVED_VAR]]
  // WIN32: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0
  // WIN32: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
  // WIN32: [[INIT_VBASES]]
  // WIN32: br label %[[SKIP_VBASES]]
  // WIN32: [[SKIP_VBASES]]
// ehcleanup:
  // WIN32: %[[CLEANUPPAD:.*]] = cleanuppad within none []
  // WIN32-NEXT: bitcast %{{.*}}* %{{.*}} to i8*
  // WIN32-NEXT: getelementptr inbounds i8, i8* %{{.*}}, i{{.*}} {{.}}
  // WIN32-NEXT: bitcast i8* %{{.*}} to %{{.*}}*
  // WIN32-NEXT: %[[SHOULD_CALL_VBASE_DTOR:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0
  // WIN32-NEXT: br i1 %[[SHOULD_CALL_VBASE_DTOR]], label %[[DTOR_VBASE:.*]], label %[[SKIP_VBASE:.*]]
  // WIN32: [[DTOR_VBASE]]
  // WIN32-NEXT: call x86_thiscallcc void @"??1class_2@@UAE@XZ"
  // WIN32: br label %[[SKIP_VBASE]]
  // WIN32: [[SKIP_VBASE]]
}

namespace PR37146 {
// Check that IRGen doesn't emit calls to synthesized destructors for
// non-trival C structs.

// WIN32: define dso_local void @"?test@PR37146@@YAXXZ"()
// WIN32: call void @llvm.memset.p0i8.i32(
// WIN32: call i32 @"?getS@PR37146@@YA?AUS@1@XZ"(
// WIN32: call void @"?func@PR37146@@YAXUS@1@0@Z"(
// WIN32-NEXT: ret void
// WIN32-NEXT: {{^}$}}

struct S {
  int f;
};

void func(S, S);
S getS();

void test() {
  func(getS(), S());
}

}