| 1 | //===--- CGCleanup.cpp - Bookkeeping and code emission for cleanups -------===// |
| 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 | // This file contains code dealing with the IR generation for cleanups |
| 10 | // and related information. |
| 11 | // |
| 12 | // A "cleanup" is a piece of code which needs to be executed whenever |
| 13 | // control transfers out of a particular scope. This can be |
| 14 | // conditionalized to occur only on exceptional control flow, only on |
| 15 | // normal control flow, or both. |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #include "CGCleanup.h" |
| 20 | #include "CodeGenFunction.h" |
| 21 | #include "llvm/Support/SaveAndRestore.h" |
| 22 | |
| 23 | using namespace clang; |
| 24 | using namespace CodeGen; |
| 25 | |
| 26 | bool DominatingValue<RValue>::saved_type::needsSaving(RValue rv) { |
| 27 | if (rv.isScalar()) |
| 28 | return DominatingLLVMValue::needsSaving(value: rv.getScalarVal()); |
| 29 | if (rv.isAggregate()) |
| 30 | return DominatingValue<Address>::needsSaving(value: rv.getAggregateAddress()); |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | DominatingValue<RValue>::saved_type |
| 35 | DominatingValue<RValue>::saved_type::save(CodeGenFunction &CGF, RValue rv) { |
| 36 | if (rv.isScalar()) { |
| 37 | llvm::Value *V = rv.getScalarVal(); |
| 38 | return saved_type(DominatingLLVMValue::save(CGF, value: V), |
| 39 | DominatingLLVMValue::needsSaving(value: V) ? ScalarAddress |
| 40 | : ScalarLiteral); |
| 41 | } |
| 42 | |
| 43 | if (rv.isComplex()) { |
| 44 | CodeGenFunction::ComplexPairTy V = rv.getComplexVal(); |
| 45 | return saved_type(DominatingLLVMValue::save(CGF, value: V.first), |
| 46 | DominatingLLVMValue::save(CGF, value: V.second)); |
| 47 | } |
| 48 | |
| 49 | assert(rv.isAggregate()); |
| 50 | Address V = rv.getAggregateAddress(); |
| 51 | return saved_type(DominatingValue<Address>::save(CGF, value: V), |
| 52 | DominatingValue<Address>::needsSaving(value: V) |
| 53 | ? AggregateAddress |
| 54 | : AggregateLiteral); |
| 55 | } |
| 56 | |
| 57 | /// Given a saved r-value produced by SaveRValue, perform the code |
| 58 | /// necessary to restore it to usability at the current insertion |
| 59 | /// point. |
| 60 | RValue DominatingValue<RValue>::saved_type::restore(CodeGenFunction &CGF) { |
| 61 | switch (K) { |
| 62 | case ScalarLiteral: |
| 63 | case ScalarAddress: |
| 64 | return RValue::get(V: DominatingLLVMValue::restore(CGF, value: Vals.first)); |
| 65 | case AggregateLiteral: |
| 66 | case AggregateAddress: |
| 67 | return RValue::getAggregate( |
| 68 | addr: DominatingValue<Address>::restore(CGF, value: AggregateAddr)); |
| 69 | case ComplexAddress: { |
| 70 | llvm::Value *real = DominatingLLVMValue::restore(CGF, value: Vals.first); |
| 71 | llvm::Value *imag = DominatingLLVMValue::restore(CGF, value: Vals.second); |
| 72 | return RValue::getComplex(V1: real, V2: imag); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | llvm_unreachable("bad saved r-value kind" ); |
| 77 | } |
| 78 | |
| 79 | /// Push an entry of the given size onto this protected-scope stack. |
| 80 | char *EHScopeStack::allocate(size_t Size) { |
| 81 | Size = llvm::alignTo(Value: Size, Align: ScopeStackAlignment); |
| 82 | if (!StartOfBuffer) { |
| 83 | unsigned Capacity = 1024; |
| 84 | while (Capacity < Size) Capacity *= 2; |
| 85 | StartOfBuffer = new char[Capacity]; |
| 86 | StartOfData = EndOfBuffer = StartOfBuffer + Capacity; |
| 87 | } else if (static_cast<size_t>(StartOfData - StartOfBuffer) < Size) { |
| 88 | unsigned CurrentCapacity = EndOfBuffer - StartOfBuffer; |
| 89 | unsigned UsedCapacity = CurrentCapacity - (StartOfData - StartOfBuffer); |
| 90 | |
| 91 | unsigned NewCapacity = CurrentCapacity; |
| 92 | do { |
| 93 | NewCapacity *= 2; |
| 94 | } while (NewCapacity < UsedCapacity + Size); |
| 95 | |
| 96 | char *NewStartOfBuffer = new char[NewCapacity]; |
| 97 | char *NewEndOfBuffer = NewStartOfBuffer + NewCapacity; |
| 98 | char *NewStartOfData = NewEndOfBuffer - UsedCapacity; |
| 99 | memcpy(dest: NewStartOfData, src: StartOfData, n: UsedCapacity); |
| 100 | delete [] StartOfBuffer; |
| 101 | StartOfBuffer = NewStartOfBuffer; |
| 102 | EndOfBuffer = NewEndOfBuffer; |
| 103 | StartOfData = NewStartOfData; |
| 104 | } |
| 105 | |
| 106 | assert(StartOfBuffer + Size <= StartOfData); |
| 107 | StartOfData -= Size; |
| 108 | return StartOfData; |
| 109 | } |
| 110 | |
| 111 | void EHScopeStack::deallocate(size_t Size) { |
| 112 | StartOfData += llvm::alignTo(Value: Size, Align: ScopeStackAlignment); |
| 113 | } |
| 114 | |
| 115 | bool EHScopeStack::containsOnlyNoopCleanups( |
| 116 | EHScopeStack::stable_iterator Old) const { |
| 117 | for (EHScopeStack::iterator it = begin(); stabilize(ir: it) != Old; it++) { |
| 118 | EHCleanupScope *cleanup = dyn_cast<EHCleanupScope>(Val: &*it); |
| 119 | // If this is anything other than a lifetime marker or fake use cleanup, |
| 120 | // then the scope stack does not contain only noop cleanups. |
| 121 | if (!cleanup) |
| 122 | return false; |
| 123 | if (!cleanup->isLifetimeMarker() && !cleanup->isFakeUse()) |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | bool EHScopeStack::requiresLandingPad() const { |
| 131 | for (stable_iterator si = getInnermostEHScope(); si != stable_end(); ) { |
| 132 | // Skip lifetime markers. |
| 133 | if (auto *cleanup = dyn_cast<EHCleanupScope>(Val: &*find(sp: si))) |
| 134 | if (cleanup->isLifetimeMarker()) { |
| 135 | si = cleanup->getEnclosingEHScope(); |
| 136 | continue; |
| 137 | } |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | EHScopeStack::stable_iterator |
| 145 | EHScopeStack::getInnermostActiveNormalCleanup() const { |
| 146 | for (stable_iterator si = getInnermostNormalCleanup(), se = stable_end(); |
| 147 | si != se; ) { |
| 148 | EHCleanupScope &cleanup = cast<EHCleanupScope>(Val&: *find(sp: si)); |
| 149 | if (cleanup.isActive()) return si; |
| 150 | si = cleanup.getEnclosingNormalCleanup(); |
| 151 | } |
| 152 | return stable_end(); |
| 153 | } |
| 154 | |
| 155 | |
| 156 | void *EHScopeStack::pushCleanup(CleanupKind Kind, size_t Size) { |
| 157 | char *Buffer = allocate(Size: EHCleanupScope::getSizeForCleanupSize(Size)); |
| 158 | bool IsNormalCleanup = Kind & NormalCleanup; |
| 159 | bool IsEHCleanup = Kind & EHCleanup; |
| 160 | bool IsLifetimeMarker = Kind & LifetimeMarker; |
| 161 | bool IsFakeUse = Kind & FakeUse; |
| 162 | bool IsSEHFinallyCleanup = Kind & SEHFinallyCleanup; |
| 163 | |
| 164 | // Per C++ [except.terminate], it is implementation-defined whether none, |
| 165 | // some, or all cleanups are called before std::terminate. Thus, when |
| 166 | // terminate is the current EH scope, we may skip adding any EH cleanup |
| 167 | // scopes. |
| 168 | if (InnermostEHScope != stable_end() && |
| 169 | find(sp: InnermostEHScope)->getKind() == EHScope::Terminate) |
| 170 | IsEHCleanup = false; |
| 171 | |
| 172 | EHCleanupScope *Scope = |
| 173 | new (Buffer) EHCleanupScope(IsNormalCleanup, |
| 174 | IsEHCleanup, |
| 175 | Size, |
| 176 | BranchFixups.size(), |
| 177 | InnermostNormalCleanup, |
| 178 | InnermostEHScope); |
| 179 | if (IsNormalCleanup) |
| 180 | InnermostNormalCleanup = stable_begin(); |
| 181 | if (IsEHCleanup) |
| 182 | InnermostEHScope = stable_begin(); |
| 183 | if (IsLifetimeMarker) |
| 184 | Scope->setLifetimeMarker(); |
| 185 | if (IsFakeUse) |
| 186 | Scope->setFakeUse(); |
| 187 | if (IsSEHFinallyCleanup) |
| 188 | Scope->setSEHFinallyCleanup(); |
| 189 | |
| 190 | // With Windows -EHa, Invoke llvm.seh.scope.begin() for EHCleanup |
| 191 | // If exceptions are disabled/ignored and SEH is not in use, then there is no |
| 192 | // invoke destination. SEH "works" even if exceptions are off. In practice, |
| 193 | // this means that C++ destructors and other EH cleanups don't run, which is |
| 194 | // consistent with MSVC's behavior, except in the presence of -EHa. |
| 195 | // Check getInvokeDest() to generate llvm.seh.scope.begin() as needed. |
| 196 | if (CGF->getLangOpts().EHAsynch && IsEHCleanup && !IsLifetimeMarker && |
| 197 | !IsSEHFinallyCleanup && CGF->getTarget().getCXXABI().isMicrosoft() && |
| 198 | CGF->getInvokeDest()) |
| 199 | CGF->EmitSehCppScopeBegin(); |
| 200 | |
| 201 | return Scope->getCleanupBuffer(); |
| 202 | } |
| 203 | |
| 204 | void EHScopeStack::popCleanup() { |
| 205 | assert(!empty() && "popping exception stack when not empty" ); |
| 206 | |
| 207 | assert(isa<EHCleanupScope>(*begin())); |
| 208 | EHCleanupScope &Cleanup = cast<EHCleanupScope>(Val&: *begin()); |
| 209 | InnermostNormalCleanup = Cleanup.getEnclosingNormalCleanup(); |
| 210 | InnermostEHScope = Cleanup.getEnclosingEHScope(); |
| 211 | deallocate(Size: Cleanup.getAllocatedSize()); |
| 212 | |
| 213 | // Destroy the cleanup. |
| 214 | Cleanup.Destroy(); |
| 215 | |
| 216 | // Check whether we can shrink the branch-fixups stack. |
| 217 | if (!BranchFixups.empty()) { |
| 218 | // If we no longer have any normal cleanups, all the fixups are |
| 219 | // complete. |
| 220 | if (!hasNormalCleanups()) |
| 221 | BranchFixups.clear(); |
| 222 | |
| 223 | // Otherwise we can still trim out unnecessary nulls. |
| 224 | else |
| 225 | popNullFixups(); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | EHFilterScope *EHScopeStack::pushFilter(unsigned numFilters) { |
| 230 | assert(getInnermostEHScope() == stable_end()); |
| 231 | char *buffer = allocate(Size: EHFilterScope::getSizeForNumFilters(numFilters)); |
| 232 | EHFilterScope *filter = new (buffer) EHFilterScope(numFilters); |
| 233 | InnermostEHScope = stable_begin(); |
| 234 | return filter; |
| 235 | } |
| 236 | |
| 237 | void EHScopeStack::popFilter() { |
| 238 | assert(!empty() && "popping exception stack when not empty" ); |
| 239 | |
| 240 | EHFilterScope &filter = cast<EHFilterScope>(Val&: *begin()); |
| 241 | deallocate(Size: EHFilterScope::getSizeForNumFilters(numFilters: filter.getNumFilters())); |
| 242 | |
| 243 | InnermostEHScope = filter.getEnclosingEHScope(); |
| 244 | } |
| 245 | |
| 246 | EHCatchScope *EHScopeStack::pushCatch(unsigned numHandlers) { |
| 247 | char *buffer = allocate(Size: EHCatchScope::getSizeForNumHandlers(N: numHandlers)); |
| 248 | EHCatchScope *scope = |
| 249 | new (buffer) EHCatchScope(numHandlers, InnermostEHScope); |
| 250 | InnermostEHScope = stable_begin(); |
| 251 | return scope; |
| 252 | } |
| 253 | |
| 254 | void EHScopeStack::pushTerminate() { |
| 255 | char *Buffer = allocate(Size: EHTerminateScope::getSize()); |
| 256 | new (Buffer) EHTerminateScope(InnermostEHScope); |
| 257 | InnermostEHScope = stable_begin(); |
| 258 | } |
| 259 | |
| 260 | /// Remove any 'null' fixups on the stack. However, we can't pop more |
| 261 | /// fixups than the fixup depth on the innermost normal cleanup, or |
| 262 | /// else fixups that we try to add to that cleanup will end up in the |
| 263 | /// wrong place. We *could* try to shrink fixup depths, but that's |
| 264 | /// actually a lot of work for little benefit. |
| 265 | void EHScopeStack::popNullFixups() { |
| 266 | // We expect this to only be called when there's still an innermost |
| 267 | // normal cleanup; otherwise there really shouldn't be any fixups. |
| 268 | assert(hasNormalCleanups()); |
| 269 | |
| 270 | EHScopeStack::iterator it = find(sp: InnermostNormalCleanup); |
| 271 | unsigned MinSize = cast<EHCleanupScope>(Val&: *it).getFixupDepth(); |
| 272 | assert(BranchFixups.size() >= MinSize && "fixup stack out of order" ); |
| 273 | |
| 274 | while (BranchFixups.size() > MinSize && |
| 275 | BranchFixups.back().Destination == nullptr) |
| 276 | BranchFixups.pop_back(); |
| 277 | } |
| 278 | |
| 279 | RawAddress CodeGenFunction::createCleanupActiveFlag() { |
| 280 | // Create a variable to decide whether the cleanup needs to be run. |
| 281 | RawAddress active = CreateTempAllocaWithoutCast( |
| 282 | Ty: Builder.getInt1Ty(), align: CharUnits::One(), Name: "cleanup.cond" ); |
| 283 | |
| 284 | // Initialize it to false at a site that's guaranteed to be run |
| 285 | // before each evaluation. |
| 286 | setBeforeOutermostConditional(value: Builder.getFalse(), addr: active, CGF&: *this); |
| 287 | |
| 288 | // Initialize it to true at the current location. |
| 289 | Builder.CreateStore(Val: Builder.getTrue(), Addr: active); |
| 290 | |
| 291 | return active; |
| 292 | } |
| 293 | |
| 294 | void CodeGenFunction::initFullExprCleanupWithFlag(RawAddress ActiveFlag) { |
| 295 | // Set that as the active flag in the cleanup. |
| 296 | EHCleanupScope &cleanup = cast<EHCleanupScope>(Val&: *EHStack.begin()); |
| 297 | assert(!cleanup.hasActiveFlag() && "cleanup already has active flag?" ); |
| 298 | cleanup.setActiveFlag(ActiveFlag); |
| 299 | |
| 300 | if (cleanup.isNormalCleanup()) cleanup.setTestFlagInNormalCleanup(); |
| 301 | if (cleanup.isEHCleanup()) cleanup.setTestFlagInEHCleanup(); |
| 302 | } |
| 303 | |
| 304 | void EHScopeStack::Cleanup::anchor() {} |
| 305 | |
| 306 | static void createStoreInstBefore(llvm::Value *value, Address addr, |
| 307 | llvm::BasicBlock::iterator beforeInst, |
| 308 | CodeGenFunction &CGF) { |
| 309 | auto store = new llvm::StoreInst(value, addr.emitRawPointer(CGF), beforeInst); |
| 310 | store->setAlignment(addr.getAlignment().getAsAlign()); |
| 311 | } |
| 312 | |
| 313 | static llvm::LoadInst * |
| 314 | createLoadInstBefore(Address addr, const Twine &name, |
| 315 | llvm::BasicBlock::iterator beforeInst, |
| 316 | CodeGenFunction &CGF) { |
| 317 | return new llvm::LoadInst(addr.getElementType(), addr.emitRawPointer(CGF), |
| 318 | name, false, addr.getAlignment().getAsAlign(), |
| 319 | beforeInst); |
| 320 | } |
| 321 | |
| 322 | static llvm::LoadInst *createLoadInstBefore(Address addr, const Twine &name, |
| 323 | CodeGenFunction &CGF) { |
| 324 | return new llvm::LoadInst(addr.getElementType(), addr.emitRawPointer(CGF), |
| 325 | name, false, addr.getAlignment().getAsAlign()); |
| 326 | } |
| 327 | |
| 328 | /// All the branch fixups on the EH stack have propagated out past the |
| 329 | /// outermost normal cleanup; resolve them all by adding cases to the |
| 330 | /// given switch instruction. |
| 331 | static void ResolveAllBranchFixups(CodeGenFunction &CGF, |
| 332 | llvm::SwitchInst *Switch, |
| 333 | llvm::BasicBlock *CleanupEntry) { |
| 334 | llvm::SmallPtrSet<llvm::BasicBlock*, 4> CasesAdded; |
| 335 | |
| 336 | for (unsigned I = 0, E = CGF.EHStack.getNumBranchFixups(); I != E; ++I) { |
| 337 | // Skip this fixup if its destination isn't set. |
| 338 | BranchFixup &Fixup = CGF.EHStack.getBranchFixup(I); |
| 339 | if (Fixup.Destination == nullptr) continue; |
| 340 | |
| 341 | // If there isn't an OptimisticBranchBlock, then InitialBranch is |
| 342 | // still pointing directly to its destination; forward it to the |
| 343 | // appropriate cleanup entry. This is required in the specific |
| 344 | // case of |
| 345 | // { std::string s; goto lbl; } |
| 346 | // lbl: |
| 347 | // i.e. where there's an unresolved fixup inside a single cleanup |
| 348 | // entry which we're currently popping. |
| 349 | if (Fixup.OptimisticBranchBlock == nullptr) { |
| 350 | createStoreInstBefore(value: CGF.Builder.getInt32(C: Fixup.DestinationIndex), |
| 351 | addr: CGF.getNormalCleanupDestSlot(), |
| 352 | beforeInst: Fixup.InitialBranch->getIterator(), CGF); |
| 353 | Fixup.InitialBranch->setSuccessor(CleanupEntry); |
| 354 | } |
| 355 | |
| 356 | // Don't add this case to the switch statement twice. |
| 357 | if (!CasesAdded.insert(Ptr: Fixup.Destination).second) |
| 358 | continue; |
| 359 | |
| 360 | Switch->addCase(OnVal: CGF.Builder.getInt32(C: Fixup.DestinationIndex), |
| 361 | Dest: Fixup.Destination); |
| 362 | } |
| 363 | |
| 364 | CGF.EHStack.clearFixups(); |
| 365 | } |
| 366 | |
| 367 | /// Transitions the terminator of the given exit-block of a cleanup to |
| 368 | /// be a cleanup switch. |
| 369 | static llvm::SwitchInst *TransitionToCleanupSwitch(CodeGenFunction &CGF, |
| 370 | llvm::BasicBlock *Block) { |
| 371 | // If it's a branch, turn it into a switch whose default |
| 372 | // destination is its original target. |
| 373 | llvm::Instruction *Term = Block->getTerminator(); |
| 374 | |
| 375 | if (llvm::UncondBrInst *Br = dyn_cast<llvm::UncondBrInst>(Val: Term)) { |
| 376 | auto Load = createLoadInstBefore(addr: CGF.getNormalCleanupDestSlot(), |
| 377 | name: "cleanup.dest" , beforeInst: Term->getIterator(), CGF); |
| 378 | llvm::SwitchInst *Switch = |
| 379 | llvm::SwitchInst::Create(Value: Load, Default: Br->getSuccessor(i: 0), NumCases: 4, InsertBefore: Block); |
| 380 | Br->eraseFromParent(); |
| 381 | return Switch; |
| 382 | } else { |
| 383 | return cast<llvm::SwitchInst>(Val: Term); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | void CodeGenFunction::ResolveBranchFixups(llvm::BasicBlock *Block) { |
| 388 | assert(Block && "resolving a null target block" ); |
| 389 | if (!EHStack.getNumBranchFixups()) return; |
| 390 | |
| 391 | assert(EHStack.hasNormalCleanups() && |
| 392 | "branch fixups exist with no normal cleanups on stack" ); |
| 393 | |
| 394 | llvm::SmallPtrSet<llvm::BasicBlock*, 4> ModifiedOptimisticBlocks; |
| 395 | bool ResolvedAny = false; |
| 396 | |
| 397 | for (unsigned I = 0, E = EHStack.getNumBranchFixups(); I != E; ++I) { |
| 398 | // Skip this fixup if its destination doesn't match. |
| 399 | BranchFixup &Fixup = EHStack.getBranchFixup(I); |
| 400 | if (Fixup.Destination != Block) continue; |
| 401 | |
| 402 | Fixup.Destination = nullptr; |
| 403 | ResolvedAny = true; |
| 404 | |
| 405 | // If it doesn't have an optimistic branch block, LatestBranch is |
| 406 | // already pointing to the right place. |
| 407 | llvm::BasicBlock *BranchBB = Fixup.OptimisticBranchBlock; |
| 408 | if (!BranchBB) |
| 409 | continue; |
| 410 | |
| 411 | // Don't process the same optimistic branch block twice. |
| 412 | if (!ModifiedOptimisticBlocks.insert(Ptr: BranchBB).second) |
| 413 | continue; |
| 414 | |
| 415 | llvm::SwitchInst *Switch = TransitionToCleanupSwitch(CGF&: *this, Block: BranchBB); |
| 416 | |
| 417 | // Add a case to the switch. |
| 418 | Switch->addCase(OnVal: Builder.getInt32(C: Fixup.DestinationIndex), Dest: Block); |
| 419 | } |
| 420 | |
| 421 | if (ResolvedAny) |
| 422 | EHStack.popNullFixups(); |
| 423 | } |
| 424 | |
| 425 | /// Pops cleanup blocks until the given savepoint is reached. |
| 426 | void CodeGenFunction::PopCleanupBlocks( |
| 427 | EHScopeStack::stable_iterator Old, |
| 428 | std::initializer_list<llvm::Value **> ValuesToReload) { |
| 429 | assert(Old.isValid()); |
| 430 | |
| 431 | bool HadBranches = false; |
| 432 | while (EHStack.stable_begin() != Old) { |
| 433 | EHCleanupScope &Scope = cast<EHCleanupScope>(Val&: *EHStack.begin()); |
| 434 | HadBranches |= Scope.hasBranches(); |
| 435 | |
| 436 | // As long as Old strictly encloses the scope's enclosing normal |
| 437 | // cleanup, we're going to emit another normal cleanup which |
| 438 | // fallthrough can propagate through. |
| 439 | bool FallThroughIsBranchThrough = |
| 440 | Old.strictlyEncloses(I: Scope.getEnclosingNormalCleanup()); |
| 441 | |
| 442 | PopCleanupBlock(FallThroughIsBranchThrough); |
| 443 | } |
| 444 | |
| 445 | // If we didn't have any branches, the insertion point before cleanups must |
| 446 | // dominate the current insertion point and we don't need to reload any |
| 447 | // values. |
| 448 | if (!HadBranches) |
| 449 | return; |
| 450 | |
| 451 | // Spill and reload all values that the caller wants to be live at the current |
| 452 | // insertion point. |
| 453 | for (llvm::Value **ReloadedValue : ValuesToReload) { |
| 454 | auto *Inst = dyn_cast_or_null<llvm::Instruction>(Val: *ReloadedValue); |
| 455 | if (!Inst) |
| 456 | continue; |
| 457 | |
| 458 | // Don't spill static allocas, they dominate all cleanups. These are created |
| 459 | // by binding a reference to a local variable or temporary. |
| 460 | auto *AI = dyn_cast<llvm::AllocaInst>(Val: Inst); |
| 461 | if (AI && AI->isStaticAlloca()) |
| 462 | continue; |
| 463 | |
| 464 | Address Tmp = |
| 465 | CreateDefaultAlignTempAlloca(Ty: Inst->getType(), Name: "tmp.exprcleanup" ); |
| 466 | |
| 467 | // Find an insertion point after Inst and spill it to the temporary. |
| 468 | llvm::BasicBlock::iterator InsertBefore; |
| 469 | if (auto *Invoke = dyn_cast<llvm::InvokeInst>(Val: Inst)) |
| 470 | InsertBefore = Invoke->getNormalDest()->getFirstInsertionPt(); |
| 471 | else |
| 472 | InsertBefore = std::next(x: Inst->getIterator()); |
| 473 | CGBuilderTy(CGM, &*InsertBefore).CreateStore(Val: Inst, Addr: Tmp); |
| 474 | |
| 475 | // Reload the value at the current insertion point. |
| 476 | *ReloadedValue = Builder.CreateLoad(Addr: Tmp); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | /// Pops cleanup blocks until the given savepoint is reached, then add the |
| 481 | /// cleanups from the given savepoint in the lifetime-extended cleanups stack. |
| 482 | void CodeGenFunction::PopCleanupBlocks( |
| 483 | EHScopeStack::stable_iterator Old, size_t OldLifetimeExtendedSize, |
| 484 | std::initializer_list<llvm::Value **> ValuesToReload) { |
| 485 | PopCleanupBlocks(Old, ValuesToReload); |
| 486 | |
| 487 | // Move our deferred cleanups onto the EH stack. |
| 488 | for (size_t I = OldLifetimeExtendedSize, |
| 489 | E = LifetimeExtendedCleanupStack.size(); I != E; /**/) { |
| 490 | // Alignment should be guaranteed by the vptrs in the individual cleanups. |
| 491 | assert((I % alignof(LifetimeExtendedCleanupHeader) == 0) && |
| 492 | "misaligned cleanup stack entry" ); |
| 493 | |
| 494 | LifetimeExtendedCleanupHeader & = |
| 495 | reinterpret_cast<LifetimeExtendedCleanupHeader&>( |
| 496 | LifetimeExtendedCleanupStack[I]); |
| 497 | I += sizeof(Header); |
| 498 | |
| 499 | EHStack.pushCopyOfCleanup(Kind: Header.getKind(), |
| 500 | Cleanup: &LifetimeExtendedCleanupStack[I], |
| 501 | Size: Header.getSize()); |
| 502 | I += Header.getSize(); |
| 503 | |
| 504 | if (Header.isConditional()) { |
| 505 | RawAddress ActiveFlag = |
| 506 | reinterpret_cast<RawAddress &>(LifetimeExtendedCleanupStack[I]); |
| 507 | initFullExprCleanupWithFlag(ActiveFlag); |
| 508 | I += sizeof(ActiveFlag); |
| 509 | } |
| 510 | } |
| 511 | LifetimeExtendedCleanupStack.resize(N: OldLifetimeExtendedSize); |
| 512 | } |
| 513 | |
| 514 | static llvm::BasicBlock *CreateNormalEntry(CodeGenFunction &CGF, |
| 515 | EHCleanupScope &Scope) { |
| 516 | assert(Scope.isNormalCleanup()); |
| 517 | llvm::BasicBlock *Entry = Scope.getNormalBlock(); |
| 518 | if (!Entry) { |
| 519 | Entry = CGF.createBasicBlock(name: "cleanup" ); |
| 520 | Scope.setNormalBlock(Entry); |
| 521 | } |
| 522 | return Entry; |
| 523 | } |
| 524 | |
| 525 | /// Attempts to reduce a cleanup's entry block to a fallthrough. This |
| 526 | /// is basically llvm::MergeBlockIntoPredecessor, except |
| 527 | /// simplified/optimized for the tighter constraints on cleanup blocks. |
| 528 | /// |
| 529 | /// Returns the new block, whatever it is. |
| 530 | static llvm::BasicBlock *SimplifyCleanupEntry(CodeGenFunction &CGF, |
| 531 | llvm::BasicBlock *Entry) { |
| 532 | llvm::BasicBlock *Pred = Entry->getSinglePredecessor(); |
| 533 | if (!Pred) return Entry; |
| 534 | |
| 535 | llvm::UncondBrInst *Br = dyn_cast<llvm::UncondBrInst>(Val: Pred->getTerminator()); |
| 536 | if (!Br) |
| 537 | return Entry; |
| 538 | assert(Br->getSuccessor() == Entry); |
| 539 | |
| 540 | // If we were previously inserting at the end of the cleanup entry |
| 541 | // block, we'll need to continue inserting at the end of the |
| 542 | // predecessor. |
| 543 | bool WasInsertBlock = CGF.Builder.GetInsertBlock() == Entry; |
| 544 | assert(!WasInsertBlock || CGF.Builder.GetInsertPoint() == Entry->end()); |
| 545 | |
| 546 | // Kill the branch. |
| 547 | Br->eraseFromParent(); |
| 548 | |
| 549 | // Replace all uses of the entry with the predecessor, in case there |
| 550 | // are phis in the cleanup. |
| 551 | Entry->replaceAllUsesWith(V: Pred); |
| 552 | |
| 553 | // Merge the blocks. |
| 554 | Pred->splice(ToIt: Pred->end(), FromBB: Entry); |
| 555 | |
| 556 | // Kill the entry block. |
| 557 | Entry->eraseFromParent(); |
| 558 | |
| 559 | if (WasInsertBlock) |
| 560 | CGF.Builder.SetInsertPoint(Pred); |
| 561 | |
| 562 | return Pred; |
| 563 | } |
| 564 | |
| 565 | static void EmitCleanup(CodeGenFunction &CGF, |
| 566 | EHScopeStack::Cleanup *Fn, |
| 567 | EHScopeStack::Cleanup::Flags flags, |
| 568 | Address ActiveFlag) { |
| 569 | // If there's an active flag, load it and skip the cleanup if it's |
| 570 | // false. |
| 571 | llvm::BasicBlock *ContBB = nullptr; |
| 572 | if (ActiveFlag.isValid()) { |
| 573 | ContBB = CGF.createBasicBlock(name: "cleanup.done" ); |
| 574 | llvm::BasicBlock *CleanupBB = CGF.createBasicBlock(name: "cleanup.action" ); |
| 575 | llvm::Value *IsActive |
| 576 | = CGF.Builder.CreateLoad(Addr: ActiveFlag, Name: "cleanup.is_active" ); |
| 577 | CGF.Builder.CreateCondBr(Cond: IsActive, True: CleanupBB, False: ContBB); |
| 578 | CGF.EmitBlock(BB: CleanupBB); |
| 579 | } |
| 580 | |
| 581 | // Ask the cleanup to emit itself. |
| 582 | Fn->Emit(CGF, flags); |
| 583 | assert(CGF.HaveInsertPoint() && "cleanup ended with no insertion point?" ); |
| 584 | |
| 585 | // Emit the continuation block if there was an active flag. |
| 586 | if (ActiveFlag.isValid()) |
| 587 | CGF.EmitBlock(BB: ContBB); |
| 588 | } |
| 589 | |
| 590 | static void ForwardPrebranchedFallthrough(llvm::BasicBlock *Exit, |
| 591 | llvm::BasicBlock *From, |
| 592 | llvm::BasicBlock *To) { |
| 593 | // Exit is the exit block of a cleanup, so it always terminates in |
| 594 | // an unconditional branch or a switch. |
| 595 | llvm::Instruction *Term = Exit->getTerminator(); |
| 596 | |
| 597 | if (llvm::UncondBrInst *Br = dyn_cast<llvm::UncondBrInst>(Val: Term)) { |
| 598 | assert(Br->getSuccessor() == From); |
| 599 | Br->setSuccessor(To); |
| 600 | } else { |
| 601 | llvm::SwitchInst *Switch = cast<llvm::SwitchInst>(Val: Term); |
| 602 | for (unsigned I = 0, E = Switch->getNumSuccessors(); I != E; ++I) |
| 603 | if (Switch->getSuccessor(idx: I) == From) |
| 604 | Switch->setSuccessor(idx: I, NewSucc: To); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | /// We don't need a normal entry block for the given cleanup. |
| 609 | /// Optimistic fixup branches can cause these blocks to come into |
| 610 | /// existence anyway; if so, destroy it. |
| 611 | /// |
| 612 | /// The validity of this transformation is very much specific to the |
| 613 | /// exact ways in which we form branches to cleanup entries. |
| 614 | static void destroyOptimisticNormalEntry(CodeGenFunction &CGF, |
| 615 | EHCleanupScope &scope) { |
| 616 | llvm::BasicBlock *entry = scope.getNormalBlock(); |
| 617 | if (!entry) return; |
| 618 | |
| 619 | // Replace all the uses with unreachable. |
| 620 | llvm::BasicBlock *unreachableBB = CGF.getUnreachableBlock(); |
| 621 | for (llvm::BasicBlock::use_iterator |
| 622 | i = entry->use_begin(), e = entry->use_end(); i != e; ) { |
| 623 | llvm::Use &use = *i; |
| 624 | ++i; |
| 625 | |
| 626 | use.set(unreachableBB); |
| 627 | |
| 628 | // The only uses should be fixup switches. |
| 629 | llvm::SwitchInst *si = cast<llvm::SwitchInst>(Val: use.getUser()); |
| 630 | if (si->getNumCases() == 1 && si->getDefaultDest() == unreachableBB) { |
| 631 | // Replace the switch with a branch. |
| 632 | llvm::UncondBrInst::Create(Target: si->case_begin()->getCaseSuccessor(), |
| 633 | InsertBefore: si->getIterator()); |
| 634 | |
| 635 | // The switch operand is a load from the cleanup-dest alloca. |
| 636 | llvm::LoadInst *condition = cast<llvm::LoadInst>(Val: si->getCondition()); |
| 637 | |
| 638 | // Destroy the switch. |
| 639 | si->eraseFromParent(); |
| 640 | |
| 641 | // Destroy the load. |
| 642 | assert(condition->getOperand(0) == CGF.NormalCleanupDest.getPointer()); |
| 643 | assert(condition->use_empty()); |
| 644 | condition->eraseFromParent(); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | assert(entry->use_empty()); |
| 649 | delete entry; |
| 650 | } |
| 651 | |
| 652 | /// Pops a cleanup block. If the block includes a normal cleanup, the |
| 653 | /// current insertion point is threaded through the cleanup, as are |
| 654 | /// any branch fixups on the cleanup. |
| 655 | void CodeGenFunction::PopCleanupBlock(bool FallthroughIsBranchThrough, |
| 656 | bool ForDeactivation) { |
| 657 | assert(!EHStack.empty() && "cleanup stack is empty!" ); |
| 658 | assert(isa<EHCleanupScope>(*EHStack.begin()) && "top not a cleanup!" ); |
| 659 | EHCleanupScope &Scope = cast<EHCleanupScope>(Val&: *EHStack.begin()); |
| 660 | assert(Scope.getFixupDepth() <= EHStack.getNumBranchFixups()); |
| 661 | |
| 662 | // If we are deactivating a normal cleanup, we need to pretend that the |
| 663 | // fallthrough is unreachable. We restore this IP before returning. |
| 664 | CGBuilderTy::InsertPoint NormalDeactivateOrigIP; |
| 665 | if (ForDeactivation && (Scope.isNormalCleanup() || !getLangOpts().EHAsynch)) { |
| 666 | NormalDeactivateOrigIP = Builder.saveAndClearIP(); |
| 667 | } |
| 668 | // Remember activation information. |
| 669 | bool IsActive = Scope.isActive(); |
| 670 | Address NormalActiveFlag = |
| 671 | Scope.shouldTestFlagInNormalCleanup() ? Scope.getActiveFlag() |
| 672 | : Address::invalid(); |
| 673 | Address EHActiveFlag = |
| 674 | Scope.shouldTestFlagInEHCleanup() ? Scope.getActiveFlag() |
| 675 | : Address::invalid(); |
| 676 | |
| 677 | // Check whether we need an EH cleanup. This is only true if we've |
| 678 | // generated a lazy EH cleanup block. |
| 679 | llvm::BasicBlock *EHEntry = Scope.getCachedEHDispatchBlock(); |
| 680 | assert(Scope.hasEHBranches() == (EHEntry != nullptr)); |
| 681 | bool RequiresEHCleanup = (EHEntry != nullptr); |
| 682 | EHScopeStack::stable_iterator EHParent = Scope.getEnclosingEHScope(); |
| 683 | |
| 684 | // Check the three conditions which might require a normal cleanup: |
| 685 | |
| 686 | // - whether there are branch fix-ups through this cleanup |
| 687 | unsigned FixupDepth = Scope.getFixupDepth(); |
| 688 | bool HasFixups = EHStack.getNumBranchFixups() != FixupDepth; |
| 689 | |
| 690 | // - whether there are branch-throughs or branch-afters |
| 691 | bool HasExistingBranches = Scope.hasBranches(); |
| 692 | |
| 693 | // - whether there's a fallthrough |
| 694 | llvm::BasicBlock *FallthroughSource = Builder.GetInsertBlock(); |
| 695 | bool HasFallthrough = |
| 696 | FallthroughSource != nullptr && (IsActive || HasExistingBranches); |
| 697 | |
| 698 | // Branch-through fall-throughs leave the insertion point set to the |
| 699 | // end of the last cleanup, which points to the current scope. The |
| 700 | // rest of IR gen doesn't need to worry about this; it only happens |
| 701 | // during the execution of PopCleanupBlocks(). |
| 702 | bool HasPrebranchedFallthrough = |
| 703 | (FallthroughSource && FallthroughSource->hasTerminator()); |
| 704 | |
| 705 | // If this is a normal cleanup, then having a prebranched |
| 706 | // fallthrough implies that the fallthrough source unconditionally |
| 707 | // jumps here. |
| 708 | assert(!Scope.isNormalCleanup() || !HasPrebranchedFallthrough || |
| 709 | (Scope.getNormalBlock() && |
| 710 | FallthroughSource->getTerminator()->getSuccessor(0) |
| 711 | == Scope.getNormalBlock())); |
| 712 | |
| 713 | bool RequiresNormalCleanup = false; |
| 714 | if (Scope.isNormalCleanup() && |
| 715 | (HasFixups || HasExistingBranches || HasFallthrough)) { |
| 716 | RequiresNormalCleanup = true; |
| 717 | } |
| 718 | |
| 719 | // If we have a prebranched fallthrough into an inactive normal |
| 720 | // cleanup, rewrite it so that it leads to the appropriate place. |
| 721 | if (Scope.isNormalCleanup() && HasPrebranchedFallthrough && |
| 722 | !RequiresNormalCleanup) { |
| 723 | // FIXME: Come up with a program which would need forwarding prebranched |
| 724 | // fallthrough and add tests. Otherwise delete this and assert against it. |
| 725 | assert(!IsActive); |
| 726 | llvm::BasicBlock *prebranchDest; |
| 727 | |
| 728 | // If the prebranch is semantically branching through the next |
| 729 | // cleanup, just forward it to the next block, leaving the |
| 730 | // insertion point in the prebranched block. |
| 731 | if (FallthroughIsBranchThrough) { |
| 732 | EHScope &enclosing = *EHStack.find(sp: Scope.getEnclosingNormalCleanup()); |
| 733 | prebranchDest = CreateNormalEntry(CGF&: *this, Scope&: cast<EHCleanupScope>(Val&: enclosing)); |
| 734 | |
| 735 | // Otherwise, we need to make a new block. If the normal cleanup |
| 736 | // isn't being used at all, we could actually reuse the normal |
| 737 | // entry block, but this is simpler, and it avoids conflicts with |
| 738 | // dead optimistic fixup branches. |
| 739 | } else { |
| 740 | prebranchDest = createBasicBlock(name: "forwarded-prebranch" ); |
| 741 | EmitBlock(BB: prebranchDest); |
| 742 | } |
| 743 | |
| 744 | llvm::BasicBlock *normalEntry = Scope.getNormalBlock(); |
| 745 | assert(normalEntry && !normalEntry->use_empty()); |
| 746 | |
| 747 | ForwardPrebranchedFallthrough(Exit: FallthroughSource, |
| 748 | From: normalEntry, To: prebranchDest); |
| 749 | } |
| 750 | |
| 751 | // If we don't need the cleanup at all, we're done. |
| 752 | if (!RequiresNormalCleanup && !RequiresEHCleanup) { |
| 753 | destroyOptimisticNormalEntry(CGF&: *this, scope&: Scope); |
| 754 | EHStack.popCleanup(); // safe because there are no fixups |
| 755 | assert(EHStack.getNumBranchFixups() == 0 || |
| 756 | EHStack.hasNormalCleanups()); |
| 757 | if (NormalDeactivateOrigIP.isSet()) |
| 758 | Builder.restoreIP(IP: NormalDeactivateOrigIP); |
| 759 | return; |
| 760 | } |
| 761 | |
| 762 | // Copy the cleanup emission data out. This uses either a stack |
| 763 | // array or malloc'd memory, depending on the size, which is |
| 764 | // behavior that SmallVector would provide, if we could use it |
| 765 | // here. Unfortunately, if you ask for a SmallVector<char>, the |
| 766 | // alignment isn't sufficient. |
| 767 | auto *CleanupSource = reinterpret_cast<char *>(Scope.getCleanupBuffer()); |
| 768 | alignas(EHScopeStack::ScopeStackAlignment) char |
| 769 | CleanupBufferStack[8 * sizeof(void *)]; |
| 770 | std::unique_ptr<char[]> CleanupBufferHeap; |
| 771 | size_t CleanupSize = Scope.getCleanupSize(); |
| 772 | EHScopeStack::Cleanup *Fn; |
| 773 | |
| 774 | if (CleanupSize <= sizeof(CleanupBufferStack)) { |
| 775 | memcpy(dest: CleanupBufferStack, src: CleanupSource, n: CleanupSize); |
| 776 | Fn = reinterpret_cast<EHScopeStack::Cleanup *>(CleanupBufferStack); |
| 777 | } else { |
| 778 | CleanupBufferHeap.reset(p: new char[CleanupSize]); |
| 779 | memcpy(dest: CleanupBufferHeap.get(), src: CleanupSource, n: CleanupSize); |
| 780 | Fn = reinterpret_cast<EHScopeStack::Cleanup *>(CleanupBufferHeap.get()); |
| 781 | } |
| 782 | |
| 783 | EHScopeStack::Cleanup::Flags cleanupFlags; |
| 784 | if (Scope.isNormalCleanup()) |
| 785 | cleanupFlags.setIsNormalCleanupKind(); |
| 786 | if (Scope.isEHCleanup()) |
| 787 | cleanupFlags.setIsEHCleanupKind(); |
| 788 | |
| 789 | // Under -EHa, invoke seh.scope.end() to mark scope end before dtor |
| 790 | bool IsEHa = getLangOpts().EHAsynch && !Scope.isLifetimeMarker(); |
| 791 | bool IsSEHFinallyCleanup = Scope.isSEHFinallyCleanup(); |
| 792 | if (!RequiresNormalCleanup) { |
| 793 | // Mark CPP scope end for passed-by-value Arg temp |
| 794 | // per Windows ABI which is "normally" Cleanup in callee |
| 795 | if (IsEHa && getInvokeDest()) { |
| 796 | // If we are deactivating a normal cleanup then we don't have a |
| 797 | // fallthrough. Restore original IP to emit CPP scope ends in the correct |
| 798 | // block. |
| 799 | if (NormalDeactivateOrigIP.isSet()) |
| 800 | Builder.restoreIP(IP: NormalDeactivateOrigIP); |
| 801 | if (Builder.GetInsertBlock() && !IsSEHFinallyCleanup) |
| 802 | EmitSehCppScopeEnd(); |
| 803 | if (NormalDeactivateOrigIP.isSet()) |
| 804 | NormalDeactivateOrigIP = Builder.saveAndClearIP(); |
| 805 | } |
| 806 | destroyOptimisticNormalEntry(CGF&: *this, scope&: Scope); |
| 807 | Scope.MarkEmitted(); |
| 808 | EHStack.popCleanup(); |
| 809 | } else { |
| 810 | // If we have a fallthrough and no other need for the cleanup, |
| 811 | // emit it directly. |
| 812 | if (HasFallthrough && !HasPrebranchedFallthrough && !HasFixups && |
| 813 | !HasExistingBranches) { |
| 814 | |
| 815 | // mark SEH scope end for fall-through flow |
| 816 | if (IsEHa && getInvokeDest()) { |
| 817 | if (Scope.isSEHFinallyCleanup()) |
| 818 | EmitSehTryScopeEnd(); |
| 819 | else |
| 820 | EmitSehCppScopeEnd(); |
| 821 | } |
| 822 | |
| 823 | destroyOptimisticNormalEntry(CGF&: *this, scope&: Scope); |
| 824 | Scope.MarkEmitted(); |
| 825 | EHStack.popCleanup(); |
| 826 | |
| 827 | EmitCleanup(CGF&: *this, Fn, flags: cleanupFlags, ActiveFlag: NormalActiveFlag); |
| 828 | |
| 829 | // Otherwise, the best approach is to thread everything through |
| 830 | // the cleanup block and then try to clean up after ourselves. |
| 831 | } else { |
| 832 | // Force the entry block to exist. |
| 833 | llvm::BasicBlock *NormalEntry = CreateNormalEntry(CGF&: *this, Scope); |
| 834 | |
| 835 | // I. Set up the fallthrough edge in. |
| 836 | |
| 837 | CGBuilderTy::InsertPoint savedInactiveFallthroughIP; |
| 838 | |
| 839 | // If there's a fallthrough, we need to store the cleanup |
| 840 | // destination index. For fall-throughs this is always zero. |
| 841 | if (HasFallthrough) { |
| 842 | if (!HasPrebranchedFallthrough) |
| 843 | Builder.CreateStore(Val: Builder.getInt32(C: 0), Addr: getNormalCleanupDestSlot()); |
| 844 | |
| 845 | // Otherwise, save and clear the IP if we don't have fallthrough |
| 846 | // because the cleanup is inactive. |
| 847 | } else if (FallthroughSource) { |
| 848 | assert(!IsActive && "source without fallthrough for active cleanup" ); |
| 849 | savedInactiveFallthroughIP = Builder.saveAndClearIP(); |
| 850 | } |
| 851 | |
| 852 | // II. Emit the entry block. This implicitly branches to it if |
| 853 | // we have fallthrough. All the fixups and existing branches |
| 854 | // should already be branched to it. |
| 855 | EmitBlock(BB: NormalEntry); |
| 856 | |
| 857 | // intercept normal cleanup to mark SEH scope end |
| 858 | if (IsEHa && getInvokeDest()) { |
| 859 | if (Scope.isSEHFinallyCleanup()) |
| 860 | EmitSehTryScopeEnd(); |
| 861 | else |
| 862 | EmitSehCppScopeEnd(); |
| 863 | } |
| 864 | |
| 865 | // III. Figure out where we're going and build the cleanup |
| 866 | // epilogue. |
| 867 | |
| 868 | bool HasEnclosingCleanups = |
| 869 | (Scope.getEnclosingNormalCleanup() != EHStack.stable_end()); |
| 870 | |
| 871 | // Compute the branch-through dest if we need it: |
| 872 | // - if there are branch-throughs threaded through the scope |
| 873 | // - if fall-through is a branch-through |
| 874 | // - if there are fixups that will be optimistically forwarded |
| 875 | // to the enclosing cleanup |
| 876 | llvm::BasicBlock *BranchThroughDest = nullptr; |
| 877 | if (Scope.hasBranchThroughs() || |
| 878 | (FallthroughSource && FallthroughIsBranchThrough) || |
| 879 | (HasFixups && HasEnclosingCleanups)) { |
| 880 | assert(HasEnclosingCleanups); |
| 881 | EHScope &S = *EHStack.find(sp: Scope.getEnclosingNormalCleanup()); |
| 882 | BranchThroughDest = CreateNormalEntry(CGF&: *this, Scope&: cast<EHCleanupScope>(Val&: S)); |
| 883 | } |
| 884 | |
| 885 | llvm::BasicBlock *FallthroughDest = nullptr; |
| 886 | SmallVector<llvm::Instruction*, 2> InstsToAppend; |
| 887 | |
| 888 | // If there's exactly one branch-after and no other threads, |
| 889 | // we can route it without a switch. |
| 890 | // Skip for SEH, since ExitSwitch is used to generate code to indicate |
| 891 | // abnormal termination. (SEH: Except _leave and fall-through at |
| 892 | // the end, all other exits in a _try (return/goto/continue/break) |
| 893 | // are considered as abnormal terminations, using NormalCleanupDestSlot |
| 894 | // to indicate abnormal termination) |
| 895 | if (!Scope.hasBranchThroughs() && !HasFixups && !HasFallthrough && |
| 896 | !currentFunctionUsesSEHTry() && Scope.getNumBranchAfters() == 1) { |
| 897 | assert(!BranchThroughDest || !IsActive); |
| 898 | |
| 899 | // Clean up the possibly dead store to the cleanup dest slot. |
| 900 | llvm::Instruction *NormalCleanupDestSlot = |
| 901 | cast<llvm::Instruction>(Val: getNormalCleanupDestSlot().getPointer()); |
| 902 | if (NormalCleanupDestSlot->hasOneUse()) { |
| 903 | NormalCleanupDestSlot->user_back()->eraseFromParent(); |
| 904 | NormalCleanupDestSlot->eraseFromParent(); |
| 905 | NormalCleanupDest = RawAddress::invalid(); |
| 906 | } |
| 907 | |
| 908 | llvm::BasicBlock *BranchAfter = Scope.getBranchAfterBlock(I: 0); |
| 909 | InstsToAppend.push_back(Elt: llvm::UncondBrInst::Create(Target: BranchAfter)); |
| 910 | |
| 911 | // Build a switch-out if we need it: |
| 912 | // - if there are branch-afters threaded through the scope |
| 913 | // - if fall-through is a branch-after |
| 914 | // - if there are fixups that have nowhere left to go and |
| 915 | // so must be immediately resolved |
| 916 | } else if (Scope.getNumBranchAfters() || |
| 917 | (HasFallthrough && !FallthroughIsBranchThrough) || |
| 918 | (HasFixups && !HasEnclosingCleanups)) { |
| 919 | |
| 920 | llvm::BasicBlock *Default = |
| 921 | (BranchThroughDest ? BranchThroughDest : getUnreachableBlock()); |
| 922 | |
| 923 | // TODO: base this on the number of branch-afters and fixups |
| 924 | const unsigned SwitchCapacity = 10; |
| 925 | |
| 926 | // pass the abnormal exit flag to Fn (SEH cleanup) |
| 927 | cleanupFlags.setHasExitSwitch(); |
| 928 | |
| 929 | llvm::LoadInst *Load = createLoadInstBefore(addr: getNormalCleanupDestSlot(), |
| 930 | name: "cleanup.dest" , CGF&: *this); |
| 931 | llvm::SwitchInst *Switch = |
| 932 | llvm::SwitchInst::Create(Value: Load, Default, NumCases: SwitchCapacity); |
| 933 | |
| 934 | InstsToAppend.push_back(Elt: Load); |
| 935 | InstsToAppend.push_back(Elt: Switch); |
| 936 | |
| 937 | // Branch-after fallthrough. |
| 938 | if (FallthroughSource && !FallthroughIsBranchThrough) { |
| 939 | FallthroughDest = createBasicBlock(name: "cleanup.cont" ); |
| 940 | if (HasFallthrough) |
| 941 | Switch->addCase(OnVal: Builder.getInt32(C: 0), Dest: FallthroughDest); |
| 942 | } |
| 943 | |
| 944 | for (unsigned I = 0, E = Scope.getNumBranchAfters(); I != E; ++I) { |
| 945 | Switch->addCase(OnVal: Scope.getBranchAfterIndex(I), |
| 946 | Dest: Scope.getBranchAfterBlock(I)); |
| 947 | } |
| 948 | |
| 949 | // If there aren't any enclosing cleanups, we can resolve all |
| 950 | // the fixups now. |
| 951 | if (HasFixups && !HasEnclosingCleanups) |
| 952 | ResolveAllBranchFixups(CGF&: *this, Switch, CleanupEntry: NormalEntry); |
| 953 | } else { |
| 954 | // We should always have a branch-through destination in this case. |
| 955 | assert(BranchThroughDest); |
| 956 | InstsToAppend.push_back(Elt: llvm::UncondBrInst::Create(Target: BranchThroughDest)); |
| 957 | } |
| 958 | |
| 959 | // IV. Pop the cleanup and emit it. |
| 960 | Scope.MarkEmitted(); |
| 961 | EHStack.popCleanup(); |
| 962 | assert(EHStack.hasNormalCleanups() == HasEnclosingCleanups); |
| 963 | |
| 964 | EmitCleanup(CGF&: *this, Fn, flags: cleanupFlags, ActiveFlag: NormalActiveFlag); |
| 965 | |
| 966 | // Append the prepared cleanup prologue from above. |
| 967 | llvm::BasicBlock *NormalExit = Builder.GetInsertBlock(); |
| 968 | for (llvm::Instruction *Inst : InstsToAppend) |
| 969 | Inst->insertInto(ParentBB: NormalExit, It: NormalExit->end()); |
| 970 | |
| 971 | // Optimistically hope that any fixups will continue falling through. |
| 972 | for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups(); |
| 973 | I < E; ++I) { |
| 974 | BranchFixup &Fixup = EHStack.getBranchFixup(I); |
| 975 | if (!Fixup.Destination) |
| 976 | continue; |
| 977 | if (!Fixup.OptimisticBranchBlock) { |
| 978 | createStoreInstBefore(value: Builder.getInt32(C: Fixup.DestinationIndex), |
| 979 | addr: getNormalCleanupDestSlot(), |
| 980 | beforeInst: Fixup.InitialBranch->getIterator(), CGF&: *this); |
| 981 | Fixup.InitialBranch->setSuccessor(NormalEntry); |
| 982 | } |
| 983 | Fixup.OptimisticBranchBlock = NormalExit; |
| 984 | } |
| 985 | |
| 986 | // V. Set up the fallthrough edge out. |
| 987 | |
| 988 | // Case 1: a fallthrough source exists but doesn't branch to the |
| 989 | // cleanup because the cleanup is inactive. |
| 990 | if (!HasFallthrough && FallthroughSource) { |
| 991 | // Prebranched fallthrough was forwarded earlier. |
| 992 | // Non-prebranched fallthrough doesn't need to be forwarded. |
| 993 | // Either way, all we need to do is restore the IP we cleared before. |
| 994 | assert(!IsActive); |
| 995 | Builder.restoreIP(IP: savedInactiveFallthroughIP); |
| 996 | |
| 997 | // Case 2: a fallthrough source exists and should branch to the |
| 998 | // cleanup, but we're not supposed to branch through to the next |
| 999 | // cleanup. |
| 1000 | } else if (HasFallthrough && FallthroughDest) { |
| 1001 | assert(!FallthroughIsBranchThrough); |
| 1002 | EmitBlock(BB: FallthroughDest); |
| 1003 | |
| 1004 | // Case 3: a fallthrough source exists and should branch to the |
| 1005 | // cleanup and then through to the next. |
| 1006 | } else if (HasFallthrough) { |
| 1007 | // Everything is already set up for this. |
| 1008 | |
| 1009 | // Case 4: no fallthrough source exists. |
| 1010 | } else { |
| 1011 | Builder.ClearInsertionPoint(); |
| 1012 | } |
| 1013 | |
| 1014 | // VI. Assorted cleaning. |
| 1015 | |
| 1016 | // Check whether we can merge NormalEntry into a single predecessor. |
| 1017 | // This might invalidate (non-IR) pointers to NormalEntry. |
| 1018 | llvm::BasicBlock *NewNormalEntry = |
| 1019 | SimplifyCleanupEntry(CGF&: *this, Entry: NormalEntry); |
| 1020 | |
| 1021 | // If it did invalidate those pointers, and NormalEntry was the same |
| 1022 | // as NormalExit, go back and patch up the fixups. |
| 1023 | if (NewNormalEntry != NormalEntry && NormalEntry == NormalExit) |
| 1024 | for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups(); |
| 1025 | I < E; ++I) |
| 1026 | EHStack.getBranchFixup(I).OptimisticBranchBlock = NewNormalEntry; |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | if (NormalDeactivateOrigIP.isSet()) |
| 1031 | Builder.restoreIP(IP: NormalDeactivateOrigIP); |
| 1032 | assert(EHStack.hasNormalCleanups() || EHStack.getNumBranchFixups() == 0); |
| 1033 | |
| 1034 | // Emit the EH cleanup if required. |
| 1035 | if (RequiresEHCleanup) { |
| 1036 | CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); |
| 1037 | |
| 1038 | EmitBlock(BB: EHEntry); |
| 1039 | |
| 1040 | llvm::BasicBlock *NextAction = getEHDispatchBlock(scope: EHParent); |
| 1041 | |
| 1042 | // Push a terminate scope or cleanupendpad scope around the potentially |
| 1043 | // throwing cleanups. For funclet EH personalities, the cleanupendpad models |
| 1044 | // program termination when cleanups throw. |
| 1045 | bool PushedTerminate = false; |
| 1046 | SaveAndRestore RestoreCurrentFuncletPad(CurrentFuncletPad); |
| 1047 | llvm::CleanupPadInst *CPI = nullptr; |
| 1048 | |
| 1049 | const EHPersonality &Personality = EHPersonality::get(CGF&: *this); |
| 1050 | if (Personality.usesFuncletPads()) { |
| 1051 | llvm::Value *ParentPad = CurrentFuncletPad; |
| 1052 | if (!ParentPad) |
| 1053 | ParentPad = llvm::ConstantTokenNone::get(Context&: CGM.getLLVMContext()); |
| 1054 | CurrentFuncletPad = CPI = Builder.CreateCleanupPad(ParentPad); |
| 1055 | } |
| 1056 | |
| 1057 | // Non-MSVC personalities need to terminate when an EH cleanup throws. |
| 1058 | if (!Personality.isMSVCPersonality()) { |
| 1059 | EHStack.pushTerminate(); |
| 1060 | PushedTerminate = true; |
| 1061 | } else if (IsEHa && getInvokeDest()) { |
| 1062 | if (!IsSEHFinallyCleanup) |
| 1063 | EmitSehCppScopeEnd(); |
| 1064 | } |
| 1065 | |
| 1066 | // We only actually emit the cleanup code if the cleanup is either |
| 1067 | // active or was used before it was deactivated. |
| 1068 | if (EHActiveFlag.isValid() || IsActive) { |
| 1069 | cleanupFlags.setIsForEHCleanup(); |
| 1070 | EmitCleanup(CGF&: *this, Fn, flags: cleanupFlags, ActiveFlag: EHActiveFlag); |
| 1071 | } |
| 1072 | |
| 1073 | if (CPI) |
| 1074 | Builder.CreateCleanupRet(CleanupPad: CPI, UnwindBB: NextAction); |
| 1075 | else |
| 1076 | Builder.CreateBr(Dest: NextAction); |
| 1077 | |
| 1078 | // Leave the terminate scope. |
| 1079 | if (PushedTerminate) |
| 1080 | EHStack.popTerminate(); |
| 1081 | |
| 1082 | Builder.restoreIP(IP: SavedIP); |
| 1083 | |
| 1084 | SimplifyCleanupEntry(CGF&: *this, Entry: EHEntry); |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | /// isObviouslyBranchWithoutCleanups - Return true if a branch to the |
| 1089 | /// specified destination obviously has no cleanups to run. 'false' is always |
| 1090 | /// a conservatively correct answer for this method. |
| 1091 | bool CodeGenFunction::isObviouslyBranchWithoutCleanups(JumpDest Dest) const { |
| 1092 | assert(Dest.getScopeDepth().encloses(EHStack.stable_begin()) |
| 1093 | && "stale jump destination" ); |
| 1094 | |
| 1095 | // Calculate the innermost active normal cleanup. |
| 1096 | EHScopeStack::stable_iterator TopCleanup = |
| 1097 | EHStack.getInnermostActiveNormalCleanup(); |
| 1098 | |
| 1099 | // If we're not in an active normal cleanup scope, or if the |
| 1100 | // destination scope is within the innermost active normal cleanup |
| 1101 | // scope, we don't need to worry about fixups. |
| 1102 | if (TopCleanup == EHStack.stable_end() || |
| 1103 | TopCleanup.encloses(I: Dest.getScopeDepth())) // works for invalid |
| 1104 | return true; |
| 1105 | |
| 1106 | // Otherwise, we might need some cleanups. |
| 1107 | return false; |
| 1108 | } |
| 1109 | |
| 1110 | |
| 1111 | /// Terminate the current block by emitting a branch which might leave |
| 1112 | /// the current cleanup-protected scope. The target scope may not yet |
| 1113 | /// be known, in which case this will require a fixup. |
| 1114 | /// |
| 1115 | /// As a side-effect, this method clears the insertion point. |
| 1116 | void CodeGenFunction::EmitBranchThroughCleanup(JumpDest Dest) { |
| 1117 | assert(Dest.getScopeDepth().encloses(EHStack.stable_begin()) |
| 1118 | && "stale jump destination" ); |
| 1119 | |
| 1120 | if (!HaveInsertPoint()) |
| 1121 | return; |
| 1122 | |
| 1123 | // Create the branch. |
| 1124 | llvm::UncondBrInst *BI = Builder.CreateBr(Dest: Dest.getBlock()); |
| 1125 | addInstToCurrentSourceAtom(KeyInstruction: BI, Backup: nullptr); |
| 1126 | |
| 1127 | // Calculate the innermost active normal cleanup. |
| 1128 | EHScopeStack::stable_iterator |
| 1129 | TopCleanup = EHStack.getInnermostActiveNormalCleanup(); |
| 1130 | |
| 1131 | // If we're not in an active normal cleanup scope, or if the |
| 1132 | // destination scope is within the innermost active normal cleanup |
| 1133 | // scope, we don't need to worry about fixups. |
| 1134 | if (TopCleanup == EHStack.stable_end() || |
| 1135 | TopCleanup.encloses(I: Dest.getScopeDepth())) { // works for invalid |
| 1136 | Builder.ClearInsertionPoint(); |
| 1137 | return; |
| 1138 | } |
| 1139 | |
| 1140 | // If we can't resolve the destination cleanup scope, just add this |
| 1141 | // to the current cleanup scope as a branch fixup. |
| 1142 | if (!Dest.getScopeDepth().isValid()) { |
| 1143 | BranchFixup &Fixup = EHStack.addBranchFixup(); |
| 1144 | Fixup.Destination = Dest.getBlock(); |
| 1145 | Fixup.DestinationIndex = Dest.getDestIndex(); |
| 1146 | Fixup.InitialBranch = BI; |
| 1147 | Fixup.OptimisticBranchBlock = nullptr; |
| 1148 | |
| 1149 | Builder.ClearInsertionPoint(); |
| 1150 | return; |
| 1151 | } |
| 1152 | |
| 1153 | // Otherwise, thread through all the normal cleanups in scope. |
| 1154 | |
| 1155 | // Store the index at the start. |
| 1156 | llvm::ConstantInt *Index = Builder.getInt32(C: Dest.getDestIndex()); |
| 1157 | createStoreInstBefore(value: Index, addr: getNormalCleanupDestSlot(), beforeInst: BI->getIterator(), |
| 1158 | CGF&: *this); |
| 1159 | |
| 1160 | // Adjust BI to point to the first cleanup block. |
| 1161 | { |
| 1162 | EHCleanupScope &Scope = |
| 1163 | cast<EHCleanupScope>(Val&: *EHStack.find(sp: TopCleanup)); |
| 1164 | BI->setSuccessor(idx: 0, NewSucc: CreateNormalEntry(CGF&: *this, Scope)); |
| 1165 | } |
| 1166 | |
| 1167 | // Add this destination to all the scopes involved. |
| 1168 | EHScopeStack::stable_iterator I = TopCleanup; |
| 1169 | EHScopeStack::stable_iterator E = Dest.getScopeDepth(); |
| 1170 | if (E.strictlyEncloses(I)) { |
| 1171 | while (true) { |
| 1172 | EHCleanupScope &Scope = cast<EHCleanupScope>(Val&: *EHStack.find(sp: I)); |
| 1173 | assert(Scope.isNormalCleanup()); |
| 1174 | I = Scope.getEnclosingNormalCleanup(); |
| 1175 | |
| 1176 | // If this is the last cleanup we're propagating through, tell it |
| 1177 | // that there's a resolved jump moving through it. |
| 1178 | if (!E.strictlyEncloses(I)) { |
| 1179 | Scope.addBranchAfter(Index, Block: Dest.getBlock()); |
| 1180 | break; |
| 1181 | } |
| 1182 | |
| 1183 | // Otherwise, tell the scope that there's a jump propagating |
| 1184 | // through it. If this isn't new information, all the rest of |
| 1185 | // the work has been done before. |
| 1186 | if (!Scope.addBranchThrough(Block: Dest.getBlock())) |
| 1187 | break; |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | Builder.ClearInsertionPoint(); |
| 1192 | } |
| 1193 | |
| 1194 | static bool IsUsedAsEHCleanup(EHScopeStack &EHStack, |
| 1195 | EHScopeStack::stable_iterator cleanup) { |
| 1196 | // If we needed an EH block for any reason, that counts. |
| 1197 | if (EHStack.find(sp: cleanup)->hasEHBranches()) |
| 1198 | return true; |
| 1199 | |
| 1200 | // Check whether any enclosed cleanups were needed. |
| 1201 | for (EHScopeStack::stable_iterator |
| 1202 | i = EHStack.getInnermostEHScope(); i != cleanup; ) { |
| 1203 | assert(cleanup.strictlyEncloses(i)); |
| 1204 | |
| 1205 | EHScope &scope = *EHStack.find(sp: i); |
| 1206 | if (scope.hasEHBranches()) |
| 1207 | return true; |
| 1208 | |
| 1209 | i = scope.getEnclosingEHScope(); |
| 1210 | } |
| 1211 | |
| 1212 | return false; |
| 1213 | } |
| 1214 | |
| 1215 | enum ForActivation_t { |
| 1216 | ForActivation, |
| 1217 | ForDeactivation |
| 1218 | }; |
| 1219 | |
| 1220 | /// The given cleanup block is changing activation state. Configure a |
| 1221 | /// cleanup variable if necessary. |
| 1222 | /// |
| 1223 | /// It would be good if we had some way of determining if there were |
| 1224 | /// extra uses *after* the change-over point. |
| 1225 | static void SetupCleanupBlockActivation(CodeGenFunction &CGF, |
| 1226 | EHScopeStack::stable_iterator C, |
| 1227 | ForActivation_t kind, |
| 1228 | llvm::Instruction *dominatingIP) { |
| 1229 | EHCleanupScope &Scope = cast<EHCleanupScope>(Val&: *CGF.EHStack.find(sp: C)); |
| 1230 | |
| 1231 | // We always need the flag if we're activating the cleanup in a |
| 1232 | // conditional context, because we have to assume that the current |
| 1233 | // location doesn't necessarily dominate the cleanup's code. |
| 1234 | bool isActivatedInConditional = |
| 1235 | (kind == ForActivation && CGF.isInConditionalBranch()); |
| 1236 | |
| 1237 | bool needFlag = false; |
| 1238 | |
| 1239 | // Calculate whether the cleanup was used: |
| 1240 | |
| 1241 | // - as a normal cleanup |
| 1242 | if (Scope.isNormalCleanup()) { |
| 1243 | Scope.setTestFlagInNormalCleanup(); |
| 1244 | needFlag = true; |
| 1245 | } |
| 1246 | |
| 1247 | // - as an EH cleanup |
| 1248 | if (Scope.isEHCleanup() && |
| 1249 | (isActivatedInConditional || IsUsedAsEHCleanup(EHStack&: CGF.EHStack, cleanup: C))) { |
| 1250 | Scope.setTestFlagInEHCleanup(); |
| 1251 | needFlag = true; |
| 1252 | } |
| 1253 | |
| 1254 | // If it hasn't yet been used as either, we're done. |
| 1255 | if (!needFlag) |
| 1256 | return; |
| 1257 | |
| 1258 | Address var = Scope.getActiveFlag(); |
| 1259 | if (!var.isValid()) { |
| 1260 | CodeGenFunction::AllocaTrackerRAII AllocaTracker(CGF); |
| 1261 | var = CGF.CreateTempAlloca(Ty: CGF.Builder.getInt1Ty(), UseAddrSpace: LangAS::Default, |
| 1262 | align: CharUnits::One(), Name: "cleanup.isactive" ); |
| 1263 | Scope.setActiveFlag(var); |
| 1264 | Scope.AddAuxAllocas(Allocas: AllocaTracker.Take()); |
| 1265 | |
| 1266 | assert(dominatingIP && "no existing variable and no dominating IP!" ); |
| 1267 | |
| 1268 | // Initialize to true or false depending on whether it was |
| 1269 | // active up to this point. |
| 1270 | llvm::Constant *value = CGF.Builder.getInt1(V: kind == ForDeactivation); |
| 1271 | |
| 1272 | // If we're in a conditional block, ignore the dominating IP and |
| 1273 | // use the outermost conditional branch. |
| 1274 | if (CGF.isInConditionalBranch()) { |
| 1275 | CGF.setBeforeOutermostConditional(value, addr: var, CGF); |
| 1276 | } else { |
| 1277 | createStoreInstBefore(value, addr: var, beforeInst: dominatingIP->getIterator(), CGF); |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | CGF.Builder.CreateStore(Val: CGF.Builder.getInt1(V: kind == ForActivation), Addr: var); |
| 1282 | } |
| 1283 | |
| 1284 | /// Activate a cleanup that was created in an inactivated state. |
| 1285 | void CodeGenFunction::ActivateCleanupBlock(EHScopeStack::stable_iterator C, |
| 1286 | llvm::Instruction *dominatingIP) { |
| 1287 | assert(C != EHStack.stable_end() && "activating bottom of stack?" ); |
| 1288 | EHCleanupScope &Scope = cast<EHCleanupScope>(Val&: *EHStack.find(sp: C)); |
| 1289 | assert(!Scope.isActive() && "double activation" ); |
| 1290 | |
| 1291 | SetupCleanupBlockActivation(CGF&: *this, C, kind: ForActivation, dominatingIP); |
| 1292 | |
| 1293 | Scope.setActive(true); |
| 1294 | } |
| 1295 | |
| 1296 | /// Deactive a cleanup that was created in an active state. |
| 1297 | void CodeGenFunction::DeactivateCleanupBlock(EHScopeStack::stable_iterator C, |
| 1298 | llvm::Instruction *dominatingIP) { |
| 1299 | assert(C != EHStack.stable_end() && "deactivating bottom of stack?" ); |
| 1300 | EHCleanupScope &Scope = cast<EHCleanupScope>(Val&: *EHStack.find(sp: C)); |
| 1301 | assert(Scope.isActive() && "double deactivation" ); |
| 1302 | |
| 1303 | // If it's the top of the stack, just pop it, but do so only if it belongs |
| 1304 | // to the current RunCleanupsScope. |
| 1305 | if (C == EHStack.stable_begin() && |
| 1306 | CurrentCleanupScopeDepth.strictlyEncloses(I: C)) { |
| 1307 | PopCleanupBlock(/*FallthroughIsBranchThrough=*/false, |
| 1308 | /*ForDeactivation=*/true); |
| 1309 | return; |
| 1310 | } |
| 1311 | |
| 1312 | // Otherwise, follow the general case. |
| 1313 | SetupCleanupBlockActivation(CGF&: *this, C, kind: ForDeactivation, dominatingIP); |
| 1314 | |
| 1315 | Scope.setActive(false); |
| 1316 | } |
| 1317 | |
| 1318 | RawAddress CodeGenFunction::getNormalCleanupDestSlot() { |
| 1319 | if (!NormalCleanupDest.isValid()) |
| 1320 | NormalCleanupDest = |
| 1321 | CreateDefaultAlignTempAlloca(Ty: Builder.getInt32Ty(), Name: "cleanup.dest.slot" ); |
| 1322 | return NormalCleanupDest; |
| 1323 | } |
| 1324 | |
| 1325 | /// Emits all the code to cause the given temporary to be cleaned up. |
| 1326 | void CodeGenFunction::EmitCXXTemporary(const CXXTemporary *Temporary, |
| 1327 | QualType TempType, |
| 1328 | Address Ptr) { |
| 1329 | pushDestroy(kind: NormalAndEHCleanup, addr: Ptr, type: TempType, destroyer: destroyCXXObject, |
| 1330 | /*useEHCleanup*/ useEHCleanupForArray: true); |
| 1331 | } |
| 1332 | |
| 1333 | // Need to set "funclet" in OperandBundle properly for noThrow |
| 1334 | // intrinsic (see CGCall.cpp) |
| 1335 | static void EmitSehScope(CodeGenFunction &CGF, |
| 1336 | llvm::FunctionCallee &SehCppScope) { |
| 1337 | llvm::BasicBlock *InvokeDest = CGF.getInvokeDest(); |
| 1338 | assert(CGF.Builder.GetInsertBlock() && InvokeDest); |
| 1339 | llvm::BasicBlock *Cont = CGF.createBasicBlock(name: "invoke.cont" ); |
| 1340 | SmallVector<llvm::OperandBundleDef, 1> BundleList = |
| 1341 | CGF.getBundlesForFunclet(Callee: SehCppScope.getCallee()); |
| 1342 | if (CGF.CurrentFuncletPad) |
| 1343 | BundleList.emplace_back(Args: "funclet" , Args&: CGF.CurrentFuncletPad); |
| 1344 | CGF.Builder.CreateInvoke(Callee: SehCppScope, NormalDest: Cont, UnwindDest: InvokeDest, Args: {}, OpBundles: BundleList); |
| 1345 | CGF.EmitBlock(BB: Cont); |
| 1346 | } |
| 1347 | |
| 1348 | // Invoke a llvm.seh.scope.begin at the beginning of a CPP scope for -EHa |
| 1349 | void CodeGenFunction::EmitSehCppScopeBegin() { |
| 1350 | assert(getLangOpts().EHAsynch); |
| 1351 | llvm::FunctionType *FTy = |
| 1352 | llvm::FunctionType::get(Result: CGM.VoidTy, /*isVarArg=*/false); |
| 1353 | llvm::FunctionCallee SehCppScope = |
| 1354 | CGM.CreateRuntimeFunction(Ty: FTy, Name: "llvm.seh.scope.begin" ); |
| 1355 | EmitSehScope(CGF&: *this, SehCppScope); |
| 1356 | } |
| 1357 | |
| 1358 | // Invoke a llvm.seh.scope.end at the end of a CPP scope for -EHa |
| 1359 | // llvm.seh.scope.end is emitted before popCleanup, so it's "invoked" |
| 1360 | void CodeGenFunction::EmitSehCppScopeEnd() { |
| 1361 | assert(getLangOpts().EHAsynch); |
| 1362 | llvm::FunctionType *FTy = |
| 1363 | llvm::FunctionType::get(Result: CGM.VoidTy, /*isVarArg=*/false); |
| 1364 | llvm::FunctionCallee SehCppScope = |
| 1365 | CGM.CreateRuntimeFunction(Ty: FTy, Name: "llvm.seh.scope.end" ); |
| 1366 | EmitSehScope(CGF&: *this, SehCppScope); |
| 1367 | } |
| 1368 | |
| 1369 | // Invoke a llvm.seh.try.begin at the beginning of a SEH scope for -EHa |
| 1370 | void CodeGenFunction::EmitSehTryScopeBegin() { |
| 1371 | assert(getLangOpts().EHAsynch); |
| 1372 | llvm::FunctionType *FTy = |
| 1373 | llvm::FunctionType::get(Result: CGM.VoidTy, /*isVarArg=*/false); |
| 1374 | llvm::FunctionCallee SehCppScope = |
| 1375 | CGM.CreateRuntimeFunction(Ty: FTy, Name: "llvm.seh.try.begin" ); |
| 1376 | EmitSehScope(CGF&: *this, SehCppScope); |
| 1377 | } |
| 1378 | |
| 1379 | // Invoke a llvm.seh.try.end at the end of a SEH scope for -EHa |
| 1380 | void CodeGenFunction::EmitSehTryScopeEnd() { |
| 1381 | assert(getLangOpts().EHAsynch); |
| 1382 | llvm::FunctionType *FTy = |
| 1383 | llvm::FunctionType::get(Result: CGM.VoidTy, /*isVarArg=*/false); |
| 1384 | llvm::FunctionCallee SehCppScope = |
| 1385 | CGM.CreateRuntimeFunction(Ty: FTy, Name: "llvm.seh.try.end" ); |
| 1386 | EmitSehScope(CGF&: *this, SehCppScope); |
| 1387 | } |
| 1388 | |