1f4a2713aSLionel Sambuc //===-- CGCleanup.h - Classes for cleanups IR generation --------*- C++ -*-===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // These classes support the generation of LLVM IR for cleanups.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc
14*0a6a1f1dSLionel Sambuc #ifndef LLVM_CLANG_LIB_CODEGEN_CGCLEANUP_H
15*0a6a1f1dSLionel Sambuc #define LLVM_CLANG_LIB_CODEGEN_CGCLEANUP_H
16f4a2713aSLionel Sambuc
17f4a2713aSLionel Sambuc #include "EHScopeStack.h"
18f4a2713aSLionel Sambuc #include "llvm/ADT/SmallPtrSet.h"
19f4a2713aSLionel Sambuc #include "llvm/ADT/SmallVector.h"
20f4a2713aSLionel Sambuc
21f4a2713aSLionel Sambuc namespace llvm {
22f4a2713aSLionel Sambuc class BasicBlock;
23f4a2713aSLionel Sambuc class Value;
24f4a2713aSLionel Sambuc class ConstantInt;
25f4a2713aSLionel Sambuc class AllocaInst;
26f4a2713aSLionel Sambuc }
27f4a2713aSLionel Sambuc
28f4a2713aSLionel Sambuc namespace clang {
29f4a2713aSLionel Sambuc namespace CodeGen {
30f4a2713aSLionel Sambuc
31f4a2713aSLionel Sambuc /// A protected scope for zero-cost EH handling.
32f4a2713aSLionel Sambuc class EHScope {
33f4a2713aSLionel Sambuc llvm::BasicBlock *CachedLandingPad;
34f4a2713aSLionel Sambuc llvm::BasicBlock *CachedEHDispatchBlock;
35f4a2713aSLionel Sambuc
36f4a2713aSLionel Sambuc EHScopeStack::stable_iterator EnclosingEHScope;
37f4a2713aSLionel Sambuc
38f4a2713aSLionel Sambuc class CommonBitFields {
39f4a2713aSLionel Sambuc friend class EHScope;
40f4a2713aSLionel Sambuc unsigned Kind : 2;
41f4a2713aSLionel Sambuc };
42f4a2713aSLionel Sambuc enum { NumCommonBits = 2 };
43f4a2713aSLionel Sambuc
44f4a2713aSLionel Sambuc protected:
45f4a2713aSLionel Sambuc class CatchBitFields {
46f4a2713aSLionel Sambuc friend class EHCatchScope;
47f4a2713aSLionel Sambuc unsigned : NumCommonBits;
48f4a2713aSLionel Sambuc
49f4a2713aSLionel Sambuc unsigned NumHandlers : 32 - NumCommonBits;
50f4a2713aSLionel Sambuc };
51f4a2713aSLionel Sambuc
52f4a2713aSLionel Sambuc class CleanupBitFields {
53f4a2713aSLionel Sambuc friend class EHCleanupScope;
54f4a2713aSLionel Sambuc unsigned : NumCommonBits;
55f4a2713aSLionel Sambuc
56f4a2713aSLionel Sambuc /// Whether this cleanup needs to be run along normal edges.
57f4a2713aSLionel Sambuc unsigned IsNormalCleanup : 1;
58f4a2713aSLionel Sambuc
59f4a2713aSLionel Sambuc /// Whether this cleanup needs to be run along exception edges.
60f4a2713aSLionel Sambuc unsigned IsEHCleanup : 1;
61f4a2713aSLionel Sambuc
62f4a2713aSLionel Sambuc /// Whether this cleanup is currently active.
63f4a2713aSLionel Sambuc unsigned IsActive : 1;
64f4a2713aSLionel Sambuc
65f4a2713aSLionel Sambuc /// Whether the normal cleanup should test the activation flag.
66f4a2713aSLionel Sambuc unsigned TestFlagInNormalCleanup : 1;
67f4a2713aSLionel Sambuc
68f4a2713aSLionel Sambuc /// Whether the EH cleanup should test the activation flag.
69f4a2713aSLionel Sambuc unsigned TestFlagInEHCleanup : 1;
70f4a2713aSLionel Sambuc
71f4a2713aSLionel Sambuc /// The amount of extra storage needed by the Cleanup.
72f4a2713aSLionel Sambuc /// Always a multiple of the scope-stack alignment.
73f4a2713aSLionel Sambuc unsigned CleanupSize : 12;
74f4a2713aSLionel Sambuc
75f4a2713aSLionel Sambuc /// The number of fixups required by enclosing scopes (not including
76f4a2713aSLionel Sambuc /// this one). If this is the top cleanup scope, all the fixups
77f4a2713aSLionel Sambuc /// from this index onwards belong to this scope.
78f4a2713aSLionel Sambuc unsigned FixupDepth : 32 - 17 - NumCommonBits; // currently 13
79f4a2713aSLionel Sambuc };
80f4a2713aSLionel Sambuc
81f4a2713aSLionel Sambuc class FilterBitFields {
82f4a2713aSLionel Sambuc friend class EHFilterScope;
83f4a2713aSLionel Sambuc unsigned : NumCommonBits;
84f4a2713aSLionel Sambuc
85f4a2713aSLionel Sambuc unsigned NumFilters : 32 - NumCommonBits;
86f4a2713aSLionel Sambuc };
87f4a2713aSLionel Sambuc
88f4a2713aSLionel Sambuc union {
89f4a2713aSLionel Sambuc CommonBitFields CommonBits;
90f4a2713aSLionel Sambuc CatchBitFields CatchBits;
91f4a2713aSLionel Sambuc CleanupBitFields CleanupBits;
92f4a2713aSLionel Sambuc FilterBitFields FilterBits;
93f4a2713aSLionel Sambuc };
94f4a2713aSLionel Sambuc
95f4a2713aSLionel Sambuc public:
96f4a2713aSLionel Sambuc enum Kind { Cleanup, Catch, Terminate, Filter };
97f4a2713aSLionel Sambuc
EHScope(Kind kind,EHScopeStack::stable_iterator enclosingEHScope)98f4a2713aSLionel Sambuc EHScope(Kind kind, EHScopeStack::stable_iterator enclosingEHScope)
99*0a6a1f1dSLionel Sambuc : CachedLandingPad(nullptr), CachedEHDispatchBlock(nullptr),
100f4a2713aSLionel Sambuc EnclosingEHScope(enclosingEHScope) {
101f4a2713aSLionel Sambuc CommonBits.Kind = kind;
102f4a2713aSLionel Sambuc }
103f4a2713aSLionel Sambuc
getKind()104f4a2713aSLionel Sambuc Kind getKind() const { return static_cast<Kind>(CommonBits.Kind); }
105f4a2713aSLionel Sambuc
getCachedLandingPad()106f4a2713aSLionel Sambuc llvm::BasicBlock *getCachedLandingPad() const {
107f4a2713aSLionel Sambuc return CachedLandingPad;
108f4a2713aSLionel Sambuc }
109f4a2713aSLionel Sambuc
setCachedLandingPad(llvm::BasicBlock * block)110f4a2713aSLionel Sambuc void setCachedLandingPad(llvm::BasicBlock *block) {
111f4a2713aSLionel Sambuc CachedLandingPad = block;
112f4a2713aSLionel Sambuc }
113f4a2713aSLionel Sambuc
getCachedEHDispatchBlock()114f4a2713aSLionel Sambuc llvm::BasicBlock *getCachedEHDispatchBlock() const {
115f4a2713aSLionel Sambuc return CachedEHDispatchBlock;
116f4a2713aSLionel Sambuc }
117f4a2713aSLionel Sambuc
setCachedEHDispatchBlock(llvm::BasicBlock * block)118f4a2713aSLionel Sambuc void setCachedEHDispatchBlock(llvm::BasicBlock *block) {
119f4a2713aSLionel Sambuc CachedEHDispatchBlock = block;
120f4a2713aSLionel Sambuc }
121f4a2713aSLionel Sambuc
hasEHBranches()122f4a2713aSLionel Sambuc bool hasEHBranches() const {
123f4a2713aSLionel Sambuc if (llvm::BasicBlock *block = getCachedEHDispatchBlock())
124f4a2713aSLionel Sambuc return !block->use_empty();
125f4a2713aSLionel Sambuc return false;
126f4a2713aSLionel Sambuc }
127f4a2713aSLionel Sambuc
getEnclosingEHScope()128f4a2713aSLionel Sambuc EHScopeStack::stable_iterator getEnclosingEHScope() const {
129f4a2713aSLionel Sambuc return EnclosingEHScope;
130f4a2713aSLionel Sambuc }
131f4a2713aSLionel Sambuc };
132f4a2713aSLionel Sambuc
133f4a2713aSLionel Sambuc /// A scope which attempts to handle some, possibly all, types of
134f4a2713aSLionel Sambuc /// exceptions.
135f4a2713aSLionel Sambuc ///
136f4a2713aSLionel Sambuc /// Objective C \@finally blocks are represented using a cleanup scope
137f4a2713aSLionel Sambuc /// after the catch scope.
138f4a2713aSLionel Sambuc class EHCatchScope : public EHScope {
139f4a2713aSLionel Sambuc // In effect, we have a flexible array member
140f4a2713aSLionel Sambuc // Handler Handlers[0];
141f4a2713aSLionel Sambuc // But that's only standard in C99, not C++, so we have to do
142f4a2713aSLionel Sambuc // annoying pointer arithmetic instead.
143f4a2713aSLionel Sambuc
144f4a2713aSLionel Sambuc public:
145f4a2713aSLionel Sambuc struct Handler {
146f4a2713aSLionel Sambuc /// A type info value, or null (C++ null, not an LLVM null pointer)
147f4a2713aSLionel Sambuc /// for a catch-all.
148*0a6a1f1dSLionel Sambuc llvm::Constant *Type;
149f4a2713aSLionel Sambuc
150f4a2713aSLionel Sambuc /// The catch handler for this type.
151f4a2713aSLionel Sambuc llvm::BasicBlock *Block;
152f4a2713aSLionel Sambuc
isCatchAllHandler153*0a6a1f1dSLionel Sambuc bool isCatchAll() const { return Type == nullptr; }
154f4a2713aSLionel Sambuc };
155f4a2713aSLionel Sambuc
156f4a2713aSLionel Sambuc private:
157f4a2713aSLionel Sambuc friend class EHScopeStack;
158f4a2713aSLionel Sambuc
getHandlers()159f4a2713aSLionel Sambuc Handler *getHandlers() {
160f4a2713aSLionel Sambuc return reinterpret_cast<Handler*>(this+1);
161f4a2713aSLionel Sambuc }
162f4a2713aSLionel Sambuc
getHandlers()163f4a2713aSLionel Sambuc const Handler *getHandlers() const {
164f4a2713aSLionel Sambuc return reinterpret_cast<const Handler*>(this+1);
165f4a2713aSLionel Sambuc }
166f4a2713aSLionel Sambuc
167f4a2713aSLionel Sambuc public:
getSizeForNumHandlers(unsigned N)168f4a2713aSLionel Sambuc static size_t getSizeForNumHandlers(unsigned N) {
169f4a2713aSLionel Sambuc return sizeof(EHCatchScope) + N * sizeof(Handler);
170f4a2713aSLionel Sambuc }
171f4a2713aSLionel Sambuc
EHCatchScope(unsigned numHandlers,EHScopeStack::stable_iterator enclosingEHScope)172f4a2713aSLionel Sambuc EHCatchScope(unsigned numHandlers,
173f4a2713aSLionel Sambuc EHScopeStack::stable_iterator enclosingEHScope)
174f4a2713aSLionel Sambuc : EHScope(Catch, enclosingEHScope) {
175f4a2713aSLionel Sambuc CatchBits.NumHandlers = numHandlers;
176f4a2713aSLionel Sambuc }
177f4a2713aSLionel Sambuc
getNumHandlers()178f4a2713aSLionel Sambuc unsigned getNumHandlers() const {
179f4a2713aSLionel Sambuc return CatchBits.NumHandlers;
180f4a2713aSLionel Sambuc }
181f4a2713aSLionel Sambuc
setCatchAllHandler(unsigned I,llvm::BasicBlock * Block)182f4a2713aSLionel Sambuc void setCatchAllHandler(unsigned I, llvm::BasicBlock *Block) {
183*0a6a1f1dSLionel Sambuc setHandler(I, /*catchall*/ nullptr, Block);
184f4a2713aSLionel Sambuc }
185f4a2713aSLionel Sambuc
setHandler(unsigned I,llvm::Constant * Type,llvm::BasicBlock * Block)186*0a6a1f1dSLionel Sambuc void setHandler(unsigned I, llvm::Constant *Type, llvm::BasicBlock *Block) {
187f4a2713aSLionel Sambuc assert(I < getNumHandlers());
188f4a2713aSLionel Sambuc getHandlers()[I].Type = Type;
189f4a2713aSLionel Sambuc getHandlers()[I].Block = Block;
190f4a2713aSLionel Sambuc }
191f4a2713aSLionel Sambuc
getHandler(unsigned I)192f4a2713aSLionel Sambuc const Handler &getHandler(unsigned I) const {
193f4a2713aSLionel Sambuc assert(I < getNumHandlers());
194f4a2713aSLionel Sambuc return getHandlers()[I];
195f4a2713aSLionel Sambuc }
196f4a2713aSLionel Sambuc
197*0a6a1f1dSLionel Sambuc // Clear all handler blocks.
198*0a6a1f1dSLionel Sambuc // FIXME: it's better to always call clearHandlerBlocks in DTOR and have a
199*0a6a1f1dSLionel Sambuc // 'takeHandler' or some such function which removes ownership from the
200*0a6a1f1dSLionel Sambuc // EHCatchScope object if the handlers should live longer than EHCatchScope.
clearHandlerBlocks()201*0a6a1f1dSLionel Sambuc void clearHandlerBlocks() {
202*0a6a1f1dSLionel Sambuc for (unsigned I = 0, N = getNumHandlers(); I != N; ++I)
203*0a6a1f1dSLionel Sambuc delete getHandler(I).Block;
204*0a6a1f1dSLionel Sambuc }
205*0a6a1f1dSLionel Sambuc
206f4a2713aSLionel Sambuc typedef const Handler *iterator;
begin()207f4a2713aSLionel Sambuc iterator begin() const { return getHandlers(); }
end()208f4a2713aSLionel Sambuc iterator end() const { return getHandlers() + getNumHandlers(); }
209f4a2713aSLionel Sambuc
classof(const EHScope * Scope)210f4a2713aSLionel Sambuc static bool classof(const EHScope *Scope) {
211f4a2713aSLionel Sambuc return Scope->getKind() == Catch;
212f4a2713aSLionel Sambuc }
213f4a2713aSLionel Sambuc };
214f4a2713aSLionel Sambuc
215f4a2713aSLionel Sambuc /// A cleanup scope which generates the cleanup blocks lazily.
216f4a2713aSLionel Sambuc class EHCleanupScope : public EHScope {
217f4a2713aSLionel Sambuc /// The nearest normal cleanup scope enclosing this one.
218f4a2713aSLionel Sambuc EHScopeStack::stable_iterator EnclosingNormal;
219f4a2713aSLionel Sambuc
220f4a2713aSLionel Sambuc /// The nearest EH scope enclosing this one.
221f4a2713aSLionel Sambuc EHScopeStack::stable_iterator EnclosingEH;
222f4a2713aSLionel Sambuc
223f4a2713aSLionel Sambuc /// The dual entry/exit block along the normal edge. This is lazily
224f4a2713aSLionel Sambuc /// created if needed before the cleanup is popped.
225f4a2713aSLionel Sambuc llvm::BasicBlock *NormalBlock;
226f4a2713aSLionel Sambuc
227f4a2713aSLionel Sambuc /// An optional i1 variable indicating whether this cleanup has been
228f4a2713aSLionel Sambuc /// activated yet.
229f4a2713aSLionel Sambuc llvm::AllocaInst *ActiveFlag;
230f4a2713aSLionel Sambuc
231f4a2713aSLionel Sambuc /// Extra information required for cleanups that have resolved
232f4a2713aSLionel Sambuc /// branches through them. This has to be allocated on the side
233f4a2713aSLionel Sambuc /// because everything on the cleanup stack has be trivially
234f4a2713aSLionel Sambuc /// movable.
235f4a2713aSLionel Sambuc struct ExtInfo {
236f4a2713aSLionel Sambuc /// The destinations of normal branch-afters and branch-throughs.
237f4a2713aSLionel Sambuc llvm::SmallPtrSet<llvm::BasicBlock*, 4> Branches;
238f4a2713aSLionel Sambuc
239f4a2713aSLionel Sambuc /// Normal branch-afters.
240f4a2713aSLionel Sambuc SmallVector<std::pair<llvm::BasicBlock*,llvm::ConstantInt*>, 4>
241f4a2713aSLionel Sambuc BranchAfters;
242f4a2713aSLionel Sambuc };
243f4a2713aSLionel Sambuc mutable struct ExtInfo *ExtInfo;
244f4a2713aSLionel Sambuc
getExtInfo()245f4a2713aSLionel Sambuc struct ExtInfo &getExtInfo() {
246f4a2713aSLionel Sambuc if (!ExtInfo) ExtInfo = new struct ExtInfo();
247f4a2713aSLionel Sambuc return *ExtInfo;
248f4a2713aSLionel Sambuc }
249f4a2713aSLionel Sambuc
getExtInfo()250f4a2713aSLionel Sambuc const struct ExtInfo &getExtInfo() const {
251f4a2713aSLionel Sambuc if (!ExtInfo) ExtInfo = new struct ExtInfo();
252f4a2713aSLionel Sambuc return *ExtInfo;
253f4a2713aSLionel Sambuc }
254f4a2713aSLionel Sambuc
255f4a2713aSLionel Sambuc public:
256f4a2713aSLionel Sambuc /// Gets the size required for a lazy cleanup scope with the given
257f4a2713aSLionel Sambuc /// cleanup-data requirements.
getSizeForCleanupSize(size_t Size)258f4a2713aSLionel Sambuc static size_t getSizeForCleanupSize(size_t Size) {
259f4a2713aSLionel Sambuc return sizeof(EHCleanupScope) + Size;
260f4a2713aSLionel Sambuc }
261f4a2713aSLionel Sambuc
getAllocatedSize()262f4a2713aSLionel Sambuc size_t getAllocatedSize() const {
263f4a2713aSLionel Sambuc return sizeof(EHCleanupScope) + CleanupBits.CleanupSize;
264f4a2713aSLionel Sambuc }
265f4a2713aSLionel Sambuc
EHCleanupScope(bool isNormal,bool isEH,bool isActive,unsigned cleanupSize,unsigned fixupDepth,EHScopeStack::stable_iterator enclosingNormal,EHScopeStack::stable_iterator enclosingEH)266f4a2713aSLionel Sambuc EHCleanupScope(bool isNormal, bool isEH, bool isActive,
267f4a2713aSLionel Sambuc unsigned cleanupSize, unsigned fixupDepth,
268f4a2713aSLionel Sambuc EHScopeStack::stable_iterator enclosingNormal,
269f4a2713aSLionel Sambuc EHScopeStack::stable_iterator enclosingEH)
270f4a2713aSLionel Sambuc : EHScope(EHScope::Cleanup, enclosingEH), EnclosingNormal(enclosingNormal),
271*0a6a1f1dSLionel Sambuc NormalBlock(nullptr), ActiveFlag(nullptr), ExtInfo(nullptr) {
272f4a2713aSLionel Sambuc CleanupBits.IsNormalCleanup = isNormal;
273f4a2713aSLionel Sambuc CleanupBits.IsEHCleanup = isEH;
274f4a2713aSLionel Sambuc CleanupBits.IsActive = isActive;
275f4a2713aSLionel Sambuc CleanupBits.TestFlagInNormalCleanup = false;
276f4a2713aSLionel Sambuc CleanupBits.TestFlagInEHCleanup = false;
277f4a2713aSLionel Sambuc CleanupBits.CleanupSize = cleanupSize;
278f4a2713aSLionel Sambuc CleanupBits.FixupDepth = fixupDepth;
279f4a2713aSLionel Sambuc
280f4a2713aSLionel Sambuc assert(CleanupBits.CleanupSize == cleanupSize && "cleanup size overflow");
281f4a2713aSLionel Sambuc }
282f4a2713aSLionel Sambuc
Destroy()283*0a6a1f1dSLionel Sambuc void Destroy() {
284f4a2713aSLionel Sambuc delete ExtInfo;
285f4a2713aSLionel Sambuc }
286*0a6a1f1dSLionel Sambuc // Objects of EHCleanupScope are not destructed. Use Destroy().
287*0a6a1f1dSLionel Sambuc ~EHCleanupScope() LLVM_DELETED_FUNCTION;
288f4a2713aSLionel Sambuc
isNormalCleanup()289f4a2713aSLionel Sambuc bool isNormalCleanup() const { return CleanupBits.IsNormalCleanup; }
getNormalBlock()290f4a2713aSLionel Sambuc llvm::BasicBlock *getNormalBlock() const { return NormalBlock; }
setNormalBlock(llvm::BasicBlock * BB)291f4a2713aSLionel Sambuc void setNormalBlock(llvm::BasicBlock *BB) { NormalBlock = BB; }
292f4a2713aSLionel Sambuc
isEHCleanup()293f4a2713aSLionel Sambuc bool isEHCleanup() const { return CleanupBits.IsEHCleanup; }
getEHBlock()294f4a2713aSLionel Sambuc llvm::BasicBlock *getEHBlock() const { return getCachedEHDispatchBlock(); }
setEHBlock(llvm::BasicBlock * BB)295f4a2713aSLionel Sambuc void setEHBlock(llvm::BasicBlock *BB) { setCachedEHDispatchBlock(BB); }
296f4a2713aSLionel Sambuc
isActive()297f4a2713aSLionel Sambuc bool isActive() const { return CleanupBits.IsActive; }
setActive(bool A)298f4a2713aSLionel Sambuc void setActive(bool A) { CleanupBits.IsActive = A; }
299f4a2713aSLionel Sambuc
getActiveFlag()300f4a2713aSLionel Sambuc llvm::AllocaInst *getActiveFlag() const { return ActiveFlag; }
setActiveFlag(llvm::AllocaInst * Var)301f4a2713aSLionel Sambuc void setActiveFlag(llvm::AllocaInst *Var) { ActiveFlag = Var; }
302f4a2713aSLionel Sambuc
setTestFlagInNormalCleanup()303f4a2713aSLionel Sambuc void setTestFlagInNormalCleanup() {
304f4a2713aSLionel Sambuc CleanupBits.TestFlagInNormalCleanup = true;
305f4a2713aSLionel Sambuc }
shouldTestFlagInNormalCleanup()306f4a2713aSLionel Sambuc bool shouldTestFlagInNormalCleanup() const {
307f4a2713aSLionel Sambuc return CleanupBits.TestFlagInNormalCleanup;
308f4a2713aSLionel Sambuc }
309f4a2713aSLionel Sambuc
setTestFlagInEHCleanup()310f4a2713aSLionel Sambuc void setTestFlagInEHCleanup() {
311f4a2713aSLionel Sambuc CleanupBits.TestFlagInEHCleanup = true;
312f4a2713aSLionel Sambuc }
shouldTestFlagInEHCleanup()313f4a2713aSLionel Sambuc bool shouldTestFlagInEHCleanup() const {
314f4a2713aSLionel Sambuc return CleanupBits.TestFlagInEHCleanup;
315f4a2713aSLionel Sambuc }
316f4a2713aSLionel Sambuc
getFixupDepth()317f4a2713aSLionel Sambuc unsigned getFixupDepth() const { return CleanupBits.FixupDepth; }
getEnclosingNormalCleanup()318f4a2713aSLionel Sambuc EHScopeStack::stable_iterator getEnclosingNormalCleanup() const {
319f4a2713aSLionel Sambuc return EnclosingNormal;
320f4a2713aSLionel Sambuc }
321f4a2713aSLionel Sambuc
getCleanupSize()322f4a2713aSLionel Sambuc size_t getCleanupSize() const { return CleanupBits.CleanupSize; }
getCleanupBuffer()323f4a2713aSLionel Sambuc void *getCleanupBuffer() { return this + 1; }
324f4a2713aSLionel Sambuc
getCleanup()325f4a2713aSLionel Sambuc EHScopeStack::Cleanup *getCleanup() {
326f4a2713aSLionel Sambuc return reinterpret_cast<EHScopeStack::Cleanup*>(getCleanupBuffer());
327f4a2713aSLionel Sambuc }
328f4a2713aSLionel Sambuc
329f4a2713aSLionel Sambuc /// True if this cleanup scope has any branch-afters or branch-throughs.
hasBranches()330f4a2713aSLionel Sambuc bool hasBranches() const { return ExtInfo && !ExtInfo->Branches.empty(); }
331f4a2713aSLionel Sambuc
332f4a2713aSLionel Sambuc /// Add a branch-after to this cleanup scope. A branch-after is a
333f4a2713aSLionel Sambuc /// branch from a point protected by this (normal) cleanup to a
334f4a2713aSLionel Sambuc /// point in the normal cleanup scope immediately containing it.
335f4a2713aSLionel Sambuc /// For example,
336f4a2713aSLionel Sambuc /// for (;;) { A a; break; }
337f4a2713aSLionel Sambuc /// contains a branch-after.
338f4a2713aSLionel Sambuc ///
339f4a2713aSLionel Sambuc /// Branch-afters each have their own destination out of the
340f4a2713aSLionel Sambuc /// cleanup, guaranteed distinct from anything else threaded through
341f4a2713aSLionel Sambuc /// it. Therefore branch-afters usually force a switch after the
342f4a2713aSLionel Sambuc /// cleanup.
addBranchAfter(llvm::ConstantInt * Index,llvm::BasicBlock * Block)343f4a2713aSLionel Sambuc void addBranchAfter(llvm::ConstantInt *Index,
344f4a2713aSLionel Sambuc llvm::BasicBlock *Block) {
345f4a2713aSLionel Sambuc struct ExtInfo &ExtInfo = getExtInfo();
346*0a6a1f1dSLionel Sambuc if (ExtInfo.Branches.insert(Block).second)
347f4a2713aSLionel Sambuc ExtInfo.BranchAfters.push_back(std::make_pair(Block, Index));
348f4a2713aSLionel Sambuc }
349f4a2713aSLionel Sambuc
350f4a2713aSLionel Sambuc /// Return the number of unique branch-afters on this scope.
getNumBranchAfters()351f4a2713aSLionel Sambuc unsigned getNumBranchAfters() const {
352f4a2713aSLionel Sambuc return ExtInfo ? ExtInfo->BranchAfters.size() : 0;
353f4a2713aSLionel Sambuc }
354f4a2713aSLionel Sambuc
getBranchAfterBlock(unsigned I)355f4a2713aSLionel Sambuc llvm::BasicBlock *getBranchAfterBlock(unsigned I) const {
356f4a2713aSLionel Sambuc assert(I < getNumBranchAfters());
357f4a2713aSLionel Sambuc return ExtInfo->BranchAfters[I].first;
358f4a2713aSLionel Sambuc }
359f4a2713aSLionel Sambuc
getBranchAfterIndex(unsigned I)360f4a2713aSLionel Sambuc llvm::ConstantInt *getBranchAfterIndex(unsigned I) const {
361f4a2713aSLionel Sambuc assert(I < getNumBranchAfters());
362f4a2713aSLionel Sambuc return ExtInfo->BranchAfters[I].second;
363f4a2713aSLionel Sambuc }
364f4a2713aSLionel Sambuc
365f4a2713aSLionel Sambuc /// Add a branch-through to this cleanup scope. A branch-through is
366f4a2713aSLionel Sambuc /// a branch from a scope protected by this (normal) cleanup to an
367f4a2713aSLionel Sambuc /// enclosing scope other than the immediately-enclosing normal
368f4a2713aSLionel Sambuc /// cleanup scope.
369f4a2713aSLionel Sambuc ///
370f4a2713aSLionel Sambuc /// In the following example, the branch through B's scope is a
371f4a2713aSLionel Sambuc /// branch-through, while the branch through A's scope is a
372f4a2713aSLionel Sambuc /// branch-after:
373f4a2713aSLionel Sambuc /// for (;;) { A a; B b; break; }
374f4a2713aSLionel Sambuc ///
375f4a2713aSLionel Sambuc /// All branch-throughs have a common destination out of the
376f4a2713aSLionel Sambuc /// cleanup, one possibly shared with the fall-through. Therefore
377f4a2713aSLionel Sambuc /// branch-throughs usually don't force a switch after the cleanup.
378f4a2713aSLionel Sambuc ///
379f4a2713aSLionel Sambuc /// \return true if the branch-through was new to this scope
addBranchThrough(llvm::BasicBlock * Block)380f4a2713aSLionel Sambuc bool addBranchThrough(llvm::BasicBlock *Block) {
381*0a6a1f1dSLionel Sambuc return getExtInfo().Branches.insert(Block).second;
382f4a2713aSLionel Sambuc }
383f4a2713aSLionel Sambuc
384f4a2713aSLionel Sambuc /// Determines if this cleanup scope has any branch throughs.
hasBranchThroughs()385f4a2713aSLionel Sambuc bool hasBranchThroughs() const {
386f4a2713aSLionel Sambuc if (!ExtInfo) return false;
387f4a2713aSLionel Sambuc return (ExtInfo->BranchAfters.size() != ExtInfo->Branches.size());
388f4a2713aSLionel Sambuc }
389f4a2713aSLionel Sambuc
classof(const EHScope * Scope)390f4a2713aSLionel Sambuc static bool classof(const EHScope *Scope) {
391f4a2713aSLionel Sambuc return (Scope->getKind() == Cleanup);
392f4a2713aSLionel Sambuc }
393f4a2713aSLionel Sambuc };
394f4a2713aSLionel Sambuc
395f4a2713aSLionel Sambuc /// An exceptions scope which filters exceptions thrown through it.
396f4a2713aSLionel Sambuc /// Only exceptions matching the filter types will be permitted to be
397f4a2713aSLionel Sambuc /// thrown.
398f4a2713aSLionel Sambuc ///
399f4a2713aSLionel Sambuc /// This is used to implement C++ exception specifications.
400f4a2713aSLionel Sambuc class EHFilterScope : public EHScope {
401f4a2713aSLionel Sambuc // Essentially ends in a flexible array member:
402f4a2713aSLionel Sambuc // llvm::Value *FilterTypes[0];
403f4a2713aSLionel Sambuc
getFilters()404f4a2713aSLionel Sambuc llvm::Value **getFilters() {
405f4a2713aSLionel Sambuc return reinterpret_cast<llvm::Value**>(this+1);
406f4a2713aSLionel Sambuc }
407f4a2713aSLionel Sambuc
getFilters()408f4a2713aSLionel Sambuc llvm::Value * const *getFilters() const {
409f4a2713aSLionel Sambuc return reinterpret_cast<llvm::Value* const *>(this+1);
410f4a2713aSLionel Sambuc }
411f4a2713aSLionel Sambuc
412f4a2713aSLionel Sambuc public:
EHFilterScope(unsigned numFilters)413f4a2713aSLionel Sambuc EHFilterScope(unsigned numFilters)
414f4a2713aSLionel Sambuc : EHScope(Filter, EHScopeStack::stable_end()) {
415f4a2713aSLionel Sambuc FilterBits.NumFilters = numFilters;
416f4a2713aSLionel Sambuc }
417f4a2713aSLionel Sambuc
getSizeForNumFilters(unsigned numFilters)418f4a2713aSLionel Sambuc static size_t getSizeForNumFilters(unsigned numFilters) {
419f4a2713aSLionel Sambuc return sizeof(EHFilterScope) + numFilters * sizeof(llvm::Value*);
420f4a2713aSLionel Sambuc }
421f4a2713aSLionel Sambuc
getNumFilters()422f4a2713aSLionel Sambuc unsigned getNumFilters() const { return FilterBits.NumFilters; }
423f4a2713aSLionel Sambuc
setFilter(unsigned i,llvm::Value * filterValue)424f4a2713aSLionel Sambuc void setFilter(unsigned i, llvm::Value *filterValue) {
425f4a2713aSLionel Sambuc assert(i < getNumFilters());
426f4a2713aSLionel Sambuc getFilters()[i] = filterValue;
427f4a2713aSLionel Sambuc }
428f4a2713aSLionel Sambuc
getFilter(unsigned i)429f4a2713aSLionel Sambuc llvm::Value *getFilter(unsigned i) const {
430f4a2713aSLionel Sambuc assert(i < getNumFilters());
431f4a2713aSLionel Sambuc return getFilters()[i];
432f4a2713aSLionel Sambuc }
433f4a2713aSLionel Sambuc
classof(const EHScope * scope)434f4a2713aSLionel Sambuc static bool classof(const EHScope *scope) {
435f4a2713aSLionel Sambuc return scope->getKind() == Filter;
436f4a2713aSLionel Sambuc }
437f4a2713aSLionel Sambuc };
438f4a2713aSLionel Sambuc
439f4a2713aSLionel Sambuc /// An exceptions scope which calls std::terminate if any exception
440f4a2713aSLionel Sambuc /// reaches it.
441f4a2713aSLionel Sambuc class EHTerminateScope : public EHScope {
442f4a2713aSLionel Sambuc public:
EHTerminateScope(EHScopeStack::stable_iterator enclosingEHScope)443f4a2713aSLionel Sambuc EHTerminateScope(EHScopeStack::stable_iterator enclosingEHScope)
444f4a2713aSLionel Sambuc : EHScope(Terminate, enclosingEHScope) {}
getSize()445f4a2713aSLionel Sambuc static size_t getSize() { return sizeof(EHTerminateScope); }
446f4a2713aSLionel Sambuc
classof(const EHScope * scope)447f4a2713aSLionel Sambuc static bool classof(const EHScope *scope) {
448f4a2713aSLionel Sambuc return scope->getKind() == Terminate;
449f4a2713aSLionel Sambuc }
450f4a2713aSLionel Sambuc };
451f4a2713aSLionel Sambuc
452f4a2713aSLionel Sambuc /// A non-stable pointer into the scope stack.
453f4a2713aSLionel Sambuc class EHScopeStack::iterator {
454f4a2713aSLionel Sambuc char *Ptr;
455f4a2713aSLionel Sambuc
456f4a2713aSLionel Sambuc friend class EHScopeStack;
iterator(char * Ptr)457f4a2713aSLionel Sambuc explicit iterator(char *Ptr) : Ptr(Ptr) {}
458f4a2713aSLionel Sambuc
459f4a2713aSLionel Sambuc public:
iterator()460*0a6a1f1dSLionel Sambuc iterator() : Ptr(nullptr) {}
461f4a2713aSLionel Sambuc
get()462f4a2713aSLionel Sambuc EHScope *get() const {
463f4a2713aSLionel Sambuc return reinterpret_cast<EHScope*>(Ptr);
464f4a2713aSLionel Sambuc }
465f4a2713aSLionel Sambuc
466f4a2713aSLionel Sambuc EHScope *operator->() const { return get(); }
467f4a2713aSLionel Sambuc EHScope &operator*() const { return *get(); }
468f4a2713aSLionel Sambuc
469f4a2713aSLionel Sambuc iterator &operator++() {
470f4a2713aSLionel Sambuc switch (get()->getKind()) {
471f4a2713aSLionel Sambuc case EHScope::Catch:
472f4a2713aSLionel Sambuc Ptr += EHCatchScope::getSizeForNumHandlers(
473f4a2713aSLionel Sambuc static_cast<const EHCatchScope*>(get())->getNumHandlers());
474f4a2713aSLionel Sambuc break;
475f4a2713aSLionel Sambuc
476f4a2713aSLionel Sambuc case EHScope::Filter:
477f4a2713aSLionel Sambuc Ptr += EHFilterScope::getSizeForNumFilters(
478f4a2713aSLionel Sambuc static_cast<const EHFilterScope*>(get())->getNumFilters());
479f4a2713aSLionel Sambuc break;
480f4a2713aSLionel Sambuc
481f4a2713aSLionel Sambuc case EHScope::Cleanup:
482f4a2713aSLionel Sambuc Ptr += static_cast<const EHCleanupScope*>(get())
483f4a2713aSLionel Sambuc ->getAllocatedSize();
484f4a2713aSLionel Sambuc break;
485f4a2713aSLionel Sambuc
486f4a2713aSLionel Sambuc case EHScope::Terminate:
487f4a2713aSLionel Sambuc Ptr += EHTerminateScope::getSize();
488f4a2713aSLionel Sambuc break;
489f4a2713aSLionel Sambuc }
490f4a2713aSLionel Sambuc
491f4a2713aSLionel Sambuc return *this;
492f4a2713aSLionel Sambuc }
493f4a2713aSLionel Sambuc
next()494f4a2713aSLionel Sambuc iterator next() {
495f4a2713aSLionel Sambuc iterator copy = *this;
496f4a2713aSLionel Sambuc ++copy;
497f4a2713aSLionel Sambuc return copy;
498f4a2713aSLionel Sambuc }
499f4a2713aSLionel Sambuc
500f4a2713aSLionel Sambuc iterator operator++(int) {
501f4a2713aSLionel Sambuc iterator copy = *this;
502f4a2713aSLionel Sambuc operator++();
503f4a2713aSLionel Sambuc return copy;
504f4a2713aSLionel Sambuc }
505f4a2713aSLionel Sambuc
encloses(iterator other)506f4a2713aSLionel Sambuc bool encloses(iterator other) const { return Ptr >= other.Ptr; }
strictlyEncloses(iterator other)507f4a2713aSLionel Sambuc bool strictlyEncloses(iterator other) const { return Ptr > other.Ptr; }
508f4a2713aSLionel Sambuc
509f4a2713aSLionel Sambuc bool operator==(iterator other) const { return Ptr == other.Ptr; }
510f4a2713aSLionel Sambuc bool operator!=(iterator other) const { return Ptr != other.Ptr; }
511f4a2713aSLionel Sambuc };
512f4a2713aSLionel Sambuc
begin()513f4a2713aSLionel Sambuc inline EHScopeStack::iterator EHScopeStack::begin() const {
514f4a2713aSLionel Sambuc return iterator(StartOfData);
515f4a2713aSLionel Sambuc }
516f4a2713aSLionel Sambuc
end()517f4a2713aSLionel Sambuc inline EHScopeStack::iterator EHScopeStack::end() const {
518f4a2713aSLionel Sambuc return iterator(EndOfBuffer);
519f4a2713aSLionel Sambuc }
520f4a2713aSLionel Sambuc
popCatch()521f4a2713aSLionel Sambuc inline void EHScopeStack::popCatch() {
522f4a2713aSLionel Sambuc assert(!empty() && "popping exception stack when not empty");
523f4a2713aSLionel Sambuc
524f4a2713aSLionel Sambuc EHCatchScope &scope = cast<EHCatchScope>(*begin());
525f4a2713aSLionel Sambuc InnermostEHScope = scope.getEnclosingEHScope();
526f4a2713aSLionel Sambuc StartOfData += EHCatchScope::getSizeForNumHandlers(scope.getNumHandlers());
527f4a2713aSLionel Sambuc }
528f4a2713aSLionel Sambuc
popTerminate()529f4a2713aSLionel Sambuc inline void EHScopeStack::popTerminate() {
530f4a2713aSLionel Sambuc assert(!empty() && "popping exception stack when not empty");
531f4a2713aSLionel Sambuc
532f4a2713aSLionel Sambuc EHTerminateScope &scope = cast<EHTerminateScope>(*begin());
533f4a2713aSLionel Sambuc InnermostEHScope = scope.getEnclosingEHScope();
534f4a2713aSLionel Sambuc StartOfData += EHTerminateScope::getSize();
535f4a2713aSLionel Sambuc }
536f4a2713aSLionel Sambuc
find(stable_iterator sp)537f4a2713aSLionel Sambuc inline EHScopeStack::iterator EHScopeStack::find(stable_iterator sp) const {
538f4a2713aSLionel Sambuc assert(sp.isValid() && "finding invalid savepoint");
539f4a2713aSLionel Sambuc assert(sp.Size <= stable_begin().Size && "finding savepoint after pop");
540f4a2713aSLionel Sambuc return iterator(EndOfBuffer - sp.Size);
541f4a2713aSLionel Sambuc }
542f4a2713aSLionel Sambuc
543f4a2713aSLionel Sambuc inline EHScopeStack::stable_iterator
stabilize(iterator ir)544f4a2713aSLionel Sambuc EHScopeStack::stabilize(iterator ir) const {
545f4a2713aSLionel Sambuc assert(StartOfData <= ir.Ptr && ir.Ptr <= EndOfBuffer);
546f4a2713aSLionel Sambuc return stable_iterator(EndOfBuffer - ir.Ptr);
547f4a2713aSLionel Sambuc }
548f4a2713aSLionel Sambuc
549f4a2713aSLionel Sambuc }
550f4a2713aSLionel Sambuc }
551f4a2713aSLionel Sambuc
552f4a2713aSLionel Sambuc #endif
553