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