Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1 | //===-- WinEHPrepare - Prepare exception handling for code generation ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass lowers LLVM IR exception handling into something closer to what the |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 11 | // backend wants for functions using a personality function from a runtime |
| 12 | // provided by MSVC. Functions with other personality functions are left alone |
| 13 | // and may be prepared by other passes. In particular, all supported MSVC |
| 14 | // personality functions require cleanup code to be outlined, and the C++ |
| 15 | // personality requires catch handler code to be outlined. |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #include "llvm/CodeGen/Passes.h" |
| 20 | #include "llvm/ADT/MapVector.h" |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/STLExtras.h" |
Benjamin Kramer | a8d61b1 | 2015-03-23 18:57:17 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallSet.h" |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SetVector.h" |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Triple.h" |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/TinyPtrVector.h" |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/CFG.h" |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/LibCallSemantics.h" |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/TargetLibraryInfo.h" |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/WinEHFuncInfo.h" |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Dominators.h" |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Function.h" |
| 32 | #include "llvm/IR/IRBuilder.h" |
| 33 | #include "llvm/IR/Instructions.h" |
| 34 | #include "llvm/IR/IntrinsicInst.h" |
| 35 | #include "llvm/IR/Module.h" |
| 36 | #include "llvm/IR/PatternMatch.h" |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 37 | #include "llvm/MC/MCSymbol.h" |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 38 | #include "llvm/Pass.h" |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | a8d61b1 | 2015-03-23 18:57:17 +0000 | [diff] [blame] | 40 | #include "llvm/Support/raw_ostream.h" |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 41 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 42 | #include "llvm/Transforms/Utils/Cloning.h" |
| 43 | #include "llvm/Transforms/Utils/Local.h" |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 44 | #include "llvm/Transforms/Utils/PromoteMemToReg.h" |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 45 | #include "llvm/Transforms/Utils/SSAUpdater.h" |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 46 | #include <memory> |
| 47 | |
| 48 | using namespace llvm; |
| 49 | using namespace llvm::PatternMatch; |
| 50 | |
| 51 | #define DEBUG_TYPE "winehprepare" |
| 52 | |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 53 | static cl::opt<bool> DisableDemotion( |
| 54 | "disable-demotion", cl::Hidden, |
| 55 | cl::desc( |
| 56 | "Clone multicolor basic blocks but do not demote cross funclet values"), |
| 57 | cl::init(false)); |
| 58 | |
| 59 | static cl::opt<bool> DisableCleanups( |
| 60 | "disable-cleanups", cl::Hidden, |
| 61 | cl::desc("Do not remove implausible terminators or other similar cleanups"), |
| 62 | cl::init(false)); |
| 63 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 64 | namespace { |
| 65 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 66 | // This map is used to model frame variable usage during outlining, to |
| 67 | // construct a structure type to hold the frame variables in a frame |
| 68 | // allocation block, and to remap the frame variable allocas (including |
| 69 | // spill locations as needed) to GEPs that get the variable from the |
| 70 | // frame allocation structure. |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 71 | typedef MapVector<Value *, TinyPtrVector<AllocaInst *>> FrameVarInfoMap; |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 72 | |
Reid Kleckner | 3567d27 | 2015-04-02 21:13:31 +0000 | [diff] [blame] | 73 | // TinyPtrVector cannot hold nullptr, so we need our own sentinel that isn't |
| 74 | // quite null. |
| 75 | AllocaInst *getCatchObjectSentinel() { |
| 76 | return static_cast<AllocaInst *>(nullptr) + 1; |
| 77 | } |
| 78 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 79 | typedef SmallSet<BasicBlock *, 4> VisitedBlockSet; |
| 80 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 81 | class LandingPadActions; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 82 | class LandingPadMap; |
| 83 | |
| 84 | typedef DenseMap<const BasicBlock *, CatchHandler *> CatchHandlerMapTy; |
| 85 | typedef DenseMap<const BasicBlock *, CleanupHandler *> CleanupHandlerMapTy; |
| 86 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 87 | class WinEHPrepare : public FunctionPass { |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 88 | public: |
| 89 | static char ID; // Pass identification, replacement for typeid. |
| 90 | WinEHPrepare(const TargetMachine *TM = nullptr) |
Reid Kleckner | 582786b | 2015-04-30 18:17:12 +0000 | [diff] [blame] | 91 | : FunctionPass(ID) { |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 92 | if (TM) |
Daniel Sanders | 110bf6d | 2015-06-24 13:25:57 +0000 | [diff] [blame] | 93 | TheTriple = TM->getTargetTriple(); |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 94 | } |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 95 | |
| 96 | bool runOnFunction(Function &Fn) override; |
| 97 | |
| 98 | bool doFinalization(Module &M) override; |
| 99 | |
| 100 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 101 | |
| 102 | const char *getPassName() const override { |
| 103 | return "Windows exception handling preparation"; |
| 104 | } |
| 105 | |
| 106 | private: |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 107 | bool prepareExceptionHandlers(Function &F, |
| 108 | SmallVectorImpl<LandingPadInst *> &LPads); |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 109 | void identifyEHBlocks(Function &F, SmallVectorImpl<LandingPadInst *> &LPads); |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 110 | void promoteLandingPadValues(LandingPadInst *LPad); |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 111 | void demoteValuesLiveAcrossHandlers(Function &F, |
| 112 | SmallVectorImpl<LandingPadInst *> &LPads); |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 113 | void findSEHEHReturnPoints(Function &F, |
| 114 | SetVector<BasicBlock *> &EHReturnBlocks); |
| 115 | void findCXXEHReturnPoints(Function &F, |
| 116 | SetVector<BasicBlock *> &EHReturnBlocks); |
Andrew Kaylor | cc14f38 | 2015-05-11 23:06:02 +0000 | [diff] [blame] | 117 | void getPossibleReturnTargets(Function *ParentF, Function *HandlerF, |
| 118 | SetVector<BasicBlock*> &Targets); |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 119 | void completeNestedLandingPad(Function *ParentFn, |
| 120 | LandingPadInst *OutlinedLPad, |
| 121 | const LandingPadInst *OriginalLPad, |
| 122 | FrameVarInfoMap &VarInfo); |
Reid Kleckner | 6511c8b | 2015-07-01 20:59:25 +0000 | [diff] [blame] | 123 | Function *createHandlerFunc(Function *ParentFn, Type *RetTy, |
| 124 | const Twine &Name, Module *M, Value *&ParentFP); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 125 | bool outlineHandler(ActionHandler *Action, Function *SrcFn, |
| 126 | LandingPadInst *LPad, BasicBlock *StartBB, |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 127 | FrameVarInfoMap &VarInfo); |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 128 | void addStubInvokeToHandlerIfNeeded(Function *Handler); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 129 | |
| 130 | void mapLandingPadBlocks(LandingPadInst *LPad, LandingPadActions &Actions); |
| 131 | CatchHandler *findCatchHandler(BasicBlock *BB, BasicBlock *&NextBB, |
| 132 | VisitedBlockSet &VisitedBlocks); |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 133 | void findCleanupHandlers(LandingPadActions &Actions, BasicBlock *StartBB, |
| 134 | BasicBlock *EndBB); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 135 | |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 136 | void processSEHCatchHandler(CatchHandler *Handler, BasicBlock *StartBB); |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 137 | void insertPHIStores(PHINode *OriginalPHI, AllocaInst *SpillSlot); |
| 138 | void |
| 139 | insertPHIStore(BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot, |
| 140 | SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist); |
| 141 | AllocaInst *insertPHILoads(PHINode *PN, Function &F); |
| 142 | void replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot, |
| 143 | DenseMap<BasicBlock *, Value *> &Loads, Function &F); |
| 144 | void demoteNonlocalUses(Value *V, std::set<BasicBlock *> &ColorsForBB, |
| 145 | Function &F); |
Joseph Tremoulet | ec18285 | 2015-08-28 01:12:35 +0000 | [diff] [blame] | 146 | bool prepareExplicitEH(Function &F, |
| 147 | SmallVectorImpl<BasicBlock *> &EntryBlocks); |
David Majnemer | 67bff0d | 2015-09-16 20:42:16 +0000 | [diff] [blame] | 148 | void replaceTerminatePadWithCleanup(Function &F); |
Joseph Tremoulet | ec18285 | 2015-08-28 01:12:35 +0000 | [diff] [blame] | 149 | void colorFunclets(Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks); |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 150 | void demotePHIsOnFunclets(Function &F); |
| 151 | void demoteUsesBetweenFunclets(Function &F); |
| 152 | void demoteArgumentUses(Function &F); |
| 153 | void cloneCommonBlocks(Function &F, |
| 154 | SmallVectorImpl<BasicBlock *> &EntryBlocks); |
| 155 | void removeImplausibleTerminators(Function &F); |
| 156 | void cleanupPreparedFunclets(Function &F); |
| 157 | void verifyPreparedFunclets(Function &F); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 158 | |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 159 | Triple TheTriple; |
| 160 | |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 161 | // All fields are reset by runOnFunction. |
Reid Kleckner | 582786b | 2015-04-30 18:17:12 +0000 | [diff] [blame] | 162 | DominatorTree *DT = nullptr; |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 163 | const TargetLibraryInfo *LibInfo = nullptr; |
Reid Kleckner | 582786b | 2015-04-30 18:17:12 +0000 | [diff] [blame] | 164 | EHPersonality Personality = EHPersonality::Unknown; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 165 | CatchHandlerMapTy CatchHandlerMap; |
| 166 | CleanupHandlerMapTy CleanupHandlerMap; |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 167 | DenseMap<const LandingPadInst *, LandingPadMap> LPadMaps; |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 168 | SmallPtrSet<BasicBlock *, 4> NormalBlocks; |
| 169 | SmallPtrSet<BasicBlock *, 4> EHBlocks; |
| 170 | SetVector<BasicBlock *> EHReturnBlocks; |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 171 | |
| 172 | // This maps landing pad instructions found in outlined handlers to |
| 173 | // the landing pad instruction in the parent function from which they |
| 174 | // were cloned. The cloned/nested landing pad is used as the key |
| 175 | // because the landing pad may be cloned into multiple handlers. |
| 176 | // This map will be used to add the llvm.eh.actions call to the nested |
| 177 | // landing pads after all handlers have been outlined. |
| 178 | DenseMap<LandingPadInst *, const LandingPadInst *> NestedLPtoOriginalLP; |
| 179 | |
| 180 | // This maps blocks in the parent function which are destinations of |
| 181 | // catch handlers to cloned blocks in (other) outlined handlers. This |
| 182 | // handles the case where a nested landing pads has a catch handler that |
| 183 | // returns to a handler function rather than the parent function. |
| 184 | // The original block is used as the key here because there should only |
| 185 | // ever be one handler function from which the cloned block is not pruned. |
| 186 | // The original block will be pruned from the parent function after all |
| 187 | // handlers have been outlined. This map will be used to adjust the |
| 188 | // return instructions of handlers which return to the block that was |
| 189 | // outlined into a handler. This is done after all handlers have been |
| 190 | // outlined but before the outlined code is pruned from the parent function. |
| 191 | DenseMap<const BasicBlock *, BasicBlock *> LPadTargetBlocks; |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 192 | |
Reid Kleckner | d5afc62f | 2015-07-07 23:23:03 +0000 | [diff] [blame] | 193 | // Map from outlined handler to call to parent local address. Only used for |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 194 | // 32-bit EH. |
| 195 | DenseMap<Function *, Value *> HandlerToParentFP; |
| 196 | |
Reid Kleckner | 582786b | 2015-04-30 18:17:12 +0000 | [diff] [blame] | 197 | AllocaInst *SEHExceptionCodeSlot = nullptr; |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 198 | |
| 199 | std::map<BasicBlock *, std::set<BasicBlock *>> BlockColors; |
| 200 | std::map<BasicBlock *, std::set<BasicBlock *>> FuncletBlocks; |
Joseph Tremoulet | ec18285 | 2015-08-28 01:12:35 +0000 | [diff] [blame] | 201 | std::map<BasicBlock *, std::set<BasicBlock *>> FuncletChildren; |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | class WinEHFrameVariableMaterializer : public ValueMaterializer { |
| 205 | public: |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 206 | WinEHFrameVariableMaterializer(Function *OutlinedFn, Value *ParentFP, |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 207 | FrameVarInfoMap &FrameVarInfo); |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 208 | ~WinEHFrameVariableMaterializer() override {} |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 209 | |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 210 | Value *materializeValueFor(Value *V) override; |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 211 | |
Reid Kleckner | 3567d27 | 2015-04-02 21:13:31 +0000 | [diff] [blame] | 212 | void escapeCatchObject(Value *V); |
| 213 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 214 | private: |
| 215 | FrameVarInfoMap &FrameVarInfo; |
| 216 | IRBuilder<> Builder; |
| 217 | }; |
| 218 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 219 | class LandingPadMap { |
| 220 | public: |
| 221 | LandingPadMap() : OriginLPad(nullptr) {} |
| 222 | void mapLandingPad(const LandingPadInst *LPad); |
| 223 | |
| 224 | bool isInitialized() { return OriginLPad != nullptr; } |
| 225 | |
Andrew Kaylor | f7118ae | 2015-03-27 22:31:12 +0000 | [diff] [blame] | 226 | bool isOriginLandingPadBlock(const BasicBlock *BB) const; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 227 | bool isLandingPadSpecificInst(const Instruction *Inst) const; |
| 228 | |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 229 | void remapEHValues(ValueToValueMapTy &VMap, Value *EHPtrValue, |
| 230 | Value *SelectorValue) const; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 231 | |
| 232 | private: |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 233 | const LandingPadInst *OriginLPad; |
| 234 | // We will normally only see one of each of these instructions, but |
| 235 | // if more than one occurs for some reason we can handle that. |
| 236 | TinyPtrVector<const ExtractValueInst *> ExtractedEHPtrs; |
| 237 | TinyPtrVector<const ExtractValueInst *> ExtractedSelectors; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 238 | }; |
| 239 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 240 | class WinEHCloningDirectorBase : public CloningDirector { |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 241 | public: |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 242 | WinEHCloningDirectorBase(Function *HandlerFn, Value *ParentFP, |
| 243 | FrameVarInfoMap &VarInfo, LandingPadMap &LPadMap) |
| 244 | : Materializer(HandlerFn, ParentFP, VarInfo), |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 245 | SelectorIDType(Type::getInt32Ty(HandlerFn->getContext())), |
| 246 | Int8PtrType(Type::getInt8PtrTy(HandlerFn->getContext())), |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 247 | LPadMap(LPadMap), ParentFP(ParentFP) {} |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 248 | |
| 249 | CloningAction handleInstruction(ValueToValueMapTy &VMap, |
| 250 | const Instruction *Inst, |
| 251 | BasicBlock *NewBB) override; |
| 252 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 253 | virtual CloningAction handleBeginCatch(ValueToValueMapTy &VMap, |
| 254 | const Instruction *Inst, |
| 255 | BasicBlock *NewBB) = 0; |
| 256 | virtual CloningAction handleEndCatch(ValueToValueMapTy &VMap, |
| 257 | const Instruction *Inst, |
| 258 | BasicBlock *NewBB) = 0; |
| 259 | virtual CloningAction handleTypeIdFor(ValueToValueMapTy &VMap, |
| 260 | const Instruction *Inst, |
| 261 | BasicBlock *NewBB) = 0; |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 262 | virtual CloningAction handleIndirectBr(ValueToValueMapTy &VMap, |
| 263 | const IndirectBrInst *IBr, |
| 264 | BasicBlock *NewBB) = 0; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 265 | virtual CloningAction handleInvoke(ValueToValueMapTy &VMap, |
| 266 | const InvokeInst *Invoke, |
| 267 | BasicBlock *NewBB) = 0; |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 268 | virtual CloningAction handleResume(ValueToValueMapTy &VMap, |
| 269 | const ResumeInst *Resume, |
| 270 | BasicBlock *NewBB) = 0; |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 271 | virtual CloningAction handleCompare(ValueToValueMapTy &VMap, |
| 272 | const CmpInst *Compare, |
| 273 | BasicBlock *NewBB) = 0; |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 274 | virtual CloningAction handleLandingPad(ValueToValueMapTy &VMap, |
| 275 | const LandingPadInst *LPad, |
| 276 | BasicBlock *NewBB) = 0; |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 277 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 278 | ValueMaterializer *getValueMaterializer() override { return &Materializer; } |
| 279 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 280 | protected: |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 281 | WinEHFrameVariableMaterializer Materializer; |
| 282 | Type *SelectorIDType; |
| 283 | Type *Int8PtrType; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 284 | LandingPadMap &LPadMap; |
Reid Kleckner | f14787d | 2015-04-22 00:07:52 +0000 | [diff] [blame] | 285 | |
| 286 | /// The value representing the parent frame pointer. |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 287 | Value *ParentFP; |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 288 | }; |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 289 | |
| 290 | class WinEHCatchDirector : public WinEHCloningDirectorBase { |
| 291 | public: |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 292 | WinEHCatchDirector( |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 293 | Function *CatchFn, Value *ParentFP, Value *Selector, |
| 294 | FrameVarInfoMap &VarInfo, LandingPadMap &LPadMap, |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 295 | DenseMap<LandingPadInst *, const LandingPadInst *> &NestedLPads, |
| 296 | DominatorTree *DT, SmallPtrSetImpl<BasicBlock *> &EHBlocks) |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 297 | : WinEHCloningDirectorBase(CatchFn, ParentFP, VarInfo, LPadMap), |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 298 | CurrentSelector(Selector->stripPointerCasts()), |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 299 | ExceptionObjectVar(nullptr), NestedLPtoOriginalLP(NestedLPads), |
| 300 | DT(DT), EHBlocks(EHBlocks) {} |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 301 | |
| 302 | CloningAction handleBeginCatch(ValueToValueMapTy &VMap, |
| 303 | const Instruction *Inst, |
| 304 | BasicBlock *NewBB) override; |
| 305 | CloningAction handleEndCatch(ValueToValueMapTy &VMap, const Instruction *Inst, |
| 306 | BasicBlock *NewBB) override; |
| 307 | CloningAction handleTypeIdFor(ValueToValueMapTy &VMap, |
| 308 | const Instruction *Inst, |
| 309 | BasicBlock *NewBB) override; |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 310 | CloningAction handleIndirectBr(ValueToValueMapTy &VMap, |
| 311 | const IndirectBrInst *IBr, |
| 312 | BasicBlock *NewBB) override; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 313 | CloningAction handleInvoke(ValueToValueMapTy &VMap, const InvokeInst *Invoke, |
| 314 | BasicBlock *NewBB) override; |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 315 | CloningAction handleResume(ValueToValueMapTy &VMap, const ResumeInst *Resume, |
| 316 | BasicBlock *NewBB) override; |
Andrew Kaylor | 9130743 | 2015-04-28 22:01:51 +0000 | [diff] [blame] | 317 | CloningAction handleCompare(ValueToValueMapTy &VMap, const CmpInst *Compare, |
| 318 | BasicBlock *NewBB) override; |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 319 | CloningAction handleLandingPad(ValueToValueMapTy &VMap, |
| 320 | const LandingPadInst *LPad, |
| 321 | BasicBlock *NewBB) override; |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 322 | |
Reid Kleckner | 3567d27 | 2015-04-02 21:13:31 +0000 | [diff] [blame] | 323 | Value *getExceptionVar() { return ExceptionObjectVar; } |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 324 | TinyPtrVector<BasicBlock *> &getReturnTargets() { return ReturnTargets; } |
| 325 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 326 | private: |
| 327 | Value *CurrentSelector; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 328 | |
Reid Kleckner | 3567d27 | 2015-04-02 21:13:31 +0000 | [diff] [blame] | 329 | Value *ExceptionObjectVar; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 330 | TinyPtrVector<BasicBlock *> ReturnTargets; |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 331 | |
| 332 | // This will be a reference to the field of the same name in the WinEHPrepare |
| 333 | // object which instantiates this WinEHCatchDirector object. |
| 334 | DenseMap<LandingPadInst *, const LandingPadInst *> &NestedLPtoOriginalLP; |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 335 | DominatorTree *DT; |
| 336 | SmallPtrSetImpl<BasicBlock *> &EHBlocks; |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 337 | }; |
| 338 | |
| 339 | class WinEHCleanupDirector : public WinEHCloningDirectorBase { |
| 340 | public: |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 341 | WinEHCleanupDirector(Function *CleanupFn, Value *ParentFP, |
| 342 | FrameVarInfoMap &VarInfo, LandingPadMap &LPadMap) |
| 343 | : WinEHCloningDirectorBase(CleanupFn, ParentFP, VarInfo, |
| 344 | LPadMap) {} |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 345 | |
| 346 | CloningAction handleBeginCatch(ValueToValueMapTy &VMap, |
| 347 | const Instruction *Inst, |
| 348 | BasicBlock *NewBB) override; |
| 349 | CloningAction handleEndCatch(ValueToValueMapTy &VMap, const Instruction *Inst, |
| 350 | BasicBlock *NewBB) override; |
| 351 | CloningAction handleTypeIdFor(ValueToValueMapTy &VMap, |
| 352 | const Instruction *Inst, |
| 353 | BasicBlock *NewBB) override; |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 354 | CloningAction handleIndirectBr(ValueToValueMapTy &VMap, |
| 355 | const IndirectBrInst *IBr, |
| 356 | BasicBlock *NewBB) override; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 357 | CloningAction handleInvoke(ValueToValueMapTy &VMap, const InvokeInst *Invoke, |
| 358 | BasicBlock *NewBB) override; |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 359 | CloningAction handleResume(ValueToValueMapTy &VMap, const ResumeInst *Resume, |
| 360 | BasicBlock *NewBB) override; |
Andrew Kaylor | 9130743 | 2015-04-28 22:01:51 +0000 | [diff] [blame] | 361 | CloningAction handleCompare(ValueToValueMapTy &VMap, const CmpInst *Compare, |
| 362 | BasicBlock *NewBB) override; |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 363 | CloningAction handleLandingPad(ValueToValueMapTy &VMap, |
| 364 | const LandingPadInst *LPad, |
| 365 | BasicBlock *NewBB) override; |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 366 | }; |
| 367 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 368 | class LandingPadActions { |
| 369 | public: |
| 370 | LandingPadActions() : HasCleanupHandlers(false) {} |
| 371 | |
| 372 | void insertCatchHandler(CatchHandler *Action) { Actions.push_back(Action); } |
| 373 | void insertCleanupHandler(CleanupHandler *Action) { |
| 374 | Actions.push_back(Action); |
| 375 | HasCleanupHandlers = true; |
| 376 | } |
| 377 | |
| 378 | bool includesCleanup() const { return HasCleanupHandlers; } |
| 379 | |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 380 | SmallVectorImpl<ActionHandler *> &actions() { return Actions; } |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 381 | SmallVectorImpl<ActionHandler *>::iterator begin() { return Actions.begin(); } |
| 382 | SmallVectorImpl<ActionHandler *>::iterator end() { return Actions.end(); } |
| 383 | |
| 384 | private: |
| 385 | // Note that this class does not own the ActionHandler objects in this vector. |
| 386 | // The ActionHandlers are owned by the CatchHandlerMap and CleanupHandlerMap |
| 387 | // in the WinEHPrepare class. |
| 388 | SmallVector<ActionHandler *, 4> Actions; |
| 389 | bool HasCleanupHandlers; |
| 390 | }; |
| 391 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 392 | } // end anonymous namespace |
| 393 | |
| 394 | char WinEHPrepare::ID = 0; |
Reid Kleckner | 47c8e7a | 2015-03-12 00:36:20 +0000 | [diff] [blame] | 395 | INITIALIZE_TM_PASS(WinEHPrepare, "winehprepare", "Prepare Windows exceptions", |
| 396 | false, false) |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 397 | |
| 398 | FunctionPass *llvm::createWinEHPass(const TargetMachine *TM) { |
| 399 | return new WinEHPrepare(TM); |
| 400 | } |
| 401 | |
David Majnemer | f828a0c | 2015-10-01 18:44:59 +0000 | [diff] [blame] | 402 | static bool |
| 403 | findExceptionalConstructs(Function &Fn, |
| 404 | SmallVectorImpl<LandingPadInst *> &LPads, |
| 405 | SmallVectorImpl<ResumeInst *> &Resumes, |
| 406 | SmallVectorImpl<BasicBlock *> &EntryBlocks) { |
| 407 | bool ForExplicitEH = false; |
| 408 | for (BasicBlock &BB : Fn) { |
| 409 | Instruction *First = BB.getFirstNonPHI(); |
| 410 | if (auto *LP = dyn_cast<LandingPadInst>(First)) { |
| 411 | LPads.push_back(LP); |
| 412 | } else if (First->isEHPad()) { |
| 413 | if (!ForExplicitEH) |
| 414 | EntryBlocks.push_back(&Fn.getEntryBlock()); |
| 415 | if (!isa<CatchEndPadInst>(First) && !isa<CleanupEndPadInst>(First)) |
| 416 | EntryBlocks.push_back(&BB); |
| 417 | ForExplicitEH = true; |
| 418 | } |
| 419 | if (auto *Resume = dyn_cast<ResumeInst>(BB.getTerminator())) |
| 420 | Resumes.push_back(Resume); |
| 421 | } |
| 422 | return ForExplicitEH; |
| 423 | } |
| 424 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 425 | bool WinEHPrepare::runOnFunction(Function &Fn) { |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 426 | if (!Fn.hasPersonalityFn()) |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 427 | return false; |
| 428 | |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 429 | // No need to prepare outlined handlers. |
| 430 | if (Fn.hasFnAttribute("wineh-parent")) |
David Majnemer | 09e1fdb | 2015-08-06 21:13:51 +0000 | [diff] [blame] | 431 | return false; |
| 432 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 433 | // Classify the personality to see what kind of preparation we need. |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 434 | Personality = classifyEHPersonality(Fn.getPersonalityFn()); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 435 | |
Joseph Tremoulet | 2afea54 | 2015-10-06 20:28:16 +0000 | [diff] [blame] | 436 | // Do nothing if this is not a funclet-based personality. |
| 437 | if (!isFuncletEHPersonality(Personality)) |
Reid Kleckner | 47c8e7a | 2015-03-12 00:36:20 +0000 | [diff] [blame] | 438 | return false; |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 439 | |
David Majnemer | c289c9f | 2015-10-07 21:08:25 +0000 | [diff] [blame] | 440 | // Remove unreachable blocks. It is not valuable to assign them a color and |
| 441 | // their existence can trick us into thinking values are alive when they are |
| 442 | // not. |
| 443 | removeUnreachableBlocks(Fn); |
| 444 | |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 445 | SmallVector<LandingPadInst *, 4> LPads; |
| 446 | SmallVector<ResumeInst *, 4> Resumes; |
Joseph Tremoulet | ec18285 | 2015-08-28 01:12:35 +0000 | [diff] [blame] | 447 | SmallVector<BasicBlock *, 4> EntryBlocks; |
David Majnemer | f828a0c | 2015-10-01 18:44:59 +0000 | [diff] [blame] | 448 | bool ForExplicitEH = |
| 449 | findExceptionalConstructs(Fn, LPads, Resumes, EntryBlocks); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 450 | |
| 451 | if (ForExplicitEH) |
Joseph Tremoulet | ec18285 | 2015-08-28 01:12:35 +0000 | [diff] [blame] | 452 | return prepareExplicitEH(Fn, EntryBlocks); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 453 | |
| 454 | // No need to prepare functions that lack landing pads. |
| 455 | if (LPads.empty()) |
| 456 | return false; |
| 457 | |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 458 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 459 | LibInfo = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 460 | |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 461 | // If there were any landing pads, prepareExceptionHandlers will make changes. |
| 462 | prepareExceptionHandlers(Fn, LPads); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 463 | return true; |
| 464 | } |
| 465 | |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 466 | bool WinEHPrepare::doFinalization(Module &M) { return false; } |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 467 | |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 468 | void WinEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const { |
| 469 | AU.addRequired<DominatorTreeWrapperPass>(); |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 470 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 471 | } |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 472 | |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 473 | static bool isSelectorDispatch(BasicBlock *BB, BasicBlock *&CatchHandler, |
| 474 | Constant *&Selector, BasicBlock *&NextBB); |
| 475 | |
| 476 | // Finds blocks reachable from the starting set Worklist. Does not follow unwind |
| 477 | // edges or blocks listed in StopPoints. |
| 478 | static void findReachableBlocks(SmallPtrSetImpl<BasicBlock *> &ReachableBBs, |
| 479 | SetVector<BasicBlock *> &Worklist, |
| 480 | const SetVector<BasicBlock *> *StopPoints) { |
| 481 | while (!Worklist.empty()) { |
| 482 | BasicBlock *BB = Worklist.pop_back_val(); |
| 483 | |
| 484 | // Don't cross blocks that we should stop at. |
| 485 | if (StopPoints && StopPoints->count(BB)) |
| 486 | continue; |
| 487 | |
| 488 | if (!ReachableBBs.insert(BB).second) |
| 489 | continue; // Already visited. |
| 490 | |
| 491 | // Don't follow unwind edges of invokes. |
| 492 | if (auto *II = dyn_cast<InvokeInst>(BB->getTerminator())) { |
| 493 | Worklist.insert(II->getNormalDest()); |
| 494 | continue; |
| 495 | } |
| 496 | |
| 497 | // Otherwise, follow all successors. |
| 498 | Worklist.insert(succ_begin(BB), succ_end(BB)); |
| 499 | } |
| 500 | } |
| 501 | |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 502 | // Attempt to find an instruction where a block can be split before |
| 503 | // a call to llvm.eh.begincatch and its operands. If the block |
| 504 | // begins with the begincatch call or one of its adjacent operands |
| 505 | // the block will not be split. |
| 506 | static Instruction *findBeginCatchSplitPoint(BasicBlock *BB, |
| 507 | IntrinsicInst *II) { |
| 508 | // If the begincatch call is already the first instruction in the block, |
| 509 | // don't split. |
| 510 | Instruction *FirstNonPHI = BB->getFirstNonPHI(); |
| 511 | if (II == FirstNonPHI) |
| 512 | return nullptr; |
| 513 | |
| 514 | // If either operand is in the same basic block as the instruction and |
| 515 | // isn't used by another instruction before the begincatch call, include it |
| 516 | // in the split block. |
| 517 | auto *Op0 = dyn_cast<Instruction>(II->getOperand(0)); |
| 518 | auto *Op1 = dyn_cast<Instruction>(II->getOperand(1)); |
| 519 | |
| 520 | Instruction *I = II->getPrevNode(); |
| 521 | Instruction *LastI = II; |
| 522 | |
| 523 | while (I == Op0 || I == Op1) { |
| 524 | // If the block begins with one of the operands and there are no other |
| 525 | // instructions between the operand and the begincatch call, don't split. |
| 526 | if (I == FirstNonPHI) |
| 527 | return nullptr; |
| 528 | |
| 529 | LastI = I; |
| 530 | I = I->getPrevNode(); |
| 531 | } |
| 532 | |
| 533 | // If there is at least one instruction in the block before the begincatch |
| 534 | // call and its operands, split the block at either the begincatch or |
| 535 | // its operand. |
| 536 | return LastI; |
| 537 | } |
| 538 | |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 539 | /// Find all points where exceptional control rejoins normal control flow via |
| 540 | /// llvm.eh.endcatch. Add them to the normal bb reachability worklist. |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 541 | void WinEHPrepare::findCXXEHReturnPoints( |
| 542 | Function &F, SetVector<BasicBlock *> &EHReturnBlocks) { |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 543 | for (auto BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI) { |
| 544 | BasicBlock *BB = BBI; |
| 545 | for (Instruction &I : *BB) { |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 546 | if (match(&I, m_Intrinsic<Intrinsic::eh_begincatch>())) { |
Andrew Kaylor | 9130743 | 2015-04-28 22:01:51 +0000 | [diff] [blame] | 547 | Instruction *SplitPt = |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 548 | findBeginCatchSplitPoint(BB, cast<IntrinsicInst>(&I)); |
| 549 | if (SplitPt) { |
| 550 | // Split the block before the llvm.eh.begincatch call to allow |
| 551 | // cleanup and catch code to be distinguished later. |
| 552 | // Do not update BBI because we still need to process the |
| 553 | // portion of the block that we are splitting off. |
Andrew Kaylor | a33f159 | 2015-04-29 17:21:26 +0000 | [diff] [blame] | 554 | SplitBlock(BB, SplitPt, DT); |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 555 | break; |
| 556 | } |
| 557 | } |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 558 | if (match(&I, m_Intrinsic<Intrinsic::eh_endcatch>())) { |
| 559 | // Split the block after the call to llvm.eh.endcatch if there is |
| 560 | // anything other than an unconditional branch, or if the successor |
| 561 | // starts with a phi. |
| 562 | auto *Br = dyn_cast<BranchInst>(I.getNextNode()); |
| 563 | if (!Br || !Br->isUnconditional() || |
| 564 | isa<PHINode>(Br->getSuccessor(0)->begin())) { |
| 565 | DEBUG(dbgs() << "splitting block " << BB->getName() |
| 566 | << " with llvm.eh.endcatch\n"); |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 567 | BBI = SplitBlock(BB, I.getNextNode(), DT); |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 568 | } |
| 569 | // The next BB is normal control flow. |
| 570 | EHReturnBlocks.insert(BB->getTerminator()->getSuccessor(0)); |
| 571 | break; |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | static bool isCatchAllLandingPad(const BasicBlock *BB) { |
| 578 | const LandingPadInst *LP = BB->getLandingPadInst(); |
| 579 | if (!LP) |
| 580 | return false; |
| 581 | unsigned N = LP->getNumClauses(); |
| 582 | return (N > 0 && LP->isCatch(N - 1) && |
| 583 | isa<ConstantPointerNull>(LP->getClause(N - 1))); |
| 584 | } |
| 585 | |
| 586 | /// Find all points where exceptions control rejoins normal control flow via |
| 587 | /// selector dispatch. |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 588 | void WinEHPrepare::findSEHEHReturnPoints( |
| 589 | Function &F, SetVector<BasicBlock *> &EHReturnBlocks) { |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 590 | for (auto BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI) { |
| 591 | BasicBlock *BB = BBI; |
| 592 | // If the landingpad is a catch-all, treat the whole lpad as if it is |
| 593 | // reachable from normal control flow. |
| 594 | // FIXME: This is imprecise. We need a better way of identifying where a |
| 595 | // catch-all starts and cleanups stop. As far as LLVM is concerned, there |
| 596 | // is no difference. |
| 597 | if (isCatchAllLandingPad(BB)) { |
| 598 | EHReturnBlocks.insert(BB); |
| 599 | continue; |
| 600 | } |
| 601 | |
| 602 | BasicBlock *CatchHandler; |
| 603 | BasicBlock *NextBB; |
| 604 | Constant *Selector; |
| 605 | if (isSelectorDispatch(BB, CatchHandler, Selector, NextBB)) { |
Reid Kleckner | ed012db | 2015-07-08 18:08:52 +0000 | [diff] [blame] | 606 | // Split the edge if there are multiple predecessors. This creates a place |
| 607 | // where we can insert EH recovery code. |
| 608 | if (!CatchHandler->getSinglePredecessor()) { |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 609 | DEBUG(dbgs() << "splitting EH return edge from " << BB->getName() |
| 610 | << " to " << CatchHandler->getName() << '\n'); |
| 611 | BBI = CatchHandler = SplitCriticalEdge( |
| 612 | BB, std::find(succ_begin(BB), succ_end(BB), CatchHandler)); |
| 613 | } |
| 614 | EHReturnBlocks.insert(CatchHandler); |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 619 | void WinEHPrepare::identifyEHBlocks(Function &F, |
| 620 | SmallVectorImpl<LandingPadInst *> &LPads) { |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 621 | DEBUG(dbgs() << "Demoting values live across exception handlers in function " |
| 622 | << F.getName() << '\n'); |
| 623 | |
| 624 | // Build a set of all non-exceptional blocks and exceptional blocks. |
| 625 | // - Non-exceptional blocks are blocks reachable from the entry block while |
| 626 | // not following invoke unwind edges. |
| 627 | // - Exceptional blocks are blocks reachable from landingpads. Analysis does |
| 628 | // not follow llvm.eh.endcatch blocks, which mark a transition from |
| 629 | // exceptional to normal control. |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 630 | |
| 631 | if (Personality == EHPersonality::MSVC_CXX) |
| 632 | findCXXEHReturnPoints(F, EHReturnBlocks); |
| 633 | else |
| 634 | findSEHEHReturnPoints(F, EHReturnBlocks); |
| 635 | |
| 636 | DEBUG({ |
| 637 | dbgs() << "identified the following blocks as EH return points:\n"; |
| 638 | for (BasicBlock *BB : EHReturnBlocks) |
| 639 | dbgs() << " " << BB->getName() << '\n'; |
| 640 | }); |
| 641 | |
Andrew Kaylor | 9130743 | 2015-04-28 22:01:51 +0000 | [diff] [blame] | 642 | // Join points should not have phis at this point, unless they are a |
| 643 | // landingpad, in which case we will demote their phis later. |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 644 | #ifndef NDEBUG |
| 645 | for (BasicBlock *BB : EHReturnBlocks) |
| 646 | assert((BB->isLandingPad() || !isa<PHINode>(BB->begin())) && |
| 647 | "non-lpad EH return block has phi"); |
| 648 | #endif |
| 649 | |
| 650 | // Normal blocks are the blocks reachable from the entry block and all EH |
| 651 | // return points. |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 652 | SetVector<BasicBlock *> Worklist; |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 653 | Worklist = EHReturnBlocks; |
| 654 | Worklist.insert(&F.getEntryBlock()); |
| 655 | findReachableBlocks(NormalBlocks, Worklist, nullptr); |
| 656 | DEBUG({ |
| 657 | dbgs() << "marked the following blocks as normal:\n"; |
| 658 | for (BasicBlock *BB : NormalBlocks) |
| 659 | dbgs() << " " << BB->getName() << '\n'; |
| 660 | }); |
| 661 | |
| 662 | // Exceptional blocks are the blocks reachable from landingpads that don't |
| 663 | // cross EH return points. |
| 664 | Worklist.clear(); |
| 665 | for (auto *LPI : LPads) |
| 666 | Worklist.insert(LPI->getParent()); |
| 667 | findReachableBlocks(EHBlocks, Worklist, &EHReturnBlocks); |
| 668 | DEBUG({ |
| 669 | dbgs() << "marked the following blocks as exceptional:\n"; |
| 670 | for (BasicBlock *BB : EHBlocks) |
| 671 | dbgs() << " " << BB->getName() << '\n'; |
| 672 | }); |
| 673 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /// Ensure that all values live into and out of exception handlers are stored |
| 677 | /// in memory. |
| 678 | /// FIXME: This falls down when values are defined in one handler and live into |
| 679 | /// another handler. For example, a cleanup defines a value used only by a |
| 680 | /// catch handler. |
| 681 | void WinEHPrepare::demoteValuesLiveAcrossHandlers( |
| 682 | Function &F, SmallVectorImpl<LandingPadInst *> &LPads) { |
| 683 | DEBUG(dbgs() << "Demoting values live across exception handlers in function " |
| 684 | << F.getName() << '\n'); |
| 685 | |
| 686 | // identifyEHBlocks() should have been called before this function. |
| 687 | assert(!NormalBlocks.empty()); |
| 688 | |
Reid Kleckner | 7ea7708 | 2015-07-10 22:21:54 +0000 | [diff] [blame] | 689 | // Try to avoid demoting EH pointer and selector values. They get in the way |
| 690 | // of our pattern matching. |
| 691 | SmallPtrSet<Instruction *, 10> EHVals; |
| 692 | for (BasicBlock &BB : F) { |
| 693 | LandingPadInst *LP = BB.getLandingPadInst(); |
| 694 | if (!LP) |
| 695 | continue; |
| 696 | EHVals.insert(LP); |
| 697 | for (User *U : LP->users()) { |
| 698 | auto *EI = dyn_cast<ExtractValueInst>(U); |
| 699 | if (!EI) |
| 700 | continue; |
| 701 | EHVals.insert(EI); |
| 702 | for (User *U2 : EI->users()) { |
| 703 | if (auto *PN = dyn_cast<PHINode>(U2)) |
| 704 | EHVals.insert(PN); |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 709 | SetVector<Argument *> ArgsToDemote; |
| 710 | SetVector<Instruction *> InstrsToDemote; |
| 711 | for (BasicBlock &BB : F) { |
| 712 | bool IsNormalBB = NormalBlocks.count(&BB); |
| 713 | bool IsEHBB = EHBlocks.count(&BB); |
| 714 | if (!IsNormalBB && !IsEHBB) |
| 715 | continue; // Blocks that are neither normal nor EH are unreachable. |
| 716 | for (Instruction &I : BB) { |
| 717 | for (Value *Op : I.operands()) { |
| 718 | // Don't demote static allocas, constants, and labels. |
| 719 | if (isa<Constant>(Op) || isa<BasicBlock>(Op) || isa<InlineAsm>(Op)) |
| 720 | continue; |
| 721 | auto *AI = dyn_cast<AllocaInst>(Op); |
| 722 | if (AI && AI->isStaticAlloca()) |
| 723 | continue; |
| 724 | |
| 725 | if (auto *Arg = dyn_cast<Argument>(Op)) { |
| 726 | if (IsEHBB) { |
| 727 | DEBUG(dbgs() << "Demoting argument " << *Arg |
| 728 | << " used by EH instr: " << I << "\n"); |
| 729 | ArgsToDemote.insert(Arg); |
| 730 | } |
| 731 | continue; |
| 732 | } |
| 733 | |
Reid Kleckner | 7ea7708 | 2015-07-10 22:21:54 +0000 | [diff] [blame] | 734 | // Don't demote EH values. |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 735 | auto *OpI = cast<Instruction>(Op); |
Reid Kleckner | 7ea7708 | 2015-07-10 22:21:54 +0000 | [diff] [blame] | 736 | if (EHVals.count(OpI)) |
| 737 | continue; |
| 738 | |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 739 | BasicBlock *OpBB = OpI->getParent(); |
| 740 | // If a value is produced and consumed in the same BB, we don't need to |
| 741 | // demote it. |
| 742 | if (OpBB == &BB) |
| 743 | continue; |
| 744 | bool IsOpNormalBB = NormalBlocks.count(OpBB); |
| 745 | bool IsOpEHBB = EHBlocks.count(OpBB); |
| 746 | if (IsNormalBB != IsOpNormalBB || IsEHBB != IsOpEHBB) { |
| 747 | DEBUG({ |
| 748 | dbgs() << "Demoting instruction live in-out from EH:\n"; |
| 749 | dbgs() << "Instr: " << *OpI << '\n'; |
| 750 | dbgs() << "User: " << I << '\n'; |
| 751 | }); |
| 752 | InstrsToDemote.insert(OpI); |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | // Demote values live into and out of handlers. |
| 759 | // FIXME: This demotion is inefficient. We should insert spills at the point |
| 760 | // of definition, insert one reload in each handler that uses the value, and |
| 761 | // insert reloads in the BB used to rejoin normal control flow. |
| 762 | Instruction *AllocaInsertPt = F.getEntryBlock().getFirstInsertionPt(); |
| 763 | for (Instruction *I : InstrsToDemote) |
| 764 | DemoteRegToStack(*I, false, AllocaInsertPt); |
| 765 | |
| 766 | // Demote arguments separately, and only for uses in EH blocks. |
| 767 | for (Argument *Arg : ArgsToDemote) { |
| 768 | auto *Slot = new AllocaInst(Arg->getType(), nullptr, |
| 769 | Arg->getName() + ".reg2mem", AllocaInsertPt); |
| 770 | SmallVector<User *, 4> Users(Arg->user_begin(), Arg->user_end()); |
| 771 | for (User *U : Users) { |
| 772 | auto *I = dyn_cast<Instruction>(U); |
| 773 | if (I && EHBlocks.count(I->getParent())) { |
| 774 | auto *Reload = new LoadInst(Slot, Arg->getName() + ".reload", false, I); |
| 775 | U->replaceUsesOfWith(Arg, Reload); |
| 776 | } |
| 777 | } |
| 778 | new StoreInst(Arg, Slot, AllocaInsertPt); |
| 779 | } |
| 780 | |
| 781 | // Demote landingpad phis, as the landingpad will be removed from the machine |
| 782 | // CFG. |
| 783 | for (LandingPadInst *LPI : LPads) { |
| 784 | BasicBlock *BB = LPI->getParent(); |
| 785 | while (auto *Phi = dyn_cast<PHINode>(BB->begin())) |
| 786 | DemotePHIToStack(Phi, AllocaInsertPt); |
| 787 | } |
| 788 | |
| 789 | DEBUG(dbgs() << "Demoted " << InstrsToDemote.size() << " instructions and " |
| 790 | << ArgsToDemote.size() << " arguments for WinEHPrepare\n\n"); |
| 791 | } |
| 792 | |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 793 | bool WinEHPrepare::prepareExceptionHandlers( |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 794 | Function &F, SmallVectorImpl<LandingPadInst *> &LPads) { |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 795 | // Don't run on functions that are already prepared. |
| 796 | for (LandingPadInst *LPad : LPads) { |
| 797 | BasicBlock *LPadBB = LPad->getParent(); |
| 798 | for (Instruction &Inst : *LPadBB) |
| 799 | if (match(&Inst, m_Intrinsic<Intrinsic::eh_actions>())) |
| 800 | return false; |
| 801 | } |
| 802 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 803 | identifyEHBlocks(F, LPads); |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 804 | demoteValuesLiveAcrossHandlers(F, LPads); |
| 805 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 806 | // These containers are used to re-map frame variables that are used in |
| 807 | // outlined catch and cleanup handlers. They will be populated as the |
| 808 | // handlers are outlined. |
| 809 | FrameVarInfoMap FrameVarInfo; |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 810 | |
| 811 | bool HandlersOutlined = false; |
| 812 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 813 | Module *M = F.getParent(); |
| 814 | LLVMContext &Context = M->getContext(); |
| 815 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 816 | // Create a new function to receive the handler contents. |
| 817 | PointerType *Int8PtrType = Type::getInt8PtrTy(Context); |
| 818 | Type *Int32Type = Type::getInt32Ty(Context); |
Reid Kleckner | 52b0779 | 2015-03-12 01:45:37 +0000 | [diff] [blame] | 819 | Function *ActionIntrin = Intrinsic::getDeclaration(M, Intrinsic::eh_actions); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 820 | |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 821 | if (isAsynchronousEHPersonality(Personality)) { |
| 822 | // FIXME: Switch the ehptr type to i32 and then switch this. |
| 823 | SEHExceptionCodeSlot = |
| 824 | new AllocaInst(Int8PtrType, nullptr, "seh_exception_code", |
| 825 | F.getEntryBlock().getFirstInsertionPt()); |
| 826 | } |
| 827 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 828 | // In order to handle the case where one outlined catch handler returns |
| 829 | // to a block within another outlined catch handler that would otherwise |
| 830 | // be unreachable, we need to outline the nested landing pad before we |
| 831 | // outline the landing pad which encloses it. |
Manuel Klimek | b00d42c | 2015-05-21 15:38:25 +0000 | [diff] [blame] | 832 | if (!isAsynchronousEHPersonality(Personality)) |
| 833 | std::sort(LPads.begin(), LPads.end(), |
| 834 | [this](LandingPadInst *const &L, LandingPadInst *const &R) { |
| 835 | return DT->properlyDominates(R->getParent(), L->getParent()); |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 836 | }); |
| 837 | |
Andrew Kaylor | cc14f38 | 2015-05-11 23:06:02 +0000 | [diff] [blame] | 838 | // This container stores the llvm.eh.recover and IndirectBr instructions |
| 839 | // that make up the body of each landing pad after it has been outlined. |
| 840 | // We need to defer the population of the target list for the indirectbr |
| 841 | // until all landing pads have been outlined so that we can handle the |
| 842 | // case of blocks in the target that are reached only from nested |
| 843 | // landing pads. |
| 844 | SmallVector<std::pair<CallInst*, IndirectBrInst *>, 4> LPadImpls; |
| 845 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 846 | for (LandingPadInst *LPad : LPads) { |
| 847 | // Look for evidence that this landingpad has already been processed. |
| 848 | bool LPadHasActionList = false; |
| 849 | BasicBlock *LPadBB = LPad->getParent(); |
Reid Kleckner | c759fe9 | 2015-03-19 22:31:02 +0000 | [diff] [blame] | 850 | for (Instruction &Inst : *LPadBB) { |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 851 | if (match(&Inst, m_Intrinsic<Intrinsic::eh_actions>())) { |
| 852 | LPadHasActionList = true; |
| 853 | break; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 854 | } |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | // If we've already outlined the handlers for this landingpad, |
| 858 | // there's nothing more to do here. |
| 859 | if (LPadHasActionList) |
| 860 | continue; |
| 861 | |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 862 | // If either of the values in the aggregate returned by the landing pad is |
| 863 | // extracted and stored to memory, promote the stored value to a register. |
| 864 | promoteLandingPadValues(LPad); |
| 865 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 866 | LandingPadActions Actions; |
| 867 | mapLandingPadBlocks(LPad, Actions); |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 868 | |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 869 | HandlersOutlined |= !Actions.actions().empty(); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 870 | for (ActionHandler *Action : Actions) { |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 871 | if (Action->hasBeenProcessed()) |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 872 | continue; |
| 873 | BasicBlock *StartBB = Action->getStartBlock(); |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 874 | |
| 875 | // SEH doesn't do any outlining for catches. Instead, pass the handler |
| 876 | // basic block addr to llvm.eh.actions and list the block as a return |
| 877 | // target. |
| 878 | if (isAsynchronousEHPersonality(Personality)) { |
| 879 | if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) { |
| 880 | processSEHCatchHandler(CatchAction, StartBB); |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 881 | continue; |
| 882 | } |
| 883 | } |
| 884 | |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 885 | outlineHandler(Action, &F, LPad, StartBB, FrameVarInfo); |
| 886 | } |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 887 | |
Reid Kleckner | 2c3ccaa | 2015-04-24 16:22:19 +0000 | [diff] [blame] | 888 | // Split the block after the landingpad instruction so that it is just a |
| 889 | // call to llvm.eh.actions followed by indirectbr. |
| 890 | assert(!isa<PHINode>(LPadBB->begin()) && "lpad phi not removed"); |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 891 | SplitBlock(LPadBB, LPad->getNextNode(), DT); |
Reid Kleckner | 2c3ccaa | 2015-04-24 16:22:19 +0000 | [diff] [blame] | 892 | // Erase the branch inserted by the split so we can insert indirectbr. |
| 893 | LPadBB->getTerminator()->eraseFromParent(); |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 894 | |
Reid Kleckner | e3af86e | 2015-04-23 21:22:30 +0000 | [diff] [blame] | 895 | // Replace all extracted values with undef and ultimately replace the |
| 896 | // landingpad with undef. |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 897 | SmallVector<Instruction *, 4> SEHCodeUses; |
| 898 | SmallVector<Instruction *, 4> EHUndefs; |
Reid Kleckner | e3af86e | 2015-04-23 21:22:30 +0000 | [diff] [blame] | 899 | for (User *U : LPad->users()) { |
| 900 | auto *E = dyn_cast<ExtractValueInst>(U); |
| 901 | if (!E) |
| 902 | continue; |
| 903 | assert(E->getNumIndices() == 1 && |
| 904 | "Unexpected operation: extracting both landing pad values"); |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 905 | unsigned Idx = *E->idx_begin(); |
| 906 | assert((Idx == 0 || Idx == 1) && "unexpected index"); |
| 907 | if (Idx == 0 && isAsynchronousEHPersonality(Personality)) |
| 908 | SEHCodeUses.push_back(E); |
| 909 | else |
| 910 | EHUndefs.push_back(E); |
Reid Kleckner | e3af86e | 2015-04-23 21:22:30 +0000 | [diff] [blame] | 911 | } |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 912 | for (Instruction *E : EHUndefs) { |
Reid Kleckner | e3af86e | 2015-04-23 21:22:30 +0000 | [diff] [blame] | 913 | E->replaceAllUsesWith(UndefValue::get(E->getType())); |
| 914 | E->eraseFromParent(); |
| 915 | } |
| 916 | LPad->replaceAllUsesWith(UndefValue::get(LPad->getType())); |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 917 | |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 918 | // Rewrite uses of the exception pointer to loads of an alloca. |
Reid Kleckner | 7ea7708 | 2015-07-10 22:21:54 +0000 | [diff] [blame] | 919 | while (!SEHCodeUses.empty()) { |
| 920 | Instruction *E = SEHCodeUses.pop_back_val(); |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 921 | SmallVector<Use *, 4> Uses; |
| 922 | for (Use &U : E->uses()) |
| 923 | Uses.push_back(&U); |
| 924 | for (Use *U : Uses) { |
| 925 | auto *I = cast<Instruction>(U->getUser()); |
| 926 | if (isa<ResumeInst>(I)) |
| 927 | continue; |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 928 | if (auto *Phi = dyn_cast<PHINode>(I)) |
Reid Kleckner | 7ea7708 | 2015-07-10 22:21:54 +0000 | [diff] [blame] | 929 | SEHCodeUses.push_back(Phi); |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 930 | else |
Reid Kleckner | 7ea7708 | 2015-07-10 22:21:54 +0000 | [diff] [blame] | 931 | U->set(new LoadInst(SEHExceptionCodeSlot, "sehcode", false, I)); |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 932 | } |
| 933 | E->replaceAllUsesWith(UndefValue::get(E->getType())); |
| 934 | E->eraseFromParent(); |
| 935 | } |
| 936 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 937 | // Add a call to describe the actions for this landing pad. |
| 938 | std::vector<Value *> ActionArgs; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 939 | for (ActionHandler *Action : Actions) { |
Reid Kleckner | c759fe9 | 2015-03-19 22:31:02 +0000 | [diff] [blame] | 940 | // Action codes from docs are: 0 cleanup, 1 catch. |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 941 | if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) { |
Reid Kleckner | c759fe9 | 2015-03-19 22:31:02 +0000 | [diff] [blame] | 942 | ActionArgs.push_back(ConstantInt::get(Int32Type, 1)); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 943 | ActionArgs.push_back(CatchAction->getSelector()); |
Reid Kleckner | 3567d27 | 2015-04-02 21:13:31 +0000 | [diff] [blame] | 944 | // Find the frame escape index of the exception object alloca in the |
| 945 | // parent. |
| 946 | int FrameEscapeIdx = -1; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 947 | Value *EHObj = const_cast<Value *>(CatchAction->getExceptionVar()); |
Reid Kleckner | 3567d27 | 2015-04-02 21:13:31 +0000 | [diff] [blame] | 948 | if (EHObj && !isa<ConstantPointerNull>(EHObj)) { |
| 949 | auto I = FrameVarInfo.find(EHObj); |
| 950 | assert(I != FrameVarInfo.end() && |
| 951 | "failed to map llvm.eh.begincatch var"); |
| 952 | FrameEscapeIdx = std::distance(FrameVarInfo.begin(), I); |
| 953 | } |
| 954 | ActionArgs.push_back(ConstantInt::get(Int32Type, FrameEscapeIdx)); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 955 | } else { |
Reid Kleckner | c759fe9 | 2015-03-19 22:31:02 +0000 | [diff] [blame] | 956 | ActionArgs.push_back(ConstantInt::get(Int32Type, 0)); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 957 | } |
Reid Kleckner | c759fe9 | 2015-03-19 22:31:02 +0000 | [diff] [blame] | 958 | ActionArgs.push_back(Action->getHandlerBlockOrFunc()); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 959 | } |
| 960 | CallInst *Recover = |
Reid Kleckner | 2c3ccaa | 2015-04-24 16:22:19 +0000 | [diff] [blame] | 961 | CallInst::Create(ActionIntrin, ActionArgs, "recover", LPadBB); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 962 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 963 | SetVector<BasicBlock *> ReturnTargets; |
| 964 | for (ActionHandler *Action : Actions) { |
| 965 | if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) { |
| 966 | const auto &CatchTargets = CatchAction->getReturnTargets(); |
| 967 | ReturnTargets.insert(CatchTargets.begin(), CatchTargets.end()); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 968 | } |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 969 | } |
| 970 | IndirectBrInst *Branch = |
| 971 | IndirectBrInst::Create(Recover, ReturnTargets.size(), LPadBB); |
| 972 | for (BasicBlock *Target : ReturnTargets) |
| 973 | Branch->addDestination(Target); |
Andrew Kaylor | cc14f38 | 2015-05-11 23:06:02 +0000 | [diff] [blame] | 974 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 975 | if (!isAsynchronousEHPersonality(Personality)) { |
| 976 | // C++ EH must repopulate the targets later to handle the case of |
| 977 | // targets that are reached indirectly through nested landing pads. |
Andrew Kaylor | cc14f38 | 2015-05-11 23:06:02 +0000 | [diff] [blame] | 978 | LPadImpls.push_back(std::make_pair(Recover, Branch)); |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 979 | } |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 980 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 981 | } // End for each landingpad |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 982 | |
| 983 | // If nothing got outlined, there is no more processing to be done. |
| 984 | if (!HandlersOutlined) |
| 985 | return false; |
| 986 | |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 987 | // Replace any nested landing pad stubs with the correct action handler. |
| 988 | // This must be done before we remove unreachable blocks because it |
| 989 | // cleans up references to outlined blocks that will be deleted. |
| 990 | for (auto &LPadPair : NestedLPtoOriginalLP) |
| 991 | completeNestedLandingPad(&F, LPadPair.first, LPadPair.second, FrameVarInfo); |
Andrew Kaylor | 67d3c03 | 2015-04-08 20:57:22 +0000 | [diff] [blame] | 992 | NestedLPtoOriginalLP.clear(); |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 993 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 994 | // Update the indirectbr instructions' target lists if necessary. |
Andrew Kaylor | cc14f38 | 2015-05-11 23:06:02 +0000 | [diff] [blame] | 995 | SetVector<BasicBlock*> CheckedTargets; |
Benjamin Kramer | a48e065 | 2015-05-16 15:40:03 +0000 | [diff] [blame] | 996 | SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList; |
Andrew Kaylor | cc14f38 | 2015-05-11 23:06:02 +0000 | [diff] [blame] | 997 | for (auto &LPadImplPair : LPadImpls) { |
| 998 | IntrinsicInst *Recover = cast<IntrinsicInst>(LPadImplPair.first); |
| 999 | IndirectBrInst *Branch = LPadImplPair.second; |
| 1000 | |
| 1001 | // Get a list of handlers called by |
Andrew Kaylor | cc14f38 | 2015-05-11 23:06:02 +0000 | [diff] [blame] | 1002 | parseEHActions(Recover, ActionList); |
| 1003 | |
| 1004 | // Add an indirect branch listing possible successors of the catch handlers. |
| 1005 | SetVector<BasicBlock *> ReturnTargets; |
Benjamin Kramer | a48e065 | 2015-05-16 15:40:03 +0000 | [diff] [blame] | 1006 | for (const auto &Action : ActionList) { |
| 1007 | if (auto *CA = dyn_cast<CatchHandler>(Action.get())) { |
Andrew Kaylor | cc14f38 | 2015-05-11 23:06:02 +0000 | [diff] [blame] | 1008 | Function *Handler = cast<Function>(CA->getHandlerBlockOrFunc()); |
| 1009 | getPossibleReturnTargets(&F, Handler, ReturnTargets); |
| 1010 | } |
| 1011 | } |
Andrew Kaylor | 0ddaf2b | 2015-05-12 00:13:51 +0000 | [diff] [blame] | 1012 | ActionList.clear(); |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 1013 | // Clear any targets we already knew about. |
| 1014 | for (unsigned int I = 0, E = Branch->getNumDestinations(); I < E; ++I) { |
| 1015 | BasicBlock *KnownTarget = Branch->getDestination(I); |
| 1016 | if (ReturnTargets.count(KnownTarget)) |
| 1017 | ReturnTargets.remove(KnownTarget); |
| 1018 | } |
Andrew Kaylor | cc14f38 | 2015-05-11 23:06:02 +0000 | [diff] [blame] | 1019 | for (BasicBlock *Target : ReturnTargets) { |
| 1020 | Branch->addDestination(Target); |
| 1021 | // The target may be a block that we excepted to get pruned. |
| 1022 | // If it is, it may contain a call to llvm.eh.endcatch. |
| 1023 | if (CheckedTargets.insert(Target)) { |
| 1024 | // Earlier preparations guarantee that all calls to llvm.eh.endcatch |
| 1025 | // will be followed by an unconditional branch. |
| 1026 | auto *Br = dyn_cast<BranchInst>(Target->getTerminator()); |
| 1027 | if (Br && Br->isUnconditional() && |
| 1028 | Br != Target->getFirstNonPHIOrDbgOrLifetime()) { |
| 1029 | Instruction *Prev = Br->getPrevNode(); |
| 1030 | if (match(cast<Value>(Prev), m_Intrinsic<Intrinsic::eh_endcatch>())) |
| 1031 | Prev->eraseFromParent(); |
| 1032 | } |
| 1033 | } |
| 1034 | } |
| 1035 | } |
| 1036 | LPadImpls.clear(); |
| 1037 | |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 1038 | F.addFnAttr("wineh-parent", F.getName()); |
| 1039 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1040 | // Delete any blocks that were only used by handlers that were outlined above. |
| 1041 | removeUnreachableBlocks(F); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1042 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1043 | BasicBlock *Entry = &F.getEntryBlock(); |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 1044 | IRBuilder<> Builder(F.getParent()->getContext()); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1045 | Builder.SetInsertPoint(Entry->getFirstInsertionPt()); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1046 | |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 1047 | Function *FrameEscapeFn = |
Reid Kleckner | 6038179 | 2015-07-07 22:25:32 +0000 | [diff] [blame] | 1048 | Intrinsic::getDeclaration(M, Intrinsic::localescape); |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 1049 | Function *RecoverFrameFn = |
Reid Kleckner | 6038179 | 2015-07-07 22:25:32 +0000 | [diff] [blame] | 1050 | Intrinsic::getDeclaration(M, Intrinsic::localrecover); |
Reid Kleckner | f14787d | 2015-04-22 00:07:52 +0000 | [diff] [blame] | 1051 | SmallVector<Value *, 8> AllocasToEscape; |
| 1052 | |
Reid Kleckner | 6038179 | 2015-07-07 22:25:32 +0000 | [diff] [blame] | 1053 | // Scan the entry block for an existing call to llvm.localescape. We need to |
Reid Kleckner | f14787d | 2015-04-22 00:07:52 +0000 | [diff] [blame] | 1054 | // keep escaping those objects. |
| 1055 | for (Instruction &I : F.front()) { |
| 1056 | auto *II = dyn_cast<IntrinsicInst>(&I); |
Reid Kleckner | 6038179 | 2015-07-07 22:25:32 +0000 | [diff] [blame] | 1057 | if (II && II->getIntrinsicID() == Intrinsic::localescape) { |
Reid Kleckner | f14787d | 2015-04-22 00:07:52 +0000 | [diff] [blame] | 1058 | auto Args = II->arg_operands(); |
| 1059 | AllocasToEscape.append(Args.begin(), Args.end()); |
| 1060 | II->eraseFromParent(); |
| 1061 | break; |
| 1062 | } |
| 1063 | } |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1064 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1065 | // Finally, replace all of the temporary allocas for frame variables used in |
Reid Kleckner | 6038179 | 2015-07-07 22:25:32 +0000 | [diff] [blame] | 1066 | // the outlined handlers with calls to llvm.localrecover. |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1067 | for (auto &VarInfoEntry : FrameVarInfo) { |
Andrew Kaylor | 72029c6 | 2015-03-03 00:41:03 +0000 | [diff] [blame] | 1068 | Value *ParentVal = VarInfoEntry.first; |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 1069 | TinyPtrVector<AllocaInst *> &Allocas = VarInfoEntry.second; |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 1070 | AllocaInst *ParentAlloca = cast<AllocaInst>(ParentVal); |
Andrew Kaylor | 72029c6 | 2015-03-03 00:41:03 +0000 | [diff] [blame] | 1071 | |
Reid Kleckner | b401941 | 2015-04-06 18:50:38 +0000 | [diff] [blame] | 1072 | // FIXME: We should try to sink unescaped allocas from the parent frame into |
| 1073 | // the child frame. If the alloca is escaped, we have to use the lifetime |
| 1074 | // markers to ensure that the alloca is only live within the child frame. |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1075 | |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 1076 | // Add this alloca to the list of things to escape. |
| 1077 | AllocasToEscape.push_back(ParentAlloca); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1078 | |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 1079 | // Next replace all outlined allocas that are mapped to it. |
| 1080 | for (AllocaInst *TempAlloca : Allocas) { |
Reid Kleckner | 3567d27 | 2015-04-02 21:13:31 +0000 | [diff] [blame] | 1081 | if (TempAlloca == getCatchObjectSentinel()) |
| 1082 | continue; // Skip catch parameter sentinels. |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 1083 | Function *HandlerFn = TempAlloca->getParent()->getParent(); |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1084 | llvm::Value *FP = HandlerToParentFP[HandlerFn]; |
| 1085 | assert(FP); |
| 1086 | |
Reid Kleckner | 6038179 | 2015-07-07 22:25:32 +0000 | [diff] [blame] | 1087 | // FIXME: Sink this localrecover into the blocks where it is used. |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 1088 | Builder.SetInsertPoint(TempAlloca); |
| 1089 | Builder.SetCurrentDebugLocation(TempAlloca->getDebugLoc()); |
| 1090 | Value *RecoverArgs[] = { |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1091 | Builder.CreateBitCast(&F, Int8PtrType, ""), FP, |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 1092 | llvm::ConstantInt::get(Int32Type, AllocasToEscape.size() - 1)}; |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1093 | Instruction *RecoveredAlloca = |
| 1094 | Builder.CreateCall(RecoverFrameFn, RecoverArgs); |
| 1095 | |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 1096 | // Add a pointer bitcast if the alloca wasn't an i8. |
| 1097 | if (RecoveredAlloca->getType() != TempAlloca->getType()) { |
| 1098 | RecoveredAlloca->setName(Twine(TempAlloca->getName()) + ".i8"); |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1099 | RecoveredAlloca = cast<Instruction>( |
| 1100 | Builder.CreateBitCast(RecoveredAlloca, TempAlloca->getType())); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1101 | } |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 1102 | TempAlloca->replaceAllUsesWith(RecoveredAlloca); |
| 1103 | TempAlloca->removeFromParent(); |
| 1104 | RecoveredAlloca->takeName(TempAlloca); |
| 1105 | delete TempAlloca; |
| 1106 | } |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1107 | } // End for each FrameVarInfo entry. |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1108 | |
Reid Kleckner | 6038179 | 2015-07-07 22:25:32 +0000 | [diff] [blame] | 1109 | // Insert 'call void (...)* @llvm.localescape(...)' at the end of the entry |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 1110 | // block. |
| 1111 | Builder.SetInsertPoint(&F.getEntryBlock().back()); |
| 1112 | Builder.CreateCall(FrameEscapeFn, AllocasToEscape); |
| 1113 | |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 1114 | if (SEHExceptionCodeSlot) { |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 1115 | if (isAllocaPromotable(SEHExceptionCodeSlot)) { |
| 1116 | SmallPtrSet<BasicBlock *, 4> UserBlocks; |
| 1117 | for (User *U : SEHExceptionCodeSlot->users()) { |
| 1118 | if (auto *Inst = dyn_cast<Instruction>(U)) |
| 1119 | UserBlocks.insert(Inst->getParent()); |
| 1120 | } |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 1121 | PromoteMemToReg(SEHExceptionCodeSlot, *DT); |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 1122 | // After the promotion, kill off dead instructions. |
| 1123 | for (BasicBlock *BB : UserBlocks) |
| 1124 | SimplifyInstructionsInBlock(BB, LibInfo); |
| 1125 | } |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1128 | // Clean up the handler action maps we created for this function |
| 1129 | DeleteContainerSeconds(CatchHandlerMap); |
| 1130 | CatchHandlerMap.clear(); |
| 1131 | DeleteContainerSeconds(CleanupHandlerMap); |
| 1132 | CleanupHandlerMap.clear(); |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1133 | HandlerToParentFP.clear(); |
| 1134 | DT = nullptr; |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 1135 | LibInfo = nullptr; |
Ahmed Bougacha | ed363c5 | 2015-05-06 01:28:58 +0000 | [diff] [blame] | 1136 | SEHExceptionCodeSlot = nullptr; |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 1137 | EHBlocks.clear(); |
| 1138 | NormalBlocks.clear(); |
| 1139 | EHReturnBlocks.clear(); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1140 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1141 | return HandlersOutlined; |
| 1142 | } |
| 1143 | |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 1144 | void WinEHPrepare::promoteLandingPadValues(LandingPadInst *LPad) { |
| 1145 | // If the return values of the landing pad instruction are extracted and |
| 1146 | // stored to memory, we want to promote the store locations to reg values. |
| 1147 | SmallVector<AllocaInst *, 2> EHAllocas; |
| 1148 | |
| 1149 | // The landingpad instruction returns an aggregate value. Typically, its |
| 1150 | // value will be passed to a pair of extract value instructions and the |
| 1151 | // results of those extracts are often passed to store instructions. |
| 1152 | // In unoptimized code the stored value will often be loaded and then stored |
| 1153 | // again. |
| 1154 | for (auto *U : LPad->users()) { |
| 1155 | ExtractValueInst *Extract = dyn_cast<ExtractValueInst>(U); |
| 1156 | if (!Extract) |
| 1157 | continue; |
| 1158 | |
| 1159 | for (auto *EU : Extract->users()) { |
| 1160 | if (auto *Store = dyn_cast<StoreInst>(EU)) { |
| 1161 | auto *AV = cast<AllocaInst>(Store->getPointerOperand()); |
| 1162 | EHAllocas.push_back(AV); |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | // We can't do this without a dominator tree. |
| 1168 | assert(DT); |
| 1169 | |
| 1170 | if (!EHAllocas.empty()) { |
| 1171 | PromoteMemToReg(EHAllocas, *DT); |
| 1172 | EHAllocas.clear(); |
| 1173 | } |
Reid Kleckner | 8676214 | 2015-04-16 00:02:04 +0000 | [diff] [blame] | 1174 | |
| 1175 | // After promotion, some extracts may be trivially dead. Remove them. |
| 1176 | SmallVector<Value *, 4> Users(LPad->user_begin(), LPad->user_end()); |
| 1177 | for (auto *U : Users) |
| 1178 | RecursivelyDeleteTriviallyDeadInstructions(U); |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
Andrew Kaylor | cc14f38 | 2015-05-11 23:06:02 +0000 | [diff] [blame] | 1181 | void WinEHPrepare::getPossibleReturnTargets(Function *ParentF, |
| 1182 | Function *HandlerF, |
| 1183 | SetVector<BasicBlock*> &Targets) { |
| 1184 | for (BasicBlock &BB : *HandlerF) { |
| 1185 | // If the handler contains landing pads, check for any |
| 1186 | // handlers that may return directly to a block in the |
| 1187 | // parent function. |
| 1188 | if (auto *LPI = BB.getLandingPadInst()) { |
| 1189 | IntrinsicInst *Recover = cast<IntrinsicInst>(LPI->getNextNode()); |
Benjamin Kramer | a48e065 | 2015-05-16 15:40:03 +0000 | [diff] [blame] | 1190 | SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList; |
Andrew Kaylor | cc14f38 | 2015-05-11 23:06:02 +0000 | [diff] [blame] | 1191 | parseEHActions(Recover, ActionList); |
Benjamin Kramer | a48e065 | 2015-05-16 15:40:03 +0000 | [diff] [blame] | 1192 | for (const auto &Action : ActionList) { |
| 1193 | if (auto *CH = dyn_cast<CatchHandler>(Action.get())) { |
Andrew Kaylor | cc14f38 | 2015-05-11 23:06:02 +0000 | [diff] [blame] | 1194 | Function *NestedF = cast<Function>(CH->getHandlerBlockOrFunc()); |
| 1195 | getPossibleReturnTargets(ParentF, NestedF, Targets); |
| 1196 | } |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()); |
| 1201 | if (!Ret) |
| 1202 | continue; |
| 1203 | |
| 1204 | // Handler functions must always return a block address. |
| 1205 | BlockAddress *BA = cast<BlockAddress>(Ret->getReturnValue()); |
| 1206 | |
| 1207 | // If this is the handler for a nested landing pad, the |
| 1208 | // return address may have been remapped to a block in the |
| 1209 | // parent handler. We're not interested in those. |
| 1210 | if (BA->getFunction() != ParentF) |
| 1211 | continue; |
| 1212 | |
| 1213 | Targets.insert(BA->getBasicBlock()); |
| 1214 | } |
| 1215 | } |
| 1216 | |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1217 | void WinEHPrepare::completeNestedLandingPad(Function *ParentFn, |
| 1218 | LandingPadInst *OutlinedLPad, |
| 1219 | const LandingPadInst *OriginalLPad, |
| 1220 | FrameVarInfoMap &FrameVarInfo) { |
| 1221 | // Get the nested block and erase the unreachable instruction that was |
| 1222 | // temporarily inserted as its terminator. |
| 1223 | LLVMContext &Context = ParentFn->getContext(); |
| 1224 | BasicBlock *OutlinedBB = OutlinedLPad->getParent(); |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 1225 | // If the nested landing pad was outlined before the landing pad that enclosed |
| 1226 | // it, it will already be in outlined form. In that case, we just need to see |
| 1227 | // if the returns and the enclosing branch instruction need to be updated. |
| 1228 | IndirectBrInst *Branch = |
| 1229 | dyn_cast<IndirectBrInst>(OutlinedBB->getTerminator()); |
| 1230 | if (!Branch) { |
| 1231 | // If the landing pad wasn't in outlined form, it should be a stub with |
| 1232 | // an unreachable terminator. |
| 1233 | assert(isa<UnreachableInst>(OutlinedBB->getTerminator())); |
| 1234 | OutlinedBB->getTerminator()->eraseFromParent(); |
| 1235 | // That should leave OutlinedLPad as the last instruction in its block. |
| 1236 | assert(&OutlinedBB->back() == OutlinedLPad); |
| 1237 | } |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1238 | |
| 1239 | // The original landing pad will have already had its action intrinsic |
| 1240 | // built by the outlining loop. We need to clone that into the outlined |
| 1241 | // location. It may also be necessary to add references to the exception |
| 1242 | // variables to the outlined handler in which this landing pad is nested |
| 1243 | // and remap return instructions in the nested handlers that should return |
| 1244 | // to an address in the outlined handler. |
| 1245 | Function *OutlinedHandlerFn = OutlinedBB->getParent(); |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1246 | BasicBlock::const_iterator II = OriginalLPad; |
| 1247 | ++II; |
| 1248 | // The instruction after the landing pad should now be a call to eh.actions. |
| 1249 | const Instruction *Recover = II; |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 1250 | const IntrinsicInst *EHActions = cast<IntrinsicInst>(Recover); |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1251 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 1252 | // Remap the return target in the nested handler. |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1253 | SmallVector<BlockAddress *, 4> ActionTargets; |
Benjamin Kramer | a48e065 | 2015-05-16 15:40:03 +0000 | [diff] [blame] | 1254 | SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList; |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1255 | parseEHActions(EHActions, ActionList); |
Benjamin Kramer | a48e065 | 2015-05-16 15:40:03 +0000 | [diff] [blame] | 1256 | for (const auto &Action : ActionList) { |
| 1257 | auto *Catch = dyn_cast<CatchHandler>(Action.get()); |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1258 | if (!Catch) |
| 1259 | continue; |
| 1260 | // The dyn_cast to function here selects C++ catch handlers and skips |
| 1261 | // SEH catch handlers. |
| 1262 | auto *Handler = dyn_cast<Function>(Catch->getHandlerBlockOrFunc()); |
| 1263 | if (!Handler) |
| 1264 | continue; |
| 1265 | // Visit all the return instructions, looking for places that return |
| 1266 | // to a location within OutlinedHandlerFn. |
| 1267 | for (BasicBlock &NestedHandlerBB : *Handler) { |
| 1268 | auto *Ret = dyn_cast<ReturnInst>(NestedHandlerBB.getTerminator()); |
| 1269 | if (!Ret) |
| 1270 | continue; |
| 1271 | |
| 1272 | // Handler functions must always return a block address. |
| 1273 | BlockAddress *BA = cast<BlockAddress>(Ret->getReturnValue()); |
| 1274 | // The original target will have been in the main parent function, |
| 1275 | // but if it is the address of a block that has been outlined, it |
| 1276 | // should be a block that was outlined into OutlinedHandlerFn. |
| 1277 | assert(BA->getFunction() == ParentFn); |
| 1278 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 1279 | // Ignore targets that aren't part of an outlined handler function. |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1280 | if (!LPadTargetBlocks.count(BA->getBasicBlock())) |
| 1281 | continue; |
| 1282 | |
| 1283 | // If the return value is the address ofF a block that we |
| 1284 | // previously outlined into the parent handler function, replace |
| 1285 | // the return instruction and add the mapped target to the list |
| 1286 | // of possible return addresses. |
| 1287 | BasicBlock *MappedBB = LPadTargetBlocks[BA->getBasicBlock()]; |
| 1288 | assert(MappedBB->getParent() == OutlinedHandlerFn); |
| 1289 | BlockAddress *NewBA = BlockAddress::get(OutlinedHandlerFn, MappedBB); |
| 1290 | Ret->eraseFromParent(); |
| 1291 | ReturnInst::Create(Context, NewBA, &NestedHandlerBB); |
| 1292 | ActionTargets.push_back(NewBA); |
| 1293 | } |
| 1294 | } |
Andrew Kaylor | 7a0cec3 | 2015-04-03 21:44:17 +0000 | [diff] [blame] | 1295 | ActionList.clear(); |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1296 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 1297 | if (Branch) { |
| 1298 | // If the landing pad was already in outlined form, just update its targets. |
| 1299 | for (unsigned int I = Branch->getNumDestinations(); I > 0; --I) |
| 1300 | Branch->removeDestination(I); |
| 1301 | // Add the previously collected action targets. |
| 1302 | for (auto *Target : ActionTargets) |
| 1303 | Branch->addDestination(Target->getBasicBlock()); |
| 1304 | } else { |
| 1305 | // If the landing pad was previously stubbed out, fill in its outlined form. |
| 1306 | IntrinsicInst *NewEHActions = cast<IntrinsicInst>(EHActions->clone()); |
| 1307 | OutlinedBB->getInstList().push_back(NewEHActions); |
| 1308 | |
| 1309 | // Insert an indirect branch into the outlined landing pad BB. |
| 1310 | IndirectBrInst *IBr = IndirectBrInst::Create(NewEHActions, 0, OutlinedBB); |
| 1311 | // Add the previously collected action targets. |
| 1312 | for (auto *Target : ActionTargets) |
| 1313 | IBr->addDestination(Target->getBasicBlock()); |
| 1314 | } |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1317 | // This function examines a block to determine whether the block ends with a |
| 1318 | // conditional branch to a catch handler based on a selector comparison. |
| 1319 | // This function is used both by the WinEHPrepare::findSelectorComparison() and |
| 1320 | // WinEHCleanupDirector::handleTypeIdFor(). |
| 1321 | static bool isSelectorDispatch(BasicBlock *BB, BasicBlock *&CatchHandler, |
| 1322 | Constant *&Selector, BasicBlock *&NextBB) { |
| 1323 | ICmpInst::Predicate Pred; |
| 1324 | BasicBlock *TBB, *FBB; |
| 1325 | Value *LHS, *RHS; |
| 1326 | |
| 1327 | if (!match(BB->getTerminator(), |
| 1328 | m_Br(m_ICmp(Pred, m_Value(LHS), m_Value(RHS)), TBB, FBB))) |
| 1329 | return false; |
| 1330 | |
| 1331 | if (!match(LHS, |
| 1332 | m_Intrinsic<Intrinsic::eh_typeid_for>(m_Constant(Selector))) && |
| 1333 | !match(RHS, m_Intrinsic<Intrinsic::eh_typeid_for>(m_Constant(Selector)))) |
| 1334 | return false; |
| 1335 | |
| 1336 | if (Pred == CmpInst::ICMP_EQ) { |
| 1337 | CatchHandler = TBB; |
| 1338 | NextBB = FBB; |
| 1339 | return true; |
| 1340 | } |
| 1341 | |
| 1342 | if (Pred == CmpInst::ICMP_NE) { |
| 1343 | CatchHandler = FBB; |
| 1344 | NextBB = TBB; |
| 1345 | return true; |
| 1346 | } |
| 1347 | |
| 1348 | return false; |
| 1349 | } |
| 1350 | |
Andrew Kaylor | 4175851 | 2015-04-20 22:04:09 +0000 | [diff] [blame] | 1351 | static bool isCatchBlock(BasicBlock *BB) { |
| 1352 | for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end(); |
| 1353 | II != IE; ++II) { |
| 1354 | if (match(cast<Value>(II), m_Intrinsic<Intrinsic::eh_begincatch>())) |
| 1355 | return true; |
| 1356 | } |
| 1357 | return false; |
| 1358 | } |
| 1359 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 1360 | static BasicBlock *createStubLandingPad(Function *Handler) { |
Andrew Kaylor | bb11132 | 2015-04-07 21:30:23 +0000 | [diff] [blame] | 1361 | // FIXME: Finish this! |
| 1362 | LLVMContext &Context = Handler->getContext(); |
| 1363 | BasicBlock *StubBB = BasicBlock::Create(Context, "stub"); |
| 1364 | Handler->getBasicBlockList().push_back(StubBB); |
| 1365 | IRBuilder<> Builder(StubBB); |
| 1366 | LandingPadInst *LPad = Builder.CreateLandingPad( |
| 1367 | llvm::StructType::get(Type::getInt8PtrTy(Context), |
| 1368 | Type::getInt32Ty(Context), nullptr), |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 1369 | 0); |
Andrew Kaylor | 43e1d76 | 2015-04-23 00:20:44 +0000 | [diff] [blame] | 1370 | // Insert a call to llvm.eh.actions so that we don't try to outline this lpad. |
Andrew Kaylor | 9130743 | 2015-04-28 22:01:51 +0000 | [diff] [blame] | 1371 | Function *ActionIntrin = |
| 1372 | Intrinsic::getDeclaration(Handler->getParent(), Intrinsic::eh_actions); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 1373 | Builder.CreateCall(ActionIntrin, {}, "recover"); |
Andrew Kaylor | bb11132 | 2015-04-07 21:30:23 +0000 | [diff] [blame] | 1374 | LPad->setCleanup(true); |
| 1375 | Builder.CreateUnreachable(); |
| 1376 | return StubBB; |
| 1377 | } |
| 1378 | |
| 1379 | // Cycles through the blocks in an outlined handler function looking for an |
| 1380 | // invoke instruction and inserts an invoke of llvm.donothing with an empty |
| 1381 | // landing pad if none is found. The code that generates the .xdata tables for |
| 1382 | // the handler needs at least one landing pad to identify the parent function's |
| 1383 | // personality. |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 1384 | void WinEHPrepare::addStubInvokeToHandlerIfNeeded(Function *Handler) { |
Andrew Kaylor | bb11132 | 2015-04-07 21:30:23 +0000 | [diff] [blame] | 1385 | ReturnInst *Ret = nullptr; |
Andrew Kaylor | 5f71552 | 2015-04-23 18:37:39 +0000 | [diff] [blame] | 1386 | UnreachableInst *Unreached = nullptr; |
Andrew Kaylor | bb11132 | 2015-04-07 21:30:23 +0000 | [diff] [blame] | 1387 | for (BasicBlock &BB : *Handler) { |
| 1388 | TerminatorInst *Terminator = BB.getTerminator(); |
| 1389 | // If we find an invoke, there is nothing to be done. |
| 1390 | auto *II = dyn_cast<InvokeInst>(Terminator); |
| 1391 | if (II) |
| 1392 | return; |
| 1393 | // If we've already recorded a return instruction, keep looking for invokes. |
Andrew Kaylor | 5f71552 | 2015-04-23 18:37:39 +0000 | [diff] [blame] | 1394 | if (!Ret) |
| 1395 | Ret = dyn_cast<ReturnInst>(Terminator); |
| 1396 | // If we haven't recorded an unreachable instruction, try this terminator. |
| 1397 | if (!Unreached) |
| 1398 | Unreached = dyn_cast<UnreachableInst>(Terminator); |
Andrew Kaylor | bb11132 | 2015-04-07 21:30:23 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | // If we got this far, the handler contains no invokes. We should have seen |
Andrew Kaylor | 5f71552 | 2015-04-23 18:37:39 +0000 | [diff] [blame] | 1402 | // at least one return or unreachable instruction. We'll insert an invoke of |
| 1403 | // llvm.donothing ahead of that instruction. |
| 1404 | assert(Ret || Unreached); |
| 1405 | TerminatorInst *Term; |
| 1406 | if (Ret) |
| 1407 | Term = Ret; |
| 1408 | else |
| 1409 | Term = Unreached; |
| 1410 | BasicBlock *OldRetBB = Term->getParent(); |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 1411 | BasicBlock *NewRetBB = SplitBlock(OldRetBB, Term, DT); |
Andrew Kaylor | bb11132 | 2015-04-07 21:30:23 +0000 | [diff] [blame] | 1412 | // SplitBlock adds an unconditional branch instruction at the end of the |
| 1413 | // parent block. We want to replace that with an invoke call, so we can |
| 1414 | // erase it now. |
| 1415 | OldRetBB->getTerminator()->eraseFromParent(); |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 1416 | BasicBlock *StubLandingPad = createStubLandingPad(Handler); |
Andrew Kaylor | bb11132 | 2015-04-07 21:30:23 +0000 | [diff] [blame] | 1417 | Function *F = |
| 1418 | Intrinsic::getDeclaration(Handler->getParent(), Intrinsic::donothing); |
| 1419 | InvokeInst::Create(F, NewRetBB, StubLandingPad, None, "", OldRetBB); |
| 1420 | } |
| 1421 | |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1422 | // FIXME: Consider sinking this into lib/Target/X86 somehow. TargetLowering |
| 1423 | // usually doesn't build LLVM IR, so that's probably the wrong place. |
Reid Kleckner | 6511c8b | 2015-07-01 20:59:25 +0000 | [diff] [blame] | 1424 | Function *WinEHPrepare::createHandlerFunc(Function *ParentFn, Type *RetTy, |
| 1425 | const Twine &Name, Module *M, |
| 1426 | Value *&ParentFP) { |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1427 | // x64 uses a two-argument prototype where the parent FP is the second |
| 1428 | // argument. x86 uses no arguments, just the incoming EBP value. |
| 1429 | LLVMContext &Context = M->getContext(); |
Reid Kleckner | 6511c8b | 2015-07-01 20:59:25 +0000 | [diff] [blame] | 1430 | Type *Int8PtrType = Type::getInt8PtrTy(Context); |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1431 | FunctionType *FnType; |
| 1432 | if (TheTriple.getArch() == Triple::x86_64) { |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1433 | Type *ArgTys[2] = {Int8PtrType, Int8PtrType}; |
| 1434 | FnType = FunctionType::get(RetTy, ArgTys, false); |
| 1435 | } else { |
| 1436 | FnType = FunctionType::get(RetTy, None, false); |
| 1437 | } |
| 1438 | |
| 1439 | Function *Handler = |
| 1440 | Function::Create(FnType, GlobalVariable::InternalLinkage, Name, M); |
| 1441 | BasicBlock *Entry = BasicBlock::Create(Context, "entry"); |
| 1442 | Handler->getBasicBlockList().push_front(Entry); |
| 1443 | if (TheTriple.getArch() == Triple::x86_64) { |
| 1444 | ParentFP = &(Handler->getArgumentList().back()); |
| 1445 | } else { |
| 1446 | assert(M); |
| 1447 | Function *FrameAddressFn = |
| 1448 | Intrinsic::getDeclaration(M, Intrinsic::frameaddress); |
Reid Kleckner | 6511c8b | 2015-07-01 20:59:25 +0000 | [diff] [blame] | 1449 | Function *RecoverFPFn = |
| 1450 | Intrinsic::getDeclaration(M, Intrinsic::x86_seh_recoverfp); |
| 1451 | IRBuilder<> Builder(&Handler->getEntryBlock()); |
| 1452 | Value *EBP = |
| 1453 | Builder.CreateCall(FrameAddressFn, {Builder.getInt32(1)}, "ebp"); |
| 1454 | Value *ParentI8Fn = Builder.CreateBitCast(ParentFn, Int8PtrType); |
| 1455 | ParentFP = Builder.CreateCall(RecoverFPFn, {ParentI8Fn, EBP}); |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1456 | } |
| 1457 | return Handler; |
| 1458 | } |
| 1459 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1460 | bool WinEHPrepare::outlineHandler(ActionHandler *Action, Function *SrcFn, |
| 1461 | LandingPadInst *LPad, BasicBlock *StartBB, |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1462 | FrameVarInfoMap &VarInfo) { |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1463 | Module *M = SrcFn->getParent(); |
| 1464 | LLVMContext &Context = M->getContext(); |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1465 | Type *Int8PtrType = Type::getInt8PtrTy(Context); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1466 | |
| 1467 | // Create a new function to receive the handler contents. |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1468 | Value *ParentFP; |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1469 | Function *Handler; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1470 | if (Action->getType() == Catch) { |
Reid Kleckner | 6511c8b | 2015-07-01 20:59:25 +0000 | [diff] [blame] | 1471 | Handler = createHandlerFunc(SrcFn, Int8PtrType, SrcFn->getName() + ".catch", M, |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1472 | ParentFP); |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1473 | } else { |
Reid Kleckner | 6511c8b | 2015-07-01 20:59:25 +0000 | [diff] [blame] | 1474 | Handler = createHandlerFunc(SrcFn, Type::getVoidTy(Context), |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1475 | SrcFn->getName() + ".cleanup", M, ParentFP); |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1476 | } |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 1477 | Handler->setPersonalityFn(SrcFn->getPersonalityFn()); |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1478 | HandlerToParentFP[Handler] = ParentFP; |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 1479 | Handler->addFnAttr("wineh-parent", SrcFn->getName()); |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1480 | BasicBlock *Entry = &Handler->getEntryBlock(); |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 1481 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1482 | // Generate a standard prolog to setup the frame recovery structure. |
| 1483 | IRBuilder<> Builder(Context); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1484 | Builder.SetInsertPoint(Entry); |
| 1485 | Builder.SetCurrentDebugLocation(LPad->getDebugLoc()); |
| 1486 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1487 | std::unique_ptr<WinEHCloningDirectorBase> Director; |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1488 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1489 | ValueToValueMapTy VMap; |
| 1490 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1491 | LandingPadMap &LPadMap = LPadMaps[LPad]; |
| 1492 | if (!LPadMap.isInitialized()) |
| 1493 | LPadMap.mapLandingPad(LPad); |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 1494 | if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) { |
| 1495 | Constant *Sel = CatchAction->getSelector(); |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 1496 | Director.reset(new WinEHCatchDirector(Handler, ParentFP, Sel, VarInfo, |
| 1497 | LPadMap, NestedLPtoOriginalLP, DT, |
| 1498 | EHBlocks)); |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 1499 | LPadMap.remapEHValues(VMap, UndefValue::get(Int8PtrType), |
| 1500 | ConstantInt::get(Type::getInt32Ty(Context), 1)); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1501 | } else { |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1502 | Director.reset( |
| 1503 | new WinEHCleanupDirector(Handler, ParentFP, VarInfo, LPadMap)); |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 1504 | LPadMap.remapEHValues(VMap, UndefValue::get(Int8PtrType), |
| 1505 | UndefValue::get(Type::getInt32Ty(Context))); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1506 | } |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1507 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1508 | SmallVector<ReturnInst *, 8> Returns; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1509 | ClonedCodeInfo OutlinedFunctionInfo; |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1510 | |
Andrew Kaylor | 3170e56 | 2015-03-20 21:42:54 +0000 | [diff] [blame] | 1511 | // If the start block contains PHI nodes, we need to map them. |
| 1512 | BasicBlock::iterator II = StartBB->begin(); |
| 1513 | while (auto *PN = dyn_cast<PHINode>(II)) { |
| 1514 | bool Mapped = false; |
| 1515 | // Look for PHI values that we have already mapped (such as the selector). |
| 1516 | for (Value *Val : PN->incoming_values()) { |
| 1517 | if (VMap.count(Val)) { |
| 1518 | VMap[PN] = VMap[Val]; |
| 1519 | Mapped = true; |
| 1520 | } |
| 1521 | } |
| 1522 | // If we didn't find a match for this value, map it as an undef. |
| 1523 | if (!Mapped) { |
| 1524 | VMap[PN] = UndefValue::get(PN->getType()); |
| 1525 | } |
| 1526 | ++II; |
| 1527 | } |
| 1528 | |
Andrew Kaylor | 00e5d9e | 2015-04-20 22:53:42 +0000 | [diff] [blame] | 1529 | // The landing pad value may be used by PHI nodes. It will ultimately be |
| 1530 | // eliminated, but we need it in the map for intermediate handling. |
| 1531 | VMap[LPad] = UndefValue::get(LPad->getType()); |
| 1532 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1533 | // Skip over PHIs and, if applicable, landingpad instructions. |
Andrew Kaylor | 3170e56 | 2015-03-20 21:42:54 +0000 | [diff] [blame] | 1534 | II = StartBB->getFirstInsertionPt(); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1535 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1536 | CloneAndPruneIntoFromInst(Handler, SrcFn, II, VMap, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1537 | /*ModuleLevelChanges=*/false, Returns, "", |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1538 | &OutlinedFunctionInfo, Director.get()); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1539 | |
Andrew Kaylor | 5dacfd8 | 2015-04-24 23:10:38 +0000 | [diff] [blame] | 1540 | // Move all the instructions in the cloned "entry" block into our entry block. |
| 1541 | // Depending on how the parent function was laid out, the block that will |
| 1542 | // correspond to the outlined entry block may not be the first block in the |
| 1543 | // list. We can recognize it, however, as the cloned block which has no |
| 1544 | // predecessors. Any other block wouldn't have been cloned if it didn't |
| 1545 | // have a predecessor which was also cloned. |
Andrew Kaylor | 9130743 | 2015-04-28 22:01:51 +0000 | [diff] [blame] | 1546 | Function::iterator ClonedIt = std::next(Function::iterator(Entry)); |
Andrew Kaylor | 5dacfd8 | 2015-04-24 23:10:38 +0000 | [diff] [blame] | 1547 | while (!pred_empty(ClonedIt)) |
| 1548 | ++ClonedIt; |
| 1549 | BasicBlock *ClonedEntryBB = ClonedIt; |
| 1550 | assert(ClonedEntryBB); |
| 1551 | Entry->getInstList().splice(Entry->end(), ClonedEntryBB->getInstList()); |
| 1552 | ClonedEntryBB->eraseFromParent(); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1553 | |
Andrew Kaylor | bb11132 | 2015-04-07 21:30:23 +0000 | [diff] [blame] | 1554 | // Make sure we can identify the handler's personality later. |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 1555 | addStubInvokeToHandlerIfNeeded(Handler); |
Andrew Kaylor | bb11132 | 2015-04-07 21:30:23 +0000 | [diff] [blame] | 1556 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1557 | if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) { |
| 1558 | WinEHCatchDirector *CatchDirector = |
| 1559 | reinterpret_cast<WinEHCatchDirector *>(Director.get()); |
| 1560 | CatchAction->setExceptionVar(CatchDirector->getExceptionVar()); |
| 1561 | CatchAction->setReturnTargets(CatchDirector->getReturnTargets()); |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1562 | |
| 1563 | // Look for blocks that are not part of the landing pad that we just |
| 1564 | // outlined but terminate with a call to llvm.eh.endcatch and a |
| 1565 | // branch to a block that is in the handler we just outlined. |
| 1566 | // These blocks will be part of a nested landing pad that intends to |
| 1567 | // return to an address in this handler. This case is best handled |
| 1568 | // after both landing pads have been outlined, so for now we'll just |
| 1569 | // save the association of the blocks in LPadTargetBlocks. The |
| 1570 | // return instructions which are created from these branches will be |
| 1571 | // replaced after all landing pads have been outlined. |
Richard Trieu | 6b1aa5f | 2015-04-15 01:21:15 +0000 | [diff] [blame] | 1572 | for (const auto MapEntry : VMap) { |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1573 | // VMap maps all values and blocks that were just cloned, but dead |
| 1574 | // blocks which were pruned will map to nullptr. |
| 1575 | if (!isa<BasicBlock>(MapEntry.first) || MapEntry.second == nullptr) |
| 1576 | continue; |
| 1577 | const BasicBlock *MappedBB = cast<BasicBlock>(MapEntry.first); |
| 1578 | for (auto *Pred : predecessors(const_cast<BasicBlock *>(MappedBB))) { |
| 1579 | auto *Branch = dyn_cast<BranchInst>(Pred->getTerminator()); |
| 1580 | if (!Branch || !Branch->isUnconditional() || Pred->size() <= 1) |
| 1581 | continue; |
| 1582 | BasicBlock::iterator II = const_cast<BranchInst *>(Branch); |
| 1583 | --II; |
| 1584 | if (match(cast<Value>(II), m_Intrinsic<Intrinsic::eh_endcatch>())) { |
| 1585 | // This would indicate that a nested landing pad wants to return |
| 1586 | // to a block that is outlined into two different handlers. |
| 1587 | assert(!LPadTargetBlocks.count(MappedBB)); |
| 1588 | LPadTargetBlocks[MappedBB] = cast<BasicBlock>(MapEntry.second); |
| 1589 | } |
| 1590 | } |
| 1591 | } |
| 1592 | } // End if (CatchAction) |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1593 | |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 1594 | Action->setHandlerBlockOrFunc(Handler); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1595 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1596 | return true; |
| 1597 | } |
| 1598 | |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 1599 | /// This BB must end in a selector dispatch. All we need to do is pass the |
| 1600 | /// handler block to llvm.eh.actions and list it as a possible indirectbr |
| 1601 | /// target. |
| 1602 | void WinEHPrepare::processSEHCatchHandler(CatchHandler *CatchAction, |
| 1603 | BasicBlock *StartBB) { |
| 1604 | BasicBlock *HandlerBB; |
| 1605 | BasicBlock *NextBB; |
| 1606 | Constant *Selector; |
| 1607 | bool Res = isSelectorDispatch(StartBB, HandlerBB, Selector, NextBB); |
| 1608 | if (Res) { |
| 1609 | // If this was EH dispatch, this must be a conditional branch to the handler |
| 1610 | // block. |
| 1611 | // FIXME: Handle instructions in the dispatch block. Currently we drop them, |
| 1612 | // leading to crashes if some optimization hoists stuff here. |
| 1613 | assert(CatchAction->getSelector() && HandlerBB && |
| 1614 | "expected catch EH dispatch"); |
| 1615 | } else { |
| 1616 | // This must be a catch-all. Split the block after the landingpad. |
| 1617 | assert(CatchAction->getSelector()->isNullValue() && "expected catch-all"); |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 1618 | HandlerBB = SplitBlock(StartBB, StartBB->getFirstInsertionPt(), DT); |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 1619 | } |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 1620 | IRBuilder<> Builder(HandlerBB->getFirstInsertionPt()); |
| 1621 | Function *EHCodeFn = Intrinsic::getDeclaration( |
Reid Kleckner | 72ba704 | 2015-10-07 00:27:33 +0000 | [diff] [blame] | 1622 | StartBB->getParent()->getParent(), Intrinsic::eh_exceptioncode_old); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 1623 | Value *Code = Builder.CreateCall(EHCodeFn, {}, "sehcode"); |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 1624 | Code = Builder.CreateIntToPtr(Code, SEHExceptionCodeSlot->getAllocatedType()); |
| 1625 | Builder.CreateStore(Code, SEHExceptionCodeSlot); |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 1626 | CatchAction->setHandlerBlockOrFunc(BlockAddress::get(HandlerBB)); |
| 1627 | TinyPtrVector<BasicBlock *> Targets(HandlerBB); |
| 1628 | CatchAction->setReturnTargets(Targets); |
| 1629 | } |
| 1630 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1631 | void LandingPadMap::mapLandingPad(const LandingPadInst *LPad) { |
| 1632 | // Each instance of this class should only ever be used to map a single |
| 1633 | // landing pad. |
| 1634 | assert(OriginLPad == nullptr || OriginLPad == LPad); |
| 1635 | |
| 1636 | // If the landing pad has already been mapped, there's nothing more to do. |
| 1637 | if (OriginLPad == LPad) |
| 1638 | return; |
| 1639 | |
| 1640 | OriginLPad = LPad; |
| 1641 | |
| 1642 | // The landingpad instruction returns an aggregate value. Typically, its |
| 1643 | // value will be passed to a pair of extract value instructions and the |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 1644 | // results of those extracts will have been promoted to reg values before |
| 1645 | // this routine is called. |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1646 | for (auto *U : LPad->users()) { |
| 1647 | const ExtractValueInst *Extract = dyn_cast<ExtractValueInst>(U); |
| 1648 | if (!Extract) |
| 1649 | continue; |
| 1650 | assert(Extract->getNumIndices() == 1 && |
| 1651 | "Unexpected operation: extracting both landing pad values"); |
| 1652 | unsigned int Idx = *(Extract->idx_begin()); |
| 1653 | assert((Idx == 0 || Idx == 1) && |
| 1654 | "Unexpected operation: extracting an unknown landing pad element"); |
| 1655 | if (Idx == 0) { |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1656 | ExtractedEHPtrs.push_back(Extract); |
| 1657 | } else if (Idx == 1) { |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1658 | ExtractedSelectors.push_back(Extract); |
| 1659 | } |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1660 | } |
| 1661 | } |
| 1662 | |
Andrew Kaylor | f7118ae | 2015-03-27 22:31:12 +0000 | [diff] [blame] | 1663 | bool LandingPadMap::isOriginLandingPadBlock(const BasicBlock *BB) const { |
| 1664 | return BB->getLandingPadInst() == OriginLPad; |
| 1665 | } |
| 1666 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1667 | bool LandingPadMap::isLandingPadSpecificInst(const Instruction *Inst) const { |
| 1668 | if (Inst == OriginLPad) |
| 1669 | return true; |
| 1670 | for (auto *Extract : ExtractedEHPtrs) { |
| 1671 | if (Inst == Extract) |
| 1672 | return true; |
| 1673 | } |
| 1674 | for (auto *Extract : ExtractedSelectors) { |
| 1675 | if (Inst == Extract) |
| 1676 | return true; |
| 1677 | } |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1678 | return false; |
| 1679 | } |
| 1680 | |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 1681 | void LandingPadMap::remapEHValues(ValueToValueMapTy &VMap, Value *EHPtrValue, |
| 1682 | Value *SelectorValue) const { |
| 1683 | // Remap all landing pad extract instructions to the specified values. |
| 1684 | for (auto *Extract : ExtractedEHPtrs) |
| 1685 | VMap[Extract] = EHPtrValue; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1686 | for (auto *Extract : ExtractedSelectors) |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 1687 | VMap[Extract] = SelectorValue; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
Reid Kleckner | d5afc62f | 2015-07-07 23:23:03 +0000 | [diff] [blame] | 1690 | static bool isLocalAddressCall(const Value *V) { |
| 1691 | return match(const_cast<Value *>(V), m_Intrinsic<Intrinsic::localaddress>()); |
Reid Kleckner | f14787d | 2015-04-22 00:07:52 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1694 | CloningDirector::CloningAction WinEHCloningDirectorBase::handleInstruction( |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1695 | ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) { |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1696 | // If this is one of the boilerplate landing pad instructions, skip it. |
| 1697 | // The instruction will have already been remapped in VMap. |
| 1698 | if (LPadMap.isLandingPadSpecificInst(Inst)) |
| 1699 | return CloningDirector::SkipInstruction; |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1700 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 1701 | // Nested landing pads that have not already been outlined will be cloned as |
| 1702 | // stubs, with just the landingpad instruction and an unreachable instruction. |
| 1703 | // When all landingpads have been outlined, we'll replace this with the |
| 1704 | // llvm.eh.actions call and indirect branch created when the landing pad was |
| 1705 | // outlined. |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1706 | if (auto *LPad = dyn_cast<LandingPadInst>(Inst)) { |
| 1707 | return handleLandingPad(VMap, LPad, NewBB); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 1710 | // Nested landing pads that have already been outlined will be cloned in their |
| 1711 | // outlined form, but we need to intercept the ibr instruction to filter out |
| 1712 | // targets that do not return to the handler we are outlining. |
| 1713 | if (auto *IBr = dyn_cast<IndirectBrInst>(Inst)) { |
| 1714 | return handleIndirectBr(VMap, IBr, NewBB); |
| 1715 | } |
| 1716 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1717 | if (auto *Invoke = dyn_cast<InvokeInst>(Inst)) |
| 1718 | return handleInvoke(VMap, Invoke, NewBB); |
| 1719 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1720 | if (auto *Resume = dyn_cast<ResumeInst>(Inst)) |
| 1721 | return handleResume(VMap, Resume, NewBB); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1722 | |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 1723 | if (auto *Cmp = dyn_cast<CmpInst>(Inst)) |
| 1724 | return handleCompare(VMap, Cmp, NewBB); |
| 1725 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1726 | if (match(Inst, m_Intrinsic<Intrinsic::eh_begincatch>())) |
| 1727 | return handleBeginCatch(VMap, Inst, NewBB); |
| 1728 | if (match(Inst, m_Intrinsic<Intrinsic::eh_endcatch>())) |
| 1729 | return handleEndCatch(VMap, Inst, NewBB); |
| 1730 | if (match(Inst, m_Intrinsic<Intrinsic::eh_typeid_for>())) |
| 1731 | return handleTypeIdFor(VMap, Inst, NewBB); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1732 | |
Reid Kleckner | d5afc62f | 2015-07-07 23:23:03 +0000 | [diff] [blame] | 1733 | // When outlining llvm.localaddress(), remap that to the second argument, |
Reid Kleckner | f14787d | 2015-04-22 00:07:52 +0000 | [diff] [blame] | 1734 | // which is the FP of the parent. |
Reid Kleckner | d5afc62f | 2015-07-07 23:23:03 +0000 | [diff] [blame] | 1735 | if (isLocalAddressCall(Inst)) { |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1736 | VMap[Inst] = ParentFP; |
Reid Kleckner | f14787d | 2015-04-22 00:07:52 +0000 | [diff] [blame] | 1737 | return CloningDirector::SkipInstruction; |
| 1738 | } |
| 1739 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1740 | // Continue with the default cloning behavior. |
| 1741 | return CloningDirector::CloneInstruction; |
| 1742 | } |
| 1743 | |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1744 | CloningDirector::CloningAction WinEHCatchDirector::handleLandingPad( |
| 1745 | ValueToValueMapTy &VMap, const LandingPadInst *LPad, BasicBlock *NewBB) { |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 1746 | // If the instruction after the landing pad is a call to llvm.eh.actions |
| 1747 | // the landing pad has already been outlined. In this case, we should |
| 1748 | // clone it because it may return to a block in the handler we are |
| 1749 | // outlining now that would otherwise be unreachable. The landing pads |
| 1750 | // are sorted before outlining begins to enable this case to work |
| 1751 | // properly. |
| 1752 | const Instruction *NextI = LPad->getNextNode(); |
| 1753 | if (match(NextI, m_Intrinsic<Intrinsic::eh_actions>())) |
| 1754 | return CloningDirector::CloneInstruction; |
| 1755 | |
| 1756 | // If the landing pad hasn't been outlined yet, the landing pad we are |
| 1757 | // outlining now does not dominate it and so it cannot return to a block |
| 1758 | // in this handler. In that case, we can just insert a stub landing |
| 1759 | // pad now and patch it up later. |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1760 | Instruction *NewInst = LPad->clone(); |
| 1761 | if (LPad->hasName()) |
| 1762 | NewInst->setName(LPad->getName()); |
| 1763 | // Save this correlation for later processing. |
| 1764 | NestedLPtoOriginalLP[cast<LandingPadInst>(NewInst)] = LPad; |
| 1765 | VMap[LPad] = NewInst; |
| 1766 | BasicBlock::InstListType &InstList = NewBB->getInstList(); |
| 1767 | InstList.push_back(NewInst); |
| 1768 | InstList.push_back(new UnreachableInst(NewBB->getContext())); |
| 1769 | return CloningDirector::StopCloningBB; |
| 1770 | } |
| 1771 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1772 | CloningDirector::CloningAction WinEHCatchDirector::handleBeginCatch( |
| 1773 | ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) { |
| 1774 | // The argument to the call is some form of the first element of the |
| 1775 | // landingpad aggregate value, but that doesn't matter. It isn't used |
| 1776 | // here. |
Reid Kleckner | 4236653 | 2015-03-03 23:20:30 +0000 | [diff] [blame] | 1777 | // The second argument is an outparameter where the exception object will be |
| 1778 | // stored. Typically the exception object is a scalar, but it can be an |
| 1779 | // aggregate when catching by value. |
| 1780 | // FIXME: Leave something behind to indicate where the exception object lives |
| 1781 | // for this handler. Should it be part of llvm.eh.actions? |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1782 | assert(ExceptionObjectVar == nullptr && "Multiple calls to " |
| 1783 | "llvm.eh.begincatch found while " |
| 1784 | "outlining catch handler."); |
| 1785 | ExceptionObjectVar = Inst->getOperand(1)->stripPointerCasts(); |
Reid Kleckner | 3567d27 | 2015-04-02 21:13:31 +0000 | [diff] [blame] | 1786 | if (isa<ConstantPointerNull>(ExceptionObjectVar)) |
| 1787 | return CloningDirector::SkipInstruction; |
Reid Kleckner | aab30e1 | 2015-04-03 18:18:06 +0000 | [diff] [blame] | 1788 | assert(cast<AllocaInst>(ExceptionObjectVar)->isStaticAlloca() && |
| 1789 | "catch parameter is not static alloca"); |
Reid Kleckner | 3567d27 | 2015-04-02 21:13:31 +0000 | [diff] [blame] | 1790 | Materializer.escapeCatchObject(ExceptionObjectVar); |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1791 | return CloningDirector::SkipInstruction; |
| 1792 | } |
| 1793 | |
| 1794 | CloningDirector::CloningAction |
| 1795 | WinEHCatchDirector::handleEndCatch(ValueToValueMapTy &VMap, |
| 1796 | const Instruction *Inst, BasicBlock *NewBB) { |
| 1797 | auto *IntrinCall = dyn_cast<IntrinsicInst>(Inst); |
| 1798 | // It might be interesting to track whether or not we are inside a catch |
| 1799 | // function, but that might make the algorithm more brittle than it needs |
| 1800 | // to be. |
| 1801 | |
| 1802 | // The end catch call can occur in one of two places: either in a |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1803 | // landingpad block that is part of the catch handlers exception mechanism, |
Andrew Kaylor | f7118ae | 2015-03-27 22:31:12 +0000 | [diff] [blame] | 1804 | // or at the end of the catch block. However, a catch-all handler may call |
| 1805 | // end catch from the original landing pad. If the call occurs in a nested |
| 1806 | // landing pad block, we must skip it and continue so that the landing pad |
| 1807 | // gets cloned. |
| 1808 | auto *ParentBB = IntrinCall->getParent(); |
| 1809 | if (ParentBB->isLandingPad() && !LPadMap.isOriginLandingPadBlock(ParentBB)) |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1810 | return CloningDirector::SkipInstruction; |
| 1811 | |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 1812 | // If an end catch occurs anywhere else we want to terminate the handler |
| 1813 | // with a return to the code that follows the endcatch call. If the |
| 1814 | // next instruction is not an unconditional branch, we need to split the |
| 1815 | // block to provide a clear target for the return instruction. |
| 1816 | BasicBlock *ContinueBB; |
| 1817 | auto Next = std::next(BasicBlock::const_iterator(IntrinCall)); |
| 1818 | const BranchInst *Branch = dyn_cast<BranchInst>(Next); |
| 1819 | if (!Branch || !Branch->isUnconditional()) { |
| 1820 | // We're interrupting the cloning process at this location, so the |
| 1821 | // const_cast we're doing here will not cause a problem. |
| 1822 | ContinueBB = SplitBlock(const_cast<BasicBlock *>(ParentBB), |
| 1823 | const_cast<Instruction *>(cast<Instruction>(Next))); |
| 1824 | } else { |
| 1825 | ContinueBB = Branch->getSuccessor(0); |
| 1826 | } |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1827 | |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 1828 | ReturnInst::Create(NewBB->getContext(), BlockAddress::get(ContinueBB), NewBB); |
| 1829 | ReturnTargets.push_back(ContinueBB); |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1830 | |
| 1831 | // We just added a terminator to the cloned block. |
| 1832 | // Tell the caller to stop processing the current basic block so that |
| 1833 | // the branch instruction will be skipped. |
| 1834 | return CloningDirector::StopCloningBB; |
| 1835 | } |
| 1836 | |
| 1837 | CloningDirector::CloningAction WinEHCatchDirector::handleTypeIdFor( |
| 1838 | ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) { |
| 1839 | auto *IntrinCall = dyn_cast<IntrinsicInst>(Inst); |
| 1840 | Value *Selector = IntrinCall->getArgOperand(0)->stripPointerCasts(); |
| 1841 | // This causes a replacement that will collapse the landing pad CFG based |
| 1842 | // on the filter function we intend to match. |
| 1843 | if (Selector == CurrentSelector) |
| 1844 | VMap[Inst] = ConstantInt::get(SelectorIDType, 1); |
| 1845 | else |
| 1846 | VMap[Inst] = ConstantInt::get(SelectorIDType, 0); |
| 1847 | // Tell the caller not to clone this instruction. |
| 1848 | return CloningDirector::SkipInstruction; |
| 1849 | } |
| 1850 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 1851 | CloningDirector::CloningAction WinEHCatchDirector::handleIndirectBr( |
| 1852 | ValueToValueMapTy &VMap, |
| 1853 | const IndirectBrInst *IBr, |
| 1854 | BasicBlock *NewBB) { |
| 1855 | // If this indirect branch is not part of a landing pad block, just clone it. |
| 1856 | const BasicBlock *ParentBB = IBr->getParent(); |
| 1857 | if (!ParentBB->isLandingPad()) |
| 1858 | return CloningDirector::CloneInstruction; |
| 1859 | |
| 1860 | // If it is part of a landing pad, we want to filter out target blocks |
| 1861 | // that are not part of the handler we are outlining. |
| 1862 | const LandingPadInst *LPad = ParentBB->getLandingPadInst(); |
| 1863 | |
| 1864 | // Save this correlation for later processing. |
| 1865 | NestedLPtoOriginalLP[cast<LandingPadInst>(VMap[LPad])] = LPad; |
| 1866 | |
| 1867 | // We should only get here for landing pads that have already been outlined. |
| 1868 | assert(match(LPad->getNextNode(), m_Intrinsic<Intrinsic::eh_actions>())); |
| 1869 | |
| 1870 | // Copy the indirectbr, but only include targets that were previously |
| 1871 | // identified as EH blocks and are dominated by the nested landing pad. |
| 1872 | SetVector<const BasicBlock *> ReturnTargets; |
| 1873 | for (int I = 0, E = IBr->getNumDestinations(); I < E; ++I) { |
| 1874 | auto *TargetBB = IBr->getDestination(I); |
| 1875 | if (EHBlocks.count(const_cast<BasicBlock*>(TargetBB)) && |
| 1876 | DT->dominates(ParentBB, TargetBB)) { |
| 1877 | DEBUG(dbgs() << " Adding destination " << TargetBB->getName() << "\n"); |
| 1878 | ReturnTargets.insert(TargetBB); |
| 1879 | } |
| 1880 | } |
| 1881 | IndirectBrInst *NewBranch = |
| 1882 | IndirectBrInst::Create(const_cast<Value *>(IBr->getAddress()), |
| 1883 | ReturnTargets.size(), NewBB); |
| 1884 | for (auto *Target : ReturnTargets) |
| 1885 | NewBranch->addDestination(const_cast<BasicBlock*>(Target)); |
| 1886 | |
| 1887 | // The operands and targets of the branch instruction are remapped later |
| 1888 | // because it is a terminator. Tell the cloning code to clone the |
| 1889 | // blocks we just added to the target list. |
| 1890 | return CloningDirector::CloneSuccessors; |
| 1891 | } |
| 1892 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1893 | CloningDirector::CloningAction |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1894 | WinEHCatchDirector::handleInvoke(ValueToValueMapTy &VMap, |
| 1895 | const InvokeInst *Invoke, BasicBlock *NewBB) { |
| 1896 | return CloningDirector::CloneInstruction; |
| 1897 | } |
| 1898 | |
| 1899 | CloningDirector::CloningAction |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1900 | WinEHCatchDirector::handleResume(ValueToValueMapTy &VMap, |
| 1901 | const ResumeInst *Resume, BasicBlock *NewBB) { |
| 1902 | // Resume instructions shouldn't be reachable from catch handlers. |
| 1903 | // We still need to handle it, but it will be pruned. |
| 1904 | BasicBlock::InstListType &InstList = NewBB->getInstList(); |
| 1905 | InstList.push_back(new UnreachableInst(NewBB->getContext())); |
| 1906 | return CloningDirector::StopCloningBB; |
| 1907 | } |
| 1908 | |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 1909 | CloningDirector::CloningAction |
| 1910 | WinEHCatchDirector::handleCompare(ValueToValueMapTy &VMap, |
| 1911 | const CmpInst *Compare, BasicBlock *NewBB) { |
| 1912 | const IntrinsicInst *IntrinCall = nullptr; |
| 1913 | if (match(Compare->getOperand(0), m_Intrinsic<Intrinsic::eh_typeid_for>())) { |
| 1914 | IntrinCall = dyn_cast<IntrinsicInst>(Compare->getOperand(0)); |
Andrew Kaylor | 9130743 | 2015-04-28 22:01:51 +0000 | [diff] [blame] | 1915 | } else if (match(Compare->getOperand(1), |
| 1916 | m_Intrinsic<Intrinsic::eh_typeid_for>())) { |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 1917 | IntrinCall = dyn_cast<IntrinsicInst>(Compare->getOperand(1)); |
| 1918 | } |
| 1919 | if (IntrinCall) { |
| 1920 | Value *Selector = IntrinCall->getArgOperand(0)->stripPointerCasts(); |
| 1921 | // This causes a replacement that will collapse the landing pad CFG based |
| 1922 | // on the filter function we intend to match. |
| 1923 | if (Selector == CurrentSelector->stripPointerCasts()) { |
| 1924 | VMap[Compare] = ConstantInt::get(SelectorIDType, 1); |
Andrew Kaylor | 9130743 | 2015-04-28 22:01:51 +0000 | [diff] [blame] | 1925 | } else { |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 1926 | VMap[Compare] = ConstantInt::get(SelectorIDType, 0); |
| 1927 | } |
| 1928 | return CloningDirector::SkipInstruction; |
| 1929 | } |
| 1930 | return CloningDirector::CloneInstruction; |
| 1931 | } |
| 1932 | |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1933 | CloningDirector::CloningAction WinEHCleanupDirector::handleLandingPad( |
| 1934 | ValueToValueMapTy &VMap, const LandingPadInst *LPad, BasicBlock *NewBB) { |
| 1935 | // The MS runtime will terminate the process if an exception occurs in a |
| 1936 | // cleanup handler, so we shouldn't encounter landing pads in the actual |
| 1937 | // cleanup code, but they may appear in catch blocks. Depending on where |
| 1938 | // we started cloning we may see one, but it will get dropped during dead |
| 1939 | // block pruning. |
| 1940 | Instruction *NewInst = new UnreachableInst(NewBB->getContext()); |
| 1941 | VMap[LPad] = NewInst; |
| 1942 | BasicBlock::InstListType &InstList = NewBB->getInstList(); |
| 1943 | InstList.push_back(NewInst); |
| 1944 | return CloningDirector::StopCloningBB; |
| 1945 | } |
| 1946 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1947 | CloningDirector::CloningAction WinEHCleanupDirector::handleBeginCatch( |
| 1948 | ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) { |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 1949 | // Cleanup code may flow into catch blocks or the catch block may be part |
| 1950 | // of a branch that will be optimized away. We'll insert a return |
| 1951 | // instruction now, but it may be pruned before the cloning process is |
| 1952 | // complete. |
| 1953 | ReturnInst::Create(NewBB->getContext(), nullptr, NewBB); |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1954 | return CloningDirector::StopCloningBB; |
| 1955 | } |
| 1956 | |
| 1957 | CloningDirector::CloningAction WinEHCleanupDirector::handleEndCatch( |
| 1958 | ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) { |
Andrew Kaylor | bb11132 | 2015-04-07 21:30:23 +0000 | [diff] [blame] | 1959 | // Cleanup handlers nested within catch handlers may begin with a call to |
| 1960 | // eh.endcatch. We can just ignore that instruction. |
| 1961 | return CloningDirector::SkipInstruction; |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | CloningDirector::CloningAction WinEHCleanupDirector::handleTypeIdFor( |
| 1965 | ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) { |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1966 | // If we encounter a selector comparison while cloning a cleanup handler, |
| 1967 | // we want to stop cloning immediately. Anything after the dispatch |
| 1968 | // will be outlined into a different handler. |
| 1969 | BasicBlock *CatchHandler; |
| 1970 | Constant *Selector; |
| 1971 | BasicBlock *NextBB; |
| 1972 | if (isSelectorDispatch(const_cast<BasicBlock *>(Inst->getParent()), |
| 1973 | CatchHandler, Selector, NextBB)) { |
| 1974 | ReturnInst::Create(NewBB->getContext(), nullptr, NewBB); |
| 1975 | return CloningDirector::StopCloningBB; |
| 1976 | } |
| 1977 | // If eg.typeid.for is called for any other reason, it can be ignored. |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1978 | VMap[Inst] = ConstantInt::get(SelectorIDType, 0); |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1979 | return CloningDirector::SkipInstruction; |
| 1980 | } |
| 1981 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 1982 | CloningDirector::CloningAction WinEHCleanupDirector::handleIndirectBr( |
| 1983 | ValueToValueMapTy &VMap, |
| 1984 | const IndirectBrInst *IBr, |
| 1985 | BasicBlock *NewBB) { |
| 1986 | // No special handling is required for cleanup cloning. |
| 1987 | return CloningDirector::CloneInstruction; |
| 1988 | } |
| 1989 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1990 | CloningDirector::CloningAction WinEHCleanupDirector::handleInvoke( |
| 1991 | ValueToValueMapTy &VMap, const InvokeInst *Invoke, BasicBlock *NewBB) { |
| 1992 | // All invokes in cleanup handlers can be replaced with calls. |
| 1993 | SmallVector<Value *, 16> CallArgs(Invoke->op_begin(), Invoke->op_end() - 3); |
| 1994 | // Insert a normal call instruction... |
| 1995 | CallInst *NewCall = |
| 1996 | CallInst::Create(const_cast<Value *>(Invoke->getCalledValue()), CallArgs, |
| 1997 | Invoke->getName(), NewBB); |
| 1998 | NewCall->setCallingConv(Invoke->getCallingConv()); |
| 1999 | NewCall->setAttributes(Invoke->getAttributes()); |
| 2000 | NewCall->setDebugLoc(Invoke->getDebugLoc()); |
| 2001 | VMap[Invoke] = NewCall; |
| 2002 | |
Reid Kleckner | 6e48a82 | 2015-04-10 16:26:42 +0000 | [diff] [blame] | 2003 | // Remap the operands. |
| 2004 | llvm::RemapInstruction(NewCall, VMap, RF_None, nullptr, &Materializer); |
| 2005 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2006 | // Insert an unconditional branch to the normal destination. |
| 2007 | BranchInst::Create(Invoke->getNormalDest(), NewBB); |
| 2008 | |
| 2009 | // The unwind destination won't be cloned into the new function, so |
| 2010 | // we don't need to clean up its phi nodes. |
| 2011 | |
| 2012 | // We just added a terminator to the cloned block. |
| 2013 | // Tell the caller to stop processing the current basic block. |
Reid Kleckner | 6e48a82 | 2015-04-10 16:26:42 +0000 | [diff] [blame] | 2014 | return CloningDirector::CloneSuccessors; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 2017 | CloningDirector::CloningAction WinEHCleanupDirector::handleResume( |
| 2018 | ValueToValueMapTy &VMap, const ResumeInst *Resume, BasicBlock *NewBB) { |
| 2019 | ReturnInst::Create(NewBB->getContext(), nullptr, NewBB); |
| 2020 | |
| 2021 | // We just added a terminator to the cloned block. |
| 2022 | // Tell the caller to stop processing the current basic block so that |
| 2023 | // the branch instruction will be skipped. |
| 2024 | return CloningDirector::StopCloningBB; |
| 2025 | } |
| 2026 | |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 2027 | CloningDirector::CloningAction |
| 2028 | WinEHCleanupDirector::handleCompare(ValueToValueMapTy &VMap, |
| 2029 | const CmpInst *Compare, BasicBlock *NewBB) { |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 2030 | if (match(Compare->getOperand(0), m_Intrinsic<Intrinsic::eh_typeid_for>()) || |
| 2031 | match(Compare->getOperand(1), m_Intrinsic<Intrinsic::eh_typeid_for>())) { |
| 2032 | VMap[Compare] = ConstantInt::get(SelectorIDType, 1); |
| 2033 | return CloningDirector::SkipInstruction; |
| 2034 | } |
| 2035 | return CloningDirector::CloneInstruction; |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 2038 | WinEHFrameVariableMaterializer::WinEHFrameVariableMaterializer( |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 2039 | Function *OutlinedFn, Value *ParentFP, FrameVarInfoMap &FrameVarInfo) |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 2040 | : FrameVarInfo(FrameVarInfo), Builder(OutlinedFn->getContext()) { |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 2041 | BasicBlock *EntryBB = &OutlinedFn->getEntryBlock(); |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 2042 | |
| 2043 | // New allocas should be inserted in the entry block, but after the parent FP |
| 2044 | // is established if it is an instruction. |
| 2045 | Instruction *InsertPoint = EntryBB->getFirstInsertionPt(); |
| 2046 | if (auto *FPInst = dyn_cast<Instruction>(ParentFP)) |
| 2047 | InsertPoint = FPInst->getNextNode(); |
| 2048 | Builder.SetInsertPoint(EntryBB, InsertPoint); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | Value *WinEHFrameVariableMaterializer::materializeValueFor(Value *V) { |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 2052 | // If we're asked to materialize a static alloca, we temporarily create an |
| 2053 | // alloca in the outlined function and add this to the FrameVarInfo map. When |
| 2054 | // all the outlining is complete, we'll replace these temporary allocas with |
Reid Kleckner | 6038179 | 2015-07-07 22:25:32 +0000 | [diff] [blame] | 2055 | // calls to llvm.localrecover. |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 2056 | if (auto *AV = dyn_cast<AllocaInst>(V)) { |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 2057 | assert(AV->isStaticAlloca() && |
| 2058 | "cannot materialize un-demoted dynamic alloca"); |
Andrew Kaylor | 72029c6 | 2015-03-03 00:41:03 +0000 | [diff] [blame] | 2059 | AllocaInst *NewAlloca = dyn_cast<AllocaInst>(AV->clone()); |
| 2060 | Builder.Insert(NewAlloca, AV->getName()); |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 2061 | FrameVarInfo[AV].push_back(NewAlloca); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 2062 | return NewAlloca; |
| 2063 | } |
| 2064 | |
Andrew Kaylor | 72029c6 | 2015-03-03 00:41:03 +0000 | [diff] [blame] | 2065 | if (isa<Instruction>(V) || isa<Argument>(V)) { |
Reid Kleckner | d1b38c4 | 2015-05-06 18:45:24 +0000 | [diff] [blame] | 2066 | Function *Parent = isa<Instruction>(V) |
| 2067 | ? cast<Instruction>(V)->getParent()->getParent() |
| 2068 | : cast<Argument>(V)->getParent(); |
| 2069 | errs() |
| 2070 | << "Failed to demote instruction used in exception handler of function " |
| 2071 | << GlobalValue::getRealLinkageName(Parent->getName()) << ":\n"; |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 2072 | errs() << " " << *V << '\n'; |
| 2073 | report_fatal_error("WinEHPrepare failed to demote instruction"); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 2074 | } |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 2075 | |
Andrew Kaylor | 72029c6 | 2015-03-03 00:41:03 +0000 | [diff] [blame] | 2076 | // Don't materialize other values. |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 2077 | return nullptr; |
| 2078 | } |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2079 | |
Reid Kleckner | 3567d27 | 2015-04-02 21:13:31 +0000 | [diff] [blame] | 2080 | void WinEHFrameVariableMaterializer::escapeCatchObject(Value *V) { |
| 2081 | // Catch parameter objects have to live in the parent frame. When we see a use |
| 2082 | // of a catch parameter, add a sentinel to the multimap to indicate that it's |
| 2083 | // used from another handler. This will prevent us from trying to sink the |
| 2084 | // alloca into the handler and ensure that the catch parameter is present in |
Reid Kleckner | 6038179 | 2015-07-07 22:25:32 +0000 | [diff] [blame] | 2085 | // the call to llvm.localescape. |
Reid Kleckner | 3567d27 | 2015-04-02 21:13:31 +0000 | [diff] [blame] | 2086 | FrameVarInfo[V].push_back(getCatchObjectSentinel()); |
| 2087 | } |
| 2088 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2089 | // This function maps the catch and cleanup handlers that are reachable from the |
| 2090 | // specified landing pad. The landing pad sequence will have this basic shape: |
| 2091 | // |
| 2092 | // <cleanup handler> |
| 2093 | // <selector comparison> |
| 2094 | // <catch handler> |
| 2095 | // <cleanup handler> |
| 2096 | // <selector comparison> |
| 2097 | // <catch handler> |
| 2098 | // <cleanup handler> |
| 2099 | // ... |
| 2100 | // |
| 2101 | // Any of the cleanup slots may be absent. The cleanup slots may be occupied by |
| 2102 | // any arbitrary control flow, but all paths through the cleanup code must |
| 2103 | // eventually reach the next selector comparison and no path can skip to a |
| 2104 | // different selector comparisons, though some paths may terminate abnormally. |
| 2105 | // Therefore, we will use a depth first search from the start of any given |
| 2106 | // cleanup block and stop searching when we find the next selector comparison. |
| 2107 | // |
| 2108 | // If the landingpad instruction does not have a catch clause, we will assume |
| 2109 | // that any instructions other than selector comparisons and catch handlers can |
| 2110 | // be ignored. In practice, these will only be the boilerplate instructions. |
| 2111 | // |
| 2112 | // The catch handlers may also have any control structure, but we are only |
| 2113 | // interested in the start of the catch handlers, so we don't need to actually |
| 2114 | // follow the flow of the catch handlers. The start of the catch handlers can |
| 2115 | // be located from the compare instructions, but they can be skipped in the |
| 2116 | // flow by following the contrary branch. |
| 2117 | void WinEHPrepare::mapLandingPadBlocks(LandingPadInst *LPad, |
| 2118 | LandingPadActions &Actions) { |
| 2119 | unsigned int NumClauses = LPad->getNumClauses(); |
| 2120 | unsigned int HandlersFound = 0; |
| 2121 | BasicBlock *BB = LPad->getParent(); |
| 2122 | |
| 2123 | DEBUG(dbgs() << "Mapping landing pad: " << BB->getName() << "\n"); |
| 2124 | |
| 2125 | if (NumClauses == 0) { |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2126 | findCleanupHandlers(Actions, BB, nullptr); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2127 | return; |
| 2128 | } |
| 2129 | |
| 2130 | VisitedBlockSet VisitedBlocks; |
| 2131 | |
| 2132 | while (HandlersFound != NumClauses) { |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2133 | BasicBlock *NextBB = nullptr; |
| 2134 | |
Andrew Kaylor | 20ae2a3 | 2015-04-23 22:38:36 +0000 | [diff] [blame] | 2135 | // Skip over filter clauses. |
| 2136 | if (LPad->isFilter(HandlersFound)) { |
| 2137 | ++HandlersFound; |
| 2138 | continue; |
| 2139 | } |
| 2140 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2141 | // See if the clause we're looking for is a catch-all. |
| 2142 | // If so, the catch begins immediately. |
Andrew Kaylor | 9130743 | 2015-04-28 22:01:51 +0000 | [diff] [blame] | 2143 | Constant *ExpectedSelector = |
| 2144 | LPad->getClause(HandlersFound)->stripPointerCasts(); |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 2145 | if (isa<ConstantPointerNull>(ExpectedSelector)) { |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2146 | // The catch all must occur last. |
| 2147 | assert(HandlersFound == NumClauses - 1); |
| 2148 | |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 2149 | // There can be additional selector dispatches in the call chain that we |
| 2150 | // need to ignore. |
| 2151 | BasicBlock *CatchBlock = nullptr; |
| 2152 | Constant *Selector; |
| 2153 | while (BB && isSelectorDispatch(BB, CatchBlock, Selector, NextBB)) { |
| 2154 | DEBUG(dbgs() << " Found extra catch dispatch in block " |
Andrew Kaylor | 9130743 | 2015-04-28 22:01:51 +0000 | [diff] [blame] | 2155 | << CatchBlock->getName() << "\n"); |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 2156 | BB = NextBB; |
| 2157 | } |
| 2158 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2159 | // Add the catch handler to the action list. |
Andrew Kaylor | f18771b | 2015-04-20 18:48:45 +0000 | [diff] [blame] | 2160 | CatchHandler *Action = nullptr; |
| 2161 | if (CatchHandlerMap.count(BB) && CatchHandlerMap[BB] != nullptr) { |
| 2162 | // If the CatchHandlerMap already has an entry for this BB, re-use it. |
| 2163 | Action = CatchHandlerMap[BB]; |
| 2164 | assert(Action->getSelector() == ExpectedSelector); |
| 2165 | } else { |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 2166 | // We don't expect a selector dispatch, but there may be a call to |
| 2167 | // llvm.eh.begincatch, which separates catch handling code from |
| 2168 | // cleanup code in the same control flow. This call looks for the |
| 2169 | // begincatch intrinsic. |
| 2170 | Action = findCatchHandler(BB, NextBB, VisitedBlocks); |
| 2171 | if (Action) { |
| 2172 | // For C++ EH, check if there is any interesting cleanup code before |
| 2173 | // we begin the catch. This is important because cleanups cannot |
| 2174 | // rethrow exceptions but code called from catches can. For SEH, it |
| 2175 | // isn't important if some finally code before a catch-all is executed |
| 2176 | // out of line or after recovering from the exception. |
| 2177 | if (Personality == EHPersonality::MSVC_CXX) |
| 2178 | findCleanupHandlers(Actions, BB, BB); |
| 2179 | } else { |
| 2180 | // If an action was not found, it means that the control flows |
| 2181 | // directly into the catch-all handler and there is no cleanup code. |
| 2182 | // That's an expected situation and we must create a catch action. |
| 2183 | // Since this is a catch-all handler, the selector won't actually |
| 2184 | // appear in the code anywhere. ExpectedSelector here is the constant |
| 2185 | // null ptr that we got from the landing pad instruction. |
| 2186 | Action = new CatchHandler(BB, ExpectedSelector, nullptr); |
| 2187 | CatchHandlerMap[BB] = Action; |
| 2188 | } |
Andrew Kaylor | f18771b | 2015-04-20 18:48:45 +0000 | [diff] [blame] | 2189 | } |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2190 | Actions.insertCatchHandler(Action); |
| 2191 | DEBUG(dbgs() << " Catch all handler at block " << BB->getName() << "\n"); |
| 2192 | ++HandlersFound; |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 2193 | |
| 2194 | // Once we reach a catch-all, don't expect to hit a resume instruction. |
| 2195 | BB = nullptr; |
| 2196 | break; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2197 | } |
| 2198 | |
| 2199 | CatchHandler *CatchAction = findCatchHandler(BB, NextBB, VisitedBlocks); |
Andrew Kaylor | 4175851 | 2015-04-20 22:04:09 +0000 | [diff] [blame] | 2200 | assert(CatchAction); |
| 2201 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2202 | // See if there is any interesting code executed before the dispatch. |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2203 | findCleanupHandlers(Actions, BB, CatchAction->getStartBlock()); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2204 | |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 2205 | // When the source program contains multiple nested try blocks the catch |
| 2206 | // handlers can get strung together in such a way that we can encounter |
| 2207 | // a dispatch for a selector that we've already had a handler for. |
| 2208 | if (CatchAction->getSelector()->stripPointerCasts() == ExpectedSelector) { |
| 2209 | ++HandlersFound; |
| 2210 | |
| 2211 | // Add the catch handler to the action list. |
| 2212 | DEBUG(dbgs() << " Found catch dispatch in block " |
| 2213 | << CatchAction->getStartBlock()->getName() << "\n"); |
| 2214 | Actions.insertCatchHandler(CatchAction); |
| 2215 | } else { |
Andrew Kaylor | 4175851 | 2015-04-20 22:04:09 +0000 | [diff] [blame] | 2216 | // Under some circumstances optimized IR will flow unconditionally into a |
| 2217 | // handler block without checking the selector. This can only happen if |
| 2218 | // the landing pad has a catch-all handler and the handler for the |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 2219 | // preceding catch clause is identical to the catch-call handler |
Andrew Kaylor | 4175851 | 2015-04-20 22:04:09 +0000 | [diff] [blame] | 2220 | // (typically an empty catch). In this case, the handler must be shared |
| 2221 | // by all remaining clauses. |
| 2222 | if (isa<ConstantPointerNull>( |
| 2223 | CatchAction->getSelector()->stripPointerCasts())) { |
| 2224 | DEBUG(dbgs() << " Applying early catch-all handler in block " |
| 2225 | << CatchAction->getStartBlock()->getName() |
| 2226 | << " to all remaining clauses.\n"); |
| 2227 | Actions.insertCatchHandler(CatchAction); |
| 2228 | return; |
| 2229 | } |
| 2230 | |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 2231 | DEBUG(dbgs() << " Found extra catch dispatch in block " |
| 2232 | << CatchAction->getStartBlock()->getName() << "\n"); |
| 2233 | } |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2234 | |
| 2235 | // Move on to the block after the catch handler. |
| 2236 | BB = NextBB; |
| 2237 | } |
| 2238 | |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 2239 | // If we didn't wind up in a catch-all, see if there is any interesting code |
| 2240 | // executed before the resume. |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2241 | findCleanupHandlers(Actions, BB, BB); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2242 | |
| 2243 | // It's possible that some optimization moved code into a landingpad that |
| 2244 | // wasn't |
| 2245 | // previously being used for cleanup. If that happens, we need to execute |
| 2246 | // that |
| 2247 | // extra code from a cleanup handler. |
| 2248 | if (Actions.includesCleanup() && !LPad->isCleanup()) |
| 2249 | LPad->setCleanup(true); |
| 2250 | } |
| 2251 | |
| 2252 | // This function searches starting with the input block for the next |
| 2253 | // block that terminates with a branch whose condition is based on a selector |
| 2254 | // comparison. This may be the input block. See the mapLandingPadBlocks |
| 2255 | // comments for a discussion of control flow assumptions. |
| 2256 | // |
| 2257 | CatchHandler *WinEHPrepare::findCatchHandler(BasicBlock *BB, |
| 2258 | BasicBlock *&NextBB, |
| 2259 | VisitedBlockSet &VisitedBlocks) { |
| 2260 | // See if we've already found a catch handler use it. |
| 2261 | // Call count() first to avoid creating a null entry for blocks |
| 2262 | // we haven't seen before. |
| 2263 | if (CatchHandlerMap.count(BB) && CatchHandlerMap[BB] != nullptr) { |
| 2264 | CatchHandler *Action = cast<CatchHandler>(CatchHandlerMap[BB]); |
| 2265 | NextBB = Action->getNextBB(); |
| 2266 | return Action; |
| 2267 | } |
| 2268 | |
| 2269 | // VisitedBlocks applies only to the current search. We still |
| 2270 | // need to consider blocks that we've visited while mapping other |
| 2271 | // landing pads. |
| 2272 | VisitedBlocks.insert(BB); |
| 2273 | |
| 2274 | BasicBlock *CatchBlock = nullptr; |
| 2275 | Constant *Selector = nullptr; |
| 2276 | |
| 2277 | // If this is the first time we've visited this block from any landing pad |
| 2278 | // look to see if it is a selector dispatch block. |
| 2279 | if (!CatchHandlerMap.count(BB)) { |
| 2280 | if (isSelectorDispatch(BB, CatchBlock, Selector, NextBB)) { |
| 2281 | CatchHandler *Action = new CatchHandler(BB, Selector, NextBB); |
| 2282 | CatchHandlerMap[BB] = Action; |
| 2283 | return Action; |
| 2284 | } |
Andrew Kaylor | 4175851 | 2015-04-20 22:04:09 +0000 | [diff] [blame] | 2285 | // If we encounter a block containing an llvm.eh.begincatch before we |
| 2286 | // find a selector dispatch block, the handler is assumed to be |
| 2287 | // reached unconditionally. This happens for catch-all blocks, but |
| 2288 | // it can also happen for other catch handlers that have been combined |
| 2289 | // with the catch-all handler during optimization. |
| 2290 | if (isCatchBlock(BB)) { |
| 2291 | PointerType *Int8PtrTy = Type::getInt8PtrTy(BB->getContext()); |
| 2292 | Constant *NullSelector = ConstantPointerNull::get(Int8PtrTy); |
| 2293 | CatchHandler *Action = new CatchHandler(BB, NullSelector, nullptr); |
| 2294 | CatchHandlerMap[BB] = Action; |
| 2295 | return Action; |
| 2296 | } |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | // Visit each successor, looking for the dispatch. |
| 2300 | // FIXME: We expect to find the dispatch quickly, so this will probably |
| 2301 | // work better as a breadth first search. |
| 2302 | for (BasicBlock *Succ : successors(BB)) { |
| 2303 | if (VisitedBlocks.count(Succ)) |
| 2304 | continue; |
| 2305 | |
| 2306 | CatchHandler *Action = findCatchHandler(Succ, NextBB, VisitedBlocks); |
| 2307 | if (Action) |
| 2308 | return Action; |
| 2309 | } |
| 2310 | return nullptr; |
| 2311 | } |
| 2312 | |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2313 | // These are helper functions to combine repeated code from findCleanupHandlers. |
| 2314 | static void createCleanupHandler(LandingPadActions &Actions, |
| 2315 | CleanupHandlerMapTy &CleanupHandlerMap, |
| 2316 | BasicBlock *BB) { |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2317 | CleanupHandler *Action = new CleanupHandler(BB); |
| 2318 | CleanupHandlerMap[BB] = Action; |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2319 | Actions.insertCleanupHandler(Action); |
| 2320 | DEBUG(dbgs() << " Found cleanup code in block " |
| 2321 | << Action->getStartBlock()->getName() << "\n"); |
| 2322 | } |
| 2323 | |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2324 | static CallSite matchOutlinedFinallyCall(BasicBlock *BB, |
| 2325 | Instruction *MaybeCall) { |
| 2326 | // Look for finally blocks that Clang has already outlined for us. |
Reid Kleckner | d5afc62f | 2015-07-07 23:23:03 +0000 | [diff] [blame] | 2327 | // %fp = call i8* @llvm.localaddress() |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2328 | // call void @"fin$parent"(iN 1, i8* %fp) |
Reid Kleckner | d5afc62f | 2015-07-07 23:23:03 +0000 | [diff] [blame] | 2329 | if (isLocalAddressCall(MaybeCall) && MaybeCall != BB->getTerminator()) |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2330 | MaybeCall = MaybeCall->getNextNode(); |
| 2331 | CallSite FinallyCall(MaybeCall); |
| 2332 | if (!FinallyCall || FinallyCall.arg_size() != 2) |
| 2333 | return CallSite(); |
| 2334 | if (!match(FinallyCall.getArgument(0), m_SpecificInt(1))) |
| 2335 | return CallSite(); |
Reid Kleckner | d5afc62f | 2015-07-07 23:23:03 +0000 | [diff] [blame] | 2336 | if (!isLocalAddressCall(FinallyCall.getArgument(1))) |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2337 | return CallSite(); |
| 2338 | return FinallyCall; |
| 2339 | } |
| 2340 | |
| 2341 | static BasicBlock *followSingleUnconditionalBranches(BasicBlock *BB) { |
| 2342 | // Skip single ubr blocks. |
| 2343 | while (BB->getFirstNonPHIOrDbg() == BB->getTerminator()) { |
| 2344 | auto *Br = dyn_cast<BranchInst>(BB->getTerminator()); |
| 2345 | if (Br && Br->isUnconditional()) |
| 2346 | BB = Br->getSuccessor(0); |
| 2347 | else |
| 2348 | return BB; |
| 2349 | } |
| 2350 | return BB; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2351 | } |
| 2352 | |
| 2353 | // This function searches starting with the input block for the next block that |
| 2354 | // contains code that is not part of a catch handler and would not be eliminated |
| 2355 | // during handler outlining. |
| 2356 | // |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2357 | void WinEHPrepare::findCleanupHandlers(LandingPadActions &Actions, |
| 2358 | BasicBlock *StartBB, BasicBlock *EndBB) { |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2359 | // Here we will skip over the following: |
| 2360 | // |
| 2361 | // landing pad prolog: |
| 2362 | // |
| 2363 | // Unconditional branches |
| 2364 | // |
| 2365 | // Selector dispatch |
| 2366 | // |
| 2367 | // Resume pattern |
| 2368 | // |
| 2369 | // Anything else marks the start of an interesting block |
| 2370 | |
| 2371 | BasicBlock *BB = StartBB; |
| 2372 | // Anything other than an unconditional branch will kick us out of this loop |
| 2373 | // one way or another. |
| 2374 | while (BB) { |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2375 | BB = followSingleUnconditionalBranches(BB); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2376 | // If we've already scanned this block, don't scan it again. If it is |
| 2377 | // a cleanup block, there will be an action in the CleanupHandlerMap. |
| 2378 | // If we've scanned it and it is not a cleanup block, there will be a |
| 2379 | // nullptr in the CleanupHandlerMap. If we have not scanned it, there will |
| 2380 | // be no entry in the CleanupHandlerMap. We must call count() first to |
| 2381 | // avoid creating a null entry for blocks we haven't scanned. |
| 2382 | if (CleanupHandlerMap.count(BB)) { |
| 2383 | if (auto *Action = CleanupHandlerMap[BB]) { |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2384 | Actions.insertCleanupHandler(Action); |
| 2385 | DEBUG(dbgs() << " Found cleanup code in block " |
Andrew Kaylor | 9130743 | 2015-04-28 22:01:51 +0000 | [diff] [blame] | 2386 | << Action->getStartBlock()->getName() << "\n"); |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2387 | // FIXME: This cleanup might chain into another, and we need to discover |
| 2388 | // that. |
| 2389 | return; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2390 | } else { |
| 2391 | // Here we handle the case where the cleanup handler map contains a |
| 2392 | // value for this block but the value is a nullptr. This means that |
| 2393 | // we have previously analyzed the block and determined that it did |
| 2394 | // not contain any cleanup code. Based on the earlier analysis, we |
Eric Christopher | 572e03a | 2015-06-19 01:53:21 +0000 | [diff] [blame] | 2395 | // know the block must end in either an unconditional branch, a |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2396 | // resume or a conditional branch that is predicated on a comparison |
| 2397 | // with a selector. Either the resume or the selector dispatch |
| 2398 | // would terminate the search for cleanup code, so the unconditional |
| 2399 | // branch is the only case for which we might need to continue |
| 2400 | // searching. |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2401 | BasicBlock *SuccBB = followSingleUnconditionalBranches(BB); |
| 2402 | if (SuccBB == BB || SuccBB == EndBB) |
| 2403 | return; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2404 | BB = SuccBB; |
| 2405 | continue; |
| 2406 | } |
| 2407 | } |
| 2408 | |
| 2409 | // Create an entry in the cleanup handler map for this block. Initially |
| 2410 | // we create an entry that says this isn't a cleanup block. If we find |
| 2411 | // cleanup code, the caller will replace this entry. |
| 2412 | CleanupHandlerMap[BB] = nullptr; |
| 2413 | |
| 2414 | TerminatorInst *Terminator = BB->getTerminator(); |
| 2415 | |
| 2416 | // Landing pad blocks have extra instructions we need to accept. |
| 2417 | LandingPadMap *LPadMap = nullptr; |
| 2418 | if (BB->isLandingPad()) { |
| 2419 | LandingPadInst *LPad = BB->getLandingPadInst(); |
| 2420 | LPadMap = &LPadMaps[LPad]; |
| 2421 | if (!LPadMap->isInitialized()) |
| 2422 | LPadMap->mapLandingPad(LPad); |
| 2423 | } |
| 2424 | |
| 2425 | // Look for the bare resume pattern: |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 2426 | // %lpad.val1 = insertvalue { i8*, i32 } undef, i8* %exn, 0 |
| 2427 | // %lpad.val2 = insertvalue { i8*, i32 } %lpad.val1, i32 %sel, 1 |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2428 | // resume { i8*, i32 } %lpad.val2 |
| 2429 | if (auto *Resume = dyn_cast<ResumeInst>(Terminator)) { |
| 2430 | InsertValueInst *Insert1 = nullptr; |
| 2431 | InsertValueInst *Insert2 = nullptr; |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 2432 | Value *ResumeVal = Resume->getOperand(0); |
Reid Kleckner | 1c130bb | 2015-04-16 17:02:23 +0000 | [diff] [blame] | 2433 | // If the resume value isn't a phi or landingpad value, it should be a |
| 2434 | // series of insertions. Identify them so we can avoid them when scanning |
| 2435 | // for cleanups. |
| 2436 | if (!isa<PHINode>(ResumeVal) && !isa<LandingPadInst>(ResumeVal)) { |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 2437 | Insert2 = dyn_cast<InsertValueInst>(ResumeVal); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2438 | if (!Insert2) |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2439 | return createCleanupHandler(Actions, CleanupHandlerMap, BB); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2440 | Insert1 = dyn_cast<InsertValueInst>(Insert2->getAggregateOperand()); |
| 2441 | if (!Insert1) |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2442 | return createCleanupHandler(Actions, CleanupHandlerMap, BB); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2443 | } |
| 2444 | for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end(); |
| 2445 | II != IE; ++II) { |
| 2446 | Instruction *Inst = II; |
| 2447 | if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst)) |
| 2448 | continue; |
| 2449 | if (Inst == Insert1 || Inst == Insert2 || Inst == Resume) |
| 2450 | continue; |
| 2451 | if (!Inst->hasOneUse() || |
| 2452 | (Inst->user_back() != Insert1 && Inst->user_back() != Insert2)) { |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2453 | return createCleanupHandler(Actions, CleanupHandlerMap, BB); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2454 | } |
| 2455 | } |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2456 | return; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
| 2459 | BranchInst *Branch = dyn_cast<BranchInst>(Terminator); |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 2460 | if (Branch && Branch->isConditional()) { |
| 2461 | // Look for the selector dispatch. |
| 2462 | // %2 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIf to i8*)) |
| 2463 | // %matches = icmp eq i32 %sel, %2 |
| 2464 | // br i1 %matches, label %catch14, label %eh.resume |
| 2465 | CmpInst *Compare = dyn_cast<CmpInst>(Branch->getCondition()); |
| 2466 | if (!Compare || !Compare->isEquality()) |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2467 | return createCleanupHandler(Actions, CleanupHandlerMap, BB); |
Andrew Kaylor | bb11132 | 2015-04-07 21:30:23 +0000 | [diff] [blame] | 2468 | for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end(); |
| 2469 | II != IE; ++II) { |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 2470 | Instruction *Inst = II; |
| 2471 | if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst)) |
| 2472 | continue; |
| 2473 | if (Inst == Compare || Inst == Branch) |
| 2474 | continue; |
| 2475 | if (match(Inst, m_Intrinsic<Intrinsic::eh_typeid_for>())) |
| 2476 | continue; |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2477 | return createCleanupHandler(Actions, CleanupHandlerMap, BB); |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 2478 | } |
| 2479 | // The selector dispatch block should always terminate our search. |
| 2480 | assert(BB == EndBB); |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2481 | return; |
| 2482 | } |
| 2483 | |
| 2484 | if (isAsynchronousEHPersonality(Personality)) { |
| 2485 | // If this is a landingpad block, split the block at the first non-landing |
| 2486 | // pad instruction. |
| 2487 | Instruction *MaybeCall = BB->getFirstNonPHIOrDbg(); |
| 2488 | if (LPadMap) { |
| 2489 | while (MaybeCall != BB->getTerminator() && |
| 2490 | LPadMap->isLandingPadSpecificInst(MaybeCall)) |
| 2491 | MaybeCall = MaybeCall->getNextNode(); |
| 2492 | } |
| 2493 | |
Reid Kleckner | 6511c8b | 2015-07-01 20:59:25 +0000 | [diff] [blame] | 2494 | // Look for outlined finally calls on x64, since those happen to match the |
| 2495 | // prototype provided by the runtime. |
| 2496 | if (TheTriple.getArch() == Triple::x86_64) { |
| 2497 | if (CallSite FinallyCall = matchOutlinedFinallyCall(BB, MaybeCall)) { |
| 2498 | Function *Fin = FinallyCall.getCalledFunction(); |
| 2499 | assert(Fin && "outlined finally call should be direct"); |
| 2500 | auto *Action = new CleanupHandler(BB); |
| 2501 | Action->setHandlerBlockOrFunc(Fin); |
| 2502 | Actions.insertCleanupHandler(Action); |
| 2503 | CleanupHandlerMap[BB] = Action; |
| 2504 | DEBUG(dbgs() << " Found frontend-outlined finally call to " |
| 2505 | << Fin->getName() << " in block " |
| 2506 | << Action->getStartBlock()->getName() << "\n"); |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2507 | |
Reid Kleckner | 6511c8b | 2015-07-01 20:59:25 +0000 | [diff] [blame] | 2508 | // Split the block if there were more interesting instructions and |
| 2509 | // look for finally calls in the normal successor block. |
| 2510 | BasicBlock *SuccBB = BB; |
| 2511 | if (FinallyCall.getInstruction() != BB->getTerminator() && |
| 2512 | FinallyCall.getInstruction()->getNextNode() != |
| 2513 | BB->getTerminator()) { |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 2514 | SuccBB = |
Reid Kleckner | 6511c8b | 2015-07-01 20:59:25 +0000 | [diff] [blame] | 2515 | SplitBlock(BB, FinallyCall.getInstruction()->getNextNode(), DT); |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2516 | } else { |
Reid Kleckner | 6511c8b | 2015-07-01 20:59:25 +0000 | [diff] [blame] | 2517 | if (FinallyCall.isInvoke()) { |
| 2518 | SuccBB = cast<InvokeInst>(FinallyCall.getInstruction()) |
| 2519 | ->getNormalDest(); |
| 2520 | } else { |
| 2521 | SuccBB = BB->getUniqueSuccessor(); |
| 2522 | assert(SuccBB && |
| 2523 | "splitOutlinedFinallyCalls didn't insert a branch"); |
| 2524 | } |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2525 | } |
Reid Kleckner | 6511c8b | 2015-07-01 20:59:25 +0000 | [diff] [blame] | 2526 | BB = SuccBB; |
| 2527 | if (BB == EndBB) |
| 2528 | return; |
| 2529 | continue; |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2530 | } |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2531 | } |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 2532 | } |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2533 | |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 2534 | // Anything else is either a catch block or interesting cleanup code. |
Andrew Kaylor | bb11132 | 2015-04-07 21:30:23 +0000 | [diff] [blame] | 2535 | for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end(); |
| 2536 | II != IE; ++II) { |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 2537 | Instruction *Inst = II; |
| 2538 | if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst)) |
| 2539 | continue; |
| 2540 | // Unconditional branches fall through to this loop. |
| 2541 | if (Inst == Branch) |
| 2542 | continue; |
| 2543 | // If this is a catch block, there is no cleanup code to be found. |
| 2544 | if (match(Inst, m_Intrinsic<Intrinsic::eh_begincatch>())) |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2545 | return; |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 2546 | // If this a nested landing pad, it may contain an endcatch call. |
| 2547 | if (match(Inst, m_Intrinsic<Intrinsic::eh_endcatch>())) |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2548 | return; |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 2549 | // Anything else makes this interesting cleanup code. |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2550 | return createCleanupHandler(Actions, CleanupHandlerMap, BB); |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 2551 | } |
| 2552 | |
| 2553 | // Only unconditional branches in empty blocks should get this far. |
| 2554 | assert(Branch && Branch->isUnconditional()); |
| 2555 | if (BB == EndBB) |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2556 | return; |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 2557 | BB = Branch->getSuccessor(0); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2558 | } |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2559 | } |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 2560 | |
| 2561 | // This is a public function, declared in WinEHFuncInfo.h and is also |
| 2562 | // referenced by WinEHNumbering in FunctionLoweringInfo.cpp. |
Benjamin Kramer | a48e065 | 2015-05-16 15:40:03 +0000 | [diff] [blame] | 2563 | void llvm::parseEHActions( |
| 2564 | const IntrinsicInst *II, |
| 2565 | SmallVectorImpl<std::unique_ptr<ActionHandler>> &Actions) { |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 2566 | assert(II->getIntrinsicID() == Intrinsic::eh_actions && |
| 2567 | "attempted to parse non eh.actions intrinsic"); |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 2568 | for (unsigned I = 0, E = II->getNumArgOperands(); I != E;) { |
| 2569 | uint64_t ActionKind = |
Andrew Kaylor | bb11132 | 2015-04-07 21:30:23 +0000 | [diff] [blame] | 2570 | cast<ConstantInt>(II->getArgOperand(I))->getZExtValue(); |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 2571 | if (ActionKind == /*catch=*/1) { |
| 2572 | auto *Selector = cast<Constant>(II->getArgOperand(I + 1)); |
| 2573 | ConstantInt *EHObjIndex = cast<ConstantInt>(II->getArgOperand(I + 2)); |
| 2574 | int64_t EHObjIndexVal = EHObjIndex->getSExtValue(); |
| 2575 | Constant *Handler = cast<Constant>(II->getArgOperand(I + 3)); |
| 2576 | I += 4; |
Benjamin Kramer | a48e065 | 2015-05-16 15:40:03 +0000 | [diff] [blame] | 2577 | auto CH = make_unique<CatchHandler>(/*BB=*/nullptr, Selector, |
| 2578 | /*NextBB=*/nullptr); |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 2579 | CH->setHandlerBlockOrFunc(Handler); |
| 2580 | CH->setExceptionVarIndex(EHObjIndexVal); |
Benjamin Kramer | a48e065 | 2015-05-16 15:40:03 +0000 | [diff] [blame] | 2581 | Actions.push_back(std::move(CH)); |
David Majnemer | 69132a7 | 2015-04-03 22:49:05 +0000 | [diff] [blame] | 2582 | } else if (ActionKind == 0) { |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 2583 | Constant *Handler = cast<Constant>(II->getArgOperand(I + 1)); |
| 2584 | I += 2; |
Benjamin Kramer | a48e065 | 2015-05-16 15:40:03 +0000 | [diff] [blame] | 2585 | auto CH = make_unique<CleanupHandler>(/*BB=*/nullptr); |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 2586 | CH->setHandlerBlockOrFunc(Handler); |
Benjamin Kramer | a48e065 | 2015-05-16 15:40:03 +0000 | [diff] [blame] | 2587 | Actions.push_back(std::move(CH)); |
David Majnemer | 69132a7 | 2015-04-03 22:49:05 +0000 | [diff] [blame] | 2588 | } else { |
| 2589 | llvm_unreachable("Expected either a catch or cleanup handler!"); |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 2590 | } |
| 2591 | } |
| 2592 | std::reverse(Actions.begin(), Actions.end()); |
| 2593 | } |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 2594 | |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 2595 | static int addUnwindMapEntry(WinEHFuncInfo &FuncInfo, int ToState, |
| 2596 | const Value *V) { |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 2597 | WinEHUnwindMapEntry UME; |
| 2598 | UME.ToState = ToState; |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 2599 | UME.Cleanup = V; |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 2600 | FuncInfo.UnwindMap.push_back(UME); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 2601 | return FuncInfo.getLastStateNumber(); |
| 2602 | } |
| 2603 | |
| 2604 | static void addTryBlockMapEntry(WinEHFuncInfo &FuncInfo, int TryLow, |
| 2605 | int TryHigh, int CatchHigh, |
| 2606 | ArrayRef<const CatchPadInst *> Handlers) { |
| 2607 | WinEHTryBlockMapEntry TBME; |
| 2608 | TBME.TryLow = TryLow; |
| 2609 | TBME.TryHigh = TryHigh; |
| 2610 | TBME.CatchHigh = CatchHigh; |
| 2611 | assert(TBME.TryLow <= TBME.TryHigh); |
| 2612 | for (const CatchPadInst *CPI : Handlers) { |
| 2613 | WinEHHandlerType HT; |
| 2614 | Constant *TypeInfo = cast<Constant>(CPI->getArgOperand(0)); |
Reid Kleckner | b005d28 | 2015-09-16 20:16:27 +0000 | [diff] [blame] | 2615 | if (TypeInfo->isNullValue()) |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 2616 | HT.TypeDescriptor = nullptr; |
Reid Kleckner | b005d28 | 2015-09-16 20:16:27 +0000 | [diff] [blame] | 2617 | else |
| 2618 | HT.TypeDescriptor = cast<GlobalVariable>(TypeInfo->stripPointerCasts()); |
| 2619 | HT.Adjectives = cast<ConstantInt>(CPI->getArgOperand(1))->getZExtValue(); |
David Majnemer | 7735a6d | 2015-10-06 23:31:59 +0000 | [diff] [blame] | 2620 | HT.Handler = CPI->getParent(); |
Reid Kleckner | b005d28 | 2015-09-16 20:16:27 +0000 | [diff] [blame] | 2621 | HT.CatchObjRecoverIdx = -2; |
| 2622 | if (isa<ConstantPointerNull>(CPI->getArgOperand(2))) |
| 2623 | HT.CatchObj.Alloca = nullptr; |
| 2624 | else |
| 2625 | HT.CatchObj.Alloca = cast<AllocaInst>(CPI->getArgOperand(2)); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 2626 | TBME.HandlerArray.push_back(HT); |
| 2627 | } |
| 2628 | FuncInfo.TryBlockMap.push_back(TBME); |
| 2629 | } |
| 2630 | |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 2631 | static const CatchPadInst *getSingleCatchPadPredecessor(const BasicBlock *BB) { |
| 2632 | for (const BasicBlock *PredBlock : predecessors(BB)) |
| 2633 | if (auto *CPI = dyn_cast<CatchPadInst>(PredBlock->getFirstNonPHI())) |
| 2634 | return CPI; |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 2635 | return nullptr; |
| 2636 | } |
| 2637 | |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 2638 | /// Find all the catchpads that feed directly into the catchendpad. Frontends |
| 2639 | /// using this personality should ensure that each catchendpad and catchpad has |
| 2640 | /// one or zero catchpad predecessors. |
| 2641 | /// |
| 2642 | /// The following C++ generates the IR after it: |
| 2643 | /// try { |
| 2644 | /// } catch (A) { |
| 2645 | /// } catch (B) { |
| 2646 | /// } |
| 2647 | /// |
| 2648 | /// IR: |
| 2649 | /// %catchpad.A |
| 2650 | /// catchpad [i8* A typeinfo] |
| 2651 | /// to label %catch.A unwind label %catchpad.B |
| 2652 | /// %catchpad.B |
| 2653 | /// catchpad [i8* B typeinfo] |
| 2654 | /// to label %catch.B unwind label %endcatches |
| 2655 | /// %endcatches |
| 2656 | /// catchendblock unwind to caller |
Benjamin Kramer | 3c96f0a | 2015-09-22 14:34:57 +0000 | [diff] [blame] | 2657 | static void |
| 2658 | findCatchPadsForCatchEndPad(const BasicBlock *CatchEndBB, |
| 2659 | SmallVectorImpl<const CatchPadInst *> &Handlers) { |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 2660 | const CatchPadInst *CPI = getSingleCatchPadPredecessor(CatchEndBB); |
| 2661 | while (CPI) { |
| 2662 | Handlers.push_back(CPI); |
| 2663 | CPI = getSingleCatchPadPredecessor(CPI->getParent()); |
| 2664 | } |
| 2665 | // We've pushed these back into reverse source order. Reverse them to get |
| 2666 | // the list back into source order. |
| 2667 | std::reverse(Handlers.begin(), Handlers.end()); |
| 2668 | } |
| 2669 | |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 2670 | // Given BB which ends in an unwind edge, return the EHPad that this BB belongs |
| 2671 | // to. If the unwind edge came from an invoke, return null. |
| 2672 | static const BasicBlock *getEHPadFromPredecessor(const BasicBlock *BB) { |
| 2673 | const TerminatorInst *TI = BB->getTerminator(); |
| 2674 | if (isa<InvokeInst>(TI)) |
| 2675 | return nullptr; |
Reid Kleckner | 7bb20bd | 2015-09-10 21:46:36 +0000 | [diff] [blame] | 2676 | if (TI->isEHPad()) |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 2677 | return BB; |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 2678 | return cast<CleanupReturnInst>(TI)->getCleanupPad()->getParent(); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 2679 | } |
| 2680 | |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 2681 | static void calculateExplicitCXXStateNumbers(WinEHFuncInfo &FuncInfo, |
| 2682 | const BasicBlock &BB, |
| 2683 | int ParentState) { |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 2684 | assert(BB.isEHPad()); |
| 2685 | const Instruction *FirstNonPHI = BB.getFirstNonPHI(); |
| 2686 | // All catchpad instructions will be handled when we process their |
| 2687 | // respective catchendpad instruction. |
| 2688 | if (isa<CatchPadInst>(FirstNonPHI)) |
| 2689 | return; |
| 2690 | |
| 2691 | if (isa<CatchEndPadInst>(FirstNonPHI)) { |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 2692 | SmallVector<const CatchPadInst *, 2> Handlers; |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 2693 | findCatchPadsForCatchEndPad(&BB, Handlers); |
| 2694 | const BasicBlock *FirstTryPad = Handlers.front()->getParent(); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 2695 | int TryLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr); |
| 2696 | FuncInfo.EHPadStateMap[Handlers.front()] = TryLow; |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 2697 | for (const BasicBlock *PredBlock : predecessors(FirstTryPad)) |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 2698 | if ((PredBlock = getEHPadFromPredecessor(PredBlock))) |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 2699 | calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, TryLow); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 2700 | int CatchLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr); |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 2701 | |
| 2702 | // catchpads are separate funclets in C++ EH due to the way rethrow works. |
| 2703 | // In SEH, they aren't, so no invokes will unwind to the catchendpad. |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 2704 | FuncInfo.EHPadStateMap[FirstNonPHI] = CatchLow; |
| 2705 | int TryHigh = CatchLow - 1; |
| 2706 | for (const BasicBlock *PredBlock : predecessors(&BB)) |
| 2707 | if ((PredBlock = getEHPadFromPredecessor(PredBlock))) |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 2708 | calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, CatchLow); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 2709 | int CatchHigh = FuncInfo.getLastStateNumber(); |
| 2710 | addTryBlockMapEntry(FuncInfo, TryLow, TryHigh, CatchHigh, Handlers); |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 2711 | DEBUG(dbgs() << "TryLow[" << FirstTryPad->getName() << "]: " << TryLow |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 2712 | << '\n'); |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 2713 | DEBUG(dbgs() << "TryHigh[" << FirstTryPad->getName() << "]: " << TryHigh |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 2714 | << '\n'); |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 2715 | DEBUG(dbgs() << "CatchHigh[" << FirstTryPad->getName() << "]: " << CatchHigh |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 2716 | << '\n'); |
| 2717 | } else if (isa<CleanupPadInst>(FirstNonPHI)) { |
Joseph Tremoulet | 676e5cf | 2015-10-09 00:46:08 +0000 | [diff] [blame^] | 2718 | // A cleanup can have multiple exits; don't re-process after the first. |
| 2719 | if (FuncInfo.EHPadStateMap.count(FirstNonPHI)) |
| 2720 | return; |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 2721 | int CleanupState = addUnwindMapEntry(FuncInfo, ParentState, &BB); |
| 2722 | FuncInfo.EHPadStateMap[FirstNonPHI] = CleanupState; |
| 2723 | DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB " |
| 2724 | << BB.getName() << '\n'); |
| 2725 | for (const BasicBlock *PredBlock : predecessors(&BB)) |
| 2726 | if ((PredBlock = getEHPadFromPredecessor(PredBlock))) |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 2727 | calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, CleanupState); |
Joseph Tremoulet | 676e5cf | 2015-10-09 00:46:08 +0000 | [diff] [blame^] | 2728 | } else if (auto *CEPI = dyn_cast<CleanupEndPadInst>(FirstNonPHI)) { |
| 2729 | // Propagate ParentState to the cleanuppad in case it doesn't have |
| 2730 | // any cleanuprets. |
| 2731 | BasicBlock *CleanupBlock = CEPI->getCleanupPad()->getParent(); |
| 2732 | calculateExplicitCXXStateNumbers(FuncInfo, *CleanupBlock, ParentState); |
| 2733 | // Anything unwinding through CleanupEndPadInst is in ParentState. |
| 2734 | for (const BasicBlock *PredBlock : predecessors(&BB)) |
| 2735 | if ((PredBlock = getEHPadFromPredecessor(PredBlock))) |
| 2736 | calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, ParentState); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 2737 | } else if (isa<TerminatePadInst>(FirstNonPHI)) { |
| 2738 | report_fatal_error("Not yet implemented!"); |
| 2739 | } else { |
| 2740 | llvm_unreachable("unexpected EH Pad!"); |
| 2741 | } |
| 2742 | } |
| 2743 | |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 2744 | static int addSEHExcept(WinEHFuncInfo &FuncInfo, int ParentState, |
| 2745 | const Function *Filter, const BasicBlock *Handler) { |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 2746 | SEHUnwindMapEntry Entry; |
| 2747 | Entry.ToState = ParentState; |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 2748 | Entry.IsFinally = false; |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 2749 | Entry.Filter = Filter; |
| 2750 | Entry.Handler = Handler; |
| 2751 | FuncInfo.SEHUnwindMap.push_back(Entry); |
| 2752 | return FuncInfo.SEHUnwindMap.size() - 1; |
| 2753 | } |
| 2754 | |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 2755 | static int addSEHFinally(WinEHFuncInfo &FuncInfo, int ParentState, |
| 2756 | const BasicBlock *Handler) { |
| 2757 | SEHUnwindMapEntry Entry; |
| 2758 | Entry.ToState = ParentState; |
| 2759 | Entry.IsFinally = true; |
| 2760 | Entry.Filter = nullptr; |
| 2761 | Entry.Handler = Handler; |
| 2762 | FuncInfo.SEHUnwindMap.push_back(Entry); |
| 2763 | return FuncInfo.SEHUnwindMap.size() - 1; |
| 2764 | } |
| 2765 | |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 2766 | static void calculateExplicitSEHStateNumbers(WinEHFuncInfo &FuncInfo, |
| 2767 | const BasicBlock &BB, |
| 2768 | int ParentState) { |
| 2769 | assert(BB.isEHPad()); |
| 2770 | const Instruction *FirstNonPHI = BB.getFirstNonPHI(); |
| 2771 | // All catchpad instructions will be handled when we process their |
| 2772 | // respective catchendpad instruction. |
| 2773 | if (isa<CatchPadInst>(FirstNonPHI)) |
| 2774 | return; |
| 2775 | |
| 2776 | if (isa<CatchEndPadInst>(FirstNonPHI)) { |
| 2777 | // Extract the filter function and the __except basic block and create a |
| 2778 | // state for them. |
| 2779 | SmallVector<const CatchPadInst *, 1> Handlers; |
| 2780 | findCatchPadsForCatchEndPad(&BB, Handlers); |
| 2781 | assert(Handlers.size() == 1 && |
| 2782 | "SEH doesn't have multiple handlers per __try"); |
| 2783 | const CatchPadInst *CPI = Handlers.front(); |
| 2784 | const BasicBlock *CatchPadBB = CPI->getParent(); |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 2785 | const Constant *FilterOrNull = |
| 2786 | cast<Constant>(CPI->getArgOperand(0)->stripPointerCasts()); |
| 2787 | const Function *Filter = dyn_cast<Function>(FilterOrNull); |
| 2788 | assert((Filter || FilterOrNull->isNullValue()) && |
| 2789 | "unexpected filter value"); |
David Majnemer | 7735a6d | 2015-10-06 23:31:59 +0000 | [diff] [blame] | 2790 | int TryState = addSEHExcept(FuncInfo, ParentState, Filter, CatchPadBB); |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 2791 | |
| 2792 | // Everything in the __try block uses TryState as its parent state. |
| 2793 | FuncInfo.EHPadStateMap[CPI] = TryState; |
| 2794 | DEBUG(dbgs() << "Assigning state #" << TryState << " to BB " |
| 2795 | << CatchPadBB->getName() << '\n'); |
| 2796 | for (const BasicBlock *PredBlock : predecessors(CatchPadBB)) |
| 2797 | if ((PredBlock = getEHPadFromPredecessor(PredBlock))) |
| 2798 | calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, TryState); |
| 2799 | |
| 2800 | // Everything in the __except block unwinds to ParentState, just like code |
| 2801 | // outside the __try. |
| 2802 | FuncInfo.EHPadStateMap[FirstNonPHI] = ParentState; |
| 2803 | DEBUG(dbgs() << "Assigning state #" << ParentState << " to BB " |
| 2804 | << BB.getName() << '\n'); |
| 2805 | for (const BasicBlock *PredBlock : predecessors(&BB)) |
| 2806 | if ((PredBlock = getEHPadFromPredecessor(PredBlock))) |
| 2807 | calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, ParentState); |
| 2808 | } else if (isa<CleanupPadInst>(FirstNonPHI)) { |
Joseph Tremoulet | 676e5cf | 2015-10-09 00:46:08 +0000 | [diff] [blame^] | 2809 | // A cleanup can have multiple exits; don't re-process after the first. |
| 2810 | if (FuncInfo.EHPadStateMap.count(FirstNonPHI)) |
| 2811 | return; |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 2812 | int CleanupState = addSEHFinally(FuncInfo, ParentState, &BB); |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 2813 | FuncInfo.EHPadStateMap[FirstNonPHI] = CleanupState; |
| 2814 | DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB " |
| 2815 | << BB.getName() << '\n'); |
| 2816 | for (const BasicBlock *PredBlock : predecessors(&BB)) |
| 2817 | if ((PredBlock = getEHPadFromPredecessor(PredBlock))) |
| 2818 | calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, CleanupState); |
Joseph Tremoulet | 676e5cf | 2015-10-09 00:46:08 +0000 | [diff] [blame^] | 2819 | } else if (auto *CEPI = dyn_cast<CleanupEndPadInst>(FirstNonPHI)) { |
| 2820 | // Propagate ParentState to the cleanuppad in case it doesn't have |
| 2821 | // any cleanuprets. |
| 2822 | BasicBlock *CleanupBlock = CEPI->getCleanupPad()->getParent(); |
| 2823 | calculateExplicitSEHStateNumbers(FuncInfo, *CleanupBlock, ParentState); |
Reid Kleckner | 7bb20bd | 2015-09-10 21:46:36 +0000 | [diff] [blame] | 2824 | // Anything unwinding through CleanupEndPadInst is in ParentState. |
| 2825 | FuncInfo.EHPadStateMap[FirstNonPHI] = ParentState; |
| 2826 | DEBUG(dbgs() << "Assigning state #" << ParentState << " to BB " |
| 2827 | << BB.getName() << '\n'); |
| 2828 | for (const BasicBlock *PredBlock : predecessors(&BB)) |
| 2829 | if ((PredBlock = getEHPadFromPredecessor(PredBlock))) |
| 2830 | calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, ParentState); |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 2831 | } else if (isa<TerminatePadInst>(FirstNonPHI)) { |
| 2832 | report_fatal_error("Not yet implemented!"); |
| 2833 | } else { |
| 2834 | llvm_unreachable("unexpected EH Pad!"); |
| 2835 | } |
| 2836 | } |
| 2837 | |
| 2838 | /// Check if the EH Pad unwinds to caller. Cleanups are a little bit of a |
| 2839 | /// special case because we have to look at the cleanupret instruction that uses |
| 2840 | /// the cleanuppad. |
| 2841 | static bool doesEHPadUnwindToCaller(const Instruction *EHPad) { |
| 2842 | auto *CPI = dyn_cast<CleanupPadInst>(EHPad); |
| 2843 | if (!CPI) |
| 2844 | return EHPad->mayThrow(); |
| 2845 | |
| 2846 | // This cleanup does not return or unwind, so we say it unwinds to caller. |
| 2847 | if (CPI->use_empty()) |
| 2848 | return true; |
| 2849 | |
| 2850 | const Instruction *User = CPI->user_back(); |
| 2851 | if (auto *CRI = dyn_cast<CleanupReturnInst>(User)) |
| 2852 | return CRI->unwindsToCaller(); |
| 2853 | return cast<CleanupEndPadInst>(User)->unwindsToCaller(); |
| 2854 | } |
| 2855 | |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 2856 | void llvm::calculateSEHStateNumbers(const Function *Fn, |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 2857 | WinEHFuncInfo &FuncInfo) { |
| 2858 | // Don't compute state numbers twice. |
| 2859 | if (!FuncInfo.SEHUnwindMap.empty()) |
| 2860 | return; |
| 2861 | |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 2862 | for (const BasicBlock &BB : *Fn) { |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 2863 | if (!BB.isEHPad() || !doesEHPadUnwindToCaller(BB.getFirstNonPHI())) |
| 2864 | continue; |
| 2865 | calculateExplicitSEHStateNumbers(FuncInfo, BB, -1); |
| 2866 | } |
| 2867 | } |
| 2868 | |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 2869 | void llvm::calculateWinCXXEHStateNumbers(const Function *Fn, |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 2870 | WinEHFuncInfo &FuncInfo) { |
| 2871 | // Return if it's already been done. |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 2872 | if (!FuncInfo.EHPadStateMap.empty()) |
| 2873 | return; |
| 2874 | |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 2875 | for (const BasicBlock &BB : *Fn) { |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 2876 | if (!BB.isEHPad()) |
| 2877 | continue; |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 2878 | if (BB.isLandingPad()) |
| 2879 | report_fatal_error("MSVC C++ EH cannot use landingpads"); |
Joseph Tremoulet | 9ce71f7 | 2015-09-03 09:09:43 +0000 | [diff] [blame] | 2880 | const Instruction *FirstNonPHI = BB.getFirstNonPHI(); |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 2881 | if (!doesEHPadUnwindToCaller(FirstNonPHI)) |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 2882 | continue; |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 2883 | calculateExplicitCXXStateNumbers(FuncInfo, BB, -1); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 2884 | } |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 2885 | } |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 2886 | |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 2887 | static int addClrEHHandler(WinEHFuncInfo &FuncInfo, int ParentState, |
| 2888 | ClrHandlerType HandlerType, uint32_t TypeToken, |
| 2889 | const BasicBlock *Handler) { |
| 2890 | ClrEHUnwindMapEntry Entry; |
| 2891 | Entry.Parent = ParentState; |
| 2892 | Entry.Handler = Handler; |
| 2893 | Entry.HandlerType = HandlerType; |
| 2894 | Entry.TypeToken = TypeToken; |
| 2895 | FuncInfo.ClrEHUnwindMap.push_back(Entry); |
| 2896 | return FuncInfo.ClrEHUnwindMap.size() - 1; |
| 2897 | } |
| 2898 | |
| 2899 | void llvm::calculateClrEHStateNumbers(const Function *Fn, |
| 2900 | WinEHFuncInfo &FuncInfo) { |
| 2901 | // Return if it's already been done. |
| 2902 | if (!FuncInfo.EHPadStateMap.empty()) |
| 2903 | return; |
| 2904 | |
| 2905 | SmallVector<std::pair<const Instruction *, int>, 8> Worklist; |
| 2906 | |
| 2907 | // Each pad needs to be able to refer to its parent, so scan the function |
| 2908 | // looking for top-level handlers and seed the worklist with them. |
| 2909 | for (const BasicBlock &BB : *Fn) { |
| 2910 | if (!BB.isEHPad()) |
| 2911 | continue; |
| 2912 | if (BB.isLandingPad()) |
| 2913 | report_fatal_error("CoreCLR EH cannot use landingpads"); |
| 2914 | const Instruction *FirstNonPHI = BB.getFirstNonPHI(); |
| 2915 | if (!doesEHPadUnwindToCaller(FirstNonPHI)) |
| 2916 | continue; |
| 2917 | // queue this with sentinel parent state -1 to mean unwind to caller. |
| 2918 | Worklist.emplace_back(FirstNonPHI, -1); |
| 2919 | } |
| 2920 | |
| 2921 | while (!Worklist.empty()) { |
| 2922 | const Instruction *Pad; |
| 2923 | int ParentState; |
| 2924 | std::tie(Pad, ParentState) = Worklist.pop_back_val(); |
| 2925 | |
| 2926 | int PredState; |
| 2927 | if (const CleanupEndPadInst *EndPad = dyn_cast<CleanupEndPadInst>(Pad)) { |
| 2928 | FuncInfo.EHPadStateMap[EndPad] = ParentState; |
| 2929 | // Queue the cleanuppad, in case it doesn't have a cleanupret. |
| 2930 | Worklist.emplace_back(EndPad->getCleanupPad(), ParentState); |
| 2931 | // Preds of the endpad should get the parent state. |
| 2932 | PredState = ParentState; |
| 2933 | } else if (const CleanupPadInst *Cleanup = dyn_cast<CleanupPadInst>(Pad)) { |
| 2934 | // A cleanup can have multiple exits; don't re-process after the first. |
| 2935 | if (FuncInfo.EHPadStateMap.count(Pad)) |
| 2936 | continue; |
| 2937 | // CoreCLR personality uses arity to distinguish faults from finallies. |
| 2938 | const BasicBlock *PadBlock = Cleanup->getParent(); |
| 2939 | ClrHandlerType HandlerType = |
| 2940 | (Cleanup->getNumOperands() ? ClrHandlerType::Fault |
| 2941 | : ClrHandlerType::Finally); |
| 2942 | int NewState = |
| 2943 | addClrEHHandler(FuncInfo, ParentState, HandlerType, 0, PadBlock); |
| 2944 | FuncInfo.EHPadStateMap[Cleanup] = NewState; |
| 2945 | // Propagate the new state to all preds of the cleanup |
| 2946 | PredState = NewState; |
| 2947 | } else if (const CatchEndPadInst *EndPad = dyn_cast<CatchEndPadInst>(Pad)) { |
| 2948 | FuncInfo.EHPadStateMap[EndPad] = ParentState; |
| 2949 | // Preds of the endpad should get the parent state. |
| 2950 | PredState = ParentState; |
| 2951 | } else if (const CatchPadInst *Catch = dyn_cast<CatchPadInst>(Pad)) { |
Joseph Tremoulet | bde46c5 | 2015-10-07 17:16:25 +0000 | [diff] [blame] | 2952 | const BasicBlock *PadBlock = Catch->getParent(); |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 2953 | uint32_t TypeToken = static_cast<uint32_t>( |
| 2954 | cast<ConstantInt>(Catch->getArgOperand(0))->getZExtValue()); |
| 2955 | int NewState = addClrEHHandler(FuncInfo, ParentState, |
Joseph Tremoulet | bde46c5 | 2015-10-07 17:16:25 +0000 | [diff] [blame] | 2956 | ClrHandlerType::Catch, TypeToken, PadBlock); |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 2957 | FuncInfo.EHPadStateMap[Catch] = NewState; |
| 2958 | // Preds of the catch get its state |
| 2959 | PredState = NewState; |
| 2960 | } else { |
| 2961 | llvm_unreachable("Unexpected EH pad"); |
| 2962 | } |
| 2963 | |
| 2964 | // Queue all predecessors with the given state |
| 2965 | for (const BasicBlock *Pred : predecessors(Pad->getParent())) { |
| 2966 | if ((Pred = getEHPadFromPredecessor(Pred))) |
| 2967 | Worklist.emplace_back(Pred->getFirstNonPHI(), PredState); |
| 2968 | } |
| 2969 | } |
| 2970 | } |
| 2971 | |
David Majnemer | 67bff0d | 2015-09-16 20:42:16 +0000 | [diff] [blame] | 2972 | void WinEHPrepare::replaceTerminatePadWithCleanup(Function &F) { |
| 2973 | if (Personality != EHPersonality::MSVC_CXX) |
| 2974 | return; |
| 2975 | for (BasicBlock &BB : F) { |
| 2976 | Instruction *First = BB.getFirstNonPHI(); |
| 2977 | auto *TPI = dyn_cast<TerminatePadInst>(First); |
| 2978 | if (!TPI) |
| 2979 | continue; |
| 2980 | |
| 2981 | if (TPI->getNumArgOperands() != 1) |
| 2982 | report_fatal_error( |
| 2983 | "Expected a unary terminatepad for MSVC C++ personalities!"); |
| 2984 | |
| 2985 | auto *TerminateFn = dyn_cast<Function>(TPI->getArgOperand(0)); |
| 2986 | if (!TerminateFn) |
| 2987 | report_fatal_error("Function operand expected in terminatepad for MSVC " |
| 2988 | "C++ personalities!"); |
| 2989 | |
| 2990 | // Insert the cleanuppad instruction. |
| 2991 | auto *CPI = CleanupPadInst::Create( |
| 2992 | BB.getContext(), {}, Twine("terminatepad.for.", BB.getName()), &BB); |
| 2993 | |
| 2994 | // Insert the call to the terminate instruction. |
| 2995 | auto *CallTerminate = CallInst::Create(TerminateFn, {}, &BB); |
| 2996 | CallTerminate->setDoesNotThrow(); |
| 2997 | CallTerminate->setDoesNotReturn(); |
| 2998 | CallTerminate->setCallingConv(TerminateFn->getCallingConv()); |
| 2999 | |
| 3000 | // Insert a new terminator for the cleanuppad using the same successor as |
| 3001 | // the terminatepad. |
| 3002 | CleanupReturnInst::Create(CPI, TPI->getUnwindDest(), &BB); |
| 3003 | |
| 3004 | // Let's remove the terminatepad now that we've inserted the new |
| 3005 | // instructions. |
| 3006 | TPI->eraseFromParent(); |
| 3007 | } |
| 3008 | } |
| 3009 | |
David Majnemer | f828a0c | 2015-10-01 18:44:59 +0000 | [diff] [blame] | 3010 | static void |
| 3011 | colorFunclets(Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks, |
| 3012 | std::map<BasicBlock *, std::set<BasicBlock *>> &BlockColors, |
| 3013 | std::map<BasicBlock *, std::set<BasicBlock *>> &FuncletBlocks, |
| 3014 | std::map<BasicBlock *, std::set<BasicBlock *>> &FuncletChildren) { |
Joseph Tremoulet | ec18285 | 2015-08-28 01:12:35 +0000 | [diff] [blame] | 3015 | SmallVector<std::pair<BasicBlock *, BasicBlock *>, 16> Worklist; |
| 3016 | BasicBlock *EntryBlock = &F.getEntryBlock(); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3017 | |
Joseph Tremoulet | ec18285 | 2015-08-28 01:12:35 +0000 | [diff] [blame] | 3018 | // Build up the color map, which maps each block to its set of 'colors'. |
| 3019 | // For any block B, the "colors" of B are the set of funclets F (possibly |
| 3020 | // including a root "funclet" representing the main function), such that |
| 3021 | // F will need to directly contain B or a copy of B (where the term "directly |
| 3022 | // contain" is used to distinguish from being "transitively contained" in |
| 3023 | // a nested funclet). |
| 3024 | // Use a CFG walk driven by a worklist of (block, color) pairs. The "color" |
| 3025 | // sets attached during this processing to a block which is the entry of some |
| 3026 | // funclet F is actually the set of F's parents -- i.e. the union of colors |
| 3027 | // of all predecessors of F's entry. For all other blocks, the color sets |
| 3028 | // are as defined above. A post-pass fixes up the block color map to reflect |
| 3029 | // the same sense of "color" for funclet entries as for other blocks. |
| 3030 | |
| 3031 | Worklist.push_back({EntryBlock, EntryBlock}); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3032 | |
| 3033 | while (!Worklist.empty()) { |
Joseph Tremoulet | ec18285 | 2015-08-28 01:12:35 +0000 | [diff] [blame] | 3034 | BasicBlock *Visiting; |
| 3035 | BasicBlock *Color; |
| 3036 | std::tie(Visiting, Color) = Worklist.pop_back_val(); |
| 3037 | Instruction *VisitingHead = Visiting->getFirstNonPHI(); |
Joseph Tremoulet | 9ce71f7 | 2015-09-03 09:09:43 +0000 | [diff] [blame] | 3038 | if (VisitingHead->isEHPad() && !isa<CatchEndPadInst>(VisitingHead) && |
| 3039 | !isa<CleanupEndPadInst>(VisitingHead)) { |
Joseph Tremoulet | ec18285 | 2015-08-28 01:12:35 +0000 | [diff] [blame] | 3040 | // Mark this as a funclet head as a member of itself. |
| 3041 | FuncletBlocks[Visiting].insert(Visiting); |
| 3042 | // Queue exits with the parent color. |
Reid Kleckner | 72ba704 | 2015-10-07 00:27:33 +0000 | [diff] [blame] | 3043 | for (User *U : VisitingHead->users()) { |
| 3044 | if (auto *Exit = dyn_cast<TerminatorInst>(U)) { |
| 3045 | for (BasicBlock *Succ : successors(Exit->getParent())) |
| 3046 | if (BlockColors[Succ].insert(Color).second) |
| 3047 | Worklist.push_back({Succ, Color}); |
Joseph Tremoulet | ec18285 | 2015-08-28 01:12:35 +0000 | [diff] [blame] | 3048 | } |
| 3049 | } |
| 3050 | // Handle CatchPad specially since its successors need different colors. |
| 3051 | if (CatchPadInst *CatchPad = dyn_cast<CatchPadInst>(VisitingHead)) { |
| 3052 | // Visit the normal successor with the color of the new EH pad, and |
| 3053 | // visit the unwind successor with the color of the parent. |
| 3054 | BasicBlock *NormalSucc = CatchPad->getNormalDest(); |
| 3055 | if (BlockColors[NormalSucc].insert(Visiting).second) { |
| 3056 | Worklist.push_back({NormalSucc, Visiting}); |
| 3057 | } |
| 3058 | BasicBlock *UnwindSucc = CatchPad->getUnwindDest(); |
| 3059 | if (BlockColors[UnwindSucc].insert(Color).second) { |
| 3060 | Worklist.push_back({UnwindSucc, Color}); |
| 3061 | } |
| 3062 | continue; |
| 3063 | } |
| 3064 | // Switch color to the current node, except for terminate pads which |
| 3065 | // have no bodies and only unwind successors and so need their successors |
| 3066 | // visited with the color of the parent. |
| 3067 | if (!isa<TerminatePadInst>(VisitingHead)) |
| 3068 | Color = Visiting; |
| 3069 | } else { |
| 3070 | // Note that this is a member of the given color. |
| 3071 | FuncletBlocks[Color].insert(Visiting); |
Joseph Tremoulet | f3aff31 | 2015-09-10 16:51:25 +0000 | [diff] [blame] | 3072 | } |
| 3073 | |
| 3074 | TerminatorInst *Terminator = Visiting->getTerminator(); |
| 3075 | if (isa<CleanupReturnInst>(Terminator) || |
| 3076 | isa<CatchReturnInst>(Terminator) || |
| 3077 | isa<CleanupEndPadInst>(Terminator)) { |
| 3078 | // These blocks' successors have already been queued with the parent |
| 3079 | // color. |
| 3080 | continue; |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3081 | } |
Joseph Tremoulet | ec18285 | 2015-08-28 01:12:35 +0000 | [diff] [blame] | 3082 | for (BasicBlock *Succ : successors(Visiting)) { |
| 3083 | if (isa<CatchEndPadInst>(Succ->getFirstNonPHI())) { |
| 3084 | // The catchendpad needs to be visited with the parent's color, not |
| 3085 | // the current color. This will happen in the code above that visits |
| 3086 | // any catchpad unwind successor with the parent color, so we can |
| 3087 | // safely skip this successor here. |
| 3088 | continue; |
| 3089 | } |
| 3090 | if (BlockColors[Succ].insert(Color).second) { |
| 3091 | Worklist.push_back({Succ, Color}); |
| 3092 | } |
| 3093 | } |
| 3094 | } |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3095 | |
Joseph Tremoulet | ec18285 | 2015-08-28 01:12:35 +0000 | [diff] [blame] | 3096 | // The processing above actually accumulated the parent set for this |
| 3097 | // funclet into the color set for its entry; use the parent set to |
| 3098 | // populate the children map, and reset the color set to include just |
| 3099 | // the funclet itself (no instruction can target a funclet entry except on |
| 3100 | // that transitions to the child funclet). |
| 3101 | for (BasicBlock *FuncletEntry : EntryBlocks) { |
| 3102 | std::set<BasicBlock *> &ColorMapItem = BlockColors[FuncletEntry]; |
| 3103 | for (BasicBlock *Parent : ColorMapItem) |
| 3104 | FuncletChildren[Parent].insert(FuncletEntry); |
| 3105 | ColorMapItem.clear(); |
| 3106 | ColorMapItem.insert(FuncletEntry); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3107 | } |
| 3108 | } |
| 3109 | |
David Majnemer | f828a0c | 2015-10-01 18:44:59 +0000 | [diff] [blame] | 3110 | void WinEHPrepare::colorFunclets(Function &F, |
| 3111 | SmallVectorImpl<BasicBlock *> &EntryBlocks) { |
| 3112 | ::colorFunclets(F, EntryBlocks, BlockColors, FuncletBlocks, FuncletChildren); |
| 3113 | } |
| 3114 | |
| 3115 | void llvm::calculateCatchReturnSuccessorColors(const Function *Fn, |
| 3116 | WinEHFuncInfo &FuncInfo) { |
| 3117 | SmallVector<LandingPadInst *, 4> LPads; |
| 3118 | SmallVector<ResumeInst *, 4> Resumes; |
| 3119 | SmallVector<BasicBlock *, 4> EntryBlocks; |
| 3120 | // colorFunclets needs the set of EntryBlocks, get them using |
| 3121 | // findExceptionalConstructs. |
| 3122 | bool ForExplicitEH = findExceptionalConstructs(const_cast<Function &>(*Fn), |
| 3123 | LPads, Resumes, EntryBlocks); |
| 3124 | if (!ForExplicitEH) |
| 3125 | return; |
| 3126 | |
| 3127 | std::map<BasicBlock *, std::set<BasicBlock *>> BlockColors; |
| 3128 | std::map<BasicBlock *, std::set<BasicBlock *>> FuncletBlocks; |
| 3129 | std::map<BasicBlock *, std::set<BasicBlock *>> FuncletChildren; |
| 3130 | // Figure out which basic blocks belong to which funclets. |
| 3131 | colorFunclets(const_cast<Function &>(*Fn), EntryBlocks, BlockColors, |
| 3132 | FuncletBlocks, FuncletChildren); |
| 3133 | |
| 3134 | // We need to find the catchret successors. To do this, we must first find |
| 3135 | // all the catchpad funclets. |
| 3136 | for (auto &Funclet : FuncletBlocks) { |
| 3137 | // Figure out what kind of funclet we are looking at; We only care about |
| 3138 | // catchpads. |
| 3139 | BasicBlock *FuncletPadBB = Funclet.first; |
| 3140 | Instruction *FirstNonPHI = FuncletPadBB->getFirstNonPHI(); |
| 3141 | auto *CatchPad = dyn_cast<CatchPadInst>(FirstNonPHI); |
| 3142 | if (!CatchPad) |
| 3143 | continue; |
| 3144 | |
| 3145 | // The users of a catchpad are always catchrets. |
| 3146 | for (User *Exit : CatchPad->users()) { |
Reid Kleckner | 72ba704 | 2015-10-07 00:27:33 +0000 | [diff] [blame] | 3147 | auto *CatchReturn = dyn_cast<CatchReturnInst>(Exit); |
| 3148 | if (!CatchReturn) |
| 3149 | continue; |
David Majnemer | f828a0c | 2015-10-01 18:44:59 +0000 | [diff] [blame] | 3150 | BasicBlock *CatchRetSuccessor = CatchReturn->getSuccessor(); |
| 3151 | std::set<BasicBlock *> &SuccessorColors = BlockColors[CatchRetSuccessor]; |
| 3152 | assert(SuccessorColors.size() == 1 && "Expected BB to be monochrome!"); |
| 3153 | BasicBlock *Color = *SuccessorColors.begin(); |
| 3154 | if (auto *CPI = dyn_cast<CatchPadInst>(Color->getFirstNonPHI())) |
| 3155 | Color = CPI->getNormalDest(); |
| 3156 | // Record the catchret successor's funclet membership. |
| 3157 | FuncInfo.CatchRetSuccessorColorMap[CatchReturn] = Color; |
| 3158 | } |
| 3159 | } |
| 3160 | } |
| 3161 | |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 3162 | void WinEHPrepare::demotePHIsOnFunclets(Function &F) { |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 3163 | // Strip PHI nodes off of EH pads. |
| 3164 | SmallVector<PHINode *, 16> PHINodes; |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3165 | for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) { |
| 3166 | BasicBlock *BB = FI++; |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3167 | if (!BB->isEHPad()) |
| 3168 | continue; |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3169 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) { |
| 3170 | Instruction *I = BI++; |
| 3171 | auto *PN = dyn_cast<PHINode>(I); |
| 3172 | // Stop at the first non-PHI. |
| 3173 | if (!PN) |
| 3174 | break; |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3175 | |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 3176 | AllocaInst *SpillSlot = insertPHILoads(PN, F); |
| 3177 | if (SpillSlot) |
| 3178 | insertPHIStores(PN, SpillSlot); |
| 3179 | |
| 3180 | PHINodes.push_back(PN); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3181 | } |
| 3182 | } |
| 3183 | |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 3184 | for (auto *PN : PHINodes) { |
| 3185 | // There may be lingering uses on other EH PHIs being removed |
| 3186 | PN->replaceAllUsesWith(UndefValue::get(PN->getType())); |
| 3187 | PN->eraseFromParent(); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3188 | } |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 3189 | } |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3190 | |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 3191 | void WinEHPrepare::demoteUsesBetweenFunclets(Function &F) { |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3192 | // Turn all inter-funclet uses of a Value into loads and stores. |
| 3193 | for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) { |
| 3194 | BasicBlock *BB = FI++; |
| 3195 | std::set<BasicBlock *> &ColorsForBB = BlockColors[BB]; |
| 3196 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) { |
| 3197 | Instruction *I = BI++; |
| 3198 | // Funclets are permitted to use static allocas. |
| 3199 | if (auto *AI = dyn_cast<AllocaInst>(I)) |
| 3200 | if (AI->isStaticAlloca()) |
| 3201 | continue; |
| 3202 | |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 3203 | demoteNonlocalUses(I, ColorsForBB, F); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3204 | } |
| 3205 | } |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 3206 | } |
| 3207 | |
| 3208 | void WinEHPrepare::demoteArgumentUses(Function &F) { |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 3209 | // Also demote function parameters used in funclets. |
| 3210 | std::set<BasicBlock *> &ColorsForEntry = BlockColors[&F.getEntryBlock()]; |
| 3211 | for (Argument &Arg : F.args()) |
| 3212 | demoteNonlocalUses(&Arg, ColorsForEntry, F); |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 3213 | } |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3214 | |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 3215 | void WinEHPrepare::cloneCommonBlocks( |
| 3216 | Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks) { |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3217 | // We need to clone all blocks which belong to multiple funclets. Values are |
| 3218 | // remapped throughout the funclet to propogate both the new instructions |
| 3219 | // *and* the new basic blocks themselves. |
Joseph Tremoulet | ec18285 | 2015-08-28 01:12:35 +0000 | [diff] [blame] | 3220 | for (BasicBlock *FuncletPadBB : EntryBlocks) { |
| 3221 | std::set<BasicBlock *> &BlocksInFunclet = FuncletBlocks[FuncletPadBB]; |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3222 | |
| 3223 | std::map<BasicBlock *, BasicBlock *> Orig2Clone; |
| 3224 | ValueToValueMapTy VMap; |
| 3225 | for (BasicBlock *BB : BlocksInFunclet) { |
| 3226 | std::set<BasicBlock *> &ColorsForBB = BlockColors[BB]; |
| 3227 | // We don't need to do anything if the block is monochromatic. |
| 3228 | size_t NumColorsForBB = ColorsForBB.size(); |
| 3229 | if (NumColorsForBB == 1) |
| 3230 | continue; |
| 3231 | |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3232 | // Create a new basic block and copy instructions into it! |
Joseph Tremoulet | ec18285 | 2015-08-28 01:12:35 +0000 | [diff] [blame] | 3233 | BasicBlock *CBB = |
| 3234 | CloneBasicBlock(BB, VMap, Twine(".for.", FuncletPadBB->getName())); |
| 3235 | // Insert the clone immediately after the original to ensure determinism |
| 3236 | // and to keep the same relative ordering of any funclet's blocks. |
| 3237 | CBB->insertInto(&F, BB->getNextNode()); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3238 | |
| 3239 | // Add basic block mapping. |
| 3240 | VMap[BB] = CBB; |
| 3241 | |
| 3242 | // Record delta operations that we need to perform to our color mappings. |
| 3243 | Orig2Clone[BB] = CBB; |
| 3244 | } |
| 3245 | |
Joseph Tremoulet | 39234fc | 2015-10-07 19:29:56 +0000 | [diff] [blame] | 3246 | // If nothing was cloned, we're done cloning in this funclet. |
| 3247 | if (Orig2Clone.empty()) |
| 3248 | continue; |
| 3249 | |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3250 | // Update our color mappings to reflect that one block has lost a color and |
| 3251 | // another has gained a color. |
| 3252 | for (auto &BBMapping : Orig2Clone) { |
| 3253 | BasicBlock *OldBlock = BBMapping.first; |
| 3254 | BasicBlock *NewBlock = BBMapping.second; |
| 3255 | |
| 3256 | BlocksInFunclet.insert(NewBlock); |
| 3257 | BlockColors[NewBlock].insert(FuncletPadBB); |
| 3258 | |
| 3259 | BlocksInFunclet.erase(OldBlock); |
| 3260 | BlockColors[OldBlock].erase(FuncletPadBB); |
| 3261 | } |
| 3262 | |
Joseph Tremoulet | 39234fc | 2015-10-07 19:29:56 +0000 | [diff] [blame] | 3263 | // Loop over all of the instructions in this funclet, fixing up operand |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3264 | // references as we go. This uses VMap to do all the hard work. |
| 3265 | for (BasicBlock *BB : BlocksInFunclet) |
| 3266 | // Loop over all instructions, fixing each one as we find it... |
| 3267 | for (Instruction &I : *BB) |
Joseph Tremoulet | 39234fc | 2015-10-07 19:29:56 +0000 | [diff] [blame] | 3268 | RemapInstruction(&I, VMap, |
| 3269 | RF_IgnoreMissingEntries | RF_NoModuleLevelChanges); |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 3270 | |
| 3271 | // Check to see if SuccBB has PHI nodes. If so, we need to add entries to |
| 3272 | // the PHI nodes for NewBB now. |
| 3273 | for (auto &BBMapping : Orig2Clone) { |
| 3274 | BasicBlock *OldBlock = BBMapping.first; |
| 3275 | BasicBlock *NewBlock = BBMapping.second; |
| 3276 | for (BasicBlock *SuccBB : successors(NewBlock)) { |
| 3277 | for (Instruction &SuccI : *SuccBB) { |
| 3278 | auto *SuccPN = dyn_cast<PHINode>(&SuccI); |
| 3279 | if (!SuccPN) |
| 3280 | break; |
| 3281 | |
| 3282 | // Ok, we have a PHI node. Figure out what the incoming value was for |
| 3283 | // the OldBlock. |
| 3284 | int OldBlockIdx = SuccPN->getBasicBlockIndex(OldBlock); |
| 3285 | if (OldBlockIdx == -1) |
| 3286 | break; |
| 3287 | Value *IV = SuccPN->getIncomingValue(OldBlockIdx); |
| 3288 | |
| 3289 | // Remap the value if necessary. |
| 3290 | if (auto *Inst = dyn_cast<Instruction>(IV)) { |
| 3291 | ValueToValueMapTy::iterator I = VMap.find(Inst); |
| 3292 | if (I != VMap.end()) |
| 3293 | IV = I->second; |
| 3294 | } |
| 3295 | |
| 3296 | SuccPN->addIncoming(IV, NewBlock); |
| 3297 | } |
| 3298 | } |
| 3299 | } |
| 3300 | |
| 3301 | for (ValueToValueMapTy::value_type VT : VMap) { |
| 3302 | // If there were values defined in BB that are used outside the funclet, |
| 3303 | // then we now have to update all uses of the value to use either the |
| 3304 | // original value, the cloned value, or some PHI derived value. This can |
| 3305 | // require arbitrary PHI insertion, of which we are prepared to do, clean |
| 3306 | // these up now. |
| 3307 | SmallVector<Use *, 16> UsesToRename; |
| 3308 | |
| 3309 | auto *OldI = dyn_cast<Instruction>(const_cast<Value *>(VT.first)); |
| 3310 | if (!OldI) |
| 3311 | continue; |
| 3312 | auto *NewI = cast<Instruction>(VT.second); |
| 3313 | // Scan all uses of this instruction to see if it is used outside of its |
| 3314 | // funclet, and if so, record them in UsesToRename. |
| 3315 | for (Use &U : OldI->uses()) { |
| 3316 | Instruction *UserI = cast<Instruction>(U.getUser()); |
| 3317 | BasicBlock *UserBB = UserI->getParent(); |
| 3318 | std::set<BasicBlock *> &ColorsForUserBB = BlockColors[UserBB]; |
| 3319 | assert(!ColorsForUserBB.empty()); |
| 3320 | if (ColorsForUserBB.size() > 1 || |
| 3321 | *ColorsForUserBB.begin() != FuncletPadBB) |
| 3322 | UsesToRename.push_back(&U); |
| 3323 | } |
| 3324 | |
| 3325 | // If there are no uses outside the block, we're done with this |
| 3326 | // instruction. |
| 3327 | if (UsesToRename.empty()) |
| 3328 | continue; |
| 3329 | |
| 3330 | // We found a use of OldI outside of the funclet. Rename all uses of OldI |
| 3331 | // that are outside its funclet to be uses of the appropriate PHI node |
| 3332 | // etc. |
| 3333 | SSAUpdater SSAUpdate; |
| 3334 | SSAUpdate.Initialize(OldI->getType(), OldI->getName()); |
| 3335 | SSAUpdate.AddAvailableValue(OldI->getParent(), OldI); |
| 3336 | SSAUpdate.AddAvailableValue(NewI->getParent(), NewI); |
| 3337 | |
| 3338 | while (!UsesToRename.empty()) |
| 3339 | SSAUpdate.RewriteUseAfterInsertions(*UsesToRename.pop_back_val()); |
| 3340 | } |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3341 | } |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 3342 | } |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3343 | |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 3344 | void WinEHPrepare::removeImplausibleTerminators(Function &F) { |
David Majnemer | 83f4bb2 | 2015-08-17 20:56:39 +0000 | [diff] [blame] | 3345 | // Remove implausible terminators and replace them with UnreachableInst. |
| 3346 | for (auto &Funclet : FuncletBlocks) { |
| 3347 | BasicBlock *FuncletPadBB = Funclet.first; |
| 3348 | std::set<BasicBlock *> &BlocksInFunclet = Funclet.second; |
| 3349 | Instruction *FirstNonPHI = FuncletPadBB->getFirstNonPHI(); |
| 3350 | auto *CatchPad = dyn_cast<CatchPadInst>(FirstNonPHI); |
| 3351 | auto *CleanupPad = dyn_cast<CleanupPadInst>(FirstNonPHI); |
| 3352 | |
| 3353 | for (BasicBlock *BB : BlocksInFunclet) { |
| 3354 | TerminatorInst *TI = BB->getTerminator(); |
| 3355 | // CatchPadInst and CleanupPadInst can't transfer control to a ReturnInst. |
| 3356 | bool IsUnreachableRet = isa<ReturnInst>(TI) && (CatchPad || CleanupPad); |
| 3357 | // The token consumed by a CatchReturnInst must match the funclet token. |
| 3358 | bool IsUnreachableCatchret = false; |
| 3359 | if (auto *CRI = dyn_cast<CatchReturnInst>(TI)) |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 3360 | IsUnreachableCatchret = CRI->getCatchPad() != CatchPad; |
Joseph Tremoulet | 9ce71f7 | 2015-09-03 09:09:43 +0000 | [diff] [blame] | 3361 | // The token consumed by a CleanupReturnInst must match the funclet token. |
David Majnemer | 83f4bb2 | 2015-08-17 20:56:39 +0000 | [diff] [blame] | 3362 | bool IsUnreachableCleanupret = false; |
| 3363 | if (auto *CRI = dyn_cast<CleanupReturnInst>(TI)) |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 3364 | IsUnreachableCleanupret = CRI->getCleanupPad() != CleanupPad; |
Joseph Tremoulet | 9ce71f7 | 2015-09-03 09:09:43 +0000 | [diff] [blame] | 3365 | // The token consumed by a CleanupEndPadInst must match the funclet token. |
| 3366 | bool IsUnreachableCleanupendpad = false; |
| 3367 | if (auto *CEPI = dyn_cast<CleanupEndPadInst>(TI)) |
| 3368 | IsUnreachableCleanupendpad = CEPI->getCleanupPad() != CleanupPad; |
| 3369 | if (IsUnreachableRet || IsUnreachableCatchret || |
| 3370 | IsUnreachableCleanupret || IsUnreachableCleanupendpad) { |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 3371 | // Loop through all of our successors and make sure they know that one |
| 3372 | // of their predecessors is going away. |
| 3373 | for (BasicBlock *SuccBB : TI->successors()) |
| 3374 | SuccBB->removePredecessor(BB); |
| 3375 | |
Joseph Tremoulet | 09af67a | 2015-09-27 01:47:46 +0000 | [diff] [blame] | 3376 | if (IsUnreachableCleanupendpad) { |
| 3377 | // We can't simply replace a cleanupendpad with unreachable, because |
| 3378 | // its predecessor edges are EH edges and unreachable is not an EH |
| 3379 | // pad. Change all predecessors to the "unwind to caller" form. |
| 3380 | for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); |
| 3381 | PI != PE;) { |
| 3382 | BasicBlock *Pred = *PI++; |
| 3383 | removeUnwindEdge(Pred); |
| 3384 | } |
| 3385 | } |
| 3386 | |
David Majnemer | 83f4bb2 | 2015-08-17 20:56:39 +0000 | [diff] [blame] | 3387 | new UnreachableInst(BB->getContext(), TI); |
| 3388 | TI->eraseFromParent(); |
| 3389 | } |
Joseph Tremoulet | 09af67a | 2015-09-27 01:47:46 +0000 | [diff] [blame] | 3390 | // FIXME: Check for invokes/cleanuprets/cleanupendpads which unwind to |
| 3391 | // implausible catchendpads (i.e. catchendpad not in immediate parent |
| 3392 | // funclet). |
David Majnemer | 83f4bb2 | 2015-08-17 20:56:39 +0000 | [diff] [blame] | 3393 | } |
| 3394 | } |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 3395 | } |
David Majnemer | 83f4bb2 | 2015-08-17 20:56:39 +0000 | [diff] [blame] | 3396 | |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 3397 | void WinEHPrepare::cleanupPreparedFunclets(Function &F) { |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3398 | // Clean-up some of the mess we made by removing useles PHI nodes, trivial |
| 3399 | // branches, etc. |
| 3400 | for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) { |
| 3401 | BasicBlock *BB = FI++; |
| 3402 | SimplifyInstructionsInBlock(BB); |
| 3403 | ConstantFoldTerminator(BB, /*DeleteDeadConditions=*/true); |
| 3404 | MergeBlockIntoPredecessor(BB); |
| 3405 | } |
| 3406 | |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3407 | // We might have some unreachable blocks after cleaning up some impossible |
| 3408 | // control flow. |
| 3409 | removeUnreachableBlocks(F); |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 3410 | } |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3411 | |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 3412 | void WinEHPrepare::verifyPreparedFunclets(Function &F) { |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3413 | // Recolor the CFG to verify that all is well. |
| 3414 | for (BasicBlock &BB : F) { |
| 3415 | size_t NumColors = BlockColors[&BB].size(); |
| 3416 | assert(NumColors == 1 && "Expected monochromatic BB!"); |
| 3417 | if (NumColors == 0) |
| 3418 | report_fatal_error("Uncolored BB!"); |
| 3419 | if (NumColors > 1) |
| 3420 | report_fatal_error("Multicolor BB!"); |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 3421 | if (!DisableDemotion) { |
| 3422 | bool EHPadHasPHI = BB.isEHPad() && isa<PHINode>(BB.begin()); |
| 3423 | assert(!EHPadHasPHI && "EH Pad still has a PHI!"); |
| 3424 | if (EHPadHasPHI) |
| 3425 | report_fatal_error("EH Pad still has a PHI!"); |
| 3426 | } |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3427 | } |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 3428 | } |
| 3429 | |
| 3430 | bool WinEHPrepare::prepareExplicitEH( |
| 3431 | Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks) { |
David Majnemer | 67bff0d | 2015-09-16 20:42:16 +0000 | [diff] [blame] | 3432 | replaceTerminatePadWithCleanup(F); |
| 3433 | |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 3434 | // Determine which blocks are reachable from which funclet entries. |
| 3435 | colorFunclets(F, EntryBlocks); |
| 3436 | |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 3437 | if (!DisableDemotion) { |
| 3438 | demotePHIsOnFunclets(F); |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 3439 | |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 3440 | demoteUsesBetweenFunclets(F); |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 3441 | |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 3442 | demoteArgumentUses(F); |
| 3443 | } |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 3444 | |
| 3445 | cloneCommonBlocks(F, EntryBlocks); |
| 3446 | |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 3447 | if (!DisableCleanups) { |
| 3448 | removeImplausibleTerminators(F); |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 3449 | |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 3450 | cleanupPreparedFunclets(F); |
| 3451 | } |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 3452 | |
| 3453 | verifyPreparedFunclets(F); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3454 | |
| 3455 | BlockColors.clear(); |
| 3456 | FuncletBlocks.clear(); |
Joseph Tremoulet | ec18285 | 2015-08-28 01:12:35 +0000 | [diff] [blame] | 3457 | FuncletChildren.clear(); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 3458 | |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 3459 | return true; |
| 3460 | } |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 3461 | |
| 3462 | // TODO: Share loads when one use dominates another, or when a catchpad exit |
| 3463 | // dominates uses (needs dominators). |
| 3464 | AllocaInst *WinEHPrepare::insertPHILoads(PHINode *PN, Function &F) { |
| 3465 | BasicBlock *PHIBlock = PN->getParent(); |
| 3466 | AllocaInst *SpillSlot = nullptr; |
| 3467 | |
| 3468 | if (isa<CleanupPadInst>(PHIBlock->getFirstNonPHI())) { |
| 3469 | // Insert a load in place of the PHI and replace all uses. |
| 3470 | SpillSlot = new AllocaInst(PN->getType(), nullptr, |
| 3471 | Twine(PN->getName(), ".wineh.spillslot"), |
| 3472 | F.getEntryBlock().begin()); |
| 3473 | Value *V = new LoadInst(SpillSlot, Twine(PN->getName(), ".wineh.reload"), |
| 3474 | PHIBlock->getFirstInsertionPt()); |
| 3475 | PN->replaceAllUsesWith(V); |
| 3476 | return SpillSlot; |
| 3477 | } |
| 3478 | |
| 3479 | DenseMap<BasicBlock *, Value *> Loads; |
| 3480 | for (Value::use_iterator UI = PN->use_begin(), UE = PN->use_end(); |
| 3481 | UI != UE;) { |
| 3482 | Use &U = *UI++; |
| 3483 | auto *UsingInst = cast<Instruction>(U.getUser()); |
| 3484 | BasicBlock *UsingBB = UsingInst->getParent(); |
| 3485 | if (UsingBB->isEHPad()) { |
| 3486 | // Use is on an EH pad phi. Leave it alone; we'll insert loads and |
| 3487 | // stores for it separately. |
| 3488 | assert(isa<PHINode>(UsingInst)); |
| 3489 | continue; |
| 3490 | } |
| 3491 | replaceUseWithLoad(PN, U, SpillSlot, Loads, F); |
| 3492 | } |
| 3493 | return SpillSlot; |
| 3494 | } |
| 3495 | |
| 3496 | // TODO: improve store placement. Inserting at def is probably good, but need |
| 3497 | // to be careful not to introduce interfering stores (needs liveness analysis). |
| 3498 | // TODO: identify related phi nodes that can share spill slots, and share them |
| 3499 | // (also needs liveness). |
| 3500 | void WinEHPrepare::insertPHIStores(PHINode *OriginalPHI, |
| 3501 | AllocaInst *SpillSlot) { |
| 3502 | // Use a worklist of (Block, Value) pairs -- the given Value needs to be |
| 3503 | // stored to the spill slot by the end of the given Block. |
| 3504 | SmallVector<std::pair<BasicBlock *, Value *>, 4> Worklist; |
| 3505 | |
| 3506 | Worklist.push_back({OriginalPHI->getParent(), OriginalPHI}); |
| 3507 | |
| 3508 | while (!Worklist.empty()) { |
| 3509 | BasicBlock *EHBlock; |
| 3510 | Value *InVal; |
| 3511 | std::tie(EHBlock, InVal) = Worklist.pop_back_val(); |
| 3512 | |
| 3513 | PHINode *PN = dyn_cast<PHINode>(InVal); |
| 3514 | if (PN && PN->getParent() == EHBlock) { |
| 3515 | // The value is defined by another PHI we need to remove, with no room to |
| 3516 | // insert a store after the PHI, so each predecessor needs to store its |
| 3517 | // incoming value. |
| 3518 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i) { |
| 3519 | Value *PredVal = PN->getIncomingValue(i); |
| 3520 | |
| 3521 | // Undef can safely be skipped. |
| 3522 | if (isa<UndefValue>(PredVal)) |
| 3523 | continue; |
| 3524 | |
| 3525 | insertPHIStore(PN->getIncomingBlock(i), PredVal, SpillSlot, Worklist); |
| 3526 | } |
| 3527 | } else { |
| 3528 | // We need to store InVal, which dominates EHBlock, but can't put a store |
| 3529 | // in EHBlock, so need to put stores in each predecessor. |
| 3530 | for (BasicBlock *PredBlock : predecessors(EHBlock)) { |
| 3531 | insertPHIStore(PredBlock, InVal, SpillSlot, Worklist); |
| 3532 | } |
| 3533 | } |
| 3534 | } |
| 3535 | } |
| 3536 | |
| 3537 | void WinEHPrepare::insertPHIStore( |
| 3538 | BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot, |
| 3539 | SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist) { |
| 3540 | |
| 3541 | if (PredBlock->isEHPad() && |
| 3542 | !isa<CleanupPadInst>(PredBlock->getFirstNonPHI())) { |
| 3543 | // Pred is unsplittable, so we need to queue it on the worklist. |
| 3544 | Worklist.push_back({PredBlock, PredVal}); |
| 3545 | return; |
| 3546 | } |
| 3547 | |
| 3548 | // Otherwise, insert the store at the end of the basic block. |
| 3549 | new StoreInst(PredVal, SpillSlot, PredBlock->getTerminator()); |
| 3550 | } |
| 3551 | |
| 3552 | // TODO: Share loads for same-funclet uses (requires dominators if funclets |
| 3553 | // aren't properly nested). |
| 3554 | void WinEHPrepare::demoteNonlocalUses(Value *V, |
| 3555 | std::set<BasicBlock *> &ColorsForBB, |
| 3556 | Function &F) { |
David Majnemer | 83f4bb2 | 2015-08-17 20:56:39 +0000 | [diff] [blame] | 3557 | // Tokens can only be used non-locally due to control flow involving |
| 3558 | // unreachable edges. Don't try to demote the token usage, we'll simply |
| 3559 | // delete the cloned user later. |
| 3560 | if (isa<CatchPadInst>(V) || isa<CleanupPadInst>(V)) |
| 3561 | return; |
| 3562 | |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 3563 | DenseMap<BasicBlock *, Value *> Loads; |
| 3564 | AllocaInst *SpillSlot = nullptr; |
| 3565 | for (Value::use_iterator UI = V->use_begin(), UE = V->use_end(); UI != UE;) { |
| 3566 | Use &U = *UI++; |
| 3567 | auto *UsingInst = cast<Instruction>(U.getUser()); |
| 3568 | BasicBlock *UsingBB = UsingInst->getParent(); |
| 3569 | |
Joseph Tremoulet | ec18285 | 2015-08-28 01:12:35 +0000 | [diff] [blame] | 3570 | // Is the Use inside a block which is colored the same as the Def? |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 3571 | // If so, we don't need to escape the Def because we will clone |
| 3572 | // ourselves our own private copy. |
| 3573 | std::set<BasicBlock *> &ColorsForUsingBB = BlockColors[UsingBB]; |
Joseph Tremoulet | ec18285 | 2015-08-28 01:12:35 +0000 | [diff] [blame] | 3574 | if (ColorsForUsingBB == ColorsForBB) |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 3575 | continue; |
| 3576 | |
| 3577 | replaceUseWithLoad(V, U, SpillSlot, Loads, F); |
| 3578 | } |
| 3579 | if (SpillSlot) { |
| 3580 | // Insert stores of the computed value into the stack slot. |
| 3581 | // We have to be careful if I is an invoke instruction, |
| 3582 | // because we can't insert the store AFTER the terminator instruction. |
| 3583 | BasicBlock::iterator InsertPt; |
| 3584 | if (isa<Argument>(V)) { |
| 3585 | InsertPt = F.getEntryBlock().getTerminator(); |
| 3586 | } else if (isa<TerminatorInst>(V)) { |
| 3587 | auto *II = cast<InvokeInst>(V); |
| 3588 | // We cannot demote invoke instructions to the stack if their normal |
| 3589 | // edge is critical. Therefore, split the critical edge and create a |
| 3590 | // basic block into which the store can be inserted. |
| 3591 | if (!II->getNormalDest()->getSinglePredecessor()) { |
| 3592 | unsigned SuccNum = |
| 3593 | GetSuccessorNumber(II->getParent(), II->getNormalDest()); |
| 3594 | assert(isCriticalEdge(II, SuccNum) && "Expected a critical edge!"); |
| 3595 | BasicBlock *NewBlock = SplitCriticalEdge(II, SuccNum); |
| 3596 | assert(NewBlock && "Unable to split critical edge."); |
| 3597 | // Update the color mapping for the newly split edge. |
| 3598 | std::set<BasicBlock *> &ColorsForUsingBB = BlockColors[II->getParent()]; |
| 3599 | BlockColors[NewBlock] = ColorsForUsingBB; |
| 3600 | for (BasicBlock *FuncletPad : ColorsForUsingBB) |
| 3601 | FuncletBlocks[FuncletPad].insert(NewBlock); |
| 3602 | } |
| 3603 | InsertPt = II->getNormalDest()->getFirstInsertionPt(); |
| 3604 | } else { |
| 3605 | InsertPt = cast<Instruction>(V); |
| 3606 | ++InsertPt; |
| 3607 | // Don't insert before PHI nodes or EH pad instrs. |
| 3608 | for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt) |
| 3609 | ; |
| 3610 | } |
| 3611 | new StoreInst(V, SpillSlot, InsertPt); |
| 3612 | } |
| 3613 | } |
| 3614 | |
| 3615 | void WinEHPrepare::replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot, |
| 3616 | DenseMap<BasicBlock *, Value *> &Loads, |
| 3617 | Function &F) { |
| 3618 | // Lazilly create the spill slot. |
| 3619 | if (!SpillSlot) |
| 3620 | SpillSlot = new AllocaInst(V->getType(), nullptr, |
| 3621 | Twine(V->getName(), ".wineh.spillslot"), |
| 3622 | F.getEntryBlock().begin()); |
| 3623 | |
| 3624 | auto *UsingInst = cast<Instruction>(U.getUser()); |
| 3625 | if (auto *UsingPHI = dyn_cast<PHINode>(UsingInst)) { |
| 3626 | // If this is a PHI node, we can't insert a load of the value before |
| 3627 | // the use. Instead insert the load in the predecessor block |
| 3628 | // corresponding to the incoming value. |
| 3629 | // |
| 3630 | // Note that if there are multiple edges from a basic block to this |
| 3631 | // PHI node that we cannot have multiple loads. The problem is that |
| 3632 | // the resulting PHI node will have multiple values (from each load) |
| 3633 | // coming in from the same block, which is illegal SSA form. |
| 3634 | // For this reason, we keep track of and reuse loads we insert. |
| 3635 | BasicBlock *IncomingBlock = UsingPHI->getIncomingBlock(U); |
Joseph Tremoulet | 7031c9f | 2015-08-17 13:51:37 +0000 | [diff] [blame] | 3636 | if (auto *CatchRet = |
| 3637 | dyn_cast<CatchReturnInst>(IncomingBlock->getTerminator())) { |
| 3638 | // Putting a load above a catchret and use on the phi would still leave |
| 3639 | // a cross-funclet def/use. We need to split the edge, change the |
| 3640 | // catchret to target the new block, and put the load there. |
| 3641 | BasicBlock *PHIBlock = UsingInst->getParent(); |
| 3642 | BasicBlock *NewBlock = SplitEdge(IncomingBlock, PHIBlock); |
| 3643 | // SplitEdge gives us: |
| 3644 | // IncomingBlock: |
| 3645 | // ... |
| 3646 | // br label %NewBlock |
| 3647 | // NewBlock: |
| 3648 | // catchret label %PHIBlock |
| 3649 | // But we need: |
| 3650 | // IncomingBlock: |
| 3651 | // ... |
| 3652 | // catchret label %NewBlock |
| 3653 | // NewBlock: |
| 3654 | // br label %PHIBlock |
| 3655 | // So move the terminators to each others' blocks and swap their |
| 3656 | // successors. |
| 3657 | BranchInst *Goto = cast<BranchInst>(IncomingBlock->getTerminator()); |
| 3658 | Goto->removeFromParent(); |
| 3659 | CatchRet->removeFromParent(); |
| 3660 | IncomingBlock->getInstList().push_back(CatchRet); |
| 3661 | NewBlock->getInstList().push_back(Goto); |
| 3662 | Goto->setSuccessor(0, PHIBlock); |
| 3663 | CatchRet->setSuccessor(NewBlock); |
| 3664 | // Update the color mapping for the newly split edge. |
| 3665 | std::set<BasicBlock *> &ColorsForPHIBlock = BlockColors[PHIBlock]; |
| 3666 | BlockColors[NewBlock] = ColorsForPHIBlock; |
| 3667 | for (BasicBlock *FuncletPad : ColorsForPHIBlock) |
| 3668 | FuncletBlocks[FuncletPad].insert(NewBlock); |
| 3669 | // Treat the new block as incoming for load insertion. |
| 3670 | IncomingBlock = NewBlock; |
| 3671 | } |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 3672 | Value *&Load = Loads[IncomingBlock]; |
| 3673 | // Insert the load into the predecessor block |
| 3674 | if (!Load) |
| 3675 | Load = new LoadInst(SpillSlot, Twine(V->getName(), ".wineh.reload"), |
| 3676 | /*Volatile=*/false, IncomingBlock->getTerminator()); |
| 3677 | |
| 3678 | U.set(Load); |
| 3679 | } else { |
| 3680 | // Reload right before the old use. |
| 3681 | auto *Load = new LoadInst(SpillSlot, Twine(V->getName(), ".wineh.reload"), |
| 3682 | /*Volatile=*/false, UsingInst); |
| 3683 | U.set(Load); |
| 3684 | } |
| 3685 | } |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 3686 | |
| 3687 | void WinEHFuncInfo::addIPToStateRange(const BasicBlock *PadBB, |
| 3688 | MCSymbol *InvokeBegin, |
| 3689 | MCSymbol *InvokeEnd) { |
| 3690 | assert(PadBB->isEHPad() && EHPadStateMap.count(PadBB->getFirstNonPHI()) && |
| 3691 | "should get EH pad BB with precomputed state"); |
| 3692 | InvokeToStateMap[InvokeBegin] = |
| 3693 | std::make_pair(EHPadStateMap[PadBB->getFirstNonPHI()], InvokeEnd); |
| 3694 | } |