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