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/Analysis/TargetTransformInfo.h" @@ -27,6 +29,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 @@ -45,11 +49,21 @@ // 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) @@ -68,9 +82,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, 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 { @@ -86,15 +108,50 @@ IRBuilder<> Builder; }; +class LandingPadMapper { +public: + LandingPadMapper() : OriginLPad(nullptr) {} + void mapLandingPad(const LandingPadInst *LPad); + + bool mapIfEHPtrLoad(const LoadInst *Load) { + return mapIfEHLoad(Load, EHPtrStores, EHPtrStoreAddrs); + } + bool mapIfSelectorLoad(const LoadInst *Load) { + return mapIfEHLoad(Load, SelectorStores, SelectorStoreAddrs); + } + + bool isLandingPadSpecificInst(const Instruction *Inst) const; + + void remapSelector(ValueToValueMapTy &VMap, Value *MappedValue) const; + +private: + bool mapIfEHLoad(const LoadInst *Load, + SmallVectorImpl &Stores, + SmallVectorImpl &StoreAddrs); + + const LandingPadInst *OriginLPad; + // We will normally only see one of each of these instructions, but + // if more than one occurs for some reason we can handle that. + TinyPtrVector ExtractedEHPtrs; + TinyPtrVector ExtractedSelectors; + + // In optimized code, there will typically be at most one instance of + // each of the following, but in unoptimized IR it is not uncommon + // for the values to be stored, loaded and then stored again. In that + // case we will create a second entry for each store and store address. + SmallVector EHPtrStores; + SmallVector SelectorStores; + SmallVector EHPtrStoreAddrs; + SmallVector SelectorStoreAddrs; +}; + class WinEHCloningDirectorBase : public CloningDirector { public: WinEHCloningDirectorBase(LandingPadInst *LPI, Function *HandlerFn, FrameVarInfoMap &VarInfo) : LPI(LPI), Materializer(HandlerFn, VarInfo), SelectorIDType(Type::getInt32Ty(LPI->getContext())), - Int8PtrType(Type::getInt8PtrTy(LPI->getContext())), - ExtractedEHPtr(nullptr), ExtractedSelector(nullptr), - EHPtrStoreAddr(nullptr), SelectorStoreAddr(nullptr) {} + Int8PtrType(Type::getInt8PtrTy(LPI->getContext())) {} CloningAction handleInstruction(ValueToValueMapTy &VMap, const Instruction *Inst, @@ -109,22 +166,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 { @@ -132,7 +190,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, @@ -142,11 +201,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 { @@ -163,10 +230,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; @@ -258,17 +408,34 @@ 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); + Type *Int32Type = Type::getInt32Ty(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, @@ -276,31 +443,63 @@ 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. - bool Outlined = outlineHandler(Catch, &F, LPad->getClause(Idx), LPad, - FrameVarInfo); - if (Outlined) { - HandlersOutlined = true; - } - } // 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()) { - bool Outlined = - outlineHandler(Cleanup, &F, nullptr, LPad, FrameVarInfo); - if (Outlined) { + LandingPadActions Actions; + mapLandingPadBlocks(LPad, Actions); + + for (ActionHandler *Action : Actions) { + if (Action->hasBeenOutlined()) + continue; + BasicBlock *StartBB = Action->getStartBlock(); + if (outlineHandler(Action, &F, LPad, StartBB, FrameVarInfo)) { HandlersOutlined = true; } + } // End for each Action + + // FIXME: We need a guard against partially outlined functions. + if (!HandlersOutlined) + continue; + + // Replace the landing pad with a new llvm.eh.action based landing pad. + BasicBlock *NewLPadBB = BasicBlock::Create(Context, "lpad", &F, LPadBB); + assert(!isa(LPadBB->begin())); + Instruction *NewLPad = LPad->clone(); + NewLPadBB->getInstList().push_back(NewLPad); + while (!pred_empty(LPadBB)) { + auto *pred = *pred_begin(LPadBB); + InvokeInst *Invoke = cast(pred->getTerminator()); + Invoke->setUnwindDest(NewLPadBB); + } + + // Add a call to describe the actions for this landing pad. + std::vector ActionArgs; + ActionArgs.push_back(NewLPad); + for (ActionHandler *Action : Actions) { + if (auto *CatchAction = dyn_cast(Action)) { + ActionArgs.push_back(ConstantInt::get(Int32Type, 0)); + ActionArgs.push_back(CatchAction->getSelector()); + Value *EHObj = const_cast(CatchAction->getExceptionVar()); + if (EHObj) + ActionArgs.push_back(EHObj); + else + ActionArgs.push_back(ConstantPointerNull::get(Int8PtrType)); + } else { + ActionArgs.push_back(ConstantInt::get(Int32Type, 1)); + } + Constant *HandlerPtr = + ConstantExpr::getBitCast(Action->getOutlinedFunction(), Int8PtrType); + ActionArgs.push_back(HandlerPtr); + } + CallInst *Recover = + CallInst::Create(ActionIntrin, ActionArgs, "recover", NewLPadBB); + + // Add an indirect branch listing possible successors of the catch handlers. + IndirectBrInst *Branch = IndirectBrInst::Create(Recover, 0, NewLPadBB); + for (ActionHandler *Action : Actions) { + if (auto *CatchAction = dyn_cast(Action)) { + for (auto *Target : CatchAction->getReturnTargets()) { + Branch->addDestination(Target); + } + } } } // End for each landingpad @@ -308,15 +507,9 @@ 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); - Module *M = F.getParent(); - LLVMContext &Context = M->getContext(); BasicBlock *Entry = &F.getEntryBlock(); IRBuilder<> Builder(F.getParent()->getContext()); Builder.SetInsertPoint(Entry->getFirstInsertionPt()); @@ -325,8 +518,6 @@ Intrinsic::getDeclaration(M, Intrinsic::frameescape); Function *RecoverFrameFn = Intrinsic::getDeclaration(M, Intrinsic::framerecover); - Type *Int8PtrType = Type::getInt8PtrTy(Context); - Type *Int32Type = Type::getInt32Ty(Context); // Finally, replace all of the temporary allocas for frame variables used in // the outlined handlers with calls to llvm.framerecover. @@ -359,7 +550,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); + } } } @@ -383,8 +587,7 @@ Builder.CreateBitCast(&F, Int8PtrType, ""), &(HandlerFn->getArgumentList().back()), llvm::ConstantInt::get(Int32Type, AllocasToEscape.size() - 1)}; - Value *RecoveredAlloca = - Builder.CreateCall(RecoverFrameFn, RecoverArgs); + Value *RecoveredAlloca = Builder.CreateCall(RecoverFrameFn, RecoverArgs); // Add a pointer bitcast if the alloca wasn't an i8. if (RecoveredAlloca->getType() != TempAlloca->getType()) { RecoveredAlloca->setName(Twine(TempAlloca->getName()) + ".i8"); @@ -396,18 +599,58 @@ RecoveredAlloca->takeName(TempAlloca); delete TempAlloca; } - } // End for each FrameVarInfo entry. + } // End for each FrameVarInfo entry. // Insert 'call void (...)* @llvm.frameescape(...)' at the end of the entry // block. Builder.SetInsertPoint(&F.getEntryBlock().back()); Builder.CreateCall(FrameEscapeFn, AllocasToEscape); + // Clean up the handler action maps we created for this function + DeleteContainerSeconds(CatchHandlerMap); + CatchHandlerMap.clear(); + DeleteContainerSeconds(CleanupHandlerMap); + CleanupHandlerMap.clear(); + 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) { + ICmpInst::Predicate Pred; + BasicBlock *TBB, *FBB; + Value *LHS, *RHS; + + if (!match(BB->getTerminator(), + m_Br(m_ICmp(Pred, m_Value(LHS), m_Value(RHS)), TBB, FBB))) + return false; + + if (!match(LHS, + m_Intrinsic(m_Constant(Selector))) && + !match(RHS, m_Intrinsic(m_Constant(Selector)))) + return false; + + if (Pred == CmpInst::ICMP_EQ) { + CatchHandler = TBB; + NextBB = FBB; + return true; + } + + if (Pred == CmpInst::ICMP_NE) { + CatchHandler = FBB; + NextBB = TBB; + return true; + } + + return false; +} + +bool WinEHPrepare::outlineHandler(ActionHandler *Action, Function *SrcFn, + LandingPadInst *LPad, BasicBlock *StartBB, FrameVarInfoMap &VarInfo) { Module *M = SrcFn->getParent(); LLVMContext &Context = M->getContext(); @@ -418,7 +661,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); @@ -438,7 +681,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 { @@ -447,88 +691,163 @@ ValueToValueMapTy VMap; - // FIXME: Map other values referenced in the filter handler. + Director->mapLandingPadUsers(LPad, VMap); SmallVector Returns; - ClonedCodeInfo InlinedFunctionInfo; + ClonedCodeInfo OutlinedFunctionInfo; - BasicBlock::iterator II = LPad; + // Skip over PHIs and, if applicable, landingpad instructions. + BasicBlock::iterator II = StartBB->getFirstInsertionPt(); - CloneAndPruneIntoFromInst(Handler, SrcFn, ++II, VMap, + CloneAndPruneIntoFromInst(Handler, SrcFn, II, VMap, /*ModuleLevelChanges=*/false, Returns, "", - &InlinedFunctionInfo, Director.get()); + &OutlinedFunctionInfo, Director.get()); // Move all the instructions in the first cloned block into our entry block. BasicBlock *FirstClonedBB = std::next(Function::iterator(Entry)); 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; - } - if (StoredValue == ExtractedSelector) { - SelectorStoreAddr = 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()); + } + } } + } +} - // Any other store just gets cloned. - return CloningDirector::CloneInstruction; +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::mapIfEHLoad(const LoadInst *Load, + SmallVectorImpl &Stores, + SmallVectorImpl &StoreAddrs) { + // 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 : StoreAddrs) { + 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)) { + Stores.push_back(Store); + StoreAddrs.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.mapIfSelectorLoad(Load)) { + VMap[Inst] = ConstantInt::get(SelectorIDType, 1); return CloningDirector::SkipInstruction; } - if (LoadAddr == SelectorStoreAddr) { - VMap[Inst] = ConstantInt::get(SelectorIDType, 1); + if (LPadMapper.mapIfEHPtrLoad(Load)) { + VMap[Inst] = UndefValue::get(Int8PtrType); return CloningDirector::SkipInstruction; } @@ -536,6 +855,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); @@ -560,6 +899,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; } @@ -572,10 +915,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()) @@ -591,8 +933,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 @@ -615,6 +959,12 @@ } CloningDirector::CloningAction +WinEHCatchDirector::handleInvoke(ValueToValueMapTy &VMap, + const InvokeInst *Invoke, BasicBlock *NewBB) { + return CloningDirector::CloneInstruction; +} + +CloningDirector::CloningAction WinEHCatchDirector::handleResume(ValueToValueMapTy &VMap, const ResumeInst *Resume, BasicBlock *NewBB) { // Resume instructions shouldn't be reachable from catch handlers. @@ -646,13 +996,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); @@ -667,8 +1050,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) { @@ -702,3 +1083,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 @@ -393,6 +393,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,12 @@ 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: +; CHECK: invoke void @_Z9may_throwv() +; CHECK: to label %invoke.cont unwind label %[[LPAD_LABEL:lpad[0-9]+]] + ; Function Attrs: uwtable define void @_Z4testv() #0 { entry: @@ -29,6 +35,16 @@ invoke.cont: ; preds = %entry br label %try.cont +; CHECK: [[LPAD_LABEL]]:{{[ ]+}}; preds = %entry +; CHECK: [[LPAD_VAL:\%.+]] = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) +; CHECK: catch i8* null +; CHECK-NOT: extractvalue { i8*, i32 } +; CHECK-NOT: store i8* +; CHECK-NOT: store i32 +; CHECK-NOT: br label %catch +; CHECK: [[RECOVER:\%.+]] = call i8* (...)* @llvm.eh.actions({ i8*, i32 } [[LPAD_VAL]], i32 0, i8* null, i8* null, i8* bitcast (i8* (i8*, i8*)* @_Z4testv.catch 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* null @@ -38,18 +54,24 @@ store i32 %tmp2, i32* %ehselector.slot br label %catch +; CHECK-NOT: catch: + catch: ; preds = %lpad %exn = load i8*, i8** %exn.slot call void @llvm.eh.begincatch(i8* %exn, i8* null) #2 call void @_Z16handle_exceptionv() br label %invoke.cont2 +; CHECK-NOT: invoke.cont2: + invoke.cont2: ; preds = %catch call void @llvm.eh.endcatch() br label %try.cont try.cont: ; preds = %invoke.cont2, %invoke.cont ret void + +; CHECK: } } ; CHECK: define internal i8* @_Z4testv.catch(i8*, i8*) { Index: test/CodeGen/WinEH/cppeh-catch-scalar.ll =================================================================== --- test/CodeGen/WinEH/cppeh-catch-scalar.ll +++ test/CodeGen/WinEH/cppeh-catch-scalar.ll @@ -23,12 +23,10 @@ ; The function entry will be rewritten like this. ; CHECK: define void @_Z4testv() #0 { ; CHECK: entry: -; CHECK: %exn.slot = alloca i8* -; CHECK: %ehselector.slot = alloca i32 -; CHECK: %i = alloca i32, align 4 -; CHECK: call void (...)* @llvm.frameescape(i32* %i) +; CHECK: [[I_PTR:\%.+]] = alloca i32, align 4 +; CHECK: call void (...)* @llvm.frameescape(i32* [[I_PTR]]) ; CHECK: invoke void @_Z9may_throwv() -; CHECK: to label %invoke.cont unwind label %lpad +; CHECK: to label %invoke.cont unwind label %[[LPAD_LABEL:lpad[0-9]+]] ; Function Attrs: uwtable define void @_Z4testv() #0 { @@ -42,6 +40,16 @@ invoke.cont: ; preds = %entry br label %try.cont +; CHECK: [[LPAD_LABEL]]:{{[ ]+}}; preds = %entry +; CHECK: [[LPAD_VAL:\%.+]] = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) +; CHECK: catch i8* bitcast (i8** @_ZTIi to i8*) +; CHECK-NOT: extractvalue { i8*, i32 } +; CHECK-NOT: store i8* +; CHECK-NOT: store i32 +; CHECK-NOT: br label +; CHECK: [[RECOVER:\%.+]] = call i8* (...)* @llvm.eh.actions({ i8*, i32 } [[LPAD_VAL]], i32 0, i8* bitcast (i8** @_ZTIi to i8*), i32* %i, i8* bitcast (i8* (i8*, i8*)* @_Z4testv.catch 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*) @@ -51,12 +59,16 @@ store i32 %tmp2, i32* %ehselector.slot br label %catch.dispatch +; CHECK-NOT: catch-dispatch: + catch.dispatch: ; preds = %lpad %sel = load i32, i32* %ehselector.slot %tmp3 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*)) #3 %matches = icmp eq i32 %sel, %tmp3 br i1 %matches, label %catch, label %eh.resume +; CHECK-NOT: catch: + catch: ; preds = %catch.dispatch %exn11 = load i8*, i8** %exn.slot %i.i8 = bitcast i32* %i to i8* @@ -65,6 +77,8 @@ call void @_Z10handle_inti(i32 %tmp7) br label %invoke.cont2 +; CHECK-NOT: invoke.cont2: + invoke.cont2: ; preds = %catch call void @llvm.eh.endcatch() #3 br label %try.cont @@ -72,20 +86,24 @@ try.cont: ; preds = %invoke.cont2, %invoke.cont ret void +; CHECK-NOT: eh.resume: + eh.resume: ; preds = %catch.dispatch %exn3 = load i8*, i8** %exn.slot %sel4 = load i32, i32* %ehselector.slot %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn3, 0 %lpad.val5 = insertvalue { i8*, i32 } %lpad.val, i32 %sel4, 1 resume { i8*, i32 } %lpad.val5 + +; CHECK: } } ; CHECK: define internal i8* @_Z4testv.catch(i8*, i8*) { ; CHECK: entry: -; CHECK: %i.i81 = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1, i32 0) -; CHECK: %i = bitcast i8* %i.i81 to i32* -; CHECK: %tmp7 = load i32, i32* %i, align 4 -; CHECK: call void @_Z10handle_inti(i32 %tmp7) +; CHECK: [[RECOVER_I:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1, i32 0) +; CHECK: [[I_PTR1:\%.+]] = bitcast i8* [[RECOVER_I]] to i32* +; CHECK: [[TMP:\%.+]] = load i32, i32* [[I_PTR1]], align 4 +; CHECK: call void @_Z10handle_inti(i32 [[TMP]]) ; CHECK: ret i8* blockaddress(@_Z4testv, %try.cont) ; CHECK: } Index: test/CodeGen/WinEH/cppeh-frame-vars.ll =================================================================== --- test/CodeGen/WinEH/cppeh-frame-vars.ll +++ test/CodeGen/WinEH/cppeh-frame-vars.ll @@ -49,18 +49,16 @@ ; The function entry should be rewritten like this. ; CHECK: define void @"\01?test@@YAXXZ"() #0 { ; CHECK: entry: -; CHECK: %NumExceptions = alloca i32, align 4 -; CHECK: %ExceptionVal = alloca [10 x i32], align 16 -; CHECK: %Data = alloca %struct.SomeData, align 4 -; CHECK: %i = alloca i32, align 4 -; CHECK: %exn.slot = alloca i8* -; CHECK: %ehselector.slot = alloca i32 -; CHECK: %e = alloca i32, align 4 -; CHECK: store i32 0, i32* %NumExceptions, align 4 -; CHECK: %tmp = bitcast %struct.SomeData* %Data to i8* -; CHECK: call void @llvm.memset(i8* %tmp, i8 0, i64 8, i32 4, i1 false) -; CHECK: store i32 0, i32* %i, align 4 -; CHECK: call void (...)* @llvm.frameescape(i32* %e, i32* %NumExceptions, [10 x i32]* %ExceptionVal, i32* %i, %struct.SomeData* %Data) +; CHECK: [[NUMEXCEPTIONS_PTR:\%.+]] = alloca i32, align 4 +; CHECK: [[EXCEPTIONVAL_PTR:\%.+]] = alloca [10 x i32], align 16 +; CHECK: [[DATA_PTR:\%.+]] = alloca %struct.SomeData, align 4 +; CHECK: [[I_PTR:\%.+]] = alloca i32, align 4 +; CHECK: [[E_PTR:\%.+]] = alloca i32, align 4 +; CHECK: store i32 0, i32* [[NUMEXCEPTIONS_PTR]], align 4 +; CHECK: [[TMP:\%.+]] = bitcast %struct.SomeData* [[DATA_PTR]] to i8* +; CHECK: call void @llvm.memset(i8* [[TMP]], i8 0, i64 8, i32 4, i1 false) +; CHECK: store i32 0, i32* [[I_PTR]], align 4 +; CHECK: call void (...)* @llvm.frameescape(i32* [[E_PTR]], i32* [[NUMEXCEPTIONS_PTR]], [10 x i32]* [[EXCEPTIONVAL_PTR]], i32* [[I_PTR]], %struct.SomeData* [[DATA_PTR]]) ; CHECK: br label %for.cond ; Function Attrs: uwtable @@ -84,6 +82,10 @@ %cmp = icmp slt i32 %tmp1, 10 br i1 %cmp, label %for.body, label %for.end +; CHECK: for.body: +; CHECK: invoke void @"\01?may_throw@@YAXXZ"() +; CHECK: to label %invoke.cont unwind label %[[LPAD_LABEL:lpad[0-9]+]] + for.body: ; preds = %for.cond invoke void @"\01?may_throw@@YAXXZ"() to label %invoke.cont unwind label %lpad @@ -96,6 +98,16 @@ store i32 %add, i32* %a, align 4 br label %try.cont +; CHECK: [[LPAD_LABEL]]:{{[ ]+}}; preds = %for.body +; CHECK: [[LPAD_VAL:\%.+]] = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) +; CHECK: catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*) +; CHECK-NOT: extractvalue { i8*, i32 } +; CHECK-NOT: store i8* +; CHECK-NOT: store i32 +; CHECK-NOT: br label +; CHECK: [[RECOVER:\%.+]] = call i8* (...)* @llvm.eh.actions({ i8*, i32 } [[LPAD_VAL]], i32 0, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*), i32* %e, i8* bitcast (i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch" 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*) @@ -105,12 +117,16 @@ store i32 %tmp6, i32* %ehselector.slot br label %catch.dispatch +; CHECK-NOT: catch.dispatch: + catch.dispatch: ; preds = %lpad %sel = load i32, i32* %ehselector.slot %tmp7 = call i32 @llvm.eh.typeid.for(i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)) #1 %matches = icmp eq i32 %sel, %tmp7 br i1 %matches, label %catch, label %eh.resume +; CHECK-NOT: catch: + catch: ; preds = %catch.dispatch %exn = load i8*, i8** %exn.slot %e.i8 = bitcast i32* %e to i8* @@ -128,6 +144,8 @@ %cmp1 = icmp eq i32 %tmp14, %tmp15 br i1 %cmp1, label %if.then, label %if.else +; CHECK-NOT: if.then: + if.then: ; preds = %catch %tmp16 = load i32, i32* %e, align 4 %b = getelementptr inbounds %struct.SomeData, %struct.SomeData* %Data, i32 0, i32 1 @@ -136,6 +154,8 @@ store i32 %add2, i32* %b, align 4 br label %if.end +; CHECK-NOT: if.else: + if.else: ; preds = %catch %tmp18 = load i32, i32* %e, align 4 %a3 = getelementptr inbounds %struct.SomeData, %struct.SomeData* %Data, i32 0, i32 0 @@ -144,6 +164,8 @@ store i32 %add4, i32* %a3, align 4 br label %if.end +; CHECK-NOT: if.end: + if.end: ; preds = %if.else, %if.then call void @llvm.eh.endcatch() #1 br label %try.cont @@ -165,54 +187,58 @@ call void @"\01?dump@@YAXPEAHHAEAUSomeData@@@Z"(i32* %arraydecay, i32 %tmp22, %struct.SomeData* dereferenceable(8) %Data) ret void +; CHECK-NOT: eh.resume: + eh.resume: ; preds = %catch.dispatch %exn6 = load i8*, i8** %exn.slot %sel7 = load i32, i32* %ehselector.slot %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn6, 0 %lpad.val8 = insertvalue { i8*, i32 } %lpad.val, i32 %sel7, 1 resume { i8*, i32 } %lpad.val8 + +; CHECK: } } ; The following catch handler should be outlined. ; CHECK-LABEL: define internal i8* @"\01?test@@YAXXZ.catch"(i8*, i8*) { ; CHECK: entry: -; CHECK: %e.i81 = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 0) -; CHECK: %e = bitcast i8* %e.i81 to i32* -; CHECK: %NumExceptions.i8 = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 1) -; CHECK: %NumExceptions = bitcast i8* %NumExceptions.i8 to i32* -; CHECK: %ExceptionVal.i8 = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 2) -; CHECK: %ExceptionVal = bitcast i8* %ExceptionVal.i8 to [10 x i32]* -; CHECK: %i.i8 = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 3) -; CHECK: %i = bitcast i8* %i.i8 to i32* -; CHECK: %Data.i8 = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 4) -; CHECK: %Data = bitcast i8* %Data.i8 to %struct.SomeData* -; CHECK: %tmp11 = load i32, i32* %e, align 4 -; CHECK: %tmp12 = load i32, i32* %NumExceptions, align 4 -; CHECK: %idxprom = sext i32 %tmp12 to i64 -; CHECK: %arrayidx = getelementptr inbounds [10 x i32], [10 x i32]* %ExceptionVal, i32 0, i64 %idxprom -; CHECK: store i32 %tmp11, i32* %arrayidx, align 4 -; CHECK: %tmp13 = load i32, i32* %NumExceptions, align 4 -; CHECK: %inc = add nsw i32 %tmp13, 1 -; CHECK: store i32 %inc, i32* %NumExceptions, align 4 -; CHECK: %tmp14 = load i32, i32* %e, align 4 -; CHECK: %tmp15 = load i32, i32* %i, align 4 -; CHECK: %cmp1 = icmp eq i32 %tmp14, %tmp15 -; CHECK: br i1 %cmp1, label %if.then, label %if.else +; CHECK: [[RECOVER_E:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 0) +; CHECK: [[E_PTR1:\%.+]] = bitcast i8* [[RECOVER_E]] to i32* +; CHECK: [[RECOVER_NUMEXCEPTIONS:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 1) +; CHECK: [[NUMEXCEPTIONS_PTR1:\%.+]] = bitcast i8* [[RECOVER_NUMEXCEPTIONS]] to i32* +; CHECK: [[RECOVER_EXCEPTIONVAL:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 2) +; CHECK: [[EXCEPTIONVAL_PTR1:\%.+]] = bitcast i8* [[RECOVER_EXCEPTIONVAL]] to [10 x i32]* +; CHECK: [[RECOVER_I:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 3) +; CHECK: [[I_PTR1:\%.+]] = bitcast i8* [[RECOVER_I]] to i32* +; CHECK: [[RECOVER_DATA:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 4) +; CHECK: [[DATA_PTR1:\%.+]] = bitcast i8* [[RECOVER_DATA]] to %struct.SomeData* +; CHECK: [[TMP:\%.+]] = load i32, i32* [[E_PTR1]], align 4 +; CHECK: [[TMP1:\%.+]] = load i32, i32* [[NUMEXCEPTIONS_PTR]], align 4 +; CHECK: [[IDXPROM:\%.+]] = sext i32 [[TMP1]] to i64 +; CHECK: [[ARRAYIDX:\%.+]] = getelementptr inbounds [10 x i32], [10 x i32]* [[EXCEPTIONVAL_PTR1]], i32 0, i64 [[IDXPROM]] +; CHECK: store i32 [[TMP]], i32* [[ARRAYIDX]], align 4 +; CHECK: [[TMP2:\%.+]] = load i32, i32* [[NUMEXCEPTIONS_PTR1]], align 4 +; CHECK: [[INC:\%.+]] = add nsw i32 [[TMP2]], 1 +; CHECK: store i32 [[INC]], i32* [[NUMEXCEPTIONS_PTR]], align 4 +; CHECK: [[TMP3:\%.+]] = load i32, i32* [[E_PTR1]], align 4 +; CHECK: [[TMP4:\%.+]] = load i32, i32* [[I_PTR1]], align 4 +; CHECK: [[CMP:\%.+]] = icmp eq i32 [[TMP3]], [[TMP4]] +; CHECK: br i1 [[CMP]], label %if.then, label %if.else ; ; CHECK: if.then: ; preds = %entry -; CHECK: %tmp16 = load i32, i32* %e, align 4 -; CHECK: %b = getelementptr inbounds %struct.SomeData, %struct.SomeData* %Data, i32 0, i32 1 -; CHECK: %tmp17 = load i32, i32* %b, align 4 -; CHECK: %add2 = add nsw i32 %tmp17, %tmp16 -; CHECK: store i32 %add2, i32* %b, align 4 +; CHECK: [[TMP5:\%.+]] = load i32, i32* [[E_PTR1]], align 4 +; CHECK: [[B_PTR:\%.+]] = getelementptr inbounds %struct.SomeData, %struct.SomeData* [[DATA_PTR1]], i32 0, i32 1 +; CHECK: [[TMP6:\%.+]] = load i32, i32* [[B_PTR]], align 4 +; CHECK: %add2 = add nsw i32 [[TMP6]], [[TMP5]] +; CHECK: store i32 [[ADD:\%.+]], i32* [[B_PTR]], align 4 ; CHECK: br label %if.end ; ; CHECK: if.else: ; preds = %entry -; CHECK: %tmp18 = load i32, i32* %e, align 4 -; CHECK: %a3 = getelementptr inbounds %struct.SomeData, %struct.SomeData* %Data, i32 0, i32 0 -; CHECK: %tmp19 = load i32, i32* %a3, align 4 -; CHECK: %add4 = add nsw i32 %tmp19, %tmp18 -; CHECK: store i32 %add4, i32* %a3, align 4 +; CHECK: [[TMP7:\%.+]] = load i32, i32* %e, align 4 +; CHECK: [[A3:\%.+]] = getelementptr inbounds %struct.SomeData, %struct.SomeData* %Data, i32 0, i32 0 +; CHECK: [[TMP8:\%.+]] = load i32, i32* %a3, align 4 +; CHECK: [[ADD1:\%.+]] = add nsw i32 [[TMP8]], [[TMP7]] +; CHECK: store i32 [[ADD1]], i32* [[A3]], align 4 ; CHECK: br label %if.end ; ; CHECK: if.end: ; preds = %if.else, %if.then @@ -220,10 +246,6 @@ ; CHECK: } - - - - ; Function Attrs: nounwind declare void @llvm.memset(i8* nocapture, i8, i64, i32, i1) #1 Index: test/CodeGen/WinEH/cppeh-inalloca.ll =================================================================== --- test/CodeGen/WinEH/cppeh-inalloca.ll +++ test/CodeGen/WinEH/cppeh-inalloca.ll @@ -38,17 +38,15 @@ ; The function entry should be rewritten like this. ; CHECK: define i32 @"\01?test@@YAHUA@@@Z"(<{ %struct.A }>* inalloca) #0 { ; CHECK: entry: -; CHECK: %.tmp.reg2mem = alloca <{ %struct.A }>* -; CHECK: %.tmp = select i1 true, <{ %struct.A }>* %0, <{ %struct.A }>* undef -; CHECK: store <{ %struct.A }>* %.tmp, <{ %struct.A }>** %.tmp.reg2mem -; CHECK: %retval = alloca i32, align 4 -; CHECK: %exn.slot = alloca i8* -; CHECK: %ehselector.slot = alloca i32 -; CHECK: %e = alloca i32, align 4 -; CHECK: %cleanup.dest.slot = alloca i32 -; CHECK: call void (...)* @llvm.frameescape(i32* %e, <{ %struct.A }>** %.tmp.reg2mem, i32* %retval, i32* %cleanup.dest.slot) +; CHECK: [[TMP_REGMEM:\%.+]] = alloca <{ %struct.A }>* +; CHECK: [[TMP:\%.+]] = select i1 true, <{ %struct.A }>* %0, <{ %struct.A }>* undef +; CHECK: store <{ %struct.A }>* [[TMP]], <{ %struct.A }>** [[TMP_REGMEM]] +; CHECK: [[RETVAL:\%.+]] = alloca i32, align 4 +; CHECK: [[E_PTR:\%.+]] = alloca i32, align 4 +; CHECK: [[CLEANUP_SLOT:\%.+]] = alloca i32 +; CHECK: call void (...)* @llvm.frameescape(i32* %e, <{ %struct.A }>** [[TMP_REGMEM]], i32* [[RETVAL]], i32* [[CLEANUP_SLOT]]) ; CHECK: invoke void @"\01?may_throw@@YAXXZ"() -; CHECK: to label %invoke.cont unwind label %lpad +; CHECK: to label %invoke.cont unwind label %[[LPAD_LABEL:lpad[0-9]*]] define i32 @"\01?test@@YAHUA@@@Z"(<{ %struct.A }>* inalloca) #0 { entry: @@ -63,6 +61,17 @@ invoke.cont: ; preds = %entry br label %try.cont +; CHECK: [[LPAD_LABEL]]:{{[ ]+}}; preds = %entry +; CHECK: [[LPAD_VAL:\%.+]] = 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: extractvalue { i8*, i32 } +; CHECK-NOT: store i8* +; CHECK-NOT: store i32 +; CHECK-NOT: br label %catch.dispatch +; CHECK: [[RECOVER:\%recover.*]] = call i8* (...)* @llvm.eh.actions({ i8*, i32 } [[LPAD_VAL]], i32 0, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*), i32* %e, i8* bitcast (i8* (i8*, i8*)* @"\01?test@@YAHUA@@@Z.catch" to i8*), i32 1, i8* bitcast (void (i8*, i8*)* @"\01?test@@YAHUA@@@Z.cleanup" to i8*)) +; CHECK: indirectbr i8* [[RECOVER]], [label %cleanup] + lpad: ; preds = %entry %1 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) cleanup @@ -73,12 +82,16 @@ store i32 %3, i32* %ehselector.slot br label %catch.dispatch +; CHECK-NOT: catch.dispatch: + catch.dispatch: ; preds = %lpad %sel = load i32, i32* %ehselector.slot %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)) #3 %matches = icmp eq i32 %sel, %4 br i1 %matches, label %catch, label %ehcleanup +; CHECK-NOT: catch: + catch: ; preds = %catch.dispatch %exn = load i8*, i8** %exn.slot %e.i8 = bitcast i32* %e to i8* @@ -99,13 +112,13 @@ br label %cleanup ; The cleanup block should be re-written like this. -; CHECK: cleanup: ; preds = %try.cont, %catch +; CHECK: cleanup:{{[ ]+}}; preds = %[[LPAD_LABEL]], %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: call x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* %a2) #2 -; CHECK: %tmp10 = load i32, i32* %retval -; CHECK: ret i32 %tmp10 +; CHECK: [[TMP_RELOAD:\%.+]] = load volatile <{ %struct.A }>*, <{ %struct.A }>** [[TMP_REGMEM]] +; 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]]) +; CHECK: [[TMP1:\%.+]] = load i32, i32* [[RETVAL]] +; CHECK: ret i32 [[TMP1]] cleanup: ; preds = %try.cont, %catch %a2 = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* %0, i32 0, i32 0 @@ -113,42 +126,59 @@ %tmp10 = load i32, i32* %retval ret i32 %tmp10 +; CHECK-NOT: ehcleanup: + ehcleanup: ; preds = %catch.dispatch %a3 = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* %0, i32 0, i32 0 call x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* %a3) #3 br label %eh.resume +; CHECK-NOT: eh.resume: + eh.resume: ; preds = %ehcleanup %exn2 = load i8*, i8** %exn.slot %sel3 = load i32, i32* %ehselector.slot %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn2, 0 %lpad.val4 = insertvalue { i8*, i32 } %lpad.val, i32 %sel3, 1 resume { i8*, i32 } %lpad.val4 + +; CHECK: } } ; The following catch handler should be outlined. ; CHECK: define internal i8* @"\01?test@@YAHUA@@@Z.catch"(i8*, i8*) { ; CHECK: entry: -; CHECK: %e.i81 = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 0) -; CHECK: %e = bitcast i8* %e.i81 to i32* -; CHECK: %eh.temp.alloca.i8 = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 1) -; CHECK: %eh.temp.alloca = bitcast i8* %eh.temp.alloca.i8 to <{ %struct.A }>** -; CHECK: %.reload = load <{ %struct.A }>*, <{ %struct.A }>** %eh.temp.alloca -; CHECK: %retval.i8 = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 2) -; CHECK: %retval = bitcast i8* %retval.i8 to i32* -; CHECK: %cleanup.dest.slot.i8 = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 3) -; CHECK: %cleanup.dest.slot = bitcast i8* %cleanup.dest.slot.i8 to i32* -; CHECK: %e.i8 = bitcast i32* %e to i8* -; CHECK: %a = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* %.reload, i32 0, i32 0 -; CHECK: %a1 = getelementptr inbounds %struct.A, %struct.A* %a, i32 0, i32 0 -; CHECK: %tmp8 = load i32, i32* %a1, align 4 -; CHECK: %tmp9 = load i32, i32* %e, align 4 -; CHECK: %add = add nsw i32 %tmp8, %tmp9 -; CHECK: store i32 %add, i32* %retval -; CHECK: store i32 1, i32* %cleanup.dest.slot +; CHECK: [[RECOVER_E:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 0) +; CHECK: [[E_PTR:\%.+]] = bitcast i8* [[RECOVER_E]] to i32* +; CHECK: [[RECOVER_EH_TEMP:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 1) +; CHECK: [[EH_TEMP:\%.+]] = bitcast i8* [[RECOVER_EH_TEMP]] to <{ %struct.A }>** +; CHECK: [[TMP_RELOAD:\%.+]] = load <{ %struct.A }>*, <{ %struct.A }>** [[EH_TEMP]] +; CHECK: [[RECOVER_RETVAL:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 2) +; CHECK: [[RETVAL1:\%.+]] = bitcast i8* [[RECOVER_RETVAL]] to i32* +; CHECK: [[RECOVER_CLEANUPSLOT:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 3) +; CHECK: [[CLEANUPSLOT1:\%.+]] = bitcast i8* [[RECOVER_CLEANUPSLOT]] to i32* +; CHECK: [[E_I8PTR:\%.+]] = bitcast i32* [[E_PTR]] to i8* +; CHECK: [[RECOVER_A:\%.+]] = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* [[TMP_RELOAD]], i32 0, i32 0 +; CHECK: [[A1:\%.+]] = getelementptr inbounds %struct.A, %struct.A* [[RECOVER_A]], i32 0, i32 0 +; CHECK: [[TMP2:\%.+]] = load i32, i32* [[A1]], align 4 +; CHECK: [[TMP3:\%.+]] = load i32, i32* [[E_PTR]], align 4 +; CHECK: [[ADD:\%.+]] = add nsw i32 [[TMP2]], [[TMP3]] +; CHECK: store i32 [[ADD]], i32* [[RETVAL1]] +; CHECK: store i32 1, i32* [[CLEANUPSLOT1]] ; 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: [[RECOVER_EH_TEMP1:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 1) +; CHECK: [[EH_TEMP1:\%.+]] = bitcast i8* [[RECOVER_EH_TEMP]] to <{ %struct.A }>** +; CHECK: [[TMP_RELOAD1:\%.+]] = load <{ %struct.A }>*, <{ %struct.A }>** [[EH_TEMP1]] +; CHECK: [[A3:\%.+]] = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* [[TMP_RELOAD1]], i32 0, i32 0 +; CHECK: call x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* [[A3]]) +; CHECK: ret void +; CHECK: } + declare void @"\01?may_throw@@YAXXZ"() #0 declare i32 @__CxxFrameHandler3(...) Index: test/CodeGen/WinEH/cppeh-min-unwind.ll =================================================================== --- test/CodeGen/WinEH/cppeh-min-unwind.ll +++ test/CodeGen/WinEH/cppeh-min-unwind.ll @@ -23,11 +23,11 @@ ; The function entry should be rewritten like this. ; CHECK: define void @_Z4testv() #0 { ; CHECK: entry: -; CHECK: %obj = alloca %class.SomeClass, align 4 -; CHECK: call void @_ZN9SomeClassC1Ev(%class.SomeClass* %obj) -; CHECK: call void (...)* @llvm.frameescape(%class.SomeClass* %obj) +; CHECK: [[OBJ_PTR:\%.+]] = alloca %class.SomeClass, align 4 +; CHECK: call void @_ZN9SomeClassC1Ev(%class.SomeClass* [[OBJ_PTR]]) +; CHECK: call void (...)* @llvm.frameescape(%class.SomeClass* [[OBJ_PTR]]) ; CHECK: invoke void @_Z9may_throwv() -; CHECK: to label %invoke.cont unwind label %lpad +; CHECK: to label %invoke.cont unwind label %[[LPAD_LABEL:lpad[0-9]+]] ; Function Attrs: uwtable define void @_Z4testv() #0 { @@ -43,6 +43,17 @@ call void @_ZN9SomeClassD1Ev(%class.SomeClass* %obj) ret void +; CHECK: [[LPAD_LABEL]]:{{[ ]+}}; preds = %entry +; CHECK: [[LPAD_VAL:\%.+]] = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) +; CHECK: cleanup +; CHECK-NOT: extractvalue { i8*, i32 } +; CHECK-NOT: store i8* +; CHECK-NOT: store i32 +; CHECK-NOT: call void @_ZN9SomeClassD1Ev +; CHECK-NOT: br label %eh.resume +; CHECK: [[RECOVER:\%.+]] = call i8* (...)* @llvm.eh.actions({ i8*, i32 } [[LPAD_VAL]], i32 1, i8* bitcast (void (i8*, i8*)* @_Z4testv.cleanup to i8*)) +; CHECK: indirectbr i8* [[RECOVER]], [] + lpad: ; preds = %entry %tmp = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) cleanup @@ -53,20 +64,24 @@ call void @_ZN9SomeClassD1Ev(%class.SomeClass* %obj) br label %eh.resume +; CHECK-NOT: eh.resume: + eh.resume: ; preds = %lpad %exn = load i8*, i8** %exn.slot %sel = load i32, i32* %ehselector.slot %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn, 0 %lpad.val2 = insertvalue { i8*, i32 } %lpad.val, i32 %sel, 1 resume { i8*, i32 } %lpad.val2 + +; CHECK: } } ; This cleanup handler should be outlined. ; CHECK: define internal void @_Z4testv.cleanup(i8*, i8*) { ; CHECK: entry: -; CHECK: %obj.i8 = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1, i32 0) -; CHECK: %obj = bitcast i8* %obj.i8 to %class.SomeClass* -; CHECK: call void @_ZN9SomeClassD1Ev(%class.SomeClass* %obj) +; CHECK: [[RECOVER_OBJ:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1, i32 0) +; CHECK: [[OBJ_PTR1:\%.+]] = bitcast i8* [[RECOVER_OBJ]] to %class.SomeClass* +; CHECK: call void @_ZN9SomeClassD1Ev(%class.SomeClass* [[OBJ_PTR1]]) ; CHECK: ret void ; CHECK: } 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,321 @@ +; 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" + +%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: %outer = alloca %class.Outer, align 1 +; CHECK: %inner = alloca %class.Inner, align 1 +; CHECK: %i = alloca i32, align 4 +; CHECK: %f = alloca float, align 4 +; CHECK: call void (...)* @llvm.frameescape(float* %f, i32* %i, %class.Outer* %outer, %class.Inner* %inner) +; CHECK: invoke void @_ZN5OuterC1Ev(%class.Outer* %outer) +; CHECK: to label %invoke.cont unwind label %[[LPAD_LABEL:lpad[0-9]*]] + +; 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 + +; CHECK: invoke.cont: +; CHECK: invoke void @_ZN5InnerC1Ev(%class.Inner* %inner) +; CHECK: to label %invoke.cont2 unwind label %[[LPAD1_LABEL:lpad[0-9]*]] + +invoke.cont: ; preds = %entry + invoke void @_ZN5InnerC1Ev(%class.Inner* %inner) + to label %invoke.cont2 unwind label %lpad1 + +; CHECK: invoke.cont2: +; CHECK: invoke void @_Z9may_throwv() +; CHECK: to label %invoke.cont4 unwind label %[[LPAD3_LABEL:lpad[0-9]*]] + +invoke.cont2: ; preds = %invoke.cont + invoke void @_Z9may_throwv() + to label %invoke.cont4 unwind label %lpad3 + +; CHECK: invoke.cont4: +; CHECK: invoke void @_ZN5InnerD1Ev(%class.Inner* %inner) +; CHECK: to label %invoke.cont5 unwind label %[[LPAD1_LABEL]] + +invoke.cont4: ; preds = %invoke.cont2 + invoke void @_ZN5InnerD1Ev(%class.Inner* %inner) + to label %invoke.cont5 unwind label %lpad1 + +; CHECK: invoke.cont5: +; CHECK: br label %try.cont + +invoke.cont5: ; preds = %invoke.cont4 + br label %try.cont + +; CHECK: [[LPAD_LABEL]]: +; CHECK: [[LPAD_VAL:\%.+]] = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) +; CHECK: catch i8* bitcast (i8** @_ZTIf to i8*) +; CHECK: [[RECOVER:\%.+]] = call i8* (...)* @llvm.eh.actions({ i8*, i32 } [[LPAD_VAL]], i32 0, i8* bitcast (i8** @_ZTIf to i8*), float* %f, i8* bitcast (i8* (i8*, i8*)* @_Z4testv.catch 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_LABEL]]: +; CHECK: [[LPAD1_VAL:\%.+]] = 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: [[RECOVER1:\%.+]] = call i8* (...)* @llvm.eh.actions({ i8*, i32 } [[LPAD1_VAL]], i32 0, i8* bitcast (i8** @_ZTIi to i8*), i32* %i, i8* bitcast (i8* (i8*, i8*)* @_Z4testv.catch1 to i8*), i32 1, i8* bitcast (void (i8*, i8*)* @_Z4testv.cleanup to i8*), i32 0, i8* bitcast (i8** @_ZTIf to i8*), float* %f, i8* bitcast (i8* (i8*, i8*)* @_Z4testv.catch to i8*)) +; CHECK: indirectbr i8* [[RECOVER1]], [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_LABEL]]: +; CHECK: [[LPAD3_VAL:\%.+]] = 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: extractvalue { i8*, i32 } +; CHECK-NOT: store i8* +; CHECK-NOT: store i32 +; CHECK-NOT: call void @_ZN5InnerD1Ev(%class.Inner* %inner) +; CHECK: [[RECOVER3:\%.+]] = call i8* (...)* @llvm.eh.actions({ i8*, i32 } [[LPAD3_VAL]], i32 1, i8* bitcast (void (i8*, i8*)* @_Z4testv.cleanup2 to i8*), i32 0, i8* bitcast (i8** @_ZTIi to i8*), i32* %i, i8* bitcast (i8* (i8*, i8*)* @_Z4testv.catch1 to i8*), i32 1, i8* bitcast (void (i8*, i8*)* @_Z4testv.cleanup to i8*), i32 0, i8* bitcast (i8** @_ZTIf to i8*), float* %f, i8* bitcast (i8* (i8*, i8*)* @_Z4testv.catch to i8*)) +; CHECK: indirectbr i8* [[RECOVER3]], [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: + +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: + +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 + +; CHECK-NOT: invoke.cont8: + +invoke.cont8: ; preds = %catch + call void @llvm.eh.endcatch() #4 + br label %try.cont + +; CHECK: try.cont: +; CHECK: invoke void @_ZN5OuterD1Ev(%class.Outer* %outer) +; CHECK: to label %invoke.cont9 unwind label %[[LPAD_LABEL]] + +try.cont: ; preds = %invoke.cont8, %invoke.cont5 + invoke void @_ZN5OuterD1Ev(%class.Outer* %outer) + to label %invoke.cont9 unwind label %lpad + +invoke.cont9: ; preds = %try.cont + br label %try.cont19 + +; CHECK-NOT: lpad7: + +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 + +ehcleanup: ; preds = %lpad7, %catch.dispatch + call void @_ZN5OuterD1Ev(%class.Outer* %outer) + br label %catch.dispatch11 + +; CHECK-NOT: catch.dispatch11: + +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: + +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 + call void @_Z12handle_floatf(float %tmp21) + call void @llvm.eh.endcatch() #4 + br label %try.cont19 + +try.cont19: ; preds = %catch14, %invoke.cont9 + call void @_Z4donev() + ret void + +; CHECK-NOT: eh.resume: + +eh.resume: ; preds = %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: [[RECOVER_F:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1, i32 0) +; CHECK: [[F_PTR:\%.+]] = bitcast i8* [[RECOVER_F]] to float* +; CHECK: [[TMP:\%.+]] = load float, float* [[F_PTR]], align 4 +; CHECK: call void @_Z12handle_floatf(float [[TMP]]) +; 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: [[RECOVER_I:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1, i32 1) +; CHECK: [[I_PTR:\%.+]] = bitcast i8* [[RECOVER_I]] to i32* +; CHECK: [[TMP1:\%.+]] = load i32, i32* [[I_PTR]], align 4 +; CHECK: invoke void @_Z10handle_inti(i32 [[TMP1]]) +; CHECK: to label %invoke.cont8 unwind label %[[LPAD7_LABEL:lpad[0-9]*]] +; +; CHECK: invoke.cont8: ; preds = %entry +; CHECK: ret i8* blockaddress(@_Z4testv, %try.cont) +; +; CHECK: [[LPAD7_LABEL]]:{{[ ]+}}; preds = %entry +; CHECK: [[LPAD7_VAL:\%.+]] = 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: [[RECOVER_OUTER:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1, i32 2) +; CHECK: [[OUTER_PTR:\%.+]] = bitcast i8* [[RECOVER_OUTER]] to %class.Outer* +; CHECK: call void @_ZN5OuterD1Ev(%class.Outer* [[OUTER_PTR]]) +; CHECK: ret void +; CHECK: } + +; This cleanup handler should be outlined. +; CHECK: define internal void @_Z4testv.cleanup2(i8*, i8*) { +; CHECK: entry: +; CHECK: [[RECOVER_INNER:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1, i32 3) +; CHECK: [[INNER_PTR:\%.+]] = bitcast i8* [[RECOVER_INNER]] to %class.Inner* +; CHECK: call void @_ZN5InnerD1Ev(%class.Inner* [[INNER_PTR]]) +; 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 @@ -53,24 +53,24 @@ ; The function entry should be rewritten like this. ; CHECK: define void @"\01?test@@YAXXZ"() #0 { ; CHECK: entry: -; CHECK: %NumExceptions.020.reg2mem = alloca i32 -; CHECK: %i.019.reg2mem = alloca i32 -; CHECK: %e = alloca i32, align 4 -; CHECK: %ExceptionVal = alloca [10 x i32], align 16 -; CHECK: %Data = alloca i64, align 8 -; CHECK: %tmpcast = bitcast i64* %Data to %struct.SomeData* -; CHECK: %0 = bitcast [10 x i32]* %ExceptionVal to i8* -; CHECK: call void @llvm.lifetime.start(i64 40, i8* %0) #1 -; CHECK: store i64 0, i64* %Data, align 8 -; CHECK: %a.reg2mem = alloca i32* -; CHECK: %a = bitcast i64* %Data to i32* -; CHECK: store i32* %a, i32** %a.reg2mem -; CHECK: %b.reg2mem = alloca i32* -; CHECK: %b = getelementptr inbounds %struct.SomeData, %struct.SomeData* %tmpcast, i64 0, i32 1 -; CHECK: store i32* %b, i32** %b.reg2mem -; CHECK: store i32 0, i32* %NumExceptions.020.reg2mem -; CHECK: store i32 0, i32* %i.019.reg2mem -; CHECK: call void (...)* @llvm.frameescape(i32* %e, i32* %NumExceptions.020.reg2mem, [10 x i32]* %ExceptionVal, i32* %i.019.reg2mem, i32** %a.reg2mem, i32** %b.reg2mem) +; CHECK: [[NUMEXCEPTIONS_REGMEM:\%.+]] = alloca i32 +; CHECK: [[I_REGMEM:\%.+]] = alloca i32 +; CHECK: [[E_PTR:\%.+]] = alloca i32, align 4 +; CHECK: [[EXCEPTIONVAL:\%.+]] = alloca [10 x i32], align 16 +; CHECK: [[DATA_PTR:\%.+]] = alloca i64, align 8 +; CHECK: [[TMPCAST:\%.+]] = bitcast i64* [[DATA_PTR]] to %struct.SomeData* +; CHECK: [[TMP:\%.+]] = bitcast [10 x i32]* [[EXCEPTIONVAL]] to i8* +; CHECK: call void @llvm.lifetime.start(i64 40, i8* [[TMP]]) +; CHECK: store i64 0, i64* [[DATA_PTR]], align 8 +; CHECK: [[A_REGMEM:\%.+]] = alloca i32* +; CHECK: [[A_PTR:\%.+]] = bitcast i64* [[DATA_PTR]] to i32* +; CHECK: store i32* [[A_PTR]], i32** [[A_REGMEM]] +; CHECK: [[B_PTR:\%.+]] = getelementptr inbounds %struct.SomeData, %struct.SomeData* [[TMPCAST]], i64 0, i32 1 +; CHECK: [[B_REGMEM:\%.+]] = alloca i32* +; CHECK: store i32* [[B_PTR]], i32** [[B_REGMEM]] +; CHECK: store i32 0, i32* [[NUMEXCEPTIONS_REGMEM]] +; CHECK: store i32 0, i32* [[I_REGMEM]] +; CHECK: call void (...)* @llvm.frameescape(i32* %e, i32* %NumExceptions.020.reg2mem, [10 x i32]* [[EXCEPTIONVAL]], i32* [[I_REGMEM]], i32** [[A_REGMEM]], i32** [[B_REGMEM]]) ; CHECK: br label %for.body ; Function Attrs: uwtable @@ -88,10 +88,10 @@ br label %for.body ; CHECK: for.body: -; CHECK-NOT: %NumExceptions.020 = phi i32 [ 0, %entry ], [ %NumExceptions.1, %try.cont ] -; CHECK-NOT: %i.019 = phi i32 [ 0, %entry ], [ %inc5, %try.cont ] -; CHECK: %i.019.reload = load i32, i32* %i.019.reg2mem -; CHECK: %NumExceptions.020.reload = load i32, i32* %NumExceptions.020.reg2mem +; CHECK-NOT: phi i32 [ 0, %entry ], [ {{\%NumExceptions.*}}, %try.cont ] +; CHECK-NOT: phi i32 [ 0, %entry ], [ {{\%inc.*}}, %try.cont ] +; CHECK: [[I_RELOAD:\%.+]] = load i32, i32* [[I_REGMEM]] +; CHECK: [[NUMEXCEPTIONS_RELOAD:\%.+]] = load i32, i32* [[NUMEXCEPTIONS_REGMEM]] for.body: ; preds = %entry, %try.cont %NumExceptions.020 = phi i32 [ 0, %entry ], [ %NumExceptions.1, %try.cont ] %i.019 = phi i32 [ 0, %entry ], [ %inc5, %try.cont ] @@ -99,14 +99,11 @@ to label %invoke.cont unwind label %lpad ; CHECK: invoke.cont: ; preds = %for.body -; 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: %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_REGMEM]] +; CHECK: [[TMP1:\%.+]] = load i32, i32* [[A_RELOAD]], align 8 +; CHECK: [[ADD:\%.+]] = add nsw i32 [[TMP1]], [[I_RELOAD]] +; CHECK: [[A_RELOAD1:\%.+]] = load volatile i32*, i32** [[A_REGMEM]] +; CHECK: store i32 [[ADD]], i32* [[A_RELOAD1]], align 8 ; CHECK: br label %try.cont invoke.cont: ; preds = %for.body %1 = load i32, i32* %a, align 8, !tbaa !2 @@ -114,6 +111,16 @@ store i32 %add, i32* %a, align 8, !tbaa !2 br label %try.cont +; CHECK: [[LPAD_LABEL:lpad[0-9]*]]:{{[ ]+}}; preds = %for.body +; CHECK: [[LPAD_VAL:\%.+]] = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) +; CHECK: catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*) +; CHECK-NOT: extractvalue { i8*, i32 } +; CHECK-NOT: tail call i32 @llvm.eh.typeid.for(i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)) +; CHECK-NOT: icmp eq i32 +; CHECK-NOT: br i1 +; CHECK: [[RECOVER:\%.+]] = call i8* (...)* @llvm.eh.actions({ i8*, i32 } [[LPAD_VAL]], i32 0, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*), i32* %e, i8* bitcast (i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch" 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*) @@ -122,6 +129,8 @@ %matches = icmp eq i32 %3, %4 br i1 %matches, label %catch, label %eh.resume +; CHECK-NOT: catch: + catch: ; preds = %lpad %5 = extractvalue { i8*, i32 } %2, 0 %e.i8 = bitcast i32* %e to i8* @@ -134,32 +143,36 @@ %cmp1 = icmp eq i32 %tmp8, %i.019 br i1 %cmp1, label %if.then, label %if.else +; CHECK-NOT: if.then: + if.then: ; preds = %catch %tmp9 = load i32, i32* %b, align 4, !tbaa !8 %add2 = add nsw i32 %tmp9, %i.019 store i32 %add2, i32* %b, align 4, !tbaa !8 br label %if.end +; CHECK-NOT: if.else: + if.else: ; preds = %catch %tmp10 = load i32, i32* %a, align 8, !tbaa !2 %add4 = add nsw i32 %tmp10, %tmp8 store i32 %add4, i32* %a, align 8, !tbaa !2 br label %if.end +; CHECK-NOT: if.end: + if.end: ; preds = %if.else, %if.then tail call void @llvm.eh.endcatch() #1 br label %try.cont -; CHECK: try.cont: ; preds = %if.end, %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-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 %inc5, i32* %i.019.reg2mem -; CHECK: br i1 %cmp, label %for.body, label %for.end +; CHECK: try.cont:{{[ ]+}}; preds = %[[LPAD_LABEL]], %invoke.cont +; CHECK-NOT: phi i32 +; CHECK: tail call void @"\01?does_not_throw@@YAXH@Z"(i32 [[NUMEXCEPTIONS_RELOAD]]) +; CHECK: [[INC:\%.+]] = add nuw nsw i32 [[I_RELOAD]], 1 +; CHECK: [[CMP:\%.+]] = icmp slt i32 [[INC]], 10 +; CHECK: store i32 [[NUMEXCEPTIONS_RELOAD]], i32* [[NUMEXCEPTIONS_REGMEM]] +; CHECK: store i32 [[INC]], i32* [[I_REGMEM]] +; CHECK: br i1 [[CMP]], label %for.body, label %for.end try.cont: ; preds = %if.end, %invoke.cont %NumExceptions.1 = phi i32 [ %NumExceptions.020, %invoke.cont ], [ %inc, %if.end ] @@ -183,44 +196,44 @@ ; The following catch handler should be outlined. ; CHECK: define internal i8* @"\01?test@@YAXXZ.catch"(i8*, i8*) { ; CHECK: entry: -; CHECK: %e.i84 = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 0) -; CHECK: %e = bitcast i8* %e.i84 to i32* -; CHECK: %eh.temp.alloca.i8 = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 1) -; CHECK: %eh.temp.alloca = bitcast i8* %eh.temp.alloca.i8 to i32* -; CHECK: %NumExceptions.020.reload = load i32, i32* %eh.temp.alloca -; CHECK: %ExceptionVal.i8 = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 2) -; CHECK: %ExceptionVal = bitcast i8* %ExceptionVal.i8 to [10 x i32]* -; CHECK: %eh.temp.alloca1.i8 = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 3) -; CHECK: %eh.temp.alloca1 = bitcast i8* %eh.temp.alloca1.i8 to i32* -; CHECK: %i.019.reload = load i32, i32* %eh.temp.alloca1 -; CHECK: %eh.temp.alloca2.i8 = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 4) -; CHECK: %eh.temp.alloca2 = bitcast i8* %eh.temp.alloca2.i8 to i32** -; CHECK: %a.reload = load i32*, i32** %eh.temp.alloca2 -; CHECK: %eh.temp.alloca3.i8 = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 5) -; CHECK: %eh.temp.alloca3 = bitcast i8* %eh.temp.alloca3.i8 to i32** -; CHECK: %b.reload = load i32*, i32** %eh.temp.alloca3 -; CHECK: %e.i8 = bitcast i32* %e to i8* -; CHECK: %tmp8 = load i32, i32* %e, align 4, !tbaa !7 -; CHECK: %idxprom = sext i32 %NumExceptions.020.reload to i64 -; CHECK: %arrayidx = getelementptr inbounds [10 x i32], [10 x i32]* %ExceptionVal, i64 0, i64 %idxprom -; CHECK: store i32 %tmp8, i32* %arrayidx, align 4, !tbaa !7 -; CHECK: %inc = add nsw i32 %NumExceptions.020.reload, 1 -; CHECK: %cmp1 = icmp eq i32 %tmp8, %i.019.reload -; CHECK: br i1 %cmp1, label %if.then, label %if.else +; CHECK: [[RECOVER_E:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 0) +; CHECK: [[E_PTR:\%.+]] = bitcast i8* [[RECOVER_E]] to i32* +; CHECK: [[RECOVER_EH_TEMP:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 1) +; CHECK: [[EH_TEMP:\%.+]] = bitcast i8* [[RECOVER_EH_TEMP]] to i32* +; CHECK: [[NUMEXCEPTIONS_RELOAD:\%.+]] = load i32, i32* [[EH_TEMP]] +; CHECK: [[RECOVER_EXCEPTIONVAL:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 2) +; CHECK: [[EXCEPTIONVAL:\%.+]] = bitcast i8* [[RECOVER_EXCEPTIONVAL]] to [10 x i32]* +; CHECK: [[RECOVER_EH_TEMP1:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 3) +; CHECK: [[EH_TEMP1:\%.+]] = bitcast i8* [[RECOVER_EH_TEMP1]] to i32* +; CHECK: [[I_RELOAD:\%.+]] = load i32, i32* [[EH_TEMP1]] +; CHECK: [[RECOVER_EH_TEMP2:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 4) +; CHECK: [[EH_TEMP2:\%.+]] = bitcast i8* [[RECOVER_EH_TEMP2]] to i32** +; CHECK: [[A_RELOAD:\%.+]] = load i32*, i32** [[EH_TEMP2]] +; CHECK: [[RECOVER_EH_TEMP3:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 5) +; CHECK: [[EH_TEMP3:\%.+]] = bitcast i8* [[RECOVER_EH_TEMP3]] to i32** +; CHECK: [[B_RELOAD:\%.+]] = load i32*, i32** [[EH_TEMP3]] +; CHECK: [[E_I8PTR:\%.+]] = bitcast i32* [[E_PTR]] to i8* +; CHECK: [[TMP:\%.+]] = load i32, i32* [[E_PTR]], align 4 +; CHECK: [[IDXPROM:\%.+]] = sext i32 [[NUMEXCEPTIONS_RELOAD]] to i64 +; CHECK: [[ARRAYIDX:\%.+]] = getelementptr inbounds [10 x i32], [10 x i32]* [[EXCEPTIONVAL]], i64 0, i64 [[IDXPROM]] +; CHECK: store i32 [[TMP]], i32* [[ARRAYIDX]], align 4 +; CHECK: [[INC:\%.+]] = add nsw i32 [[NUMEXCEPTIONS_RELOAD]], 1 +; CHECK: [[CMP:\%.+]] = icmp eq i32 [[TMP]], [[I_RELOAD]] +; CHECK: br i1 [[CMP]], label %if.then, label %if.else ; -; CHECK: if.then: ; preds = %entry -; CHECK: %tmp9 = load i32, i32* %b.reload, align 4, !tbaa !8 -; CHECK: %add2 = add nsw i32 %tmp9, %i.019.reload -; CHECK: store i32 %add2, i32* %b.reload, align 4, !tbaa !8 +; CHECK: if.then:{{[ ]+}}; preds = %entry +; CHECK: [[TMP1:\%.+]] = load i32, i32* [[B_RELOAD]], align 4 +; CHECK: [[ADD:\%.+]] = add nsw i32 [[TMP1]], [[I_RELOAD]] +; CHECK: store i32 [[ADD]], i32* [[B_RELOAD]], align 4 ; CHECK: br label %if.end ; -; CHECK: if.else: ; preds = %entry -; CHECK: %tmp10 = load i32, i32* %a.reload, align 8, !tbaa !2 -; CHECK: %add4 = add nsw i32 %tmp10, %tmp8 -; CHECK: store i32 %add4, i32* %a.reload, align 8, !tbaa !2 +; CHECK: if.else:{{[ ]+}}; preds = %entry +; CHECK: [[TMP2:\%.+]] = load i32, i32* [[A_RELOAD]], align 8 +; CHECK: [[ADD2:\%.+]] = add nsw i32 [[TMP2]], [[TMP]] +; CHECK: store i32 [[ADD2]], i32* [[A_RELOAD]], align 8 ; CHECK: br label %if.end ; -; CHECK: if.end: ; preds = %if.else, %if.then +; CHECK: if.end:{{[ ]+}}; preds = %if.else, %if.then ; CHECK: ret i8* blockaddress(@"\01?test@@YAXXZ", %try.cont) ; CHECK: }