23 using namespace clang;
24 using namespace CodeGen;
41 return saved_type(V, ScalarLiteral);
45 CGF.
Builder.CreateStore(V, addr);
46 return saved_type(addr, ScalarAddress);
51 llvm::Type *ComplexTy =
52 llvm::StructType::get(V.first->getType(), V.second->getType(),
55 CGF.
Builder.CreateStore(V.first,
56 CGF.
Builder.CreateStructGEP(ComplexTy, addr, 0));
57 CGF.
Builder.CreateStore(V.second,
58 CGF.
Builder.CreateStructGEP(ComplexTy, addr, 1));
59 return saved_type(addr, ComplexAddress);
65 return saved_type(V, AggregateLiteral);
68 CGF.
Builder.CreateStore(V, addr);
69 return saved_type(addr, AggregateAddress);
81 case AggregateLiteral:
83 case AggregateAddress:
85 case ComplexAddress: {
94 llvm_unreachable(
"bad saved r-value kind");
98 char *EHScopeStack::allocate(
size_t Size) {
100 unsigned Capacity = 1024;
101 while (Capacity < Size) Capacity *= 2;
102 StartOfBuffer =
new char[Capacity];
103 StartOfData = EndOfBuffer = StartOfBuffer + Capacity;
104 }
else if (static_cast<size_t>(StartOfData - StartOfBuffer) < Size) {
105 unsigned CurrentCapacity = EndOfBuffer - StartOfBuffer;
106 unsigned UsedCapacity = CurrentCapacity - (StartOfData - StartOfBuffer);
108 unsigned NewCapacity = CurrentCapacity;
111 }
while (NewCapacity < UsedCapacity + Size);
113 char *NewStartOfBuffer =
new char[NewCapacity];
114 char *NewEndOfBuffer = NewStartOfBuffer + NewCapacity;
115 char *NewStartOfData = NewEndOfBuffer - UsedCapacity;
116 memcpy(NewStartOfData, StartOfData, UsedCapacity);
117 delete [] StartOfBuffer;
118 StartOfBuffer = NewStartOfBuffer;
119 EndOfBuffer = NewEndOfBuffer;
120 StartOfData = NewStartOfData;
123 assert(StartOfBuffer + Size <= StartOfData);
155 if (cleanup && !cleanup->
isActive()) {
169 assert(((Size %
sizeof(
void*)) == 0) &&
"cleanup type is misaligned");
180 InnermostNormalCleanup,
191 assert(!
empty() &&
"popping exception stack when not empty");
193 assert(isa<EHCleanupScope>(*
begin()));
203 if (!BranchFixups.empty()) {
207 BranchFixups.clear();
224 assert(!
empty() &&
"popping exception stack when not empty");
235 new (buffer)
EHCatchScope(numHandlers, InnermostEHScope);
257 unsigned MinSize = cast<EHCleanupScope>(*it).getFixupDepth();
258 assert(BranchFixups.size() >= MinSize &&
"fixup stack out of order");
260 while (BranchFixups.size() > MinSize &&
261 BranchFixups.back().Destination ==
nullptr)
262 BranchFixups.pop_back();
267 llvm::AllocaInst *active
279 assert(!cleanup.
getActiveFlag() &&
"cleanup already has active flag?");
286 void EHScopeStack::Cleanup::anchor() {}
292 llvm::SwitchInst *Switch,
293 llvm::BasicBlock *CleanupEntry) {
294 llvm::SmallPtrSet<llvm::BasicBlock*, 4> CasesAdded;
330 llvm::BasicBlock *Block) {
333 llvm::TerminatorInst *Term = Block->getTerminator();
334 assert(Term &&
"can't transition block without terminator");
336 if (llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Term)) {
337 assert(Br->isUnconditional());
338 llvm::LoadInst *Load =
340 llvm::SwitchInst *Switch =
342 Br->eraseFromParent();
345 return cast<llvm::SwitchInst>(Term);
350 assert(Block &&
"resolving a null target block");
351 if (!EHStack.getNumBranchFixups())
return;
353 assert(EHStack.hasNormalCleanups() &&
354 "branch fixups exist with no normal cleanups on stack");
356 llvm::SmallPtrSet<llvm::BasicBlock*, 4> ModifiedOptimisticBlocks;
357 bool ResolvedAny =
false;
359 for (
unsigned I = 0, E = EHStack.getNumBranchFixups(); I != E; ++I) {
374 if (!ModifiedOptimisticBlocks.insert(BranchBB).second)
384 EHStack.popNullFixups();
391 while (EHStack.stable_begin() != Old) {
397 bool FallThroughIsBranchThrough =
400 PopCleanupBlock(FallThroughIsBranchThrough);
408 size_t OldLifetimeExtendedSize) {
409 PopCleanupBlocks(Old);
412 for (
size_t I = OldLifetimeExtendedSize,
413 E = LifetimeExtendedCleanupStack.size(); I != E; ) {
415 assert((I % llvm::alignOf<LifetimeExtendedCleanupHeader>() == 0) &&
416 "misaligned cleanup stack entry");
420 LifetimeExtendedCleanupStack[I]);
423 EHStack.pushCopyOfCleanup(Header.
getKind(),
424 &LifetimeExtendedCleanupStack[I],
428 LifetimeExtendedCleanupStack.resize(OldLifetimeExtendedSize);
448 llvm::BasicBlock *Entry) {
449 llvm::BasicBlock *Pred = Entry->getSinglePredecessor();
450 if (!Pred)
return Entry;
452 llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Pred->getTerminator());
453 if (!Br || Br->isConditional())
return Entry;
454 assert(Br->getSuccessor(0) == Entry);
459 bool WasInsertBlock = CGF.
Builder.GetInsertBlock() == Entry;
460 assert(!WasInsertBlock || CGF.
Builder.GetInsertPoint() == Entry->end());
463 Br->eraseFromParent();
467 Entry->replaceAllUsesWith(Pred);
470 Pred->getInstList().splice(Pred->end(), Entry->getInstList());
473 Entry->eraseFromParent();
476 CGF.
Builder.SetInsertPoint(Pred);
488 bool PushedTerminate =
false;
491 PushedTerminate =
true;
496 llvm::BasicBlock *ContBB =
nullptr;
501 = CGF.
Builder.CreateLoad(ActiveFlag,
"cleanup.is_active");
502 CGF.
Builder.CreateCondBr(IsActive, CleanupBB, ContBB);
507 Fn->
Emit(CGF, flags);
508 assert(CGF.
HaveInsertPoint() &&
"cleanup ended with no insertion point?");
520 llvm::BasicBlock *From,
521 llvm::BasicBlock *To) {
524 llvm::TerminatorInst *Term = Exit->getTerminator();
526 if (llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Term)) {
527 assert(Br->isUnconditional() && Br->getSuccessor(0) == From);
528 Br->setSuccessor(0, To);
530 llvm::SwitchInst *Switch = cast<llvm::SwitchInst>(Term);
531 for (
unsigned I = 0, E = Switch->getNumSuccessors(); I != E; ++I)
532 if (Switch->getSuccessor(I) == From)
533 Switch->setSuccessor(I, To);
550 for (llvm::BasicBlock::use_iterator
551 i = entry->use_begin(), e = entry->use_end(); i != e; ) {
555 use.set(unreachableBB);
558 llvm::SwitchInst *si = cast<llvm::SwitchInst>(use.getUser());
559 if (si->getNumCases() == 1 && si->getDefaultDest() == unreachableBB) {
564 llvm::LoadInst *condition = cast<llvm::LoadInst>(si->getCondition());
567 si->eraseFromParent();
571 assert(condition->use_empty());
572 condition->eraseFromParent();
576 assert(entry->use_empty());
584 assert(!EHStack.empty() &&
"cleanup stack is empty!");
585 assert(isa<EHCleanupScope>(*EHStack.begin()) &&
"top not a cleanup!");
587 assert(Scope.
getFixupDepth() <= EHStack.getNumBranchFixups());
600 bool RequiresEHCleanup = (EHEntry !=
nullptr);
607 bool HasFixups = EHStack.getNumBranchFixups() != FixupDepth;
613 llvm::BasicBlock *FallthroughSource =
Builder.GetInsertBlock();
614 bool HasFallthrough = (FallthroughSource !=
nullptr && IsActive);
620 bool HasPrebranchedFallthrough =
621 (FallthroughSource && FallthroughSource->getTerminator());
628 FallthroughSource->getTerminator()->getSuccessor(0)
631 bool RequiresNormalCleanup =
false;
633 (HasFixups || HasExistingBranches || HasFallthrough)) {
634 RequiresNormalCleanup =
true;
639 if (Scope.
isNormalCleanup() && HasPrebranchedFallthrough && !IsActive) {
640 llvm::BasicBlock *prebranchDest;
645 if (FallthroughIsBranchThrough) {
654 prebranchDest = createBasicBlock(
"forwarded-prebranch");
655 EmitBlock(prebranchDest);
659 assert(normalEntry && !normalEntry->use_empty());
662 normalEntry, prebranchDest);
666 if (!RequiresNormalCleanup && !RequiresEHCleanup) {
668 EHStack.popCleanup();
669 assert(EHStack.getNumBranchFixups() == 0 ||
670 EHStack.hasNormalCleanups());
679 memcpy(CleanupBuffer.data(),
691 if (!RequiresNormalCleanup) {
693 EHStack.popCleanup();
697 if (HasFallthrough && !HasPrebranchedFallthrough &&
698 !HasFixups && !HasExistingBranches) {
701 EHStack.popCleanup();
703 EmitCleanup(*
this, Fn, cleanupFlags, NormalActiveFlag);
713 CGBuilderTy::InsertPoint savedInactiveFallthroughIP;
717 if (HasFallthrough) {
718 if (!HasPrebranchedFallthrough)
719 Builder.CreateStore(
Builder.getInt32(0), getNormalCleanupDestSlot());
723 }
else if (FallthroughSource) {
724 assert(!IsActive &&
"source without fallthrough for active cleanup");
725 savedInactiveFallthroughIP =
Builder.saveAndClearIP();
731 EmitBlock(NormalEntry);
736 bool HasEnclosingCleanups =
744 llvm::BasicBlock *BranchThroughDest =
nullptr;
746 (FallthroughSource && FallthroughIsBranchThrough) ||
747 (HasFixups && HasEnclosingCleanups)) {
748 assert(HasEnclosingCleanups);
753 llvm::BasicBlock *FallthroughDest =
nullptr;
760 assert(!BranchThroughDest || !IsActive);
763 llvm::Instruction *NormalCleanupDestSlot =
764 cast<llvm::Instruction>(getNormalCleanupDestSlot());
765 if (NormalCleanupDestSlot->hasOneUse()) {
766 NormalCleanupDestSlot->user_back()->eraseFromParent();
767 NormalCleanupDestSlot->eraseFromParent();
768 NormalCleanupDest =
nullptr;
780 (HasFallthrough && !FallthroughIsBranchThrough) ||
781 (HasFixups && !HasEnclosingCleanups)) {
783 llvm::BasicBlock *Default =
784 (BranchThroughDest ? BranchThroughDest : getUnreachableBlock());
787 const unsigned SwitchCapacity = 10;
789 llvm::LoadInst *Load =
790 new llvm::LoadInst(getNormalCleanupDestSlot(),
"cleanup.dest");
791 llvm::SwitchInst *Switch =
794 InstsToAppend.push_back(Load);
795 InstsToAppend.push_back(Switch);
798 if (FallthroughSource && !FallthroughIsBranchThrough) {
799 FallthroughDest = createBasicBlock(
"cleanup.cont");
801 Switch->addCase(
Builder.getInt32(0), FallthroughDest);
811 if (HasFixups && !HasEnclosingCleanups)
815 assert(BranchThroughDest);
820 EHStack.popCleanup();
821 assert(EHStack.hasNormalCleanups() == HasEnclosingCleanups);
823 EmitCleanup(*
this, Fn, cleanupFlags, NormalActiveFlag);
826 llvm::BasicBlock *NormalExit =
Builder.GetInsertBlock();
827 for (
unsigned I = 0, E = InstsToAppend.size(); I != E; ++I)
828 NormalExit->getInstList().push_back(InstsToAppend[I]);
831 for (
unsigned I = FixupDepth, E = EHStack.getNumBranchFixups();
837 getNormalCleanupDestSlot(),
848 if (!HasFallthrough && FallthroughSource) {
853 Builder.restoreIP(savedInactiveFallthroughIP);
858 }
else if (HasFallthrough && FallthroughDest) {
859 assert(!FallthroughIsBranchThrough);
860 EmitBlock(FallthroughDest);
864 }
else if (HasFallthrough) {
876 llvm::BasicBlock *NewNormalEntry =
881 if (NewNormalEntry != NormalEntry && NormalEntry == NormalExit)
882 for (
unsigned I = FixupDepth, E = EHStack.getNumBranchFixups();
884 EHStack.getBranchFixup(I).OptimisticBranchBlock = NewNormalEntry;
888 assert(EHStack.hasNormalCleanups() || EHStack.getNumBranchFixups() == 0);
891 if (RequiresEHCleanup) {
892 CGBuilderTy::InsertPoint SavedIP =
Builder.saveAndClearIP();
898 if (EHActiveFlag || IsActive) {
901 EmitCleanup(*
this, Fn, cleanupFlags, EHActiveFlag);
904 Builder.CreateBr(getEHDispatchBlock(EHParent));
917 &&
"stale jump destination");
921 EHStack.getInnermostActiveNormalCleanup();
926 if (TopCleanup == EHStack.stable_end() ||
942 &&
"stale jump destination");
944 if (!HaveInsertPoint())
952 TopCleanup = EHStack.getInnermostActiveNormalCleanup();
957 if (TopCleanup == EHStack.stable_end() ||
980 new llvm::StoreInst(Index, getNormalCleanupDestSlot(), BI);
985 cast<EHCleanupScope>(*EHStack.find(TopCleanup));
1013 Builder.ClearInsertionPoint();
1019 if (cast<EHCleanupScope>(*EHStack.
find(C)).getNormalBlock())
1028 if (S.getNormalBlock())
return true;
1029 I = S.getEnclosingNormalCleanup();
1047 if (scope.hasEHBranches())
1050 i = scope.getEnclosingEHScope();
1069 llvm::Instruction *dominatingIP) {
1075 bool isActivatedInConditional =
1078 bool needFlag =
false;
1097 if (!needFlag)
return;
1104 assert(dominatingIP &&
"no existing variable and no dominating IP!");
1115 new llvm::StoreInst(value, var, dominatingIP);
1124 llvm::Instruction *dominatingIP) {
1125 assert(C != EHStack.stable_end() &&
"activating bottom of stack?");
1127 assert(!Scope.isActive() &&
"double activation");
1131 Scope.setActive(
true);
1136 llvm::Instruction *dominatingIP) {
1137 assert(C != EHStack.stable_end() &&
"deactivating bottom of stack?");
1139 assert(Scope.isActive() &&
"double deactivation");
1142 if (C == EHStack.stable_begin()) {
1145 CGBuilderTy::InsertPoint SavedIP =
Builder.saveAndClearIP();
1154 Scope.setActive(
false);
1158 if (!NormalCleanupDest)
1160 CreateTempAlloca(
Builder.getInt32Ty(),
"cleanup.dest.slot");
1161 return NormalCleanupDest;
void pushTerminate()
Push a terminate handler on the stack.
static llvm::BasicBlock * CreateNormalEntry(CodeGenFunction &CGF, EHCleanupScope &Scope)
void ActivateCleanupBlock(EHScopeStack::stable_iterator Cleanup, llvm::Instruction *DominatingIP)
Activate a cleanup that was created in an inactivated state.
void setIsEHCleanupKind()
static void destroyOptimisticNormalEntry(CodeGenFunction &CGF, EHCleanupScope &scope)
bool isInConditionalBranch() const
const TargetInfo & getTarget() const
std::pair< llvm::Value *, llvm::Value * > getComplexVal() const
static stable_iterator stable_end()
Create a stable reference to the bottom of the EH stack.
llvm::AllocaInst * CreateTempAlloca(llvm::Type *Ty, const Twine &Name="tmp")
static llvm::SwitchInst * TransitionToCleanupSwitch(CodeGenFunction &CGF, llvm::BasicBlock *Block)
static bool needsSaving(llvm::Value *value)
Answer whether the given value needs extra work to be saved.
stable_iterator getInnermostActiveEHScope() const
EHScopeStack::stable_iterator getEnclosingNormalCleanup() const
A protected scope for zero-cost EH handling.
llvm::BasicBlock * getCachedEHDispatchBlock() const
llvm::Value * getNormalCleanupDestSlot()
bool addBranchThrough(llvm::BasicBlock *Block)
static size_t getSizeForCleanupSize(size_t Size)
stable_iterator stabilize(iterator it) const
Translates an iterator into a stable_iterator.
bool hasEHBranches() const
llvm::AllocaInst * getActiveFlag() const
class EHCatchScope * pushCatch(unsigned NumHandlers)
bool isLifetimeMarker() const
iterator begin() const
Returns an iterator pointing to the innermost EH scope.
BranchFixup & getBranchFixup(unsigned I)
void setNormalBlock(llvm::BasicBlock *BB)
llvm::Value * getAggregateAddr() const
getAggregateAddr() - Return the Value* of the address of the aggregate.
static OMPLinearClause * Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef< Expr * > VL, ArrayRef< Expr * > IL, Expr *Step, Expr *CalcStep)
Creates clause with a list of variables VL and a linear step Step.
bool isObviouslyBranchWithoutCleanups(JumpDest Dest) const
void initFullExprCleanup()
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
size_t getCleanupSize() const
void popFilter()
Pops an exceptions filter off the stack.
static bool IsUsedAsEHCleanup(EHScopeStack &EHStack, EHScopeStack::stable_iterator cleanup)
static size_t getSizeForNumHandlers(unsigned N)
iterator find(stable_iterator save) const
std::pair< llvm::Value *, llvm::Value * > ComplexPairTy
unsigned getNumBranchFixups() const
static void ResolveAllBranchFixups(CodeGenFunction &CGF, llvm::SwitchInst *Switch, llvm::BasicBlock *CleanupEntry)
llvm::BranchInst * InitialBranch
The initial branch of the fixup.
bool empty() const
Determines whether the exception-scopes stack is empty.
bool isMicrosoft() const
Is this ABI an MSVC-compatible ABI?
void * getCleanupBuffer()
llvm::AllocaInst * NormalCleanupDest
i32s containing the indexes of the cleanup destinations.
llvm::BasicBlock * getBranchAfterBlock(unsigned I) const
llvm::BasicBlock * getBlock() const
EHScopeStack::stable_iterator getScopeDepth() const
EHScopeStack::stable_iterator getEnclosingEHScope() const
bool hasBranchThroughs() const
Determines if this cleanup scope has any branch throughs.
stable_iterator stable_begin() const
size_t getAllocatedSize() const
bool isNormalCleanup() const
bool containsOnlyLifetimeMarkers(stable_iterator Old) const
void ResolveBranchFixups(llvm::BasicBlock *Target)
bool shouldTestFlagInNormalCleanup() const
unsigned getDestIndex() const
void setBeforeOutermostConditional(llvm::Value *value, llvm::Value *addr)
void setTestFlagInNormalCleanup()
llvm::BasicBlock * OptimisticBranchBlock
void popCleanup()
Pops a cleanup scope off the stack. This is private to CGCleanup.cpp.
bool HaveInsertPoint() const
llvm::BasicBlock * Destination
Represents a C++ temporary.
llvm::BasicBlock * getUnreachableBlock()
void DeactivateCleanupBlock(EHScopeStack::stable_iterator Cleanup, llvm::Instruction *DominatingIP)
Deactive a cleanup that was created in an active state.
llvm::BasicBlock * getNormalBlock() const
static RValue getAggregate(llvm::Value *V, bool Volatile=false)
unsigned DestinationIndex
The destination index value.
void setIsNormalCleanupKind()
virtual void Emit(CodeGenFunction &CGF, Flags flags)=0
static size_t getSizeForNumFilters(unsigned numFilters)
static bool IsUsedAsNormalCleanup(EHScopeStack &EHStack, EHScopeStack::stable_iterator C)
static void ForwardPrebranchedFallthrough(llvm::BasicBlock *Exit, llvm::BasicBlock *From, llvm::BasicBlock *To)
class EHFilterScope * pushFilter(unsigned NumFilters)
Push an exceptions filter on the stack.
bool encloses(stable_iterator I) const
static RValue getComplex(llvm::Value *V1, llvm::Value *V2)
llvm::Value * getScalarVal() const
getScalarVal() - Return the Value* of this scalar value.
bool hasBranches() const
True if this cleanup scope has any branch-afters or branch-throughs.
void addBranchAfter(llvm::ConstantInt *Index, llvm::BasicBlock *Block)
static llvm::BasicBlock * SimplifyCleanupEntry(CodeGenFunction &CGF, llvm::BasicBlock *Entry)
static void EmitCleanup(CodeGenFunction &CGF, EHScopeStack::Cleanup *Fn, EHScopeStack::Cleanup::Flags flags, llvm::Value *ActiveFlag)
unsigned getFixupDepth() const
static void SetupCleanupBlockActivation(CodeGenFunction &CGF, EHScopeStack::stable_iterator C, ForActivation_t kind, llvm::Instruction *dominatingIP)
void setActiveFlag(llvm::AllocaInst *Var)
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
void EmitCXXTemporary(const CXXTemporary *Temporary, QualType TempType, llvm::Value *Ptr)
Emits all the code to cause the given temporary to be cleaned up.
BoundNodesTreeBuilder *const Builder
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
stable_iterator getInnermostActiveNormalCleanup() const
unsigned getNumBranchAfters() const
Return the number of unique branch-afters on this scope.
void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize)
Takes the old cleanup stack size and emits the cleanup blocks that have been added.
void popTerminate()
Pops a terminate handler off the stack.
unsigned kind
All of the diagnostics that can be emitted by the frontend.
A cleanup scope which generates the cleanup blocks lazily.
bool hasNormalCleanups() const
Determines whether there are any normal cleanups on the stack.
llvm::ConstantInt * getBranchAfterIndex(unsigned I) const
unsigned getNumFilters() const
stable_iterator getInnermostEHScope() const
bool strictlyEncloses(stable_iterator I) const
stable_iterator getInnermostNormalCleanup() const
static RValue get(llvm::Value *V)
void EmitBranchThroughCleanup(JumpDest Dest)
bool isForEHCleanup() const
isForEH - true if the current emission is for an EH cleanup.
A non-stable pointer into the scope stack.
void setTestFlagInEHCleanup()
void PopCleanupBlock(bool FallThroughIsBranchThrough=false)
bool shouldTestFlagInEHCleanup() const