Index: include/llvm/Transforms/Utils/Cloning.h =================================================================== --- include/llvm/Transforms/Utils/Cloning.h +++ include/llvm/Transforms/Utils/Cloning.h @@ -144,7 +144,9 @@ ///< Skip this instruction but continue cloning the current basic block. SkipInstruction, ///< Skip this instruction and stop cloning the current basic block. - StopCloningBB + StopCloningBB, + ///< Don't clone the terminator but clone the current block's successors. + CloneSuccessors }; virtual ~CloningDirector() {} Index: lib/CodeGen/WinEHPrepare.cpp =================================================================== --- lib/CodeGen/WinEHPrepare.cpp +++ lib/CodeGen/WinEHPrepare.cpp @@ -16,6 +16,8 @@ #include "llvm/CodeGen/Passes.h" #include "llvm/ADT/MapVector.h" +#include "llvm/ADT/SmallSet.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/TinyPtrVector.h" #include "llvm/Analysis/LibCallSemantics.h" #include "llvm/IR/Function.h" @@ -25,6 +27,8 @@ #include "llvm/IR/Module.h" #include "llvm/IR/PatternMatch.h" #include "llvm/Pass.h" +#include "llvm/Support/Debug.h" +#include "llvm/Transforms/Utils/BasicBlockUtils.h" #include "llvm/Transforms/Utils/Cloning.h" #include "llvm/Transforms/Utils/Local.h" #include @@ -48,15 +52,29 @@ // frame allocation structure. typedef MapVector FrameVarInfoMap; +typedef SmallSet VisitedBlockSet; + +enum ActionType { Catch, Cleanup }; + +class LandingPadActions; +class ActionHandler; +class CatchHandler; +class CleanupHandler; + +typedef DenseMap CatchHandlerMapTy; +typedef DenseMap CleanupHandlerMapTy; + class WinEHPrepare : public FunctionPass { std::unique_ptr DwarfPrepare; - enum HandlerType { Catch, Cleanup }; - public: static char ID; // Pass identification, replacement for typeid. WinEHPrepare(const TargetMachine *TM = nullptr) : FunctionPass(ID), DwarfPrepare(createDwarfEHPass(TM)) {} + ~WinEHPrepare() { + DeleteContainerSeconds(CatchHandlerMap); + DeleteContainerSeconds(CleanupHandlerMap); + } bool runOnFunction(Function &Fn) override; @@ -71,9 +89,17 @@ private: bool prepareCPPEHHandlers(Function &F, SmallVectorImpl &LPads); - bool outlineHandler(HandlerType CatchOrCleanup, Function *SrcFn, - Constant *SelectorType, LandingPadInst *LPad, + bool outlineHandler(ActionHandler *Action, Function *SrcFn, + LandingPadInst *LPad, BasicBlock *StartBB, CallInst *&EHAlloc, FrameVarInfoMap &VarInfo); + + void mapLandingPadBlocks(LandingPadInst *LPad, LandingPadActions &Actions); + CatchHandler *findCatchHandler(BasicBlock *BB, BasicBlock *&NextBB, + VisitedBlockSet &VisitedBlocks); + CleanupHandler *findCleanupHandler(BasicBlock *StartBB, BasicBlock *EndBB); + + CatchHandlerMapTy CatchHandlerMap; + CleanupHandlerMapTy CleanupHandlerMap; }; class WinEHFrameVariableMaterializer : public ValueMaterializer { @@ -89,6 +115,27 @@ IRBuilder<> Builder; }; +class LandingPadMapper { +public: + LandingPadMapper() : OriginLPad(nullptr) {} + void mapLandingPad(const LandingPadInst *LPad); + + bool isLandingPadSpecificInst(const Instruction *Inst) const; + bool isEHPtrLoad(const LoadInst *Load); + bool isSelectorLoad(const LoadInst *Load); + + void remapSelector(ValueToValueMapTy &VMap, Value *MappedValue) const; + +private: + const LandingPadInst *OriginLPad; + TinyPtrVector ExtractedEHPtrs; + TinyPtrVector ExtractedSelectors; + TinyPtrVector EHPtrStores; + TinyPtrVector SelectorStores; + TinyPtrVector EHPtrStoreAddrs; + TinyPtrVector SelectorStoreAddrs; +}; + class WinEHCloningDirectorBase : public CloningDirector { public: WinEHCloningDirectorBase(LandingPadInst *LPI, Function *HandlerFn, @@ -110,22 +157,23 @@ virtual CloningAction handleTypeIdFor(ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) = 0; + virtual CloningAction handleInvoke(ValueToValueMapTy &VMap, + const InvokeInst *Invoke, + BasicBlock *NewBB) = 0; virtual CloningAction handleResume(ValueToValueMapTy &VMap, const ResumeInst *Resume, BasicBlock *NewBB) = 0; ValueMaterializer *getValueMaterializer() override { return &Materializer; } + void mapLandingPadUsers(LandingPadInst *LPad, ValueToValueMapTy &VMap); + protected: LandingPadInst *LPI; WinEHFrameVariableMaterializer Materializer; Type *SelectorIDType; Type *Int8PtrType; - - const Value *ExtractedEHPtr; - const Value *ExtractedSelector; - const Value *EHPtrStoreAddr; - const Value *SelectorStoreAddr; + LandingPadMapper LPadMapper; }; class WinEHCatchDirector : public WinEHCloningDirectorBase { @@ -133,7 +181,8 @@ WinEHCatchDirector(LandingPadInst *LPI, Function *CatchFn, Value *Selector, FrameVarInfoMap &VarInfo) : WinEHCloningDirectorBase(LPI, CatchFn, VarInfo), - CurrentSelector(Selector->stripPointerCasts()) {} + CurrentSelector(Selector->stripPointerCasts()), + ExceptionObjectVar(nullptr) {} CloningAction handleBeginCatch(ValueToValueMapTy &VMap, const Instruction *Inst, @@ -143,11 +192,19 @@ CloningAction handleTypeIdFor(ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) override; + CloningAction handleInvoke(ValueToValueMapTy &VMap, const InvokeInst *Invoke, + BasicBlock *NewBB) override; CloningAction handleResume(ValueToValueMapTy &VMap, const ResumeInst *Resume, BasicBlock *NewBB) override; + const Value *getExceptionVar() { return ExceptionObjectVar; } + TinyPtrVector &getReturnTargets() { return ReturnTargets; } + private: Value *CurrentSelector; + + const Value *ExceptionObjectVar; + TinyPtrVector ReturnTargets; }; class WinEHCleanupDirector : public WinEHCloningDirectorBase { @@ -164,10 +221,93 @@ CloningAction handleTypeIdFor(ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) override; + CloningAction handleInvoke(ValueToValueMapTy &VMap, const InvokeInst *Invoke, + BasicBlock *NewBB) override; CloningAction handleResume(ValueToValueMapTy &VMap, const ResumeInst *Resume, BasicBlock *NewBB) override; }; +class ActionHandler { +public: + ActionHandler(BasicBlock *BB, ActionType Type) + : StartBB(BB), Type(Type), OutlinedFn(nullptr) {} + + ActionType getType() const { return Type; } + BasicBlock *getStartBlock() const { return StartBB; } + + bool hasBeenOutlined() { return OutlinedFn != nullptr; } + + void setOutlinedFunction(Function *F) { OutlinedFn = F; } + Function *getOutlinedFunction() { return OutlinedFn; } + +private: + BasicBlock *StartBB; + ActionType Type; + Function *OutlinedFn; +}; + +class CatchHandler : public ActionHandler { +public: + CatchHandler(BasicBlock *BB, Constant *Selector, BasicBlock *NextBB) + : Selector(Selector), NextBB(NextBB), ExceptionObjectVar(nullptr), + ActionHandler(BB, ActionType::Catch) {} + + // Method for support type inquiry through isa, cast, and dyn_cast: + static inline bool classof(const ActionHandler *H) { + return H->getType() == ActionType::Catch; + } + + Constant *getSelector() const { return Selector; } + BasicBlock *getNextBB() const { return NextBB; } + + const Value *getExceptionVar() { return ExceptionObjectVar; } + TinyPtrVector &getReturnTargets() { return ReturnTargets; } + + void setExceptionVar(const Value *Val) { ExceptionObjectVar = Val; } + void setReturnTargets(TinyPtrVector &Targets) { + ReturnTargets = Targets; + } + +private: + Constant *Selector; + BasicBlock *NextBB; + const Value *ExceptionObjectVar; + TinyPtrVector ReturnTargets; +}; + +class CleanupHandler : public ActionHandler { +public: + CleanupHandler(BasicBlock *BB) : ActionHandler(BB, ActionType::Cleanup) {} + + // Method for support type inquiry through isa, cast, and dyn_cast: + static inline bool classof(const ActionHandler *H) { + return H->getType() == ActionType::Cleanup; + } +}; + +class LandingPadActions { +public: + LandingPadActions() : HasCleanupHandlers(false) {} + + void insertCatchHandler(CatchHandler *Action) { Actions.push_back(Action); } + void insertCleanupHandler(CleanupHandler *Action) { + Actions.push_back(Action); + HasCleanupHandlers = true; + } + + bool includesCleanup() const { return HasCleanupHandlers; } + + SmallVectorImpl::iterator begin() { return Actions.begin(); } + SmallVectorImpl::iterator end() { return Actions.end(); } + +private: + // Note that this class does not own the ActionHandler objects in this vector. + // The ActionHandlers are owned by the CatchHandlerMap and CleanupHandlerMap + // in the WinEHPrepare class. + SmallVector Actions; + bool HasCleanupHandlers; +}; + } // end anonymous namespace char WinEHPrepare::ID = 0; @@ -240,17 +380,33 @@ bool HandlersOutlined = false; + Module *M = F.getParent(); + LLVMContext &Context = M->getContext(); + + // FIXME: Make this an intrinsic. + // Create a new function to receive the handler contents. + PointerType *Int8PtrType = Type::getInt8PtrTy(Context); + FunctionType *ActionTy = FunctionType::get(Int8PtrType, true); + Value *ActionIntrin = M->getOrInsertFunction("llvm.eh.actions", ActionTy); + for (LandingPadInst *LPad : LPads) { // Look for evidence that this landingpad has already been processed. bool LPadHasActionList = false; BasicBlock *LPadBB = LPad->getParent(); for (Instruction &Inst : LPadBB->getInstList()) { // FIXME: Make this an intrinsic. - if (auto *Call = dyn_cast(&Inst)) + if (auto *Call = dyn_cast(&Inst)) { if (Call->getCalledFunction()->getName() == "llvm.eh.actions") { LPadHasActionList = true; break; } + } + // FIXME: This is here to help with the development of nested landing pad + // outlining. It should be removed when that is finished. + if (isa(Inst)) { + LPadHasActionList = true; + break; + } } // If we've already outlined the handlers for this landingpad, @@ -258,40 +414,71 @@ if (LPadHasActionList) continue; - for (unsigned Idx = 0, NumClauses = LPad->getNumClauses(); Idx < NumClauses; - ++Idx) { - if (LPad->isCatch(Idx)) { - // Create a new instance of the handler data structure in the - // HandlerData vector. - CallInst *EHAlloc = nullptr; - bool Outlined = outlineHandler(Catch, &F, LPad->getClause(Idx), LPad, - EHAlloc, FrameVarInfo); - if (Outlined) { - HandlersOutlined = true; - // These values must be resolved after all handlers have been - // outlined. - if (EHAlloc) - HandlerAllocs.push_back(EHAlloc); - } - } // End if (isCatch) - } // End for each clause - - // FIXME: This only handles the simple case where there is a 1:1 - // correspondence between landing pad and cleanup blocks. - // It does not handle cases where there are catch blocks between - // cleanup blocks or the case where a cleanup block is shared by - // multiple landing pads. Those cases will be supported later - // when landing pad block analysis is added. - if (LPad->isCleanup()) { + LandingPadActions Actions; + mapLandingPadBlocks(LPad, Actions); + + for (ActionHandler *Action : Actions) { + if (Action->hasBeenOutlined()) + continue; + BasicBlock *StartBB = Action->getStartBlock(); CallInst *EHAlloc = nullptr; - bool Outlined = - outlineHandler(Cleanup, &F, nullptr, LPad, EHAlloc, FrameVarInfo); - if (Outlined) { + if (outlineHandler(Action, &F, LPad, StartBB, EHAlloc, FrameVarInfo)) { HandlersOutlined = true; - // This value must be resolved after all handlers have been outlined. + // These values must be resolved after all handlers have been + // outlined. if (EHAlloc) HandlerAllocs.push_back(EHAlloc); } + } // End for each Action + + // FIXME: We need a guard against partially outlined functions. + if (HandlersOutlined) { + // Insert a call to eh_actions + std::vector ActionArgs; + ActionArgs.push_back(LPad); + // FIXME: When the form of llvm.eh.actions is finalized this will need + // to be updated. + for (ActionHandler *Action : Actions) { + // This is a placeholder for the EH context value of the action. + // FIXME: Add code somewhere to compute this value. + ActionArgs.push_back(ConstantInt::get(Type::getInt32Ty(Context), 0)); + CastInst *HandlerPtr = CastInst::CreatePointerCast( + Action->getOutlinedFunction(), Int8PtrType, "handler", LPadBB); + ActionArgs.push_back(HandlerPtr); + if (auto *CatchAction = dyn_cast(Action)) { + Value *EHObj = const_cast(CatchAction->getExceptionVar()); + if (EHObj) + ActionArgs.push_back(EHObj); + else + ActionArgs.push_back(ConstantPointerNull::get(Int8PtrType)); + + ActionArgs.push_back(CatchAction->getSelector()); + } else { + ActionArgs.push_back(ConstantPointerNull::get(Int8PtrType)); + ActionArgs.push_back(ConstantPointerNull::get(Int8PtrType)); + } + } + CallInst *Recover = + CallInst::Create(ActionIntrin, ActionArgs, "recover", LPadBB); + IndirectBrInst *Branch = IndirectBrInst::Create(Recover, 0, LPadBB); + for (ActionHandler *Action : Actions) { + if (auto *CatchAction = dyn_cast(Action)) { + for (auto *Target : CatchAction->getReturnTargets()) { + Branch->addDestination(Target); + } + } + } + + // Remove all other instructions in this block. + BasicBlock::iterator II = Recover; + --II; + while (cast(II) != LPad) { + Instruction *Inst = II; + --II; + // This skips the handler bitcasts. + if (!(Inst->hasOneUse() && Inst->user_back() == Recover)) + Inst->eraseFromParent(); + } } } // End for each landingpad @@ -299,12 +486,8 @@ if (!HandlersOutlined) return false; - // FIXME: We will replace the landingpad bodies with llvm.eh.actions - // calls and indirect branches here and then delete blocks - // which are no longer reachable. That will get rid of the - // handlers that we have outlined. There is code below - // that looks for allocas with no uses in the parent function. - // That will only happen after the pruning is implemented. + // Delete any blocks that were only used by handlers that were outlined above. + removeUnreachableBlocks(F); // Remap the frame variables. SmallVector StructTys; @@ -332,9 +515,6 @@ HandlerAllocas &AllocaInfo = VarInfoEntry.second; if (auto *ParentAlloca = dyn_cast(ParentVal)) { - // If the instruction still has uses in the parent function or if it is - // referenced by more than one handler, add it to the frame allocation - // structure. if (ParentAlloca->getNumUses() != 0 || AllocaInfo.Allocas.size() > 1) { Type *VarTy = ParentAlloca->getAllocatedType(); StructTys.push_back(VarTy); @@ -363,8 +543,6 @@ IRBuilder<> Builder(F.getParent()->getContext()); // Create a frame allocation. - Module *M = F.getParent(); - LLVMContext &Context = M->getContext(); BasicBlock *Entry = &F.getEntryBlock(); Builder.SetInsertPoint(Entry->getFirstInsertionPt()); Function *FrameAllocFn = @@ -426,7 +604,20 @@ ParentAlloca = DemotePHIToStack(PN, AllocaInsertPt); } else { Instruction *ParentInst = cast(ParentVal); - ParentAlloca = DemoteRegToStack(*ParentInst, true, ParentInst); + // FIXME: This is a work-around to temporarily handle the case where an + // instruction that is only used in handlers is not sunk. + // Without uses, DemoteRegToStack would just eliminate the value. + // This will fail if ParentInst is an invoke. + if (ParentInst->getNumUses() == 0) { + BasicBlock::iterator InsertPt = ParentInst; + ++InsertPt; + ParentAlloca = + new AllocaInst(ParentInst->getType(), nullptr, + ParentInst->getName() + ".reg2mem", InsertPt); + new StoreInst(ParentInst, ParentAlloca, InsertPt); + } else { + ParentAlloca = DemoteRegToStack(*ParentInst, true, ParentInst); + } } } @@ -469,8 +660,50 @@ return HandlersOutlined; } -bool WinEHPrepare::outlineHandler(HandlerType CatchOrCleanup, Function *SrcFn, - Constant *SelectorType, LandingPadInst *LPad, +// This function examines a block to determine whether the block ends with a +// conditional branch to a catch handler based on a selector comparison. +// This function is used both by the WinEHPrepare::findSelectorComparison() and +// WinEHCleanupDirector::handleTypeIdFor(). +static bool isSelectorDispatch(BasicBlock *BB, BasicBlock *&CatchHandler, + Constant *&Selector, BasicBlock *&NextBB) { + TerminatorInst *Terminator = BB->getTerminator(); + BranchInst *Branch = dyn_cast(Terminator); + if (!Branch || !Branch->isConditional()) + return false; + + CmpInst *Compare = dyn_cast(Branch->getCondition()); + if (!Compare || !Compare->isEquality()) + return false; + + // Check to see if either operand is a call to get an eh selector id. + const IntrinsicInst *Intrin = dyn_cast(Compare->getOperand(0)); + if (!Intrin || Intrin->getIntrinsicID() != llvm::Intrinsic::eh_typeid_for) + Intrin = dyn_cast(Compare->getOperand(1)); + if (!Intrin || Intrin->getIntrinsicID() != llvm::Intrinsic::eh_typeid_for) + return false; + + Selector = dyn_cast(Intrin->getOperand(0)); + if (!Selector) + return false; + + if (Compare->getPredicate() == CmpInst::ICMP_EQ) { + CatchHandler = Branch->getSuccessor(0); + NextBB = Branch->getSuccessor(1); + return true; + } + + if (Compare->getPredicate() == CmpInst::ICMP_NE) { + CatchHandler = Branch->getSuccessor(1); + NextBB = Branch->getSuccessor(0); + return true; + } + + // Unexpected predicate + return false; +} + +bool WinEHPrepare::outlineHandler(ActionHandler *Action, Function *SrcFn, + LandingPadInst *LPad, BasicBlock *StartBB, CallInst *&EHAlloc, FrameVarInfoMap &VarInfo) { Module *M = SrcFn->getParent(); @@ -482,7 +715,7 @@ ArgTys.push_back(Int8PtrType); ArgTys.push_back(Int8PtrType); Function *Handler; - if (CatchOrCleanup == Catch) { + if (Action->getType() == Catch) { FunctionType *FnType = FunctionType::get(Int8PtrType, ArgTys, false); Handler = Function::Create(FnType, GlobalVariable::InternalLinkage, SrcFn->getName() + ".catch", M); @@ -519,7 +752,8 @@ std::unique_ptr Director; - if (CatchOrCleanup == Catch) { + if (Action->getType() == Catch) { + Constant *SelectorType = cast(Action)->getSelector(); Director.reset( new WinEHCatchDirector(LPad, Handler, SelectorType, VarInfo)); } else { @@ -528,15 +762,17 @@ ValueToValueMapTy VMap; - // FIXME: Map other values referenced in the filter handler. + Director->mapLandingPadUsers(LPad, VMap); SmallVector Returns; ClonedCodeInfo InlinedFunctionInfo; - BasicBlock::iterator II = LPad; + BasicBlock::iterator II = StartBB->begin(); + if (cast(II) == LPad) + ++II; CloneAndPruneIntoFromInst( - Handler, SrcFn, ++II, VMap, + Handler, SrcFn, II, VMap, /*ModuleLevelChanges=*/false, Returns, "", &InlinedFunctionInfo, SrcFn->getParent()->getDataLayout(), Director.get()); @@ -545,72 +781,166 @@ Entry->getInstList().splice(Entry->end(), FirstClonedBB->getInstList()); FirstClonedBB->eraseFromParent(); + if (auto *CatchAction = dyn_cast(Action)) { + WinEHCatchDirector *CatchDirector = + reinterpret_cast(Director.get()); + CatchAction->setExceptionVar(CatchDirector->getExceptionVar()); + CatchAction->setReturnTargets(CatchDirector->getReturnTargets()); + } + + Action->setOutlinedFunction(Handler); + return true; } -CloningDirector::CloningAction WinEHCloningDirectorBase::handleInstruction( - ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) { - // Intercept instructions which extract values from the landing pad aggregate. - if (auto *Extract = dyn_cast(Inst)) { - if (Extract->getAggregateOperand() == LPI) { - assert(Extract->getNumIndices() == 1 && - "Unexpected operation: extracting both landing pad values"); - assert((*(Extract->idx_begin()) == 0 || *(Extract->idx_begin()) == 1) && - "Unexpected operation: extracting an unknown landing pad element"); - - if (*(Extract->idx_begin()) == 0) { - // Element 0 doesn't directly corresponds to anything in the WinEH - // scheme. - // It will be stored to a memory location, then later loaded and finally - // the loaded value will be used as the argument to an - // llvm.eh.begincatch - // call. We're tracking it here so that we can skip the store and load. - ExtractedEHPtr = Inst; - } else { - // Element 1 corresponds to the filter selector. We'll map it to 1 for - // matching purposes, but it will also probably be stored to memory and - // reloaded, so we need to track the instuction so that we can map the - // loaded value too. - VMap[Inst] = ConstantInt::get(SelectorIDType, 1); - ExtractedSelector = Inst; - } +void LandingPadMapper::mapLandingPad(const LandingPadInst *LPad) { + OriginLPad = LPad; - // Tell the caller not to clone this instruction. - return CloningDirector::SkipInstruction; + // The landingpad instruction returns an aggregate value. Typically, its + // value will be passed to a pair of extract value instructions and the + // results of those extracts are often passed to store instructions. + // In unoptimized code the stored value will often be loaded and then stored + // again. + for (auto *U : LPad->users()) { + const ExtractValueInst *Extract = dyn_cast(U); + if (!Extract) + continue; + assert(Extract->getNumIndices() == 1 && + "Unexpected operation: extracting both landing pad values"); + unsigned int Idx = *(Extract->idx_begin()); + assert((Idx == 0 || Idx == 1) && + "Unexpected operation: extracting an unknown landing pad element"); + if (Idx == 0) { + // Element 0 doesn't directly corresponds to anything in the WinEH + // scheme. + // It will be stored to a memory location, then later loaded and finally + // the loaded value will be used as the argument to an + // llvm.eh.begincatch + // call. We're tracking it here so that we can skip the store and load. + ExtractedEHPtrs.push_back(Extract); + } else if (Idx == 1) { + // Element 1 corresponds to the filter selector. We'll map it to 1 for + // matching purposes, but it will also probably be stored to memory and + // reloaded, so we need to track the instuction so that we can map the + // loaded value too. + ExtractedSelectors.push_back(Extract); } - // Other extract value instructions just get cloned. - return CloningDirector::CloneInstruction; - } - if (auto *Store = dyn_cast(Inst)) { - // Look for and suppress stores of the extracted landingpad values. - const Value *StoredValue = Store->getValueOperand(); - if (StoredValue == ExtractedEHPtr) { - EHPtrStoreAddr = Store->getPointerOperand(); - return CloningDirector::SkipInstruction; + // Look for stores of the extracted values. + for (auto *EU : Extract->users()) { + if (auto *Store = dyn_cast(EU)) { + if (Idx == 1) { + SelectorStores.push_back(Store); + SelectorStoreAddrs.push_back(Store->getPointerOperand()); + } else { + EHPtrStores.push_back(Store); + EHPtrStoreAddrs.push_back(Store->getPointerOperand()); + } + } } - if (StoredValue == ExtractedSelector) { - SelectorStoreAddr = Store->getPointerOperand(); - return CloningDirector::SkipInstruction; + } +} + +bool LandingPadMapper::isLandingPadSpecificInst(const Instruction *Inst) const { + if (Inst == OriginLPad) + return true; + for (auto *Extract : ExtractedEHPtrs) { + if (Inst == Extract) + return true; + } + for (auto *Extract : ExtractedSelectors) { + if (Inst == Extract) + return true; + } + for (auto *Store : EHPtrStores) { + if (Inst == Store) + return true; + } + for (auto *Store : SelectorStores) { + if (Inst == Store) + return true; + } + + return false; +} + +void LandingPadMapper::remapSelector(ValueToValueMapTy &VMap, + Value *MappedValue) const { + // Remap all selector extract instructions to the specified value. + for (auto *Extract : ExtractedSelectors) + VMap[Extract] = MappedValue; +} + +bool LandingPadMapper::isEHPtrLoad(const LoadInst *Load) { + // This makes the assumption that a store we've previously seen dominates + // this load instruction. That might seem like a rather huge assumption, + // but given the way that landingpads are constructed its fairly safe. + // FIXME: Add debug/assert code that verifies this. + const Value *LoadAddr = Load->getPointerOperand(); + for (auto *StoreAddr : EHPtrStoreAddrs) { + if (LoadAddr == StoreAddr) { + // Handle the common debug scenario where this loaded value is stored + // to a different location. + for (auto *U : Load->users()) { + if (auto *Store = dyn_cast(U)) { + EHPtrStores.push_back(Store); + EHPtrStoreAddrs.push_back(Store->getPointerOperand()); + } + } + return true; } + } + return false; +} - // Any other store just gets cloned. - return CloningDirector::CloneInstruction; +bool LandingPadMapper::isSelectorLoad(const LoadInst *Load) { + // This makes the assumption that a store we've previously seen dominates + // this load instruction. That might seem like a rather huge assumption, + // but given the way that landingpads are constructed its fairly safe. + // FIXME: Add debug/assert code that verifies this. + const Value *LoadAddr = Load->getPointerOperand(); + for (auto *StoreAddr : SelectorStoreAddrs) { + if (LoadAddr == StoreAddr) { + // Handle the common debug scenario where this loaded value is stored + // to a different location. + for (auto *U : Load->users()) { + if (auto *Store = dyn_cast(U)) { + SelectorStores.push_back(Store); + SelectorStoreAddrs.push_back(Store->getPointerOperand()); + } + } + return true; + } } + return false; +} + +void WinEHCloningDirectorBase::mapLandingPadUsers(LandingPadInst *LPad, + ValueToValueMapTy &VMap) { + // Defer parse of the landing pad to a helper object. + LPadMapper.mapLandingPad(LPad); + LPadMapper.remapSelector(VMap, ConstantInt::get(SelectorIDType, 1)); +} + +CloningDirector::CloningAction WinEHCloningDirectorBase::handleInstruction( + ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) { + // If this is one of the boilerplate landing pad instructions, skip it. + // The instruction will have already been remapped in VMap. + if (LPadMapper.isLandingPadSpecificInst(Inst)) + return CloningDirector::SkipInstruction; if (auto *Load = dyn_cast(Inst)) { // Look for loads of (previously suppressed) landingpad values. - // The EHPtr load can be ignored (it should only be used as - // an argument to llvm.eh.begincatch), but the selector value - // needs to be mapped to a constant value of 1 to be used to - // simplify the branching to always flow to the current handler. - const Value *LoadAddr = Load->getPointerOperand(); - if (LoadAddr == EHPtrStoreAddr) { - VMap[Inst] = UndefValue::get(Int8PtrType); + // The EHPtr load can be mapped to an undef value as it should only be used + // as an argument to llvm.eh.begincatch, but the selector value needs to be + // mapped to a constant value of 1. This value will be used to simplify the + // branching to always flow to the current handler. + if (LPadMapper.isSelectorLoad(Load)) { + VMap[Inst] = ConstantInt::get(SelectorIDType, 1); return CloningDirector::SkipInstruction; } - if (LoadAddr == SelectorStoreAddr) { - VMap[Inst] = ConstantInt::get(SelectorIDType, 1); + if (LPadMapper.isEHPtrLoad(Load)) { + VMap[Inst] = UndefValue::get(Int8PtrType); return CloningDirector::SkipInstruction; } @@ -618,6 +948,26 @@ return CloningDirector::CloneInstruction; } + // Nested landing pads will be cloned as stubs, with just the + // landingpad instruction and an unreachable instruction. When + // all landingpads have been outlined, we'll replace this with the + // llvm.eh.actions call and indirect branch created when the + // landing pad was outlined. + if (auto *NestedLPad = dyn_cast(Inst)) { + Instruction *NewInst = NestedLPad->clone(); + if (NestedLPad->hasName()) + NewInst->setName(NestedLPad->getName()); + // FIXME: Store this mapping somewhere else also. + VMap[NestedLPad] = NewInst; + BasicBlock::InstListType &InstList = NewBB->getInstList(); + InstList.push_back(NewInst); + InstList.push_back(new UnreachableInst(NewBB->getContext())); + return CloningDirector::StopCloningBB; + } + + if (auto *Invoke = dyn_cast(Inst)) + return handleInvoke(VMap, Invoke, NewBB); + if (auto *Resume = dyn_cast(Inst)) return handleResume(VMap, Resume, NewBB); @@ -642,6 +992,10 @@ // aggregate when catching by value. // FIXME: Leave something behind to indicate where the exception object lives // for this handler. Should it be part of llvm.eh.actions? + assert(ExceptionObjectVar == nullptr && "Multiple calls to " + "llvm.eh.begincatch found while " + "outlining catch handler."); + ExceptionObjectVar = Inst->getOperand(1)->stripPointerCasts(); return CloningDirector::SkipInstruction; } @@ -654,10 +1008,9 @@ // to be. // The end catch call can occur in one of two places: either in a - // landingpad - // block that is part of the catch handlers exception mechanism, or at the - // end of the catch block. If it occurs in a landing pad, we must skip it - // and continue so that the landing pad gets cloned. + // landingpad block that is part of the catch handlers exception mechanism, + // or at the end of the catch block. If it occurs in a landing pad, we must + // skip it and continue so that the landing pad gets cloned. // FIXME: This case isn't fully supported yet and shouldn't turn up in any // of the test cases until it is. if (IntrinCall->getParent()->isLandingPad()) @@ -673,8 +1026,10 @@ assert(std::next(BasicBlock::const_iterator(IntrinCall)) == BasicBlock::const_iterator(Branch)); - ReturnInst::Create(NewBB->getContext(), - BlockAddress::get(Branch->getSuccessor(0)), NewBB); + BasicBlock *ContinueLabel = Branch->getSuccessor(0); + ReturnInst::Create(NewBB->getContext(), BlockAddress::get(ContinueLabel), + NewBB); + ReturnTargets.push_back(ContinueLabel); // We just added a terminator to the cloned block. // Tell the caller to stop processing the current basic block so that @@ -696,6 +1051,120 @@ return CloningDirector::SkipInstruction; } +// This function parses a landing pad to see if all it does is resume. +// If so, it would be called from within another landing pad for +// exception handling, but since it does nothing any invoke that leads +// to this landing pad can be replaced with a call and an unconditional +// branch to the normal destination. Any exceptions that occur within the +// called function will be handled by the normal runtime exception handling +// process. +static bool isLandingPadEmptyResume(const BasicBlock *LPadBB) { + assert(LPadBB->isLandingPad()); + const LandingPadInst *LPad = LPadBB->getLandingPadInst(); + const BasicBlock *BB = LPadBB; + + const Value *LoadedEHPtr = nullptr; + const Value *LoadedSelector = nullptr; + + LandingPadMapper LPadMapper; + LPadMapper.mapLandingPad(LPad); + + while (BB) { + for (const Instruction &Inst : BB->getInstList()) { + // If this is one of the boilerplate landing pad instructions, skip it. + // The instruction will have already been remapped in VMap. + if (LPadMapper.isLandingPadSpecificInst(&Inst)) + continue; + + // There should be a call to llvm.eh.endcatch somewhere in the landing + // pad. + if (match(&Inst, m_Intrinsic())) + continue; + + if (auto *Load = dyn_cast(&Inst)) { + // Look for loads of (previously suppressed) landingpad values. + // The EHPtr load can be mapped to an undef value as it should only be + // used + // as an argument to llvm.eh.begincatch, but the selector value needs to + // be + // mapped to a constant value of 1. This value will be used to simplify + // the + // branching to always flow to the current handler. + if (LPadMapper.isSelectorLoad(Load)) { + LoadedEHPtr = Load; + continue; + } + if (LPadMapper.isEHPtrLoad(Load)) { + LoadedSelector = Load; + continue; + } + + // Any other load constitutes non-trival content. + return false; + } + + if (auto *Insert = dyn_cast(&Inst)) { + if (Insert->getInsertedValueOperand() == LoadedEHPtr || + Insert->getInsertedValueOperand() == LoadedSelector) + continue; + // Any other insert constitutes non-trival content. + return false; + } + + // If we reach a resume instruction without having bailed out for some + // other reason, the landing pad is an empty resume. + if (isa(Inst)) + return true; + + // It's likely that the landingpad block will unconditionally branch to + // another block which actually executes the resume. This code handles + // that scenario. + if (auto *Branch = dyn_cast(&Inst)) { + if (!Branch->isUnconditional()) + return false; + BB = Branch->getSuccessor(0); + continue; + } + + // Any other instruction is outside the pattern we were looking for. + return false; + } // End for (Instruction) + } // End while (BB) + + return false; +} + +CloningDirector::CloningAction +WinEHCatchDirector::handleInvoke(ValueToValueMapTy &VMap, + const InvokeInst *Invoke, BasicBlock *NewBB) { + if (isLandingPadEmptyResume(Invoke->getUnwindDest())) { + SmallVector CallArgs(Invoke->op_begin(), Invoke->op_end() - 3); + // Insert a normal call instruction... + CallInst *NewCall = + CallInst::Create(const_cast(Invoke->getCalledValue()), + CallArgs, Invoke->getName(), NewBB); + NewCall->setCallingConv(Invoke->getCallingConv()); + NewCall->setAttributes(Invoke->getAttributes()); + NewCall->setDebugLoc(Invoke->getDebugLoc()); + + RemapInstruction(NewCall, VMap, RF_None, getTypeRemapper(), + getValueMaterializer()); + + VMap[Invoke] = NewCall; + + // Insert an unconditional branch to the normal destination. + BranchInst::Create(Invoke->getNormalDest(), NewBB); + + // The unwind destination won't be cloned into the new function, so + // we don't need to clean up its phi nodes. + + // We've added a new terminator so we want to stop cloning this block, but + // we need the successor of the block to be added to the work list. + return CloningDirector::CloneSuccessors; + } + return CloningDirector::CloneInstruction; +} + CloningDirector::CloningAction WinEHCatchDirector::handleResume(ValueToValueMapTy &VMap, const ResumeInst *Resume, BasicBlock *NewBB) { @@ -728,13 +1197,46 @@ CloningDirector::CloningAction WinEHCleanupDirector::handleTypeIdFor( ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) { - // This causes a replacement that will collapse the landing pad CFG - // to just the cleanup code. + // If we encounter a selector comparison while cloning a cleanup handler, + // we want to stop cloning immediately. Anything after the dispatch + // will be outlined into a different handler. + BasicBlock *CatchHandler; + Constant *Selector; + BasicBlock *NextBB; + if (isSelectorDispatch(const_cast(Inst->getParent()), + CatchHandler, Selector, NextBB)) { + ReturnInst::Create(NewBB->getContext(), nullptr, NewBB); + return CloningDirector::StopCloningBB; + } + // If eg.typeid.for is called for any other reason, it can be ignored. VMap[Inst] = ConstantInt::get(SelectorIDType, 0); - // Tell the caller not to clone this instruction. return CloningDirector::SkipInstruction; } +CloningDirector::CloningAction WinEHCleanupDirector::handleInvoke( + ValueToValueMapTy &VMap, const InvokeInst *Invoke, BasicBlock *NewBB) { + // All invokes in cleanup handlers can be replaced with calls. + SmallVector CallArgs(Invoke->op_begin(), Invoke->op_end() - 3); + // Insert a normal call instruction... + CallInst *NewCall = + CallInst::Create(const_cast(Invoke->getCalledValue()), CallArgs, + Invoke->getName(), NewBB); + NewCall->setCallingConv(Invoke->getCallingConv()); + NewCall->setAttributes(Invoke->getAttributes()); + NewCall->setDebugLoc(Invoke->getDebugLoc()); + VMap[Invoke] = NewCall; + + // Insert an unconditional branch to the normal destination. + BranchInst::Create(Invoke->getNormalDest(), NewBB); + + // The unwind destination won't be cloned into the new function, so + // we don't need to clean up its phi nodes. + + // We just added a terminator to the cloned block. + // Tell the caller to stop processing the current basic block. + return CloningDirector::StopCloningBB; +} + CloningDirector::CloningAction WinEHCleanupDirector::handleResume( ValueToValueMapTy &VMap, const ResumeInst *Resume, BasicBlock *NewBB) { ReturnInst::Create(NewBB->getContext(), nullptr, NewBB); @@ -749,8 +1251,6 @@ Function *OutlinedFn, FrameVarInfoMap &FrameVarInfo) : FrameVarInfo(FrameVarInfo), Builder(OutlinedFn->getContext()) { Builder.SetInsertPoint(&OutlinedFn->getEntryBlock()); - // FIXME: Do something with the FrameVarMapped so that it is shared across the - // function. } Value *WinEHFrameVariableMaterializer::materializeValueFor(Value *V) { @@ -784,3 +1284,362 @@ // Don't materialize other values. return nullptr; } + +// This function maps the catch and cleanup handlers that are reachable from the +// specified landing pad. The landing pad sequence will have this basic shape: +// +// +// +// +// +// +// +// +// ... +// +// Any of the cleanup slots may be absent. The cleanup slots may be occupied by +// any arbitrary control flow, but all paths through the cleanup code must +// eventually reach the next selector comparison and no path can skip to a +// different selector comparisons, though some paths may terminate abnormally. +// Therefore, we will use a depth first search from the start of any given +// cleanup block and stop searching when we find the next selector comparison. +// +// If the landingpad instruction does not have a catch clause, we will assume +// that any instructions other than selector comparisons and catch handlers can +// be ignored. In practice, these will only be the boilerplate instructions. +// +// The catch handlers may also have any control structure, but we are only +// interested in the start of the catch handlers, so we don't need to actually +// follow the flow of the catch handlers. The start of the catch handlers can +// be located from the compare instructions, but they can be skipped in the +// flow by following the contrary branch. +// +// FIXME: Update this to avoid re-parsing blocks that are shared by multiple +// landingpads. +void WinEHPrepare::mapLandingPadBlocks(LandingPadInst *LPad, + LandingPadActions &Actions) { + unsigned int NumClauses = LPad->getNumClauses(); + unsigned int HandlersFound = 0; + BasicBlock *BB = LPad->getParent(); + + DEBUG(dbgs() << "Mapping landing pad: " << BB->getName() << "\n"); + + if (NumClauses == 0) { + // This landing pad contains only cleanup code. + CleanupHandler *Action = new CleanupHandler(BB); + CleanupHandlerMap[BB] = Action; + Actions.insertCleanupHandler(Action); + DEBUG(dbgs() << " Assuming cleanup code in block " << BB->getName() + << "\n"); + assert(LPad->isCleanup()); + return; + } + + VisitedBlockSet VisitedBlocks; + + while (HandlersFound != NumClauses) { + Constant *Selector = nullptr; + BasicBlock *NextBB = nullptr; + + // See if the clause we're looking for is a catch-all. + // If so, the catch begins immediately. + if (isa(LPad->getClause(HandlersFound))) { + // The catch all must occur last. + assert(HandlersFound == NumClauses - 1); + + // See if there is any interesting code executed before the catch. + if (auto *CleanupAction = findCleanupHandler(BB, BB)) { + // Add a cleanup entry to the list + Actions.insertCleanupHandler(CleanupAction); + DEBUG(dbgs() << " Found cleanup code in block " + << CleanupAction->getStartBlock()->getName() << "\n"); + } + + // Add the catch handler to the action list. + CatchHandler *Action = + new CatchHandler(BB, LPad->getClause(HandlersFound), nullptr); + CatchHandlerMap[BB] = Action; + Actions.insertCatchHandler(Action); + DEBUG(dbgs() << " Catch all handler at block " << BB->getName() << "\n"); + ++HandlersFound; + continue; + } + + CatchHandler *CatchAction = findCatchHandler(BB, NextBB, VisitedBlocks); + // See if there is any interesting code executed before the dispatch. + if (auto *CleanupAction = + findCleanupHandler(BB, CatchAction->getStartBlock())) { + // Add a cleanup entry to the list + Actions.insertCleanupHandler(CleanupAction); + DEBUG(dbgs() << " Found cleanup code in block " + << CleanupAction->getStartBlock()->getName() << "\n"); + } + + assert(CatchAction); + ++HandlersFound; + + // Add the catch handler to the action list. + Actions.insertCatchHandler(CatchAction); + DEBUG(dbgs() << " Found catch dispatch in block " + << CatchAction->getStartBlock()->getName() << "\n"); + + // Move on to the block after the catch handler. + BB = NextBB; + } + + // See if there is any interesting code executed before the resume. + if (auto *CleanupAction = findCleanupHandler(BB, BB)) { + // Add a cleanup entry to the list + Actions.insertCleanupHandler(CleanupAction); + DEBUG(dbgs() << " Found cleanup code in block " + << CleanupAction->getStartBlock()->getName() << "\n"); + } + + // It's possible that some optimization moved code into a landingpad that + // wasn't + // previously being used for cleanup. If that happens, we need to execute + // that + // extra code from a cleanup handler. + if (Actions.includesCleanup() && !LPad->isCleanup()) + LPad->setCleanup(true); +} + +// This function searches starting with the input block for the next +// block that terminates with a branch whose condition is based on a selector +// comparison. This may be the input block. See the mapLandingPadBlocks +// comments for a discussion of control flow assumptions. +// +CatchHandler *WinEHPrepare::findCatchHandler(BasicBlock *BB, + BasicBlock *&NextBB, + VisitedBlockSet &VisitedBlocks) { + // See if we've already found a catch handler use it. + // Call count() first to avoid creating a null entry for blocks + // we haven't seen before. + if (CatchHandlerMap.count(BB) && CatchHandlerMap[BB] != nullptr) { + CatchHandler *Action = cast(CatchHandlerMap[BB]); + NextBB = Action->getNextBB(); + return Action; + } + + // VisitedBlocks applies only to the current search. We still + // need to consider blocks that we've visited while mapping other + // landing pads. + VisitedBlocks.insert(BB); + + BasicBlock *CatchBlock = nullptr; + Constant *Selector = nullptr; + + // If this is the first time we've visited this block from any landing pad + // look to see if it is a selector dispatch block. + if (!CatchHandlerMap.count(BB)) { + if (isSelectorDispatch(BB, CatchBlock, Selector, NextBB)) { + CatchHandler *Action = new CatchHandler(BB, Selector, NextBB); + CatchHandlerMap[BB] = Action; + return Action; + } + } + + // Visit each successor, looking for the dispatch. + // FIXME: We expect to find the dispatch quickly, so this will probably + // work better as a breadth first search. + for (BasicBlock *Succ : successors(BB)) { + if (VisitedBlocks.count(Succ)) + continue; + + CatchHandler *Action = findCatchHandler(Succ, NextBB, VisitedBlocks); + if (Action) + return Action; + } + return nullptr; +} + +// This function searches starting with the input block for the next block that +// contains code that is not part of a catch handler and would not be eliminated +// during handler outlining. +// +CleanupHandler *WinEHPrepare::findCleanupHandler(BasicBlock *StartBB, + BasicBlock *EndBB) { + // Here we will skip over the following: + // + // landing pad prolog: + // + // Unconditional branches + // + // Selector dispatch + // + // Resume pattern + // + // Anything else marks the start of an interesting block + + BasicBlock *BB = StartBB; + // Anything other than an unconditional branch will kick us out of this loop + // one way or another. + while (BB) { + // If we've already scanned this block, don't scan it again. If it is + // a cleanup block, there will be an action in the CleanupHandlerMap. + // If we've scanned it and it is not a cleanup block, there will be a + // nullptr in the CleanupHandlerMap. If we have not scanned it, there will + // be no entry in the CleanupHandlerMap. We must call count() first to + // avoid creating a null entry for blocks we haven't scanned. + if (CleanupHandlerMap.count(BB)) { + ActionHandler; + if (auto *Action = CleanupHandlerMap[BB]) { + return cast(Action); + } else { + // Any block that we determined wasn't cleanup code will end with a + // branch instruction. If it's a conditional branch, the condition + // will be a selector compare and the code below will find the non-catch + // successor. + if (BB == EndBB) + return nullptr; + BranchInst *Branch = cast(BB->getTerminator()); + if (!Branch->isConditional()) { + BB = Branch->getSuccessor(0); + continue; + } + CmpInst *Compare = cast(Branch->getCondition()); + assert(Compare->isEquality()); + if (Compare->getPredicate() == CmpInst::ICMP_EQ) + BB = Branch->getSuccessor(1); + else + BB = Branch->getSuccessor(0); + continue; + } + } + + // Create an entry in the cleanup handler map for this block. Initially + // we create an entry that says this isn't a cleanup block. If we find + // cleanup code, the caller will replace this entry. + CleanupHandlerMap[BB] = nullptr; + + TerminatorInst *Terminator = BB->getTerminator(); + + // Landing pad blocks have extra instructions we need to accept. + bool IsLandingPadBB = BB->isLandingPad(); + LandingPadMapper LPadMapper; + if (IsLandingPadBB) { + LandingPadInst *LPad = BB->getLandingPadInst(); + LPadMapper.mapLandingPad(LPad); + } + + // Look for the bare resume pattern: + // %exn2 = load i8** %exn.slot + // %sel2 = load i32* %ehselector.slot + // %lpad.val1 = insertvalue { i8*, i32 } undef, i8* %exn2, 0 + // %lpad.val2 = insertvalue { i8*, i32 } %lpad.val1, i32 %sel2, 1 + // resume { i8*, i32 } %lpad.val2 + if (auto *Resume = dyn_cast(Terminator)) { + InsertValueInst *Insert1 = nullptr; + InsertValueInst *Insert2 = nullptr; + if (!isa(Resume->getOperand(0))) { + Insert2 = dyn_cast(Resume->getOperand(0)); + if (!Insert2) { + CleanupHandler *Action = new CleanupHandler(BB); + CleanupHandlerMap[BB] = Action; + return Action; + } + Insert1 = dyn_cast(Insert2->getAggregateOperand()); + if (!Insert1) { + CleanupHandler *Action = new CleanupHandler(BB); + CleanupHandlerMap[BB] = Action; + return Action; + } + } + for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end(); + II != IE; ++II) { + Instruction *Inst = II; + if (IsLandingPadBB && LPadMapper.isLandingPadSpecificInst(Inst)) + continue; + if (isa(Inst) || isa(Inst)) + continue; + if (Inst == Insert1 || Inst == Insert2 || Inst == Resume) + continue; + if (!Inst->hasOneUse() || + (Inst->user_back() != Insert1 && Inst->user_back() != Insert2)) { + CleanupHandler *Action = new CleanupHandler(BB); + CleanupHandlerMap[BB] = Action; + return Action; + } + } + return nullptr; + } + + BranchInst *Branch = dyn_cast(Terminator); + if (Branch) { + if (Branch->isConditional()) { + // Look for the selector dispatch. + // %sel = load i32* %ehselector.slot + // %2 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIf to i8*)) + // #4 + // %matches = icmp eq i32 %sel12, %2 + // br i1 %matches, label %catch14, label %eh.resume + CmpInst *Compare = dyn_cast(Branch->getCondition()); + if (!Compare || !Compare->isEquality()) { + CleanupHandler *Action = new CleanupHandler(BB); + CleanupHandlerMap[BB] = Action; + return Action; + } + for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), + IE = BB->end(); + II != IE; ++II) { + Instruction *Inst = II; + if (IsLandingPadBB && LPadMapper.isLandingPadSpecificInst(Inst)) + continue; + if (Inst == Compare || Inst == Branch) + continue; + if (!Inst->hasOneUse() || (Inst->user_back() != Compare)) { + CleanupHandler *Action = new CleanupHandler(BB); + CleanupHandlerMap[BB] = Action; + return Action; + } + if (match(Inst, m_Intrinsic())) + continue; + if (!isa(Inst)) { + CleanupHandler *Action = new CleanupHandler(BB); + CleanupHandlerMap[BB] = Action; + return Action; + } + } + if (BB == EndBB) + return nullptr; + if (Compare->getPredicate() == CmpInst::ICMP_EQ) + BB = Branch->getSuccessor(1); + else + BB = Branch->getSuccessor(0); + continue; + } else { + // Look for empty blocks with unconditional branches. + for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), + IE = BB->end(); + II != IE; ++II) { + Instruction *Inst = II; + if (IsLandingPadBB && LPadMapper.isLandingPadSpecificInst(Inst)) + continue; + if (Inst == Branch) + continue; + IntrinsicInst *IntrinCall = dyn_cast(Inst); + if (IntrinCall && + IntrinCall->getIntrinsicID() == Intrinsic::eh_endcatch) + continue; + if (match(Inst, m_Intrinsic())) + continue; + // Anything else makes this interesting cleanup code. + CleanupHandler *Action = new CleanupHandler(BB); + CleanupHandlerMap[BB] = Action; + return Action; + } + if (BB == EndBB) + return nullptr; + // The branch was unconditional. + BB = Branch->getSuccessor(0); + continue; + } // End else of if branch was conditional + } // End if Branch + + // Anything else makes this interesting cleanup code. + CleanupHandler *Action = new CleanupHandler(BB); + CleanupHandlerMap[BB] = Action; + return Action; + } + return nullptr; +} Index: lib/Transforms/Utils/CloneFunction.cpp =================================================================== --- lib/Transforms/Utils/CloneFunction.cpp +++ lib/Transforms/Utils/CloneFunction.cpp @@ -397,6 +397,14 @@ // terminator into the new basic block in this case. if (Action == CloningDirector::StopCloningBB) return; + if (Action == CloningDirector::CloneSuccessors) { + // If the director says to skip with a terminate instruction, we still + // need to clone this block's successors. + const TerminatorInst *TI = BB->getTerminator(); + for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) + ToClone.push_back(TI->getSuccessor(i)); + return; + } assert(Action != CloningDirector::SkipInstruction && "SkipInstruction is not valid for terminators."); } Index: test/CodeGen/WinEH/cppeh-catch-all.ll =================================================================== --- test/CodeGen/WinEH/cppeh-catch-all.ll +++ test/CodeGen/WinEH/cppeh-catch-all.ll @@ -18,6 +18,10 @@ target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-pc-windows-msvc" +; The function entry in this case remains unchanged. +; CHECK: define void @_Z4testv() #0 { +; CHECK: entry: + ; Function Attrs: uwtable define void @_Z4testv() #0 { entry: @@ -29,6 +33,18 @@ invoke.cont: ; preds = %entry br label %try.cont +; CHECK: lpad: ; preds = %entry +; CHECK: %tmp = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) +; CHECK: catch i8* null +; CHECK-NOT: %tmp1 = extractvalue { i8*, i32 } %tmp, 0 +; CHECK-NOT: store i8* %tmp1, i8** %exn.slot +; CHECK-NOT: %tmp2 = extractvalue { i8*, i32 } %tmp, 1 +; CHECK-NOT: store i32 %tmp2, i32* %ehselector.slot +; CHECK-NOT: br label %catch +; CHECK: %handler = bitcast i8* (i8*, i8*)* @_Z4testv.catch to i8* +; CHECK: %recover = call i8* (...)* @llvm.eh.actions({ i8*, i32 } %tmp, i32 0, i8* %handler, i8* null, i8* null) +; CHECK: indirectbr i8* %recover, [label %try.cont] + lpad: ; preds = %entry %tmp = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) catch i8* null Index: test/CodeGen/WinEH/cppeh-catch-scalar.ll =================================================================== --- test/CodeGen/WinEH/cppeh-catch-scalar.ll +++ test/CodeGen/WinEH/cppeh-catch-scalar.ll @@ -45,6 +45,18 @@ invoke.cont: ; preds = %entry br label %try.cont +; CHECK: lpad: ; preds = %entry +; CHECK: %tmp = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) +; CHECK: catch i8* bitcast (i8** @_ZTIi to i8*) +; CHECK-NOT: %tmp1 = extractvalue { i8*, i32 } %tmp, 0 +; CHECK-NOT: store i8* %tmp1, i8** %exn.slot +; CHECK-NOT: %tmp2 = extractvalue { i8*, i32 } %tmp, 1 +; CHECK-NOT: store i32 %tmp2, i32* %ehselector.slot +; CHECK-NOT: br label %catch.dispatch +; CHECK: %handler = bitcast i8* (i8*, i8*)* @_Z4testv.catch to i8* +; CHECK: %recover = call i8* (...)* @llvm.eh.actions({ i8*, i32 } %tmp, i32 0, i8* %handler, i32* %i, i8* bitcast (i8** @_ZTIi to i8*)) +; CHECK: indirectbr i8* %recover, [label %try.cont] + lpad: ; preds = %entry %tmp = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) catch i8* bitcast (i8** @_ZTIi to i8*) Index: test/CodeGen/WinEH/cppeh-frame-vars.ll =================================================================== --- test/CodeGen/WinEH/cppeh-frame-vars.ll +++ test/CodeGen/WinEH/cppeh-frame-vars.ll @@ -99,6 +99,18 @@ store i32 %add, i32* %a, align 4 br label %try.cont +; CHECK: lpad: ; preds = %for.body +; CHECK: %tmp4 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) +; CHECK: catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*) +; CHECK-NOT: %tmp5 = extractvalue { i8*, i32 } %tmp4, 0 +; CHECK-NOT: store i8* %tmp5, i8** %exn.slot +; CHECK-NOT: %tmp6 = extractvalue { i8*, i32 } %tmp4, 1 +; CHECK-NOT: store i32 %tmp6, i32* %ehselector.slot +; CHECK-NOT: br label %catch.dispatch +; CHECK: %handler = bitcast i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch" to i8* +; CHECK: %recover = call i8* (...)* @llvm.eh.actions({ i8*, i32 } %tmp4, i32 0, i8* %handler, i32* %e, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)) +; CHECK: indirectbr i8* %recover, [label %try.cont] + lpad: ; preds = %for.body %tmp4 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*) Index: test/CodeGen/WinEH/cppeh-inalloca.ll =================================================================== --- test/CodeGen/WinEH/cppeh-inalloca.ll +++ test/CodeGen/WinEH/cppeh-inalloca.ll @@ -66,6 +66,20 @@ invoke.cont: ; preds = %entry br label %try.cont +; CHECK: lpad: ; preds = %entry +; CHECK: %1 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) +; CHECK: cleanup +; CHECK: catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*) +; CHECK-NOT: %2 = extractvalue { i8*, i32 } %1, 0 +; CHECK-NOT: store i8* %2, i8** %exn.slot +; CHECK-NOT: %3 = extractvalue { i8*, i32 } %1, 1 +; CHECK-NOT: store i32 %3, i32* %ehselector.slot +; CHECK-NOT: br label %catch.dispatch +; CHECK: %handler = bitcast i8* (i8*, i8*)* @"\01?test@@YAHUA@@@Z.catch" to i8* +; CHECK: %handler1 = bitcast void (i8*, i8*)* @"\01?test@@YAHUA@@@Z.cleanup" to i8* +; CHECK: %recover = call i8* (...)* @llvm.eh.actions({ i8*, i32 } %1, i32 0, i8* %handler, i32* %e, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*), i32 0, i8* %handler1, i8* null, i8* null) +; CHECK: indirectbr i8* %recover, [label %cleanup] + lpad: ; preds = %entry %1 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) cleanup @@ -102,10 +116,10 @@ br label %cleanup ; The cleanup block should be re-written like this. -; CHECK: cleanup: ; preds = %try.cont, %catch +; CHECK: cleanup: ; preds = %lpad, %try.cont ; CHECK-NOT: %a2 = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* %0, i32 0, i32 0 -; CHECK: %.tmp.reload1 = load volatile <{ %struct.A }>*, <{ %struct.A }>** %.tmp.reg2mem -; CHECK: %a2 = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* %.tmp.reload1, i32 0, i32 0 +; CHECK: %.tmp.reload = load volatile <{ %struct.A }>*, <{ %struct.A }>** %.tmp.reg2mem +; CHECK: %a2 = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* %.tmp.reload, i32 0, i32 0 ; CHECK: call x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* %a2) #2 ; CHECK: %tmp10 = load i32, i32* %retval ; CHECK: ret i32 %tmp10 @@ -149,6 +163,17 @@ ; CHECK: ret i8* blockaddress(@"\01?test@@YAHUA@@@Z", %cleanup) ; CHECK: } +; The following cleanup handler should be outlined. +; CHECK: define internal void @"\01?test@@YAHUA@@@Z.cleanup"(i8*, i8*) { +; CHECK: entry: +; CHECK: %eh.alloc = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1) +; CHECK: %eh.data = bitcast i8* %eh.alloc to %"struct.\01?test@@YAHUA@@@Z.ehdata"* +; CHECK: %eh.temp.alloca = getelementptr inbounds %"struct.\01?test@@YAHUA@@@Z.ehdata", %"struct.\01?test@@YAHUA@@@Z.ehdata"* %eh.data, i32 0, i32 3 +; CHECK: %.reload = load <{ %struct.A }>*, <{ %struct.A }>** %eh.temp.alloca +; CHECK: %a3 = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* %.reload, i32 0, i32 0 +; CHECK: call x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* %a3) #2 +; CHECK: ret void +; CHECK: } declare void @"\01?may_throw@@YAXXZ"() #0 Index: test/CodeGen/WinEH/cppeh-min-unwind.ll =================================================================== --- test/CodeGen/WinEH/cppeh-min-unwind.ll +++ test/CodeGen/WinEH/cppeh-min-unwind.ll @@ -45,6 +45,19 @@ call void @_ZN9SomeClassD1Ev(%class.SomeClass* %obj) ret void +; CHECK: lpad: ; preds = %entry +; CHECK: %tmp = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) +; CHECK: cleanup +; CHECK-NOT: %tmp1 = extractvalue { i8*, i32 } %tmp, 0 +; CHECK-NOT: store i8* %tmp1, i8** %exn.slot +; CHECK-NOT: %tmp2 = extractvalue { i8*, i32 } %tmp, 1 +; CHECK-NOT: store i32 %tmp2, i32* %ehselector.slot +; CHECK-NOT: call void @_ZN9SomeClassD1Ev(%class.SomeClass* %obj) +; CHECK-NOT: br label %eh.resume +; CHECK: %handler = bitcast void (i8*, i8*)* @_Z4testv.cleanup to i8* +; CHECK: %recover = call i8* (...)* @llvm.eh.actions({ i8*, i32 } %tmp, i32 0, i8* %handler, i8* null, i8* null) +; CHECK: indirectbr i8* %recover, [] + lpad: ; preds = %entry %tmp = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) cleanup Index: test/CodeGen/WinEH/cppeh-nested-2.ll =================================================================== --- test/CodeGen/WinEH/cppeh-nested-2.ll +++ test/CodeGen/WinEH/cppeh-nested-2.ll @@ -0,0 +1,389 @@ +; RUN: opt -mtriple=x86_64-pc-windows-msvc -winehprepare -S -o - < %s | FileCheck %s + +; This test is based on the following code: +; +; class Inner { +; public: +; Inner(); +; ~Inner(); +; }; +; class Outer { +; public: +; Outer(); +; ~Outer(); +; }; +; void test() { +; try { +; Outer outer; +; try { +; Inner inner; +; may_throw(); +; } catch (int i) { +; handle_int(i); +; } +; } catch (float f) { +; handle_float(f); +; } +; done(); +; } + +; ModuleID = 'nested-2.cpp' +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-pc-windows-msvc" + +; This structure should be created for the frame allocation. +; CHECK: %struct._Z4testv.ehdata = type { i32, i8*, float, i32, %class.Outer, %class.Inner } + +%class.Outer = type { i8 } +%class.Inner = type { i8 } + +@_ZTIf = external constant i8* +@_ZTIi = external constant i8* + +; The function entry should be rewritten like this. +; CHECK: define void @_Z4testv() #0 { +; CHECK: entry: +; CHECK: %frame.alloc = call i8* @llvm.frameallocate(i32 32) +; CHECK: %eh.data = bitcast i8* %frame.alloc to %struct._Z4testv.ehdata* +; CHECK-NOT: %outer = alloca %class.Outer, align 1 +; CHECK: %outer = getelementptr inbounds %struct._Z4testv.ehdata, %struct._Z4testv.ehdata* %eh.data, i32 0, i32 4 +; (FIXME -- This should be eliminated, but it isn't yet.) %exn.slot = alloca i8* +; (FIXME -- This should be eliminated, but it isn't yet.) %ehselector.slot = alloca i32 +; CHECK-NOT: %inner = alloca %class.Inner, align 1 +; CHECK: %inner = getelementptr inbounds %struct._Z4testv.ehdata, %struct._Z4testv.ehdata* %eh.data, i32 0, i32 5 +; CHECK-NOT: %i = alloca i32, align 4 +; CHECK: %i = getelementptr inbounds %struct._Z4testv.ehdata, %struct._Z4testv.ehdata* %eh.data, i32 0, i32 3 +; CHECK-NOT: %f = alloca float, align 4 +; CHECK: %f = getelementptr inbounds %struct._Z4testv.ehdata, %struct._Z4testv.ehdata* %eh.data, i32 0, i32 2 + +; Function Attrs: uwtable +define void @_Z4testv() #0 { +entry: + %outer = alloca %class.Outer, align 1 + %exn.slot = alloca i8* + %ehselector.slot = alloca i32 + %inner = alloca %class.Inner, align 1 + %i = alloca i32, align 4 + %f = alloca float, align 4 + invoke void @_ZN5OuterC1Ev(%class.Outer* %outer) + to label %invoke.cont unwind label %lpad + +invoke.cont: ; preds = %entry + invoke void @_ZN5InnerC1Ev(%class.Inner* %inner) + to label %invoke.cont2 unwind label %lpad1 + +invoke.cont2: ; preds = %invoke.cont + invoke void @_Z9may_throwv() + to label %invoke.cont4 unwind label %lpad3 + +invoke.cont4: ; preds = %invoke.cont2 + invoke void @_ZN5InnerD1Ev(%class.Inner* %inner) + to label %invoke.cont5 unwind label %lpad1 + +invoke.cont5: ; preds = %invoke.cont4 + br label %try.cont + +; CHECK: lpad: ; preds = %try.cont, %entry +; CHECK: %tmp = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) +; CHECK: catch i8* bitcast (i8** @_ZTIf to i8*) +; CHECK: %handler = bitcast i8* (i8*, i8*)* @_Z4testv.catch to i8* +; CHECK: %recover = call i8* (...)* @llvm.eh.actions({ i8*, i32 } %tmp, i32 0, i8* %handler, float* %f, i8* bitcast (i8** @_ZTIf to i8*)) +; CHECK: indirectbr i8* %recover, [label %try.cont19] + +lpad: ; preds = %try.cont, %entry + %tmp = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) + catch i8* bitcast (i8** @_ZTIf to i8*) + %tmp1 = extractvalue { i8*, i32 } %tmp, 0 + store i8* %tmp1, i8** %exn.slot + %tmp2 = extractvalue { i8*, i32 } %tmp, 1 + store i32 %tmp2, i32* %ehselector.slot + br label %catch.dispatch11 + +; CHECK: lpad1: ; preds = %invoke.cont4, %invoke.cont +; CHECK: %tmp3 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) +; CHECK: cleanup +; CHECK: catch i8* bitcast (i8** @_ZTIi to i8*) +; CHECK: catch i8* bitcast (i8** @_ZTIf to i8*) +; CHECK: %handler1 = bitcast i8* (i8*, i8*)* @_Z4testv.catch1 to i8* +; CHECK: %handler2 = bitcast void (i8*, i8*)* @_Z4testv.cleanup to i8* +; CHECK: %handler3 = bitcast i8* (i8*, i8*)* @_Z4testv.catch to i8* +; CHECK: %recover4 = call i8* (...)* @llvm.eh.actions({ i8*, i32 } %tmp3, i32 0, i8* %handler1, i32* %i, i8* bitcast (i8** @_ZTIi to i8*), i32 0, i8* %handler2, i8* null, i8* null, i32 0, i8* %handler3, float* %f, i8* bitcast (i8** @_ZTIf to i8*)) +; CHECK: indirectbr i8* %recover4, [label %try.cont, label %try.cont19] + +lpad1: ; preds = %invoke.cont4, %invoke.cont + %tmp3 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) + cleanup + catch i8* bitcast (i8** @_ZTIi to i8*) + catch i8* bitcast (i8** @_ZTIf to i8*) + %tmp4 = extractvalue { i8*, i32 } %tmp3, 0 + store i8* %tmp4, i8** %exn.slot + %tmp5 = extractvalue { i8*, i32 } %tmp3, 1 + store i32 %tmp5, i32* %ehselector.slot + br label %catch.dispatch + +; CHECK: lpad3: ; preds = %invoke.cont2 +; CHECK: %tmp6 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) +; CHECK: cleanup +; CHECK: catch i8* bitcast (i8** @_ZTIi to i8*) +; CHECK: catch i8* bitcast (i8** @_ZTIf to i8*) +; CHECK-NOT: %tmp7 = extractvalue { i8*, i32 } %tmp6, 0 +; CHECK-NOT: store i8* %tmp7, i8** %exn.slot +; CHECK-NOT: %tmp8 = extractvalue { i8*, i32 } %tmp6, 1 +; CHECK-NOT: store i32 %tmp8, i32* %ehselector.slot +; CHECK-NOT: call void @_ZN5InnerD1Ev(%class.Inner* %inner) +; CHECK: %handler5 = bitcast void (i8*, i8*)* @_Z4testv.cleanup2 to i8* +; CHECK: %handler6 = bitcast i8* (i8*, i8*)* @_Z4testv.catch1 to i8* +; CHECK: %handler7 = bitcast void (i8*, i8*)* @_Z4testv.cleanup to i8* +; CHECK: %handler8 = bitcast i8* (i8*, i8*)* @_Z4testv.catch to i8* +; CHECK: %recover9 = call i8* (...)* @llvm.eh.actions({ i8*, i32 } %tmp6, i32 0, i8* %handler5, i8* null, i8* null, i32 0, i8* %handler6, i32* %i, i8* bitcast (i8** @_ZTIi to i8*), i32 0, i8* %handler7, i8* null, i8* null, i32 0, i8* %handler8, float* %f, i8* bitcast (i8** @_ZTIf to i8*)) +; CHECK: indirectbr i8* %recover9, [label %try.cont, label %try.cont19] + +lpad3: ; preds = %invoke.cont2 + %tmp6 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) + cleanup + catch i8* bitcast (i8** @_ZTIi to i8*) + catch i8* bitcast (i8** @_ZTIf to i8*) + %tmp7 = extractvalue { i8*, i32 } %tmp6, 0 + store i8* %tmp7, i8** %exn.slot + %tmp8 = extractvalue { i8*, i32 } %tmp6, 1 + store i32 %tmp8, i32* %ehselector.slot + call void @_ZN5InnerD1Ev(%class.Inner* %inner) + br label %catch.dispatch + +; CHECK-NOT: catch.dispatch: ; preds = %lpad3, %lpad1 +; CHECK-NOT: %sel = load i32, i32* %ehselector.slot +; CHECK-NOT: %tmp9 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*)) #4 +; CHECK-NOT: %matches = icmp eq i32 %sel, %tmp9 +; CHECK-NOT: br i1 %matches, label %catch, label %ehcleanup + +catch.dispatch: ; preds = %lpad3, %lpad1 + %sel = load i32, i32* %ehselector.slot + %tmp9 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*)) #4 + %matches = icmp eq i32 %sel, %tmp9 + br i1 %matches, label %catch, label %ehcleanup + +; CHECK-NOT: catch: ; preds = %catch.dispatch +; CHECK-NOT: %exn = load i8*, i8** %exn.slot +; CHECK-NOT: %i.i8 = bitcast i32* %i to i8* +; CHECK-NOT: call void @llvm.eh.begincatch(i8* %exn, i8* %i.i8) #4 +; CHECK-NOT: %tmp13 = load i32, i32* %i, align 4 +; CHECK-NOT: invoke void @_Z10handle_inti(i32 %tmp13) +; CHECK-NOT: to label %invoke.cont8 unwind label %lpad7 + +catch: ; preds = %catch.dispatch + %exn = load i8*, i8** %exn.slot + %i.i8 = bitcast i32* %i to i8* + call void @llvm.eh.begincatch(i8* %exn, i8* %i.i8) #4 + %tmp13 = load i32, i32* %i, align 4 + invoke void @_Z10handle_inti(i32 %tmp13) + to label %invoke.cont8 unwind label %lpad7 + +invoke.cont8: ; preds = %catch + call void @llvm.eh.endcatch() #4 + br label %try.cont + +try.cont: ; preds = %invoke.cont8, %invoke.cont5 + invoke void @_ZN5OuterD1Ev(%class.Outer* %outer) + to label %invoke.cont9 unwind label %lpad + +; We need to check this here so that we can verify that an identical branch +; instruction is not present later. +; CHECK: invoke.cont9: ; preds = %try.cont +; CHECK: br label %try.cont19 + +invoke.cont9: ; preds = %try.cont + br label %try.cont19 + +; CHECK-NOT: lpad7: ; preds = %catch +; CHECK-NOT: %tmp14 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) +; CHECK-NOT: cleanup +; CHECK-NOT: catch i8* bitcast (i8** @_ZTIf to i8*) +; CHECK-NOT: %tmp15 = extractvalue { i8*, i32 } %tmp14, 0 +; CHECK-NOT: store i8* %tmp15, i8** %exn.slot +; CHECK-NOT: %tmp16 = extractvalue { i8*, i32 } %tmp14, 1 +; CHECK-NOT: store i32 %tmp16, i32* %ehselector.slot +; CHECK-NOT: call void @llvm.eh.endcatch() #4 +; CHECK-NOT: br label %ehcleanup + +lpad7: ; preds = %catch + %tmp14 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) + cleanup + catch i8* bitcast (i8** @_ZTIf to i8*) + %tmp15 = extractvalue { i8*, i32 } %tmp14, 0 + store i8* %tmp15, i8** %exn.slot + %tmp16 = extractvalue { i8*, i32 } %tmp14, 1 + store i32 %tmp16, i32* %ehselector.slot + call void @llvm.eh.endcatch() #4 + br label %ehcleanup + +; CHECK-NOT: ehcleanup: ; preds = %lpad7, %catch.dispatch +; CHECK-NOT: call void @_ZN5OuterD1Ev(%class.Outer* %outer) +; CHECK-NOT: br label %catch.dispatch11 + +ehcleanup: ; preds = %lpad7, %catch.dispatch + call void @_ZN5OuterD1Ev(%class.Outer* %outer) + br label %catch.dispatch11 + +; CHECK-NOT: catch.dispatch11: ; preds = %ehcleanup, %lpad +; CHECK-NOT: %sel12 = load i32, i32* %ehselector.slot +; CHECK-NOT: %tmp17 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIf to i8*)) #4 +; CHECK-NOT: %matches13 = icmp eq i32 %sel12, %tmp17 +; CHECK-NOT: br i1 %matches13, label %catch14, label %eh.resume + +catch.dispatch11: ; preds = %ehcleanup, %lpad + %sel12 = load i32, i32* %ehselector.slot + %tmp17 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIf to i8*)) #4 + %matches13 = icmp eq i32 %sel12, %tmp17 + br i1 %matches13, label %catch14, label %eh.resume + +; CHECK-NOT: catch14: ; preds = %catch.dispatch11 +; CHECK-NOT: %exn15 = load i8*, i8** %exn.slot +; CHECK-NOT: %f.i8 = bitcast float* %f to i8* +; CHECK-NOT: call void @llvm.eh.begincatch(i8* %exn15, i8* %f.i8) #4 +; CHECK-NOT: %tmp21 = load float, float* %f, align 4 +; CHECK-NOT: invoke void @_Z12handle_floatf(float %tmp21) +; CHECK-NOT: to label %invoke.cont17 unwind label %lpad16 + +catch14: ; preds = %catch.dispatch11 + %exn15 = load i8*, i8** %exn.slot + %f.i8 = bitcast float* %f to i8* + call void @llvm.eh.begincatch(i8* %exn15, i8* %f.i8) #4 + %tmp21 = load float, float* %f, align 4 + invoke void @_Z12handle_floatf(float %tmp21) + to label %invoke.cont17 unwind label %lpad16 + +; CHECK-NOT: invoke.cont17: ; preds = %catch14 +; CHECK-NOT: call void @llvm.eh.endcatch() #4 +; CHECK-NOT: br label %try.cont19 + +invoke.cont17: ; preds = %catch14 + call void @llvm.eh.endcatch() #4 + br label %try.cont19 + +try.cont19: ; preds = %invoke.cont17, %invoke.cont9 + call void @_Z4donev() + ret void + +; CHECK-NOT: lpad16: ; preds = %catch14 +; CHECK-NOT: %tmp22 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) +; CHECK-NOT: cleanup +; CHECK-NOT: %tmp23 = extractvalue { i8*, i32 } %tmp22, 0 +; CHECK-NOT: store i8* %tmp23, i8** %exn.slot +; CHECK-NOT: %tmp24 = extractvalue { i8*, i32 } %tmp22, 1 +; CHECK-NOT: store i32 %tmp24, i32* %ehselector.slot +; CHECK-NOT: call void @llvm.eh.endcatch() #4 +; CHECK-NOT: br label %eh.resume + +lpad16: ; preds = %catch14 + %tmp22 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) + cleanup + %tmp23 = extractvalue { i8*, i32 } %tmp22, 0 + store i8* %tmp23, i8** %exn.slot + %tmp24 = extractvalue { i8*, i32 } %tmp22, 1 + store i32 %tmp24, i32* %ehselector.slot + call void @llvm.eh.endcatch() #4 + br label %eh.resume + +; CHECK-NOT: eh.resume: ; preds = %lpad16, %catch.dispatch11 +; CHECK-NOT: %exn20 = load i8*, i8** %exn.slot +; CHECK-NOT: %sel21 = load i32, i32* %ehselector.slot +; CHECK-NOT: %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn20, 0 +; CHECK-NOT: %lpad.val22 = insertvalue { i8*, i32 } %lpad.val, i32 %sel21, 1 +; CHECK-NOT: resume { i8*, i32 } %lpad.val22 + +eh.resume: ; preds = %lpad16, %catch.dispatch11 + %exn20 = load i8*, i8** %exn.slot + %sel21 = load i32, i32* %ehselector.slot + %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn20, 0 + %lpad.val22 = insertvalue { i8*, i32 } %lpad.val, i32 %sel21, 1 + resume { i8*, i32 } %lpad.val22 + +; CHECK: } +} + +; This catch handler should be outlined. +; CHECK: define internal i8* @_Z4testv.catch(i8*, i8*) { +; CHECK: entry: +; CHECK: %eh.alloc = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1) +; CHECK: %eh.data = bitcast i8* %eh.alloc to %struct._Z4testv.ehdata* +; CHECK: %f = getelementptr inbounds %struct._Z4testv.ehdata, %struct._Z4testv.ehdata* %eh.data, i32 0, i32 2 +; CHECK: %tmp21 = load float, float* %f, align 4 +; CHECK: call void @_Z12handle_floatf(float %tmp21) +; CHECK: ret i8* blockaddress(@_Z4testv, %try.cont19) +; CHECK: } + +; This catch handler should be outlined. +; CHECK: define internal i8* @_Z4testv.catch1(i8*, i8*) { +; CHECK: entry: +; CHECK: %eh.alloc = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1) +; CHECK: %eh.data = bitcast i8* %eh.alloc to %struct._Z4testv.ehdata* +; CHECK: %i = getelementptr inbounds %struct._Z4testv.ehdata, %struct._Z4testv.ehdata* %eh.data, i32 0, i32 3 +; CHECK: %tmp13 = load i32, i32* %i, align 4 +; CHECK: invoke void @_Z10handle_inti(i32 %tmp13) +; CHECK: to label %invoke.cont8 unwind label %lpad7 +; +; CHECK: invoke.cont8: ; preds = %entry +; CHECK: ret i8* blockaddress(@_Z4testv, %try.cont) +; +; CHECK: lpad7: ; preds = %entry +; CHECK: %tmp14 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) +; (FIXME) The nested handler body isn't being populated yet. +; CHECK: } + +; This cleanup handler should be outlined. +; CHECK: define internal void @_Z4testv.cleanup(i8*, i8*) { +; CHECK: entry: +; CHECK: %eh.alloc = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1) +; CHECK: %eh.data = bitcast i8* %eh.alloc to %struct._Z4testv.ehdata* +; CHECK: %outer = getelementptr inbounds %struct._Z4testv.ehdata, %struct._Z4testv.ehdata* %eh.data, i32 0, i32 4 +; CHECK: call void @_ZN5OuterD1Ev(%class.Outer* %outer) +; CHECK: ret void +; CHECK: } + +; This cleanup handler should be outlined. +; CHECK: define internal void @_Z4testv.cleanup2(i8*, i8*) { +; CHECK: entry: +; CHECK: %eh.alloc = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1) +; CHECK: %eh.data = bitcast i8* %eh.alloc to %struct._Z4testv.ehdata* +; CHECK: %inner = getelementptr inbounds %struct._Z4testv.ehdata, %struct._Z4testv.ehdata* %eh.data, i32 0, i32 5 +; CHECK: call void @_ZN5InnerD1Ev(%class.Inner* %inner) +; CHECK: ret void +; CHECK: } + + + +declare void @_ZN5OuterC1Ev(%class.Outer*) #1 + +declare i32 @__CxxFrameHandler3(...) + +declare void @_ZN5InnerC1Ev(%class.Inner*) #1 + +declare void @_Z9may_throwv() #1 + +declare void @_ZN5InnerD1Ev(%class.Inner*) #1 + +declare void @llvm.eh.begincatch(i8*, i8*) + +; Function Attrs: nounwind readnone +declare i32 @llvm.eh.typeid.for(i8*) #3 + +declare void @_Z10handle_inti(i32) #1 + +declare void @llvm.eh.endcatch() + +declare void @_ZN5OuterD1Ev(%class.Outer*) #1 + +declare void @_Z12handle_floatf(float) #1 + +declare void @_Z4donev() #1 + +attributes #0 = { uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #2 = { noinline noreturn nounwind } +attributes #3 = { nounwind readnone } +attributes #4 = { nounwind } +attributes #5 = { noreturn nounwind } + +!llvm.ident = !{!0} + +!0 = !{!"clang version 3.7.0 (trunk 226027)"} Index: test/CodeGen/WinEH/cppeh-nonalloca-frame-values.ll =================================================================== --- test/CodeGen/WinEH/cppeh-nonalloca-frame-values.ll +++ test/CodeGen/WinEH/cppeh-nonalloca-frame-values.ll @@ -70,8 +70,8 @@ ; CHECK: %a.reg2mem = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 6 ; CHECK: %a = bitcast i64* %Data to i32* ; CHECK: store i32* %a, i32** %a.reg2mem -; CHECK: %b.reg2mem = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 7 ; CHECK: %b = getelementptr inbounds %struct.SomeData, %struct.SomeData* %tmpcast, i64 0, i32 1 +; CHECK: %b.reg2mem = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 7 ; CHECK: store i32* %b, i32** %b.reg2mem ; CHECK: store i32 0, i32* %NumExceptions.020.reg2mem ; CHECK: store i32 0, i32* %i.019.reg2mem @@ -106,11 +106,11 @@ ; CHECK-NOT: %1 = load i32, i32* %a, align 8, !tbaa !2 ; CHECK-NOT: %add = add nsw i32 %1, %i.019 ; CHECK-NOT: store i32 %add, i32* %a, align 8, !tbaa !2 -; CHECK: %a.reload3 = load volatile i32*, i32** %a.reg2mem -; CHECK: %1 = load i32, i32* %a.reload3, align 8, !tbaa !2 +; CHECK: %a.reload1 = load volatile i32*, i32** %a.reg2mem +; CHECK: %1 = load i32, i32* %a.reload1, align 8, !tbaa !2 ; CHECK: %add = add nsw i32 %1, %i.019.reload -; CHECK: %a.reload2 = load volatile i32*, i32** %a.reg2mem -; CHECK: store i32 %add, i32* %a.reload2, align 8, !tbaa !2 +; CHECK: %a.reload = load volatile i32*, i32** %a.reg2mem +; CHECK: store i32 %add, i32* %a.reload, align 8, !tbaa !2 ; CHECK: br label %try.cont invoke.cont: ; preds = %for.body %1 = load i32, i32* %a, align 8, !tbaa !2 @@ -118,6 +118,17 @@ store i32 %add, i32* %a, align 8, !tbaa !2 br label %try.cont +; CHECK: lpad: ; preds = %for.body +; CHECK: %2 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) +; CHECK: catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*) +; CHECK-NOT: %3 = extractvalue { i8*, i32 } %2, 1 +; CHECK-NOT: %4 = tail call i32 @llvm.eh.typeid.for(i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)) #1 +; CHECK-NOT: %matches = icmp eq i32 %3, %4 +; CHECK-NOT: br i1 %matches, label %catch, label %eh.resume +; CHECK: %handler = bitcast i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch" to i8* +; CHECK: %recover = call i8* (...)* @llvm.eh.actions({ i8*, i32 } %2, i32 0, i8* %handler, i32* %e, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)) +; CHECK: indirectbr i8* %recover, [label %try.cont] + lpad: ; preds = %for.body %2 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*) @@ -154,14 +165,13 @@ tail call void @llvm.eh.endcatch() #1 br label %try.cont -; CHECK: try.cont: ; preds = %if.end, %invoke.cont +; CHECK: try.cont: ; preds = %lpad, %invoke.cont ; CHECK-NOT: %NumExceptions.1 = phi i32 [ %NumExceptions.020, %invoke.cont ], [ %inc, %if.end ] -; CHECK: %NumExceptions.1 = phi i32 [ %NumExceptions.020.reload, %invoke.cont ], [ %inc, %if.end ] -; CHECK: tail call void @"\01?does_not_throw@@YAXH@Z"(i32 %NumExceptions.1) +; CHECK: tail call void @"\01?does_not_throw@@YAXH@Z"(i32 %NumExceptions.020.reload) ; CHECK-NOT: %inc5 = add nuw nsw i32 %i.019, 1 ; CHECK: %inc5 = add nuw nsw i32 %i.019.reload, 1 ; CHECK: %cmp = icmp slt i32 %inc5, 10 -; CHECK: store i32 %NumExceptions.1, i32* %NumExceptions.020.reg2mem +; CHECK: store i32 %NumExceptions.020.reload, i32* %NumExceptions.020.reg2mem ; CHECK: store i32 %inc5, i32* %i.019.reg2mem ; CHECK: br i1 %cmp, label %for.body, label %for.end