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" |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/MapVector.h" |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/CFG.h" |
David Majnemer | 70497c6 | 2015-12-02 23:06:39 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/EHPersonalities.h" |
Chandler Carruth | e011534 | 2015-12-29 09:24:39 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineBasicBlock.h" |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/WinEHFuncInfo.h" |
Chandler Carruth | e011534 | 2015-12-29 09:24:39 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCSymbol.h" |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 26 | #include "llvm/Pass.h" |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | a8d61b1 | 2015-03-23 18:57:17 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 30 | #include "llvm/Transforms/Utils/Cloning.h" |
| 31 | #include "llvm/Transforms/Utils/Local.h" |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 32 | #include "llvm/Transforms/Utils/SSAUpdater.h" |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 33 | |
| 34 | using namespace llvm; |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 35 | |
| 36 | #define DEBUG_TYPE "winehprepare" |
| 37 | |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 38 | static cl::opt<bool> DisableDemotion( |
| 39 | "disable-demotion", cl::Hidden, |
| 40 | cl::desc( |
| 41 | "Clone multicolor basic blocks but do not demote cross funclet values"), |
| 42 | cl::init(false)); |
| 43 | |
| 44 | static cl::opt<bool> DisableCleanups( |
| 45 | "disable-cleanups", cl::Hidden, |
| 46 | cl::desc("Do not remove implausible terminators or other similar cleanups"), |
| 47 | cl::init(false)); |
| 48 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 49 | namespace { |
Andrew Kaylor | fdd48fa | 2015-11-09 19:59:02 +0000 | [diff] [blame] | 50 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 51 | class WinEHPrepare : public FunctionPass { |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 52 | public: |
| 53 | static char ID; // Pass identification, replacement for typeid. |
David Majnemer | e696583 | 2015-10-16 19:59:52 +0000 | [diff] [blame] | 54 | WinEHPrepare(const TargetMachine *TM = nullptr) : FunctionPass(ID) {} |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 55 | |
| 56 | bool runOnFunction(Function &Fn) override; |
| 57 | |
| 58 | bool doFinalization(Module &M) override; |
| 59 | |
| 60 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 61 | |
| 62 | const char *getPassName() const override { |
| 63 | return "Windows exception handling preparation"; |
| 64 | } |
| 65 | |
| 66 | private: |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 67 | void insertPHIStores(PHINode *OriginalPHI, AllocaInst *SpillSlot); |
| 68 | void |
| 69 | insertPHIStore(BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot, |
| 70 | SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist); |
| 71 | AllocaInst *insertPHILoads(PHINode *PN, Function &F); |
| 72 | void replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot, |
| 73 | DenseMap<BasicBlock *, Value *> &Loads, Function &F); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 74 | bool prepareExplicitEH(Function &F); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 75 | void colorFunclets(Function &F); |
Andrew Kaylor | fdd48fa | 2015-11-09 19:59:02 +0000 | [diff] [blame] | 76 | |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 77 | void demotePHIsOnFunclets(Function &F); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 78 | void cloneCommonBlocks(Function &F); |
David Majnemer | 3bb88c0 | 2015-12-15 21:27:27 +0000 | [diff] [blame] | 79 | void removeImplausibleInstructions(Function &F); |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 80 | void cleanupPreparedFunclets(Function &F); |
| 81 | void verifyPreparedFunclets(Function &F); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 82 | |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 83 | // All fields are reset by runOnFunction. |
Reid Kleckner | 582786b | 2015-04-30 18:17:12 +0000 | [diff] [blame] | 84 | EHPersonality Personality = EHPersonality::Unknown; |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 85 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 86 | DenseMap<BasicBlock *, ColorVector> BlockColors; |
| 87 | MapVector<BasicBlock *, std::vector<BasicBlock *>> FuncletBlocks; |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 90 | } // end anonymous namespace |
| 91 | |
| 92 | char WinEHPrepare::ID = 0; |
Reid Kleckner | 47c8e7a | 2015-03-12 00:36:20 +0000 | [diff] [blame] | 93 | INITIALIZE_TM_PASS(WinEHPrepare, "winehprepare", "Prepare Windows exceptions", |
| 94 | false, false) |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 95 | |
| 96 | FunctionPass *llvm::createWinEHPass(const TargetMachine *TM) { |
| 97 | return new WinEHPrepare(TM); |
| 98 | } |
| 99 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 100 | bool WinEHPrepare::runOnFunction(Function &Fn) { |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 101 | if (!Fn.hasPersonalityFn()) |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 102 | return false; |
| 103 | |
| 104 | // Classify the personality to see what kind of preparation we need. |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 105 | Personality = classifyEHPersonality(Fn.getPersonalityFn()); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 106 | |
Joseph Tremoulet | 2afea54 | 2015-10-06 20:28:16 +0000 | [diff] [blame] | 107 | // Do nothing if this is not a funclet-based personality. |
| 108 | if (!isFuncletEHPersonality(Personality)) |
Reid Kleckner | 47c8e7a | 2015-03-12 00:36:20 +0000 | [diff] [blame] | 109 | return false; |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 110 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 111 | return prepareExplicitEH(Fn); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 114 | bool WinEHPrepare::doFinalization(Module &M) { return false; } |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 115 | |
David Majnemer | e696583 | 2015-10-16 19:59:52 +0000 | [diff] [blame] | 116 | void WinEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const {} |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 117 | |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 118 | static int addUnwindMapEntry(WinEHFuncInfo &FuncInfo, int ToState, |
David Majnemer | bfa5b98 | 2015-10-10 00:04:29 +0000 | [diff] [blame] | 119 | const BasicBlock *BB) { |
Reid Kleckner | 14e7735 | 2015-10-09 23:34:53 +0000 | [diff] [blame] | 120 | CxxUnwindMapEntry UME; |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 121 | UME.ToState = ToState; |
David Majnemer | bfa5b98 | 2015-10-10 00:04:29 +0000 | [diff] [blame] | 122 | UME.Cleanup = BB; |
Reid Kleckner | 14e7735 | 2015-10-09 23:34:53 +0000 | [diff] [blame] | 123 | FuncInfo.CxxUnwindMap.push_back(UME); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 124 | return FuncInfo.getLastStateNumber(); |
| 125 | } |
| 126 | |
| 127 | static void addTryBlockMapEntry(WinEHFuncInfo &FuncInfo, int TryLow, |
| 128 | int TryHigh, int CatchHigh, |
| 129 | ArrayRef<const CatchPadInst *> Handlers) { |
| 130 | WinEHTryBlockMapEntry TBME; |
| 131 | TBME.TryLow = TryLow; |
| 132 | TBME.TryHigh = TryHigh; |
| 133 | TBME.CatchHigh = CatchHigh; |
| 134 | assert(TBME.TryLow <= TBME.TryHigh); |
| 135 | for (const CatchPadInst *CPI : Handlers) { |
| 136 | WinEHHandlerType HT; |
| 137 | Constant *TypeInfo = cast<Constant>(CPI->getArgOperand(0)); |
Reid Kleckner | b005d28 | 2015-09-16 20:16:27 +0000 | [diff] [blame] | 138 | if (TypeInfo->isNullValue()) |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 139 | HT.TypeDescriptor = nullptr; |
Reid Kleckner | b005d28 | 2015-09-16 20:16:27 +0000 | [diff] [blame] | 140 | else |
| 141 | HT.TypeDescriptor = cast<GlobalVariable>(TypeInfo->stripPointerCasts()); |
| 142 | HT.Adjectives = cast<ConstantInt>(CPI->getArgOperand(1))->getZExtValue(); |
David Majnemer | 7735a6d | 2015-10-06 23:31:59 +0000 | [diff] [blame] | 143 | HT.Handler = CPI->getParent(); |
Reid Kleckner | b005d28 | 2015-09-16 20:16:27 +0000 | [diff] [blame] | 144 | if (isa<ConstantPointerNull>(CPI->getArgOperand(2))) |
| 145 | HT.CatchObj.Alloca = nullptr; |
| 146 | else |
| 147 | HT.CatchObj.Alloca = cast<AllocaInst>(CPI->getArgOperand(2)); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 148 | TBME.HandlerArray.push_back(HT); |
| 149 | } |
| 150 | FuncInfo.TryBlockMap.push_back(TBME); |
| 151 | } |
| 152 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 153 | static BasicBlock *getCleanupRetUnwindDest(const CleanupPadInst *CleanupPad) { |
| 154 | for (const User *U : CleanupPad->users()) |
| 155 | if (const auto *CRI = dyn_cast<CleanupReturnInst>(U)) |
| 156 | return CRI->getUnwindDest(); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 157 | return nullptr; |
| 158 | } |
| 159 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 160 | static void calculateStateNumbersForInvokes(const Function *Fn, |
| 161 | WinEHFuncInfo &FuncInfo) { |
| 162 | auto *F = const_cast<Function *>(Fn); |
| 163 | DenseMap<BasicBlock *, ColorVector> BlockColors = colorEHFunclets(*F); |
| 164 | for (BasicBlock &BB : *F) { |
| 165 | auto *II = dyn_cast<InvokeInst>(BB.getTerminator()); |
| 166 | if (!II) |
| 167 | continue; |
| 168 | |
| 169 | auto &BBColors = BlockColors[&BB]; |
David Majnemer | c640f86 | 2015-12-23 03:59:04 +0000 | [diff] [blame] | 170 | assert(BBColors.size() == 1 && "multi-color BB not removed by preparation"); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 171 | BasicBlock *FuncletEntryBB = BBColors.front(); |
| 172 | |
| 173 | BasicBlock *FuncletUnwindDest; |
| 174 | auto *FuncletPad = |
| 175 | dyn_cast<FuncletPadInst>(FuncletEntryBB->getFirstNonPHI()); |
| 176 | assert(FuncletPad || FuncletEntryBB == &Fn->getEntryBlock()); |
| 177 | if (!FuncletPad) |
| 178 | FuncletUnwindDest = nullptr; |
| 179 | else if (auto *CatchPad = dyn_cast<CatchPadInst>(FuncletPad)) |
| 180 | FuncletUnwindDest = CatchPad->getCatchSwitch()->getUnwindDest(); |
| 181 | else if (auto *CleanupPad = dyn_cast<CleanupPadInst>(FuncletPad)) |
| 182 | FuncletUnwindDest = getCleanupRetUnwindDest(CleanupPad); |
| 183 | else |
| 184 | llvm_unreachable("unexpected funclet pad!"); |
| 185 | |
| 186 | BasicBlock *InvokeUnwindDest = II->getUnwindDest(); |
| 187 | int BaseState = -1; |
| 188 | if (FuncletUnwindDest == InvokeUnwindDest) { |
| 189 | auto BaseStateI = FuncInfo.FuncletBaseStateMap.find(FuncletPad); |
| 190 | if (BaseStateI != FuncInfo.FuncletBaseStateMap.end()) |
| 191 | BaseState = BaseStateI->second; |
| 192 | } |
| 193 | |
| 194 | if (BaseState != -1) { |
| 195 | FuncInfo.InvokeStateMap[II] = BaseState; |
| 196 | } else { |
| 197 | Instruction *PadInst = InvokeUnwindDest->getFirstNonPHI(); |
| 198 | assert(FuncInfo.EHPadStateMap.count(PadInst) && "EH Pad has no state!"); |
| 199 | FuncInfo.InvokeStateMap[II] = FuncInfo.EHPadStateMap[PadInst]; |
| 200 | } |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 201 | } |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 202 | } |
| 203 | |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 204 | // Given BB which ends in an unwind edge, return the EHPad that this BB belongs |
| 205 | // to. If the unwind edge came from an invoke, return null. |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 206 | static const BasicBlock *getEHPadFromPredecessor(const BasicBlock *BB, |
| 207 | Value *ParentPad) { |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 208 | const TerminatorInst *TI = BB->getTerminator(); |
| 209 | if (isa<InvokeInst>(TI)) |
| 210 | return nullptr; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 211 | if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(TI)) { |
| 212 | if (CatchSwitch->getParentPad() != ParentPad) |
| 213 | return nullptr; |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 214 | return BB; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 215 | } |
| 216 | assert(!TI->isEHPad() && "unexpected EHPad!"); |
| 217 | auto *CleanupPad = cast<CleanupReturnInst>(TI)->getCleanupPad(); |
| 218 | if (CleanupPad->getParentPad() != ParentPad) |
| 219 | return nullptr; |
| 220 | return CleanupPad->getParent(); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 221 | } |
| 222 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 223 | static void calculateCXXStateNumbers(WinEHFuncInfo &FuncInfo, |
| 224 | const Instruction *FirstNonPHI, |
| 225 | int ParentState) { |
| 226 | const BasicBlock *BB = FirstNonPHI->getParent(); |
| 227 | assert(BB->isEHPad() && "not a funclet!"); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 228 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 229 | if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(FirstNonPHI)) { |
| 230 | assert(FuncInfo.EHPadStateMap.count(CatchSwitch) == 0 && |
| 231 | "shouldn't revist catch funclets!"); |
| 232 | |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 233 | SmallVector<const CatchPadInst *, 2> Handlers; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 234 | for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) { |
| 235 | auto *CatchPad = cast<CatchPadInst>(CatchPadBB->getFirstNonPHI()); |
| 236 | Handlers.push_back(CatchPad); |
| 237 | } |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 238 | int TryLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 239 | FuncInfo.EHPadStateMap[CatchSwitch] = TryLow; |
| 240 | for (const BasicBlock *PredBlock : predecessors(BB)) |
| 241 | if ((PredBlock = getEHPadFromPredecessor(PredBlock, |
| 242 | CatchSwitch->getParentPad()))) |
| 243 | calculateCXXStateNumbers(FuncInfo, PredBlock->getFirstNonPHI(), |
| 244 | TryLow); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 245 | int CatchLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr); |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 246 | |
| 247 | // catchpads are separate funclets in C++ EH due to the way rethrow works. |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 248 | int TryHigh = CatchLow - 1; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 249 | for (const auto *CatchPad : Handlers) { |
| 250 | FuncInfo.FuncletBaseStateMap[CatchPad] = CatchLow; |
| 251 | for (const User *U : CatchPad->users()) { |
| 252 | const auto *UserI = cast<Instruction>(U); |
David Majnemer | c640f86 | 2015-12-23 03:59:04 +0000 | [diff] [blame] | 253 | if (auto *InnerCatchSwitch = dyn_cast<CatchSwitchInst>(UserI)) |
| 254 | if (InnerCatchSwitch->getUnwindDest() == CatchSwitch->getUnwindDest()) |
| 255 | calculateCXXStateNumbers(FuncInfo, UserI, CatchLow); |
| 256 | if (auto *InnerCleanupPad = dyn_cast<CleanupPadInst>(UserI)) |
| 257 | if (getCleanupRetUnwindDest(InnerCleanupPad) == |
| 258 | CatchSwitch->getUnwindDest()) |
| 259 | calculateCXXStateNumbers(FuncInfo, UserI, CatchLow); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 260 | } |
| 261 | } |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 262 | int CatchHigh = FuncInfo.getLastStateNumber(); |
| 263 | addTryBlockMapEntry(FuncInfo, TryLow, TryHigh, CatchHigh, Handlers); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 264 | DEBUG(dbgs() << "TryLow[" << BB->getName() << "]: " << TryLow << '\n'); |
| 265 | DEBUG(dbgs() << "TryHigh[" << BB->getName() << "]: " << TryHigh << '\n'); |
| 266 | DEBUG(dbgs() << "CatchHigh[" << BB->getName() << "]: " << CatchHigh |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 267 | << '\n'); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 268 | } else { |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 269 | auto *CleanupPad = cast<CleanupPadInst>(FirstNonPHI); |
| 270 | |
| 271 | // It's possible for a cleanup to be visited twice: it might have multiple |
| 272 | // cleanupret instructions. |
| 273 | if (FuncInfo.EHPadStateMap.count(CleanupPad)) |
| 274 | return; |
| 275 | |
| 276 | int CleanupState = addUnwindMapEntry(FuncInfo, ParentState, BB); |
| 277 | FuncInfo.EHPadStateMap[CleanupPad] = CleanupState; |
| 278 | DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB " |
| 279 | << BB->getName() << '\n'); |
| 280 | for (const BasicBlock *PredBlock : predecessors(BB)) { |
| 281 | if ((PredBlock = getEHPadFromPredecessor(PredBlock, |
| 282 | CleanupPad->getParentPad()))) { |
| 283 | calculateCXXStateNumbers(FuncInfo, PredBlock->getFirstNonPHI(), |
| 284 | CleanupState); |
| 285 | } |
| 286 | } |
| 287 | for (const User *U : CleanupPad->users()) { |
| 288 | const auto *UserI = cast<Instruction>(U); |
| 289 | if (UserI->isEHPad()) |
| 290 | report_fatal_error("Cleanup funclets for the MSVC++ personality cannot " |
| 291 | "contain exceptional actions"); |
| 292 | } |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 296 | static int addSEHExcept(WinEHFuncInfo &FuncInfo, int ParentState, |
| 297 | const Function *Filter, const BasicBlock *Handler) { |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 298 | SEHUnwindMapEntry Entry; |
| 299 | Entry.ToState = ParentState; |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 300 | Entry.IsFinally = false; |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 301 | Entry.Filter = Filter; |
| 302 | Entry.Handler = Handler; |
| 303 | FuncInfo.SEHUnwindMap.push_back(Entry); |
| 304 | return FuncInfo.SEHUnwindMap.size() - 1; |
| 305 | } |
| 306 | |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 307 | static int addSEHFinally(WinEHFuncInfo &FuncInfo, int ParentState, |
| 308 | const BasicBlock *Handler) { |
| 309 | SEHUnwindMapEntry Entry; |
| 310 | Entry.ToState = ParentState; |
| 311 | Entry.IsFinally = true; |
| 312 | Entry.Filter = nullptr; |
| 313 | Entry.Handler = Handler; |
| 314 | FuncInfo.SEHUnwindMap.push_back(Entry); |
| 315 | return FuncInfo.SEHUnwindMap.size() - 1; |
| 316 | } |
| 317 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 318 | static void calculateSEHStateNumbers(WinEHFuncInfo &FuncInfo, |
| 319 | const Instruction *FirstNonPHI, |
| 320 | int ParentState) { |
| 321 | const BasicBlock *BB = FirstNonPHI->getParent(); |
| 322 | assert(BB->isEHPad() && "no a funclet!"); |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 323 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 324 | if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(FirstNonPHI)) { |
| 325 | assert(FuncInfo.EHPadStateMap.count(CatchSwitch) == 0 && |
| 326 | "shouldn't revist catch funclets!"); |
| 327 | |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 328 | // Extract the filter function and the __except basic block and create a |
| 329 | // state for them. |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 330 | assert(CatchSwitch->getNumHandlers() == 1 && |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 331 | "SEH doesn't have multiple handlers per __try"); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 332 | const auto *CatchPad = |
| 333 | cast<CatchPadInst>((*CatchSwitch->handler_begin())->getFirstNonPHI()); |
| 334 | const BasicBlock *CatchPadBB = CatchPad->getParent(); |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 335 | const Constant *FilterOrNull = |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 336 | cast<Constant>(CatchPad->getArgOperand(0)->stripPointerCasts()); |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 337 | const Function *Filter = dyn_cast<Function>(FilterOrNull); |
| 338 | assert((Filter || FilterOrNull->isNullValue()) && |
| 339 | "unexpected filter value"); |
David Majnemer | 7735a6d | 2015-10-06 23:31:59 +0000 | [diff] [blame] | 340 | int TryState = addSEHExcept(FuncInfo, ParentState, Filter, CatchPadBB); |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 341 | |
| 342 | // Everything in the __try block uses TryState as its parent state. |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 343 | FuncInfo.EHPadStateMap[CatchSwitch] = TryState; |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 344 | DEBUG(dbgs() << "Assigning state #" << TryState << " to BB " |
| 345 | << CatchPadBB->getName() << '\n'); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 346 | for (const BasicBlock *PredBlock : predecessors(BB)) |
| 347 | if ((PredBlock = getEHPadFromPredecessor(PredBlock, |
| 348 | CatchSwitch->getParentPad()))) |
| 349 | calculateSEHStateNumbers(FuncInfo, PredBlock->getFirstNonPHI(), |
| 350 | TryState); |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 351 | |
| 352 | // Everything in the __except block unwinds to ParentState, just like code |
| 353 | // outside the __try. |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 354 | for (const User *U : CatchPad->users()) { |
| 355 | const auto *UserI = cast<Instruction>(U); |
David Majnemer | c640f86 | 2015-12-23 03:59:04 +0000 | [diff] [blame] | 356 | if (auto *InnerCatchSwitch = dyn_cast<CatchSwitchInst>(UserI)) |
| 357 | if (InnerCatchSwitch->getUnwindDest() == CatchSwitch->getUnwindDest()) |
| 358 | calculateSEHStateNumbers(FuncInfo, UserI, ParentState); |
| 359 | if (auto *InnerCleanupPad = dyn_cast<CleanupPadInst>(UserI)) |
| 360 | if (getCleanupRetUnwindDest(InnerCleanupPad) == |
| 361 | CatchSwitch->getUnwindDest()) |
| 362 | calculateSEHStateNumbers(FuncInfo, UserI, ParentState); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 363 | } |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 364 | } else { |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 365 | auto *CleanupPad = cast<CleanupPadInst>(FirstNonPHI); |
| 366 | |
| 367 | // It's possible for a cleanup to be visited twice: it might have multiple |
| 368 | // cleanupret instructions. |
| 369 | if (FuncInfo.EHPadStateMap.count(CleanupPad)) |
| 370 | return; |
| 371 | |
| 372 | int CleanupState = addSEHFinally(FuncInfo, ParentState, BB); |
| 373 | FuncInfo.EHPadStateMap[CleanupPad] = CleanupState; |
| 374 | DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB " |
| 375 | << BB->getName() << '\n'); |
| 376 | for (const BasicBlock *PredBlock : predecessors(BB)) |
| 377 | if ((PredBlock = |
| 378 | getEHPadFromPredecessor(PredBlock, CleanupPad->getParentPad()))) |
| 379 | calculateSEHStateNumbers(FuncInfo, PredBlock->getFirstNonPHI(), |
| 380 | CleanupState); |
| 381 | for (const User *U : CleanupPad->users()) { |
| 382 | const auto *UserI = cast<Instruction>(U); |
| 383 | if (UserI->isEHPad()) |
| 384 | report_fatal_error("Cleanup funclets for the SEH personality cannot " |
| 385 | "contain exceptional actions"); |
| 386 | } |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 390 | static bool isTopLevelPadForMSVC(const Instruction *EHPad) { |
| 391 | if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(EHPad)) |
| 392 | return isa<ConstantTokenNone>(CatchSwitch->getParentPad()) && |
| 393 | CatchSwitch->unwindsToCaller(); |
| 394 | if (auto *CleanupPad = dyn_cast<CleanupPadInst>(EHPad)) |
| 395 | return isa<ConstantTokenNone>(CleanupPad->getParentPad()) && |
| 396 | getCleanupRetUnwindDest(CleanupPad) == nullptr; |
| 397 | if (isa<CatchPadInst>(EHPad)) |
| 398 | return false; |
| 399 | llvm_unreachable("unexpected EHPad!"); |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 402 | void llvm::calculateSEHStateNumbers(const Function *Fn, |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 403 | WinEHFuncInfo &FuncInfo) { |
| 404 | // Don't compute state numbers twice. |
| 405 | if (!FuncInfo.SEHUnwindMap.empty()) |
| 406 | return; |
| 407 | |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 408 | for (const BasicBlock &BB : *Fn) { |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 409 | if (!BB.isEHPad()) |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 410 | continue; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 411 | const Instruction *FirstNonPHI = BB.getFirstNonPHI(); |
| 412 | if (!isTopLevelPadForMSVC(FirstNonPHI)) |
| 413 | continue; |
| 414 | ::calculateSEHStateNumbers(FuncInfo, FirstNonPHI, -1); |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 415 | } |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 416 | |
| 417 | calculateStateNumbersForInvokes(Fn, FuncInfo); |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 420 | void llvm::calculateWinCXXEHStateNumbers(const Function *Fn, |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 421 | WinEHFuncInfo &FuncInfo) { |
| 422 | // Return if it's already been done. |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 423 | if (!FuncInfo.EHPadStateMap.empty()) |
| 424 | return; |
| 425 | |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 426 | for (const BasicBlock &BB : *Fn) { |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 427 | if (!BB.isEHPad()) |
| 428 | continue; |
Joseph Tremoulet | 9ce71f7 | 2015-09-03 09:09:43 +0000 | [diff] [blame] | 429 | const Instruction *FirstNonPHI = BB.getFirstNonPHI(); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 430 | if (!isTopLevelPadForMSVC(FirstNonPHI)) |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 431 | continue; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 432 | calculateCXXStateNumbers(FuncInfo, FirstNonPHI, -1); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 433 | } |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 434 | |
| 435 | calculateStateNumbersForInvokes(Fn, FuncInfo); |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 436 | } |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 437 | |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 438 | static int addClrEHHandler(WinEHFuncInfo &FuncInfo, int ParentState, |
| 439 | ClrHandlerType HandlerType, uint32_t TypeToken, |
| 440 | const BasicBlock *Handler) { |
| 441 | ClrEHUnwindMapEntry Entry; |
| 442 | Entry.Parent = ParentState; |
| 443 | Entry.Handler = Handler; |
| 444 | Entry.HandlerType = HandlerType; |
| 445 | Entry.TypeToken = TypeToken; |
| 446 | FuncInfo.ClrEHUnwindMap.push_back(Entry); |
| 447 | return FuncInfo.ClrEHUnwindMap.size() - 1; |
| 448 | } |
| 449 | |
| 450 | void llvm::calculateClrEHStateNumbers(const Function *Fn, |
| 451 | WinEHFuncInfo &FuncInfo) { |
| 452 | // Return if it's already been done. |
| 453 | if (!FuncInfo.EHPadStateMap.empty()) |
| 454 | return; |
| 455 | |
| 456 | SmallVector<std::pair<const Instruction *, int>, 8> Worklist; |
| 457 | |
| 458 | // Each pad needs to be able to refer to its parent, so scan the function |
| 459 | // looking for top-level handlers and seed the worklist with them. |
| 460 | for (const BasicBlock &BB : *Fn) { |
| 461 | if (!BB.isEHPad()) |
| 462 | continue; |
| 463 | if (BB.isLandingPad()) |
| 464 | report_fatal_error("CoreCLR EH cannot use landingpads"); |
| 465 | const Instruction *FirstNonPHI = BB.getFirstNonPHI(); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 466 | if (!isTopLevelPadForMSVC(FirstNonPHI)) |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 467 | continue; |
| 468 | // queue this with sentinel parent state -1 to mean unwind to caller. |
| 469 | Worklist.emplace_back(FirstNonPHI, -1); |
| 470 | } |
| 471 | |
| 472 | while (!Worklist.empty()) { |
| 473 | const Instruction *Pad; |
| 474 | int ParentState; |
| 475 | std::tie(Pad, ParentState) = Worklist.pop_back_val(); |
| 476 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 477 | Value *ParentPad; |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 478 | int PredState; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 479 | if (const CleanupPadInst *Cleanup = dyn_cast<CleanupPadInst>(Pad)) { |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 480 | // A cleanup can have multiple exits; don't re-process after the first. |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 481 | if (FuncInfo.EHPadStateMap.count(Cleanup)) |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 482 | continue; |
| 483 | // CoreCLR personality uses arity to distinguish faults from finallies. |
| 484 | const BasicBlock *PadBlock = Cleanup->getParent(); |
| 485 | ClrHandlerType HandlerType = |
| 486 | (Cleanup->getNumOperands() ? ClrHandlerType::Fault |
| 487 | : ClrHandlerType::Finally); |
| 488 | int NewState = |
| 489 | addClrEHHandler(FuncInfo, ParentState, HandlerType, 0, PadBlock); |
| 490 | FuncInfo.EHPadStateMap[Cleanup] = NewState; |
| 491 | // Propagate the new state to all preds of the cleanup |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 492 | ParentPad = Cleanup->getParentPad(); |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 493 | PredState = NewState; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 494 | } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Pad)) { |
| 495 | SmallVector<const CatchPadInst *, 1> Handlers; |
| 496 | for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) { |
| 497 | const auto *Catch = cast<CatchPadInst>(CatchPadBB->getFirstNonPHI()); |
| 498 | Handlers.push_back(Catch); |
| 499 | } |
| 500 | FuncInfo.EHPadStateMap[CatchSwitch] = ParentState; |
| 501 | int NewState = ParentState; |
| 502 | for (auto HandlerI = Handlers.rbegin(), HandlerE = Handlers.rend(); |
| 503 | HandlerI != HandlerE; ++HandlerI) { |
| 504 | const CatchPadInst *Catch = *HandlerI; |
| 505 | const BasicBlock *PadBlock = Catch->getParent(); |
| 506 | uint32_t TypeToken = static_cast<uint32_t>( |
| 507 | cast<ConstantInt>(Catch->getArgOperand(0))->getZExtValue()); |
| 508 | NewState = addClrEHHandler(FuncInfo, NewState, ClrHandlerType::Catch, |
| 509 | TypeToken, PadBlock); |
| 510 | FuncInfo.EHPadStateMap[Catch] = NewState; |
| 511 | } |
| 512 | for (const auto *CatchPad : Handlers) { |
| 513 | for (const User *U : CatchPad->users()) { |
| 514 | const auto *UserI = cast<Instruction>(U); |
| 515 | if (UserI->isEHPad()) |
| 516 | Worklist.emplace_back(UserI, ParentState); |
| 517 | } |
| 518 | } |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 519 | PredState = NewState; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 520 | ParentPad = CatchSwitch->getParentPad(); |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 521 | } else { |
| 522 | llvm_unreachable("Unexpected EH pad"); |
| 523 | } |
| 524 | |
| 525 | // Queue all predecessors with the given state |
| 526 | for (const BasicBlock *Pred : predecessors(Pad->getParent())) { |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 527 | if ((Pred = getEHPadFromPredecessor(Pred, ParentPad))) |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 528 | Worklist.emplace_back(Pred->getFirstNonPHI(), PredState); |
| 529 | } |
| 530 | } |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 531 | |
| 532 | calculateStateNumbersForInvokes(Fn, FuncInfo); |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 533 | } |
| 534 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 535 | void WinEHPrepare::colorFunclets(Function &F) { |
| 536 | BlockColors = colorEHFunclets(F); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 537 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 538 | // Invert the map from BB to colors to color to BBs. |
| 539 | for (BasicBlock &BB : F) { |
| 540 | ColorVector &Colors = BlockColors[&BB]; |
| 541 | for (BasicBlock *Color : Colors) |
| 542 | FuncletBlocks[Color].push_back(&BB); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
David Majnemer | f828a0c | 2015-10-01 18:44:59 +0000 | [diff] [blame] | 546 | void llvm::calculateCatchReturnSuccessorColors(const Function *Fn, |
| 547 | WinEHFuncInfo &FuncInfo) { |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 548 | for (const BasicBlock &BB : *Fn) { |
| 549 | const auto *CatchRet = dyn_cast<CatchReturnInst>(BB.getTerminator()); |
| 550 | if (!CatchRet) |
David Majnemer | f828a0c | 2015-10-01 18:44:59 +0000 | [diff] [blame] | 551 | continue; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 552 | // A 'catchret' returns to the outer scope's color. |
| 553 | Value *ParentPad = CatchRet->getParentPad(); |
| 554 | const BasicBlock *Color; |
| 555 | if (isa<ConstantTokenNone>(ParentPad)) |
| 556 | Color = &Fn->getEntryBlock(); |
| 557 | else |
| 558 | Color = cast<Instruction>(ParentPad)->getParent(); |
| 559 | // Record the catchret successor's funclet membership. |
| 560 | FuncInfo.CatchRetSuccessorColorMap[CatchRet] = Color; |
David Majnemer | f828a0c | 2015-10-01 18:44:59 +0000 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 564 | void WinEHPrepare::demotePHIsOnFunclets(Function &F) { |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 565 | // Strip PHI nodes off of EH pads. |
| 566 | SmallVector<PHINode *, 16> PHINodes; |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 567 | for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) { |
Duncan P. N. Exon Smith | f1ff53e | 2015-10-09 22:56:24 +0000 | [diff] [blame] | 568 | BasicBlock *BB = &*FI++; |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 569 | if (!BB->isEHPad()) |
| 570 | continue; |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 571 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) { |
Duncan P. N. Exon Smith | f1ff53e | 2015-10-09 22:56:24 +0000 | [diff] [blame] | 572 | Instruction *I = &*BI++; |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 573 | auto *PN = dyn_cast<PHINode>(I); |
| 574 | // Stop at the first non-PHI. |
| 575 | if (!PN) |
| 576 | break; |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 577 | |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 578 | AllocaInst *SpillSlot = insertPHILoads(PN, F); |
| 579 | if (SpillSlot) |
| 580 | insertPHIStores(PN, SpillSlot); |
| 581 | |
| 582 | PHINodes.push_back(PN); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 586 | for (auto *PN : PHINodes) { |
| 587 | // There may be lingering uses on other EH PHIs being removed |
| 588 | PN->replaceAllUsesWith(UndefValue::get(PN->getType())); |
| 589 | PN->eraseFromParent(); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 590 | } |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 591 | } |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 592 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 593 | void WinEHPrepare::cloneCommonBlocks(Function &F) { |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 594 | // We need to clone all blocks which belong to multiple funclets. Values are |
| 595 | // remapped throughout the funclet to propogate both the new instructions |
| 596 | // *and* the new basic blocks themselves. |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 597 | for (auto &Funclets : FuncletBlocks) { |
| 598 | BasicBlock *FuncletPadBB = Funclets.first; |
| 599 | std::vector<BasicBlock *> &BlocksInFunclet = Funclets.second; |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 600 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 601 | std::vector<std::pair<BasicBlock *, BasicBlock *>> Orig2Clone; |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 602 | ValueToValueMapTy VMap; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 603 | for (BasicBlock *BB : BlocksInFunclet) { |
| 604 | ColorVector &ColorsForBB = BlockColors[BB]; |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 605 | // We don't need to do anything if the block is monochromatic. |
| 606 | size_t NumColorsForBB = ColorsForBB.size(); |
| 607 | if (NumColorsForBB == 1) |
| 608 | continue; |
| 609 | |
Andrew Kaylor | fdd48fa | 2015-11-09 19:59:02 +0000 | [diff] [blame] | 610 | DEBUG_WITH_TYPE("winehprepare-coloring", |
| 611 | dbgs() << " Cloning block \'" << BB->getName() |
| 612 | << "\' for funclet \'" << FuncletPadBB->getName() |
| 613 | << "\'.\n"); |
| 614 | |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 615 | // Create a new basic block and copy instructions into it! |
Joseph Tremoulet | ec18285 | 2015-08-28 01:12:35 +0000 | [diff] [blame] | 616 | BasicBlock *CBB = |
| 617 | CloneBasicBlock(BB, VMap, Twine(".for.", FuncletPadBB->getName())); |
| 618 | // Insert the clone immediately after the original to ensure determinism |
| 619 | // and to keep the same relative ordering of any funclet's blocks. |
| 620 | CBB->insertInto(&F, BB->getNextNode()); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 621 | |
| 622 | // Add basic block mapping. |
| 623 | VMap[BB] = CBB; |
| 624 | |
| 625 | // Record delta operations that we need to perform to our color mappings. |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 626 | Orig2Clone.emplace_back(BB, CBB); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Joseph Tremoulet | 39234fc | 2015-10-07 19:29:56 +0000 | [diff] [blame] | 629 | // If nothing was cloned, we're done cloning in this funclet. |
| 630 | if (Orig2Clone.empty()) |
| 631 | continue; |
| 632 | |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 633 | // Update our color mappings to reflect that one block has lost a color and |
| 634 | // another has gained a color. |
| 635 | for (auto &BBMapping : Orig2Clone) { |
| 636 | BasicBlock *OldBlock = BBMapping.first; |
| 637 | BasicBlock *NewBlock = BBMapping.second; |
| 638 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 639 | BlocksInFunclet.push_back(NewBlock); |
| 640 | ColorVector &NewColors = BlockColors[NewBlock]; |
| 641 | assert(NewColors.empty() && "A new block should only have one color!"); |
| 642 | NewColors.push_back(FuncletPadBB); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 643 | |
Andrew Kaylor | fdd48fa | 2015-11-09 19:59:02 +0000 | [diff] [blame] | 644 | DEBUG_WITH_TYPE("winehprepare-coloring", |
| 645 | dbgs() << " Assigned color \'" << FuncletPadBB->getName() |
| 646 | << "\' to block \'" << NewBlock->getName() |
| 647 | << "\'.\n"); |
| 648 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 649 | BlocksInFunclet.erase( |
| 650 | std::remove(BlocksInFunclet.begin(), BlocksInFunclet.end(), OldBlock), |
| 651 | BlocksInFunclet.end()); |
| 652 | ColorVector &OldColors = BlockColors[OldBlock]; |
| 653 | OldColors.erase( |
| 654 | std::remove(OldColors.begin(), OldColors.end(), FuncletPadBB), |
| 655 | OldColors.end()); |
Andrew Kaylor | fdd48fa | 2015-11-09 19:59:02 +0000 | [diff] [blame] | 656 | |
| 657 | DEBUG_WITH_TYPE("winehprepare-coloring", |
| 658 | dbgs() << " Removed color \'" << FuncletPadBB->getName() |
| 659 | << "\' from block \'" << OldBlock->getName() |
| 660 | << "\'.\n"); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Joseph Tremoulet | 39234fc | 2015-10-07 19:29:56 +0000 | [diff] [blame] | 663 | // Loop over all of the instructions in this funclet, fixing up operand |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 664 | // references as we go. This uses VMap to do all the hard work. |
| 665 | for (BasicBlock *BB : BlocksInFunclet) |
| 666 | // Loop over all instructions, fixing each one as we find it... |
| 667 | for (Instruction &I : *BB) |
Joseph Tremoulet | 39234fc | 2015-10-07 19:29:56 +0000 | [diff] [blame] | 668 | RemapInstruction(&I, VMap, |
| 669 | RF_IgnoreMissingEntries | RF_NoModuleLevelChanges); |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 670 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 671 | auto UpdatePHIOnClonedBlock = [&](PHINode *PN, bool IsForOldBlock) { |
| 672 | unsigned NumPreds = PN->getNumIncomingValues(); |
| 673 | for (unsigned PredIdx = 0, PredEnd = NumPreds; PredIdx != PredEnd; |
| 674 | ++PredIdx) { |
| 675 | BasicBlock *IncomingBlock = PN->getIncomingBlock(PredIdx); |
| 676 | ColorVector &IncomingColors = BlockColors[IncomingBlock]; |
| 677 | bool BlockInFunclet = IncomingColors.size() == 1 && |
| 678 | IncomingColors.front() == FuncletPadBB; |
| 679 | if (IsForOldBlock != BlockInFunclet) |
| 680 | continue; |
| 681 | PN->removeIncomingValue(IncomingBlock, /*DeletePHIIfEmpty=*/false); |
| 682 | // Revisit the next entry. |
| 683 | --PredIdx; |
| 684 | --PredEnd; |
| 685 | } |
| 686 | }; |
| 687 | |
| 688 | for (auto &BBMapping : Orig2Clone) { |
| 689 | BasicBlock *OldBlock = BBMapping.first; |
| 690 | BasicBlock *NewBlock = BBMapping.second; |
| 691 | for (Instruction &OldI : *OldBlock) { |
| 692 | auto *OldPN = dyn_cast<PHINode>(&OldI); |
| 693 | if (!OldPN) |
| 694 | break; |
| 695 | UpdatePHIOnClonedBlock(OldPN, /*IsForOldBlock=*/true); |
| 696 | } |
| 697 | for (Instruction &NewI : *NewBlock) { |
| 698 | auto *NewPN = dyn_cast<PHINode>(&NewI); |
| 699 | if (!NewPN) |
| 700 | break; |
| 701 | UpdatePHIOnClonedBlock(NewPN, /*IsForOldBlock=*/false); |
| 702 | } |
| 703 | } |
| 704 | |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 705 | // Check to see if SuccBB has PHI nodes. If so, we need to add entries to |
| 706 | // the PHI nodes for NewBB now. |
| 707 | for (auto &BBMapping : Orig2Clone) { |
| 708 | BasicBlock *OldBlock = BBMapping.first; |
| 709 | BasicBlock *NewBlock = BBMapping.second; |
| 710 | for (BasicBlock *SuccBB : successors(NewBlock)) { |
| 711 | for (Instruction &SuccI : *SuccBB) { |
| 712 | auto *SuccPN = dyn_cast<PHINode>(&SuccI); |
| 713 | if (!SuccPN) |
| 714 | break; |
| 715 | |
| 716 | // Ok, we have a PHI node. Figure out what the incoming value was for |
| 717 | // the OldBlock. |
| 718 | int OldBlockIdx = SuccPN->getBasicBlockIndex(OldBlock); |
| 719 | if (OldBlockIdx == -1) |
| 720 | break; |
| 721 | Value *IV = SuccPN->getIncomingValue(OldBlockIdx); |
| 722 | |
| 723 | // Remap the value if necessary. |
| 724 | if (auto *Inst = dyn_cast<Instruction>(IV)) { |
| 725 | ValueToValueMapTy::iterator I = VMap.find(Inst); |
| 726 | if (I != VMap.end()) |
| 727 | IV = I->second; |
| 728 | } |
| 729 | |
| 730 | SuccPN->addIncoming(IV, NewBlock); |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | for (ValueToValueMapTy::value_type VT : VMap) { |
| 736 | // If there were values defined in BB that are used outside the funclet, |
| 737 | // then we now have to update all uses of the value to use either the |
| 738 | // original value, the cloned value, or some PHI derived value. This can |
| 739 | // require arbitrary PHI insertion, of which we are prepared to do, clean |
| 740 | // these up now. |
| 741 | SmallVector<Use *, 16> UsesToRename; |
| 742 | |
| 743 | auto *OldI = dyn_cast<Instruction>(const_cast<Value *>(VT.first)); |
| 744 | if (!OldI) |
| 745 | continue; |
| 746 | auto *NewI = cast<Instruction>(VT.second); |
| 747 | // Scan all uses of this instruction to see if it is used outside of its |
| 748 | // funclet, and if so, record them in UsesToRename. |
| 749 | for (Use &U : OldI->uses()) { |
| 750 | Instruction *UserI = cast<Instruction>(U.getUser()); |
| 751 | BasicBlock *UserBB = UserI->getParent(); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 752 | ColorVector &ColorsForUserBB = BlockColors[UserBB]; |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 753 | assert(!ColorsForUserBB.empty()); |
| 754 | if (ColorsForUserBB.size() > 1 || |
| 755 | *ColorsForUserBB.begin() != FuncletPadBB) |
| 756 | UsesToRename.push_back(&U); |
| 757 | } |
| 758 | |
| 759 | // If there are no uses outside the block, we're done with this |
| 760 | // instruction. |
| 761 | if (UsesToRename.empty()) |
| 762 | continue; |
| 763 | |
| 764 | // We found a use of OldI outside of the funclet. Rename all uses of OldI |
| 765 | // that are outside its funclet to be uses of the appropriate PHI node |
| 766 | // etc. |
| 767 | SSAUpdater SSAUpdate; |
| 768 | SSAUpdate.Initialize(OldI->getType(), OldI->getName()); |
| 769 | SSAUpdate.AddAvailableValue(OldI->getParent(), OldI); |
| 770 | SSAUpdate.AddAvailableValue(NewI->getParent(), NewI); |
| 771 | |
| 772 | while (!UsesToRename.empty()) |
| 773 | SSAUpdate.RewriteUseAfterInsertions(*UsesToRename.pop_back_val()); |
| 774 | } |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 775 | } |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 776 | } |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 777 | |
David Majnemer | 3bb88c0 | 2015-12-15 21:27:27 +0000 | [diff] [blame] | 778 | void WinEHPrepare::removeImplausibleInstructions(Function &F) { |
David Majnemer | 83f4bb2 | 2015-08-17 20:56:39 +0000 | [diff] [blame] | 779 | // Remove implausible terminators and replace them with UnreachableInst. |
| 780 | for (auto &Funclet : FuncletBlocks) { |
| 781 | BasicBlock *FuncletPadBB = Funclet.first; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 782 | std::vector<BasicBlock *> &BlocksInFunclet = Funclet.second; |
David Majnemer | 3bb88c0 | 2015-12-15 21:27:27 +0000 | [diff] [blame] | 783 | Instruction *FirstNonPHI = FuncletPadBB->getFirstNonPHI(); |
| 784 | auto *FuncletPad = dyn_cast<FuncletPadInst>(FirstNonPHI); |
| 785 | auto *CatchPad = dyn_cast_or_null<CatchPadInst>(FuncletPad); |
| 786 | auto *CleanupPad = dyn_cast_or_null<CleanupPadInst>(FuncletPad); |
David Majnemer | 83f4bb2 | 2015-08-17 20:56:39 +0000 | [diff] [blame] | 787 | |
| 788 | for (BasicBlock *BB : BlocksInFunclet) { |
David Majnemer | 3bb88c0 | 2015-12-15 21:27:27 +0000 | [diff] [blame] | 789 | for (Instruction &I : *BB) { |
| 790 | CallSite CS(&I); |
| 791 | if (!CS) |
| 792 | continue; |
| 793 | |
| 794 | Value *FuncletBundleOperand = nullptr; |
| 795 | if (auto BU = CS.getOperandBundle(LLVMContext::OB_funclet)) |
| 796 | FuncletBundleOperand = BU->Inputs.front(); |
| 797 | |
| 798 | if (FuncletBundleOperand == FuncletPad) |
| 799 | continue; |
| 800 | |
| 801 | // Skip call sites which are nounwind intrinsics. |
| 802 | auto *CalledFn = |
| 803 | dyn_cast<Function>(CS.getCalledValue()->stripPointerCasts()); |
| 804 | if (CalledFn && CalledFn->isIntrinsic() && CS.doesNotThrow()) |
| 805 | continue; |
| 806 | |
| 807 | // This call site was not part of this funclet, remove it. |
| 808 | if (CS.isInvoke()) { |
| 809 | // Remove the unwind edge if it was an invoke. |
| 810 | removeUnwindEdge(BB); |
| 811 | // Get a pointer to the new call. |
| 812 | BasicBlock::iterator CallI = |
| 813 | std::prev(BB->getTerminator()->getIterator()); |
| 814 | auto *CI = cast<CallInst>(&*CallI); |
| 815 | changeToUnreachable(CI, /*UseLLVMTrap=*/false); |
| 816 | } else { |
| 817 | changeToUnreachable(&I, /*UseLLVMTrap=*/false); |
| 818 | } |
| 819 | |
| 820 | // There are no more instructions in the block (except for unreachable), |
| 821 | // we are done. |
| 822 | break; |
| 823 | } |
| 824 | |
David Majnemer | 83f4bb2 | 2015-08-17 20:56:39 +0000 | [diff] [blame] | 825 | TerminatorInst *TI = BB->getTerminator(); |
| 826 | // CatchPadInst and CleanupPadInst can't transfer control to a ReturnInst. |
David Majnemer | 3bb88c0 | 2015-12-15 21:27:27 +0000 | [diff] [blame] | 827 | bool IsUnreachableRet = isa<ReturnInst>(TI) && FuncletPad; |
David Majnemer | 83f4bb2 | 2015-08-17 20:56:39 +0000 | [diff] [blame] | 828 | // The token consumed by a CatchReturnInst must match the funclet token. |
| 829 | bool IsUnreachableCatchret = false; |
| 830 | if (auto *CRI = dyn_cast<CatchReturnInst>(TI)) |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 831 | IsUnreachableCatchret = CRI->getCatchPad() != CatchPad; |
Joseph Tremoulet | 9ce71f7 | 2015-09-03 09:09:43 +0000 | [diff] [blame] | 832 | // The token consumed by a CleanupReturnInst must match the funclet token. |
David Majnemer | 83f4bb2 | 2015-08-17 20:56:39 +0000 | [diff] [blame] | 833 | bool IsUnreachableCleanupret = false; |
| 834 | if (auto *CRI = dyn_cast<CleanupReturnInst>(TI)) |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 835 | IsUnreachableCleanupret = CRI->getCleanupPad() != CleanupPad; |
Joseph Tremoulet | 9ce71f7 | 2015-09-03 09:09:43 +0000 | [diff] [blame] | 836 | if (IsUnreachableRet || IsUnreachableCatchret || |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 837 | IsUnreachableCleanupret) { |
David Majnemer | 3bb88c0 | 2015-12-15 21:27:27 +0000 | [diff] [blame] | 838 | changeToUnreachable(TI, /*UseLLVMTrap=*/false); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 839 | } else if (isa<InvokeInst>(TI)) { |
David Majnemer | 3bb88c0 | 2015-12-15 21:27:27 +0000 | [diff] [blame] | 840 | if (Personality == EHPersonality::MSVC_CXX && CleanupPad) { |
| 841 | // Invokes within a cleanuppad for the MSVC++ personality never |
| 842 | // transfer control to their unwind edge: the personality will |
| 843 | // terminate the program. |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 844 | removeUnwindEdge(BB); |
David Majnemer | 3bb88c0 | 2015-12-15 21:27:27 +0000 | [diff] [blame] | 845 | } |
David Majnemer | 83f4bb2 | 2015-08-17 20:56:39 +0000 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | } |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 849 | } |
David Majnemer | 83f4bb2 | 2015-08-17 20:56:39 +0000 | [diff] [blame] | 850 | |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 851 | void WinEHPrepare::cleanupPreparedFunclets(Function &F) { |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 852 | // Clean-up some of the mess we made by removing useles PHI nodes, trivial |
| 853 | // branches, etc. |
| 854 | for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) { |
Duncan P. N. Exon Smith | f1ff53e | 2015-10-09 22:56:24 +0000 | [diff] [blame] | 855 | BasicBlock *BB = &*FI++; |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 856 | SimplifyInstructionsInBlock(BB); |
| 857 | ConstantFoldTerminator(BB, /*DeleteDeadConditions=*/true); |
| 858 | MergeBlockIntoPredecessor(BB); |
| 859 | } |
| 860 | |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 861 | // We might have some unreachable blocks after cleaning up some impossible |
| 862 | // control flow. |
| 863 | removeUnreachableBlocks(F); |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 864 | } |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 865 | |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 866 | void WinEHPrepare::verifyPreparedFunclets(Function &F) { |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 867 | // Recolor the CFG to verify that all is well. |
| 868 | for (BasicBlock &BB : F) { |
| 869 | size_t NumColors = BlockColors[&BB].size(); |
| 870 | assert(NumColors == 1 && "Expected monochromatic BB!"); |
| 871 | if (NumColors == 0) |
| 872 | report_fatal_error("Uncolored BB!"); |
| 873 | if (NumColors > 1) |
| 874 | report_fatal_error("Multicolor BB!"); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 875 | if (!DisableDemotion) { |
| 876 | bool EHPadHasPHI = BB.isEHPad() && isa<PHINode>(BB.begin()); |
| 877 | assert(!EHPadHasPHI && "EH Pad still has a PHI!"); |
| 878 | if (EHPadHasPHI) |
| 879 | report_fatal_error("EH Pad still has a PHI!"); |
| 880 | } |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 881 | } |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 882 | } |
| 883 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 884 | bool WinEHPrepare::prepareExplicitEH(Function &F) { |
| 885 | // Remove unreachable blocks. It is not valuable to assign them a color and |
| 886 | // their existence can trick us into thinking values are alive when they are |
| 887 | // not. |
| 888 | removeUnreachableBlocks(F); |
| 889 | |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 890 | // Determine which blocks are reachable from which funclet entries. |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 891 | colorFunclets(F); |
| 892 | |
| 893 | cloneCommonBlocks(F); |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 894 | |
Reid Kleckner | cc2f6c3 | 2015-11-19 23:23:33 +0000 | [diff] [blame] | 895 | if (!DisableDemotion) |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 896 | demotePHIsOnFunclets(F); |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 897 | |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 898 | if (!DisableCleanups) { |
David Majnemer | 3bb88c0 | 2015-12-15 21:27:27 +0000 | [diff] [blame] | 899 | removeImplausibleInstructions(F); |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 900 | |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 901 | cleanupPreparedFunclets(F); |
| 902 | } |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 903 | |
| 904 | verifyPreparedFunclets(F); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 905 | |
| 906 | BlockColors.clear(); |
| 907 | FuncletBlocks.clear(); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 908 | |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 909 | return true; |
| 910 | } |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 911 | |
| 912 | // TODO: Share loads when one use dominates another, or when a catchpad exit |
| 913 | // dominates uses (needs dominators). |
| 914 | AllocaInst *WinEHPrepare::insertPHILoads(PHINode *PN, Function &F) { |
| 915 | BasicBlock *PHIBlock = PN->getParent(); |
| 916 | AllocaInst *SpillSlot = nullptr; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 917 | Instruction *EHPad = PHIBlock->getFirstNonPHI(); |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 918 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 919 | if (!isa<TerminatorInst>(EHPad)) { |
| 920 | // If the EHPad isn't a terminator, then we can insert a load in this block |
| 921 | // that will dominate all uses. |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 922 | SpillSlot = new AllocaInst(PN->getType(), nullptr, |
| 923 | Twine(PN->getName(), ".wineh.spillslot"), |
Duncan P. N. Exon Smith | f1ff53e | 2015-10-09 22:56:24 +0000 | [diff] [blame] | 924 | &F.getEntryBlock().front()); |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 925 | Value *V = new LoadInst(SpillSlot, Twine(PN->getName(), ".wineh.reload"), |
Duncan P. N. Exon Smith | f1ff53e | 2015-10-09 22:56:24 +0000 | [diff] [blame] | 926 | &*PHIBlock->getFirstInsertionPt()); |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 927 | PN->replaceAllUsesWith(V); |
| 928 | return SpillSlot; |
| 929 | } |
| 930 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 931 | // Otherwise, we have a PHI on a terminator EHPad, and we give up and insert |
| 932 | // loads of the slot before every use. |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 933 | DenseMap<BasicBlock *, Value *> Loads; |
| 934 | for (Value::use_iterator UI = PN->use_begin(), UE = PN->use_end(); |
| 935 | UI != UE;) { |
| 936 | Use &U = *UI++; |
| 937 | auto *UsingInst = cast<Instruction>(U.getUser()); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 938 | if (isa<PHINode>(UsingInst) && UsingInst->getParent()->isEHPad()) { |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 939 | // Use is on an EH pad phi. Leave it alone; we'll insert loads and |
| 940 | // stores for it separately. |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 941 | continue; |
| 942 | } |
| 943 | replaceUseWithLoad(PN, U, SpillSlot, Loads, F); |
| 944 | } |
| 945 | return SpillSlot; |
| 946 | } |
| 947 | |
| 948 | // TODO: improve store placement. Inserting at def is probably good, but need |
| 949 | // to be careful not to introduce interfering stores (needs liveness analysis). |
| 950 | // TODO: identify related phi nodes that can share spill slots, and share them |
| 951 | // (also needs liveness). |
| 952 | void WinEHPrepare::insertPHIStores(PHINode *OriginalPHI, |
| 953 | AllocaInst *SpillSlot) { |
| 954 | // Use a worklist of (Block, Value) pairs -- the given Value needs to be |
| 955 | // stored to the spill slot by the end of the given Block. |
| 956 | SmallVector<std::pair<BasicBlock *, Value *>, 4> Worklist; |
| 957 | |
| 958 | Worklist.push_back({OriginalPHI->getParent(), OriginalPHI}); |
| 959 | |
| 960 | while (!Worklist.empty()) { |
| 961 | BasicBlock *EHBlock; |
| 962 | Value *InVal; |
| 963 | std::tie(EHBlock, InVal) = Worklist.pop_back_val(); |
| 964 | |
| 965 | PHINode *PN = dyn_cast<PHINode>(InVal); |
| 966 | if (PN && PN->getParent() == EHBlock) { |
| 967 | // The value is defined by another PHI we need to remove, with no room to |
| 968 | // insert a store after the PHI, so each predecessor needs to store its |
| 969 | // incoming value. |
| 970 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i) { |
| 971 | Value *PredVal = PN->getIncomingValue(i); |
| 972 | |
| 973 | // Undef can safely be skipped. |
| 974 | if (isa<UndefValue>(PredVal)) |
| 975 | continue; |
| 976 | |
| 977 | insertPHIStore(PN->getIncomingBlock(i), PredVal, SpillSlot, Worklist); |
| 978 | } |
| 979 | } else { |
| 980 | // We need to store InVal, which dominates EHBlock, but can't put a store |
| 981 | // in EHBlock, so need to put stores in each predecessor. |
| 982 | for (BasicBlock *PredBlock : predecessors(EHBlock)) { |
| 983 | insertPHIStore(PredBlock, InVal, SpillSlot, Worklist); |
| 984 | } |
| 985 | } |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | void WinEHPrepare::insertPHIStore( |
| 990 | BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot, |
| 991 | SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist) { |
| 992 | |
| 993 | if (PredBlock->isEHPad() && |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 994 | isa<TerminatorInst>(PredBlock->getFirstNonPHI())) { |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 995 | // Pred is unsplittable, so we need to queue it on the worklist. |
| 996 | Worklist.push_back({PredBlock, PredVal}); |
| 997 | return; |
| 998 | } |
| 999 | |
| 1000 | // Otherwise, insert the store at the end of the basic block. |
| 1001 | new StoreInst(PredVal, SpillSlot, PredBlock->getTerminator()); |
| 1002 | } |
| 1003 | |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 1004 | void WinEHPrepare::replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot, |
| 1005 | DenseMap<BasicBlock *, Value *> &Loads, |
| 1006 | Function &F) { |
| 1007 | // Lazilly create the spill slot. |
| 1008 | if (!SpillSlot) |
| 1009 | SpillSlot = new AllocaInst(V->getType(), nullptr, |
| 1010 | Twine(V->getName(), ".wineh.spillslot"), |
Duncan P. N. Exon Smith | f1ff53e | 2015-10-09 22:56:24 +0000 | [diff] [blame] | 1011 | &F.getEntryBlock().front()); |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 1012 | |
| 1013 | auto *UsingInst = cast<Instruction>(U.getUser()); |
| 1014 | if (auto *UsingPHI = dyn_cast<PHINode>(UsingInst)) { |
| 1015 | // If this is a PHI node, we can't insert a load of the value before |
| 1016 | // the use. Instead insert the load in the predecessor block |
| 1017 | // corresponding to the incoming value. |
| 1018 | // |
| 1019 | // Note that if there are multiple edges from a basic block to this |
| 1020 | // PHI node that we cannot have multiple loads. The problem is that |
| 1021 | // the resulting PHI node will have multiple values (from each load) |
| 1022 | // coming in from the same block, which is illegal SSA form. |
| 1023 | // For this reason, we keep track of and reuse loads we insert. |
| 1024 | BasicBlock *IncomingBlock = UsingPHI->getIncomingBlock(U); |
Joseph Tremoulet | 7031c9f | 2015-08-17 13:51:37 +0000 | [diff] [blame] | 1025 | if (auto *CatchRet = |
| 1026 | dyn_cast<CatchReturnInst>(IncomingBlock->getTerminator())) { |
| 1027 | // Putting a load above a catchret and use on the phi would still leave |
| 1028 | // a cross-funclet def/use. We need to split the edge, change the |
| 1029 | // catchret to target the new block, and put the load there. |
| 1030 | BasicBlock *PHIBlock = UsingInst->getParent(); |
| 1031 | BasicBlock *NewBlock = SplitEdge(IncomingBlock, PHIBlock); |
| 1032 | // SplitEdge gives us: |
| 1033 | // IncomingBlock: |
| 1034 | // ... |
| 1035 | // br label %NewBlock |
| 1036 | // NewBlock: |
| 1037 | // catchret label %PHIBlock |
| 1038 | // But we need: |
| 1039 | // IncomingBlock: |
| 1040 | // ... |
| 1041 | // catchret label %NewBlock |
| 1042 | // NewBlock: |
| 1043 | // br label %PHIBlock |
| 1044 | // So move the terminators to each others' blocks and swap their |
| 1045 | // successors. |
| 1046 | BranchInst *Goto = cast<BranchInst>(IncomingBlock->getTerminator()); |
| 1047 | Goto->removeFromParent(); |
| 1048 | CatchRet->removeFromParent(); |
| 1049 | IncomingBlock->getInstList().push_back(CatchRet); |
| 1050 | NewBlock->getInstList().push_back(Goto); |
| 1051 | Goto->setSuccessor(0, PHIBlock); |
| 1052 | CatchRet->setSuccessor(NewBlock); |
| 1053 | // Update the color mapping for the newly split edge. |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1054 | ColorVector &ColorsForPHIBlock = BlockColors[PHIBlock]; |
Joseph Tremoulet | 7031c9f | 2015-08-17 13:51:37 +0000 | [diff] [blame] | 1055 | BlockColors[NewBlock] = ColorsForPHIBlock; |
| 1056 | for (BasicBlock *FuncletPad : ColorsForPHIBlock) |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1057 | FuncletBlocks[FuncletPad].push_back(NewBlock); |
Joseph Tremoulet | 7031c9f | 2015-08-17 13:51:37 +0000 | [diff] [blame] | 1058 | // Treat the new block as incoming for load insertion. |
| 1059 | IncomingBlock = NewBlock; |
| 1060 | } |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 1061 | Value *&Load = Loads[IncomingBlock]; |
| 1062 | // Insert the load into the predecessor block |
| 1063 | if (!Load) |
| 1064 | Load = new LoadInst(SpillSlot, Twine(V->getName(), ".wineh.reload"), |
| 1065 | /*Volatile=*/false, IncomingBlock->getTerminator()); |
| 1066 | |
| 1067 | U.set(Load); |
| 1068 | } else { |
| 1069 | // Reload right before the old use. |
| 1070 | auto *Load = new LoadInst(SpillSlot, Twine(V->getName(), ".wineh.reload"), |
| 1071 | /*Volatile=*/false, UsingInst); |
| 1072 | U.set(Load); |
| 1073 | } |
| 1074 | } |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 1075 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1076 | void WinEHFuncInfo::addIPToStateRange(const InvokeInst *II, |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 1077 | MCSymbol *InvokeBegin, |
| 1078 | MCSymbol *InvokeEnd) { |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1079 | assert(InvokeStateMap.count(II) && |
| 1080 | "should get invoke with precomputed state"); |
| 1081 | LabelToStateMap[InvokeBegin] = std::make_pair(InvokeStateMap[II], InvokeEnd); |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 1082 | } |
Chandler Carruth | e011534 | 2015-12-29 09:24:39 +0000 | [diff] [blame] | 1083 | |
| 1084 | WinEHFuncInfo::WinEHFuncInfo() {} |