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" |
| 26 | #include "llvm/Analysis/LibCallSemantics.h" |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/WinEHFuncInfo.h" |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Dominators.h" |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Function.h" |
| 30 | #include "llvm/IR/IRBuilder.h" |
| 31 | #include "llvm/IR/Instructions.h" |
| 32 | #include "llvm/IR/IntrinsicInst.h" |
| 33 | #include "llvm/IR/Module.h" |
| 34 | #include "llvm/IR/PatternMatch.h" |
| 35 | #include "llvm/Pass.h" |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | a8d61b1 | 2015-03-23 18:57:17 +0000 | [diff] [blame] | 37 | #include "llvm/Support/raw_ostream.h" |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 38 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 39 | #include "llvm/Transforms/Utils/Cloning.h" |
| 40 | #include "llvm/Transforms/Utils/Local.h" |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 41 | #include "llvm/Transforms/Utils/PromoteMemToReg.h" |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 42 | #include <memory> |
| 43 | |
| 44 | using namespace llvm; |
| 45 | using namespace llvm::PatternMatch; |
| 46 | |
| 47 | #define DEBUG_TYPE "winehprepare" |
| 48 | |
| 49 | namespace { |
| 50 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 51 | // This map is used to model frame variable usage during outlining, to |
| 52 | // construct a structure type to hold the frame variables in a frame |
| 53 | // allocation block, and to remap the frame variable allocas (including |
| 54 | // spill locations as needed) to GEPs that get the variable from the |
| 55 | // frame allocation structure. |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 56 | typedef MapVector<Value *, TinyPtrVector<AllocaInst *>> FrameVarInfoMap; |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 57 | |
Reid Kleckner | 3567d27 | 2015-04-02 21:13:31 +0000 | [diff] [blame] | 58 | // TinyPtrVector cannot hold nullptr, so we need our own sentinel that isn't |
| 59 | // quite null. |
| 60 | AllocaInst *getCatchObjectSentinel() { |
| 61 | return static_cast<AllocaInst *>(nullptr) + 1; |
| 62 | } |
| 63 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 64 | typedef SmallSet<BasicBlock *, 4> VisitedBlockSet; |
| 65 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 66 | class LandingPadActions; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 67 | class LandingPadMap; |
| 68 | |
| 69 | typedef DenseMap<const BasicBlock *, CatchHandler *> CatchHandlerMapTy; |
| 70 | typedef DenseMap<const BasicBlock *, CleanupHandler *> CleanupHandlerMapTy; |
| 71 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 72 | class WinEHPrepare : public FunctionPass { |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 73 | public: |
| 74 | static char ID; // Pass identification, replacement for typeid. |
| 75 | WinEHPrepare(const TargetMachine *TM = nullptr) |
Reid Kleckner | 582786b | 2015-04-30 18:17:12 +0000 | [diff] [blame] | 76 | : FunctionPass(ID) { |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 77 | if (TM) |
| 78 | TheTriple = Triple(TM->getTargetTriple()); |
| 79 | } |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 80 | |
| 81 | bool runOnFunction(Function &Fn) override; |
| 82 | |
| 83 | bool doFinalization(Module &M) override; |
| 84 | |
| 85 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 86 | |
| 87 | const char *getPassName() const override { |
| 88 | return "Windows exception handling preparation"; |
| 89 | } |
| 90 | |
| 91 | private: |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 92 | bool prepareExceptionHandlers(Function &F, |
| 93 | SmallVectorImpl<LandingPadInst *> &LPads); |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 94 | void identifyEHBlocks(Function &F, SmallVectorImpl<LandingPadInst *> &LPads); |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 95 | void promoteLandingPadValues(LandingPadInst *LPad); |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 96 | void demoteValuesLiveAcrossHandlers(Function &F, |
| 97 | SmallVectorImpl<LandingPadInst *> &LPads); |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 98 | void findSEHEHReturnPoints(Function &F, |
| 99 | SetVector<BasicBlock *> &EHReturnBlocks); |
| 100 | void findCXXEHReturnPoints(Function &F, |
| 101 | SetVector<BasicBlock *> &EHReturnBlocks); |
Andrew Kaylor | cc14f38 | 2015-05-11 23:06:02 +0000 | [diff] [blame] | 102 | void getPossibleReturnTargets(Function *ParentF, Function *HandlerF, |
| 103 | SetVector<BasicBlock*> &Targets); |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 104 | void completeNestedLandingPad(Function *ParentFn, |
| 105 | LandingPadInst *OutlinedLPad, |
| 106 | const LandingPadInst *OriginalLPad, |
| 107 | FrameVarInfoMap &VarInfo); |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 108 | Function *createHandlerFunc(Type *RetTy, const Twine &Name, Module *M, |
| 109 | Value *&ParentFP); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 110 | bool outlineHandler(ActionHandler *Action, Function *SrcFn, |
| 111 | LandingPadInst *LPad, BasicBlock *StartBB, |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 112 | FrameVarInfoMap &VarInfo); |
Andrew Kaylor | bb11132 | 2015-04-07 21:30:23 +0000 | [diff] [blame] | 113 | void addStubInvokeToHandlerIfNeeded(Function *Handler, Value *PersonalityFn); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 114 | |
| 115 | void mapLandingPadBlocks(LandingPadInst *LPad, LandingPadActions &Actions); |
| 116 | CatchHandler *findCatchHandler(BasicBlock *BB, BasicBlock *&NextBB, |
| 117 | VisitedBlockSet &VisitedBlocks); |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 118 | void findCleanupHandlers(LandingPadActions &Actions, BasicBlock *StartBB, |
| 119 | BasicBlock *EndBB); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 120 | |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 121 | void processSEHCatchHandler(CatchHandler *Handler, BasicBlock *StartBB); |
| 122 | |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 123 | Triple TheTriple; |
| 124 | |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 125 | // All fields are reset by runOnFunction. |
Reid Kleckner | 582786b | 2015-04-30 18:17:12 +0000 | [diff] [blame] | 126 | DominatorTree *DT = nullptr; |
| 127 | EHPersonality Personality = EHPersonality::Unknown; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 128 | CatchHandlerMapTy CatchHandlerMap; |
| 129 | CleanupHandlerMapTy CleanupHandlerMap; |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 130 | DenseMap<const LandingPadInst *, LandingPadMap> LPadMaps; |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 131 | SmallPtrSet<BasicBlock *, 4> NormalBlocks; |
| 132 | SmallPtrSet<BasicBlock *, 4> EHBlocks; |
| 133 | SetVector<BasicBlock *> EHReturnBlocks; |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 134 | |
| 135 | // This maps landing pad instructions found in outlined handlers to |
| 136 | // the landing pad instruction in the parent function from which they |
| 137 | // were cloned. The cloned/nested landing pad is used as the key |
| 138 | // because the landing pad may be cloned into multiple handlers. |
| 139 | // This map will be used to add the llvm.eh.actions call to the nested |
| 140 | // landing pads after all handlers have been outlined. |
| 141 | DenseMap<LandingPadInst *, const LandingPadInst *> NestedLPtoOriginalLP; |
| 142 | |
| 143 | // This maps blocks in the parent function which are destinations of |
| 144 | // catch handlers to cloned blocks in (other) outlined handlers. This |
| 145 | // handles the case where a nested landing pads has a catch handler that |
| 146 | // returns to a handler function rather than the parent function. |
| 147 | // The original block is used as the key here because there should only |
| 148 | // ever be one handler function from which the cloned block is not pruned. |
| 149 | // The original block will be pruned from the parent function after all |
| 150 | // handlers have been outlined. This map will be used to adjust the |
| 151 | // return instructions of handlers which return to the block that was |
| 152 | // outlined into a handler. This is done after all handlers have been |
| 153 | // outlined but before the outlined code is pruned from the parent function. |
| 154 | DenseMap<const BasicBlock *, BasicBlock *> LPadTargetBlocks; |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 155 | |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 156 | // Map from outlined handler to call to llvm.frameaddress(1). Only used for |
| 157 | // 32-bit EH. |
| 158 | DenseMap<Function *, Value *> HandlerToParentFP; |
| 159 | |
Reid Kleckner | 582786b | 2015-04-30 18:17:12 +0000 | [diff] [blame] | 160 | AllocaInst *SEHExceptionCodeSlot = nullptr; |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | class WinEHFrameVariableMaterializer : public ValueMaterializer { |
| 164 | public: |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 165 | WinEHFrameVariableMaterializer(Function *OutlinedFn, Value *ParentFP, |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 166 | FrameVarInfoMap &FrameVarInfo); |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 167 | ~WinEHFrameVariableMaterializer() override {} |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 168 | |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 169 | Value *materializeValueFor(Value *V) override; |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 170 | |
Reid Kleckner | 3567d27 | 2015-04-02 21:13:31 +0000 | [diff] [blame] | 171 | void escapeCatchObject(Value *V); |
| 172 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 173 | private: |
| 174 | FrameVarInfoMap &FrameVarInfo; |
| 175 | IRBuilder<> Builder; |
| 176 | }; |
| 177 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 178 | class LandingPadMap { |
| 179 | public: |
| 180 | LandingPadMap() : OriginLPad(nullptr) {} |
| 181 | void mapLandingPad(const LandingPadInst *LPad); |
| 182 | |
| 183 | bool isInitialized() { return OriginLPad != nullptr; } |
| 184 | |
Andrew Kaylor | f7118ae | 2015-03-27 22:31:12 +0000 | [diff] [blame] | 185 | bool isOriginLandingPadBlock(const BasicBlock *BB) const; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 186 | bool isLandingPadSpecificInst(const Instruction *Inst) const; |
| 187 | |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 188 | void remapEHValues(ValueToValueMapTy &VMap, Value *EHPtrValue, |
| 189 | Value *SelectorValue) const; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 190 | |
| 191 | private: |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 192 | const LandingPadInst *OriginLPad; |
| 193 | // We will normally only see one of each of these instructions, but |
| 194 | // if more than one occurs for some reason we can handle that. |
| 195 | TinyPtrVector<const ExtractValueInst *> ExtractedEHPtrs; |
| 196 | TinyPtrVector<const ExtractValueInst *> ExtractedSelectors; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 199 | class WinEHCloningDirectorBase : public CloningDirector { |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 200 | public: |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 201 | WinEHCloningDirectorBase(Function *HandlerFn, Value *ParentFP, |
| 202 | FrameVarInfoMap &VarInfo, LandingPadMap &LPadMap) |
| 203 | : Materializer(HandlerFn, ParentFP, VarInfo), |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 204 | SelectorIDType(Type::getInt32Ty(HandlerFn->getContext())), |
| 205 | Int8PtrType(Type::getInt8PtrTy(HandlerFn->getContext())), |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 206 | LPadMap(LPadMap), ParentFP(ParentFP) {} |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 207 | |
| 208 | CloningAction handleInstruction(ValueToValueMapTy &VMap, |
| 209 | const Instruction *Inst, |
| 210 | BasicBlock *NewBB) override; |
| 211 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 212 | virtual CloningAction handleBeginCatch(ValueToValueMapTy &VMap, |
| 213 | const Instruction *Inst, |
| 214 | BasicBlock *NewBB) = 0; |
| 215 | virtual CloningAction handleEndCatch(ValueToValueMapTy &VMap, |
| 216 | const Instruction *Inst, |
| 217 | BasicBlock *NewBB) = 0; |
| 218 | virtual CloningAction handleTypeIdFor(ValueToValueMapTy &VMap, |
| 219 | const Instruction *Inst, |
| 220 | BasicBlock *NewBB) = 0; |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 221 | virtual CloningAction handleIndirectBr(ValueToValueMapTy &VMap, |
| 222 | const IndirectBrInst *IBr, |
| 223 | BasicBlock *NewBB) = 0; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 224 | virtual CloningAction handleInvoke(ValueToValueMapTy &VMap, |
| 225 | const InvokeInst *Invoke, |
| 226 | BasicBlock *NewBB) = 0; |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 227 | virtual CloningAction handleResume(ValueToValueMapTy &VMap, |
| 228 | const ResumeInst *Resume, |
| 229 | BasicBlock *NewBB) = 0; |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 230 | virtual CloningAction handleCompare(ValueToValueMapTy &VMap, |
| 231 | const CmpInst *Compare, |
| 232 | BasicBlock *NewBB) = 0; |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 233 | virtual CloningAction handleLandingPad(ValueToValueMapTy &VMap, |
| 234 | const LandingPadInst *LPad, |
| 235 | BasicBlock *NewBB) = 0; |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 236 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 237 | ValueMaterializer *getValueMaterializer() override { return &Materializer; } |
| 238 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 239 | protected: |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 240 | WinEHFrameVariableMaterializer Materializer; |
| 241 | Type *SelectorIDType; |
| 242 | Type *Int8PtrType; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 243 | LandingPadMap &LPadMap; |
Reid Kleckner | f14787d | 2015-04-22 00:07:52 +0000 | [diff] [blame] | 244 | |
| 245 | /// The value representing the parent frame pointer. |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 246 | Value *ParentFP; |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 247 | }; |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 248 | |
| 249 | class WinEHCatchDirector : public WinEHCloningDirectorBase { |
| 250 | public: |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 251 | WinEHCatchDirector( |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 252 | Function *CatchFn, Value *ParentFP, Value *Selector, |
| 253 | FrameVarInfoMap &VarInfo, LandingPadMap &LPadMap, |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 254 | DenseMap<LandingPadInst *, const LandingPadInst *> &NestedLPads, |
| 255 | DominatorTree *DT, SmallPtrSetImpl<BasicBlock *> &EHBlocks) |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 256 | : WinEHCloningDirectorBase(CatchFn, ParentFP, VarInfo, LPadMap), |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 257 | CurrentSelector(Selector->stripPointerCasts()), |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 258 | ExceptionObjectVar(nullptr), NestedLPtoOriginalLP(NestedLPads), |
| 259 | DT(DT), EHBlocks(EHBlocks) {} |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 260 | |
| 261 | CloningAction handleBeginCatch(ValueToValueMapTy &VMap, |
| 262 | const Instruction *Inst, |
| 263 | BasicBlock *NewBB) override; |
| 264 | CloningAction handleEndCatch(ValueToValueMapTy &VMap, const Instruction *Inst, |
| 265 | BasicBlock *NewBB) override; |
| 266 | CloningAction handleTypeIdFor(ValueToValueMapTy &VMap, |
| 267 | const Instruction *Inst, |
| 268 | BasicBlock *NewBB) override; |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 269 | CloningAction handleIndirectBr(ValueToValueMapTy &VMap, |
| 270 | const IndirectBrInst *IBr, |
| 271 | BasicBlock *NewBB) override; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 272 | CloningAction handleInvoke(ValueToValueMapTy &VMap, const InvokeInst *Invoke, |
| 273 | BasicBlock *NewBB) override; |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 274 | CloningAction handleResume(ValueToValueMapTy &VMap, const ResumeInst *Resume, |
| 275 | BasicBlock *NewBB) override; |
Andrew Kaylor | 9130743 | 2015-04-28 22:01:51 +0000 | [diff] [blame] | 276 | CloningAction handleCompare(ValueToValueMapTy &VMap, const CmpInst *Compare, |
| 277 | BasicBlock *NewBB) override; |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 278 | CloningAction handleLandingPad(ValueToValueMapTy &VMap, |
| 279 | const LandingPadInst *LPad, |
| 280 | BasicBlock *NewBB) override; |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 281 | |
Reid Kleckner | 3567d27 | 2015-04-02 21:13:31 +0000 | [diff] [blame] | 282 | Value *getExceptionVar() { return ExceptionObjectVar; } |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 283 | TinyPtrVector<BasicBlock *> &getReturnTargets() { return ReturnTargets; } |
| 284 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 285 | private: |
| 286 | Value *CurrentSelector; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 287 | |
Reid Kleckner | 3567d27 | 2015-04-02 21:13:31 +0000 | [diff] [blame] | 288 | Value *ExceptionObjectVar; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 289 | TinyPtrVector<BasicBlock *> ReturnTargets; |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 290 | |
| 291 | // This will be a reference to the field of the same name in the WinEHPrepare |
| 292 | // object which instantiates this WinEHCatchDirector object. |
| 293 | DenseMap<LandingPadInst *, const LandingPadInst *> &NestedLPtoOriginalLP; |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 294 | DominatorTree *DT; |
| 295 | SmallPtrSetImpl<BasicBlock *> &EHBlocks; |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 296 | }; |
| 297 | |
| 298 | class WinEHCleanupDirector : public WinEHCloningDirectorBase { |
| 299 | public: |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 300 | WinEHCleanupDirector(Function *CleanupFn, Value *ParentFP, |
| 301 | FrameVarInfoMap &VarInfo, LandingPadMap &LPadMap) |
| 302 | : WinEHCloningDirectorBase(CleanupFn, ParentFP, VarInfo, |
| 303 | LPadMap) {} |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 304 | |
| 305 | CloningAction handleBeginCatch(ValueToValueMapTy &VMap, |
| 306 | const Instruction *Inst, |
| 307 | BasicBlock *NewBB) override; |
| 308 | CloningAction handleEndCatch(ValueToValueMapTy &VMap, const Instruction *Inst, |
| 309 | BasicBlock *NewBB) override; |
| 310 | CloningAction handleTypeIdFor(ValueToValueMapTy &VMap, |
| 311 | const Instruction *Inst, |
| 312 | BasicBlock *NewBB) override; |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 313 | CloningAction handleIndirectBr(ValueToValueMapTy &VMap, |
| 314 | const IndirectBrInst *IBr, |
| 315 | BasicBlock *NewBB) override; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 316 | CloningAction handleInvoke(ValueToValueMapTy &VMap, const InvokeInst *Invoke, |
| 317 | BasicBlock *NewBB) override; |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 318 | CloningAction handleResume(ValueToValueMapTy &VMap, const ResumeInst *Resume, |
| 319 | BasicBlock *NewBB) override; |
Andrew Kaylor | 9130743 | 2015-04-28 22:01:51 +0000 | [diff] [blame] | 320 | CloningAction handleCompare(ValueToValueMapTy &VMap, const CmpInst *Compare, |
| 321 | BasicBlock *NewBB) override; |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 322 | CloningAction handleLandingPad(ValueToValueMapTy &VMap, |
| 323 | const LandingPadInst *LPad, |
| 324 | BasicBlock *NewBB) override; |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 325 | }; |
| 326 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 327 | class LandingPadActions { |
| 328 | public: |
| 329 | LandingPadActions() : HasCleanupHandlers(false) {} |
| 330 | |
| 331 | void insertCatchHandler(CatchHandler *Action) { Actions.push_back(Action); } |
| 332 | void insertCleanupHandler(CleanupHandler *Action) { |
| 333 | Actions.push_back(Action); |
| 334 | HasCleanupHandlers = true; |
| 335 | } |
| 336 | |
| 337 | bool includesCleanup() const { return HasCleanupHandlers; } |
| 338 | |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 339 | SmallVectorImpl<ActionHandler *> &actions() { return Actions; } |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 340 | SmallVectorImpl<ActionHandler *>::iterator begin() { return Actions.begin(); } |
| 341 | SmallVectorImpl<ActionHandler *>::iterator end() { return Actions.end(); } |
| 342 | |
| 343 | private: |
| 344 | // Note that this class does not own the ActionHandler objects in this vector. |
| 345 | // The ActionHandlers are owned by the CatchHandlerMap and CleanupHandlerMap |
| 346 | // in the WinEHPrepare class. |
| 347 | SmallVector<ActionHandler *, 4> Actions; |
| 348 | bool HasCleanupHandlers; |
| 349 | }; |
| 350 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 351 | } // end anonymous namespace |
| 352 | |
| 353 | char WinEHPrepare::ID = 0; |
Reid Kleckner | 47c8e7a | 2015-03-12 00:36:20 +0000 | [diff] [blame] | 354 | INITIALIZE_TM_PASS(WinEHPrepare, "winehprepare", "Prepare Windows exceptions", |
| 355 | false, false) |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 356 | |
| 357 | FunctionPass *llvm::createWinEHPass(const TargetMachine *TM) { |
| 358 | return new WinEHPrepare(TM); |
| 359 | } |
| 360 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 361 | bool WinEHPrepare::runOnFunction(Function &Fn) { |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 362 | // No need to prepare outlined handlers. |
| 363 | if (Fn.hasFnAttribute("wineh-parent")) |
| 364 | return false; |
| 365 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 366 | SmallVector<LandingPadInst *, 4> LPads; |
| 367 | SmallVector<ResumeInst *, 4> Resumes; |
| 368 | for (BasicBlock &BB : Fn) { |
| 369 | if (auto *LP = BB.getLandingPadInst()) |
| 370 | LPads.push_back(LP); |
| 371 | if (auto *Resume = dyn_cast<ResumeInst>(BB.getTerminator())) |
| 372 | Resumes.push_back(Resume); |
| 373 | } |
| 374 | |
| 375 | // No need to prepare functions that lack landing pads. |
| 376 | if (LPads.empty()) |
| 377 | return false; |
| 378 | |
| 379 | // Classify the personality to see what kind of preparation we need. |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 380 | Personality = classifyEHPersonality(LPads.back()->getPersonalityFn()); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 381 | |
Reid Kleckner | 47c8e7a | 2015-03-12 00:36:20 +0000 | [diff] [blame] | 382 | // Do nothing if this is not an MSVC personality. |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 383 | if (!isMSVCEHPersonality(Personality)) |
Reid Kleckner | 47c8e7a | 2015-03-12 00:36:20 +0000 | [diff] [blame] | 384 | return false; |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 385 | |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 386 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 387 | |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 388 | // If there were any landing pads, prepareExceptionHandlers will make changes. |
| 389 | prepareExceptionHandlers(Fn, LPads); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 390 | return true; |
| 391 | } |
| 392 | |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 393 | bool WinEHPrepare::doFinalization(Module &M) { return false; } |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 394 | |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 395 | void WinEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const { |
| 396 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 397 | } |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 398 | |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 399 | static bool isSelectorDispatch(BasicBlock *BB, BasicBlock *&CatchHandler, |
| 400 | Constant *&Selector, BasicBlock *&NextBB); |
| 401 | |
| 402 | // Finds blocks reachable from the starting set Worklist. Does not follow unwind |
| 403 | // edges or blocks listed in StopPoints. |
| 404 | static void findReachableBlocks(SmallPtrSetImpl<BasicBlock *> &ReachableBBs, |
| 405 | SetVector<BasicBlock *> &Worklist, |
| 406 | const SetVector<BasicBlock *> *StopPoints) { |
| 407 | while (!Worklist.empty()) { |
| 408 | BasicBlock *BB = Worklist.pop_back_val(); |
| 409 | |
| 410 | // Don't cross blocks that we should stop at. |
| 411 | if (StopPoints && StopPoints->count(BB)) |
| 412 | continue; |
| 413 | |
| 414 | if (!ReachableBBs.insert(BB).second) |
| 415 | continue; // Already visited. |
| 416 | |
| 417 | // Don't follow unwind edges of invokes. |
| 418 | if (auto *II = dyn_cast<InvokeInst>(BB->getTerminator())) { |
| 419 | Worklist.insert(II->getNormalDest()); |
| 420 | continue; |
| 421 | } |
| 422 | |
| 423 | // Otherwise, follow all successors. |
| 424 | Worklist.insert(succ_begin(BB), succ_end(BB)); |
| 425 | } |
| 426 | } |
| 427 | |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 428 | // Attempt to find an instruction where a block can be split before |
| 429 | // a call to llvm.eh.begincatch and its operands. If the block |
| 430 | // begins with the begincatch call or one of its adjacent operands |
| 431 | // the block will not be split. |
| 432 | static Instruction *findBeginCatchSplitPoint(BasicBlock *BB, |
| 433 | IntrinsicInst *II) { |
| 434 | // If the begincatch call is already the first instruction in the block, |
| 435 | // don't split. |
| 436 | Instruction *FirstNonPHI = BB->getFirstNonPHI(); |
| 437 | if (II == FirstNonPHI) |
| 438 | return nullptr; |
| 439 | |
| 440 | // If either operand is in the same basic block as the instruction and |
| 441 | // isn't used by another instruction before the begincatch call, include it |
| 442 | // in the split block. |
| 443 | auto *Op0 = dyn_cast<Instruction>(II->getOperand(0)); |
| 444 | auto *Op1 = dyn_cast<Instruction>(II->getOperand(1)); |
| 445 | |
| 446 | Instruction *I = II->getPrevNode(); |
| 447 | Instruction *LastI = II; |
| 448 | |
| 449 | while (I == Op0 || I == Op1) { |
| 450 | // If the block begins with one of the operands and there are no other |
| 451 | // instructions between the operand and the begincatch call, don't split. |
| 452 | if (I == FirstNonPHI) |
| 453 | return nullptr; |
| 454 | |
| 455 | LastI = I; |
| 456 | I = I->getPrevNode(); |
| 457 | } |
| 458 | |
| 459 | // If there is at least one instruction in the block before the begincatch |
| 460 | // call and its operands, split the block at either the begincatch or |
| 461 | // its operand. |
| 462 | return LastI; |
| 463 | } |
| 464 | |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 465 | /// Find all points where exceptional control rejoins normal control flow via |
| 466 | /// llvm.eh.endcatch. Add them to the normal bb reachability worklist. |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 467 | void WinEHPrepare::findCXXEHReturnPoints( |
| 468 | Function &F, SetVector<BasicBlock *> &EHReturnBlocks) { |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 469 | for (auto BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI) { |
| 470 | BasicBlock *BB = BBI; |
| 471 | for (Instruction &I : *BB) { |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 472 | if (match(&I, m_Intrinsic<Intrinsic::eh_begincatch>())) { |
Andrew Kaylor | 9130743 | 2015-04-28 22:01:51 +0000 | [diff] [blame] | 473 | Instruction *SplitPt = |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 474 | findBeginCatchSplitPoint(BB, cast<IntrinsicInst>(&I)); |
| 475 | if (SplitPt) { |
| 476 | // Split the block before the llvm.eh.begincatch call to allow |
| 477 | // cleanup and catch code to be distinguished later. |
| 478 | // Do not update BBI because we still need to process the |
| 479 | // portion of the block that we are splitting off. |
Andrew Kaylor | a33f159 | 2015-04-29 17:21:26 +0000 | [diff] [blame] | 480 | SplitBlock(BB, SplitPt, DT); |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 481 | break; |
| 482 | } |
| 483 | } |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 484 | if (match(&I, m_Intrinsic<Intrinsic::eh_endcatch>())) { |
| 485 | // Split the block after the call to llvm.eh.endcatch if there is |
| 486 | // anything other than an unconditional branch, or if the successor |
| 487 | // starts with a phi. |
| 488 | auto *Br = dyn_cast<BranchInst>(I.getNextNode()); |
| 489 | if (!Br || !Br->isUnconditional() || |
| 490 | isa<PHINode>(Br->getSuccessor(0)->begin())) { |
| 491 | DEBUG(dbgs() << "splitting block " << BB->getName() |
| 492 | << " with llvm.eh.endcatch\n"); |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 493 | BBI = SplitBlock(BB, I.getNextNode(), DT); |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 494 | } |
| 495 | // The next BB is normal control flow. |
| 496 | EHReturnBlocks.insert(BB->getTerminator()->getSuccessor(0)); |
| 497 | break; |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | static bool isCatchAllLandingPad(const BasicBlock *BB) { |
| 504 | const LandingPadInst *LP = BB->getLandingPadInst(); |
| 505 | if (!LP) |
| 506 | return false; |
| 507 | unsigned N = LP->getNumClauses(); |
| 508 | return (N > 0 && LP->isCatch(N - 1) && |
| 509 | isa<ConstantPointerNull>(LP->getClause(N - 1))); |
| 510 | } |
| 511 | |
| 512 | /// Find all points where exceptions control rejoins normal control flow via |
| 513 | /// selector dispatch. |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 514 | void WinEHPrepare::findSEHEHReturnPoints( |
| 515 | Function &F, SetVector<BasicBlock *> &EHReturnBlocks) { |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 516 | for (auto BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI) { |
| 517 | BasicBlock *BB = BBI; |
| 518 | // If the landingpad is a catch-all, treat the whole lpad as if it is |
| 519 | // reachable from normal control flow. |
| 520 | // FIXME: This is imprecise. We need a better way of identifying where a |
| 521 | // catch-all starts and cleanups stop. As far as LLVM is concerned, there |
| 522 | // is no difference. |
| 523 | if (isCatchAllLandingPad(BB)) { |
| 524 | EHReturnBlocks.insert(BB); |
| 525 | continue; |
| 526 | } |
| 527 | |
| 528 | BasicBlock *CatchHandler; |
| 529 | BasicBlock *NextBB; |
| 530 | Constant *Selector; |
| 531 | if (isSelectorDispatch(BB, CatchHandler, Selector, NextBB)) { |
| 532 | // Split the edge if there is a phi node. Returning from EH to a phi node |
| 533 | // is just as impossible as having a phi after an indirectbr. |
| 534 | if (isa<PHINode>(CatchHandler->begin())) { |
| 535 | DEBUG(dbgs() << "splitting EH return edge from " << BB->getName() |
| 536 | << " to " << CatchHandler->getName() << '\n'); |
| 537 | BBI = CatchHandler = SplitCriticalEdge( |
| 538 | BB, std::find(succ_begin(BB), succ_end(BB), CatchHandler)); |
| 539 | } |
| 540 | EHReturnBlocks.insert(CatchHandler); |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 545 | void WinEHPrepare::identifyEHBlocks(Function &F, |
| 546 | SmallVectorImpl<LandingPadInst *> &LPads) { |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 547 | DEBUG(dbgs() << "Demoting values live across exception handlers in function " |
| 548 | << F.getName() << '\n'); |
| 549 | |
| 550 | // Build a set of all non-exceptional blocks and exceptional blocks. |
| 551 | // - Non-exceptional blocks are blocks reachable from the entry block while |
| 552 | // not following invoke unwind edges. |
| 553 | // - Exceptional blocks are blocks reachable from landingpads. Analysis does |
| 554 | // not follow llvm.eh.endcatch blocks, which mark a transition from |
| 555 | // exceptional to normal control. |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 556 | |
| 557 | if (Personality == EHPersonality::MSVC_CXX) |
| 558 | findCXXEHReturnPoints(F, EHReturnBlocks); |
| 559 | else |
| 560 | findSEHEHReturnPoints(F, EHReturnBlocks); |
| 561 | |
| 562 | DEBUG({ |
| 563 | dbgs() << "identified the following blocks as EH return points:\n"; |
| 564 | for (BasicBlock *BB : EHReturnBlocks) |
| 565 | dbgs() << " " << BB->getName() << '\n'; |
| 566 | }); |
| 567 | |
Andrew Kaylor | 9130743 | 2015-04-28 22:01:51 +0000 | [diff] [blame] | 568 | // Join points should not have phis at this point, unless they are a |
| 569 | // landingpad, in which case we will demote their phis later. |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 570 | #ifndef NDEBUG |
| 571 | for (BasicBlock *BB : EHReturnBlocks) |
| 572 | assert((BB->isLandingPad() || !isa<PHINode>(BB->begin())) && |
| 573 | "non-lpad EH return block has phi"); |
| 574 | #endif |
| 575 | |
| 576 | // Normal blocks are the blocks reachable from the entry block and all EH |
| 577 | // return points. |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 578 | SetVector<BasicBlock *> Worklist; |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 579 | Worklist = EHReturnBlocks; |
| 580 | Worklist.insert(&F.getEntryBlock()); |
| 581 | findReachableBlocks(NormalBlocks, Worklist, nullptr); |
| 582 | DEBUG({ |
| 583 | dbgs() << "marked the following blocks as normal:\n"; |
| 584 | for (BasicBlock *BB : NormalBlocks) |
| 585 | dbgs() << " " << BB->getName() << '\n'; |
| 586 | }); |
| 587 | |
| 588 | // Exceptional blocks are the blocks reachable from landingpads that don't |
| 589 | // cross EH return points. |
| 590 | Worklist.clear(); |
| 591 | for (auto *LPI : LPads) |
| 592 | Worklist.insert(LPI->getParent()); |
| 593 | findReachableBlocks(EHBlocks, Worklist, &EHReturnBlocks); |
| 594 | DEBUG({ |
| 595 | dbgs() << "marked the following blocks as exceptional:\n"; |
| 596 | for (BasicBlock *BB : EHBlocks) |
| 597 | dbgs() << " " << BB->getName() << '\n'; |
| 598 | }); |
| 599 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | /// Ensure that all values live into and out of exception handlers are stored |
| 603 | /// in memory. |
| 604 | /// FIXME: This falls down when values are defined in one handler and live into |
| 605 | /// another handler. For example, a cleanup defines a value used only by a |
| 606 | /// catch handler. |
| 607 | void WinEHPrepare::demoteValuesLiveAcrossHandlers( |
| 608 | Function &F, SmallVectorImpl<LandingPadInst *> &LPads) { |
| 609 | DEBUG(dbgs() << "Demoting values live across exception handlers in function " |
| 610 | << F.getName() << '\n'); |
| 611 | |
| 612 | // identifyEHBlocks() should have been called before this function. |
| 613 | assert(!NormalBlocks.empty()); |
| 614 | |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 615 | SetVector<Argument *> ArgsToDemote; |
| 616 | SetVector<Instruction *> InstrsToDemote; |
| 617 | for (BasicBlock &BB : F) { |
| 618 | bool IsNormalBB = NormalBlocks.count(&BB); |
| 619 | bool IsEHBB = EHBlocks.count(&BB); |
| 620 | if (!IsNormalBB && !IsEHBB) |
| 621 | continue; // Blocks that are neither normal nor EH are unreachable. |
| 622 | for (Instruction &I : BB) { |
| 623 | for (Value *Op : I.operands()) { |
| 624 | // Don't demote static allocas, constants, and labels. |
| 625 | if (isa<Constant>(Op) || isa<BasicBlock>(Op) || isa<InlineAsm>(Op)) |
| 626 | continue; |
| 627 | auto *AI = dyn_cast<AllocaInst>(Op); |
| 628 | if (AI && AI->isStaticAlloca()) |
| 629 | continue; |
| 630 | |
| 631 | if (auto *Arg = dyn_cast<Argument>(Op)) { |
| 632 | if (IsEHBB) { |
| 633 | DEBUG(dbgs() << "Demoting argument " << *Arg |
| 634 | << " used by EH instr: " << I << "\n"); |
| 635 | ArgsToDemote.insert(Arg); |
| 636 | } |
| 637 | continue; |
| 638 | } |
| 639 | |
| 640 | auto *OpI = cast<Instruction>(Op); |
| 641 | BasicBlock *OpBB = OpI->getParent(); |
| 642 | // If a value is produced and consumed in the same BB, we don't need to |
| 643 | // demote it. |
| 644 | if (OpBB == &BB) |
| 645 | continue; |
| 646 | bool IsOpNormalBB = NormalBlocks.count(OpBB); |
| 647 | bool IsOpEHBB = EHBlocks.count(OpBB); |
| 648 | if (IsNormalBB != IsOpNormalBB || IsEHBB != IsOpEHBB) { |
| 649 | DEBUG({ |
| 650 | dbgs() << "Demoting instruction live in-out from EH:\n"; |
| 651 | dbgs() << "Instr: " << *OpI << '\n'; |
| 652 | dbgs() << "User: " << I << '\n'; |
| 653 | }); |
| 654 | InstrsToDemote.insert(OpI); |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | // Demote values live into and out of handlers. |
| 661 | // FIXME: This demotion is inefficient. We should insert spills at the point |
| 662 | // of definition, insert one reload in each handler that uses the value, and |
| 663 | // insert reloads in the BB used to rejoin normal control flow. |
| 664 | Instruction *AllocaInsertPt = F.getEntryBlock().getFirstInsertionPt(); |
| 665 | for (Instruction *I : InstrsToDemote) |
| 666 | DemoteRegToStack(*I, false, AllocaInsertPt); |
| 667 | |
| 668 | // Demote arguments separately, and only for uses in EH blocks. |
| 669 | for (Argument *Arg : ArgsToDemote) { |
| 670 | auto *Slot = new AllocaInst(Arg->getType(), nullptr, |
| 671 | Arg->getName() + ".reg2mem", AllocaInsertPt); |
| 672 | SmallVector<User *, 4> Users(Arg->user_begin(), Arg->user_end()); |
| 673 | for (User *U : Users) { |
| 674 | auto *I = dyn_cast<Instruction>(U); |
| 675 | if (I && EHBlocks.count(I->getParent())) { |
| 676 | auto *Reload = new LoadInst(Slot, Arg->getName() + ".reload", false, I); |
| 677 | U->replaceUsesOfWith(Arg, Reload); |
| 678 | } |
| 679 | } |
| 680 | new StoreInst(Arg, Slot, AllocaInsertPt); |
| 681 | } |
| 682 | |
| 683 | // Demote landingpad phis, as the landingpad will be removed from the machine |
| 684 | // CFG. |
| 685 | for (LandingPadInst *LPI : LPads) { |
| 686 | BasicBlock *BB = LPI->getParent(); |
| 687 | while (auto *Phi = dyn_cast<PHINode>(BB->begin())) |
| 688 | DemotePHIToStack(Phi, AllocaInsertPt); |
| 689 | } |
| 690 | |
| 691 | DEBUG(dbgs() << "Demoted " << InstrsToDemote.size() << " instructions and " |
| 692 | << ArgsToDemote.size() << " arguments for WinEHPrepare\n\n"); |
| 693 | } |
| 694 | |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 695 | bool WinEHPrepare::prepareExceptionHandlers( |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 696 | Function &F, SmallVectorImpl<LandingPadInst *> &LPads) { |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 697 | // Don't run on functions that are already prepared. |
| 698 | for (LandingPadInst *LPad : LPads) { |
| 699 | BasicBlock *LPadBB = LPad->getParent(); |
| 700 | for (Instruction &Inst : *LPadBB) |
| 701 | if (match(&Inst, m_Intrinsic<Intrinsic::eh_actions>())) |
| 702 | return false; |
| 703 | } |
| 704 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 705 | identifyEHBlocks(F, LPads); |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 706 | demoteValuesLiveAcrossHandlers(F, LPads); |
| 707 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 708 | // These containers are used to re-map frame variables that are used in |
| 709 | // outlined catch and cleanup handlers. They will be populated as the |
| 710 | // handlers are outlined. |
| 711 | FrameVarInfoMap FrameVarInfo; |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 712 | |
| 713 | bool HandlersOutlined = false; |
| 714 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 715 | Module *M = F.getParent(); |
| 716 | LLVMContext &Context = M->getContext(); |
| 717 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 718 | // Create a new function to receive the handler contents. |
| 719 | PointerType *Int8PtrType = Type::getInt8PtrTy(Context); |
| 720 | Type *Int32Type = Type::getInt32Ty(Context); |
Reid Kleckner | 52b0779 | 2015-03-12 01:45:37 +0000 | [diff] [blame] | 721 | Function *ActionIntrin = Intrinsic::getDeclaration(M, Intrinsic::eh_actions); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 722 | |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 723 | if (isAsynchronousEHPersonality(Personality)) { |
| 724 | // FIXME: Switch the ehptr type to i32 and then switch this. |
| 725 | SEHExceptionCodeSlot = |
| 726 | new AllocaInst(Int8PtrType, nullptr, "seh_exception_code", |
| 727 | F.getEntryBlock().getFirstInsertionPt()); |
| 728 | } |
| 729 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 730 | // In order to handle the case where one outlined catch handler returns |
| 731 | // to a block within another outlined catch handler that would otherwise |
| 732 | // be unreachable, we need to outline the nested landing pad before we |
| 733 | // outline the landing pad which encloses it. |
Manuel Klimek | b00d42c | 2015-05-21 15:38:25 +0000 | [diff] [blame] | 734 | if (!isAsynchronousEHPersonality(Personality)) |
| 735 | std::sort(LPads.begin(), LPads.end(), |
| 736 | [this](LandingPadInst *const &L, LandingPadInst *const &R) { |
| 737 | return DT->properlyDominates(R->getParent(), L->getParent()); |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 738 | }); |
| 739 | |
Andrew Kaylor | cc14f38 | 2015-05-11 23:06:02 +0000 | [diff] [blame] | 740 | // This container stores the llvm.eh.recover and IndirectBr instructions |
| 741 | // that make up the body of each landing pad after it has been outlined. |
| 742 | // We need to defer the population of the target list for the indirectbr |
| 743 | // until all landing pads have been outlined so that we can handle the |
| 744 | // case of blocks in the target that are reached only from nested |
| 745 | // landing pads. |
| 746 | SmallVector<std::pair<CallInst*, IndirectBrInst *>, 4> LPadImpls; |
| 747 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 748 | for (LandingPadInst *LPad : LPads) { |
| 749 | // Look for evidence that this landingpad has already been processed. |
| 750 | bool LPadHasActionList = false; |
| 751 | BasicBlock *LPadBB = LPad->getParent(); |
Reid Kleckner | c759fe9 | 2015-03-19 22:31:02 +0000 | [diff] [blame] | 752 | for (Instruction &Inst : *LPadBB) { |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 753 | if (match(&Inst, m_Intrinsic<Intrinsic::eh_actions>())) { |
| 754 | LPadHasActionList = true; |
| 755 | break; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 756 | } |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | // If we've already outlined the handlers for this landingpad, |
| 760 | // there's nothing more to do here. |
| 761 | if (LPadHasActionList) |
| 762 | continue; |
| 763 | |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 764 | // If either of the values in the aggregate returned by the landing pad is |
| 765 | // extracted and stored to memory, promote the stored value to a register. |
| 766 | promoteLandingPadValues(LPad); |
| 767 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 768 | LandingPadActions Actions; |
| 769 | mapLandingPadBlocks(LPad, Actions); |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 770 | |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 771 | HandlersOutlined |= !Actions.actions().empty(); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 772 | for (ActionHandler *Action : Actions) { |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 773 | if (Action->hasBeenProcessed()) |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 774 | continue; |
| 775 | BasicBlock *StartBB = Action->getStartBlock(); |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 776 | |
| 777 | // SEH doesn't do any outlining for catches. Instead, pass the handler |
| 778 | // basic block addr to llvm.eh.actions and list the block as a return |
| 779 | // target. |
| 780 | if (isAsynchronousEHPersonality(Personality)) { |
| 781 | if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) { |
| 782 | processSEHCatchHandler(CatchAction, StartBB); |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 783 | continue; |
| 784 | } |
| 785 | } |
| 786 | |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 787 | outlineHandler(Action, &F, LPad, StartBB, FrameVarInfo); |
| 788 | } |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 789 | |
Reid Kleckner | 2c3ccaa | 2015-04-24 16:22:19 +0000 | [diff] [blame] | 790 | // Split the block after the landingpad instruction so that it is just a |
| 791 | // call to llvm.eh.actions followed by indirectbr. |
| 792 | assert(!isa<PHINode>(LPadBB->begin()) && "lpad phi not removed"); |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 793 | SplitBlock(LPadBB, LPad->getNextNode(), DT); |
Reid Kleckner | 2c3ccaa | 2015-04-24 16:22:19 +0000 | [diff] [blame] | 794 | // Erase the branch inserted by the split so we can insert indirectbr. |
| 795 | LPadBB->getTerminator()->eraseFromParent(); |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 796 | |
Reid Kleckner | e3af86e | 2015-04-23 21:22:30 +0000 | [diff] [blame] | 797 | // Replace all extracted values with undef and ultimately replace the |
| 798 | // landingpad with undef. |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 799 | SmallVector<Instruction *, 4> SEHCodeUses; |
| 800 | SmallVector<Instruction *, 4> EHUndefs; |
Reid Kleckner | e3af86e | 2015-04-23 21:22:30 +0000 | [diff] [blame] | 801 | for (User *U : LPad->users()) { |
| 802 | auto *E = dyn_cast<ExtractValueInst>(U); |
| 803 | if (!E) |
| 804 | continue; |
| 805 | assert(E->getNumIndices() == 1 && |
| 806 | "Unexpected operation: extracting both landing pad values"); |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 807 | unsigned Idx = *E->idx_begin(); |
| 808 | assert((Idx == 0 || Idx == 1) && "unexpected index"); |
| 809 | if (Idx == 0 && isAsynchronousEHPersonality(Personality)) |
| 810 | SEHCodeUses.push_back(E); |
| 811 | else |
| 812 | EHUndefs.push_back(E); |
Reid Kleckner | e3af86e | 2015-04-23 21:22:30 +0000 | [diff] [blame] | 813 | } |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 814 | for (Instruction *E : EHUndefs) { |
Reid Kleckner | e3af86e | 2015-04-23 21:22:30 +0000 | [diff] [blame] | 815 | E->replaceAllUsesWith(UndefValue::get(E->getType())); |
| 816 | E->eraseFromParent(); |
| 817 | } |
| 818 | LPad->replaceAllUsesWith(UndefValue::get(LPad->getType())); |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 819 | |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 820 | // Rewrite uses of the exception pointer to loads of an alloca. |
| 821 | for (Instruction *E : SEHCodeUses) { |
| 822 | SmallVector<Use *, 4> Uses; |
| 823 | for (Use &U : E->uses()) |
| 824 | Uses.push_back(&U); |
| 825 | for (Use *U : Uses) { |
| 826 | auto *I = cast<Instruction>(U->getUser()); |
| 827 | if (isa<ResumeInst>(I)) |
| 828 | continue; |
| 829 | LoadInst *LI; |
| 830 | if (auto *Phi = dyn_cast<PHINode>(I)) |
| 831 | LI = new LoadInst(SEHExceptionCodeSlot, "sehcode", false, |
| 832 | Phi->getIncomingBlock(*U)); |
| 833 | else |
| 834 | LI = new LoadInst(SEHExceptionCodeSlot, "sehcode", false, I); |
| 835 | U->set(LI); |
| 836 | } |
| 837 | E->replaceAllUsesWith(UndefValue::get(E->getType())); |
| 838 | E->eraseFromParent(); |
| 839 | } |
| 840 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 841 | // Add a call to describe the actions for this landing pad. |
| 842 | std::vector<Value *> ActionArgs; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 843 | for (ActionHandler *Action : Actions) { |
Reid Kleckner | c759fe9 | 2015-03-19 22:31:02 +0000 | [diff] [blame] | 844 | // Action codes from docs are: 0 cleanup, 1 catch. |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 845 | if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) { |
Reid Kleckner | c759fe9 | 2015-03-19 22:31:02 +0000 | [diff] [blame] | 846 | ActionArgs.push_back(ConstantInt::get(Int32Type, 1)); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 847 | ActionArgs.push_back(CatchAction->getSelector()); |
Reid Kleckner | 3567d27 | 2015-04-02 21:13:31 +0000 | [diff] [blame] | 848 | // Find the frame escape index of the exception object alloca in the |
| 849 | // parent. |
| 850 | int FrameEscapeIdx = -1; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 851 | Value *EHObj = const_cast<Value *>(CatchAction->getExceptionVar()); |
Reid Kleckner | 3567d27 | 2015-04-02 21:13:31 +0000 | [diff] [blame] | 852 | if (EHObj && !isa<ConstantPointerNull>(EHObj)) { |
| 853 | auto I = FrameVarInfo.find(EHObj); |
| 854 | assert(I != FrameVarInfo.end() && |
| 855 | "failed to map llvm.eh.begincatch var"); |
| 856 | FrameEscapeIdx = std::distance(FrameVarInfo.begin(), I); |
| 857 | } |
| 858 | ActionArgs.push_back(ConstantInt::get(Int32Type, FrameEscapeIdx)); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 859 | } else { |
Reid Kleckner | c759fe9 | 2015-03-19 22:31:02 +0000 | [diff] [blame] | 860 | ActionArgs.push_back(ConstantInt::get(Int32Type, 0)); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 861 | } |
Reid Kleckner | c759fe9 | 2015-03-19 22:31:02 +0000 | [diff] [blame] | 862 | ActionArgs.push_back(Action->getHandlerBlockOrFunc()); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 863 | } |
| 864 | CallInst *Recover = |
Reid Kleckner | 2c3ccaa | 2015-04-24 16:22:19 +0000 | [diff] [blame] | 865 | CallInst::Create(ActionIntrin, ActionArgs, "recover", LPadBB); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 866 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 867 | SetVector<BasicBlock *> ReturnTargets; |
| 868 | for (ActionHandler *Action : Actions) { |
| 869 | if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) { |
| 870 | const auto &CatchTargets = CatchAction->getReturnTargets(); |
| 871 | ReturnTargets.insert(CatchTargets.begin(), CatchTargets.end()); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 872 | } |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 873 | } |
| 874 | IndirectBrInst *Branch = |
| 875 | IndirectBrInst::Create(Recover, ReturnTargets.size(), LPadBB); |
| 876 | for (BasicBlock *Target : ReturnTargets) |
| 877 | Branch->addDestination(Target); |
Andrew Kaylor | cc14f38 | 2015-05-11 23:06:02 +0000 | [diff] [blame] | 878 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 879 | if (!isAsynchronousEHPersonality(Personality)) { |
| 880 | // C++ EH must repopulate the targets later to handle the case of |
| 881 | // targets that are reached indirectly through nested landing pads. |
Andrew Kaylor | cc14f38 | 2015-05-11 23:06:02 +0000 | [diff] [blame] | 882 | LPadImpls.push_back(std::make_pair(Recover, Branch)); |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 883 | } |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 884 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 885 | } // End for each landingpad |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 886 | |
| 887 | // If nothing got outlined, there is no more processing to be done. |
| 888 | if (!HandlersOutlined) |
| 889 | return false; |
| 890 | |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 891 | // Replace any nested landing pad stubs with the correct action handler. |
| 892 | // This must be done before we remove unreachable blocks because it |
| 893 | // cleans up references to outlined blocks that will be deleted. |
| 894 | for (auto &LPadPair : NestedLPtoOriginalLP) |
| 895 | completeNestedLandingPad(&F, LPadPair.first, LPadPair.second, FrameVarInfo); |
Andrew Kaylor | 67d3c03 | 2015-04-08 20:57:22 +0000 | [diff] [blame] | 896 | NestedLPtoOriginalLP.clear(); |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 897 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 898 | // Update the indirectbr instructions' target lists if necessary. |
Andrew Kaylor | cc14f38 | 2015-05-11 23:06:02 +0000 | [diff] [blame] | 899 | SetVector<BasicBlock*> CheckedTargets; |
Benjamin Kramer | a48e065 | 2015-05-16 15:40:03 +0000 | [diff] [blame] | 900 | SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList; |
Andrew Kaylor | cc14f38 | 2015-05-11 23:06:02 +0000 | [diff] [blame] | 901 | for (auto &LPadImplPair : LPadImpls) { |
| 902 | IntrinsicInst *Recover = cast<IntrinsicInst>(LPadImplPair.first); |
| 903 | IndirectBrInst *Branch = LPadImplPair.second; |
| 904 | |
| 905 | // Get a list of handlers called by |
Andrew Kaylor | cc14f38 | 2015-05-11 23:06:02 +0000 | [diff] [blame] | 906 | parseEHActions(Recover, ActionList); |
| 907 | |
| 908 | // Add an indirect branch listing possible successors of the catch handlers. |
| 909 | SetVector<BasicBlock *> ReturnTargets; |
Benjamin Kramer | a48e065 | 2015-05-16 15:40:03 +0000 | [diff] [blame] | 910 | for (const auto &Action : ActionList) { |
| 911 | if (auto *CA = dyn_cast<CatchHandler>(Action.get())) { |
Andrew Kaylor | cc14f38 | 2015-05-11 23:06:02 +0000 | [diff] [blame] | 912 | Function *Handler = cast<Function>(CA->getHandlerBlockOrFunc()); |
| 913 | getPossibleReturnTargets(&F, Handler, ReturnTargets); |
| 914 | } |
| 915 | } |
Andrew Kaylor | 0ddaf2b | 2015-05-12 00:13:51 +0000 | [diff] [blame] | 916 | ActionList.clear(); |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 917 | // Clear any targets we already knew about. |
| 918 | for (unsigned int I = 0, E = Branch->getNumDestinations(); I < E; ++I) { |
| 919 | BasicBlock *KnownTarget = Branch->getDestination(I); |
| 920 | if (ReturnTargets.count(KnownTarget)) |
| 921 | ReturnTargets.remove(KnownTarget); |
| 922 | } |
Andrew Kaylor | cc14f38 | 2015-05-11 23:06:02 +0000 | [diff] [blame] | 923 | for (BasicBlock *Target : ReturnTargets) { |
| 924 | Branch->addDestination(Target); |
| 925 | // The target may be a block that we excepted to get pruned. |
| 926 | // If it is, it may contain a call to llvm.eh.endcatch. |
| 927 | if (CheckedTargets.insert(Target)) { |
| 928 | // Earlier preparations guarantee that all calls to llvm.eh.endcatch |
| 929 | // will be followed by an unconditional branch. |
| 930 | auto *Br = dyn_cast<BranchInst>(Target->getTerminator()); |
| 931 | if (Br && Br->isUnconditional() && |
| 932 | Br != Target->getFirstNonPHIOrDbgOrLifetime()) { |
| 933 | Instruction *Prev = Br->getPrevNode(); |
| 934 | if (match(cast<Value>(Prev), m_Intrinsic<Intrinsic::eh_endcatch>())) |
| 935 | Prev->eraseFromParent(); |
| 936 | } |
| 937 | } |
| 938 | } |
| 939 | } |
| 940 | LPadImpls.clear(); |
| 941 | |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 942 | F.addFnAttr("wineh-parent", F.getName()); |
| 943 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 944 | // Delete any blocks that were only used by handlers that were outlined above. |
| 945 | removeUnreachableBlocks(F); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 946 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 947 | BasicBlock *Entry = &F.getEntryBlock(); |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 948 | IRBuilder<> Builder(F.getParent()->getContext()); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 949 | Builder.SetInsertPoint(Entry->getFirstInsertionPt()); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 950 | |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 951 | Function *FrameEscapeFn = |
| 952 | Intrinsic::getDeclaration(M, Intrinsic::frameescape); |
| 953 | Function *RecoverFrameFn = |
| 954 | Intrinsic::getDeclaration(M, Intrinsic::framerecover); |
Reid Kleckner | f14787d | 2015-04-22 00:07:52 +0000 | [diff] [blame] | 955 | SmallVector<Value *, 8> AllocasToEscape; |
| 956 | |
| 957 | // Scan the entry block for an existing call to llvm.frameescape. We need to |
| 958 | // keep escaping those objects. |
| 959 | for (Instruction &I : F.front()) { |
| 960 | auto *II = dyn_cast<IntrinsicInst>(&I); |
| 961 | if (II && II->getIntrinsicID() == Intrinsic::frameescape) { |
| 962 | auto Args = II->arg_operands(); |
| 963 | AllocasToEscape.append(Args.begin(), Args.end()); |
| 964 | II->eraseFromParent(); |
| 965 | break; |
| 966 | } |
| 967 | } |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 968 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 969 | // Finally, replace all of the temporary allocas for frame variables used in |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 970 | // the outlined handlers with calls to llvm.framerecover. |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 971 | for (auto &VarInfoEntry : FrameVarInfo) { |
Andrew Kaylor | 72029c6 | 2015-03-03 00:41:03 +0000 | [diff] [blame] | 972 | Value *ParentVal = VarInfoEntry.first; |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 973 | TinyPtrVector<AllocaInst *> &Allocas = VarInfoEntry.second; |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 974 | AllocaInst *ParentAlloca = cast<AllocaInst>(ParentVal); |
Andrew Kaylor | 72029c6 | 2015-03-03 00:41:03 +0000 | [diff] [blame] | 975 | |
Reid Kleckner | b401941 | 2015-04-06 18:50:38 +0000 | [diff] [blame] | 976 | // FIXME: We should try to sink unescaped allocas from the parent frame into |
| 977 | // the child frame. If the alloca is escaped, we have to use the lifetime |
| 978 | // 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] | 979 | |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 980 | // Add this alloca to the list of things to escape. |
| 981 | AllocasToEscape.push_back(ParentAlloca); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 982 | |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 983 | // Next replace all outlined allocas that are mapped to it. |
| 984 | for (AllocaInst *TempAlloca : Allocas) { |
Reid Kleckner | 3567d27 | 2015-04-02 21:13:31 +0000 | [diff] [blame] | 985 | if (TempAlloca == getCatchObjectSentinel()) |
| 986 | continue; // Skip catch parameter sentinels. |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 987 | Function *HandlerFn = TempAlloca->getParent()->getParent(); |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 988 | llvm::Value *FP = HandlerToParentFP[HandlerFn]; |
| 989 | assert(FP); |
| 990 | |
| 991 | // FIXME: Sink this framerecover into the blocks where it is used. |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 992 | Builder.SetInsertPoint(TempAlloca); |
| 993 | Builder.SetCurrentDebugLocation(TempAlloca->getDebugLoc()); |
| 994 | Value *RecoverArgs[] = { |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 995 | Builder.CreateBitCast(&F, Int8PtrType, ""), FP, |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 996 | llvm::ConstantInt::get(Int32Type, AllocasToEscape.size() - 1)}; |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 997 | Instruction *RecoveredAlloca = |
| 998 | Builder.CreateCall(RecoverFrameFn, RecoverArgs); |
| 999 | |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 1000 | // Add a pointer bitcast if the alloca wasn't an i8. |
| 1001 | if (RecoveredAlloca->getType() != TempAlloca->getType()) { |
| 1002 | RecoveredAlloca->setName(Twine(TempAlloca->getName()) + ".i8"); |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1003 | RecoveredAlloca = cast<Instruction>( |
| 1004 | Builder.CreateBitCast(RecoveredAlloca, TempAlloca->getType())); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1005 | } |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 1006 | TempAlloca->replaceAllUsesWith(RecoveredAlloca); |
| 1007 | TempAlloca->removeFromParent(); |
| 1008 | RecoveredAlloca->takeName(TempAlloca); |
| 1009 | delete TempAlloca; |
| 1010 | } |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1011 | } // End for each FrameVarInfo entry. |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1012 | |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 1013 | // Insert 'call void (...)* @llvm.frameescape(...)' at the end of the entry |
| 1014 | // block. |
| 1015 | Builder.SetInsertPoint(&F.getEntryBlock().back()); |
| 1016 | Builder.CreateCall(FrameEscapeFn, AllocasToEscape); |
| 1017 | |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 1018 | if (SEHExceptionCodeSlot) { |
| 1019 | if (SEHExceptionCodeSlot->hasNUses(0)) |
| 1020 | SEHExceptionCodeSlot->eraseFromParent(); |
Reid Kleckner | 0738a9c | 2015-05-05 17:44:16 +0000 | [diff] [blame] | 1021 | else if (isAllocaPromotable(SEHExceptionCodeSlot)) |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 1022 | PromoteMemToReg(SEHExceptionCodeSlot, *DT); |
| 1023 | } |
| 1024 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1025 | // Clean up the handler action maps we created for this function |
| 1026 | DeleteContainerSeconds(CatchHandlerMap); |
| 1027 | CatchHandlerMap.clear(); |
| 1028 | DeleteContainerSeconds(CleanupHandlerMap); |
| 1029 | CleanupHandlerMap.clear(); |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1030 | HandlerToParentFP.clear(); |
| 1031 | DT = nullptr; |
Ahmed Bougacha | ed363c5 | 2015-05-06 01:28:58 +0000 | [diff] [blame] | 1032 | SEHExceptionCodeSlot = nullptr; |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 1033 | EHBlocks.clear(); |
| 1034 | NormalBlocks.clear(); |
| 1035 | EHReturnBlocks.clear(); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1036 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1037 | return HandlersOutlined; |
| 1038 | } |
| 1039 | |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 1040 | void WinEHPrepare::promoteLandingPadValues(LandingPadInst *LPad) { |
| 1041 | // If the return values of the landing pad instruction are extracted and |
| 1042 | // stored to memory, we want to promote the store locations to reg values. |
| 1043 | SmallVector<AllocaInst *, 2> EHAllocas; |
| 1044 | |
| 1045 | // The landingpad instruction returns an aggregate value. Typically, its |
| 1046 | // value will be passed to a pair of extract value instructions and the |
| 1047 | // results of those extracts are often passed to store instructions. |
| 1048 | // In unoptimized code the stored value will often be loaded and then stored |
| 1049 | // again. |
| 1050 | for (auto *U : LPad->users()) { |
| 1051 | ExtractValueInst *Extract = dyn_cast<ExtractValueInst>(U); |
| 1052 | if (!Extract) |
| 1053 | continue; |
| 1054 | |
| 1055 | for (auto *EU : Extract->users()) { |
| 1056 | if (auto *Store = dyn_cast<StoreInst>(EU)) { |
| 1057 | auto *AV = cast<AllocaInst>(Store->getPointerOperand()); |
| 1058 | EHAllocas.push_back(AV); |
| 1059 | } |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | // We can't do this without a dominator tree. |
| 1064 | assert(DT); |
| 1065 | |
| 1066 | if (!EHAllocas.empty()) { |
| 1067 | PromoteMemToReg(EHAllocas, *DT); |
| 1068 | EHAllocas.clear(); |
| 1069 | } |
Reid Kleckner | 8676214 | 2015-04-16 00:02:04 +0000 | [diff] [blame] | 1070 | |
| 1071 | // After promotion, some extracts may be trivially dead. Remove them. |
| 1072 | SmallVector<Value *, 4> Users(LPad->user_begin(), LPad->user_end()); |
| 1073 | for (auto *U : Users) |
| 1074 | RecursivelyDeleteTriviallyDeadInstructions(U); |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Andrew Kaylor | cc14f38 | 2015-05-11 23:06:02 +0000 | [diff] [blame] | 1077 | void WinEHPrepare::getPossibleReturnTargets(Function *ParentF, |
| 1078 | Function *HandlerF, |
| 1079 | SetVector<BasicBlock*> &Targets) { |
| 1080 | for (BasicBlock &BB : *HandlerF) { |
| 1081 | // If the handler contains landing pads, check for any |
| 1082 | // handlers that may return directly to a block in the |
| 1083 | // parent function. |
| 1084 | if (auto *LPI = BB.getLandingPadInst()) { |
| 1085 | IntrinsicInst *Recover = cast<IntrinsicInst>(LPI->getNextNode()); |
Benjamin Kramer | a48e065 | 2015-05-16 15:40:03 +0000 | [diff] [blame] | 1086 | SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList; |
Andrew Kaylor | cc14f38 | 2015-05-11 23:06:02 +0000 | [diff] [blame] | 1087 | parseEHActions(Recover, ActionList); |
Benjamin Kramer | a48e065 | 2015-05-16 15:40:03 +0000 | [diff] [blame] | 1088 | for (const auto &Action : ActionList) { |
| 1089 | if (auto *CH = dyn_cast<CatchHandler>(Action.get())) { |
Andrew Kaylor | cc14f38 | 2015-05-11 23:06:02 +0000 | [diff] [blame] | 1090 | Function *NestedF = cast<Function>(CH->getHandlerBlockOrFunc()); |
| 1091 | getPossibleReturnTargets(ParentF, NestedF, Targets); |
| 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()); |
| 1097 | if (!Ret) |
| 1098 | continue; |
| 1099 | |
| 1100 | // Handler functions must always return a block address. |
| 1101 | BlockAddress *BA = cast<BlockAddress>(Ret->getReturnValue()); |
| 1102 | |
| 1103 | // If this is the handler for a nested landing pad, the |
| 1104 | // return address may have been remapped to a block in the |
| 1105 | // parent handler. We're not interested in those. |
| 1106 | if (BA->getFunction() != ParentF) |
| 1107 | continue; |
| 1108 | |
| 1109 | Targets.insert(BA->getBasicBlock()); |
| 1110 | } |
| 1111 | } |
| 1112 | |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1113 | void WinEHPrepare::completeNestedLandingPad(Function *ParentFn, |
| 1114 | LandingPadInst *OutlinedLPad, |
| 1115 | const LandingPadInst *OriginalLPad, |
| 1116 | FrameVarInfoMap &FrameVarInfo) { |
| 1117 | // Get the nested block and erase the unreachable instruction that was |
| 1118 | // temporarily inserted as its terminator. |
| 1119 | LLVMContext &Context = ParentFn->getContext(); |
| 1120 | BasicBlock *OutlinedBB = OutlinedLPad->getParent(); |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 1121 | // If the nested landing pad was outlined before the landing pad that enclosed |
| 1122 | // it, it will already be in outlined form. In that case, we just need to see |
| 1123 | // if the returns and the enclosing branch instruction need to be updated. |
| 1124 | IndirectBrInst *Branch = |
| 1125 | dyn_cast<IndirectBrInst>(OutlinedBB->getTerminator()); |
| 1126 | if (!Branch) { |
| 1127 | // If the landing pad wasn't in outlined form, it should be a stub with |
| 1128 | // an unreachable terminator. |
| 1129 | assert(isa<UnreachableInst>(OutlinedBB->getTerminator())); |
| 1130 | OutlinedBB->getTerminator()->eraseFromParent(); |
| 1131 | // That should leave OutlinedLPad as the last instruction in its block. |
| 1132 | assert(&OutlinedBB->back() == OutlinedLPad); |
| 1133 | } |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1134 | |
| 1135 | // The original landing pad will have already had its action intrinsic |
| 1136 | // built by the outlining loop. We need to clone that into the outlined |
| 1137 | // location. It may also be necessary to add references to the exception |
| 1138 | // variables to the outlined handler in which this landing pad is nested |
| 1139 | // and remap return instructions in the nested handlers that should return |
| 1140 | // to an address in the outlined handler. |
| 1141 | Function *OutlinedHandlerFn = OutlinedBB->getParent(); |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1142 | BasicBlock::const_iterator II = OriginalLPad; |
| 1143 | ++II; |
| 1144 | // The instruction after the landing pad should now be a call to eh.actions. |
| 1145 | const Instruction *Recover = II; |
| 1146 | assert(match(Recover, m_Intrinsic<Intrinsic::eh_actions>())); |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 1147 | const IntrinsicInst *EHActions = cast<IntrinsicInst>(Recover); |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1148 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 1149 | // Remap the return target in the nested handler. |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1150 | SmallVector<BlockAddress *, 4> ActionTargets; |
Benjamin Kramer | a48e065 | 2015-05-16 15:40:03 +0000 | [diff] [blame] | 1151 | SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList; |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1152 | parseEHActions(EHActions, ActionList); |
Benjamin Kramer | a48e065 | 2015-05-16 15:40:03 +0000 | [diff] [blame] | 1153 | for (const auto &Action : ActionList) { |
| 1154 | auto *Catch = dyn_cast<CatchHandler>(Action.get()); |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1155 | if (!Catch) |
| 1156 | continue; |
| 1157 | // The dyn_cast to function here selects C++ catch handlers and skips |
| 1158 | // SEH catch handlers. |
| 1159 | auto *Handler = dyn_cast<Function>(Catch->getHandlerBlockOrFunc()); |
| 1160 | if (!Handler) |
| 1161 | continue; |
| 1162 | // Visit all the return instructions, looking for places that return |
| 1163 | // to a location within OutlinedHandlerFn. |
| 1164 | for (BasicBlock &NestedHandlerBB : *Handler) { |
| 1165 | auto *Ret = dyn_cast<ReturnInst>(NestedHandlerBB.getTerminator()); |
| 1166 | if (!Ret) |
| 1167 | continue; |
| 1168 | |
| 1169 | // Handler functions must always return a block address. |
| 1170 | BlockAddress *BA = cast<BlockAddress>(Ret->getReturnValue()); |
| 1171 | // The original target will have been in the main parent function, |
| 1172 | // but if it is the address of a block that has been outlined, it |
| 1173 | // should be a block that was outlined into OutlinedHandlerFn. |
| 1174 | assert(BA->getFunction() == ParentFn); |
| 1175 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 1176 | // Ignore targets that aren't part of an outlined handler function. |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1177 | if (!LPadTargetBlocks.count(BA->getBasicBlock())) |
| 1178 | continue; |
| 1179 | |
| 1180 | // If the return value is the address ofF a block that we |
| 1181 | // previously outlined into the parent handler function, replace |
| 1182 | // the return instruction and add the mapped target to the list |
| 1183 | // of possible return addresses. |
| 1184 | BasicBlock *MappedBB = LPadTargetBlocks[BA->getBasicBlock()]; |
| 1185 | assert(MappedBB->getParent() == OutlinedHandlerFn); |
| 1186 | BlockAddress *NewBA = BlockAddress::get(OutlinedHandlerFn, MappedBB); |
| 1187 | Ret->eraseFromParent(); |
| 1188 | ReturnInst::Create(Context, NewBA, &NestedHandlerBB); |
| 1189 | ActionTargets.push_back(NewBA); |
| 1190 | } |
| 1191 | } |
Andrew Kaylor | 7a0cec3 | 2015-04-03 21:44:17 +0000 | [diff] [blame] | 1192 | ActionList.clear(); |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1193 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 1194 | if (Branch) { |
| 1195 | // If the landing pad was already in outlined form, just update its targets. |
| 1196 | for (unsigned int I = Branch->getNumDestinations(); I > 0; --I) |
| 1197 | Branch->removeDestination(I); |
| 1198 | // Add the previously collected action targets. |
| 1199 | for (auto *Target : ActionTargets) |
| 1200 | Branch->addDestination(Target->getBasicBlock()); |
| 1201 | } else { |
| 1202 | // If the landing pad was previously stubbed out, fill in its outlined form. |
| 1203 | IntrinsicInst *NewEHActions = cast<IntrinsicInst>(EHActions->clone()); |
| 1204 | OutlinedBB->getInstList().push_back(NewEHActions); |
| 1205 | |
| 1206 | // Insert an indirect branch into the outlined landing pad BB. |
| 1207 | IndirectBrInst *IBr = IndirectBrInst::Create(NewEHActions, 0, OutlinedBB); |
| 1208 | // Add the previously collected action targets. |
| 1209 | for (auto *Target : ActionTargets) |
| 1210 | IBr->addDestination(Target->getBasicBlock()); |
| 1211 | } |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1214 | // This function examines a block to determine whether the block ends with a |
| 1215 | // conditional branch to a catch handler based on a selector comparison. |
| 1216 | // This function is used both by the WinEHPrepare::findSelectorComparison() and |
| 1217 | // WinEHCleanupDirector::handleTypeIdFor(). |
| 1218 | static bool isSelectorDispatch(BasicBlock *BB, BasicBlock *&CatchHandler, |
| 1219 | Constant *&Selector, BasicBlock *&NextBB) { |
| 1220 | ICmpInst::Predicate Pred; |
| 1221 | BasicBlock *TBB, *FBB; |
| 1222 | Value *LHS, *RHS; |
| 1223 | |
| 1224 | if (!match(BB->getTerminator(), |
| 1225 | m_Br(m_ICmp(Pred, m_Value(LHS), m_Value(RHS)), TBB, FBB))) |
| 1226 | return false; |
| 1227 | |
| 1228 | if (!match(LHS, |
| 1229 | m_Intrinsic<Intrinsic::eh_typeid_for>(m_Constant(Selector))) && |
| 1230 | !match(RHS, m_Intrinsic<Intrinsic::eh_typeid_for>(m_Constant(Selector)))) |
| 1231 | return false; |
| 1232 | |
| 1233 | if (Pred == CmpInst::ICMP_EQ) { |
| 1234 | CatchHandler = TBB; |
| 1235 | NextBB = FBB; |
| 1236 | return true; |
| 1237 | } |
| 1238 | |
| 1239 | if (Pred == CmpInst::ICMP_NE) { |
| 1240 | CatchHandler = FBB; |
| 1241 | NextBB = TBB; |
| 1242 | return true; |
| 1243 | } |
| 1244 | |
| 1245 | return false; |
| 1246 | } |
| 1247 | |
Andrew Kaylor | 4175851 | 2015-04-20 22:04:09 +0000 | [diff] [blame] | 1248 | static bool isCatchBlock(BasicBlock *BB) { |
| 1249 | for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end(); |
| 1250 | II != IE; ++II) { |
| 1251 | if (match(cast<Value>(II), m_Intrinsic<Intrinsic::eh_begincatch>())) |
| 1252 | return true; |
| 1253 | } |
| 1254 | return false; |
| 1255 | } |
| 1256 | |
Andrew Kaylor | bb11132 | 2015-04-07 21:30:23 +0000 | [diff] [blame] | 1257 | static BasicBlock *createStubLandingPad(Function *Handler, |
| 1258 | Value *PersonalityFn) { |
| 1259 | // FIXME: Finish this! |
| 1260 | LLVMContext &Context = Handler->getContext(); |
| 1261 | BasicBlock *StubBB = BasicBlock::Create(Context, "stub"); |
| 1262 | Handler->getBasicBlockList().push_back(StubBB); |
| 1263 | IRBuilder<> Builder(StubBB); |
| 1264 | LandingPadInst *LPad = Builder.CreateLandingPad( |
| 1265 | llvm::StructType::get(Type::getInt8PtrTy(Context), |
| 1266 | Type::getInt32Ty(Context), nullptr), |
| 1267 | PersonalityFn, 0); |
Andrew Kaylor | 43e1d76 | 2015-04-23 00:20:44 +0000 | [diff] [blame] | 1268 | // 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] | 1269 | Function *ActionIntrin = |
| 1270 | Intrinsic::getDeclaration(Handler->getParent(), Intrinsic::eh_actions); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 1271 | Builder.CreateCall(ActionIntrin, {}, "recover"); |
Andrew Kaylor | bb11132 | 2015-04-07 21:30:23 +0000 | [diff] [blame] | 1272 | LPad->setCleanup(true); |
| 1273 | Builder.CreateUnreachable(); |
| 1274 | return StubBB; |
| 1275 | } |
| 1276 | |
| 1277 | // Cycles through the blocks in an outlined handler function looking for an |
| 1278 | // invoke instruction and inserts an invoke of llvm.donothing with an empty |
| 1279 | // landing pad if none is found. The code that generates the .xdata tables for |
| 1280 | // the handler needs at least one landing pad to identify the parent function's |
| 1281 | // personality. |
| 1282 | void WinEHPrepare::addStubInvokeToHandlerIfNeeded(Function *Handler, |
| 1283 | Value *PersonalityFn) { |
| 1284 | ReturnInst *Ret = nullptr; |
Andrew Kaylor | 5f71552 | 2015-04-23 18:37:39 +0000 | [diff] [blame] | 1285 | UnreachableInst *Unreached = nullptr; |
Andrew Kaylor | bb11132 | 2015-04-07 21:30:23 +0000 | [diff] [blame] | 1286 | for (BasicBlock &BB : *Handler) { |
| 1287 | TerminatorInst *Terminator = BB.getTerminator(); |
| 1288 | // If we find an invoke, there is nothing to be done. |
| 1289 | auto *II = dyn_cast<InvokeInst>(Terminator); |
| 1290 | if (II) |
| 1291 | return; |
| 1292 | // If we've already recorded a return instruction, keep looking for invokes. |
Andrew Kaylor | 5f71552 | 2015-04-23 18:37:39 +0000 | [diff] [blame] | 1293 | if (!Ret) |
| 1294 | Ret = dyn_cast<ReturnInst>(Terminator); |
| 1295 | // If we haven't recorded an unreachable instruction, try this terminator. |
| 1296 | if (!Unreached) |
| 1297 | Unreached = dyn_cast<UnreachableInst>(Terminator); |
Andrew Kaylor | bb11132 | 2015-04-07 21:30:23 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | // 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] | 1301 | // at least one return or unreachable instruction. We'll insert an invoke of |
| 1302 | // llvm.donothing ahead of that instruction. |
| 1303 | assert(Ret || Unreached); |
| 1304 | TerminatorInst *Term; |
| 1305 | if (Ret) |
| 1306 | Term = Ret; |
| 1307 | else |
| 1308 | Term = Unreached; |
| 1309 | BasicBlock *OldRetBB = Term->getParent(); |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 1310 | BasicBlock *NewRetBB = SplitBlock(OldRetBB, Term, DT); |
Andrew Kaylor | bb11132 | 2015-04-07 21:30:23 +0000 | [diff] [blame] | 1311 | // SplitBlock adds an unconditional branch instruction at the end of the |
| 1312 | // parent block. We want to replace that with an invoke call, so we can |
| 1313 | // erase it now. |
| 1314 | OldRetBB->getTerminator()->eraseFromParent(); |
| 1315 | BasicBlock *StubLandingPad = createStubLandingPad(Handler, PersonalityFn); |
| 1316 | Function *F = |
| 1317 | Intrinsic::getDeclaration(Handler->getParent(), Intrinsic::donothing); |
| 1318 | InvokeInst::Create(F, NewRetBB, StubLandingPad, None, "", OldRetBB); |
| 1319 | } |
| 1320 | |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1321 | // FIXME: Consider sinking this into lib/Target/X86 somehow. TargetLowering |
| 1322 | // usually doesn't build LLVM IR, so that's probably the wrong place. |
| 1323 | Function *WinEHPrepare::createHandlerFunc(Type *RetTy, const Twine &Name, |
| 1324 | Module *M, Value *&ParentFP) { |
| 1325 | // x64 uses a two-argument prototype where the parent FP is the second |
| 1326 | // argument. x86 uses no arguments, just the incoming EBP value. |
| 1327 | LLVMContext &Context = M->getContext(); |
| 1328 | FunctionType *FnType; |
| 1329 | if (TheTriple.getArch() == Triple::x86_64) { |
| 1330 | Type *Int8PtrType = Type::getInt8PtrTy(Context); |
| 1331 | Type *ArgTys[2] = {Int8PtrType, Int8PtrType}; |
| 1332 | FnType = FunctionType::get(RetTy, ArgTys, false); |
| 1333 | } else { |
| 1334 | FnType = FunctionType::get(RetTy, None, false); |
| 1335 | } |
| 1336 | |
| 1337 | Function *Handler = |
| 1338 | Function::Create(FnType, GlobalVariable::InternalLinkage, Name, M); |
| 1339 | BasicBlock *Entry = BasicBlock::Create(Context, "entry"); |
| 1340 | Handler->getBasicBlockList().push_front(Entry); |
| 1341 | if (TheTriple.getArch() == Triple::x86_64) { |
| 1342 | ParentFP = &(Handler->getArgumentList().back()); |
| 1343 | } else { |
| 1344 | assert(M); |
| 1345 | Function *FrameAddressFn = |
| 1346 | Intrinsic::getDeclaration(M, Intrinsic::frameaddress); |
| 1347 | Value *Args[1] = {ConstantInt::get(Type::getInt32Ty(Context), 1)}; |
| 1348 | ParentFP = CallInst::Create(FrameAddressFn, Args, "parent_fp", |
| 1349 | &Handler->getEntryBlock()); |
| 1350 | } |
| 1351 | return Handler; |
| 1352 | } |
| 1353 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1354 | bool WinEHPrepare::outlineHandler(ActionHandler *Action, Function *SrcFn, |
| 1355 | LandingPadInst *LPad, BasicBlock *StartBB, |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1356 | FrameVarInfoMap &VarInfo) { |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1357 | Module *M = SrcFn->getParent(); |
| 1358 | LLVMContext &Context = M->getContext(); |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1359 | Type *Int8PtrType = Type::getInt8PtrTy(Context); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1360 | |
| 1361 | // Create a new function to receive the handler contents. |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1362 | Value *ParentFP; |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1363 | Function *Handler; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1364 | if (Action->getType() == Catch) { |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1365 | Handler = createHandlerFunc(Int8PtrType, SrcFn->getName() + ".catch", M, |
| 1366 | ParentFP); |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1367 | } else { |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1368 | Handler = createHandlerFunc(Type::getVoidTy(Context), |
| 1369 | SrcFn->getName() + ".cleanup", M, ParentFP); |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1370 | } |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1371 | HandlerToParentFP[Handler] = ParentFP; |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 1372 | Handler->addFnAttr("wineh-parent", SrcFn->getName()); |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1373 | BasicBlock *Entry = &Handler->getEntryBlock(); |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 1374 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1375 | // Generate a standard prolog to setup the frame recovery structure. |
| 1376 | IRBuilder<> Builder(Context); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1377 | Builder.SetInsertPoint(Entry); |
| 1378 | Builder.SetCurrentDebugLocation(LPad->getDebugLoc()); |
| 1379 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1380 | std::unique_ptr<WinEHCloningDirectorBase> Director; |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1381 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1382 | ValueToValueMapTy VMap; |
| 1383 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1384 | LandingPadMap &LPadMap = LPadMaps[LPad]; |
| 1385 | if (!LPadMap.isInitialized()) |
| 1386 | LPadMap.mapLandingPad(LPad); |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 1387 | if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) { |
| 1388 | Constant *Sel = CatchAction->getSelector(); |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 1389 | Director.reset(new WinEHCatchDirector(Handler, ParentFP, Sel, VarInfo, |
| 1390 | LPadMap, NestedLPtoOriginalLP, DT, |
| 1391 | EHBlocks)); |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 1392 | LPadMap.remapEHValues(VMap, UndefValue::get(Int8PtrType), |
| 1393 | ConstantInt::get(Type::getInt32Ty(Context), 1)); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1394 | } else { |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1395 | Director.reset( |
| 1396 | new WinEHCleanupDirector(Handler, ParentFP, VarInfo, LPadMap)); |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 1397 | LPadMap.remapEHValues(VMap, UndefValue::get(Int8PtrType), |
| 1398 | UndefValue::get(Type::getInt32Ty(Context))); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1399 | } |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1400 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1401 | SmallVector<ReturnInst *, 8> Returns; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1402 | ClonedCodeInfo OutlinedFunctionInfo; |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1403 | |
Andrew Kaylor | 3170e56 | 2015-03-20 21:42:54 +0000 | [diff] [blame] | 1404 | // If the start block contains PHI nodes, we need to map them. |
| 1405 | BasicBlock::iterator II = StartBB->begin(); |
| 1406 | while (auto *PN = dyn_cast<PHINode>(II)) { |
| 1407 | bool Mapped = false; |
| 1408 | // Look for PHI values that we have already mapped (such as the selector). |
| 1409 | for (Value *Val : PN->incoming_values()) { |
| 1410 | if (VMap.count(Val)) { |
| 1411 | VMap[PN] = VMap[Val]; |
| 1412 | Mapped = true; |
| 1413 | } |
| 1414 | } |
| 1415 | // If we didn't find a match for this value, map it as an undef. |
| 1416 | if (!Mapped) { |
| 1417 | VMap[PN] = UndefValue::get(PN->getType()); |
| 1418 | } |
| 1419 | ++II; |
| 1420 | } |
| 1421 | |
Andrew Kaylor | 00e5d9e | 2015-04-20 22:53:42 +0000 | [diff] [blame] | 1422 | // The landing pad value may be used by PHI nodes. It will ultimately be |
| 1423 | // eliminated, but we need it in the map for intermediate handling. |
| 1424 | VMap[LPad] = UndefValue::get(LPad->getType()); |
| 1425 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1426 | // Skip over PHIs and, if applicable, landingpad instructions. |
Andrew Kaylor | 3170e56 | 2015-03-20 21:42:54 +0000 | [diff] [blame] | 1427 | II = StartBB->getFirstInsertionPt(); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1428 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1429 | CloneAndPruneIntoFromInst(Handler, SrcFn, II, VMap, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1430 | /*ModuleLevelChanges=*/false, Returns, "", |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1431 | &OutlinedFunctionInfo, Director.get()); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1432 | |
Andrew Kaylor | 5dacfd8 | 2015-04-24 23:10:38 +0000 | [diff] [blame] | 1433 | // Move all the instructions in the cloned "entry" block into our entry block. |
| 1434 | // Depending on how the parent function was laid out, the block that will |
| 1435 | // correspond to the outlined entry block may not be the first block in the |
| 1436 | // list. We can recognize it, however, as the cloned block which has no |
| 1437 | // predecessors. Any other block wouldn't have been cloned if it didn't |
| 1438 | // have a predecessor which was also cloned. |
Andrew Kaylor | 9130743 | 2015-04-28 22:01:51 +0000 | [diff] [blame] | 1439 | Function::iterator ClonedIt = std::next(Function::iterator(Entry)); |
Andrew Kaylor | 5dacfd8 | 2015-04-24 23:10:38 +0000 | [diff] [blame] | 1440 | while (!pred_empty(ClonedIt)) |
| 1441 | ++ClonedIt; |
| 1442 | BasicBlock *ClonedEntryBB = ClonedIt; |
| 1443 | assert(ClonedEntryBB); |
| 1444 | Entry->getInstList().splice(Entry->end(), ClonedEntryBB->getInstList()); |
| 1445 | ClonedEntryBB->eraseFromParent(); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1446 | |
Andrew Kaylor | bb11132 | 2015-04-07 21:30:23 +0000 | [diff] [blame] | 1447 | // Make sure we can identify the handler's personality later. |
| 1448 | addStubInvokeToHandlerIfNeeded(Handler, LPad->getPersonalityFn()); |
| 1449 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1450 | if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) { |
| 1451 | WinEHCatchDirector *CatchDirector = |
| 1452 | reinterpret_cast<WinEHCatchDirector *>(Director.get()); |
| 1453 | CatchAction->setExceptionVar(CatchDirector->getExceptionVar()); |
| 1454 | CatchAction->setReturnTargets(CatchDirector->getReturnTargets()); |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1455 | |
| 1456 | // Look for blocks that are not part of the landing pad that we just |
| 1457 | // outlined but terminate with a call to llvm.eh.endcatch and a |
| 1458 | // branch to a block that is in the handler we just outlined. |
| 1459 | // These blocks will be part of a nested landing pad that intends to |
| 1460 | // return to an address in this handler. This case is best handled |
| 1461 | // after both landing pads have been outlined, so for now we'll just |
| 1462 | // save the association of the blocks in LPadTargetBlocks. The |
| 1463 | // return instructions which are created from these branches will be |
| 1464 | // replaced after all landing pads have been outlined. |
Richard Trieu | 6b1aa5f | 2015-04-15 01:21:15 +0000 | [diff] [blame] | 1465 | for (const auto MapEntry : VMap) { |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1466 | // VMap maps all values and blocks that were just cloned, but dead |
| 1467 | // blocks which were pruned will map to nullptr. |
| 1468 | if (!isa<BasicBlock>(MapEntry.first) || MapEntry.second == nullptr) |
| 1469 | continue; |
| 1470 | const BasicBlock *MappedBB = cast<BasicBlock>(MapEntry.first); |
| 1471 | for (auto *Pred : predecessors(const_cast<BasicBlock *>(MappedBB))) { |
| 1472 | auto *Branch = dyn_cast<BranchInst>(Pred->getTerminator()); |
| 1473 | if (!Branch || !Branch->isUnconditional() || Pred->size() <= 1) |
| 1474 | continue; |
| 1475 | BasicBlock::iterator II = const_cast<BranchInst *>(Branch); |
| 1476 | --II; |
| 1477 | if (match(cast<Value>(II), m_Intrinsic<Intrinsic::eh_endcatch>())) { |
| 1478 | // This would indicate that a nested landing pad wants to return |
| 1479 | // to a block that is outlined into two different handlers. |
| 1480 | assert(!LPadTargetBlocks.count(MappedBB)); |
| 1481 | LPadTargetBlocks[MappedBB] = cast<BasicBlock>(MapEntry.second); |
| 1482 | } |
| 1483 | } |
| 1484 | } |
| 1485 | } // End if (CatchAction) |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1486 | |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 1487 | Action->setHandlerBlockOrFunc(Handler); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1488 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1489 | return true; |
| 1490 | } |
| 1491 | |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 1492 | /// This BB must end in a selector dispatch. All we need to do is pass the |
| 1493 | /// handler block to llvm.eh.actions and list it as a possible indirectbr |
| 1494 | /// target. |
| 1495 | void WinEHPrepare::processSEHCatchHandler(CatchHandler *CatchAction, |
| 1496 | BasicBlock *StartBB) { |
| 1497 | BasicBlock *HandlerBB; |
| 1498 | BasicBlock *NextBB; |
| 1499 | Constant *Selector; |
| 1500 | bool Res = isSelectorDispatch(StartBB, HandlerBB, Selector, NextBB); |
| 1501 | if (Res) { |
| 1502 | // If this was EH dispatch, this must be a conditional branch to the handler |
| 1503 | // block. |
| 1504 | // FIXME: Handle instructions in the dispatch block. Currently we drop them, |
| 1505 | // leading to crashes if some optimization hoists stuff here. |
| 1506 | assert(CatchAction->getSelector() && HandlerBB && |
| 1507 | "expected catch EH dispatch"); |
| 1508 | } else { |
| 1509 | // This must be a catch-all. Split the block after the landingpad. |
| 1510 | assert(CatchAction->getSelector()->isNullValue() && "expected catch-all"); |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 1511 | HandlerBB = SplitBlock(StartBB, StartBB->getFirstInsertionPt(), DT); |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 1512 | } |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 1513 | IRBuilder<> Builder(HandlerBB->getFirstInsertionPt()); |
| 1514 | Function *EHCodeFn = Intrinsic::getDeclaration( |
| 1515 | StartBB->getParent()->getParent(), Intrinsic::eh_exceptioncode); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 1516 | Value *Code = Builder.CreateCall(EHCodeFn, {}, "sehcode"); |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 1517 | Code = Builder.CreateIntToPtr(Code, SEHExceptionCodeSlot->getAllocatedType()); |
| 1518 | Builder.CreateStore(Code, SEHExceptionCodeSlot); |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 1519 | CatchAction->setHandlerBlockOrFunc(BlockAddress::get(HandlerBB)); |
| 1520 | TinyPtrVector<BasicBlock *> Targets(HandlerBB); |
| 1521 | CatchAction->setReturnTargets(Targets); |
| 1522 | } |
| 1523 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1524 | void LandingPadMap::mapLandingPad(const LandingPadInst *LPad) { |
| 1525 | // Each instance of this class should only ever be used to map a single |
| 1526 | // landing pad. |
| 1527 | assert(OriginLPad == nullptr || OriginLPad == LPad); |
| 1528 | |
| 1529 | // If the landing pad has already been mapped, there's nothing more to do. |
| 1530 | if (OriginLPad == LPad) |
| 1531 | return; |
| 1532 | |
| 1533 | OriginLPad = LPad; |
| 1534 | |
| 1535 | // The landingpad instruction returns an aggregate value. Typically, its |
| 1536 | // 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] | 1537 | // results of those extracts will have been promoted to reg values before |
| 1538 | // this routine is called. |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1539 | for (auto *U : LPad->users()) { |
| 1540 | const ExtractValueInst *Extract = dyn_cast<ExtractValueInst>(U); |
| 1541 | if (!Extract) |
| 1542 | continue; |
| 1543 | assert(Extract->getNumIndices() == 1 && |
| 1544 | "Unexpected operation: extracting both landing pad values"); |
| 1545 | unsigned int Idx = *(Extract->idx_begin()); |
| 1546 | assert((Idx == 0 || Idx == 1) && |
| 1547 | "Unexpected operation: extracting an unknown landing pad element"); |
| 1548 | if (Idx == 0) { |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1549 | ExtractedEHPtrs.push_back(Extract); |
| 1550 | } else if (Idx == 1) { |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1551 | ExtractedSelectors.push_back(Extract); |
| 1552 | } |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1553 | } |
| 1554 | } |
| 1555 | |
Andrew Kaylor | f7118ae | 2015-03-27 22:31:12 +0000 | [diff] [blame] | 1556 | bool LandingPadMap::isOriginLandingPadBlock(const BasicBlock *BB) const { |
| 1557 | return BB->getLandingPadInst() == OriginLPad; |
| 1558 | } |
| 1559 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1560 | bool LandingPadMap::isLandingPadSpecificInst(const Instruction *Inst) const { |
| 1561 | if (Inst == OriginLPad) |
| 1562 | return true; |
| 1563 | for (auto *Extract : ExtractedEHPtrs) { |
| 1564 | if (Inst == Extract) |
| 1565 | return true; |
| 1566 | } |
| 1567 | for (auto *Extract : ExtractedSelectors) { |
| 1568 | if (Inst == Extract) |
| 1569 | return true; |
| 1570 | } |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1571 | return false; |
| 1572 | } |
| 1573 | |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 1574 | void LandingPadMap::remapEHValues(ValueToValueMapTy &VMap, Value *EHPtrValue, |
| 1575 | Value *SelectorValue) const { |
| 1576 | // Remap all landing pad extract instructions to the specified values. |
| 1577 | for (auto *Extract : ExtractedEHPtrs) |
| 1578 | VMap[Extract] = EHPtrValue; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1579 | for (auto *Extract : ExtractedSelectors) |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 1580 | VMap[Extract] = SelectorValue; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
Reid Kleckner | f14787d | 2015-04-22 00:07:52 +0000 | [diff] [blame] | 1583 | static bool isFrameAddressCall(const Value *V) { |
| 1584 | return match(const_cast<Value *>(V), |
| 1585 | m_Intrinsic<Intrinsic::frameaddress>(m_SpecificInt(0))); |
| 1586 | } |
| 1587 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1588 | CloningDirector::CloningAction WinEHCloningDirectorBase::handleInstruction( |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1589 | ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) { |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1590 | // If this is one of the boilerplate landing pad instructions, skip it. |
| 1591 | // The instruction will have already been remapped in VMap. |
| 1592 | if (LPadMap.isLandingPadSpecificInst(Inst)) |
| 1593 | return CloningDirector::SkipInstruction; |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1594 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 1595 | // Nested landing pads that have not already been outlined will be cloned as |
| 1596 | // stubs, with just the landingpad instruction and an unreachable instruction. |
| 1597 | // When all landingpads have been outlined, we'll replace this with the |
| 1598 | // llvm.eh.actions call and indirect branch created when the landing pad was |
| 1599 | // outlined. |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1600 | if (auto *LPad = dyn_cast<LandingPadInst>(Inst)) { |
| 1601 | return handleLandingPad(VMap, LPad, NewBB); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 1604 | // Nested landing pads that have already been outlined will be cloned in their |
| 1605 | // outlined form, but we need to intercept the ibr instruction to filter out |
| 1606 | // targets that do not return to the handler we are outlining. |
| 1607 | if (auto *IBr = dyn_cast<IndirectBrInst>(Inst)) { |
| 1608 | return handleIndirectBr(VMap, IBr, NewBB); |
| 1609 | } |
| 1610 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1611 | if (auto *Invoke = dyn_cast<InvokeInst>(Inst)) |
| 1612 | return handleInvoke(VMap, Invoke, NewBB); |
| 1613 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1614 | if (auto *Resume = dyn_cast<ResumeInst>(Inst)) |
| 1615 | return handleResume(VMap, Resume, NewBB); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1616 | |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 1617 | if (auto *Cmp = dyn_cast<CmpInst>(Inst)) |
| 1618 | return handleCompare(VMap, Cmp, NewBB); |
| 1619 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1620 | if (match(Inst, m_Intrinsic<Intrinsic::eh_begincatch>())) |
| 1621 | return handleBeginCatch(VMap, Inst, NewBB); |
| 1622 | if (match(Inst, m_Intrinsic<Intrinsic::eh_endcatch>())) |
| 1623 | return handleEndCatch(VMap, Inst, NewBB); |
| 1624 | if (match(Inst, m_Intrinsic<Intrinsic::eh_typeid_for>())) |
| 1625 | return handleTypeIdFor(VMap, Inst, NewBB); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1626 | |
Reid Kleckner | f14787d | 2015-04-22 00:07:52 +0000 | [diff] [blame] | 1627 | // When outlining llvm.frameaddress(i32 0), remap that to the second argument, |
| 1628 | // which is the FP of the parent. |
| 1629 | if (isFrameAddressCall(Inst)) { |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1630 | VMap[Inst] = ParentFP; |
Reid Kleckner | f14787d | 2015-04-22 00:07:52 +0000 | [diff] [blame] | 1631 | return CloningDirector::SkipInstruction; |
| 1632 | } |
| 1633 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1634 | // Continue with the default cloning behavior. |
| 1635 | return CloningDirector::CloneInstruction; |
| 1636 | } |
| 1637 | |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1638 | CloningDirector::CloningAction WinEHCatchDirector::handleLandingPad( |
| 1639 | ValueToValueMapTy &VMap, const LandingPadInst *LPad, BasicBlock *NewBB) { |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 1640 | // If the instruction after the landing pad is a call to llvm.eh.actions |
| 1641 | // the landing pad has already been outlined. In this case, we should |
| 1642 | // clone it because it may return to a block in the handler we are |
| 1643 | // outlining now that would otherwise be unreachable. The landing pads |
| 1644 | // are sorted before outlining begins to enable this case to work |
| 1645 | // properly. |
| 1646 | const Instruction *NextI = LPad->getNextNode(); |
| 1647 | if (match(NextI, m_Intrinsic<Intrinsic::eh_actions>())) |
| 1648 | return CloningDirector::CloneInstruction; |
| 1649 | |
| 1650 | // If the landing pad hasn't been outlined yet, the landing pad we are |
| 1651 | // outlining now does not dominate it and so it cannot return to a block |
| 1652 | // in this handler. In that case, we can just insert a stub landing |
| 1653 | // pad now and patch it up later. |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1654 | Instruction *NewInst = LPad->clone(); |
| 1655 | if (LPad->hasName()) |
| 1656 | NewInst->setName(LPad->getName()); |
| 1657 | // Save this correlation for later processing. |
| 1658 | NestedLPtoOriginalLP[cast<LandingPadInst>(NewInst)] = LPad; |
| 1659 | VMap[LPad] = NewInst; |
| 1660 | BasicBlock::InstListType &InstList = NewBB->getInstList(); |
| 1661 | InstList.push_back(NewInst); |
| 1662 | InstList.push_back(new UnreachableInst(NewBB->getContext())); |
| 1663 | return CloningDirector::StopCloningBB; |
| 1664 | } |
| 1665 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1666 | CloningDirector::CloningAction WinEHCatchDirector::handleBeginCatch( |
| 1667 | ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) { |
| 1668 | // The argument to the call is some form of the first element of the |
| 1669 | // landingpad aggregate value, but that doesn't matter. It isn't used |
| 1670 | // here. |
Reid Kleckner | 4236653 | 2015-03-03 23:20:30 +0000 | [diff] [blame] | 1671 | // The second argument is an outparameter where the exception object will be |
| 1672 | // stored. Typically the exception object is a scalar, but it can be an |
| 1673 | // aggregate when catching by value. |
| 1674 | // FIXME: Leave something behind to indicate where the exception object lives |
| 1675 | // for this handler. Should it be part of llvm.eh.actions? |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1676 | assert(ExceptionObjectVar == nullptr && "Multiple calls to " |
| 1677 | "llvm.eh.begincatch found while " |
| 1678 | "outlining catch handler."); |
| 1679 | ExceptionObjectVar = Inst->getOperand(1)->stripPointerCasts(); |
Reid Kleckner | 3567d27 | 2015-04-02 21:13:31 +0000 | [diff] [blame] | 1680 | if (isa<ConstantPointerNull>(ExceptionObjectVar)) |
| 1681 | return CloningDirector::SkipInstruction; |
Reid Kleckner | aab30e1 | 2015-04-03 18:18:06 +0000 | [diff] [blame] | 1682 | assert(cast<AllocaInst>(ExceptionObjectVar)->isStaticAlloca() && |
| 1683 | "catch parameter is not static alloca"); |
Reid Kleckner | 3567d27 | 2015-04-02 21:13:31 +0000 | [diff] [blame] | 1684 | Materializer.escapeCatchObject(ExceptionObjectVar); |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1685 | return CloningDirector::SkipInstruction; |
| 1686 | } |
| 1687 | |
| 1688 | CloningDirector::CloningAction |
| 1689 | WinEHCatchDirector::handleEndCatch(ValueToValueMapTy &VMap, |
| 1690 | const Instruction *Inst, BasicBlock *NewBB) { |
| 1691 | auto *IntrinCall = dyn_cast<IntrinsicInst>(Inst); |
| 1692 | // It might be interesting to track whether or not we are inside a catch |
| 1693 | // function, but that might make the algorithm more brittle than it needs |
| 1694 | // to be. |
| 1695 | |
| 1696 | // 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] | 1697 | // landingpad block that is part of the catch handlers exception mechanism, |
Andrew Kaylor | f7118ae | 2015-03-27 22:31:12 +0000 | [diff] [blame] | 1698 | // or at the end of the catch block. However, a catch-all handler may call |
| 1699 | // end catch from the original landing pad. If the call occurs in a nested |
| 1700 | // landing pad block, we must skip it and continue so that the landing pad |
| 1701 | // gets cloned. |
| 1702 | auto *ParentBB = IntrinCall->getParent(); |
| 1703 | if (ParentBB->isLandingPad() && !LPadMap.isOriginLandingPadBlock(ParentBB)) |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1704 | return CloningDirector::SkipInstruction; |
| 1705 | |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 1706 | // If an end catch occurs anywhere else we want to terminate the handler |
| 1707 | // with a return to the code that follows the endcatch call. If the |
| 1708 | // next instruction is not an unconditional branch, we need to split the |
| 1709 | // block to provide a clear target for the return instruction. |
| 1710 | BasicBlock *ContinueBB; |
| 1711 | auto Next = std::next(BasicBlock::const_iterator(IntrinCall)); |
| 1712 | const BranchInst *Branch = dyn_cast<BranchInst>(Next); |
| 1713 | if (!Branch || !Branch->isUnconditional()) { |
| 1714 | // We're interrupting the cloning process at this location, so the |
| 1715 | // const_cast we're doing here will not cause a problem. |
| 1716 | ContinueBB = SplitBlock(const_cast<BasicBlock *>(ParentBB), |
| 1717 | const_cast<Instruction *>(cast<Instruction>(Next))); |
| 1718 | } else { |
| 1719 | ContinueBB = Branch->getSuccessor(0); |
| 1720 | } |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1721 | |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 1722 | ReturnInst::Create(NewBB->getContext(), BlockAddress::get(ContinueBB), NewBB); |
| 1723 | ReturnTargets.push_back(ContinueBB); |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1724 | |
| 1725 | // We just added a terminator to the cloned block. |
| 1726 | // Tell the caller to stop processing the current basic block so that |
| 1727 | // the branch instruction will be skipped. |
| 1728 | return CloningDirector::StopCloningBB; |
| 1729 | } |
| 1730 | |
| 1731 | CloningDirector::CloningAction WinEHCatchDirector::handleTypeIdFor( |
| 1732 | ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) { |
| 1733 | auto *IntrinCall = dyn_cast<IntrinsicInst>(Inst); |
| 1734 | Value *Selector = IntrinCall->getArgOperand(0)->stripPointerCasts(); |
| 1735 | // This causes a replacement that will collapse the landing pad CFG based |
| 1736 | // on the filter function we intend to match. |
| 1737 | if (Selector == CurrentSelector) |
| 1738 | VMap[Inst] = ConstantInt::get(SelectorIDType, 1); |
| 1739 | else |
| 1740 | VMap[Inst] = ConstantInt::get(SelectorIDType, 0); |
| 1741 | // Tell the caller not to clone this instruction. |
| 1742 | return CloningDirector::SkipInstruction; |
| 1743 | } |
| 1744 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 1745 | CloningDirector::CloningAction WinEHCatchDirector::handleIndirectBr( |
| 1746 | ValueToValueMapTy &VMap, |
| 1747 | const IndirectBrInst *IBr, |
| 1748 | BasicBlock *NewBB) { |
| 1749 | // If this indirect branch is not part of a landing pad block, just clone it. |
| 1750 | const BasicBlock *ParentBB = IBr->getParent(); |
| 1751 | if (!ParentBB->isLandingPad()) |
| 1752 | return CloningDirector::CloneInstruction; |
| 1753 | |
| 1754 | // If it is part of a landing pad, we want to filter out target blocks |
| 1755 | // that are not part of the handler we are outlining. |
| 1756 | const LandingPadInst *LPad = ParentBB->getLandingPadInst(); |
| 1757 | |
| 1758 | // Save this correlation for later processing. |
| 1759 | NestedLPtoOriginalLP[cast<LandingPadInst>(VMap[LPad])] = LPad; |
| 1760 | |
| 1761 | // We should only get here for landing pads that have already been outlined. |
| 1762 | assert(match(LPad->getNextNode(), m_Intrinsic<Intrinsic::eh_actions>())); |
| 1763 | |
| 1764 | // Copy the indirectbr, but only include targets that were previously |
| 1765 | // identified as EH blocks and are dominated by the nested landing pad. |
| 1766 | SetVector<const BasicBlock *> ReturnTargets; |
| 1767 | for (int I = 0, E = IBr->getNumDestinations(); I < E; ++I) { |
| 1768 | auto *TargetBB = IBr->getDestination(I); |
| 1769 | if (EHBlocks.count(const_cast<BasicBlock*>(TargetBB)) && |
| 1770 | DT->dominates(ParentBB, TargetBB)) { |
| 1771 | DEBUG(dbgs() << " Adding destination " << TargetBB->getName() << "\n"); |
| 1772 | ReturnTargets.insert(TargetBB); |
| 1773 | } |
| 1774 | } |
| 1775 | IndirectBrInst *NewBranch = |
| 1776 | IndirectBrInst::Create(const_cast<Value *>(IBr->getAddress()), |
| 1777 | ReturnTargets.size(), NewBB); |
| 1778 | for (auto *Target : ReturnTargets) |
| 1779 | NewBranch->addDestination(const_cast<BasicBlock*>(Target)); |
| 1780 | |
| 1781 | // The operands and targets of the branch instruction are remapped later |
| 1782 | // because it is a terminator. Tell the cloning code to clone the |
| 1783 | // blocks we just added to the target list. |
| 1784 | return CloningDirector::CloneSuccessors; |
| 1785 | } |
| 1786 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1787 | CloningDirector::CloningAction |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1788 | WinEHCatchDirector::handleInvoke(ValueToValueMapTy &VMap, |
| 1789 | const InvokeInst *Invoke, BasicBlock *NewBB) { |
| 1790 | return CloningDirector::CloneInstruction; |
| 1791 | } |
| 1792 | |
| 1793 | CloningDirector::CloningAction |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1794 | WinEHCatchDirector::handleResume(ValueToValueMapTy &VMap, |
| 1795 | const ResumeInst *Resume, BasicBlock *NewBB) { |
| 1796 | // Resume instructions shouldn't be reachable from catch handlers. |
| 1797 | // We still need to handle it, but it will be pruned. |
| 1798 | BasicBlock::InstListType &InstList = NewBB->getInstList(); |
| 1799 | InstList.push_back(new UnreachableInst(NewBB->getContext())); |
| 1800 | return CloningDirector::StopCloningBB; |
| 1801 | } |
| 1802 | |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 1803 | CloningDirector::CloningAction |
| 1804 | WinEHCatchDirector::handleCompare(ValueToValueMapTy &VMap, |
| 1805 | const CmpInst *Compare, BasicBlock *NewBB) { |
| 1806 | const IntrinsicInst *IntrinCall = nullptr; |
| 1807 | if (match(Compare->getOperand(0), m_Intrinsic<Intrinsic::eh_typeid_for>())) { |
| 1808 | IntrinCall = dyn_cast<IntrinsicInst>(Compare->getOperand(0)); |
Andrew Kaylor | 9130743 | 2015-04-28 22:01:51 +0000 | [diff] [blame] | 1809 | } else if (match(Compare->getOperand(1), |
| 1810 | m_Intrinsic<Intrinsic::eh_typeid_for>())) { |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 1811 | IntrinCall = dyn_cast<IntrinsicInst>(Compare->getOperand(1)); |
| 1812 | } |
| 1813 | if (IntrinCall) { |
| 1814 | Value *Selector = IntrinCall->getArgOperand(0)->stripPointerCasts(); |
| 1815 | // This causes a replacement that will collapse the landing pad CFG based |
| 1816 | // on the filter function we intend to match. |
| 1817 | if (Selector == CurrentSelector->stripPointerCasts()) { |
| 1818 | VMap[Compare] = ConstantInt::get(SelectorIDType, 1); |
Andrew Kaylor | 9130743 | 2015-04-28 22:01:51 +0000 | [diff] [blame] | 1819 | } else { |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 1820 | VMap[Compare] = ConstantInt::get(SelectorIDType, 0); |
| 1821 | } |
| 1822 | return CloningDirector::SkipInstruction; |
| 1823 | } |
| 1824 | return CloningDirector::CloneInstruction; |
| 1825 | } |
| 1826 | |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1827 | CloningDirector::CloningAction WinEHCleanupDirector::handleLandingPad( |
| 1828 | ValueToValueMapTy &VMap, const LandingPadInst *LPad, BasicBlock *NewBB) { |
| 1829 | // The MS runtime will terminate the process if an exception occurs in a |
| 1830 | // cleanup handler, so we shouldn't encounter landing pads in the actual |
| 1831 | // cleanup code, but they may appear in catch blocks. Depending on where |
| 1832 | // we started cloning we may see one, but it will get dropped during dead |
| 1833 | // block pruning. |
| 1834 | Instruction *NewInst = new UnreachableInst(NewBB->getContext()); |
| 1835 | VMap[LPad] = NewInst; |
| 1836 | BasicBlock::InstListType &InstList = NewBB->getInstList(); |
| 1837 | InstList.push_back(NewInst); |
| 1838 | return CloningDirector::StopCloningBB; |
| 1839 | } |
| 1840 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1841 | CloningDirector::CloningAction WinEHCleanupDirector::handleBeginCatch( |
| 1842 | ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) { |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 1843 | // Cleanup code may flow into catch blocks or the catch block may be part |
| 1844 | // of a branch that will be optimized away. We'll insert a return |
| 1845 | // instruction now, but it may be pruned before the cloning process is |
| 1846 | // complete. |
| 1847 | ReturnInst::Create(NewBB->getContext(), nullptr, NewBB); |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1848 | return CloningDirector::StopCloningBB; |
| 1849 | } |
| 1850 | |
| 1851 | CloningDirector::CloningAction WinEHCleanupDirector::handleEndCatch( |
| 1852 | ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) { |
Andrew Kaylor | bb11132 | 2015-04-07 21:30:23 +0000 | [diff] [blame] | 1853 | // Cleanup handlers nested within catch handlers may begin with a call to |
| 1854 | // eh.endcatch. We can just ignore that instruction. |
| 1855 | return CloningDirector::SkipInstruction; |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
| 1858 | CloningDirector::CloningAction WinEHCleanupDirector::handleTypeIdFor( |
| 1859 | ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) { |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1860 | // If we encounter a selector comparison while cloning a cleanup handler, |
| 1861 | // we want to stop cloning immediately. Anything after the dispatch |
| 1862 | // will be outlined into a different handler. |
| 1863 | BasicBlock *CatchHandler; |
| 1864 | Constant *Selector; |
| 1865 | BasicBlock *NextBB; |
| 1866 | if (isSelectorDispatch(const_cast<BasicBlock *>(Inst->getParent()), |
| 1867 | CatchHandler, Selector, NextBB)) { |
| 1868 | ReturnInst::Create(NewBB->getContext(), nullptr, NewBB); |
| 1869 | return CloningDirector::StopCloningBB; |
| 1870 | } |
| 1871 | // 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] | 1872 | VMap[Inst] = ConstantInt::get(SelectorIDType, 0); |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1873 | return CloningDirector::SkipInstruction; |
| 1874 | } |
| 1875 | |
Andrew Kaylor | a6c5b96 | 2015-05-20 23:22:24 +0000 | [diff] [blame] | 1876 | CloningDirector::CloningAction WinEHCleanupDirector::handleIndirectBr( |
| 1877 | ValueToValueMapTy &VMap, |
| 1878 | const IndirectBrInst *IBr, |
| 1879 | BasicBlock *NewBB) { |
| 1880 | // No special handling is required for cleanup cloning. |
| 1881 | return CloningDirector::CloneInstruction; |
| 1882 | } |
| 1883 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1884 | CloningDirector::CloningAction WinEHCleanupDirector::handleInvoke( |
| 1885 | ValueToValueMapTy &VMap, const InvokeInst *Invoke, BasicBlock *NewBB) { |
| 1886 | // All invokes in cleanup handlers can be replaced with calls. |
| 1887 | SmallVector<Value *, 16> CallArgs(Invoke->op_begin(), Invoke->op_end() - 3); |
| 1888 | // Insert a normal call instruction... |
| 1889 | CallInst *NewCall = |
| 1890 | CallInst::Create(const_cast<Value *>(Invoke->getCalledValue()), CallArgs, |
| 1891 | Invoke->getName(), NewBB); |
| 1892 | NewCall->setCallingConv(Invoke->getCallingConv()); |
| 1893 | NewCall->setAttributes(Invoke->getAttributes()); |
| 1894 | NewCall->setDebugLoc(Invoke->getDebugLoc()); |
| 1895 | VMap[Invoke] = NewCall; |
| 1896 | |
Reid Kleckner | 6e48a82 | 2015-04-10 16:26:42 +0000 | [diff] [blame] | 1897 | // Remap the operands. |
| 1898 | llvm::RemapInstruction(NewCall, VMap, RF_None, nullptr, &Materializer); |
| 1899 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1900 | // Insert an unconditional branch to the normal destination. |
| 1901 | BranchInst::Create(Invoke->getNormalDest(), NewBB); |
| 1902 | |
| 1903 | // The unwind destination won't be cloned into the new function, so |
| 1904 | // we don't need to clean up its phi nodes. |
| 1905 | |
| 1906 | // We just added a terminator to the cloned block. |
| 1907 | // Tell the caller to stop processing the current basic block. |
Reid Kleckner | 6e48a82 | 2015-04-10 16:26:42 +0000 | [diff] [blame] | 1908 | return CloningDirector::CloneSuccessors; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
Andrew Kaylor | f0f5e46 | 2015-03-03 20:00:16 +0000 | [diff] [blame] | 1911 | CloningDirector::CloningAction WinEHCleanupDirector::handleResume( |
| 1912 | ValueToValueMapTy &VMap, const ResumeInst *Resume, BasicBlock *NewBB) { |
| 1913 | ReturnInst::Create(NewBB->getContext(), nullptr, NewBB); |
| 1914 | |
| 1915 | // We just added a terminator to the cloned block. |
| 1916 | // Tell the caller to stop processing the current basic block so that |
| 1917 | // the branch instruction will be skipped. |
| 1918 | return CloningDirector::StopCloningBB; |
| 1919 | } |
| 1920 | |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 1921 | CloningDirector::CloningAction |
| 1922 | WinEHCleanupDirector::handleCompare(ValueToValueMapTy &VMap, |
| 1923 | const CmpInst *Compare, BasicBlock *NewBB) { |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 1924 | if (match(Compare->getOperand(0), m_Intrinsic<Intrinsic::eh_typeid_for>()) || |
| 1925 | match(Compare->getOperand(1), m_Intrinsic<Intrinsic::eh_typeid_for>())) { |
| 1926 | VMap[Compare] = ConstantInt::get(SelectorIDType, 1); |
| 1927 | return CloningDirector::SkipInstruction; |
| 1928 | } |
| 1929 | return CloningDirector::CloneInstruction; |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1932 | WinEHFrameVariableMaterializer::WinEHFrameVariableMaterializer( |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1933 | Function *OutlinedFn, Value *ParentFP, FrameVarInfoMap &FrameVarInfo) |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1934 | : FrameVarInfo(FrameVarInfo), Builder(OutlinedFn->getContext()) { |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 1935 | BasicBlock *EntryBB = &OutlinedFn->getEntryBlock(); |
Reid Kleckner | bcda1cd | 2015-04-29 22:49:54 +0000 | [diff] [blame] | 1936 | |
| 1937 | // New allocas should be inserted in the entry block, but after the parent FP |
| 1938 | // is established if it is an instruction. |
| 1939 | Instruction *InsertPoint = EntryBB->getFirstInsertionPt(); |
| 1940 | if (auto *FPInst = dyn_cast<Instruction>(ParentFP)) |
| 1941 | InsertPoint = FPInst->getNextNode(); |
| 1942 | Builder.SetInsertPoint(EntryBB, InsertPoint); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | Value *WinEHFrameVariableMaterializer::materializeValueFor(Value *V) { |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 1946 | // If we're asked to materialize a static alloca, we temporarily create an |
| 1947 | // alloca in the outlined function and add this to the FrameVarInfo map. When |
| 1948 | // all the outlining is complete, we'll replace these temporary allocas with |
| 1949 | // calls to llvm.framerecover. |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1950 | if (auto *AV = dyn_cast<AllocaInst>(V)) { |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 1951 | assert(AV->isStaticAlloca() && |
| 1952 | "cannot materialize un-demoted dynamic alloca"); |
Andrew Kaylor | 72029c6 | 2015-03-03 00:41:03 +0000 | [diff] [blame] | 1953 | AllocaInst *NewAlloca = dyn_cast<AllocaInst>(AV->clone()); |
| 1954 | Builder.Insert(NewAlloca, AV->getName()); |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 1955 | FrameVarInfo[AV].push_back(NewAlloca); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1956 | return NewAlloca; |
| 1957 | } |
| 1958 | |
Andrew Kaylor | 72029c6 | 2015-03-03 00:41:03 +0000 | [diff] [blame] | 1959 | if (isa<Instruction>(V) || isa<Argument>(V)) { |
Reid Kleckner | d1b38c4 | 2015-05-06 18:45:24 +0000 | [diff] [blame] | 1960 | Function *Parent = isa<Instruction>(V) |
| 1961 | ? cast<Instruction>(V)->getParent()->getParent() |
| 1962 | : cast<Argument>(V)->getParent(); |
| 1963 | errs() |
| 1964 | << "Failed to demote instruction used in exception handler of function " |
| 1965 | << GlobalValue::getRealLinkageName(Parent->getName()) << ":\n"; |
Reid Kleckner | fd7df28 | 2015-04-22 21:05:21 +0000 | [diff] [blame] | 1966 | errs() << " " << *V << '\n'; |
| 1967 | report_fatal_error("WinEHPrepare failed to demote instruction"); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1968 | } |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1969 | |
Andrew Kaylor | 72029c6 | 2015-03-03 00:41:03 +0000 | [diff] [blame] | 1970 | // Don't materialize other values. |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1971 | return nullptr; |
| 1972 | } |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1973 | |
Reid Kleckner | 3567d27 | 2015-04-02 21:13:31 +0000 | [diff] [blame] | 1974 | void WinEHFrameVariableMaterializer::escapeCatchObject(Value *V) { |
| 1975 | // Catch parameter objects have to live in the parent frame. When we see a use |
| 1976 | // of a catch parameter, add a sentinel to the multimap to indicate that it's |
| 1977 | // used from another handler. This will prevent us from trying to sink the |
| 1978 | // alloca into the handler and ensure that the catch parameter is present in |
| 1979 | // the call to llvm.frameescape. |
| 1980 | FrameVarInfo[V].push_back(getCatchObjectSentinel()); |
| 1981 | } |
| 1982 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 1983 | // This function maps the catch and cleanup handlers that are reachable from the |
| 1984 | // specified landing pad. The landing pad sequence will have this basic shape: |
| 1985 | // |
| 1986 | // <cleanup handler> |
| 1987 | // <selector comparison> |
| 1988 | // <catch handler> |
| 1989 | // <cleanup handler> |
| 1990 | // <selector comparison> |
| 1991 | // <catch handler> |
| 1992 | // <cleanup handler> |
| 1993 | // ... |
| 1994 | // |
| 1995 | // Any of the cleanup slots may be absent. The cleanup slots may be occupied by |
| 1996 | // any arbitrary control flow, but all paths through the cleanup code must |
| 1997 | // eventually reach the next selector comparison and no path can skip to a |
| 1998 | // different selector comparisons, though some paths may terminate abnormally. |
| 1999 | // Therefore, we will use a depth first search from the start of any given |
| 2000 | // cleanup block and stop searching when we find the next selector comparison. |
| 2001 | // |
| 2002 | // If the landingpad instruction does not have a catch clause, we will assume |
| 2003 | // that any instructions other than selector comparisons and catch handlers can |
| 2004 | // be ignored. In practice, these will only be the boilerplate instructions. |
| 2005 | // |
| 2006 | // The catch handlers may also have any control structure, but we are only |
| 2007 | // interested in the start of the catch handlers, so we don't need to actually |
| 2008 | // follow the flow of the catch handlers. The start of the catch handlers can |
| 2009 | // be located from the compare instructions, but they can be skipped in the |
| 2010 | // flow by following the contrary branch. |
| 2011 | void WinEHPrepare::mapLandingPadBlocks(LandingPadInst *LPad, |
| 2012 | LandingPadActions &Actions) { |
| 2013 | unsigned int NumClauses = LPad->getNumClauses(); |
| 2014 | unsigned int HandlersFound = 0; |
| 2015 | BasicBlock *BB = LPad->getParent(); |
| 2016 | |
| 2017 | DEBUG(dbgs() << "Mapping landing pad: " << BB->getName() << "\n"); |
| 2018 | |
| 2019 | if (NumClauses == 0) { |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2020 | findCleanupHandlers(Actions, BB, nullptr); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2021 | return; |
| 2022 | } |
| 2023 | |
| 2024 | VisitedBlockSet VisitedBlocks; |
| 2025 | |
| 2026 | while (HandlersFound != NumClauses) { |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2027 | BasicBlock *NextBB = nullptr; |
| 2028 | |
Andrew Kaylor | 20ae2a3 | 2015-04-23 22:38:36 +0000 | [diff] [blame] | 2029 | // Skip over filter clauses. |
| 2030 | if (LPad->isFilter(HandlersFound)) { |
| 2031 | ++HandlersFound; |
| 2032 | continue; |
| 2033 | } |
| 2034 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2035 | // See if the clause we're looking for is a catch-all. |
| 2036 | // If so, the catch begins immediately. |
Andrew Kaylor | 9130743 | 2015-04-28 22:01:51 +0000 | [diff] [blame] | 2037 | Constant *ExpectedSelector = |
| 2038 | LPad->getClause(HandlersFound)->stripPointerCasts(); |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 2039 | if (isa<ConstantPointerNull>(ExpectedSelector)) { |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2040 | // The catch all must occur last. |
| 2041 | assert(HandlersFound == NumClauses - 1); |
| 2042 | |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 2043 | // There can be additional selector dispatches in the call chain that we |
| 2044 | // need to ignore. |
| 2045 | BasicBlock *CatchBlock = nullptr; |
| 2046 | Constant *Selector; |
| 2047 | while (BB && isSelectorDispatch(BB, CatchBlock, Selector, NextBB)) { |
| 2048 | DEBUG(dbgs() << " Found extra catch dispatch in block " |
Andrew Kaylor | 9130743 | 2015-04-28 22:01:51 +0000 | [diff] [blame] | 2049 | << CatchBlock->getName() << "\n"); |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 2050 | BB = NextBB; |
| 2051 | } |
| 2052 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2053 | // Add the catch handler to the action list. |
Andrew Kaylor | f18771b | 2015-04-20 18:48:45 +0000 | [diff] [blame] | 2054 | CatchHandler *Action = nullptr; |
| 2055 | if (CatchHandlerMap.count(BB) && CatchHandlerMap[BB] != nullptr) { |
| 2056 | // If the CatchHandlerMap already has an entry for this BB, re-use it. |
| 2057 | Action = CatchHandlerMap[BB]; |
| 2058 | assert(Action->getSelector() == ExpectedSelector); |
| 2059 | } else { |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 2060 | // We don't expect a selector dispatch, but there may be a call to |
| 2061 | // llvm.eh.begincatch, which separates catch handling code from |
| 2062 | // cleanup code in the same control flow. This call looks for the |
| 2063 | // begincatch intrinsic. |
| 2064 | Action = findCatchHandler(BB, NextBB, VisitedBlocks); |
| 2065 | if (Action) { |
| 2066 | // For C++ EH, check if there is any interesting cleanup code before |
| 2067 | // we begin the catch. This is important because cleanups cannot |
| 2068 | // rethrow exceptions but code called from catches can. For SEH, it |
| 2069 | // isn't important if some finally code before a catch-all is executed |
| 2070 | // out of line or after recovering from the exception. |
| 2071 | if (Personality == EHPersonality::MSVC_CXX) |
| 2072 | findCleanupHandlers(Actions, BB, BB); |
| 2073 | } else { |
| 2074 | // If an action was not found, it means that the control flows |
| 2075 | // directly into the catch-all handler and there is no cleanup code. |
| 2076 | // That's an expected situation and we must create a catch action. |
| 2077 | // Since this is a catch-all handler, the selector won't actually |
| 2078 | // appear in the code anywhere. ExpectedSelector here is the constant |
| 2079 | // null ptr that we got from the landing pad instruction. |
| 2080 | Action = new CatchHandler(BB, ExpectedSelector, nullptr); |
| 2081 | CatchHandlerMap[BB] = Action; |
| 2082 | } |
Andrew Kaylor | f18771b | 2015-04-20 18:48:45 +0000 | [diff] [blame] | 2083 | } |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2084 | Actions.insertCatchHandler(Action); |
| 2085 | DEBUG(dbgs() << " Catch all handler at block " << BB->getName() << "\n"); |
| 2086 | ++HandlersFound; |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 2087 | |
| 2088 | // Once we reach a catch-all, don't expect to hit a resume instruction. |
| 2089 | BB = nullptr; |
| 2090 | break; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
| 2093 | CatchHandler *CatchAction = findCatchHandler(BB, NextBB, VisitedBlocks); |
Andrew Kaylor | 4175851 | 2015-04-20 22:04:09 +0000 | [diff] [blame] | 2094 | assert(CatchAction); |
| 2095 | |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2096 | // See if there is any interesting code executed before the dispatch. |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2097 | findCleanupHandlers(Actions, BB, CatchAction->getStartBlock()); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2098 | |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 2099 | // When the source program contains multiple nested try blocks the catch |
| 2100 | // handlers can get strung together in such a way that we can encounter |
| 2101 | // a dispatch for a selector that we've already had a handler for. |
| 2102 | if (CatchAction->getSelector()->stripPointerCasts() == ExpectedSelector) { |
| 2103 | ++HandlersFound; |
| 2104 | |
| 2105 | // Add the catch handler to the action list. |
| 2106 | DEBUG(dbgs() << " Found catch dispatch in block " |
| 2107 | << CatchAction->getStartBlock()->getName() << "\n"); |
| 2108 | Actions.insertCatchHandler(CatchAction); |
| 2109 | } else { |
Andrew Kaylor | 4175851 | 2015-04-20 22:04:09 +0000 | [diff] [blame] | 2110 | // Under some circumstances optimized IR will flow unconditionally into a |
| 2111 | // handler block without checking the selector. This can only happen if |
| 2112 | // the landing pad has a catch-all handler and the handler for the |
| 2113 | // preceeding catch clause is identical to the catch-call handler |
| 2114 | // (typically an empty catch). In this case, the handler must be shared |
| 2115 | // by all remaining clauses. |
| 2116 | if (isa<ConstantPointerNull>( |
| 2117 | CatchAction->getSelector()->stripPointerCasts())) { |
| 2118 | DEBUG(dbgs() << " Applying early catch-all handler in block " |
| 2119 | << CatchAction->getStartBlock()->getName() |
| 2120 | << " to all remaining clauses.\n"); |
| 2121 | Actions.insertCatchHandler(CatchAction); |
| 2122 | return; |
| 2123 | } |
| 2124 | |
Andrew Kaylor | ea8df61 | 2015-04-17 23:05:43 +0000 | [diff] [blame] | 2125 | DEBUG(dbgs() << " Found extra catch dispatch in block " |
| 2126 | << CatchAction->getStartBlock()->getName() << "\n"); |
| 2127 | } |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2128 | |
| 2129 | // Move on to the block after the catch handler. |
| 2130 | BB = NextBB; |
| 2131 | } |
| 2132 | |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 2133 | // If we didn't wind up in a catch-all, see if there is any interesting code |
| 2134 | // executed before the resume. |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2135 | findCleanupHandlers(Actions, BB, BB); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2136 | |
| 2137 | // It's possible that some optimization moved code into a landingpad that |
| 2138 | // wasn't |
| 2139 | // previously being used for cleanup. If that happens, we need to execute |
| 2140 | // that |
| 2141 | // extra code from a cleanup handler. |
| 2142 | if (Actions.includesCleanup() && !LPad->isCleanup()) |
| 2143 | LPad->setCleanup(true); |
| 2144 | } |
| 2145 | |
| 2146 | // This function searches starting with the input block for the next |
| 2147 | // block that terminates with a branch whose condition is based on a selector |
| 2148 | // comparison. This may be the input block. See the mapLandingPadBlocks |
| 2149 | // comments for a discussion of control flow assumptions. |
| 2150 | // |
| 2151 | CatchHandler *WinEHPrepare::findCatchHandler(BasicBlock *BB, |
| 2152 | BasicBlock *&NextBB, |
| 2153 | VisitedBlockSet &VisitedBlocks) { |
| 2154 | // See if we've already found a catch handler use it. |
| 2155 | // Call count() first to avoid creating a null entry for blocks |
| 2156 | // we haven't seen before. |
| 2157 | if (CatchHandlerMap.count(BB) && CatchHandlerMap[BB] != nullptr) { |
| 2158 | CatchHandler *Action = cast<CatchHandler>(CatchHandlerMap[BB]); |
| 2159 | NextBB = Action->getNextBB(); |
| 2160 | return Action; |
| 2161 | } |
| 2162 | |
| 2163 | // VisitedBlocks applies only to the current search. We still |
| 2164 | // need to consider blocks that we've visited while mapping other |
| 2165 | // landing pads. |
| 2166 | VisitedBlocks.insert(BB); |
| 2167 | |
| 2168 | BasicBlock *CatchBlock = nullptr; |
| 2169 | Constant *Selector = nullptr; |
| 2170 | |
| 2171 | // If this is the first time we've visited this block from any landing pad |
| 2172 | // look to see if it is a selector dispatch block. |
| 2173 | if (!CatchHandlerMap.count(BB)) { |
| 2174 | if (isSelectorDispatch(BB, CatchBlock, Selector, NextBB)) { |
| 2175 | CatchHandler *Action = new CatchHandler(BB, Selector, NextBB); |
| 2176 | CatchHandlerMap[BB] = Action; |
| 2177 | return Action; |
| 2178 | } |
Andrew Kaylor | 4175851 | 2015-04-20 22:04:09 +0000 | [diff] [blame] | 2179 | // If we encounter a block containing an llvm.eh.begincatch before we |
| 2180 | // find a selector dispatch block, the handler is assumed to be |
| 2181 | // reached unconditionally. This happens for catch-all blocks, but |
| 2182 | // it can also happen for other catch handlers that have been combined |
| 2183 | // with the catch-all handler during optimization. |
| 2184 | if (isCatchBlock(BB)) { |
| 2185 | PointerType *Int8PtrTy = Type::getInt8PtrTy(BB->getContext()); |
| 2186 | Constant *NullSelector = ConstantPointerNull::get(Int8PtrTy); |
| 2187 | CatchHandler *Action = new CatchHandler(BB, NullSelector, nullptr); |
| 2188 | CatchHandlerMap[BB] = Action; |
| 2189 | return Action; |
| 2190 | } |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2191 | } |
| 2192 | |
| 2193 | // Visit each successor, looking for the dispatch. |
| 2194 | // FIXME: We expect to find the dispatch quickly, so this will probably |
| 2195 | // work better as a breadth first search. |
| 2196 | for (BasicBlock *Succ : successors(BB)) { |
| 2197 | if (VisitedBlocks.count(Succ)) |
| 2198 | continue; |
| 2199 | |
| 2200 | CatchHandler *Action = findCatchHandler(Succ, NextBB, VisitedBlocks); |
| 2201 | if (Action) |
| 2202 | return Action; |
| 2203 | } |
| 2204 | return nullptr; |
| 2205 | } |
| 2206 | |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2207 | // These are helper functions to combine repeated code from findCleanupHandlers. |
| 2208 | static void createCleanupHandler(LandingPadActions &Actions, |
| 2209 | CleanupHandlerMapTy &CleanupHandlerMap, |
| 2210 | BasicBlock *BB) { |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2211 | CleanupHandler *Action = new CleanupHandler(BB); |
| 2212 | CleanupHandlerMap[BB] = Action; |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2213 | Actions.insertCleanupHandler(Action); |
| 2214 | DEBUG(dbgs() << " Found cleanup code in block " |
| 2215 | << Action->getStartBlock()->getName() << "\n"); |
| 2216 | } |
| 2217 | |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2218 | static CallSite matchOutlinedFinallyCall(BasicBlock *BB, |
| 2219 | Instruction *MaybeCall) { |
| 2220 | // Look for finally blocks that Clang has already outlined for us. |
| 2221 | // %fp = call i8* @llvm.frameaddress(i32 0) |
| 2222 | // call void @"fin$parent"(iN 1, i8* %fp) |
| 2223 | if (isFrameAddressCall(MaybeCall) && MaybeCall != BB->getTerminator()) |
| 2224 | MaybeCall = MaybeCall->getNextNode(); |
| 2225 | CallSite FinallyCall(MaybeCall); |
| 2226 | if (!FinallyCall || FinallyCall.arg_size() != 2) |
| 2227 | return CallSite(); |
| 2228 | if (!match(FinallyCall.getArgument(0), m_SpecificInt(1))) |
| 2229 | return CallSite(); |
| 2230 | if (!isFrameAddressCall(FinallyCall.getArgument(1))) |
| 2231 | return CallSite(); |
| 2232 | return FinallyCall; |
| 2233 | } |
| 2234 | |
| 2235 | static BasicBlock *followSingleUnconditionalBranches(BasicBlock *BB) { |
| 2236 | // Skip single ubr blocks. |
| 2237 | while (BB->getFirstNonPHIOrDbg() == BB->getTerminator()) { |
| 2238 | auto *Br = dyn_cast<BranchInst>(BB->getTerminator()); |
| 2239 | if (Br && Br->isUnconditional()) |
| 2240 | BB = Br->getSuccessor(0); |
| 2241 | else |
| 2242 | return BB; |
| 2243 | } |
| 2244 | return BB; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | // This function searches starting with the input block for the next block that |
| 2248 | // contains code that is not part of a catch handler and would not be eliminated |
| 2249 | // during handler outlining. |
| 2250 | // |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2251 | void WinEHPrepare::findCleanupHandlers(LandingPadActions &Actions, |
| 2252 | BasicBlock *StartBB, BasicBlock *EndBB) { |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2253 | // Here we will skip over the following: |
| 2254 | // |
| 2255 | // landing pad prolog: |
| 2256 | // |
| 2257 | // Unconditional branches |
| 2258 | // |
| 2259 | // Selector dispatch |
| 2260 | // |
| 2261 | // Resume pattern |
| 2262 | // |
| 2263 | // Anything else marks the start of an interesting block |
| 2264 | |
| 2265 | BasicBlock *BB = StartBB; |
| 2266 | // Anything other than an unconditional branch will kick us out of this loop |
| 2267 | // one way or another. |
| 2268 | while (BB) { |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2269 | BB = followSingleUnconditionalBranches(BB); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2270 | // If we've already scanned this block, don't scan it again. If it is |
| 2271 | // a cleanup block, there will be an action in the CleanupHandlerMap. |
| 2272 | // If we've scanned it and it is not a cleanup block, there will be a |
| 2273 | // nullptr in the CleanupHandlerMap. If we have not scanned it, there will |
| 2274 | // be no entry in the CleanupHandlerMap. We must call count() first to |
| 2275 | // avoid creating a null entry for blocks we haven't scanned. |
| 2276 | if (CleanupHandlerMap.count(BB)) { |
| 2277 | if (auto *Action = CleanupHandlerMap[BB]) { |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2278 | Actions.insertCleanupHandler(Action); |
| 2279 | DEBUG(dbgs() << " Found cleanup code in block " |
Andrew Kaylor | 9130743 | 2015-04-28 22:01:51 +0000 | [diff] [blame] | 2280 | << Action->getStartBlock()->getName() << "\n"); |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2281 | // FIXME: This cleanup might chain into another, and we need to discover |
| 2282 | // that. |
| 2283 | return; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2284 | } else { |
| 2285 | // Here we handle the case where the cleanup handler map contains a |
| 2286 | // value for this block but the value is a nullptr. This means that |
| 2287 | // we have previously analyzed the block and determined that it did |
| 2288 | // not contain any cleanup code. Based on the earlier analysis, we |
| 2289 | // know the the block must end in either an unconditional branch, a |
| 2290 | // resume or a conditional branch that is predicated on a comparison |
| 2291 | // with a selector. Either the resume or the selector dispatch |
| 2292 | // would terminate the search for cleanup code, so the unconditional |
| 2293 | // branch is the only case for which we might need to continue |
| 2294 | // searching. |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2295 | BasicBlock *SuccBB = followSingleUnconditionalBranches(BB); |
| 2296 | if (SuccBB == BB || SuccBB == EndBB) |
| 2297 | return; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2298 | BB = SuccBB; |
| 2299 | continue; |
| 2300 | } |
| 2301 | } |
| 2302 | |
| 2303 | // Create an entry in the cleanup handler map for this block. Initially |
| 2304 | // we create an entry that says this isn't a cleanup block. If we find |
| 2305 | // cleanup code, the caller will replace this entry. |
| 2306 | CleanupHandlerMap[BB] = nullptr; |
| 2307 | |
| 2308 | TerminatorInst *Terminator = BB->getTerminator(); |
| 2309 | |
| 2310 | // Landing pad blocks have extra instructions we need to accept. |
| 2311 | LandingPadMap *LPadMap = nullptr; |
| 2312 | if (BB->isLandingPad()) { |
| 2313 | LandingPadInst *LPad = BB->getLandingPadInst(); |
| 2314 | LPadMap = &LPadMaps[LPad]; |
| 2315 | if (!LPadMap->isInitialized()) |
| 2316 | LPadMap->mapLandingPad(LPad); |
| 2317 | } |
| 2318 | |
| 2319 | // Look for the bare resume pattern: |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 2320 | // %lpad.val1 = insertvalue { i8*, i32 } undef, i8* %exn, 0 |
| 2321 | // %lpad.val2 = insertvalue { i8*, i32 } %lpad.val1, i32 %sel, 1 |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2322 | // resume { i8*, i32 } %lpad.val2 |
| 2323 | if (auto *Resume = dyn_cast<ResumeInst>(Terminator)) { |
| 2324 | InsertValueInst *Insert1 = nullptr; |
| 2325 | InsertValueInst *Insert2 = nullptr; |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 2326 | Value *ResumeVal = Resume->getOperand(0); |
Reid Kleckner | 1c130bb | 2015-04-16 17:02:23 +0000 | [diff] [blame] | 2327 | // If the resume value isn't a phi or landingpad value, it should be a |
| 2328 | // series of insertions. Identify them so we can avoid them when scanning |
| 2329 | // for cleanups. |
| 2330 | if (!isa<PHINode>(ResumeVal) && !isa<LandingPadInst>(ResumeVal)) { |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 2331 | Insert2 = dyn_cast<InsertValueInst>(ResumeVal); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2332 | if (!Insert2) |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2333 | return createCleanupHandler(Actions, CleanupHandlerMap, BB); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2334 | Insert1 = dyn_cast<InsertValueInst>(Insert2->getAggregateOperand()); |
| 2335 | if (!Insert1) |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2336 | return createCleanupHandler(Actions, CleanupHandlerMap, BB); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2337 | } |
| 2338 | for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end(); |
| 2339 | II != IE; ++II) { |
| 2340 | Instruction *Inst = II; |
| 2341 | if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst)) |
| 2342 | continue; |
| 2343 | if (Inst == Insert1 || Inst == Insert2 || Inst == Resume) |
| 2344 | continue; |
| 2345 | if (!Inst->hasOneUse() || |
| 2346 | (Inst->user_back() != Insert1 && Inst->user_back() != Insert2)) { |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2347 | return createCleanupHandler(Actions, CleanupHandlerMap, BB); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2348 | } |
| 2349 | } |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2350 | return; |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2351 | } |
| 2352 | |
| 2353 | BranchInst *Branch = dyn_cast<BranchInst>(Terminator); |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 2354 | if (Branch && Branch->isConditional()) { |
| 2355 | // Look for the selector dispatch. |
| 2356 | // %2 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIf to i8*)) |
| 2357 | // %matches = icmp eq i32 %sel, %2 |
| 2358 | // br i1 %matches, label %catch14, label %eh.resume |
| 2359 | CmpInst *Compare = dyn_cast<CmpInst>(Branch->getCondition()); |
| 2360 | if (!Compare || !Compare->isEquality()) |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2361 | return createCleanupHandler(Actions, CleanupHandlerMap, BB); |
Andrew Kaylor | bb11132 | 2015-04-07 21:30:23 +0000 | [diff] [blame] | 2362 | for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end(); |
| 2363 | II != IE; ++II) { |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 2364 | Instruction *Inst = II; |
| 2365 | if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst)) |
| 2366 | continue; |
| 2367 | if (Inst == Compare || Inst == Branch) |
| 2368 | continue; |
| 2369 | if (match(Inst, m_Intrinsic<Intrinsic::eh_typeid_for>())) |
| 2370 | continue; |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2371 | return createCleanupHandler(Actions, CleanupHandlerMap, BB); |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 2372 | } |
| 2373 | // The selector dispatch block should always terminate our search. |
| 2374 | assert(BB == EndBB); |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2375 | return; |
| 2376 | } |
| 2377 | |
| 2378 | if (isAsynchronousEHPersonality(Personality)) { |
| 2379 | // If this is a landingpad block, split the block at the first non-landing |
| 2380 | // pad instruction. |
| 2381 | Instruction *MaybeCall = BB->getFirstNonPHIOrDbg(); |
| 2382 | if (LPadMap) { |
| 2383 | while (MaybeCall != BB->getTerminator() && |
| 2384 | LPadMap->isLandingPadSpecificInst(MaybeCall)) |
| 2385 | MaybeCall = MaybeCall->getNextNode(); |
| 2386 | } |
| 2387 | |
| 2388 | // Look for outlined finally calls. |
| 2389 | if (CallSite FinallyCall = matchOutlinedFinallyCall(BB, MaybeCall)) { |
| 2390 | Function *Fin = FinallyCall.getCalledFunction(); |
| 2391 | assert(Fin && "outlined finally call should be direct"); |
| 2392 | auto *Action = new CleanupHandler(BB); |
| 2393 | Action->setHandlerBlockOrFunc(Fin); |
| 2394 | Actions.insertCleanupHandler(Action); |
| 2395 | CleanupHandlerMap[BB] = Action; |
| 2396 | DEBUG(dbgs() << " Found frontend-outlined finally call to " |
| 2397 | << Fin->getName() << " in block " |
| 2398 | << Action->getStartBlock()->getName() << "\n"); |
| 2399 | |
| 2400 | // Split the block if there were more interesting instructions and look |
| 2401 | // for finally calls in the normal successor block. |
| 2402 | BasicBlock *SuccBB = BB; |
| 2403 | if (FinallyCall.getInstruction() != BB->getTerminator() && |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 2404 | FinallyCall.getInstruction()->getNextNode() != |
| 2405 | BB->getTerminator()) { |
| 2406 | SuccBB = |
| 2407 | SplitBlock(BB, FinallyCall.getInstruction()->getNextNode(), DT); |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2408 | } else { |
| 2409 | if (FinallyCall.isInvoke()) { |
Andrew Kaylor | 046f7b4 | 2015-04-28 21:54:14 +0000 | [diff] [blame] | 2410 | SuccBB = |
| 2411 | cast<InvokeInst>(FinallyCall.getInstruction())->getNormalDest(); |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2412 | } else { |
| 2413 | SuccBB = BB->getUniqueSuccessor(); |
Andrew Kaylor | 9130743 | 2015-04-28 22:01:51 +0000 | [diff] [blame] | 2414 | assert(SuccBB && |
| 2415 | "splitOutlinedFinallyCalls didn't insert a branch"); |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2416 | } |
| 2417 | } |
| 2418 | BB = SuccBB; |
| 2419 | if (BB == EndBB) |
| 2420 | return; |
| 2421 | continue; |
| 2422 | } |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 2423 | } |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2424 | |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 2425 | // Anything else is either a catch block or interesting cleanup code. |
Andrew Kaylor | bb11132 | 2015-04-07 21:30:23 +0000 | [diff] [blame] | 2426 | for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end(); |
| 2427 | II != IE; ++II) { |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 2428 | Instruction *Inst = II; |
| 2429 | if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst)) |
| 2430 | continue; |
| 2431 | // Unconditional branches fall through to this loop. |
| 2432 | if (Inst == Branch) |
| 2433 | continue; |
| 2434 | // If this is a catch block, there is no cleanup code to be found. |
| 2435 | if (match(Inst, m_Intrinsic<Intrinsic::eh_begincatch>())) |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2436 | return; |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 2437 | // If this a nested landing pad, it may contain an endcatch call. |
| 2438 | if (match(Inst, m_Intrinsic<Intrinsic::eh_endcatch>())) |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2439 | return; |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 2440 | // Anything else makes this interesting cleanup code. |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2441 | return createCleanupHandler(Actions, CleanupHandlerMap, BB); |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 2442 | } |
| 2443 | |
| 2444 | // Only unconditional branches in empty blocks should get this far. |
| 2445 | assert(Branch && Branch->isUnconditional()); |
| 2446 | if (BB == EndBB) |
Reid Kleckner | 9405ef0 | 2015-04-10 23:12:29 +0000 | [diff] [blame] | 2447 | return; |
Andrew Kaylor | 64622aa | 2015-04-01 17:21:25 +0000 | [diff] [blame] | 2448 | BB = Branch->getSuccessor(0); |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2449 | } |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 2450 | } |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 2451 | |
| 2452 | // This is a public function, declared in WinEHFuncInfo.h and is also |
| 2453 | // referenced by WinEHNumbering in FunctionLoweringInfo.cpp. |
Benjamin Kramer | a48e065 | 2015-05-16 15:40:03 +0000 | [diff] [blame] | 2454 | void llvm::parseEHActions( |
| 2455 | const IntrinsicInst *II, |
| 2456 | SmallVectorImpl<std::unique_ptr<ActionHandler>> &Actions) { |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 2457 | for (unsigned I = 0, E = II->getNumArgOperands(); I != E;) { |
| 2458 | uint64_t ActionKind = |
Andrew Kaylor | bb11132 | 2015-04-07 21:30:23 +0000 | [diff] [blame] | 2459 | cast<ConstantInt>(II->getArgOperand(I))->getZExtValue(); |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 2460 | if (ActionKind == /*catch=*/1) { |
| 2461 | auto *Selector = cast<Constant>(II->getArgOperand(I + 1)); |
| 2462 | ConstantInt *EHObjIndex = cast<ConstantInt>(II->getArgOperand(I + 2)); |
| 2463 | int64_t EHObjIndexVal = EHObjIndex->getSExtValue(); |
| 2464 | Constant *Handler = cast<Constant>(II->getArgOperand(I + 3)); |
| 2465 | I += 4; |
Benjamin Kramer | a48e065 | 2015-05-16 15:40:03 +0000 | [diff] [blame] | 2466 | auto CH = make_unique<CatchHandler>(/*BB=*/nullptr, Selector, |
| 2467 | /*NextBB=*/nullptr); |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 2468 | CH->setHandlerBlockOrFunc(Handler); |
| 2469 | CH->setExceptionVarIndex(EHObjIndexVal); |
Benjamin Kramer | a48e065 | 2015-05-16 15:40:03 +0000 | [diff] [blame] | 2470 | Actions.push_back(std::move(CH)); |
David Majnemer | 69132a7 | 2015-04-03 22:49:05 +0000 | [diff] [blame] | 2471 | } else if (ActionKind == 0) { |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 2472 | Constant *Handler = cast<Constant>(II->getArgOperand(I + 1)); |
| 2473 | I += 2; |
Benjamin Kramer | a48e065 | 2015-05-16 15:40:03 +0000 | [diff] [blame] | 2474 | auto CH = make_unique<CleanupHandler>(/*BB=*/nullptr); |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 2475 | CH->setHandlerBlockOrFunc(Handler); |
Benjamin Kramer | a48e065 | 2015-05-16 15:40:03 +0000 | [diff] [blame] | 2476 | Actions.push_back(std::move(CH)); |
David Majnemer | 69132a7 | 2015-04-03 22:49:05 +0000 | [diff] [blame] | 2477 | } else { |
| 2478 | llvm_unreachable("Expected either a catch or cleanup handler!"); |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 2479 | } |
| 2480 | } |
| 2481 | std::reverse(Actions.begin(), Actions.end()); |
| 2482 | } |