xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGenCXX/microsoft-abi-exceptions.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -emit-llvm %s -o - -triple=i386-pc-win32 -mconstructor-aliases -fexceptions -fno-rtti | FileCheck -check-prefix WIN32 %s
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc struct A {
4f4a2713aSLionel Sambuc   A();
5f4a2713aSLionel Sambuc   ~A();
6f4a2713aSLionel Sambuc   int a;
7f4a2713aSLionel Sambuc };
8f4a2713aSLionel Sambuc 
9f4a2713aSLionel Sambuc A getA();
10f4a2713aSLionel Sambuc 
11f4a2713aSLionel Sambuc int TakesTwo(A a, A b);
HasEHCleanup()12f4a2713aSLionel Sambuc void HasEHCleanup() {
13f4a2713aSLionel Sambuc   TakesTwo(getA(), getA());
14f4a2713aSLionel Sambuc }
15f4a2713aSLionel Sambuc 
16f4a2713aSLionel Sambuc // With exceptions, we need to clean up at least one of these temporaries.
17*0a6a1f1dSLionel Sambuc // WIN32-LABEL: define void @"\01?HasEHCleanup@@YAXXZ"() {{.*}} {
18*0a6a1f1dSLionel Sambuc // WIN32:   %[[base:.*]] = call i8* @llvm.stacksave()
19*0a6a1f1dSLionel Sambuc //    If this call throws, we have to restore the stack.
20*0a6a1f1dSLionel Sambuc // WIN32:   invoke void @"\01?getA@@YA?AUA@@XZ"(%struct.A* sret %{{.*}})
21f4a2713aSLionel Sambuc //    If this call throws, we have to cleanup the first temporary.
22f4a2713aSLionel Sambuc // WIN32:   invoke void @"\01?getA@@YA?AUA@@XZ"(%struct.A* sret %{{.*}})
23*0a6a1f1dSLionel Sambuc //    If this call throws, we have to cleanup the stacksave.
24*0a6a1f1dSLionel Sambuc // WIN32:   invoke i32 @"\01?TakesTwo@@YAHUA@@0@Z"
25*0a6a1f1dSLionel Sambuc // WIN32:   call void @llvm.stackrestore(i8* %[[base]])
26f4a2713aSLionel Sambuc // WIN32:   ret void
27f4a2713aSLionel Sambuc //
28f4a2713aSLionel Sambuc //    There should be one dtor call for unwinding from the second getA.
29f4a2713aSLionel Sambuc // WIN32:   invoke x86_thiscallcc void @"\01??1A@@QAE@XZ"
30*0a6a1f1dSLionel Sambuc // WIN32-NOT: @"\01??1A@@QAE@XZ"
31*0a6a1f1dSLionel Sambuc // WIN32:   call void @llvm.stackrestore
32f4a2713aSLionel Sambuc // WIN32: }
33f4a2713aSLionel Sambuc 
34f4a2713aSLionel Sambuc void TakeRef(const A &a);
HasDeactivatedCleanups()35f4a2713aSLionel Sambuc int HasDeactivatedCleanups() {
36f4a2713aSLionel Sambuc   return TakesTwo((TakeRef(A()), A()), (TakeRef(A()), A()));
37f4a2713aSLionel Sambuc }
38f4a2713aSLionel Sambuc 
39*0a6a1f1dSLionel Sambuc // WIN32-LABEL: define i32 @"\01?HasDeactivatedCleanups@@YAHXZ"() {{.*}} {
40f4a2713aSLionel Sambuc // WIN32:   %[[isactive:.*]] = alloca i1
41*0a6a1f1dSLionel Sambuc // WIN32:   call i8* @llvm.stacksave()
42*0a6a1f1dSLionel Sambuc // WIN32:   %[[argmem:.*]] = alloca inalloca [[argmem_ty:<{ %struct.A, %struct.A }>]]
43*0a6a1f1dSLionel Sambuc // WIN32:   %[[arg1:.*]] = getelementptr inbounds [[argmem_ty]]* %[[argmem]], i32 0, i32 1
44*0a6a1f1dSLionel Sambuc // WIN32:   invoke x86_thiscallcc %struct.A* @"\01??0A@@QAE@XZ"
45f4a2713aSLionel Sambuc // WIN32:   invoke void @"\01?TakeRef@@YAXABUA@@@Z"
46*0a6a1f1dSLionel Sambuc //
47*0a6a1f1dSLionel Sambuc // WIN32:   invoke x86_thiscallcc %struct.A* @"\01??0A@@QAE@XZ"(%struct.A* %[[arg1]])
48f4a2713aSLionel Sambuc // WIN32:   store i1 true, i1* %[[isactive]]
49*0a6a1f1dSLionel Sambuc //
50*0a6a1f1dSLionel Sambuc // WIN32:   %[[arg0:.*]] = getelementptr inbounds [[argmem_ty]]* %[[argmem]], i32 0, i32 0
51f4a2713aSLionel Sambuc // WIN32:   invoke x86_thiscallcc %struct.A* @"\01??0A@@QAE@XZ"
52f4a2713aSLionel Sambuc // WIN32:   invoke void @"\01?TakeRef@@YAXABUA@@@Z"
53f4a2713aSLionel Sambuc // WIN32:   invoke x86_thiscallcc %struct.A* @"\01??0A@@QAE@XZ"
54f4a2713aSLionel Sambuc // WIN32:   store i1 false, i1* %[[isactive]]
55*0a6a1f1dSLionel Sambuc //
56*0a6a1f1dSLionel Sambuc // WIN32:   invoke i32 @"\01?TakesTwo@@YAHUA@@0@Z"([[argmem_ty]]* inalloca %[[argmem]])
57*0a6a1f1dSLionel Sambuc // WIN32:   call void @llvm.stackrestore
58f4a2713aSLionel Sambuc //        Destroy the two const ref temporaries.
59f4a2713aSLionel Sambuc // WIN32:   invoke x86_thiscallcc void @"\01??1A@@QAE@XZ"
60*0a6a1f1dSLionel Sambuc // WIN32:   invoke x86_thiscallcc void @"\01??1A@@QAE@XZ"
61f4a2713aSLionel Sambuc // WIN32:   ret i32
62f4a2713aSLionel Sambuc //
63f4a2713aSLionel Sambuc //        Conditionally destroy arg1.
64f4a2713aSLionel Sambuc // WIN32:   %[[cond:.*]] = load i1* %[[isactive]]
65f4a2713aSLionel Sambuc // WIN32:   br i1 %[[cond]]
66f4a2713aSLionel Sambuc // WIN32:   invoke x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* %[[arg1]])
67f4a2713aSLionel Sambuc // WIN32: }
68f4a2713aSLionel Sambuc 
69f4a2713aSLionel Sambuc // Test putting the cleanups inside a conditional.
70f4a2713aSLionel Sambuc int CouldThrow();
HasConditionalCleanup(bool cond)71f4a2713aSLionel Sambuc int HasConditionalCleanup(bool cond) {
72f4a2713aSLionel Sambuc   return (cond ? TakesTwo(A(), A()) : CouldThrow());
73f4a2713aSLionel Sambuc }
74f4a2713aSLionel Sambuc 
75*0a6a1f1dSLionel Sambuc // WIN32-LABEL: define i32 @"\01?HasConditionalCleanup@@YAH_N@Z"(i1 zeroext %{{.*}}) {{.*}} {
76f4a2713aSLionel Sambuc // WIN32:   store i1 false
77f4a2713aSLionel Sambuc // WIN32:   br i1
78*0a6a1f1dSLionel Sambuc // WIN32:   call i8* @llvm.stacksave()
79f4a2713aSLionel Sambuc // WIN32:   invoke x86_thiscallcc %struct.A* @"\01??0A@@QAE@XZ"(%struct.A* %{{.*}})
80*0a6a1f1dSLionel Sambuc // WIN32:   store i1 true
81*0a6a1f1dSLionel Sambuc // WIN32:   invoke x86_thiscallcc %struct.A* @"\01??0A@@QAE@XZ"(%struct.A* %{{.*}})
82*0a6a1f1dSLionel Sambuc // WIN32:   invoke i32 @"\01?TakesTwo@@YAHUA@@0@Z"
83*0a6a1f1dSLionel Sambuc // WIN32:   call void @llvm.stackrestore
84*0a6a1f1dSLionel Sambuc //
85f4a2713aSLionel Sambuc // WIN32:   call i32 @"\01?CouldThrow@@YAHXZ"()
86*0a6a1f1dSLionel Sambuc //
87*0a6a1f1dSLionel Sambuc //        Only one dtor in the invoke for arg1
88*0a6a1f1dSLionel Sambuc // WIN32:   invoke x86_thiscallcc void @"\01??1A@@QAE@XZ"({{.*}})
89*0a6a1f1dSLionel Sambuc // WIN32-NOT: invoke x86_thiscallcc void @"\01??1A@@QAE@XZ"
90*0a6a1f1dSLionel Sambuc // WIN32:   call void @llvm.stackrestore
91f4a2713aSLionel Sambuc // WIN32: }
92f4a2713aSLionel Sambuc 
93f4a2713aSLionel Sambuc // Now test both.
HasConditionalDeactivatedCleanups(bool cond)94f4a2713aSLionel Sambuc int HasConditionalDeactivatedCleanups(bool cond) {
95f4a2713aSLionel Sambuc   return (cond ? TakesTwo((TakeRef(A()), A()), (TakeRef(A()), A())) : CouldThrow());
96f4a2713aSLionel Sambuc }
97f4a2713aSLionel Sambuc 
98*0a6a1f1dSLionel Sambuc // WIN32-LABEL: define i32 @"\01?HasConditionalDeactivatedCleanups@@YAH_N@Z"{{.*}} {
99f4a2713aSLionel Sambuc // WIN32:   alloca i1
100f4a2713aSLionel Sambuc // WIN32:   %[[arg1_cond:.*]] = alloca i1
101f4a2713aSLionel Sambuc //        Start all four cleanups as deactivated.
102f4a2713aSLionel Sambuc // WIN32:   store i1 false
103f4a2713aSLionel Sambuc // WIN32:   store i1 false
104f4a2713aSLionel Sambuc // WIN32:   store i1 false
105f4a2713aSLionel Sambuc // WIN32:   store i1 false
106f4a2713aSLionel Sambuc // WIN32:   br i1
107f4a2713aSLionel Sambuc //        True condition.
108*0a6a1f1dSLionel Sambuc // WIN32:   invoke x86_thiscallcc %struct.A* @"\01??0A@@QAE@XZ"
109f4a2713aSLionel Sambuc // WIN32:   store i1 true
110f4a2713aSLionel Sambuc // WIN32:   invoke void @"\01?TakeRef@@YAXABUA@@@Z"
111*0a6a1f1dSLionel Sambuc // WIN32:   invoke x86_thiscallcc %struct.A* @"\01??0A@@QAE@XZ"
112f4a2713aSLionel Sambuc // WIN32:   store i1 true, i1* %[[arg1_cond]]
113f4a2713aSLionel Sambuc // WIN32:   invoke x86_thiscallcc %struct.A* @"\01??0A@@QAE@XZ"
114f4a2713aSLionel Sambuc // WIN32:   store i1 true
115f4a2713aSLionel Sambuc // WIN32:   invoke void @"\01?TakeRef@@YAXABUA@@@Z"
116f4a2713aSLionel Sambuc // WIN32:   invoke x86_thiscallcc %struct.A* @"\01??0A@@QAE@XZ"
117f4a2713aSLionel Sambuc // WIN32:   store i1 true
118f4a2713aSLionel Sambuc // WIN32:   store i1 false, i1* %[[arg1_cond]]
119f4a2713aSLionel Sambuc // WIN32:   invoke i32 @"\01?TakesTwo@@YAHUA@@0@Z"
120f4a2713aSLionel Sambuc //        False condition.
121f4a2713aSLionel Sambuc // WIN32:   invoke i32 @"\01?CouldThrow@@YAHXZ"()
122f4a2713aSLionel Sambuc //        Two normal cleanups for TakeRef args.
123f4a2713aSLionel Sambuc // WIN32:   invoke x86_thiscallcc void @"\01??1A@@QAE@XZ"
124*0a6a1f1dSLionel Sambuc // WIN32:   invoke x86_thiscallcc void @"\01??1A@@QAE@XZ"
125f4a2713aSLionel Sambuc // WIN32:   ret i32
126f4a2713aSLionel Sambuc //
127f4a2713aSLionel Sambuc //        Somewhere in the landing pad soup, we conditionally destroy arg1.
128f4a2713aSLionel Sambuc // WIN32:   %[[isactive:.*]] = load i1* %[[arg1_cond]]
129f4a2713aSLionel Sambuc // WIN32:   br i1 %[[isactive]]
130*0a6a1f1dSLionel Sambuc // WIN32:   invoke x86_thiscallcc void @"\01??1A@@QAE@XZ"
131f4a2713aSLionel Sambuc // WIN32: }
132f4a2713aSLionel Sambuc 
133f4a2713aSLionel Sambuc namespace crash_on_partial_destroy {
134f4a2713aSLionel Sambuc struct A {
135f4a2713aSLionel Sambuc   virtual ~A();
136f4a2713aSLionel Sambuc };
137f4a2713aSLionel Sambuc 
138f4a2713aSLionel Sambuc struct B : virtual A {
139f4a2713aSLionel Sambuc   // Has an implicit destructor.
140f4a2713aSLionel Sambuc };
141f4a2713aSLionel Sambuc 
142f4a2713aSLionel Sambuc struct C : B {
143f4a2713aSLionel Sambuc   C();
144f4a2713aSLionel Sambuc };
145f4a2713aSLionel Sambuc 
146f4a2713aSLionel Sambuc void foo();
147f4a2713aSLionel Sambuc // We used to crash when emitting this.
C()148f4a2713aSLionel Sambuc C::C() { foo(); }
149f4a2713aSLionel Sambuc 
150f4a2713aSLionel Sambuc // Verify that we don't bother with a vbtable lookup when adjusting the this
151f4a2713aSLionel Sambuc // pointer to call a base destructor from a constructor while unwinding.
152f4a2713aSLionel Sambuc // WIN32-LABEL: define {{.*}} @"\01??0C@crash_on_partial_destroy@@QAE@XZ"{{.*}} {
153f4a2713aSLionel Sambuc // WIN32:      landingpad
154f4a2713aSLionel Sambuc //
155f4a2713aSLionel Sambuc //        We shouldn't do any vbptr loads, just constant GEPs.
156f4a2713aSLionel Sambuc // WIN32-NOT:  load
157f4a2713aSLionel Sambuc // WIN32:      getelementptr i8* %{{.*}}, i32 4
158f4a2713aSLionel Sambuc // WIN32-NOT:  load
159f4a2713aSLionel Sambuc // WIN32:      bitcast i8* %{{.*}} to %"struct.crash_on_partial_destroy::B"*
160f4a2713aSLionel Sambuc // WIN32:      invoke x86_thiscallcc void @"\01??1B@crash_on_partial_destroy@@UAE@XZ"
161f4a2713aSLionel Sambuc //
162f4a2713aSLionel Sambuc // WIN32-NOT:  load
163f4a2713aSLionel Sambuc // WIN32:      bitcast %"struct.crash_on_partial_destroy::C"* %{{.*}} to i8*
164f4a2713aSLionel Sambuc // WIN32-NOT:  load
165f4a2713aSLionel Sambuc // WIN32:      getelementptr inbounds i8* %{{.*}}, i64 4
166f4a2713aSLionel Sambuc // WIN32-NOT:  load
167f4a2713aSLionel Sambuc // WIN32:      bitcast i8* %{{.*}} to %"struct.crash_on_partial_destroy::A"*
168f4a2713aSLionel Sambuc // WIN32:      invoke x86_thiscallcc void @"\01??1A@crash_on_partial_destroy@@UAE@XZ"
169f4a2713aSLionel Sambuc // WIN32: }
170f4a2713aSLionel Sambuc }
171