Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CodeGenCXX / stack-reuse-exceptions.cpp
blobe036bcaf729a6bd643aeb9c2071ead156697d436
1 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu %s -o - -emit-llvm -O1 \
2 // RUN: -fexceptions -fcxx-exceptions -mllvm -simplifycfg-sink-common=false | FileCheck %s
3 //
4 // We should emit lifetime.ends for these temporaries in both the 'exception'
5 // and 'normal' paths in functions.
6 //
7 // -O1 is necessary to make lifetime markers appear.
9 struct Large {
10 int cs[32];
13 Large getLarge();
15 // Used to ensure we emit invokes.
16 struct NontrivialDtor {
17 int i;
18 ~NontrivialDtor();
21 // CHECK-LABEL: define{{.*}} void @_Z33cleanupsAreEmittedWithoutTryCatchv
22 void cleanupsAreEmittedWithoutTryCatch() {
23 // CHECK: call void @llvm.lifetime.start.p0({{[^,]+}}, ptr nonnull %[[CLEAN:.*]])
24 // CHECK: call void @llvm.lifetime.start.p0({{[^,]+}}, ptr nonnull %[[T1:.*]])
25 // CHECK-NEXT: invoke void @_Z8getLargev
26 // CHECK-NEXT: to label %[[CONT:[^ ]+]] unwind label %[[LPAD:[^ ]+]]
28 // CHECK: [[CONT]]:
29 // CHECK: call void @llvm.lifetime.end.p0({{[^,]+}}, ptr nonnull %[[T1]])
30 // CHECK: call void @llvm.lifetime.start.p0({{[^,]+}}, ptr nonnull %[[T2:.*]])
31 // CHECK-NEXT: invoke void @_Z8getLargev
32 // CHECK-NEXT: to label %[[CONT2:[^ ]+]] unwind label %[[LPAD2:.+]]
34 // CHECK: [[CONT2]]:
35 // CHECK: call void @llvm.lifetime.end.p0({{[^,]+}}, ptr nonnull %[[T2]])
36 // CHECK: call void @llvm.lifetime.end.p0({{[^,]+}}, ptr nonnull %[[CLEAN]])
37 // CHECK: ret void
39 // CHECK: [[LPAD]]:
40 // CHECK: call void @llvm.lifetime.end.p0({{[^,]+}}, ptr nonnull %[[T1]])
41 // CHECK: br label %[[EHCLEANUP:.+]]
43 // CHECK: [[LPAD2]]:
44 // CHECK: call void @llvm.lifetime.end.p0({{[^,]+}}, ptr nonnull %[[T2]])
45 // CHECK: br label %[[EHCLEANUP]]
47 // CHECK: [[EHCLEANUP]]:
48 // CHECK: call void @llvm.lifetime.end.p0({{[^,]+}}, ptr nonnull %[[CLEAN]])
50 NontrivialDtor clean;
52 getLarge();
53 getLarge();
56 // CHECK-LABEL: define{{.*}} void @_Z30cleanupsAreEmittedWithTryCatchv
57 void cleanupsAreEmittedWithTryCatch() {
58 // CHECK: call void @llvm.lifetime.start.p0({{[^,]+}}, ptr nonnull %[[CLEAN:.*]])
59 // CHECK: call void @llvm.lifetime.start.p0({{[^,]+}}, ptr nonnull %[[T1:.*]])
60 // CHECK-NEXT: invoke void @_Z8getLargev
61 // CHECK-NEXT: to label %[[CONT:[^ ]+]] unwind label %[[LPAD:[^ ]+]]
63 // CHECK: [[CONT]]:
64 // CHECK: call void @llvm.lifetime.end.p0({{[^,]+}}, ptr nonnull %[[T1]])
65 // CHECK: call void @llvm.lifetime.start.p0({{[^,]+}}, ptr nonnull %[[T2:.*]])
66 // CHECK-NEXT: invoke void @_Z8getLargev
67 // CHECK-NEXT: to label %[[CONT2:[^ ]+]] unwind label %[[LPAD2:.+]]
69 // CHECK: [[CONT2]]:
70 // CHECK: call void @llvm.lifetime.end.p0({{[^,]+}}, ptr nonnull %[[T2]])
71 // CHECK: br label %[[TRY_CONT:.+]]
73 // CHECK: [[LPAD]]:
74 // CHECK: call void @llvm.lifetime.end.p0({{[^,]+}}, ptr nonnull %[[T1]])
75 // CHECK: br label %[[CATCH:.+]]
77 // CHECK: [[LPAD2]]:
78 // CHECK: call void @llvm.lifetime.end.p0({{[^,]+}}, ptr nonnull %[[T2]])
79 // CHECK: br label %[[CATCH]]
81 // CHECK: [[CATCH]]:
82 // CHECK-NOT: call void @llvm.lifetime
83 // CHECK: invoke void
84 // CHECK-NEXT: to label %[[TRY_CONT]] unwind label %[[OUTER_LPAD:.+]]
86 // CHECK: [[TRY_CONT]]:
87 // CHECK: call void @llvm.lifetime.start.p0({{[^,]+}}, ptr nonnull %[[T_OUTER:.*]])
88 // CHECK-NEXT: invoke void @_Z8getLargev
89 // CHECK-NEXT: to label %[[OUTER_CONT:[^ ]+]] unwind label %[[OUTER_LPAD2:.+]]
91 // CHECK: [[OUTER_CONT]]:
92 // CHECK: call void @llvm.lifetime.end.p0({{[^,]+}}, ptr nonnull %[[T_OUTER]])
93 // CHECK: call void @llvm.lifetime.end.p0({{[^,]+}}, ptr nonnull %[[CLEAN]])
94 // CHECK: ret void
96 // CHECK: [[OUTER_LPAD]]:
97 // CHECK-NOT: call void @llvm.lifetime
98 // CHECK: br label %[[EHCLEANUP:.+]]
100 // CHECK: [[OUTER_LPAD2]]:
101 // CHECK: call void @llvm.lifetime.end.p0({{[^,]+}}, ptr nonnull %[[T_OUTER]])
102 // CHECK: br label %[[EHCLEANUP]]
104 // CHECK: [[EHCLEANUP]]:
105 // CHECK: call void @llvm.lifetime.end.p0({{[^,]+}}, ptr nonnull %[[CLEAN]])
107 NontrivialDtor clean;
109 try {
110 getLarge();
111 getLarge();
112 } catch (...) {}
114 getLarge();
117 // CHECK-LABEL: define{{.*}} void @_Z39cleanupInTryHappensBeforeCleanupInCatchv
118 void cleanupInTryHappensBeforeCleanupInCatch() {
119 // CHECK: call void @llvm.lifetime.start.p0({{[^,]+}}, ptr nonnull %[[T1:.*]])
120 // CHECK-NEXT: invoke void @_Z8getLargev
121 // CHECK-NEXT: to label %[[CONT:[^ ]+]] unwind label %[[LPAD:[^ ]+]]
123 // CHECK: [[CONT]]:
124 // CHECK: call void @llvm.lifetime.end.p0({{[^,]+}}, ptr nonnull %[[T1]])
125 // CHECK: br label %[[TRY_CONT]]
127 // CHECK: [[LPAD]]:
128 // CHECK: call void @llvm.lifetime.end.p0({{[^,]+}}, ptr nonnull %[[T1]])
129 // CHECK: br i1 {{[^,]+}}, label %[[CATCH_INT_MATCH:[^,]+]], label %[[CATCH_ALL:.+]]
131 // CHECK: [[CATCH_INT_MATCH]]:
132 // CHECK: call void @llvm.lifetime.start.p0({{[^,]+}}, ptr nonnull %[[T2:.*]])
133 // CHECK-NEXT: invoke void @_Z8getLargev
134 // CHECK-NEXT: to label %[[CATCH_INT_CONT:[^ ]+]] unwind label %[[CATCH_INT_LPAD:[^ ]+]]
136 // CHECK: [[CATCH_INT_CONT]]:
137 // CHECK: call void @llvm.lifetime.end.p0({{[^,]+}}, ptr nonnull %[[T2]])
138 // CHECK: br label %[[TRY_CONT]]
140 // CHECK: [[TRY_CONT]]:
141 // CHECK: ret void
143 // CHECK: [[CATCH_ALL]]:
144 // CHECK: call void @llvm.lifetime.start.p0({{[^,]+}}, ptr nonnull %[[T3:.*]])
145 // CHECK-NEXT: invoke void @_Z8getLargev
146 // CHECK-NEXT: to label %[[CATCH_ALL_CONT:[^ ]+]] unwind label %[[CATCH_ALL_LPAD:[^ ]+]]
148 // CHECK: [[CATCH_ALL_CONT]]:
149 // CHECK: call void @llvm.lifetime.end.p0({{[^,]+}}, ptr nonnull %[[T3]])
150 // CHECK: br label %[[TRY_CONT]]
152 // CHECK: [[CATCH_ALL_LPAD]]:
153 // CHECK: call void @llvm.lifetime.end.p0({{[^,]+}}, ptr nonnull %[[T3]])
155 // CHECK: [[CATCH_INT_LPAD]]:
156 // CHECK: call void @llvm.lifetime.end.p0({{[^,]+}}, ptr nonnull %[[T2]])
157 // CHECK-NOT: call void @llvm.lifetime
159 try {
160 getLarge();
161 } catch (const int &) {
162 getLarge();
163 } catch (...) {
164 getLarge();
168 // FIXME: We don't currently emit lifetime markers for aggregate by-value
169 // temporaries (e.g. given a function `Large combine(Large, Large);`
170 // combine(getLarge(), getLarge()) "leaks" two `Large`s). We probably should. We
171 // also don't emit markers for things like:
173 // {
174 // Large L = getLarge();
175 // combine(L, L);
176 // }
178 // Though this arguably isn't as bad, since we pass a pointer to `L` as one of
179 // the two args.