Index: include/llvm/CodeGen/Passes.h =================================================================== --- include/llvm/CodeGen/Passes.h +++ include/llvm/CodeGen/Passes.h @@ -574,6 +574,11 @@ /// FunctionPass *createSjLjEHPreparePass(const TargetMachine *TM); + /// createMSVCEHPreparePass - This pass adapts exception handling code to use + /// the MSVC-style (outlined) exception and unwind handlers. + /// + FunctionPass *createMSVCEHPreparePass(const TargetMachine *TM); + /// LocalStackSlotAllocation - This pass assigns local frame indices to stack /// slots relative to one another and allocates base registers to access them /// when it is estimated by the target to be out of range of normal frame Index: include/llvm/InitializePasses.h =================================================================== --- include/llvm/InitializePasses.h +++ include/llvm/InitializePasses.h @@ -97,6 +97,7 @@ void initializeCFGViewerPass(PassRegistry&); void initializeConstantHoistingPass(PassRegistry&); void initializeCodeGenPreparePass(PassRegistry&); +void initializeMSVCEHPreparePass(PassRegistry&); void initializeConstantMergePass(PassRegistry&); void initializeConstantPropagationPass(PassRegistry&); void initializeMachineCopyPropagationPass(PassRegistry&); Index: include/llvm/Transforms/Utils/Cloning.h =================================================================== --- include/llvm/Transforms/Utils/Cloning.h +++ include/llvm/Transforms/Utils/Cloning.h @@ -135,6 +135,42 @@ ValueMapTypeRemapper *TypeMapper = nullptr, ValueMaterializer *Materializer = nullptr); +/// A helper class used with CloneAndPruneIntoFromInst to change the default +/// behavior while instructions are being cloned. +class CloningDirector +{ +public: + /// This enumeration describes the way CloneAndPruneIntoFromInst should + /// proceed after the CloningDirector has examined an instruction. + enum CloningAction { + ///< Continue cloning the instruction (default behavior). + CloneInstruction, + ///< Skip this instruction but continue cloning the current basic block. + SkipInstruction, + ///< Skip this instruction and stop cloning the current basic block. + StopCloningBB + }; + + CloningDirector() {} + virtual ~CloningDirector() {} + + /// Subclasses must override this function to customize cloning behavior. + virtual CloningAction handleInstruction(ValueToValueMapTy &VMap, + const Instruction *Inst, + BasicBlock *NewBB) = 0; +}; + +void CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc, + const Instruction *StartingInst, + ValueToValueMapTy &VMap, + bool ModuleLevelChanges, + SmallVectorImpl &Returns, + const char *NameSuffix = "", + ClonedCodeInfo *CodeInfo = nullptr, + const DataLayout *DL = nullptr, + CloningDirector *Director = nullptr); + + /// CloneAndPruneFunctionInto - This works exactly like CloneFunctionInto, /// except that it does some simple constant prop and DCE on the fly. The /// effect of this is to copy significantly less code in cases where (for Index: lib/CodeGen/CMakeLists.txt =================================================================== --- lib/CodeGen/CMakeLists.txt +++ lib/CodeGen/CMakeLists.txt @@ -71,6 +71,7 @@ MachineSink.cpp MachineTraceMetrics.cpp MachineVerifier.cpp + MSVCEHPrepare.cpp OcamlGC.cpp OptimizePHIs.cpp PHIElimination.cpp Index: lib/CodeGen/CodeGen.cpp =================================================================== --- lib/CodeGen/CodeGen.cpp +++ lib/CodeGen/CodeGen.cpp @@ -24,6 +24,7 @@ initializeBasicTTIPass(Registry); initializeBranchFolderPassPass(Registry); initializeCodeGenPreparePass(Registry); + initializeMSVCEHPreparePass(Registry); initializeDeadMachineInstructionElimPass(Registry); initializeEarlyIfConverterPass(Registry); initializeExpandPostRAPass(Registry); Index: lib/CodeGen/MSVCEHPrepare.cpp =================================================================== --- lib/CodeGen/MSVCEHPrepare.cpp +++ lib/CodeGen/MSVCEHPrepare.cpp @@ -0,0 +1,463 @@ +//===------ MSVCEHPrepare.cpp - Outline exception handling sequences ------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This transformation is designed for use by code generators which use +// Windows exception handling targeted at the MSVC environment. +// +//===----------------------------------------------------------------------===// + +#include "llvm/CodeGen/Passes.h" +#include "llvm/ADT/DenseSet.h" +#include "llvm/IR/CFG.h" +#include "llvm/IR/Function.h" +#include "llvm/IR/Instructions.h" +#include "llvm/IR/InstVisitor.h" +#include "llvm/IR/IntrinsicInst.h" +#include "llvm/IR/IRBuilder.h" +#include "llvm/IR/Module.h" +#include "llvm/IR/ValueMap.h" +#include "llvm/Pass.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Transforms/Utils/BasicBlockUtils.h" +#include "llvm/Transforms/Utils/Cloning.h" +#include "llvm/Transforms/Utils/Local.h" + +using namespace llvm; + +#define DEBUG_TYPE "msvcehprepare" + +namespace { +class MSVCEHPrepare : public FunctionPass { + const TargetMachine *TM; + +public: + static char ID; // Pass identification, replacement for typeid + explicit MSVCEHPrepare(const TargetMachine *TM = nullptr) + : FunctionPass(ID), TM(TM) { + initializeMSVCEHPreparePass(*PassRegistry::getPassRegistry()); + } + bool runOnFunction(Function &F) override; + + void getAnalysisUsage(AnalysisUsage &AU) const override {} + const char *getPassName() const override { + return "MSVC Exception Handling preparation"; + } + +private: + bool prepareSEHHandlers(Function &F, BasicBlock *LandingPadBB); + + bool outlineSEHFilterHandler(Function *SrcFn, Function *FilterFn, + LandingPadInst *LPI); + + bool prepareCPPEHHandlers(Function &F, BasicBlock *LandingPadBB); +}; + +class SEHFilterDirector : public CloningDirector { +public: + SEHFilterDirector(Function *FilterFn, LandingPadInst *LPI) + : CurrentFilterFn(FilterFn), LPI(LPI), + FilterIDType(Type::getInt32Ty(FilterFn->getContext())) {} + virtual ~SEHFilterDirector() {} + + CloningAction handleInstruction(ValueToValueMapTy &VMap, + const Instruction *Inst, + BasicBlock *NewBB) override; + +private: + Function *CurrentFilterFn; + LandingPadInst *LPI; + Type *FilterIDType; +}; + +class LandingPadVisitor : public InstVisitor { +public: + LandingPadVisitor(LandingPadInst *LPad); + + // Template method instantiations. + void visitCallInst(CallInst &Call); + void visitTerminatorInst(TerminatorInst &Terminator); + + // Entry point for subclasses. + void beginSearch(BasicBlock *BB); + + // Callbacks for subclasses. + virtual void handleCallInst(CallInst &Call) {} + virtual void handleTerminatorInst(TerminatorInst &Terminator) {} + +protected: + bool TargetFound; + LandingPadInst *OriginLPad; + +private: + DenseSet VisitedBlocks; +}; + +class SEHFilterVisitor : public LandingPadVisitor { +public: + SEHFilterVisitor(LandingPadInst *LPad) : LandingPadVisitor(LPad) {} + + bool reachesFilterIntrinsicCall(BasicBlock *BB); + + void handleCallInst(CallInst &Call) override; +}; + +class MSVCEHPruningVisitor : public LandingPadVisitor { +public: + MSVCEHPruningVisitor(LandingPadInst *LPad) : LandingPadVisitor(LPad), + FilterToPrune(nullptr) {} + + bool pruneOutlinedFilterCode(BasicBlock *BB, Function *FilterFn); + + void handleTerminatorInst(TerminatorInst &Terminator) override; + +private: + bool pruneFilterBranch(TerminatorInst &Terminator); + + Function *FilterToPrune; +}; + +} // end anonymous namespace + +char MSVCEHPrepare::ID = 0; +INITIALIZE_TM_PASS(MSVCEHPrepare, "msvcehprepare", + "Outline exception handlers for the MSVC environment", false, + false) + +// Public Interface To the MSVCEHPrepare pass. +FunctionPass *llvm::createMSVCEHPreparePass(const TargetMachine *TM) { + return new MSVCEHPrepare(TM); +} + +bool MSVCEHPrepare::runOnFunction(Function &F) { + bool MadeChange = false; + + for (Function::iterator BB : F) { + if (BB->isLandingPad()) { + BasicBlock::iterator II = BB->begin(); + LandingPadInst *lpad = BB->getLandingPadInst(); + + // The code below is not robust enough for actual use, + // but it is sufficient for getting things working. + Function *PersonalityFn = + dyn_cast(lpad->getPersonalityFn()->stripPointerCasts()); + if (!PersonalityFn) + continue; + + StringRef Personality = PersonalityFn->getName(); + if (Personality == "__C_specific_handler") { + MadeChange |= prepareSEHHandlers(F, BB); + } else if (Personality == "__CxxFrameHandler3") { + MadeChange |= prepareCPPEHHandlers(F, BB); + } else if (Personality == "__gcc_personality_v0" || + Personality == "__gxx_personality_v0") { + // FIXME: ShadowStackGC inserts this personality function without + // regard to exception handling type, and eight of the tests in + // CodeGen/Generic use __gxx_personality_v0. + continue; + } else { + // TODO: Add 32-bit handlers and other varieties as needed. + llvm_unreachable("unsupported MSVC EH personality function"); + } + } // if (BB->isLandingPad()) + } // for (BB : F) + + return MadeChange; +} + +bool MSVCEHPrepare::prepareSEHHandlers(Function &F, BasicBlock *LandingPadBB) { + LandingPadInst *LPI = LandingPadBB->getLandingPadInst(); + + if (!LPI) { + DEBUG(dbgs() << + "Couldn't find the landing pad instruction for a landing pad block!\n"); + return false; + } + + // If there is no filter ID check, that must mean the filter functions + // have already been outlined. + SEHFilterVisitor Visitor(LPI); + if (!Visitor.reachesFilterIntrinsicCall(LPI->getParent())) { + DEBUG(dbgs() << + "No filter intrinsic calls are reachable from the landing pad.\n" + "Skipping outlining pass."); + return false; + } + + + for (unsigned Idx = 0, NumClauses = LPI->getNumClauses(); Idx < NumClauses; + ++Idx) { + + if (LPI->isCatch(Idx)) { + Function *FilterFn; + FilterFn = dyn_cast(LPI->getClause(Idx)->stripPointerCasts()); + + outlineSEHFilterHandler(&F, FilterFn, LPI); + } + } + + return true; +} + +bool MSVCEHPrepare::outlineSEHFilterHandler(Function *SrcFn, Function *FilterFn, + LandingPadInst *LPI) { + // Initially the filter function is expected to contain a single + // "unreachable" instruction. + assert(FilterFn->isDeclaration() || + isa(FilterFn->front().getInstList().front())); + + // Discard the contents of the filter function stub. + FilterFn->getBasicBlockList().clear(); + + // TODO: Insert a call to llvm.recoverframeallocation + + ValueToValueMapTy VMap; + + // TODO: Map other values referenced in the filter handler. + + SEHFilterDirector Director(FilterFn, LPI); + + SmallVector Returns; + ClonedCodeInfo InlinedFunctionInfo; + + BasicBlock::iterator II = LPI; + + CloneAndPruneIntoFromInst(FilterFn, SrcFn, ++II, VMap, + /*ModuleLevelChanges=*/false, Returns, ".seh", + &InlinedFunctionInfo, + SrcFn->getParent()->getDataLayout(), &Director); + + MSVCEHPruningVisitor Visitor(LPI); + Visitor.pruneOutlinedFilterCode(LPI->getParent(), FilterFn); + + return true; +} + +LandingPadVisitor::LandingPadVisitor(LandingPadInst *LPad) + : TargetFound(false), OriginLPad(LPad) { + // The VisitedBlocks set is used to prevent the search from visiting blocks + // that have already been visited, but it is also used to avoid visiting + // blocks that are beyond the scope of the landing pad. This is accomplished + // by finding the invoke instructions which are predecessors of the landing + // pad we are visiting. The "normal destination" of the invoke instruction + // marks the end of the landing pad. All paths through the landing pad will + // reach either a normal destination, a resume instruction, a call to the + // llvm.eh.filer intrinsic or a terminate call. The normal destination is the + // only path that has the potential to take us outside the landing pad scope. + BasicBlock *LPadBB = LPad->getParent(); + for (pred_iterator i = pred_begin(LPadBB), e = pred_end(LPadBB); i != e; ++i) { + BasicBlock *PredBB = *i; + InvokeInst *Invoke = dyn_cast(PredBB->getTerminator()); + // The landing pad predecessors must be invoke instructions. + assert(Invoke); + VisitedBlocks.insert(Invoke->getNormalDest()); + } +} + +void LandingPadVisitor::beginSearch(BasicBlock *BB) { + VisitedBlocks.insert(BB); + visit(BB); +} + +void LandingPadVisitor::visitCallInst(CallInst &Call) { + handleCallInst(Call); +} + +void LandingPadVisitor::visitTerminatorInst(TerminatorInst &Terminator) { + // Give the subclasses a chance to examine this instruction. + handleTerminatorInst(Terminator); + + // If we have found what we were looking for, stop visiting. + if (TargetFound) + return; + + // Otherwise, visit successors unless they are in the visited blocks + // set or are landing pad blocks. Landing pad blocks within a landing pad + // indicate an error path and do not need to be searched. + // The visited block set will contain the normal destination block of the + // invoke instruction that led to the landing pad we are searching, so + // this check will keep us from going outside the landing pad clauses. + for (unsigned i = 0, e = Terminator.getNumSuccessors(); i != e; ++i) { + BasicBlock *BB = Terminator.getSuccessor(i); + if (BB->isLandingPad() || VisitedBlocks.find(BB) != VisitedBlocks.end()) + continue; + VisitedBlocks.insert(BB); + visit(BB); + } +} + +void SEHFilterVisitor::handleCallInst(CallInst &Call) { + if (Call.getCalledFunction()->getName() == "llvm.eh.seh.filter") + TargetFound = true; +} + +bool SEHFilterVisitor::reachesFilterIntrinsicCall(BasicBlock *BB) { + beginSearch(BB); + return TargetFound; +} + +bool MSVCEHPruningVisitor::pruneOutlinedFilterCode(BasicBlock *BB, + Function *FilterFn) { + FilterToPrune = FilterFn; + beginSearch(BB); + return TargetFound; +} + +bool MSVCEHPruningVisitor::pruneFilterBranch(TerminatorInst &Terminator) { + BranchInst *Branch = dyn_cast(&Terminator); + if (!Branch || !Branch->isConditional()) + return false; + + CmpInst *Compare = dyn_cast(Branch->getCondition()); + if (!Compare || !Compare->isEquality()) + return false; + + // Check to see if either operand is a call to get an eh selector id. + // FIXME: Make this an intrinsic check. + CallInst *Call = dyn_cast(Compare->getOperand(0)); + if (!Call || Call->getCalledFunction()->getName() != "llvm.eh.selector.for") { + Call = dyn_cast(Compare->getOperand(1)); + if (!Call || Call->getCalledFunction()->getName() != "llvm.eh.selector.for") + return false; + } + + if (Call->getArgOperand(0)->stripPointerCasts() != FilterToPrune) + return false; + + BasicBlock *FilterBranch = nullptr; + if (Compare->getPredicate() == CmpInst::ICMP_EQ) + FilterBranch = Branch->getSuccessor(0); + else if (Compare->getPredicate() == CmpInst::ICMP_NE) + FilterBranch = Branch->getSuccessor(1); + else + return false; // Unexpected predicate; + + SEHFilterVisitor Visitor(OriginLPad); + if (!Visitor.reachesFilterIntrinsicCall(FilterBranch)) { + DEBUG(dbgs() << "Compare doesn't lead to filter intrinsic.\n"); + return false; + } + + if (Compare->getPredicate() == CmpInst::ICMP_EQ) + Branch->setCondition(ConstantInt::get(Compare->getType(), 0)); + else + Branch->setCondition(ConstantInt::get(Compare->getType(), 1)); + + // Save the branch's basic block before we attempt to fold it. + BasicBlock *Pred = Branch->getParent(); + + ConstantFoldTerminator(Pred, true); + + // In most cases replacing the branch instruction's condition will leave + // the compare and the the selector intrinsic call as unused code. + if (Compare->getNumUses() == 0) + Compare->eraseFromParent(); + if (Call->getNumUses() == 0) + Call->eraseFromParent(); + + // The call to ConstantFoldTerminator should have replaced the conditional + // branch instruction with an unconditional branch. + Branch = dyn_cast(Pred->getTerminator()); + assert(Branch && Branch->isUnconditional()); + + // If nothing else leads to the Filter branch, delete it now. + if (pred_begin(FilterBranch) == pred_end(FilterBranch)) + DeleteDeadBlock(FilterBranch); + + // After the constant folding above, the branch instruction should be + // unconditional. If the successor has no other predecessors, fold + // the successor block into the branch's block. + BasicBlock *Dest = Branch->getSuccessor(0); + if (Dest->getSinglePredecessor()) { + // We shouldn't be able to get single-entry PHI nodes here, as instsimplify + // above should have zapped all of them.. + assert(!isa(Dest->begin())); + + // We know all single-entry PHI nodes in the inlined function have been + // removed, so we just need to splice the blocks. + Branch->eraseFromParent(); + + // Make all PHI nodes that referred to Dest now refer to I as their source. + Dest->replaceAllUsesWith(Pred); + + // Move all the instructions in the succ to the pred. + Pred->getInstList().splice(Pred->end(), Dest->getInstList()); + + // Remove the dest block. + Dest->eraseFromParent(); + } + + return true; +} + +void MSVCEHPruningVisitor::handleTerminatorInst(TerminatorInst &Terminator) { + if (pruneFilterBranch(Terminator)) + TargetFound = true; +} + +CloningDirector::CloningAction SEHFilterDirector::handleInstruction( + ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) { + // Intercept instructions which extract values from the landing pad aggregate. + const ExtractValueInst *Extract = dyn_cast(Inst); + if (Extract && 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"); + + // Element 0 corresponds to the first argument of the filter function. + // Element 1 corresponds to the filter ID (use 1 to match). + if (*(Extract->idx_begin()) == 0) + VMap[Inst] = CurrentFilterFn->arg_begin(); + else + VMap[Inst] = ConstantInt::get(FilterIDType, 1); + + // Tell the caller not to clone this instruction. + return CloningDirector::SkipInstruction; + } + + // TODO: Make this use intrinsics. + const CallInst *Call = dyn_cast(Inst); + if (Call && Call->getCalledFunction()->getName() == "llvm.eh.selector.for") { + Function *FilterFn = + dyn_cast(Call->getArgOperand(0)->stripPointerCasts()); + if (FilterFn) { + // This causes a replacement that will collapse the landing pad CFG based + // on the filter function we intend to match. + if (FilterFn == CurrentFilterFn) + VMap[Inst] = ConstantInt::get(FilterIDType, 1); + else + VMap[Inst] = ConstantInt::get(FilterIDType, 0); + // Tell the caller not to clone this instruction. + return CloningDirector::SkipInstruction; + } + } + else if (Call && Call->getCalledFunction()->getName() == "llvm.eh.seh.filter") { + // The argument to the call should already have been cloned and mapped, + // but the cloning function will want to remap the operands of the + // terminator, so we insert an instruction here that uses the original + // return value. + Value *FilterResult = Call->getArgOperand(0); + ReturnInst::Create(NewBB->getContext(), FilterResult, NewBB); + // We've found our endpoint and inserted a terminator. + // Tell the caller to stop processing the current basic block. + return CloningDirector::StopCloningBB; + } + + // Continue with the default cloning behavior. + return CloningDirector::CloneInstruction; +} + +bool MSVCEHPrepare::prepareCPPEHHandlers(Function &F, + BasicBlock *LandingPadBB) { + // TODO: Implement C++ exception handler outlining. + llvm_unreachable("unsupported MSVC EH personality function"); + return false; +} Index: lib/CodeGen/Passes.cpp =================================================================== --- lib/CodeGen/Passes.cpp +++ lib/CodeGen/Passes.cpp @@ -449,7 +449,10 @@ case ExceptionHandling::DwarfCFI: case ExceptionHandling::ARM: case ExceptionHandling::ItaniumWinEH: - case ExceptionHandling::MSVC: // FIXME: Needs preparation. + addPass(createDwarfEHPass(TM)); + break; + case ExceptionHandling::MSVC: + addPass(createMSVCEHPreparePass(TM)); addPass(createDwarfEHPass(TM)); break; case ExceptionHandling::None: Index: lib/Transforms/Utils/CloneFunction.cpp =================================================================== --- lib/Transforms/Utils/CloneFunction.cpp +++ lib/Transforms/Utils/CloneFunction.cpp @@ -260,21 +260,26 @@ const char *NameSuffix; ClonedCodeInfo *CodeInfo; const DataLayout *DL; + CloningDirector *Director; + public: PruningFunctionCloner(Function *newFunc, const Function *oldFunc, ValueToValueMapTy &valueMap, bool moduleLevelChanges, const char *nameSuffix, ClonedCodeInfo *codeInfo, - const DataLayout *DL) + const DataLayout *DL, + CloningDirector *Director) : NewFunc(newFunc), OldFunc(oldFunc), VMap(valueMap), ModuleLevelChanges(moduleLevelChanges), - NameSuffix(nameSuffix), CodeInfo(codeInfo), DL(DL) { + NameSuffix(nameSuffix), CodeInfo(codeInfo), DL(DL), + Director(Director) { } /// CloneBlock - The specified block is found to be reachable, clone it and /// anything that it can reach. - void CloneBlock(const BasicBlock *BB, + void CloneBlock(const BasicBlock *BB, + BasicBlock::const_iterator StartingInst, std::vector &ToClone); }; } @@ -282,6 +287,7 @@ /// CloneBlock - The specified block is found to be reachable, clone it and /// anything that it can reach. void PruningFunctionCloner::CloneBlock(const BasicBlock *BB, + BasicBlock::const_iterator StartingInst, std::vector &ToClone){ WeakVH &BBEntry = VMap[BB]; @@ -307,14 +313,31 @@ const_cast(BB)); VMap[OldBBAddr] = BlockAddress::get(NewFunc, NewBB); } - bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false; - + // Loop over all instructions, and copy them over, DCE'ing as we go. This // loop doesn't include the terminator. - for (BasicBlock::const_iterator II = BB->begin(), IE = --BB->end(); + for (BasicBlock::const_iterator II = StartingInst, IE = --BB->end(); II != IE; ++II) { + // If the "Director" remaps the instruction, don't clone it. + if (Director) { + CloningDirector::CloningAction Action + = Director->handleInstruction(VMap, II, NewBB); + // If the cloning director says stop, we want to stop everything, not + // just break out of the loop (which would cause the terminator to be + // cloned). The cloning director is responsible for inserting a proper + // terminator into the new basic block in this case. + if (Action == CloningDirector::StopCloningBB) + return; + // If the cloning director says skip, continue to the next instruction. + // In this case, the cloning director is responsible for mapping the + // skipped instruction to some value that is defined in the new + // basic block. + if (Action == CloningDirector::SkipInstruction) + continue; + } + Instruction *NewInst = II->clone(); // Eagerly remap operands to the newly cloned instruction, except for PHI @@ -354,6 +377,18 @@ // Finally, clone over the terminator. const TerminatorInst *OldTI = BB->getTerminator(); bool TerminatorDone = false; + if (Director) { + CloningDirector::CloningAction Action + = Director->handleInstruction(VMap, OldTI, NewBB); + // If the cloning director says stop, we want to stop everything, not + // just break out of the loop (which would cause the terminator to be + // cloned). The cloning director is responsible for inserting a proper + // terminator into the new basic block in this case. + if (Action == CloningDirector::StopCloningBB) + return; + assert(Action != CloningDirector::SkipInstruction && + "SkipInstruction is not valid for terminators."); + } if (const BranchInst *BI = dyn_cast(OldTI)) { if (BI->isConditional()) { // If the condition was a known constant in the callee... @@ -409,39 +444,47 @@ } } -/// CloneAndPruneFunctionInto - This works exactly like CloneFunctionInto, -/// except that it does some simple constant prop and DCE on the fly. The -/// effect of this is to copy significantly less code in cases where (for -/// example) a function call with constant arguments is inlined, and those -/// constant arguments cause a significant amount of code in the callee to be -/// dead. Since this doesn't produce an exact copy of the input, it can't be -/// used for things like CloneFunction or CloneModule. -void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc, +/// CloneAndPruneIntoFromInst - This works like CloneAndPruneFunctionInto, except +/// that it does not clone the entire function. Instead it starts at an +/// instruction provided by the caller and copies (and prunes) only the code +/// reachable from that instruction. +void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc, + const Instruction *StartingInst, ValueToValueMapTy &VMap, bool ModuleLevelChanges, SmallVectorImpl &Returns, const char *NameSuffix, ClonedCodeInfo *CodeInfo, const DataLayout *DL, - Instruction *TheCall) { + CloningDirector *Director) { assert(NameSuffix && "NameSuffix cannot be null!"); - + #ifndef NDEBUG - for (Function::const_arg_iterator II = OldFunc->arg_begin(), - E = OldFunc->arg_end(); II != E; ++II) - assert(VMap.count(II) && "No mapping from source argument specified!"); + // If the cloning starts at the begining of the function, verify that + // the function arguments are mapped. + if (!StartingInst) + for (Function::const_arg_iterator II = OldFunc->arg_begin(), + E = OldFunc->arg_end(); II != E; ++II) + assert(VMap.count(II) && "No mapping from source argument specified!"); #endif PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, ModuleLevelChanges, - NameSuffix, CodeInfo, DL); + NameSuffix, CodeInfo, DL, Director); + const BasicBlock *StartingBB; + if (StartingInst) + StartingBB = StartingInst->getParent(); + else { + StartingBB = &OldFunc->getEntryBlock(); + StartingInst = StartingBB->begin(); + } // Clone the entry block, and anything recursively reachable from it. std::vector CloneWorklist; - CloneWorklist.push_back(&OldFunc->getEntryBlock()); + PFC.CloneBlock(StartingBB, StartingInst, CloneWorklist); while (!CloneWorklist.empty()) { const BasicBlock *BB = CloneWorklist.back(); CloneWorklist.pop_back(); - PFC.CloneBlock(BB, CloneWorklist); + PFC.CloneBlock(BB, BB->begin(), CloneWorklist); } // Loop over all of the basic blocks in the old function. If the block was @@ -569,7 +612,7 @@ // and zap unconditional fall-through branches. This happen all the time when // specializing code: code specialization turns conditional branches into // uncond branches, and this code folds them. - Function::iterator Begin = cast(VMap[&OldFunc->getEntryBlock()]); + Function::iterator Begin = cast(VMap[StartingBB]); Function::iterator I = Begin; while (I != NewFunc->end()) { // Check if this block has become dead during inlining or other @@ -620,9 +663,30 @@ // Make a final pass over the basic blocks from theh old function to gather // any return instructions which survived folding. We have to do this here // because we can iteratively remove and merge returns above. - for (Function::iterator I = cast(VMap[&OldFunc->getEntryBlock()]), + for (Function::iterator I = cast(VMap[StartingBB]), E = NewFunc->end(); I != E; ++I) if (ReturnInst *RI = dyn_cast(I->getTerminator())) Returns.push_back(RI); } + + +/// CloneAndPruneFunctionInto - This works exactly like CloneFunctionInto, +/// except that it does some simple constant prop and DCE on the fly. The +/// effect of this is to copy significantly less code in cases where (for +/// example) a function call with constant arguments is inlined, and those +/// constant arguments cause a significant amount of code in the callee to be +/// dead. Since this doesn't produce an exact copy of the input, it can't be +/// used for things like CloneFunction or CloneModule. +void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc, + ValueToValueMapTy &VMap, + bool ModuleLevelChanges, + SmallVectorImpl &Returns, + const char *NameSuffix, + ClonedCodeInfo *CodeInfo, + const DataLayout *DL, + Instruction *TheCall) { + CloneAndPruneIntoFromInst(NewFunc, OldFunc, OldFunc->front().begin(), + VMap, ModuleLevelChanges, Returns, NameSuffix, + CodeInfo, DL, nullptr); +} Index: test/CodeGen/X86/seh-outline.ll =================================================================== --- test/CodeGen/X86/seh-outline.ll +++ test/CodeGen/X86/seh-outline.ll @@ -0,0 +1,180 @@ +; RUN: opt -mtriple=x86_64-pc-msvc -msvcehprepare -S -o - < %s | FileCheck %s + +define i32 @safe_div_filt0(i8* %ehp, i8* %rbp) { + unreachable ; Stub function, filled in by MSVCEHPrepare +} + +; Verify the body of the outlined function. +; CHECK: define i32 @safe_div_filt0(i8* %ehp, i8* %rbp) { +; CHECK: lpad.seh: +; CHECK: %eh_ptrs_c.0.seh = bitcast i8* %ehp to i32** +; CHECK: %eh_rec.0.seh = load i32** %eh_ptrs_c.0.seh +; CHECK: %eh_code.0.seh = load i32* %eh_rec.0.seh +; CHECK: %cmp.0.seh = icmp eq i32 %eh_code.0.seh, -1073741819 +; CHECK: %filt0.res.seh = zext i1 %cmp.0.seh to i32 +; CHECK: ret i32 %filt0.res.seh +; CHECK: } + +define i32 @safe_div_filt1(i8* %ehp, i8* %rbp) { + unreachable ; Stub function, filled in by MSVCEHPrepare +} + +; Verify the body of the outlined function. +; CHECK: define i32 @safe_div_filt1(i8* %ehp, i8* %rbp) { +; CHECK: lpad.seh: +; CHECK: %eh_ptrs_c.1.seh = bitcast i8* %ehp to i32** +; CHECK: %eh_rec.1.seh = load i32** %eh_ptrs_c.1.seh +; CHECK: %eh_code.1.seh = load i32* %eh_rec.1.seh +; CHECK: %cmp.1.seh = icmp eq i32 %eh_code.1.seh, -1073741676 +; CHECK: %filt1.res.seh = zext i1 %cmp.1.seh to i32 +; CHECK: ret i32 %filt1.res.seh +; CHECK: } + +@str1=linkonce_odr unnamed_addr constant [3 x i8] c"One", align 1 +@str2=linkonce_odr unnamed_addr constant [3 x i8] c"Two", align 1 + +define i32 @safe_div(i32* %n, i32* %d) { +entry: + %r = alloca i32, align 4 + invoke void @try_body(i32* %r, i32* %n, i32* %d) + to label %__try.cont unwind label %lpad + +lpad: + %vals = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__C_specific_handler to i8*) + catch i8* bitcast (i32 (i8*, i8*)* @safe_div_filt0 to i8*) + catch i8* bitcast (i32 (i8*, i8*)* @safe_div_filt1 to i8*) + br label %seh.filter.dispatch + +seh.filter.dispatch: + %eh_ptrs = extractvalue { i8*, i32 } %vals, 0 + %sel = extractvalue { i8*, i32 } %vals, 1 + %filt0_val = call i32 @llvm.eh.selector.for(i8* bitcast (i32 (i8*, i8*)* @safe_div_filt0 to i8*)) + %is_filt0 = icmp eq i32 %sel, %filt0_val + br i1 %is_filt0, label %filter0, label %seh.filter.dispatch1 + +seh.filter.dispatch1: + %filt1_val = call i32 @llvm.eh.selector.for(i8* bitcast (i32 (i8*, i8*)* @safe_div_filt1 to i8*)) + %is_filt1 = icmp eq i32 %sel, %filt1_val + br i1 %is_filt1, label %filter1, label %seh.handler.dispatch + +filter0: ; MSVCEHPrepare traces out this basic block, after folding away preceding dispatch. + %eh_ptrs_c.0 = bitcast i8* %eh_ptrs to i32** + %eh_rec.0 = load i32** %eh_ptrs_c.0 + %eh_code.0 = load i32* %eh_rec.0 + ; EXCEPTION_ACCESS_VIOLATION = 0xC0000005 + %cmp.0 = icmp eq i32 %eh_code.0, 3221225477 + %filt0.res = zext i1 %cmp.0 to i32 + ; FILTER OUTLINING ENDS HERE + call void @llvm.eh.seh.filter(i32 %filt0.res) + br label %seh.handler.dispatch + +filter1: + %eh_ptrs_c.1 = bitcast i8* %eh_ptrs to i32** + %eh_rec.1 = load i32** %eh_ptrs_c.1 + %eh_code.1 = load i32* %eh_rec.1 + ; EXCEPTION_INT_DIVIDE_BY_ZERO = 0xC0000094 + %cmp.1 = icmp eq i32 %eh_code.1, 3221225620 + %filt1.res = zext i1 %cmp.1 to i32 + ; FILTER OUTLINING ENDS HERE + call void @llvm.eh.seh.filter(i32 %filt1.res) + br label %seh.handler.dispatch + +seh.handler.dispatch: + %handler0_val = call i32 @llvm.eh.selector.for(i8* bitcast (i32 (i8*, i8*)* @safe_div_filt0 to i8*)) + %is_handler0 = icmp eq i32 %sel, %handler0_val + br i1 %is_handler0, label %handler0, label %seh.handler.dispatch1 + +seh.handler.dispatch1: + %handler1_val = call i32 @llvm.eh.selector.for(i8* bitcast (i32 (i8*, i8*)* @safe_div_filt1 to i8*)) + %is_handler1 = icmp eq i32 %sel, %handler1_val + br i1 %is_handler1, label %handler1, label %seh.resume + +handler0: + call void @puts(i8* getelementptr inbounds ([3 x i8]* @str1, i32 0, i32 0)) + store i32 -1, i32* %r, align 4 + br label %__try.cont + +handler1: + call void @puts(i8* getelementptr inbounds ([3 x i8]* @str2, i32 0, i32 0)) + store i32 -2, i32* %r, align 4 + br label %__try.cont + +seh.resume: + resume { i8*, i32 } %vals + +__try.cont: + %safe_ret = load i32* %r, align 4 + ret i32 %safe_ret +} + +; Verify that the filters have been removed from the body of the original function. +; CHECK: define i32 @safe_div(i32* %n, i32* %d) { +; CHECK: entry: +; CHECK: %r = alloca i32, align 4 +; CHECK: invoke void @try_body(i32* %r, i32* %n, i32* %d) +; CHECK: to label %__try.cont unwind label %lpad +; +; CHECK: lpad: +; CHECK: %vals = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__C_specific_handler to i8*) +; CHECK: catch i8* bitcast (i32 (i8*, i8*)* @safe_div_filt0 to i8*) +; CHECK: catch i8* bitcast (i32 (i8*, i8*)* @safe_div_filt1 to i8*) +; +; Leave some flexibility as to how the branching is now implemented, +; but at some point it should contain the following definition. +; CHECK: %sel = extractvalue { i8*, i32 } %vals, 1 +; +; CHECK-NOT: filter0: +; CHECK-NOT: %eh_ptrs_c.0 = bitcast i8* %eh_ptrs to i32** +; CHECK-NOT: %eh_rec.0 = load i32** %eh_ptrs_c.0 +; CHECK-NOT: %eh_code.0 = load i32* %eh_rec.0 +; CHECK-NOT: %cmp.0 = icmp eq i32 %eh_code.0, 3221225477 +; CHECK-NOT: %filt0.res = zext i1 %cmp.0 to i32 +; CHECK-NOT: call void @llvm.eh.seh.filter(i32 %filt0.res) +; CHECK-NOT: br label %seh.handler.dispatch +; +; CHECK-NOT: filter1: +; CHECK-NOT: %eh_ptrs_c.1 = bitcast i8* %eh_ptrs to i32** +; CHECK-NOT: %eh_rec.1 = load i32** %eh_ptrs_c.1 +; CHECK-NOT: %eh_code.1 = load i32* %eh_rec.1 +; CHECK-NOT: %cmp.1 = icmp eq i32 %eh_code.1, 3221225620 +; CHECK-NOT: %filt1.res = zext i1 %cmp.1 to i32 +; CHECK-NOT: call void @llvm.eh.seh.filter(i32 %filt1.res) +; CHECK-NOT: br label %seh.handler.dispatch +; +; Everything else should be left intact. +; +; CHECK: %handler0_val = call i32 @llvm.eh.selector.for(i8* bitcast (i32 (i8*, i8*)* @safe_div_filt0 to i8*)) +; CHECK: %is_handler0 = icmp eq i32 %sel, %handler0_val +; CHECK: br i1 %is_handler0, label %handler0, label %seh.handler.dispatch1 +; +; CHECK: seh.handler.dispatch1: +; CHECK: %handler1_val = call i32 @llvm.eh.selector.for(i8* bitcast (i32 (i8*, i8*)* @safe_div_filt1 to i8*)) +; CHECK: %is_handler1 = icmp eq i32 %sel, %handler1_val +; CHECK: br i1 %is_handler1, label %handler1, label %seh.resume +; +; CHECK: handler0: +; CHECK: call void @puts(i8* getelementptr inbounds ([3 x i8]* @str1, i32 0, i32 0)) +; CHECK: store i32 -1, i32* %r, align 4 +; CHECK: br label %__try.cont +; +; CHECK: handler1: +; CHECK: call void @puts(i8* getelementptr inbounds ([3 x i8]* @str2, i32 0, i32 0)) +; CHECK: store i32 -2, i32* %r, align 4 +; CHECK: br label %__try.cont +; +; CHECK: seh.resume: +; CHECK: resume { i8*, i32 } %vals +; +; CHECK: __try.cont: ; preds +; CHECK: %safe_ret = load i32* %r, align 4 +; CHECK: ret i32 %safe_ret +; CHECK: } + +declare i32 @__C_specific_handler(...) + +declare void @try_body(i32*, i32*, i32*) + +declare void @puts(i8*) + +declare void @llvm.eh.seh.filter(i32) +declare i32 @llvm.eh.selector.for(i8*) Index: tools/opt/opt.cpp =================================================================== --- tools/opt/opt.cpp +++ tools/opt/opt.cpp @@ -320,6 +320,7 @@ // For codegen passes, only passes that do IR to IR transformation are // supported. initializeCodeGenPreparePass(Registry); + initializeMSVCEHPreparePass(Registry); initializeAtomicExpandPass(Registry); initializeRewriteSymbolsPass(Registry);