1 //===-- CGCleanup.h - Classes for cleanups IR generation --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // These classes support the generation of LLVM IR for cleanups.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LIB_CODEGEN_CGCLEANUP_H
15 #define LLVM_CLANG_LIB_CODEGEN_CGCLEANUP_H
17 #include "EHScopeStack.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/SmallVector.h"
31 /// A protected scope for zero-cost EH handling.
33 llvm::BasicBlock *CachedLandingPad;
34 llvm::BasicBlock *CachedEHDispatchBlock;
36 EHScopeStack::stable_iterator EnclosingEHScope;
38 class CommonBitFields {
42 enum { NumCommonBits = 2 };
45 class CatchBitFields {
46 friend class EHCatchScope;
47 unsigned : NumCommonBits;
49 unsigned NumHandlers : 32 - NumCommonBits;
52 class CleanupBitFields {
53 friend class EHCleanupScope;
54 unsigned : NumCommonBits;
56 /// Whether this cleanup needs to be run along normal edges.
57 unsigned IsNormalCleanup : 1;
59 /// Whether this cleanup needs to be run along exception edges.
60 unsigned IsEHCleanup : 1;
62 /// Whether this cleanup is currently active.
63 unsigned IsActive : 1;
65 /// Whether the normal cleanup should test the activation flag.
66 unsigned TestFlagInNormalCleanup : 1;
68 /// Whether the EH cleanup should test the activation flag.
69 unsigned TestFlagInEHCleanup : 1;
71 /// The amount of extra storage needed by the Cleanup.
72 /// Always a multiple of the scope-stack alignment.
73 unsigned CleanupSize : 12;
75 /// The number of fixups required by enclosing scopes (not including
76 /// this one). If this is the top cleanup scope, all the fixups
77 /// from this index onwards belong to this scope.
78 unsigned FixupDepth : 32 - 17 - NumCommonBits; // currently 13
81 class FilterBitFields {
82 friend class EHFilterScope;
83 unsigned : NumCommonBits;
85 unsigned NumFilters : 32 - NumCommonBits;
89 CommonBitFields CommonBits;
90 CatchBitFields CatchBits;
91 CleanupBitFields CleanupBits;
92 FilterBitFields FilterBits;
96 enum Kind { Cleanup, Catch, Terminate, Filter };
98 EHScope(Kind kind, EHScopeStack::stable_iterator enclosingEHScope)
99 : CachedLandingPad(nullptr), CachedEHDispatchBlock(nullptr),
100 EnclosingEHScope(enclosingEHScope) {
101 CommonBits.Kind = kind;
104 Kind getKind() const { return static_cast<Kind>(CommonBits.Kind); }
106 llvm::BasicBlock *getCachedLandingPad() const {
107 return CachedLandingPad;
110 void setCachedLandingPad(llvm::BasicBlock *block) {
111 CachedLandingPad = block;
114 llvm::BasicBlock *getCachedEHDispatchBlock() const {
115 return CachedEHDispatchBlock;
118 void setCachedEHDispatchBlock(llvm::BasicBlock *block) {
119 CachedEHDispatchBlock = block;
122 bool hasEHBranches() const {
123 if (llvm::BasicBlock *block = getCachedEHDispatchBlock())
124 return !block->use_empty();
128 EHScopeStack::stable_iterator getEnclosingEHScope() const {
129 return EnclosingEHScope;
133 /// A scope which attempts to handle some, possibly all, types of
136 /// Objective C \@finally blocks are represented using a cleanup scope
137 /// after the catch scope.
138 class EHCatchScope : public EHScope {
139 // In effect, we have a flexible array member
140 // Handler Handlers[0];
141 // But that's only standard in C99, not C++, so we have to do
142 // annoying pointer arithmetic instead.
146 /// A type info value, or null (C++ null, not an LLVM null pointer)
148 llvm::Constant *Type;
150 /// The catch handler for this type.
151 llvm::BasicBlock *Block;
153 bool isCatchAll() const { return Type == nullptr; }
157 friend class EHScopeStack;
159 Handler *getHandlers() {
160 return reinterpret_cast<Handler*>(this+1);
163 const Handler *getHandlers() const {
164 return reinterpret_cast<const Handler*>(this+1);
168 static size_t getSizeForNumHandlers(unsigned N) {
169 return sizeof(EHCatchScope) + N * sizeof(Handler);
172 EHCatchScope(unsigned numHandlers,
173 EHScopeStack::stable_iterator enclosingEHScope)
174 : EHScope(Catch, enclosingEHScope) {
175 CatchBits.NumHandlers = numHandlers;
178 unsigned getNumHandlers() const {
179 return CatchBits.NumHandlers;
182 void setCatchAllHandler(unsigned I, llvm::BasicBlock *Block) {
183 setHandler(I, /*catchall*/ nullptr, Block);
186 void setHandler(unsigned I, llvm::Constant *Type, llvm::BasicBlock *Block) {
187 assert(I < getNumHandlers());
188 getHandlers()[I].Type = Type;
189 getHandlers()[I].Block = Block;
192 const Handler &getHandler(unsigned I) const {
193 assert(I < getNumHandlers());
194 return getHandlers()[I];
197 // Clear all handler blocks.
198 // FIXME: it's better to always call clearHandlerBlocks in DTOR and have a
199 // 'takeHandler' or some such function which removes ownership from the
200 // EHCatchScope object if the handlers should live longer than EHCatchScope.
201 void clearHandlerBlocks() {
202 for (unsigned I = 0, N = getNumHandlers(); I != N; ++I)
203 delete getHandler(I).Block;
206 typedef const Handler *iterator;
207 iterator begin() const { return getHandlers(); }
208 iterator end() const { return getHandlers() + getNumHandlers(); }
210 static bool classof(const EHScope *Scope) {
211 return Scope->getKind() == Catch;
215 /// A cleanup scope which generates the cleanup blocks lazily.
216 class EHCleanupScope : public EHScope {
217 /// The nearest normal cleanup scope enclosing this one.
218 EHScopeStack::stable_iterator EnclosingNormal;
220 /// The nearest EH scope enclosing this one.
221 EHScopeStack::stable_iterator EnclosingEH;
223 /// The dual entry/exit block along the normal edge. This is lazily
224 /// created if needed before the cleanup is popped.
225 llvm::BasicBlock *NormalBlock;
227 /// An optional i1 variable indicating whether this cleanup has been
229 llvm::AllocaInst *ActiveFlag;
231 /// Extra information required for cleanups that have resolved
232 /// branches through them. This has to be allocated on the side
233 /// because everything on the cleanup stack has be trivially
236 /// The destinations of normal branch-afters and branch-throughs.
237 llvm::SmallPtrSet<llvm::BasicBlock*, 4> Branches;
239 /// Normal branch-afters.
240 SmallVector<std::pair<llvm::BasicBlock*,llvm::ConstantInt*>, 4>
243 mutable struct ExtInfo *ExtInfo;
245 struct ExtInfo &getExtInfo() {
246 if (!ExtInfo) ExtInfo = new struct ExtInfo();
250 const struct ExtInfo &getExtInfo() const {
251 if (!ExtInfo) ExtInfo = new struct ExtInfo();
256 /// Gets the size required for a lazy cleanup scope with the given
257 /// cleanup-data requirements.
258 static size_t getSizeForCleanupSize(size_t Size) {
259 return sizeof(EHCleanupScope) + Size;
262 size_t getAllocatedSize() const {
263 return sizeof(EHCleanupScope) + CleanupBits.CleanupSize;
266 EHCleanupScope(bool isNormal, bool isEH, bool isActive,
267 unsigned cleanupSize, unsigned fixupDepth,
268 EHScopeStack::stable_iterator enclosingNormal,
269 EHScopeStack::stable_iterator enclosingEH)
270 : EHScope(EHScope::Cleanup, enclosingEH), EnclosingNormal(enclosingNormal),
271 NormalBlock(nullptr), ActiveFlag(nullptr), ExtInfo(nullptr) {
272 CleanupBits.IsNormalCleanup = isNormal;
273 CleanupBits.IsEHCleanup = isEH;
274 CleanupBits.IsActive = isActive;
275 CleanupBits.TestFlagInNormalCleanup = false;
276 CleanupBits.TestFlagInEHCleanup = false;
277 CleanupBits.CleanupSize = cleanupSize;
278 CleanupBits.FixupDepth = fixupDepth;
280 assert(CleanupBits.CleanupSize == cleanupSize && "cleanup size overflow");
286 // Objects of EHCleanupScope are not destructed. Use Destroy().
287 ~EHCleanupScope() LLVM_DELETED_FUNCTION;
289 bool isNormalCleanup() const { return CleanupBits.IsNormalCleanup; }
290 llvm::BasicBlock *getNormalBlock() const { return NormalBlock; }
291 void setNormalBlock(llvm::BasicBlock *BB) { NormalBlock = BB; }
293 bool isEHCleanup() const { return CleanupBits.IsEHCleanup; }
294 llvm::BasicBlock *getEHBlock() const { return getCachedEHDispatchBlock(); }
295 void setEHBlock(llvm::BasicBlock *BB) { setCachedEHDispatchBlock(BB); }
297 bool isActive() const { return CleanupBits.IsActive; }
298 void setActive(bool A) { CleanupBits.IsActive = A; }
300 llvm::AllocaInst *getActiveFlag() const { return ActiveFlag; }
301 void setActiveFlag(llvm::AllocaInst *Var) { ActiveFlag = Var; }
303 void setTestFlagInNormalCleanup() {
304 CleanupBits.TestFlagInNormalCleanup = true;
306 bool shouldTestFlagInNormalCleanup() const {
307 return CleanupBits.TestFlagInNormalCleanup;
310 void setTestFlagInEHCleanup() {
311 CleanupBits.TestFlagInEHCleanup = true;
313 bool shouldTestFlagInEHCleanup() const {
314 return CleanupBits.TestFlagInEHCleanup;
317 unsigned getFixupDepth() const { return CleanupBits.FixupDepth; }
318 EHScopeStack::stable_iterator getEnclosingNormalCleanup() const {
319 return EnclosingNormal;
322 size_t getCleanupSize() const { return CleanupBits.CleanupSize; }
323 void *getCleanupBuffer() { return this + 1; }
325 EHScopeStack::Cleanup *getCleanup() {
326 return reinterpret_cast<EHScopeStack::Cleanup*>(getCleanupBuffer());
329 /// True if this cleanup scope has any branch-afters or branch-throughs.
330 bool hasBranches() const { return ExtInfo && !ExtInfo->Branches.empty(); }
332 /// Add a branch-after to this cleanup scope. A branch-after is a
333 /// branch from a point protected by this (normal) cleanup to a
334 /// point in the normal cleanup scope immediately containing it.
336 /// for (;;) { A a; break; }
337 /// contains a branch-after.
339 /// Branch-afters each have their own destination out of the
340 /// cleanup, guaranteed distinct from anything else threaded through
341 /// it. Therefore branch-afters usually force a switch after the
343 void addBranchAfter(llvm::ConstantInt *Index,
344 llvm::BasicBlock *Block) {
345 struct ExtInfo &ExtInfo = getExtInfo();
346 if (ExtInfo.Branches.insert(Block).second)
347 ExtInfo.BranchAfters.push_back(std::make_pair(Block, Index));
350 /// Return the number of unique branch-afters on this scope.
351 unsigned getNumBranchAfters() const {
352 return ExtInfo ? ExtInfo->BranchAfters.size() : 0;
355 llvm::BasicBlock *getBranchAfterBlock(unsigned I) const {
356 assert(I < getNumBranchAfters());
357 return ExtInfo->BranchAfters[I].first;
360 llvm::ConstantInt *getBranchAfterIndex(unsigned I) const {
361 assert(I < getNumBranchAfters());
362 return ExtInfo->BranchAfters[I].second;
365 /// Add a branch-through to this cleanup scope. A branch-through is
366 /// a branch from a scope protected by this (normal) cleanup to an
367 /// enclosing scope other than the immediately-enclosing normal
370 /// In the following example, the branch through B's scope is a
371 /// branch-through, while the branch through A's scope is a
373 /// for (;;) { A a; B b; break; }
375 /// All branch-throughs have a common destination out of the
376 /// cleanup, one possibly shared with the fall-through. Therefore
377 /// branch-throughs usually don't force a switch after the cleanup.
379 /// \return true if the branch-through was new to this scope
380 bool addBranchThrough(llvm::BasicBlock *Block) {
381 return getExtInfo().Branches.insert(Block).second;
384 /// Determines if this cleanup scope has any branch throughs.
385 bool hasBranchThroughs() const {
386 if (!ExtInfo) return false;
387 return (ExtInfo->BranchAfters.size() != ExtInfo->Branches.size());
390 static bool classof(const EHScope *Scope) {
391 return (Scope->getKind() == Cleanup);
395 /// An exceptions scope which filters exceptions thrown through it.
396 /// Only exceptions matching the filter types will be permitted to be
399 /// This is used to implement C++ exception specifications.
400 class EHFilterScope : public EHScope {
401 // Essentially ends in a flexible array member:
402 // llvm::Value *FilterTypes[0];
404 llvm::Value **getFilters() {
405 return reinterpret_cast<llvm::Value**>(this+1);
408 llvm::Value * const *getFilters() const {
409 return reinterpret_cast<llvm::Value* const *>(this+1);
413 EHFilterScope(unsigned numFilters)
414 : EHScope(Filter, EHScopeStack::stable_end()) {
415 FilterBits.NumFilters = numFilters;
418 static size_t getSizeForNumFilters(unsigned numFilters) {
419 return sizeof(EHFilterScope) + numFilters * sizeof(llvm::Value*);
422 unsigned getNumFilters() const { return FilterBits.NumFilters; }
424 void setFilter(unsigned i, llvm::Value *filterValue) {
425 assert(i < getNumFilters());
426 getFilters()[i] = filterValue;
429 llvm::Value *getFilter(unsigned i) const {
430 assert(i < getNumFilters());
431 return getFilters()[i];
434 static bool classof(const EHScope *scope) {
435 return scope->getKind() == Filter;
439 /// An exceptions scope which calls std::terminate if any exception
441 class EHTerminateScope : public EHScope {
443 EHTerminateScope(EHScopeStack::stable_iterator enclosingEHScope)
444 : EHScope(Terminate, enclosingEHScope) {}
445 static size_t getSize() { return sizeof(EHTerminateScope); }
447 static bool classof(const EHScope *scope) {
448 return scope->getKind() == Terminate;
452 /// A non-stable pointer into the scope stack.
453 class EHScopeStack::iterator {
456 friend class EHScopeStack;
457 explicit iterator(char *Ptr) : Ptr(Ptr) {}
460 iterator() : Ptr(nullptr) {}
462 EHScope *get() const {
463 return reinterpret_cast<EHScope*>(Ptr);
466 EHScope *operator->() const { return get(); }
467 EHScope &operator*() const { return *get(); }
469 iterator &operator++() {
470 switch (get()->getKind()) {
472 Ptr += EHCatchScope::getSizeForNumHandlers(
473 static_cast<const EHCatchScope*>(get())->getNumHandlers());
476 case EHScope::Filter:
477 Ptr += EHFilterScope::getSizeForNumFilters(
478 static_cast<const EHFilterScope*>(get())->getNumFilters());
481 case EHScope::Cleanup:
482 Ptr += static_cast<const EHCleanupScope*>(get())
483 ->getAllocatedSize();
486 case EHScope::Terminate:
487 Ptr += EHTerminateScope::getSize();
495 iterator copy = *this;
500 iterator operator++(int) {
501 iterator copy = *this;
506 bool encloses(iterator other) const { return Ptr >= other.Ptr; }
507 bool strictlyEncloses(iterator other) const { return Ptr > other.Ptr; }
509 bool operator==(iterator other) const { return Ptr == other.Ptr; }
510 bool operator!=(iterator other) const { return Ptr != other.Ptr; }
513 inline EHScopeStack::iterator EHScopeStack::begin() const {
514 return iterator(StartOfData);
517 inline EHScopeStack::iterator EHScopeStack::end() const {
518 return iterator(EndOfBuffer);
521 inline void EHScopeStack::popCatch() {
522 assert(!empty() && "popping exception stack when not empty");
524 EHCatchScope &scope = cast<EHCatchScope>(*begin());
525 InnermostEHScope = scope.getEnclosingEHScope();
526 StartOfData += EHCatchScope::getSizeForNumHandlers(scope.getNumHandlers());
529 inline void EHScopeStack::popTerminate() {
530 assert(!empty() && "popping exception stack when not empty");
532 EHTerminateScope &scope = cast<EHTerminateScope>(*begin());
533 InnermostEHScope = scope.getEnclosingEHScope();
534 StartOfData += EHTerminateScope::getSize();
537 inline EHScopeStack::iterator EHScopeStack::find(stable_iterator sp) const {
538 assert(sp.isValid() && "finding invalid savepoint");
539 assert(sp.Size <= stable_begin().Size && "finding savepoint after pop");
540 return iterator(EndOfBuffer - sp.Size);
543 inline EHScopeStack::stable_iterator
544 EHScopeStack::stabilize(iterator ir) const {
545 assert(StartOfData <= ir.Ptr && ir.Ptr <= EndOfBuffer);
546 return stable_iterator(EndOfBuffer - ir.Ptr);