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