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 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.
17 //===----------------------------------------------------------------------===//
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/Pass.h"
38 #include "llvm/Support/Debug.h"
39 #include "llvm/Support/raw_ostream.h"
40 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
41 #include "llvm/Transforms/Utils/Cloning.h"
42 #include "llvm/Transforms/Utils/Local.h"
43 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
44 #include "llvm/Transforms/Utils/SSAUpdater.h"
48 using namespace llvm::PatternMatch;
50 #define DEBUG_TYPE "winehprepare"
52 static cl::opt<bool> DisableDemotion(
53 "disable-demotion", cl::Hidden,
55 "Clone multicolor basic blocks but do not demote cross funclet values"),
58 static cl::opt<bool> DisableCleanups(
59 "disable-cleanups", cl::Hidden,
60 cl::desc("Do not remove implausible terminators or other similar cleanups"),
65 // This map is used to model frame variable usage during outlining, to
66 // construct a structure type to hold the frame variables in a frame
67 // allocation block, and to remap the frame variable allocas (including
68 // spill locations as needed) to GEPs that get the variable from the
69 // frame allocation structure.
70 typedef MapVector<Value *, TinyPtrVector<AllocaInst *>> FrameVarInfoMap;
72 // TinyPtrVector cannot hold nullptr, so we need our own sentinel that isn't
74 AllocaInst *getCatchObjectSentinel() {
75 return static_cast<AllocaInst *>(nullptr) + 1;
78 typedef SmallSet<BasicBlock *, 4> VisitedBlockSet;
80 class LandingPadActions;
83 typedef DenseMap<const BasicBlock *, CatchHandler *> CatchHandlerMapTy;
84 typedef DenseMap<const BasicBlock *, CleanupHandler *> CleanupHandlerMapTy;
86 class WinEHPrepare : public FunctionPass {
88 static char ID; // Pass identification, replacement for typeid.
89 WinEHPrepare(const TargetMachine *TM = nullptr)
92 TheTriple = TM->getTargetTriple();
95 bool runOnFunction(Function &Fn) override;
97 bool doFinalization(Module &M) override;
99 void getAnalysisUsage(AnalysisUsage &AU) const override;
101 const char *getPassName() const override {
102 return "Windows exception handling preparation";
106 bool prepareExceptionHandlers(Function &F,
107 SmallVectorImpl<LandingPadInst *> &LPads);
108 void identifyEHBlocks(Function &F, SmallVectorImpl<LandingPadInst *> &LPads);
109 void promoteLandingPadValues(LandingPadInst *LPad);
110 void demoteValuesLiveAcrossHandlers(Function &F,
111 SmallVectorImpl<LandingPadInst *> &LPads);
112 void findSEHEHReturnPoints(Function &F,
113 SetVector<BasicBlock *> &EHReturnBlocks);
114 void findCXXEHReturnPoints(Function &F,
115 SetVector<BasicBlock *> &EHReturnBlocks);
116 void getPossibleReturnTargets(Function *ParentF, Function *HandlerF,
117 SetVector<BasicBlock*> &Targets);
118 void completeNestedLandingPad(Function *ParentFn,
119 LandingPadInst *OutlinedLPad,
120 const LandingPadInst *OriginalLPad,
121 FrameVarInfoMap &VarInfo);
122 Function *createHandlerFunc(Function *ParentFn, Type *RetTy,
123 const Twine &Name, Module *M, Value *&ParentFP);
124 bool outlineHandler(ActionHandler *Action, Function *SrcFn,
125 LandingPadInst *LPad, BasicBlock *StartBB,
126 FrameVarInfoMap &VarInfo);
127 void addStubInvokeToHandlerIfNeeded(Function *Handler);
129 void mapLandingPadBlocks(LandingPadInst *LPad, LandingPadActions &Actions);
130 CatchHandler *findCatchHandler(BasicBlock *BB, BasicBlock *&NextBB,
131 VisitedBlockSet &VisitedBlocks);
132 void findCleanupHandlers(LandingPadActions &Actions, BasicBlock *StartBB,
135 void processSEHCatchHandler(CatchHandler *Handler, BasicBlock *StartBB);
136 void insertPHIStores(PHINode *OriginalPHI, AllocaInst *SpillSlot);
138 insertPHIStore(BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot,
139 SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist);
140 AllocaInst *insertPHILoads(PHINode *PN, Function &F);
141 void replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
142 DenseMap<BasicBlock *, Value *> &Loads, Function &F);
143 void demoteNonlocalUses(Value *V, std::set<BasicBlock *> &ColorsForBB,
145 bool prepareExplicitEH(Function &F,
146 SmallVectorImpl<BasicBlock *> &EntryBlocks);
147 void replaceTerminatePadWithCleanup(Function &F);
148 void colorFunclets(Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks);
149 void demotePHIsOnFunclets(Function &F);
150 void demoteUsesBetweenFunclets(Function &F);
151 void demoteArgumentUses(Function &F);
152 void cloneCommonBlocks(Function &F,
153 SmallVectorImpl<BasicBlock *> &EntryBlocks);
154 void removeImplausibleTerminators(Function &F);
155 void cleanupPreparedFunclets(Function &F);
156 void verifyPreparedFunclets(Function &F);
160 // All fields are reset by runOnFunction.
161 DominatorTree *DT = nullptr;
162 const TargetLibraryInfo *LibInfo = nullptr;
163 EHPersonality Personality = EHPersonality::Unknown;
164 CatchHandlerMapTy CatchHandlerMap;
165 CleanupHandlerMapTy CleanupHandlerMap;
166 DenseMap<const LandingPadInst *, LandingPadMap> LPadMaps;
167 SmallPtrSet<BasicBlock *, 4> NormalBlocks;
168 SmallPtrSet<BasicBlock *, 4> EHBlocks;
169 SetVector<BasicBlock *> EHReturnBlocks;
171 // This maps landing pad instructions found in outlined handlers to
172 // the landing pad instruction in the parent function from which they
173 // were cloned. The cloned/nested landing pad is used as the key
174 // because the landing pad may be cloned into multiple handlers.
175 // This map will be used to add the llvm.eh.actions call to the nested
176 // landing pads after all handlers have been outlined.
177 DenseMap<LandingPadInst *, const LandingPadInst *> NestedLPtoOriginalLP;
179 // This maps blocks in the parent function which are destinations of
180 // catch handlers to cloned blocks in (other) outlined handlers. This
181 // handles the case where a nested landing pads has a catch handler that
182 // returns to a handler function rather than the parent function.
183 // The original block is used as the key here because there should only
184 // ever be one handler function from which the cloned block is not pruned.
185 // The original block will be pruned from the parent function after all
186 // handlers have been outlined. This map will be used to adjust the
187 // return instructions of handlers which return to the block that was
188 // outlined into a handler. This is done after all handlers have been
189 // outlined but before the outlined code is pruned from the parent function.
190 DenseMap<const BasicBlock *, BasicBlock *> LPadTargetBlocks;
192 // Map from outlined handler to call to parent local address. Only used for
194 DenseMap<Function *, Value *> HandlerToParentFP;
196 AllocaInst *SEHExceptionCodeSlot = nullptr;
198 std::map<BasicBlock *, std::set<BasicBlock *>> BlockColors;
199 std::map<BasicBlock *, std::set<BasicBlock *>> FuncletBlocks;
200 std::map<BasicBlock *, std::set<BasicBlock *>> FuncletChildren;
203 class WinEHFrameVariableMaterializer : public ValueMaterializer {
205 WinEHFrameVariableMaterializer(Function *OutlinedFn, Value *ParentFP,
206 FrameVarInfoMap &FrameVarInfo);
207 ~WinEHFrameVariableMaterializer() override {}
209 Value *materializeValueFor(Value *V) override;
211 void escapeCatchObject(Value *V);
214 FrameVarInfoMap &FrameVarInfo;
218 class LandingPadMap {
220 LandingPadMap() : OriginLPad(nullptr) {}
221 void mapLandingPad(const LandingPadInst *LPad);
223 bool isInitialized() { return OriginLPad != nullptr; }
225 bool isOriginLandingPadBlock(const BasicBlock *BB) const;
226 bool isLandingPadSpecificInst(const Instruction *Inst) const;
228 void remapEHValues(ValueToValueMapTy &VMap, Value *EHPtrValue,
229 Value *SelectorValue) const;
232 const LandingPadInst *OriginLPad;
233 // We will normally only see one of each of these instructions, but
234 // if more than one occurs for some reason we can handle that.
235 TinyPtrVector<const ExtractValueInst *> ExtractedEHPtrs;
236 TinyPtrVector<const ExtractValueInst *> ExtractedSelectors;
239 class WinEHCloningDirectorBase : public CloningDirector {
241 WinEHCloningDirectorBase(Function *HandlerFn, Value *ParentFP,
242 FrameVarInfoMap &VarInfo, LandingPadMap &LPadMap)
243 : Materializer(HandlerFn, ParentFP, VarInfo),
244 SelectorIDType(Type::getInt32Ty(HandlerFn->getContext())),
245 Int8PtrType(Type::getInt8PtrTy(HandlerFn->getContext())),
246 LPadMap(LPadMap), ParentFP(ParentFP) {}
248 CloningAction handleInstruction(ValueToValueMapTy &VMap,
249 const Instruction *Inst,
250 BasicBlock *NewBB) override;
252 virtual CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
253 const Instruction *Inst,
254 BasicBlock *NewBB) = 0;
255 virtual CloningAction handleEndCatch(ValueToValueMapTy &VMap,
256 const Instruction *Inst,
257 BasicBlock *NewBB) = 0;
258 virtual CloningAction handleTypeIdFor(ValueToValueMapTy &VMap,
259 const Instruction *Inst,
260 BasicBlock *NewBB) = 0;
261 virtual CloningAction handleIndirectBr(ValueToValueMapTy &VMap,
262 const IndirectBrInst *IBr,
263 BasicBlock *NewBB) = 0;
264 virtual CloningAction handleInvoke(ValueToValueMapTy &VMap,
265 const InvokeInst *Invoke,
266 BasicBlock *NewBB) = 0;
267 virtual CloningAction handleResume(ValueToValueMapTy &VMap,
268 const ResumeInst *Resume,
269 BasicBlock *NewBB) = 0;
270 virtual CloningAction handleCompare(ValueToValueMapTy &VMap,
271 const CmpInst *Compare,
272 BasicBlock *NewBB) = 0;
273 virtual CloningAction handleLandingPad(ValueToValueMapTy &VMap,
274 const LandingPadInst *LPad,
275 BasicBlock *NewBB) = 0;
277 ValueMaterializer *getValueMaterializer() override { return &Materializer; }
280 WinEHFrameVariableMaterializer Materializer;
281 Type *SelectorIDType;
283 LandingPadMap &LPadMap;
285 /// The value representing the parent frame pointer.
289 class WinEHCatchDirector : public WinEHCloningDirectorBase {
292 Function *CatchFn, Value *ParentFP, Value *Selector,
293 FrameVarInfoMap &VarInfo, LandingPadMap &LPadMap,
294 DenseMap<LandingPadInst *, const LandingPadInst *> &NestedLPads,
295 DominatorTree *DT, SmallPtrSetImpl<BasicBlock *> &EHBlocks)
296 : WinEHCloningDirectorBase(CatchFn, ParentFP, VarInfo, LPadMap),
297 CurrentSelector(Selector->stripPointerCasts()),
298 ExceptionObjectVar(nullptr), NestedLPtoOriginalLP(NestedLPads),
299 DT(DT), EHBlocks(EHBlocks) {}
301 CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
302 const Instruction *Inst,
303 BasicBlock *NewBB) override;
304 CloningAction handleEndCatch(ValueToValueMapTy &VMap, const Instruction *Inst,
305 BasicBlock *NewBB) override;
306 CloningAction handleTypeIdFor(ValueToValueMapTy &VMap,
307 const Instruction *Inst,
308 BasicBlock *NewBB) override;
309 CloningAction handleIndirectBr(ValueToValueMapTy &VMap,
310 const IndirectBrInst *IBr,
311 BasicBlock *NewBB) override;
312 CloningAction handleInvoke(ValueToValueMapTy &VMap, const InvokeInst *Invoke,
313 BasicBlock *NewBB) override;
314 CloningAction handleResume(ValueToValueMapTy &VMap, const ResumeInst *Resume,
315 BasicBlock *NewBB) override;
316 CloningAction handleCompare(ValueToValueMapTy &VMap, const CmpInst *Compare,
317 BasicBlock *NewBB) override;
318 CloningAction handleLandingPad(ValueToValueMapTy &VMap,
319 const LandingPadInst *LPad,
320 BasicBlock *NewBB) override;
322 Value *getExceptionVar() { return ExceptionObjectVar; }
323 TinyPtrVector<BasicBlock *> &getReturnTargets() { return ReturnTargets; }
326 Value *CurrentSelector;
328 Value *ExceptionObjectVar;
329 TinyPtrVector<BasicBlock *> ReturnTargets;
331 // This will be a reference to the field of the same name in the WinEHPrepare
332 // object which instantiates this WinEHCatchDirector object.
333 DenseMap<LandingPadInst *, const LandingPadInst *> &NestedLPtoOriginalLP;
335 SmallPtrSetImpl<BasicBlock *> &EHBlocks;
338 class WinEHCleanupDirector : public WinEHCloningDirectorBase {
340 WinEHCleanupDirector(Function *CleanupFn, Value *ParentFP,
341 FrameVarInfoMap &VarInfo, LandingPadMap &LPadMap)
342 : WinEHCloningDirectorBase(CleanupFn, ParentFP, VarInfo,
345 CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
346 const Instruction *Inst,
347 BasicBlock *NewBB) override;
348 CloningAction handleEndCatch(ValueToValueMapTy &VMap, const Instruction *Inst,
349 BasicBlock *NewBB) override;
350 CloningAction handleTypeIdFor(ValueToValueMapTy &VMap,
351 const Instruction *Inst,
352 BasicBlock *NewBB) override;
353 CloningAction handleIndirectBr(ValueToValueMapTy &VMap,
354 const IndirectBrInst *IBr,
355 BasicBlock *NewBB) override;
356 CloningAction handleInvoke(ValueToValueMapTy &VMap, const InvokeInst *Invoke,
357 BasicBlock *NewBB) override;
358 CloningAction handleResume(ValueToValueMapTy &VMap, const ResumeInst *Resume,
359 BasicBlock *NewBB) override;
360 CloningAction handleCompare(ValueToValueMapTy &VMap, const CmpInst *Compare,
361 BasicBlock *NewBB) override;
362 CloningAction handleLandingPad(ValueToValueMapTy &VMap,
363 const LandingPadInst *LPad,
364 BasicBlock *NewBB) override;
367 class LandingPadActions {
369 LandingPadActions() : HasCleanupHandlers(false) {}
371 void insertCatchHandler(CatchHandler *Action) { Actions.push_back(Action); }
372 void insertCleanupHandler(CleanupHandler *Action) {
373 Actions.push_back(Action);
374 HasCleanupHandlers = true;
377 bool includesCleanup() const { return HasCleanupHandlers; }
379 SmallVectorImpl<ActionHandler *> &actions() { return Actions; }
380 SmallVectorImpl<ActionHandler *>::iterator begin() { return Actions.begin(); }
381 SmallVectorImpl<ActionHandler *>::iterator end() { return Actions.end(); }
384 // Note that this class does not own the ActionHandler objects in this vector.
385 // The ActionHandlers are owned by the CatchHandlerMap and CleanupHandlerMap
386 // in the WinEHPrepare class.
387 SmallVector<ActionHandler *, 4> Actions;
388 bool HasCleanupHandlers;
391 } // end anonymous namespace
393 char WinEHPrepare::ID = 0;
394 INITIALIZE_TM_PASS(WinEHPrepare, "winehprepare", "Prepare Windows exceptions",
397 FunctionPass *llvm::createWinEHPass(const TargetMachine *TM) {
398 return new WinEHPrepare(TM);
401 bool WinEHPrepare::runOnFunction(Function &Fn) {
402 if (!Fn.hasPersonalityFn())
405 // No need to prepare outlined handlers.
406 if (Fn.hasFnAttribute("wineh-parent"))
409 // Classify the personality to see what kind of preparation we need.
410 Personality = classifyEHPersonality(Fn.getPersonalityFn());
412 // Do nothing if this is not an MSVC personality.
413 if (!isMSVCEHPersonality(Personality))
416 SmallVector<LandingPadInst *, 4> LPads;
417 SmallVector<ResumeInst *, 4> Resumes;
418 SmallVector<BasicBlock *, 4> EntryBlocks;
419 bool ForExplicitEH = false;
420 for (BasicBlock &BB : Fn) {
421 Instruction *First = BB.getFirstNonPHI();
422 if (auto *LP = dyn_cast<LandingPadInst>(First)) {
424 } else if (First->isEHPad()) {
426 EntryBlocks.push_back(&Fn.getEntryBlock());
427 if (!isa<CatchEndPadInst>(First) && !isa<CleanupEndPadInst>(First))
428 EntryBlocks.push_back(&BB);
429 ForExplicitEH = true;
431 if (auto *Resume = dyn_cast<ResumeInst>(BB.getTerminator()))
432 Resumes.push_back(Resume);
436 return prepareExplicitEH(Fn, EntryBlocks);
438 // No need to prepare functions that lack landing pads.
442 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
443 LibInfo = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
445 // If there were any landing pads, prepareExceptionHandlers will make changes.
446 prepareExceptionHandlers(Fn, LPads);
450 bool WinEHPrepare::doFinalization(Module &M) { return false; }
452 void WinEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const {
453 AU.addRequired<DominatorTreeWrapperPass>();
454 AU.addRequired<TargetLibraryInfoWrapperPass>();
457 static bool isSelectorDispatch(BasicBlock *BB, BasicBlock *&CatchHandler,
458 Constant *&Selector, BasicBlock *&NextBB);
460 // Finds blocks reachable from the starting set Worklist. Does not follow unwind
461 // edges or blocks listed in StopPoints.
462 static void findReachableBlocks(SmallPtrSetImpl<BasicBlock *> &ReachableBBs,
463 SetVector<BasicBlock *> &Worklist,
464 const SetVector<BasicBlock *> *StopPoints) {
465 while (!Worklist.empty()) {
466 BasicBlock *BB = Worklist.pop_back_val();
468 // Don't cross blocks that we should stop at.
469 if (StopPoints && StopPoints->count(BB))
472 if (!ReachableBBs.insert(BB).second)
473 continue; // Already visited.
475 // Don't follow unwind edges of invokes.
476 if (auto *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
477 Worklist.insert(II->getNormalDest());
481 // Otherwise, follow all successors.
482 Worklist.insert(succ_begin(BB), succ_end(BB));
486 // Attempt to find an instruction where a block can be split before
487 // a call to llvm.eh.begincatch and its operands. If the block
488 // begins with the begincatch call or one of its adjacent operands
489 // the block will not be split.
490 static Instruction *findBeginCatchSplitPoint(BasicBlock *BB,
492 // If the begincatch call is already the first instruction in the block,
494 Instruction *FirstNonPHI = BB->getFirstNonPHI();
495 if (II == FirstNonPHI)
498 // If either operand is in the same basic block as the instruction and
499 // isn't used by another instruction before the begincatch call, include it
500 // in the split block.
501 auto *Op0 = dyn_cast<Instruction>(II->getOperand(0));
502 auto *Op1 = dyn_cast<Instruction>(II->getOperand(1));
504 Instruction *I = II->getPrevNode();
505 Instruction *LastI = II;
507 while (I == Op0 || I == Op1) {
508 // If the block begins with one of the operands and there are no other
509 // instructions between the operand and the begincatch call, don't split.
510 if (I == FirstNonPHI)
514 I = I->getPrevNode();
517 // If there is at least one instruction in the block before the begincatch
518 // call and its operands, split the block at either the begincatch or
523 /// Find all points where exceptional control rejoins normal control flow via
524 /// llvm.eh.endcatch. Add them to the normal bb reachability worklist.
525 void WinEHPrepare::findCXXEHReturnPoints(
526 Function &F, SetVector<BasicBlock *> &EHReturnBlocks) {
527 for (auto BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI) {
528 BasicBlock *BB = BBI;
529 for (Instruction &I : *BB) {
530 if (match(&I, m_Intrinsic<Intrinsic::eh_begincatch>())) {
531 Instruction *SplitPt =
532 findBeginCatchSplitPoint(BB, cast<IntrinsicInst>(&I));
534 // Split the block before the llvm.eh.begincatch call to allow
535 // cleanup and catch code to be distinguished later.
536 // Do not update BBI because we still need to process the
537 // portion of the block that we are splitting off.
538 SplitBlock(BB, SplitPt, DT);
542 if (match(&I, m_Intrinsic<Intrinsic::eh_endcatch>())) {
543 // Split the block after the call to llvm.eh.endcatch if there is
544 // anything other than an unconditional branch, or if the successor
545 // starts with a phi.
546 auto *Br = dyn_cast<BranchInst>(I.getNextNode());
547 if (!Br || !Br->isUnconditional() ||
548 isa<PHINode>(Br->getSuccessor(0)->begin())) {
549 DEBUG(dbgs() << "splitting block " << BB->getName()
550 << " with llvm.eh.endcatch\n");
551 BBI = SplitBlock(BB, I.getNextNode(), DT);
553 // The next BB is normal control flow.
554 EHReturnBlocks.insert(BB->getTerminator()->getSuccessor(0));
561 static bool isCatchAllLandingPad(const BasicBlock *BB) {
562 const LandingPadInst *LP = BB->getLandingPadInst();
565 unsigned N = LP->getNumClauses();
566 return (N > 0 && LP->isCatch(N - 1) &&
567 isa<ConstantPointerNull>(LP->getClause(N - 1)));
570 /// Find all points where exceptions control rejoins normal control flow via
571 /// selector dispatch.
572 void WinEHPrepare::findSEHEHReturnPoints(
573 Function &F, SetVector<BasicBlock *> &EHReturnBlocks) {
574 for (auto BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI) {
575 BasicBlock *BB = BBI;
576 // If the landingpad is a catch-all, treat the whole lpad as if it is
577 // reachable from normal control flow.
578 // FIXME: This is imprecise. We need a better way of identifying where a
579 // catch-all starts and cleanups stop. As far as LLVM is concerned, there
581 if (isCatchAllLandingPad(BB)) {
582 EHReturnBlocks.insert(BB);
586 BasicBlock *CatchHandler;
589 if (isSelectorDispatch(BB, CatchHandler, Selector, NextBB)) {
590 // Split the edge if there are multiple predecessors. This creates a place
591 // where we can insert EH recovery code.
592 if (!CatchHandler->getSinglePredecessor()) {
593 DEBUG(dbgs() << "splitting EH return edge from " << BB->getName()
594 << " to " << CatchHandler->getName() << '\n');
595 BBI = CatchHandler = SplitCriticalEdge(
596 BB, std::find(succ_begin(BB), succ_end(BB), CatchHandler));
598 EHReturnBlocks.insert(CatchHandler);
603 void WinEHPrepare::identifyEHBlocks(Function &F,
604 SmallVectorImpl<LandingPadInst *> &LPads) {
605 DEBUG(dbgs() << "Demoting values live across exception handlers in function "
606 << F.getName() << '\n');
608 // Build a set of all non-exceptional blocks and exceptional blocks.
609 // - Non-exceptional blocks are blocks reachable from the entry block while
610 // not following invoke unwind edges.
611 // - Exceptional blocks are blocks reachable from landingpads. Analysis does
612 // not follow llvm.eh.endcatch blocks, which mark a transition from
613 // exceptional to normal control.
615 if (Personality == EHPersonality::MSVC_CXX)
616 findCXXEHReturnPoints(F, EHReturnBlocks);
618 findSEHEHReturnPoints(F, EHReturnBlocks);
621 dbgs() << "identified the following blocks as EH return points:\n";
622 for (BasicBlock *BB : EHReturnBlocks)
623 dbgs() << " " << BB->getName() << '\n';
626 // Join points should not have phis at this point, unless they are a
627 // landingpad, in which case we will demote their phis later.
629 for (BasicBlock *BB : EHReturnBlocks)
630 assert((BB->isLandingPad() || !isa<PHINode>(BB->begin())) &&
631 "non-lpad EH return block has phi");
634 // Normal blocks are the blocks reachable from the entry block and all EH
636 SetVector<BasicBlock *> Worklist;
637 Worklist = EHReturnBlocks;
638 Worklist.insert(&F.getEntryBlock());
639 findReachableBlocks(NormalBlocks, Worklist, nullptr);
641 dbgs() << "marked the following blocks as normal:\n";
642 for (BasicBlock *BB : NormalBlocks)
643 dbgs() << " " << BB->getName() << '\n';
646 // Exceptional blocks are the blocks reachable from landingpads that don't
647 // cross EH return points.
649 for (auto *LPI : LPads)
650 Worklist.insert(LPI->getParent());
651 findReachableBlocks(EHBlocks, Worklist, &EHReturnBlocks);
653 dbgs() << "marked the following blocks as exceptional:\n";
654 for (BasicBlock *BB : EHBlocks)
655 dbgs() << " " << BB->getName() << '\n';
660 /// Ensure that all values live into and out of exception handlers are stored
662 /// FIXME: This falls down when values are defined in one handler and live into
663 /// another handler. For example, a cleanup defines a value used only by a
665 void WinEHPrepare::demoteValuesLiveAcrossHandlers(
666 Function &F, SmallVectorImpl<LandingPadInst *> &LPads) {
667 DEBUG(dbgs() << "Demoting values live across exception handlers in function "
668 << F.getName() << '\n');
670 // identifyEHBlocks() should have been called before this function.
671 assert(!NormalBlocks.empty());
673 // Try to avoid demoting EH pointer and selector values. They get in the way
674 // of our pattern matching.
675 SmallPtrSet<Instruction *, 10> EHVals;
676 for (BasicBlock &BB : F) {
677 LandingPadInst *LP = BB.getLandingPadInst();
681 for (User *U : LP->users()) {
682 auto *EI = dyn_cast<ExtractValueInst>(U);
686 for (User *U2 : EI->users()) {
687 if (auto *PN = dyn_cast<PHINode>(U2))
693 SetVector<Argument *> ArgsToDemote;
694 SetVector<Instruction *> InstrsToDemote;
695 for (BasicBlock &BB : F) {
696 bool IsNormalBB = NormalBlocks.count(&BB);
697 bool IsEHBB = EHBlocks.count(&BB);
698 if (!IsNormalBB && !IsEHBB)
699 continue; // Blocks that are neither normal nor EH are unreachable.
700 for (Instruction &I : BB) {
701 for (Value *Op : I.operands()) {
702 // Don't demote static allocas, constants, and labels.
703 if (isa<Constant>(Op) || isa<BasicBlock>(Op) || isa<InlineAsm>(Op))
705 auto *AI = dyn_cast<AllocaInst>(Op);
706 if (AI && AI->isStaticAlloca())
709 if (auto *Arg = dyn_cast<Argument>(Op)) {
711 DEBUG(dbgs() << "Demoting argument " << *Arg
712 << " used by EH instr: " << I << "\n");
713 ArgsToDemote.insert(Arg);
718 // Don't demote EH values.
719 auto *OpI = cast<Instruction>(Op);
720 if (EHVals.count(OpI))
723 BasicBlock *OpBB = OpI->getParent();
724 // If a value is produced and consumed in the same BB, we don't need to
728 bool IsOpNormalBB = NormalBlocks.count(OpBB);
729 bool IsOpEHBB = EHBlocks.count(OpBB);
730 if (IsNormalBB != IsOpNormalBB || IsEHBB != IsOpEHBB) {
732 dbgs() << "Demoting instruction live in-out from EH:\n";
733 dbgs() << "Instr: " << *OpI << '\n';
734 dbgs() << "User: " << I << '\n';
736 InstrsToDemote.insert(OpI);
742 // Demote values live into and out of handlers.
743 // FIXME: This demotion is inefficient. We should insert spills at the point
744 // of definition, insert one reload in each handler that uses the value, and
745 // insert reloads in the BB used to rejoin normal control flow.
746 Instruction *AllocaInsertPt = F.getEntryBlock().getFirstInsertionPt();
747 for (Instruction *I : InstrsToDemote)
748 DemoteRegToStack(*I, false, AllocaInsertPt);
750 // Demote arguments separately, and only for uses in EH blocks.
751 for (Argument *Arg : ArgsToDemote) {
752 auto *Slot = new AllocaInst(Arg->getType(), nullptr,
753 Arg->getName() + ".reg2mem", AllocaInsertPt);
754 SmallVector<User *, 4> Users(Arg->user_begin(), Arg->user_end());
755 for (User *U : Users) {
756 auto *I = dyn_cast<Instruction>(U);
757 if (I && EHBlocks.count(I->getParent())) {
758 auto *Reload = new LoadInst(Slot, Arg->getName() + ".reload", false, I);
759 U->replaceUsesOfWith(Arg, Reload);
762 new StoreInst(Arg, Slot, AllocaInsertPt);
765 // Demote landingpad phis, as the landingpad will be removed from the machine
767 for (LandingPadInst *LPI : LPads) {
768 BasicBlock *BB = LPI->getParent();
769 while (auto *Phi = dyn_cast<PHINode>(BB->begin()))
770 DemotePHIToStack(Phi, AllocaInsertPt);
773 DEBUG(dbgs() << "Demoted " << InstrsToDemote.size() << " instructions and "
774 << ArgsToDemote.size() << " arguments for WinEHPrepare\n\n");
777 bool WinEHPrepare::prepareExceptionHandlers(
778 Function &F, SmallVectorImpl<LandingPadInst *> &LPads) {
779 // Don't run on functions that are already prepared.
780 for (LandingPadInst *LPad : LPads) {
781 BasicBlock *LPadBB = LPad->getParent();
782 for (Instruction &Inst : *LPadBB)
783 if (match(&Inst, m_Intrinsic<Intrinsic::eh_actions>()))
787 identifyEHBlocks(F, LPads);
788 demoteValuesLiveAcrossHandlers(F, LPads);
790 // These containers are used to re-map frame variables that are used in
791 // outlined catch and cleanup handlers. They will be populated as the
792 // handlers are outlined.
793 FrameVarInfoMap FrameVarInfo;
795 bool HandlersOutlined = false;
797 Module *M = F.getParent();
798 LLVMContext &Context = M->getContext();
800 // Create a new function to receive the handler contents.
801 PointerType *Int8PtrType = Type::getInt8PtrTy(Context);
802 Type *Int32Type = Type::getInt32Ty(Context);
803 Function *ActionIntrin = Intrinsic::getDeclaration(M, Intrinsic::eh_actions);
805 if (isAsynchronousEHPersonality(Personality)) {
806 // FIXME: Switch the ehptr type to i32 and then switch this.
807 SEHExceptionCodeSlot =
808 new AllocaInst(Int8PtrType, nullptr, "seh_exception_code",
809 F.getEntryBlock().getFirstInsertionPt());
812 // In order to handle the case where one outlined catch handler returns
813 // to a block within another outlined catch handler that would otherwise
814 // be unreachable, we need to outline the nested landing pad before we
815 // outline the landing pad which encloses it.
816 if (!isAsynchronousEHPersonality(Personality))
817 std::sort(LPads.begin(), LPads.end(),
818 [this](LandingPadInst *const &L, LandingPadInst *const &R) {
819 return DT->properlyDominates(R->getParent(), L->getParent());
822 // This container stores the llvm.eh.recover and IndirectBr instructions
823 // that make up the body of each landing pad after it has been outlined.
824 // We need to defer the population of the target list for the indirectbr
825 // until all landing pads have been outlined so that we can handle the
826 // case of blocks in the target that are reached only from nested
828 SmallVector<std::pair<CallInst*, IndirectBrInst *>, 4> LPadImpls;
830 for (LandingPadInst *LPad : LPads) {
831 // Look for evidence that this landingpad has already been processed.
832 bool LPadHasActionList = false;
833 BasicBlock *LPadBB = LPad->getParent();
834 for (Instruction &Inst : *LPadBB) {
835 if (match(&Inst, m_Intrinsic<Intrinsic::eh_actions>())) {
836 LPadHasActionList = true;
841 // If we've already outlined the handlers for this landingpad,
842 // there's nothing more to do here.
843 if (LPadHasActionList)
846 // If either of the values in the aggregate returned by the landing pad is
847 // extracted and stored to memory, promote the stored value to a register.
848 promoteLandingPadValues(LPad);
850 LandingPadActions Actions;
851 mapLandingPadBlocks(LPad, Actions);
853 HandlersOutlined |= !Actions.actions().empty();
854 for (ActionHandler *Action : Actions) {
855 if (Action->hasBeenProcessed())
857 BasicBlock *StartBB = Action->getStartBlock();
859 // SEH doesn't do any outlining for catches. Instead, pass the handler
860 // basic block addr to llvm.eh.actions and list the block as a return
862 if (isAsynchronousEHPersonality(Personality)) {
863 if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
864 processSEHCatchHandler(CatchAction, StartBB);
869 outlineHandler(Action, &F, LPad, StartBB, FrameVarInfo);
872 // Split the block after the landingpad instruction so that it is just a
873 // call to llvm.eh.actions followed by indirectbr.
874 assert(!isa<PHINode>(LPadBB->begin()) && "lpad phi not removed");
875 SplitBlock(LPadBB, LPad->getNextNode(), DT);
876 // Erase the branch inserted by the split so we can insert indirectbr.
877 LPadBB->getTerminator()->eraseFromParent();
879 // Replace all extracted values with undef and ultimately replace the
880 // landingpad with undef.
881 SmallVector<Instruction *, 4> SEHCodeUses;
882 SmallVector<Instruction *, 4> EHUndefs;
883 for (User *U : LPad->users()) {
884 auto *E = dyn_cast<ExtractValueInst>(U);
887 assert(E->getNumIndices() == 1 &&
888 "Unexpected operation: extracting both landing pad values");
889 unsigned Idx = *E->idx_begin();
890 assert((Idx == 0 || Idx == 1) && "unexpected index");
891 if (Idx == 0 && isAsynchronousEHPersonality(Personality))
892 SEHCodeUses.push_back(E);
894 EHUndefs.push_back(E);
896 for (Instruction *E : EHUndefs) {
897 E->replaceAllUsesWith(UndefValue::get(E->getType()));
898 E->eraseFromParent();
900 LPad->replaceAllUsesWith(UndefValue::get(LPad->getType()));
902 // Rewrite uses of the exception pointer to loads of an alloca.
903 while (!SEHCodeUses.empty()) {
904 Instruction *E = SEHCodeUses.pop_back_val();
905 SmallVector<Use *, 4> Uses;
906 for (Use &U : E->uses())
908 for (Use *U : Uses) {
909 auto *I = cast<Instruction>(U->getUser());
910 if (isa<ResumeInst>(I))
912 if (auto *Phi = dyn_cast<PHINode>(I))
913 SEHCodeUses.push_back(Phi);
915 U->set(new LoadInst(SEHExceptionCodeSlot, "sehcode", false, I));
917 E->replaceAllUsesWith(UndefValue::get(E->getType()));
918 E->eraseFromParent();
921 // Add a call to describe the actions for this landing pad.
922 std::vector<Value *> ActionArgs;
923 for (ActionHandler *Action : Actions) {
924 // Action codes from docs are: 0 cleanup, 1 catch.
925 if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
926 ActionArgs.push_back(ConstantInt::get(Int32Type, 1));
927 ActionArgs.push_back(CatchAction->getSelector());
928 // Find the frame escape index of the exception object alloca in the
930 int FrameEscapeIdx = -1;
931 Value *EHObj = const_cast<Value *>(CatchAction->getExceptionVar());
932 if (EHObj && !isa<ConstantPointerNull>(EHObj)) {
933 auto I = FrameVarInfo.find(EHObj);
934 assert(I != FrameVarInfo.end() &&
935 "failed to map llvm.eh.begincatch var");
936 FrameEscapeIdx = std::distance(FrameVarInfo.begin(), I);
938 ActionArgs.push_back(ConstantInt::get(Int32Type, FrameEscapeIdx));
940 ActionArgs.push_back(ConstantInt::get(Int32Type, 0));
942 ActionArgs.push_back(Action->getHandlerBlockOrFunc());
945 CallInst::Create(ActionIntrin, ActionArgs, "recover", LPadBB);
947 SetVector<BasicBlock *> ReturnTargets;
948 for (ActionHandler *Action : Actions) {
949 if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
950 const auto &CatchTargets = CatchAction->getReturnTargets();
951 ReturnTargets.insert(CatchTargets.begin(), CatchTargets.end());
954 IndirectBrInst *Branch =
955 IndirectBrInst::Create(Recover, ReturnTargets.size(), LPadBB);
956 for (BasicBlock *Target : ReturnTargets)
957 Branch->addDestination(Target);
959 if (!isAsynchronousEHPersonality(Personality)) {
960 // C++ EH must repopulate the targets later to handle the case of
961 // targets that are reached indirectly through nested landing pads.
962 LPadImpls.push_back(std::make_pair(Recover, Branch));
965 } // End for each landingpad
967 // If nothing got outlined, there is no more processing to be done.
968 if (!HandlersOutlined)
971 // Replace any nested landing pad stubs with the correct action handler.
972 // This must be done before we remove unreachable blocks because it
973 // cleans up references to outlined blocks that will be deleted.
974 for (auto &LPadPair : NestedLPtoOriginalLP)
975 completeNestedLandingPad(&F, LPadPair.first, LPadPair.second, FrameVarInfo);
976 NestedLPtoOriginalLP.clear();
978 // Update the indirectbr instructions' target lists if necessary.
979 SetVector<BasicBlock*> CheckedTargets;
980 SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList;
981 for (auto &LPadImplPair : LPadImpls) {
982 IntrinsicInst *Recover = cast<IntrinsicInst>(LPadImplPair.first);
983 IndirectBrInst *Branch = LPadImplPair.second;
985 // Get a list of handlers called by
986 parseEHActions(Recover, ActionList);
988 // Add an indirect branch listing possible successors of the catch handlers.
989 SetVector<BasicBlock *> ReturnTargets;
990 for (const auto &Action : ActionList) {
991 if (auto *CA = dyn_cast<CatchHandler>(Action.get())) {
992 Function *Handler = cast<Function>(CA->getHandlerBlockOrFunc());
993 getPossibleReturnTargets(&F, Handler, ReturnTargets);
997 // Clear any targets we already knew about.
998 for (unsigned int I = 0, E = Branch->getNumDestinations(); I < E; ++I) {
999 BasicBlock *KnownTarget = Branch->getDestination(I);
1000 if (ReturnTargets.count(KnownTarget))
1001 ReturnTargets.remove(KnownTarget);
1003 for (BasicBlock *Target : ReturnTargets) {
1004 Branch->addDestination(Target);
1005 // The target may be a block that we excepted to get pruned.
1006 // If it is, it may contain a call to llvm.eh.endcatch.
1007 if (CheckedTargets.insert(Target)) {
1008 // Earlier preparations guarantee that all calls to llvm.eh.endcatch
1009 // will be followed by an unconditional branch.
1010 auto *Br = dyn_cast<BranchInst>(Target->getTerminator());
1011 if (Br && Br->isUnconditional() &&
1012 Br != Target->getFirstNonPHIOrDbgOrLifetime()) {
1013 Instruction *Prev = Br->getPrevNode();
1014 if (match(cast<Value>(Prev), m_Intrinsic<Intrinsic::eh_endcatch>()))
1015 Prev->eraseFromParent();
1022 F.addFnAttr("wineh-parent", F.getName());
1024 // Delete any blocks that were only used by handlers that were outlined above.
1025 removeUnreachableBlocks(F);
1027 BasicBlock *Entry = &F.getEntryBlock();
1028 IRBuilder<> Builder(F.getParent()->getContext());
1029 Builder.SetInsertPoint(Entry->getFirstInsertionPt());
1031 Function *FrameEscapeFn =
1032 Intrinsic::getDeclaration(M, Intrinsic::localescape);
1033 Function *RecoverFrameFn =
1034 Intrinsic::getDeclaration(M, Intrinsic::localrecover);
1035 SmallVector<Value *, 8> AllocasToEscape;
1037 // Scan the entry block for an existing call to llvm.localescape. We need to
1038 // keep escaping those objects.
1039 for (Instruction &I : F.front()) {
1040 auto *II = dyn_cast<IntrinsicInst>(&I);
1041 if (II && II->getIntrinsicID() == Intrinsic::localescape) {
1042 auto Args = II->arg_operands();
1043 AllocasToEscape.append(Args.begin(), Args.end());
1044 II->eraseFromParent();
1049 // Finally, replace all of the temporary allocas for frame variables used in
1050 // the outlined handlers with calls to llvm.localrecover.
1051 for (auto &VarInfoEntry : FrameVarInfo) {
1052 Value *ParentVal = VarInfoEntry.first;
1053 TinyPtrVector<AllocaInst *> &Allocas = VarInfoEntry.second;
1054 AllocaInst *ParentAlloca = cast<AllocaInst>(ParentVal);
1056 // FIXME: We should try to sink unescaped allocas from the parent frame into
1057 // the child frame. If the alloca is escaped, we have to use the lifetime
1058 // markers to ensure that the alloca is only live within the child frame.
1060 // Add this alloca to the list of things to escape.
1061 AllocasToEscape.push_back(ParentAlloca);
1063 // Next replace all outlined allocas that are mapped to it.
1064 for (AllocaInst *TempAlloca : Allocas) {
1065 if (TempAlloca == getCatchObjectSentinel())
1066 continue; // Skip catch parameter sentinels.
1067 Function *HandlerFn = TempAlloca->getParent()->getParent();
1068 llvm::Value *FP = HandlerToParentFP[HandlerFn];
1071 // FIXME: Sink this localrecover into the blocks where it is used.
1072 Builder.SetInsertPoint(TempAlloca);
1073 Builder.SetCurrentDebugLocation(TempAlloca->getDebugLoc());
1074 Value *RecoverArgs[] = {
1075 Builder.CreateBitCast(&F, Int8PtrType, ""), FP,
1076 llvm::ConstantInt::get(Int32Type, AllocasToEscape.size() - 1)};
1077 Instruction *RecoveredAlloca =
1078 Builder.CreateCall(RecoverFrameFn, RecoverArgs);
1080 // Add a pointer bitcast if the alloca wasn't an i8.
1081 if (RecoveredAlloca->getType() != TempAlloca->getType()) {
1082 RecoveredAlloca->setName(Twine(TempAlloca->getName()) + ".i8");
1083 RecoveredAlloca = cast<Instruction>(
1084 Builder.CreateBitCast(RecoveredAlloca, TempAlloca->getType()));
1086 TempAlloca->replaceAllUsesWith(RecoveredAlloca);
1087 TempAlloca->removeFromParent();
1088 RecoveredAlloca->takeName(TempAlloca);
1091 } // End for each FrameVarInfo entry.
1093 // Insert 'call void (...)* @llvm.localescape(...)' at the end of the entry
1095 Builder.SetInsertPoint(&F.getEntryBlock().back());
1096 Builder.CreateCall(FrameEscapeFn, AllocasToEscape);
1098 if (SEHExceptionCodeSlot) {
1099 if (isAllocaPromotable(SEHExceptionCodeSlot)) {
1100 SmallPtrSet<BasicBlock *, 4> UserBlocks;
1101 for (User *U : SEHExceptionCodeSlot->users()) {
1102 if (auto *Inst = dyn_cast<Instruction>(U))
1103 UserBlocks.insert(Inst->getParent());
1105 PromoteMemToReg(SEHExceptionCodeSlot, *DT);
1106 // After the promotion, kill off dead instructions.
1107 for (BasicBlock *BB : UserBlocks)
1108 SimplifyInstructionsInBlock(BB, LibInfo);
1112 // Clean up the handler action maps we created for this function
1113 DeleteContainerSeconds(CatchHandlerMap);
1114 CatchHandlerMap.clear();
1115 DeleteContainerSeconds(CleanupHandlerMap);
1116 CleanupHandlerMap.clear();
1117 HandlerToParentFP.clear();
1120 SEHExceptionCodeSlot = nullptr;
1122 NormalBlocks.clear();
1123 EHReturnBlocks.clear();
1125 return HandlersOutlined;
1128 void WinEHPrepare::promoteLandingPadValues(LandingPadInst *LPad) {
1129 // If the return values of the landing pad instruction are extracted and
1130 // stored to memory, we want to promote the store locations to reg values.
1131 SmallVector<AllocaInst *, 2> EHAllocas;
1133 // The landingpad instruction returns an aggregate value. Typically, its
1134 // value will be passed to a pair of extract value instructions and the
1135 // results of those extracts are often passed to store instructions.
1136 // In unoptimized code the stored value will often be loaded and then stored
1138 for (auto *U : LPad->users()) {
1139 ExtractValueInst *Extract = dyn_cast<ExtractValueInst>(U);
1143 for (auto *EU : Extract->users()) {
1144 if (auto *Store = dyn_cast<StoreInst>(EU)) {
1145 auto *AV = cast<AllocaInst>(Store->getPointerOperand());
1146 EHAllocas.push_back(AV);
1151 // We can't do this without a dominator tree.
1154 if (!EHAllocas.empty()) {
1155 PromoteMemToReg(EHAllocas, *DT);
1159 // After promotion, some extracts may be trivially dead. Remove them.
1160 SmallVector<Value *, 4> Users(LPad->user_begin(), LPad->user_end());
1161 for (auto *U : Users)
1162 RecursivelyDeleteTriviallyDeadInstructions(U);
1165 void WinEHPrepare::getPossibleReturnTargets(Function *ParentF,
1167 SetVector<BasicBlock*> &Targets) {
1168 for (BasicBlock &BB : *HandlerF) {
1169 // If the handler contains landing pads, check for any
1170 // handlers that may return directly to a block in the
1172 if (auto *LPI = BB.getLandingPadInst()) {
1173 IntrinsicInst *Recover = cast<IntrinsicInst>(LPI->getNextNode());
1174 SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList;
1175 parseEHActions(Recover, ActionList);
1176 for (const auto &Action : ActionList) {
1177 if (auto *CH = dyn_cast<CatchHandler>(Action.get())) {
1178 Function *NestedF = cast<Function>(CH->getHandlerBlockOrFunc());
1179 getPossibleReturnTargets(ParentF, NestedF, Targets);
1184 auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator());
1188 // Handler functions must always return a block address.
1189 BlockAddress *BA = cast<BlockAddress>(Ret->getReturnValue());
1191 // If this is the handler for a nested landing pad, the
1192 // return address may have been remapped to a block in the
1193 // parent handler. We're not interested in those.
1194 if (BA->getFunction() != ParentF)
1197 Targets.insert(BA->getBasicBlock());
1201 void WinEHPrepare::completeNestedLandingPad(Function *ParentFn,
1202 LandingPadInst *OutlinedLPad,
1203 const LandingPadInst *OriginalLPad,
1204 FrameVarInfoMap &FrameVarInfo) {
1205 // Get the nested block and erase the unreachable instruction that was
1206 // temporarily inserted as its terminator.
1207 LLVMContext &Context = ParentFn->getContext();
1208 BasicBlock *OutlinedBB = OutlinedLPad->getParent();
1209 // If the nested landing pad was outlined before the landing pad that enclosed
1210 // it, it will already be in outlined form. In that case, we just need to see
1211 // if the returns and the enclosing branch instruction need to be updated.
1212 IndirectBrInst *Branch =
1213 dyn_cast<IndirectBrInst>(OutlinedBB->getTerminator());
1215 // If the landing pad wasn't in outlined form, it should be a stub with
1216 // an unreachable terminator.
1217 assert(isa<UnreachableInst>(OutlinedBB->getTerminator()));
1218 OutlinedBB->getTerminator()->eraseFromParent();
1219 // That should leave OutlinedLPad as the last instruction in its block.
1220 assert(&OutlinedBB->back() == OutlinedLPad);
1223 // The original landing pad will have already had its action intrinsic
1224 // built by the outlining loop. We need to clone that into the outlined
1225 // location. It may also be necessary to add references to the exception
1226 // variables to the outlined handler in which this landing pad is nested
1227 // and remap return instructions in the nested handlers that should return
1228 // to an address in the outlined handler.
1229 Function *OutlinedHandlerFn = OutlinedBB->getParent();
1230 BasicBlock::const_iterator II = OriginalLPad;
1232 // The instruction after the landing pad should now be a call to eh.actions.
1233 const Instruction *Recover = II;
1234 const IntrinsicInst *EHActions = cast<IntrinsicInst>(Recover);
1236 // Remap the return target in the nested handler.
1237 SmallVector<BlockAddress *, 4> ActionTargets;
1238 SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList;
1239 parseEHActions(EHActions, ActionList);
1240 for (const auto &Action : ActionList) {
1241 auto *Catch = dyn_cast<CatchHandler>(Action.get());
1244 // The dyn_cast to function here selects C++ catch handlers and skips
1245 // SEH catch handlers.
1246 auto *Handler = dyn_cast<Function>(Catch->getHandlerBlockOrFunc());
1249 // Visit all the return instructions, looking for places that return
1250 // to a location within OutlinedHandlerFn.
1251 for (BasicBlock &NestedHandlerBB : *Handler) {
1252 auto *Ret = dyn_cast<ReturnInst>(NestedHandlerBB.getTerminator());
1256 // Handler functions must always return a block address.
1257 BlockAddress *BA = cast<BlockAddress>(Ret->getReturnValue());
1258 // The original target will have been in the main parent function,
1259 // but if it is the address of a block that has been outlined, it
1260 // should be a block that was outlined into OutlinedHandlerFn.
1261 assert(BA->getFunction() == ParentFn);
1263 // Ignore targets that aren't part of an outlined handler function.
1264 if (!LPadTargetBlocks.count(BA->getBasicBlock()))
1267 // If the return value is the address ofF a block that we
1268 // previously outlined into the parent handler function, replace
1269 // the return instruction and add the mapped target to the list
1270 // of possible return addresses.
1271 BasicBlock *MappedBB = LPadTargetBlocks[BA->getBasicBlock()];
1272 assert(MappedBB->getParent() == OutlinedHandlerFn);
1273 BlockAddress *NewBA = BlockAddress::get(OutlinedHandlerFn, MappedBB);
1274 Ret->eraseFromParent();
1275 ReturnInst::Create(Context, NewBA, &NestedHandlerBB);
1276 ActionTargets.push_back(NewBA);
1282 // If the landing pad was already in outlined form, just update its targets.
1283 for (unsigned int I = Branch->getNumDestinations(); I > 0; --I)
1284 Branch->removeDestination(I);
1285 // Add the previously collected action targets.
1286 for (auto *Target : ActionTargets)
1287 Branch->addDestination(Target->getBasicBlock());
1289 // If the landing pad was previously stubbed out, fill in its outlined form.
1290 IntrinsicInst *NewEHActions = cast<IntrinsicInst>(EHActions->clone());
1291 OutlinedBB->getInstList().push_back(NewEHActions);
1293 // Insert an indirect branch into the outlined landing pad BB.
1294 IndirectBrInst *IBr = IndirectBrInst::Create(NewEHActions, 0, OutlinedBB);
1295 // Add the previously collected action targets.
1296 for (auto *Target : ActionTargets)
1297 IBr->addDestination(Target->getBasicBlock());
1301 // This function examines a block to determine whether the block ends with a
1302 // conditional branch to a catch handler based on a selector comparison.
1303 // This function is used both by the WinEHPrepare::findSelectorComparison() and
1304 // WinEHCleanupDirector::handleTypeIdFor().
1305 static bool isSelectorDispatch(BasicBlock *BB, BasicBlock *&CatchHandler,
1306 Constant *&Selector, BasicBlock *&NextBB) {
1307 ICmpInst::Predicate Pred;
1308 BasicBlock *TBB, *FBB;
1311 if (!match(BB->getTerminator(),
1312 m_Br(m_ICmp(Pred, m_Value(LHS), m_Value(RHS)), TBB, FBB)))
1316 m_Intrinsic<Intrinsic::eh_typeid_for>(m_Constant(Selector))) &&
1317 !match(RHS, m_Intrinsic<Intrinsic::eh_typeid_for>(m_Constant(Selector))))
1320 if (Pred == CmpInst::ICMP_EQ) {
1326 if (Pred == CmpInst::ICMP_NE) {
1335 static bool isCatchBlock(BasicBlock *BB) {
1336 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end();
1338 if (match(cast<Value>(II), m_Intrinsic<Intrinsic::eh_begincatch>()))
1344 static BasicBlock *createStubLandingPad(Function *Handler) {
1345 // FIXME: Finish this!
1346 LLVMContext &Context = Handler->getContext();
1347 BasicBlock *StubBB = BasicBlock::Create(Context, "stub");
1348 Handler->getBasicBlockList().push_back(StubBB);
1349 IRBuilder<> Builder(StubBB);
1350 LandingPadInst *LPad = Builder.CreateLandingPad(
1351 llvm::StructType::get(Type::getInt8PtrTy(Context),
1352 Type::getInt32Ty(Context), nullptr),
1354 // Insert a call to llvm.eh.actions so that we don't try to outline this lpad.
1355 Function *ActionIntrin =
1356 Intrinsic::getDeclaration(Handler->getParent(), Intrinsic::eh_actions);
1357 Builder.CreateCall(ActionIntrin, {}, "recover");
1358 LPad->setCleanup(true);
1359 Builder.CreateUnreachable();
1363 // Cycles through the blocks in an outlined handler function looking for an
1364 // invoke instruction and inserts an invoke of llvm.donothing with an empty
1365 // landing pad if none is found. The code that generates the .xdata tables for
1366 // the handler needs at least one landing pad to identify the parent function's
1368 void WinEHPrepare::addStubInvokeToHandlerIfNeeded(Function *Handler) {
1369 ReturnInst *Ret = nullptr;
1370 UnreachableInst *Unreached = nullptr;
1371 for (BasicBlock &BB : *Handler) {
1372 TerminatorInst *Terminator = BB.getTerminator();
1373 // If we find an invoke, there is nothing to be done.
1374 auto *II = dyn_cast<InvokeInst>(Terminator);
1377 // If we've already recorded a return instruction, keep looking for invokes.
1379 Ret = dyn_cast<ReturnInst>(Terminator);
1380 // If we haven't recorded an unreachable instruction, try this terminator.
1382 Unreached = dyn_cast<UnreachableInst>(Terminator);
1385 // If we got this far, the handler contains no invokes. We should have seen
1386 // at least one return or unreachable instruction. We'll insert an invoke of
1387 // llvm.donothing ahead of that instruction.
1388 assert(Ret || Unreached);
1389 TerminatorInst *Term;
1394 BasicBlock *OldRetBB = Term->getParent();
1395 BasicBlock *NewRetBB = SplitBlock(OldRetBB, Term, DT);
1396 // SplitBlock adds an unconditional branch instruction at the end of the
1397 // parent block. We want to replace that with an invoke call, so we can
1399 OldRetBB->getTerminator()->eraseFromParent();
1400 BasicBlock *StubLandingPad = createStubLandingPad(Handler);
1402 Intrinsic::getDeclaration(Handler->getParent(), Intrinsic::donothing);
1403 InvokeInst::Create(F, NewRetBB, StubLandingPad, None, "", OldRetBB);
1406 // FIXME: Consider sinking this into lib/Target/X86 somehow. TargetLowering
1407 // usually doesn't build LLVM IR, so that's probably the wrong place.
1408 Function *WinEHPrepare::createHandlerFunc(Function *ParentFn, Type *RetTy,
1409 const Twine &Name, Module *M,
1411 // x64 uses a two-argument prototype where the parent FP is the second
1412 // argument. x86 uses no arguments, just the incoming EBP value.
1413 LLVMContext &Context = M->getContext();
1414 Type *Int8PtrType = Type::getInt8PtrTy(Context);
1415 FunctionType *FnType;
1416 if (TheTriple.getArch() == Triple::x86_64) {
1417 Type *ArgTys[2] = {Int8PtrType, Int8PtrType};
1418 FnType = FunctionType::get(RetTy, ArgTys, false);
1420 FnType = FunctionType::get(RetTy, None, false);
1424 Function::Create(FnType, GlobalVariable::InternalLinkage, Name, M);
1425 BasicBlock *Entry = BasicBlock::Create(Context, "entry");
1426 Handler->getBasicBlockList().push_front(Entry);
1427 if (TheTriple.getArch() == Triple::x86_64) {
1428 ParentFP = &(Handler->getArgumentList().back());
1431 Function *FrameAddressFn =
1432 Intrinsic::getDeclaration(M, Intrinsic::frameaddress);
1433 Function *RecoverFPFn =
1434 Intrinsic::getDeclaration(M, Intrinsic::x86_seh_recoverfp);
1435 IRBuilder<> Builder(&Handler->getEntryBlock());
1437 Builder.CreateCall(FrameAddressFn, {Builder.getInt32(1)}, "ebp");
1438 Value *ParentI8Fn = Builder.CreateBitCast(ParentFn, Int8PtrType);
1439 ParentFP = Builder.CreateCall(RecoverFPFn, {ParentI8Fn, EBP});
1444 bool WinEHPrepare::outlineHandler(ActionHandler *Action, Function *SrcFn,
1445 LandingPadInst *LPad, BasicBlock *StartBB,
1446 FrameVarInfoMap &VarInfo) {
1447 Module *M = SrcFn->getParent();
1448 LLVMContext &Context = M->getContext();
1449 Type *Int8PtrType = Type::getInt8PtrTy(Context);
1451 // Create a new function to receive the handler contents.
1454 if (Action->getType() == Catch) {
1455 Handler = createHandlerFunc(SrcFn, Int8PtrType, SrcFn->getName() + ".catch", M,
1458 Handler = createHandlerFunc(SrcFn, Type::getVoidTy(Context),
1459 SrcFn->getName() + ".cleanup", M, ParentFP);
1461 Handler->setPersonalityFn(SrcFn->getPersonalityFn());
1462 HandlerToParentFP[Handler] = ParentFP;
1463 Handler->addFnAttr("wineh-parent", SrcFn->getName());
1464 BasicBlock *Entry = &Handler->getEntryBlock();
1466 // Generate a standard prolog to setup the frame recovery structure.
1467 IRBuilder<> Builder(Context);
1468 Builder.SetInsertPoint(Entry);
1469 Builder.SetCurrentDebugLocation(LPad->getDebugLoc());
1471 std::unique_ptr<WinEHCloningDirectorBase> Director;
1473 ValueToValueMapTy VMap;
1475 LandingPadMap &LPadMap = LPadMaps[LPad];
1476 if (!LPadMap.isInitialized())
1477 LPadMap.mapLandingPad(LPad);
1478 if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
1479 Constant *Sel = CatchAction->getSelector();
1480 Director.reset(new WinEHCatchDirector(Handler, ParentFP, Sel, VarInfo,
1481 LPadMap, NestedLPtoOriginalLP, DT,
1483 LPadMap.remapEHValues(VMap, UndefValue::get(Int8PtrType),
1484 ConstantInt::get(Type::getInt32Ty(Context), 1));
1487 new WinEHCleanupDirector(Handler, ParentFP, VarInfo, LPadMap));
1488 LPadMap.remapEHValues(VMap, UndefValue::get(Int8PtrType),
1489 UndefValue::get(Type::getInt32Ty(Context)));
1492 SmallVector<ReturnInst *, 8> Returns;
1493 ClonedCodeInfo OutlinedFunctionInfo;
1495 // If the start block contains PHI nodes, we need to map them.
1496 BasicBlock::iterator II = StartBB->begin();
1497 while (auto *PN = dyn_cast<PHINode>(II)) {
1498 bool Mapped = false;
1499 // Look for PHI values that we have already mapped (such as the selector).
1500 for (Value *Val : PN->incoming_values()) {
1501 if (VMap.count(Val)) {
1502 VMap[PN] = VMap[Val];
1506 // If we didn't find a match for this value, map it as an undef.
1508 VMap[PN] = UndefValue::get(PN->getType());
1513 // The landing pad value may be used by PHI nodes. It will ultimately be
1514 // eliminated, but we need it in the map for intermediate handling.
1515 VMap[LPad] = UndefValue::get(LPad->getType());
1517 // Skip over PHIs and, if applicable, landingpad instructions.
1518 II = StartBB->getFirstInsertionPt();
1520 CloneAndPruneIntoFromInst(Handler, SrcFn, II, VMap,
1521 /*ModuleLevelChanges=*/false, Returns, "",
1522 &OutlinedFunctionInfo, Director.get());
1524 // Move all the instructions in the cloned "entry" block into our entry block.
1525 // Depending on how the parent function was laid out, the block that will
1526 // correspond to the outlined entry block may not be the first block in the
1527 // list. We can recognize it, however, as the cloned block which has no
1528 // predecessors. Any other block wouldn't have been cloned if it didn't
1529 // have a predecessor which was also cloned.
1530 Function::iterator ClonedIt = std::next(Function::iterator(Entry));
1531 while (!pred_empty(ClonedIt))
1533 BasicBlock *ClonedEntryBB = ClonedIt;
1534 assert(ClonedEntryBB);
1535 Entry->getInstList().splice(Entry->end(), ClonedEntryBB->getInstList());
1536 ClonedEntryBB->eraseFromParent();
1538 // Make sure we can identify the handler's personality later.
1539 addStubInvokeToHandlerIfNeeded(Handler);
1541 if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
1542 WinEHCatchDirector *CatchDirector =
1543 reinterpret_cast<WinEHCatchDirector *>(Director.get());
1544 CatchAction->setExceptionVar(CatchDirector->getExceptionVar());
1545 CatchAction->setReturnTargets(CatchDirector->getReturnTargets());
1547 // Look for blocks that are not part of the landing pad that we just
1548 // outlined but terminate with a call to llvm.eh.endcatch and a
1549 // branch to a block that is in the handler we just outlined.
1550 // These blocks will be part of a nested landing pad that intends to
1551 // return to an address in this handler. This case is best handled
1552 // after both landing pads have been outlined, so for now we'll just
1553 // save the association of the blocks in LPadTargetBlocks. The
1554 // return instructions which are created from these branches will be
1555 // replaced after all landing pads have been outlined.
1556 for (const auto MapEntry : VMap) {
1557 // VMap maps all values and blocks that were just cloned, but dead
1558 // blocks which were pruned will map to nullptr.
1559 if (!isa<BasicBlock>(MapEntry.first) || MapEntry.second == nullptr)
1561 const BasicBlock *MappedBB = cast<BasicBlock>(MapEntry.first);
1562 for (auto *Pred : predecessors(const_cast<BasicBlock *>(MappedBB))) {
1563 auto *Branch = dyn_cast<BranchInst>(Pred->getTerminator());
1564 if (!Branch || !Branch->isUnconditional() || Pred->size() <= 1)
1566 BasicBlock::iterator II = const_cast<BranchInst *>(Branch);
1568 if (match(cast<Value>(II), m_Intrinsic<Intrinsic::eh_endcatch>())) {
1569 // This would indicate that a nested landing pad wants to return
1570 // to a block that is outlined into two different handlers.
1571 assert(!LPadTargetBlocks.count(MappedBB));
1572 LPadTargetBlocks[MappedBB] = cast<BasicBlock>(MapEntry.second);
1576 } // End if (CatchAction)
1578 Action->setHandlerBlockOrFunc(Handler);
1583 /// This BB must end in a selector dispatch. All we need to do is pass the
1584 /// handler block to llvm.eh.actions and list it as a possible indirectbr
1586 void WinEHPrepare::processSEHCatchHandler(CatchHandler *CatchAction,
1587 BasicBlock *StartBB) {
1588 BasicBlock *HandlerBB;
1591 bool Res = isSelectorDispatch(StartBB, HandlerBB, Selector, NextBB);
1593 // If this was EH dispatch, this must be a conditional branch to the handler
1595 // FIXME: Handle instructions in the dispatch block. Currently we drop them,
1596 // leading to crashes if some optimization hoists stuff here.
1597 assert(CatchAction->getSelector() && HandlerBB &&
1598 "expected catch EH dispatch");
1600 // This must be a catch-all. Split the block after the landingpad.
1601 assert(CatchAction->getSelector()->isNullValue() && "expected catch-all");
1602 HandlerBB = SplitBlock(StartBB, StartBB->getFirstInsertionPt(), DT);
1604 IRBuilder<> Builder(HandlerBB->getFirstInsertionPt());
1605 Function *EHCodeFn = Intrinsic::getDeclaration(
1606 StartBB->getParent()->getParent(), Intrinsic::eh_exceptioncode);
1607 Value *Code = Builder.CreateCall(EHCodeFn, {}, "sehcode");
1608 Code = Builder.CreateIntToPtr(Code, SEHExceptionCodeSlot->getAllocatedType());
1609 Builder.CreateStore(Code, SEHExceptionCodeSlot);
1610 CatchAction->setHandlerBlockOrFunc(BlockAddress::get(HandlerBB));
1611 TinyPtrVector<BasicBlock *> Targets(HandlerBB);
1612 CatchAction->setReturnTargets(Targets);
1615 void LandingPadMap::mapLandingPad(const LandingPadInst *LPad) {
1616 // Each instance of this class should only ever be used to map a single
1618 assert(OriginLPad == nullptr || OriginLPad == LPad);
1620 // If the landing pad has already been mapped, there's nothing more to do.
1621 if (OriginLPad == LPad)
1626 // The landingpad instruction returns an aggregate value. Typically, its
1627 // value will be passed to a pair of extract value instructions and the
1628 // results of those extracts will have been promoted to reg values before
1629 // this routine is called.
1630 for (auto *U : LPad->users()) {
1631 const ExtractValueInst *Extract = dyn_cast<ExtractValueInst>(U);
1634 assert(Extract->getNumIndices() == 1 &&
1635 "Unexpected operation: extracting both landing pad values");
1636 unsigned int Idx = *(Extract->idx_begin());
1637 assert((Idx == 0 || Idx == 1) &&
1638 "Unexpected operation: extracting an unknown landing pad element");
1640 ExtractedEHPtrs.push_back(Extract);
1641 } else if (Idx == 1) {
1642 ExtractedSelectors.push_back(Extract);
1647 bool LandingPadMap::isOriginLandingPadBlock(const BasicBlock *BB) const {
1648 return BB->getLandingPadInst() == OriginLPad;
1651 bool LandingPadMap::isLandingPadSpecificInst(const Instruction *Inst) const {
1652 if (Inst == OriginLPad)
1654 for (auto *Extract : ExtractedEHPtrs) {
1655 if (Inst == Extract)
1658 for (auto *Extract : ExtractedSelectors) {
1659 if (Inst == Extract)
1665 void LandingPadMap::remapEHValues(ValueToValueMapTy &VMap, Value *EHPtrValue,
1666 Value *SelectorValue) const {
1667 // Remap all landing pad extract instructions to the specified values.
1668 for (auto *Extract : ExtractedEHPtrs)
1669 VMap[Extract] = EHPtrValue;
1670 for (auto *Extract : ExtractedSelectors)
1671 VMap[Extract] = SelectorValue;
1674 static bool isLocalAddressCall(const Value *V) {
1675 return match(const_cast<Value *>(V), m_Intrinsic<Intrinsic::localaddress>());
1678 CloningDirector::CloningAction WinEHCloningDirectorBase::handleInstruction(
1679 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1680 // If this is one of the boilerplate landing pad instructions, skip it.
1681 // The instruction will have already been remapped in VMap.
1682 if (LPadMap.isLandingPadSpecificInst(Inst))
1683 return CloningDirector::SkipInstruction;
1685 // Nested landing pads that have not already been outlined will be cloned as
1686 // stubs, with just the landingpad instruction and an unreachable instruction.
1687 // When all landingpads have been outlined, we'll replace this with the
1688 // llvm.eh.actions call and indirect branch created when the landing pad was
1690 if (auto *LPad = dyn_cast<LandingPadInst>(Inst)) {
1691 return handleLandingPad(VMap, LPad, NewBB);
1694 // Nested landing pads that have already been outlined will be cloned in their
1695 // outlined form, but we need to intercept the ibr instruction to filter out
1696 // targets that do not return to the handler we are outlining.
1697 if (auto *IBr = dyn_cast<IndirectBrInst>(Inst)) {
1698 return handleIndirectBr(VMap, IBr, NewBB);
1701 if (auto *Invoke = dyn_cast<InvokeInst>(Inst))
1702 return handleInvoke(VMap, Invoke, NewBB);
1704 if (auto *Resume = dyn_cast<ResumeInst>(Inst))
1705 return handleResume(VMap, Resume, NewBB);
1707 if (auto *Cmp = dyn_cast<CmpInst>(Inst))
1708 return handleCompare(VMap, Cmp, NewBB);
1710 if (match(Inst, m_Intrinsic<Intrinsic::eh_begincatch>()))
1711 return handleBeginCatch(VMap, Inst, NewBB);
1712 if (match(Inst, m_Intrinsic<Intrinsic::eh_endcatch>()))
1713 return handleEndCatch(VMap, Inst, NewBB);
1714 if (match(Inst, m_Intrinsic<Intrinsic::eh_typeid_for>()))
1715 return handleTypeIdFor(VMap, Inst, NewBB);
1717 // When outlining llvm.localaddress(), remap that to the second argument,
1718 // which is the FP of the parent.
1719 if (isLocalAddressCall(Inst)) {
1720 VMap[Inst] = ParentFP;
1721 return CloningDirector::SkipInstruction;
1724 // Continue with the default cloning behavior.
1725 return CloningDirector::CloneInstruction;
1728 CloningDirector::CloningAction WinEHCatchDirector::handleLandingPad(
1729 ValueToValueMapTy &VMap, const LandingPadInst *LPad, BasicBlock *NewBB) {
1730 // If the instruction after the landing pad is a call to llvm.eh.actions
1731 // the landing pad has already been outlined. In this case, we should
1732 // clone it because it may return to a block in the handler we are
1733 // outlining now that would otherwise be unreachable. The landing pads
1734 // are sorted before outlining begins to enable this case to work
1736 const Instruction *NextI = LPad->getNextNode();
1737 if (match(NextI, m_Intrinsic<Intrinsic::eh_actions>()))
1738 return CloningDirector::CloneInstruction;
1740 // If the landing pad hasn't been outlined yet, the landing pad we are
1741 // outlining now does not dominate it and so it cannot return to a block
1742 // in this handler. In that case, we can just insert a stub landing
1743 // pad now and patch it up later.
1744 Instruction *NewInst = LPad->clone();
1745 if (LPad->hasName())
1746 NewInst->setName(LPad->getName());
1747 // Save this correlation for later processing.
1748 NestedLPtoOriginalLP[cast<LandingPadInst>(NewInst)] = LPad;
1749 VMap[LPad] = NewInst;
1750 BasicBlock::InstListType &InstList = NewBB->getInstList();
1751 InstList.push_back(NewInst);
1752 InstList.push_back(new UnreachableInst(NewBB->getContext()));
1753 return CloningDirector::StopCloningBB;
1756 CloningDirector::CloningAction WinEHCatchDirector::handleBeginCatch(
1757 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1758 // The argument to the call is some form of the first element of the
1759 // landingpad aggregate value, but that doesn't matter. It isn't used
1761 // The second argument is an outparameter where the exception object will be
1762 // stored. Typically the exception object is a scalar, but it can be an
1763 // aggregate when catching by value.
1764 // FIXME: Leave something behind to indicate where the exception object lives
1765 // for this handler. Should it be part of llvm.eh.actions?
1766 assert(ExceptionObjectVar == nullptr && "Multiple calls to "
1767 "llvm.eh.begincatch found while "
1768 "outlining catch handler.");
1769 ExceptionObjectVar = Inst->getOperand(1)->stripPointerCasts();
1770 if (isa<ConstantPointerNull>(ExceptionObjectVar))
1771 return CloningDirector::SkipInstruction;
1772 assert(cast<AllocaInst>(ExceptionObjectVar)->isStaticAlloca() &&
1773 "catch parameter is not static alloca");
1774 Materializer.escapeCatchObject(ExceptionObjectVar);
1775 return CloningDirector::SkipInstruction;
1778 CloningDirector::CloningAction
1779 WinEHCatchDirector::handleEndCatch(ValueToValueMapTy &VMap,
1780 const Instruction *Inst, BasicBlock *NewBB) {
1781 auto *IntrinCall = dyn_cast<IntrinsicInst>(Inst);
1782 // It might be interesting to track whether or not we are inside a catch
1783 // function, but that might make the algorithm more brittle than it needs
1786 // The end catch call can occur in one of two places: either in a
1787 // landingpad block that is part of the catch handlers exception mechanism,
1788 // or at the end of the catch block. However, a catch-all handler may call
1789 // end catch from the original landing pad. If the call occurs in a nested
1790 // landing pad block, we must skip it and continue so that the landing pad
1792 auto *ParentBB = IntrinCall->getParent();
1793 if (ParentBB->isLandingPad() && !LPadMap.isOriginLandingPadBlock(ParentBB))
1794 return CloningDirector::SkipInstruction;
1796 // If an end catch occurs anywhere else we want to terminate the handler
1797 // with a return to the code that follows the endcatch call. If the
1798 // next instruction is not an unconditional branch, we need to split the
1799 // block to provide a clear target for the return instruction.
1800 BasicBlock *ContinueBB;
1801 auto Next = std::next(BasicBlock::const_iterator(IntrinCall));
1802 const BranchInst *Branch = dyn_cast<BranchInst>(Next);
1803 if (!Branch || !Branch->isUnconditional()) {
1804 // We're interrupting the cloning process at this location, so the
1805 // const_cast we're doing here will not cause a problem.
1806 ContinueBB = SplitBlock(const_cast<BasicBlock *>(ParentBB),
1807 const_cast<Instruction *>(cast<Instruction>(Next)));
1809 ContinueBB = Branch->getSuccessor(0);
1812 ReturnInst::Create(NewBB->getContext(), BlockAddress::get(ContinueBB), NewBB);
1813 ReturnTargets.push_back(ContinueBB);
1815 // We just added a terminator to the cloned block.
1816 // Tell the caller to stop processing the current basic block so that
1817 // the branch instruction will be skipped.
1818 return CloningDirector::StopCloningBB;
1821 CloningDirector::CloningAction WinEHCatchDirector::handleTypeIdFor(
1822 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1823 auto *IntrinCall = dyn_cast<IntrinsicInst>(Inst);
1824 Value *Selector = IntrinCall->getArgOperand(0)->stripPointerCasts();
1825 // This causes a replacement that will collapse the landing pad CFG based
1826 // on the filter function we intend to match.
1827 if (Selector == CurrentSelector)
1828 VMap[Inst] = ConstantInt::get(SelectorIDType, 1);
1830 VMap[Inst] = ConstantInt::get(SelectorIDType, 0);
1831 // Tell the caller not to clone this instruction.
1832 return CloningDirector::SkipInstruction;
1835 CloningDirector::CloningAction WinEHCatchDirector::handleIndirectBr(
1836 ValueToValueMapTy &VMap,
1837 const IndirectBrInst *IBr,
1838 BasicBlock *NewBB) {
1839 // If this indirect branch is not part of a landing pad block, just clone it.
1840 const BasicBlock *ParentBB = IBr->getParent();
1841 if (!ParentBB->isLandingPad())
1842 return CloningDirector::CloneInstruction;
1844 // If it is part of a landing pad, we want to filter out target blocks
1845 // that are not part of the handler we are outlining.
1846 const LandingPadInst *LPad = ParentBB->getLandingPadInst();
1848 // Save this correlation for later processing.
1849 NestedLPtoOriginalLP[cast<LandingPadInst>(VMap[LPad])] = LPad;
1851 // We should only get here for landing pads that have already been outlined.
1852 assert(match(LPad->getNextNode(), m_Intrinsic<Intrinsic::eh_actions>()));
1854 // Copy the indirectbr, but only include targets that were previously
1855 // identified as EH blocks and are dominated by the nested landing pad.
1856 SetVector<const BasicBlock *> ReturnTargets;
1857 for (int I = 0, E = IBr->getNumDestinations(); I < E; ++I) {
1858 auto *TargetBB = IBr->getDestination(I);
1859 if (EHBlocks.count(const_cast<BasicBlock*>(TargetBB)) &&
1860 DT->dominates(ParentBB, TargetBB)) {
1861 DEBUG(dbgs() << " Adding destination " << TargetBB->getName() << "\n");
1862 ReturnTargets.insert(TargetBB);
1865 IndirectBrInst *NewBranch =
1866 IndirectBrInst::Create(const_cast<Value *>(IBr->getAddress()),
1867 ReturnTargets.size(), NewBB);
1868 for (auto *Target : ReturnTargets)
1869 NewBranch->addDestination(const_cast<BasicBlock*>(Target));
1871 // The operands and targets of the branch instruction are remapped later
1872 // because it is a terminator. Tell the cloning code to clone the
1873 // blocks we just added to the target list.
1874 return CloningDirector::CloneSuccessors;
1877 CloningDirector::CloningAction
1878 WinEHCatchDirector::handleInvoke(ValueToValueMapTy &VMap,
1879 const InvokeInst *Invoke, BasicBlock *NewBB) {
1880 return CloningDirector::CloneInstruction;
1883 CloningDirector::CloningAction
1884 WinEHCatchDirector::handleResume(ValueToValueMapTy &VMap,
1885 const ResumeInst *Resume, BasicBlock *NewBB) {
1886 // Resume instructions shouldn't be reachable from catch handlers.
1887 // We still need to handle it, but it will be pruned.
1888 BasicBlock::InstListType &InstList = NewBB->getInstList();
1889 InstList.push_back(new UnreachableInst(NewBB->getContext()));
1890 return CloningDirector::StopCloningBB;
1893 CloningDirector::CloningAction
1894 WinEHCatchDirector::handleCompare(ValueToValueMapTy &VMap,
1895 const CmpInst *Compare, BasicBlock *NewBB) {
1896 const IntrinsicInst *IntrinCall = nullptr;
1897 if (match(Compare->getOperand(0), m_Intrinsic<Intrinsic::eh_typeid_for>())) {
1898 IntrinCall = dyn_cast<IntrinsicInst>(Compare->getOperand(0));
1899 } else if (match(Compare->getOperand(1),
1900 m_Intrinsic<Intrinsic::eh_typeid_for>())) {
1901 IntrinCall = dyn_cast<IntrinsicInst>(Compare->getOperand(1));
1904 Value *Selector = IntrinCall->getArgOperand(0)->stripPointerCasts();
1905 // This causes a replacement that will collapse the landing pad CFG based
1906 // on the filter function we intend to match.
1907 if (Selector == CurrentSelector->stripPointerCasts()) {
1908 VMap[Compare] = ConstantInt::get(SelectorIDType, 1);
1910 VMap[Compare] = ConstantInt::get(SelectorIDType, 0);
1912 return CloningDirector::SkipInstruction;
1914 return CloningDirector::CloneInstruction;
1917 CloningDirector::CloningAction WinEHCleanupDirector::handleLandingPad(
1918 ValueToValueMapTy &VMap, const LandingPadInst *LPad, BasicBlock *NewBB) {
1919 // The MS runtime will terminate the process if an exception occurs in a
1920 // cleanup handler, so we shouldn't encounter landing pads in the actual
1921 // cleanup code, but they may appear in catch blocks. Depending on where
1922 // we started cloning we may see one, but it will get dropped during dead
1924 Instruction *NewInst = new UnreachableInst(NewBB->getContext());
1925 VMap[LPad] = NewInst;
1926 BasicBlock::InstListType &InstList = NewBB->getInstList();
1927 InstList.push_back(NewInst);
1928 return CloningDirector::StopCloningBB;
1931 CloningDirector::CloningAction WinEHCleanupDirector::handleBeginCatch(
1932 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1933 // Cleanup code may flow into catch blocks or the catch block may be part
1934 // of a branch that will be optimized away. We'll insert a return
1935 // instruction now, but it may be pruned before the cloning process is
1937 ReturnInst::Create(NewBB->getContext(), nullptr, NewBB);
1938 return CloningDirector::StopCloningBB;
1941 CloningDirector::CloningAction WinEHCleanupDirector::handleEndCatch(
1942 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1943 // Cleanup handlers nested within catch handlers may begin with a call to
1944 // eh.endcatch. We can just ignore that instruction.
1945 return CloningDirector::SkipInstruction;
1948 CloningDirector::CloningAction WinEHCleanupDirector::handleTypeIdFor(
1949 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1950 // If we encounter a selector comparison while cloning a cleanup handler,
1951 // we want to stop cloning immediately. Anything after the dispatch
1952 // will be outlined into a different handler.
1953 BasicBlock *CatchHandler;
1956 if (isSelectorDispatch(const_cast<BasicBlock *>(Inst->getParent()),
1957 CatchHandler, Selector, NextBB)) {
1958 ReturnInst::Create(NewBB->getContext(), nullptr, NewBB);
1959 return CloningDirector::StopCloningBB;
1961 // If eg.typeid.for is called for any other reason, it can be ignored.
1962 VMap[Inst] = ConstantInt::get(SelectorIDType, 0);
1963 return CloningDirector::SkipInstruction;
1966 CloningDirector::CloningAction WinEHCleanupDirector::handleIndirectBr(
1967 ValueToValueMapTy &VMap,
1968 const IndirectBrInst *IBr,
1969 BasicBlock *NewBB) {
1970 // No special handling is required for cleanup cloning.
1971 return CloningDirector::CloneInstruction;
1974 CloningDirector::CloningAction WinEHCleanupDirector::handleInvoke(
1975 ValueToValueMapTy &VMap, const InvokeInst *Invoke, BasicBlock *NewBB) {
1976 // All invokes in cleanup handlers can be replaced with calls.
1977 SmallVector<Value *, 16> CallArgs(Invoke->op_begin(), Invoke->op_end() - 3);
1978 // Insert a normal call instruction...
1980 CallInst::Create(const_cast<Value *>(Invoke->getCalledValue()), CallArgs,
1981 Invoke->getName(), NewBB);
1982 NewCall->setCallingConv(Invoke->getCallingConv());
1983 NewCall->setAttributes(Invoke->getAttributes());
1984 NewCall->setDebugLoc(Invoke->getDebugLoc());
1985 VMap[Invoke] = NewCall;
1987 // Remap the operands.
1988 llvm::RemapInstruction(NewCall, VMap, RF_None, nullptr, &Materializer);
1990 // Insert an unconditional branch to the normal destination.
1991 BranchInst::Create(Invoke->getNormalDest(), NewBB);
1993 // The unwind destination won't be cloned into the new function, so
1994 // we don't need to clean up its phi nodes.
1996 // We just added a terminator to the cloned block.
1997 // Tell the caller to stop processing the current basic block.
1998 return CloningDirector::CloneSuccessors;
2001 CloningDirector::CloningAction WinEHCleanupDirector::handleResume(
2002 ValueToValueMapTy &VMap, const ResumeInst *Resume, BasicBlock *NewBB) {
2003 ReturnInst::Create(NewBB->getContext(), nullptr, NewBB);
2005 // We just added a terminator to the cloned block.
2006 // Tell the caller to stop processing the current basic block so that
2007 // the branch instruction will be skipped.
2008 return CloningDirector::StopCloningBB;
2011 CloningDirector::CloningAction
2012 WinEHCleanupDirector::handleCompare(ValueToValueMapTy &VMap,
2013 const CmpInst *Compare, BasicBlock *NewBB) {
2014 if (match(Compare->getOperand(0), m_Intrinsic<Intrinsic::eh_typeid_for>()) ||
2015 match(Compare->getOperand(1), m_Intrinsic<Intrinsic::eh_typeid_for>())) {
2016 VMap[Compare] = ConstantInt::get(SelectorIDType, 1);
2017 return CloningDirector::SkipInstruction;
2019 return CloningDirector::CloneInstruction;
2022 WinEHFrameVariableMaterializer::WinEHFrameVariableMaterializer(
2023 Function *OutlinedFn, Value *ParentFP, FrameVarInfoMap &FrameVarInfo)
2024 : FrameVarInfo(FrameVarInfo), Builder(OutlinedFn->getContext()) {
2025 BasicBlock *EntryBB = &OutlinedFn->getEntryBlock();
2027 // New allocas should be inserted in the entry block, but after the parent FP
2028 // is established if it is an instruction.
2029 Instruction *InsertPoint = EntryBB->getFirstInsertionPt();
2030 if (auto *FPInst = dyn_cast<Instruction>(ParentFP))
2031 InsertPoint = FPInst->getNextNode();
2032 Builder.SetInsertPoint(EntryBB, InsertPoint);
2035 Value *WinEHFrameVariableMaterializer::materializeValueFor(Value *V) {
2036 // If we're asked to materialize a static alloca, we temporarily create an
2037 // alloca in the outlined function and add this to the FrameVarInfo map. When
2038 // all the outlining is complete, we'll replace these temporary allocas with
2039 // calls to llvm.localrecover.
2040 if (auto *AV = dyn_cast<AllocaInst>(V)) {
2041 assert(AV->isStaticAlloca() &&
2042 "cannot materialize un-demoted dynamic alloca");
2043 AllocaInst *NewAlloca = dyn_cast<AllocaInst>(AV->clone());
2044 Builder.Insert(NewAlloca, AV->getName());
2045 FrameVarInfo[AV].push_back(NewAlloca);
2049 if (isa<Instruction>(V) || isa<Argument>(V)) {
2050 Function *Parent = isa<Instruction>(V)
2051 ? cast<Instruction>(V)->getParent()->getParent()
2052 : cast<Argument>(V)->getParent();
2054 << "Failed to demote instruction used in exception handler of function "
2055 << GlobalValue::getRealLinkageName(Parent->getName()) << ":\n";
2056 errs() << " " << *V << '\n';
2057 report_fatal_error("WinEHPrepare failed to demote instruction");
2060 // Don't materialize other values.
2064 void WinEHFrameVariableMaterializer::escapeCatchObject(Value *V) {
2065 // Catch parameter objects have to live in the parent frame. When we see a use
2066 // of a catch parameter, add a sentinel to the multimap to indicate that it's
2067 // used from another handler. This will prevent us from trying to sink the
2068 // alloca into the handler and ensure that the catch parameter is present in
2069 // the call to llvm.localescape.
2070 FrameVarInfo[V].push_back(getCatchObjectSentinel());
2073 // This function maps the catch and cleanup handlers that are reachable from the
2074 // specified landing pad. The landing pad sequence will have this basic shape:
2076 // <cleanup handler>
2077 // <selector comparison>
2079 // <cleanup handler>
2080 // <selector comparison>
2082 // <cleanup handler>
2085 // Any of the cleanup slots may be absent. The cleanup slots may be occupied by
2086 // any arbitrary control flow, but all paths through the cleanup code must
2087 // eventually reach the next selector comparison and no path can skip to a
2088 // different selector comparisons, though some paths may terminate abnormally.
2089 // Therefore, we will use a depth first search from the start of any given
2090 // cleanup block and stop searching when we find the next selector comparison.
2092 // If the landingpad instruction does not have a catch clause, we will assume
2093 // that any instructions other than selector comparisons and catch handlers can
2094 // be ignored. In practice, these will only be the boilerplate instructions.
2096 // The catch handlers may also have any control structure, but we are only
2097 // interested in the start of the catch handlers, so we don't need to actually
2098 // follow the flow of the catch handlers. The start of the catch handlers can
2099 // be located from the compare instructions, but they can be skipped in the
2100 // flow by following the contrary branch.
2101 void WinEHPrepare::mapLandingPadBlocks(LandingPadInst *LPad,
2102 LandingPadActions &Actions) {
2103 unsigned int NumClauses = LPad->getNumClauses();
2104 unsigned int HandlersFound = 0;
2105 BasicBlock *BB = LPad->getParent();
2107 DEBUG(dbgs() << "Mapping landing pad: " << BB->getName() << "\n");
2109 if (NumClauses == 0) {
2110 findCleanupHandlers(Actions, BB, nullptr);
2114 VisitedBlockSet VisitedBlocks;
2116 while (HandlersFound != NumClauses) {
2117 BasicBlock *NextBB = nullptr;
2119 // Skip over filter clauses.
2120 if (LPad->isFilter(HandlersFound)) {
2125 // See if the clause we're looking for is a catch-all.
2126 // If so, the catch begins immediately.
2127 Constant *ExpectedSelector =
2128 LPad->getClause(HandlersFound)->stripPointerCasts();
2129 if (isa<ConstantPointerNull>(ExpectedSelector)) {
2130 // The catch all must occur last.
2131 assert(HandlersFound == NumClauses - 1);
2133 // There can be additional selector dispatches in the call chain that we
2135 BasicBlock *CatchBlock = nullptr;
2137 while (BB && isSelectorDispatch(BB, CatchBlock, Selector, NextBB)) {
2138 DEBUG(dbgs() << " Found extra catch dispatch in block "
2139 << CatchBlock->getName() << "\n");
2143 // Add the catch handler to the action list.
2144 CatchHandler *Action = nullptr;
2145 if (CatchHandlerMap.count(BB) && CatchHandlerMap[BB] != nullptr) {
2146 // If the CatchHandlerMap already has an entry for this BB, re-use it.
2147 Action = CatchHandlerMap[BB];
2148 assert(Action->getSelector() == ExpectedSelector);
2150 // We don't expect a selector dispatch, but there may be a call to
2151 // llvm.eh.begincatch, which separates catch handling code from
2152 // cleanup code in the same control flow. This call looks for the
2153 // begincatch intrinsic.
2154 Action = findCatchHandler(BB, NextBB, VisitedBlocks);
2156 // For C++ EH, check if there is any interesting cleanup code before
2157 // we begin the catch. This is important because cleanups cannot
2158 // rethrow exceptions but code called from catches can. For SEH, it
2159 // isn't important if some finally code before a catch-all is executed
2160 // out of line or after recovering from the exception.
2161 if (Personality == EHPersonality::MSVC_CXX)
2162 findCleanupHandlers(Actions, BB, BB);
2164 // If an action was not found, it means that the control flows
2165 // directly into the catch-all handler and there is no cleanup code.
2166 // That's an expected situation and we must create a catch action.
2167 // Since this is a catch-all handler, the selector won't actually
2168 // appear in the code anywhere. ExpectedSelector here is the constant
2169 // null ptr that we got from the landing pad instruction.
2170 Action = new CatchHandler(BB, ExpectedSelector, nullptr);
2171 CatchHandlerMap[BB] = Action;
2174 Actions.insertCatchHandler(Action);
2175 DEBUG(dbgs() << " Catch all handler at block " << BB->getName() << "\n");
2178 // Once we reach a catch-all, don't expect to hit a resume instruction.
2183 CatchHandler *CatchAction = findCatchHandler(BB, NextBB, VisitedBlocks);
2184 assert(CatchAction);
2186 // See if there is any interesting code executed before the dispatch.
2187 findCleanupHandlers(Actions, BB, CatchAction->getStartBlock());
2189 // When the source program contains multiple nested try blocks the catch
2190 // handlers can get strung together in such a way that we can encounter
2191 // a dispatch for a selector that we've already had a handler for.
2192 if (CatchAction->getSelector()->stripPointerCasts() == ExpectedSelector) {
2195 // Add the catch handler to the action list.
2196 DEBUG(dbgs() << " Found catch dispatch in block "
2197 << CatchAction->getStartBlock()->getName() << "\n");
2198 Actions.insertCatchHandler(CatchAction);
2200 // Under some circumstances optimized IR will flow unconditionally into a
2201 // handler block without checking the selector. This can only happen if
2202 // the landing pad has a catch-all handler and the handler for the
2203 // preceding catch clause is identical to the catch-call handler
2204 // (typically an empty catch). In this case, the handler must be shared
2205 // by all remaining clauses.
2206 if (isa<ConstantPointerNull>(
2207 CatchAction->getSelector()->stripPointerCasts())) {
2208 DEBUG(dbgs() << " Applying early catch-all handler in block "
2209 << CatchAction->getStartBlock()->getName()
2210 << " to all remaining clauses.\n");
2211 Actions.insertCatchHandler(CatchAction);
2215 DEBUG(dbgs() << " Found extra catch dispatch in block "
2216 << CatchAction->getStartBlock()->getName() << "\n");
2219 // Move on to the block after the catch handler.
2223 // If we didn't wind up in a catch-all, see if there is any interesting code
2224 // executed before the resume.
2225 findCleanupHandlers(Actions, BB, BB);
2227 // It's possible that some optimization moved code into a landingpad that
2229 // previously being used for cleanup. If that happens, we need to execute
2231 // extra code from a cleanup handler.
2232 if (Actions.includesCleanup() && !LPad->isCleanup())
2233 LPad->setCleanup(true);
2236 // This function searches starting with the input block for the next
2237 // block that terminates with a branch whose condition is based on a selector
2238 // comparison. This may be the input block. See the mapLandingPadBlocks
2239 // comments for a discussion of control flow assumptions.
2241 CatchHandler *WinEHPrepare::findCatchHandler(BasicBlock *BB,
2242 BasicBlock *&NextBB,
2243 VisitedBlockSet &VisitedBlocks) {
2244 // See if we've already found a catch handler use it.
2245 // Call count() first to avoid creating a null entry for blocks
2246 // we haven't seen before.
2247 if (CatchHandlerMap.count(BB) && CatchHandlerMap[BB] != nullptr) {
2248 CatchHandler *Action = cast<CatchHandler>(CatchHandlerMap[BB]);
2249 NextBB = Action->getNextBB();
2253 // VisitedBlocks applies only to the current search. We still
2254 // need to consider blocks that we've visited while mapping other
2256 VisitedBlocks.insert(BB);
2258 BasicBlock *CatchBlock = nullptr;
2259 Constant *Selector = nullptr;
2261 // If this is the first time we've visited this block from any landing pad
2262 // look to see if it is a selector dispatch block.
2263 if (!CatchHandlerMap.count(BB)) {
2264 if (isSelectorDispatch(BB, CatchBlock, Selector, NextBB)) {
2265 CatchHandler *Action = new CatchHandler(BB, Selector, NextBB);
2266 CatchHandlerMap[BB] = Action;
2269 // If we encounter a block containing an llvm.eh.begincatch before we
2270 // find a selector dispatch block, the handler is assumed to be
2271 // reached unconditionally. This happens for catch-all blocks, but
2272 // it can also happen for other catch handlers that have been combined
2273 // with the catch-all handler during optimization.
2274 if (isCatchBlock(BB)) {
2275 PointerType *Int8PtrTy = Type::getInt8PtrTy(BB->getContext());
2276 Constant *NullSelector = ConstantPointerNull::get(Int8PtrTy);
2277 CatchHandler *Action = new CatchHandler(BB, NullSelector, nullptr);
2278 CatchHandlerMap[BB] = Action;
2283 // Visit each successor, looking for the dispatch.
2284 // FIXME: We expect to find the dispatch quickly, so this will probably
2285 // work better as a breadth first search.
2286 for (BasicBlock *Succ : successors(BB)) {
2287 if (VisitedBlocks.count(Succ))
2290 CatchHandler *Action = findCatchHandler(Succ, NextBB, VisitedBlocks);
2297 // These are helper functions to combine repeated code from findCleanupHandlers.
2298 static void createCleanupHandler(LandingPadActions &Actions,
2299 CleanupHandlerMapTy &CleanupHandlerMap,
2301 CleanupHandler *Action = new CleanupHandler(BB);
2302 CleanupHandlerMap[BB] = Action;
2303 Actions.insertCleanupHandler(Action);
2304 DEBUG(dbgs() << " Found cleanup code in block "
2305 << Action->getStartBlock()->getName() << "\n");
2308 static CallSite matchOutlinedFinallyCall(BasicBlock *BB,
2309 Instruction *MaybeCall) {
2310 // Look for finally blocks that Clang has already outlined for us.
2311 // %fp = call i8* @llvm.localaddress()
2312 // call void @"fin$parent"(iN 1, i8* %fp)
2313 if (isLocalAddressCall(MaybeCall) && MaybeCall != BB->getTerminator())
2314 MaybeCall = MaybeCall->getNextNode();
2315 CallSite FinallyCall(MaybeCall);
2316 if (!FinallyCall || FinallyCall.arg_size() != 2)
2318 if (!match(FinallyCall.getArgument(0), m_SpecificInt(1)))
2320 if (!isLocalAddressCall(FinallyCall.getArgument(1)))
2325 static BasicBlock *followSingleUnconditionalBranches(BasicBlock *BB) {
2326 // Skip single ubr blocks.
2327 while (BB->getFirstNonPHIOrDbg() == BB->getTerminator()) {
2328 auto *Br = dyn_cast<BranchInst>(BB->getTerminator());
2329 if (Br && Br->isUnconditional())
2330 BB = Br->getSuccessor(0);
2337 // This function searches starting with the input block for the next block that
2338 // contains code that is not part of a catch handler and would not be eliminated
2339 // during handler outlining.
2341 void WinEHPrepare::findCleanupHandlers(LandingPadActions &Actions,
2342 BasicBlock *StartBB, BasicBlock *EndBB) {
2343 // Here we will skip over the following:
2345 // landing pad prolog:
2347 // Unconditional branches
2349 // Selector dispatch
2353 // Anything else marks the start of an interesting block
2355 BasicBlock *BB = StartBB;
2356 // Anything other than an unconditional branch will kick us out of this loop
2357 // one way or another.
2359 BB = followSingleUnconditionalBranches(BB);
2360 // If we've already scanned this block, don't scan it again. If it is
2361 // a cleanup block, there will be an action in the CleanupHandlerMap.
2362 // If we've scanned it and it is not a cleanup block, there will be a
2363 // nullptr in the CleanupHandlerMap. If we have not scanned it, there will
2364 // be no entry in the CleanupHandlerMap. We must call count() first to
2365 // avoid creating a null entry for blocks we haven't scanned.
2366 if (CleanupHandlerMap.count(BB)) {
2367 if (auto *Action = CleanupHandlerMap[BB]) {
2368 Actions.insertCleanupHandler(Action);
2369 DEBUG(dbgs() << " Found cleanup code in block "
2370 << Action->getStartBlock()->getName() << "\n");
2371 // FIXME: This cleanup might chain into another, and we need to discover
2375 // Here we handle the case where the cleanup handler map contains a
2376 // value for this block but the value is a nullptr. This means that
2377 // we have previously analyzed the block and determined that it did
2378 // not contain any cleanup code. Based on the earlier analysis, we
2379 // know the block must end in either an unconditional branch, a
2380 // resume or a conditional branch that is predicated on a comparison
2381 // with a selector. Either the resume or the selector dispatch
2382 // would terminate the search for cleanup code, so the unconditional
2383 // branch is the only case for which we might need to continue
2385 BasicBlock *SuccBB = followSingleUnconditionalBranches(BB);
2386 if (SuccBB == BB || SuccBB == EndBB)
2393 // Create an entry in the cleanup handler map for this block. Initially
2394 // we create an entry that says this isn't a cleanup block. If we find
2395 // cleanup code, the caller will replace this entry.
2396 CleanupHandlerMap[BB] = nullptr;
2398 TerminatorInst *Terminator = BB->getTerminator();
2400 // Landing pad blocks have extra instructions we need to accept.
2401 LandingPadMap *LPadMap = nullptr;
2402 if (BB->isLandingPad()) {
2403 LandingPadInst *LPad = BB->getLandingPadInst();
2404 LPadMap = &LPadMaps[LPad];
2405 if (!LPadMap->isInitialized())
2406 LPadMap->mapLandingPad(LPad);
2409 // Look for the bare resume pattern:
2410 // %lpad.val1 = insertvalue { i8*, i32 } undef, i8* %exn, 0
2411 // %lpad.val2 = insertvalue { i8*, i32 } %lpad.val1, i32 %sel, 1
2412 // resume { i8*, i32 } %lpad.val2
2413 if (auto *Resume = dyn_cast<ResumeInst>(Terminator)) {
2414 InsertValueInst *Insert1 = nullptr;
2415 InsertValueInst *Insert2 = nullptr;
2416 Value *ResumeVal = Resume->getOperand(0);
2417 // If the resume value isn't a phi or landingpad value, it should be a
2418 // series of insertions. Identify them so we can avoid them when scanning
2420 if (!isa<PHINode>(ResumeVal) && !isa<LandingPadInst>(ResumeVal)) {
2421 Insert2 = dyn_cast<InsertValueInst>(ResumeVal);
2423 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
2424 Insert1 = dyn_cast<InsertValueInst>(Insert2->getAggregateOperand());
2426 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
2428 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end();
2430 Instruction *Inst = II;
2431 if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst))
2433 if (Inst == Insert1 || Inst == Insert2 || Inst == Resume)
2435 if (!Inst->hasOneUse() ||
2436 (Inst->user_back() != Insert1 && Inst->user_back() != Insert2)) {
2437 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
2443 BranchInst *Branch = dyn_cast<BranchInst>(Terminator);
2444 if (Branch && Branch->isConditional()) {
2445 // Look for the selector dispatch.
2446 // %2 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIf to i8*))
2447 // %matches = icmp eq i32 %sel, %2
2448 // br i1 %matches, label %catch14, label %eh.resume
2449 CmpInst *Compare = dyn_cast<CmpInst>(Branch->getCondition());
2450 if (!Compare || !Compare->isEquality())
2451 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
2452 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end();
2454 Instruction *Inst = II;
2455 if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst))
2457 if (Inst == Compare || Inst == Branch)
2459 if (match(Inst, m_Intrinsic<Intrinsic::eh_typeid_for>()))
2461 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
2463 // The selector dispatch block should always terminate our search.
2464 assert(BB == EndBB);
2468 if (isAsynchronousEHPersonality(Personality)) {
2469 // If this is a landingpad block, split the block at the first non-landing
2471 Instruction *MaybeCall = BB->getFirstNonPHIOrDbg();
2473 while (MaybeCall != BB->getTerminator() &&
2474 LPadMap->isLandingPadSpecificInst(MaybeCall))
2475 MaybeCall = MaybeCall->getNextNode();
2478 // Look for outlined finally calls on x64, since those happen to match the
2479 // prototype provided by the runtime.
2480 if (TheTriple.getArch() == Triple::x86_64) {
2481 if (CallSite FinallyCall = matchOutlinedFinallyCall(BB, MaybeCall)) {
2482 Function *Fin = FinallyCall.getCalledFunction();
2483 assert(Fin && "outlined finally call should be direct");
2484 auto *Action = new CleanupHandler(BB);
2485 Action->setHandlerBlockOrFunc(Fin);
2486 Actions.insertCleanupHandler(Action);
2487 CleanupHandlerMap[BB] = Action;
2488 DEBUG(dbgs() << " Found frontend-outlined finally call to "
2489 << Fin->getName() << " in block "
2490 << Action->getStartBlock()->getName() << "\n");
2492 // Split the block if there were more interesting instructions and
2493 // look for finally calls in the normal successor block.
2494 BasicBlock *SuccBB = BB;
2495 if (FinallyCall.getInstruction() != BB->getTerminator() &&
2496 FinallyCall.getInstruction()->getNextNode() !=
2497 BB->getTerminator()) {
2499 SplitBlock(BB, FinallyCall.getInstruction()->getNextNode(), DT);
2501 if (FinallyCall.isInvoke()) {
2502 SuccBB = cast<InvokeInst>(FinallyCall.getInstruction())
2505 SuccBB = BB->getUniqueSuccessor();
2507 "splitOutlinedFinallyCalls didn't insert a branch");
2518 // Anything else is either a catch block or interesting cleanup code.
2519 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end();
2521 Instruction *Inst = II;
2522 if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst))
2524 // Unconditional branches fall through to this loop.
2527 // If this is a catch block, there is no cleanup code to be found.
2528 if (match(Inst, m_Intrinsic<Intrinsic::eh_begincatch>()))
2530 // If this a nested landing pad, it may contain an endcatch call.
2531 if (match(Inst, m_Intrinsic<Intrinsic::eh_endcatch>()))
2533 // Anything else makes this interesting cleanup code.
2534 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
2537 // Only unconditional branches in empty blocks should get this far.
2538 assert(Branch && Branch->isUnconditional());
2541 BB = Branch->getSuccessor(0);
2545 // This is a public function, declared in WinEHFuncInfo.h and is also
2546 // referenced by WinEHNumbering in FunctionLoweringInfo.cpp.
2547 void llvm::parseEHActions(
2548 const IntrinsicInst *II,
2549 SmallVectorImpl<std::unique_ptr<ActionHandler>> &Actions) {
2550 assert(II->getIntrinsicID() == Intrinsic::eh_actions &&
2551 "attempted to parse non eh.actions intrinsic");
2552 for (unsigned I = 0, E = II->getNumArgOperands(); I != E;) {
2553 uint64_t ActionKind =
2554 cast<ConstantInt>(II->getArgOperand(I))->getZExtValue();
2555 if (ActionKind == /*catch=*/1) {
2556 auto *Selector = cast<Constant>(II->getArgOperand(I + 1));
2557 ConstantInt *EHObjIndex = cast<ConstantInt>(II->getArgOperand(I + 2));
2558 int64_t EHObjIndexVal = EHObjIndex->getSExtValue();
2559 Constant *Handler = cast<Constant>(II->getArgOperand(I + 3));
2561 auto CH = make_unique<CatchHandler>(/*BB=*/nullptr, Selector,
2562 /*NextBB=*/nullptr);
2563 CH->setHandlerBlockOrFunc(Handler);
2564 CH->setExceptionVarIndex(EHObjIndexVal);
2565 Actions.push_back(std::move(CH));
2566 } else if (ActionKind == 0) {
2567 Constant *Handler = cast<Constant>(II->getArgOperand(I + 1));
2569 auto CH = make_unique<CleanupHandler>(/*BB=*/nullptr);
2570 CH->setHandlerBlockOrFunc(Handler);
2571 Actions.push_back(std::move(CH));
2573 llvm_unreachable("Expected either a catch or cleanup handler!");
2576 std::reverse(Actions.begin(), Actions.end());
2580 struct WinEHNumbering {
2581 WinEHNumbering(WinEHFuncInfo &FuncInfo) : FuncInfo(FuncInfo),
2582 CurrentBaseState(-1), NextState(0) {}
2584 WinEHFuncInfo &FuncInfo;
2585 int CurrentBaseState;
2588 SmallVector<std::unique_ptr<ActionHandler>, 4> HandlerStack;
2589 SmallPtrSet<const Function *, 4> VisitedHandlers;
2591 int currentEHNumber() const {
2592 return HandlerStack.empty() ? CurrentBaseState : HandlerStack.back()->getEHState();
2595 void createUnwindMapEntry(int ToState, ActionHandler *AH);
2596 void createTryBlockMapEntry(int TryLow, int TryHigh,
2597 ArrayRef<CatchHandler *> Handlers);
2598 void processCallSite(MutableArrayRef<std::unique_ptr<ActionHandler>> Actions,
2599 ImmutableCallSite CS);
2600 void popUnmatchedActions(int FirstMismatch);
2601 void calculateStateNumbers(const Function &F);
2602 void findActionRootLPads(const Function &F);
2606 static int addUnwindMapEntry(WinEHFuncInfo &FuncInfo, int ToState,
2608 WinEHUnwindMapEntry UME;
2609 UME.ToState = ToState;
2611 FuncInfo.UnwindMap.push_back(UME);
2612 return FuncInfo.getLastStateNumber();
2615 static void addTryBlockMapEntry(WinEHFuncInfo &FuncInfo, int TryLow,
2616 int TryHigh, int CatchHigh,
2617 ArrayRef<const CatchPadInst *> Handlers) {
2618 WinEHTryBlockMapEntry TBME;
2619 TBME.TryLow = TryLow;
2620 TBME.TryHigh = TryHigh;
2621 TBME.CatchHigh = CatchHigh;
2622 assert(TBME.TryLow <= TBME.TryHigh);
2623 for (const CatchPadInst *CPI : Handlers) {
2624 WinEHHandlerType HT;
2625 Constant *TypeInfo = cast<Constant>(CPI->getArgOperand(0));
2626 if (TypeInfo->isNullValue())
2627 HT.TypeDescriptor = nullptr;
2629 HT.TypeDescriptor = cast<GlobalVariable>(TypeInfo->stripPointerCasts());
2630 HT.Adjectives = cast<ConstantInt>(CPI->getArgOperand(1))->getZExtValue();
2631 HT.Handler = CPI->getNormalDest();
2632 HT.CatchObjRecoverIdx = -2;
2633 if (isa<ConstantPointerNull>(CPI->getArgOperand(2)))
2634 HT.CatchObj.Alloca = nullptr;
2636 HT.CatchObj.Alloca = cast<AllocaInst>(CPI->getArgOperand(2));
2637 TBME.HandlerArray.push_back(HT);
2639 FuncInfo.TryBlockMap.push_back(TBME);
2642 void WinEHNumbering::createUnwindMapEntry(int ToState, ActionHandler *AH) {
2644 if (auto *CH = dyn_cast_or_null<CleanupHandler>(AH))
2645 V = cast<Function>(CH->getHandlerBlockOrFunc());
2646 addUnwindMapEntry(FuncInfo, ToState, V);
2649 void WinEHNumbering::createTryBlockMapEntry(int TryLow, int TryHigh,
2650 ArrayRef<CatchHandler *> Handlers) {
2651 // See if we already have an entry for this set of handlers.
2652 // This is using iterators rather than a range-based for loop because
2653 // if we find the entry we're looking for we'll need the iterator to erase it.
2654 int NumHandlers = Handlers.size();
2655 auto I = FuncInfo.TryBlockMap.begin();
2656 auto E = FuncInfo.TryBlockMap.end();
2657 for ( ; I != E; ++I) {
2659 if (Entry.HandlerArray.size() != (size_t)NumHandlers)
2662 for (N = 0; N < NumHandlers; ++N) {
2663 if (Entry.HandlerArray[N].Handler.get<const Value *>() !=
2664 Handlers[N]->getHandlerBlockOrFunc())
2665 break; // breaks out of inner loop
2667 // If all the handlers match, this is what we were looking for.
2668 if (N == NumHandlers) {
2673 // If we found an existing entry for this set of handlers, extend the range
2674 // but move the entry to the end of the map vector. The order of entries
2675 // in the map is critical to the way that the runtime finds handlers.
2676 // FIXME: Depending on what has happened with block ordering, this may
2677 // incorrectly combine entries that should remain separate.
2679 // Copy the existing entry.
2680 WinEHTryBlockMapEntry Entry = *I;
2681 Entry.TryLow = std::min(TryLow, Entry.TryLow);
2682 Entry.TryHigh = std::max(TryHigh, Entry.TryHigh);
2683 assert(Entry.TryLow <= Entry.TryHigh);
2684 // Erase the old entry and add this one to the back.
2685 FuncInfo.TryBlockMap.erase(I);
2686 FuncInfo.TryBlockMap.push_back(Entry);
2690 // If we didn't find an entry, create a new one.
2691 WinEHTryBlockMapEntry TBME;
2692 TBME.TryLow = TryLow;
2693 TBME.TryHigh = TryHigh;
2694 assert(TBME.TryLow <= TBME.TryHigh);
2695 for (CatchHandler *CH : Handlers) {
2696 WinEHHandlerType HT;
2697 if (CH->getSelector()->isNullValue()) {
2698 HT.Adjectives = 0x40;
2699 HT.TypeDescriptor = nullptr;
2701 auto *GV = cast<GlobalVariable>(CH->getSelector()->stripPointerCasts());
2702 // Selectors are always pointers to GlobalVariables with 'struct' type.
2703 // The struct has two fields, adjectives and a type descriptor.
2704 auto *CS = cast<ConstantStruct>(GV->getInitializer());
2706 cast<ConstantInt>(CS->getAggregateElement(0U))->getZExtValue();
2708 cast<GlobalVariable>(CS->getAggregateElement(1)->stripPointerCasts());
2710 HT.Handler = cast<Function>(CH->getHandlerBlockOrFunc());
2711 HT.CatchObjRecoverIdx = CH->getExceptionVarIndex();
2712 HT.CatchObj.Alloca = nullptr;
2713 TBME.HandlerArray.push_back(HT);
2715 FuncInfo.TryBlockMap.push_back(TBME);
2718 static void print_name(const Value *V) {
2721 DEBUG(dbgs() << "null");
2725 if (const auto *F = dyn_cast<Function>(V))
2726 DEBUG(dbgs() << F->getName());
2732 void WinEHNumbering::processCallSite(
2733 MutableArrayRef<std::unique_ptr<ActionHandler>> Actions,
2734 ImmutableCallSite CS) {
2735 DEBUG(dbgs() << "processCallSite (EH state = " << currentEHNumber()
2737 print_name(CS ? CS.getCalledValue() : nullptr);
2738 DEBUG(dbgs() << '\n');
2740 DEBUG(dbgs() << "HandlerStack: \n");
2741 for (int I = 0, E = HandlerStack.size(); I < E; ++I) {
2742 DEBUG(dbgs() << " ");
2743 print_name(HandlerStack[I]->getHandlerBlockOrFunc());
2744 DEBUG(dbgs() << '\n');
2746 DEBUG(dbgs() << "Actions: \n");
2747 for (int I = 0, E = Actions.size(); I < E; ++I) {
2748 DEBUG(dbgs() << " ");
2749 print_name(Actions[I]->getHandlerBlockOrFunc());
2750 DEBUG(dbgs() << '\n');
2752 int FirstMismatch = 0;
2753 for (int E = std::min(HandlerStack.size(), Actions.size()); FirstMismatch < E;
2755 if (HandlerStack[FirstMismatch]->getHandlerBlockOrFunc() !=
2756 Actions[FirstMismatch]->getHandlerBlockOrFunc())
2760 // Remove unmatched actions from the stack and process their EH states.
2761 popUnmatchedActions(FirstMismatch);
2763 DEBUG(dbgs() << "Pushing actions for CallSite: ");
2764 print_name(CS ? CS.getCalledValue() : nullptr);
2765 DEBUG(dbgs() << '\n');
2767 bool LastActionWasCatch = false;
2768 const LandingPadInst *LastRootLPad = nullptr;
2769 for (size_t I = FirstMismatch; I != Actions.size(); ++I) {
2770 // We can reuse eh states when pushing two catches for the same invoke.
2771 bool CurrActionIsCatch = isa<CatchHandler>(Actions[I].get());
2772 auto *Handler = cast<Function>(Actions[I]->getHandlerBlockOrFunc());
2773 // Various conditions can lead to a handler being popped from the
2774 // stack and re-pushed later. That shouldn't create a new state.
2775 // FIXME: Can code optimization lead to re-used handlers?
2776 if (FuncInfo.HandlerEnclosedState.count(Handler)) {
2777 // If we already assigned the state enclosed by this handler re-use it.
2778 Actions[I]->setEHState(FuncInfo.HandlerEnclosedState[Handler]);
2781 const LandingPadInst* RootLPad = FuncInfo.RootLPad[Handler];
2782 if (CurrActionIsCatch && LastActionWasCatch && RootLPad == LastRootLPad) {
2783 DEBUG(dbgs() << "setEHState for handler to " << currentEHNumber() << "\n");
2784 Actions[I]->setEHState(currentEHNumber());
2786 DEBUG(dbgs() << "createUnwindMapEntry(" << currentEHNumber() << ", ");
2787 print_name(Actions[I]->getHandlerBlockOrFunc());
2788 DEBUG(dbgs() << ") with EH state " << NextState << "\n");
2789 createUnwindMapEntry(currentEHNumber(), Actions[I].get());
2790 DEBUG(dbgs() << "setEHState for handler to " << NextState << "\n");
2791 Actions[I]->setEHState(NextState);
2794 HandlerStack.push_back(std::move(Actions[I]));
2795 LastActionWasCatch = CurrActionIsCatch;
2796 LastRootLPad = RootLPad;
2799 // This is used to defer numbering states for a handler until after the
2800 // last time it appears in an invoke action list.
2801 if (CS.isInvoke()) {
2802 for (int I = 0, E = HandlerStack.size(); I < E; ++I) {
2803 auto *Handler = cast<Function>(HandlerStack[I]->getHandlerBlockOrFunc());
2804 if (FuncInfo.LastInvoke[Handler] != cast<InvokeInst>(CS.getInstruction()))
2806 FuncInfo.LastInvokeVisited[Handler] = true;
2807 DEBUG(dbgs() << "Last invoke of ");
2808 print_name(Handler);
2809 DEBUG(dbgs() << " has been visited.\n");
2813 DEBUG(dbgs() << "In EHState " << currentEHNumber() << " for CallSite: ");
2814 print_name(CS ? CS.getCalledValue() : nullptr);
2815 DEBUG(dbgs() << '\n');
2818 void WinEHNumbering::popUnmatchedActions(int FirstMismatch) {
2819 // Don't recurse while we are looping over the handler stack. Instead, defer
2820 // the numbering of the catch handlers until we are done popping.
2821 SmallVector<CatchHandler *, 4> PoppedCatches;
2822 for (int I = HandlerStack.size() - 1; I >= FirstMismatch; --I) {
2823 std::unique_ptr<ActionHandler> Handler = HandlerStack.pop_back_val();
2824 if (isa<CatchHandler>(Handler.get()))
2825 PoppedCatches.push_back(cast<CatchHandler>(Handler.release()));
2828 int TryHigh = NextState - 1;
2829 int LastTryLowIdx = 0;
2830 for (int I = 0, E = PoppedCatches.size(); I != E; ++I) {
2831 CatchHandler *CH = PoppedCatches[I];
2832 DEBUG(dbgs() << "Popped handler with state " << CH->getEHState() << "\n");
2833 if (I + 1 == E || CH->getEHState() != PoppedCatches[I + 1]->getEHState()) {
2834 int TryLow = CH->getEHState();
2836 makeArrayRef(&PoppedCatches[LastTryLowIdx], I - LastTryLowIdx + 1);
2837 DEBUG(dbgs() << "createTryBlockMapEntry(" << TryLow << ", " << TryHigh);
2838 for (size_t J = 0; J < Handlers.size(); ++J) {
2839 DEBUG(dbgs() << ", ");
2840 print_name(Handlers[J]->getHandlerBlockOrFunc());
2842 DEBUG(dbgs() << ")\n");
2843 createTryBlockMapEntry(TryLow, TryHigh, Handlers);
2844 LastTryLowIdx = I + 1;
2848 for (CatchHandler *CH : PoppedCatches) {
2849 if (auto *F = dyn_cast<Function>(CH->getHandlerBlockOrFunc())) {
2850 if (FuncInfo.LastInvokeVisited[F]) {
2851 DEBUG(dbgs() << "Assigning base state " << NextState << " to ");
2853 DEBUG(dbgs() << '\n');
2854 FuncInfo.HandlerBaseState[F] = NextState;
2855 DEBUG(dbgs() << "createUnwindMapEntry(" << currentEHNumber()
2857 createUnwindMapEntry(currentEHNumber(), nullptr);
2859 calculateStateNumbers(*F);
2862 DEBUG(dbgs() << "Deferring handling of ");
2864 DEBUG(dbgs() << " until last invoke visited.\n");
2871 void WinEHNumbering::calculateStateNumbers(const Function &F) {
2872 auto I = VisitedHandlers.insert(&F);
2874 return; // We've already visited this handler, don't renumber it.
2876 int OldBaseState = CurrentBaseState;
2877 if (FuncInfo.HandlerBaseState.count(&F)) {
2878 CurrentBaseState = FuncInfo.HandlerBaseState[&F];
2881 size_t SavedHandlerStackSize = HandlerStack.size();
2883 DEBUG(dbgs() << "Calculating state numbers for: " << F.getName() << '\n');
2884 SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList;
2885 for (const BasicBlock &BB : F) {
2886 for (const Instruction &I : BB) {
2887 const auto *CI = dyn_cast<CallInst>(&I);
2888 if (!CI || CI->doesNotThrow())
2890 processCallSite(None, CI);
2892 const auto *II = dyn_cast<InvokeInst>(BB.getTerminator());
2895 const LandingPadInst *LPI = II->getLandingPadInst();
2896 auto *ActionsCall = dyn_cast<IntrinsicInst>(LPI->getNextNode());
2899 parseEHActions(ActionsCall, ActionList);
2900 if (ActionList.empty())
2902 processCallSite(ActionList, II);
2904 FuncInfo.EHPadStateMap[LPI] = currentEHNumber();
2905 DEBUG(dbgs() << "Assigning state " << currentEHNumber()
2906 << " to landing pad at " << LPI->getParent()->getName()
2910 // Pop any actions that were pushed on the stack for this function.
2911 popUnmatchedActions(SavedHandlerStackSize);
2913 DEBUG(dbgs() << "Assigning max state " << NextState - 1
2914 << " to " << F.getName() << '\n');
2915 FuncInfo.CatchHandlerMaxState[&F] = NextState - 1;
2917 CurrentBaseState = OldBaseState;
2920 // This function follows the same basic traversal as calculateStateNumbers
2921 // but it is necessary to identify the root landing pad associated
2922 // with each action before we start assigning state numbers.
2923 void WinEHNumbering::findActionRootLPads(const Function &F) {
2924 auto I = VisitedHandlers.insert(&F);
2926 return; // We've already visited this handler, don't revisit it.
2928 SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList;
2929 for (const BasicBlock &BB : F) {
2930 const auto *II = dyn_cast<InvokeInst>(BB.getTerminator());
2933 const LandingPadInst *LPI = II->getLandingPadInst();
2934 auto *ActionsCall = dyn_cast<IntrinsicInst>(LPI->getNextNode());
2938 assert(ActionsCall->getIntrinsicID() == Intrinsic::eh_actions);
2939 parseEHActions(ActionsCall, ActionList);
2940 if (ActionList.empty())
2942 for (int I = 0, E = ActionList.size(); I < E; ++I) {
2944 = dyn_cast<Function>(ActionList[I]->getHandlerBlockOrFunc())) {
2945 FuncInfo.LastInvoke[Handler] = II;
2946 // Don't replace the root landing pad if we previously saw this
2947 // handler in a different function.
2948 if (FuncInfo.RootLPad.count(Handler) &&
2949 FuncInfo.RootLPad[Handler]->getParent()->getParent() != &F)
2951 DEBUG(dbgs() << "Setting root lpad for ");
2952 print_name(Handler);
2953 DEBUG(dbgs() << " to " << LPI->getParent()->getName() << '\n');
2954 FuncInfo.RootLPad[Handler] = LPI;
2957 // Walk the actions again and look for nested handlers. This has to
2958 // happen after all of the actions have been processed in the current
2960 for (int I = 0, E = ActionList.size(); I < E; ++I)
2962 = dyn_cast<Function>(ActionList[I]->getHandlerBlockOrFunc()))
2963 findActionRootLPads(*Handler);
2968 static const CatchPadInst *getSingleCatchPadPredecessor(const BasicBlock *BB) {
2969 for (const BasicBlock *PredBlock : predecessors(BB))
2970 if (auto *CPI = dyn_cast<CatchPadInst>(PredBlock->getFirstNonPHI()))
2975 /// Find all the catchpads that feed directly into the catchendpad. Frontends
2976 /// using this personality should ensure that each catchendpad and catchpad has
2977 /// one or zero catchpad predecessors.
2979 /// The following C++ generates the IR after it:
2987 /// catchpad [i8* A typeinfo]
2988 /// to label %catch.A unwind label %catchpad.B
2990 /// catchpad [i8* B typeinfo]
2991 /// to label %catch.B unwind label %endcatches
2993 /// catchendblock unwind to caller
2994 void findCatchPadsForCatchEndPad(
2995 const BasicBlock *CatchEndBB,
2996 SmallVectorImpl<const CatchPadInst *> &Handlers) {
2997 const CatchPadInst *CPI = getSingleCatchPadPredecessor(CatchEndBB);
2999 Handlers.push_back(CPI);
3000 CPI = getSingleCatchPadPredecessor(CPI->getParent());
3002 // We've pushed these back into reverse source order. Reverse them to get
3003 // the list back into source order.
3004 std::reverse(Handlers.begin(), Handlers.end());
3007 // Given BB which ends in an unwind edge, return the EHPad that this BB belongs
3008 // to. If the unwind edge came from an invoke, return null.
3009 static const BasicBlock *getEHPadFromPredecessor(const BasicBlock *BB) {
3010 const TerminatorInst *TI = BB->getTerminator();
3011 if (isa<InvokeInst>(TI))
3015 return cast<CleanupReturnInst>(TI)->getCleanupPad()->getParent();
3018 static void calculateExplicitCXXStateNumbers(WinEHFuncInfo &FuncInfo,
3019 const BasicBlock &BB,
3021 assert(BB.isEHPad());
3022 const Instruction *FirstNonPHI = BB.getFirstNonPHI();
3023 // All catchpad instructions will be handled when we process their
3024 // respective catchendpad instruction.
3025 if (isa<CatchPadInst>(FirstNonPHI))
3028 if (isa<CatchEndPadInst>(FirstNonPHI)) {
3029 SmallVector<const CatchPadInst *, 2> Handlers;
3030 findCatchPadsForCatchEndPad(&BB, Handlers);
3031 const BasicBlock *FirstTryPad = Handlers.front()->getParent();
3032 int TryLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr);
3033 FuncInfo.EHPadStateMap[Handlers.front()] = TryLow;
3034 for (const BasicBlock *PredBlock : predecessors(FirstTryPad))
3035 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
3036 calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, TryLow);
3037 int CatchLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr);
3039 // catchpads are separate funclets in C++ EH due to the way rethrow works.
3040 // In SEH, they aren't, so no invokes will unwind to the catchendpad.
3041 FuncInfo.EHPadStateMap[FirstNonPHI] = CatchLow;
3042 int TryHigh = CatchLow - 1;
3043 for (const BasicBlock *PredBlock : predecessors(&BB))
3044 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
3045 calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, CatchLow);
3046 int CatchHigh = FuncInfo.getLastStateNumber();
3047 addTryBlockMapEntry(FuncInfo, TryLow, TryHigh, CatchHigh, Handlers);
3048 DEBUG(dbgs() << "TryLow[" << FirstTryPad->getName() << "]: " << TryLow
3050 DEBUG(dbgs() << "TryHigh[" << FirstTryPad->getName() << "]: " << TryHigh
3052 DEBUG(dbgs() << "CatchHigh[" << FirstTryPad->getName() << "]: " << CatchHigh
3054 } else if (isa<CleanupPadInst>(FirstNonPHI)) {
3055 int CleanupState = addUnwindMapEntry(FuncInfo, ParentState, &BB);
3056 FuncInfo.EHPadStateMap[FirstNonPHI] = CleanupState;
3057 DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB "
3058 << BB.getName() << '\n');
3059 for (const BasicBlock *PredBlock : predecessors(&BB))
3060 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
3061 calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, CleanupState);
3062 } else if (isa<TerminatePadInst>(FirstNonPHI)) {
3063 report_fatal_error("Not yet implemented!");
3065 llvm_unreachable("unexpected EH Pad!");
3069 static int addSEHHandler(WinEHFuncInfo &FuncInfo, int ParentState,
3070 const Function *Filter, const BasicBlock *Handler) {
3071 SEHUnwindMapEntry Entry;
3072 Entry.ToState = ParentState;
3073 Entry.Filter = Filter;
3074 Entry.Handler = Handler;
3075 FuncInfo.SEHUnwindMap.push_back(Entry);
3076 return FuncInfo.SEHUnwindMap.size() - 1;
3079 static void calculateExplicitSEHStateNumbers(WinEHFuncInfo &FuncInfo,
3080 const BasicBlock &BB,
3082 assert(BB.isEHPad());
3083 const Instruction *FirstNonPHI = BB.getFirstNonPHI();
3084 // All catchpad instructions will be handled when we process their
3085 // respective catchendpad instruction.
3086 if (isa<CatchPadInst>(FirstNonPHI))
3089 if (isa<CatchEndPadInst>(FirstNonPHI)) {
3090 // Extract the filter function and the __except basic block and create a
3092 SmallVector<const CatchPadInst *, 1> Handlers;
3093 findCatchPadsForCatchEndPad(&BB, Handlers);
3094 assert(Handlers.size() == 1 &&
3095 "SEH doesn't have multiple handlers per __try");
3096 const CatchPadInst *CPI = Handlers.front();
3097 const BasicBlock *CatchPadBB = CPI->getParent();
3098 const Function *Filter =
3099 cast<Function>(CPI->getArgOperand(0)->stripPointerCasts());
3101 addSEHHandler(FuncInfo, ParentState, Filter, CPI->getNormalDest());
3103 // Everything in the __try block uses TryState as its parent state.
3104 FuncInfo.EHPadStateMap[CPI] = TryState;
3105 DEBUG(dbgs() << "Assigning state #" << TryState << " to BB "
3106 << CatchPadBB->getName() << '\n');
3107 for (const BasicBlock *PredBlock : predecessors(CatchPadBB))
3108 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
3109 calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, TryState);
3111 // Everything in the __except block unwinds to ParentState, just like code
3112 // outside the __try.
3113 FuncInfo.EHPadStateMap[FirstNonPHI] = ParentState;
3114 DEBUG(dbgs() << "Assigning state #" << ParentState << " to BB "
3115 << BB.getName() << '\n');
3116 for (const BasicBlock *PredBlock : predecessors(&BB))
3117 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
3118 calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, ParentState);
3119 } else if (isa<CleanupPadInst>(FirstNonPHI)) {
3121 addSEHHandler(FuncInfo, ParentState, /*Filter=*/nullptr, &BB);
3122 FuncInfo.EHPadStateMap[FirstNonPHI] = CleanupState;
3123 DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB "
3124 << BB.getName() << '\n');
3125 for (const BasicBlock *PredBlock : predecessors(&BB))
3126 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
3127 calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, CleanupState);
3128 } else if (isa<CleanupEndPadInst>(FirstNonPHI)) {
3129 // Anything unwinding through CleanupEndPadInst is in ParentState.
3130 FuncInfo.EHPadStateMap[FirstNonPHI] = ParentState;
3131 DEBUG(dbgs() << "Assigning state #" << ParentState << " to BB "
3132 << BB.getName() << '\n');
3133 for (const BasicBlock *PredBlock : predecessors(&BB))
3134 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
3135 calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, ParentState);
3136 } else if (isa<TerminatePadInst>(FirstNonPHI)) {
3137 report_fatal_error("Not yet implemented!");
3139 llvm_unreachable("unexpected EH Pad!");
3143 /// Check if the EH Pad unwinds to caller. Cleanups are a little bit of a
3144 /// special case because we have to look at the cleanupret instruction that uses
3146 static bool doesEHPadUnwindToCaller(const Instruction *EHPad) {
3147 auto *CPI = dyn_cast<CleanupPadInst>(EHPad);
3149 return EHPad->mayThrow();
3151 // This cleanup does not return or unwind, so we say it unwinds to caller.
3152 if (CPI->use_empty())
3155 const Instruction *User = CPI->user_back();
3156 if (auto *CRI = dyn_cast<CleanupReturnInst>(User))
3157 return CRI->unwindsToCaller();
3158 return cast<CleanupEndPadInst>(User)->unwindsToCaller();
3161 void llvm::calculateSEHStateNumbers(const Function *ParentFn,
3162 WinEHFuncInfo &FuncInfo) {
3163 // Don't compute state numbers twice.
3164 if (!FuncInfo.SEHUnwindMap.empty())
3167 for (const BasicBlock &BB : *ParentFn) {
3168 if (!BB.isEHPad() || !doesEHPadUnwindToCaller(BB.getFirstNonPHI()))
3170 calculateExplicitSEHStateNumbers(FuncInfo, BB, -1);
3174 void llvm::calculateWinCXXEHStateNumbers(const Function *ParentFn,
3175 WinEHFuncInfo &FuncInfo) {
3176 // Return if it's already been done.
3177 if (!FuncInfo.EHPadStateMap.empty())
3180 bool IsExplicit = false;
3181 for (const BasicBlock &BB : *ParentFn) {
3184 const Instruction *FirstNonPHI = BB.getFirstNonPHI();
3185 // Skip cleanupendpads; they are exits, not entries.
3186 if (isa<CleanupEndPadInst>(FirstNonPHI))
3188 if (!doesEHPadUnwindToCaller(FirstNonPHI))
3190 calculateExplicitCXXStateNumbers(FuncInfo, BB, -1);
3197 WinEHNumbering Num(FuncInfo);
3198 Num.findActionRootLPads(*ParentFn);
3199 // The VisitedHandlers list is used by both findActionRootLPads and
3200 // calculateStateNumbers, but both functions need to visit all handlers.
3201 Num.VisitedHandlers.clear();
3202 Num.calculateStateNumbers(*ParentFn);
3203 // Pop everything on the handler stack.
3204 // It may be necessary to call this more than once because a handler can
3205 // be pushed on the stack as a result of clearing the stack.
3206 while (!Num.HandlerStack.empty())
3207 Num.processCallSite(None, ImmutableCallSite());
3210 void WinEHPrepare::replaceTerminatePadWithCleanup(Function &F) {
3211 if (Personality != EHPersonality::MSVC_CXX)
3213 for (BasicBlock &BB : F) {
3214 Instruction *First = BB.getFirstNonPHI();
3215 auto *TPI = dyn_cast<TerminatePadInst>(First);
3219 if (TPI->getNumArgOperands() != 1)
3221 "Expected a unary terminatepad for MSVC C++ personalities!");
3223 auto *TerminateFn = dyn_cast<Function>(TPI->getArgOperand(0));
3225 report_fatal_error("Function operand expected in terminatepad for MSVC "
3226 "C++ personalities!");
3228 // Insert the cleanuppad instruction.
3229 auto *CPI = CleanupPadInst::Create(
3230 BB.getContext(), {}, Twine("terminatepad.for.", BB.getName()), &BB);
3232 // Insert the call to the terminate instruction.
3233 auto *CallTerminate = CallInst::Create(TerminateFn, {}, &BB);
3234 CallTerminate->setDoesNotThrow();
3235 CallTerminate->setDoesNotReturn();
3236 CallTerminate->setCallingConv(TerminateFn->getCallingConv());
3238 // Insert a new terminator for the cleanuppad using the same successor as
3239 // the terminatepad.
3240 CleanupReturnInst::Create(CPI, TPI->getUnwindDest(), &BB);
3242 // Let's remove the terminatepad now that we've inserted the new
3244 TPI->eraseFromParent();
3248 void WinEHPrepare::colorFunclets(Function &F,
3249 SmallVectorImpl<BasicBlock *> &EntryBlocks) {
3250 SmallVector<std::pair<BasicBlock *, BasicBlock *>, 16> Worklist;
3251 BasicBlock *EntryBlock = &F.getEntryBlock();
3253 // Build up the color map, which maps each block to its set of 'colors'.
3254 // For any block B, the "colors" of B are the set of funclets F (possibly
3255 // including a root "funclet" representing the main function), such that
3256 // F will need to directly contain B or a copy of B (where the term "directly
3257 // contain" is used to distinguish from being "transitively contained" in
3258 // a nested funclet).
3259 // Use a CFG walk driven by a worklist of (block, color) pairs. The "color"
3260 // sets attached during this processing to a block which is the entry of some
3261 // funclet F is actually the set of F's parents -- i.e. the union of colors
3262 // of all predecessors of F's entry. For all other blocks, the color sets
3263 // are as defined above. A post-pass fixes up the block color map to reflect
3264 // the same sense of "color" for funclet entries as for other blocks.
3266 Worklist.push_back({EntryBlock, EntryBlock});
3268 while (!Worklist.empty()) {
3269 BasicBlock *Visiting;
3271 std::tie(Visiting, Color) = Worklist.pop_back_val();
3272 Instruction *VisitingHead = Visiting->getFirstNonPHI();
3273 if (VisitingHead->isEHPad() && !isa<CatchEndPadInst>(VisitingHead) &&
3274 !isa<CleanupEndPadInst>(VisitingHead)) {
3275 // Mark this as a funclet head as a member of itself.
3276 FuncletBlocks[Visiting].insert(Visiting);
3277 // Queue exits with the parent color.
3278 for (User *Exit : VisitingHead->users()) {
3279 for (BasicBlock *Succ :
3280 successors(cast<Instruction>(Exit)->getParent())) {
3281 if (BlockColors[Succ].insert(Color).second) {
3282 Worklist.push_back({Succ, Color});
3286 // Handle CatchPad specially since its successors need different colors.
3287 if (CatchPadInst *CatchPad = dyn_cast<CatchPadInst>(VisitingHead)) {
3288 // Visit the normal successor with the color of the new EH pad, and
3289 // visit the unwind successor with the color of the parent.
3290 BasicBlock *NormalSucc = CatchPad->getNormalDest();
3291 if (BlockColors[NormalSucc].insert(Visiting).second) {
3292 Worklist.push_back({NormalSucc, Visiting});
3294 BasicBlock *UnwindSucc = CatchPad->getUnwindDest();
3295 if (BlockColors[UnwindSucc].insert(Color).second) {
3296 Worklist.push_back({UnwindSucc, Color});
3300 // Switch color to the current node, except for terminate pads which
3301 // have no bodies and only unwind successors and so need their successors
3302 // visited with the color of the parent.
3303 if (!isa<TerminatePadInst>(VisitingHead))
3306 // Note that this is a member of the given color.
3307 FuncletBlocks[Color].insert(Visiting);
3310 TerminatorInst *Terminator = Visiting->getTerminator();
3311 if (isa<CleanupReturnInst>(Terminator) ||
3312 isa<CatchReturnInst>(Terminator) ||
3313 isa<CleanupEndPadInst>(Terminator)) {
3314 // These blocks' successors have already been queued with the parent
3318 for (BasicBlock *Succ : successors(Visiting)) {
3319 if (isa<CatchEndPadInst>(Succ->getFirstNonPHI())) {
3320 // The catchendpad needs to be visited with the parent's color, not
3321 // the current color. This will happen in the code above that visits
3322 // any catchpad unwind successor with the parent color, so we can
3323 // safely skip this successor here.
3326 if (BlockColors[Succ].insert(Color).second) {
3327 Worklist.push_back({Succ, Color});
3332 // The processing above actually accumulated the parent set for this
3333 // funclet into the color set for its entry; use the parent set to
3334 // populate the children map, and reset the color set to include just
3335 // the funclet itself (no instruction can target a funclet entry except on
3336 // that transitions to the child funclet).
3337 for (BasicBlock *FuncletEntry : EntryBlocks) {
3338 std::set<BasicBlock *> &ColorMapItem = BlockColors[FuncletEntry];
3339 for (BasicBlock *Parent : ColorMapItem)
3340 FuncletChildren[Parent].insert(FuncletEntry);
3341 ColorMapItem.clear();
3342 ColorMapItem.insert(FuncletEntry);
3346 void WinEHPrepare::demotePHIsOnFunclets(Function &F) {
3347 // Strip PHI nodes off of EH pads.
3348 SmallVector<PHINode *, 16> PHINodes;
3349 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) {
3350 BasicBlock *BB = FI++;
3353 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
3354 Instruction *I = BI++;
3355 auto *PN = dyn_cast<PHINode>(I);
3356 // Stop at the first non-PHI.
3360 AllocaInst *SpillSlot = insertPHILoads(PN, F);
3362 insertPHIStores(PN, SpillSlot);
3364 PHINodes.push_back(PN);
3368 for (auto *PN : PHINodes) {
3369 // There may be lingering uses on other EH PHIs being removed
3370 PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
3371 PN->eraseFromParent();
3375 void WinEHPrepare::demoteUsesBetweenFunclets(Function &F) {
3376 // Turn all inter-funclet uses of a Value into loads and stores.
3377 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) {
3378 BasicBlock *BB = FI++;
3379 std::set<BasicBlock *> &ColorsForBB = BlockColors[BB];
3380 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
3381 Instruction *I = BI++;
3382 // Funclets are permitted to use static allocas.
3383 if (auto *AI = dyn_cast<AllocaInst>(I))
3384 if (AI->isStaticAlloca())
3387 demoteNonlocalUses(I, ColorsForBB, F);
3392 void WinEHPrepare::demoteArgumentUses(Function &F) {
3393 // Also demote function parameters used in funclets.
3394 std::set<BasicBlock *> &ColorsForEntry = BlockColors[&F.getEntryBlock()];
3395 for (Argument &Arg : F.args())
3396 demoteNonlocalUses(&Arg, ColorsForEntry, F);
3399 void WinEHPrepare::cloneCommonBlocks(
3400 Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks) {
3401 // We need to clone all blocks which belong to multiple funclets. Values are
3402 // remapped throughout the funclet to propogate both the new instructions
3403 // *and* the new basic blocks themselves.
3404 for (BasicBlock *FuncletPadBB : EntryBlocks) {
3405 std::set<BasicBlock *> &BlocksInFunclet = FuncletBlocks[FuncletPadBB];
3407 std::map<BasicBlock *, BasicBlock *> Orig2Clone;
3408 ValueToValueMapTy VMap;
3409 for (BasicBlock *BB : BlocksInFunclet) {
3410 std::set<BasicBlock *> &ColorsForBB = BlockColors[BB];
3411 // We don't need to do anything if the block is monochromatic.
3412 size_t NumColorsForBB = ColorsForBB.size();
3413 if (NumColorsForBB == 1)
3416 // Create a new basic block and copy instructions into it!
3418 CloneBasicBlock(BB, VMap, Twine(".for.", FuncletPadBB->getName()));
3419 // Insert the clone immediately after the original to ensure determinism
3420 // and to keep the same relative ordering of any funclet's blocks.
3421 CBB->insertInto(&F, BB->getNextNode());
3423 // Add basic block mapping.
3426 // Record delta operations that we need to perform to our color mappings.
3427 Orig2Clone[BB] = CBB;
3430 // Update our color mappings to reflect that one block has lost a color and
3431 // another has gained a color.
3432 for (auto &BBMapping : Orig2Clone) {
3433 BasicBlock *OldBlock = BBMapping.first;
3434 BasicBlock *NewBlock = BBMapping.second;
3436 BlocksInFunclet.insert(NewBlock);
3437 BlockColors[NewBlock].insert(FuncletPadBB);
3439 BlocksInFunclet.erase(OldBlock);
3440 BlockColors[OldBlock].erase(FuncletPadBB);
3443 // Loop over all of the instructions in the function, fixing up operand
3444 // references as we go. This uses VMap to do all the hard work.
3445 for (BasicBlock *BB : BlocksInFunclet)
3446 // Loop over all instructions, fixing each one as we find it...
3447 for (Instruction &I : *BB)
3448 RemapInstruction(&I, VMap, RF_IgnoreMissingEntries);
3450 // Check to see if SuccBB has PHI nodes. If so, we need to add entries to
3451 // the PHI nodes for NewBB now.
3452 for (auto &BBMapping : Orig2Clone) {
3453 BasicBlock *OldBlock = BBMapping.first;
3454 BasicBlock *NewBlock = BBMapping.second;
3455 for (BasicBlock *SuccBB : successors(NewBlock)) {
3456 for (Instruction &SuccI : *SuccBB) {
3457 auto *SuccPN = dyn_cast<PHINode>(&SuccI);
3461 // Ok, we have a PHI node. Figure out what the incoming value was for
3463 int OldBlockIdx = SuccPN->getBasicBlockIndex(OldBlock);
3464 if (OldBlockIdx == -1)
3466 Value *IV = SuccPN->getIncomingValue(OldBlockIdx);
3468 // Remap the value if necessary.
3469 if (auto *Inst = dyn_cast<Instruction>(IV)) {
3470 ValueToValueMapTy::iterator I = VMap.find(Inst);
3471 if (I != VMap.end())
3475 SuccPN->addIncoming(IV, NewBlock);
3480 for (ValueToValueMapTy::value_type VT : VMap) {
3481 // If there were values defined in BB that are used outside the funclet,
3482 // then we now have to update all uses of the value to use either the
3483 // original value, the cloned value, or some PHI derived value. This can
3484 // require arbitrary PHI insertion, of which we are prepared to do, clean
3486 SmallVector<Use *, 16> UsesToRename;
3488 auto *OldI = dyn_cast<Instruction>(const_cast<Value *>(VT.first));
3491 auto *NewI = cast<Instruction>(VT.second);
3492 // Scan all uses of this instruction to see if it is used outside of its
3493 // funclet, and if so, record them in UsesToRename.
3494 for (Use &U : OldI->uses()) {
3495 Instruction *UserI = cast<Instruction>(U.getUser());
3496 BasicBlock *UserBB = UserI->getParent();
3497 std::set<BasicBlock *> &ColorsForUserBB = BlockColors[UserBB];
3498 assert(!ColorsForUserBB.empty());
3499 if (ColorsForUserBB.size() > 1 ||
3500 *ColorsForUserBB.begin() != FuncletPadBB)
3501 UsesToRename.push_back(&U);
3504 // If there are no uses outside the block, we're done with this
3506 if (UsesToRename.empty())
3509 // We found a use of OldI outside of the funclet. Rename all uses of OldI
3510 // that are outside its funclet to be uses of the appropriate PHI node
3512 SSAUpdater SSAUpdate;
3513 SSAUpdate.Initialize(OldI->getType(), OldI->getName());
3514 SSAUpdate.AddAvailableValue(OldI->getParent(), OldI);
3515 SSAUpdate.AddAvailableValue(NewI->getParent(), NewI);
3517 while (!UsesToRename.empty())
3518 SSAUpdate.RewriteUseAfterInsertions(*UsesToRename.pop_back_val());
3523 void WinEHPrepare::removeImplausibleTerminators(Function &F) {
3524 // Remove implausible terminators and replace them with UnreachableInst.
3525 for (auto &Funclet : FuncletBlocks) {
3526 BasicBlock *FuncletPadBB = Funclet.first;
3527 std::set<BasicBlock *> &BlocksInFunclet = Funclet.second;
3528 Instruction *FirstNonPHI = FuncletPadBB->getFirstNonPHI();
3529 auto *CatchPad = dyn_cast<CatchPadInst>(FirstNonPHI);
3530 auto *CleanupPad = dyn_cast<CleanupPadInst>(FirstNonPHI);
3532 for (BasicBlock *BB : BlocksInFunclet) {
3533 TerminatorInst *TI = BB->getTerminator();
3534 // CatchPadInst and CleanupPadInst can't transfer control to a ReturnInst.
3535 bool IsUnreachableRet = isa<ReturnInst>(TI) && (CatchPad || CleanupPad);
3536 // The token consumed by a CatchReturnInst must match the funclet token.
3537 bool IsUnreachableCatchret = false;
3538 if (auto *CRI = dyn_cast<CatchReturnInst>(TI))
3539 IsUnreachableCatchret = CRI->getCatchPad() != CatchPad;
3540 // The token consumed by a CleanupReturnInst must match the funclet token.
3541 bool IsUnreachableCleanupret = false;
3542 if (auto *CRI = dyn_cast<CleanupReturnInst>(TI))
3543 IsUnreachableCleanupret = CRI->getCleanupPad() != CleanupPad;
3544 // The token consumed by a CleanupEndPadInst must match the funclet token.
3545 bool IsUnreachableCleanupendpad = false;
3546 if (auto *CEPI = dyn_cast<CleanupEndPadInst>(TI))
3547 IsUnreachableCleanupendpad = CEPI->getCleanupPad() != CleanupPad;
3548 if (IsUnreachableRet || IsUnreachableCatchret ||
3549 IsUnreachableCleanupret || IsUnreachableCleanupendpad) {
3550 // Loop through all of our successors and make sure they know that one
3551 // of their predecessors is going away.
3552 for (BasicBlock *SuccBB : TI->successors())
3553 SuccBB->removePredecessor(BB);
3555 new UnreachableInst(BB->getContext(), TI);
3556 TI->eraseFromParent();
3562 void WinEHPrepare::cleanupPreparedFunclets(Function &F) {
3563 // Clean-up some of the mess we made by removing useles PHI nodes, trivial
3565 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) {
3566 BasicBlock *BB = FI++;
3567 SimplifyInstructionsInBlock(BB);
3568 ConstantFoldTerminator(BB, /*DeleteDeadConditions=*/true);
3569 MergeBlockIntoPredecessor(BB);
3572 // We might have some unreachable blocks after cleaning up some impossible
3574 removeUnreachableBlocks(F);
3577 void WinEHPrepare::verifyPreparedFunclets(Function &F) {
3578 // Recolor the CFG to verify that all is well.
3579 for (BasicBlock &BB : F) {
3580 size_t NumColors = BlockColors[&BB].size();
3581 assert(NumColors == 1 && "Expected monochromatic BB!");
3583 report_fatal_error("Uncolored BB!");
3585 report_fatal_error("Multicolor BB!");
3586 if (!DisableDemotion) {
3587 bool EHPadHasPHI = BB.isEHPad() && isa<PHINode>(BB.begin());
3588 assert(!EHPadHasPHI && "EH Pad still has a PHI!");
3590 report_fatal_error("EH Pad still has a PHI!");
3595 bool WinEHPrepare::prepareExplicitEH(
3596 Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks) {
3597 // Remove unreachable blocks. It is not valuable to assign them a color and
3598 // their existence can trick us into thinking values are alive when they are
3600 removeUnreachableBlocks(F);
3602 replaceTerminatePadWithCleanup(F);
3604 // Determine which blocks are reachable from which funclet entries.
3605 colorFunclets(F, EntryBlocks);
3607 if (!DisableDemotion) {
3608 demotePHIsOnFunclets(F);
3610 demoteUsesBetweenFunclets(F);
3612 demoteArgumentUses(F);
3615 cloneCommonBlocks(F, EntryBlocks);
3617 if (!DisableCleanups) {
3618 removeImplausibleTerminators(F);
3620 cleanupPreparedFunclets(F);
3623 verifyPreparedFunclets(F);
3625 BlockColors.clear();
3626 FuncletBlocks.clear();
3627 FuncletChildren.clear();
3632 // TODO: Share loads when one use dominates another, or when a catchpad exit
3633 // dominates uses (needs dominators).
3634 AllocaInst *WinEHPrepare::insertPHILoads(PHINode *PN, Function &F) {
3635 BasicBlock *PHIBlock = PN->getParent();
3636 AllocaInst *SpillSlot = nullptr;
3638 if (isa<CleanupPadInst>(PHIBlock->getFirstNonPHI())) {
3639 // Insert a load in place of the PHI and replace all uses.
3640 SpillSlot = new AllocaInst(PN->getType(), nullptr,
3641 Twine(PN->getName(), ".wineh.spillslot"),
3642 F.getEntryBlock().begin());
3643 Value *V = new LoadInst(SpillSlot, Twine(PN->getName(), ".wineh.reload"),
3644 PHIBlock->getFirstInsertionPt());
3645 PN->replaceAllUsesWith(V);
3649 DenseMap<BasicBlock *, Value *> Loads;
3650 for (Value::use_iterator UI = PN->use_begin(), UE = PN->use_end();
3653 auto *UsingInst = cast<Instruction>(U.getUser());
3654 BasicBlock *UsingBB = UsingInst->getParent();
3655 if (UsingBB->isEHPad()) {
3656 // Use is on an EH pad phi. Leave it alone; we'll insert loads and
3657 // stores for it separately.
3658 assert(isa<PHINode>(UsingInst));
3661 replaceUseWithLoad(PN, U, SpillSlot, Loads, F);
3666 // TODO: improve store placement. Inserting at def is probably good, but need
3667 // to be careful not to introduce interfering stores (needs liveness analysis).
3668 // TODO: identify related phi nodes that can share spill slots, and share them
3669 // (also needs liveness).
3670 void WinEHPrepare::insertPHIStores(PHINode *OriginalPHI,
3671 AllocaInst *SpillSlot) {
3672 // Use a worklist of (Block, Value) pairs -- the given Value needs to be
3673 // stored to the spill slot by the end of the given Block.
3674 SmallVector<std::pair<BasicBlock *, Value *>, 4> Worklist;
3676 Worklist.push_back({OriginalPHI->getParent(), OriginalPHI});
3678 while (!Worklist.empty()) {
3679 BasicBlock *EHBlock;
3681 std::tie(EHBlock, InVal) = Worklist.pop_back_val();
3683 PHINode *PN = dyn_cast<PHINode>(InVal);
3684 if (PN && PN->getParent() == EHBlock) {
3685 // The value is defined by another PHI we need to remove, with no room to
3686 // insert a store after the PHI, so each predecessor needs to store its
3688 for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i) {
3689 Value *PredVal = PN->getIncomingValue(i);
3691 // Undef can safely be skipped.
3692 if (isa<UndefValue>(PredVal))
3695 insertPHIStore(PN->getIncomingBlock(i), PredVal, SpillSlot, Worklist);
3698 // We need to store InVal, which dominates EHBlock, but can't put a store
3699 // in EHBlock, so need to put stores in each predecessor.
3700 for (BasicBlock *PredBlock : predecessors(EHBlock)) {
3701 insertPHIStore(PredBlock, InVal, SpillSlot, Worklist);
3707 void WinEHPrepare::insertPHIStore(
3708 BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot,
3709 SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist) {
3711 if (PredBlock->isEHPad() &&
3712 !isa<CleanupPadInst>(PredBlock->getFirstNonPHI())) {
3713 // Pred is unsplittable, so we need to queue it on the worklist.
3714 Worklist.push_back({PredBlock, PredVal});
3718 // Otherwise, insert the store at the end of the basic block.
3719 new StoreInst(PredVal, SpillSlot, PredBlock->getTerminator());
3722 // TODO: Share loads for same-funclet uses (requires dominators if funclets
3723 // aren't properly nested).
3724 void WinEHPrepare::demoteNonlocalUses(Value *V,
3725 std::set<BasicBlock *> &ColorsForBB,
3727 // Tokens can only be used non-locally due to control flow involving
3728 // unreachable edges. Don't try to demote the token usage, we'll simply
3729 // delete the cloned user later.
3730 if (isa<CatchPadInst>(V) || isa<CleanupPadInst>(V))
3733 DenseMap<BasicBlock *, Value *> Loads;
3734 AllocaInst *SpillSlot = nullptr;
3735 for (Value::use_iterator UI = V->use_begin(), UE = V->use_end(); UI != UE;) {
3737 auto *UsingInst = cast<Instruction>(U.getUser());
3738 BasicBlock *UsingBB = UsingInst->getParent();
3740 // Is the Use inside a block which is colored the same as the Def?
3741 // If so, we don't need to escape the Def because we will clone
3742 // ourselves our own private copy.
3743 std::set<BasicBlock *> &ColorsForUsingBB = BlockColors[UsingBB];
3744 if (ColorsForUsingBB == ColorsForBB)
3747 replaceUseWithLoad(V, U, SpillSlot, Loads, F);
3750 // Insert stores of the computed value into the stack slot.
3751 // We have to be careful if I is an invoke instruction,
3752 // because we can't insert the store AFTER the terminator instruction.
3753 BasicBlock::iterator InsertPt;
3754 if (isa<Argument>(V)) {
3755 InsertPt = F.getEntryBlock().getTerminator();
3756 } else if (isa<TerminatorInst>(V)) {
3757 auto *II = cast<InvokeInst>(V);
3758 // We cannot demote invoke instructions to the stack if their normal
3759 // edge is critical. Therefore, split the critical edge and create a
3760 // basic block into which the store can be inserted.
3761 if (!II->getNormalDest()->getSinglePredecessor()) {
3763 GetSuccessorNumber(II->getParent(), II->getNormalDest());
3764 assert(isCriticalEdge(II, SuccNum) && "Expected a critical edge!");
3765 BasicBlock *NewBlock = SplitCriticalEdge(II, SuccNum);
3766 assert(NewBlock && "Unable to split critical edge.");
3767 // Update the color mapping for the newly split edge.
3768 std::set<BasicBlock *> &ColorsForUsingBB = BlockColors[II->getParent()];
3769 BlockColors[NewBlock] = ColorsForUsingBB;
3770 for (BasicBlock *FuncletPad : ColorsForUsingBB)
3771 FuncletBlocks[FuncletPad].insert(NewBlock);
3773 InsertPt = II->getNormalDest()->getFirstInsertionPt();
3775 InsertPt = cast<Instruction>(V);
3777 // Don't insert before PHI nodes or EH pad instrs.
3778 for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt)
3781 new StoreInst(V, SpillSlot, InsertPt);
3785 void WinEHPrepare::replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
3786 DenseMap<BasicBlock *, Value *> &Loads,
3788 // Lazilly create the spill slot.
3790 SpillSlot = new AllocaInst(V->getType(), nullptr,
3791 Twine(V->getName(), ".wineh.spillslot"),
3792 F.getEntryBlock().begin());
3794 auto *UsingInst = cast<Instruction>(U.getUser());
3795 if (auto *UsingPHI = dyn_cast<PHINode>(UsingInst)) {
3796 // If this is a PHI node, we can't insert a load of the value before
3797 // the use. Instead insert the load in the predecessor block
3798 // corresponding to the incoming value.
3800 // Note that if there are multiple edges from a basic block to this
3801 // PHI node that we cannot have multiple loads. The problem is that
3802 // the resulting PHI node will have multiple values (from each load)
3803 // coming in from the same block, which is illegal SSA form.
3804 // For this reason, we keep track of and reuse loads we insert.
3805 BasicBlock *IncomingBlock = UsingPHI->getIncomingBlock(U);
3806 if (auto *CatchRet =
3807 dyn_cast<CatchReturnInst>(IncomingBlock->getTerminator())) {
3808 // Putting a load above a catchret and use on the phi would still leave
3809 // a cross-funclet def/use. We need to split the edge, change the
3810 // catchret to target the new block, and put the load there.
3811 BasicBlock *PHIBlock = UsingInst->getParent();
3812 BasicBlock *NewBlock = SplitEdge(IncomingBlock, PHIBlock);
3813 // SplitEdge gives us:
3816 // br label %NewBlock
3818 // catchret label %PHIBlock
3822 // catchret label %NewBlock
3824 // br label %PHIBlock
3825 // So move the terminators to each others' blocks and swap their
3827 BranchInst *Goto = cast<BranchInst>(IncomingBlock->getTerminator());
3828 Goto->removeFromParent();
3829 CatchRet->removeFromParent();
3830 IncomingBlock->getInstList().push_back(CatchRet);
3831 NewBlock->getInstList().push_back(Goto);
3832 Goto->setSuccessor(0, PHIBlock);
3833 CatchRet->setSuccessor(NewBlock);
3834 // Update the color mapping for the newly split edge.
3835 std::set<BasicBlock *> &ColorsForPHIBlock = BlockColors[PHIBlock];
3836 BlockColors[NewBlock] = ColorsForPHIBlock;
3837 for (BasicBlock *FuncletPad : ColorsForPHIBlock)
3838 FuncletBlocks[FuncletPad].insert(NewBlock);
3839 // Treat the new block as incoming for load insertion.
3840 IncomingBlock = NewBlock;
3842 Value *&Load = Loads[IncomingBlock];
3843 // Insert the load into the predecessor block
3845 Load = new LoadInst(SpillSlot, Twine(V->getName(), ".wineh.reload"),
3846 /*Volatile=*/false, IncomingBlock->getTerminator());
3850 // Reload right before the old use.
3851 auto *Load = new LoadInst(SpillSlot, Twine(V->getName(), ".wineh.reload"),
3852 /*Volatile=*/false, UsingInst);