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