Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 1 | //===-- WinEHPrepare - Prepare exception handling for code generation ---===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // 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] | 10 | // backend wants for functions using a personality function from a runtime |
| 11 | // provided by MSVC. Functions with other personality functions are left alone |
| 12 | // and may be prepared by other passes. In particular, all supported MSVC |
| 13 | // personality functions require cleanup code to be outlined, and the C++ |
| 14 | // personality requires catch handler code to be outlined. |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/MapVector.h" |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.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" |
David Blaikie | 31b98d2 | 2018-06-04 21:23:21 +0000 | [diff] [blame] | 23 | #include "llvm/Transforms/Utils/Local.h" |
Chandler Carruth | e011534 | 2015-12-29 09:24:39 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/Passes.h" |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/WinEHFuncInfo.h" |
David Majnemer | dbdc9c2 | 2016-01-02 09:26:36 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Verifier.h" |
Chandler Carruth | e011534 | 2015-12-29 09:24:39 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCSymbol.h" |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 29 | #include "llvm/Pass.h" |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | a8d61b1 | 2015-03-23 18:57:17 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
Andrew Kaylor | 6b67d42 | 2015-03-11 23:22:06 +0000 | [diff] [blame] | 32 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 33 | #include "llvm/Transforms/Utils/Cloning.h" |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 34 | #include "llvm/Transforms/Utils/SSAUpdater.h" |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 35 | |
| 36 | using namespace llvm; |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 37 | |
| 38 | #define DEBUG_TYPE "winehprepare" |
| 39 | |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 40 | static cl::opt<bool> DisableDemotion( |
| 41 | "disable-demotion", cl::Hidden, |
| 42 | cl::desc( |
Heejin Ahn | b4be38f | 2018-05-17 20:52:03 +0000 | [diff] [blame] | 43 | "Clone multicolor basic blocks but do not demote cross scopes"), |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 44 | cl::init(false)); |
| 45 | |
| 46 | static cl::opt<bool> DisableCleanups( |
| 47 | "disable-cleanups", cl::Hidden, |
| 48 | cl::desc("Do not remove implausible terminators or other similar cleanups"), |
| 49 | cl::init(false)); |
| 50 | |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 51 | static cl::opt<bool> DemoteCatchSwitchPHIOnlyOpt( |
| 52 | "demote-catchswitch-only", cl::Hidden, |
| 53 | cl::desc("Demote catchswitch BBs only (for wasm EH)"), cl::init(false)); |
| 54 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 55 | namespace { |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 56 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 57 | class WinEHPrepare : public FunctionPass { |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 58 | public: |
| 59 | static char ID; // Pass identification, replacement for typeid. |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 60 | WinEHPrepare(bool DemoteCatchSwitchPHIOnly = false) |
| 61 | : FunctionPass(ID), DemoteCatchSwitchPHIOnly(DemoteCatchSwitchPHIOnly) {} |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 62 | |
| 63 | bool runOnFunction(Function &Fn) override; |
| 64 | |
| 65 | bool doFinalization(Module &M) override; |
| 66 | |
| 67 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 68 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 69 | StringRef getPassName() const override { |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 70 | return "Windows exception handling preparation"; |
| 71 | } |
| 72 | |
| 73 | private: |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 74 | void insertPHIStores(PHINode *OriginalPHI, AllocaInst *SpillSlot); |
| 75 | void |
| 76 | insertPHIStore(BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot, |
| 77 | SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist); |
| 78 | AllocaInst *insertPHILoads(PHINode *PN, Function &F); |
| 79 | void replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot, |
| 80 | DenseMap<BasicBlock *, Value *> &Loads, Function &F); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 81 | bool prepareExplicitEH(Function &F); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 82 | void colorFunclets(Function &F); |
Andrew Kaylor | fdd48fa | 2015-11-09 19:59:02 +0000 | [diff] [blame] | 83 | |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 84 | void demotePHIsOnFunclets(Function &F, bool DemoteCatchSwitchPHIOnly); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 85 | void cloneCommonBlocks(Function &F); |
David Majnemer | 3bb88c0 | 2015-12-15 21:27:27 +0000 | [diff] [blame] | 86 | void removeImplausibleInstructions(Function &F); |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 87 | void cleanupPreparedFunclets(Function &F); |
| 88 | void verifyPreparedFunclets(Function &F); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 89 | |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 90 | bool DemoteCatchSwitchPHIOnly; |
| 91 | |
Reid Kleckner | 0f9e27a | 2015-03-18 20:26:53 +0000 | [diff] [blame] | 92 | // All fields are reset by runOnFunction. |
Reid Kleckner | 582786b | 2015-04-30 18:17:12 +0000 | [diff] [blame] | 93 | EHPersonality Personality = EHPersonality::Unknown; |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 94 | |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 95 | const DataLayout *DL = nullptr; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 96 | DenseMap<BasicBlock *, ColorVector> BlockColors; |
| 97 | MapVector<BasicBlock *, std::vector<BasicBlock *>> FuncletBlocks; |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 100 | } // end anonymous namespace |
| 101 | |
| 102 | char WinEHPrepare::ID = 0; |
Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 103 | INITIALIZE_PASS(WinEHPrepare, DEBUG_TYPE, "Prepare Windows exceptions", |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 104 | false, false) |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 105 | |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 106 | FunctionPass *llvm::createWinEHPass(bool DemoteCatchSwitchPHIOnly) { |
| 107 | return new WinEHPrepare(DemoteCatchSwitchPHIOnly); |
| 108 | } |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 109 | |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 110 | bool WinEHPrepare::runOnFunction(Function &Fn) { |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 111 | if (!Fn.hasPersonalityFn()) |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 112 | return false; |
| 113 | |
| 114 | // Classify the personality to see what kind of preparation we need. |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 115 | Personality = classifyEHPersonality(Fn.getPersonalityFn()); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 116 | |
Heejin Ahn | b4be38f | 2018-05-17 20:52:03 +0000 | [diff] [blame] | 117 | // Do nothing if this is not a scope-based personality. |
| 118 | if (!isScopedEHPersonality(Personality)) |
Reid Kleckner | 47c8e7a | 2015-03-12 00:36:20 +0000 | [diff] [blame] | 119 | return false; |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 120 | |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 121 | DL = &Fn.getParent()->getDataLayout(); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 122 | return prepareExplicitEH(Fn); |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Andrew Kaylor | aa92ab0 | 2015-04-03 19:37:50 +0000 | [diff] [blame] | 125 | bool WinEHPrepare::doFinalization(Module &M) { return false; } |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 126 | |
David Majnemer | e696583 | 2015-10-16 19:59:52 +0000 | [diff] [blame] | 127 | void WinEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const {} |
Andrew Kaylor | 1476e6d | 2015-02-24 20:49:35 +0000 | [diff] [blame] | 128 | |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 129 | static int addUnwindMapEntry(WinEHFuncInfo &FuncInfo, int ToState, |
David Majnemer | bfa5b98 | 2015-10-10 00:04:29 +0000 | [diff] [blame] | 130 | const BasicBlock *BB) { |
Reid Kleckner | 14e7735 | 2015-10-09 23:34:53 +0000 | [diff] [blame] | 131 | CxxUnwindMapEntry UME; |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 132 | UME.ToState = ToState; |
David Majnemer | bfa5b98 | 2015-10-10 00:04:29 +0000 | [diff] [blame] | 133 | UME.Cleanup = BB; |
Reid Kleckner | 14e7735 | 2015-10-09 23:34:53 +0000 | [diff] [blame] | 134 | FuncInfo.CxxUnwindMap.push_back(UME); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 135 | return FuncInfo.getLastStateNumber(); |
| 136 | } |
| 137 | |
| 138 | static void addTryBlockMapEntry(WinEHFuncInfo &FuncInfo, int TryLow, |
| 139 | int TryHigh, int CatchHigh, |
| 140 | ArrayRef<const CatchPadInst *> Handlers) { |
| 141 | WinEHTryBlockMapEntry TBME; |
| 142 | TBME.TryLow = TryLow; |
| 143 | TBME.TryHigh = TryHigh; |
| 144 | TBME.CatchHigh = CatchHigh; |
| 145 | assert(TBME.TryLow <= TBME.TryHigh); |
| 146 | for (const CatchPadInst *CPI : Handlers) { |
| 147 | WinEHHandlerType HT; |
| 148 | Constant *TypeInfo = cast<Constant>(CPI->getArgOperand(0)); |
Reid Kleckner | b005d28 | 2015-09-16 20:16:27 +0000 | [diff] [blame] | 149 | if (TypeInfo->isNullValue()) |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 150 | HT.TypeDescriptor = nullptr; |
Reid Kleckner | b005d28 | 2015-09-16 20:16:27 +0000 | [diff] [blame] | 151 | else |
| 152 | HT.TypeDescriptor = cast<GlobalVariable>(TypeInfo->stripPointerCasts()); |
| 153 | HT.Adjectives = cast<ConstantInt>(CPI->getArgOperand(1))->getZExtValue(); |
David Majnemer | 7735a6d | 2015-10-06 23:31:59 +0000 | [diff] [blame] | 154 | HT.Handler = CPI->getParent(); |
David Majnemer | 086fec2 | 2016-01-08 08:03:55 +0000 | [diff] [blame] | 155 | if (auto *AI = |
| 156 | dyn_cast<AllocaInst>(CPI->getArgOperand(2)->stripPointerCasts())) |
| 157 | HT.CatchObj.Alloca = AI; |
Reid Kleckner | b005d28 | 2015-09-16 20:16:27 +0000 | [diff] [blame] | 158 | else |
David Majnemer | 086fec2 | 2016-01-08 08:03:55 +0000 | [diff] [blame] | 159 | HT.CatchObj.Alloca = nullptr; |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 160 | TBME.HandlerArray.push_back(HT); |
| 161 | } |
| 162 | FuncInfo.TryBlockMap.push_back(TBME); |
| 163 | } |
| 164 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 165 | static BasicBlock *getCleanupRetUnwindDest(const CleanupPadInst *CleanupPad) { |
| 166 | for (const User *U : CleanupPad->users()) |
| 167 | if (const auto *CRI = dyn_cast<CleanupReturnInst>(U)) |
| 168 | return CRI->getUnwindDest(); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 169 | return nullptr; |
| 170 | } |
| 171 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 172 | static void calculateStateNumbersForInvokes(const Function *Fn, |
| 173 | WinEHFuncInfo &FuncInfo) { |
| 174 | auto *F = const_cast<Function *>(Fn); |
| 175 | DenseMap<BasicBlock *, ColorVector> BlockColors = colorEHFunclets(*F); |
| 176 | for (BasicBlock &BB : *F) { |
| 177 | auto *II = dyn_cast<InvokeInst>(BB.getTerminator()); |
| 178 | if (!II) |
| 179 | continue; |
| 180 | |
| 181 | auto &BBColors = BlockColors[&BB]; |
David Majnemer | c640f86 | 2015-12-23 03:59:04 +0000 | [diff] [blame] | 182 | assert(BBColors.size() == 1 && "multi-color BB not removed by preparation"); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 183 | BasicBlock *FuncletEntryBB = BBColors.front(); |
| 184 | |
| 185 | BasicBlock *FuncletUnwindDest; |
| 186 | auto *FuncletPad = |
| 187 | dyn_cast<FuncletPadInst>(FuncletEntryBB->getFirstNonPHI()); |
| 188 | assert(FuncletPad || FuncletEntryBB == &Fn->getEntryBlock()); |
| 189 | if (!FuncletPad) |
| 190 | FuncletUnwindDest = nullptr; |
| 191 | else if (auto *CatchPad = dyn_cast<CatchPadInst>(FuncletPad)) |
| 192 | FuncletUnwindDest = CatchPad->getCatchSwitch()->getUnwindDest(); |
| 193 | else if (auto *CleanupPad = dyn_cast<CleanupPadInst>(FuncletPad)) |
| 194 | FuncletUnwindDest = getCleanupRetUnwindDest(CleanupPad); |
| 195 | else |
| 196 | llvm_unreachable("unexpected funclet pad!"); |
| 197 | |
| 198 | BasicBlock *InvokeUnwindDest = II->getUnwindDest(); |
| 199 | int BaseState = -1; |
| 200 | if (FuncletUnwindDest == InvokeUnwindDest) { |
| 201 | auto BaseStateI = FuncInfo.FuncletBaseStateMap.find(FuncletPad); |
| 202 | if (BaseStateI != FuncInfo.FuncletBaseStateMap.end()) |
| 203 | BaseState = BaseStateI->second; |
| 204 | } |
| 205 | |
| 206 | if (BaseState != -1) { |
| 207 | FuncInfo.InvokeStateMap[II] = BaseState; |
| 208 | } else { |
| 209 | Instruction *PadInst = InvokeUnwindDest->getFirstNonPHI(); |
| 210 | assert(FuncInfo.EHPadStateMap.count(PadInst) && "EH Pad has no state!"); |
| 211 | FuncInfo.InvokeStateMap[II] = FuncInfo.EHPadStateMap[PadInst]; |
| 212 | } |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 213 | } |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 214 | } |
| 215 | |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 216 | // Given BB which ends in an unwind edge, return the EHPad that this BB belongs |
| 217 | // to. If the unwind edge came from an invoke, return null. |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 218 | static const BasicBlock *getEHPadFromPredecessor(const BasicBlock *BB, |
| 219 | Value *ParentPad) { |
Chandler Carruth | edb12a8 | 2018-10-15 10:04:59 +0000 | [diff] [blame] | 220 | const Instruction *TI = BB->getTerminator(); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 221 | if (isa<InvokeInst>(TI)) |
| 222 | return nullptr; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 223 | if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(TI)) { |
| 224 | if (CatchSwitch->getParentPad() != ParentPad) |
| 225 | return nullptr; |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 226 | return BB; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 227 | } |
| 228 | assert(!TI->isEHPad() && "unexpected EHPad!"); |
| 229 | auto *CleanupPad = cast<CleanupReturnInst>(TI)->getCleanupPad(); |
| 230 | if (CleanupPad->getParentPad() != ParentPad) |
| 231 | return nullptr; |
| 232 | return CleanupPad->getParent(); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 233 | } |
| 234 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 235 | static void calculateCXXStateNumbers(WinEHFuncInfo &FuncInfo, |
| 236 | const Instruction *FirstNonPHI, |
| 237 | int ParentState) { |
| 238 | const BasicBlock *BB = FirstNonPHI->getParent(); |
| 239 | assert(BB->isEHPad() && "not a funclet!"); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 240 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 241 | if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(FirstNonPHI)) { |
| 242 | assert(FuncInfo.EHPadStateMap.count(CatchSwitch) == 0 && |
| 243 | "shouldn't revist catch funclets!"); |
| 244 | |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 245 | SmallVector<const CatchPadInst *, 2> Handlers; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 246 | for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) { |
| 247 | auto *CatchPad = cast<CatchPadInst>(CatchPadBB->getFirstNonPHI()); |
| 248 | Handlers.push_back(CatchPad); |
| 249 | } |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 250 | int TryLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 251 | FuncInfo.EHPadStateMap[CatchSwitch] = TryLow; |
| 252 | for (const BasicBlock *PredBlock : predecessors(BB)) |
| 253 | if ((PredBlock = getEHPadFromPredecessor(PredBlock, |
| 254 | CatchSwitch->getParentPad()))) |
| 255 | calculateCXXStateNumbers(FuncInfo, PredBlock->getFirstNonPHI(), |
| 256 | TryLow); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 257 | int CatchLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr); |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 258 | |
| 259 | // 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] | 260 | int TryHigh = CatchLow - 1; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 261 | for (const auto *CatchPad : Handlers) { |
| 262 | FuncInfo.FuncletBaseStateMap[CatchPad] = CatchLow; |
| 263 | for (const User *U : CatchPad->users()) { |
| 264 | const auto *UserI = cast<Instruction>(U); |
David Majnemer | 17525ab | 2016-02-23 07:18:15 +0000 | [diff] [blame] | 265 | if (auto *InnerCatchSwitch = dyn_cast<CatchSwitchInst>(UserI)) { |
| 266 | BasicBlock *UnwindDest = InnerCatchSwitch->getUnwindDest(); |
| 267 | if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest()) |
David Majnemer | c640f86 | 2015-12-23 03:59:04 +0000 | [diff] [blame] | 268 | calculateCXXStateNumbers(FuncInfo, UserI, CatchLow); |
David Majnemer | 17525ab | 2016-02-23 07:18:15 +0000 | [diff] [blame] | 269 | } |
Andrew Kaylor | d1188dd | 2016-02-12 21:10:16 +0000 | [diff] [blame] | 270 | if (auto *InnerCleanupPad = dyn_cast<CleanupPadInst>(UserI)) { |
| 271 | BasicBlock *UnwindDest = getCleanupRetUnwindDest(InnerCleanupPad); |
| 272 | // If a nested cleanup pad reports a null unwind destination and the |
| 273 | // enclosing catch pad doesn't it must be post-dominated by an |
| 274 | // unreachable instruction. |
| 275 | if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest()) |
David Majnemer | c640f86 | 2015-12-23 03:59:04 +0000 | [diff] [blame] | 276 | calculateCXXStateNumbers(FuncInfo, UserI, CatchLow); |
Andrew Kaylor | d1188dd | 2016-02-12 21:10:16 +0000 | [diff] [blame] | 277 | } |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 278 | } |
| 279 | } |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 280 | int CatchHigh = FuncInfo.getLastStateNumber(); |
| 281 | addTryBlockMapEntry(FuncInfo, TryLow, TryHigh, CatchHigh, Handlers); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 282 | LLVM_DEBUG(dbgs() << "TryLow[" << BB->getName() << "]: " << TryLow << '\n'); |
| 283 | LLVM_DEBUG(dbgs() << "TryHigh[" << BB->getName() << "]: " << TryHigh |
| 284 | << '\n'); |
| 285 | LLVM_DEBUG(dbgs() << "CatchHigh[" << BB->getName() << "]: " << CatchHigh |
| 286 | << '\n'); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 287 | } else { |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 288 | auto *CleanupPad = cast<CleanupPadInst>(FirstNonPHI); |
| 289 | |
| 290 | // It's possible for a cleanup to be visited twice: it might have multiple |
| 291 | // cleanupret instructions. |
| 292 | if (FuncInfo.EHPadStateMap.count(CleanupPad)) |
| 293 | return; |
| 294 | |
| 295 | int CleanupState = addUnwindMapEntry(FuncInfo, ParentState, BB); |
| 296 | FuncInfo.EHPadStateMap[CleanupPad] = CleanupState; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 297 | LLVM_DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB " |
| 298 | << BB->getName() << '\n'); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 299 | for (const BasicBlock *PredBlock : predecessors(BB)) { |
| 300 | if ((PredBlock = getEHPadFromPredecessor(PredBlock, |
| 301 | CleanupPad->getParentPad()))) { |
| 302 | calculateCXXStateNumbers(FuncInfo, PredBlock->getFirstNonPHI(), |
| 303 | CleanupState); |
| 304 | } |
| 305 | } |
| 306 | for (const User *U : CleanupPad->users()) { |
| 307 | const auto *UserI = cast<Instruction>(U); |
| 308 | if (UserI->isEHPad()) |
| 309 | report_fatal_error("Cleanup funclets for the MSVC++ personality cannot " |
| 310 | "contain exceptional actions"); |
| 311 | } |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 315 | static int addSEHExcept(WinEHFuncInfo &FuncInfo, int ParentState, |
| 316 | const Function *Filter, const BasicBlock *Handler) { |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 317 | SEHUnwindMapEntry Entry; |
| 318 | Entry.ToState = ParentState; |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 319 | Entry.IsFinally = false; |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 320 | Entry.Filter = Filter; |
| 321 | Entry.Handler = Handler; |
| 322 | FuncInfo.SEHUnwindMap.push_back(Entry); |
| 323 | return FuncInfo.SEHUnwindMap.size() - 1; |
| 324 | } |
| 325 | |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 326 | static int addSEHFinally(WinEHFuncInfo &FuncInfo, int ParentState, |
| 327 | const BasicBlock *Handler) { |
| 328 | SEHUnwindMapEntry Entry; |
| 329 | Entry.ToState = ParentState; |
| 330 | Entry.IsFinally = true; |
| 331 | Entry.Filter = nullptr; |
| 332 | Entry.Handler = Handler; |
| 333 | FuncInfo.SEHUnwindMap.push_back(Entry); |
| 334 | return FuncInfo.SEHUnwindMap.size() - 1; |
| 335 | } |
| 336 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 337 | static void calculateSEHStateNumbers(WinEHFuncInfo &FuncInfo, |
| 338 | const Instruction *FirstNonPHI, |
| 339 | int ParentState) { |
| 340 | const BasicBlock *BB = FirstNonPHI->getParent(); |
| 341 | assert(BB->isEHPad() && "no a funclet!"); |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 342 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 343 | if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(FirstNonPHI)) { |
| 344 | assert(FuncInfo.EHPadStateMap.count(CatchSwitch) == 0 && |
| 345 | "shouldn't revist catch funclets!"); |
| 346 | |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 347 | // Extract the filter function and the __except basic block and create a |
| 348 | // state for them. |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 349 | assert(CatchSwitch->getNumHandlers() == 1 && |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 350 | "SEH doesn't have multiple handlers per __try"); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 351 | const auto *CatchPad = |
| 352 | cast<CatchPadInst>((*CatchSwitch->handler_begin())->getFirstNonPHI()); |
| 353 | const BasicBlock *CatchPadBB = CatchPad->getParent(); |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 354 | const Constant *FilterOrNull = |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 355 | cast<Constant>(CatchPad->getArgOperand(0)->stripPointerCasts()); |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 356 | const Function *Filter = dyn_cast<Function>(FilterOrNull); |
| 357 | assert((Filter || FilterOrNull->isNullValue()) && |
| 358 | "unexpected filter value"); |
David Majnemer | 7735a6d | 2015-10-06 23:31:59 +0000 | [diff] [blame] | 359 | int TryState = addSEHExcept(FuncInfo, ParentState, Filter, CatchPadBB); |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 360 | |
| 361 | // Everything in the __try block uses TryState as its parent state. |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 362 | FuncInfo.EHPadStateMap[CatchSwitch] = TryState; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 363 | LLVM_DEBUG(dbgs() << "Assigning state #" << TryState << " to BB " |
| 364 | << CatchPadBB->getName() << '\n'); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 365 | for (const BasicBlock *PredBlock : predecessors(BB)) |
| 366 | if ((PredBlock = getEHPadFromPredecessor(PredBlock, |
| 367 | CatchSwitch->getParentPad()))) |
| 368 | calculateSEHStateNumbers(FuncInfo, PredBlock->getFirstNonPHI(), |
| 369 | TryState); |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 370 | |
| 371 | // Everything in the __except block unwinds to ParentState, just like code |
| 372 | // outside the __try. |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 373 | for (const User *U : CatchPad->users()) { |
| 374 | const auto *UserI = cast<Instruction>(U); |
David Majnemer | 17525ab | 2016-02-23 07:18:15 +0000 | [diff] [blame] | 375 | if (auto *InnerCatchSwitch = dyn_cast<CatchSwitchInst>(UserI)) { |
| 376 | BasicBlock *UnwindDest = InnerCatchSwitch->getUnwindDest(); |
| 377 | if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest()) |
David Majnemer | c640f86 | 2015-12-23 03:59:04 +0000 | [diff] [blame] | 378 | calculateSEHStateNumbers(FuncInfo, UserI, ParentState); |
David Majnemer | 17525ab | 2016-02-23 07:18:15 +0000 | [diff] [blame] | 379 | } |
Andrew Kaylor | d1188dd | 2016-02-12 21:10:16 +0000 | [diff] [blame] | 380 | if (auto *InnerCleanupPad = dyn_cast<CleanupPadInst>(UserI)) { |
| 381 | BasicBlock *UnwindDest = getCleanupRetUnwindDest(InnerCleanupPad); |
| 382 | // If a nested cleanup pad reports a null unwind destination and the |
| 383 | // enclosing catch pad doesn't it must be post-dominated by an |
| 384 | // unreachable instruction. |
| 385 | if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest()) |
David Majnemer | c640f86 | 2015-12-23 03:59:04 +0000 | [diff] [blame] | 386 | calculateSEHStateNumbers(FuncInfo, UserI, ParentState); |
Andrew Kaylor | d1188dd | 2016-02-12 21:10:16 +0000 | [diff] [blame] | 387 | } |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 388 | } |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 389 | } else { |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 390 | auto *CleanupPad = cast<CleanupPadInst>(FirstNonPHI); |
| 391 | |
| 392 | // It's possible for a cleanup to be visited twice: it might have multiple |
| 393 | // cleanupret instructions. |
| 394 | if (FuncInfo.EHPadStateMap.count(CleanupPad)) |
| 395 | return; |
| 396 | |
| 397 | int CleanupState = addSEHFinally(FuncInfo, ParentState, BB); |
| 398 | FuncInfo.EHPadStateMap[CleanupPad] = CleanupState; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 399 | LLVM_DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB " |
| 400 | << BB->getName() << '\n'); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 401 | for (const BasicBlock *PredBlock : predecessors(BB)) |
| 402 | if ((PredBlock = |
| 403 | getEHPadFromPredecessor(PredBlock, CleanupPad->getParentPad()))) |
| 404 | calculateSEHStateNumbers(FuncInfo, PredBlock->getFirstNonPHI(), |
| 405 | CleanupState); |
| 406 | for (const User *U : CleanupPad->users()) { |
| 407 | const auto *UserI = cast<Instruction>(U); |
| 408 | if (UserI->isEHPad()) |
| 409 | report_fatal_error("Cleanup funclets for the SEH personality cannot " |
| 410 | "contain exceptional actions"); |
| 411 | } |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 415 | static bool isTopLevelPadForMSVC(const Instruction *EHPad) { |
| 416 | if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(EHPad)) |
| 417 | return isa<ConstantTokenNone>(CatchSwitch->getParentPad()) && |
| 418 | CatchSwitch->unwindsToCaller(); |
| 419 | if (auto *CleanupPad = dyn_cast<CleanupPadInst>(EHPad)) |
| 420 | return isa<ConstantTokenNone>(CleanupPad->getParentPad()) && |
| 421 | getCleanupRetUnwindDest(CleanupPad) == nullptr; |
| 422 | if (isa<CatchPadInst>(EHPad)) |
| 423 | return false; |
| 424 | llvm_unreachable("unexpected EHPad!"); |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 427 | void llvm::calculateSEHStateNumbers(const Function *Fn, |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 428 | WinEHFuncInfo &FuncInfo) { |
| 429 | // Don't compute state numbers twice. |
| 430 | if (!FuncInfo.SEHUnwindMap.empty()) |
| 431 | return; |
| 432 | |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 433 | for (const BasicBlock &BB : *Fn) { |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 434 | if (!BB.isEHPad()) |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 435 | continue; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 436 | const Instruction *FirstNonPHI = BB.getFirstNonPHI(); |
| 437 | if (!isTopLevelPadForMSVC(FirstNonPHI)) |
| 438 | continue; |
| 439 | ::calculateSEHStateNumbers(FuncInfo, FirstNonPHI, -1); |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 440 | } |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 441 | |
| 442 | calculateStateNumbersForInvokes(Fn, FuncInfo); |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 445 | void llvm::calculateWinCXXEHStateNumbers(const Function *Fn, |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 446 | WinEHFuncInfo &FuncInfo) { |
| 447 | // Return if it's already been done. |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 448 | if (!FuncInfo.EHPadStateMap.empty()) |
| 449 | return; |
| 450 | |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 451 | for (const BasicBlock &BB : *Fn) { |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 452 | if (!BB.isEHPad()) |
| 453 | continue; |
Joseph Tremoulet | 9ce71f7 | 2015-09-03 09:09:43 +0000 | [diff] [blame] | 454 | const Instruction *FirstNonPHI = BB.getFirstNonPHI(); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 455 | if (!isTopLevelPadForMSVC(FirstNonPHI)) |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 456 | continue; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 457 | calculateCXXStateNumbers(FuncInfo, FirstNonPHI, -1); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 458 | } |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 459 | |
| 460 | calculateStateNumbersForInvokes(Fn, FuncInfo); |
Reid Kleckner | fe4d491 | 2015-05-28 22:00:24 +0000 | [diff] [blame] | 461 | } |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 462 | |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame] | 463 | static int addClrEHHandler(WinEHFuncInfo &FuncInfo, int HandlerParentState, |
| 464 | int TryParentState, ClrHandlerType HandlerType, |
| 465 | uint32_t TypeToken, const BasicBlock *Handler) { |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 466 | ClrEHUnwindMapEntry Entry; |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame] | 467 | Entry.HandlerParentState = HandlerParentState; |
| 468 | Entry.TryParentState = TryParentState; |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 469 | Entry.Handler = Handler; |
| 470 | Entry.HandlerType = HandlerType; |
| 471 | Entry.TypeToken = TypeToken; |
| 472 | FuncInfo.ClrEHUnwindMap.push_back(Entry); |
| 473 | return FuncInfo.ClrEHUnwindMap.size() - 1; |
| 474 | } |
| 475 | |
| 476 | void llvm::calculateClrEHStateNumbers(const Function *Fn, |
| 477 | WinEHFuncInfo &FuncInfo) { |
| 478 | // Return if it's already been done. |
| 479 | if (!FuncInfo.EHPadStateMap.empty()) |
| 480 | return; |
| 481 | |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame] | 482 | // This numbering assigns one state number to each catchpad and cleanuppad. |
| 483 | // It also computes two tree-like relations over states: |
| 484 | // 1) Each state has a "HandlerParentState", which is the state of the next |
| 485 | // outer handler enclosing this state's handler (same as nearest ancestor |
| 486 | // per the ParentPad linkage on EH pads, but skipping over catchswitches). |
| 487 | // 2) Each state has a "TryParentState", which: |
| 488 | // a) for a catchpad that's not the last handler on its catchswitch, is |
| 489 | // the state of the next catchpad on that catchswitch |
| 490 | // b) for all other pads, is the state of the pad whose try region is the |
| 491 | // next outer try region enclosing this state's try region. The "try |
| 492 | // regions are not present as such in the IR, but will be inferred |
| 493 | // based on the placement of invokes and pads which reach each other |
| 494 | // by exceptional exits |
| 495 | // Catchswitches do not get their own states, but each gets mapped to the |
| 496 | // state of its first catchpad. |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 497 | |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame] | 498 | // Step one: walk down from outermost to innermost funclets, assigning each |
| 499 | // catchpad and cleanuppad a state number. Add an entry to the |
| 500 | // ClrEHUnwindMap for each state, recording its HandlerParentState and |
| 501 | // handler attributes. Record the TryParentState as well for each catchpad |
| 502 | // that's not the last on its catchswitch, but initialize all other entries' |
| 503 | // TryParentStates to a sentinel -1 value that the next pass will update. |
| 504 | |
| 505 | // Seed a worklist with pads that have no parent. |
| 506 | SmallVector<std::pair<const Instruction *, int>, 8> Worklist; |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 507 | for (const BasicBlock &BB : *Fn) { |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 508 | const Instruction *FirstNonPHI = BB.getFirstNonPHI(); |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame] | 509 | const Value *ParentPad; |
| 510 | if (const auto *CPI = dyn_cast<CleanupPadInst>(FirstNonPHI)) |
| 511 | ParentPad = CPI->getParentPad(); |
| 512 | else if (const auto *CSI = dyn_cast<CatchSwitchInst>(FirstNonPHI)) |
| 513 | ParentPad = CSI->getParentPad(); |
| 514 | else |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 515 | continue; |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame] | 516 | if (isa<ConstantTokenNone>(ParentPad)) |
| 517 | Worklist.emplace_back(FirstNonPHI, -1); |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame] | 520 | // Use the worklist to visit all pads, from outer to inner. Record |
| 521 | // HandlerParentState for all pads. Record TryParentState only for catchpads |
| 522 | // that aren't the last on their catchswitch (setting all other entries' |
| 523 | // TryParentStates to an initial value of -1). This loop is also responsible |
| 524 | // for setting the EHPadStateMap entry for all catchpads, cleanuppads, and |
| 525 | // catchswitches. |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 526 | while (!Worklist.empty()) { |
| 527 | const Instruction *Pad; |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame] | 528 | int HandlerParentState; |
| 529 | std::tie(Pad, HandlerParentState) = Worklist.pop_back_val(); |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 530 | |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame] | 531 | if (const auto *Cleanup = dyn_cast<CleanupPadInst>(Pad)) { |
| 532 | // Create the entry for this cleanup with the appropriate handler |
Simon Pilgrim | f2fbf43 | 2016-11-20 13:47:59 +0000 | [diff] [blame] | 533 | // properties. Finally and fault handlers are distinguished by arity. |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 534 | ClrHandlerType HandlerType = |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame] | 535 | (Cleanup->getNumArgOperands() ? ClrHandlerType::Fault |
| 536 | : ClrHandlerType::Finally); |
| 537 | int CleanupState = addClrEHHandler(FuncInfo, HandlerParentState, -1, |
| 538 | HandlerType, 0, Pad->getParent()); |
| 539 | // Queue any child EH pads on the worklist. |
| 540 | for (const User *U : Cleanup->users()) |
| 541 | if (const auto *I = dyn_cast<Instruction>(U)) |
| 542 | if (I->isEHPad()) |
| 543 | Worklist.emplace_back(I, CleanupState); |
| 544 | // Remember this pad's state. |
| 545 | FuncInfo.EHPadStateMap[Cleanup] = CleanupState; |
| 546 | } else { |
| 547 | // Walk the handlers of this catchswitch in reverse order since all but |
| 548 | // the last need to set the following one as its TryParentState. |
| 549 | const auto *CatchSwitch = cast<CatchSwitchInst>(Pad); |
| 550 | int CatchState = -1, FollowerState = -1; |
| 551 | SmallVector<const BasicBlock *, 4> CatchBlocks(CatchSwitch->handlers()); |
| 552 | for (auto CBI = CatchBlocks.rbegin(), CBE = CatchBlocks.rend(); |
| 553 | CBI != CBE; ++CBI, FollowerState = CatchState) { |
| 554 | const BasicBlock *CatchBlock = *CBI; |
| 555 | // Create the entry for this catch with the appropriate handler |
| 556 | // properties. |
| 557 | const auto *Catch = cast<CatchPadInst>(CatchBlock->getFirstNonPHI()); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 558 | uint32_t TypeToken = static_cast<uint32_t>( |
| 559 | cast<ConstantInt>(Catch->getArgOperand(0))->getZExtValue()); |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame] | 560 | CatchState = |
| 561 | addClrEHHandler(FuncInfo, HandlerParentState, FollowerState, |
| 562 | ClrHandlerType::Catch, TypeToken, CatchBlock); |
| 563 | // Queue any child EH pads on the worklist. |
| 564 | for (const User *U : Catch->users()) |
| 565 | if (const auto *I = dyn_cast<Instruction>(U)) |
| 566 | if (I->isEHPad()) |
| 567 | Worklist.emplace_back(I, CatchState); |
| 568 | // Remember this catch's state. |
| 569 | FuncInfo.EHPadStateMap[Catch] = CatchState; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 570 | } |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame] | 571 | // Associate the catchswitch with the state of its first catch. |
| 572 | assert(CatchSwitch->getNumHandlers()); |
| 573 | FuncInfo.EHPadStateMap[CatchSwitch] = CatchState; |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 574 | } |
| 575 | } |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 576 | |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame] | 577 | // Step two: record the TryParentState of each state. For cleanuppads that |
| 578 | // don't have cleanuprets, we may need to infer this from their child pads, |
| 579 | // so visit pads in descendant-most to ancestor-most order. |
| 580 | for (auto Entry = FuncInfo.ClrEHUnwindMap.rbegin(), |
| 581 | End = FuncInfo.ClrEHUnwindMap.rend(); |
| 582 | Entry != End; ++Entry) { |
| 583 | const Instruction *Pad = |
| 584 | Entry->Handler.get<const BasicBlock *>()->getFirstNonPHI(); |
| 585 | // For most pads, the TryParentState is the state associated with the |
| 586 | // unwind dest of exceptional exits from it. |
| 587 | const BasicBlock *UnwindDest; |
| 588 | if (const auto *Catch = dyn_cast<CatchPadInst>(Pad)) { |
| 589 | // If a catch is not the last in its catchswitch, its TryParentState is |
| 590 | // the state associated with the next catch in the switch, even though |
| 591 | // that's not the unwind dest of exceptions escaping the catch. Those |
| 592 | // cases were already assigned a TryParentState in the first pass, so |
| 593 | // skip them. |
| 594 | if (Entry->TryParentState != -1) |
| 595 | continue; |
| 596 | // Otherwise, get the unwind dest from the catchswitch. |
| 597 | UnwindDest = Catch->getCatchSwitch()->getUnwindDest(); |
| 598 | } else { |
| 599 | const auto *Cleanup = cast<CleanupPadInst>(Pad); |
| 600 | UnwindDest = nullptr; |
| 601 | for (const User *U : Cleanup->users()) { |
| 602 | if (auto *CleanupRet = dyn_cast<CleanupReturnInst>(U)) { |
| 603 | // Common and unambiguous case -- cleanupret indicates cleanup's |
| 604 | // unwind dest. |
| 605 | UnwindDest = CleanupRet->getUnwindDest(); |
| 606 | break; |
| 607 | } |
| 608 | |
| 609 | // Get an unwind dest for the user |
| 610 | const BasicBlock *UserUnwindDest = nullptr; |
| 611 | if (auto *Invoke = dyn_cast<InvokeInst>(U)) { |
| 612 | UserUnwindDest = Invoke->getUnwindDest(); |
| 613 | } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(U)) { |
| 614 | UserUnwindDest = CatchSwitch->getUnwindDest(); |
| 615 | } else if (auto *ChildCleanup = dyn_cast<CleanupPadInst>(U)) { |
| 616 | int UserState = FuncInfo.EHPadStateMap[ChildCleanup]; |
| 617 | int UserUnwindState = |
| 618 | FuncInfo.ClrEHUnwindMap[UserState].TryParentState; |
| 619 | if (UserUnwindState != -1) |
| 620 | UserUnwindDest = FuncInfo.ClrEHUnwindMap[UserUnwindState] |
| 621 | .Handler.get<const BasicBlock *>(); |
| 622 | } |
| 623 | |
| 624 | // Not having an unwind dest for this user might indicate that it |
| 625 | // doesn't unwind, so can't be taken as proof that the cleanup itself |
| 626 | // may unwind to caller (see e.g. SimplifyUnreachable and |
| 627 | // RemoveUnwindEdge). |
| 628 | if (!UserUnwindDest) |
| 629 | continue; |
| 630 | |
| 631 | // Now we have an unwind dest for the user, but we need to see if it |
| 632 | // unwinds all the way out of the cleanup or if it stays within it. |
| 633 | const Instruction *UserUnwindPad = UserUnwindDest->getFirstNonPHI(); |
| 634 | const Value *UserUnwindParent; |
| 635 | if (auto *CSI = dyn_cast<CatchSwitchInst>(UserUnwindPad)) |
| 636 | UserUnwindParent = CSI->getParentPad(); |
| 637 | else |
| 638 | UserUnwindParent = |
| 639 | cast<CleanupPadInst>(UserUnwindPad)->getParentPad(); |
| 640 | |
| 641 | // The unwind stays within the cleanup iff it targets a child of the |
| 642 | // cleanup. |
| 643 | if (UserUnwindParent == Cleanup) |
| 644 | continue; |
| 645 | |
| 646 | // This unwind exits the cleanup, so its dest is the cleanup's dest. |
| 647 | UnwindDest = UserUnwindDest; |
| 648 | break; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | // Record the state of the unwind dest as the TryParentState. |
| 653 | int UnwindDestState; |
| 654 | |
| 655 | // If UnwindDest is null at this point, either the pad in question can |
| 656 | // be exited by unwind to caller, or it cannot be exited by unwind. In |
| 657 | // either case, reporting such cases as unwinding to caller is correct. |
| 658 | // This can lead to EH tables that "look strange" -- if this pad's is in |
| 659 | // a parent funclet which has other children that do unwind to an enclosing |
| 660 | // pad, the try region for this pad will be missing the "duplicate" EH |
| 661 | // clause entries that you'd expect to see covering the whole parent. That |
| 662 | // should be benign, since the unwind never actually happens. If it were |
| 663 | // an issue, we could add a subsequent pass that pushes unwind dests down |
| 664 | // from parents that have them to children that appear to unwind to caller. |
| 665 | if (!UnwindDest) { |
| 666 | UnwindDestState = -1; |
| 667 | } else { |
| 668 | UnwindDestState = FuncInfo.EHPadStateMap[UnwindDest->getFirstNonPHI()]; |
| 669 | } |
| 670 | |
| 671 | Entry->TryParentState = UnwindDestState; |
| 672 | } |
| 673 | |
| 674 | // Step three: transfer information from pads to invokes. |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 675 | calculateStateNumbersForInvokes(Fn, FuncInfo); |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 676 | } |
| 677 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 678 | void WinEHPrepare::colorFunclets(Function &F) { |
| 679 | BlockColors = colorEHFunclets(F); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 680 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 681 | // Invert the map from BB to colors to color to BBs. |
| 682 | for (BasicBlock &BB : F) { |
| 683 | ColorVector &Colors = BlockColors[&BB]; |
| 684 | for (BasicBlock *Color : Colors) |
| 685 | FuncletBlocks[Color].push_back(&BB); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 689 | void WinEHPrepare::demotePHIsOnFunclets(Function &F, |
| 690 | bool DemoteCatchSwitchPHIOnly) { |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 691 | // Strip PHI nodes off of EH pads. |
| 692 | SmallVector<PHINode *, 16> PHINodes; |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 693 | 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] | 694 | BasicBlock *BB = &*FI++; |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 695 | if (!BB->isEHPad()) |
| 696 | continue; |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 697 | if (DemoteCatchSwitchPHIOnly && !isa<CatchSwitchInst>(BB->getFirstNonPHI())) |
| 698 | continue; |
| 699 | |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 700 | 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] | 701 | Instruction *I = &*BI++; |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 702 | auto *PN = dyn_cast<PHINode>(I); |
| 703 | // Stop at the first non-PHI. |
| 704 | if (!PN) |
| 705 | break; |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 706 | |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 707 | AllocaInst *SpillSlot = insertPHILoads(PN, F); |
| 708 | if (SpillSlot) |
| 709 | insertPHIStores(PN, SpillSlot); |
| 710 | |
| 711 | PHINodes.push_back(PN); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 715 | for (auto *PN : PHINodes) { |
| 716 | // There may be lingering uses on other EH PHIs being removed |
| 717 | PN->replaceAllUsesWith(UndefValue::get(PN->getType())); |
| 718 | PN->eraseFromParent(); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 719 | } |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 720 | } |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 721 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 722 | void WinEHPrepare::cloneCommonBlocks(Function &F) { |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 723 | // We need to clone all blocks which belong to multiple funclets. Values are |
Simon Pilgrim | f2fbf43 | 2016-11-20 13:47:59 +0000 | [diff] [blame] | 724 | // remapped throughout the funclet to propagate both the new instructions |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 725 | // *and* the new basic blocks themselves. |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 726 | for (auto &Funclets : FuncletBlocks) { |
| 727 | BasicBlock *FuncletPadBB = Funclets.first; |
| 728 | std::vector<BasicBlock *> &BlocksInFunclet = Funclets.second; |
Joseph Tremoulet | 71e5676 | 2016-01-02 15:22:36 +0000 | [diff] [blame] | 729 | Value *FuncletToken; |
| 730 | if (FuncletPadBB == &F.getEntryBlock()) |
| 731 | FuncletToken = ConstantTokenNone::get(F.getContext()); |
| 732 | else |
| 733 | FuncletToken = FuncletPadBB->getFirstNonPHI(); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 734 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 735 | std::vector<std::pair<BasicBlock *, BasicBlock *>> Orig2Clone; |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 736 | ValueToValueMapTy VMap; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 737 | for (BasicBlock *BB : BlocksInFunclet) { |
| 738 | ColorVector &ColorsForBB = BlockColors[BB]; |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 739 | // We don't need to do anything if the block is monochromatic. |
| 740 | size_t NumColorsForBB = ColorsForBB.size(); |
| 741 | if (NumColorsForBB == 1) |
| 742 | continue; |
| 743 | |
Andrew Kaylor | fdd48fa | 2015-11-09 19:59:02 +0000 | [diff] [blame] | 744 | DEBUG_WITH_TYPE("winehprepare-coloring", |
| 745 | dbgs() << " Cloning block \'" << BB->getName() |
| 746 | << "\' for funclet \'" << FuncletPadBB->getName() |
| 747 | << "\'.\n"); |
| 748 | |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 749 | // Create a new basic block and copy instructions into it! |
Joseph Tremoulet | ec18285 | 2015-08-28 01:12:35 +0000 | [diff] [blame] | 750 | BasicBlock *CBB = |
| 751 | CloneBasicBlock(BB, VMap, Twine(".for.", FuncletPadBB->getName())); |
| 752 | // Insert the clone immediately after the original to ensure determinism |
| 753 | // and to keep the same relative ordering of any funclet's blocks. |
| 754 | CBB->insertInto(&F, BB->getNextNode()); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 755 | |
| 756 | // Add basic block mapping. |
| 757 | VMap[BB] = CBB; |
| 758 | |
| 759 | // Record delta operations that we need to perform to our color mappings. |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 760 | Orig2Clone.emplace_back(BB, CBB); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Joseph Tremoulet | 39234fc | 2015-10-07 19:29:56 +0000 | [diff] [blame] | 763 | // If nothing was cloned, we're done cloning in this funclet. |
| 764 | if (Orig2Clone.empty()) |
| 765 | continue; |
| 766 | |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 767 | // Update our color mappings to reflect that one block has lost a color and |
| 768 | // another has gained a color. |
| 769 | for (auto &BBMapping : Orig2Clone) { |
| 770 | BasicBlock *OldBlock = BBMapping.first; |
| 771 | BasicBlock *NewBlock = BBMapping.second; |
| 772 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 773 | BlocksInFunclet.push_back(NewBlock); |
| 774 | ColorVector &NewColors = BlockColors[NewBlock]; |
| 775 | assert(NewColors.empty() && "A new block should only have one color!"); |
| 776 | NewColors.push_back(FuncletPadBB); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 777 | |
Andrew Kaylor | fdd48fa | 2015-11-09 19:59:02 +0000 | [diff] [blame] | 778 | DEBUG_WITH_TYPE("winehprepare-coloring", |
| 779 | dbgs() << " Assigned color \'" << FuncletPadBB->getName() |
| 780 | << "\' to block \'" << NewBlock->getName() |
| 781 | << "\'.\n"); |
| 782 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 783 | BlocksInFunclet.erase( |
| 784 | std::remove(BlocksInFunclet.begin(), BlocksInFunclet.end(), OldBlock), |
| 785 | BlocksInFunclet.end()); |
| 786 | ColorVector &OldColors = BlockColors[OldBlock]; |
| 787 | OldColors.erase( |
| 788 | std::remove(OldColors.begin(), OldColors.end(), FuncletPadBB), |
| 789 | OldColors.end()); |
Andrew Kaylor | fdd48fa | 2015-11-09 19:59:02 +0000 | [diff] [blame] | 790 | |
| 791 | DEBUG_WITH_TYPE("winehprepare-coloring", |
| 792 | dbgs() << " Removed color \'" << FuncletPadBB->getName() |
| 793 | << "\' from block \'" << OldBlock->getName() |
| 794 | << "\'.\n"); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Joseph Tremoulet | 39234fc | 2015-10-07 19:29:56 +0000 | [diff] [blame] | 797 | // Loop over all of the instructions in this funclet, fixing up operand |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 798 | // references as we go. This uses VMap to do all the hard work. |
| 799 | for (BasicBlock *BB : BlocksInFunclet) |
| 800 | // Loop over all instructions, fixing each one as we find it... |
| 801 | for (Instruction &I : *BB) |
Joseph Tremoulet | 39234fc | 2015-10-07 19:29:56 +0000 | [diff] [blame] | 802 | RemapInstruction(&I, VMap, |
Duncan P. N. Exon Smith | da68cbc | 2016-04-07 00:26:43 +0000 | [diff] [blame] | 803 | RF_IgnoreMissingLocals | RF_NoModuleLevelChanges); |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 804 | |
Joseph Tremoulet | 71e5676 | 2016-01-02 15:22:36 +0000 | [diff] [blame] | 805 | // Catchrets targeting cloned blocks need to be updated separately from |
| 806 | // the loop above because they are not in the current funclet. |
| 807 | SmallVector<CatchReturnInst *, 2> FixupCatchrets; |
| 808 | for (auto &BBMapping : Orig2Clone) { |
| 809 | BasicBlock *OldBlock = BBMapping.first; |
| 810 | BasicBlock *NewBlock = BBMapping.second; |
| 811 | |
| 812 | FixupCatchrets.clear(); |
| 813 | for (BasicBlock *Pred : predecessors(OldBlock)) |
| 814 | if (auto *CatchRet = dyn_cast<CatchReturnInst>(Pred->getTerminator())) |
Joseph Tremoulet | 44b3f96 | 2016-01-15 21:16:19 +0000 | [diff] [blame] | 815 | if (CatchRet->getCatchSwitchParentPad() == FuncletToken) |
Joseph Tremoulet | 71e5676 | 2016-01-02 15:22:36 +0000 | [diff] [blame] | 816 | FixupCatchrets.push_back(CatchRet); |
| 817 | |
| 818 | for (CatchReturnInst *CatchRet : FixupCatchrets) |
| 819 | CatchRet->setSuccessor(NewBlock); |
| 820 | } |
| 821 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 822 | auto UpdatePHIOnClonedBlock = [&](PHINode *PN, bool IsForOldBlock) { |
| 823 | unsigned NumPreds = PN->getNumIncomingValues(); |
| 824 | for (unsigned PredIdx = 0, PredEnd = NumPreds; PredIdx != PredEnd; |
| 825 | ++PredIdx) { |
| 826 | BasicBlock *IncomingBlock = PN->getIncomingBlock(PredIdx); |
Joseph Tremoulet | 71e5676 | 2016-01-02 15:22:36 +0000 | [diff] [blame] | 827 | bool EdgeTargetsFunclet; |
| 828 | if (auto *CRI = |
| 829 | dyn_cast<CatchReturnInst>(IncomingBlock->getTerminator())) { |
Joseph Tremoulet | 44b3f96 | 2016-01-15 21:16:19 +0000 | [diff] [blame] | 830 | EdgeTargetsFunclet = (CRI->getCatchSwitchParentPad() == FuncletToken); |
Joseph Tremoulet | 71e5676 | 2016-01-02 15:22:36 +0000 | [diff] [blame] | 831 | } else { |
| 832 | ColorVector &IncomingColors = BlockColors[IncomingBlock]; |
| 833 | assert(!IncomingColors.empty() && "Block not colored!"); |
| 834 | assert((IncomingColors.size() == 1 || |
| 835 | llvm::all_of(IncomingColors, |
| 836 | [&](BasicBlock *Color) { |
| 837 | return Color != FuncletPadBB; |
| 838 | })) && |
| 839 | "Cloning should leave this funclet's blocks monochromatic"); |
| 840 | EdgeTargetsFunclet = (IncomingColors.front() == FuncletPadBB); |
| 841 | } |
| 842 | if (IsForOldBlock != EdgeTargetsFunclet) |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 843 | continue; |
| 844 | PN->removeIncomingValue(IncomingBlock, /*DeletePHIIfEmpty=*/false); |
| 845 | // Revisit the next entry. |
| 846 | --PredIdx; |
| 847 | --PredEnd; |
| 848 | } |
| 849 | }; |
| 850 | |
| 851 | for (auto &BBMapping : Orig2Clone) { |
| 852 | BasicBlock *OldBlock = BBMapping.first; |
| 853 | BasicBlock *NewBlock = BBMapping.second; |
Benjamin Kramer | c7fc81e | 2017-12-30 15:27:33 +0000 | [diff] [blame] | 854 | for (PHINode &OldPN : OldBlock->phis()) { |
| 855 | UpdatePHIOnClonedBlock(&OldPN, /*IsForOldBlock=*/true); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 856 | } |
Benjamin Kramer | c7fc81e | 2017-12-30 15:27:33 +0000 | [diff] [blame] | 857 | for (PHINode &NewPN : NewBlock->phis()) { |
| 858 | UpdatePHIOnClonedBlock(&NewPN, /*IsForOldBlock=*/false); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 859 | } |
| 860 | } |
| 861 | |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 862 | // Check to see if SuccBB has PHI nodes. If so, we need to add entries to |
| 863 | // the PHI nodes for NewBB now. |
| 864 | for (auto &BBMapping : Orig2Clone) { |
| 865 | BasicBlock *OldBlock = BBMapping.first; |
| 866 | BasicBlock *NewBlock = BBMapping.second; |
| 867 | for (BasicBlock *SuccBB : successors(NewBlock)) { |
Benjamin Kramer | c7fc81e | 2017-12-30 15:27:33 +0000 | [diff] [blame] | 868 | for (PHINode &SuccPN : SuccBB->phis()) { |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 869 | // Ok, we have a PHI node. Figure out what the incoming value was for |
| 870 | // the OldBlock. |
Benjamin Kramer | c7fc81e | 2017-12-30 15:27:33 +0000 | [diff] [blame] | 871 | int OldBlockIdx = SuccPN.getBasicBlockIndex(OldBlock); |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 872 | if (OldBlockIdx == -1) |
| 873 | break; |
Benjamin Kramer | c7fc81e | 2017-12-30 15:27:33 +0000 | [diff] [blame] | 874 | Value *IV = SuccPN.getIncomingValue(OldBlockIdx); |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 875 | |
| 876 | // Remap the value if necessary. |
| 877 | if (auto *Inst = dyn_cast<Instruction>(IV)) { |
| 878 | ValueToValueMapTy::iterator I = VMap.find(Inst); |
| 879 | if (I != VMap.end()) |
| 880 | IV = I->second; |
| 881 | } |
| 882 | |
Benjamin Kramer | c7fc81e | 2017-12-30 15:27:33 +0000 | [diff] [blame] | 883 | SuccPN.addIncoming(IV, NewBlock); |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | for (ValueToValueMapTy::value_type VT : VMap) { |
| 889 | // If there were values defined in BB that are used outside the funclet, |
| 890 | // then we now have to update all uses of the value to use either the |
| 891 | // original value, the cloned value, or some PHI derived value. This can |
| 892 | // require arbitrary PHI insertion, of which we are prepared to do, clean |
| 893 | // these up now. |
| 894 | SmallVector<Use *, 16> UsesToRename; |
| 895 | |
| 896 | auto *OldI = dyn_cast<Instruction>(const_cast<Value *>(VT.first)); |
| 897 | if (!OldI) |
| 898 | continue; |
| 899 | auto *NewI = cast<Instruction>(VT.second); |
| 900 | // Scan all uses of this instruction to see if it is used outside of its |
| 901 | // funclet, and if so, record them in UsesToRename. |
| 902 | for (Use &U : OldI->uses()) { |
| 903 | Instruction *UserI = cast<Instruction>(U.getUser()); |
| 904 | BasicBlock *UserBB = UserI->getParent(); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 905 | ColorVector &ColorsForUserBB = BlockColors[UserBB]; |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 906 | assert(!ColorsForUserBB.empty()); |
| 907 | if (ColorsForUserBB.size() > 1 || |
| 908 | *ColorsForUserBB.begin() != FuncletPadBB) |
| 909 | UsesToRename.push_back(&U); |
| 910 | } |
| 911 | |
| 912 | // If there are no uses outside the block, we're done with this |
| 913 | // instruction. |
| 914 | if (UsesToRename.empty()) |
| 915 | continue; |
| 916 | |
| 917 | // We found a use of OldI outside of the funclet. Rename all uses of OldI |
| 918 | // that are outside its funclet to be uses of the appropriate PHI node |
| 919 | // etc. |
| 920 | SSAUpdater SSAUpdate; |
| 921 | SSAUpdate.Initialize(OldI->getType(), OldI->getName()); |
| 922 | SSAUpdate.AddAvailableValue(OldI->getParent(), OldI); |
| 923 | SSAUpdate.AddAvailableValue(NewI->getParent(), NewI); |
| 924 | |
| 925 | while (!UsesToRename.empty()) |
| 926 | SSAUpdate.RewriteUseAfterInsertions(*UsesToRename.pop_back_val()); |
| 927 | } |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 928 | } |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 929 | } |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 930 | |
David Majnemer | 3bb88c0 | 2015-12-15 21:27:27 +0000 | [diff] [blame] | 931 | void WinEHPrepare::removeImplausibleInstructions(Function &F) { |
David Majnemer | 83f4bb2 | 2015-08-17 20:56:39 +0000 | [diff] [blame] | 932 | // Remove implausible terminators and replace them with UnreachableInst. |
| 933 | for (auto &Funclet : FuncletBlocks) { |
| 934 | BasicBlock *FuncletPadBB = Funclet.first; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 935 | std::vector<BasicBlock *> &BlocksInFunclet = Funclet.second; |
David Majnemer | 3bb88c0 | 2015-12-15 21:27:27 +0000 | [diff] [blame] | 936 | Instruction *FirstNonPHI = FuncletPadBB->getFirstNonPHI(); |
| 937 | auto *FuncletPad = dyn_cast<FuncletPadInst>(FirstNonPHI); |
| 938 | auto *CatchPad = dyn_cast_or_null<CatchPadInst>(FuncletPad); |
| 939 | auto *CleanupPad = dyn_cast_or_null<CleanupPadInst>(FuncletPad); |
David Majnemer | 83f4bb2 | 2015-08-17 20:56:39 +0000 | [diff] [blame] | 940 | |
| 941 | for (BasicBlock *BB : BlocksInFunclet) { |
David Majnemer | 3bb88c0 | 2015-12-15 21:27:27 +0000 | [diff] [blame] | 942 | for (Instruction &I : *BB) { |
| 943 | CallSite CS(&I); |
| 944 | if (!CS) |
| 945 | continue; |
| 946 | |
| 947 | Value *FuncletBundleOperand = nullptr; |
| 948 | if (auto BU = CS.getOperandBundle(LLVMContext::OB_funclet)) |
| 949 | FuncletBundleOperand = BU->Inputs.front(); |
| 950 | |
| 951 | if (FuncletBundleOperand == FuncletPad) |
| 952 | continue; |
| 953 | |
David Majnemer | 08dd52dc | 2016-02-26 00:04:25 +0000 | [diff] [blame] | 954 | // Skip call sites which are nounwind intrinsics or inline asm. |
David Majnemer | 3bb88c0 | 2015-12-15 21:27:27 +0000 | [diff] [blame] | 955 | auto *CalledFn = |
| 956 | dyn_cast<Function>(CS.getCalledValue()->stripPointerCasts()); |
Justin Lebar | 9cbc301 | 2016-07-28 23:58:15 +0000 | [diff] [blame] | 957 | if (CalledFn && ((CalledFn->isIntrinsic() && CS.doesNotThrow()) || |
| 958 | CS.isInlineAsm())) |
David Majnemer | 3bb88c0 | 2015-12-15 21:27:27 +0000 | [diff] [blame] | 959 | continue; |
| 960 | |
| 961 | // This call site was not part of this funclet, remove it. |
| 962 | if (CS.isInvoke()) { |
| 963 | // Remove the unwind edge if it was an invoke. |
| 964 | removeUnwindEdge(BB); |
| 965 | // Get a pointer to the new call. |
| 966 | BasicBlock::iterator CallI = |
| 967 | std::prev(BB->getTerminator()->getIterator()); |
| 968 | auto *CI = cast<CallInst>(&*CallI); |
David Majnemer | e14e7bc | 2016-06-25 08:19:55 +0000 | [diff] [blame] | 969 | changeToUnreachable(CI, /*UseLLVMTrap=*/false); |
David Majnemer | 3bb88c0 | 2015-12-15 21:27:27 +0000 | [diff] [blame] | 970 | } else { |
David Majnemer | e14e7bc | 2016-06-25 08:19:55 +0000 | [diff] [blame] | 971 | changeToUnreachable(&I, /*UseLLVMTrap=*/false); |
David Majnemer | 3bb88c0 | 2015-12-15 21:27:27 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | // There are no more instructions in the block (except for unreachable), |
| 975 | // we are done. |
| 976 | break; |
| 977 | } |
| 978 | |
Chandler Carruth | edb12a8 | 2018-10-15 10:04:59 +0000 | [diff] [blame] | 979 | Instruction *TI = BB->getTerminator(); |
David Majnemer | 83f4bb2 | 2015-08-17 20:56:39 +0000 | [diff] [blame] | 980 | // CatchPadInst and CleanupPadInst can't transfer control to a ReturnInst. |
David Majnemer | 3bb88c0 | 2015-12-15 21:27:27 +0000 | [diff] [blame] | 981 | bool IsUnreachableRet = isa<ReturnInst>(TI) && FuncletPad; |
David Majnemer | 83f4bb2 | 2015-08-17 20:56:39 +0000 | [diff] [blame] | 982 | // The token consumed by a CatchReturnInst must match the funclet token. |
| 983 | bool IsUnreachableCatchret = false; |
| 984 | if (auto *CRI = dyn_cast<CatchReturnInst>(TI)) |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 985 | IsUnreachableCatchret = CRI->getCatchPad() != CatchPad; |
Joseph Tremoulet | 9ce71f7 | 2015-09-03 09:09:43 +0000 | [diff] [blame] | 986 | // The token consumed by a CleanupReturnInst must match the funclet token. |
David Majnemer | 83f4bb2 | 2015-08-17 20:56:39 +0000 | [diff] [blame] | 987 | bool IsUnreachableCleanupret = false; |
| 988 | if (auto *CRI = dyn_cast<CleanupReturnInst>(TI)) |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 989 | IsUnreachableCleanupret = CRI->getCleanupPad() != CleanupPad; |
Joseph Tremoulet | 9ce71f7 | 2015-09-03 09:09:43 +0000 | [diff] [blame] | 990 | if (IsUnreachableRet || IsUnreachableCatchret || |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 991 | IsUnreachableCleanupret) { |
David Majnemer | e14e7bc | 2016-06-25 08:19:55 +0000 | [diff] [blame] | 992 | changeToUnreachable(TI, /*UseLLVMTrap=*/false); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 993 | } else if (isa<InvokeInst>(TI)) { |
David Majnemer | 3bb88c0 | 2015-12-15 21:27:27 +0000 | [diff] [blame] | 994 | if (Personality == EHPersonality::MSVC_CXX && CleanupPad) { |
| 995 | // Invokes within a cleanuppad for the MSVC++ personality never |
| 996 | // transfer control to their unwind edge: the personality will |
| 997 | // terminate the program. |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 998 | removeUnwindEdge(BB); |
David Majnemer | 3bb88c0 | 2015-12-15 21:27:27 +0000 | [diff] [blame] | 999 | } |
David Majnemer | 83f4bb2 | 2015-08-17 20:56:39 +0000 | [diff] [blame] | 1000 | } |
| 1001 | } |
| 1002 | } |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 1003 | } |
David Majnemer | 83f4bb2 | 2015-08-17 20:56:39 +0000 | [diff] [blame] | 1004 | |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 1005 | void WinEHPrepare::cleanupPreparedFunclets(Function &F) { |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 1006 | // Clean-up some of the mess we made by removing useles PHI nodes, trivial |
| 1007 | // branches, etc. |
| 1008 | 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] | 1009 | BasicBlock *BB = &*FI++; |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 1010 | SimplifyInstructionsInBlock(BB); |
| 1011 | ConstantFoldTerminator(BB, /*DeleteDeadConditions=*/true); |
| 1012 | MergeBlockIntoPredecessor(BB); |
| 1013 | } |
| 1014 | |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 1015 | // We might have some unreachable blocks after cleaning up some impossible |
| 1016 | // control flow. |
| 1017 | removeUnreachableBlocks(F); |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 1018 | } |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 1019 | |
Florian Hahn | 6b3216a | 2017-07-31 10:07:49 +0000 | [diff] [blame] | 1020 | #ifndef NDEBUG |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 1021 | void WinEHPrepare::verifyPreparedFunclets(Function &F) { |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 1022 | for (BasicBlock &BB : F) { |
| 1023 | size_t NumColors = BlockColors[&BB].size(); |
| 1024 | assert(NumColors == 1 && "Expected monochromatic BB!"); |
| 1025 | if (NumColors == 0) |
| 1026 | report_fatal_error("Uncolored BB!"); |
| 1027 | if (NumColors > 1) |
| 1028 | report_fatal_error("Multicolor BB!"); |
NAKAMURA Takumi | ded575e | 2016-01-03 01:41:00 +0000 | [diff] [blame] | 1029 | assert((DisableDemotion || !(BB.isEHPad() && isa<PHINode>(BB.begin()))) && |
| 1030 | "EH Pad still has a PHI!"); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 1031 | } |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 1032 | } |
Florian Hahn | 6b3216a | 2017-07-31 10:07:49 +0000 | [diff] [blame] | 1033 | #endif |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 1034 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1035 | bool WinEHPrepare::prepareExplicitEH(Function &F) { |
| 1036 | // Remove unreachable blocks. It is not valuable to assign them a color and |
| 1037 | // their existence can trick us into thinking values are alive when they are |
| 1038 | // not. |
| 1039 | removeUnreachableBlocks(F); |
| 1040 | |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 1041 | // Determine which blocks are reachable from which funclet entries. |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1042 | colorFunclets(F); |
| 1043 | |
| 1044 | cloneCommonBlocks(F); |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 1045 | |
Reid Kleckner | cc2f6c3 | 2015-11-19 23:23:33 +0000 | [diff] [blame] | 1046 | if (!DisableDemotion) |
Heejin Ahn | 99d60e0 | 2018-05-31 22:02:34 +0000 | [diff] [blame] | 1047 | demotePHIsOnFunclets(F, DemoteCatchSwitchPHIOnly || |
| 1048 | DemoteCatchSwitchPHIOnlyOpt); |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 1049 | |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 1050 | if (!DisableCleanups) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1051 | LLVM_DEBUG(verifyFunction(F)); |
David Majnemer | 3bb88c0 | 2015-12-15 21:27:27 +0000 | [diff] [blame] | 1052 | removeImplausibleInstructions(F); |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 1053 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1054 | LLVM_DEBUG(verifyFunction(F)); |
David Majnemer | 459a64a | 2015-09-16 18:40:37 +0000 | [diff] [blame] | 1055 | cleanupPreparedFunclets(F); |
| 1056 | } |
David Majnemer | b3d9b96 | 2015-09-16 18:40:24 +0000 | [diff] [blame] | 1057 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1058 | LLVM_DEBUG(verifyPreparedFunclets(F)); |
David Majnemer | dbdc9c2 | 2016-01-02 09:26:36 +0000 | [diff] [blame] | 1059 | // Recolor the CFG to verify that all is well. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1060 | LLVM_DEBUG(colorFunclets(F)); |
| 1061 | LLVM_DEBUG(verifyPreparedFunclets(F)); |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 1062 | |
| 1063 | BlockColors.clear(); |
| 1064 | FuncletBlocks.clear(); |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 1065 | |
David Majnemer | fd9f477 | 2015-08-11 01:15:26 +0000 | [diff] [blame] | 1066 | return true; |
| 1067 | } |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 1068 | |
| 1069 | // TODO: Share loads when one use dominates another, or when a catchpad exit |
| 1070 | // dominates uses (needs dominators). |
| 1071 | AllocaInst *WinEHPrepare::insertPHILoads(PHINode *PN, Function &F) { |
| 1072 | BasicBlock *PHIBlock = PN->getParent(); |
| 1073 | AllocaInst *SpillSlot = nullptr; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1074 | Instruction *EHPad = PHIBlock->getFirstNonPHI(); |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 1075 | |
Chandler Carruth | 9ae926b | 2018-08-26 09:51:22 +0000 | [diff] [blame] | 1076 | if (!EHPad->isTerminator()) { |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1077 | // If the EHPad isn't a terminator, then we can insert a load in this block |
| 1078 | // that will dominate all uses. |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1079 | SpillSlot = new AllocaInst(PN->getType(), DL->getAllocaAddrSpace(), nullptr, |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 1080 | Twine(PN->getName(), ".wineh.spillslot"), |
Duncan P. N. Exon Smith | f1ff53e | 2015-10-09 22:56:24 +0000 | [diff] [blame] | 1081 | &F.getEntryBlock().front()); |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 1082 | Value *V = new LoadInst(PN->getType(), SpillSlot, |
| 1083 | Twine(PN->getName(), ".wineh.reload"), |
Duncan P. N. Exon Smith | f1ff53e | 2015-10-09 22:56:24 +0000 | [diff] [blame] | 1084 | &*PHIBlock->getFirstInsertionPt()); |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 1085 | PN->replaceAllUsesWith(V); |
| 1086 | return SpillSlot; |
| 1087 | } |
| 1088 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1089 | // Otherwise, we have a PHI on a terminator EHPad, and we give up and insert |
| 1090 | // loads of the slot before every use. |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 1091 | DenseMap<BasicBlock *, Value *> Loads; |
| 1092 | for (Value::use_iterator UI = PN->use_begin(), UE = PN->use_end(); |
| 1093 | UI != UE;) { |
| 1094 | Use &U = *UI++; |
| 1095 | auto *UsingInst = cast<Instruction>(U.getUser()); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1096 | if (isa<PHINode>(UsingInst) && UsingInst->getParent()->isEHPad()) { |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 1097 | // Use is on an EH pad phi. Leave it alone; we'll insert loads and |
| 1098 | // stores for it separately. |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 1099 | continue; |
| 1100 | } |
| 1101 | replaceUseWithLoad(PN, U, SpillSlot, Loads, F); |
| 1102 | } |
| 1103 | return SpillSlot; |
| 1104 | } |
| 1105 | |
| 1106 | // TODO: improve store placement. Inserting at def is probably good, but need |
| 1107 | // to be careful not to introduce interfering stores (needs liveness analysis). |
| 1108 | // TODO: identify related phi nodes that can share spill slots, and share them |
| 1109 | // (also needs liveness). |
| 1110 | void WinEHPrepare::insertPHIStores(PHINode *OriginalPHI, |
| 1111 | AllocaInst *SpillSlot) { |
| 1112 | // Use a worklist of (Block, Value) pairs -- the given Value needs to be |
| 1113 | // stored to the spill slot by the end of the given Block. |
| 1114 | SmallVector<std::pair<BasicBlock *, Value *>, 4> Worklist; |
| 1115 | |
| 1116 | Worklist.push_back({OriginalPHI->getParent(), OriginalPHI}); |
| 1117 | |
| 1118 | while (!Worklist.empty()) { |
| 1119 | BasicBlock *EHBlock; |
| 1120 | Value *InVal; |
| 1121 | std::tie(EHBlock, InVal) = Worklist.pop_back_val(); |
| 1122 | |
| 1123 | PHINode *PN = dyn_cast<PHINode>(InVal); |
| 1124 | if (PN && PN->getParent() == EHBlock) { |
| 1125 | // The value is defined by another PHI we need to remove, with no room to |
| 1126 | // insert a store after the PHI, so each predecessor needs to store its |
| 1127 | // incoming value. |
| 1128 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i) { |
| 1129 | Value *PredVal = PN->getIncomingValue(i); |
| 1130 | |
| 1131 | // Undef can safely be skipped. |
| 1132 | if (isa<UndefValue>(PredVal)) |
| 1133 | continue; |
| 1134 | |
| 1135 | insertPHIStore(PN->getIncomingBlock(i), PredVal, SpillSlot, Worklist); |
| 1136 | } |
| 1137 | } else { |
| 1138 | // We need to store InVal, which dominates EHBlock, but can't put a store |
| 1139 | // in EHBlock, so need to put stores in each predecessor. |
| 1140 | for (BasicBlock *PredBlock : predecessors(EHBlock)) { |
| 1141 | insertPHIStore(PredBlock, InVal, SpillSlot, Worklist); |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | void WinEHPrepare::insertPHIStore( |
| 1148 | BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot, |
| 1149 | SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist) { |
| 1150 | |
Chandler Carruth | 9ae926b | 2018-08-26 09:51:22 +0000 | [diff] [blame] | 1151 | if (PredBlock->isEHPad() && PredBlock->getFirstNonPHI()->isTerminator()) { |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 1152 | // Pred is unsplittable, so we need to queue it on the worklist. |
| 1153 | Worklist.push_back({PredBlock, PredVal}); |
| 1154 | return; |
| 1155 | } |
| 1156 | |
| 1157 | // Otherwise, insert the store at the end of the basic block. |
| 1158 | new StoreInst(PredVal, SpillSlot, PredBlock->getTerminator()); |
| 1159 | } |
| 1160 | |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 1161 | void WinEHPrepare::replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot, |
| 1162 | DenseMap<BasicBlock *, Value *> &Loads, |
| 1163 | Function &F) { |
| 1164 | // Lazilly create the spill slot. |
| 1165 | if (!SpillSlot) |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1166 | SpillSlot = new AllocaInst(V->getType(), DL->getAllocaAddrSpace(), nullptr, |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 1167 | Twine(V->getName(), ".wineh.spillslot"), |
Duncan P. N. Exon Smith | f1ff53e | 2015-10-09 22:56:24 +0000 | [diff] [blame] | 1168 | &F.getEntryBlock().front()); |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 1169 | |
| 1170 | auto *UsingInst = cast<Instruction>(U.getUser()); |
| 1171 | if (auto *UsingPHI = dyn_cast<PHINode>(UsingInst)) { |
| 1172 | // If this is a PHI node, we can't insert a load of the value before |
| 1173 | // the use. Instead insert the load in the predecessor block |
| 1174 | // corresponding to the incoming value. |
| 1175 | // |
| 1176 | // Note that if there are multiple edges from a basic block to this |
| 1177 | // PHI node that we cannot have multiple loads. The problem is that |
| 1178 | // the resulting PHI node will have multiple values (from each load) |
| 1179 | // coming in from the same block, which is illegal SSA form. |
| 1180 | // For this reason, we keep track of and reuse loads we insert. |
| 1181 | BasicBlock *IncomingBlock = UsingPHI->getIncomingBlock(U); |
Joseph Tremoulet | 7031c9f | 2015-08-17 13:51:37 +0000 | [diff] [blame] | 1182 | if (auto *CatchRet = |
| 1183 | dyn_cast<CatchReturnInst>(IncomingBlock->getTerminator())) { |
| 1184 | // Putting a load above a catchret and use on the phi would still leave |
| 1185 | // a cross-funclet def/use. We need to split the edge, change the |
| 1186 | // catchret to target the new block, and put the load there. |
| 1187 | BasicBlock *PHIBlock = UsingInst->getParent(); |
| 1188 | BasicBlock *NewBlock = SplitEdge(IncomingBlock, PHIBlock); |
| 1189 | // SplitEdge gives us: |
| 1190 | // IncomingBlock: |
| 1191 | // ... |
| 1192 | // br label %NewBlock |
| 1193 | // NewBlock: |
| 1194 | // catchret label %PHIBlock |
| 1195 | // But we need: |
| 1196 | // IncomingBlock: |
| 1197 | // ... |
| 1198 | // catchret label %NewBlock |
| 1199 | // NewBlock: |
| 1200 | // br label %PHIBlock |
| 1201 | // So move the terminators to each others' blocks and swap their |
| 1202 | // successors. |
| 1203 | BranchInst *Goto = cast<BranchInst>(IncomingBlock->getTerminator()); |
| 1204 | Goto->removeFromParent(); |
| 1205 | CatchRet->removeFromParent(); |
| 1206 | IncomingBlock->getInstList().push_back(CatchRet); |
| 1207 | NewBlock->getInstList().push_back(Goto); |
| 1208 | Goto->setSuccessor(0, PHIBlock); |
| 1209 | CatchRet->setSuccessor(NewBlock); |
| 1210 | // Update the color mapping for the newly split edge. |
Andrew Kaylor | ce3bcae | 2016-12-14 19:30:18 +0000 | [diff] [blame] | 1211 | // Grab a reference to the ColorVector to be inserted before getting the |
| 1212 | // reference to the vector we are copying because inserting the new |
| 1213 | // element in BlockColors might cause the map to be reallocated. |
| 1214 | ColorVector &ColorsForNewBlock = BlockColors[NewBlock]; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1215 | ColorVector &ColorsForPHIBlock = BlockColors[PHIBlock]; |
Andrew Kaylor | ce3bcae | 2016-12-14 19:30:18 +0000 | [diff] [blame] | 1216 | ColorsForNewBlock = ColorsForPHIBlock; |
Joseph Tremoulet | 7031c9f | 2015-08-17 13:51:37 +0000 | [diff] [blame] | 1217 | for (BasicBlock *FuncletPad : ColorsForPHIBlock) |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1218 | FuncletBlocks[FuncletPad].push_back(NewBlock); |
Joseph Tremoulet | 7031c9f | 2015-08-17 13:51:37 +0000 | [diff] [blame] | 1219 | // Treat the new block as incoming for load insertion. |
| 1220 | IncomingBlock = NewBlock; |
| 1221 | } |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 1222 | Value *&Load = Loads[IncomingBlock]; |
| 1223 | // Insert the load into the predecessor block |
| 1224 | if (!Load) |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 1225 | Load = new LoadInst(V->getType(), SpillSlot, |
| 1226 | Twine(V->getName(), ".wineh.reload"), |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 1227 | /*isVolatile=*/false, IncomingBlock->getTerminator()); |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 1228 | |
| 1229 | U.set(Load); |
| 1230 | } else { |
| 1231 | // Reload right before the old use. |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 1232 | auto *Load = new LoadInst(V->getType(), SpillSlot, |
| 1233 | Twine(V->getName(), ".wineh.reload"), |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 1234 | /*isVolatile=*/false, UsingInst); |
Joseph Tremoulet | c9ff914 | 2015-08-13 14:30:10 +0000 | [diff] [blame] | 1235 | U.set(Load); |
| 1236 | } |
| 1237 | } |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 1238 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1239 | void WinEHFuncInfo::addIPToStateRange(const InvokeInst *II, |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 1240 | MCSymbol *InvokeBegin, |
| 1241 | MCSymbol *InvokeEnd) { |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1242 | assert(InvokeStateMap.count(II) && |
| 1243 | "should get invoke with precomputed state"); |
| 1244 | LabelToStateMap[InvokeBegin] = std::make_pair(InvokeStateMap[II], InvokeEnd); |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 1245 | } |
Chandler Carruth | e011534 | 2015-12-29 09:24:39 +0000 | [diff] [blame] | 1246 | |
| 1247 | WinEHFuncInfo::WinEHFuncInfo() {} |