[MachineScheduler] Fix physreg dependencies of ExitSU (#123541)
[llvm-project.git] / llvm / lib / CodeGen / WinEHPrepare.cpp
blobb98523cac1f2fc5fa53cd2d8fac245aa9b340323
1 //===-- WinEHPrepare - Prepare exception handling for code generation ---===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass lowers LLVM IR exception handling into something closer to what the
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.
16 //===----------------------------------------------------------------------===//
18 #include "llvm/CodeGen/WinEHPrepare.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/MapVector.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/CodeGen/MachineBasicBlock.h"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/CodeGen/WinEHFuncInfo.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/EHPersonalities.h"
27 #include "llvm/IR/Instructions.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/IR/Verifier.h"
30 #include "llvm/InitializePasses.h"
31 #include "llvm/Pass.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/TargetParser/Triple.h"
36 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
37 #include "llvm/Transforms/Utils/Cloning.h"
38 #include "llvm/Transforms/Utils/Local.h"
39 #include "llvm/Transforms/Utils/SSAUpdater.h"
41 using namespace llvm;
43 #define DEBUG_TYPE "win-eh-prepare"
45 static cl::opt<bool> DisableDemotion(
46 "disable-demotion", cl::Hidden,
47 cl::desc(
48 "Clone multicolor basic blocks but do not demote cross scopes"),
49 cl::init(false));
51 static cl::opt<bool> DisableCleanups(
52 "disable-cleanups", cl::Hidden,
53 cl::desc("Do not remove implausible terminators or other similar cleanups"),
54 cl::init(false));
56 // TODO: Remove this option when we fully migrate to new pass manager
57 static cl::opt<bool> DemoteCatchSwitchPHIOnlyOpt(
58 "demote-catchswitch-only", cl::Hidden,
59 cl::desc("Demote catchswitch BBs only (for wasm EH)"), cl::init(false));
61 namespace {
63 class WinEHPrepareImpl {
64 public:
65 WinEHPrepareImpl(bool DemoteCatchSwitchPHIOnly)
66 : DemoteCatchSwitchPHIOnly(DemoteCatchSwitchPHIOnly) {}
68 bool runOnFunction(Function &Fn);
70 private:
71 void insertPHIStores(PHINode *OriginalPHI, AllocaInst *SpillSlot);
72 void
73 insertPHIStore(BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot,
74 SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist);
75 AllocaInst *insertPHILoads(PHINode *PN, Function &F);
76 void replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
77 DenseMap<BasicBlock *, Value *> &Loads, Function &F);
78 bool prepareExplicitEH(Function &F);
79 void colorFunclets(Function &F);
81 void demotePHIsOnFunclets(Function &F, bool DemoteCatchSwitchPHIOnly);
82 void cloneCommonBlocks(Function &F);
83 void removeImplausibleInstructions(Function &F);
84 void cleanupPreparedFunclets(Function &F);
85 void verifyPreparedFunclets(Function &F);
87 bool DemoteCatchSwitchPHIOnly;
89 // All fields are reset by runOnFunction.
90 EHPersonality Personality = EHPersonality::Unknown;
92 const DataLayout *DL = nullptr;
93 DenseMap<BasicBlock *, ColorVector> BlockColors;
94 MapVector<BasicBlock *, std::vector<BasicBlock *>> FuncletBlocks;
97 class WinEHPrepare : public FunctionPass {
98 bool DemoteCatchSwitchPHIOnly;
100 public:
101 static char ID; // Pass identification, replacement for typeid.
103 WinEHPrepare(bool DemoteCatchSwitchPHIOnly = false)
104 : FunctionPass(ID), DemoteCatchSwitchPHIOnly(DemoteCatchSwitchPHIOnly) {}
106 StringRef getPassName() const override {
107 return "Windows exception handling preparation";
110 bool runOnFunction(Function &Fn) override {
111 return WinEHPrepareImpl(DemoteCatchSwitchPHIOnly).runOnFunction(Fn);
115 } // end anonymous namespace
117 PreservedAnalyses WinEHPreparePass::run(Function &F,
118 FunctionAnalysisManager &) {
119 bool Changed = WinEHPrepareImpl(DemoteCatchSwitchPHIOnly).runOnFunction(F);
120 return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
123 char WinEHPrepare::ID = 0;
124 INITIALIZE_PASS(WinEHPrepare, DEBUG_TYPE, "Prepare Windows exceptions", false,
125 false)
127 FunctionPass *llvm::createWinEHPass(bool DemoteCatchSwitchPHIOnly) {
128 return new WinEHPrepare(DemoteCatchSwitchPHIOnly);
131 bool WinEHPrepareImpl::runOnFunction(Function &Fn) {
132 if (!Fn.hasPersonalityFn())
133 return false;
135 // Classify the personality to see what kind of preparation we need.
136 Personality = classifyEHPersonality(Fn.getPersonalityFn());
138 // Do nothing if this is not a scope-based personality.
139 if (!isScopedEHPersonality(Personality))
140 return false;
142 DL = &Fn.getDataLayout();
143 return prepareExplicitEH(Fn);
146 static int addUnwindMapEntry(WinEHFuncInfo &FuncInfo, int ToState,
147 const BasicBlock *BB) {
148 CxxUnwindMapEntry UME;
149 UME.ToState = ToState;
150 UME.Cleanup = BB;
151 FuncInfo.CxxUnwindMap.push_back(UME);
152 return FuncInfo.getLastStateNumber();
155 static void addTryBlockMapEntry(WinEHFuncInfo &FuncInfo, int TryLow,
156 int TryHigh, int CatchHigh,
157 ArrayRef<const CatchPadInst *> Handlers) {
158 WinEHTryBlockMapEntry TBME;
159 TBME.TryLow = TryLow;
160 TBME.TryHigh = TryHigh;
161 TBME.CatchHigh = CatchHigh;
162 assert(TBME.TryLow <= TBME.TryHigh);
163 for (const CatchPadInst *CPI : Handlers) {
164 WinEHHandlerType HT;
165 Constant *TypeInfo = cast<Constant>(CPI->getArgOperand(0));
166 if (TypeInfo->isNullValue())
167 HT.TypeDescriptor = nullptr;
168 else
169 HT.TypeDescriptor = cast<GlobalVariable>(TypeInfo->stripPointerCasts());
170 HT.Adjectives = cast<ConstantInt>(CPI->getArgOperand(1))->getZExtValue();
171 HT.Handler = CPI->getParent();
172 if (auto *AI =
173 dyn_cast<AllocaInst>(CPI->getArgOperand(2)->stripPointerCasts()))
174 HT.CatchObj.Alloca = AI;
175 else
176 HT.CatchObj.Alloca = nullptr;
177 TBME.HandlerArray.push_back(HT);
179 FuncInfo.TryBlockMap.push_back(TBME);
182 static BasicBlock *getCleanupRetUnwindDest(const CleanupPadInst *CleanupPad) {
183 for (const User *U : CleanupPad->users())
184 if (const auto *CRI = dyn_cast<CleanupReturnInst>(U))
185 return CRI->getUnwindDest();
186 return nullptr;
189 static void calculateStateNumbersForInvokes(const Function *Fn,
190 WinEHFuncInfo &FuncInfo) {
191 auto *F = const_cast<Function *>(Fn);
192 DenseMap<BasicBlock *, ColorVector> BlockColors = colorEHFunclets(*F);
193 for (BasicBlock &BB : *F) {
194 auto *II = dyn_cast<InvokeInst>(BB.getTerminator());
195 if (!II)
196 continue;
198 auto &BBColors = BlockColors[&BB];
199 assert(BBColors.size() == 1 && "multi-color BB not removed by preparation");
200 BasicBlock *FuncletEntryBB = BBColors.front();
202 BasicBlock *FuncletUnwindDest;
203 auto *FuncletPad =
204 dyn_cast<FuncletPadInst>(FuncletEntryBB->getFirstNonPHIIt());
205 assert(FuncletPad || FuncletEntryBB == &Fn->getEntryBlock());
206 if (!FuncletPad)
207 FuncletUnwindDest = nullptr;
208 else if (auto *CatchPad = dyn_cast<CatchPadInst>(FuncletPad))
209 FuncletUnwindDest = CatchPad->getCatchSwitch()->getUnwindDest();
210 else if (auto *CleanupPad = dyn_cast<CleanupPadInst>(FuncletPad))
211 FuncletUnwindDest = getCleanupRetUnwindDest(CleanupPad);
212 else
213 llvm_unreachable("unexpected funclet pad!");
215 BasicBlock *InvokeUnwindDest = II->getUnwindDest();
216 int BaseState = -1;
217 if (FuncletUnwindDest == InvokeUnwindDest) {
218 auto BaseStateI = FuncInfo.FuncletBaseStateMap.find(FuncletPad);
219 if (BaseStateI != FuncInfo.FuncletBaseStateMap.end())
220 BaseState = BaseStateI->second;
223 if (BaseState != -1) {
224 FuncInfo.InvokeStateMap[II] = BaseState;
225 } else {
226 Instruction *PadInst = &*InvokeUnwindDest->getFirstNonPHIIt();
227 assert(FuncInfo.EHPadStateMap.count(PadInst) && "EH Pad has no state!");
228 FuncInfo.InvokeStateMap[II] = FuncInfo.EHPadStateMap[PadInst];
233 // See comments below for calculateSEHStateForAsynchEH().
234 // State - incoming State of normal paths
235 struct WorkItem {
236 const BasicBlock *Block;
237 int State;
238 WorkItem(const BasicBlock *BB, int St) {
239 Block = BB;
240 State = St;
243 void llvm::calculateCXXStateForAsynchEH(const BasicBlock *BB, int State,
244 WinEHFuncInfo &EHInfo) {
245 SmallVector<struct WorkItem *, 8> WorkList;
246 struct WorkItem *WI = new WorkItem(BB, State);
247 WorkList.push_back(WI);
249 while (!WorkList.empty()) {
250 WI = WorkList.pop_back_val();
251 const BasicBlock *BB = WI->Block;
252 int State = WI->State;
253 delete WI;
254 if (auto It = EHInfo.BlockToStateMap.find(BB);
255 It != EHInfo.BlockToStateMap.end() && It->second <= State)
256 continue; // skip blocks already visited by lower State
258 BasicBlock::const_iterator It = BB->getFirstNonPHIIt();
259 const llvm::Instruction *TI = BB->getTerminator();
260 if (It->isEHPad())
261 State = EHInfo.EHPadStateMap[&*It];
262 EHInfo.BlockToStateMap[BB] = State; // Record state, also flag visiting
264 if ((isa<CleanupReturnInst>(TI) || isa<CatchReturnInst>(TI)) && State > 0) {
265 // Retrive the new State
266 State = EHInfo.CxxUnwindMap[State].ToState; // Retrive next State
267 } else if (isa<InvokeInst>(TI)) {
268 auto *Call = cast<CallBase>(TI);
269 const Function *Fn = Call->getCalledFunction();
270 if (Fn && Fn->isIntrinsic() &&
271 (Fn->getIntrinsicID() == Intrinsic::seh_scope_begin ||
272 Fn->getIntrinsicID() == Intrinsic::seh_try_begin))
273 // Retrive the new State from seh_scope_begin
274 State = EHInfo.InvokeStateMap[cast<InvokeInst>(TI)];
275 else if (Fn && Fn->isIntrinsic() &&
276 (Fn->getIntrinsicID() == Intrinsic::seh_scope_end ||
277 Fn->getIntrinsicID() == Intrinsic::seh_try_end)) {
278 // In case of conditional ctor, let's retrieve State from Invoke
279 State = EHInfo.InvokeStateMap[cast<InvokeInst>(TI)];
280 // end of current state, retrive new state from UnwindMap
281 State = EHInfo.CxxUnwindMap[State].ToState;
284 // Continue push successors into worklist
285 for (auto *SuccBB : successors(BB)) {
286 WI = new WorkItem(SuccBB, State);
287 WorkList.push_back(WI);
292 // The central theory of this routine is based on the following:
293 // A _try scope is always a SEME (Single Entry Multiple Exits) region
294 // as jumping into a _try is not allowed
295 // The single entry must start with a seh_try_begin() invoke with a
296 // correct State number that is the initial state of the SEME.
297 // Through control-flow, state number is propagated into all blocks.
298 // Side exits marked by seh_try_end() will unwind to parent state via
299 // existing SEHUnwindMap[].
300 // Side exits can ONLY jump into parent scopes (lower state number).
301 // Thus, when a block succeeds various states from its predecessors,
302 // the lowest State trumphs others.
303 // If some exits flow to unreachable, propagation on those paths terminate,
304 // not affecting remaining blocks.
305 void llvm::calculateSEHStateForAsynchEH(const BasicBlock *BB, int State,
306 WinEHFuncInfo &EHInfo) {
307 SmallVector<struct WorkItem *, 8> WorkList;
308 struct WorkItem *WI = new WorkItem(BB, State);
309 WorkList.push_back(WI);
311 while (!WorkList.empty()) {
312 WI = WorkList.pop_back_val();
313 const BasicBlock *BB = WI->Block;
314 int State = WI->State;
315 delete WI;
316 if (auto It = EHInfo.BlockToStateMap.find(BB);
317 It != EHInfo.BlockToStateMap.end() && It->second <= State)
318 continue; // skip blocks already visited by lower State
320 BasicBlock::const_iterator It = BB->getFirstNonPHIIt();
321 const llvm::Instruction *TI = BB->getTerminator();
322 if (It->isEHPad())
323 State = EHInfo.EHPadStateMap[&*It];
324 EHInfo.BlockToStateMap[BB] = State; // Record state
326 if (isa<CatchPadInst>(It) && isa<CatchReturnInst>(TI)) {
327 const Constant *FilterOrNull = cast<Constant>(
328 cast<CatchPadInst>(It)->getArgOperand(0)->stripPointerCasts());
329 const Function *Filter = dyn_cast<Function>(FilterOrNull);
330 if (!Filter || !Filter->getName().starts_with("__IsLocalUnwind"))
331 State = EHInfo.SEHUnwindMap[State].ToState; // Retrive next State
332 } else if ((isa<CleanupReturnInst>(TI) || isa<CatchReturnInst>(TI)) &&
333 State > 0) {
334 // Retrive the new State.
335 State = EHInfo.SEHUnwindMap[State].ToState; // Retrive next State
336 } else if (isa<InvokeInst>(TI)) {
337 auto *Call = cast<CallBase>(TI);
338 const Function *Fn = Call->getCalledFunction();
339 if (Fn && Fn->isIntrinsic() &&
340 Fn->getIntrinsicID() == Intrinsic::seh_try_begin)
341 // Retrive the new State from seh_try_begin
342 State = EHInfo.InvokeStateMap[cast<InvokeInst>(TI)];
343 else if (Fn && Fn->isIntrinsic() &&
344 Fn->getIntrinsicID() == Intrinsic::seh_try_end)
345 // end of current state, retrive new state from UnwindMap
346 State = EHInfo.SEHUnwindMap[State].ToState;
348 // Continue push successors into worklist
349 for (auto *SuccBB : successors(BB)) {
350 WI = new WorkItem(SuccBB, State);
351 WorkList.push_back(WI);
356 // Given BB which ends in an unwind edge, return the EHPad that this BB belongs
357 // to. If the unwind edge came from an invoke, return null.
358 static const BasicBlock *getEHPadFromPredecessor(const BasicBlock *BB,
359 Value *ParentPad) {
360 const Instruction *TI = BB->getTerminator();
361 if (isa<InvokeInst>(TI))
362 return nullptr;
363 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(TI)) {
364 if (CatchSwitch->getParentPad() != ParentPad)
365 return nullptr;
366 return BB;
368 assert(!TI->isEHPad() && "unexpected EHPad!");
369 auto *CleanupPad = cast<CleanupReturnInst>(TI)->getCleanupPad();
370 if (CleanupPad->getParentPad() != ParentPad)
371 return nullptr;
372 return CleanupPad->getParent();
375 // Starting from a EHPad, Backward walk through control-flow graph
376 // to produce two primary outputs:
377 // FuncInfo.EHPadStateMap[] and FuncInfo.CxxUnwindMap[]
378 static void calculateCXXStateNumbers(WinEHFuncInfo &FuncInfo,
379 const Instruction *FirstNonPHI,
380 int ParentState) {
381 const BasicBlock *BB = FirstNonPHI->getParent();
382 assert(BB->isEHPad() && "not a funclet!");
384 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(FirstNonPHI)) {
385 assert(FuncInfo.EHPadStateMap.count(CatchSwitch) == 0 &&
386 "shouldn't revist catch funclets!");
388 SmallVector<const CatchPadInst *, 2> Handlers;
389 for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
390 auto *CatchPad = cast<CatchPadInst>(CatchPadBB->getFirstNonPHIIt());
391 Handlers.push_back(CatchPad);
393 int TryLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr);
394 FuncInfo.EHPadStateMap[CatchSwitch] = TryLow;
395 for (const BasicBlock *PredBlock : predecessors(BB))
396 if ((PredBlock = getEHPadFromPredecessor(PredBlock,
397 CatchSwitch->getParentPad())))
398 calculateCXXStateNumbers(FuncInfo, &*PredBlock->getFirstNonPHIIt(),
399 TryLow);
400 int CatchLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr);
402 // catchpads are separate funclets in C++ EH due to the way rethrow works.
403 int TryHigh = CatchLow - 1;
405 // MSVC FrameHandler3/4 on x64&Arm64 expect Catch Handlers in $tryMap$
406 // stored in pre-order (outer first, inner next), not post-order
407 // Add to map here. Fix the CatchHigh after children are processed
408 const Module *Mod = BB->getParent()->getParent();
409 bool IsPreOrder = Triple(Mod->getTargetTriple()).isArch64Bit();
410 if (IsPreOrder)
411 addTryBlockMapEntry(FuncInfo, TryLow, TryHigh, CatchLow, Handlers);
412 unsigned TBMEIdx = FuncInfo.TryBlockMap.size() - 1;
414 for (const auto *CatchPad : Handlers) {
415 FuncInfo.FuncletBaseStateMap[CatchPad] = CatchLow;
416 FuncInfo.EHPadStateMap[CatchPad] = CatchLow;
417 for (const User *U : CatchPad->users()) {
418 const auto *UserI = cast<Instruction>(U);
419 if (auto *InnerCatchSwitch = dyn_cast<CatchSwitchInst>(UserI)) {
420 BasicBlock *UnwindDest = InnerCatchSwitch->getUnwindDest();
421 if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest())
422 calculateCXXStateNumbers(FuncInfo, UserI, CatchLow);
424 if (auto *InnerCleanupPad = dyn_cast<CleanupPadInst>(UserI)) {
425 BasicBlock *UnwindDest = getCleanupRetUnwindDest(InnerCleanupPad);
426 // If a nested cleanup pad reports a null unwind destination and the
427 // enclosing catch pad doesn't it must be post-dominated by an
428 // unreachable instruction.
429 if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest())
430 calculateCXXStateNumbers(FuncInfo, UserI, CatchLow);
434 int CatchHigh = FuncInfo.getLastStateNumber();
435 // Now child Catches are processed, update CatchHigh
436 if (IsPreOrder)
437 FuncInfo.TryBlockMap[TBMEIdx].CatchHigh = CatchHigh;
438 else // PostOrder
439 addTryBlockMapEntry(FuncInfo, TryLow, TryHigh, CatchHigh, Handlers);
441 LLVM_DEBUG(dbgs() << "TryLow[" << BB->getName() << "]: " << TryLow << '\n');
442 LLVM_DEBUG(dbgs() << "TryHigh[" << BB->getName() << "]: " << TryHigh
443 << '\n');
444 LLVM_DEBUG(dbgs() << "CatchHigh[" << BB->getName() << "]: " << CatchHigh
445 << '\n');
446 } else {
447 auto *CleanupPad = cast<CleanupPadInst>(FirstNonPHI);
449 // It's possible for a cleanup to be visited twice: it might have multiple
450 // cleanupret instructions.
451 if (FuncInfo.EHPadStateMap.count(CleanupPad))
452 return;
454 int CleanupState = addUnwindMapEntry(FuncInfo, ParentState, BB);
455 FuncInfo.EHPadStateMap[CleanupPad] = CleanupState;
456 LLVM_DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB "
457 << BB->getName() << '\n');
458 for (const BasicBlock *PredBlock : predecessors(BB)) {
459 if ((PredBlock = getEHPadFromPredecessor(PredBlock,
460 CleanupPad->getParentPad()))) {
461 calculateCXXStateNumbers(FuncInfo, &*PredBlock->getFirstNonPHIIt(),
462 CleanupState);
465 for (const User *U : CleanupPad->users()) {
466 const auto *UserI = cast<Instruction>(U);
467 if (UserI->isEHPad())
468 report_fatal_error("Cleanup funclets for the MSVC++ personality cannot "
469 "contain exceptional actions");
474 static int addSEHExcept(WinEHFuncInfo &FuncInfo, int ParentState,
475 const Function *Filter, const BasicBlock *Handler) {
476 SEHUnwindMapEntry Entry;
477 Entry.ToState = ParentState;
478 Entry.IsFinally = false;
479 Entry.Filter = Filter;
480 Entry.Handler = Handler;
481 FuncInfo.SEHUnwindMap.push_back(Entry);
482 return FuncInfo.SEHUnwindMap.size() - 1;
485 static int addSEHFinally(WinEHFuncInfo &FuncInfo, int ParentState,
486 const BasicBlock *Handler) {
487 SEHUnwindMapEntry Entry;
488 Entry.ToState = ParentState;
489 Entry.IsFinally = true;
490 Entry.Filter = nullptr;
491 Entry.Handler = Handler;
492 FuncInfo.SEHUnwindMap.push_back(Entry);
493 return FuncInfo.SEHUnwindMap.size() - 1;
496 // Starting from a EHPad, Backward walk through control-flow graph
497 // to produce two primary outputs:
498 // FuncInfo.EHPadStateMap[] and FuncInfo.SEHUnwindMap[]
499 static void calculateSEHStateNumbers(WinEHFuncInfo &FuncInfo,
500 const Instruction *FirstNonPHI,
501 int ParentState) {
502 const BasicBlock *BB = FirstNonPHI->getParent();
503 assert(BB->isEHPad() && "no a funclet!");
505 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(FirstNonPHI)) {
506 assert(FuncInfo.EHPadStateMap.count(CatchSwitch) == 0 &&
507 "shouldn't revist catch funclets!");
509 // Extract the filter function and the __except basic block and create a
510 // state for them.
511 assert(CatchSwitch->getNumHandlers() == 1 &&
512 "SEH doesn't have multiple handlers per __try");
513 const auto *CatchPad =
514 cast<CatchPadInst>((*CatchSwitch->handler_begin())->getFirstNonPHIIt());
515 const BasicBlock *CatchPadBB = CatchPad->getParent();
516 const Constant *FilterOrNull =
517 cast<Constant>(CatchPad->getArgOperand(0)->stripPointerCasts());
518 const Function *Filter = dyn_cast<Function>(FilterOrNull);
519 assert((Filter || FilterOrNull->isNullValue()) &&
520 "unexpected filter value");
521 int TryState = addSEHExcept(FuncInfo, ParentState, Filter, CatchPadBB);
523 // Everything in the __try block uses TryState as its parent state.
524 FuncInfo.EHPadStateMap[CatchSwitch] = TryState;
525 FuncInfo.EHPadStateMap[CatchPad] = TryState;
526 LLVM_DEBUG(dbgs() << "Assigning state #" << TryState << " to BB "
527 << CatchPadBB->getName() << '\n');
528 for (const BasicBlock *PredBlock : predecessors(BB))
529 if ((PredBlock = getEHPadFromPredecessor(PredBlock,
530 CatchSwitch->getParentPad())))
531 calculateSEHStateNumbers(FuncInfo, &*PredBlock->getFirstNonPHIIt(),
532 TryState);
534 // Everything in the __except block unwinds to ParentState, just like code
535 // outside the __try.
536 for (const User *U : CatchPad->users()) {
537 const auto *UserI = cast<Instruction>(U);
538 if (auto *InnerCatchSwitch = dyn_cast<CatchSwitchInst>(UserI)) {
539 BasicBlock *UnwindDest = InnerCatchSwitch->getUnwindDest();
540 if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest())
541 calculateSEHStateNumbers(FuncInfo, UserI, ParentState);
543 if (auto *InnerCleanupPad = dyn_cast<CleanupPadInst>(UserI)) {
544 BasicBlock *UnwindDest = getCleanupRetUnwindDest(InnerCleanupPad);
545 // If a nested cleanup pad reports a null unwind destination and the
546 // enclosing catch pad doesn't it must be post-dominated by an
547 // unreachable instruction.
548 if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest())
549 calculateSEHStateNumbers(FuncInfo, UserI, ParentState);
552 } else {
553 auto *CleanupPad = cast<CleanupPadInst>(FirstNonPHI);
555 // It's possible for a cleanup to be visited twice: it might have multiple
556 // cleanupret instructions.
557 if (FuncInfo.EHPadStateMap.count(CleanupPad))
558 return;
560 int CleanupState = addSEHFinally(FuncInfo, ParentState, BB);
561 FuncInfo.EHPadStateMap[CleanupPad] = CleanupState;
562 LLVM_DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB "
563 << BB->getName() << '\n');
564 for (const BasicBlock *PredBlock : predecessors(BB))
565 if ((PredBlock =
566 getEHPadFromPredecessor(PredBlock, CleanupPad->getParentPad())))
567 calculateSEHStateNumbers(FuncInfo, &*PredBlock->getFirstNonPHIIt(),
568 CleanupState);
569 for (const User *U : CleanupPad->users()) {
570 const auto *UserI = cast<Instruction>(U);
571 if (UserI->isEHPad())
572 report_fatal_error("Cleanup funclets for the SEH personality cannot "
573 "contain exceptional actions");
578 static bool isTopLevelPadForMSVC(const Instruction *EHPad) {
579 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(EHPad))
580 return isa<ConstantTokenNone>(CatchSwitch->getParentPad()) &&
581 CatchSwitch->unwindsToCaller();
582 if (auto *CleanupPad = dyn_cast<CleanupPadInst>(EHPad))
583 return isa<ConstantTokenNone>(CleanupPad->getParentPad()) &&
584 getCleanupRetUnwindDest(CleanupPad) == nullptr;
585 if (isa<CatchPadInst>(EHPad))
586 return false;
587 llvm_unreachable("unexpected EHPad!");
590 void llvm::calculateSEHStateNumbers(const Function *Fn,
591 WinEHFuncInfo &FuncInfo) {
592 // Don't compute state numbers twice.
593 if (!FuncInfo.SEHUnwindMap.empty())
594 return;
596 for (const BasicBlock &BB : *Fn) {
597 if (!BB.isEHPad())
598 continue;
599 const Instruction *FirstNonPHI = &*BB.getFirstNonPHIIt();
600 if (!isTopLevelPadForMSVC(FirstNonPHI))
601 continue;
602 ::calculateSEHStateNumbers(FuncInfo, FirstNonPHI, -1);
605 calculateStateNumbersForInvokes(Fn, FuncInfo);
607 bool IsEHa = Fn->getParent()->getModuleFlag("eh-asynch");
608 if (IsEHa) {
609 const BasicBlock *EntryBB = &(Fn->getEntryBlock());
610 calculateSEHStateForAsynchEH(EntryBB, -1, FuncInfo);
614 void llvm::calculateWinCXXEHStateNumbers(const Function *Fn,
615 WinEHFuncInfo &FuncInfo) {
616 // Return if it's already been done.
617 if (!FuncInfo.EHPadStateMap.empty())
618 return;
620 for (const BasicBlock &BB : *Fn) {
621 if (!BB.isEHPad())
622 continue;
623 const Instruction *FirstNonPHI = &*BB.getFirstNonPHIIt();
624 if (!isTopLevelPadForMSVC(FirstNonPHI))
625 continue;
626 calculateCXXStateNumbers(FuncInfo, FirstNonPHI, -1);
629 calculateStateNumbersForInvokes(Fn, FuncInfo);
631 bool IsEHa = Fn->getParent()->getModuleFlag("eh-asynch");
632 if (IsEHa) {
633 const BasicBlock *EntryBB = &(Fn->getEntryBlock());
634 calculateCXXStateForAsynchEH(EntryBB, -1, FuncInfo);
638 static int addClrEHHandler(WinEHFuncInfo &FuncInfo, int HandlerParentState,
639 int TryParentState, ClrHandlerType HandlerType,
640 uint32_t TypeToken, const BasicBlock *Handler) {
641 ClrEHUnwindMapEntry Entry;
642 Entry.HandlerParentState = HandlerParentState;
643 Entry.TryParentState = TryParentState;
644 Entry.Handler = Handler;
645 Entry.HandlerType = HandlerType;
646 Entry.TypeToken = TypeToken;
647 FuncInfo.ClrEHUnwindMap.push_back(Entry);
648 return FuncInfo.ClrEHUnwindMap.size() - 1;
651 void llvm::calculateClrEHStateNumbers(const Function *Fn,
652 WinEHFuncInfo &FuncInfo) {
653 // Return if it's already been done.
654 if (!FuncInfo.EHPadStateMap.empty())
655 return;
657 // This numbering assigns one state number to each catchpad and cleanuppad.
658 // It also computes two tree-like relations over states:
659 // 1) Each state has a "HandlerParentState", which is the state of the next
660 // outer handler enclosing this state's handler (same as nearest ancestor
661 // per the ParentPad linkage on EH pads, but skipping over catchswitches).
662 // 2) Each state has a "TryParentState", which:
663 // a) for a catchpad that's not the last handler on its catchswitch, is
664 // the state of the next catchpad on that catchswitch
665 // b) for all other pads, is the state of the pad whose try region is the
666 // next outer try region enclosing this state's try region. The "try
667 // regions are not present as such in the IR, but will be inferred
668 // based on the placement of invokes and pads which reach each other
669 // by exceptional exits
670 // Catchswitches do not get their own states, but each gets mapped to the
671 // state of its first catchpad.
673 // Step one: walk down from outermost to innermost funclets, assigning each
674 // catchpad and cleanuppad a state number. Add an entry to the
675 // ClrEHUnwindMap for each state, recording its HandlerParentState and
676 // handler attributes. Record the TryParentState as well for each catchpad
677 // that's not the last on its catchswitch, but initialize all other entries'
678 // TryParentStates to a sentinel -1 value that the next pass will update.
680 // Seed a worklist with pads that have no parent.
681 SmallVector<std::pair<const Instruction *, int>, 8> Worklist;
682 for (const BasicBlock &BB : *Fn) {
683 const Instruction *FirstNonPHI = &*BB.getFirstNonPHIIt();
684 const Value *ParentPad;
685 if (const auto *CPI = dyn_cast<CleanupPadInst>(FirstNonPHI))
686 ParentPad = CPI->getParentPad();
687 else if (const auto *CSI = dyn_cast<CatchSwitchInst>(FirstNonPHI))
688 ParentPad = CSI->getParentPad();
689 else
690 continue;
691 if (isa<ConstantTokenNone>(ParentPad))
692 Worklist.emplace_back(FirstNonPHI, -1);
695 // Use the worklist to visit all pads, from outer to inner. Record
696 // HandlerParentState for all pads. Record TryParentState only for catchpads
697 // that aren't the last on their catchswitch (setting all other entries'
698 // TryParentStates to an initial value of -1). This loop is also responsible
699 // for setting the EHPadStateMap entry for all catchpads, cleanuppads, and
700 // catchswitches.
701 while (!Worklist.empty()) {
702 const Instruction *Pad;
703 int HandlerParentState;
704 std::tie(Pad, HandlerParentState) = Worklist.pop_back_val();
706 if (const auto *Cleanup = dyn_cast<CleanupPadInst>(Pad)) {
707 // Create the entry for this cleanup with the appropriate handler
708 // properties. Finally and fault handlers are distinguished by arity.
709 ClrHandlerType HandlerType =
710 (Cleanup->arg_size() ? ClrHandlerType::Fault
711 : ClrHandlerType::Finally);
712 int CleanupState = addClrEHHandler(FuncInfo, HandlerParentState, -1,
713 HandlerType, 0, Pad->getParent());
714 // Queue any child EH pads on the worklist.
715 for (const User *U : Cleanup->users())
716 if (const auto *I = dyn_cast<Instruction>(U))
717 if (I->isEHPad())
718 Worklist.emplace_back(I, CleanupState);
719 // Remember this pad's state.
720 FuncInfo.EHPadStateMap[Cleanup] = CleanupState;
721 } else {
722 // Walk the handlers of this catchswitch in reverse order since all but
723 // the last need to set the following one as its TryParentState.
724 const auto *CatchSwitch = cast<CatchSwitchInst>(Pad);
725 int CatchState = -1, FollowerState = -1;
726 SmallVector<const BasicBlock *, 4> CatchBlocks(CatchSwitch->handlers());
727 for (const BasicBlock *CatchBlock : llvm::reverse(CatchBlocks)) {
728 // Create the entry for this catch with the appropriate handler
729 // properties.
730 const auto *Catch = cast<CatchPadInst>(CatchBlock->getFirstNonPHIIt());
731 uint32_t TypeToken = static_cast<uint32_t>(
732 cast<ConstantInt>(Catch->getArgOperand(0))->getZExtValue());
733 CatchState =
734 addClrEHHandler(FuncInfo, HandlerParentState, FollowerState,
735 ClrHandlerType::Catch, TypeToken, CatchBlock);
736 // Queue any child EH pads on the worklist.
737 for (const User *U : Catch->users())
738 if (const auto *I = dyn_cast<Instruction>(U))
739 if (I->isEHPad())
740 Worklist.emplace_back(I, CatchState);
741 // Remember this catch's state.
742 FuncInfo.EHPadStateMap[Catch] = CatchState;
743 FollowerState = CatchState;
745 // Associate the catchswitch with the state of its first catch.
746 assert(CatchSwitch->getNumHandlers());
747 FuncInfo.EHPadStateMap[CatchSwitch] = CatchState;
751 // Step two: record the TryParentState of each state. For cleanuppads that
752 // don't have cleanuprets, we may need to infer this from their child pads,
753 // so visit pads in descendant-most to ancestor-most order.
754 for (ClrEHUnwindMapEntry &Entry : llvm::reverse(FuncInfo.ClrEHUnwindMap)) {
755 const Instruction *Pad =
756 &*cast<const BasicBlock *>(Entry.Handler)->getFirstNonPHIIt();
757 // For most pads, the TryParentState is the state associated with the
758 // unwind dest of exceptional exits from it.
759 const BasicBlock *UnwindDest;
760 if (const auto *Catch = dyn_cast<CatchPadInst>(Pad)) {
761 // If a catch is not the last in its catchswitch, its TryParentState is
762 // the state associated with the next catch in the switch, even though
763 // that's not the unwind dest of exceptions escaping the catch. Those
764 // cases were already assigned a TryParentState in the first pass, so
765 // skip them.
766 if (Entry.TryParentState != -1)
767 continue;
768 // Otherwise, get the unwind dest from the catchswitch.
769 UnwindDest = Catch->getCatchSwitch()->getUnwindDest();
770 } else {
771 const auto *Cleanup = cast<CleanupPadInst>(Pad);
772 UnwindDest = nullptr;
773 for (const User *U : Cleanup->users()) {
774 if (auto *CleanupRet = dyn_cast<CleanupReturnInst>(U)) {
775 // Common and unambiguous case -- cleanupret indicates cleanup's
776 // unwind dest.
777 UnwindDest = CleanupRet->getUnwindDest();
778 break;
781 // Get an unwind dest for the user
782 const BasicBlock *UserUnwindDest = nullptr;
783 if (auto *Invoke = dyn_cast<InvokeInst>(U)) {
784 UserUnwindDest = Invoke->getUnwindDest();
785 } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(U)) {
786 UserUnwindDest = CatchSwitch->getUnwindDest();
787 } else if (auto *ChildCleanup = dyn_cast<CleanupPadInst>(U)) {
788 int UserState = FuncInfo.EHPadStateMap[ChildCleanup];
789 int UserUnwindState =
790 FuncInfo.ClrEHUnwindMap[UserState].TryParentState;
791 if (UserUnwindState != -1)
792 UserUnwindDest = cast<const BasicBlock *>(
793 FuncInfo.ClrEHUnwindMap[UserUnwindState].Handler);
796 // Not having an unwind dest for this user might indicate that it
797 // doesn't unwind, so can't be taken as proof that the cleanup itself
798 // may unwind to caller (see e.g. SimplifyUnreachable and
799 // RemoveUnwindEdge).
800 if (!UserUnwindDest)
801 continue;
803 // Now we have an unwind dest for the user, but we need to see if it
804 // unwinds all the way out of the cleanup or if it stays within it.
805 const Instruction *UserUnwindPad = &*UserUnwindDest->getFirstNonPHIIt();
806 const Value *UserUnwindParent;
807 if (auto *CSI = dyn_cast<CatchSwitchInst>(UserUnwindPad))
808 UserUnwindParent = CSI->getParentPad();
809 else
810 UserUnwindParent =
811 cast<CleanupPadInst>(UserUnwindPad)->getParentPad();
813 // The unwind stays within the cleanup iff it targets a child of the
814 // cleanup.
815 if (UserUnwindParent == Cleanup)
816 continue;
818 // This unwind exits the cleanup, so its dest is the cleanup's dest.
819 UnwindDest = UserUnwindDest;
820 break;
824 // Record the state of the unwind dest as the TryParentState.
825 int UnwindDestState;
827 // If UnwindDest is null at this point, either the pad in question can
828 // be exited by unwind to caller, or it cannot be exited by unwind. In
829 // either case, reporting such cases as unwinding to caller is correct.
830 // This can lead to EH tables that "look strange" -- if this pad's is in
831 // a parent funclet which has other children that do unwind to an enclosing
832 // pad, the try region for this pad will be missing the "duplicate" EH
833 // clause entries that you'd expect to see covering the whole parent. That
834 // should be benign, since the unwind never actually happens. If it were
835 // an issue, we could add a subsequent pass that pushes unwind dests down
836 // from parents that have them to children that appear to unwind to caller.
837 if (!UnwindDest) {
838 UnwindDestState = -1;
839 } else {
840 UnwindDestState =
841 FuncInfo.EHPadStateMap[&*UnwindDest->getFirstNonPHIIt()];
844 Entry.TryParentState = UnwindDestState;
847 // Step three: transfer information from pads to invokes.
848 calculateStateNumbersForInvokes(Fn, FuncInfo);
851 void WinEHPrepareImpl::colorFunclets(Function &F) {
852 BlockColors = colorEHFunclets(F);
854 // Invert the map from BB to colors to color to BBs.
855 for (BasicBlock &BB : F) {
856 ColorVector &Colors = BlockColors[&BB];
857 for (BasicBlock *Color : Colors)
858 FuncletBlocks[Color].push_back(&BB);
862 void WinEHPrepareImpl::demotePHIsOnFunclets(Function &F,
863 bool DemoteCatchSwitchPHIOnly) {
864 // Strip PHI nodes off of EH pads.
865 SmallVector<PHINode *, 16> PHINodes;
866 for (BasicBlock &BB : make_early_inc_range(F)) {
867 if (!BB.isEHPad())
868 continue;
869 if (DemoteCatchSwitchPHIOnly &&
870 !isa<CatchSwitchInst>(BB.getFirstNonPHIIt()))
871 continue;
873 for (Instruction &I : make_early_inc_range(BB)) {
874 auto *PN = dyn_cast<PHINode>(&I);
875 // Stop at the first non-PHI.
876 if (!PN)
877 break;
879 AllocaInst *SpillSlot = insertPHILoads(PN, F);
880 if (SpillSlot)
881 insertPHIStores(PN, SpillSlot);
883 PHINodes.push_back(PN);
887 for (auto *PN : PHINodes) {
888 // There may be lingering uses on other EH PHIs being removed
889 PN->replaceAllUsesWith(PoisonValue::get(PN->getType()));
890 PN->eraseFromParent();
894 void WinEHPrepareImpl::cloneCommonBlocks(Function &F) {
895 // We need to clone all blocks which belong to multiple funclets. Values are
896 // remapped throughout the funclet to propagate both the new instructions
897 // *and* the new basic blocks themselves.
898 for (auto &Funclets : FuncletBlocks) {
899 BasicBlock *FuncletPadBB = Funclets.first;
900 std::vector<BasicBlock *> &BlocksInFunclet = Funclets.second;
901 Value *FuncletToken;
902 if (FuncletPadBB == &F.getEntryBlock())
903 FuncletToken = ConstantTokenNone::get(F.getContext());
904 else
905 FuncletToken = &*FuncletPadBB->getFirstNonPHIIt();
907 std::vector<std::pair<BasicBlock *, BasicBlock *>> Orig2Clone;
908 ValueToValueMapTy VMap;
909 for (BasicBlock *BB : BlocksInFunclet) {
910 ColorVector &ColorsForBB = BlockColors[BB];
911 // We don't need to do anything if the block is monochromatic.
912 size_t NumColorsForBB = ColorsForBB.size();
913 if (NumColorsForBB == 1)
914 continue;
916 DEBUG_WITH_TYPE("win-eh-prepare-coloring",
917 dbgs() << " Cloning block \'" << BB->getName()
918 << "\' for funclet \'" << FuncletPadBB->getName()
919 << "\'.\n");
921 // Create a new basic block and copy instructions into it!
922 BasicBlock *CBB =
923 CloneBasicBlock(BB, VMap, Twine(".for.", FuncletPadBB->getName()));
924 // Insert the clone immediately after the original to ensure determinism
925 // and to keep the same relative ordering of any funclet's blocks.
926 CBB->insertInto(&F, BB->getNextNode());
928 // Add basic block mapping.
929 VMap[BB] = CBB;
931 // Record delta operations that we need to perform to our color mappings.
932 Orig2Clone.emplace_back(BB, CBB);
935 // If nothing was cloned, we're done cloning in this funclet.
936 if (Orig2Clone.empty())
937 continue;
939 // Update our color mappings to reflect that one block has lost a color and
940 // another has gained a color.
941 for (auto &BBMapping : Orig2Clone) {
942 BasicBlock *OldBlock = BBMapping.first;
943 BasicBlock *NewBlock = BBMapping.second;
945 BlocksInFunclet.push_back(NewBlock);
946 ColorVector &NewColors = BlockColors[NewBlock];
947 assert(NewColors.empty() && "A new block should only have one color!");
948 NewColors.push_back(FuncletPadBB);
950 DEBUG_WITH_TYPE("win-eh-prepare-coloring",
951 dbgs() << " Assigned color \'" << FuncletPadBB->getName()
952 << "\' to block \'" << NewBlock->getName()
953 << "\'.\n");
955 llvm::erase(BlocksInFunclet, OldBlock);
956 ColorVector &OldColors = BlockColors[OldBlock];
957 llvm::erase(OldColors, FuncletPadBB);
959 DEBUG_WITH_TYPE("win-eh-prepare-coloring",
960 dbgs() << " Removed color \'" << FuncletPadBB->getName()
961 << "\' from block \'" << OldBlock->getName()
962 << "\'.\n");
965 // Loop over all of the instructions in this funclet, fixing up operand
966 // references as we go. This uses VMap to do all the hard work.
967 for (BasicBlock *BB : BlocksInFunclet)
968 // Loop over all instructions, fixing each one as we find it...
969 for (Instruction &I : *BB)
970 RemapInstruction(&I, VMap,
971 RF_IgnoreMissingLocals | RF_NoModuleLevelChanges);
973 // Catchrets targeting cloned blocks need to be updated separately from
974 // the loop above because they are not in the current funclet.
975 SmallVector<CatchReturnInst *, 2> FixupCatchrets;
976 for (auto &BBMapping : Orig2Clone) {
977 BasicBlock *OldBlock = BBMapping.first;
978 BasicBlock *NewBlock = BBMapping.second;
980 FixupCatchrets.clear();
981 for (BasicBlock *Pred : predecessors(OldBlock))
982 if (auto *CatchRet = dyn_cast<CatchReturnInst>(Pred->getTerminator()))
983 if (CatchRet->getCatchSwitchParentPad() == FuncletToken)
984 FixupCatchrets.push_back(CatchRet);
986 for (CatchReturnInst *CatchRet : FixupCatchrets)
987 CatchRet->setSuccessor(NewBlock);
990 auto UpdatePHIOnClonedBlock = [&](PHINode *PN, bool IsForOldBlock) {
991 unsigned NumPreds = PN->getNumIncomingValues();
992 for (unsigned PredIdx = 0, PredEnd = NumPreds; PredIdx != PredEnd;
993 ++PredIdx) {
994 BasicBlock *IncomingBlock = PN->getIncomingBlock(PredIdx);
995 bool EdgeTargetsFunclet;
996 if (auto *CRI =
997 dyn_cast<CatchReturnInst>(IncomingBlock->getTerminator())) {
998 EdgeTargetsFunclet = (CRI->getCatchSwitchParentPad() == FuncletToken);
999 } else {
1000 ColorVector &IncomingColors = BlockColors[IncomingBlock];
1001 assert(!IncomingColors.empty() && "Block not colored!");
1002 assert((IncomingColors.size() == 1 ||
1003 !llvm::is_contained(IncomingColors, FuncletPadBB)) &&
1004 "Cloning should leave this funclet's blocks monochromatic");
1005 EdgeTargetsFunclet = (IncomingColors.front() == FuncletPadBB);
1007 if (IsForOldBlock != EdgeTargetsFunclet)
1008 continue;
1009 PN->removeIncomingValue(IncomingBlock, /*DeletePHIIfEmpty=*/false);
1010 // Revisit the next entry.
1011 --PredIdx;
1012 --PredEnd;
1016 for (auto &BBMapping : Orig2Clone) {
1017 BasicBlock *OldBlock = BBMapping.first;
1018 BasicBlock *NewBlock = BBMapping.second;
1019 for (PHINode &OldPN : OldBlock->phis()) {
1020 UpdatePHIOnClonedBlock(&OldPN, /*IsForOldBlock=*/true);
1022 for (PHINode &NewPN : NewBlock->phis()) {
1023 UpdatePHIOnClonedBlock(&NewPN, /*IsForOldBlock=*/false);
1027 // Check to see if SuccBB has PHI nodes. If so, we need to add entries to
1028 // the PHI nodes for NewBB now.
1029 for (auto &BBMapping : Orig2Clone) {
1030 BasicBlock *OldBlock = BBMapping.first;
1031 BasicBlock *NewBlock = BBMapping.second;
1032 for (BasicBlock *SuccBB : successors(NewBlock)) {
1033 for (PHINode &SuccPN : SuccBB->phis()) {
1034 // Ok, we have a PHI node. Figure out what the incoming value was for
1035 // the OldBlock.
1036 int OldBlockIdx = SuccPN.getBasicBlockIndex(OldBlock);
1037 if (OldBlockIdx == -1)
1038 break;
1039 Value *IV = SuccPN.getIncomingValue(OldBlockIdx);
1041 // Remap the value if necessary.
1042 if (auto *Inst = dyn_cast<Instruction>(IV)) {
1043 ValueToValueMapTy::iterator I = VMap.find(Inst);
1044 if (I != VMap.end())
1045 IV = I->second;
1048 SuccPN.addIncoming(IV, NewBlock);
1053 for (ValueToValueMapTy::value_type VT : VMap) {
1054 // If there were values defined in BB that are used outside the funclet,
1055 // then we now have to update all uses of the value to use either the
1056 // original value, the cloned value, or some PHI derived value. This can
1057 // require arbitrary PHI insertion, of which we are prepared to do, clean
1058 // these up now.
1059 SmallVector<Use *, 16> UsesToRename;
1061 auto *OldI = dyn_cast<Instruction>(const_cast<Value *>(VT.first));
1062 if (!OldI)
1063 continue;
1064 auto *NewI = cast<Instruction>(VT.second);
1065 // Scan all uses of this instruction to see if it is used outside of its
1066 // funclet, and if so, record them in UsesToRename.
1067 for (Use &U : OldI->uses()) {
1068 Instruction *UserI = cast<Instruction>(U.getUser());
1069 BasicBlock *UserBB = UserI->getParent();
1070 ColorVector &ColorsForUserBB = BlockColors[UserBB];
1071 assert(!ColorsForUserBB.empty());
1072 if (ColorsForUserBB.size() > 1 ||
1073 *ColorsForUserBB.begin() != FuncletPadBB)
1074 UsesToRename.push_back(&U);
1077 // If there are no uses outside the block, we're done with this
1078 // instruction.
1079 if (UsesToRename.empty())
1080 continue;
1082 // We found a use of OldI outside of the funclet. Rename all uses of OldI
1083 // that are outside its funclet to be uses of the appropriate PHI node
1084 // etc.
1085 SSAUpdater SSAUpdate;
1086 SSAUpdate.Initialize(OldI->getType(), OldI->getName());
1087 SSAUpdate.AddAvailableValue(OldI->getParent(), OldI);
1088 SSAUpdate.AddAvailableValue(NewI->getParent(), NewI);
1090 while (!UsesToRename.empty())
1091 SSAUpdate.RewriteUseAfterInsertions(*UsesToRename.pop_back_val());
1096 void WinEHPrepareImpl::removeImplausibleInstructions(Function &F) {
1097 // Remove implausible terminators and replace them with UnreachableInst.
1098 for (auto &Funclet : FuncletBlocks) {
1099 BasicBlock *FuncletPadBB = Funclet.first;
1100 std::vector<BasicBlock *> &BlocksInFunclet = Funclet.second;
1101 Instruction *FirstNonPHI = &*FuncletPadBB->getFirstNonPHIIt();
1102 auto *FuncletPad = dyn_cast<FuncletPadInst>(FirstNonPHI);
1103 auto *CatchPad = dyn_cast_or_null<CatchPadInst>(FuncletPad);
1104 auto *CleanupPad = dyn_cast_or_null<CleanupPadInst>(FuncletPad);
1106 for (BasicBlock *BB : BlocksInFunclet) {
1107 for (Instruction &I : *BB) {
1108 auto *CB = dyn_cast<CallBase>(&I);
1109 if (!CB)
1110 continue;
1112 Value *FuncletBundleOperand = nullptr;
1113 if (auto BU = CB->getOperandBundle(LLVMContext::OB_funclet))
1114 FuncletBundleOperand = BU->Inputs.front();
1116 if (FuncletBundleOperand == FuncletPad)
1117 continue;
1119 // Skip call sites which are nounwind intrinsics or inline asm.
1120 auto *CalledFn =
1121 dyn_cast<Function>(CB->getCalledOperand()->stripPointerCasts());
1122 if (CalledFn && ((CalledFn->isIntrinsic() && CB->doesNotThrow()) ||
1123 CB->isInlineAsm()))
1124 continue;
1126 // This call site was not part of this funclet, remove it.
1127 if (isa<InvokeInst>(CB)) {
1128 // Remove the unwind edge if it was an invoke.
1129 removeUnwindEdge(BB);
1130 // Get a pointer to the new call.
1131 BasicBlock::iterator CallI =
1132 std::prev(BB->getTerminator()->getIterator());
1133 auto *CI = cast<CallInst>(&*CallI);
1134 changeToUnreachable(CI);
1135 } else {
1136 changeToUnreachable(&I);
1139 // There are no more instructions in the block (except for unreachable),
1140 // we are done.
1141 break;
1144 Instruction *TI = BB->getTerminator();
1145 // CatchPadInst and CleanupPadInst can't transfer control to a ReturnInst.
1146 bool IsUnreachableRet = isa<ReturnInst>(TI) && FuncletPad;
1147 // The token consumed by a CatchReturnInst must match the funclet token.
1148 bool IsUnreachableCatchret = false;
1149 if (auto *CRI = dyn_cast<CatchReturnInst>(TI))
1150 IsUnreachableCatchret = CRI->getCatchPad() != CatchPad;
1151 // The token consumed by a CleanupReturnInst must match the funclet token.
1152 bool IsUnreachableCleanupret = false;
1153 if (auto *CRI = dyn_cast<CleanupReturnInst>(TI))
1154 IsUnreachableCleanupret = CRI->getCleanupPad() != CleanupPad;
1155 if (IsUnreachableRet || IsUnreachableCatchret ||
1156 IsUnreachableCleanupret) {
1157 changeToUnreachable(TI);
1158 } else if (isa<InvokeInst>(TI)) {
1159 if (Personality == EHPersonality::MSVC_CXX && CleanupPad) {
1160 // Invokes within a cleanuppad for the MSVC++ personality never
1161 // transfer control to their unwind edge: the personality will
1162 // terminate the program.
1163 removeUnwindEdge(BB);
1170 void WinEHPrepareImpl::cleanupPreparedFunclets(Function &F) {
1171 // Clean-up some of the mess we made by removing useles PHI nodes, trivial
1172 // branches, etc.
1173 for (BasicBlock &BB : llvm::make_early_inc_range(F)) {
1174 SimplifyInstructionsInBlock(&BB);
1175 ConstantFoldTerminator(&BB, /*DeleteDeadConditions=*/true);
1176 MergeBlockIntoPredecessor(&BB);
1179 // We might have some unreachable blocks after cleaning up some impossible
1180 // control flow.
1181 removeUnreachableBlocks(F);
1184 #ifndef NDEBUG
1185 void WinEHPrepareImpl::verifyPreparedFunclets(Function &F) {
1186 for (BasicBlock &BB : F) {
1187 size_t NumColors = BlockColors[&BB].size();
1188 assert(NumColors == 1 && "Expected monochromatic BB!");
1189 if (NumColors == 0)
1190 report_fatal_error("Uncolored BB!");
1191 if (NumColors > 1)
1192 report_fatal_error("Multicolor BB!");
1193 assert((DisableDemotion || !(BB.isEHPad() && isa<PHINode>(BB.begin()))) &&
1194 "EH Pad still has a PHI!");
1197 #endif
1199 bool WinEHPrepareImpl::prepareExplicitEH(Function &F) {
1200 // Remove unreachable blocks. It is not valuable to assign them a color and
1201 // their existence can trick us into thinking values are alive when they are
1202 // not.
1203 removeUnreachableBlocks(F);
1205 // Determine which blocks are reachable from which funclet entries.
1206 colorFunclets(F);
1208 cloneCommonBlocks(F);
1210 if (!DisableDemotion)
1211 demotePHIsOnFunclets(F, DemoteCatchSwitchPHIOnly ||
1212 DemoteCatchSwitchPHIOnlyOpt);
1214 if (!DisableCleanups) {
1215 assert(!verifyFunction(F, &dbgs()));
1216 removeImplausibleInstructions(F);
1218 assert(!verifyFunction(F, &dbgs()));
1219 cleanupPreparedFunclets(F);
1222 LLVM_DEBUG(verifyPreparedFunclets(F));
1223 // Recolor the CFG to verify that all is well.
1224 LLVM_DEBUG(colorFunclets(F));
1225 LLVM_DEBUG(verifyPreparedFunclets(F));
1227 return true;
1230 // TODO: Share loads when one use dominates another, or when a catchpad exit
1231 // dominates uses (needs dominators).
1232 AllocaInst *WinEHPrepareImpl::insertPHILoads(PHINode *PN, Function &F) {
1233 BasicBlock *PHIBlock = PN->getParent();
1234 AllocaInst *SpillSlot = nullptr;
1235 Instruction *EHPad = &*PHIBlock->getFirstNonPHIIt();
1237 if (!EHPad->isTerminator()) {
1238 // If the EHPad isn't a terminator, then we can insert a load in this block
1239 // that will dominate all uses.
1240 SpillSlot = new AllocaInst(PN->getType(), DL->getAllocaAddrSpace(), nullptr,
1241 Twine(PN->getName(), ".wineh.spillslot"),
1242 F.getEntryBlock().begin());
1243 Value *V = new LoadInst(PN->getType(), SpillSlot,
1244 Twine(PN->getName(), ".wineh.reload"),
1245 PHIBlock->getFirstInsertionPt());
1246 PN->replaceAllUsesWith(V);
1247 return SpillSlot;
1250 // Otherwise, we have a PHI on a terminator EHPad, and we give up and insert
1251 // loads of the slot before every use.
1252 DenseMap<BasicBlock *, Value *> Loads;
1253 for (Use &U : llvm::make_early_inc_range(PN->uses())) {
1254 auto *UsingInst = cast<Instruction>(U.getUser());
1255 if (isa<PHINode>(UsingInst) && UsingInst->getParent()->isEHPad()) {
1256 // Use is on an EH pad phi. Leave it alone; we'll insert loads and
1257 // stores for it separately.
1258 continue;
1260 replaceUseWithLoad(PN, U, SpillSlot, Loads, F);
1262 return SpillSlot;
1265 // TODO: improve store placement. Inserting at def is probably good, but need
1266 // to be careful not to introduce interfering stores (needs liveness analysis).
1267 // TODO: identify related phi nodes that can share spill slots, and share them
1268 // (also needs liveness).
1269 void WinEHPrepareImpl::insertPHIStores(PHINode *OriginalPHI,
1270 AllocaInst *SpillSlot) {
1271 // Use a worklist of (Block, Value) pairs -- the given Value needs to be
1272 // stored to the spill slot by the end of the given Block.
1273 SmallVector<std::pair<BasicBlock *, Value *>, 4> Worklist;
1275 Worklist.push_back({OriginalPHI->getParent(), OriginalPHI});
1277 while (!Worklist.empty()) {
1278 BasicBlock *EHBlock;
1279 Value *InVal;
1280 std::tie(EHBlock, InVal) = Worklist.pop_back_val();
1282 PHINode *PN = dyn_cast<PHINode>(InVal);
1283 if (PN && PN->getParent() == EHBlock) {
1284 // The value is defined by another PHI we need to remove, with no room to
1285 // insert a store after the PHI, so each predecessor needs to store its
1286 // incoming value.
1287 for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i) {
1288 Value *PredVal = PN->getIncomingValue(i);
1290 // Undef can safely be skipped.
1291 if (isa<UndefValue>(PredVal))
1292 continue;
1294 insertPHIStore(PN->getIncomingBlock(i), PredVal, SpillSlot, Worklist);
1296 } else {
1297 // We need to store InVal, which dominates EHBlock, but can't put a store
1298 // in EHBlock, so need to put stores in each predecessor.
1299 for (BasicBlock *PredBlock : predecessors(EHBlock)) {
1300 insertPHIStore(PredBlock, InVal, SpillSlot, Worklist);
1306 void WinEHPrepareImpl::insertPHIStore(
1307 BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot,
1308 SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist) {
1310 if (PredBlock->isEHPad() && PredBlock->getFirstNonPHIIt()->isTerminator()) {
1311 // Pred is unsplittable, so we need to queue it on the worklist.
1312 Worklist.push_back({PredBlock, PredVal});
1313 return;
1316 // Otherwise, insert the store at the end of the basic block.
1317 new StoreInst(PredVal, SpillSlot, PredBlock->getTerminator()->getIterator());
1320 void WinEHPrepareImpl::replaceUseWithLoad(
1321 Value *V, Use &U, AllocaInst *&SpillSlot,
1322 DenseMap<BasicBlock *, Value *> &Loads, Function &F) {
1323 // Lazilly create the spill slot.
1324 if (!SpillSlot)
1325 SpillSlot = new AllocaInst(V->getType(), DL->getAllocaAddrSpace(), nullptr,
1326 Twine(V->getName(), ".wineh.spillslot"),
1327 F.getEntryBlock().begin());
1329 auto *UsingInst = cast<Instruction>(U.getUser());
1330 if (auto *UsingPHI = dyn_cast<PHINode>(UsingInst)) {
1331 // If this is a PHI node, we can't insert a load of the value before
1332 // the use. Instead insert the load in the predecessor block
1333 // corresponding to the incoming value.
1335 // Note that if there are multiple edges from a basic block to this
1336 // PHI node that we cannot have multiple loads. The problem is that
1337 // the resulting PHI node will have multiple values (from each load)
1338 // coming in from the same block, which is illegal SSA form.
1339 // For this reason, we keep track of and reuse loads we insert.
1340 BasicBlock *IncomingBlock = UsingPHI->getIncomingBlock(U);
1341 if (auto *CatchRet =
1342 dyn_cast<CatchReturnInst>(IncomingBlock->getTerminator())) {
1343 // Putting a load above a catchret and use on the phi would still leave
1344 // a cross-funclet def/use. We need to split the edge, change the
1345 // catchret to target the new block, and put the load there.
1346 BasicBlock *PHIBlock = UsingInst->getParent();
1347 BasicBlock *NewBlock = SplitEdge(IncomingBlock, PHIBlock);
1348 // SplitEdge gives us:
1349 // IncomingBlock:
1350 // ...
1351 // br label %NewBlock
1352 // NewBlock:
1353 // catchret label %PHIBlock
1354 // But we need:
1355 // IncomingBlock:
1356 // ...
1357 // catchret label %NewBlock
1358 // NewBlock:
1359 // br label %PHIBlock
1360 // So move the terminators to each others' blocks and swap their
1361 // successors.
1362 BranchInst *Goto = cast<BranchInst>(IncomingBlock->getTerminator());
1363 Goto->removeFromParent();
1364 CatchRet->removeFromParent();
1365 CatchRet->insertInto(IncomingBlock, IncomingBlock->end());
1366 Goto->insertInto(NewBlock, NewBlock->end());
1367 Goto->setSuccessor(0, PHIBlock);
1368 CatchRet->setSuccessor(NewBlock);
1369 // Update the color mapping for the newly split edge.
1370 // Grab a reference to the ColorVector to be inserted before getting the
1371 // reference to the vector we are copying because inserting the new
1372 // element in BlockColors might cause the map to be reallocated.
1373 ColorVector &ColorsForNewBlock = BlockColors[NewBlock];
1374 ColorVector &ColorsForPHIBlock = BlockColors[PHIBlock];
1375 ColorsForNewBlock = ColorsForPHIBlock;
1376 for (BasicBlock *FuncletPad : ColorsForPHIBlock)
1377 FuncletBlocks[FuncletPad].push_back(NewBlock);
1378 // Treat the new block as incoming for load insertion.
1379 IncomingBlock = NewBlock;
1381 Value *&Load = Loads[IncomingBlock];
1382 // Insert the load into the predecessor block
1383 if (!Load)
1384 Load = new LoadInst(
1385 V->getType(), SpillSlot, Twine(V->getName(), ".wineh.reload"),
1386 /*isVolatile=*/false, IncomingBlock->getTerminator()->getIterator());
1388 U.set(Load);
1389 } else {
1390 // Reload right before the old use.
1391 auto *Load = new LoadInst(V->getType(), SpillSlot,
1392 Twine(V->getName(), ".wineh.reload"),
1393 /*isVolatile=*/false, UsingInst->getIterator());
1394 U.set(Load);
1398 void WinEHFuncInfo::addIPToStateRange(const InvokeInst *II,
1399 MCSymbol *InvokeBegin,
1400 MCSymbol *InvokeEnd) {
1401 assert(InvokeStateMap.count(II) &&
1402 "should get invoke with precomputed state");
1403 LabelToStateMap[InvokeBegin] = std::make_pair(InvokeStateMap[II], InvokeEnd);
1406 void WinEHFuncInfo::addIPToStateRange(int State, MCSymbol* InvokeBegin,
1407 MCSymbol* InvokeEnd) {
1408 LabelToStateMap[InvokeBegin] = std::make_pair(State, InvokeEnd);
1411 WinEHFuncInfo::WinEHFuncInfo() = default;