1 //===-- WinEHPrepare - Prepare exception handling for code generation ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
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"
33 using namespace llvm::PatternMatch;
35 #define DEBUG_TYPE "winehprepare"
39 struct HandlerAllocas {
40 TinyPtrVector<AllocaInst *> Allocas;
41 int ParentFrameAllocationIndex;
44 // This map is used to model frame variable usage during outlining, to
45 // construct a structure type to hold the frame variables in a frame
46 // allocation block, and to remap the frame variable allocas (including
47 // spill locations as needed) to GEPs that get the variable from the
48 // frame allocation structure.
49 typedef MapVector<Value *, HandlerAllocas> FrameVarInfoMap;
51 class WinEHPrepare : public FunctionPass {
52 std::unique_ptr<FunctionPass> DwarfPrepare;
54 enum HandlerType { Catch, Cleanup };
57 static char ID; // Pass identification, replacement for typeid.
58 WinEHPrepare(const TargetMachine *TM = nullptr)
59 : FunctionPass(ID), DwarfPrepare(createDwarfEHPass(TM)) {}
61 bool runOnFunction(Function &Fn) override;
63 bool doFinalization(Module &M) override;
65 void getAnalysisUsage(AnalysisUsage &AU) const override;
67 const char *getPassName() const override {
68 return "Windows exception handling preparation";
72 bool prepareCPPEHHandlers(Function &F,
73 SmallVectorImpl<LandingPadInst *> &LPads);
74 bool outlineHandler(HandlerType CatchOrCleanup, Function *SrcFn,
75 Constant *SelectorType, LandingPadInst *LPad,
76 CallInst *&EHAlloc, AllocaInst *&EHObjPtr,
77 FrameVarInfoMap &VarInfo);
80 class WinEHFrameVariableMaterializer : public ValueMaterializer {
82 WinEHFrameVariableMaterializer(Function *OutlinedFn,
83 FrameVarInfoMap &FrameVarInfo);
84 ~WinEHFrameVariableMaterializer() {}
86 virtual Value *materializeValueFor(Value *V) override;
89 FrameVarInfoMap &FrameVarInfo;
93 class WinEHCloningDirectorBase : public CloningDirector {
95 WinEHCloningDirectorBase(LandingPadInst *LPI, Function *HandlerFn,
96 FrameVarInfoMap &VarInfo)
97 : LPI(LPI), Materializer(HandlerFn, VarInfo),
98 SelectorIDType(Type::getInt32Ty(LPI->getContext())),
99 Int8PtrType(Type::getInt8PtrTy(LPI->getContext())) {}
101 CloningAction handleInstruction(ValueToValueMapTy &VMap,
102 const Instruction *Inst,
103 BasicBlock *NewBB) override;
105 virtual CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
106 const Instruction *Inst,
107 BasicBlock *NewBB) = 0;
108 virtual CloningAction handleEndCatch(ValueToValueMapTy &VMap,
109 const Instruction *Inst,
110 BasicBlock *NewBB) = 0;
111 virtual CloningAction handleTypeIdFor(ValueToValueMapTy &VMap,
112 const Instruction *Inst,
113 BasicBlock *NewBB) = 0;
114 virtual CloningAction handleResume(ValueToValueMapTy &VMap,
115 const ResumeInst *Resume,
116 BasicBlock *NewBB) = 0;
118 ValueMaterializer *getValueMaterializer() override { return &Materializer; }
122 WinEHFrameVariableMaterializer Materializer;
123 Type *SelectorIDType;
126 const Value *ExtractedEHPtr;
127 const Value *ExtractedSelector;
128 const Value *EHPtrStoreAddr;
129 const Value *SelectorStoreAddr;
132 class WinEHCatchDirector : public WinEHCloningDirectorBase {
134 WinEHCatchDirector(LandingPadInst *LPI, Function *CatchFn, Value *Selector,
135 Value *EHObj, FrameVarInfoMap &VarInfo)
136 : WinEHCloningDirectorBase(LPI, CatchFn, VarInfo), EHObj(EHObj),
137 CurrentSelector(Selector->stripPointerCasts()) {}
139 CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
140 const Instruction *Inst,
141 BasicBlock *NewBB) override;
142 CloningAction handleEndCatch(ValueToValueMapTy &VMap, const Instruction *Inst,
143 BasicBlock *NewBB) override;
144 CloningAction handleTypeIdFor(ValueToValueMapTy &VMap,
145 const Instruction *Inst,
146 BasicBlock *NewBB) override;
147 CloningAction handleResume(ValueToValueMapTy &VMap, const ResumeInst *Resume,
148 BasicBlock *NewBB) override;
151 Value *CurrentSelector;
155 class WinEHCleanupDirector : public WinEHCloningDirectorBase {
157 WinEHCleanupDirector(LandingPadInst *LPI, Function *CleanupFn,
158 FrameVarInfoMap &VarInfo)
159 : WinEHCloningDirectorBase(LPI, CleanupFn, VarInfo) {}
161 CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
162 const Instruction *Inst,
163 BasicBlock *NewBB) override;
164 CloningAction handleEndCatch(ValueToValueMapTy &VMap, const Instruction *Inst,
165 BasicBlock *NewBB) override;
166 CloningAction handleTypeIdFor(ValueToValueMapTy &VMap,
167 const Instruction *Inst,
168 BasicBlock *NewBB) override;
169 CloningAction handleResume(ValueToValueMapTy &VMap, const ResumeInst *Resume,
170 BasicBlock *NewBB) override;
173 } // end anonymous namespace
175 char WinEHPrepare::ID = 0;
176 INITIALIZE_TM_PASS(WinEHPrepare, "winehprepare", "Prepare Windows exceptions",
179 FunctionPass *llvm::createWinEHPass(const TargetMachine *TM) {
180 return new WinEHPrepare(TM);
183 static bool isMSVCPersonality(EHPersonality Pers) {
184 return Pers == EHPersonality::MSVC_Win64SEH ||
185 Pers == EHPersonality::MSVC_CXX;
188 bool WinEHPrepare::runOnFunction(Function &Fn) {
189 SmallVector<LandingPadInst *, 4> LPads;
190 SmallVector<ResumeInst *, 4> Resumes;
191 for (BasicBlock &BB : Fn) {
192 if (auto *LP = BB.getLandingPadInst())
194 if (auto *Resume = dyn_cast<ResumeInst>(BB.getTerminator()))
195 Resumes.push_back(Resume);
198 // No need to prepare functions that lack landing pads.
202 // Classify the personality to see what kind of preparation we need.
203 EHPersonality Pers = classifyEHPersonality(LPads.back()->getPersonalityFn());
205 // Delegate through to the DWARF pass if this is unrecognized.
206 if (!isMSVCPersonality(Pers))
207 return DwarfPrepare->runOnFunction(Fn);
209 // FIXME: This only returns true if the C++ EH handlers were outlined.
210 // When that code is complete, it should always return whatever
211 // prepareCPPEHHandlers returns.
212 if (Pers == EHPersonality::MSVC_CXX && prepareCPPEHHandlers(Fn, LPads))
215 // FIXME: SEH Cleanups are unimplemented. Replace them with unreachable.
219 for (ResumeInst *Resume : Resumes) {
220 IRBuilder<>(Resume).CreateUnreachable();
221 Resume->eraseFromParent();
227 bool WinEHPrepare::doFinalization(Module &M) {
228 return DwarfPrepare->doFinalization(M);
231 void WinEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const {
232 DwarfPrepare->getAnalysisUsage(AU);
235 bool WinEHPrepare::prepareCPPEHHandlers(
236 Function &F, SmallVectorImpl<LandingPadInst *> &LPads) {
237 // These containers are used to re-map frame variables that are used in
238 // outlined catch and cleanup handlers. They will be populated as the
239 // handlers are outlined.
240 FrameVarInfoMap FrameVarInfo;
241 SmallVector<CallInst *, 4> HandlerAllocs;
242 SmallVector<AllocaInst *, 4> HandlerEHObjPtrs;
244 bool HandlersOutlined = false;
246 for (LandingPadInst *LPad : LPads) {
247 // Look for evidence that this landingpad has already been processed.
248 bool LPadHasActionList = false;
249 BasicBlock *LPadBB = LPad->getParent();
250 for (Instruction &Inst : LPadBB->getInstList()) {
251 // FIXME: Make this an intrinsic.
252 if (auto *Call = dyn_cast<CallInst>(&Inst))
253 if (Call->getCalledFunction()->getName() == "llvm.eh.actions") {
254 LPadHasActionList = true;
259 // If we've already outlined the handlers for this landingpad,
260 // there's nothing more to do here.
261 if (LPadHasActionList)
264 for (unsigned Idx = 0, NumClauses = LPad->getNumClauses(); Idx < NumClauses;
266 if (LPad->isCatch(Idx)) {
267 // Create a new instance of the handler data structure in the
268 // HandlerData vector.
269 CallInst *EHAlloc = nullptr;
270 AllocaInst *EHObjPtr = nullptr;
271 bool Outlined = outlineHandler(Catch, &F, LPad->getClause(Idx), LPad,
272 EHAlloc, EHObjPtr, FrameVarInfo);
274 HandlersOutlined = true;
275 // These values must be resolved after all handlers have been
278 HandlerAllocs.push_back(EHAlloc);
280 HandlerEHObjPtrs.push_back(EHObjPtr);
282 } // End if (isCatch)
283 } // End for each clause
285 // FIXME: This only handles the simple case where there is a 1:1
286 // correspondence between landing pad and cleanup blocks.
287 // It does not handle cases where there are catch blocks between
288 // cleanup blocks or the case where a cleanup block is shared by
289 // multiple landing pads. Those cases will be supported later
290 // when landing pad block analysis is added.
291 if (LPad->isCleanup()) {
292 CallInst *EHAlloc = nullptr;
293 AllocaInst *IgnoreEHObjPtr = nullptr;
294 bool Outlined = outlineHandler(Cleanup, &F, nullptr, LPad, EHAlloc,
295 IgnoreEHObjPtr, FrameVarInfo);
297 HandlersOutlined = true;
298 // This value must be resolved after all handlers have been outlined.
300 HandlerAllocs.push_back(EHAlloc);
303 } // End for each landingpad
305 // If nothing got outlined, there is no more processing to be done.
306 if (!HandlersOutlined)
309 // FIXME: We will replace the landingpad bodies with llvm.eh.actions
310 // calls and indirect branches here and then delete blocks
311 // which are no longer reachable. That will get rid of the
312 // handlers that we have outlined. There is code below
313 // that looks for allocas with no uses in the parent function.
314 // That will only happen after the pruning is implemented.
316 // Remap the frame variables.
317 SmallVector<Type *, 2> StructTys;
318 StructTys.push_back(Type::getInt32Ty(F.getContext())); // EH state
319 StructTys.push_back(Type::getInt8PtrTy(F.getContext())); // EH object
321 // Start the index at two since we always have the above fields at 0 and 1.
324 // FIXME: Sort the FrameVarInfo vector by the ParentAlloca size and alignment
325 // and add padding as necessary to provide the proper alignment.
327 // Map the alloca instructions to the corresponding index in the
328 // frame allocation structure. If any alloca is used only in a single
329 // handler and is not used in the parent frame after outlining, it will
330 // be assigned an index of -1, meaning the handler can keep its
331 // "temporary" alloca and the original alloca can be erased from the
332 // parent function. If we later encounter this alloca in a second
333 // handler, we will assign it a place in the frame allocation structure
334 // at that time. Since the instruction replacement doesn't happen until
335 // all the entries in the HandlerData have been processed this isn't a
337 for (auto &VarInfoEntry : FrameVarInfo) {
338 Value *ParentVal = VarInfoEntry.first;
339 HandlerAllocas &AllocaInfo = VarInfoEntry.second;
341 if (auto *ParentAlloca = dyn_cast<AllocaInst>(ParentVal)) {
342 // If the instruction still has uses in the parent function or if it is
343 // referenced by more than one handler, add it to the frame allocation
345 if (ParentAlloca->getNumUses() != 0 || AllocaInfo.Allocas.size() > 1) {
346 Type *VarTy = ParentAlloca->getAllocatedType();
347 StructTys.push_back(VarTy);
348 AllocaInfo.ParentFrameAllocationIndex = Idx++;
350 // If the variable is not used in the parent frame and it is only used
351 // in one handler, the alloca can be removed from the parent frame
352 // and the handler will keep its "temporary" alloca to define the value.
353 // An element index of -1 is used to indicate this condition.
354 AllocaInfo.ParentFrameAllocationIndex = -1;
357 // FIXME: Sink non-alloca values into the handler if they have no other
358 // uses in the parent function after outlining and are only used in
360 Type *VarTy = ParentVal->getType();
361 StructTys.push_back(VarTy);
362 AllocaInfo.ParentFrameAllocationIndex = Idx++;
366 // Having filled the StructTys vector and assigned an index to each element,
367 // we can now create the structure.
368 StructType *EHDataStructTy = StructType::create(
369 F.getContext(), StructTys, "struct." + F.getName().str() + ".ehdata");
370 IRBuilder<> Builder(F.getParent()->getContext());
372 // Create a frame allocation.
373 Module *M = F.getParent();
374 LLVMContext &Context = M->getContext();
375 BasicBlock *Entry = &F.getEntryBlock();
376 Builder.SetInsertPoint(Entry->getFirstInsertionPt());
377 Function *FrameAllocFn =
378 Intrinsic::getDeclaration(M, Intrinsic::frameallocate);
379 uint64_t EHAllocSize = M->getDataLayout()->getTypeAllocSize(EHDataStructTy);
380 Value *FrameAllocArgs[] = {
381 ConstantInt::get(Type::getInt32Ty(Context), EHAllocSize)};
382 CallInst *FrameAlloc =
383 Builder.CreateCall(FrameAllocFn, FrameAllocArgs, "frame.alloc");
385 Value *FrameEHData = Builder.CreateBitCast(
386 FrameAlloc, EHDataStructTy->getPointerTo(), "eh.data");
388 // Now visit each handler that is using the structure and bitcast its EHAlloc
389 // value to be a pointer to the frame alloc structure.
390 DenseMap<Function *, Value *> EHDataMap;
391 for (CallInst *EHAlloc : HandlerAllocs) {
392 // The EHAlloc has no uses at this time, so we need to just insert the
393 // cast before the next instruction. There is always a next instruction.
394 BasicBlock::iterator II = EHAlloc;
396 Builder.SetInsertPoint(cast<Instruction>(II));
397 Value *EHData = Builder.CreateBitCast(
398 EHAlloc, EHDataStructTy->getPointerTo(), "eh.data");
399 EHDataMap[EHAlloc->getParent()->getParent()] = EHData;
402 // Next, replace the place-holder EHObjPtr allocas with GEP instructions
403 // that pull the EHObjPtr from the frame alloc structure
404 for (AllocaInst *EHObjPtr : HandlerEHObjPtrs) {
405 Value *EHData = EHDataMap[EHObjPtr->getParent()->getParent()];
406 Builder.SetInsertPoint(EHObjPtr);
407 Value *ElementPtr = Builder.CreateConstInBoundsGEP2_32(EHData, 0, 1);
408 EHObjPtr->replaceAllUsesWith(ElementPtr);
409 EHObjPtr->removeFromParent();
410 ElementPtr->takeName(EHObjPtr);
414 // Finally, replace all of the temporary allocas for frame variables used in
415 // the outlined handlers and the original frame allocas with GEP instructions
416 // that get the equivalent pointer from the frame allocation struct.
417 Instruction *FrameEHDataInst = cast<Instruction>(FrameEHData);
418 BasicBlock::iterator II = FrameEHDataInst;
420 Instruction *AllocaInsertPt = II;
421 for (auto &VarInfoEntry : FrameVarInfo) {
422 Value *ParentVal = VarInfoEntry.first;
423 HandlerAllocas &AllocaInfo = VarInfoEntry.second;
424 int Idx = AllocaInfo.ParentFrameAllocationIndex;
426 // If the mapped value isn't already an alloca, we need to spill it if it
427 // is a computed value or copy it if it is an argument.
428 AllocaInst *ParentAlloca = dyn_cast<AllocaInst>(ParentVal);
430 if (auto *Arg = dyn_cast<Argument>(ParentVal)) {
431 // Lower this argument to a copy and then demote that to the stack.
432 // We can't just use the argument location because the handler needs
433 // it to be in the frame allocation block.
434 // Use 'select i8 true, %arg, undef' to simulate a 'no-op' instruction.
435 Value *TrueValue = ConstantInt::getTrue(Context);
436 Value *UndefValue = UndefValue::get(Arg->getType());
438 SelectInst::Create(TrueValue, Arg, UndefValue,
439 Arg->getName() + ".tmp", AllocaInsertPt);
440 Arg->replaceAllUsesWith(SI);
441 // Reset the select operand, because it was clobbered by the RAUW above.
442 SI->setOperand(1, Arg);
443 ParentAlloca = DemoteRegToStack(*SI, true, SI);
444 } else if (auto *PN = dyn_cast<PHINode>(ParentVal)) {
445 ParentAlloca = DemotePHIToStack(PN, AllocaInsertPt);
447 Instruction *ParentInst = cast<Instruction>(ParentVal);
448 ParentAlloca = DemoteRegToStack(*ParentInst, true, ParentInst);
452 // If we have an index of -1 for this instruction, it means it isn't used
453 // outside of this handler. In that case, we just keep the "temporary"
454 // alloca in the handler and erase the original alloca from the parent.
456 ParentAlloca->eraseFromParent();
458 // Otherwise, we replace the parent alloca and all outlined allocas
459 // which map to it with GEP instructions.
461 // First replace the original alloca.
462 Builder.SetInsertPoint(ParentAlloca);
463 Builder.SetCurrentDebugLocation(ParentAlloca->getDebugLoc());
465 Builder.CreateConstInBoundsGEP2_32(FrameEHData, 0, Idx);
466 ParentAlloca->replaceAllUsesWith(ElementPtr);
467 ParentAlloca->removeFromParent();
468 ElementPtr->takeName(ParentAlloca);
469 if (ParentAlloca == AllocaInsertPt)
470 AllocaInsertPt = dyn_cast<Instruction>(ElementPtr);
473 // Next replace all outlined allocas that are mapped to it.
474 for (AllocaInst *TempAlloca : AllocaInfo.Allocas) {
475 Value *EHData = EHDataMap[TempAlloca->getParent()->getParent()];
476 // FIXME: Sink this GEP into the blocks where it is used.
477 Builder.SetInsertPoint(TempAlloca);
478 Builder.SetCurrentDebugLocation(TempAlloca->getDebugLoc());
479 ElementPtr = Builder.CreateConstInBoundsGEP2_32(EHData, 0, Idx);
480 TempAlloca->replaceAllUsesWith(ElementPtr);
481 TempAlloca->removeFromParent();
482 ElementPtr->takeName(TempAlloca);
485 } // end else of if (Idx == -1)
486 } // End for each FrameVarInfo entry.
488 return HandlersOutlined;
491 bool WinEHPrepare::outlineHandler(HandlerType CatchOrCleanup, Function *SrcFn,
492 Constant *SelectorType, LandingPadInst *LPad,
493 CallInst *&EHAlloc, AllocaInst *&EHObjPtr,
494 FrameVarInfoMap &VarInfo) {
495 Module *M = SrcFn->getParent();
496 LLVMContext &Context = M->getContext();
498 // Create a new function to receive the handler contents.
499 Type *Int8PtrType = Type::getInt8PtrTy(Context);
500 std::vector<Type *> ArgTys;
501 ArgTys.push_back(Int8PtrType);
502 ArgTys.push_back(Int8PtrType);
504 if (CatchOrCleanup == Catch) {
505 FunctionType *FnType = FunctionType::get(Int8PtrType, ArgTys, false);
506 Handler = Function::Create(FnType, GlobalVariable::InternalLinkage,
507 SrcFn->getName() + ".catch", M);
509 FunctionType *FnType =
510 FunctionType::get(Type::getVoidTy(Context), ArgTys, false);
511 Handler = Function::Create(FnType, GlobalVariable::InternalLinkage,
512 SrcFn->getName() + ".cleanup", M);
515 // Generate a standard prolog to setup the frame recovery structure.
516 IRBuilder<> Builder(Context);
517 BasicBlock *Entry = BasicBlock::Create(Context, "entry");
518 Handler->getBasicBlockList().push_front(Entry);
519 Builder.SetInsertPoint(Entry);
520 Builder.SetCurrentDebugLocation(LPad->getDebugLoc());
522 // The outlined handler will be called with the parent's frame pointer as
523 // its second argument. To enable the handler to access variables from
524 // the parent frame, we use that pointer to get locate a special block
525 // of memory that was allocated using llvm.eh.allocateframe for this
526 // purpose. During the outlining process we will determine which frame
527 // variables are used in handlers and create a structure that maps these
528 // variables into the frame allocation block.
530 // The frame allocation block also contains an exception state variable
531 // used by the runtime and a pointer to the exception object pointer
532 // which will be filled in by the runtime for use in the handler.
533 Function *RecoverFrameFn =
534 Intrinsic::getDeclaration(M, Intrinsic::framerecover);
535 Value *RecoverArgs[] = {Builder.CreateBitCast(SrcFn, Int8PtrType, ""),
536 &(Handler->getArgumentList().back())};
537 EHAlloc = Builder.CreateCall(RecoverFrameFn, RecoverArgs, "eh.alloc");
539 std::unique_ptr<WinEHCloningDirectorBase> Director;
541 if (CatchOrCleanup == Catch) {
542 // This alloca is only temporary. We'll be replacing it once we know all
543 // the frame variables that need to go in the frame allocation structure.
544 EHObjPtr = Builder.CreateAlloca(Int8PtrType, 0, "eh.obj.ptr");
546 // This will give us a raw pointer to the exception object, which
547 // corresponds to the formal parameter of the catch statement. If the
548 // handler uses this object, we will generate code during the outlining
549 // process to cast the pointer to the appropriate type and deference it
550 // as necessary. The un-outlined landing pad code represents the
551 // exception object as the result of the llvm.eh.begincatch call.
552 Value *EHObj = Builder.CreateLoad(EHObjPtr, false, "eh.obj");
555 new WinEHCatchDirector(LPad, Handler, SelectorType, EHObj, VarInfo));
557 Director.reset(new WinEHCleanupDirector(LPad, Handler, VarInfo));
560 ValueToValueMapTy VMap;
562 // FIXME: Map other values referenced in the filter handler.
564 SmallVector<ReturnInst *, 8> Returns;
565 ClonedCodeInfo InlinedFunctionInfo;
567 BasicBlock::iterator II = LPad;
569 CloneAndPruneIntoFromInst(
570 Handler, SrcFn, ++II, VMap,
571 /*ModuleLevelChanges=*/false, Returns, "", &InlinedFunctionInfo,
572 SrcFn->getParent()->getDataLayout(), Director.get());
574 // Move all the instructions in the first cloned block into our entry block.
575 BasicBlock *FirstClonedBB = std::next(Function::iterator(Entry));
576 Entry->getInstList().splice(Entry->end(), FirstClonedBB->getInstList());
577 FirstClonedBB->eraseFromParent();
582 CloningDirector::CloningAction WinEHCloningDirectorBase::handleInstruction(
583 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
584 // Intercept instructions which extract values from the landing pad aggregate.
585 if (auto *Extract = dyn_cast<ExtractValueInst>(Inst)) {
586 if (Extract->getAggregateOperand() == LPI) {
587 assert(Extract->getNumIndices() == 1 &&
588 "Unexpected operation: extracting both landing pad values");
589 assert((*(Extract->idx_begin()) == 0 || *(Extract->idx_begin()) == 1) &&
590 "Unexpected operation: extracting an unknown landing pad element");
592 if (*(Extract->idx_begin()) == 0) {
593 // Element 0 doesn't directly corresponds to anything in the WinEH
595 // It will be stored to a memory location, then later loaded and finally
596 // the loaded value will be used as the argument to an
597 // llvm.eh.begincatch
598 // call. We're tracking it here so that we can skip the store and load.
599 ExtractedEHPtr = Inst;
601 // Element 1 corresponds to the filter selector. We'll map it to 1 for
602 // matching purposes, but it will also probably be stored to memory and
603 // reloaded, so we need to track the instuction so that we can map the
605 VMap[Inst] = ConstantInt::get(SelectorIDType, 1);
606 ExtractedSelector = Inst;
609 // Tell the caller not to clone this instruction.
610 return CloningDirector::SkipInstruction;
612 // Other extract value instructions just get cloned.
613 return CloningDirector::CloneInstruction;
616 if (auto *Store = dyn_cast<StoreInst>(Inst)) {
617 // Look for and suppress stores of the extracted landingpad values.
618 const Value *StoredValue = Store->getValueOperand();
619 if (StoredValue == ExtractedEHPtr) {
620 EHPtrStoreAddr = Store->getPointerOperand();
621 return CloningDirector::SkipInstruction;
623 if (StoredValue == ExtractedSelector) {
624 SelectorStoreAddr = Store->getPointerOperand();
625 return CloningDirector::SkipInstruction;
628 // Any other store just gets cloned.
629 return CloningDirector::CloneInstruction;
632 if (auto *Load = dyn_cast<LoadInst>(Inst)) {
633 // Look for loads of (previously suppressed) landingpad values.
634 // The EHPtr load can be ignored (it should only be used as
635 // an argument to llvm.eh.begincatch), but the selector value
636 // needs to be mapped to a constant value of 1 to be used to
637 // simplify the branching to always flow to the current handler.
638 const Value *LoadAddr = Load->getPointerOperand();
639 if (LoadAddr == EHPtrStoreAddr) {
640 VMap[Inst] = UndefValue::get(Int8PtrType);
641 return CloningDirector::SkipInstruction;
643 if (LoadAddr == SelectorStoreAddr) {
644 VMap[Inst] = ConstantInt::get(SelectorIDType, 1);
645 return CloningDirector::SkipInstruction;
648 // Any other loads just get cloned.
649 return CloningDirector::CloneInstruction;
652 if (auto *Resume = dyn_cast<ResumeInst>(Inst))
653 return handleResume(VMap, Resume, NewBB);
655 if (match(Inst, m_Intrinsic<Intrinsic::eh_begincatch>()))
656 return handleBeginCatch(VMap, Inst, NewBB);
657 if (match(Inst, m_Intrinsic<Intrinsic::eh_endcatch>()))
658 return handleEndCatch(VMap, Inst, NewBB);
659 if (match(Inst, m_Intrinsic<Intrinsic::eh_typeid_for>()))
660 return handleTypeIdFor(VMap, Inst, NewBB);
662 // Continue with the default cloning behavior.
663 return CloningDirector::CloneInstruction;
666 CloningDirector::CloningAction WinEHCatchDirector::handleBeginCatch(
667 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
668 // The argument to the call is some form of the first element of the
669 // landingpad aggregate value, but that doesn't matter. It isn't used
671 // The return value of this instruction, however, is used to access the
672 // EH object pointer. We have generated an instruction to get that value
673 // from the EH alloc block, so we can just map to that here.
675 return CloningDirector::SkipInstruction;
678 CloningDirector::CloningAction
679 WinEHCatchDirector::handleEndCatch(ValueToValueMapTy &VMap,
680 const Instruction *Inst, BasicBlock *NewBB) {
681 auto *IntrinCall = dyn_cast<IntrinsicInst>(Inst);
682 // It might be interesting to track whether or not we are inside a catch
683 // function, but that might make the algorithm more brittle than it needs
686 // The end catch call can occur in one of two places: either in a
688 // block that is part of the catch handlers exception mechanism, or at the
689 // end of the catch block. If it occurs in a landing pad, we must skip it
690 // and continue so that the landing pad gets cloned.
691 // FIXME: This case isn't fully supported yet and shouldn't turn up in any
692 // of the test cases until it is.
693 if (IntrinCall->getParent()->isLandingPad())
694 return CloningDirector::SkipInstruction;
696 // If an end catch occurs anywhere else the next instruction should be an
697 // unconditional branch instruction that we want to replace with a return
698 // to the the address of the branch target.
699 const BasicBlock *EndCatchBB = IntrinCall->getParent();
700 const TerminatorInst *Terminator = EndCatchBB->getTerminator();
701 const BranchInst *Branch = dyn_cast<BranchInst>(Terminator);
702 assert(Branch && Branch->isUnconditional());
703 assert(std::next(BasicBlock::const_iterator(IntrinCall)) ==
704 BasicBlock::const_iterator(Branch));
706 ReturnInst::Create(NewBB->getContext(),
707 BlockAddress::get(Branch->getSuccessor(0)), NewBB);
709 // We just added a terminator to the cloned block.
710 // Tell the caller to stop processing the current basic block so that
711 // the branch instruction will be skipped.
712 return CloningDirector::StopCloningBB;
715 CloningDirector::CloningAction WinEHCatchDirector::handleTypeIdFor(
716 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
717 auto *IntrinCall = dyn_cast<IntrinsicInst>(Inst);
718 Value *Selector = IntrinCall->getArgOperand(0)->stripPointerCasts();
719 // This causes a replacement that will collapse the landing pad CFG based
720 // on the filter function we intend to match.
721 if (Selector == CurrentSelector)
722 VMap[Inst] = ConstantInt::get(SelectorIDType, 1);
724 VMap[Inst] = ConstantInt::get(SelectorIDType, 0);
725 // Tell the caller not to clone this instruction.
726 return CloningDirector::SkipInstruction;
729 CloningDirector::CloningAction
730 WinEHCatchDirector::handleResume(ValueToValueMapTy &VMap,
731 const ResumeInst *Resume, BasicBlock *NewBB) {
732 // Resume instructions shouldn't be reachable from catch handlers.
733 // We still need to handle it, but it will be pruned.
734 BasicBlock::InstListType &InstList = NewBB->getInstList();
735 InstList.push_back(new UnreachableInst(NewBB->getContext()));
736 return CloningDirector::StopCloningBB;
739 CloningDirector::CloningAction WinEHCleanupDirector::handleBeginCatch(
740 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
741 // Catch blocks within cleanup handlers will always be unreachable.
742 // We'll insert an unreachable instruction now, but it will be pruned
743 // before the cloning process is complete.
744 BasicBlock::InstListType &InstList = NewBB->getInstList();
745 InstList.push_back(new UnreachableInst(NewBB->getContext()));
746 return CloningDirector::StopCloningBB;
749 CloningDirector::CloningAction WinEHCleanupDirector::handleEndCatch(
750 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
751 // Catch blocks within cleanup handlers will always be unreachable.
752 // We'll insert an unreachable instruction now, but it will be pruned
753 // before the cloning process is complete.
754 BasicBlock::InstListType &InstList = NewBB->getInstList();
755 InstList.push_back(new UnreachableInst(NewBB->getContext()));
756 return CloningDirector::StopCloningBB;
759 CloningDirector::CloningAction WinEHCleanupDirector::handleTypeIdFor(
760 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
761 // This causes a replacement that will collapse the landing pad CFG
762 // to just the cleanup code.
763 VMap[Inst] = ConstantInt::get(SelectorIDType, 0);
764 // Tell the caller not to clone this instruction.
765 return CloningDirector::SkipInstruction;
768 CloningDirector::CloningAction WinEHCleanupDirector::handleResume(
769 ValueToValueMapTy &VMap, const ResumeInst *Resume, BasicBlock *NewBB) {
770 ReturnInst::Create(NewBB->getContext(), nullptr, NewBB);
772 // We just added a terminator to the cloned block.
773 // Tell the caller to stop processing the current basic block so that
774 // the branch instruction will be skipped.
775 return CloningDirector::StopCloningBB;
778 WinEHFrameVariableMaterializer::WinEHFrameVariableMaterializer(
779 Function *OutlinedFn, FrameVarInfoMap &FrameVarInfo)
780 : FrameVarInfo(FrameVarInfo), Builder(OutlinedFn->getContext()) {
781 Builder.SetInsertPoint(&OutlinedFn->getEntryBlock());
782 // FIXME: Do something with the FrameVarMapped so that it is shared across the
786 Value *WinEHFrameVariableMaterializer::materializeValueFor(Value *V) {
787 // If we're asked to materialize a value that is an instruction, we
788 // temporarily create an alloca in the outlined function and add this
789 // to the FrameVarInfo map. When all the outlining is complete, we'll
790 // collect these into a structure, spilling non-alloca values in the
791 // parent frame as necessary, and replace these temporary allocas with
792 // GEPs referencing the frame allocation block.
794 // If the value is an alloca, the mapping is direct.
795 if (auto *AV = dyn_cast<AllocaInst>(V)) {
796 AllocaInst *NewAlloca = dyn_cast<AllocaInst>(AV->clone());
797 Builder.Insert(NewAlloca, AV->getName());
798 FrameVarInfo[AV].Allocas.push_back(NewAlloca);
802 // For other types of instructions or arguments, we need an alloca based on
803 // the value's type and a load of the alloca. The alloca will be replaced
804 // by a GEP, but the load will stay. In the parent function, the value will
805 // be spilled to a location in the frame allocation block.
806 if (isa<Instruction>(V) || isa<Argument>(V)) {
807 AllocaInst *NewAlloca =
808 Builder.CreateAlloca(V->getType(), nullptr, "eh.temp.alloca");
809 FrameVarInfo[V].Allocas.push_back(NewAlloca);
810 LoadInst *NewLoad = Builder.CreateLoad(NewAlloca, V->getName() + ".reload");
814 // Don't materialize other values.