CodeGen: Remove implicit ilist iterator conversions, NFC
[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/ADT/STLExtras.h"
22 #include "llvm/ADT/SmallSet.h"
23 #include "llvm/ADT/SetVector.h"
24 #include "llvm/ADT/Triple.h"
25 #include "llvm/ADT/TinyPtrVector.h"
26 #include "llvm/Analysis/CFG.h"
27 #include "llvm/Analysis/LibCallSemantics.h"
28 #include "llvm/Analysis/TargetLibraryInfo.h"
29 #include "llvm/CodeGen/WinEHFuncInfo.h"
30 #include "llvm/IR/Dominators.h"
31 #include "llvm/IR/Function.h"
32 #include "llvm/IR/IRBuilder.h"
33 #include "llvm/IR/Instructions.h"
34 #include "llvm/IR/IntrinsicInst.h"
35 #include "llvm/IR/Module.h"
36 #include "llvm/IR/PatternMatch.h"
37 #include "llvm/MC/MCSymbol.h"
38 #include "llvm/Pass.h"
39 #include "llvm/Support/Debug.h"
40 #include "llvm/Support/raw_ostream.h"
41 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
42 #include "llvm/Transforms/Utils/Cloning.h"
43 #include "llvm/Transforms/Utils/Local.h"
44 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
45 #include "llvm/Transforms/Utils/SSAUpdater.h"
46 #include <memory>
47
48 using namespace llvm;
49 using namespace llvm::PatternMatch;
50
51 #define DEBUG_TYPE "winehprepare"
52
53 static cl::opt<bool> DisableDemotion(
54     "disable-demotion", cl::Hidden,
55     cl::desc(
56         "Clone multicolor basic blocks but do not demote cross funclet values"),
57     cl::init(false));
58
59 static cl::opt<bool> DisableCleanups(
60     "disable-cleanups", cl::Hidden,
61     cl::desc("Do not remove implausible terminators or other similar cleanups"),
62     cl::init(false));
63
64 namespace {
65
66 // This map is used to model frame variable usage during outlining, to
67 // construct a structure type to hold the frame variables in a frame
68 // allocation block, and to remap the frame variable allocas (including
69 // spill locations as needed) to GEPs that get the variable from the
70 // frame allocation structure.
71 typedef MapVector<Value *, TinyPtrVector<AllocaInst *>> FrameVarInfoMap;
72
73 // TinyPtrVector cannot hold nullptr, so we need our own sentinel that isn't
74 // quite null.
75 AllocaInst *getCatchObjectSentinel() {
76   return static_cast<AllocaInst *>(nullptr) + 1;
77 }
78
79 typedef SmallSet<BasicBlock *, 4> VisitedBlockSet;
80
81 class LandingPadActions;
82 class LandingPadMap;
83
84 typedef DenseMap<const BasicBlock *, CatchHandler *> CatchHandlerMapTy;
85 typedef DenseMap<const BasicBlock *, CleanupHandler *> CleanupHandlerMapTy;
86
87 class WinEHPrepare : public FunctionPass {
88 public:
89   static char ID; // Pass identification, replacement for typeid.
90   WinEHPrepare(const TargetMachine *TM = nullptr)
91       : FunctionPass(ID) {
92     if (TM)
93       TheTriple = TM->getTargetTriple();
94   }
95
96   bool runOnFunction(Function &Fn) override;
97
98   bool doFinalization(Module &M) override;
99
100   void getAnalysisUsage(AnalysisUsage &AU) const override;
101
102   const char *getPassName() const override {
103     return "Windows exception handling preparation";
104   }
105
106 private:
107   bool prepareExceptionHandlers(Function &F,
108                                 SmallVectorImpl<LandingPadInst *> &LPads);
109   void identifyEHBlocks(Function &F, SmallVectorImpl<LandingPadInst *> &LPads);
110   void promoteLandingPadValues(LandingPadInst *LPad);
111   void demoteValuesLiveAcrossHandlers(Function &F,
112                                       SmallVectorImpl<LandingPadInst *> &LPads);
113   void findSEHEHReturnPoints(Function &F,
114                              SetVector<BasicBlock *> &EHReturnBlocks);
115   void findCXXEHReturnPoints(Function &F,
116                              SetVector<BasicBlock *> &EHReturnBlocks);
117   void getPossibleReturnTargets(Function *ParentF, Function *HandlerF,
118                                 SetVector<BasicBlock*> &Targets);
119   void completeNestedLandingPad(Function *ParentFn,
120                                 LandingPadInst *OutlinedLPad,
121                                 const LandingPadInst *OriginalLPad,
122                                 FrameVarInfoMap &VarInfo);
123   Function *createHandlerFunc(Function *ParentFn, Type *RetTy,
124                               const Twine &Name, Module *M, Value *&ParentFP);
125   bool outlineHandler(ActionHandler *Action, Function *SrcFn,
126                       LandingPadInst *LPad, BasicBlock *StartBB,
127                       FrameVarInfoMap &VarInfo);
128   void addStubInvokeToHandlerIfNeeded(Function *Handler);
129
130   void mapLandingPadBlocks(LandingPadInst *LPad, LandingPadActions &Actions);
131   CatchHandler *findCatchHandler(BasicBlock *BB, BasicBlock *&NextBB,
132                                  VisitedBlockSet &VisitedBlocks);
133   void findCleanupHandlers(LandingPadActions &Actions, BasicBlock *StartBB,
134                            BasicBlock *EndBB);
135
136   void processSEHCatchHandler(CatchHandler *Handler, BasicBlock *StartBB);
137   void insertPHIStores(PHINode *OriginalPHI, AllocaInst *SpillSlot);
138   void
139   insertPHIStore(BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot,
140                  SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist);
141   AllocaInst *insertPHILoads(PHINode *PN, Function &F);
142   void replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
143                           DenseMap<BasicBlock *, Value *> &Loads, Function &F);
144   void demoteNonlocalUses(Value *V, std::set<BasicBlock *> &ColorsForBB,
145                           Function &F);
146   bool prepareExplicitEH(Function &F,
147                          SmallVectorImpl<BasicBlock *> &EntryBlocks);
148   void replaceTerminatePadWithCleanup(Function &F);
149   void colorFunclets(Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks);
150   void demotePHIsOnFunclets(Function &F);
151   void demoteUsesBetweenFunclets(Function &F);
152   void demoteArgumentUses(Function &F);
153   void cloneCommonBlocks(Function &F,
154                          SmallVectorImpl<BasicBlock *> &EntryBlocks);
155   void removeImplausibleTerminators(Function &F);
156   void cleanupPreparedFunclets(Function &F);
157   void verifyPreparedFunclets(Function &F);
158
159   Triple TheTriple;
160
161   // All fields are reset by runOnFunction.
162   DominatorTree *DT = nullptr;
163   const TargetLibraryInfo *LibInfo = nullptr;
164   EHPersonality Personality = EHPersonality::Unknown;
165   CatchHandlerMapTy CatchHandlerMap;
166   CleanupHandlerMapTy CleanupHandlerMap;
167   DenseMap<const LandingPadInst *, LandingPadMap> LPadMaps;
168   SmallPtrSet<BasicBlock *, 4> NormalBlocks;
169   SmallPtrSet<BasicBlock *, 4> EHBlocks;
170   SetVector<BasicBlock *> EHReturnBlocks;
171
172   // This maps landing pad instructions found in outlined handlers to
173   // the landing pad instruction in the parent function from which they
174   // were cloned.  The cloned/nested landing pad is used as the key
175   // because the landing pad may be cloned into multiple handlers.
176   // This map will be used to add the llvm.eh.actions call to the nested
177   // landing pads after all handlers have been outlined.
178   DenseMap<LandingPadInst *, const LandingPadInst *> NestedLPtoOriginalLP;
179
180   // This maps blocks in the parent function which are destinations of
181   // catch handlers to cloned blocks in (other) outlined handlers. This
182   // handles the case where a nested landing pads has a catch handler that
183   // returns to a handler function rather than the parent function.
184   // The original block is used as the key here because there should only
185   // ever be one handler function from which the cloned block is not pruned.
186   // The original block will be pruned from the parent function after all
187   // handlers have been outlined.  This map will be used to adjust the
188   // return instructions of handlers which return to the block that was
189   // outlined into a handler.  This is done after all handlers have been
190   // outlined but before the outlined code is pruned from the parent function.
191   DenseMap<const BasicBlock *, BasicBlock *> LPadTargetBlocks;
192
193   // Map from outlined handler to call to parent local address. Only used for
194   // 32-bit EH.
195   DenseMap<Function *, Value *> HandlerToParentFP;
196
197   AllocaInst *SEHExceptionCodeSlot = nullptr;
198
199   std::map<BasicBlock *, std::set<BasicBlock *>> BlockColors;
200   std::map<BasicBlock *, std::set<BasicBlock *>> FuncletBlocks;
201   std::map<BasicBlock *, std::set<BasicBlock *>> FuncletChildren;
202 };
203
204 class WinEHFrameVariableMaterializer : public ValueMaterializer {
205 public:
206   WinEHFrameVariableMaterializer(Function *OutlinedFn, Value *ParentFP,
207                                  FrameVarInfoMap &FrameVarInfo);
208   ~WinEHFrameVariableMaterializer() override {}
209
210   Value *materializeValueFor(Value *V) override;
211
212   void escapeCatchObject(Value *V);
213
214 private:
215   FrameVarInfoMap &FrameVarInfo;
216   IRBuilder<> Builder;
217 };
218
219 class LandingPadMap {
220 public:
221   LandingPadMap() : OriginLPad(nullptr) {}
222   void mapLandingPad(const LandingPadInst *LPad);
223
224   bool isInitialized() { return OriginLPad != nullptr; }
225
226   bool isOriginLandingPadBlock(const BasicBlock *BB) const;
227   bool isLandingPadSpecificInst(const Instruction *Inst) const;
228
229   void remapEHValues(ValueToValueMapTy &VMap, Value *EHPtrValue,
230                      Value *SelectorValue) const;
231
232 private:
233   const LandingPadInst *OriginLPad;
234   // We will normally only see one of each of these instructions, but
235   // if more than one occurs for some reason we can handle that.
236   TinyPtrVector<const ExtractValueInst *> ExtractedEHPtrs;
237   TinyPtrVector<const ExtractValueInst *> ExtractedSelectors;
238 };
239
240 class WinEHCloningDirectorBase : public CloningDirector {
241 public:
242   WinEHCloningDirectorBase(Function *HandlerFn, Value *ParentFP,
243                            FrameVarInfoMap &VarInfo, LandingPadMap &LPadMap)
244       : Materializer(HandlerFn, ParentFP, VarInfo),
245         SelectorIDType(Type::getInt32Ty(HandlerFn->getContext())),
246         Int8PtrType(Type::getInt8PtrTy(HandlerFn->getContext())),
247         LPadMap(LPadMap), ParentFP(ParentFP) {}
248
249   CloningAction handleInstruction(ValueToValueMapTy &VMap,
250                                   const Instruction *Inst,
251                                   BasicBlock *NewBB) override;
252
253   virtual CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
254                                          const Instruction *Inst,
255                                          BasicBlock *NewBB) = 0;
256   virtual CloningAction handleEndCatch(ValueToValueMapTy &VMap,
257                                        const Instruction *Inst,
258                                        BasicBlock *NewBB) = 0;
259   virtual CloningAction handleTypeIdFor(ValueToValueMapTy &VMap,
260                                         const Instruction *Inst,
261                                         BasicBlock *NewBB) = 0;
262   virtual CloningAction handleIndirectBr(ValueToValueMapTy &VMap,
263                                          const IndirectBrInst *IBr,
264                                          BasicBlock *NewBB) = 0;
265   virtual CloningAction handleInvoke(ValueToValueMapTy &VMap,
266                                      const InvokeInst *Invoke,
267                                      BasicBlock *NewBB) = 0;
268   virtual CloningAction handleResume(ValueToValueMapTy &VMap,
269                                      const ResumeInst *Resume,
270                                      BasicBlock *NewBB) = 0;
271   virtual CloningAction handleCompare(ValueToValueMapTy &VMap,
272                                       const CmpInst *Compare,
273                                       BasicBlock *NewBB) = 0;
274   virtual CloningAction handleLandingPad(ValueToValueMapTy &VMap,
275                                          const LandingPadInst *LPad,
276                                          BasicBlock *NewBB) = 0;
277
278   ValueMaterializer *getValueMaterializer() override { return &Materializer; }
279
280 protected:
281   WinEHFrameVariableMaterializer Materializer;
282   Type *SelectorIDType;
283   Type *Int8PtrType;
284   LandingPadMap &LPadMap;
285
286   /// The value representing the parent frame pointer.
287   Value *ParentFP;
288 };
289
290 class WinEHCatchDirector : public WinEHCloningDirectorBase {
291 public:
292   WinEHCatchDirector(
293       Function *CatchFn, Value *ParentFP, Value *Selector,
294       FrameVarInfoMap &VarInfo, LandingPadMap &LPadMap,
295       DenseMap<LandingPadInst *, const LandingPadInst *> &NestedLPads,
296       DominatorTree *DT, SmallPtrSetImpl<BasicBlock *> &EHBlocks)
297       : WinEHCloningDirectorBase(CatchFn, ParentFP, VarInfo, LPadMap),
298         CurrentSelector(Selector->stripPointerCasts()),
299         ExceptionObjectVar(nullptr), NestedLPtoOriginalLP(NestedLPads),
300         DT(DT), EHBlocks(EHBlocks) {}
301
302   CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
303                                  const Instruction *Inst,
304                                  BasicBlock *NewBB) override;
305   CloningAction handleEndCatch(ValueToValueMapTy &VMap, const Instruction *Inst,
306                                BasicBlock *NewBB) override;
307   CloningAction handleTypeIdFor(ValueToValueMapTy &VMap,
308                                 const Instruction *Inst,
309                                 BasicBlock *NewBB) override;
310   CloningAction handleIndirectBr(ValueToValueMapTy &VMap,
311                                  const IndirectBrInst *IBr,
312                                  BasicBlock *NewBB) override;
313   CloningAction handleInvoke(ValueToValueMapTy &VMap, const InvokeInst *Invoke,
314                              BasicBlock *NewBB) override;
315   CloningAction handleResume(ValueToValueMapTy &VMap, const ResumeInst *Resume,
316                              BasicBlock *NewBB) override;
317   CloningAction handleCompare(ValueToValueMapTy &VMap, const CmpInst *Compare,
318                               BasicBlock *NewBB) override;
319   CloningAction handleLandingPad(ValueToValueMapTy &VMap,
320                                  const LandingPadInst *LPad,
321                                  BasicBlock *NewBB) override;
322
323   Value *getExceptionVar() { return ExceptionObjectVar; }
324   TinyPtrVector<BasicBlock *> &getReturnTargets() { return ReturnTargets; }
325
326 private:
327   Value *CurrentSelector;
328
329   Value *ExceptionObjectVar;
330   TinyPtrVector<BasicBlock *> ReturnTargets;
331
332   // This will be a reference to the field of the same name in the WinEHPrepare
333   // object which instantiates this WinEHCatchDirector object.
334   DenseMap<LandingPadInst *, const LandingPadInst *> &NestedLPtoOriginalLP;
335   DominatorTree *DT;
336   SmallPtrSetImpl<BasicBlock *> &EHBlocks;
337 };
338
339 class WinEHCleanupDirector : public WinEHCloningDirectorBase {
340 public:
341   WinEHCleanupDirector(Function *CleanupFn, Value *ParentFP,
342                        FrameVarInfoMap &VarInfo, LandingPadMap &LPadMap)
343       : WinEHCloningDirectorBase(CleanupFn, ParentFP, VarInfo,
344                                  LPadMap) {}
345
346   CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
347                                  const Instruction *Inst,
348                                  BasicBlock *NewBB) override;
349   CloningAction handleEndCatch(ValueToValueMapTy &VMap, const Instruction *Inst,
350                                BasicBlock *NewBB) override;
351   CloningAction handleTypeIdFor(ValueToValueMapTy &VMap,
352                                 const Instruction *Inst,
353                                 BasicBlock *NewBB) override;
354   CloningAction handleIndirectBr(ValueToValueMapTy &VMap,
355                                  const IndirectBrInst *IBr,
356                                  BasicBlock *NewBB) override;
357   CloningAction handleInvoke(ValueToValueMapTy &VMap, const InvokeInst *Invoke,
358                              BasicBlock *NewBB) override;
359   CloningAction handleResume(ValueToValueMapTy &VMap, const ResumeInst *Resume,
360                              BasicBlock *NewBB) override;
361   CloningAction handleCompare(ValueToValueMapTy &VMap, const CmpInst *Compare,
362                               BasicBlock *NewBB) override;
363   CloningAction handleLandingPad(ValueToValueMapTy &VMap,
364                                  const LandingPadInst *LPad,
365                                  BasicBlock *NewBB) override;
366 };
367
368 class LandingPadActions {
369 public:
370   LandingPadActions() : HasCleanupHandlers(false) {}
371
372   void insertCatchHandler(CatchHandler *Action) { Actions.push_back(Action); }
373   void insertCleanupHandler(CleanupHandler *Action) {
374     Actions.push_back(Action);
375     HasCleanupHandlers = true;
376   }
377
378   bool includesCleanup() const { return HasCleanupHandlers; }
379
380   SmallVectorImpl<ActionHandler *> &actions() { return Actions; }
381   SmallVectorImpl<ActionHandler *>::iterator begin() { return Actions.begin(); }
382   SmallVectorImpl<ActionHandler *>::iterator end() { return Actions.end(); }
383
384 private:
385   // Note that this class does not own the ActionHandler objects in this vector.
386   // The ActionHandlers are owned by the CatchHandlerMap and CleanupHandlerMap
387   // in the WinEHPrepare class.
388   SmallVector<ActionHandler *, 4> Actions;
389   bool HasCleanupHandlers;
390 };
391
392 } // end anonymous namespace
393
394 char WinEHPrepare::ID = 0;
395 INITIALIZE_TM_PASS(WinEHPrepare, "winehprepare", "Prepare Windows exceptions",
396                    false, false)
397
398 FunctionPass *llvm::createWinEHPass(const TargetMachine *TM) {
399   return new WinEHPrepare(TM);
400 }
401
402 static bool
403 findExceptionalConstructs(Function &Fn,
404                           SmallVectorImpl<LandingPadInst *> &LPads,
405                           SmallVectorImpl<ResumeInst *> &Resumes,
406                           SmallVectorImpl<BasicBlock *> &EntryBlocks) {
407   bool ForExplicitEH = false;
408   for (BasicBlock &BB : Fn) {
409     Instruction *First = BB.getFirstNonPHI();
410     if (auto *LP = dyn_cast<LandingPadInst>(First)) {
411       LPads.push_back(LP);
412     } else if (First->isEHPad()) {
413       if (!ForExplicitEH)
414         EntryBlocks.push_back(&Fn.getEntryBlock());
415       if (!isa<CatchEndPadInst>(First) && !isa<CleanupEndPadInst>(First))
416         EntryBlocks.push_back(&BB);
417       ForExplicitEH = true;
418     }
419     if (auto *Resume = dyn_cast<ResumeInst>(BB.getTerminator()))
420       Resumes.push_back(Resume);
421   }
422   return ForExplicitEH;
423 }
424
425 bool WinEHPrepare::runOnFunction(Function &Fn) {
426   if (!Fn.hasPersonalityFn())
427     return false;
428
429   // No need to prepare outlined handlers.
430   if (Fn.hasFnAttribute("wineh-parent"))
431     return false;
432
433   // Classify the personality to see what kind of preparation we need.
434   Personality = classifyEHPersonality(Fn.getPersonalityFn());
435
436   // Do nothing if this is not a funclet-based personality.
437   if (!isFuncletEHPersonality(Personality))
438     return false;
439
440   // Remove unreachable blocks.  It is not valuable to assign them a color and
441   // their existence can trick us into thinking values are alive when they are
442   // not.
443   removeUnreachableBlocks(Fn);
444
445   SmallVector<LandingPadInst *, 4> LPads;
446   SmallVector<ResumeInst *, 4> Resumes;
447   SmallVector<BasicBlock *, 4> EntryBlocks;
448   bool ForExplicitEH =
449       findExceptionalConstructs(Fn, LPads, Resumes, EntryBlocks);
450
451   if (ForExplicitEH)
452     return prepareExplicitEH(Fn, EntryBlocks);
453
454   // No need to prepare functions that lack landing pads.
455   if (LPads.empty())
456     return false;
457
458   DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
459   LibInfo = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
460
461   // If there were any landing pads, prepareExceptionHandlers will make changes.
462   prepareExceptionHandlers(Fn, LPads);
463   return true;
464 }
465
466 bool WinEHPrepare::doFinalization(Module &M) { return false; }
467
468 void WinEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const {
469   AU.addRequired<DominatorTreeWrapperPass>();
470   AU.addRequired<TargetLibraryInfoWrapperPass>();
471 }
472
473 static bool isSelectorDispatch(BasicBlock *BB, BasicBlock *&CatchHandler,
474                                Constant *&Selector, BasicBlock *&NextBB);
475
476 // Finds blocks reachable from the starting set Worklist. Does not follow unwind
477 // edges or blocks listed in StopPoints.
478 static void findReachableBlocks(SmallPtrSetImpl<BasicBlock *> &ReachableBBs,
479                                 SetVector<BasicBlock *> &Worklist,
480                                 const SetVector<BasicBlock *> *StopPoints) {
481   while (!Worklist.empty()) {
482     BasicBlock *BB = Worklist.pop_back_val();
483
484     // Don't cross blocks that we should stop at.
485     if (StopPoints && StopPoints->count(BB))
486       continue;
487
488     if (!ReachableBBs.insert(BB).second)
489       continue; // Already visited.
490
491     // Don't follow unwind edges of invokes.
492     if (auto *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
493       Worklist.insert(II->getNormalDest());
494       continue;
495     }
496
497     // Otherwise, follow all successors.
498     Worklist.insert(succ_begin(BB), succ_end(BB));
499   }
500 }
501
502 // Attempt to find an instruction where a block can be split before
503 // a call to llvm.eh.begincatch and its operands.  If the block
504 // begins with the begincatch call or one of its adjacent operands
505 // the block will not be split.
506 static Instruction *findBeginCatchSplitPoint(BasicBlock *BB,
507                                              IntrinsicInst *II) {
508   // If the begincatch call is already the first instruction in the block,
509   // don't split.
510   Instruction *FirstNonPHI = BB->getFirstNonPHI();
511   if (II == FirstNonPHI)
512     return nullptr;
513
514   // If either operand is in the same basic block as the instruction and
515   // isn't used by another instruction before the begincatch call, include it
516   // in the split block.
517   auto *Op0 = dyn_cast<Instruction>(II->getOperand(0));
518   auto *Op1 = dyn_cast<Instruction>(II->getOperand(1));
519
520   Instruction *I = II->getPrevNode();
521   Instruction *LastI = II;
522
523   while (I == Op0 || I == Op1) {
524     // If the block begins with one of the operands and there are no other
525     // instructions between the operand and the begincatch call, don't split.
526     if (I == FirstNonPHI)
527       return nullptr;
528
529     LastI = I;
530     I = I->getPrevNode();
531   }
532
533   // If there is at least one instruction in the block before the begincatch
534   // call and its operands, split the block at either the begincatch or
535   // its operand.
536   return LastI;
537 }
538
539 /// Find all points where exceptional control rejoins normal control flow via
540 /// llvm.eh.endcatch. Add them to the normal bb reachability worklist.
541 void WinEHPrepare::findCXXEHReturnPoints(
542     Function &F, SetVector<BasicBlock *> &EHReturnBlocks) {
543   for (auto BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI) {
544     BasicBlock *BB = &*BBI;
545     for (Instruction &I : *BB) {
546       if (match(&I, m_Intrinsic<Intrinsic::eh_begincatch>())) {
547         Instruction *SplitPt =
548             findBeginCatchSplitPoint(BB, cast<IntrinsicInst>(&I));
549         if (SplitPt) {
550           // Split the block before the llvm.eh.begincatch call to allow
551           // cleanup and catch code to be distinguished later.
552           // Do not update BBI because we still need to process the
553           // portion of the block that we are splitting off.
554           SplitBlock(BB, SplitPt, DT);
555           break;
556         }
557       }
558       if (match(&I, m_Intrinsic<Intrinsic::eh_endcatch>())) {
559         // Split the block after the call to llvm.eh.endcatch if there is
560         // anything other than an unconditional branch, or if the successor
561         // starts with a phi.
562         auto *Br = dyn_cast<BranchInst>(I.getNextNode());
563         if (!Br || !Br->isUnconditional() ||
564             isa<PHINode>(Br->getSuccessor(0)->begin())) {
565           DEBUG(dbgs() << "splitting block " << BB->getName()
566                        << " with llvm.eh.endcatch\n");
567           BBI = SplitBlock(BB, I.getNextNode(), DT)->getIterator();
568         }
569         // The next BB is normal control flow.
570         EHReturnBlocks.insert(BB->getTerminator()->getSuccessor(0));
571         break;
572       }
573     }
574   }
575 }
576
577 static bool isCatchAllLandingPad(const BasicBlock *BB) {
578   const LandingPadInst *LP = BB->getLandingPadInst();
579   if (!LP)
580     return false;
581   unsigned N = LP->getNumClauses();
582   return (N > 0 && LP->isCatch(N - 1) &&
583           isa<ConstantPointerNull>(LP->getClause(N - 1)));
584 }
585
586 /// Find all points where exceptions control rejoins normal control flow via
587 /// selector dispatch.
588 void WinEHPrepare::findSEHEHReturnPoints(
589     Function &F, SetVector<BasicBlock *> &EHReturnBlocks) {
590   for (auto BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI) {
591     BasicBlock *BB = &*BBI;
592     // If the landingpad is a catch-all, treat the whole lpad as if it is
593     // reachable from normal control flow.
594     // FIXME: This is imprecise. We need a better way of identifying where a
595     // catch-all starts and cleanups stop. As far as LLVM is concerned, there
596     // is no difference.
597     if (isCatchAllLandingPad(BB)) {
598       EHReturnBlocks.insert(BB);
599       continue;
600     }
601
602     BasicBlock *CatchHandler;
603     BasicBlock *NextBB;
604     Constant *Selector;
605     if (isSelectorDispatch(BB, CatchHandler, Selector, NextBB)) {
606       // Split the edge if there are multiple predecessors. This creates a place
607       // where we can insert EH recovery code.
608       if (!CatchHandler->getSinglePredecessor()) {
609         DEBUG(dbgs() << "splitting EH return edge from " << BB->getName()
610                      << " to " << CatchHandler->getName() << '\n');
611         CatchHandler = SplitCriticalEdge(
612             BB, std::find(succ_begin(BB), succ_end(BB), CatchHandler));
613         BBI = CatchHandler->getIterator();
614       }
615       EHReturnBlocks.insert(CatchHandler);
616     }
617   }
618 }
619
620 void WinEHPrepare::identifyEHBlocks(Function &F, 
621                                     SmallVectorImpl<LandingPadInst *> &LPads) {
622   DEBUG(dbgs() << "Demoting values live across exception handlers in function "
623                << F.getName() << '\n');
624
625   // Build a set of all non-exceptional blocks and exceptional blocks.
626   // - Non-exceptional blocks are blocks reachable from the entry block while
627   //   not following invoke unwind edges.
628   // - Exceptional blocks are blocks reachable from landingpads. Analysis does
629   //   not follow llvm.eh.endcatch blocks, which mark a transition from
630   //   exceptional to normal control.
631
632   if (Personality == EHPersonality::MSVC_CXX)
633     findCXXEHReturnPoints(F, EHReturnBlocks);
634   else
635     findSEHEHReturnPoints(F, EHReturnBlocks);
636
637   DEBUG({
638     dbgs() << "identified the following blocks as EH return points:\n";
639     for (BasicBlock *BB : EHReturnBlocks)
640       dbgs() << "  " << BB->getName() << '\n';
641   });
642
643 // Join points should not have phis at this point, unless they are a
644 // landingpad, in which case we will demote their phis later.
645 #ifndef NDEBUG
646   for (BasicBlock *BB : EHReturnBlocks)
647     assert((BB->isLandingPad() || !isa<PHINode>(BB->begin())) &&
648            "non-lpad EH return block has phi");
649 #endif
650
651   // Normal blocks are the blocks reachable from the entry block and all EH
652   // return points.
653   SetVector<BasicBlock *> Worklist;
654   Worklist = EHReturnBlocks;
655   Worklist.insert(&F.getEntryBlock());
656   findReachableBlocks(NormalBlocks, Worklist, nullptr);
657   DEBUG({
658     dbgs() << "marked the following blocks as normal:\n";
659     for (BasicBlock *BB : NormalBlocks)
660       dbgs() << "  " << BB->getName() << '\n';
661   });
662
663   // Exceptional blocks are the blocks reachable from landingpads that don't
664   // cross EH return points.
665   Worklist.clear();
666   for (auto *LPI : LPads)
667     Worklist.insert(LPI->getParent());
668   findReachableBlocks(EHBlocks, Worklist, &EHReturnBlocks);
669   DEBUG({
670     dbgs() << "marked the following blocks as exceptional:\n";
671     for (BasicBlock *BB : EHBlocks)
672       dbgs() << "  " << BB->getName() << '\n';
673   });
674
675 }
676
677 /// Ensure that all values live into and out of exception handlers are stored
678 /// in memory.
679 /// FIXME: This falls down when values are defined in one handler and live into
680 /// another handler. For example, a cleanup defines a value used only by a
681 /// catch handler.
682 void WinEHPrepare::demoteValuesLiveAcrossHandlers(
683     Function &F, SmallVectorImpl<LandingPadInst *> &LPads) {
684   DEBUG(dbgs() << "Demoting values live across exception handlers in function "
685                << F.getName() << '\n');
686
687   // identifyEHBlocks() should have been called before this function.
688   assert(!NormalBlocks.empty());
689
690   // Try to avoid demoting EH pointer and selector values. They get in the way
691   // of our pattern matching.
692   SmallPtrSet<Instruction *, 10> EHVals;
693   for (BasicBlock &BB : F) {
694     LandingPadInst *LP = BB.getLandingPadInst();
695     if (!LP)
696       continue;
697     EHVals.insert(LP);
698     for (User *U : LP->users()) {
699       auto *EI = dyn_cast<ExtractValueInst>(U);
700       if (!EI)
701         continue;
702       EHVals.insert(EI);
703       for (User *U2 : EI->users()) {
704         if (auto *PN = dyn_cast<PHINode>(U2))
705           EHVals.insert(PN);
706       }
707     }
708   }
709
710   SetVector<Argument *> ArgsToDemote;
711   SetVector<Instruction *> InstrsToDemote;
712   for (BasicBlock &BB : F) {
713     bool IsNormalBB = NormalBlocks.count(&BB);
714     bool IsEHBB = EHBlocks.count(&BB);
715     if (!IsNormalBB && !IsEHBB)
716       continue; // Blocks that are neither normal nor EH are unreachable.
717     for (Instruction &I : BB) {
718       for (Value *Op : I.operands()) {
719         // Don't demote static allocas, constants, and labels.
720         if (isa<Constant>(Op) || isa<BasicBlock>(Op) || isa<InlineAsm>(Op))
721           continue;
722         auto *AI = dyn_cast<AllocaInst>(Op);
723         if (AI && AI->isStaticAlloca())
724           continue;
725
726         if (auto *Arg = dyn_cast<Argument>(Op)) {
727           if (IsEHBB) {
728             DEBUG(dbgs() << "Demoting argument " << *Arg
729                          << " used by EH instr: " << I << "\n");
730             ArgsToDemote.insert(Arg);
731           }
732           continue;
733         }
734
735         // Don't demote EH values.
736         auto *OpI = cast<Instruction>(Op);
737         if (EHVals.count(OpI))
738           continue;
739
740         BasicBlock *OpBB = OpI->getParent();
741         // If a value is produced and consumed in the same BB, we don't need to
742         // demote it.
743         if (OpBB == &BB)
744           continue;
745         bool IsOpNormalBB = NormalBlocks.count(OpBB);
746         bool IsOpEHBB = EHBlocks.count(OpBB);
747         if (IsNormalBB != IsOpNormalBB || IsEHBB != IsOpEHBB) {
748           DEBUG({
749             dbgs() << "Demoting instruction live in-out from EH:\n";
750             dbgs() << "Instr: " << *OpI << '\n';
751             dbgs() << "User: " << I << '\n';
752           });
753           InstrsToDemote.insert(OpI);
754         }
755       }
756     }
757   }
758
759   // Demote values live into and out of handlers.
760   // FIXME: This demotion is inefficient. We should insert spills at the point
761   // of definition, insert one reload in each handler that uses the value, and
762   // insert reloads in the BB used to rejoin normal control flow.
763   Instruction *AllocaInsertPt = &*F.getEntryBlock().getFirstInsertionPt();
764   for (Instruction *I : InstrsToDemote)
765     DemoteRegToStack(*I, false, AllocaInsertPt);
766
767   // Demote arguments separately, and only for uses in EH blocks.
768   for (Argument *Arg : ArgsToDemote) {
769     auto *Slot = new AllocaInst(Arg->getType(), nullptr,
770                                 Arg->getName() + ".reg2mem", AllocaInsertPt);
771     SmallVector<User *, 4> Users(Arg->user_begin(), Arg->user_end());
772     for (User *U : Users) {
773       auto *I = dyn_cast<Instruction>(U);
774       if (I && EHBlocks.count(I->getParent())) {
775         auto *Reload = new LoadInst(Slot, Arg->getName() + ".reload", false, I);
776         U->replaceUsesOfWith(Arg, Reload);
777       }
778     }
779     new StoreInst(Arg, Slot, AllocaInsertPt);
780   }
781
782   // Demote landingpad phis, as the landingpad will be removed from the machine
783   // CFG.
784   for (LandingPadInst *LPI : LPads) {
785     BasicBlock *BB = LPI->getParent();
786     while (auto *Phi = dyn_cast<PHINode>(BB->begin()))
787       DemotePHIToStack(Phi, AllocaInsertPt);
788   }
789
790   DEBUG(dbgs() << "Demoted " << InstrsToDemote.size() << " instructions and "
791                << ArgsToDemote.size() << " arguments for WinEHPrepare\n\n");
792 }
793
794 bool WinEHPrepare::prepareExceptionHandlers(
795     Function &F, SmallVectorImpl<LandingPadInst *> &LPads) {
796   // Don't run on functions that are already prepared.
797   for (LandingPadInst *LPad : LPads) {
798     BasicBlock *LPadBB = LPad->getParent();
799     for (Instruction &Inst : *LPadBB)
800       if (match(&Inst, m_Intrinsic<Intrinsic::eh_actions>()))
801         return false;
802   }
803
804   identifyEHBlocks(F, LPads);
805   demoteValuesLiveAcrossHandlers(F, LPads);
806
807   // These containers are used to re-map frame variables that are used in
808   // outlined catch and cleanup handlers.  They will be populated as the
809   // handlers are outlined.
810   FrameVarInfoMap FrameVarInfo;
811
812   bool HandlersOutlined = false;
813
814   Module *M = F.getParent();
815   LLVMContext &Context = M->getContext();
816
817   // Create a new function to receive the handler contents.
818   PointerType *Int8PtrType = Type::getInt8PtrTy(Context);
819   Type *Int32Type = Type::getInt32Ty(Context);
820   Function *ActionIntrin = Intrinsic::getDeclaration(M, Intrinsic::eh_actions);
821
822   if (isAsynchronousEHPersonality(Personality)) {
823     // FIXME: Switch the ehptr type to i32 and then switch this.
824     SEHExceptionCodeSlot =
825         new AllocaInst(Int8PtrType, nullptr, "seh_exception_code",
826                        &*F.getEntryBlock().getFirstInsertionPt());
827   }
828
829   // In order to handle the case where one outlined catch handler returns
830   // to a block within another outlined catch handler that would otherwise
831   // be unreachable, we need to outline the nested landing pad before we
832   // outline the landing pad which encloses it.
833   if (!isAsynchronousEHPersonality(Personality))
834     std::sort(LPads.begin(), LPads.end(),
835               [this](LandingPadInst *const &L, LandingPadInst *const &R) {
836                 return DT->properlyDominates(R->getParent(), L->getParent());
837               });
838
839   // This container stores the llvm.eh.recover and IndirectBr instructions
840   // that make up the body of each landing pad after it has been outlined.
841   // We need to defer the population of the target list for the indirectbr
842   // until all landing pads have been outlined so that we can handle the
843   // case of blocks in the target that are reached only from nested
844   // landing pads.
845   SmallVector<std::pair<CallInst*, IndirectBrInst *>, 4> LPadImpls;
846
847   for (LandingPadInst *LPad : LPads) {
848     // Look for evidence that this landingpad has already been processed.
849     bool LPadHasActionList = false;
850     BasicBlock *LPadBB = LPad->getParent();
851     for (Instruction &Inst : *LPadBB) {
852       if (match(&Inst, m_Intrinsic<Intrinsic::eh_actions>())) {
853         LPadHasActionList = true;
854         break;
855       }
856     }
857
858     // If we've already outlined the handlers for this landingpad,
859     // there's nothing more to do here.
860     if (LPadHasActionList)
861       continue;
862
863     // If either of the values in the aggregate returned by the landing pad is
864     // extracted and stored to memory, promote the stored value to a register.
865     promoteLandingPadValues(LPad);
866
867     LandingPadActions Actions;
868     mapLandingPadBlocks(LPad, Actions);
869
870     HandlersOutlined |= !Actions.actions().empty();
871     for (ActionHandler *Action : Actions) {
872       if (Action->hasBeenProcessed())
873         continue;
874       BasicBlock *StartBB = Action->getStartBlock();
875
876       // SEH doesn't do any outlining for catches. Instead, pass the handler
877       // basic block addr to llvm.eh.actions and list the block as a return
878       // target.
879       if (isAsynchronousEHPersonality(Personality)) {
880         if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
881           processSEHCatchHandler(CatchAction, StartBB);
882           continue;
883         }
884       }
885
886       outlineHandler(Action, &F, LPad, StartBB, FrameVarInfo);
887     }
888
889     // Split the block after the landingpad instruction so that it is just a
890     // call to llvm.eh.actions followed by indirectbr.
891     assert(!isa<PHINode>(LPadBB->begin()) && "lpad phi not removed");
892     SplitBlock(LPadBB, LPad->getNextNode(), DT);
893     // Erase the branch inserted by the split so we can insert indirectbr.
894     LPadBB->getTerminator()->eraseFromParent();
895
896     // Replace all extracted values with undef and ultimately replace the
897     // landingpad with undef.
898     SmallVector<Instruction *, 4> SEHCodeUses;
899     SmallVector<Instruction *, 4> EHUndefs;
900     for (User *U : LPad->users()) {
901       auto *E = dyn_cast<ExtractValueInst>(U);
902       if (!E)
903         continue;
904       assert(E->getNumIndices() == 1 &&
905              "Unexpected operation: extracting both landing pad values");
906       unsigned Idx = *E->idx_begin();
907       assert((Idx == 0 || Idx == 1) && "unexpected index");
908       if (Idx == 0 && isAsynchronousEHPersonality(Personality))
909         SEHCodeUses.push_back(E);
910       else
911         EHUndefs.push_back(E);
912     }
913     for (Instruction *E : EHUndefs) {
914       E->replaceAllUsesWith(UndefValue::get(E->getType()));
915       E->eraseFromParent();
916     }
917     LPad->replaceAllUsesWith(UndefValue::get(LPad->getType()));
918
919     // Rewrite uses of the exception pointer to loads of an alloca.
920     while (!SEHCodeUses.empty()) {
921       Instruction *E = SEHCodeUses.pop_back_val();
922       SmallVector<Use *, 4> Uses;
923       for (Use &U : E->uses())
924         Uses.push_back(&U);
925       for (Use *U : Uses) {
926         auto *I = cast<Instruction>(U->getUser());
927         if (isa<ResumeInst>(I))
928           continue;
929         if (auto *Phi = dyn_cast<PHINode>(I))
930           SEHCodeUses.push_back(Phi);
931         else
932           U->set(new LoadInst(SEHExceptionCodeSlot, "sehcode", false, I));
933       }
934       E->replaceAllUsesWith(UndefValue::get(E->getType()));
935       E->eraseFromParent();
936     }
937
938     // Add a call to describe the actions for this landing pad.
939     std::vector<Value *> ActionArgs;
940     for (ActionHandler *Action : Actions) {
941       // Action codes from docs are: 0 cleanup, 1 catch.
942       if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
943         ActionArgs.push_back(ConstantInt::get(Int32Type, 1));
944         ActionArgs.push_back(CatchAction->getSelector());
945         // Find the frame escape index of the exception object alloca in the
946         // parent.
947         int FrameEscapeIdx = -1;
948         Value *EHObj = const_cast<Value *>(CatchAction->getExceptionVar());
949         if (EHObj && !isa<ConstantPointerNull>(EHObj)) {
950           auto I = FrameVarInfo.find(EHObj);
951           assert(I != FrameVarInfo.end() &&
952                  "failed to map llvm.eh.begincatch var");
953           FrameEscapeIdx = std::distance(FrameVarInfo.begin(), I);
954         }
955         ActionArgs.push_back(ConstantInt::get(Int32Type, FrameEscapeIdx));
956       } else {
957         ActionArgs.push_back(ConstantInt::get(Int32Type, 0));
958       }
959       ActionArgs.push_back(Action->getHandlerBlockOrFunc());
960     }
961     CallInst *Recover =
962         CallInst::Create(ActionIntrin, ActionArgs, "recover", LPadBB);
963
964     SetVector<BasicBlock *> ReturnTargets;
965     for (ActionHandler *Action : Actions) {
966       if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
967         const auto &CatchTargets = CatchAction->getReturnTargets();
968         ReturnTargets.insert(CatchTargets.begin(), CatchTargets.end());
969       }
970     }
971     IndirectBrInst *Branch =
972         IndirectBrInst::Create(Recover, ReturnTargets.size(), LPadBB);
973     for (BasicBlock *Target : ReturnTargets)
974       Branch->addDestination(Target);
975
976     if (!isAsynchronousEHPersonality(Personality)) {
977       // C++ EH must repopulate the targets later to handle the case of
978       // targets that are reached indirectly through nested landing pads.
979       LPadImpls.push_back(std::make_pair(Recover, Branch));
980     }
981
982   } // End for each landingpad
983
984   // If nothing got outlined, there is no more processing to be done.
985   if (!HandlersOutlined)
986     return false;
987
988   // Replace any nested landing pad stubs with the correct action handler.
989   // This must be done before we remove unreachable blocks because it
990   // cleans up references to outlined blocks that will be deleted.
991   for (auto &LPadPair : NestedLPtoOriginalLP)
992     completeNestedLandingPad(&F, LPadPair.first, LPadPair.second, FrameVarInfo);
993   NestedLPtoOriginalLP.clear();
994
995   // Update the indirectbr instructions' target lists if necessary.
996   SetVector<BasicBlock*> CheckedTargets;
997   SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList;
998   for (auto &LPadImplPair : LPadImpls) {
999     IntrinsicInst *Recover = cast<IntrinsicInst>(LPadImplPair.first);
1000     IndirectBrInst *Branch = LPadImplPair.second;
1001
1002     // Get a list of handlers called by 
1003     parseEHActions(Recover, ActionList);
1004
1005     // Add an indirect branch listing possible successors of the catch handlers.
1006     SetVector<BasicBlock *> ReturnTargets;
1007     for (const auto &Action : ActionList) {
1008       if (auto *CA = dyn_cast<CatchHandler>(Action.get())) {
1009         Function *Handler = cast<Function>(CA->getHandlerBlockOrFunc());
1010         getPossibleReturnTargets(&F, Handler, ReturnTargets);
1011       }
1012     }
1013     ActionList.clear();
1014     // Clear any targets we already knew about.
1015     for (unsigned int I = 0, E = Branch->getNumDestinations(); I < E; ++I) {
1016       BasicBlock *KnownTarget = Branch->getDestination(I);
1017       if (ReturnTargets.count(KnownTarget))
1018         ReturnTargets.remove(KnownTarget);
1019     }
1020     for (BasicBlock *Target : ReturnTargets) {
1021       Branch->addDestination(Target);
1022       // The target may be a block that we excepted to get pruned.
1023       // If it is, it may contain a call to llvm.eh.endcatch.
1024       if (CheckedTargets.insert(Target)) {
1025         // Earlier preparations guarantee that all calls to llvm.eh.endcatch
1026         // will be followed by an unconditional branch.
1027         auto *Br = dyn_cast<BranchInst>(Target->getTerminator());
1028         if (Br && Br->isUnconditional() &&
1029             Br != Target->getFirstNonPHIOrDbgOrLifetime()) {
1030           Instruction *Prev = Br->getPrevNode();
1031           if (match(cast<Value>(Prev), m_Intrinsic<Intrinsic::eh_endcatch>()))
1032             Prev->eraseFromParent();
1033         }
1034       }
1035     }
1036   }
1037   LPadImpls.clear();
1038
1039   F.addFnAttr("wineh-parent", F.getName());
1040
1041   // Delete any blocks that were only used by handlers that were outlined above.
1042   removeUnreachableBlocks(F);
1043
1044   BasicBlock *Entry = &F.getEntryBlock();
1045   IRBuilder<> Builder(F.getParent()->getContext());
1046   Builder.SetInsertPoint(Entry, Entry->getFirstInsertionPt());
1047
1048   Function *FrameEscapeFn =
1049       Intrinsic::getDeclaration(M, Intrinsic::localescape);
1050   Function *RecoverFrameFn =
1051       Intrinsic::getDeclaration(M, Intrinsic::localrecover);
1052   SmallVector<Value *, 8> AllocasToEscape;
1053
1054   // Scan the entry block for an existing call to llvm.localescape. We need to
1055   // keep escaping those objects.
1056   for (Instruction &I : F.front()) {
1057     auto *II = dyn_cast<IntrinsicInst>(&I);
1058     if (II && II->getIntrinsicID() == Intrinsic::localescape) {
1059       auto Args = II->arg_operands();
1060       AllocasToEscape.append(Args.begin(), Args.end());
1061       II->eraseFromParent();
1062       break;
1063     }
1064   }
1065
1066   // Finally, replace all of the temporary allocas for frame variables used in
1067   // the outlined handlers with calls to llvm.localrecover.
1068   for (auto &VarInfoEntry : FrameVarInfo) {
1069     Value *ParentVal = VarInfoEntry.first;
1070     TinyPtrVector<AllocaInst *> &Allocas = VarInfoEntry.second;
1071     AllocaInst *ParentAlloca = cast<AllocaInst>(ParentVal);
1072
1073     // FIXME: We should try to sink unescaped allocas from the parent frame into
1074     // the child frame. If the alloca is escaped, we have to use the lifetime
1075     // markers to ensure that the alloca is only live within the child frame.
1076
1077     // Add this alloca to the list of things to escape.
1078     AllocasToEscape.push_back(ParentAlloca);
1079
1080     // Next replace all outlined allocas that are mapped to it.
1081     for (AllocaInst *TempAlloca : Allocas) {
1082       if (TempAlloca == getCatchObjectSentinel())
1083         continue; // Skip catch parameter sentinels.
1084       Function *HandlerFn = TempAlloca->getParent()->getParent();
1085       llvm::Value *FP = HandlerToParentFP[HandlerFn];
1086       assert(FP);
1087
1088       // FIXME: Sink this localrecover into the blocks where it is used.
1089       Builder.SetInsertPoint(TempAlloca);
1090       Builder.SetCurrentDebugLocation(TempAlloca->getDebugLoc());
1091       Value *RecoverArgs[] = {
1092           Builder.CreateBitCast(&F, Int8PtrType, ""), FP,
1093           llvm::ConstantInt::get(Int32Type, AllocasToEscape.size() - 1)};
1094       Instruction *RecoveredAlloca =
1095           Builder.CreateCall(RecoverFrameFn, RecoverArgs);
1096
1097       // Add a pointer bitcast if the alloca wasn't an i8.
1098       if (RecoveredAlloca->getType() != TempAlloca->getType()) {
1099         RecoveredAlloca->setName(Twine(TempAlloca->getName()) + ".i8");
1100         RecoveredAlloca = cast<Instruction>(
1101             Builder.CreateBitCast(RecoveredAlloca, TempAlloca->getType()));
1102       }
1103       TempAlloca->replaceAllUsesWith(RecoveredAlloca);
1104       TempAlloca->removeFromParent();
1105       RecoveredAlloca->takeName(TempAlloca);
1106       delete TempAlloca;
1107     }
1108   } // End for each FrameVarInfo entry.
1109
1110   // Insert 'call void (...)* @llvm.localescape(...)' at the end of the entry
1111   // block.
1112   Builder.SetInsertPoint(&F.getEntryBlock().back());
1113   Builder.CreateCall(FrameEscapeFn, AllocasToEscape);
1114
1115   if (SEHExceptionCodeSlot) {
1116     if (isAllocaPromotable(SEHExceptionCodeSlot)) {
1117       SmallPtrSet<BasicBlock *, 4> UserBlocks;
1118       for (User *U : SEHExceptionCodeSlot->users()) {
1119         if (auto *Inst = dyn_cast<Instruction>(U))
1120           UserBlocks.insert(Inst->getParent());
1121       }
1122       PromoteMemToReg(SEHExceptionCodeSlot, *DT);
1123       // After the promotion, kill off dead instructions.
1124       for (BasicBlock *BB : UserBlocks)
1125         SimplifyInstructionsInBlock(BB, LibInfo);
1126     }
1127   }
1128
1129   // Clean up the handler action maps we created for this function
1130   DeleteContainerSeconds(CatchHandlerMap);
1131   CatchHandlerMap.clear();
1132   DeleteContainerSeconds(CleanupHandlerMap);
1133   CleanupHandlerMap.clear();
1134   HandlerToParentFP.clear();
1135   DT = nullptr;
1136   LibInfo = nullptr;
1137   SEHExceptionCodeSlot = nullptr;
1138   EHBlocks.clear();
1139   NormalBlocks.clear();
1140   EHReturnBlocks.clear();
1141
1142   return HandlersOutlined;
1143 }
1144
1145 void WinEHPrepare::promoteLandingPadValues(LandingPadInst *LPad) {
1146   // If the return values of the landing pad instruction are extracted and
1147   // stored to memory, we want to promote the store locations to reg values.
1148   SmallVector<AllocaInst *, 2> EHAllocas;
1149
1150   // The landingpad instruction returns an aggregate value.  Typically, its
1151   // value will be passed to a pair of extract value instructions and the
1152   // results of those extracts are often passed to store instructions.
1153   // In unoptimized code the stored value will often be loaded and then stored
1154   // again.
1155   for (auto *U : LPad->users()) {
1156     ExtractValueInst *Extract = dyn_cast<ExtractValueInst>(U);
1157     if (!Extract)
1158       continue;
1159
1160     for (auto *EU : Extract->users()) {
1161       if (auto *Store = dyn_cast<StoreInst>(EU)) {
1162         auto *AV = cast<AllocaInst>(Store->getPointerOperand());
1163         EHAllocas.push_back(AV);
1164       }
1165     }
1166   }
1167
1168   // We can't do this without a dominator tree.
1169   assert(DT);
1170
1171   if (!EHAllocas.empty()) {
1172     PromoteMemToReg(EHAllocas, *DT);
1173     EHAllocas.clear();
1174   }
1175
1176   // After promotion, some extracts may be trivially dead. Remove them.
1177   SmallVector<Value *, 4> Users(LPad->user_begin(), LPad->user_end());
1178   for (auto *U : Users)
1179     RecursivelyDeleteTriviallyDeadInstructions(U);
1180 }
1181
1182 void WinEHPrepare::getPossibleReturnTargets(Function *ParentF,
1183                                             Function *HandlerF,
1184                                             SetVector<BasicBlock*> &Targets) {
1185   for (BasicBlock &BB : *HandlerF) {
1186     // If the handler contains landing pads, check for any
1187     // handlers that may return directly to a block in the
1188     // parent function.
1189     if (auto *LPI = BB.getLandingPadInst()) {
1190       IntrinsicInst *Recover = cast<IntrinsicInst>(LPI->getNextNode());
1191       SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList;
1192       parseEHActions(Recover, ActionList);
1193       for (const auto &Action : ActionList) {
1194         if (auto *CH = dyn_cast<CatchHandler>(Action.get())) {
1195           Function *NestedF = cast<Function>(CH->getHandlerBlockOrFunc());
1196           getPossibleReturnTargets(ParentF, NestedF, Targets);
1197         }
1198       }
1199     }
1200
1201     auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator());
1202     if (!Ret)
1203       continue;
1204
1205     // Handler functions must always return a block address.
1206     BlockAddress *BA = cast<BlockAddress>(Ret->getReturnValue());
1207
1208     // If this is the handler for a nested landing pad, the
1209     // return address may have been remapped to a block in the
1210     // parent handler.  We're not interested in those.
1211     if (BA->getFunction() != ParentF)
1212       continue;
1213
1214     Targets.insert(BA->getBasicBlock());
1215   }
1216 }
1217
1218 void WinEHPrepare::completeNestedLandingPad(Function *ParentFn,
1219                                             LandingPadInst *OutlinedLPad,
1220                                             const LandingPadInst *OriginalLPad,
1221                                             FrameVarInfoMap &FrameVarInfo) {
1222   // Get the nested block and erase the unreachable instruction that was
1223   // temporarily inserted as its terminator.
1224   LLVMContext &Context = ParentFn->getContext();
1225   BasicBlock *OutlinedBB = OutlinedLPad->getParent();
1226   // If the nested landing pad was outlined before the landing pad that enclosed
1227   // it, it will already be in outlined form.  In that case, we just need to see
1228   // if the returns and the enclosing branch instruction need to be updated.
1229   IndirectBrInst *Branch =
1230       dyn_cast<IndirectBrInst>(OutlinedBB->getTerminator());
1231   if (!Branch) {
1232     // If the landing pad wasn't in outlined form, it should be a stub with
1233     // an unreachable terminator.
1234     assert(isa<UnreachableInst>(OutlinedBB->getTerminator()));
1235     OutlinedBB->getTerminator()->eraseFromParent();
1236     // That should leave OutlinedLPad as the last instruction in its block.
1237     assert(&OutlinedBB->back() == OutlinedLPad);
1238   }
1239
1240   // The original landing pad will have already had its action intrinsic
1241   // built by the outlining loop.  We need to clone that into the outlined
1242   // location.  It may also be necessary to add references to the exception
1243   // variables to the outlined handler in which this landing pad is nested
1244   // and remap return instructions in the nested handlers that should return
1245   // to an address in the outlined handler.
1246   Function *OutlinedHandlerFn = OutlinedBB->getParent();
1247   BasicBlock::const_iterator II = OriginalLPad->getIterator();
1248   ++II;
1249   // The instruction after the landing pad should now be a call to eh.actions.
1250   const Instruction *Recover = &*II;
1251   const IntrinsicInst *EHActions = cast<IntrinsicInst>(Recover);
1252
1253   // Remap the return target in the nested handler.
1254   SmallVector<BlockAddress *, 4> ActionTargets;
1255   SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList;
1256   parseEHActions(EHActions, ActionList);
1257   for (const auto &Action : ActionList) {
1258     auto *Catch = dyn_cast<CatchHandler>(Action.get());
1259     if (!Catch)
1260       continue;
1261     // The dyn_cast to function here selects C++ catch handlers and skips
1262     // SEH catch handlers.
1263     auto *Handler = dyn_cast<Function>(Catch->getHandlerBlockOrFunc());
1264     if (!Handler)
1265       continue;
1266     // Visit all the return instructions, looking for places that return
1267     // to a location within OutlinedHandlerFn.
1268     for (BasicBlock &NestedHandlerBB : *Handler) {
1269       auto *Ret = dyn_cast<ReturnInst>(NestedHandlerBB.getTerminator());
1270       if (!Ret)
1271         continue;
1272
1273       // Handler functions must always return a block address.
1274       BlockAddress *BA = cast<BlockAddress>(Ret->getReturnValue());
1275       // The original target will have been in the main parent function,
1276       // but if it is the address of a block that has been outlined, it
1277       // should be a block that was outlined into OutlinedHandlerFn.
1278       assert(BA->getFunction() == ParentFn);
1279
1280       // Ignore targets that aren't part of an outlined handler function.
1281       if (!LPadTargetBlocks.count(BA->getBasicBlock()))
1282         continue;
1283
1284       // If the return value is the address ofF a block that we
1285       // previously outlined into the parent handler function, replace
1286       // the return instruction and add the mapped target to the list
1287       // of possible return addresses.
1288       BasicBlock *MappedBB = LPadTargetBlocks[BA->getBasicBlock()];
1289       assert(MappedBB->getParent() == OutlinedHandlerFn);
1290       BlockAddress *NewBA = BlockAddress::get(OutlinedHandlerFn, MappedBB);
1291       Ret->eraseFromParent();
1292       ReturnInst::Create(Context, NewBA, &NestedHandlerBB);
1293       ActionTargets.push_back(NewBA);
1294     }
1295   }
1296   ActionList.clear();
1297
1298   if (Branch) {
1299     // If the landing pad was already in outlined form, just update its targets.
1300     for (unsigned int I = Branch->getNumDestinations(); I > 0; --I)
1301       Branch->removeDestination(I);
1302     // Add the previously collected action targets.
1303     for (auto *Target : ActionTargets)
1304       Branch->addDestination(Target->getBasicBlock());
1305   } else {
1306     // If the landing pad was previously stubbed out, fill in its outlined form.
1307     IntrinsicInst *NewEHActions = cast<IntrinsicInst>(EHActions->clone());
1308     OutlinedBB->getInstList().push_back(NewEHActions);
1309
1310     // Insert an indirect branch into the outlined landing pad BB.
1311     IndirectBrInst *IBr = IndirectBrInst::Create(NewEHActions, 0, OutlinedBB);
1312     // Add the previously collected action targets.
1313     for (auto *Target : ActionTargets)
1314       IBr->addDestination(Target->getBasicBlock());
1315   }
1316 }
1317
1318 // This function examines a block to determine whether the block ends with a
1319 // conditional branch to a catch handler based on a selector comparison.
1320 // This function is used both by the WinEHPrepare::findSelectorComparison() and
1321 // WinEHCleanupDirector::handleTypeIdFor().
1322 static bool isSelectorDispatch(BasicBlock *BB, BasicBlock *&CatchHandler,
1323                                Constant *&Selector, BasicBlock *&NextBB) {
1324   ICmpInst::Predicate Pred;
1325   BasicBlock *TBB, *FBB;
1326   Value *LHS, *RHS;
1327
1328   if (!match(BB->getTerminator(),
1329              m_Br(m_ICmp(Pred, m_Value(LHS), m_Value(RHS)), TBB, FBB)))
1330     return false;
1331
1332   if (!match(LHS,
1333              m_Intrinsic<Intrinsic::eh_typeid_for>(m_Constant(Selector))) &&
1334       !match(RHS, m_Intrinsic<Intrinsic::eh_typeid_for>(m_Constant(Selector))))
1335     return false;
1336
1337   if (Pred == CmpInst::ICMP_EQ) {
1338     CatchHandler = TBB;
1339     NextBB = FBB;
1340     return true;
1341   }
1342
1343   if (Pred == CmpInst::ICMP_NE) {
1344     CatchHandler = FBB;
1345     NextBB = TBB;
1346     return true;
1347   }
1348
1349   return false;
1350 }
1351
1352 static bool isCatchBlock(BasicBlock *BB) {
1353   for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg()->getIterator(),
1354                             IE = BB->end();
1355        II != IE; ++II) {
1356     if (match(cast<Value>(II), m_Intrinsic<Intrinsic::eh_begincatch>()))
1357       return true;
1358   }
1359   return false;
1360 }
1361
1362 static BasicBlock *createStubLandingPad(Function *Handler) {
1363   // FIXME: Finish this!
1364   LLVMContext &Context = Handler->getContext();
1365   BasicBlock *StubBB = BasicBlock::Create(Context, "stub");
1366   Handler->getBasicBlockList().push_back(StubBB);
1367   IRBuilder<> Builder(StubBB);
1368   LandingPadInst *LPad = Builder.CreateLandingPad(
1369       llvm::StructType::get(Type::getInt8PtrTy(Context),
1370                             Type::getInt32Ty(Context), nullptr),
1371       0);
1372   // Insert a call to llvm.eh.actions so that we don't try to outline this lpad.
1373   Function *ActionIntrin =
1374       Intrinsic::getDeclaration(Handler->getParent(), Intrinsic::eh_actions);
1375   Builder.CreateCall(ActionIntrin, {}, "recover");
1376   LPad->setCleanup(true);
1377   Builder.CreateUnreachable();
1378   return StubBB;
1379 }
1380
1381 // Cycles through the blocks in an outlined handler function looking for an
1382 // invoke instruction and inserts an invoke of llvm.donothing with an empty
1383 // landing pad if none is found.  The code that generates the .xdata tables for
1384 // the handler needs at least one landing pad to identify the parent function's
1385 // personality.
1386 void WinEHPrepare::addStubInvokeToHandlerIfNeeded(Function *Handler) {
1387   ReturnInst *Ret = nullptr;
1388   UnreachableInst *Unreached = nullptr;
1389   for (BasicBlock &BB : *Handler) {
1390     TerminatorInst *Terminator = BB.getTerminator();
1391     // If we find an invoke, there is nothing to be done.
1392     auto *II = dyn_cast<InvokeInst>(Terminator);
1393     if (II)
1394       return;
1395     // If we've already recorded a return instruction, keep looking for invokes.
1396     if (!Ret)
1397       Ret = dyn_cast<ReturnInst>(Terminator);
1398     // If we haven't recorded an unreachable instruction, try this terminator.
1399     if (!Unreached)
1400       Unreached = dyn_cast<UnreachableInst>(Terminator);
1401   }
1402
1403   // If we got this far, the handler contains no invokes.  We should have seen
1404   // at least one return or unreachable instruction.  We'll insert an invoke of
1405   // llvm.donothing ahead of that instruction.
1406   assert(Ret || Unreached);
1407   TerminatorInst *Term;
1408   if (Ret)
1409     Term = Ret;
1410   else
1411     Term = Unreached;
1412   BasicBlock *OldRetBB = Term->getParent();
1413   BasicBlock *NewRetBB = SplitBlock(OldRetBB, Term, DT);
1414   // SplitBlock adds an unconditional branch instruction at the end of the
1415   // parent block.  We want to replace that with an invoke call, so we can
1416   // erase it now.
1417   OldRetBB->getTerminator()->eraseFromParent();
1418   BasicBlock *StubLandingPad = createStubLandingPad(Handler);
1419   Function *F =
1420       Intrinsic::getDeclaration(Handler->getParent(), Intrinsic::donothing);
1421   InvokeInst::Create(F, NewRetBB, StubLandingPad, None, "", OldRetBB);
1422 }
1423
1424 // FIXME: Consider sinking this into lib/Target/X86 somehow. TargetLowering
1425 // usually doesn't build LLVM IR, so that's probably the wrong place.
1426 Function *WinEHPrepare::createHandlerFunc(Function *ParentFn, Type *RetTy,
1427                                           const Twine &Name, Module *M,
1428                                           Value *&ParentFP) {
1429   // x64 uses a two-argument prototype where the parent FP is the second
1430   // argument. x86 uses no arguments, just the incoming EBP value.
1431   LLVMContext &Context = M->getContext();
1432   Type *Int8PtrType = Type::getInt8PtrTy(Context);
1433   FunctionType *FnType;
1434   if (TheTriple.getArch() == Triple::x86_64) {
1435     Type *ArgTys[2] = {Int8PtrType, Int8PtrType};
1436     FnType = FunctionType::get(RetTy, ArgTys, false);
1437   } else {
1438     FnType = FunctionType::get(RetTy, None, false);
1439   }
1440
1441   Function *Handler =
1442       Function::Create(FnType, GlobalVariable::InternalLinkage, Name, M);
1443   BasicBlock *Entry = BasicBlock::Create(Context, "entry");
1444   Handler->getBasicBlockList().push_front(Entry);
1445   if (TheTriple.getArch() == Triple::x86_64) {
1446     ParentFP = &(Handler->getArgumentList().back());
1447   } else {
1448     assert(M);
1449     Function *FrameAddressFn =
1450         Intrinsic::getDeclaration(M, Intrinsic::frameaddress);
1451     Function *RecoverFPFn =
1452         Intrinsic::getDeclaration(M, Intrinsic::x86_seh_recoverfp);
1453     IRBuilder<> Builder(&Handler->getEntryBlock());
1454     Value *EBP =
1455         Builder.CreateCall(FrameAddressFn, {Builder.getInt32(1)}, "ebp");
1456     Value *ParentI8Fn = Builder.CreateBitCast(ParentFn, Int8PtrType);
1457     ParentFP = Builder.CreateCall(RecoverFPFn, {ParentI8Fn, EBP});
1458   }
1459   return Handler;
1460 }
1461
1462 bool WinEHPrepare::outlineHandler(ActionHandler *Action, Function *SrcFn,
1463                                   LandingPadInst *LPad, BasicBlock *StartBB,
1464                                   FrameVarInfoMap &VarInfo) {
1465   Module *M = SrcFn->getParent();
1466   LLVMContext &Context = M->getContext();
1467   Type *Int8PtrType = Type::getInt8PtrTy(Context);
1468
1469   // Create a new function to receive the handler contents.
1470   Value *ParentFP;
1471   Function *Handler;
1472   if (Action->getType() == Catch) {
1473     Handler = createHandlerFunc(SrcFn, Int8PtrType, SrcFn->getName() + ".catch", M,
1474                                 ParentFP);
1475   } else {
1476     Handler = createHandlerFunc(SrcFn, Type::getVoidTy(Context),
1477                                 SrcFn->getName() + ".cleanup", M, ParentFP);
1478   }
1479   Handler->setPersonalityFn(SrcFn->getPersonalityFn());
1480   HandlerToParentFP[Handler] = ParentFP;
1481   Handler->addFnAttr("wineh-parent", SrcFn->getName());
1482   BasicBlock *Entry = &Handler->getEntryBlock();
1483
1484   // Generate a standard prolog to setup the frame recovery structure.
1485   IRBuilder<> Builder(Context);
1486   Builder.SetInsertPoint(Entry);
1487   Builder.SetCurrentDebugLocation(LPad->getDebugLoc());
1488
1489   std::unique_ptr<WinEHCloningDirectorBase> Director;
1490
1491   ValueToValueMapTy VMap;
1492
1493   LandingPadMap &LPadMap = LPadMaps[LPad];
1494   if (!LPadMap.isInitialized())
1495     LPadMap.mapLandingPad(LPad);
1496   if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
1497     Constant *Sel = CatchAction->getSelector();
1498     Director.reset(new WinEHCatchDirector(Handler, ParentFP, Sel, VarInfo,
1499                                           LPadMap, NestedLPtoOriginalLP, DT,
1500                                           EHBlocks));
1501     LPadMap.remapEHValues(VMap, UndefValue::get(Int8PtrType),
1502                           ConstantInt::get(Type::getInt32Ty(Context), 1));
1503   } else {
1504     Director.reset(
1505         new WinEHCleanupDirector(Handler, ParentFP, VarInfo, LPadMap));
1506     LPadMap.remapEHValues(VMap, UndefValue::get(Int8PtrType),
1507                           UndefValue::get(Type::getInt32Ty(Context)));
1508   }
1509
1510   SmallVector<ReturnInst *, 8> Returns;
1511   ClonedCodeInfo OutlinedFunctionInfo;
1512
1513   // If the start block contains PHI nodes, we need to map them.
1514   BasicBlock::iterator II = StartBB->begin();
1515   while (auto *PN = dyn_cast<PHINode>(II)) {
1516     bool Mapped = false;
1517     // Look for PHI values that we have already mapped (such as the selector).
1518     for (Value *Val : PN->incoming_values()) {
1519       if (VMap.count(Val)) {
1520         VMap[PN] = VMap[Val];
1521         Mapped = true;
1522       }
1523     }
1524     // If we didn't find a match for this value, map it as an undef.
1525     if (!Mapped) {
1526       VMap[PN] = UndefValue::get(PN->getType());
1527     }
1528     ++II;
1529   }
1530
1531   // The landing pad value may be used by PHI nodes.  It will ultimately be
1532   // eliminated, but we need it in the map for intermediate handling.
1533   VMap[LPad] = UndefValue::get(LPad->getType());
1534
1535   // Skip over PHIs and, if applicable, landingpad instructions.
1536   II = StartBB->getFirstInsertionPt();
1537
1538   CloneAndPruneIntoFromInst(Handler, SrcFn, &*II, VMap,
1539                             /*ModuleLevelChanges=*/false, Returns, "",
1540                             &OutlinedFunctionInfo, Director.get());
1541
1542   // Move all the instructions in the cloned "entry" block into our entry block.
1543   // Depending on how the parent function was laid out, the block that will
1544   // correspond to the outlined entry block may not be the first block in the
1545   // list.  We can recognize it, however, as the cloned block which has no
1546   // predecessors.  Any other block wouldn't have been cloned if it didn't
1547   // have a predecessor which was also cloned.
1548   Function::iterator ClonedIt = std::next(Function::iterator(Entry));
1549   while (!pred_empty(&*ClonedIt))
1550     ++ClonedIt;
1551   assert(ClonedIt != Entry->getParent()->end());
1552   BasicBlock *ClonedEntryBB = &*ClonedIt;
1553   Entry->getInstList().splice(Entry->end(), ClonedEntryBB->getInstList());
1554   ClonedEntryBB->eraseFromParent();
1555
1556   // Make sure we can identify the handler's personality later.
1557   addStubInvokeToHandlerIfNeeded(Handler);
1558
1559   if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
1560     WinEHCatchDirector *CatchDirector =
1561         reinterpret_cast<WinEHCatchDirector *>(Director.get());
1562     CatchAction->setExceptionVar(CatchDirector->getExceptionVar());
1563     CatchAction->setReturnTargets(CatchDirector->getReturnTargets());
1564
1565     // Look for blocks that are not part of the landing pad that we just
1566     // outlined but terminate with a call to llvm.eh.endcatch and a
1567     // branch to a block that is in the handler we just outlined.
1568     // These blocks will be part of a nested landing pad that intends to
1569     // return to an address in this handler.  This case is best handled
1570     // after both landing pads have been outlined, so for now we'll just
1571     // save the association of the blocks in LPadTargetBlocks.  The
1572     // return instructions which are created from these branches will be
1573     // replaced after all landing pads have been outlined.
1574     for (const auto MapEntry : VMap) {
1575       // VMap maps all values and blocks that were just cloned, but dead
1576       // blocks which were pruned will map to nullptr.
1577       if (!isa<BasicBlock>(MapEntry.first) || MapEntry.second == nullptr)
1578         continue;
1579       const BasicBlock *MappedBB = cast<BasicBlock>(MapEntry.first);
1580       for (auto *Pred : predecessors(const_cast<BasicBlock *>(MappedBB))) {
1581         auto *Branch = dyn_cast<BranchInst>(Pred->getTerminator());
1582         if (!Branch || !Branch->isUnconditional() || Pred->size() <= 1)
1583           continue;
1584         BasicBlock::iterator II =
1585             const_cast<BranchInst *>(Branch)->getIterator();
1586         --II;
1587         if (match(cast<Value>(II), m_Intrinsic<Intrinsic::eh_endcatch>())) {
1588           // This would indicate that a nested landing pad wants to return
1589           // to a block that is outlined into two different handlers.
1590           assert(!LPadTargetBlocks.count(MappedBB));
1591           LPadTargetBlocks[MappedBB] = cast<BasicBlock>(MapEntry.second);
1592         }
1593       }
1594     }
1595   } // End if (CatchAction)
1596
1597   Action->setHandlerBlockOrFunc(Handler);
1598
1599   return true;
1600 }
1601
1602 /// This BB must end in a selector dispatch. All we need to do is pass the
1603 /// handler block to llvm.eh.actions and list it as a possible indirectbr
1604 /// target.
1605 void WinEHPrepare::processSEHCatchHandler(CatchHandler *CatchAction,
1606                                           BasicBlock *StartBB) {
1607   BasicBlock *HandlerBB;
1608   BasicBlock *NextBB;
1609   Constant *Selector;
1610   bool Res = isSelectorDispatch(StartBB, HandlerBB, Selector, NextBB);
1611   if (Res) {
1612     // If this was EH dispatch, this must be a conditional branch to the handler
1613     // block.
1614     // FIXME: Handle instructions in the dispatch block. Currently we drop them,
1615     // leading to crashes if some optimization hoists stuff here.
1616     assert(CatchAction->getSelector() && HandlerBB &&
1617            "expected catch EH dispatch");
1618   } else {
1619     // This must be a catch-all. Split the block after the landingpad.
1620     assert(CatchAction->getSelector()->isNullValue() && "expected catch-all");
1621     HandlerBB = SplitBlock(StartBB, &*StartBB->getFirstInsertionPt(), DT);
1622   }
1623   IRBuilder<> Builder(&*HandlerBB->getFirstInsertionPt());
1624   Function *EHCodeFn = Intrinsic::getDeclaration(
1625       StartBB->getParent()->getParent(), Intrinsic::eh_exceptioncode_old);
1626   Value *Code = Builder.CreateCall(EHCodeFn, {}, "sehcode");
1627   Code = Builder.CreateIntToPtr(Code, SEHExceptionCodeSlot->getAllocatedType());
1628   Builder.CreateStore(Code, SEHExceptionCodeSlot);
1629   CatchAction->setHandlerBlockOrFunc(BlockAddress::get(HandlerBB));
1630   TinyPtrVector<BasicBlock *> Targets(HandlerBB);
1631   CatchAction->setReturnTargets(Targets);
1632 }
1633
1634 void LandingPadMap::mapLandingPad(const LandingPadInst *LPad) {
1635   // Each instance of this class should only ever be used to map a single
1636   // landing pad.
1637   assert(OriginLPad == nullptr || OriginLPad == LPad);
1638
1639   // If the landing pad has already been mapped, there's nothing more to do.
1640   if (OriginLPad == LPad)
1641     return;
1642
1643   OriginLPad = LPad;
1644
1645   // The landingpad instruction returns an aggregate value.  Typically, its
1646   // value will be passed to a pair of extract value instructions and the
1647   // results of those extracts will have been promoted to reg values before
1648   // this routine is called.
1649   for (auto *U : LPad->users()) {
1650     const ExtractValueInst *Extract = dyn_cast<ExtractValueInst>(U);
1651     if (!Extract)
1652       continue;
1653     assert(Extract->getNumIndices() == 1 &&
1654            "Unexpected operation: extracting both landing pad values");
1655     unsigned int Idx = *(Extract->idx_begin());
1656     assert((Idx == 0 || Idx == 1) &&
1657            "Unexpected operation: extracting an unknown landing pad element");
1658     if (Idx == 0) {
1659       ExtractedEHPtrs.push_back(Extract);
1660     } else if (Idx == 1) {
1661       ExtractedSelectors.push_back(Extract);
1662     }
1663   }
1664 }
1665
1666 bool LandingPadMap::isOriginLandingPadBlock(const BasicBlock *BB) const {
1667   return BB->getLandingPadInst() == OriginLPad;
1668 }
1669
1670 bool LandingPadMap::isLandingPadSpecificInst(const Instruction *Inst) const {
1671   if (Inst == OriginLPad)
1672     return true;
1673   for (auto *Extract : ExtractedEHPtrs) {
1674     if (Inst == Extract)
1675       return true;
1676   }
1677   for (auto *Extract : ExtractedSelectors) {
1678     if (Inst == Extract)
1679       return true;
1680   }
1681   return false;
1682 }
1683
1684 void LandingPadMap::remapEHValues(ValueToValueMapTy &VMap, Value *EHPtrValue,
1685                                   Value *SelectorValue) const {
1686   // Remap all landing pad extract instructions to the specified values.
1687   for (auto *Extract : ExtractedEHPtrs)
1688     VMap[Extract] = EHPtrValue;
1689   for (auto *Extract : ExtractedSelectors)
1690     VMap[Extract] = SelectorValue;
1691 }
1692
1693 static bool isLocalAddressCall(const Value *V) {
1694   return match(const_cast<Value *>(V), m_Intrinsic<Intrinsic::localaddress>());
1695 }
1696
1697 CloningDirector::CloningAction WinEHCloningDirectorBase::handleInstruction(
1698     ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1699   // If this is one of the boilerplate landing pad instructions, skip it.
1700   // The instruction will have already been remapped in VMap.
1701   if (LPadMap.isLandingPadSpecificInst(Inst))
1702     return CloningDirector::SkipInstruction;
1703
1704   // Nested landing pads that have not already been outlined will be cloned as
1705   // stubs, with just the landingpad instruction and an unreachable instruction.
1706   // When all landingpads have been outlined, we'll replace this with the
1707   // llvm.eh.actions call and indirect branch created when the landing pad was
1708   // outlined.
1709   if (auto *LPad = dyn_cast<LandingPadInst>(Inst)) {
1710     return handleLandingPad(VMap, LPad, NewBB);
1711   }
1712
1713   // Nested landing pads that have already been outlined will be cloned in their
1714   // outlined form, but we need to intercept the ibr instruction to filter out
1715   // targets that do not return to the handler we are outlining.
1716   if (auto *IBr = dyn_cast<IndirectBrInst>(Inst)) {
1717     return handleIndirectBr(VMap, IBr, NewBB);
1718   }
1719
1720   if (auto *Invoke = dyn_cast<InvokeInst>(Inst))
1721     return handleInvoke(VMap, Invoke, NewBB);
1722
1723   if (auto *Resume = dyn_cast<ResumeInst>(Inst))
1724     return handleResume(VMap, Resume, NewBB);
1725
1726   if (auto *Cmp = dyn_cast<CmpInst>(Inst))
1727     return handleCompare(VMap, Cmp, NewBB);
1728
1729   if (match(Inst, m_Intrinsic<Intrinsic::eh_begincatch>()))
1730     return handleBeginCatch(VMap, Inst, NewBB);
1731   if (match(Inst, m_Intrinsic<Intrinsic::eh_endcatch>()))
1732     return handleEndCatch(VMap, Inst, NewBB);
1733   if (match(Inst, m_Intrinsic<Intrinsic::eh_typeid_for>()))
1734     return handleTypeIdFor(VMap, Inst, NewBB);
1735
1736   // When outlining llvm.localaddress(), remap that to the second argument,
1737   // which is the FP of the parent.
1738   if (isLocalAddressCall(Inst)) {
1739     VMap[Inst] = ParentFP;
1740     return CloningDirector::SkipInstruction;
1741   }
1742
1743   // Continue with the default cloning behavior.
1744   return CloningDirector::CloneInstruction;
1745 }
1746
1747 CloningDirector::CloningAction WinEHCatchDirector::handleLandingPad(
1748     ValueToValueMapTy &VMap, const LandingPadInst *LPad, BasicBlock *NewBB) {
1749   // If the instruction after the landing pad is a call to llvm.eh.actions
1750   // the landing pad has already been outlined.  In this case, we should
1751   // clone it because it may return to a block in the handler we are
1752   // outlining now that would otherwise be unreachable.  The landing pads
1753   // are sorted before outlining begins to enable this case to work
1754   // properly.
1755   const Instruction *NextI = LPad->getNextNode();
1756   if (match(NextI, m_Intrinsic<Intrinsic::eh_actions>()))
1757     return CloningDirector::CloneInstruction;
1758
1759   // If the landing pad hasn't been outlined yet, the landing pad we are
1760   // outlining now does not dominate it and so it cannot return to a block
1761   // in this handler.  In that case, we can just insert a stub landing
1762   // pad now and patch it up later.
1763   Instruction *NewInst = LPad->clone();
1764   if (LPad->hasName())
1765     NewInst->setName(LPad->getName());
1766   // Save this correlation for later processing.
1767   NestedLPtoOriginalLP[cast<LandingPadInst>(NewInst)] = LPad;
1768   VMap[LPad] = NewInst;
1769   BasicBlock::InstListType &InstList = NewBB->getInstList();
1770   InstList.push_back(NewInst);
1771   InstList.push_back(new UnreachableInst(NewBB->getContext()));
1772   return CloningDirector::StopCloningBB;
1773 }
1774
1775 CloningDirector::CloningAction WinEHCatchDirector::handleBeginCatch(
1776     ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1777   // The argument to the call is some form of the first element of the
1778   // landingpad aggregate value, but that doesn't matter.  It isn't used
1779   // here.
1780   // The second argument is an outparameter where the exception object will be
1781   // stored. Typically the exception object is a scalar, but it can be an
1782   // aggregate when catching by value.
1783   // FIXME: Leave something behind to indicate where the exception object lives
1784   // for this handler. Should it be part of llvm.eh.actions?
1785   assert(ExceptionObjectVar == nullptr && "Multiple calls to "
1786                                           "llvm.eh.begincatch found while "
1787                                           "outlining catch handler.");
1788   ExceptionObjectVar = Inst->getOperand(1)->stripPointerCasts();
1789   if (isa<ConstantPointerNull>(ExceptionObjectVar))
1790     return CloningDirector::SkipInstruction;
1791   assert(cast<AllocaInst>(ExceptionObjectVar)->isStaticAlloca() &&
1792          "catch parameter is not static alloca");
1793   Materializer.escapeCatchObject(ExceptionObjectVar);
1794   return CloningDirector::SkipInstruction;
1795 }
1796
1797 CloningDirector::CloningAction
1798 WinEHCatchDirector::handleEndCatch(ValueToValueMapTy &VMap,
1799                                    const Instruction *Inst, BasicBlock *NewBB) {
1800   auto *IntrinCall = dyn_cast<IntrinsicInst>(Inst);
1801   // It might be interesting to track whether or not we are inside a catch
1802   // function, but that might make the algorithm more brittle than it needs
1803   // to be.
1804
1805   // The end catch call can occur in one of two places: either in a
1806   // landingpad block that is part of the catch handlers exception mechanism,
1807   // or at the end of the catch block.  However, a catch-all handler may call
1808   // end catch from the original landing pad.  If the call occurs in a nested
1809   // landing pad block, we must skip it and continue so that the landing pad
1810   // gets cloned.
1811   auto *ParentBB = IntrinCall->getParent();
1812   if (ParentBB->isLandingPad() && !LPadMap.isOriginLandingPadBlock(ParentBB))
1813     return CloningDirector::SkipInstruction;
1814
1815   // If an end catch occurs anywhere else we want to terminate the handler
1816   // with a return to the code that follows the endcatch call.  If the
1817   // next instruction is not an unconditional branch, we need to split the
1818   // block to provide a clear target for the return instruction.
1819   BasicBlock *ContinueBB;
1820   auto Next = std::next(BasicBlock::const_iterator(IntrinCall));
1821   const BranchInst *Branch = dyn_cast<BranchInst>(Next);
1822   if (!Branch || !Branch->isUnconditional()) {
1823     // We're interrupting the cloning process at this location, so the
1824     // const_cast we're doing here will not cause a problem.
1825     ContinueBB = SplitBlock(const_cast<BasicBlock *>(ParentBB),
1826                             const_cast<Instruction *>(cast<Instruction>(Next)));
1827   } else {
1828     ContinueBB = Branch->getSuccessor(0);
1829   }
1830
1831   ReturnInst::Create(NewBB->getContext(), BlockAddress::get(ContinueBB), NewBB);
1832   ReturnTargets.push_back(ContinueBB);
1833
1834   // We just added a terminator to the cloned block.
1835   // Tell the caller to stop processing the current basic block so that
1836   // the branch instruction will be skipped.
1837   return CloningDirector::StopCloningBB;
1838 }
1839
1840 CloningDirector::CloningAction WinEHCatchDirector::handleTypeIdFor(
1841     ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1842   auto *IntrinCall = dyn_cast<IntrinsicInst>(Inst);
1843   Value *Selector = IntrinCall->getArgOperand(0)->stripPointerCasts();
1844   // This causes a replacement that will collapse the landing pad CFG based
1845   // on the filter function we intend to match.
1846   if (Selector == CurrentSelector)
1847     VMap[Inst] = ConstantInt::get(SelectorIDType, 1);
1848   else
1849     VMap[Inst] = ConstantInt::get(SelectorIDType, 0);
1850   // Tell the caller not to clone this instruction.
1851   return CloningDirector::SkipInstruction;
1852 }
1853
1854 CloningDirector::CloningAction WinEHCatchDirector::handleIndirectBr(
1855     ValueToValueMapTy &VMap,
1856     const IndirectBrInst *IBr,
1857     BasicBlock *NewBB) {
1858   // If this indirect branch is not part of a landing pad block, just clone it.
1859   const BasicBlock *ParentBB = IBr->getParent();
1860   if (!ParentBB->isLandingPad())
1861     return CloningDirector::CloneInstruction;
1862
1863   // If it is part of a landing pad, we want to filter out target blocks
1864   // that are not part of the handler we are outlining.
1865   const LandingPadInst *LPad = ParentBB->getLandingPadInst();
1866
1867   // Save this correlation for later processing.
1868   NestedLPtoOriginalLP[cast<LandingPadInst>(VMap[LPad])] = LPad;
1869
1870   // We should only get here for landing pads that have already been outlined.
1871   assert(match(LPad->getNextNode(), m_Intrinsic<Intrinsic::eh_actions>()));
1872
1873   // Copy the indirectbr, but only include targets that were previously
1874   // identified as EH blocks and are dominated by the nested landing pad.
1875   SetVector<const BasicBlock *> ReturnTargets;
1876   for (int I = 0, E = IBr->getNumDestinations(); I < E; ++I) {
1877     auto *TargetBB = IBr->getDestination(I);
1878     if (EHBlocks.count(const_cast<BasicBlock*>(TargetBB)) &&
1879         DT->dominates(ParentBB, TargetBB)) {
1880       DEBUG(dbgs() << "  Adding destination " << TargetBB->getName() << "\n");
1881       ReturnTargets.insert(TargetBB);
1882     }
1883   }
1884   IndirectBrInst *NewBranch = 
1885         IndirectBrInst::Create(const_cast<Value *>(IBr->getAddress()),
1886                                ReturnTargets.size(), NewBB);
1887   for (auto *Target : ReturnTargets)
1888     NewBranch->addDestination(const_cast<BasicBlock*>(Target));
1889
1890   // The operands and targets of the branch instruction are remapped later
1891   // because it is a terminator.  Tell the cloning code to clone the
1892   // blocks we just added to the target list.
1893   return CloningDirector::CloneSuccessors;
1894 }
1895
1896 CloningDirector::CloningAction
1897 WinEHCatchDirector::handleInvoke(ValueToValueMapTy &VMap,
1898                                  const InvokeInst *Invoke, BasicBlock *NewBB) {
1899   return CloningDirector::CloneInstruction;
1900 }
1901
1902 CloningDirector::CloningAction
1903 WinEHCatchDirector::handleResume(ValueToValueMapTy &VMap,
1904                                  const ResumeInst *Resume, BasicBlock *NewBB) {
1905   // Resume instructions shouldn't be reachable from catch handlers.
1906   // We still need to handle it, but it will be pruned.
1907   BasicBlock::InstListType &InstList = NewBB->getInstList();
1908   InstList.push_back(new UnreachableInst(NewBB->getContext()));
1909   return CloningDirector::StopCloningBB;
1910 }
1911
1912 CloningDirector::CloningAction
1913 WinEHCatchDirector::handleCompare(ValueToValueMapTy &VMap,
1914                                   const CmpInst *Compare, BasicBlock *NewBB) {
1915   const IntrinsicInst *IntrinCall = nullptr;
1916   if (match(Compare->getOperand(0), m_Intrinsic<Intrinsic::eh_typeid_for>())) {
1917     IntrinCall = dyn_cast<IntrinsicInst>(Compare->getOperand(0));
1918   } else if (match(Compare->getOperand(1),
1919                    m_Intrinsic<Intrinsic::eh_typeid_for>())) {
1920     IntrinCall = dyn_cast<IntrinsicInst>(Compare->getOperand(1));
1921   }
1922   if (IntrinCall) {
1923     Value *Selector = IntrinCall->getArgOperand(0)->stripPointerCasts();
1924     // This causes a replacement that will collapse the landing pad CFG based
1925     // on the filter function we intend to match.
1926     if (Selector == CurrentSelector->stripPointerCasts()) {
1927       VMap[Compare] = ConstantInt::get(SelectorIDType, 1);
1928     } else {
1929       VMap[Compare] = ConstantInt::get(SelectorIDType, 0);
1930     }
1931     return CloningDirector::SkipInstruction;
1932   }
1933   return CloningDirector::CloneInstruction;
1934 }
1935
1936 CloningDirector::CloningAction WinEHCleanupDirector::handleLandingPad(
1937     ValueToValueMapTy &VMap, const LandingPadInst *LPad, BasicBlock *NewBB) {
1938   // The MS runtime will terminate the process if an exception occurs in a
1939   // cleanup handler, so we shouldn't encounter landing pads in the actual
1940   // cleanup code, but they may appear in catch blocks.  Depending on where
1941   // we started cloning we may see one, but it will get dropped during dead
1942   // block pruning.
1943   Instruction *NewInst = new UnreachableInst(NewBB->getContext());
1944   VMap[LPad] = NewInst;
1945   BasicBlock::InstListType &InstList = NewBB->getInstList();
1946   InstList.push_back(NewInst);
1947   return CloningDirector::StopCloningBB;
1948 }
1949
1950 CloningDirector::CloningAction WinEHCleanupDirector::handleBeginCatch(
1951     ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1952   // Cleanup code may flow into catch blocks or the catch block may be part
1953   // of a branch that will be optimized away.  We'll insert a return
1954   // instruction now, but it may be pruned before the cloning process is
1955   // complete.
1956   ReturnInst::Create(NewBB->getContext(), nullptr, NewBB);
1957   return CloningDirector::StopCloningBB;
1958 }
1959
1960 CloningDirector::CloningAction WinEHCleanupDirector::handleEndCatch(
1961     ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1962   // Cleanup handlers nested within catch handlers may begin with a call to
1963   // eh.endcatch.  We can just ignore that instruction.
1964   return CloningDirector::SkipInstruction;
1965 }
1966
1967 CloningDirector::CloningAction WinEHCleanupDirector::handleTypeIdFor(
1968     ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1969   // If we encounter a selector comparison while cloning a cleanup handler,
1970   // we want to stop cloning immediately.  Anything after the dispatch
1971   // will be outlined into a different handler.
1972   BasicBlock *CatchHandler;
1973   Constant *Selector;
1974   BasicBlock *NextBB;
1975   if (isSelectorDispatch(const_cast<BasicBlock *>(Inst->getParent()),
1976                          CatchHandler, Selector, NextBB)) {
1977     ReturnInst::Create(NewBB->getContext(), nullptr, NewBB);
1978     return CloningDirector::StopCloningBB;
1979   }
1980   // If eg.typeid.for is called for any other reason, it can be ignored.
1981   VMap[Inst] = ConstantInt::get(SelectorIDType, 0);
1982   return CloningDirector::SkipInstruction;
1983 }
1984
1985 CloningDirector::CloningAction WinEHCleanupDirector::handleIndirectBr(
1986     ValueToValueMapTy &VMap,
1987     const IndirectBrInst *IBr,
1988     BasicBlock *NewBB) {
1989   // No special handling is required for cleanup cloning.
1990   return CloningDirector::CloneInstruction;
1991 }
1992
1993 CloningDirector::CloningAction WinEHCleanupDirector::handleInvoke(
1994     ValueToValueMapTy &VMap, const InvokeInst *Invoke, BasicBlock *NewBB) {
1995   // All invokes in cleanup handlers can be replaced with calls.
1996   SmallVector<Value *, 16> CallArgs(Invoke->op_begin(), Invoke->op_end() - 3);
1997   // Insert a normal call instruction...
1998   CallInst *NewCall =
1999       CallInst::Create(const_cast<Value *>(Invoke->getCalledValue()), CallArgs,
2000                        Invoke->getName(), NewBB);
2001   NewCall->setCallingConv(Invoke->getCallingConv());
2002   NewCall->setAttributes(Invoke->getAttributes());
2003   NewCall->setDebugLoc(Invoke->getDebugLoc());
2004   VMap[Invoke] = NewCall;
2005
2006   // Remap the operands.
2007   llvm::RemapInstruction(NewCall, VMap, RF_None, nullptr, &Materializer);
2008
2009   // Insert an unconditional branch to the normal destination.
2010   BranchInst::Create(Invoke->getNormalDest(), NewBB);
2011
2012   // The unwind destination won't be cloned into the new function, so
2013   // we don't need to clean up its phi nodes.
2014
2015   // We just added a terminator to the cloned block.
2016   // Tell the caller to stop processing the current basic block.
2017   return CloningDirector::CloneSuccessors;
2018 }
2019
2020 CloningDirector::CloningAction WinEHCleanupDirector::handleResume(
2021     ValueToValueMapTy &VMap, const ResumeInst *Resume, BasicBlock *NewBB) {
2022   ReturnInst::Create(NewBB->getContext(), nullptr, NewBB);
2023
2024   // We just added a terminator to the cloned block.
2025   // Tell the caller to stop processing the current basic block so that
2026   // the branch instruction will be skipped.
2027   return CloningDirector::StopCloningBB;
2028 }
2029
2030 CloningDirector::CloningAction
2031 WinEHCleanupDirector::handleCompare(ValueToValueMapTy &VMap,
2032                                     const CmpInst *Compare, BasicBlock *NewBB) {
2033   if (match(Compare->getOperand(0), m_Intrinsic<Intrinsic::eh_typeid_for>()) ||
2034       match(Compare->getOperand(1), m_Intrinsic<Intrinsic::eh_typeid_for>())) {
2035     VMap[Compare] = ConstantInt::get(SelectorIDType, 1);
2036     return CloningDirector::SkipInstruction;
2037   }
2038   return CloningDirector::CloneInstruction;
2039 }
2040
2041 WinEHFrameVariableMaterializer::WinEHFrameVariableMaterializer(
2042     Function *OutlinedFn, Value *ParentFP, FrameVarInfoMap &FrameVarInfo)
2043     : FrameVarInfo(FrameVarInfo), Builder(OutlinedFn->getContext()) {
2044   BasicBlock *EntryBB = &OutlinedFn->getEntryBlock();
2045
2046   // New allocas should be inserted in the entry block, but after the parent FP
2047   // is established if it is an instruction.
2048   BasicBlock::iterator InsertPoint = EntryBB->getFirstInsertionPt();
2049   if (auto *FPInst = dyn_cast<Instruction>(ParentFP))
2050     InsertPoint = std::next(FPInst->getIterator());
2051   Builder.SetInsertPoint(EntryBB, InsertPoint);
2052 }
2053
2054 Value *WinEHFrameVariableMaterializer::materializeValueFor(Value *V) {
2055   // If we're asked to materialize a static alloca, we temporarily create an
2056   // alloca in the outlined function and add this to the FrameVarInfo map.  When
2057   // all the outlining is complete, we'll replace these temporary allocas with
2058   // calls to llvm.localrecover.
2059   if (auto *AV = dyn_cast<AllocaInst>(V)) {
2060     assert(AV->isStaticAlloca() &&
2061            "cannot materialize un-demoted dynamic alloca");
2062     AllocaInst *NewAlloca = dyn_cast<AllocaInst>(AV->clone());
2063     Builder.Insert(NewAlloca, AV->getName());
2064     FrameVarInfo[AV].push_back(NewAlloca);
2065     return NewAlloca;
2066   }
2067
2068   if (isa<Instruction>(V) || isa<Argument>(V)) {
2069     Function *Parent = isa<Instruction>(V)
2070                            ? cast<Instruction>(V)->getParent()->getParent()
2071                            : cast<Argument>(V)->getParent();
2072     errs()
2073         << "Failed to demote instruction used in exception handler of function "
2074         << GlobalValue::getRealLinkageName(Parent->getName()) << ":\n";
2075     errs() << "  " << *V << '\n';
2076     report_fatal_error("WinEHPrepare failed to demote instruction");
2077   }
2078
2079   // Don't materialize other values.
2080   return nullptr;
2081 }
2082
2083 void WinEHFrameVariableMaterializer::escapeCatchObject(Value *V) {
2084   // Catch parameter objects have to live in the parent frame. When we see a use
2085   // of a catch parameter, add a sentinel to the multimap to indicate that it's
2086   // used from another handler. This will prevent us from trying to sink the
2087   // alloca into the handler and ensure that the catch parameter is present in
2088   // the call to llvm.localescape.
2089   FrameVarInfo[V].push_back(getCatchObjectSentinel());
2090 }
2091
2092 // This function maps the catch and cleanup handlers that are reachable from the
2093 // specified landing pad. The landing pad sequence will have this basic shape:
2094 //
2095 //  <cleanup handler>
2096 //  <selector comparison>
2097 //  <catch handler>
2098 //  <cleanup handler>
2099 //  <selector comparison>
2100 //  <catch handler>
2101 //  <cleanup handler>
2102 //  ...
2103 //
2104 // Any of the cleanup slots may be absent.  The cleanup slots may be occupied by
2105 // any arbitrary control flow, but all paths through the cleanup code must
2106 // eventually reach the next selector comparison and no path can skip to a
2107 // different selector comparisons, though some paths may terminate abnormally.
2108 // Therefore, we will use a depth first search from the start of any given
2109 // cleanup block and stop searching when we find the next selector comparison.
2110 //
2111 // If the landingpad instruction does not have a catch clause, we will assume
2112 // that any instructions other than selector comparisons and catch handlers can
2113 // be ignored.  In practice, these will only be the boilerplate instructions.
2114 //
2115 // The catch handlers may also have any control structure, but we are only
2116 // interested in the start of the catch handlers, so we don't need to actually
2117 // follow the flow of the catch handlers.  The start of the catch handlers can
2118 // be located from the compare instructions, but they can be skipped in the
2119 // flow by following the contrary branch.
2120 void WinEHPrepare::mapLandingPadBlocks(LandingPadInst *LPad,
2121                                        LandingPadActions &Actions) {
2122   unsigned int NumClauses = LPad->getNumClauses();
2123   unsigned int HandlersFound = 0;
2124   BasicBlock *BB = LPad->getParent();
2125
2126   DEBUG(dbgs() << "Mapping landing pad: " << BB->getName() << "\n");
2127
2128   if (NumClauses == 0) {
2129     findCleanupHandlers(Actions, BB, nullptr);
2130     return;
2131   }
2132
2133   VisitedBlockSet VisitedBlocks;
2134
2135   while (HandlersFound != NumClauses) {
2136     BasicBlock *NextBB = nullptr;
2137
2138     // Skip over filter clauses.
2139     if (LPad->isFilter(HandlersFound)) {
2140       ++HandlersFound;
2141       continue;
2142     }
2143
2144     // See if the clause we're looking for is a catch-all.
2145     // If so, the catch begins immediately.
2146     Constant *ExpectedSelector =
2147         LPad->getClause(HandlersFound)->stripPointerCasts();
2148     if (isa<ConstantPointerNull>(ExpectedSelector)) {
2149       // The catch all must occur last.
2150       assert(HandlersFound == NumClauses - 1);
2151
2152       // There can be additional selector dispatches in the call chain that we
2153       // need to ignore.
2154       BasicBlock *CatchBlock = nullptr;
2155       Constant *Selector;
2156       while (BB && isSelectorDispatch(BB, CatchBlock, Selector, NextBB)) {
2157         DEBUG(dbgs() << "  Found extra catch dispatch in block "
2158                      << CatchBlock->getName() << "\n");
2159         BB = NextBB;
2160       }
2161
2162       // Add the catch handler to the action list.
2163       CatchHandler *Action = nullptr;
2164       if (CatchHandlerMap.count(BB) && CatchHandlerMap[BB] != nullptr) {
2165         // If the CatchHandlerMap already has an entry for this BB, re-use it.
2166         Action = CatchHandlerMap[BB];
2167         assert(Action->getSelector() == ExpectedSelector);
2168       } else {
2169         // We don't expect a selector dispatch, but there may be a call to
2170         // llvm.eh.begincatch, which separates catch handling code from
2171         // cleanup code in the same control flow.  This call looks for the
2172         // begincatch intrinsic.
2173         Action = findCatchHandler(BB, NextBB, VisitedBlocks);
2174         if (Action) {
2175           // For C++ EH, check if there is any interesting cleanup code before
2176           // we begin the catch. This is important because cleanups cannot
2177           // rethrow exceptions but code called from catches can. For SEH, it
2178           // isn't important if some finally code before a catch-all is executed
2179           // out of line or after recovering from the exception.
2180           if (Personality == EHPersonality::MSVC_CXX)
2181             findCleanupHandlers(Actions, BB, BB);
2182         } else {
2183           // If an action was not found, it means that the control flows
2184           // directly into the catch-all handler and there is no cleanup code.
2185           // That's an expected situation and we must create a catch action.
2186           // Since this is a catch-all handler, the selector won't actually
2187           // appear in the code anywhere.  ExpectedSelector here is the constant
2188           // null ptr that we got from the landing pad instruction.
2189           Action = new CatchHandler(BB, ExpectedSelector, nullptr);
2190           CatchHandlerMap[BB] = Action;
2191         }
2192       }
2193       Actions.insertCatchHandler(Action);
2194       DEBUG(dbgs() << "  Catch all handler at block " << BB->getName() << "\n");
2195       ++HandlersFound;
2196
2197       // Once we reach a catch-all, don't expect to hit a resume instruction.
2198       BB = nullptr;
2199       break;
2200     }
2201
2202     CatchHandler *CatchAction = findCatchHandler(BB, NextBB, VisitedBlocks);
2203     assert(CatchAction);
2204
2205     // See if there is any interesting code executed before the dispatch.
2206     findCleanupHandlers(Actions, BB, CatchAction->getStartBlock());
2207
2208     // When the source program contains multiple nested try blocks the catch
2209     // handlers can get strung together in such a way that we can encounter
2210     // a dispatch for a selector that we've already had a handler for.
2211     if (CatchAction->getSelector()->stripPointerCasts() == ExpectedSelector) {
2212       ++HandlersFound;
2213
2214       // Add the catch handler to the action list.
2215       DEBUG(dbgs() << "  Found catch dispatch in block "
2216                    << CatchAction->getStartBlock()->getName() << "\n");
2217       Actions.insertCatchHandler(CatchAction);
2218     } else {
2219       // Under some circumstances optimized IR will flow unconditionally into a
2220       // handler block without checking the selector.  This can only happen if
2221       // the landing pad has a catch-all handler and the handler for the
2222       // preceding catch clause is identical to the catch-call handler
2223       // (typically an empty catch).  In this case, the handler must be shared
2224       // by all remaining clauses.
2225       if (isa<ConstantPointerNull>(
2226               CatchAction->getSelector()->stripPointerCasts())) {
2227         DEBUG(dbgs() << "  Applying early catch-all handler in block "
2228                      << CatchAction->getStartBlock()->getName()
2229                      << "  to all remaining clauses.\n");
2230         Actions.insertCatchHandler(CatchAction);
2231         return;
2232       }
2233
2234       DEBUG(dbgs() << "  Found extra catch dispatch in block "
2235                    << CatchAction->getStartBlock()->getName() << "\n");
2236     }
2237
2238     // Move on to the block after the catch handler.
2239     BB = NextBB;
2240   }
2241
2242   // If we didn't wind up in a catch-all, see if there is any interesting code
2243   // executed before the resume.
2244   findCleanupHandlers(Actions, BB, BB);
2245
2246   // It's possible that some optimization moved code into a landingpad that
2247   // wasn't
2248   // previously being used for cleanup.  If that happens, we need to execute
2249   // that
2250   // extra code from a cleanup handler.
2251   if (Actions.includesCleanup() && !LPad->isCleanup())
2252     LPad->setCleanup(true);
2253 }
2254
2255 // This function searches starting with the input block for the next
2256 // block that terminates with a branch whose condition is based on a selector
2257 // comparison.  This may be the input block.  See the mapLandingPadBlocks
2258 // comments for a discussion of control flow assumptions.
2259 //
2260 CatchHandler *WinEHPrepare::findCatchHandler(BasicBlock *BB,
2261                                              BasicBlock *&NextBB,
2262                                              VisitedBlockSet &VisitedBlocks) {
2263   // See if we've already found a catch handler use it.
2264   // Call count() first to avoid creating a null entry for blocks
2265   // we haven't seen before.
2266   if (CatchHandlerMap.count(BB) && CatchHandlerMap[BB] != nullptr) {
2267     CatchHandler *Action = cast<CatchHandler>(CatchHandlerMap[BB]);
2268     NextBB = Action->getNextBB();
2269     return Action;
2270   }
2271
2272   // VisitedBlocks applies only to the current search.  We still
2273   // need to consider blocks that we've visited while mapping other
2274   // landing pads.
2275   VisitedBlocks.insert(BB);
2276
2277   BasicBlock *CatchBlock = nullptr;
2278   Constant *Selector = nullptr;
2279
2280   // If this is the first time we've visited this block from any landing pad
2281   // look to see if it is a selector dispatch block.
2282   if (!CatchHandlerMap.count(BB)) {
2283     if (isSelectorDispatch(BB, CatchBlock, Selector, NextBB)) {
2284       CatchHandler *Action = new CatchHandler(BB, Selector, NextBB);
2285       CatchHandlerMap[BB] = Action;
2286       return Action;
2287     }
2288     // If we encounter a block containing an llvm.eh.begincatch before we
2289     // find a selector dispatch block, the handler is assumed to be
2290     // reached unconditionally.  This happens for catch-all blocks, but
2291     // it can also happen for other catch handlers that have been combined
2292     // with the catch-all handler during optimization.
2293     if (isCatchBlock(BB)) {
2294       PointerType *Int8PtrTy = Type::getInt8PtrTy(BB->getContext());
2295       Constant *NullSelector = ConstantPointerNull::get(Int8PtrTy);
2296       CatchHandler *Action = new CatchHandler(BB, NullSelector, nullptr);
2297       CatchHandlerMap[BB] = Action;
2298       return Action;
2299     }
2300   }
2301
2302   // Visit each successor, looking for the dispatch.
2303   // FIXME: We expect to find the dispatch quickly, so this will probably
2304   //        work better as a breadth first search.
2305   for (BasicBlock *Succ : successors(BB)) {
2306     if (VisitedBlocks.count(Succ))
2307       continue;
2308
2309     CatchHandler *Action = findCatchHandler(Succ, NextBB, VisitedBlocks);
2310     if (Action)
2311       return Action;
2312   }
2313   return nullptr;
2314 }
2315
2316 // These are helper functions to combine repeated code from findCleanupHandlers.
2317 static void createCleanupHandler(LandingPadActions &Actions,
2318                                  CleanupHandlerMapTy &CleanupHandlerMap,
2319                                  BasicBlock *BB) {
2320   CleanupHandler *Action = new CleanupHandler(BB);
2321   CleanupHandlerMap[BB] = Action;
2322   Actions.insertCleanupHandler(Action);
2323   DEBUG(dbgs() << "  Found cleanup code in block "
2324                << Action->getStartBlock()->getName() << "\n");
2325 }
2326
2327 static CallSite matchOutlinedFinallyCall(BasicBlock *BB,
2328                                          Instruction *MaybeCall) {
2329   // Look for finally blocks that Clang has already outlined for us.
2330   //   %fp = call i8* @llvm.localaddress()
2331   //   call void @"fin$parent"(iN 1, i8* %fp)
2332   if (isLocalAddressCall(MaybeCall) && MaybeCall != BB->getTerminator())
2333     MaybeCall = MaybeCall->getNextNode();
2334   CallSite FinallyCall(MaybeCall);
2335   if (!FinallyCall || FinallyCall.arg_size() != 2)
2336     return CallSite();
2337   if (!match(FinallyCall.getArgument(0), m_SpecificInt(1)))
2338     return CallSite();
2339   if (!isLocalAddressCall(FinallyCall.getArgument(1)))
2340     return CallSite();
2341   return FinallyCall;
2342 }
2343
2344 static BasicBlock *followSingleUnconditionalBranches(BasicBlock *BB) {
2345   // Skip single ubr blocks.
2346   while (BB->getFirstNonPHIOrDbg() == BB->getTerminator()) {
2347     auto *Br = dyn_cast<BranchInst>(BB->getTerminator());
2348     if (Br && Br->isUnconditional())
2349       BB = Br->getSuccessor(0);
2350     else
2351       return BB;
2352   }
2353   return BB;
2354 }
2355
2356 // This function searches starting with the input block for the next block that
2357 // contains code that is not part of a catch handler and would not be eliminated
2358 // during handler outlining.
2359 //
2360 void WinEHPrepare::findCleanupHandlers(LandingPadActions &Actions,
2361                                        BasicBlock *StartBB, BasicBlock *EndBB) {
2362   // Here we will skip over the following:
2363   //
2364   // landing pad prolog:
2365   //
2366   // Unconditional branches
2367   //
2368   // Selector dispatch
2369   //
2370   // Resume pattern
2371   //
2372   // Anything else marks the start of an interesting block
2373
2374   BasicBlock *BB = StartBB;
2375   // Anything other than an unconditional branch will kick us out of this loop
2376   // one way or another.
2377   while (BB) {
2378     BB = followSingleUnconditionalBranches(BB);
2379     // If we've already scanned this block, don't scan it again.  If it is
2380     // a cleanup block, there will be an action in the CleanupHandlerMap.
2381     // If we've scanned it and it is not a cleanup block, there will be a
2382     // nullptr in the CleanupHandlerMap.  If we have not scanned it, there will
2383     // be no entry in the CleanupHandlerMap.  We must call count() first to
2384     // avoid creating a null entry for blocks we haven't scanned.
2385     if (CleanupHandlerMap.count(BB)) {
2386       if (auto *Action = CleanupHandlerMap[BB]) {
2387         Actions.insertCleanupHandler(Action);
2388         DEBUG(dbgs() << "  Found cleanup code in block "
2389                      << Action->getStartBlock()->getName() << "\n");
2390         // FIXME: This cleanup might chain into another, and we need to discover
2391         // that.
2392         return;
2393       } else {
2394         // Here we handle the case where the cleanup handler map contains a
2395         // value for this block but the value is a nullptr.  This means that
2396         // we have previously analyzed the block and determined that it did
2397         // not contain any cleanup code.  Based on the earlier analysis, we
2398         // know the block must end in either an unconditional branch, a
2399         // resume or a conditional branch that is predicated on a comparison
2400         // with a selector.  Either the resume or the selector dispatch
2401         // would terminate the search for cleanup code, so the unconditional
2402         // branch is the only case for which we might need to continue
2403         // searching.
2404         BasicBlock *SuccBB = followSingleUnconditionalBranches(BB);
2405         if (SuccBB == BB || SuccBB == EndBB)
2406           return;
2407         BB = SuccBB;
2408         continue;
2409       }
2410     }
2411
2412     // Create an entry in the cleanup handler map for this block.  Initially
2413     // we create an entry that says this isn't a cleanup block.  If we find
2414     // cleanup code, the caller will replace this entry.
2415     CleanupHandlerMap[BB] = nullptr;
2416
2417     TerminatorInst *Terminator = BB->getTerminator();
2418
2419     // Landing pad blocks have extra instructions we need to accept.
2420     LandingPadMap *LPadMap = nullptr;
2421     if (BB->isLandingPad()) {
2422       LandingPadInst *LPad = BB->getLandingPadInst();
2423       LPadMap = &LPadMaps[LPad];
2424       if (!LPadMap->isInitialized())
2425         LPadMap->mapLandingPad(LPad);
2426     }
2427
2428     // Look for the bare resume pattern:
2429     //   %lpad.val1 = insertvalue { i8*, i32 } undef, i8* %exn, 0
2430     //   %lpad.val2 = insertvalue { i8*, i32 } %lpad.val1, i32 %sel, 1
2431     //   resume { i8*, i32 } %lpad.val2
2432     if (auto *Resume = dyn_cast<ResumeInst>(Terminator)) {
2433       InsertValueInst *Insert1 = nullptr;
2434       InsertValueInst *Insert2 = nullptr;
2435       Value *ResumeVal = Resume->getOperand(0);
2436       // If the resume value isn't a phi or landingpad value, it should be a
2437       // series of insertions. Identify them so we can avoid them when scanning
2438       // for cleanups.
2439       if (!isa<PHINode>(ResumeVal) && !isa<LandingPadInst>(ResumeVal)) {
2440         Insert2 = dyn_cast<InsertValueInst>(ResumeVal);
2441         if (!Insert2)
2442           return createCleanupHandler(Actions, CleanupHandlerMap, BB);
2443         Insert1 = dyn_cast<InsertValueInst>(Insert2->getAggregateOperand());
2444         if (!Insert1)
2445           return createCleanupHandler(Actions, CleanupHandlerMap, BB);
2446       }
2447       for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg()->getIterator(),
2448                                 IE = BB->end();
2449            II != IE; ++II) {
2450         Instruction *Inst = &*II;
2451         if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst))
2452           continue;
2453         if (Inst == Insert1 || Inst == Insert2 || Inst == Resume)
2454           continue;
2455         if (!Inst->hasOneUse() ||
2456             (Inst->user_back() != Insert1 && Inst->user_back() != Insert2)) {
2457           return createCleanupHandler(Actions, CleanupHandlerMap, BB);
2458         }
2459       }
2460       return;
2461     }
2462
2463     BranchInst *Branch = dyn_cast<BranchInst>(Terminator);
2464     if (Branch && Branch->isConditional()) {
2465       // Look for the selector dispatch.
2466       //   %2 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIf to i8*))
2467       //   %matches = icmp eq i32 %sel, %2
2468       //   br i1 %matches, label %catch14, label %eh.resume
2469       CmpInst *Compare = dyn_cast<CmpInst>(Branch->getCondition());
2470       if (!Compare || !Compare->isEquality())
2471         return createCleanupHandler(Actions, CleanupHandlerMap, BB);
2472       for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg()->getIterator(),
2473                                 IE = BB->end();
2474            II != IE; ++II) {
2475         Instruction *Inst = &*II;
2476         if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst))
2477           continue;
2478         if (Inst == Compare || Inst == Branch)
2479           continue;
2480         if (match(Inst, m_Intrinsic<Intrinsic::eh_typeid_for>()))
2481           continue;
2482         return createCleanupHandler(Actions, CleanupHandlerMap, BB);
2483       }
2484       // The selector dispatch block should always terminate our search.
2485       assert(BB == EndBB);
2486       return;
2487     }
2488
2489     if (isAsynchronousEHPersonality(Personality)) {
2490       // If this is a landingpad block, split the block at the first non-landing
2491       // pad instruction.
2492       Instruction *MaybeCall = BB->getFirstNonPHIOrDbg();
2493       if (LPadMap) {
2494         while (MaybeCall != BB->getTerminator() &&
2495                LPadMap->isLandingPadSpecificInst(MaybeCall))
2496           MaybeCall = MaybeCall->getNextNode();
2497       }
2498
2499       // Look for outlined finally calls on x64, since those happen to match the
2500       // prototype provided by the runtime.
2501       if (TheTriple.getArch() == Triple::x86_64) {
2502         if (CallSite FinallyCall = matchOutlinedFinallyCall(BB, MaybeCall)) {
2503           Function *Fin = FinallyCall.getCalledFunction();
2504           assert(Fin && "outlined finally call should be direct");
2505           auto *Action = new CleanupHandler(BB);
2506           Action->setHandlerBlockOrFunc(Fin);
2507           Actions.insertCleanupHandler(Action);
2508           CleanupHandlerMap[BB] = Action;
2509           DEBUG(dbgs() << "  Found frontend-outlined finally call to "
2510                        << Fin->getName() << " in block "
2511                        << Action->getStartBlock()->getName() << "\n");
2512
2513           // Split the block if there were more interesting instructions and
2514           // look for finally calls in the normal successor block.
2515           BasicBlock *SuccBB = BB;
2516           if (FinallyCall.getInstruction() != BB->getTerminator() &&
2517               FinallyCall.getInstruction()->getNextNode() !=
2518                   BB->getTerminator()) {
2519             SuccBB =
2520                 SplitBlock(BB, FinallyCall.getInstruction()->getNextNode(), DT);
2521           } else {
2522             if (FinallyCall.isInvoke()) {
2523               SuccBB = cast<InvokeInst>(FinallyCall.getInstruction())
2524                            ->getNormalDest();
2525             } else {
2526               SuccBB = BB->getUniqueSuccessor();
2527               assert(SuccBB &&
2528                      "splitOutlinedFinallyCalls didn't insert a branch");
2529             }
2530           }
2531           BB = SuccBB;
2532           if (BB == EndBB)
2533             return;
2534           continue;
2535         }
2536       }
2537     }
2538
2539     // Anything else is either a catch block or interesting cleanup code.
2540     for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg()->getIterator(),
2541                               IE = BB->end();
2542          II != IE; ++II) {
2543       Instruction *Inst = &*II;
2544       if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst))
2545         continue;
2546       // Unconditional branches fall through to this loop.
2547       if (Inst == Branch)
2548         continue;
2549       // If this is a catch block, there is no cleanup code to be found.
2550       if (match(Inst, m_Intrinsic<Intrinsic::eh_begincatch>()))
2551         return;
2552       // If this a nested landing pad, it may contain an endcatch call.
2553       if (match(Inst, m_Intrinsic<Intrinsic::eh_endcatch>()))
2554         return;
2555       // Anything else makes this interesting cleanup code.
2556       return createCleanupHandler(Actions, CleanupHandlerMap, BB);
2557     }
2558
2559     // Only unconditional branches in empty blocks should get this far.
2560     assert(Branch && Branch->isUnconditional());
2561     if (BB == EndBB)
2562       return;
2563     BB = Branch->getSuccessor(0);
2564   }
2565 }
2566
2567 // This is a public function, declared in WinEHFuncInfo.h and is also
2568 // referenced by WinEHNumbering in FunctionLoweringInfo.cpp.
2569 void llvm::parseEHActions(
2570     const IntrinsicInst *II,
2571     SmallVectorImpl<std::unique_ptr<ActionHandler>> &Actions) {
2572   assert(II->getIntrinsicID() == Intrinsic::eh_actions &&
2573          "attempted to parse non eh.actions intrinsic");
2574   for (unsigned I = 0, E = II->getNumArgOperands(); I != E;) {
2575     uint64_t ActionKind =
2576         cast<ConstantInt>(II->getArgOperand(I))->getZExtValue();
2577     if (ActionKind == /*catch=*/1) {
2578       auto *Selector = cast<Constant>(II->getArgOperand(I + 1));
2579       ConstantInt *EHObjIndex = cast<ConstantInt>(II->getArgOperand(I + 2));
2580       int64_t EHObjIndexVal = EHObjIndex->getSExtValue();
2581       Constant *Handler = cast<Constant>(II->getArgOperand(I + 3));
2582       I += 4;
2583       auto CH = make_unique<CatchHandler>(/*BB=*/nullptr, Selector,
2584                                           /*NextBB=*/nullptr);
2585       CH->setHandlerBlockOrFunc(Handler);
2586       CH->setExceptionVarIndex(EHObjIndexVal);
2587       Actions.push_back(std::move(CH));
2588     } else if (ActionKind == 0) {
2589       Constant *Handler = cast<Constant>(II->getArgOperand(I + 1));
2590       I += 2;
2591       auto CH = make_unique<CleanupHandler>(/*BB=*/nullptr);
2592       CH->setHandlerBlockOrFunc(Handler);
2593       Actions.push_back(std::move(CH));
2594     } else {
2595       llvm_unreachable("Expected either a catch or cleanup handler!");
2596     }
2597   }
2598   std::reverse(Actions.begin(), Actions.end());
2599 }
2600
2601 static int addUnwindMapEntry(WinEHFuncInfo &FuncInfo, int ToState,
2602                              const Value *V) {
2603   WinEHUnwindMapEntry UME;
2604   UME.ToState = ToState;
2605   UME.Cleanup = V;
2606   FuncInfo.UnwindMap.push_back(UME);
2607   return FuncInfo.getLastStateNumber();
2608 }
2609
2610 static void addTryBlockMapEntry(WinEHFuncInfo &FuncInfo, int TryLow,
2611                                 int TryHigh, int CatchHigh,
2612                                 ArrayRef<const CatchPadInst *> Handlers) {
2613   WinEHTryBlockMapEntry TBME;
2614   TBME.TryLow = TryLow;
2615   TBME.TryHigh = TryHigh;
2616   TBME.CatchHigh = CatchHigh;
2617   assert(TBME.TryLow <= TBME.TryHigh);
2618   for (const CatchPadInst *CPI : Handlers) {
2619     WinEHHandlerType HT;
2620     Constant *TypeInfo = cast<Constant>(CPI->getArgOperand(0));
2621     if (TypeInfo->isNullValue())
2622       HT.TypeDescriptor = nullptr;
2623     else
2624       HT.TypeDescriptor = cast<GlobalVariable>(TypeInfo->stripPointerCasts());
2625     HT.Adjectives = cast<ConstantInt>(CPI->getArgOperand(1))->getZExtValue();
2626     HT.Handler = CPI->getParent();
2627     HT.CatchObjRecoverIdx = -2;
2628     if (isa<ConstantPointerNull>(CPI->getArgOperand(2)))
2629       HT.CatchObj.Alloca = nullptr;
2630     else
2631       HT.CatchObj.Alloca = cast<AllocaInst>(CPI->getArgOperand(2));
2632     TBME.HandlerArray.push_back(HT);
2633   }
2634   FuncInfo.TryBlockMap.push_back(TBME);
2635 }
2636
2637 static const CatchPadInst *getSingleCatchPadPredecessor(const BasicBlock *BB) {
2638   for (const BasicBlock *PredBlock : predecessors(BB))
2639     if (auto *CPI = dyn_cast<CatchPadInst>(PredBlock->getFirstNonPHI()))
2640       return CPI;
2641   return nullptr;
2642 }
2643
2644 /// Find all the catchpads that feed directly into the catchendpad. Frontends
2645 /// using this personality should ensure that each catchendpad and catchpad has
2646 /// one or zero catchpad predecessors.
2647 ///
2648 /// The following C++ generates the IR after it:
2649 ///   try {
2650 ///   } catch (A) {
2651 ///   } catch (B) {
2652 ///   }
2653 ///
2654 /// IR:
2655 ///   %catchpad.A
2656 ///     catchpad [i8* A typeinfo]
2657 ///         to label %catch.A unwind label %catchpad.B
2658 ///   %catchpad.B
2659 ///     catchpad [i8* B typeinfo]
2660 ///         to label %catch.B unwind label %endcatches
2661 ///   %endcatches
2662 ///     catchendblock unwind to caller
2663 static void
2664 findCatchPadsForCatchEndPad(const BasicBlock *CatchEndBB,
2665                             SmallVectorImpl<const CatchPadInst *> &Handlers) {
2666   const CatchPadInst *CPI = getSingleCatchPadPredecessor(CatchEndBB);
2667   while (CPI) {
2668     Handlers.push_back(CPI);
2669     CPI = getSingleCatchPadPredecessor(CPI->getParent());
2670   }
2671   // We've pushed these back into reverse source order.  Reverse them to get
2672   // the list back into source order.
2673   std::reverse(Handlers.begin(), Handlers.end());
2674 }
2675
2676 // Given BB which ends in an unwind edge, return the EHPad that this BB belongs
2677 // to. If the unwind edge came from an invoke, return null.
2678 static const BasicBlock *getEHPadFromPredecessor(const BasicBlock *BB) {
2679   const TerminatorInst *TI = BB->getTerminator();
2680   if (isa<InvokeInst>(TI))
2681     return nullptr;
2682   if (TI->isEHPad())
2683     return BB;
2684   return cast<CleanupReturnInst>(TI)->getCleanupPad()->getParent();
2685 }
2686
2687 static void calculateExplicitCXXStateNumbers(WinEHFuncInfo &FuncInfo,
2688                                              const BasicBlock &BB,
2689                                              int ParentState) {
2690   assert(BB.isEHPad());
2691   const Instruction *FirstNonPHI = BB.getFirstNonPHI();
2692   // All catchpad instructions will be handled when we process their
2693   // respective catchendpad instruction.
2694   if (isa<CatchPadInst>(FirstNonPHI))
2695     return;
2696
2697   if (isa<CatchEndPadInst>(FirstNonPHI)) {
2698     SmallVector<const CatchPadInst *, 2> Handlers;
2699     findCatchPadsForCatchEndPad(&BB, Handlers);
2700     const BasicBlock *FirstTryPad = Handlers.front()->getParent();
2701     int TryLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr);
2702     FuncInfo.EHPadStateMap[Handlers.front()] = TryLow;
2703     for (const BasicBlock *PredBlock : predecessors(FirstTryPad))
2704       if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
2705         calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, TryLow);
2706     int CatchLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr);
2707
2708     // catchpads are separate funclets in C++ EH due to the way rethrow works.
2709     // In SEH, they aren't, so no invokes will unwind to the catchendpad.
2710     FuncInfo.EHPadStateMap[FirstNonPHI] = CatchLow;
2711     int TryHigh = CatchLow - 1;
2712     for (const BasicBlock *PredBlock : predecessors(&BB))
2713       if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
2714         calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, CatchLow);
2715     int CatchHigh = FuncInfo.getLastStateNumber();
2716     addTryBlockMapEntry(FuncInfo, TryLow, TryHigh, CatchHigh, Handlers);
2717     DEBUG(dbgs() << "TryLow[" << FirstTryPad->getName() << "]: " << TryLow
2718                  << '\n');
2719     DEBUG(dbgs() << "TryHigh[" << FirstTryPad->getName() << "]: " << TryHigh
2720                  << '\n');
2721     DEBUG(dbgs() << "CatchHigh[" << FirstTryPad->getName() << "]: " << CatchHigh
2722                  << '\n');
2723   } else if (isa<CleanupPadInst>(FirstNonPHI)) {
2724     // A cleanup can have multiple exits; don't re-process after the first.
2725     if (FuncInfo.EHPadStateMap.count(FirstNonPHI))
2726       return;
2727     int CleanupState = addUnwindMapEntry(FuncInfo, ParentState, &BB);
2728     FuncInfo.EHPadStateMap[FirstNonPHI] = CleanupState;
2729     DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB "
2730                  << BB.getName() << '\n');
2731     for (const BasicBlock *PredBlock : predecessors(&BB))
2732       if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
2733         calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, CleanupState);
2734   } else if (auto *CEPI = dyn_cast<CleanupEndPadInst>(FirstNonPHI)) {
2735     // Propagate ParentState to the cleanuppad in case it doesn't have
2736     // any cleanuprets.
2737     BasicBlock *CleanupBlock = CEPI->getCleanupPad()->getParent();
2738     calculateExplicitCXXStateNumbers(FuncInfo, *CleanupBlock, ParentState);
2739     // Anything unwinding through CleanupEndPadInst is in ParentState.
2740     for (const BasicBlock *PredBlock : predecessors(&BB))
2741       if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
2742         calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, ParentState);
2743   } else if (isa<TerminatePadInst>(FirstNonPHI)) {
2744     report_fatal_error("Not yet implemented!");
2745   } else {
2746     llvm_unreachable("unexpected EH Pad!");
2747   }
2748 }
2749
2750 static int addSEHExcept(WinEHFuncInfo &FuncInfo, int ParentState,
2751                         const Function *Filter, const BasicBlock *Handler) {
2752   SEHUnwindMapEntry Entry;
2753   Entry.ToState = ParentState;
2754   Entry.IsFinally = false;
2755   Entry.Filter = Filter;
2756   Entry.Handler = Handler;
2757   FuncInfo.SEHUnwindMap.push_back(Entry);
2758   return FuncInfo.SEHUnwindMap.size() - 1;
2759 }
2760
2761 static int addSEHFinally(WinEHFuncInfo &FuncInfo, int ParentState,
2762                          const BasicBlock *Handler) {
2763   SEHUnwindMapEntry Entry;
2764   Entry.ToState = ParentState;
2765   Entry.IsFinally = true;
2766   Entry.Filter = nullptr;
2767   Entry.Handler = Handler;
2768   FuncInfo.SEHUnwindMap.push_back(Entry);
2769   return FuncInfo.SEHUnwindMap.size() - 1;
2770 }
2771
2772 static void calculateExplicitSEHStateNumbers(WinEHFuncInfo &FuncInfo,
2773                                              const BasicBlock &BB,
2774                                              int ParentState) {
2775   assert(BB.isEHPad());
2776   const Instruction *FirstNonPHI = BB.getFirstNonPHI();
2777   // All catchpad instructions will be handled when we process their
2778   // respective catchendpad instruction.
2779   if (isa<CatchPadInst>(FirstNonPHI))
2780     return;
2781
2782   if (isa<CatchEndPadInst>(FirstNonPHI)) {
2783     // Extract the filter function and the __except basic block and create a
2784     // state for them.
2785     SmallVector<const CatchPadInst *, 1> Handlers;
2786     findCatchPadsForCatchEndPad(&BB, Handlers);
2787     assert(Handlers.size() == 1 &&
2788            "SEH doesn't have multiple handlers per __try");
2789     const CatchPadInst *CPI = Handlers.front();
2790     const BasicBlock *CatchPadBB = CPI->getParent();
2791     const Constant *FilterOrNull =
2792         cast<Constant>(CPI->getArgOperand(0)->stripPointerCasts());
2793     const Function *Filter = dyn_cast<Function>(FilterOrNull);
2794     assert((Filter || FilterOrNull->isNullValue()) &&
2795            "unexpected filter value");
2796     int TryState = addSEHExcept(FuncInfo, ParentState, Filter, CatchPadBB);
2797
2798     // Everything in the __try block uses TryState as its parent state.
2799     FuncInfo.EHPadStateMap[CPI] = TryState;
2800     DEBUG(dbgs() << "Assigning state #" << TryState << " to BB "
2801                  << CatchPadBB->getName() << '\n');
2802     for (const BasicBlock *PredBlock : predecessors(CatchPadBB))
2803       if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
2804         calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, TryState);
2805
2806     // Everything in the __except block unwinds to ParentState, just like code
2807     // outside the __try.
2808     FuncInfo.EHPadStateMap[FirstNonPHI] = ParentState;
2809     DEBUG(dbgs() << "Assigning state #" << ParentState << " to BB "
2810                  << BB.getName() << '\n');
2811     for (const BasicBlock *PredBlock : predecessors(&BB))
2812       if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
2813         calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, ParentState);
2814   } else if (isa<CleanupPadInst>(FirstNonPHI)) {
2815     // A cleanup can have multiple exits; don't re-process after the first.
2816     if (FuncInfo.EHPadStateMap.count(FirstNonPHI))
2817       return;
2818     int CleanupState = addSEHFinally(FuncInfo, ParentState, &BB);
2819     FuncInfo.EHPadStateMap[FirstNonPHI] = CleanupState;
2820     DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB "
2821                  << BB.getName() << '\n');
2822     for (const BasicBlock *PredBlock : predecessors(&BB))
2823       if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
2824         calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, CleanupState);
2825   } else if (auto *CEPI = dyn_cast<CleanupEndPadInst>(FirstNonPHI)) {
2826     // Propagate ParentState to the cleanuppad in case it doesn't have
2827     // any cleanuprets.
2828     BasicBlock *CleanupBlock = CEPI->getCleanupPad()->getParent();
2829     calculateExplicitSEHStateNumbers(FuncInfo, *CleanupBlock, ParentState);
2830     // Anything unwinding through CleanupEndPadInst is in ParentState.
2831     FuncInfo.EHPadStateMap[FirstNonPHI] = ParentState;
2832     DEBUG(dbgs() << "Assigning state #" << ParentState << " to BB "
2833                  << BB.getName() << '\n');
2834     for (const BasicBlock *PredBlock : predecessors(&BB))
2835       if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
2836         calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, ParentState);
2837   } else if (isa<TerminatePadInst>(FirstNonPHI)) {
2838     report_fatal_error("Not yet implemented!");
2839   } else {
2840     llvm_unreachable("unexpected EH Pad!");
2841   }
2842 }
2843
2844 /// Check if the EH Pad unwinds to caller.  Cleanups are a little bit of a
2845 /// special case because we have to look at the cleanupret instruction that uses
2846 /// the cleanuppad.
2847 static bool doesEHPadUnwindToCaller(const Instruction *EHPad) {
2848   auto *CPI = dyn_cast<CleanupPadInst>(EHPad);
2849   if (!CPI)
2850     return EHPad->mayThrow();
2851
2852   // This cleanup does not return or unwind, so we say it unwinds to caller.
2853   if (CPI->use_empty())
2854     return true;
2855
2856   const Instruction *User = CPI->user_back();
2857   if (auto *CRI = dyn_cast<CleanupReturnInst>(User))
2858     return CRI->unwindsToCaller();
2859   return cast<CleanupEndPadInst>(User)->unwindsToCaller();
2860 }
2861
2862 void llvm::calculateSEHStateNumbers(const Function *Fn,
2863                                     WinEHFuncInfo &FuncInfo) {
2864   // Don't compute state numbers twice.
2865   if (!FuncInfo.SEHUnwindMap.empty())
2866     return;
2867
2868   for (const BasicBlock &BB : *Fn) {
2869     if (!BB.isEHPad() || !doesEHPadUnwindToCaller(BB.getFirstNonPHI()))
2870       continue;
2871     calculateExplicitSEHStateNumbers(FuncInfo, BB, -1);
2872   }
2873 }
2874
2875 void llvm::calculateWinCXXEHStateNumbers(const Function *Fn,
2876                                          WinEHFuncInfo &FuncInfo) {
2877   // Return if it's already been done.
2878   if (!FuncInfo.EHPadStateMap.empty())
2879     return;
2880
2881   for (const BasicBlock &BB : *Fn) {
2882     if (!BB.isEHPad())
2883       continue;
2884     if (BB.isLandingPad())
2885       report_fatal_error("MSVC C++ EH cannot use landingpads");
2886     const Instruction *FirstNonPHI = BB.getFirstNonPHI();
2887     if (!doesEHPadUnwindToCaller(FirstNonPHI))
2888       continue;
2889     calculateExplicitCXXStateNumbers(FuncInfo, BB, -1);
2890   }
2891 }
2892
2893 static int addClrEHHandler(WinEHFuncInfo &FuncInfo, int ParentState,
2894                            ClrHandlerType HandlerType, uint32_t TypeToken,
2895                            const BasicBlock *Handler) {
2896   ClrEHUnwindMapEntry Entry;
2897   Entry.Parent = ParentState;
2898   Entry.Handler = Handler;
2899   Entry.HandlerType = HandlerType;
2900   Entry.TypeToken = TypeToken;
2901   FuncInfo.ClrEHUnwindMap.push_back(Entry);
2902   return FuncInfo.ClrEHUnwindMap.size() - 1;
2903 }
2904
2905 void llvm::calculateClrEHStateNumbers(const Function *Fn,
2906                                       WinEHFuncInfo &FuncInfo) {
2907   // Return if it's already been done.
2908   if (!FuncInfo.EHPadStateMap.empty())
2909     return;
2910
2911   SmallVector<std::pair<const Instruction *, int>, 8> Worklist;
2912
2913   // Each pad needs to be able to refer to its parent, so scan the function
2914   // looking for top-level handlers and seed the worklist with them.
2915   for (const BasicBlock &BB : *Fn) {
2916     if (!BB.isEHPad())
2917       continue;
2918     if (BB.isLandingPad())
2919       report_fatal_error("CoreCLR EH cannot use landingpads");
2920     const Instruction *FirstNonPHI = BB.getFirstNonPHI();
2921     if (!doesEHPadUnwindToCaller(FirstNonPHI))
2922       continue;
2923     // queue this with sentinel parent state -1 to mean unwind to caller.
2924     Worklist.emplace_back(FirstNonPHI, -1);
2925   }
2926
2927   while (!Worklist.empty()) {
2928     const Instruction *Pad;
2929     int ParentState;
2930     std::tie(Pad, ParentState) = Worklist.pop_back_val();
2931
2932     int PredState;
2933     if (const CleanupEndPadInst *EndPad = dyn_cast<CleanupEndPadInst>(Pad)) {
2934       FuncInfo.EHPadStateMap[EndPad] = ParentState;
2935       // Queue the cleanuppad, in case it doesn't have a cleanupret.
2936       Worklist.emplace_back(EndPad->getCleanupPad(), ParentState);
2937       // Preds of the endpad should get the parent state.
2938       PredState = ParentState;
2939     } else if (const CleanupPadInst *Cleanup = dyn_cast<CleanupPadInst>(Pad)) {
2940       // A cleanup can have multiple exits; don't re-process after the first.
2941       if (FuncInfo.EHPadStateMap.count(Pad))
2942         continue;
2943       // CoreCLR personality uses arity to distinguish faults from finallies.
2944       const BasicBlock *PadBlock = Cleanup->getParent();
2945       ClrHandlerType HandlerType =
2946           (Cleanup->getNumOperands() ? ClrHandlerType::Fault
2947                                      : ClrHandlerType::Finally);
2948       int NewState =
2949           addClrEHHandler(FuncInfo, ParentState, HandlerType, 0, PadBlock);
2950       FuncInfo.EHPadStateMap[Cleanup] = NewState;
2951       // Propagate the new state to all preds of the cleanup
2952       PredState = NewState;
2953     } else if (const CatchEndPadInst *EndPad = dyn_cast<CatchEndPadInst>(Pad)) {
2954       FuncInfo.EHPadStateMap[EndPad] = ParentState;
2955       // Preds of the endpad should get the parent state.
2956       PredState = ParentState;
2957     } else if (const CatchPadInst *Catch = dyn_cast<CatchPadInst>(Pad)) {
2958       const BasicBlock *PadBlock = Catch->getParent();
2959       uint32_t TypeToken = static_cast<uint32_t>(
2960           cast<ConstantInt>(Catch->getArgOperand(0))->getZExtValue());
2961       int NewState = addClrEHHandler(FuncInfo, ParentState,
2962                                      ClrHandlerType::Catch, TypeToken, PadBlock);
2963       FuncInfo.EHPadStateMap[Catch] = NewState;
2964       // Preds of the catch get its state
2965       PredState = NewState;
2966     } else {
2967       llvm_unreachable("Unexpected EH pad");
2968     }
2969
2970     // Queue all predecessors with the given state
2971     for (const BasicBlock *Pred : predecessors(Pad->getParent())) {
2972       if ((Pred = getEHPadFromPredecessor(Pred)))
2973         Worklist.emplace_back(Pred->getFirstNonPHI(), PredState);
2974     }
2975   }
2976 }
2977
2978 void WinEHPrepare::replaceTerminatePadWithCleanup(Function &F) {
2979   if (Personality != EHPersonality::MSVC_CXX)
2980     return;
2981   for (BasicBlock &BB : F) {
2982     Instruction *First = BB.getFirstNonPHI();
2983     auto *TPI = dyn_cast<TerminatePadInst>(First);
2984     if (!TPI)
2985       continue;
2986
2987     if (TPI->getNumArgOperands() != 1)
2988       report_fatal_error(
2989           "Expected a unary terminatepad for MSVC C++ personalities!");
2990
2991     auto *TerminateFn = dyn_cast<Function>(TPI->getArgOperand(0));
2992     if (!TerminateFn)
2993       report_fatal_error("Function operand expected in terminatepad for MSVC "
2994                          "C++ personalities!");
2995
2996     // Insert the cleanuppad instruction.
2997     auto *CPI = CleanupPadInst::Create(
2998         BB.getContext(), {}, Twine("terminatepad.for.", BB.getName()), &BB);
2999
3000     // Insert the call to the terminate instruction.
3001     auto *CallTerminate = CallInst::Create(TerminateFn, {}, &BB);
3002     CallTerminate->setDoesNotThrow();
3003     CallTerminate->setDoesNotReturn();
3004     CallTerminate->setCallingConv(TerminateFn->getCallingConv());
3005
3006     // Insert a new terminator for the cleanuppad using the same successor as
3007     // the terminatepad.
3008     CleanupReturnInst::Create(CPI, TPI->getUnwindDest(), &BB);
3009
3010     // Let's remove the terminatepad now that we've inserted the new
3011     // instructions.
3012     TPI->eraseFromParent();
3013   }
3014 }
3015
3016 static void
3017 colorFunclets(Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks,
3018               std::map<BasicBlock *, std::set<BasicBlock *>> &BlockColors,
3019               std::map<BasicBlock *, std::set<BasicBlock *>> &FuncletBlocks,
3020               std::map<BasicBlock *, std::set<BasicBlock *>> &FuncletChildren) {
3021   SmallVector<std::pair<BasicBlock *, BasicBlock *>, 16> Worklist;
3022   BasicBlock *EntryBlock = &F.getEntryBlock();
3023
3024   // Build up the color map, which maps each block to its set of 'colors'.
3025   // For any block B, the "colors" of B are the set of funclets F (possibly
3026   // including a root "funclet" representing the main function), such that
3027   // F will need to directly contain B or a copy of B (where the term "directly
3028   // contain" is used to distinguish from being "transitively contained" in
3029   // a nested funclet).
3030   // Use a CFG walk driven by a worklist of (block, color) pairs.  The "color"
3031   // sets attached during this processing to a block which is the entry of some
3032   // funclet F is actually the set of F's parents -- i.e. the union of colors
3033   // of all predecessors of F's entry.  For all other blocks, the color sets
3034   // are as defined above.  A post-pass fixes up the block color map to reflect
3035   // the same sense of "color" for funclet entries as for other blocks.
3036
3037   Worklist.push_back({EntryBlock, EntryBlock});
3038
3039   while (!Worklist.empty()) {
3040     BasicBlock *Visiting;
3041     BasicBlock *Color;
3042     std::tie(Visiting, Color) = Worklist.pop_back_val();
3043     Instruction *VisitingHead = Visiting->getFirstNonPHI();
3044     if (VisitingHead->isEHPad() && !isa<CatchEndPadInst>(VisitingHead) &&
3045         !isa<CleanupEndPadInst>(VisitingHead)) {
3046       // Mark this as a funclet head as a member of itself.
3047       FuncletBlocks[Visiting].insert(Visiting);
3048       // Queue exits with the parent color.
3049       for (User *U : VisitingHead->users()) {
3050         if (auto *Exit = dyn_cast<TerminatorInst>(U)) {
3051           for (BasicBlock *Succ : successors(Exit->getParent()))
3052             if (BlockColors[Succ].insert(Color).second)
3053               Worklist.push_back({Succ, Color});
3054         }
3055       }
3056       // Handle CatchPad specially since its successors need different colors.
3057       if (CatchPadInst *CatchPad = dyn_cast<CatchPadInst>(VisitingHead)) {
3058         // Visit the normal successor with the color of the new EH pad, and
3059         // visit the unwind successor with the color of the parent.
3060         BasicBlock *NormalSucc = CatchPad->getNormalDest();
3061         if (BlockColors[NormalSucc].insert(Visiting).second) {
3062           Worklist.push_back({NormalSucc, Visiting});
3063         }
3064         BasicBlock *UnwindSucc = CatchPad->getUnwindDest();
3065         if (BlockColors[UnwindSucc].insert(Color).second) {
3066           Worklist.push_back({UnwindSucc, Color});
3067         }
3068         continue;
3069       }
3070       // Switch color to the current node, except for terminate pads which
3071       // have no bodies and only unwind successors and so need their successors
3072       // visited with the color of the parent.
3073       if (!isa<TerminatePadInst>(VisitingHead))
3074         Color = Visiting;
3075     } else {
3076       // Note that this is a member of the given color.
3077       FuncletBlocks[Color].insert(Visiting);
3078     }
3079
3080     TerminatorInst *Terminator = Visiting->getTerminator();
3081     if (isa<CleanupReturnInst>(Terminator) ||
3082         isa<CatchReturnInst>(Terminator) ||
3083         isa<CleanupEndPadInst>(Terminator)) {
3084       // These blocks' successors have already been queued with the parent
3085       // color.
3086       continue;
3087     }
3088     for (BasicBlock *Succ : successors(Visiting)) {
3089       if (isa<CatchEndPadInst>(Succ->getFirstNonPHI())) {
3090         // The catchendpad needs to be visited with the parent's color, not
3091         // the current color.  This will happen in the code above that visits
3092         // any catchpad unwind successor with the parent color, so we can
3093         // safely skip this successor here.
3094         continue;
3095       }
3096       if (BlockColors[Succ].insert(Color).second) {
3097         Worklist.push_back({Succ, Color});
3098       }
3099     }
3100   }
3101
3102   // The processing above actually accumulated the parent set for this
3103   // funclet into the color set for its entry; use the parent set to
3104   // populate the children map, and reset the color set to include just
3105   // the funclet itself (no instruction can target a funclet entry except on
3106   // that transitions to the child funclet).
3107   for (BasicBlock *FuncletEntry : EntryBlocks) {
3108     std::set<BasicBlock *> &ColorMapItem = BlockColors[FuncletEntry];
3109     for (BasicBlock *Parent : ColorMapItem)
3110       FuncletChildren[Parent].insert(FuncletEntry);
3111     ColorMapItem.clear();
3112     ColorMapItem.insert(FuncletEntry);
3113   }
3114 }
3115
3116 void WinEHPrepare::colorFunclets(Function &F,
3117                                  SmallVectorImpl<BasicBlock *> &EntryBlocks) {
3118   ::colorFunclets(F, EntryBlocks, BlockColors, FuncletBlocks, FuncletChildren);
3119 }
3120
3121 void llvm::calculateCatchReturnSuccessorColors(const Function *Fn,
3122                                                WinEHFuncInfo &FuncInfo) {
3123   SmallVector<LandingPadInst *, 4> LPads;
3124   SmallVector<ResumeInst *, 4> Resumes;
3125   SmallVector<BasicBlock *, 4> EntryBlocks;
3126   // colorFunclets needs the set of EntryBlocks, get them using
3127   // findExceptionalConstructs.
3128   bool ForExplicitEH = findExceptionalConstructs(const_cast<Function &>(*Fn),
3129                                                  LPads, Resumes, EntryBlocks);
3130   if (!ForExplicitEH)
3131     return;
3132
3133   std::map<BasicBlock *, std::set<BasicBlock *>> BlockColors;
3134   std::map<BasicBlock *, std::set<BasicBlock *>> FuncletBlocks;
3135   std::map<BasicBlock *, std::set<BasicBlock *>> FuncletChildren;
3136   // Figure out which basic blocks belong to which funclets.
3137   colorFunclets(const_cast<Function &>(*Fn), EntryBlocks, BlockColors,
3138                 FuncletBlocks, FuncletChildren);
3139
3140   // We need to find the catchret successors.  To do this, we must first find
3141   // all the catchpad funclets.
3142   for (auto &Funclet : FuncletBlocks) {
3143     // Figure out what kind of funclet we are looking at; We only care about
3144     // catchpads.
3145     BasicBlock *FuncletPadBB = Funclet.first;
3146     Instruction *FirstNonPHI = FuncletPadBB->getFirstNonPHI();
3147     auto *CatchPad = dyn_cast<CatchPadInst>(FirstNonPHI);
3148     if (!CatchPad)
3149       continue;
3150
3151     // The users of a catchpad are always catchrets.
3152     for (User *Exit : CatchPad->users()) {
3153       auto *CatchReturn = dyn_cast<CatchReturnInst>(Exit);
3154       if (!CatchReturn)
3155         continue;
3156       BasicBlock *CatchRetSuccessor = CatchReturn->getSuccessor();
3157       std::set<BasicBlock *> &SuccessorColors = BlockColors[CatchRetSuccessor];
3158       assert(SuccessorColors.size() == 1 && "Expected BB to be monochrome!");
3159       BasicBlock *Color = *SuccessorColors.begin();
3160       if (auto *CPI = dyn_cast<CatchPadInst>(Color->getFirstNonPHI()))
3161         Color = CPI->getNormalDest();
3162       // Record the catchret successor's funclet membership.
3163       FuncInfo.CatchRetSuccessorColorMap[CatchReturn] = Color;
3164     }
3165   }
3166 }
3167
3168 void WinEHPrepare::demotePHIsOnFunclets(Function &F) {
3169   // Strip PHI nodes off of EH pads.
3170   SmallVector<PHINode *, 16> PHINodes;
3171   for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) {
3172     BasicBlock *BB = &*FI++;
3173     if (!BB->isEHPad())
3174       continue;
3175     for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
3176       Instruction *I = &*BI++;
3177       auto *PN = dyn_cast<PHINode>(I);
3178       // Stop at the first non-PHI.
3179       if (!PN)
3180         break;
3181
3182       AllocaInst *SpillSlot = insertPHILoads(PN, F);
3183       if (SpillSlot)
3184         insertPHIStores(PN, SpillSlot);
3185
3186       PHINodes.push_back(PN);
3187     }
3188   }
3189
3190   for (auto *PN : PHINodes) {
3191     // There may be lingering uses on other EH PHIs being removed
3192     PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
3193     PN->eraseFromParent();
3194   }
3195 }
3196
3197 void WinEHPrepare::demoteUsesBetweenFunclets(Function &F) {
3198   // Turn all inter-funclet uses of a Value into loads and stores.
3199   for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) {
3200     BasicBlock *BB = &*FI++;
3201     std::set<BasicBlock *> &ColorsForBB = BlockColors[BB];
3202     for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
3203       Instruction *I = &*BI++;
3204       // Funclets are permitted to use static allocas.
3205       if (auto *AI = dyn_cast<AllocaInst>(I))
3206         if (AI->isStaticAlloca())
3207           continue;
3208
3209       demoteNonlocalUses(I, ColorsForBB, F);
3210     }
3211   }
3212 }
3213
3214 void WinEHPrepare::demoteArgumentUses(Function &F) {
3215   // Also demote function parameters used in funclets.
3216   std::set<BasicBlock *> &ColorsForEntry = BlockColors[&F.getEntryBlock()];
3217   for (Argument &Arg : F.args())
3218     demoteNonlocalUses(&Arg, ColorsForEntry, F);
3219 }
3220
3221 void WinEHPrepare::cloneCommonBlocks(
3222     Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks) {
3223   // We need to clone all blocks which belong to multiple funclets.  Values are
3224   // remapped throughout the funclet to propogate both the new instructions
3225   // *and* the new basic blocks themselves.
3226   for (BasicBlock *FuncletPadBB : EntryBlocks) {
3227     std::set<BasicBlock *> &BlocksInFunclet = FuncletBlocks[FuncletPadBB];
3228
3229     std::map<BasicBlock *, BasicBlock *> Orig2Clone;
3230     ValueToValueMapTy VMap;
3231     for (BasicBlock *BB : BlocksInFunclet) {
3232       std::set<BasicBlock *> &ColorsForBB = BlockColors[BB];
3233       // We don't need to do anything if the block is monochromatic.
3234       size_t NumColorsForBB = ColorsForBB.size();
3235       if (NumColorsForBB == 1)
3236         continue;
3237
3238       // Create a new basic block and copy instructions into it!
3239       BasicBlock *CBB =
3240           CloneBasicBlock(BB, VMap, Twine(".for.", FuncletPadBB->getName()));
3241       // Insert the clone immediately after the original to ensure determinism
3242       // and to keep the same relative ordering of any funclet's blocks.
3243       CBB->insertInto(&F, BB->getNextNode());
3244
3245       // Add basic block mapping.
3246       VMap[BB] = CBB;
3247
3248       // Record delta operations that we need to perform to our color mappings.
3249       Orig2Clone[BB] = CBB;
3250     }
3251
3252     // If nothing was cloned, we're done cloning in this funclet.
3253     if (Orig2Clone.empty())
3254       continue;
3255
3256     // Update our color mappings to reflect that one block has lost a color and
3257     // another has gained a color.
3258     for (auto &BBMapping : Orig2Clone) {
3259       BasicBlock *OldBlock = BBMapping.first;
3260       BasicBlock *NewBlock = BBMapping.second;
3261
3262       BlocksInFunclet.insert(NewBlock);
3263       BlockColors[NewBlock].insert(FuncletPadBB);
3264
3265       BlocksInFunclet.erase(OldBlock);
3266       BlockColors[OldBlock].erase(FuncletPadBB);
3267     }
3268
3269     // Loop over all of the instructions in this funclet, fixing up operand
3270     // references as we go.  This uses VMap to do all the hard work.
3271     for (BasicBlock *BB : BlocksInFunclet)
3272       // Loop over all instructions, fixing each one as we find it...
3273       for (Instruction &I : *BB)
3274         RemapInstruction(&I, VMap,
3275                          RF_IgnoreMissingEntries | RF_NoModuleLevelChanges);
3276
3277     // Check to see if SuccBB has PHI nodes. If so, we need to add entries to
3278     // the PHI nodes for NewBB now.
3279     for (auto &BBMapping : Orig2Clone) {
3280       BasicBlock *OldBlock = BBMapping.first;
3281       BasicBlock *NewBlock = BBMapping.second;
3282       for (BasicBlock *SuccBB : successors(NewBlock)) {
3283         for (Instruction &SuccI : *SuccBB) {
3284           auto *SuccPN = dyn_cast<PHINode>(&SuccI);
3285           if (!SuccPN)
3286             break;
3287
3288           // Ok, we have a PHI node.  Figure out what the incoming value was for
3289           // the OldBlock.
3290           int OldBlockIdx = SuccPN->getBasicBlockIndex(OldBlock);
3291           if (OldBlockIdx == -1)
3292             break;
3293           Value *IV = SuccPN->getIncomingValue(OldBlockIdx);
3294
3295           // Remap the value if necessary.
3296           if (auto *Inst = dyn_cast<Instruction>(IV)) {
3297             ValueToValueMapTy::iterator I = VMap.find(Inst);
3298             if (I != VMap.end())
3299               IV = I->second;
3300           }
3301
3302           SuccPN->addIncoming(IV, NewBlock);
3303         }
3304       }
3305     }
3306
3307     for (ValueToValueMapTy::value_type VT : VMap) {
3308       // If there were values defined in BB that are used outside the funclet,
3309       // then we now have to update all uses of the value to use either the
3310       // original value, the cloned value, or some PHI derived value.  This can
3311       // require arbitrary PHI insertion, of which we are prepared to do, clean
3312       // these up now.
3313       SmallVector<Use *, 16> UsesToRename;
3314
3315       auto *OldI = dyn_cast<Instruction>(const_cast<Value *>(VT.first));
3316       if (!OldI)
3317         continue;
3318       auto *NewI = cast<Instruction>(VT.second);
3319       // Scan all uses of this instruction to see if it is used outside of its
3320       // funclet, and if so, record them in UsesToRename.
3321       for (Use &U : OldI->uses()) {
3322         Instruction *UserI = cast<Instruction>(U.getUser());
3323         BasicBlock *UserBB = UserI->getParent();
3324         std::set<BasicBlock *> &ColorsForUserBB = BlockColors[UserBB];
3325         assert(!ColorsForUserBB.empty());
3326         if (ColorsForUserBB.size() > 1 ||
3327             *ColorsForUserBB.begin() != FuncletPadBB)
3328           UsesToRename.push_back(&U);
3329       }
3330
3331       // If there are no uses outside the block, we're done with this
3332       // instruction.
3333       if (UsesToRename.empty())
3334         continue;
3335
3336       // We found a use of OldI outside of the funclet.  Rename all uses of OldI
3337       // that are outside its funclet to be uses of the appropriate PHI node
3338       // etc.
3339       SSAUpdater SSAUpdate;
3340       SSAUpdate.Initialize(OldI->getType(), OldI->getName());
3341       SSAUpdate.AddAvailableValue(OldI->getParent(), OldI);
3342       SSAUpdate.AddAvailableValue(NewI->getParent(), NewI);
3343
3344       while (!UsesToRename.empty())
3345         SSAUpdate.RewriteUseAfterInsertions(*UsesToRename.pop_back_val());
3346     }
3347   }
3348 }
3349
3350 void WinEHPrepare::removeImplausibleTerminators(Function &F) {
3351   // Remove implausible terminators and replace them with UnreachableInst.
3352   for (auto &Funclet : FuncletBlocks) {
3353     BasicBlock *FuncletPadBB = Funclet.first;
3354     std::set<BasicBlock *> &BlocksInFunclet = Funclet.second;
3355     Instruction *FirstNonPHI = FuncletPadBB->getFirstNonPHI();
3356     auto *CatchPad = dyn_cast<CatchPadInst>(FirstNonPHI);
3357     auto *CleanupPad = dyn_cast<CleanupPadInst>(FirstNonPHI);
3358
3359     for (BasicBlock *BB : BlocksInFunclet) {
3360       TerminatorInst *TI = BB->getTerminator();
3361       // CatchPadInst and CleanupPadInst can't transfer control to a ReturnInst.
3362       bool IsUnreachableRet = isa<ReturnInst>(TI) && (CatchPad || CleanupPad);
3363       // The token consumed by a CatchReturnInst must match the funclet token.
3364       bool IsUnreachableCatchret = false;
3365       if (auto *CRI = dyn_cast<CatchReturnInst>(TI))
3366         IsUnreachableCatchret = CRI->getCatchPad() != CatchPad;
3367       // The token consumed by a CleanupReturnInst must match the funclet token.
3368       bool IsUnreachableCleanupret = false;
3369       if (auto *CRI = dyn_cast<CleanupReturnInst>(TI))
3370         IsUnreachableCleanupret = CRI->getCleanupPad() != CleanupPad;
3371       // The token consumed by a CleanupEndPadInst must match the funclet token.
3372       bool IsUnreachableCleanupendpad = false;
3373       if (auto *CEPI = dyn_cast<CleanupEndPadInst>(TI))
3374         IsUnreachableCleanupendpad = CEPI->getCleanupPad() != CleanupPad;
3375       if (IsUnreachableRet || IsUnreachableCatchret ||
3376           IsUnreachableCleanupret || IsUnreachableCleanupendpad) {
3377         // Loop through all of our successors and make sure they know that one
3378         // of their predecessors is going away.
3379         for (BasicBlock *SuccBB : TI->successors())
3380           SuccBB->removePredecessor(BB);
3381
3382         if (IsUnreachableCleanupendpad) {
3383           // We can't simply replace a cleanupendpad with unreachable, because
3384           // its predecessor edges are EH edges and unreachable is not an EH
3385           // pad.  Change all predecessors to the "unwind to caller" form.
3386           for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
3387                PI != PE;) {
3388             BasicBlock *Pred = *PI++;
3389             removeUnwindEdge(Pred);
3390           }
3391         }
3392
3393         new UnreachableInst(BB->getContext(), TI);
3394         TI->eraseFromParent();
3395       }
3396       // FIXME: Check for invokes/cleanuprets/cleanupendpads which unwind to
3397       // implausible catchendpads (i.e. catchendpad not in immediate parent
3398       // funclet).
3399     }
3400   }
3401 }
3402
3403 void WinEHPrepare::cleanupPreparedFunclets(Function &F) {
3404   // Clean-up some of the mess we made by removing useles PHI nodes, trivial
3405   // branches, etc.
3406   for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) {
3407     BasicBlock *BB = &*FI++;
3408     SimplifyInstructionsInBlock(BB);
3409     ConstantFoldTerminator(BB, /*DeleteDeadConditions=*/true);
3410     MergeBlockIntoPredecessor(BB);
3411   }
3412
3413   // We might have some unreachable blocks after cleaning up some impossible
3414   // control flow.
3415   removeUnreachableBlocks(F);
3416 }
3417
3418 void WinEHPrepare::verifyPreparedFunclets(Function &F) {
3419   // Recolor the CFG to verify that all is well.
3420   for (BasicBlock &BB : F) {
3421     size_t NumColors = BlockColors[&BB].size();
3422     assert(NumColors == 1 && "Expected monochromatic BB!");
3423     if (NumColors == 0)
3424       report_fatal_error("Uncolored BB!");
3425     if (NumColors > 1)
3426       report_fatal_error("Multicolor BB!");
3427     if (!DisableDemotion) {
3428       bool EHPadHasPHI = BB.isEHPad() && isa<PHINode>(BB.begin());
3429       assert(!EHPadHasPHI && "EH Pad still has a PHI!");
3430       if (EHPadHasPHI)
3431         report_fatal_error("EH Pad still has a PHI!");
3432     }
3433   }
3434 }
3435
3436 bool WinEHPrepare::prepareExplicitEH(
3437     Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks) {
3438   replaceTerminatePadWithCleanup(F);
3439
3440   // Determine which blocks are reachable from which funclet entries.
3441   colorFunclets(F, EntryBlocks);
3442
3443   if (!DisableDemotion) {
3444     demotePHIsOnFunclets(F);
3445
3446     demoteUsesBetweenFunclets(F);
3447
3448     demoteArgumentUses(F);
3449   }
3450
3451   cloneCommonBlocks(F, EntryBlocks);
3452
3453   if (!DisableCleanups) {
3454     removeImplausibleTerminators(F);
3455
3456     cleanupPreparedFunclets(F);
3457   }
3458
3459   verifyPreparedFunclets(F);
3460
3461   BlockColors.clear();
3462   FuncletBlocks.clear();
3463   FuncletChildren.clear();
3464
3465   return true;
3466 }
3467
3468 // TODO: Share loads when one use dominates another, or when a catchpad exit
3469 // dominates uses (needs dominators).
3470 AllocaInst *WinEHPrepare::insertPHILoads(PHINode *PN, Function &F) {
3471   BasicBlock *PHIBlock = PN->getParent();
3472   AllocaInst *SpillSlot = nullptr;
3473
3474   if (isa<CleanupPadInst>(PHIBlock->getFirstNonPHI())) {
3475     // Insert a load in place of the PHI and replace all uses.
3476     SpillSlot = new AllocaInst(PN->getType(), nullptr,
3477                                Twine(PN->getName(), ".wineh.spillslot"),
3478                                &F.getEntryBlock().front());
3479     Value *V = new LoadInst(SpillSlot, Twine(PN->getName(), ".wineh.reload"),
3480                             &*PHIBlock->getFirstInsertionPt());
3481     PN->replaceAllUsesWith(V);
3482     return SpillSlot;
3483   }
3484
3485   DenseMap<BasicBlock *, Value *> Loads;
3486   for (Value::use_iterator UI = PN->use_begin(), UE = PN->use_end();
3487        UI != UE;) {
3488     Use &U = *UI++;
3489     auto *UsingInst = cast<Instruction>(U.getUser());
3490     BasicBlock *UsingBB = UsingInst->getParent();
3491     if (UsingBB->isEHPad()) {
3492       // Use is on an EH pad phi.  Leave it alone; we'll insert loads and
3493       // stores for it separately.
3494       assert(isa<PHINode>(UsingInst));
3495       continue;
3496     }
3497     replaceUseWithLoad(PN, U, SpillSlot, Loads, F);
3498   }
3499   return SpillSlot;
3500 }
3501
3502 // TODO: improve store placement.  Inserting at def is probably good, but need
3503 // to be careful not to introduce interfering stores (needs liveness analysis).
3504 // TODO: identify related phi nodes that can share spill slots, and share them
3505 // (also needs liveness).
3506 void WinEHPrepare::insertPHIStores(PHINode *OriginalPHI,
3507                                    AllocaInst *SpillSlot) {
3508   // Use a worklist of (Block, Value) pairs -- the given Value needs to be
3509   // stored to the spill slot by the end of the given Block.
3510   SmallVector<std::pair<BasicBlock *, Value *>, 4> Worklist;
3511
3512   Worklist.push_back({OriginalPHI->getParent(), OriginalPHI});
3513
3514   while (!Worklist.empty()) {
3515     BasicBlock *EHBlock;
3516     Value *InVal;
3517     std::tie(EHBlock, InVal) = Worklist.pop_back_val();
3518
3519     PHINode *PN = dyn_cast<PHINode>(InVal);
3520     if (PN && PN->getParent() == EHBlock) {
3521       // The value is defined by another PHI we need to remove, with no room to
3522       // insert a store after the PHI, so each predecessor needs to store its
3523       // incoming value.
3524       for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i) {
3525         Value *PredVal = PN->getIncomingValue(i);
3526
3527         // Undef can safely be skipped.
3528         if (isa<UndefValue>(PredVal))
3529           continue;
3530
3531         insertPHIStore(PN->getIncomingBlock(i), PredVal, SpillSlot, Worklist);
3532       }
3533     } else {
3534       // We need to store InVal, which dominates EHBlock, but can't put a store
3535       // in EHBlock, so need to put stores in each predecessor.
3536       for (BasicBlock *PredBlock : predecessors(EHBlock)) {
3537         insertPHIStore(PredBlock, InVal, SpillSlot, Worklist);
3538       }
3539     }
3540   }
3541 }
3542
3543 void WinEHPrepare::insertPHIStore(
3544     BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot,
3545     SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist) {
3546
3547   if (PredBlock->isEHPad() &&
3548       !isa<CleanupPadInst>(PredBlock->getFirstNonPHI())) {
3549     // Pred is unsplittable, so we need to queue it on the worklist.
3550     Worklist.push_back({PredBlock, PredVal});
3551     return;
3552   }
3553
3554   // Otherwise, insert the store at the end of the basic block.
3555   new StoreInst(PredVal, SpillSlot, PredBlock->getTerminator());
3556 }
3557
3558 // TODO: Share loads for same-funclet uses (requires dominators if funclets
3559 // aren't properly nested).
3560 void WinEHPrepare::demoteNonlocalUses(Value *V,
3561                                       std::set<BasicBlock *> &ColorsForBB,
3562                                       Function &F) {
3563   // Tokens can only be used non-locally due to control flow involving
3564   // unreachable edges.  Don't try to demote the token usage, we'll simply
3565   // delete the cloned user later.
3566   if (isa<CatchPadInst>(V) || isa<CleanupPadInst>(V))
3567     return;
3568
3569   DenseMap<BasicBlock *, Value *> Loads;
3570   AllocaInst *SpillSlot = nullptr;
3571   for (Value::use_iterator UI = V->use_begin(), UE = V->use_end(); UI != UE;) {
3572     Use &U = *UI++;
3573     auto *UsingInst = cast<Instruction>(U.getUser());
3574     BasicBlock *UsingBB = UsingInst->getParent();
3575
3576     // Is the Use inside a block which is colored the same as the Def?
3577     // If so, we don't need to escape the Def because we will clone
3578     // ourselves our own private copy.
3579     std::set<BasicBlock *> &ColorsForUsingBB = BlockColors[UsingBB];
3580     if (ColorsForUsingBB == ColorsForBB)
3581       continue;
3582
3583     replaceUseWithLoad(V, U, SpillSlot, Loads, F);
3584   }
3585   if (SpillSlot) {
3586     // Insert stores of the computed value into the stack slot.
3587     // We have to be careful if I is an invoke instruction,
3588     // because we can't insert the store AFTER the terminator instruction.
3589     BasicBlock::iterator InsertPt;
3590     if (isa<Argument>(V)) {
3591       InsertPt = F.getEntryBlock().getTerminator()->getIterator();
3592     } else if (isa<TerminatorInst>(V)) {
3593       auto *II = cast<InvokeInst>(V);
3594       // We cannot demote invoke instructions to the stack if their normal
3595       // edge is critical. Therefore, split the critical edge and create a
3596       // basic block into which the store can be inserted.
3597       if (!II->getNormalDest()->getSinglePredecessor()) {
3598         unsigned SuccNum =
3599             GetSuccessorNumber(II->getParent(), II->getNormalDest());
3600         assert(isCriticalEdge(II, SuccNum) && "Expected a critical edge!");
3601         BasicBlock *NewBlock = SplitCriticalEdge(II, SuccNum);
3602         assert(NewBlock && "Unable to split critical edge.");
3603         // Update the color mapping for the newly split edge.
3604         std::set<BasicBlock *> &ColorsForUsingBB = BlockColors[II->getParent()];
3605         BlockColors[NewBlock] = ColorsForUsingBB;
3606         for (BasicBlock *FuncletPad : ColorsForUsingBB)
3607           FuncletBlocks[FuncletPad].insert(NewBlock);
3608       }
3609       InsertPt = II->getNormalDest()->getFirstInsertionPt();
3610     } else {
3611       InsertPt = cast<Instruction>(V)->getIterator();
3612       ++InsertPt;
3613       // Don't insert before PHI nodes or EH pad instrs.
3614       for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt)
3615         ;
3616     }
3617     new StoreInst(V, SpillSlot, &*InsertPt);
3618   }
3619 }
3620
3621 void WinEHPrepare::replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
3622                                       DenseMap<BasicBlock *, Value *> &Loads,
3623                                       Function &F) {
3624   // Lazilly create the spill slot.
3625   if (!SpillSlot)
3626     SpillSlot = new AllocaInst(V->getType(), nullptr,
3627                                Twine(V->getName(), ".wineh.spillslot"),
3628                                &F.getEntryBlock().front());
3629
3630   auto *UsingInst = cast<Instruction>(U.getUser());
3631   if (auto *UsingPHI = dyn_cast<PHINode>(UsingInst)) {
3632     // If this is a PHI node, we can't insert a load of the value before
3633     // the use.  Instead insert the load in the predecessor block
3634     // corresponding to the incoming value.
3635     //
3636     // Note that if there are multiple edges from a basic block to this
3637     // PHI node that we cannot have multiple loads.  The problem is that
3638     // the resulting PHI node will have multiple values (from each load)
3639     // coming in from the same block, which is illegal SSA form.
3640     // For this reason, we keep track of and reuse loads we insert.
3641     BasicBlock *IncomingBlock = UsingPHI->getIncomingBlock(U);
3642     if (auto *CatchRet =
3643             dyn_cast<CatchReturnInst>(IncomingBlock->getTerminator())) {
3644       // Putting a load above a catchret and use on the phi would still leave
3645       // a cross-funclet def/use.  We need to split the edge, change the
3646       // catchret to target the new block, and put the load there.
3647       BasicBlock *PHIBlock = UsingInst->getParent();
3648       BasicBlock *NewBlock = SplitEdge(IncomingBlock, PHIBlock);
3649       // SplitEdge gives us:
3650       //   IncomingBlock:
3651       //     ...
3652       //     br label %NewBlock
3653       //   NewBlock:
3654       //     catchret label %PHIBlock
3655       // But we need:
3656       //   IncomingBlock:
3657       //     ...
3658       //     catchret label %NewBlock
3659       //   NewBlock:
3660       //     br label %PHIBlock
3661       // So move the terminators to each others' blocks and swap their
3662       // successors.
3663       BranchInst *Goto = cast<BranchInst>(IncomingBlock->getTerminator());
3664       Goto->removeFromParent();
3665       CatchRet->removeFromParent();
3666       IncomingBlock->getInstList().push_back(CatchRet);
3667       NewBlock->getInstList().push_back(Goto);
3668       Goto->setSuccessor(0, PHIBlock);
3669       CatchRet->setSuccessor(NewBlock);
3670       // Update the color mapping for the newly split edge.
3671       std::set<BasicBlock *> &ColorsForPHIBlock = BlockColors[PHIBlock];
3672       BlockColors[NewBlock] = ColorsForPHIBlock;
3673       for (BasicBlock *FuncletPad : ColorsForPHIBlock)
3674         FuncletBlocks[FuncletPad].insert(NewBlock);
3675       // Treat the new block as incoming for load insertion.
3676       IncomingBlock = NewBlock;
3677     }
3678     Value *&Load = Loads[IncomingBlock];
3679     // Insert the load into the predecessor block
3680     if (!Load)
3681       Load = new LoadInst(SpillSlot, Twine(V->getName(), ".wineh.reload"),
3682                           /*Volatile=*/false, IncomingBlock->getTerminator());
3683
3684     U.set(Load);
3685   } else {
3686     // Reload right before the old use.
3687     auto *Load = new LoadInst(SpillSlot, Twine(V->getName(), ".wineh.reload"),
3688                               /*Volatile=*/false, UsingInst);
3689     U.set(Load);
3690   }
3691 }
3692
3693 void WinEHFuncInfo::addIPToStateRange(const BasicBlock *PadBB,
3694                                       MCSymbol *InvokeBegin,
3695                                       MCSymbol *InvokeEnd) {
3696   assert(PadBB->isEHPad() && EHPadStateMap.count(PadBB->getFirstNonPHI()) &&
3697          "should get EH pad BB with precomputed state");
3698   InvokeToStateMap[InvokeBegin] =
3699       std::make_pair(EHPadStateMap[PadBB->getFirstNonPHI()], InvokeEnd);
3700 }