Replace llvm.frameallocate with llvm.frameescape
[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. It snifs the personality function to see which kind of
12 // preparation is necessary. If the personality function uses the Itanium LSDA,
13 // this pass delegates to the DWARF EH preparation pass.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/ADT/MapVector.h"
19 #include "llvm/ADT/TinyPtrVector.h"
20 #include "llvm/Analysis/LibCallSemantics.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/IRBuilder.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/IntrinsicInst.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/IR/PatternMatch.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Transforms/Utils/Cloning.h"
29 #include "llvm/Transforms/Utils/Local.h"
30 #include <memory>
31
32 using namespace llvm;
33 using namespace llvm::PatternMatch;
34
35 #define DEBUG_TYPE "winehprepare"
36
37 namespace {
38
39 // This map is used to model frame variable usage during outlining, to
40 // construct a structure type to hold the frame variables in a frame
41 // allocation block, and to remap the frame variable allocas (including
42 // spill locations as needed) to GEPs that get the variable from the
43 // frame allocation structure.
44 typedef MapVector<Value *, TinyPtrVector<AllocaInst *>> FrameVarInfoMap;
45
46 class WinEHPrepare : public FunctionPass {
47   std::unique_ptr<FunctionPass> DwarfPrepare;
48
49   enum HandlerType { Catch, Cleanup };
50
51 public:
52   static char ID; // Pass identification, replacement for typeid.
53   WinEHPrepare(const TargetMachine *TM = nullptr)
54       : FunctionPass(ID), DwarfPrepare(createDwarfEHPass(TM)) {}
55
56   bool runOnFunction(Function &Fn) override;
57
58   bool doFinalization(Module &M) override;
59
60   void getAnalysisUsage(AnalysisUsage &AU) const override;
61
62   const char *getPassName() const override {
63     return "Windows exception handling preparation";
64   }
65
66 private:
67   bool prepareCPPEHHandlers(Function &F,
68                             SmallVectorImpl<LandingPadInst *> &LPads);
69   bool outlineHandler(HandlerType CatchOrCleanup, Function *SrcFn,
70                       Constant *SelectorType, LandingPadInst *LPad,
71                       FrameVarInfoMap &VarInfo);
72 };
73
74 class WinEHFrameVariableMaterializer : public ValueMaterializer {
75 public:
76   WinEHFrameVariableMaterializer(Function *OutlinedFn,
77                                  FrameVarInfoMap &FrameVarInfo);
78   ~WinEHFrameVariableMaterializer() {}
79
80   virtual Value *materializeValueFor(Value *V) override;
81
82 private:
83   FrameVarInfoMap &FrameVarInfo;
84   IRBuilder<> Builder;
85 };
86
87 class WinEHCloningDirectorBase : public CloningDirector {
88 public:
89   WinEHCloningDirectorBase(LandingPadInst *LPI, Function *HandlerFn,
90                            FrameVarInfoMap &VarInfo)
91       : LPI(LPI), Materializer(HandlerFn, VarInfo),
92         SelectorIDType(Type::getInt32Ty(LPI->getContext())),
93         Int8PtrType(Type::getInt8PtrTy(LPI->getContext())) {}
94
95   CloningAction handleInstruction(ValueToValueMapTy &VMap,
96                                   const Instruction *Inst,
97                                   BasicBlock *NewBB) override;
98
99   virtual CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
100                                          const Instruction *Inst,
101                                          BasicBlock *NewBB) = 0;
102   virtual CloningAction handleEndCatch(ValueToValueMapTy &VMap,
103                                        const Instruction *Inst,
104                                        BasicBlock *NewBB) = 0;
105   virtual CloningAction handleTypeIdFor(ValueToValueMapTy &VMap,
106                                         const Instruction *Inst,
107                                         BasicBlock *NewBB) = 0;
108   virtual CloningAction handleResume(ValueToValueMapTy &VMap,
109                                      const ResumeInst *Resume,
110                                      BasicBlock *NewBB) = 0;
111
112   ValueMaterializer *getValueMaterializer() override { return &Materializer; }
113
114 protected:
115   LandingPadInst *LPI;
116   WinEHFrameVariableMaterializer Materializer;
117   Type *SelectorIDType;
118   Type *Int8PtrType;
119
120   const Value *ExtractedEHPtr;
121   const Value *ExtractedSelector;
122   const Value *EHPtrStoreAddr;
123   const Value *SelectorStoreAddr;
124 };
125
126 class WinEHCatchDirector : public WinEHCloningDirectorBase {
127 public:
128   WinEHCatchDirector(LandingPadInst *LPI, Function *CatchFn, Value *Selector,
129                      FrameVarInfoMap &VarInfo)
130       : WinEHCloningDirectorBase(LPI, CatchFn, VarInfo),
131         CurrentSelector(Selector->stripPointerCasts()) {}
132
133   CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
134                                  const Instruction *Inst,
135                                  BasicBlock *NewBB) override;
136   CloningAction handleEndCatch(ValueToValueMapTy &VMap, const Instruction *Inst,
137                                BasicBlock *NewBB) override;
138   CloningAction handleTypeIdFor(ValueToValueMapTy &VMap,
139                                 const Instruction *Inst,
140                                 BasicBlock *NewBB) override;
141   CloningAction handleResume(ValueToValueMapTy &VMap, const ResumeInst *Resume,
142                              BasicBlock *NewBB) override;
143
144 private:
145   Value *CurrentSelector;
146 };
147
148 class WinEHCleanupDirector : public WinEHCloningDirectorBase {
149 public:
150   WinEHCleanupDirector(LandingPadInst *LPI, Function *CleanupFn,
151                        FrameVarInfoMap &VarInfo)
152       : WinEHCloningDirectorBase(LPI, CleanupFn, VarInfo) {}
153
154   CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
155                                  const Instruction *Inst,
156                                  BasicBlock *NewBB) override;
157   CloningAction handleEndCatch(ValueToValueMapTy &VMap, const Instruction *Inst,
158                                BasicBlock *NewBB) override;
159   CloningAction handleTypeIdFor(ValueToValueMapTy &VMap,
160                                 const Instruction *Inst,
161                                 BasicBlock *NewBB) override;
162   CloningAction handleResume(ValueToValueMapTy &VMap, const ResumeInst *Resume,
163                              BasicBlock *NewBB) override;
164 };
165
166 } // end anonymous namespace
167
168 char WinEHPrepare::ID = 0;
169 INITIALIZE_TM_PASS(WinEHPrepare, "winehprepare", "Prepare Windows exceptions",
170                    false, false)
171
172 FunctionPass *llvm::createWinEHPass(const TargetMachine *TM) {
173   return new WinEHPrepare(TM);
174 }
175
176 static bool isMSVCPersonality(EHPersonality Pers) {
177   return Pers == EHPersonality::MSVC_Win64SEH ||
178          Pers == EHPersonality::MSVC_CXX;
179 }
180
181 bool WinEHPrepare::runOnFunction(Function &Fn) {
182   SmallVector<LandingPadInst *, 4> LPads;
183   SmallVector<ResumeInst *, 4> Resumes;
184   for (BasicBlock &BB : Fn) {
185     if (auto *LP = BB.getLandingPadInst())
186       LPads.push_back(LP);
187     if (auto *Resume = dyn_cast<ResumeInst>(BB.getTerminator()))
188       Resumes.push_back(Resume);
189   }
190
191   // No need to prepare functions that lack landing pads.
192   if (LPads.empty())
193     return false;
194
195   // Classify the personality to see what kind of preparation we need.
196   EHPersonality Pers = classifyEHPersonality(LPads.back()->getPersonalityFn());
197
198   // Delegate through to the DWARF pass if this is unrecognized.
199   if (!isMSVCPersonality(Pers))
200     return DwarfPrepare->runOnFunction(Fn);
201
202   // FIXME: This only returns true if the C++ EH handlers were outlined.
203   //        When that code is complete, it should always return whatever
204   //        prepareCPPEHHandlers returns.
205   if (Pers == EHPersonality::MSVC_CXX && prepareCPPEHHandlers(Fn, LPads))
206     return true;
207
208   // FIXME: SEH Cleanups are unimplemented. Replace them with unreachable.
209   if (Resumes.empty())
210     return false;
211
212   for (ResumeInst *Resume : Resumes) {
213     IRBuilder<>(Resume).CreateUnreachable();
214     Resume->eraseFromParent();
215   }
216
217   return true;
218 }
219
220 bool WinEHPrepare::doFinalization(Module &M) {
221   return DwarfPrepare->doFinalization(M);
222 }
223
224 void WinEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const {
225   DwarfPrepare->getAnalysisUsage(AU);
226 }
227
228 bool WinEHPrepare::prepareCPPEHHandlers(
229     Function &F, SmallVectorImpl<LandingPadInst *> &LPads) {
230   // These containers are used to re-map frame variables that are used in
231   // outlined catch and cleanup handlers.  They will be populated as the
232   // handlers are outlined.
233   FrameVarInfoMap FrameVarInfo;
234
235   bool HandlersOutlined = false;
236
237   for (LandingPadInst *LPad : LPads) {
238     // Look for evidence that this landingpad has already been processed.
239     bool LPadHasActionList = false;
240     BasicBlock *LPadBB = LPad->getParent();
241     for (Instruction &Inst : LPadBB->getInstList()) {
242       // FIXME: Make this an intrinsic.
243       if (auto *Call = dyn_cast<CallInst>(&Inst))
244         if (Call->getCalledFunction()->getName() == "llvm.eh.actions") {
245           LPadHasActionList = true;
246           break;
247         }
248     }
249
250     // If we've already outlined the handlers for this landingpad,
251     // there's nothing more to do here.
252     if (LPadHasActionList)
253       continue;
254
255     for (unsigned Idx = 0, NumClauses = LPad->getNumClauses(); Idx < NumClauses;
256          ++Idx) {
257       if (LPad->isCatch(Idx)) {
258         // Create a new instance of the handler data structure in the
259         // HandlerData vector.
260         bool Outlined = outlineHandler(Catch, &F, LPad->getClause(Idx), LPad,
261                                        FrameVarInfo);
262         if (Outlined) {
263           HandlersOutlined = true;
264         }
265       } // End if (isCatch)
266     }   // End for each clause
267
268     // FIXME: This only handles the simple case where there is a 1:1
269     //        correspondence between landing pad and cleanup blocks.
270     //        It does not handle cases where there are catch blocks between
271     //        cleanup blocks or the case where a cleanup block is shared by
272     //        multiple landing pads.  Those cases will be supported later
273     //        when landing pad block analysis is added.
274     if (LPad->isCleanup()) {
275       bool Outlined =
276           outlineHandler(Cleanup, &F, nullptr, LPad, FrameVarInfo);
277       if (Outlined) {
278         HandlersOutlined = true;
279       }
280     }
281   } // End for each landingpad
282
283   // If nothing got outlined, there is no more processing to be done.
284   if (!HandlersOutlined)
285     return false;
286
287   // FIXME: We will replace the landingpad bodies with llvm.eh.actions
288   //        calls and indirect branches here and then delete blocks
289   //        which are no longer reachable.  That will get rid of the
290   //        handlers that we have outlined.  There is code below
291   //        that looks for allocas with no uses in the parent function.
292   //        That will only happen after the pruning is implemented.
293
294   Module *M = F.getParent();
295   LLVMContext &Context = M->getContext();
296   BasicBlock *Entry = &F.getEntryBlock();
297   IRBuilder<> Builder(F.getParent()->getContext());
298   Builder.SetInsertPoint(Entry->getFirstInsertionPt());
299
300   Function *FrameEscapeFn =
301       Intrinsic::getDeclaration(M, Intrinsic::frameescape);
302   Function *RecoverFrameFn =
303       Intrinsic::getDeclaration(M, Intrinsic::framerecover);
304   Type *Int8PtrType = Type::getInt8PtrTy(Context);
305   Type *Int32Type = Type::getInt32Ty(Context);
306
307   // Finally, replace all of the temporary allocas for frame variables used in
308   // the outlined handlers with calls to llvm.framerecover.
309   BasicBlock::iterator II = Entry->getFirstInsertionPt();
310   Instruction *AllocaInsertPt = II;
311   SmallVector<Value *, 8> AllocasToEscape;
312   for (auto &VarInfoEntry : FrameVarInfo) {
313     Value *ParentVal = VarInfoEntry.first;
314     TinyPtrVector<AllocaInst *> &Allocas = VarInfoEntry.second;
315
316     // If the mapped value isn't already an alloca, we need to spill it if it
317     // is a computed value or copy it if it is an argument.
318     AllocaInst *ParentAlloca = dyn_cast<AllocaInst>(ParentVal);
319     if (!ParentAlloca) {
320       if (auto *Arg = dyn_cast<Argument>(ParentVal)) {
321         // Lower this argument to a copy and then demote that to the stack.
322         // We can't just use the argument location because the handler needs
323         // it to be in the frame allocation block.
324         // Use 'select i8 true, %arg, undef' to simulate a 'no-op' instruction.
325         Value *TrueValue = ConstantInt::getTrue(Context);
326         Value *UndefValue = UndefValue::get(Arg->getType());
327         Instruction *SI =
328             SelectInst::Create(TrueValue, Arg, UndefValue,
329                                Arg->getName() + ".tmp", AllocaInsertPt);
330         Arg->replaceAllUsesWith(SI);
331         // Reset the select operand, because it was clobbered by the RAUW above.
332         SI->setOperand(1, Arg);
333         ParentAlloca = DemoteRegToStack(*SI, true, SI);
334       } else if (auto *PN = dyn_cast<PHINode>(ParentVal)) {
335         ParentAlloca = DemotePHIToStack(PN, AllocaInsertPt);
336       } else {
337         Instruction *ParentInst = cast<Instruction>(ParentVal);
338         ParentAlloca = DemoteRegToStack(*ParentInst, true, ParentInst);
339       }
340     }
341
342     // If the parent alloca is no longer used and only one of the handlers used
343     // it, erase the parent and leave the copy in the outlined handler.
344     if (ParentAlloca->getNumUses() == 0 && Allocas.size() == 1) {
345       ParentAlloca->eraseFromParent();
346       continue;
347     }
348
349     // Add this alloca to the list of things to escape.
350     AllocasToEscape.push_back(ParentAlloca);
351
352     // Next replace all outlined allocas that are mapped to it.
353     for (AllocaInst *TempAlloca : Allocas) {
354       Function *HandlerFn = TempAlloca->getParent()->getParent();
355       // FIXME: Sink this GEP into the blocks where it is used.
356       Builder.SetInsertPoint(TempAlloca);
357       Builder.SetCurrentDebugLocation(TempAlloca->getDebugLoc());
358       Value *RecoverArgs[] = {
359           Builder.CreateBitCast(&F, Int8PtrType, ""),
360           &(HandlerFn->getArgumentList().back()),
361           llvm::ConstantInt::get(Int32Type, AllocasToEscape.size() - 1)};
362       Value *RecoveredAlloca =
363           Builder.CreateCall(RecoverFrameFn, RecoverArgs);
364       // Add a pointer bitcast if the alloca wasn't an i8.
365       if (RecoveredAlloca->getType() != TempAlloca->getType()) {
366         RecoveredAlloca->setName(Twine(TempAlloca->getName()) + ".i8");
367         RecoveredAlloca =
368             Builder.CreateBitCast(RecoveredAlloca, TempAlloca->getType());
369       }
370       TempAlloca->replaceAllUsesWith(RecoveredAlloca);
371       TempAlloca->removeFromParent();
372       RecoveredAlloca->takeName(TempAlloca);
373       delete TempAlloca;
374     }
375   }   // End for each FrameVarInfo entry.
376
377   // Insert 'call void (...)* @llvm.frameescape(...)' at the end of the entry
378   // block.
379   Builder.SetInsertPoint(&F.getEntryBlock().back());
380   Builder.CreateCall(FrameEscapeFn, AllocasToEscape);
381
382   return HandlersOutlined;
383 }
384
385 bool WinEHPrepare::outlineHandler(HandlerType CatchOrCleanup, Function *SrcFn,
386                                   Constant *SelectorType, LandingPadInst *LPad,
387                                   FrameVarInfoMap &VarInfo) {
388   Module *M = SrcFn->getParent();
389   LLVMContext &Context = M->getContext();
390
391   // Create a new function to receive the handler contents.
392   Type *Int8PtrType = Type::getInt8PtrTy(Context);
393   std::vector<Type *> ArgTys;
394   ArgTys.push_back(Int8PtrType);
395   ArgTys.push_back(Int8PtrType);
396   Function *Handler;
397   if (CatchOrCleanup == Catch) {
398     FunctionType *FnType = FunctionType::get(Int8PtrType, ArgTys, false);
399     Handler = Function::Create(FnType, GlobalVariable::InternalLinkage,
400                                SrcFn->getName() + ".catch", M);
401   } else {
402     FunctionType *FnType =
403         FunctionType::get(Type::getVoidTy(Context), ArgTys, false);
404     Handler = Function::Create(FnType, GlobalVariable::InternalLinkage,
405                                SrcFn->getName() + ".cleanup", M);
406   }
407
408   // Generate a standard prolog to setup the frame recovery structure.
409   IRBuilder<> Builder(Context);
410   BasicBlock *Entry = BasicBlock::Create(Context, "entry");
411   Handler->getBasicBlockList().push_front(Entry);
412   Builder.SetInsertPoint(Entry);
413   Builder.SetCurrentDebugLocation(LPad->getDebugLoc());
414
415   std::unique_ptr<WinEHCloningDirectorBase> Director;
416
417   if (CatchOrCleanup == Catch) {
418     Director.reset(
419         new WinEHCatchDirector(LPad, Handler, SelectorType, VarInfo));
420   } else {
421     Director.reset(new WinEHCleanupDirector(LPad, Handler, VarInfo));
422   }
423
424   ValueToValueMapTy VMap;
425
426   // FIXME: Map other values referenced in the filter handler.
427
428   SmallVector<ReturnInst *, 8> Returns;
429   ClonedCodeInfo InlinedFunctionInfo;
430
431   BasicBlock::iterator II = LPad;
432
433   CloneAndPruneIntoFromInst(
434       Handler, SrcFn, ++II, VMap,
435       /*ModuleLevelChanges=*/false, Returns, "", &InlinedFunctionInfo,
436       &SrcFn->getParent()->getDataLayout(), Director.get());
437
438   // Move all the instructions in the first cloned block into our entry block.
439   BasicBlock *FirstClonedBB = std::next(Function::iterator(Entry));
440   Entry->getInstList().splice(Entry->end(), FirstClonedBB->getInstList());
441   FirstClonedBB->eraseFromParent();
442
443   return true;
444 }
445
446 CloningDirector::CloningAction WinEHCloningDirectorBase::handleInstruction(
447     ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
448   // Intercept instructions which extract values from the landing pad aggregate.
449   if (auto *Extract = dyn_cast<ExtractValueInst>(Inst)) {
450     if (Extract->getAggregateOperand() == LPI) {
451       assert(Extract->getNumIndices() == 1 &&
452              "Unexpected operation: extracting both landing pad values");
453       assert((*(Extract->idx_begin()) == 0 || *(Extract->idx_begin()) == 1) &&
454              "Unexpected operation: extracting an unknown landing pad element");
455
456       if (*(Extract->idx_begin()) == 0) {
457         // Element 0 doesn't directly corresponds to anything in the WinEH
458         // scheme.
459         // It will be stored to a memory location, then later loaded and finally
460         // the loaded value will be used as the argument to an
461         // llvm.eh.begincatch
462         // call.  We're tracking it here so that we can skip the store and load.
463         ExtractedEHPtr = Inst;
464       } else {
465         // Element 1 corresponds to the filter selector.  We'll map it to 1 for
466         // matching purposes, but it will also probably be stored to memory and
467         // reloaded, so we need to track the instuction so that we can map the
468         // loaded value too.
469         VMap[Inst] = ConstantInt::get(SelectorIDType, 1);
470         ExtractedSelector = Inst;
471       }
472
473       // Tell the caller not to clone this instruction.
474       return CloningDirector::SkipInstruction;
475     }
476     // Other extract value instructions just get cloned.
477     return CloningDirector::CloneInstruction;
478   }
479
480   if (auto *Store = dyn_cast<StoreInst>(Inst)) {
481     // Look for and suppress stores of the extracted landingpad values.
482     const Value *StoredValue = Store->getValueOperand();
483     if (StoredValue == ExtractedEHPtr) {
484       EHPtrStoreAddr = Store->getPointerOperand();
485       return CloningDirector::SkipInstruction;
486     }
487     if (StoredValue == ExtractedSelector) {
488       SelectorStoreAddr = Store->getPointerOperand();
489       return CloningDirector::SkipInstruction;
490     }
491
492     // Any other store just gets cloned.
493     return CloningDirector::CloneInstruction;
494   }
495
496   if (auto *Load = dyn_cast<LoadInst>(Inst)) {
497     // Look for loads of (previously suppressed) landingpad values.
498     // The EHPtr load can be ignored (it should only be used as
499     // an argument to llvm.eh.begincatch), but the selector value
500     // needs to be mapped to a constant value of 1 to be used to
501     // simplify the branching to always flow to the current handler.
502     const Value *LoadAddr = Load->getPointerOperand();
503     if (LoadAddr == EHPtrStoreAddr) {
504       VMap[Inst] = UndefValue::get(Int8PtrType);
505       return CloningDirector::SkipInstruction;
506     }
507     if (LoadAddr == SelectorStoreAddr) {
508       VMap[Inst] = ConstantInt::get(SelectorIDType, 1);
509       return CloningDirector::SkipInstruction;
510     }
511
512     // Any other loads just get cloned.
513     return CloningDirector::CloneInstruction;
514   }
515
516   if (auto *Resume = dyn_cast<ResumeInst>(Inst))
517     return handleResume(VMap, Resume, NewBB);
518
519   if (match(Inst, m_Intrinsic<Intrinsic::eh_begincatch>()))
520     return handleBeginCatch(VMap, Inst, NewBB);
521   if (match(Inst, m_Intrinsic<Intrinsic::eh_endcatch>()))
522     return handleEndCatch(VMap, Inst, NewBB);
523   if (match(Inst, m_Intrinsic<Intrinsic::eh_typeid_for>()))
524     return handleTypeIdFor(VMap, Inst, NewBB);
525
526   // Continue with the default cloning behavior.
527   return CloningDirector::CloneInstruction;
528 }
529
530 CloningDirector::CloningAction WinEHCatchDirector::handleBeginCatch(
531     ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
532   // The argument to the call is some form of the first element of the
533   // landingpad aggregate value, but that doesn't matter.  It isn't used
534   // here.
535   // The second argument is an outparameter where the exception object will be
536   // stored. Typically the exception object is a scalar, but it can be an
537   // aggregate when catching by value.
538   // FIXME: Leave something behind to indicate where the exception object lives
539   // for this handler. Should it be part of llvm.eh.actions?
540   return CloningDirector::SkipInstruction;
541 }
542
543 CloningDirector::CloningAction
544 WinEHCatchDirector::handleEndCatch(ValueToValueMapTy &VMap,
545                                    const Instruction *Inst, BasicBlock *NewBB) {
546   auto *IntrinCall = dyn_cast<IntrinsicInst>(Inst);
547   // It might be interesting to track whether or not we are inside a catch
548   // function, but that might make the algorithm more brittle than it needs
549   // to be.
550
551   // The end catch call can occur in one of two places: either in a
552   // landingpad
553   // block that is part of the catch handlers exception mechanism, or at the
554   // end of the catch block.  If it occurs in a landing pad, we must skip it
555   // and continue so that the landing pad gets cloned.
556   // FIXME: This case isn't fully supported yet and shouldn't turn up in any
557   //        of the test cases until it is.
558   if (IntrinCall->getParent()->isLandingPad())
559     return CloningDirector::SkipInstruction;
560
561   // If an end catch occurs anywhere else the next instruction should be an
562   // unconditional branch instruction that we want to replace with a return
563   // to the the address of the branch target.
564   const BasicBlock *EndCatchBB = IntrinCall->getParent();
565   const TerminatorInst *Terminator = EndCatchBB->getTerminator();
566   const BranchInst *Branch = dyn_cast<BranchInst>(Terminator);
567   assert(Branch && Branch->isUnconditional());
568   assert(std::next(BasicBlock::const_iterator(IntrinCall)) ==
569          BasicBlock::const_iterator(Branch));
570
571   ReturnInst::Create(NewBB->getContext(),
572                      BlockAddress::get(Branch->getSuccessor(0)), NewBB);
573
574   // We just added a terminator to the cloned block.
575   // Tell the caller to stop processing the current basic block so that
576   // the branch instruction will be skipped.
577   return CloningDirector::StopCloningBB;
578 }
579
580 CloningDirector::CloningAction WinEHCatchDirector::handleTypeIdFor(
581     ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
582   auto *IntrinCall = dyn_cast<IntrinsicInst>(Inst);
583   Value *Selector = IntrinCall->getArgOperand(0)->stripPointerCasts();
584   // This causes a replacement that will collapse the landing pad CFG based
585   // on the filter function we intend to match.
586   if (Selector == CurrentSelector)
587     VMap[Inst] = ConstantInt::get(SelectorIDType, 1);
588   else
589     VMap[Inst] = ConstantInt::get(SelectorIDType, 0);
590   // Tell the caller not to clone this instruction.
591   return CloningDirector::SkipInstruction;
592 }
593
594 CloningDirector::CloningAction
595 WinEHCatchDirector::handleResume(ValueToValueMapTy &VMap,
596                                  const ResumeInst *Resume, BasicBlock *NewBB) {
597   // Resume instructions shouldn't be reachable from catch handlers.
598   // We still need to handle it, but it will be pruned.
599   BasicBlock::InstListType &InstList = NewBB->getInstList();
600   InstList.push_back(new UnreachableInst(NewBB->getContext()));
601   return CloningDirector::StopCloningBB;
602 }
603
604 CloningDirector::CloningAction WinEHCleanupDirector::handleBeginCatch(
605     ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
606   // Catch blocks within cleanup handlers will always be unreachable.
607   // We'll insert an unreachable instruction now, but it will be pruned
608   // before the cloning process is complete.
609   BasicBlock::InstListType &InstList = NewBB->getInstList();
610   InstList.push_back(new UnreachableInst(NewBB->getContext()));
611   return CloningDirector::StopCloningBB;
612 }
613
614 CloningDirector::CloningAction WinEHCleanupDirector::handleEndCatch(
615     ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
616   // Catch blocks within cleanup handlers will always be unreachable.
617   // We'll insert an unreachable instruction now, but it will be pruned
618   // before the cloning process is complete.
619   BasicBlock::InstListType &InstList = NewBB->getInstList();
620   InstList.push_back(new UnreachableInst(NewBB->getContext()));
621   return CloningDirector::StopCloningBB;
622 }
623
624 CloningDirector::CloningAction WinEHCleanupDirector::handleTypeIdFor(
625     ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
626   // This causes a replacement that will collapse the landing pad CFG
627   // to just the cleanup code.
628   VMap[Inst] = ConstantInt::get(SelectorIDType, 0);
629   // Tell the caller not to clone this instruction.
630   return CloningDirector::SkipInstruction;
631 }
632
633 CloningDirector::CloningAction WinEHCleanupDirector::handleResume(
634     ValueToValueMapTy &VMap, const ResumeInst *Resume, BasicBlock *NewBB) {
635   ReturnInst::Create(NewBB->getContext(), nullptr, NewBB);
636
637   // We just added a terminator to the cloned block.
638   // Tell the caller to stop processing the current basic block so that
639   // the branch instruction will be skipped.
640   return CloningDirector::StopCloningBB;
641 }
642
643 WinEHFrameVariableMaterializer::WinEHFrameVariableMaterializer(
644     Function *OutlinedFn, FrameVarInfoMap &FrameVarInfo)
645     : FrameVarInfo(FrameVarInfo), Builder(OutlinedFn->getContext()) {
646   Builder.SetInsertPoint(&OutlinedFn->getEntryBlock());
647   // FIXME: Do something with the FrameVarMapped so that it is shared across the
648   // function.
649 }
650
651 Value *WinEHFrameVariableMaterializer::materializeValueFor(Value *V) {
652   // If we're asked to materialize a value that is an instruction, we
653   // temporarily create an alloca in the outlined function and add this
654   // to the FrameVarInfo map.  When all the outlining is complete, we'll
655   // collect these into a structure, spilling non-alloca values in the
656   // parent frame as necessary, and replace these temporary allocas with
657   // GEPs referencing the frame allocation block.
658
659   // If the value is an alloca, the mapping is direct.
660   if (auto *AV = dyn_cast<AllocaInst>(V)) {
661     AllocaInst *NewAlloca = dyn_cast<AllocaInst>(AV->clone());
662     Builder.Insert(NewAlloca, AV->getName());
663     FrameVarInfo[AV].push_back(NewAlloca);
664     return NewAlloca;
665   }
666
667   // For other types of instructions or arguments, we need an alloca based on
668   // the value's type and a load of the alloca.  The alloca will be replaced
669   // by a GEP, but the load will stay.  In the parent function, the value will
670   // be spilled to a location in the frame allocation block.
671   if (isa<Instruction>(V) || isa<Argument>(V)) {
672     AllocaInst *NewAlloca =
673         Builder.CreateAlloca(V->getType(), nullptr, "eh.temp.alloca");
674     FrameVarInfo[V].push_back(NewAlloca);
675     LoadInst *NewLoad = Builder.CreateLoad(NewAlloca, V->getName() + ".reload");
676     return NewLoad;
677   }
678
679   // Don't materialize other values.
680   return nullptr;
681 }