clang 23.0.0git
EHScopeStack.h
Go to the documentation of this file.
1//===-- EHScopeStack.h - Stack for cleanup IR generation --------*- C++ -*-===//
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 should be the minimum interface required for other parts of
10// CodeGen to emit cleanups. The implementation is in CGCleanup.cpp and other
11// implemenentation details that are not widely needed are in CGCleanup.h.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_LIB_CODEGEN_EHSCOPESTACK_H
16#define LLVM_CLANG_LIB_CODEGEN_EHSCOPESTACK_H
17
18#include "clang/Basic/LLVM.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/IR/BasicBlock.h"
22#include "llvm/IR/Instructions.h"
23#include "llvm/IR/Value.h"
24
25namespace clang {
26namespace CodeGen {
27
28class CodeGenFunction;
29
30/// A branch fixup. These are required when emitting a goto to a
31/// label which hasn't been emitted yet. The goto is optimistically
32/// emitted as a branch to the basic block for the label, and (if it
33/// occurs in a scope with non-trivial cleanups) a fixup is added to
34/// the innermost cleanup. When a (normal) cleanup is popped, any
35/// unresolved fixups in that scope are threaded through the cleanup.
37 /// The block containing the terminator which needs to be modified
38 /// into a switch if this fixup is resolved into the current scope.
39 /// If null, LatestBranch points directly to the destination.
40 llvm::BasicBlock *OptimisticBranchBlock;
41
42 /// The ultimate destination of the branch.
43 ///
44 /// This can be set to null to indicate that this fixup was
45 /// successfully resolved.
46 llvm::BasicBlock *Destination;
47
48 /// The destination index value.
50
51 /// The initial branch of the fixup.
52 llvm::UncondBrInst *InitialBranch;
53};
54
55template <class T> struct InvariantValue {
56 typedef T type;
57 typedef T saved_type;
58 static bool needsSaving(type value) { return false; }
59 static saved_type save(CodeGenFunction &CGF, type value) { return value; }
60 static type restore(CodeGenFunction &CGF, saved_type value) { return value; }
61};
62
63/// A metaprogramming class for ensuring that a value will dominate an
64/// arbitrary position in a function.
65template <class T> struct DominatingValue : InvariantValue<T> {};
66
67template <class T, bool mightBeInstruction =
68 std::is_base_of<llvm::Value, T>::value &&
69 !std::is_base_of<llvm::Constant, T>::value &&
70 !std::is_base_of<llvm::BasicBlock, T>::value>
72template <class T> struct DominatingPointer<T,false> : InvariantValue<T*> {};
73// template <class T> struct DominatingPointer<T,true> at end of file
74
75template <class T> struct DominatingValue<T*> : DominatingPointer<T> {};
76
77enum CleanupKind : unsigned {
78 /// Denotes a cleanup that should run when a scope is exited using exceptional
79 /// control flow (a throw statement leading to stack unwinding, ).
80 EHCleanup = 0x1,
81
82 /// Denotes a cleanup that should run when a scope is exited using normal
83 /// control flow (falling off the end of the scope, return, goto, ...).
85
87
90
91 // FakeUse needs to be recognized as a special cleanup similar to lifetime
92 // markers chiefly to be ignored in most contexts.
93 FakeUse = 0x10,
95
98};
99
100/// A stack of scopes which respond to exceptions, including cleanups
101/// and catch blocks.
103public:
104 /* Should switch to alignof(uint64_t) instead of 8, when EHCleanupScope can */
106
107 /// A saved depth on the scope stack. This is necessary because
108 /// pushing scopes onto the stack invalidates iterators.
109 class stable_iterator {
110 friend class EHScopeStack;
111
112 /// Offset from StartOfData to EndOfBuffer.
113 ptrdiff_t Size;
114
115 stable_iterator(ptrdiff_t Size) : Size(Size) {}
116
117 public:
118 static stable_iterator invalid() { return stable_iterator(-1); }
119 stable_iterator() : Size(-1) {}
120
121 bool isValid() const { return Size >= 0; }
122
123 /// Returns true if this scope encloses I.
124 /// Returns false if I is invalid.
125 /// This scope must be valid.
126 bool encloses(stable_iterator I) const { return Size <= I.Size; }
127
128 /// Returns true if this scope strictly encloses I: that is,
129 /// if it encloses I and is not I.
130 /// Returns false is I is invalid.
131 /// This scope must be valid.
132 bool strictlyEncloses(stable_iterator I) const { return Size < I.Size; }
133
134 friend bool operator==(stable_iterator A, stable_iterator B) {
135 return A.Size == B.Size;
136 }
137 friend bool operator!=(stable_iterator A, stable_iterator B) {
138 return A.Size != B.Size;
139 }
140 };
141
142 /// Information for lazily generating a cleanup. Subclasses must be
143 /// POD-like: cleanups will not be destructed, and they will be
144 /// allocated on the cleanup stack and freely copied and moved
145 /// around.
146 ///
147 /// Cleanup implementations should generally be declared in an
148 /// anonymous namespace.
149 class LLVM_MOVABLE_POLYMORPHIC_TYPE alignas(uint64_t) Cleanup {
150 // Anchor the construction vtable.
151 virtual void anchor();
152
153 protected:
154 ~Cleanup() = default;
155
156 public:
157 Cleanup(const Cleanup &) = default;
158 Cleanup(Cleanup &&) {}
159
160 // The copy and move assignment operator is defined as deleted pending
161 // further motivation.
162 Cleanup &operator=(const Cleanup &) = delete;
163 Cleanup &operator=(Cleanup &&) = delete;
164
165 Cleanup() = default;
166
167 virtual bool isRedundantBeforeReturn() { return false; }
168
169 /// Generation flags.
170 class Flags {
171 enum {
172 F_IsForEH = 0x1,
173 F_IsNormalCleanupKind = 0x2,
174 F_IsEHCleanupKind = 0x4,
175 F_HasExitSwitch = 0x8,
176 };
177 unsigned flags = 0;
178
179 public:
180 Flags() = default;
181
182 /// isForEH - true if the current emission is for an EH cleanup.
183 bool isForEHCleanup() const { return flags & F_IsForEH; }
184 bool isForNormalCleanup() const { return !isForEHCleanup(); }
185 void setIsForEHCleanup() { flags |= F_IsForEH; }
186
187 bool isNormalCleanupKind() const { return flags & F_IsNormalCleanupKind; }
188 void setIsNormalCleanupKind() { flags |= F_IsNormalCleanupKind; }
189
190 /// isEHCleanupKind - true if the cleanup was pushed as an EH
191 /// cleanup.
192 bool isEHCleanupKind() const { return flags & F_IsEHCleanupKind; }
193 void setIsEHCleanupKind() { flags |= F_IsEHCleanupKind; }
194
195 bool hasExitSwitch() const { return flags & F_HasExitSwitch; }
196 void setHasExitSwitch() { flags |= F_HasExitSwitch; }
197 };
198
199 /// Emit the cleanup. For normal cleanups, this is run in the
200 /// same EH context as when the cleanup was pushed, i.e. the
201 /// immediately-enclosing context of the cleanup scope. For
202 /// EH cleanups, this is run in a terminate context.
203 ///
204 // \param flags cleanup kind.
205 virtual void Emit(CodeGenFunction &CGF, Flags flags) = 0;
206 };
207
208 /// ConditionalCleanup stores the saved form of its parameters,
209 /// then restores them and performs the cleanup.
210 template <class T, class... As>
211 class ConditionalCleanup final : public Cleanup {
212 typedef std::tuple<typename DominatingValue<As>::saved_type...> SavedTuple;
213 SavedTuple Saved;
214
215 template <std::size_t... Is>
216 T restore(CodeGenFunction &CGF, std::index_sequence<Is...>) {
217 // It's important that the restores are emitted in order. The braced init
218 // list guarantees that.
219 return T{DominatingValue<As>::restore(CGF, std::get<Is>(Saved))...};
220 }
221
222 void Emit(CodeGenFunction &CGF, Flags flags) override {
223 restore(CGF, std::index_sequence_for<As...>()).Emit(CGF, flags);
224 }
225
226 public:
228 : Saved(A...) {}
229
230 ConditionalCleanup(SavedTuple Tuple) : Saved(std::move(Tuple)) {}
231 };
232
233private:
234 // The implementation for this class is in CGException.h and
235 // CGException.cpp; the definition is here because it's used as a
236 // member of CodeGenFunction.
237
238 /// The start of the scope-stack buffer, i.e. the allocated pointer
239 /// for the buffer. All of these pointers are either simultaneously
240 /// null or simultaneously valid.
241 char *StartOfBuffer;
242
243 /// The end of the buffer.
244 char *EndOfBuffer;
245
246 /// The first valid entry in the buffer.
247 char *StartOfData;
248
249 /// The innermost normal cleanup on the stack.
250 stable_iterator InnermostNormalCleanup;
251
252 /// The innermost EH scope on the stack.
253 stable_iterator InnermostEHScope;
254
255 /// The CGF this Stack belong to
256 CodeGenFunction* CGF;
257
258 /// The current set of branch fixups. A branch fixup is a jump to
259 /// an as-yet unemitted label, i.e. a label for which we don't yet
260 /// know the EH stack depth. Whenever we pop a cleanup, we have
261 /// to thread all the current branch fixups through it.
262 ///
263 /// Fixups are recorded as the Use of the respective branch or
264 /// switch statement. The use points to the final destination.
265 /// When popping out of a cleanup, these uses are threaded through
266 /// the cleanup and adjusted to point to the new cleanup.
267 ///
268 /// Note that branches are allowed to jump into protected scopes
269 /// in certain situations; e.g. the following code is legal:
270 /// struct A { ~A(); }; // trivial ctor, non-trivial dtor
271 /// goto foo;
272 /// A a;
273 /// foo:
274 /// bar();
275 SmallVector<BranchFixup, 8> BranchFixups;
276
277 char *allocate(size_t Size);
278 void deallocate(size_t Size);
279
280 void *pushCleanup(CleanupKind K, size_t DataSize);
281
282public:
284 : StartOfBuffer(nullptr), EndOfBuffer(nullptr), StartOfData(nullptr),
285 InnermostNormalCleanup(stable_end()), InnermostEHScope(stable_end()),
286 CGF(nullptr) {}
287 ~EHScopeStack() { delete[] StartOfBuffer; }
288
289 EHScopeStack(const EHScopeStack &) = delete;
291
292 /// Push a lazily-created cleanup on the stack.
293 template <class T, class... As> void pushCleanup(CleanupKind Kind, As... A) {
294 static_assert(alignof(T) <= ScopeStackAlignment,
295 "Cleanup's alignment is too large.");
296 void *Buffer = pushCleanup(Kind, sizeof(T));
297 Cleanup *Obj = new (Buffer) T(A...);
298 (void) Obj;
299 }
300
301 /// Push a lazily-created cleanup on the stack. Tuple version.
302 template <class T, class... As>
303 void pushCleanupTuple(CleanupKind Kind, std::tuple<As...> A) {
304 static_assert(alignof(T) <= ScopeStackAlignment,
305 "Cleanup's alignment is too large.");
306 void *Buffer = pushCleanup(Kind, sizeof(T));
307 Cleanup *Obj = new (Buffer) T(std::move(A));
308 (void) Obj;
309 }
310
311 // Feel free to add more variants of the following:
312
313 /// Push a cleanup with non-constant storage requirements on the
314 /// stack. The cleanup type must provide an additional static method:
315 /// static size_t getExtraSize(size_t);
316 /// The argument to this method will be the value N, which will also
317 /// be passed as the first argument to the constructor.
318 ///
319 /// The data stored in the extra storage must obey the same
320 /// restrictions as normal cleanup member data.
321 ///
322 /// The pointer returned from this method is valid until the cleanup
323 /// stack is modified.
324 template <class T, class... As>
325 T *pushCleanupWithExtra(CleanupKind Kind, size_t N, As... A) {
326 static_assert(alignof(T) <= ScopeStackAlignment,
327 "Cleanup's alignment is too large.");
328 void *Buffer = pushCleanup(Kind, sizeof(T) + T::getExtraSize(N));
329 return new (Buffer) T(N, A...);
330 }
331
332 void pushCopyOfCleanup(CleanupKind Kind, const void *Cleanup, size_t Size) {
333 void *Buffer = pushCleanup(Kind, Size);
334 std::memcpy(Buffer, Cleanup, Size);
335 }
336
337 void setCGF(CodeGenFunction *inCGF) { CGF = inCGF; }
338
339 /// Pops a cleanup scope off the stack. This is private to CGCleanup.cpp.
341
342 /// Push a set of catch handlers on the stack. The catch is
343 /// uninitialized and will need to have the given number of handlers
344 /// set on it.
345 class EHCatchScope *pushCatch(unsigned NumHandlers);
346
347 /// Pops a catch scope off the stack. This is private to CGException.cpp.
348 void popCatch();
349
350 /// Push an exceptions filter on the stack.
351 class EHFilterScope *pushFilter(unsigned NumFilters);
352
353 /// Pops an exceptions filter off the stack.
354 void popFilter();
355
356 /// Push a terminate handler on the stack.
358
359 /// Pops a terminate handler off the stack.
360 void popTerminate();
361
362 // Returns true iff the current scope is either empty or contains only
363 // noop cleanups, i.e. lifetime markers and fake uses.
365
366 /// Determines whether the exception-scopes stack is empty.
367 bool empty() const { return StartOfData == EndOfBuffer; }
368
369 bool requiresLandingPad() const;
370
371 /// Determines whether there are any normal cleanups on the stack.
372 bool hasNormalCleanups() const {
373 return InnermostNormalCleanup != stable_end();
374 }
375
376 /// Returns the innermost normal cleanup on the stack, or
377 /// stable_end() if there are no normal cleanups.
379 return InnermostNormalCleanup;
380 }
382
384 return InnermostEHScope;
385 }
386
387
388 /// An unstable reference to a scope-stack depth. Invalidated by
389 /// pushes but not pops.
390 class iterator;
391
392 /// Returns an iterator pointing to the innermost EH scope.
393 iterator begin() const;
394
395 /// Returns an iterator pointing to the outermost EH scope.
396 iterator end() const;
397
398 /// Create a stable reference to the top of the EH stack. The
399 /// returned reference is valid until that scope is popped off the
400 /// stack.
402 return stable_iterator(EndOfBuffer - StartOfData);
403 }
404
405 /// Create a stable reference to the bottom of the EH stack.
407 return stable_iterator(0);
408 }
409
410 /// Translates an iterator into a stable_iterator.
411 stable_iterator stabilize(iterator it) const;
412
413 /// Turn a stable reference to a scope depth into a unstable pointer
414 /// to the EH stack.
415 iterator find(stable_iterator save) const;
416
417 /// Add a branch fixup to the current cleanup scope.
419 assert(hasNormalCleanups() && "adding fixup in scope without cleanups");
420 BranchFixups.push_back(BranchFixup());
421 return BranchFixups.back();
422 }
423
424 unsigned getNumBranchFixups() const { return BranchFixups.size(); }
426 assert(I < getNumBranchFixups());
427 return BranchFixups[I];
428 }
429
430 /// Pops lazily-removed fixups from the end of the list. This
431 /// should only be called by procedures which have just popped a
432 /// cleanup or resolved one or more fixups.
434
435 /// Clears the branch-fixups list. This should only be called by
436 /// ResolveAllBranchFixups.
437 void clearFixups() { BranchFixups.clear(); }
438};
439
440} // namespace CodeGen
441} // namespace clang
442
443#endif
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
__PTRDIFF_TYPE__ ptrdiff_t
A signed integer type that is the result of subtracting two pointers.
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
A scope which attempts to handle some, possibly all, types of exceptions.
Definition CGCleanup.h:165
An exceptions scope which filters exceptions thrown through it.
Definition CGCleanup.h:516
ConditionalCleanup(typename DominatingValue< As >::saved_type... A)
A saved depth on the scope stack.
bool encloses(stable_iterator I) const
Returns true if this scope encloses I.
bool strictlyEncloses(stable_iterator I) const
Returns true if this scope strictly encloses I: that is, if it encloses I and is not I.
friend bool operator==(stable_iterator A, stable_iterator B)
friend bool operator!=(stable_iterator A, stable_iterator B)
void popNullFixups()
Pops lazily-removed fixups from the end of the list.
BranchFixup & getBranchFixup(unsigned I)
stable_iterator getInnermostNormalCleanup() const
Returns the innermost normal cleanup on the stack, or stable_end() if there are no normal cleanups.
EHScopeStack & operator=(const EHScopeStack &)=delete
void setCGF(CodeGenFunction *inCGF)
stable_iterator stable_begin() const
Create a stable reference to the top of the EH stack.
unsigned getNumBranchFixups() const
class EHFilterScope * pushFilter(unsigned NumFilters)
Push an exceptions filter on the stack.
stable_iterator getInnermostEHScope() const
void pushCopyOfCleanup(CleanupKind Kind, const void *Cleanup, size_t Size)
bool empty() const
Determines whether the exception-scopes stack is empty.
void pushCleanup(CleanupKind Kind, As... A)
Push a lazily-created cleanup on the stack.
T * pushCleanupWithExtra(CleanupKind Kind, size_t N, As... A)
Push a cleanup with non-constant storage requirements on the stack.
iterator end() const
Returns an iterator pointing to the outermost EH scope.
Definition CGCleanup.h:634
bool containsOnlyNoopCleanups(stable_iterator Old) const
iterator begin() const
Returns an iterator pointing to the innermost EH scope.
Definition CGCleanup.h:630
void pushTerminate()
Push a terminate handler on the stack.
BranchFixup & addBranchFixup()
Add a branch fixup to the current cleanup scope.
void popCleanup()
Pops a cleanup scope off the stack. This is private to CGCleanup.cpp.
void popCatch()
Pops a catch scope off the stack. This is private to CGException.cpp.
Definition CGCleanup.h:638
iterator find(stable_iterator save) const
Turn a stable reference to a scope depth into a unstable pointer to the EH stack.
Definition CGCleanup.h:654
stable_iterator getInnermostActiveNormalCleanup() const
void popFilter()
Pops an exceptions filter off the stack.
bool hasNormalCleanups() const
Determines whether there are any normal cleanups on the stack.
stable_iterator stabilize(iterator it) const
Translates an iterator into a stable_iterator.
Definition CGCleanup.h:661
static stable_iterator stable_end()
Create a stable reference to the bottom of the EH stack.
void clearFixups()
Clears the branch-fixups list.
void popTerminate()
Pops a terminate handler off the stack.
Definition CGCleanup.h:646
class LLVM_MOVABLE_POLYMORPHIC_TYPE alignas(uint64_t) Cleanup
Information for lazily generating a cleanup.
EHScopeStack(const EHScopeStack &)=delete
void pushCleanupTuple(CleanupKind Kind, std::tuple< As... > A)
Push a lazily-created cleanup on the stack. Tuple version.
class EHCatchScope * pushCatch(unsigned NumHandlers)
Push a set of catch handlers on the stack.
@ NormalCleanup
Denotes a cleanup that should run when a scope is exited using normal control flow (falling off the e...
@ NormalAndEHSEHFinallyCleanup
@ EHCleanup
Denotes a cleanup that should run when a scope is exited using exceptional control flow (a throw stat...
The JSON file list parser is used to communicate input to InstallAPI.
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
#define false
Definition stdbool.h:26
unsigned DestinationIndex
The destination index value.
llvm::BasicBlock * Destination
The ultimate destination of the branch.
llvm::BasicBlock * OptimisticBranchBlock
The block containing the terminator which needs to be modified into a switch if this fixup is resolve...
llvm::UncondBrInst * InitialBranch
The initial branch of the fixup.
A metaprogramming class for ensuring that a value will dominate an arbitrary position in a function.
static bool needsSaving(type value)
static type restore(CodeGenFunction &CGF, saved_type value)
static saved_type save(CodeGenFunction &CGF, type value)