- DominatorTree *DT;
- EHPersonality Personality;
- CatchHandlerMapTy CatchHandlerMap;
- CleanupHandlerMapTy CleanupHandlerMap;
- DenseMap<const LandingPadInst *, LandingPadMap> LPadMaps;
-
- // This maps landing pad instructions found in outlined handlers to
- // the landing pad instruction in the parent function from which they
- // were cloned. The cloned/nested landing pad is used as the key
- // because the landing pad may be cloned into multiple handlers.
- // This map will be used to add the llvm.eh.actions call to the nested
- // landing pads after all handlers have been outlined.
- DenseMap<LandingPadInst *, const LandingPadInst *> NestedLPtoOriginalLP;
-
- // This maps blocks in the parent function which are destinations of
- // catch handlers to cloned blocks in (other) outlined handlers. This
- // handles the case where a nested landing pads has a catch handler that
- // returns to a handler function rather than the parent function.
- // The original block is used as the key here because there should only
- // ever be one handler function from which the cloned block is not pruned.
- // The original block will be pruned from the parent function after all
- // handlers have been outlined. This map will be used to adjust the
- // return instructions of handlers which return to the block that was
- // outlined into a handler. This is done after all handlers have been
- // outlined but before the outlined code is pruned from the parent function.
- DenseMap<const BasicBlock *, BasicBlock *> LPadTargetBlocks;
-
- AllocaInst *SEHExceptionCodeSlot;
-};
-
-class WinEHFrameVariableMaterializer : public ValueMaterializer {
-public:
- WinEHFrameVariableMaterializer(Function *OutlinedFn,
- FrameVarInfoMap &FrameVarInfo);
- ~WinEHFrameVariableMaterializer() override {}
-
- Value *materializeValueFor(Value *V) override;
-
- void escapeCatchObject(Value *V);
-
-private:
- FrameVarInfoMap &FrameVarInfo;
- IRBuilder<> Builder;
-};
-
-class LandingPadMap {
-public:
- LandingPadMap() : OriginLPad(nullptr) {}
- void mapLandingPad(const LandingPadInst *LPad);
-
- bool isInitialized() { return OriginLPad != nullptr; }
-
- bool isOriginLandingPadBlock(const BasicBlock *BB) const;
- bool isLandingPadSpecificInst(const Instruction *Inst) const;
-
- void remapEHValues(ValueToValueMapTy &VMap, Value *EHPtrValue,
- Value *SelectorValue) const;
-
-private:
- 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<const ExtractValueInst *> ExtractedEHPtrs;
- TinyPtrVector<const ExtractValueInst *> ExtractedSelectors;
-};
-
-class WinEHCloningDirectorBase : public CloningDirector {
-public:
- WinEHCloningDirectorBase(Function *HandlerFn, FrameVarInfoMap &VarInfo,
- LandingPadMap &LPadMap)
- : Materializer(HandlerFn, VarInfo),
- SelectorIDType(Type::getInt32Ty(HandlerFn->getContext())),
- Int8PtrType(Type::getInt8PtrTy(HandlerFn->getContext())),
- LPadMap(LPadMap) {
- auto AI = HandlerFn->getArgumentList().begin();
- ++AI;
- EstablisherFrame = AI;
- }
-
- CloningAction handleInstruction(ValueToValueMapTy &VMap,
- const Instruction *Inst,
- BasicBlock *NewBB) override;
-
- virtual CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
- const Instruction *Inst,
- BasicBlock *NewBB) = 0;
- virtual CloningAction handleEndCatch(ValueToValueMapTy &VMap,
- const Instruction *Inst,
- BasicBlock *NewBB) = 0;
- 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;
- virtual CloningAction handleCompare(ValueToValueMapTy &VMap,
- const CmpInst *Compare,
- BasicBlock *NewBB) = 0;
- virtual CloningAction handleLandingPad(ValueToValueMapTy &VMap,
- const LandingPadInst *LPad,
- BasicBlock *NewBB) = 0;
-
- ValueMaterializer *getValueMaterializer() override { return &Materializer; }
-
-protected:
- WinEHFrameVariableMaterializer Materializer;
- Type *SelectorIDType;
- Type *Int8PtrType;
- LandingPadMap &LPadMap;
-
- /// The value representing the parent frame pointer.
- Value *EstablisherFrame;
-};
-
-class WinEHCatchDirector : public WinEHCloningDirectorBase {
-public:
- WinEHCatchDirector(
- Function *CatchFn, Value *Selector, FrameVarInfoMap &VarInfo,
- LandingPadMap &LPadMap,
- DenseMap<LandingPadInst *, const LandingPadInst *> &NestedLPads)
- : WinEHCloningDirectorBase(CatchFn, VarInfo, LPadMap),
- CurrentSelector(Selector->stripPointerCasts()),
- ExceptionObjectVar(nullptr), NestedLPtoOriginalLP(NestedLPads) {}
-
- CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
- const Instruction *Inst,
- BasicBlock *NewBB) override;
- CloningAction handleEndCatch(ValueToValueMapTy &VMap, const Instruction *Inst,
- BasicBlock *NewBB) override;
- 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;
- CloningAction handleCompare(ValueToValueMapTy &VMap,
- const CmpInst *Compare, BasicBlock *NewBB) override;
- CloningAction handleLandingPad(ValueToValueMapTy &VMap,
- const LandingPadInst *LPad,
- BasicBlock *NewBB) override;
-
- Value *getExceptionVar() { return ExceptionObjectVar; }
- TinyPtrVector<BasicBlock *> &getReturnTargets() { return ReturnTargets; }
-
-private:
- Value *CurrentSelector;
-
- Value *ExceptionObjectVar;
- TinyPtrVector<BasicBlock *> ReturnTargets;
-
- // This will be a reference to the field of the same name in the WinEHPrepare
- // object which instantiates this WinEHCatchDirector object.
- DenseMap<LandingPadInst *, const LandingPadInst *> &NestedLPtoOriginalLP;
-};
-
-class WinEHCleanupDirector : public WinEHCloningDirectorBase {
-public:
- WinEHCleanupDirector(Function *CleanupFn, FrameVarInfoMap &VarInfo,
- LandingPadMap &LPadMap)
- : WinEHCloningDirectorBase(CleanupFn, VarInfo, LPadMap) {}
-
- CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
- const Instruction *Inst,
- BasicBlock *NewBB) override;
- CloningAction handleEndCatch(ValueToValueMapTy &VMap, const Instruction *Inst,
- BasicBlock *NewBB) override;
- 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;
- CloningAction handleCompare(ValueToValueMapTy &VMap,
- const CmpInst *Compare, BasicBlock *NewBB) override;
- CloningAction handleLandingPad(ValueToValueMapTy &VMap,
- const LandingPadInst *LPad,
- BasicBlock *NewBB) override;
-};
-
-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<ActionHandler *> &actions() { return Actions; }
- SmallVectorImpl<ActionHandler *>::iterator begin() { return Actions.begin(); }
- SmallVectorImpl<ActionHandler *>::iterator end() { return Actions.end(); }