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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// These classes support the generation of CIR for cleanups, initially based
// on LLVM IR cleanup handling, but ought to change as CIR evolves.
//
//===----------------------------------------------------------------------===//
#ifndef CLANG_LIB_CIR_CODEGEN_CIRGENCLEANUP_H
#define CLANG_LIB_CIR_CODEGEN_CIRGENCLEANUP_H
#include "Address.h"
#include "CIRGenModule.h"
#include "EHScopeStack.h"
#include "mlir/IR/Value.h"
#include "clang/AST/StmtCXX.h"
namespace clang::CIRGen {
/// The MS C++ ABI needs a pointer to RTTI data plus some flags to describe the
/// type of a catch handler, so we use this wrapper.
struct CatchTypeInfo {
mlir::TypedAttr rtti;
unsigned flags;
};
/// A protected scope for zero-cost EH handling.
class EHScope {
EHScopeStack::stable_iterator enclosingEHScope;
class CommonBitFields {
friend class EHScope;
unsigned kind : 3;
};
enum { NumCommonBits = 3 };
bool scopeMayThrow;
protected:
class CatchBitFields {
friend class EHCatchScope;
unsigned : NumCommonBits;
unsigned numHandlers : 32 - NumCommonBits;
};
class CleanupBitFields {
friend class EHCleanupScope;
unsigned : NumCommonBits;
/// Whether this cleanup needs to be run along normal edges.
unsigned isNormalCleanup : 1;
/// Whether this cleanup needs to be run along exception edges.
unsigned isEHCleanup : 1;
/// Whether this cleanup is currently active.
unsigned isActive : 1;
/// Whether this cleanup is a lifetime marker
unsigned isLifetimeMarker : 1;
/// Whether the normal cleanup should test the activation flag.
unsigned testFlagInNormalCleanup : 1;
/// Whether the EH cleanup should test the activation flag.
unsigned testFlagInEHCleanup : 1;
/// The amount of extra storage needed by the Cleanup.
/// Always a multiple of the scope-stack alignment.
unsigned cleanupSize : 12;
};
union {
CommonBitFields commonBits;
CatchBitFields catchBits;
CleanupBitFields cleanupBits;
};
public:
enum Kind { Cleanup, Catch, Terminate, Filter };
EHScope(Kind kind, EHScopeStack::stable_iterator enclosingEHScope)
: enclosingEHScope(enclosingEHScope) {
commonBits.kind = kind;
}
Kind getKind() const { return static_cast<Kind>(commonBits.kind); }
bool mayThrow() const {
// Traditional LLVM codegen also checks for `!block->use_empty()`, but
// in CIRGen the block content is not important, just used as a way to
// signal `hasEHBranches`.
return scopeMayThrow;
}
void setMayThrow(bool mayThrow) { scopeMayThrow = mayThrow; }
EHScopeStack::stable_iterator getEnclosingEHScope() const {
return enclosingEHScope;
}
};
/// A scope which attempts to handle some, possibly all, types of
/// exceptions.
///
/// Objective C \@finally blocks are represented using a cleanup scope
/// after the catch scope.
class EHCatchScope : public EHScope {
// In effect, we have a flexible array member
// Handler Handlers[0];
// But that's only standard in C99, not C++, so we have to do
// annoying pointer arithmetic instead.
public:
struct Handler {
/// A type info value, or null MLIR attribute for a catch-all
CatchTypeInfo type;
/// The catch handler for this type.
mlir::Region *region;
/// The catch handler stmt.
const CXXCatchStmt *stmt;
bool isCatchAll() const { return type.rtti == nullptr; }
};
private:
friend class EHScopeStack;
Handler *getHandlers() { return reinterpret_cast<Handler *>(this + 1); }
const Handler *getHandlers() const {
return reinterpret_cast<const Handler *>(this + 1);
}
public:
static size_t getSizeForNumHandlers(unsigned n) {
return sizeof(EHCatchScope) + n * sizeof(Handler);
}
EHCatchScope(unsigned numHandlers,
EHScopeStack::stable_iterator enclosingEHScope)
: EHScope(Catch, enclosingEHScope) {
catchBits.numHandlers = numHandlers;
assert(catchBits.numHandlers == numHandlers && "NumHandlers overflow?");
}
unsigned getNumHandlers() const { return catchBits.numHandlers; }
void setHandler(unsigned i, CatchTypeInfo type, mlir::Region *region,
const CXXCatchStmt *stmt) {
assert(i < getNumHandlers());
Handler *handler = &getHandlers()[i];
handler->type = type;
handler->region = region;
handler->stmt = stmt;
}
const Handler &getHandler(unsigned i) const {
assert(i < getNumHandlers());
return getHandlers()[i];
}
// Clear all handler blocks.
// FIXME: it's better to always call clearHandlerBlocks in DTOR and have a
// 'takeHandler' or some such function which removes ownership from the
// EHCatchScope object if the handlers should live longer than EHCatchScope.
void clearHandlerBlocks() {
// The blocks are owned by TryOp, nothing to delete.
}
using iterator = const Handler *;
iterator begin() const { return getHandlers(); }
iterator end() const { return getHandlers() + getNumHandlers(); }
static bool classof(const EHScope *scope) {
return scope->getKind() == Catch;
}
};
/// A cleanup scope which generates the cleanup blocks lazily.
class alignas(EHScopeStack::ScopeStackAlignment) EHCleanupScope
: public EHScope {
/// The nearest normal cleanup scope enclosing this one.
EHScopeStack::stable_iterator enclosingNormal;
/// The dual entry/exit block along the normal edge. This is lazily
/// created if needed before the cleanup is popped.
mlir::Block *normalBlock = nullptr;
/// The number of fixups required by enclosing scopes (not including
/// this one). If this is the top cleanup scope, all the fixups
/// from this index onwards belong to this scope.
unsigned fixupDepth = 0;
public:
/// Gets the size required for a lazy cleanup scope with the given
/// cleanup-data requirements.
static size_t getSizeForCleanupSize(size_t size) {
return sizeof(EHCleanupScope) + size;
}
size_t getAllocatedSize() const {
return sizeof(EHCleanupScope) + cleanupBits.cleanupSize;
}
EHCleanupScope(unsigned cleanupSize, unsigned fixupDepth,
EHScopeStack::stable_iterator enclosingNormal,
EHScopeStack::stable_iterator enclosingEH)
: EHScope(EHScope::Cleanup, enclosingEH),
enclosingNormal(enclosingNormal), fixupDepth(fixupDepth) {
// TODO(cir): When exception handling is upstreamed, isNormalCleanup and
// isEHCleanup will be arguments to the constructor.
cleanupBits.isNormalCleanup = true;
cleanupBits.isEHCleanup = false;
cleanupBits.isActive = true;
cleanupBits.isLifetimeMarker = false;
cleanupBits.testFlagInNormalCleanup = false;
cleanupBits.testFlagInEHCleanup = false;
cleanupBits.cleanupSize = cleanupSize;
assert(cleanupBits.cleanupSize == cleanupSize && "cleanup size overflow");
}
void destroy() {}
// Objects of EHCleanupScope are not destructed. Use destroy().
~EHCleanupScope() = delete;
mlir::Block *getNormalBlock() const { return normalBlock; }
void setNormalBlock(mlir::Block *bb) { normalBlock = bb; }
bool isNormalCleanup() const { return cleanupBits.isNormalCleanup; }
bool isActive() const { return cleanupBits.isActive; }
void setActive(bool isActive) { cleanupBits.isActive = isActive; }
bool isLifetimeMarker() const { return cleanupBits.isLifetimeMarker; }
unsigned getFixupDepth() const { return fixupDepth; }
EHScopeStack::stable_iterator getEnclosingNormalCleanup() const {
return enclosingNormal;
}
size_t getCleanupSize() const { return cleanupBits.cleanupSize; }
void *getCleanupBuffer() { return this + 1; }
EHScopeStack::Cleanup *getCleanup() {
return reinterpret_cast<EHScopeStack::Cleanup *>(getCleanupBuffer());
}
static bool classof(const EHScope *scope) {
return (scope->getKind() == Cleanup);
}
void markEmitted() {}
};
/// A non-stable pointer into the scope stack.
class EHScopeStack::iterator {
char *ptr = nullptr;
friend class EHScopeStack;
explicit iterator(char *ptr) : ptr(ptr) {}
public:
iterator() = default;
EHScope *get() const { return reinterpret_cast<EHScope *>(ptr); }
EHScope *operator->() const { return get(); }
EHScope &operator*() const { return *get(); }
iterator &operator++() {
size_t size;
switch (get()->getKind()) {
case EHScope::Catch:
size = EHCatchScope::getSizeForNumHandlers(
static_cast<const EHCatchScope *>(get())->getNumHandlers());
break;
case EHScope::Filter:
llvm_unreachable("EHScopeStack::iterator Filter");
break;
case EHScope::Cleanup:
llvm_unreachable("EHScopeStack::iterator Cleanup");
break;
case EHScope::Terminate:
llvm_unreachable("EHScopeStack::iterator Terminate");
break;
}
ptr += llvm::alignTo(size, ScopeStackAlignment);
return *this;
}
bool operator==(iterator other) const { return ptr == other.ptr; }
bool operator!=(iterator other) const { return ptr != other.ptr; }
};
inline EHScopeStack::iterator EHScopeStack::begin() const {
return iterator(startOfData);
}
inline EHScopeStack::iterator EHScopeStack::end() const {
return iterator(endOfBuffer);
}
inline EHScopeStack::iterator
EHScopeStack::find(stable_iterator savePoint) const {
assert(savePoint.isValid() && "finding invalid savepoint");
assert(savePoint.size <= stable_begin().size &&
"finding savepoint after pop");
return iterator(endOfBuffer - savePoint.size);
}
inline void EHScopeStack::popCatch() {
assert(!empty() && "popping exception stack when not empty");
EHCatchScope &scope = llvm::cast<EHCatchScope>(*begin());
innermostEHScope = scope.getEnclosingEHScope();
deallocate(EHCatchScope::getSizeForNumHandlers(scope.getNumHandlers()));
}
/// The exceptions personality for a function.
struct EHPersonality {
const char *personalityFn = nullptr;
// If this is non-null, this personality requires a non-standard
// function for rethrowing an exception after a catchall cleanup.
// This function must have prototype void(void*).
const char *catchallRethrowFn = nullptr;
static const EHPersonality &get(CIRGenModule &cgm,
const clang::FunctionDecl *fd);
static const EHPersonality &get(CIRGenFunction &cgf);
static const EHPersonality GNU_C;
static const EHPersonality GNU_C_SJLJ;
static const EHPersonality GNU_C_SEH;
static const EHPersonality GNU_ObjC;
static const EHPersonality GNU_ObjC_SJLJ;
static const EHPersonality GNU_ObjC_SEH;
static const EHPersonality GNUstep_ObjC;
static const EHPersonality GNU_ObjCXX;
static const EHPersonality NeXT_ObjC;
static const EHPersonality GNU_CPlusPlus;
static const EHPersonality GNU_CPlusPlus_SJLJ;
static const EHPersonality GNU_CPlusPlus_SEH;
static const EHPersonality MSVC_except_handler;
static const EHPersonality MSVC_C_specific_handler;
static const EHPersonality MSVC_CxxFrameHandler3;
static const EHPersonality GNU_Wasm_CPlusPlus;
static const EHPersonality XL_CPlusPlus;
static const EHPersonality ZOS_CPlusPlus;
/// Does this personality use landingpads or the family of pad instructions
/// designed to form funclets?
bool usesFuncletPads() const {
return isMSVCPersonality() || isWasmPersonality();
}
bool isMSVCPersonality() const {
return this == &MSVC_except_handler || this == &MSVC_C_specific_handler ||
this == &MSVC_CxxFrameHandler3;
}
bool isWasmPersonality() const { return this == &GNU_Wasm_CPlusPlus; }
bool isMSVCXXPersonality() const { return this == &MSVC_CxxFrameHandler3; }
};
} // namespace clang::CIRGen
#endif // CLANG_LIB_CIR_CODEGEN_CIRGENCLEANUP_H
|