1 //===-- WinEHPrepare - Prepare exception handling for code generation ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass lowers LLVM IR exception handling into something closer to what the
11 // backend wants for functions using a personality function from a runtime
12 // provided by MSVC. Functions with other personality functions are left alone
13 // and may be prepared by other passes. In particular, all supported MSVC
14 // personality functions require cleanup code to be outlined, and the C++
15 // personality requires catch handler code to be outlined.
17 //===----------------------------------------------------------------------===//
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/ADT/SetVector.h"
21 #include "llvm/Analysis/CFG.h"
22 #include "llvm/Analysis/LibCallSemantics.h"
23 #include "llvm/CodeGen/WinEHFuncInfo.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
28 #include "llvm/Transforms/Utils/Cloning.h"
29 #include "llvm/Transforms/Utils/Local.h"
30 #include "llvm/Transforms/Utils/SSAUpdater.h"
34 #define DEBUG_TYPE "winehprepare"
36 static cl::opt<bool> DisableDemotion(
37 "disable-demotion", cl::Hidden,
39 "Clone multicolor basic blocks but do not demote cross funclet values"),
42 static cl::opt<bool> DisableCleanups(
43 "disable-cleanups", cl::Hidden,
44 cl::desc("Do not remove implausible terminators or other similar cleanups"),
49 class WinEHPrepare : public FunctionPass {
51 static char ID; // Pass identification, replacement for typeid.
52 WinEHPrepare(const TargetMachine *TM = nullptr) : FunctionPass(ID) {}
54 bool runOnFunction(Function &Fn) override;
56 bool doFinalization(Module &M) override;
58 void getAnalysisUsage(AnalysisUsage &AU) const override;
60 const char *getPassName() const override {
61 return "Windows exception handling preparation";
65 void insertPHIStores(PHINode *OriginalPHI, AllocaInst *SpillSlot);
67 insertPHIStore(BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot,
68 SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist);
69 AllocaInst *insertPHILoads(PHINode *PN, Function &F);
70 void replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
71 DenseMap<BasicBlock *, Value *> &Loads, Function &F);
72 void demoteNonlocalUses(Value *V, SetVector<BasicBlock *> &ColorsForBB,
74 bool prepareExplicitEH(Function &F,
75 SmallVectorImpl<BasicBlock *> &EntryBlocks);
76 void replaceTerminatePadWithCleanup(Function &F);
77 void colorFunclets(Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks);
78 void resolveFuncletAncestry(Function &F,
79 SmallVectorImpl<BasicBlock *> &EntryBlocks);
80 void resolveFuncletAncestryForPath(
81 Function &F, SmallVectorImpl<BasicBlock *> &FuncletPath,
82 std::map<BasicBlock *, BasicBlock *> &IdentityMap);
83 void makeFuncletEdgeUnreachable(BasicBlock *Parent, BasicBlock *Child);
84 BasicBlock *cloneFuncletForParent(Function &F, BasicBlock *FuncletEntry,
86 void updateTerminatorsAfterFuncletClone(
87 Function &F, BasicBlock *OrigFunclet, BasicBlock *CloneFunclet,
88 BasicBlock *OrigBlock, BasicBlock *CloneBlock, BasicBlock *CloneParent,
89 ValueToValueMapTy &VMap,
90 std::map<BasicBlock *, BasicBlock *> &Orig2Clone);
92 void demotePHIsOnFunclets(Function &F);
93 void demoteUsesBetweenFunclets(Function &F);
94 void demoteArgumentUses(Function &F);
95 void cloneCommonBlocks(Function &F,
96 SmallVectorImpl<BasicBlock *> &EntryBlocks);
97 void removeImplausibleTerminators(Function &F);
98 void cleanupPreparedFunclets(Function &F);
99 void verifyPreparedFunclets(Function &F);
101 // All fields are reset by runOnFunction.
102 EHPersonality Personality = EHPersonality::Unknown;
104 std::map<BasicBlock *, SetVector<BasicBlock *>> BlockColors;
105 std::map<BasicBlock *, std::set<BasicBlock *>> FuncletBlocks;
106 std::map<BasicBlock *, std::vector<BasicBlock *>> FuncletChildren;
107 std::map<BasicBlock *, std::vector<BasicBlock *>> FuncletParents;
109 // This is a flag that indicates an uncommon situation where we need to
110 // clone funclets has been detected.
111 bool FuncletCloningRequired = false;
112 // When a funclet with multiple parents contains a catchret, the block to
113 // which it returns will be cloned so that there is a copy in each parent
114 // but one of the copies will not be properly linked to the catchret and
115 // in most cases will have no predecessors. This double map allows us
116 // to find these cloned blocks when we clone the child funclet.
117 std::map<BasicBlock *, std::map<BasicBlock *, BasicBlock*>> EstrangedBlocks;
120 } // end anonymous namespace
122 char WinEHPrepare::ID = 0;
123 INITIALIZE_TM_PASS(WinEHPrepare, "winehprepare", "Prepare Windows exceptions",
126 FunctionPass *llvm::createWinEHPass(const TargetMachine *TM) {
127 return new WinEHPrepare(TM);
130 static void findFuncletEntryPoints(Function &Fn,
131 SmallVectorImpl<BasicBlock *> &EntryBlocks) {
132 EntryBlocks.push_back(&Fn.getEntryBlock());
133 for (BasicBlock &BB : Fn) {
134 Instruction *First = BB.getFirstNonPHI();
135 if (!First->isEHPad())
137 assert(!isa<LandingPadInst>(First) &&
138 "landingpad cannot be used with funclet EH personality");
139 // Find EH pad blocks that represent funclet start points.
140 if (!isa<CatchEndPadInst>(First) && !isa<CleanupEndPadInst>(First))
141 EntryBlocks.push_back(&BB);
145 bool WinEHPrepare::runOnFunction(Function &Fn) {
146 if (!Fn.hasPersonalityFn())
149 // Classify the personality to see what kind of preparation we need.
150 Personality = classifyEHPersonality(Fn.getPersonalityFn());
152 // Do nothing if this is not a funclet-based personality.
153 if (!isFuncletEHPersonality(Personality))
156 // Remove unreachable blocks. It is not valuable to assign them a color and
157 // their existence can trick us into thinking values are alive when they are
159 removeUnreachableBlocks(Fn);
161 SmallVector<BasicBlock *, 4> EntryBlocks;
162 findFuncletEntryPoints(Fn, EntryBlocks);
163 return prepareExplicitEH(Fn, EntryBlocks);
166 bool WinEHPrepare::doFinalization(Module &M) { return false; }
168 void WinEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const {}
170 static int addUnwindMapEntry(WinEHFuncInfo &FuncInfo, int ToState,
171 const BasicBlock *BB) {
172 CxxUnwindMapEntry UME;
173 UME.ToState = ToState;
175 FuncInfo.CxxUnwindMap.push_back(UME);
176 return FuncInfo.getLastStateNumber();
179 static void addTryBlockMapEntry(WinEHFuncInfo &FuncInfo, int TryLow,
180 int TryHigh, int CatchHigh,
181 ArrayRef<const CatchPadInst *> Handlers) {
182 WinEHTryBlockMapEntry TBME;
183 TBME.TryLow = TryLow;
184 TBME.TryHigh = TryHigh;
185 TBME.CatchHigh = CatchHigh;
186 assert(TBME.TryLow <= TBME.TryHigh);
187 for (const CatchPadInst *CPI : Handlers) {
189 Constant *TypeInfo = cast<Constant>(CPI->getArgOperand(0));
190 if (TypeInfo->isNullValue())
191 HT.TypeDescriptor = nullptr;
193 HT.TypeDescriptor = cast<GlobalVariable>(TypeInfo->stripPointerCasts());
194 HT.Adjectives = cast<ConstantInt>(CPI->getArgOperand(1))->getZExtValue();
195 HT.Handler = CPI->getParent();
196 if (isa<ConstantPointerNull>(CPI->getArgOperand(2)))
197 HT.CatchObj.Alloca = nullptr;
199 HT.CatchObj.Alloca = cast<AllocaInst>(CPI->getArgOperand(2));
200 TBME.HandlerArray.push_back(HT);
202 FuncInfo.TryBlockMap.push_back(TBME);
205 static const CatchPadInst *getSingleCatchPadPredecessor(const BasicBlock *BB) {
206 for (const BasicBlock *PredBlock : predecessors(BB))
207 if (auto *CPI = dyn_cast<CatchPadInst>(PredBlock->getFirstNonPHI()))
212 /// Find all the catchpads that feed directly into the catchendpad. Frontends
213 /// using this personality should ensure that each catchendpad and catchpad has
214 /// one or zero catchpad predecessors.
216 /// The following C++ generates the IR after it:
224 /// catchpad [i8* A typeinfo]
225 /// to label %catch.A unwind label %catchpad.B
227 /// catchpad [i8* B typeinfo]
228 /// to label %catch.B unwind label %endcatches
230 /// catchendblock unwind to caller
232 findCatchPadsForCatchEndPad(const BasicBlock *CatchEndBB,
233 SmallVectorImpl<const CatchPadInst *> &Handlers) {
234 const CatchPadInst *CPI = getSingleCatchPadPredecessor(CatchEndBB);
236 Handlers.push_back(CPI);
237 CPI = getSingleCatchPadPredecessor(CPI->getParent());
239 // We've pushed these back into reverse source order. Reverse them to get
240 // the list back into source order.
241 std::reverse(Handlers.begin(), Handlers.end());
244 // Given BB which ends in an unwind edge, return the EHPad that this BB belongs
245 // to. If the unwind edge came from an invoke, return null.
246 static const BasicBlock *getEHPadFromPredecessor(const BasicBlock *BB) {
247 const TerminatorInst *TI = BB->getTerminator();
248 if (isa<InvokeInst>(TI))
252 return cast<CleanupReturnInst>(TI)->getCleanupPad()->getParent();
255 static void calculateExplicitCXXStateNumbers(WinEHFuncInfo &FuncInfo,
256 const BasicBlock &BB,
258 assert(BB.isEHPad());
259 const Instruction *FirstNonPHI = BB.getFirstNonPHI();
260 // All catchpad instructions will be handled when we process their
261 // respective catchendpad instruction.
262 if (isa<CatchPadInst>(FirstNonPHI))
265 if (isa<CatchEndPadInst>(FirstNonPHI)) {
266 SmallVector<const CatchPadInst *, 2> Handlers;
267 findCatchPadsForCatchEndPad(&BB, Handlers);
268 const BasicBlock *FirstTryPad = Handlers.front()->getParent();
269 int TryLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr);
270 FuncInfo.EHPadStateMap[Handlers.front()] = TryLow;
271 for (const BasicBlock *PredBlock : predecessors(FirstTryPad))
272 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
273 calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, TryLow);
274 int CatchLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr);
276 // catchpads are separate funclets in C++ EH due to the way rethrow works.
277 // In SEH, they aren't, so no invokes will unwind to the catchendpad.
278 FuncInfo.EHPadStateMap[FirstNonPHI] = CatchLow;
279 int TryHigh = CatchLow - 1;
280 for (const BasicBlock *PredBlock : predecessors(&BB))
281 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
282 calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, CatchLow);
283 int CatchHigh = FuncInfo.getLastStateNumber();
284 addTryBlockMapEntry(FuncInfo, TryLow, TryHigh, CatchHigh, Handlers);
285 DEBUG(dbgs() << "TryLow[" << FirstTryPad->getName() << "]: " << TryLow
287 DEBUG(dbgs() << "TryHigh[" << FirstTryPad->getName() << "]: " << TryHigh
289 DEBUG(dbgs() << "CatchHigh[" << FirstTryPad->getName() << "]: " << CatchHigh
291 } else if (isa<CleanupPadInst>(FirstNonPHI)) {
292 // A cleanup can have multiple exits; don't re-process after the first.
293 if (FuncInfo.EHPadStateMap.count(FirstNonPHI))
295 int CleanupState = addUnwindMapEntry(FuncInfo, ParentState, &BB);
296 FuncInfo.EHPadStateMap[FirstNonPHI] = CleanupState;
297 DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB "
298 << BB.getName() << '\n');
299 for (const BasicBlock *PredBlock : predecessors(&BB))
300 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
301 calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, CleanupState);
302 } else if (auto *CEPI = dyn_cast<CleanupEndPadInst>(FirstNonPHI)) {
303 // Propagate ParentState to the cleanuppad in case it doesn't have
305 BasicBlock *CleanupBlock = CEPI->getCleanupPad()->getParent();
306 calculateExplicitCXXStateNumbers(FuncInfo, *CleanupBlock, ParentState);
307 // Anything unwinding through CleanupEndPadInst is in ParentState.
308 FuncInfo.EHPadStateMap[FirstNonPHI] = ParentState;
309 for (const BasicBlock *PredBlock : predecessors(&BB))
310 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
311 calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, ParentState);
312 } else if (isa<TerminatePadInst>(FirstNonPHI)) {
313 report_fatal_error("Not yet implemented!");
315 llvm_unreachable("unexpected EH Pad!");
319 static int addSEHExcept(WinEHFuncInfo &FuncInfo, int ParentState,
320 const Function *Filter, const BasicBlock *Handler) {
321 SEHUnwindMapEntry Entry;
322 Entry.ToState = ParentState;
323 Entry.IsFinally = false;
324 Entry.Filter = Filter;
325 Entry.Handler = Handler;
326 FuncInfo.SEHUnwindMap.push_back(Entry);
327 return FuncInfo.SEHUnwindMap.size() - 1;
330 static int addSEHFinally(WinEHFuncInfo &FuncInfo, int ParentState,
331 const BasicBlock *Handler) {
332 SEHUnwindMapEntry Entry;
333 Entry.ToState = ParentState;
334 Entry.IsFinally = true;
335 Entry.Filter = nullptr;
336 Entry.Handler = Handler;
337 FuncInfo.SEHUnwindMap.push_back(Entry);
338 return FuncInfo.SEHUnwindMap.size() - 1;
341 static void calculateExplicitSEHStateNumbers(WinEHFuncInfo &FuncInfo,
342 const BasicBlock &BB,
344 assert(BB.isEHPad());
345 const Instruction *FirstNonPHI = BB.getFirstNonPHI();
346 // All catchpad instructions will be handled when we process their
347 // respective catchendpad instruction.
348 if (isa<CatchPadInst>(FirstNonPHI))
351 if (isa<CatchEndPadInst>(FirstNonPHI)) {
352 // Extract the filter function and the __except basic block and create a
354 SmallVector<const CatchPadInst *, 1> Handlers;
355 findCatchPadsForCatchEndPad(&BB, Handlers);
356 assert(Handlers.size() == 1 &&
357 "SEH doesn't have multiple handlers per __try");
358 const CatchPadInst *CPI = Handlers.front();
359 const BasicBlock *CatchPadBB = CPI->getParent();
360 const Constant *FilterOrNull =
361 cast<Constant>(CPI->getArgOperand(0)->stripPointerCasts());
362 const Function *Filter = dyn_cast<Function>(FilterOrNull);
363 assert((Filter || FilterOrNull->isNullValue()) &&
364 "unexpected filter value");
365 int TryState = addSEHExcept(FuncInfo, ParentState, Filter, CatchPadBB);
367 // Everything in the __try block uses TryState as its parent state.
368 FuncInfo.EHPadStateMap[CPI] = TryState;
369 DEBUG(dbgs() << "Assigning state #" << TryState << " to BB "
370 << CatchPadBB->getName() << '\n');
371 for (const BasicBlock *PredBlock : predecessors(CatchPadBB))
372 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
373 calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, TryState);
375 // Everything in the __except block unwinds to ParentState, just like code
376 // outside the __try.
377 FuncInfo.EHPadStateMap[FirstNonPHI] = ParentState;
378 DEBUG(dbgs() << "Assigning state #" << ParentState << " to BB "
379 << BB.getName() << '\n');
380 for (const BasicBlock *PredBlock : predecessors(&BB))
381 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
382 calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, ParentState);
383 } else if (isa<CleanupPadInst>(FirstNonPHI)) {
384 // A cleanup can have multiple exits; don't re-process after the first.
385 if (FuncInfo.EHPadStateMap.count(FirstNonPHI))
387 int CleanupState = addSEHFinally(FuncInfo, ParentState, &BB);
388 FuncInfo.EHPadStateMap[FirstNonPHI] = CleanupState;
389 DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB "
390 << BB.getName() << '\n');
391 for (const BasicBlock *PredBlock : predecessors(&BB))
392 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
393 calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, CleanupState);
394 } else if (auto *CEPI = dyn_cast<CleanupEndPadInst>(FirstNonPHI)) {
395 // Propagate ParentState to the cleanuppad in case it doesn't have
397 BasicBlock *CleanupBlock = CEPI->getCleanupPad()->getParent();
398 calculateExplicitSEHStateNumbers(FuncInfo, *CleanupBlock, ParentState);
399 // Anything unwinding through CleanupEndPadInst is in ParentState.
400 FuncInfo.EHPadStateMap[FirstNonPHI] = ParentState;
401 DEBUG(dbgs() << "Assigning state #" << ParentState << " to BB "
402 << BB.getName() << '\n');
403 for (const BasicBlock *PredBlock : predecessors(&BB))
404 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
405 calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, ParentState);
406 } else if (isa<TerminatePadInst>(FirstNonPHI)) {
407 report_fatal_error("Not yet implemented!");
409 llvm_unreachable("unexpected EH Pad!");
413 /// Check if the EH Pad unwinds to caller. Cleanups are a little bit of a
414 /// special case because we have to look at the cleanupret instruction that uses
416 static bool doesEHPadUnwindToCaller(const Instruction *EHPad) {
417 auto *CPI = dyn_cast<CleanupPadInst>(EHPad);
419 return EHPad->mayThrow();
421 // This cleanup does not return or unwind, so we say it unwinds to caller.
422 if (CPI->use_empty())
425 const Instruction *User = CPI->user_back();
426 if (auto *CRI = dyn_cast<CleanupReturnInst>(User))
427 return CRI->unwindsToCaller();
428 return cast<CleanupEndPadInst>(User)->unwindsToCaller();
431 void llvm::calculateSEHStateNumbers(const Function *Fn,
432 WinEHFuncInfo &FuncInfo) {
433 // Don't compute state numbers twice.
434 if (!FuncInfo.SEHUnwindMap.empty())
437 for (const BasicBlock &BB : *Fn) {
438 if (!BB.isEHPad() || !doesEHPadUnwindToCaller(BB.getFirstNonPHI()))
440 calculateExplicitSEHStateNumbers(FuncInfo, BB, -1);
444 void llvm::calculateWinCXXEHStateNumbers(const Function *Fn,
445 WinEHFuncInfo &FuncInfo) {
446 // Return if it's already been done.
447 if (!FuncInfo.EHPadStateMap.empty())
450 for (const BasicBlock &BB : *Fn) {
453 if (BB.isLandingPad())
454 report_fatal_error("MSVC C++ EH cannot use landingpads");
455 const Instruction *FirstNonPHI = BB.getFirstNonPHI();
456 if (!doesEHPadUnwindToCaller(FirstNonPHI))
458 calculateExplicitCXXStateNumbers(FuncInfo, BB, -1);
462 static int addClrEHHandler(WinEHFuncInfo &FuncInfo, int ParentState,
463 ClrHandlerType HandlerType, uint32_t TypeToken,
464 const BasicBlock *Handler) {
465 ClrEHUnwindMapEntry Entry;
466 Entry.Parent = ParentState;
467 Entry.Handler = Handler;
468 Entry.HandlerType = HandlerType;
469 Entry.TypeToken = TypeToken;
470 FuncInfo.ClrEHUnwindMap.push_back(Entry);
471 return FuncInfo.ClrEHUnwindMap.size() - 1;
474 void llvm::calculateClrEHStateNumbers(const Function *Fn,
475 WinEHFuncInfo &FuncInfo) {
476 // Return if it's already been done.
477 if (!FuncInfo.EHPadStateMap.empty())
480 SmallVector<std::pair<const Instruction *, int>, 8> Worklist;
482 // Each pad needs to be able to refer to its parent, so scan the function
483 // looking for top-level handlers and seed the worklist with them.
484 for (const BasicBlock &BB : *Fn) {
487 if (BB.isLandingPad())
488 report_fatal_error("CoreCLR EH cannot use landingpads");
489 const Instruction *FirstNonPHI = BB.getFirstNonPHI();
490 if (!doesEHPadUnwindToCaller(FirstNonPHI))
492 // queue this with sentinel parent state -1 to mean unwind to caller.
493 Worklist.emplace_back(FirstNonPHI, -1);
496 while (!Worklist.empty()) {
497 const Instruction *Pad;
499 std::tie(Pad, ParentState) = Worklist.pop_back_val();
502 if (const CleanupEndPadInst *EndPad = dyn_cast<CleanupEndPadInst>(Pad)) {
503 FuncInfo.EHPadStateMap[EndPad] = ParentState;
504 // Queue the cleanuppad, in case it doesn't have a cleanupret.
505 Worklist.emplace_back(EndPad->getCleanupPad(), ParentState);
506 // Preds of the endpad should get the parent state.
507 PredState = ParentState;
508 } else if (const CleanupPadInst *Cleanup = dyn_cast<CleanupPadInst>(Pad)) {
509 // A cleanup can have multiple exits; don't re-process after the first.
510 if (FuncInfo.EHPadStateMap.count(Pad))
512 // CoreCLR personality uses arity to distinguish faults from finallies.
513 const BasicBlock *PadBlock = Cleanup->getParent();
514 ClrHandlerType HandlerType =
515 (Cleanup->getNumOperands() ? ClrHandlerType::Fault
516 : ClrHandlerType::Finally);
518 addClrEHHandler(FuncInfo, ParentState, HandlerType, 0, PadBlock);
519 FuncInfo.EHPadStateMap[Cleanup] = NewState;
520 // Propagate the new state to all preds of the cleanup
521 PredState = NewState;
522 } else if (const CatchEndPadInst *EndPad = dyn_cast<CatchEndPadInst>(Pad)) {
523 FuncInfo.EHPadStateMap[EndPad] = ParentState;
524 // Preds of the endpad should get the parent state.
525 PredState = ParentState;
526 } else if (const CatchPadInst *Catch = dyn_cast<CatchPadInst>(Pad)) {
527 const BasicBlock *PadBlock = Catch->getParent();
528 uint32_t TypeToken = static_cast<uint32_t>(
529 cast<ConstantInt>(Catch->getArgOperand(0))->getZExtValue());
530 int NewState = addClrEHHandler(FuncInfo, ParentState,
531 ClrHandlerType::Catch, TypeToken, PadBlock);
532 FuncInfo.EHPadStateMap[Catch] = NewState;
533 // Preds of the catch get its state
534 PredState = NewState;
536 llvm_unreachable("Unexpected EH pad");
539 // Queue all predecessors with the given state
540 for (const BasicBlock *Pred : predecessors(Pad->getParent())) {
541 if ((Pred = getEHPadFromPredecessor(Pred)))
542 Worklist.emplace_back(Pred->getFirstNonPHI(), PredState);
547 void WinEHPrepare::replaceTerminatePadWithCleanup(Function &F) {
548 if (Personality != EHPersonality::MSVC_CXX)
550 for (BasicBlock &BB : F) {
551 Instruction *First = BB.getFirstNonPHI();
552 auto *TPI = dyn_cast<TerminatePadInst>(First);
556 if (TPI->getNumArgOperands() != 1)
558 "Expected a unary terminatepad for MSVC C++ personalities!");
560 auto *TerminateFn = dyn_cast<Function>(TPI->getArgOperand(0));
562 report_fatal_error("Function operand expected in terminatepad for MSVC "
563 "C++ personalities!");
565 // Insert the cleanuppad instruction.
566 auto *CPI = CleanupPadInst::Create(
567 BB.getContext(), {}, Twine("terminatepad.for.", BB.getName()), &BB);
569 // Insert the call to the terminate instruction.
570 auto *CallTerminate = CallInst::Create(TerminateFn, {}, &BB);
571 CallTerminate->setDoesNotThrow();
572 CallTerminate->setDoesNotReturn();
573 CallTerminate->setCallingConv(TerminateFn->getCallingConv());
575 // Insert a new terminator for the cleanuppad using the same successor as
577 CleanupReturnInst::Create(CPI, TPI->getUnwindDest(), &BB);
579 // Let's remove the terminatepad now that we've inserted the new
581 TPI->eraseFromParent();
586 colorFunclets(Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks,
587 std::map<BasicBlock *, SetVector<BasicBlock *>> &BlockColors,
588 std::map<BasicBlock *, std::set<BasicBlock *>> &FuncletBlocks) {
589 SmallVector<std::pair<BasicBlock *, BasicBlock *>, 16> Worklist;
590 BasicBlock *EntryBlock = &F.getEntryBlock();
592 // Build up the color map, which maps each block to its set of 'colors'.
593 // For any block B, the "colors" of B are the set of funclets F (possibly
594 // including a root "funclet" representing the main function), such that
595 // F will need to directly contain B or a copy of B (where the term "directly
596 // contain" is used to distinguish from being "transitively contained" in
597 // a nested funclet).
598 // Use a CFG walk driven by a worklist of (block, color) pairs. The "color"
599 // sets attached during this processing to a block which is the entry of some
600 // funclet F is actually the set of F's parents -- i.e. the union of colors
601 // of all predecessors of F's entry. For all other blocks, the color sets
602 // are as defined above. A post-pass fixes up the block color map to reflect
603 // the same sense of "color" for funclet entries as for other blocks.
605 DEBUG_WITH_TYPE("winehprepare-coloring", dbgs() << "\nColoring funclets for "
606 << F.getName() << "\n");
608 Worklist.push_back({EntryBlock, EntryBlock});
610 while (!Worklist.empty()) {
611 BasicBlock *Visiting;
613 std::tie(Visiting, Color) = Worklist.pop_back_val();
614 DEBUG_WITH_TYPE("winehprepare-coloring",
615 dbgs() << "Visiting " << Visiting->getName() << ", "
616 << Color->getName() << "\n");
617 Instruction *VisitingHead = Visiting->getFirstNonPHI();
618 if (VisitingHead->isEHPad() && !isa<CatchEndPadInst>(VisitingHead) &&
619 !isa<CleanupEndPadInst>(VisitingHead)) {
620 // Mark this as a funclet head as a member of itself.
621 FuncletBlocks[Visiting].insert(Visiting);
622 // Queue exits (i.e. successors of rets/endpads) with the parent color.
623 // Skip any exits that are catchendpads, since the parent color must then
624 // represent one of the catches chained to that catchendpad, but the
625 // catchendpad should get the color of the common parent of all its
626 // chained catches (i.e. the grandparent color of the current pad).
627 // We don't need to worry abou catchendpads going unvisited, since the
628 // catches chained to them must have unwind edges to them by which we will
630 for (User *U : VisitingHead->users()) {
631 if (auto *Exit = dyn_cast<TerminatorInst>(U)) {
632 for (BasicBlock *Succ : successors(Exit->getParent()))
633 if (!isa<CatchEndPadInst>(*Succ->getFirstNonPHI()))
634 if (BlockColors[Succ].insert(Color)) {
635 DEBUG_WITH_TYPE("winehprepare-coloring",
636 dbgs() << " Assigned color \'"
637 << Color->getName() << "\' to block \'"
638 << Succ->getName() << "\'.\n");
639 Worklist.push_back({Succ, Color});
643 // Handle CatchPad specially since its successors need different colors.
644 if (CatchPadInst *CatchPad = dyn_cast<CatchPadInst>(VisitingHead)) {
645 // Visit the normal successor with the color of the new EH pad, and
646 // visit the unwind successor with the color of the parent.
647 BasicBlock *NormalSucc = CatchPad->getNormalDest();
648 if (BlockColors[NormalSucc].insert(Visiting)) {
649 DEBUG_WITH_TYPE("winehprepare-coloring",
650 dbgs() << " Assigned color \'" << Visiting->getName()
651 << "\' to block \'" << NormalSucc->getName()
653 Worklist.push_back({NormalSucc, Visiting});
655 BasicBlock *UnwindSucc = CatchPad->getUnwindDest();
656 if (BlockColors[UnwindSucc].insert(Color)) {
657 DEBUG_WITH_TYPE("winehprepare-coloring",
658 dbgs() << " Assigned color \'" << Color->getName()
659 << "\' to block \'" << UnwindSucc->getName()
661 Worklist.push_back({UnwindSucc, Color});
665 // Switch color to the current node, except for terminate pads which
666 // have no bodies and only unwind successors and so need their successors
667 // visited with the color of the parent.
668 if (!isa<TerminatePadInst>(VisitingHead))
671 // Note that this is a member of the given color.
672 FuncletBlocks[Color].insert(Visiting);
675 TerminatorInst *Terminator = Visiting->getTerminator();
676 if (isa<CleanupReturnInst>(Terminator) ||
677 isa<CatchReturnInst>(Terminator) ||
678 isa<CleanupEndPadInst>(Terminator)) {
679 // These blocks' successors have already been queued with the parent
683 for (BasicBlock *Succ : successors(Visiting)) {
684 if (isa<CatchEndPadInst>(Succ->getFirstNonPHI())) {
685 // The catchendpad needs to be visited with the parent's color, not
686 // the current color. This will happen in the code above that visits
687 // any catchpad unwind successor with the parent color, so we can
688 // safely skip this successor here.
691 if (BlockColors[Succ].insert(Color)) {
692 DEBUG_WITH_TYPE("winehprepare-coloring",
693 dbgs() << " Assigned color \'" << Color->getName()
694 << "\' to block \'" << Succ->getName()
696 Worklist.push_back({Succ, Color});
702 static BasicBlock *getEndPadForCatch(CatchPadInst *Catch) {
703 // The catch may have sibling catches. Follow the unwind chain until we get
704 // to the catchendpad.
705 BasicBlock *NextUnwindDest = Catch->getUnwindDest();
706 auto *UnwindTerminator = NextUnwindDest->getTerminator();
707 while (auto *NextCatch = dyn_cast<CatchPadInst>(UnwindTerminator)) {
708 NextUnwindDest = NextCatch->getUnwindDest();
709 UnwindTerminator = NextUnwindDest->getTerminator();
711 // The last catch in the chain must unwind to a catchendpad.
712 assert(isa<CatchEndPadInst>(UnwindTerminator));
713 return NextUnwindDest;
716 static void updateClonedEHPadUnwindToParent(
717 BasicBlock *UnwindDest, BasicBlock *OrigBlock, BasicBlock *CloneBlock,
718 std::vector<BasicBlock *> &OrigParents, BasicBlock *CloneParent) {
719 auto updateUnwindTerminator = [](BasicBlock *BB) {
720 auto *Terminator = BB->getTerminator();
721 if (isa<CatchEndPadInst>(Terminator) ||
722 isa<CleanupEndPadInst>(Terminator)) {
723 removeUnwindEdge(BB);
725 // If the block we're updating has a cleanupendpad or cleanupret
726 // terminator, we just want to replace that terminator with an
727 // unreachable instruction.
728 assert(isa<CleanupEndPadInst>(Terminator) ||
729 isa<CleanupReturnInst>(Terminator));
730 // Loop over all of the successors, removing the block's entry from any
732 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
733 (*SI)->removePredecessor(BB);
734 // Remove the terminator and replace it with an unreachable instruction.
735 BB->getTerminator()->eraseFromParent();
736 new UnreachableInst(BB->getContext(), BB);
740 assert(UnwindDest->isEHPad());
741 // There are many places to which this EH terminator can unwind and each has
742 // slightly different rules for whether or not it fits with the given
744 auto *EHPadInst = UnwindDest->getFirstNonPHI();
745 if (isa<CatchEndPadInst>(EHPadInst)) {
746 auto *CloneParentCatch =
747 dyn_cast<CatchPadInst>(CloneParent->getFirstNonPHI());
748 if (!CloneParentCatch ||
749 getEndPadForCatch(CloneParentCatch) != UnwindDest) {
751 "winehprepare-coloring",
752 dbgs() << " removing unwind destination of clone block \'"
753 << CloneBlock->getName() << "\'.\n");
754 updateUnwindTerminator(CloneBlock);
756 // It's possible that the catch end pad is a legal match for both the clone
757 // and the original, so they must be checked separately. If the original
758 // funclet will still have multiple parents after the current clone parent
759 // is removed, we'll leave its unwind terminator until later.
760 assert(OrigParents.size() >= 2);
761 if (OrigParents.size() != 2)
764 // If the original funclet will have a single parent after the clone parent
765 // is removed, check that parent's unwind destination.
766 assert(OrigParents.front() == CloneParent ||
767 OrigParents.back() == CloneParent);
768 BasicBlock *OrigParent;
769 if (OrigParents.front() == CloneParent)
770 OrigParent = OrigParents.back();
772 OrigParent = OrigParents.front();
774 auto *OrigParentCatch =
775 dyn_cast<CatchPadInst>(OrigParent->getFirstNonPHI());
776 if (!OrigParentCatch || getEndPadForCatch(OrigParentCatch) != UnwindDest) {
778 "winehprepare-coloring",
779 dbgs() << " removing unwind destination of original block \'"
780 << OrigBlock << "\'.\n");
781 updateUnwindTerminator(OrigBlock);
783 } else if (auto *CleanupEnd = dyn_cast<CleanupEndPadInst>(EHPadInst)) {
784 // If the EH terminator unwinds to a cleanupendpad, that cleanupendpad
785 // must be ending a cleanuppad of either our clone parent or one
786 // one of the parents of the original funclet.
787 auto *CloneParentCP =
788 dyn_cast<CleanupPadInst>(CloneParent->getFirstNonPHI());
789 auto *EndedCP = CleanupEnd->getCleanupPad();
790 if (EndedCP == CloneParentCP) {
791 // If it is ending the cleanuppad of our cloned parent, then we
792 // want to remove the unwind destination of the EH terminator that
793 // we associated with the original funclet.
794 assert(isa<CatchEndPadInst>(OrigBlock->getFirstNonPHI()));
796 "winehprepare-coloring",
797 dbgs() << " removing unwind destination of original block \'"
798 << OrigBlock->getName() << "\'.\n");
799 updateUnwindTerminator(OrigBlock);
801 // If it isn't ending the cleanuppad of our clone parent, then we
802 // want to remove the unwind destination of the EH terminator that
803 // associated with our cloned funclet.
804 assert(isa<CatchEndPadInst>(CloneBlock->getFirstNonPHI()));
806 "winehprepare-coloring",
807 dbgs() << " removing unwind destination of clone block \'"
808 << CloneBlock->getName() << "\'.\n");
809 updateUnwindTerminator(CloneBlock);
812 // If the EH terminator unwinds to a catchpad, cleanuppad or
813 // terminatepad that EH pad must be a sibling of the funclet we're
814 // cloning. We'll clone it later and update one of the catchendpad
815 // instrunctions that unwinds to it at that time.
816 assert(isa<CatchPadInst>(EHPadInst) || isa<CleanupPadInst>(EHPadInst) ||
817 isa<TerminatePadInst>(EHPadInst));
821 // If the terminator is a catchpad, we must also clone the catchendpad to which
822 // it unwinds and add this to the clone parent's block list. The catchendpad
823 // unwinds to either its caller, a sibling EH pad, a cleanup end pad in its
824 // parent funclet or a catch end pad in its grandparent funclet (which must be
825 // coupled with the parent funclet). If it has no unwind destination
826 // (i.e. unwind to caller), there is nothing to be done. If the unwind
827 // destination is a sibling EH pad, we will update the terminators later (in
828 // resolveFuncletAncestryForPath). If it unwinds to a cleanup end pad or a
829 // catch end pad and this end pad corresponds to the clone parent, we will
830 // remove the unwind destination in the original catchendpad. If it unwinds to
831 // a cleanup end pad or a catch end pad that does not correspond to the clone
832 // parent, we will remove the unwind destination in the cloned catchendpad.
833 static void updateCatchTerminators(
834 Function &F, CatchPadInst *OrigCatch, CatchPadInst *CloneCatch,
835 std::vector<BasicBlock *> &OrigParents, BasicBlock *CloneParent,
836 ValueToValueMapTy &VMap,
837 std::map<BasicBlock *, SetVector<BasicBlock *>> &BlockColors,
838 std::map<BasicBlock *, std::set<BasicBlock *>> &FuncletBlocks) {
839 // If we're cloning a catch pad that unwinds to a catchendpad, we also
840 // need to clone the catchendpad. The coloring algorithm associates
841 // the catchendpad block with the funclet's parent, so we have some work
842 // to do here to figure out whether the original belongs to the clone
843 // parent or one of the original funclets other parents (it might have
844 // more than one at this point). In either case, we might also need to
845 // remove the unwind edge if the catchendpad doesn't unwind to a block
846 // in the right grandparent funclet.
847 Instruction *I = CloneCatch->getUnwindDest()->getFirstNonPHI();
848 if (auto *CEP = dyn_cast<CatchEndPadInst>(I)) {
849 assert(BlockColors[CEP->getParent()].size() == 1);
850 BasicBlock *CEPFunclet = *(BlockColors[CEP->getParent()].begin());
851 BasicBlock *CEPCloneParent = nullptr;
852 CatchPadInst *PredCatch = nullptr;
853 if (CEPFunclet == CloneParent) {
854 // The catchendpad is in the clone parent, so we need to clone it
855 // and associate the clone with the original funclet's parent. If
856 // the original funclet had multiple parents, we'll add it to the
857 // first parent that isn't the clone parent. The logic in
858 // updateClonedEHPadUnwindToParent() will only remove the unwind edge
859 // if there is only one parent other than the clone parent, so we don't
860 // need to verify the ancestry. The catchendpad will eventually be
861 // cloned into the correct parent and all invalid unwind edges will be
863 for (auto *Parent : OrigParents) {
864 if (Parent != CloneParent) {
865 CEPCloneParent = Parent;
869 PredCatch = OrigCatch;
871 CEPCloneParent = CloneParent;
872 PredCatch = CloneCatch;
874 assert(CEPCloneParent && PredCatch);
875 DEBUG_WITH_TYPE("winehprepare-coloring",
876 dbgs() << " Cloning catchendpad \'"
877 << CEP->getParent()->getName() << "\' for funclet \'"
878 << CEPCloneParent->getName() << "\'.\n");
879 BasicBlock *ClonedCEP = CloneBasicBlock(
880 CEP->getParent(), VMap, Twine(".from.", CEPCloneParent->getName()));
881 // Insert the clone immediately after the original to ensure determinism
882 // and to keep the same relative ordering of any funclet's blocks.
883 ClonedCEP->insertInto(&F, CEP->getParent()->getNextNode());
884 PredCatch->setUnwindDest(ClonedCEP);
885 FuncletBlocks[CEPCloneParent].insert(ClonedCEP);
886 BlockColors[ClonedCEP].insert(CEPCloneParent);
887 DEBUG_WITH_TYPE("winehprepare-coloring",
888 dbgs() << " Assigning color \'"
889 << CEPCloneParent->getName() << "\' to block \'"
890 << ClonedCEP->getName() << "\'.\n");
891 auto *ClonedCEPInst = cast<CatchEndPadInst>(ClonedCEP->getTerminator());
892 if (auto *Dest = ClonedCEPInst->getUnwindDest())
893 updateClonedEHPadUnwindToParent(Dest, OrigCatch->getUnwindDest(),
894 CloneCatch->getUnwindDest(), OrigParents,
899 // While we are cloning a funclet because it has multiple parents, we will call
900 // this routine to update the terminators for the original and cloned copies
901 // of each basic block. All blocks in the funclet have been clone by this time.
902 // OrigBlock and CloneBlock will be identical except for their block label.
904 // If the terminator is a catchpad, we must also clone the catchendpad to which
905 // it unwinds and in most cases update either the original catchendpad or the
906 // clone. See the updateCatchTerminators() helper routine for details.
908 // If the terminator is a catchret its successor is a block in its parent
909 // funclet. If the instruction returns to a block in the parent for which the
910 // cloned funclet was created, the terminator in the original block must be
911 // replaced by an unreachable instruction. Otherwise the terminator in the
912 // clone block must be replaced by an unreachable instruction.
914 // If the terminator is a cleanupret or cleanupendpad it either unwinds to
915 // caller or unwinds to a sibling EH pad, a cleanup end pad in its parent
916 // funclet or a catch end pad in its grandparent funclet (which must be
917 // coupled with the parent funclet). If it unwinds to caller there is
918 // nothing to be done. If the unwind destination is a sibling EH pad, we will
919 // update the terminators later (in resolveFuncletAncestryForPath). If it
920 // unwinds to a cleanup end pad or a catch end pad and this end pad corresponds
921 // to the clone parent, we will replace the terminator in the original block
922 // with an unreachable instruction. If it unwinds to a cleanup end pad or a
923 // catch end pad that does not correspond to the clone parent, we will replace
924 // the terminator in the clone block with an unreachable instruction.
926 // If the terminator is an invoke instruction, we will handle it after all
927 // siblings of the current funclet have been cloned.
928 void WinEHPrepare::updateTerminatorsAfterFuncletClone(
929 Function &F, BasicBlock *OrigFunclet, BasicBlock *CloneFunclet,
930 BasicBlock *OrigBlock, BasicBlock *CloneBlock, BasicBlock *CloneParent,
931 ValueToValueMapTy &VMap, std::map<BasicBlock *, BasicBlock *> &Orig2Clone) {
932 // If the cloned block doesn't have an exceptional terminator, there is
933 // nothing to be done here.
934 TerminatorInst *CloneTerminator = CloneBlock->getTerminator();
935 if (!CloneTerminator->isExceptional())
938 if (auto *CloneCatch = dyn_cast<CatchPadInst>(CloneTerminator)) {
939 // A cloned catch pad has a lot of wrinkles, so we'll call a helper function
940 // to update this case.
941 auto *OrigCatch = cast<CatchPadInst>(OrigBlock->getTerminator());
942 updateCatchTerminators(F, OrigCatch, CloneCatch,
943 FuncletParents[OrigFunclet], CloneParent, VMap,
944 BlockColors, FuncletBlocks);
945 } else if (auto *CRI = dyn_cast<CatchReturnInst>(CloneTerminator)) {
946 if (FuncletBlocks[CloneParent].count(CRI->getSuccessor())) {
947 BasicBlock *OrigParent;
948 // The original funclet may have more than two parents, but that's OK.
949 // We just need to remap the original catchret to any of the parents.
950 // All of the parents should have an entry in the EstrangedBlocks map
951 // if any of them do.
952 if (FuncletParents[OrigFunclet].front() == CloneParent)
953 OrigParent = FuncletParents[OrigFunclet].back();
955 OrigParent = FuncletParents[OrigFunclet].front();
956 for (succ_iterator SI = succ_begin(OrigBlock), SE = succ_end(OrigBlock);
958 (*SI)->removePredecessor(OrigBlock);
959 BasicBlock *LostBlock = EstrangedBlocks[OrigParent][CRI->getSuccessor()];
960 auto *OrigCatchRet = cast<CatchReturnInst>(OrigBlock->getTerminator());
962 OrigCatchRet->setSuccessor(LostBlock);
964 OrigCatchRet->eraseFromParent();
965 new UnreachableInst(OrigBlock->getContext(), OrigBlock);
968 for (succ_iterator SI = succ_begin(CloneBlock), SE = succ_end(CloneBlock);
970 (*SI)->removePredecessor(CloneBlock);
971 BasicBlock *LostBlock = EstrangedBlocks[CloneParent][CRI->getSuccessor()];
973 CRI->setSuccessor(LostBlock);
975 CRI->eraseFromParent();
976 new UnreachableInst(CloneBlock->getContext(), CloneBlock);
979 } else if (isa<CleanupReturnInst>(CloneTerminator) ||
980 isa<CleanupEndPadInst>(CloneTerminator)) {
981 BasicBlock *UnwindDest = nullptr;
983 // A cleanup pad can unwind through either a cleanupret or a cleanupendpad
984 // but both are handled the same way.
985 if (auto *CRI = dyn_cast<CleanupReturnInst>(CloneTerminator))
986 UnwindDest = CRI->getUnwindDest();
987 else if (auto *CEI = dyn_cast<CleanupEndPadInst>(CloneTerminator))
988 UnwindDest = CEI->getUnwindDest();
990 // If the instruction has no local unwind destination, there is nothing
995 // The unwind destination may be a sibling EH pad, a catchendpad in
996 // a grandparent funclet (ending a catchpad in the parent) or a cleanup
997 // cleanupendpad in the parent. Call a helper routine to diagnose this
998 // and remove either the clone or original terminator as needed.
999 updateClonedEHPadUnwindToParent(UnwindDest, OrigBlock, CloneBlock,
1000 FuncletParents[OrigFunclet], CloneParent);
1004 // Clones all blocks used by the specified funclet to avoid the funclet having
1005 // multiple parent funclets. All terminators in the parent that unwind to the
1006 // original funclet are remapped to unwind to the clone. Any terminator in the
1007 // original funclet which returned to this parent is converted to an unreachable
1008 // instruction. Likewise, any terminator in the cloned funclet which returns to
1009 // a parent funclet other than the specified parent is converted to an
1010 // unreachable instruction.
1011 BasicBlock *WinEHPrepare::cloneFuncletForParent(Function &F,
1012 BasicBlock *FuncletEntry,
1013 BasicBlock *Parent) {
1014 std::set<BasicBlock *> &BlocksInFunclet = FuncletBlocks[FuncletEntry];
1016 DEBUG_WITH_TYPE("winehprepare-coloring",
1017 dbgs() << "Cloning funclet \'" << FuncletEntry->getName()
1018 << "\' for parent \'" << Parent->getName() << "\'.\n");
1020 std::map<BasicBlock *, BasicBlock *> Orig2Clone;
1021 ValueToValueMapTy VMap;
1022 for (BasicBlock *BB : BlocksInFunclet) {
1023 // Create a new basic block and copy instructions into it.
1025 CloneBasicBlock(BB, VMap, Twine(".from.", Parent->getName()));
1027 // Insert the clone immediately after the original to ensure determinism
1028 // and to keep the same relative ordering of any funclet's blocks.
1029 CBB->insertInto(&F, BB->getNextNode());
1031 // Add basic block mapping.
1034 // Record delta operations that we need to perform to our color mappings.
1035 Orig2Clone[BB] = CBB;
1036 } // end for (BasicBlock *BB : BlocksInFunclet)
1038 BasicBlock *ClonedFunclet = Orig2Clone[FuncletEntry];
1039 assert(ClonedFunclet);
1041 // Set the coloring for the blocks we just cloned.
1042 std::set<BasicBlock *> &ClonedBlocks = FuncletBlocks[ClonedFunclet];
1043 for (auto &BBMapping : Orig2Clone) {
1044 BasicBlock *NewBlock = BBMapping.second;
1045 ClonedBlocks.insert(NewBlock);
1046 BlockColors[NewBlock].insert(ClonedFunclet);
1048 DEBUG_WITH_TYPE("winehprepare-coloring",
1049 dbgs() << " Assigning color \'" << ClonedFunclet->getName()
1050 << "\' to block \'" << NewBlock->getName()
1053 // Use the VMap to remap the instructions in this cloned block.
1054 for (Instruction &I : *NewBlock)
1055 RemapInstruction(&I, VMap, RF_IgnoreMissingEntries);
1058 // All the cloned blocks have to be colored in the loop above before we can
1059 // update the terminators because doing so can require checking the color of
1060 // other blocks in the cloned funclet.
1061 for (auto &BBMapping : Orig2Clone) {
1062 BasicBlock *OldBlock = BBMapping.first;
1063 BasicBlock *NewBlock = BBMapping.second;
1065 // Update the terminator, if necessary, in both the original block and the
1066 // cloned so that the original funclet never returns to a block in the
1067 // clone parent and the clone funclet never returns to a block in any other
1068 // of the original funclet's parents.
1069 updateTerminatorsAfterFuncletClone(F, FuncletEntry, ClonedFunclet, OldBlock,
1070 NewBlock, Parent, VMap, Orig2Clone);
1072 // Check to see if the cloned block successor has PHI nodes. If so, we need
1073 // to add entries to the PHI nodes for the cloned block now.
1074 for (BasicBlock *SuccBB : successors(NewBlock)) {
1075 for (Instruction &SuccI : *SuccBB) {
1076 auto *SuccPN = dyn_cast<PHINode>(&SuccI);
1080 // Ok, we have a PHI node. Figure out what the incoming value was for
1082 int OldBlockIdx = SuccPN->getBasicBlockIndex(OldBlock);
1083 if (OldBlockIdx == -1)
1085 Value *IV = SuccPN->getIncomingValue(OldBlockIdx);
1087 // Remap the value if necessary.
1088 if (auto *Inst = dyn_cast<Instruction>(IV)) {
1089 ValueToValueMapTy::iterator I = VMap.find(Inst);
1090 if (I != VMap.end())
1094 SuccPN->addIncoming(IV, NewBlock);
1099 // Erase the clone's parent from the original funclet's parent list.
1100 std::vector<BasicBlock *> &Parents = FuncletParents[FuncletEntry];
1101 Parents.erase(std::remove(Parents.begin(), Parents.end(), Parent),
1104 // Store the cloned funclet's parent.
1105 assert(std::find(FuncletParents[ClonedFunclet].begin(),
1106 FuncletParents[ClonedFunclet].end(),
1107 Parent) == std::end(FuncletParents[ClonedFunclet]));
1108 FuncletParents[ClonedFunclet].push_back(Parent);
1110 // Copy any children of the original funclet to the clone. We'll either
1111 // clone them too or make that path unreachable when we take the next step
1112 // in resolveFuncletAncestryForPath().
1113 for (auto *Child : FuncletChildren[FuncletEntry]) {
1114 assert(std::find(FuncletChildren[ClonedFunclet].begin(),
1115 FuncletChildren[ClonedFunclet].end(),
1116 Child) == std::end(FuncletChildren[ClonedFunclet]));
1117 FuncletChildren[ClonedFunclet].push_back(Child);
1118 assert(std::find(FuncletParents[Child].begin(), FuncletParents[Child].end(),
1119 ClonedFunclet) == std::end(FuncletParents[Child]));
1120 FuncletParents[Child].push_back(ClonedFunclet);
1123 // Find any blocks that unwound to the original funclet entry from the
1124 // clone parent block and remap them to the clone.
1125 for (auto *U : FuncletEntry->users()) {
1126 auto *UT = dyn_cast<TerminatorInst>(U);
1129 BasicBlock *UBB = UT->getParent();
1130 assert(BlockColors[UBB].size() == 1);
1131 BasicBlock *UFunclet = *(BlockColors[UBB].begin());
1132 // Funclets shouldn't be able to loop back on themselves.
1133 assert(UFunclet != FuncletEntry);
1134 // If this instruction unwinds to the original funclet from the clone
1135 // parent, remap the terminator so that it unwinds to the clone instead.
1136 // We will perform a similar transformation for siblings after all
1137 // the siblings have been cloned.
1138 if (UFunclet == Parent) {
1139 // We're about to break the path from this block to the uncloned funclet
1140 // entry, so remove it as a predeccessor to clean up the PHIs.
1141 FuncletEntry->removePredecessor(UBB);
1142 TerminatorInst *Terminator = UBB->getTerminator();
1143 RemapInstruction(Terminator, VMap, RF_IgnoreMissingEntries);
1147 // This asserts a condition that is relied upon inside the loop below,
1148 // namely that no predecessors of the original funclet entry block
1149 // are also predecessors of the cloned funclet entry block.
1150 assert(std::all_of(pred_begin(FuncletEntry), pred_end(FuncletEntry),
1151 [&ClonedFunclet](BasicBlock *Pred) {
1152 return std::find(pred_begin(ClonedFunclet),
1153 pred_end(ClonedFunclet),
1154 Pred) == pred_end(ClonedFunclet);
1157 // Remove any invalid PHI node entries in the cloned funclet.cl
1158 std::vector<PHINode *> PHIsToErase;
1159 for (Instruction &I : *ClonedFunclet) {
1160 auto *PN = dyn_cast<PHINode>(&I);
1164 // Predecessors of the original funclet do not reach the cloned funclet,
1165 // but the cloning process assumes they will. Remove them now.
1166 for (auto *Pred : predecessors(FuncletEntry))
1167 PN->removeIncomingValue(Pred, false);
1169 for (auto *PN : PHIsToErase)
1170 PN->eraseFromParent();
1172 // Replace the original funclet in the parent's children vector with the
1174 for (auto &It : FuncletChildren[Parent]) {
1175 if (It == FuncletEntry) {
1181 return ClonedFunclet;
1184 // Removes the unwind edge for any exceptional terminators within the specified
1185 // parent funclet that previously unwound to the specified child funclet.
1186 void WinEHPrepare::makeFuncletEdgeUnreachable(BasicBlock *Parent,
1187 BasicBlock *Child) {
1188 for (BasicBlock *BB : FuncletBlocks[Parent]) {
1189 TerminatorInst *Terminator = BB->getTerminator();
1190 if (!Terminator->isExceptional())
1193 // Look for terninators that unwind to the child funclet.
1194 BasicBlock *UnwindDest = nullptr;
1195 if (auto *I = dyn_cast<InvokeInst>(Terminator))
1196 UnwindDest = I->getUnwindDest();
1197 else if (auto *I = dyn_cast<CatchEndPadInst>(Terminator))
1198 UnwindDest = I->getUnwindDest();
1199 else if (auto *I = dyn_cast<TerminatePadInst>(Terminator))
1200 UnwindDest = I->getUnwindDest();
1201 // cleanupendpad, catchret and cleanupret don't represent a parent-to-child
1202 // funclet transition, so we don't need to consider them here.
1204 // If the child funclet is the unwind destination, replace the terminator
1205 // with an unreachable instruction.
1206 if (UnwindDest == Child)
1207 removeUnwindEdge(BB);
1209 // The specified parent is no longer a parent of the specified child.
1210 std::vector<BasicBlock *> &Children = FuncletChildren[Parent];
1211 Children.erase(std::remove(Children.begin(), Children.end(), Child),
1215 // This routine is called after funclets with multiple parents are cloned for
1216 // a specific parent. Here we look for children of the specified funclet that
1217 // unwind to other children of that funclet and update the unwind destinations
1218 // to ensure that each sibling is connected to the correct clone of the sibling
1219 // to which it unwinds.
1221 // If the terminator is an invoke instruction, it unwinds either to a child
1222 // EH pad, a cleanup end pad in the current funclet, or a catch end pad in a
1223 // parent funclet (which ends either the current catch pad or a sibling
1224 // catch pad). If it unwinds to a child EH pad, the child will have multiple
1225 // parents after this funclet is cloned and this case will be handled later in
1226 // the resolveFuncletAncestryForPath processing. If it unwinds to a
1227 // cleanup end pad in the current funclet, the instruction remapping during
1228 // the cloning process should have already mapped the unwind destination to
1229 // the cloned copy of the cleanup end pad. If it unwinds to a catch end pad
1230 // there are two possibilities: either the catch end pad is the unwind
1231 // destination for the catch pad we are currently cloning or it is the unwind
1232 // destination for a sibling catch pad. If it it the unwind destination of the
1233 // catch pad we are cloning, we need to update the cloned invoke instruction
1234 // to unwind to the cloned catch end pad. Otherwise, we will handle this
1235 // later (in resolveFuncletAncestryForPath).
1236 static void updateSiblingToSiblingUnwind(
1237 BasicBlock *CurFunclet,
1238 std::map<BasicBlock *, SetVector<BasicBlock *>> &BlockColors,
1239 std::map<BasicBlock *, std::set<BasicBlock *>> &FuncletBlocks,
1240 std::map<BasicBlock *, std::vector<BasicBlock *>> &FuncletParents,
1241 std::map<BasicBlock *, std::vector<BasicBlock *>> &FuncletChildren,
1242 std::map<BasicBlock *, BasicBlock *> &Funclet2Orig) {
1243 // Remap any bad sibling-to-sibling transitions for funclets that
1245 for (BasicBlock *ChildFunclet : FuncletChildren[CurFunclet]) {
1246 for (auto *BB : FuncletBlocks[ChildFunclet]) {
1247 TerminatorInst *Terminator = BB->getTerminator();
1248 if (!Terminator->isExceptional())
1251 // See if this terminator has an unwind destination.
1252 // Note that catchendpads are handled when the associated catchpad
1253 // is cloned. They don't fit the pattern we're looking for here.
1254 BasicBlock *UnwindDest = nullptr;
1255 if (auto *I = dyn_cast<CatchPadInst>(Terminator)) {
1256 UnwindDest = I->getUnwindDest();
1257 // The catchendpad is not a sibling destination. This case should
1258 // have been handled in cloneFuncletForParent().
1259 if (isa<CatchEndPadInst>(Terminator)) {
1260 assert(BlockColors[UnwindDest].size() == 1 &&
1261 "Cloned catchpad unwinds to an pad with multiple parents.");
1262 assert(FuncletParents[UnwindDest].front() == CurFunclet &&
1263 "Cloned catchpad unwinds to the wrong parent.");
1267 if (auto *I = dyn_cast<CleanupReturnInst>(Terminator))
1268 UnwindDest = I->getUnwindDest();
1269 else if (auto *I = dyn_cast<CleanupEndPadInst>(Terminator))
1270 UnwindDest = I->getUnwindDest();
1272 // If the cleanup unwinds to caller, there is nothing to be done.
1277 // If the destination is not a cleanup pad, catch pad or terminate pad
1278 // we don't need to handle it here.
1279 Instruction *EHPad = UnwindDest->getFirstNonPHI();
1280 if (!isa<CleanupPadInst>(EHPad) && !isa<CatchPadInst>(EHPad) &&
1281 !isa<TerminatePadInst>(EHPad))
1284 // If it is one of these, then it is either a sibling of the current
1285 // child funclet or a clone of one of those siblings.
1286 // If it is a sibling, no action is needed.
1287 if (FuncletParents[UnwindDest].size() == 1 &&
1288 FuncletParents[UnwindDest].front() == CurFunclet)
1291 // If the unwind destination is a clone of a sibling, we need to figure
1292 // out which sibling it is a clone of and use that sibling as the
1293 // unwind destination.
1294 BasicBlock *DestOrig = Funclet2Orig[UnwindDest];
1295 BasicBlock *TargetSibling = nullptr;
1296 for (auto &Mapping : Funclet2Orig) {
1297 if (Mapping.second != DestOrig)
1299 BasicBlock *MappedFunclet = Mapping.first;
1300 if (FuncletParents[MappedFunclet].size() == 1 &&
1301 FuncletParents[MappedFunclet].front() == CurFunclet) {
1302 TargetSibling = MappedFunclet;
1305 // If we didn't find the sibling we were looking for then the
1306 // unwind destination is not a clone of one of child's siblings.
1307 // That's unexpected.
1308 assert(TargetSibling && "Funclet unwinds to unexpected destination.");
1310 // Update the terminator instruction to unwind to the correct sibling.
1311 if (auto *I = dyn_cast<CatchPadInst>(Terminator))
1312 I->setUnwindDest(TargetSibling);
1313 else if (auto *I = dyn_cast<CleanupReturnInst>(Terminator))
1314 I->setUnwindDest(TargetSibling);
1315 else if (auto *I = dyn_cast<CleanupEndPadInst>(Terminator))
1316 I->setUnwindDest(TargetSibling);
1320 // Invoke remapping can't be done correctly until after all of their
1321 // other sibling-to-sibling unwinds have been remapped.
1322 for (BasicBlock *ChildFunclet : FuncletChildren[CurFunclet]) {
1323 bool NeedOrigInvokeRemapping = false;
1324 for (auto *BB : FuncletBlocks[ChildFunclet]) {
1325 TerminatorInst *Terminator = BB->getTerminator();
1326 auto *II = dyn_cast<InvokeInst>(Terminator);
1330 BasicBlock *UnwindDest = II->getUnwindDest();
1331 assert(UnwindDest && "Invoke unwinds to a null destination.");
1332 assert(UnwindDest->isEHPad() && "Invoke does not unwind to an EH pad.");
1333 auto *EHPadInst = UnwindDest->getFirstNonPHI();
1334 if (isa<CleanupEndPadInst>(EHPadInst)) {
1335 // An invoke that unwinds to a cleanup end pad must be in a cleanup pad.
1336 assert(isa<CleanupPadInst>(ChildFunclet->getFirstNonPHI()) &&
1337 "Unwinding to cleanup end pad from a non cleanup pad funclet.");
1338 // The funclet cloning should have remapped the destination to the cloned
1340 assert(FuncletBlocks[ChildFunclet].count(UnwindDest) &&
1341 "Unwind destination for invoke was not updated during cloning.");
1342 } else if (isa<CatchEndPadInst>(EHPadInst)) {
1343 // If the invoke unwind destination is the unwind destination for
1344 // the current child catch pad funclet, there is nothing to be done.
1345 BasicBlock *OrigFunclet = Funclet2Orig[ChildFunclet];
1346 auto *CurCatch = cast<CatchPadInst>(ChildFunclet->getFirstNonPHI());
1347 auto *OrigCatch = cast<CatchPadInst>(OrigFunclet->getFirstNonPHI());
1348 if (OrigCatch->getUnwindDest() == UnwindDest) {
1349 // If the invoke unwinds to a catch end pad that is the unwind
1350 // destination for the original catch pad, the cloned invoke should
1351 // unwind to the cloned catch end pad.
1352 II->setUnwindDest(CurCatch->getUnwindDest());
1353 } else if (CurCatch->getUnwindDest() == UnwindDest) {
1354 // If the invoke unwinds to a catch end pad that is the unwind
1355 // destination for the clone catch pad, the original invoke should
1356 // unwind to the unwind destination of the original catch pad.
1357 // This happens when the catch end pad is matched to the clone
1358 // parent when the catchpad instruction is cloned and the original
1359 // invoke instruction unwinds to the original catch end pad (which
1360 // is now the unwind destination of the cloned catch pad).
1361 NeedOrigInvokeRemapping = true;
1363 // Otherwise, the invoke unwinds to a catch end pad that is the unwind
1364 // destination another catch pad in the unwind chain from either the
1365 // current catch pad or one of its clones. If it is already the
1366 // catch end pad at the end unwind chain from the current catch pad,
1367 // we'll need to check the invoke instructions in the original funclet
1368 // later. Otherwise, we need to remap this invoke now.
1369 assert((getEndPadForCatch(OrigCatch) == UnwindDest ||
1370 getEndPadForCatch(CurCatch) == UnwindDest) &&
1371 "Invoke within catch pad unwinds to an invalid catch end pad.");
1372 BasicBlock *CurCatchEnd = getEndPadForCatch(CurCatch);
1373 if (CurCatchEnd == UnwindDest)
1374 NeedOrigInvokeRemapping = true;
1376 II->setUnwindDest(CurCatchEnd);
1380 if (NeedOrigInvokeRemapping) {
1381 // To properly remap invoke instructions that unwind to catch end pads
1382 // that are not the unwind destination of the catch pad funclet in which
1383 // the invoke appears, we must also look at the uncloned invoke in the
1384 // original funclet. If we saw an invoke that was already properly
1385 // unwinding to a sibling's catch end pad, we need to check the invokes
1386 // in the original funclet.
1387 BasicBlock *OrigFunclet = Funclet2Orig[ChildFunclet];
1388 for (auto *BB : FuncletBlocks[OrigFunclet]) {
1389 auto *II = dyn_cast<InvokeInst>(BB->getTerminator());
1393 BasicBlock *UnwindDest = II->getUnwindDest();
1394 assert(UnwindDest && "Invoke unwinds to a null destination.");
1395 assert(UnwindDest->isEHPad() && "Invoke does not unwind to an EH pad.");
1396 auto *CEP = dyn_cast<CatchEndPadInst>(UnwindDest->getFirstNonPHI());
1400 // If the invoke unwind destination is the unwind destination for
1401 // the original catch pad funclet, there is nothing to be done.
1402 auto *OrigCatch = cast<CatchPadInst>(OrigFunclet->getFirstNonPHI());
1404 // If the invoke unwinds to a catch end pad that is neither the unwind
1405 // destination of OrigCatch or the destination another catch pad in the
1406 // unwind chain from current catch pad, we need to remap the invoke.
1407 BasicBlock *OrigCatchEnd = getEndPadForCatch(OrigCatch);
1408 if (OrigCatchEnd != UnwindDest)
1409 II->setUnwindDest(OrigCatchEnd);
1415 void WinEHPrepare::resolveFuncletAncestry(
1416 Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks) {
1417 // Most of the time this will be unnecessary. If the conditions arise that
1418 // require this work, this flag will be set.
1419 if (!FuncletCloningRequired)
1422 // Funclet2Orig is used to map any cloned funclets back to the original
1423 // funclet from which they were cloned. The map is seeded with the
1424 // original funclets mapping to themselves.
1425 std::map<BasicBlock *, BasicBlock *> Funclet2Orig;
1426 for (auto *Funclet : EntryBlocks)
1427 Funclet2Orig[Funclet] = Funclet;
1429 // Start with the entry funclet and walk the funclet parent-child tree.
1430 SmallVector<BasicBlock *, 4> FuncletPath;
1431 FuncletPath.push_back(&(F.getEntryBlock()));
1432 resolveFuncletAncestryForPath(F, FuncletPath, Funclet2Orig);
1435 // Walks the funclet control flow, cloning any funclets that have more than one
1436 // parent funclet and breaking any cyclic unwind chains so that the path becomes
1437 // unreachable at the point where a funclet would have unwound to a funclet that
1438 // was already in the chain.
1439 void WinEHPrepare::resolveFuncletAncestryForPath(
1440 Function &F, SmallVectorImpl<BasicBlock *> &FuncletPath,
1441 std::map<BasicBlock *, BasicBlock *> &Funclet2Orig) {
1442 bool ClonedAnyChildren = false;
1443 BasicBlock *CurFunclet = FuncletPath.back();
1444 // Copy the children vector because we might changing it.
1445 std::vector<BasicBlock *> Children(FuncletChildren[CurFunclet]);
1446 for (BasicBlock *ChildFunclet : Children) {
1447 // Don't allow the funclet chain to unwind back on itself.
1448 // If this funclet is already in the current funclet chain, make the
1449 // path to it through the current funclet unreachable.
1450 bool IsCyclic = false;
1451 BasicBlock *ChildIdentity = Funclet2Orig[ChildFunclet];
1452 for (BasicBlock *Ancestor : FuncletPath) {
1453 BasicBlock *AncestorIdentity = Funclet2Orig[Ancestor];
1454 if (AncestorIdentity == ChildIdentity) {
1459 // If the unwind chain wraps back on itself, break the chain.
1461 makeFuncletEdgeUnreachable(CurFunclet, ChildFunclet);
1464 // If this child funclet has other parents, clone the entire funclet.
1465 if (FuncletParents[ChildFunclet].size() > 1) {
1466 ChildFunclet = cloneFuncletForParent(F, ChildFunclet, CurFunclet);
1467 Funclet2Orig[ChildFunclet] = ChildIdentity;
1468 ClonedAnyChildren = true;
1470 FuncletPath.push_back(ChildFunclet);
1471 resolveFuncletAncestryForPath(F, FuncletPath, Funclet2Orig);
1472 FuncletPath.pop_back();
1474 // If we didn't clone any children, we can return now.
1475 if (!ClonedAnyChildren)
1478 updateSiblingToSiblingUnwind(CurFunclet, BlockColors, FuncletBlocks,
1479 FuncletParents, FuncletChildren, Funclet2Orig);
1482 void WinEHPrepare::colorFunclets(Function &F,
1483 SmallVectorImpl<BasicBlock *> &EntryBlocks) {
1484 ::colorFunclets(F, EntryBlocks, BlockColors, FuncletBlocks);
1486 // The processing above actually accumulated the parent set for this
1487 // funclet into the color set for its entry; use the parent set to
1488 // populate the children map, and reset the color set to include just
1489 // the funclet itself (no instruction can target a funclet entry except on
1490 // that transitions to the child funclet).
1491 for (BasicBlock *FuncletEntry : EntryBlocks) {
1492 SetVector<BasicBlock *> &ColorMapItem = BlockColors[FuncletEntry];
1493 // It will be rare for funclets to have multiple parents, but if any
1494 // do we need to clone the funclet later to address that. Here we
1495 // set a flag indicating that this case has arisen so that we don't
1496 // have to do a lot of checking later to handle the more common case.
1497 if (ColorMapItem.size() > 1)
1498 FuncletCloningRequired = true;
1499 for (BasicBlock *Parent : ColorMapItem) {
1500 assert(std::find(FuncletChildren[Parent].begin(),
1501 FuncletChildren[Parent].end(),
1502 FuncletEntry) == std::end(FuncletChildren[Parent]));
1503 FuncletChildren[Parent].push_back(FuncletEntry);
1504 assert(std::find(FuncletParents[FuncletEntry].begin(),
1505 FuncletParents[FuncletEntry].end(),
1506 Parent) == std::end(FuncletParents[FuncletEntry]));
1507 FuncletParents[FuncletEntry].push_back(Parent);
1509 ColorMapItem.clear();
1510 ColorMapItem.insert(FuncletEntry);
1514 void llvm::calculateCatchReturnSuccessorColors(const Function *Fn,
1515 WinEHFuncInfo &FuncInfo) {
1516 SmallVector<BasicBlock *, 4> EntryBlocks;
1517 // colorFunclets needs the set of EntryBlocks, get them using
1518 // findFuncletEntryPoints.
1519 findFuncletEntryPoints(const_cast<Function &>(*Fn), EntryBlocks);
1521 std::map<BasicBlock *, SetVector<BasicBlock *>> BlockColors;
1522 std::map<BasicBlock *, std::set<BasicBlock *>> FuncletBlocks;
1523 // Figure out which basic blocks belong to which funclets.
1524 colorFunclets(const_cast<Function &>(*Fn), EntryBlocks, BlockColors,
1527 // The static colorFunclets routine assigns multiple colors to funclet entries
1528 // because that information is needed to calculate funclets' parent-child
1529 // relationship, but we don't need those relationship here and ultimately the
1530 // entry blocks should have the color of the funclet they begin.
1531 for (BasicBlock *FuncletEntry : EntryBlocks) {
1532 BlockColors[FuncletEntry].clear();
1533 BlockColors[FuncletEntry].insert(FuncletEntry);
1536 // We need to find the catchret successors. To do this, we must first find
1537 // all the catchpad funclets.
1538 for (auto &Funclet : FuncletBlocks) {
1539 // Figure out what kind of funclet we are looking at; We only care about
1541 BasicBlock *FuncletPadBB = Funclet.first;
1542 Instruction *FirstNonPHI = FuncletPadBB->getFirstNonPHI();
1543 auto *CatchPad = dyn_cast<CatchPadInst>(FirstNonPHI);
1547 // The users of a catchpad are always catchrets.
1548 for (User *Exit : CatchPad->users()) {
1549 auto *CatchReturn = dyn_cast<CatchReturnInst>(Exit);
1552 BasicBlock *CatchRetSuccessor = CatchReturn->getSuccessor();
1553 SetVector<BasicBlock *> &SuccessorColors = BlockColors[CatchRetSuccessor];
1554 assert(SuccessorColors.size() == 1 && "Expected BB to be monochrome!");
1555 BasicBlock *Color = *SuccessorColors.begin();
1556 // Record the catchret successor's funclet membership.
1557 FuncInfo.CatchRetSuccessorColorMap[CatchReturn] = Color;
1562 void WinEHPrepare::demotePHIsOnFunclets(Function &F) {
1563 // Strip PHI nodes off of EH pads.
1564 SmallVector<PHINode *, 16> PHINodes;
1565 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) {
1566 BasicBlock *BB = &*FI++;
1569 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
1570 Instruction *I = &*BI++;
1571 auto *PN = dyn_cast<PHINode>(I);
1572 // Stop at the first non-PHI.
1576 AllocaInst *SpillSlot = insertPHILoads(PN, F);
1578 insertPHIStores(PN, SpillSlot);
1580 PHINodes.push_back(PN);
1584 for (auto *PN : PHINodes) {
1585 // There may be lingering uses on other EH PHIs being removed
1586 PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
1587 PN->eraseFromParent();
1591 void WinEHPrepare::demoteUsesBetweenFunclets(Function &F) {
1592 // Turn all inter-funclet uses of a Value into loads and stores.
1593 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) {
1594 BasicBlock *BB = &*FI++;
1595 SetVector<BasicBlock *> &ColorsForBB = BlockColors[BB];
1596 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
1597 Instruction *I = &*BI++;
1598 // Funclets are permitted to use static allocas.
1599 if (auto *AI = dyn_cast<AllocaInst>(I))
1600 if (AI->isStaticAlloca())
1603 demoteNonlocalUses(I, ColorsForBB, F);
1608 void WinEHPrepare::demoteArgumentUses(Function &F) {
1609 // Also demote function parameters used in funclets.
1610 SetVector<BasicBlock *> &ColorsForEntry = BlockColors[&F.getEntryBlock()];
1611 for (Argument &Arg : F.args())
1612 demoteNonlocalUses(&Arg, ColorsForEntry, F);
1615 void WinEHPrepare::cloneCommonBlocks(
1616 Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks) {
1617 // We need to clone all blocks which belong to multiple funclets. Values are
1618 // remapped throughout the funclet to propogate both the new instructions
1619 // *and* the new basic blocks themselves.
1620 for (BasicBlock *FuncletPadBB : EntryBlocks) {
1621 std::set<BasicBlock *> &BlocksInFunclet = FuncletBlocks[FuncletPadBB];
1623 std::map<BasicBlock *, BasicBlock *> Orig2Clone;
1624 ValueToValueMapTy VMap;
1625 for (auto BlockIt = BlocksInFunclet.begin(),
1626 BlockEnd = BlocksInFunclet.end();
1627 BlockIt != BlockEnd;) {
1628 // Increment the iterator inside the loop because we might be removing
1629 // blocks from the set.
1630 BasicBlock *BB = *BlockIt++;
1631 SetVector<BasicBlock *> &ColorsForBB = BlockColors[BB];
1632 // We don't need to do anything if the block is monochromatic.
1633 size_t NumColorsForBB = ColorsForBB.size();
1634 if (NumColorsForBB == 1)
1637 // If this block is a catchendpad, it shouldn't be cloned.
1638 // We will only see a catchendpad with multiple colors in the case where
1639 // some funclet has multiple parents. In that case, the color will be
1640 // resolved during the resolveFuncletAncestry processing.
1641 // For now, find the catchpad that unwinds to this block and assign
1642 // that catchpad's first parent to be the color for this block.
1643 if (isa<CatchEndPadInst>(BB->getFirstNonPHI())) {
1645 FuncletCloningRequired &&
1646 "Found multi-colored catchendpad with no multi-parent funclets.");
1647 BasicBlock *CatchParent = nullptr;
1648 // There can only be one catchpad predecessor for a catchendpad.
1649 for (BasicBlock *PredBB : predecessors(BB)) {
1650 if (isa<CatchPadInst>(PredBB->getTerminator())) {
1651 CatchParent = PredBB;
1655 // There must be one catchpad predecessor for a catchendpad.
1656 assert(CatchParent && "No catchpad found for catchendpad.");
1658 // If the catchpad has multiple parents, we'll clone the catchendpad
1659 // when we clone the catchpad funclet and insert it into the correct
1660 // funclet. For now, we just select the first parent of the catchpad
1661 // and give the catchendpad that color.
1662 BasicBlock *CorrectColor = FuncletParents[CatchParent].front();
1663 assert(FuncletBlocks[CorrectColor].count(BB));
1664 assert(BlockColors[BB].count(CorrectColor));
1666 // Remove this block from the FuncletBlocks set of any funclet that
1667 // isn't the funclet whose color we just selected.
1668 for (auto It = BlockColors[BB].begin(), End = BlockColors[BB].end();
1670 // The iterator must be incremented here because we are removing
1671 // elements from the set we're walking.
1673 BasicBlock *ContainingFunclet = *Temp;
1674 if (ContainingFunclet != CorrectColor) {
1675 FuncletBlocks[ContainingFunclet].erase(BB);
1676 BlockColors[BB].remove(ContainingFunclet);
1680 // This should leave just one color for BB.
1681 assert(BlockColors[BB].size() == 1);
1685 DEBUG_WITH_TYPE("winehprepare-coloring",
1686 dbgs() << " Cloning block \'" << BB->getName()
1687 << "\' for funclet \'" << FuncletPadBB->getName()
1690 // Create a new basic block and copy instructions into it!
1692 CloneBasicBlock(BB, VMap, Twine(".for.", FuncletPadBB->getName()));
1693 // Insert the clone immediately after the original to ensure determinism
1694 // and to keep the same relative ordering of any funclet's blocks.
1695 CBB->insertInto(&F, BB->getNextNode());
1697 // Add basic block mapping.
1700 // Record delta operations that we need to perform to our color mappings.
1701 Orig2Clone[BB] = CBB;
1704 // If nothing was cloned, we're done cloning in this funclet.
1705 if (Orig2Clone.empty())
1708 // Update our color mappings to reflect that one block has lost a color and
1709 // another has gained a color.
1710 for (auto &BBMapping : Orig2Clone) {
1711 BasicBlock *OldBlock = BBMapping.first;
1712 BasicBlock *NewBlock = BBMapping.second;
1714 BlocksInFunclet.insert(NewBlock);
1715 BlockColors[NewBlock].insert(FuncletPadBB);
1717 DEBUG_WITH_TYPE("winehprepare-coloring",
1718 dbgs() << " Assigned color \'" << FuncletPadBB->getName()
1719 << "\' to block \'" << NewBlock->getName()
1722 BlocksInFunclet.erase(OldBlock);
1723 BlockColors[OldBlock].remove(FuncletPadBB);
1725 DEBUG_WITH_TYPE("winehprepare-coloring",
1726 dbgs() << " Removed color \'" << FuncletPadBB->getName()
1727 << "\' from block \'" << OldBlock->getName()
1730 // If we are cloning a funclet that might share a child funclet with
1731 // another funclet, look to see if the cloned block is reached from a
1732 // catchret instruction. If so, save this association so we can retrieve
1733 // the possibly orphaned clone when we clone the child funclet.
1734 if (FuncletCloningRequired) {
1735 for (auto *Pred : predecessors(OldBlock)) {
1736 auto *Terminator = Pred->getTerminator();
1737 if (!isa<CatchReturnInst>(Terminator))
1739 // If this block is reached from a catchret instruction in a funclet
1740 // that has multiple parents, it will have a color for each of those
1741 // parents. We just removed the color of one of the parents, but
1742 // the cloned block will be unreachable until we clone the child
1743 // funclet that contains the catchret instruction. In that case we
1744 // need to create a mapping that will let us find the cloned block
1745 // later and associate it with the cloned child funclet.
1746 bool BlockWillBeEstranged = false;
1747 for (auto *Color : BlockColors[Pred]) {
1748 if (FuncletParents[Color].size() > 1) {
1749 BlockWillBeEstranged = true;
1750 break; // Breaks out of the color loop
1753 if (BlockWillBeEstranged) {
1754 EstrangedBlocks[FuncletPadBB][OldBlock] = NewBlock;
1755 DEBUG_WITH_TYPE("winehprepare-coloring",
1756 dbgs() << " Saved mapping of estranged block \'"
1757 << NewBlock->getName() << "\' for \'"
1758 << FuncletPadBB->getName() << "\'.\n");
1759 break; // Breaks out of the predecessor loop
1765 // Loop over all of the instructions in this funclet, fixing up operand
1766 // references as we go. This uses VMap to do all the hard work.
1767 for (BasicBlock *BB : BlocksInFunclet)
1768 // Loop over all instructions, fixing each one as we find it...
1769 for (Instruction &I : *BB)
1770 RemapInstruction(&I, VMap,
1771 RF_IgnoreMissingEntries | RF_NoModuleLevelChanges);
1773 // Check to see if SuccBB has PHI nodes. If so, we need to add entries to
1774 // the PHI nodes for NewBB now.
1775 for (auto &BBMapping : Orig2Clone) {
1776 BasicBlock *OldBlock = BBMapping.first;
1777 BasicBlock *NewBlock = BBMapping.second;
1778 for (BasicBlock *SuccBB : successors(NewBlock)) {
1779 for (Instruction &SuccI : *SuccBB) {
1780 auto *SuccPN = dyn_cast<PHINode>(&SuccI);
1784 // Ok, we have a PHI node. Figure out what the incoming value was for
1786 int OldBlockIdx = SuccPN->getBasicBlockIndex(OldBlock);
1787 if (OldBlockIdx == -1)
1789 Value *IV = SuccPN->getIncomingValue(OldBlockIdx);
1791 // Remap the value if necessary.
1792 if (auto *Inst = dyn_cast<Instruction>(IV)) {
1793 ValueToValueMapTy::iterator I = VMap.find(Inst);
1794 if (I != VMap.end())
1798 SuccPN->addIncoming(IV, NewBlock);
1803 for (ValueToValueMapTy::value_type VT : VMap) {
1804 // If there were values defined in BB that are used outside the funclet,
1805 // then we now have to update all uses of the value to use either the
1806 // original value, the cloned value, or some PHI derived value. This can
1807 // require arbitrary PHI insertion, of which we are prepared to do, clean
1809 SmallVector<Use *, 16> UsesToRename;
1811 auto *OldI = dyn_cast<Instruction>(const_cast<Value *>(VT.first));
1814 auto *NewI = cast<Instruction>(VT.second);
1815 // Scan all uses of this instruction to see if it is used outside of its
1816 // funclet, and if so, record them in UsesToRename.
1817 for (Use &U : OldI->uses()) {
1818 Instruction *UserI = cast<Instruction>(U.getUser());
1819 BasicBlock *UserBB = UserI->getParent();
1820 SetVector<BasicBlock *> &ColorsForUserBB = BlockColors[UserBB];
1821 assert(!ColorsForUserBB.empty());
1822 if (ColorsForUserBB.size() > 1 ||
1823 *ColorsForUserBB.begin() != FuncletPadBB)
1824 UsesToRename.push_back(&U);
1827 // If there are no uses outside the block, we're done with this
1829 if (UsesToRename.empty())
1832 // We found a use of OldI outside of the funclet. Rename all uses of OldI
1833 // that are outside its funclet to be uses of the appropriate PHI node
1835 SSAUpdater SSAUpdate;
1836 SSAUpdate.Initialize(OldI->getType(), OldI->getName());
1837 SSAUpdate.AddAvailableValue(OldI->getParent(), OldI);
1838 SSAUpdate.AddAvailableValue(NewI->getParent(), NewI);
1840 while (!UsesToRename.empty())
1841 SSAUpdate.RewriteUseAfterInsertions(*UsesToRename.pop_back_val());
1846 void WinEHPrepare::removeImplausibleTerminators(Function &F) {
1847 // Remove implausible terminators and replace them with UnreachableInst.
1848 for (auto &Funclet : FuncletBlocks) {
1849 BasicBlock *FuncletPadBB = Funclet.first;
1850 std::set<BasicBlock *> &BlocksInFunclet = Funclet.second;
1851 Instruction *FirstNonPHI = FuncletPadBB->getFirstNonPHI();
1852 auto *CatchPad = dyn_cast<CatchPadInst>(FirstNonPHI);
1853 auto *CleanupPad = dyn_cast<CleanupPadInst>(FirstNonPHI);
1855 for (BasicBlock *BB : BlocksInFunclet) {
1856 TerminatorInst *TI = BB->getTerminator();
1857 // CatchPadInst and CleanupPadInst can't transfer control to a ReturnInst.
1858 bool IsUnreachableRet = isa<ReturnInst>(TI) && (CatchPad || CleanupPad);
1859 // The token consumed by a CatchReturnInst must match the funclet token.
1860 bool IsUnreachableCatchret = false;
1861 if (auto *CRI = dyn_cast<CatchReturnInst>(TI))
1862 IsUnreachableCatchret = CRI->getCatchPad() != CatchPad;
1863 // The token consumed by a CleanupReturnInst must match the funclet token.
1864 bool IsUnreachableCleanupret = false;
1865 if (auto *CRI = dyn_cast<CleanupReturnInst>(TI))
1866 IsUnreachableCleanupret = CRI->getCleanupPad() != CleanupPad;
1867 // The token consumed by a CleanupEndPadInst must match the funclet token.
1868 bool IsUnreachableCleanupendpad = false;
1869 if (auto *CEPI = dyn_cast<CleanupEndPadInst>(TI))
1870 IsUnreachableCleanupendpad = CEPI->getCleanupPad() != CleanupPad;
1871 if (IsUnreachableRet || IsUnreachableCatchret ||
1872 IsUnreachableCleanupret || IsUnreachableCleanupendpad) {
1873 // Loop through all of our successors and make sure they know that one
1874 // of their predecessors is going away.
1875 for (BasicBlock *SuccBB : TI->successors())
1876 SuccBB->removePredecessor(BB);
1878 if (IsUnreachableCleanupendpad) {
1879 // We can't simply replace a cleanupendpad with unreachable, because
1880 // its predecessor edges are EH edges and unreachable is not an EH
1881 // pad. Change all predecessors to the "unwind to caller" form.
1882 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1884 BasicBlock *Pred = *PI++;
1885 removeUnwindEdge(Pred);
1889 new UnreachableInst(BB->getContext(), TI);
1890 TI->eraseFromParent();
1892 // FIXME: Check for invokes/cleanuprets/cleanupendpads which unwind to
1893 // implausible catchendpads (i.e. catchendpad not in immediate parent
1899 void WinEHPrepare::cleanupPreparedFunclets(Function &F) {
1900 // Clean-up some of the mess we made by removing useles PHI nodes, trivial
1902 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) {
1903 BasicBlock *BB = &*FI++;
1904 SimplifyInstructionsInBlock(BB);
1905 ConstantFoldTerminator(BB, /*DeleteDeadConditions=*/true);
1906 MergeBlockIntoPredecessor(BB);
1909 // We might have some unreachable blocks after cleaning up some impossible
1911 removeUnreachableBlocks(F);
1914 void WinEHPrepare::verifyPreparedFunclets(Function &F) {
1915 // Recolor the CFG to verify that all is well.
1916 for (BasicBlock &BB : F) {
1917 size_t NumColors = BlockColors[&BB].size();
1918 assert(NumColors == 1 && "Expected monochromatic BB!");
1920 report_fatal_error("Uncolored BB!");
1922 report_fatal_error("Multicolor BB!");
1923 if (!DisableDemotion) {
1924 bool EHPadHasPHI = BB.isEHPad() && isa<PHINode>(BB.begin());
1925 assert(!EHPadHasPHI && "EH Pad still has a PHI!");
1927 report_fatal_error("EH Pad still has a PHI!");
1932 bool WinEHPrepare::prepareExplicitEH(
1933 Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks) {
1934 replaceTerminatePadWithCleanup(F);
1936 // Determine which blocks are reachable from which funclet entries.
1937 colorFunclets(F, EntryBlocks);
1939 if (!DisableDemotion) {
1940 demotePHIsOnFunclets(F);
1942 demoteUsesBetweenFunclets(F);
1944 demoteArgumentUses(F);
1947 cloneCommonBlocks(F, EntryBlocks);
1949 resolveFuncletAncestry(F, EntryBlocks);
1951 if (!DisableCleanups) {
1952 removeImplausibleTerminators(F);
1954 cleanupPreparedFunclets(F);
1957 verifyPreparedFunclets(F);
1959 BlockColors.clear();
1960 FuncletBlocks.clear();
1961 FuncletChildren.clear();
1962 FuncletParents.clear();
1963 EstrangedBlocks.clear();
1964 FuncletCloningRequired = false;
1969 // TODO: Share loads when one use dominates another, or when a catchpad exit
1970 // dominates uses (needs dominators).
1971 AllocaInst *WinEHPrepare::insertPHILoads(PHINode *PN, Function &F) {
1972 BasicBlock *PHIBlock = PN->getParent();
1973 AllocaInst *SpillSlot = nullptr;
1975 if (isa<CleanupPadInst>(PHIBlock->getFirstNonPHI())) {
1976 // Insert a load in place of the PHI and replace all uses.
1977 SpillSlot = new AllocaInst(PN->getType(), nullptr,
1978 Twine(PN->getName(), ".wineh.spillslot"),
1979 &F.getEntryBlock().front());
1980 Value *V = new LoadInst(SpillSlot, Twine(PN->getName(), ".wineh.reload"),
1981 &*PHIBlock->getFirstInsertionPt());
1982 PN->replaceAllUsesWith(V);
1986 DenseMap<BasicBlock *, Value *> Loads;
1987 for (Value::use_iterator UI = PN->use_begin(), UE = PN->use_end();
1990 auto *UsingInst = cast<Instruction>(U.getUser());
1991 BasicBlock *UsingBB = UsingInst->getParent();
1992 if (UsingBB->isEHPad()) {
1993 // Use is on an EH pad phi. Leave it alone; we'll insert loads and
1994 // stores for it separately.
1995 assert(isa<PHINode>(UsingInst));
1998 replaceUseWithLoad(PN, U, SpillSlot, Loads, F);
2003 // TODO: improve store placement. Inserting at def is probably good, but need
2004 // to be careful not to introduce interfering stores (needs liveness analysis).
2005 // TODO: identify related phi nodes that can share spill slots, and share them
2006 // (also needs liveness).
2007 void WinEHPrepare::insertPHIStores(PHINode *OriginalPHI,
2008 AllocaInst *SpillSlot) {
2009 // Use a worklist of (Block, Value) pairs -- the given Value needs to be
2010 // stored to the spill slot by the end of the given Block.
2011 SmallVector<std::pair<BasicBlock *, Value *>, 4> Worklist;
2013 Worklist.push_back({OriginalPHI->getParent(), OriginalPHI});
2015 while (!Worklist.empty()) {
2016 BasicBlock *EHBlock;
2018 std::tie(EHBlock, InVal) = Worklist.pop_back_val();
2020 PHINode *PN = dyn_cast<PHINode>(InVal);
2021 if (PN && PN->getParent() == EHBlock) {
2022 // The value is defined by another PHI we need to remove, with no room to
2023 // insert a store after the PHI, so each predecessor needs to store its
2025 for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i) {
2026 Value *PredVal = PN->getIncomingValue(i);
2028 // Undef can safely be skipped.
2029 if (isa<UndefValue>(PredVal))
2032 insertPHIStore(PN->getIncomingBlock(i), PredVal, SpillSlot, Worklist);
2035 // We need to store InVal, which dominates EHBlock, but can't put a store
2036 // in EHBlock, so need to put stores in each predecessor.
2037 for (BasicBlock *PredBlock : predecessors(EHBlock)) {
2038 insertPHIStore(PredBlock, InVal, SpillSlot, Worklist);
2044 void WinEHPrepare::insertPHIStore(
2045 BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot,
2046 SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist) {
2048 if (PredBlock->isEHPad() &&
2049 !isa<CleanupPadInst>(PredBlock->getFirstNonPHI())) {
2050 // Pred is unsplittable, so we need to queue it on the worklist.
2051 Worklist.push_back({PredBlock, PredVal});
2055 // Otherwise, insert the store at the end of the basic block.
2056 new StoreInst(PredVal, SpillSlot, PredBlock->getTerminator());
2059 // The SetVector == operator uses the std::vector == operator, so it doesn't
2060 // actually tell us whether or not the two sets contain the same colors. This
2061 // function does that.
2062 // FIXME: Would it be better to add a isSetEquivalent() method to SetVector?
2063 static bool isBlockColorSetEquivalent(SetVector<BasicBlock *> &SetA,
2064 SetVector<BasicBlock *> &SetB) {
2065 if (SetA.size() != SetB.size())
2067 for (auto *Color : SetA)
2068 if (!SetB.count(Color))
2073 // TODO: Share loads for same-funclet uses (requires dominators if funclets
2074 // aren't properly nested).
2075 void WinEHPrepare::demoteNonlocalUses(Value *V,
2076 SetVector<BasicBlock *> &ColorsForBB,
2078 // Tokens can only be used non-locally due to control flow involving
2079 // unreachable edges. Don't try to demote the token usage, we'll simply
2080 // delete the cloned user later.
2081 if (isa<CatchPadInst>(V) || isa<CleanupPadInst>(V))
2084 DenseMap<BasicBlock *, Value *> Loads;
2085 AllocaInst *SpillSlot = nullptr;
2086 for (Value::use_iterator UI = V->use_begin(), UE = V->use_end(); UI != UE;) {
2088 auto *UsingInst = cast<Instruction>(U.getUser());
2089 BasicBlock *UsingBB = UsingInst->getParent();
2091 // Is the Use inside a block which is colored the same as the Def?
2092 // If so, we don't need to escape the Def because we will clone
2093 // ourselves our own private copy.
2094 SetVector<BasicBlock *> &ColorsForUsingBB = BlockColors[UsingBB];
2095 if (isBlockColorSetEquivalent(ColorsForUsingBB, ColorsForBB))
2098 replaceUseWithLoad(V, U, SpillSlot, Loads, F);
2101 // Insert stores of the computed value into the stack slot.
2102 // We have to be careful if I is an invoke instruction,
2103 // because we can't insert the store AFTER the terminator instruction.
2104 BasicBlock::iterator InsertPt;
2105 if (isa<Argument>(V)) {
2106 InsertPt = F.getEntryBlock().getTerminator()->getIterator();
2107 } else if (isa<TerminatorInst>(V)) {
2108 auto *II = cast<InvokeInst>(V);
2109 // We cannot demote invoke instructions to the stack if their normal
2110 // edge is critical. Therefore, split the critical edge and create a
2111 // basic block into which the store can be inserted.
2112 if (!II->getNormalDest()->getSinglePredecessor()) {
2114 GetSuccessorNumber(II->getParent(), II->getNormalDest());
2115 assert(isCriticalEdge(II, SuccNum) && "Expected a critical edge!");
2116 BasicBlock *NewBlock = SplitCriticalEdge(II, SuccNum);
2117 assert(NewBlock && "Unable to split critical edge.");
2118 // Update the color mapping for the newly split edge.
2119 SetVector<BasicBlock *> &ColorsForUsingBB = BlockColors[II->getParent()];
2120 BlockColors[NewBlock] = ColorsForUsingBB;
2121 for (BasicBlock *FuncletPad : ColorsForUsingBB)
2122 FuncletBlocks[FuncletPad].insert(NewBlock);
2124 InsertPt = II->getNormalDest()->getFirstInsertionPt();
2126 InsertPt = cast<Instruction>(V)->getIterator();
2128 // Don't insert before PHI nodes or EH pad instrs.
2129 for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt)
2132 new StoreInst(V, SpillSlot, &*InsertPt);
2136 void WinEHPrepare::replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
2137 DenseMap<BasicBlock *, Value *> &Loads,
2139 // Lazilly create the spill slot.
2141 SpillSlot = new AllocaInst(V->getType(), nullptr,
2142 Twine(V->getName(), ".wineh.spillslot"),
2143 &F.getEntryBlock().front());
2145 auto *UsingInst = cast<Instruction>(U.getUser());
2146 if (auto *UsingPHI = dyn_cast<PHINode>(UsingInst)) {
2147 // If this is a PHI node, we can't insert a load of the value before
2148 // the use. Instead insert the load in the predecessor block
2149 // corresponding to the incoming value.
2151 // Note that if there are multiple edges from a basic block to this
2152 // PHI node that we cannot have multiple loads. The problem is that
2153 // the resulting PHI node will have multiple values (from each load)
2154 // coming in from the same block, which is illegal SSA form.
2155 // For this reason, we keep track of and reuse loads we insert.
2156 BasicBlock *IncomingBlock = UsingPHI->getIncomingBlock(U);
2157 if (auto *CatchRet =
2158 dyn_cast<CatchReturnInst>(IncomingBlock->getTerminator())) {
2159 // Putting a load above a catchret and use on the phi would still leave
2160 // a cross-funclet def/use. We need to split the edge, change the
2161 // catchret to target the new block, and put the load there.
2162 BasicBlock *PHIBlock = UsingInst->getParent();
2163 BasicBlock *NewBlock = SplitEdge(IncomingBlock, PHIBlock);
2164 // SplitEdge gives us:
2167 // br label %NewBlock
2169 // catchret label %PHIBlock
2173 // catchret label %NewBlock
2175 // br label %PHIBlock
2176 // So move the terminators to each others' blocks and swap their
2178 BranchInst *Goto = cast<BranchInst>(IncomingBlock->getTerminator());
2179 Goto->removeFromParent();
2180 CatchRet->removeFromParent();
2181 IncomingBlock->getInstList().push_back(CatchRet);
2182 NewBlock->getInstList().push_back(Goto);
2183 Goto->setSuccessor(0, PHIBlock);
2184 CatchRet->setSuccessor(NewBlock);
2185 // Update the color mapping for the newly split edge.
2186 SetVector<BasicBlock *> &ColorsForPHIBlock = BlockColors[PHIBlock];
2187 BlockColors[NewBlock] = ColorsForPHIBlock;
2188 for (BasicBlock *FuncletPad : ColorsForPHIBlock)
2189 FuncletBlocks[FuncletPad].insert(NewBlock);
2190 // Treat the new block as incoming for load insertion.
2191 IncomingBlock = NewBlock;
2193 Value *&Load = Loads[IncomingBlock];
2194 // Insert the load into the predecessor block
2196 Load = new LoadInst(SpillSlot, Twine(V->getName(), ".wineh.reload"),
2197 /*Volatile=*/false, IncomingBlock->getTerminator());
2201 // Reload right before the old use.
2202 auto *Load = new LoadInst(SpillSlot, Twine(V->getName(), ".wineh.reload"),
2203 /*Volatile=*/false, UsingInst);
2208 void WinEHFuncInfo::addIPToStateRange(const BasicBlock *PadBB,
2209 MCSymbol *InvokeBegin,
2210 MCSymbol *InvokeEnd) {
2211 assert(PadBB->isEHPad() && EHPadStateMap.count(PadBB->getFirstNonPHI()) &&
2212 "should get EH pad BB with precomputed state");
2213 InvokeToStateMap[InvokeBegin] =
2214 std::make_pair(EHPadStateMap[PadBB->getFirstNonPHI()], InvokeEnd);