clang 22.0.0git
CIRGenCleanup.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// These classes support the generation of CIR for cleanups, initially based
10// on LLVM IR cleanup handling, but ought to change as CIR evolves.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef CLANG_LIB_CIR_CODEGEN_CIRGENCLEANUP_H
15#define CLANG_LIB_CIR_CODEGEN_CIRGENCLEANUP_H
16
17#include "Address.h"
18#include "CIRGenModule.h"
19#include "EHScopeStack.h"
20#include "mlir/IR/Value.h"
21
22namespace clang::CIRGen {
23
24/// The MS C++ ABI needs a pointer to RTTI data plus some flags to describe the
25/// type of a catch handler, so we use this wrapper.
27 mlir::TypedAttr rtti;
28 unsigned flags;
29};
30
31/// A protected scope for zero-cost EH handling.
32class EHScope {
33 EHScopeStack::stable_iterator enclosingEHScope;
34
35 class CommonBitFields {
36 friend class EHScope;
37 unsigned kind : 3;
38 };
39 enum { NumCommonBits = 3 };
40
41protected:
43 friend class EHCatchScope;
44 unsigned : NumCommonBits;
45 unsigned numHandlers : 32 - NumCommonBits;
46 };
47
49 friend class EHCleanupScope;
50 unsigned : NumCommonBits;
51
52 /// Whether this cleanup needs to be run along normal edges.
53 unsigned isNormalCleanup : 1;
54
55 /// Whether this cleanup needs to be run along exception edges.
56 unsigned isEHCleanup : 1;
57
58 /// Whether this cleanup is currently active.
59 unsigned isActive : 1;
60
61 /// Whether this cleanup is a lifetime marker
62 unsigned isLifetimeMarker : 1;
63
64 /// Whether the normal cleanup should test the activation flag.
65 unsigned testFlagInNormalCleanup : 1;
66
67 /// Whether the EH cleanup should test the activation flag.
68 unsigned testFlagInEHCleanup : 1;
69
70 /// The amount of extra storage needed by the Cleanup.
71 /// Always a multiple of the scope-stack alignment.
72 unsigned cleanupSize : 12;
73 };
74
75 union {
76 CommonBitFields commonBits;
79 };
80
81public:
83
85 : enclosingEHScope(enclosingEHScope) {
86 commonBits.kind = kind;
87 }
88
89 Kind getKind() const { return static_cast<Kind>(commonBits.kind); }
90
91 bool mayThrow() const {
92 // Traditional LLVM codegen also checks for `!block->use_empty()`, but
93 // in CIRGen the block content is not important, just used as a way to
94 // signal `hasEHBranches`.
96 return false;
97 }
98
100 return enclosingEHScope;
101 }
102};
103
104/// A scope which attempts to handle some, possibly all, types of
105/// exceptions.
106///
107/// Objective C \@finally blocks are represented using a cleanup scope
108/// after the catch scope.
109
110class EHCatchScope : public EHScope {
111 // In effect, we have a flexible array member
112 // Handler Handlers[0];
113 // But that's only standard in C99, not C++, so we have to do
114 // annoying pointer arithmetic instead.
115
116public:
117 struct Handler {
118 /// A type info value, or null MLIR attribute for a catch-all
120
121 /// The catch handler for this type.
122 mlir::Region *region;
123
124 bool isCatchAll() const { return type.rtti == nullptr; }
125 };
126
127private:
128 friend class EHScopeStack;
129
130 Handler *getHandlers() { return reinterpret_cast<Handler *>(this + 1); }
131
132 const Handler *getHandlers() const {
133 return reinterpret_cast<const Handler *>(this + 1);
134 }
135
136public:
137 static size_t getSizeForNumHandlers(unsigned n) {
138 return sizeof(EHCatchScope) + n * sizeof(Handler);
139 }
140
141 EHCatchScope(unsigned numHandlers,
142 EHScopeStack::stable_iterator enclosingEHScope)
143 : EHScope(Catch, enclosingEHScope) {
144 catchBits.numHandlers = numHandlers;
145 assert(catchBits.numHandlers == numHandlers && "NumHandlers overflow?");
146 }
147
148 unsigned getNumHandlers() const { return catchBits.numHandlers; }
149
150 void setHandler(unsigned i, CatchTypeInfo type, mlir::Region *region) {
151 assert(i < getNumHandlers());
152 getHandlers()[i].type = type;
153 getHandlers()[i].region = region;
154 }
155
156 const Handler &getHandler(unsigned i) const {
157 assert(i < getNumHandlers());
158 return getHandlers()[i];
159 }
160
161 // Clear all handler blocks.
162 // FIXME: it's better to always call clearHandlerBlocks in DTOR and have a
163 // 'takeHandler' or some such function which removes ownership from the
164 // EHCatchScope object if the handlers should live longer than EHCatchScope.
166 // The blocks are owned by TryOp, nothing to delete.
167 }
168
169 using iterator = const Handler *;
170 iterator begin() const { return getHandlers(); }
171 iterator end() const { return getHandlers() + getNumHandlers(); }
172
173 static bool classof(const EHScope *scope) {
174 return scope->getKind() == Catch;
175 }
176};
177
178/// A cleanup scope which generates the cleanup blocks lazily.
179class alignas(EHScopeStack::ScopeStackAlignment) EHCleanupScope
180 : public EHScope {
181 /// The nearest normal cleanup scope enclosing this one.
182 EHScopeStack::stable_iterator enclosingNormal;
183
184 /// The dual entry/exit block along the normal edge. This is lazily
185 /// created if needed before the cleanup is popped.
186 mlir::Block *normalBlock = nullptr;
187
188 /// The number of fixups required by enclosing scopes (not including
189 /// this one). If this is the top cleanup scope, all the fixups
190 /// from this index onwards belong to this scope.
191 unsigned fixupDepth = 0;
192
193public:
194 /// Gets the size required for a lazy cleanup scope with the given
195 /// cleanup-data requirements.
196 static size_t getSizeForCleanupSize(size_t size) {
197 return sizeof(EHCleanupScope) + size;
198 }
199
200 size_t getAllocatedSize() const {
201 return sizeof(EHCleanupScope) + cleanupBits.cleanupSize;
202 }
203
204 EHCleanupScope(unsigned cleanupSize, unsigned fixupDepth,
205 EHScopeStack::stable_iterator enclosingNormal,
207 : EHScope(EHScope::Cleanup, enclosingEH),
208 enclosingNormal(enclosingNormal), fixupDepth(fixupDepth) {
209 // TODO(cir): When exception handling is upstreamed, isNormalCleanup and
210 // isEHCleanup will be arguments to the constructor.
211 cleanupBits.isNormalCleanup = true;
212 cleanupBits.isEHCleanup = false;
213 cleanupBits.isActive = true;
214 cleanupBits.isLifetimeMarker = false;
215 cleanupBits.testFlagInNormalCleanup = false;
216 cleanupBits.testFlagInEHCleanup = false;
217 cleanupBits.cleanupSize = cleanupSize;
218
219 assert(cleanupBits.cleanupSize == cleanupSize && "cleanup size overflow");
220 }
221
222 void destroy() {}
223 // Objects of EHCleanupScope are not destructed. Use destroy().
224 ~EHCleanupScope() = delete;
225
226 mlir::Block *getNormalBlock() const { return normalBlock; }
227 void setNormalBlock(mlir::Block *bb) { normalBlock = bb; }
228
229 bool isNormalCleanup() const { return cleanupBits.isNormalCleanup; }
230
231 bool isActive() const { return cleanupBits.isActive; }
232 void setActive(bool isActive) { cleanupBits.isActive = isActive; }
233
234 unsigned getFixupDepth() const { return fixupDepth; }
236 return enclosingNormal;
237 }
238
239 size_t getCleanupSize() const { return cleanupBits.cleanupSize; }
240 void *getCleanupBuffer() { return this + 1; }
241
243 return reinterpret_cast<EHScopeStack::Cleanup *>(getCleanupBuffer());
244 }
245
246 static bool classof(const EHScope *scope) {
247 return (scope->getKind() == Cleanup);
248 }
249
250 void markEmitted() {}
251};
252
253/// A non-stable pointer into the scope stack.
255 char *ptr = nullptr;
256
257 friend class EHScopeStack;
258 explicit iterator(char *ptr) : ptr(ptr) {}
259
260public:
261 iterator() = default;
262
263 EHScope *get() const { return reinterpret_cast<EHScope *>(ptr); }
264
265 EHScope *operator->() const { return get(); }
266 EHScope &operator*() const { return *get(); }
267
268 iterator &operator++() {
269 size_t size;
270 switch (get()->getKind()) {
271 case EHScope::Catch:
273 static_cast<const EHCatchScope *>(get())->getNumHandlers());
274 break;
275
276 case EHScope::Filter:
277 llvm_unreachable("EHScopeStack::iterator Filter");
278 break;
279
280 case EHScope::Cleanup:
281 llvm_unreachable("EHScopeStack::iterator Cleanup");
282 break;
283
285 llvm_unreachable("EHScopeStack::iterator Terminate");
286 break;
287 }
288 ptr += llvm::alignTo(size, ScopeStackAlignment);
289 return *this;
290 }
291
292 bool operator==(iterator other) const { return ptr == other.ptr; }
293 bool operator!=(iterator other) const { return ptr != other.ptr; }
294};
295
297 return iterator(startOfData);
298}
299
301 return iterator(endOfBuffer);
302}
303
306 assert(savePoint.isValid() && "finding invalid savepoint");
307 assert(savePoint.size <= stable_begin().size &&
308 "finding savepoint after pop");
309 return iterator(endOfBuffer - savePoint.size);
310}
311
313 assert(!empty() && "popping exception stack when not empty");
314
315 EHCatchScope &scope = llvm::cast<EHCatchScope>(*begin());
316 innermostEHScope = scope.getEnclosingEHScope();
318}
319
320/// The exceptions personality for a function.
322 const char *personalityFn = nullptr;
323
324 // If this is non-null, this personality requires a non-standard
325 // function for rethrowing an exception after a catchall cleanup.
326 // This function must have prototype void(void*).
327 const char *catchallRethrowFn = nullptr;
328
329 static const EHPersonality &get(CIRGenModule &cgm,
330 const clang::FunctionDecl *fd);
331 static const EHPersonality &get(CIRGenFunction &cgf);
332
333 static const EHPersonality GNU_C;
351
352 /// Does this personality use landingpads or the family of pad instructions
353 /// designed to form funclets?
354 bool usesFuncletPads() const {
356 }
357
358 bool isMSVCPersonality() const {
359 return this == &MSVC_except_handler || this == &MSVC_C_specific_handler ||
360 this == &MSVC_CxxFrameHandler3;
361 }
362
363 bool isWasmPersonality() const { return this == &GNU_Wasm_CPlusPlus; }
364
365 bool isMSVCXXPersonality() const { return this == &MSVC_CxxFrameHandler3; }
366};
367
368} // namespace clang::CIRGen
369#endif // CLANG_LIB_CIR_CODEGEN_CIRGENCLEANUP_H
static Decl::Kind getKind(const Decl *D)
This class organizes the cross-function state that is used while generating CIR code.
A scope which attempts to handle some, possibly all, types of exceptions.
static bool classof(const EHScope *scope)
void setHandler(unsigned i, CatchTypeInfo type, mlir::Region *region)
const Handler & getHandler(unsigned i) const
EHCatchScope(unsigned numHandlers, EHScopeStack::stable_iterator enclosingEHScope)
unsigned getNumHandlers() const
static size_t getSizeForNumHandlers(unsigned n)
EHCleanupScope(unsigned cleanupSize, unsigned fixupDepth, EHScopeStack::stable_iterator enclosingNormal, EHScopeStack::stable_iterator enclosingEH)
mlir::Block * getNormalBlock() const
EHScopeStack::Cleanup * getCleanup()
static bool classof(const EHScope *scope)
static size_t getSizeForCleanupSize(size_t size)
Gets the size required for a lazy cleanup scope with the given cleanup-data requirements.
EHScopeStack::stable_iterator getEnclosingNormalCleanup() const
void setNormalBlock(mlir::Block *bb)
void setActive(bool isActive)
Information for lazily generating a cleanup.
A non-stable pointer into the scope stack.
bool operator!=(iterator other) const
bool operator==(iterator other) const
A saved depth on the scope stack.
void popCatch()
Pops a catch scope off the stack. This is private to CIRGenException.cpp.
iterator find(stable_iterator savePoint) const
Turn a stable reference to a scope depth into a unstable pointer to the EH stack.
stable_iterator stable_begin() const
Create a stable reference to the top of the EH stack.
bool empty() const
Determines whether the exception-scopes stack is empty.
iterator end() const
Returns an iterator pointing to the outermost EH scope.
iterator begin() const
Returns an iterator pointing to the innermost EH scope.
A protected scope for zero-cost EH handling.
CleanupBitFields cleanupBits
CatchBitFields catchBits
EHScopeStack::stable_iterator getEnclosingEHScope() const
CommonBitFields commonBits
EHScope(Kind kind, EHScopeStack::stable_iterator enclosingEHScope)
Represents a function declaration or definition.
Definition Decl.h:2000
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
static bool ehstackBranches()
The MS C++ ABI needs a pointer to RTTI data plus some flags to describe the type of a catch handler,...
mlir::Region * region
The catch handler for this type.
CatchTypeInfo type
A type info value, or null MLIR attribute for a catch-all.
The exceptions personality for a function.
bool usesFuncletPads() const
Does this personality use landingpads or the family of pad instructions designed to form funclets?
static const EHPersonality XL_CPlusPlus
static const EHPersonality GNU_ObjC_SJLJ
static const EHPersonality ZOS_CPlusPlus
static const EHPersonality GNUstep_ObjC
static const EHPersonality MSVC_CxxFrameHandler3
static const EHPersonality MSVC_C_specific_handler
static const EHPersonality GNU_CPlusPlus_SEH
static const EHPersonality GNU_ObjC
static const EHPersonality GNU_CPlusPlus_SJLJ
static const EHPersonality GNU_C_SJLJ
static const EHPersonality GNU_C
static const EHPersonality NeXT_ObjC
static const EHPersonality & get(CIRGenModule &cgm, const clang::FunctionDecl *fd)
static const EHPersonality GNU_CPlusPlus
static const EHPersonality GNU_ObjCXX
static const EHPersonality GNU_C_SEH
static const EHPersonality MSVC_except_handler
static const EHPersonality GNU_ObjC_SEH
static const EHPersonality GNU_Wasm_CPlusPlus