[WinEH] Fix problem with removing an element from a SetVector while iterating.
[oota-llvm.git] / lib / CodeGen / WinEHPrepare.cpp
1 //===-- WinEHPrepare - Prepare exception handling for code generation ---===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass lowers LLVM IR exception handling into something closer to what the
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.
16 //
17 //===----------------------------------------------------------------------===//
18
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"
31
32 using namespace llvm;
33
34 #define DEBUG_TYPE "winehprepare"
35
36 static cl::opt<bool> DisableDemotion(
37     "disable-demotion", cl::Hidden,
38     cl::desc(
39         "Clone multicolor basic blocks but do not demote cross funclet values"),
40     cl::init(false));
41
42 static cl::opt<bool> DisableCleanups(
43     "disable-cleanups", cl::Hidden,
44     cl::desc("Do not remove implausible terminators or other similar cleanups"),
45     cl::init(false));
46
47 namespace {
48   
49 class WinEHPrepare : public FunctionPass {
50 public:
51   static char ID; // Pass identification, replacement for typeid.
52   WinEHPrepare(const TargetMachine *TM = nullptr) : FunctionPass(ID) {}
53
54   bool runOnFunction(Function &Fn) override;
55
56   bool doFinalization(Module &M) override;
57
58   void getAnalysisUsage(AnalysisUsage &AU) const override;
59
60   const char *getPassName() const override {
61     return "Windows exception handling preparation";
62   }
63
64 private:
65   void insertPHIStores(PHINode *OriginalPHI, AllocaInst *SpillSlot);
66   void
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,
73                           Function &F);
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,
85                                     BasicBlock *Parent);
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);
91
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);
100
101   // All fields are reset by runOnFunction.
102   EHPersonality Personality = EHPersonality::Unknown;
103
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;
108
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;
118 };
119
120 } // end anonymous namespace
121
122 char WinEHPrepare::ID = 0;
123 INITIALIZE_TM_PASS(WinEHPrepare, "winehprepare", "Prepare Windows exceptions",
124                    false, false)
125
126 FunctionPass *llvm::createWinEHPass(const TargetMachine *TM) {
127   return new WinEHPrepare(TM);
128 }
129
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())
136       continue;
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);
142   }
143 }
144
145 bool WinEHPrepare::runOnFunction(Function &Fn) {
146   if (!Fn.hasPersonalityFn())
147     return false;
148
149   // Classify the personality to see what kind of preparation we need.
150   Personality = classifyEHPersonality(Fn.getPersonalityFn());
151
152   // Do nothing if this is not a funclet-based personality.
153   if (!isFuncletEHPersonality(Personality))
154     return false;
155
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
158   // not.
159   removeUnreachableBlocks(Fn);
160
161   SmallVector<BasicBlock *, 4> EntryBlocks;
162   findFuncletEntryPoints(Fn, EntryBlocks);
163   return prepareExplicitEH(Fn, EntryBlocks);
164 }
165
166 bool WinEHPrepare::doFinalization(Module &M) { return false; }
167
168 void WinEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const {}
169
170 static int addUnwindMapEntry(WinEHFuncInfo &FuncInfo, int ToState,
171                              const BasicBlock *BB) {
172   CxxUnwindMapEntry UME;
173   UME.ToState = ToState;
174   UME.Cleanup = BB;
175   FuncInfo.CxxUnwindMap.push_back(UME);
176   return FuncInfo.getLastStateNumber();
177 }
178
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) {
188     WinEHHandlerType HT;
189     Constant *TypeInfo = cast<Constant>(CPI->getArgOperand(0));
190     if (TypeInfo->isNullValue())
191       HT.TypeDescriptor = nullptr;
192     else
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;
198     else
199       HT.CatchObj.Alloca = cast<AllocaInst>(CPI->getArgOperand(2));
200     TBME.HandlerArray.push_back(HT);
201   }
202   FuncInfo.TryBlockMap.push_back(TBME);
203 }
204
205 static const CatchPadInst *getSingleCatchPadPredecessor(const BasicBlock *BB) {
206   for (const BasicBlock *PredBlock : predecessors(BB))
207     if (auto *CPI = dyn_cast<CatchPadInst>(PredBlock->getFirstNonPHI()))
208       return CPI;
209   return nullptr;
210 }
211
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.
215 ///
216 /// The following C++ generates the IR after it:
217 ///   try {
218 ///   } catch (A) {
219 ///   } catch (B) {
220 ///   }
221 ///
222 /// IR:
223 ///   %catchpad.A
224 ///     catchpad [i8* A typeinfo]
225 ///         to label %catch.A unwind label %catchpad.B
226 ///   %catchpad.B
227 ///     catchpad [i8* B typeinfo]
228 ///         to label %catch.B unwind label %endcatches
229 ///   %endcatches
230 ///     catchendblock unwind to caller
231 static void
232 findCatchPadsForCatchEndPad(const BasicBlock *CatchEndBB,
233                             SmallVectorImpl<const CatchPadInst *> &Handlers) {
234   const CatchPadInst *CPI = getSingleCatchPadPredecessor(CatchEndBB);
235   while (CPI) {
236     Handlers.push_back(CPI);
237     CPI = getSingleCatchPadPredecessor(CPI->getParent());
238   }
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());
242 }
243
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))
249     return nullptr;
250   if (TI->isEHPad())
251     return BB;
252   return cast<CleanupReturnInst>(TI)->getCleanupPad()->getParent();
253 }
254
255 static void calculateExplicitCXXStateNumbers(WinEHFuncInfo &FuncInfo,
256                                              const BasicBlock &BB,
257                                              int ParentState) {
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))
263     return;
264
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);
275
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
286                  << '\n');
287     DEBUG(dbgs() << "TryHigh[" << FirstTryPad->getName() << "]: " << TryHigh
288                  << '\n');
289     DEBUG(dbgs() << "CatchHigh[" << FirstTryPad->getName() << "]: " << CatchHigh
290                  << '\n');
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))
294       return;
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
304     // any cleanuprets.
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!");
314   } else {
315     llvm_unreachable("unexpected EH Pad!");
316   }
317 }
318
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;
328 }
329
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;
339 }
340
341 static void calculateExplicitSEHStateNumbers(WinEHFuncInfo &FuncInfo,
342                                              const BasicBlock &BB,
343                                              int ParentState) {
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))
349     return;
350
351   if (isa<CatchEndPadInst>(FirstNonPHI)) {
352     // Extract the filter function and the __except basic block and create a
353     // state for them.
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);
366
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);
374
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))
386       return;
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
396     // any cleanuprets.
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!");
408   } else {
409     llvm_unreachable("unexpected EH Pad!");
410   }
411 }
412
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
415 /// the cleanuppad.
416 static bool doesEHPadUnwindToCaller(const Instruction *EHPad) {
417   auto *CPI = dyn_cast<CleanupPadInst>(EHPad);
418   if (!CPI)
419     return EHPad->mayThrow();
420
421   // This cleanup does not return or unwind, so we say it unwinds to caller.
422   if (CPI->use_empty())
423     return true;
424
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();
429 }
430
431 void llvm::calculateSEHStateNumbers(const Function *Fn,
432                                     WinEHFuncInfo &FuncInfo) {
433   // Don't compute state numbers twice.
434   if (!FuncInfo.SEHUnwindMap.empty())
435     return;
436
437   for (const BasicBlock &BB : *Fn) {
438     if (!BB.isEHPad() || !doesEHPadUnwindToCaller(BB.getFirstNonPHI()))
439       continue;
440     calculateExplicitSEHStateNumbers(FuncInfo, BB, -1);
441   }
442 }
443
444 void llvm::calculateWinCXXEHStateNumbers(const Function *Fn,
445                                          WinEHFuncInfo &FuncInfo) {
446   // Return if it's already been done.
447   if (!FuncInfo.EHPadStateMap.empty())
448     return;
449
450   for (const BasicBlock &BB : *Fn) {
451     if (!BB.isEHPad())
452       continue;
453     if (BB.isLandingPad())
454       report_fatal_error("MSVC C++ EH cannot use landingpads");
455     const Instruction *FirstNonPHI = BB.getFirstNonPHI();
456     if (!doesEHPadUnwindToCaller(FirstNonPHI))
457       continue;
458     calculateExplicitCXXStateNumbers(FuncInfo, BB, -1);
459   }
460 }
461
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;
472 }
473
474 void llvm::calculateClrEHStateNumbers(const Function *Fn,
475                                       WinEHFuncInfo &FuncInfo) {
476   // Return if it's already been done.
477   if (!FuncInfo.EHPadStateMap.empty())
478     return;
479
480   SmallVector<std::pair<const Instruction *, int>, 8> Worklist;
481
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) {
485     if (!BB.isEHPad())
486       continue;
487     if (BB.isLandingPad())
488       report_fatal_error("CoreCLR EH cannot use landingpads");
489     const Instruction *FirstNonPHI = BB.getFirstNonPHI();
490     if (!doesEHPadUnwindToCaller(FirstNonPHI))
491       continue;
492     // queue this with sentinel parent state -1 to mean unwind to caller.
493     Worklist.emplace_back(FirstNonPHI, -1);
494   }
495
496   while (!Worklist.empty()) {
497     const Instruction *Pad;
498     int ParentState;
499     std::tie(Pad, ParentState) = Worklist.pop_back_val();
500
501     int PredState;
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))
511         continue;
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);
517       int NewState =
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;
535     } else {
536       llvm_unreachable("Unexpected EH pad");
537     }
538
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);
543     }
544   }
545 }
546
547 void WinEHPrepare::replaceTerminatePadWithCleanup(Function &F) {
548   if (Personality != EHPersonality::MSVC_CXX)
549     return;
550   for (BasicBlock &BB : F) {
551     Instruction *First = BB.getFirstNonPHI();
552     auto *TPI = dyn_cast<TerminatePadInst>(First);
553     if (!TPI)
554       continue;
555
556     if (TPI->getNumArgOperands() != 1)
557       report_fatal_error(
558           "Expected a unary terminatepad for MSVC C++ personalities!");
559
560     auto *TerminateFn = dyn_cast<Function>(TPI->getArgOperand(0));
561     if (!TerminateFn)
562       report_fatal_error("Function operand expected in terminatepad for MSVC "
563                          "C++ personalities!");
564
565     // Insert the cleanuppad instruction.
566     auto *CPI = CleanupPadInst::Create(
567         BB.getContext(), {}, Twine("terminatepad.for.", BB.getName()), &BB);
568
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());
574
575     // Insert a new terminator for the cleanuppad using the same successor as
576     // the terminatepad.
577     CleanupReturnInst::Create(CPI, TPI->getUnwindDest(), &BB);
578
579     // Let's remove the terminatepad now that we've inserted the new
580     // instructions.
581     TPI->eraseFromParent();
582   }
583 }
584
585 static void
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();
591
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.
604
605   DEBUG_WITH_TYPE("winehprepare-coloring", dbgs() << "\nColoring funclets for "
606                                                   << F.getName() << "\n");
607
608   Worklist.push_back({EntryBlock, EntryBlock});
609
610   while (!Worklist.empty()) {
611     BasicBlock *Visiting;
612     BasicBlock *Color;
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
629       // visit them.
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});
640               }
641         }
642       }
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()
652                                  << "\'.\n");
653           Worklist.push_back({NormalSucc, Visiting});
654         }
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()
660                                  << "\'.\n");
661           Worklist.push_back({UnwindSucc, Color});
662         }
663         continue;
664       }
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))
669         Color = Visiting;
670     } else {
671       // Note that this is a member of the given color.
672       FuncletBlocks[Color].insert(Visiting);
673     }
674
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
680       // color.
681       continue;
682     }
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.
689         continue;
690       }
691       if (BlockColors[Succ].insert(Color)) {
692         DEBUG_WITH_TYPE("winehprepare-coloring",
693                         dbgs() << "  Assigned color \'" << Color->getName()
694                                << "\' to block \'" << Succ->getName()
695                                << "\'.\n");
696         Worklist.push_back({Succ, Color});
697       }
698     }
699   }
700 }
701
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();
710   }
711   // The last catch in the chain must unwind to a catchendpad.
712   assert(isa<CatchEndPadInst>(UnwindTerminator));
713   return NextUnwindDest;
714 }
715
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);
724     } else {
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
731       // PHI nodes.
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);
737     }
738   };
739
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
743   // location.
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) {
750       DEBUG_WITH_TYPE(
751           "winehprepare-coloring",
752           dbgs() << "      removing unwind destination of clone block \'"
753                  << CloneBlock->getName() << "\'.\n");
754       updateUnwindTerminator(CloneBlock);
755     }
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)
762       return;
763
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();
771     else
772       OrigParent = OrigParents.front();
773
774     auto *OrigParentCatch =
775         dyn_cast<CatchPadInst>(OrigParent->getFirstNonPHI());
776     if (!OrigParentCatch || getEndPadForCatch(OrigParentCatch) != UnwindDest) {
777       DEBUG_WITH_TYPE(
778           "winehprepare-coloring",
779           dbgs() << "      removing unwind destination of original block \'"
780                  << OrigBlock << "\'.\n");
781       updateUnwindTerminator(OrigBlock);
782     }
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()));
795       DEBUG_WITH_TYPE(
796           "winehprepare-coloring",
797           dbgs() << "      removing unwind destination of original block \'"
798                  << OrigBlock->getName() << "\'.\n");
799       updateUnwindTerminator(OrigBlock);
800     } else {
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()));
805       DEBUG_WITH_TYPE(
806           "winehprepare-coloring",
807           dbgs() << "      removing unwind destination of clone block \'"
808                  << CloneBlock->getName() << "\'.\n");
809       updateUnwindTerminator(CloneBlock);
810     }
811   } else {
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));
818   }
819 }
820
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
862       // removed.
863       for (auto *Parent : OrigParents) {
864         if (Parent != CloneParent) {
865           CEPCloneParent = Parent;
866           break;
867         }
868       }
869       PredCatch = OrigCatch;
870     } else {
871       CEPCloneParent = CloneParent;
872       PredCatch = CloneCatch;
873     }
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,
895                                       CloneParent);
896   }
897 }
898
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.
903 //
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.
907 //
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.
913 //
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.
925 //
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())
936     return;
937
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();
954       else
955         OrigParent = FuncletParents[OrigFunclet].front();
956       for (succ_iterator SI = succ_begin(OrigBlock), SE = succ_end(OrigBlock);
957            SI != SE; ++SI)
958         (*SI)->removePredecessor(OrigBlock);
959       BasicBlock *LostBlock = EstrangedBlocks[OrigParent][CRI->getSuccessor()];
960       auto *OrigCatchRet = cast<CatchReturnInst>(OrigBlock->getTerminator());
961       if (LostBlock) {
962         OrigCatchRet->setSuccessor(LostBlock);
963       } else {
964         OrigCatchRet->eraseFromParent();
965         new UnreachableInst(OrigBlock->getContext(), OrigBlock);
966       }
967     } else {
968       for (succ_iterator SI = succ_begin(CloneBlock), SE = succ_end(CloneBlock);
969            SI != SE; ++SI)
970         (*SI)->removePredecessor(CloneBlock);
971       BasicBlock *LostBlock = EstrangedBlocks[CloneParent][CRI->getSuccessor()];
972       if (LostBlock) {
973         CRI->setSuccessor(LostBlock);
974       } else {
975         CRI->eraseFromParent();
976         new UnreachableInst(CloneBlock->getContext(), CloneBlock);
977       }
978     }
979   } else if (isa<CleanupReturnInst>(CloneTerminator) ||
980              isa<CleanupEndPadInst>(CloneTerminator)) {
981     BasicBlock *UnwindDest = nullptr;
982
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();
989
990     // If the instruction has no local unwind destination, there is nothing
991     // to be done.
992     if (!UnwindDest)
993       return;
994
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);
1001   }
1002 }
1003
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];
1015
1016   DEBUG_WITH_TYPE("winehprepare-coloring",
1017                   dbgs() << "Cloning funclet \'" << FuncletEntry->getName()
1018                          << "\' for parent \'" << Parent->getName() << "\'.\n");
1019
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.
1024     BasicBlock *CBB =
1025         CloneBasicBlock(BB, VMap, Twine(".from.", Parent->getName()));
1026
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());
1030
1031     // Add basic block mapping.
1032     VMap[BB] = CBB;
1033
1034     // Record delta operations that we need to perform to our color mappings.
1035     Orig2Clone[BB] = CBB;
1036   } // end for (BasicBlock *BB : BlocksInFunclet)
1037
1038   BasicBlock *ClonedFunclet = Orig2Clone[FuncletEntry];
1039   assert(ClonedFunclet);
1040
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);
1047
1048     DEBUG_WITH_TYPE("winehprepare-coloring",
1049                     dbgs() << "  Assigning color \'" << ClonedFunclet->getName()
1050                            << "\' to block \'" << NewBlock->getName()
1051                            << "\'.\n");
1052
1053     // Use the VMap to remap the instructions in this cloned block.
1054     for (Instruction &I : *NewBlock)
1055       RemapInstruction(&I, VMap, RF_IgnoreMissingEntries);
1056   }
1057
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;
1064
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);
1071
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);
1077         if (!SuccPN)
1078           break;
1079
1080         // Ok, we have a PHI node.  Figure out what the incoming value was for
1081         // the OldBlock.
1082         int OldBlockIdx = SuccPN->getBasicBlockIndex(OldBlock);
1083         if (OldBlockIdx == -1)
1084           break;
1085         Value *IV = SuccPN->getIncomingValue(OldBlockIdx);
1086
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())
1091             IV = I->second;
1092         }
1093
1094         SuccPN->addIncoming(IV, NewBlock);
1095       }
1096     }
1097   }
1098
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),
1102                 Parents.end());
1103
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);
1109
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);
1121   }
1122
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);
1127     if (!UT)
1128       continue;
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);
1144     }
1145   }
1146
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);
1155                      }));
1156
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);
1161     if (!PN)
1162       break;
1163
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);
1168   }
1169   for (auto *PN : PHIsToErase)
1170     PN->eraseFromParent();
1171
1172   // Replace the original funclet in the parent's children vector with the
1173   // cloned funclet.
1174   for (auto &It : FuncletChildren[Parent]) {
1175     if (It == FuncletEntry) {
1176       It = ClonedFunclet;
1177       break;
1178     }
1179   }
1180
1181   return ClonedFunclet;
1182 }
1183
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())
1191       continue;
1192
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.
1203
1204     // If the child funclet is the unwind destination, replace the terminator
1205     // with an unreachable instruction.
1206     if (UnwindDest == Child)
1207       removeUnwindEdge(BB);
1208   }
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),
1212                  Children.end());
1213 }
1214
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.
1220 //
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
1244   // we just cloned.
1245   for (BasicBlock *ChildFunclet : FuncletChildren[CurFunclet]) {
1246     for (auto *BB : FuncletBlocks[ChildFunclet]) {
1247       TerminatorInst *Terminator = BB->getTerminator();
1248       if (!Terminator->isExceptional())
1249         continue;
1250
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.");
1264           continue;
1265         }
1266       } else {
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();
1271
1272         // If the cleanup unwinds to caller, there is nothing to be done.
1273         if (!UnwindDest)
1274           continue;
1275       }
1276
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))
1282         continue;
1283
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)
1289         continue;
1290
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)
1298           continue;
1299         BasicBlock *MappedFunclet = Mapping.first;
1300         if (FuncletParents[MappedFunclet].size() == 1 &&
1301             FuncletParents[MappedFunclet].front() == CurFunclet) {
1302           TargetSibling = MappedFunclet;
1303         }
1304       }
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.");
1309
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);
1317     }
1318   }
1319   
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);
1327       if (!II)
1328         continue;
1329
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
1339         // cleanup end pad.
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;
1362         } else {
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;
1375           else
1376             II->setUnwindDest(CurCatchEnd);
1377         }
1378       }
1379     }
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());
1390         if (!II)
1391           continue;
1392
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());
1397         if (!CEP)
1398           continue;
1399
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());
1403
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);
1410       }
1411     }
1412   }
1413 }
1414
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)
1420     return;
1421   
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;
1428
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);
1433 }
1434
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) {
1455         IsCyclic = true;
1456         break;
1457       }
1458     }
1459     // If the unwind chain wraps back on itself, break the chain.
1460     if (IsCyclic) {
1461       makeFuncletEdgeUnreachable(CurFunclet, ChildFunclet);
1462       continue;
1463     }
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;
1469     }
1470     FuncletPath.push_back(ChildFunclet);
1471     resolveFuncletAncestryForPath(F, FuncletPath, Funclet2Orig);
1472     FuncletPath.pop_back();
1473   }
1474   // If we didn't clone any children, we can return now.
1475   if (!ClonedAnyChildren)
1476     return;
1477
1478   updateSiblingToSiblingUnwind(CurFunclet, BlockColors, FuncletBlocks,
1479                                FuncletParents, FuncletChildren, Funclet2Orig);
1480 }
1481
1482 void WinEHPrepare::colorFunclets(Function &F,
1483                                  SmallVectorImpl<BasicBlock *> &EntryBlocks) {
1484   ::colorFunclets(F, EntryBlocks, BlockColors, FuncletBlocks);
1485
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);
1508     }
1509     ColorMapItem.clear();
1510     ColorMapItem.insert(FuncletEntry);
1511   }
1512 }
1513
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);
1520
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,
1525                 FuncletBlocks);
1526
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);
1534   }
1535
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
1540     // catchpads.
1541     BasicBlock *FuncletPadBB = Funclet.first;
1542     Instruction *FirstNonPHI = FuncletPadBB->getFirstNonPHI();
1543     auto *CatchPad = dyn_cast<CatchPadInst>(FirstNonPHI);
1544     if (!CatchPad)
1545       continue;
1546
1547     // The users of a catchpad are always catchrets.
1548     for (User *Exit : CatchPad->users()) {
1549       auto *CatchReturn = dyn_cast<CatchReturnInst>(Exit);
1550       if (!CatchReturn)
1551         continue;
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;
1558     }
1559   }
1560 }
1561
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++;
1567     if (!BB->isEHPad())
1568       continue;
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.
1573       if (!PN)
1574         break;
1575
1576       AllocaInst *SpillSlot = insertPHILoads(PN, F);
1577       if (SpillSlot)
1578         insertPHIStores(PN, SpillSlot);
1579
1580       PHINodes.push_back(PN);
1581     }
1582   }
1583
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();
1588   }
1589 }
1590
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())
1601           continue;
1602
1603       demoteNonlocalUses(I, ColorsForBB, F);
1604     }
1605   }
1606 }
1607
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);
1613 }
1614
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];
1622
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)
1635         continue;
1636
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())) {
1644         assert(
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;
1652             break;
1653           }
1654         }
1655         // There must be one catchpad predecessor for a catchendpad.
1656         assert(CatchParent && "No catchpad found for catchendpad.");
1657
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));
1665
1666         // Remove this block from the FuncletBlocks set of any funclet that
1667         // isn't the funclet whose color we just selected.
1668         for (BasicBlock *ContainingFunclet : BlockColors[BB])
1669           if (ContainingFunclet != CorrectColor)
1670             FuncletBlocks[ContainingFunclet].erase(BB);
1671         BlockColors[BB].remove_if([&](BasicBlock *ContainingFunclet) {
1672           return ContainingFunclet != CorrectColor;
1673         });
1674         // This should leave just one color for BB.
1675         assert(BlockColors[BB].size() == 1);
1676         continue;
1677       }
1678
1679       DEBUG_WITH_TYPE("winehprepare-coloring",
1680                       dbgs() << "  Cloning block \'" << BB->getName()
1681                               << "\' for funclet \'" << FuncletPadBB->getName()
1682                               << "\'.\n");
1683
1684       // Create a new basic block and copy instructions into it!
1685       BasicBlock *CBB =
1686           CloneBasicBlock(BB, VMap, Twine(".for.", FuncletPadBB->getName()));
1687       // Insert the clone immediately after the original to ensure determinism
1688       // and to keep the same relative ordering of any funclet's blocks.
1689       CBB->insertInto(&F, BB->getNextNode());
1690
1691       // Add basic block mapping.
1692       VMap[BB] = CBB;
1693
1694       // Record delta operations that we need to perform to our color mappings.
1695       Orig2Clone[BB] = CBB;
1696     }
1697
1698     // If nothing was cloned, we're done cloning in this funclet.
1699     if (Orig2Clone.empty())
1700       continue;
1701
1702     // Update our color mappings to reflect that one block has lost a color and
1703     // another has gained a color.
1704     for (auto &BBMapping : Orig2Clone) {
1705       BasicBlock *OldBlock = BBMapping.first;
1706       BasicBlock *NewBlock = BBMapping.second;
1707
1708       BlocksInFunclet.insert(NewBlock);
1709       BlockColors[NewBlock].insert(FuncletPadBB);
1710
1711       DEBUG_WITH_TYPE("winehprepare-coloring",
1712                       dbgs() << "  Assigned color \'" << FuncletPadBB->getName()
1713                               << "\' to block \'" << NewBlock->getName()
1714                               << "\'.\n");
1715
1716       BlocksInFunclet.erase(OldBlock);
1717       BlockColors[OldBlock].remove(FuncletPadBB);
1718
1719       DEBUG_WITH_TYPE("winehprepare-coloring",
1720                       dbgs() << "  Removed color \'" << FuncletPadBB->getName()
1721                               << "\' from block \'" << OldBlock->getName()
1722                               << "\'.\n");
1723
1724       // If we are cloning a funclet that might share a child funclet with
1725       // another funclet, look to see if the cloned block is reached from a
1726       // catchret instruction.  If so, save this association so we can retrieve
1727       // the possibly orphaned clone when we clone the child funclet.
1728       if (FuncletCloningRequired) {
1729         for (auto *Pred : predecessors(OldBlock)) {
1730           auto *Terminator = Pred->getTerminator();
1731           if (!isa<CatchReturnInst>(Terminator))
1732             continue;
1733           // If this block is reached from a catchret instruction in a funclet
1734           // that has multiple parents, it will have a color for each of those
1735           // parents.  We just removed the color of one of the parents, but
1736           // the cloned block will be unreachable until we clone the child
1737           // funclet that contains the catchret instruction.  In that case we
1738           // need to create a mapping that will let us find the cloned block
1739           // later and associate it with the cloned child funclet.
1740           bool BlockWillBeEstranged = false;
1741           for (auto *Color : BlockColors[Pred]) {
1742             if (FuncletParents[Color].size() > 1) {
1743               BlockWillBeEstranged = true;
1744               break; // Breaks out of the color loop
1745             }
1746           }
1747           if (BlockWillBeEstranged) {
1748             EstrangedBlocks[FuncletPadBB][OldBlock] = NewBlock;
1749             DEBUG_WITH_TYPE("winehprepare-coloring",
1750                             dbgs() << "  Saved mapping of estranged block \'"
1751                                   << NewBlock->getName() << "\' for \'"
1752                                   << FuncletPadBB->getName() << "\'.\n");
1753             break; // Breaks out of the predecessor loop
1754           }
1755         }
1756       }
1757     }
1758
1759     // Loop over all of the instructions in this funclet, fixing up operand
1760     // references as we go.  This uses VMap to do all the hard work.
1761     for (BasicBlock *BB : BlocksInFunclet)
1762       // Loop over all instructions, fixing each one as we find it...
1763       for (Instruction &I : *BB)
1764         RemapInstruction(&I, VMap,
1765                          RF_IgnoreMissingEntries | RF_NoModuleLevelChanges);
1766
1767     // Check to see if SuccBB has PHI nodes. If so, we need to add entries to
1768     // the PHI nodes for NewBB now.
1769     for (auto &BBMapping : Orig2Clone) {
1770       BasicBlock *OldBlock = BBMapping.first;
1771       BasicBlock *NewBlock = BBMapping.second;
1772       for (BasicBlock *SuccBB : successors(NewBlock)) {
1773         for (Instruction &SuccI : *SuccBB) {
1774           auto *SuccPN = dyn_cast<PHINode>(&SuccI);
1775           if (!SuccPN)
1776             break;
1777
1778           // Ok, we have a PHI node.  Figure out what the incoming value was for
1779           // the OldBlock.
1780           int OldBlockIdx = SuccPN->getBasicBlockIndex(OldBlock);
1781           if (OldBlockIdx == -1)
1782             break;
1783           Value *IV = SuccPN->getIncomingValue(OldBlockIdx);
1784
1785           // Remap the value if necessary.
1786           if (auto *Inst = dyn_cast<Instruction>(IV)) {
1787             ValueToValueMapTy::iterator I = VMap.find(Inst);
1788             if (I != VMap.end())
1789               IV = I->second;
1790           }
1791
1792           SuccPN->addIncoming(IV, NewBlock);
1793         }
1794       }
1795     }
1796
1797     for (ValueToValueMapTy::value_type VT : VMap) {
1798       // If there were values defined in BB that are used outside the funclet,
1799       // then we now have to update all uses of the value to use either the
1800       // original value, the cloned value, or some PHI derived value.  This can
1801       // require arbitrary PHI insertion, of which we are prepared to do, clean
1802       // these up now.
1803       SmallVector<Use *, 16> UsesToRename;
1804
1805       auto *OldI = dyn_cast<Instruction>(const_cast<Value *>(VT.first));
1806       if (!OldI)
1807         continue;
1808       auto *NewI = cast<Instruction>(VT.second);
1809       // Scan all uses of this instruction to see if it is used outside of its
1810       // funclet, and if so, record them in UsesToRename.
1811       for (Use &U : OldI->uses()) {
1812         Instruction *UserI = cast<Instruction>(U.getUser());
1813         BasicBlock *UserBB = UserI->getParent();
1814         SetVector<BasicBlock *> &ColorsForUserBB = BlockColors[UserBB];
1815         assert(!ColorsForUserBB.empty());
1816         if (ColorsForUserBB.size() > 1 ||
1817             *ColorsForUserBB.begin() != FuncletPadBB)
1818           UsesToRename.push_back(&U);
1819       }
1820
1821       // If there are no uses outside the block, we're done with this
1822       // instruction.
1823       if (UsesToRename.empty())
1824         continue;
1825
1826       // We found a use of OldI outside of the funclet.  Rename all uses of OldI
1827       // that are outside its funclet to be uses of the appropriate PHI node
1828       // etc.
1829       SSAUpdater SSAUpdate;
1830       SSAUpdate.Initialize(OldI->getType(), OldI->getName());
1831       SSAUpdate.AddAvailableValue(OldI->getParent(), OldI);
1832       SSAUpdate.AddAvailableValue(NewI->getParent(), NewI);
1833
1834       while (!UsesToRename.empty())
1835         SSAUpdate.RewriteUseAfterInsertions(*UsesToRename.pop_back_val());
1836     }
1837   }
1838 }
1839
1840 void WinEHPrepare::removeImplausibleTerminators(Function &F) {
1841   // Remove implausible terminators and replace them with UnreachableInst.
1842   for (auto &Funclet : FuncletBlocks) {
1843     BasicBlock *FuncletPadBB = Funclet.first;
1844     std::set<BasicBlock *> &BlocksInFunclet = Funclet.second;
1845     Instruction *FirstNonPHI = FuncletPadBB->getFirstNonPHI();
1846     auto *CatchPad = dyn_cast<CatchPadInst>(FirstNonPHI);
1847     auto *CleanupPad = dyn_cast<CleanupPadInst>(FirstNonPHI);
1848
1849     for (BasicBlock *BB : BlocksInFunclet) {
1850       TerminatorInst *TI = BB->getTerminator();
1851       // CatchPadInst and CleanupPadInst can't transfer control to a ReturnInst.
1852       bool IsUnreachableRet = isa<ReturnInst>(TI) && (CatchPad || CleanupPad);
1853       // The token consumed by a CatchReturnInst must match the funclet token.
1854       bool IsUnreachableCatchret = false;
1855       if (auto *CRI = dyn_cast<CatchReturnInst>(TI))
1856         IsUnreachableCatchret = CRI->getCatchPad() != CatchPad;
1857       // The token consumed by a CleanupReturnInst must match the funclet token.
1858       bool IsUnreachableCleanupret = false;
1859       if (auto *CRI = dyn_cast<CleanupReturnInst>(TI))
1860         IsUnreachableCleanupret = CRI->getCleanupPad() != CleanupPad;
1861       // The token consumed by a CleanupEndPadInst must match the funclet token.
1862       bool IsUnreachableCleanupendpad = false;
1863       if (auto *CEPI = dyn_cast<CleanupEndPadInst>(TI))
1864         IsUnreachableCleanupendpad = CEPI->getCleanupPad() != CleanupPad;
1865       if (IsUnreachableRet || IsUnreachableCatchret ||
1866           IsUnreachableCleanupret || IsUnreachableCleanupendpad) {
1867         // Loop through all of our successors and make sure they know that one
1868         // of their predecessors is going away.
1869         for (BasicBlock *SuccBB : TI->successors())
1870           SuccBB->removePredecessor(BB);
1871
1872         if (IsUnreachableCleanupendpad) {
1873           // We can't simply replace a cleanupendpad with unreachable, because
1874           // its predecessor edges are EH edges and unreachable is not an EH
1875           // pad.  Change all predecessors to the "unwind to caller" form.
1876           for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1877                PI != PE;) {
1878             BasicBlock *Pred = *PI++;
1879             removeUnwindEdge(Pred);
1880           }
1881         }
1882
1883         new UnreachableInst(BB->getContext(), TI);
1884         TI->eraseFromParent();
1885       }
1886       // FIXME: Check for invokes/cleanuprets/cleanupendpads which unwind to
1887       // implausible catchendpads (i.e. catchendpad not in immediate parent
1888       // funclet).
1889     }
1890   }
1891 }
1892
1893 void WinEHPrepare::cleanupPreparedFunclets(Function &F) {
1894   // Clean-up some of the mess we made by removing useles PHI nodes, trivial
1895   // branches, etc.
1896   for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) {
1897     BasicBlock *BB = &*FI++;
1898     SimplifyInstructionsInBlock(BB);
1899     ConstantFoldTerminator(BB, /*DeleteDeadConditions=*/true);
1900     MergeBlockIntoPredecessor(BB);
1901   }
1902
1903   // We might have some unreachable blocks after cleaning up some impossible
1904   // control flow.
1905   removeUnreachableBlocks(F);
1906 }
1907
1908 void WinEHPrepare::verifyPreparedFunclets(Function &F) {
1909   // Recolor the CFG to verify that all is well.
1910   for (BasicBlock &BB : F) {
1911     size_t NumColors = BlockColors[&BB].size();
1912     assert(NumColors == 1 && "Expected monochromatic BB!");
1913     if (NumColors == 0)
1914       report_fatal_error("Uncolored BB!");
1915     if (NumColors > 1)
1916       report_fatal_error("Multicolor BB!");
1917     if (!DisableDemotion) {
1918       bool EHPadHasPHI = BB.isEHPad() && isa<PHINode>(BB.begin());
1919       assert(!EHPadHasPHI && "EH Pad still has a PHI!");
1920       if (EHPadHasPHI)
1921         report_fatal_error("EH Pad still has a PHI!");
1922     }
1923   }
1924 }
1925
1926 bool WinEHPrepare::prepareExplicitEH(
1927     Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks) {
1928   replaceTerminatePadWithCleanup(F);
1929
1930   // Determine which blocks are reachable from which funclet entries.
1931   colorFunclets(F, EntryBlocks);
1932
1933   if (!DisableDemotion) {
1934     demotePHIsOnFunclets(F);
1935
1936     demoteUsesBetweenFunclets(F);
1937
1938     demoteArgumentUses(F);
1939   }
1940
1941   cloneCommonBlocks(F, EntryBlocks);
1942
1943   resolveFuncletAncestry(F, EntryBlocks);
1944
1945   if (!DisableCleanups) {
1946     removeImplausibleTerminators(F);
1947
1948     cleanupPreparedFunclets(F);
1949   }
1950
1951   verifyPreparedFunclets(F);
1952
1953   BlockColors.clear();
1954   FuncletBlocks.clear();
1955   FuncletChildren.clear();
1956   FuncletParents.clear();
1957   EstrangedBlocks.clear();
1958   FuncletCloningRequired = false;
1959
1960   return true;
1961 }
1962
1963 // TODO: Share loads when one use dominates another, or when a catchpad exit
1964 // dominates uses (needs dominators).
1965 AllocaInst *WinEHPrepare::insertPHILoads(PHINode *PN, Function &F) {
1966   BasicBlock *PHIBlock = PN->getParent();
1967   AllocaInst *SpillSlot = nullptr;
1968
1969   if (isa<CleanupPadInst>(PHIBlock->getFirstNonPHI())) {
1970     // Insert a load in place of the PHI and replace all uses.
1971     SpillSlot = new AllocaInst(PN->getType(), nullptr,
1972                                Twine(PN->getName(), ".wineh.spillslot"),
1973                                &F.getEntryBlock().front());
1974     Value *V = new LoadInst(SpillSlot, Twine(PN->getName(), ".wineh.reload"),
1975                             &*PHIBlock->getFirstInsertionPt());
1976     PN->replaceAllUsesWith(V);
1977     return SpillSlot;
1978   }
1979
1980   DenseMap<BasicBlock *, Value *> Loads;
1981   for (Value::use_iterator UI = PN->use_begin(), UE = PN->use_end();
1982        UI != UE;) {
1983     Use &U = *UI++;
1984     auto *UsingInst = cast<Instruction>(U.getUser());
1985     BasicBlock *UsingBB = UsingInst->getParent();
1986     if (UsingBB->isEHPad()) {
1987       // Use is on an EH pad phi.  Leave it alone; we'll insert loads and
1988       // stores for it separately.
1989       assert(isa<PHINode>(UsingInst));
1990       continue;
1991     }
1992     replaceUseWithLoad(PN, U, SpillSlot, Loads, F);
1993   }
1994   return SpillSlot;
1995 }
1996
1997 // TODO: improve store placement.  Inserting at def is probably good, but need
1998 // to be careful not to introduce interfering stores (needs liveness analysis).
1999 // TODO: identify related phi nodes that can share spill slots, and share them
2000 // (also needs liveness).
2001 void WinEHPrepare::insertPHIStores(PHINode *OriginalPHI,
2002                                    AllocaInst *SpillSlot) {
2003   // Use a worklist of (Block, Value) pairs -- the given Value needs to be
2004   // stored to the spill slot by the end of the given Block.
2005   SmallVector<std::pair<BasicBlock *, Value *>, 4> Worklist;
2006
2007   Worklist.push_back({OriginalPHI->getParent(), OriginalPHI});
2008
2009   while (!Worklist.empty()) {
2010     BasicBlock *EHBlock;
2011     Value *InVal;
2012     std::tie(EHBlock, InVal) = Worklist.pop_back_val();
2013
2014     PHINode *PN = dyn_cast<PHINode>(InVal);
2015     if (PN && PN->getParent() == EHBlock) {
2016       // The value is defined by another PHI we need to remove, with no room to
2017       // insert a store after the PHI, so each predecessor needs to store its
2018       // incoming value.
2019       for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i) {
2020         Value *PredVal = PN->getIncomingValue(i);
2021
2022         // Undef can safely be skipped.
2023         if (isa<UndefValue>(PredVal))
2024           continue;
2025
2026         insertPHIStore(PN->getIncomingBlock(i), PredVal, SpillSlot, Worklist);
2027       }
2028     } else {
2029       // We need to store InVal, which dominates EHBlock, but can't put a store
2030       // in EHBlock, so need to put stores in each predecessor.
2031       for (BasicBlock *PredBlock : predecessors(EHBlock)) {
2032         insertPHIStore(PredBlock, InVal, SpillSlot, Worklist);
2033       }
2034     }
2035   }
2036 }
2037
2038 void WinEHPrepare::insertPHIStore(
2039     BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot,
2040     SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist) {
2041
2042   if (PredBlock->isEHPad() &&
2043       !isa<CleanupPadInst>(PredBlock->getFirstNonPHI())) {
2044     // Pred is unsplittable, so we need to queue it on the worklist.
2045     Worklist.push_back({PredBlock, PredVal});
2046     return;
2047   }
2048
2049   // Otherwise, insert the store at the end of the basic block.
2050   new StoreInst(PredVal, SpillSlot, PredBlock->getTerminator());
2051 }
2052
2053 // The SetVector == operator uses the std::vector == operator, so it doesn't
2054 // actually tell us whether or not the two sets contain the same colors. This
2055 // function does that.
2056 // FIXME: Would it be better to add a isSetEquivalent() method to SetVector?
2057 static bool isBlockColorSetEquivalent(SetVector<BasicBlock *> &SetA,
2058                                       SetVector<BasicBlock *> &SetB) {
2059   if (SetA.size() != SetB.size())
2060     return false;
2061   for (auto *Color : SetA)
2062     if (!SetB.count(Color))
2063       return false;
2064   return true;
2065 }
2066
2067 // TODO: Share loads for same-funclet uses (requires dominators if funclets
2068 // aren't properly nested).
2069 void WinEHPrepare::demoteNonlocalUses(Value *V,
2070                                       SetVector<BasicBlock *> &ColorsForBB,
2071                                       Function &F) {
2072   // Tokens can only be used non-locally due to control flow involving
2073   // unreachable edges.  Don't try to demote the token usage, we'll simply
2074   // delete the cloned user later.
2075   if (isa<CatchPadInst>(V) || isa<CleanupPadInst>(V))
2076     return;
2077
2078   DenseMap<BasicBlock *, Value *> Loads;
2079   AllocaInst *SpillSlot = nullptr;
2080   for (Value::use_iterator UI = V->use_begin(), UE = V->use_end(); UI != UE;) {
2081     Use &U = *UI++;
2082     auto *UsingInst = cast<Instruction>(U.getUser());
2083     BasicBlock *UsingBB = UsingInst->getParent();
2084
2085     // Is the Use inside a block which is colored the same as the Def?
2086     // If so, we don't need to escape the Def because we will clone
2087     // ourselves our own private copy.
2088     SetVector<BasicBlock *> &ColorsForUsingBB = BlockColors[UsingBB];
2089     if (isBlockColorSetEquivalent(ColorsForUsingBB, ColorsForBB))
2090       continue;
2091
2092     replaceUseWithLoad(V, U, SpillSlot, Loads, F);
2093   }
2094   if (SpillSlot) {
2095     // Insert stores of the computed value into the stack slot.
2096     // We have to be careful if I is an invoke instruction,
2097     // because we can't insert the store AFTER the terminator instruction.
2098     BasicBlock::iterator InsertPt;
2099     if (isa<Argument>(V)) {
2100       InsertPt = F.getEntryBlock().getTerminator()->getIterator();
2101     } else if (isa<TerminatorInst>(V)) {
2102       auto *II = cast<InvokeInst>(V);
2103       // We cannot demote invoke instructions to the stack if their normal
2104       // edge is critical. Therefore, split the critical edge and create a
2105       // basic block into which the store can be inserted.
2106       if (!II->getNormalDest()->getSinglePredecessor()) {
2107         unsigned SuccNum =
2108             GetSuccessorNumber(II->getParent(), II->getNormalDest());
2109         assert(isCriticalEdge(II, SuccNum) && "Expected a critical edge!");
2110         BasicBlock *NewBlock = SplitCriticalEdge(II, SuccNum);
2111         assert(NewBlock && "Unable to split critical edge.");
2112         // Update the color mapping for the newly split edge.
2113         SetVector<BasicBlock *> &ColorsForUsingBB = BlockColors[II->getParent()];
2114         BlockColors[NewBlock] = ColorsForUsingBB;
2115         for (BasicBlock *FuncletPad : ColorsForUsingBB)
2116           FuncletBlocks[FuncletPad].insert(NewBlock);
2117       }
2118       InsertPt = II->getNormalDest()->getFirstInsertionPt();
2119     } else {
2120       InsertPt = cast<Instruction>(V)->getIterator();
2121       ++InsertPt;
2122       // Don't insert before PHI nodes or EH pad instrs.
2123       for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt)
2124         ;
2125     }
2126     new StoreInst(V, SpillSlot, &*InsertPt);
2127   }
2128 }
2129
2130 void WinEHPrepare::replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
2131                                       DenseMap<BasicBlock *, Value *> &Loads,
2132                                       Function &F) {
2133   // Lazilly create the spill slot.
2134   if (!SpillSlot)
2135     SpillSlot = new AllocaInst(V->getType(), nullptr,
2136                                Twine(V->getName(), ".wineh.spillslot"),
2137                                &F.getEntryBlock().front());
2138
2139   auto *UsingInst = cast<Instruction>(U.getUser());
2140   if (auto *UsingPHI = dyn_cast<PHINode>(UsingInst)) {
2141     // If this is a PHI node, we can't insert a load of the value before
2142     // the use.  Instead insert the load in the predecessor block
2143     // corresponding to the incoming value.
2144     //
2145     // Note that if there are multiple edges from a basic block to this
2146     // PHI node that we cannot have multiple loads.  The problem is that
2147     // the resulting PHI node will have multiple values (from each load)
2148     // coming in from the same block, which is illegal SSA form.
2149     // For this reason, we keep track of and reuse loads we insert.
2150     BasicBlock *IncomingBlock = UsingPHI->getIncomingBlock(U);
2151     if (auto *CatchRet =
2152             dyn_cast<CatchReturnInst>(IncomingBlock->getTerminator())) {
2153       // Putting a load above a catchret and use on the phi would still leave
2154       // a cross-funclet def/use.  We need to split the edge, change the
2155       // catchret to target the new block, and put the load there.
2156       BasicBlock *PHIBlock = UsingInst->getParent();
2157       BasicBlock *NewBlock = SplitEdge(IncomingBlock, PHIBlock);
2158       // SplitEdge gives us:
2159       //   IncomingBlock:
2160       //     ...
2161       //     br label %NewBlock
2162       //   NewBlock:
2163       //     catchret label %PHIBlock
2164       // But we need:
2165       //   IncomingBlock:
2166       //     ...
2167       //     catchret label %NewBlock
2168       //   NewBlock:
2169       //     br label %PHIBlock
2170       // So move the terminators to each others' blocks and swap their
2171       // successors.
2172       BranchInst *Goto = cast<BranchInst>(IncomingBlock->getTerminator());
2173       Goto->removeFromParent();
2174       CatchRet->removeFromParent();
2175       IncomingBlock->getInstList().push_back(CatchRet);
2176       NewBlock->getInstList().push_back(Goto);
2177       Goto->setSuccessor(0, PHIBlock);
2178       CatchRet->setSuccessor(NewBlock);
2179       // Update the color mapping for the newly split edge.
2180       SetVector<BasicBlock *> &ColorsForPHIBlock = BlockColors[PHIBlock];
2181       BlockColors[NewBlock] = ColorsForPHIBlock;
2182       for (BasicBlock *FuncletPad : ColorsForPHIBlock)
2183         FuncletBlocks[FuncletPad].insert(NewBlock);
2184       // Treat the new block as incoming for load insertion.
2185       IncomingBlock = NewBlock;
2186     }
2187     Value *&Load = Loads[IncomingBlock];
2188     // Insert the load into the predecessor block
2189     if (!Load)
2190       Load = new LoadInst(SpillSlot, Twine(V->getName(), ".wineh.reload"),
2191                           /*Volatile=*/false, IncomingBlock->getTerminator());
2192
2193     U.set(Load);
2194   } else {
2195     // Reload right before the old use.
2196     auto *Load = new LoadInst(SpillSlot, Twine(V->getName(), ".wineh.reload"),
2197                               /*Volatile=*/false, UsingInst);
2198     U.set(Load);
2199   }
2200 }
2201
2202 void WinEHFuncInfo::addIPToStateRange(const BasicBlock *PadBB,
2203                                       MCSymbol *InvokeBegin,
2204                                       MCSymbol *InvokeEnd) {
2205   assert(PadBB->isEHPad() && EHPadStateMap.count(PadBB->getFirstNonPHI()) &&
2206          "should get EH pad BB with precomputed state");
2207   InvokeToStateMap[InvokeBegin] =
2208       std::make_pair(EHPadStateMap[PadBB->getFirstNonPHI()], InvokeEnd);
2209 }