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