llvm.org GIT mirror llvm / c839e94
[WinEH] Delete the old landingpad implementation of Windows EH The new implementation works at least as well as the old implementation did. Also delete the associated preparation tests. They don't exercise interesting corner cases of the new implementation. All the codegen tests of the EH tables have already been ported. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@249918 91177308-0d34-0410-b5e6-96231b3b80d8 Reid Kleckner 4 years ago
39 changed file(s) with 119 addition(s) and 8050 deletion(s). Raw diff Collapse all Expand all
3232 class MachineBasicBlock;
3333 class Value;
3434
35 enum ActionType { Catch, Cleanup };
36
37 class ActionHandler {
38 public:
39 ActionHandler(BasicBlock *BB, ActionType Type)
40 : StartBB(BB), Type(Type), EHState(-1), HandlerBlockOrFunc(nullptr) {}
41
42 ActionType getType() const { return Type; }
43 BasicBlock *getStartBlock() const { return StartBB; }
44
45 bool hasBeenProcessed() { return HandlerBlockOrFunc != nullptr; }
46
47 void setHandlerBlockOrFunc(Constant *F) { HandlerBlockOrFunc = F; }
48 Constant *getHandlerBlockOrFunc() { return HandlerBlockOrFunc; }
49
50 void setEHState(int State) { EHState = State; }
51 int getEHState() const { return EHState; }
52
53 private:
54 BasicBlock *StartBB;
55 ActionType Type;
56 int EHState;
57
58 // Can be either a BlockAddress or a Function depending on the EH personality.
59 Constant *HandlerBlockOrFunc;
60 };
61
62 class CatchHandler : public ActionHandler {
63 public:
64 CatchHandler(BasicBlock *BB, Constant *Selector, BasicBlock *NextBB)
65 : ActionHandler(BB, ActionType::Catch), Selector(Selector),
66 NextBB(NextBB), ExceptionObjectVar(nullptr),
67 ExceptionObjectIndex(-1) {}
68
69 // Method for support type inquiry through isa, cast, and dyn_cast:
70 static inline bool classof(const ActionHandler *H) {
71 return H->getType() == ActionType::Catch;
72 }
73
74 Constant *getSelector() const { return Selector; }
75 BasicBlock *getNextBB() const { return NextBB; }
76
77 const Value *getExceptionVar() { return ExceptionObjectVar; }
78 TinyPtrVector &getReturnTargets() { return ReturnTargets; }
79
80 void setExceptionVar(const Value *Val) { ExceptionObjectVar = Val; }
81 void setExceptionVarIndex(int Index) { ExceptionObjectIndex = Index; }
82 int getExceptionVarIndex() const { return ExceptionObjectIndex; }
83 void setReturnTargets(TinyPtrVector &Targets) {
84 ReturnTargets = Targets;
85 }
86
87 private:
88 Constant *Selector;
89 BasicBlock *NextBB;
90 // While catch handlers are being outlined the ExceptionObjectVar field will
91 // be populated with the instruction in the parent frame that corresponds
92 // to the exception object (or nullptr if the catch does not use an
93 // exception object) and the ExceptionObjectIndex field will be -1.
94 // When the parseEHActions function is called to populate a vector of
95 // instances of this class, the ExceptionObjectVar field will be nullptr
96 // and the ExceptionObjectIndex will be the index of the exception object in
97 // the parent function's localescape block.
98 const Value *ExceptionObjectVar;
99 int ExceptionObjectIndex;
100 TinyPtrVector ReturnTargets;
101 };
102
103 class CleanupHandler : public ActionHandler {
104 public:
105 CleanupHandler(BasicBlock *BB) : ActionHandler(BB, ActionType::Cleanup) {}
106
107 // Method for support type inquiry through isa, cast, and dyn_cast:
108 static inline bool classof(const ActionHandler *H) {
109 return H->getType() == ActionType::Cleanup;
110 }
111 };
112
113 void parseEHActions(const IntrinsicInst *II,
114 SmallVectorImpl> &Actions);
115
116 // The following structs respresent the .xdata for functions using C++
117 // exceptions on Windows.
35 // The following structs respresent the .xdata tables for various
36 // Windows-related EH personalities.
11837
11938 typedef PointerUnion MBBOrBasicBlock;
12039 typedef PointerUnion ValueOrMBB;
12140
122 struct WinEHUnwindMapEntry {
41 struct CxxUnwindMapEntry {
12342 int ToState;
12443 ValueOrMBB Cleanup;
12544 };
12645
127 /// Similar to WinEHUnwindMapEntry, but supports SEH filters.
46 /// Similar to CxxUnwindMapEntry, but supports SEH filters.
12847 struct SEHUnwindMapEntry {
12948 /// If unwinding continues through this handler, transition to the handler at
13049 /// this state. This indexes into SEHUnwindMap.
17392 DenseMap
17493 CatchRetSuccessorColorMap;
17594 DenseMap> InvokeToStateMap;
176 SmallVector<WinEHUnwindMapEntry, 4> UnwindMap;
95 SmallVector<CxxUnwindMapEntry, 4> CxxUnwindMap;
17796 SmallVector TryBlockMap;
17897 SmallVector SEHUnwindMap;
17998 SmallVector ClrEHUnwindMap;
18099 int UnwindHelpFrameIdx = INT_MAX;
181100
182 int getLastStateNumber() const { return UnwindMap.size() - 1; }
101 int getLastStateNumber() const { return CxxUnwindMap.size() - 1; }
183102
184103 void addIPToStateRange(const BasicBlock *PadBB, MCSymbol *InvokeBegin,
185104 MCSymbol *InvokeEnd);
428428 def int_eh_return_i32 : Intrinsic<[], [llvm_i32_ty, llvm_ptr_ty]>;
429429 def int_eh_return_i64 : Intrinsic<[], [llvm_i64_ty, llvm_ptr_ty]>;
430430
431 // eh.begincatch takes a pointer returned by a landingpad instruction and
432 // copies the exception object into the memory pointed to by the second
433 // parameter. If the second parameter is null, no copy occurs.
434 def int_eh_begincatch : Intrinsic<[], [llvm_ptr_ty, llvm_ptr_ty],
435 [NoCapture<0>, NoCapture<1>]>;
436 def int_eh_endcatch : Intrinsic<[], []>;
437
438431 // eh.exceptionpointer returns the pointer to the exception caught by
439432 // the given `catchpad`.
440433 def int_eh_exceptionpointer : Intrinsic<[llvm_anyptr_ty], [llvm_token_ty],
442435
443436 // Gets the exception code from a catchpad token. Only used on some platforms.
444437 def int_eh_exceptioncode : Intrinsic<[llvm_i32_ty], [llvm_token_ty], [IntrNoMem]>;
445
446 // Represents the list of actions to take when an exception is thrown.
447 def int_eh_actions : Intrinsic<[llvm_ptr_ty], [llvm_vararg_ty], []>;
448
449 // FIXME: Remove this when landing pad EH can be removed.
450 def int_eh_exceptioncode_old : Intrinsic<[llvm_i32_ty], [], [IntrReadMem]>;
451438
452439 // __builtin_unwind_init is an undocumented GCC intrinsic that causes all
453440 // callee-saved registers to be saved and restored (regardless of whether they
344344 visitMemoryReference(I, CS.getArgument(0), MemoryLocation::UnknownSize, 0,
345345 nullptr, MemRef::Read | MemRef::Write);
346346 break;
347
348 case Intrinsic::eh_begincatch:
349 visitEHBeginCatch(II);
350 break;
351 case Intrinsic::eh_endcatch:
352 visitEHEndCatch(II);
353 break;
354347 }
355348 }
356349
508501 dyn_cast(findValue(I.getOperand(1), /*OffsetOk=*/false)))
509502 Assert(CI->getValue().ult(cast(I.getType())->getBitWidth()),
510503 "Undefined result: Shift count out of range", &I);
511 }
512
513 static bool
514 allPredsCameFromLandingPad(BasicBlock *BB,
515 SmallSet &VisitedBlocks) {
516 VisitedBlocks.insert(BB);
517 if (BB->isLandingPad())
518 return true;
519 // If we find a block with no predecessors, the search failed.
520 if (pred_empty(BB))
521 return false;
522 for (BasicBlock *Pred : predecessors(BB)) {
523 if (VisitedBlocks.count(Pred))
524 continue;
525 if (!allPredsCameFromLandingPad(Pred, VisitedBlocks))
526 return false;
527 }
528 return true;
529 }
530
531 static bool
532 allSuccessorsReachEndCatch(BasicBlock *BB, BasicBlock::iterator InstBegin,
533 IntrinsicInst **SecondBeginCatch,
534 SmallSet &VisitedBlocks) {
535 VisitedBlocks.insert(BB);
536 for (BasicBlock::iterator I = InstBegin, E = BB->end(); I != E; ++I) {
537 IntrinsicInst *IC = dyn_cast(I);
538 if (IC && IC->getIntrinsicID() == Intrinsic::eh_endcatch)
539 return true;
540 // If we find another begincatch while looking for an endcatch,
541 // that's also an error.
542 if (IC && IC->getIntrinsicID() == Intrinsic::eh_begincatch) {
543 *SecondBeginCatch = IC;
544 return false;
545 }
546 }
547
548 // If we reach a block with no successors while searching, the
549 // search has failed.
550 if (succ_empty(BB))
551 return false;
552 // Otherwise, search all of the successors.
553 for (BasicBlock *Succ : successors(BB)) {
554 if (VisitedBlocks.count(Succ))
555 continue;
556 if (!allSuccessorsReachEndCatch(Succ, Succ->begin(), SecondBeginCatch,
557 VisitedBlocks))
558 return false;
559 }
560 return true;
561 }
562
563 void Lint::visitEHBeginCatch(IntrinsicInst *II) {
564 // The checks in this function make a potentially dubious assumption about
565 // the CFG, namely that any block involved in a catch is only used for the
566 // catch. This will very likely be true of IR generated by a front end,
567 // but it may cease to be true, for example, if the IR is run through a
568 // pass which combines similar blocks.
569 //
570 // In general, if we encounter a block the isn't dominated by the catch
571 // block while we are searching the catch block's successors for a call
572 // to end catch intrinsic, then it is possible that it will be legal for
573 // a path through this block to never reach a call to llvm.eh.endcatch.
574 // An analogous statement could be made about our search for a landing
575 // pad among the catch block's predecessors.
576 //
577 // What is actually required is that no path is possible at runtime that
578 // reaches a call to llvm.eh.begincatch without having previously visited
579 // a landingpad instruction and that no path is possible at runtime that
580 // calls llvm.eh.begincatch and does not subsequently call llvm.eh.endcatch
581 // (mentally adjusting for the fact that in reality these calls will be
582 // removed before code generation).
583 //
584 // Because this is a lint check, we take a pessimistic approach and warn if
585 // the control flow is potentially incorrect.
586
587 SmallSet VisitedBlocks;
588 BasicBlock *CatchBB = II->getParent();
589
590 // The begin catch must occur in a landing pad block or all paths
591 // to it must have come from a landing pad.
592 Assert(allPredsCameFromLandingPad(CatchBB, VisitedBlocks),
593 "llvm.eh.begincatch may be reachable without passing a landingpad",
594 II);
595
596 // Reset the visited block list.
597 VisitedBlocks.clear();
598
599 IntrinsicInst *SecondBeginCatch = nullptr;
600
601 // This has to be called before it is asserted. Otherwise, the first assert
602 // below can never be hit.
603 bool EndCatchFound = allSuccessorsReachEndCatch(
604 CatchBB, std::next(static_cast(II)),
605 &SecondBeginCatch, VisitedBlocks);
606 Assert(
607 SecondBeginCatch == nullptr,
608 "llvm.eh.begincatch may be called a second time before llvm.eh.endcatch",
609 II, SecondBeginCatch);
610 Assert(EndCatchFound,
611 "Some paths from llvm.eh.begincatch may not reach llvm.eh.endcatch",
612 II);
613 }
614
615 static bool allPredCameFromBeginCatch(
616 BasicBlock *BB, BasicBlock::reverse_iterator InstRbegin,
617 IntrinsicInst **SecondEndCatch, SmallSet &VisitedBlocks) {
618 VisitedBlocks.insert(BB);
619 // Look for a begincatch in this block.
620 for (BasicBlock::reverse_iterator RI = InstRbegin, RE = BB->rend(); RI != RE;
621 ++RI) {
622 IntrinsicInst *IC = dyn_cast(&*RI);
623 if (IC && IC->getIntrinsicID() == Intrinsic::eh_begincatch)
624 return true;
625 // If we find another end catch before we find a begin catch, that's
626 // an error.
627 if (IC && IC->getIntrinsicID() == Intrinsic::eh_endcatch) {
628 *SecondEndCatch = IC;
629 return false;
630 }
631 // If we encounter a landingpad instruction, the search failed.
632 if (isa(*RI))
633 return false;
634 }
635 // If while searching we find a block with no predeccesors,
636 // the search failed.
637 if (pred_empty(BB))
638 return false;
639 // Search any predecessors we haven't seen before.
640 for (BasicBlock *Pred : predecessors(BB)) {
641 if (VisitedBlocks.count(Pred))
642 continue;
643 if (!allPredCameFromBeginCatch(Pred, Pred->rbegin(), SecondEndCatch,
644 VisitedBlocks))
645 return false;
646 }
647 return true;
648 }
649
650 void Lint::visitEHEndCatch(IntrinsicInst *II) {
651 // The check in this function makes a potentially dubious assumption about
652 // the CFG, namely that any block involved in a catch is only used for the
653 // catch. This will very likely be true of IR generated by a front end,
654 // but it may cease to be true, for example, if the IR is run through a
655 // pass which combines similar blocks.
656 //
657 // In general, if we encounter a block the isn't post-dominated by the
658 // end catch block while we are searching the end catch block's predecessors
659 // for a call to the begin catch intrinsic, then it is possible that it will
660 // be legal for a path to reach the end catch block without ever having
661 // called llvm.eh.begincatch.
662 //
663 // What is actually required is that no path is possible at runtime that
664 // reaches a call to llvm.eh.endcatch without having previously visited
665 // a call to llvm.eh.begincatch (mentally adjusting for the fact that in
666 // reality these calls will be removed before code generation).
667 //
668 // Because this is a lint check, we take a pessimistic approach and warn if
669 // the control flow is potentially incorrect.
670
671 BasicBlock *EndCatchBB = II->getParent();
672
673 // Alls paths to the end catch call must pass through a begin catch call.
674
675 // If llvm.eh.begincatch wasn't called in the current block, we'll use this
676 // lambda to recursively look for it in predecessors.
677 SmallSet VisitedBlocks;
678 IntrinsicInst *SecondEndCatch = nullptr;
679
680 // This has to be called before it is asserted. Otherwise, the first assert
681 // below can never be hit.
682 bool BeginCatchFound =
683 allPredCameFromBeginCatch(EndCatchBB, BasicBlock::reverse_iterator(II),
684 &SecondEndCatch, VisitedBlocks);
685 Assert(
686 SecondEndCatch == nullptr,
687 "llvm.eh.endcatch may be called a second time after llvm.eh.begincatch",
688 II, SecondEndCatch);
689 Assert(BeginCatchFound,
690 "llvm.eh.endcatch may be reachable without passing llvm.eh.begincatch",
691 II);
692504 }
693505
694506 static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT,
417417 /// imagerel32 LabelStart;
418418 /// imagerel32 LabelEnd;
419419 /// imagerel32 FilterOrFinally; // One means catch-all.
420 /// imagerel32 ExceptOrNull; // Zero means __finally.
420 /// imagerel32 LabelLPad; // Zero means __finally.
421421 /// } Entries[NumEntries];
422422 /// };
423423 void WinException::emitCSpecificHandlerTable(const MachineFunction *MF) {
425425 MCContext &Ctx = Asm->OutContext;
426426
427427 WinEHFuncInfo &FuncInfo = MMI->getWinEHFuncInfo(MF->getFunction());
428 if (!FuncInfo.SEHUnwindMap.empty()) {
429 // Remember what state we were in the last time we found a begin try label.
430 // This allows us to coalesce many nearby invokes with the same state into
431 // one entry.
432 int LastEHState = -1;
433 MCSymbol *LastBeginLabel = nullptr;
434 MCSymbol *LastEndLabel = nullptr;
435
436 // Use the assembler to compute the number of table entries through label
437 // difference and division.
438 MCSymbol *TableBegin =
439 Ctx.createTempSymbol("lsda_begin", /*AlwaysAddSuffix=*/true);
440 MCSymbol *TableEnd =
441 Ctx.createTempSymbol("lsda_end", /*AlwaysAddSuffix=*/true);
442 const MCExpr *LabelDiff =
443 MCBinaryExpr::createSub(MCSymbolRefExpr::create(TableEnd, Ctx),
444 MCSymbolRefExpr::create(TableBegin, Ctx), Ctx);
445 const MCExpr *EntrySize = MCConstantExpr::create(16, Ctx);
446 const MCExpr *EntryCount =
447 MCBinaryExpr::createDiv(LabelDiff, EntrySize, Ctx);
448 OS.EmitValue(EntryCount, 4);
449
450 OS.EmitLabel(TableBegin);
451
452 // Iterate over all the invoke try ranges. Unlike MSVC, LLVM currently only
453 // models exceptions from invokes. LLVM also allows arbitrary reordering of
454 // the code, so our tables end up looking a bit different. Rather than
455 // trying to match MSVC's tables exactly, we emit a denormalized table. For
456 // each range of invokes in the same state, we emit table entries for all
457 // the actions that would be taken in that state. This means our tables are
458 // slightly bigger, which is OK.
459 for (const auto &MBB : *MF) {
460 // Break out before we enter into a finally funclet.
461 // FIXME: We need to emit separate EH tables for cleanups.
462 if (MBB.isEHFuncletEntry() && &MBB != MF->begin())
463 break;
464
465 for (InvokeRange &I : invoke_ranges(FuncInfo, MBB)) {
466 // If this invoke is in the same state as the last invoke and there were
467 // no non-throwing calls between it, extend the range to include both
468 // and continue.
469 if (!I.SawPotentiallyThrowing && I.State == LastEHState) {
470 LastEndLabel = I.EndLabel;
471 continue;
472 }
473
474 // If this invoke ends a previous one, emit all the actions for this
475 // state.
476 if (LastEHState != -1)
477 emitSEHActionsForRange(FuncInfo, LastBeginLabel, LastEndLabel,
478 LastEHState);
479
480 LastBeginLabel = I.BeginLabel;
428
429 // Remember what state we were in the last time we found a begin try label.
430 // This allows us to coalesce many nearby invokes with the same state into
431 // one entry.
432 int LastEHState = -1;
433 MCSymbol *LastBeginLabel = nullptr;
434 MCSymbol *LastEndLabel = nullptr;
435
436 // Use the assembler to compute the number of table entries through label
437 // difference and division.
438 MCSymbol *TableBegin =
439 Ctx.createTempSymbol("lsda_begin", /*AlwaysAddSuffix=*/true);
440 MCSymbol *TableEnd =
441 Ctx.createTempSymbol("lsda_end", /*AlwaysAddSuffix=*/true);
442 const MCExpr *LabelDiff =
443 MCBinaryExpr::createSub(MCSymbolRefExpr::create(TableEnd, Ctx),
444 MCSymbolRefExpr::create(TableBegin, Ctx), Ctx);
445 const MCExpr *EntrySize = MCConstantExpr::create(16, Ctx);
446 const MCExpr *EntryCount = MCBinaryExpr::createDiv(LabelDiff, EntrySize, Ctx);
447 OS.EmitValue(EntryCount, 4);
448
449 OS.EmitLabel(TableBegin);
450
451 // Iterate over all the invoke try ranges. Unlike MSVC, LLVM currently only
452 // models exceptions from invokes. LLVM also allows arbitrary reordering of
453 // the code, so our tables end up looking a bit different. Rather than
454 // trying to match MSVC's tables exactly, we emit a denormalized table. For
455 // each range of invokes in the same state, we emit table entries for all
456 // the actions that would be taken in that state. This means our tables are
457 // slightly bigger, which is OK.
458 for (const auto &MBB : *MF) {
459 // Break out before we enter into a finally funclet.
460 // FIXME: We need to emit separate EH tables for cleanups.
461 if (MBB.isEHFuncletEntry() && &MBB != MF->begin())
462 break;
463
464 for (InvokeRange &I : invoke_ranges(FuncInfo, MBB)) {
465 // If this invoke is in the same state as the last invoke and there were
466 // no non-throwing calls between it, extend the range to include both
467 // and continue.
468 if (!I.SawPotentiallyThrowing && I.State == LastEHState) {
481469 LastEndLabel = I.EndLabel;
482 LastEHState = I.State;
470 continue;
483471 }
472
473 // If this invoke ends a previous one, emit all the actions for this
474 // state.
475 if (LastEHState != -1)
476 emitSEHActionsForRange(FuncInfo, LastBeginLabel, LastEndLabel,
477 LastEHState);
478
479 LastBeginLabel = I.BeginLabel;
480 LastEndLabel = I.EndLabel;
481 LastEHState = I.State;
484482 }
485
486 if (LastEndLabel)
487 emitSEHActionsForRange(FuncInfo, LastBeginLabel, LastEndLabel,
488 LastEHState);
489
490 OS.EmitLabel(TableEnd);
491 return;
492 }
493
494 // Simplifying assumptions for first implementation:
495 // - Cleanups are not implemented.
496 // - Filters are not implemented.
497
498 // The Itanium LSDA table sorts similar landing pads together to simplify the
499 // actions table, but we don't need that.
500 const std::vector &PadInfos = MMI->getLandingPads();
501 SmallVector LandingPads;
502 LandingPads.reserve(PadInfos.size());
503 for (const auto &LP : PadInfos)
504 LandingPads.push_back(&LP);
505
506 // Compute label ranges for call sites as we would for the Itanium LSDA, but
507 // use an all zero action table because we aren't using these actions.
508 SmallVector FirstActions;
509 FirstActions.resize(LandingPads.size());
510 SmallVector CallSites;
511 computeCallSiteTable(CallSites, LandingPads, FirstActions);
512
513 MCSymbol *EHFuncBeginSym = Asm->getFunctionBegin();
514 MCSymbol *EHFuncEndSym = Asm->getFunctionEnd();
515
516 // Emit the number of table entries.
517 unsigned NumEntries = 0;
518 for (const CallSiteEntry &CSE : CallSites) {
519 if (!CSE.LPad)
520 continue; // Ignore gaps.
521 NumEntries += CSE.LPad->SEHHandlers.size();
522 }
523 OS.EmitIntValue(NumEntries, 4);
524
525 // If there are no actions, we don't need to iterate again.
526 if (NumEntries == 0)
527 return;
528
529 // Emit the four-label records for each call site entry. The table has to be
530 // sorted in layout order, and the call sites should already be sorted.
531 for (const CallSiteEntry &CSE : CallSites) {
532 // Ignore gaps. Unlike the Itanium model, unwinding through a frame without
533 // an EH table entry will propagate the exception rather than terminating
534 // the program.
535 if (!CSE.LPad)
536 continue;
537 const LandingPadInfo *LPad = CSE.LPad;
538
539 // Compute the label range. We may reuse the function begin and end labels
540 // rather than forming new ones.
541 const MCExpr *Begin =
542 create32bitRef(CSE.BeginLabel ? CSE.BeginLabel : EHFuncBeginSym);
543 const MCExpr *End;
544 if (CSE.EndLabel) {
545 // The interval is half-open, so we have to add one to include the return
546 // address of the last invoke in the range.
547 End = getLabelPlusOne(CSE.EndLabel);
548 } else {
549 End = create32bitRef(EHFuncEndSym);
550 }
551
552 // Emit an entry for each action.
553 for (SEHHandler Handler : LPad->SEHHandlers) {
554 OS.EmitValue(Begin, 4);
555 OS.EmitValue(End, 4);
556
557 // Emit the filter or finally function pointer, if present. Otherwise,
558 // emit '1' to indicate a catch-all.
559 const Function *F = Handler.FilterOrFinally;
560 if (F)
561 OS.EmitValue(create32bitRef(Asm->getSymbol(F)), 4);
562 else
563 OS.EmitIntValue(1, 4);
564
565 // Emit the recovery address, if present. Otherwise, this must be a
566 // finally.
567 const BlockAddress *BA = Handler.RecoverBA;
568 if (BA)
569 OS.EmitValue(
570 create32bitRef(Asm->GetBlockAddressSymbol(BA)), 4);
571 else
572 OS.EmitIntValue(0, 4);
573 }
574 }
483 }
484
485 // Hitting the end of the function causes us to emit the range for the
486 // previous invoke.
487 if (LastEndLabel)
488 emitSEHActionsForRange(FuncInfo, LastBeginLabel, LastEndLabel, LastEHState);
489
490 OS.EmitLabel(TableEnd);
575491 }
576492
577493 void WinException::emitSEHActionsForRange(WinEHFuncInfo &FuncInfo,
582498
583499 assert(BeginLabel && EndLabel);
584500 while (State != -1) {
585 // struct Entry {
586 // imagerel32 LabelStart;
587 // imagerel32 LabelEnd;
588 // imagerel32 FilterOrFinally; // One means catch-all.
589 // imagerel32 ExceptOrNull; // Zero means __finally.
590 // };
591501 SEHUnwindMapEntry &UME = FuncInfo.SEHUnwindMap[State];
592502 const MCExpr *FilterOrFinally;
593503 const MCExpr *ExceptOrNull;
640550 MCSymbol *UnwindMapXData = nullptr;
641551 MCSymbol *TryBlockMapXData = nullptr;
642552 MCSymbol *IPToStateXData = nullptr;
643 if (!FuncInfo.UnwindMap.empty())
553 if (!FuncInfo.CxxUnwindMap.empty())
644554 UnwindMapXData = Asm->OutContext.getOrCreateSymbol(
645555 Twine("$stateUnwindMap$", FuncLinkageName));
646556 if (!FuncInfo.TryBlockMap.empty())
668578 OS.EmitValueToAlignment(4);
669579 OS.EmitLabel(FuncInfoXData);
670580 OS.EmitIntValue(0x19930522, 4); // MagicNumber
671 OS.EmitIntValue(FuncInfo.UnwindMap.size(), 4); // MaxState
581 OS.EmitIntValue(FuncInfo.CxxUnwindMap.size(), 4); // MaxState
672582 OS.EmitValue(create32bitRef(UnwindMapXData), 4); // UnwindMap
673583 OS.EmitIntValue(FuncInfo.TryBlockMap.size(), 4); // NumTryBlocks
674584 OS.EmitValue(create32bitRef(TryBlockMapXData), 4); // TryBlockMap
685595 // };
686596 if (UnwindMapXData) {
687597 OS.EmitLabel(UnwindMapXData);
688 for (const WinEHUnwindMapEntry &UME : FuncInfo.UnwindMap) {
598 for (const CxxUnwindMapEntry &UME : FuncInfo.CxxUnwindMap) {
689599 MCSymbol *CleanupSym = getMCSymbolForMBBOrGV(Asm, UME.Cleanup);
690600 OS.EmitIntValue(UME.ToState, 4); // ToState
691601 OS.EmitValue(create32bitRef(CleanupSym), 4); // Action
718628 assert(0 <= TBME.TryLow && "bad trymap interval");
719629 assert(TBME.TryLow <= TBME.TryHigh && "bad trymap interval");
720630 assert(TBME.TryHigh < TBME.CatchHigh && "bad trymap interval");
721 assert(TBME.CatchHigh < int(FuncInfo.UnwindMap.size()) &&
631 assert(TBME.CatchHigh < int(FuncInfo.CxxUnwindMap.size()) &&
722632 "bad trymap interval");
723633
724634 OS.EmitIntValue(TBME.TryLow, 4); // TryLow
902812 BaseState = -2;
903813 }
904814
905 if (!FuncInfo.SEHUnwindMap.empty()) {
906 for (SEHUnwindMapEntry &UME : FuncInfo.SEHUnwindMap) {
907 MCSymbol *ExceptOrFinally =
908 UME.Handler.get()->getSymbol();
909 // -1 is usually the base state for "unwind to caller", but for
910 // _except_handler4 it's -2. Do that replacement here if necessary.
911 int ToState = UME.ToState == -1 ? BaseState : UME.ToState;
912 OS.EmitIntValue(ToState, 4); // ToState
913 OS.EmitValue(create32bitRef(UME.Filter), 4); // Filter
914 OS.EmitValue(create32bitRef(ExceptOrFinally), 4); // Except/Finally
915 }
916 return;
917 }
918 // FIXME: The following code is for the old landingpad-based SEH
919 // implementation. Remove it when possible.
920
921 // Build a list of pointers to LandingPadInfos and then sort by WinEHState.
922 const std::vector &PadInfos = MMI->getLandingPads();
923 SmallVector LPads;
924 LPads.reserve((PadInfos.size()));
925 for (const LandingPadInfo &LPInfo : PadInfos)
926 LPads.push_back(&LPInfo);
927 std::sort(LPads.begin(), LPads.end(),
928 [](const LandingPadInfo *L, const LandingPadInfo *R) {
929 return L->WinEHState < R->WinEHState;
930 });
931
932 // For each action in each lpad, emit one of these:
933 // struct ScopeTableEntry {
934 // int32_t EnclosingLevel;
935 // int32_t (__cdecl *Filter)();
936 // void *HandlerOrFinally;
937 // };
938 //
939 // The "outermost" action will use BaseState as its enclosing level. Each
940 // other action will refer to the previous state as its enclosing level.
941 int CurState = 0;
942 for (const LandingPadInfo *LPInfo : LPads) {
943 int EnclosingLevel = BaseState;
944 assert(CurState + int(LPInfo->SEHHandlers.size()) - 1 ==
945 LPInfo->WinEHState &&
946 "gaps in the SEH scope table");
947 for (auto I = LPInfo->SEHHandlers.rbegin(), E = LPInfo->SEHHandlers.rend();
948 I != E; ++I) {
949 const SEHHandler &Handler = *I;
950 const BlockAddress *BA = Handler.RecoverBA;
951 const Function *F = Handler.FilterOrFinally;
952 assert(F && "cannot catch all in 32-bit SEH without filter function");
953 const MCExpr *FilterOrNull =
954 create32bitRef(BA ? Asm->getSymbol(F) : nullptr);
955 const MCExpr *ExceptOrFinally = create32bitRef(
956 BA ? Asm->GetBlockAddressSymbol(BA) : Asm->getSymbol(F));
957
958 OS.EmitIntValue(EnclosingLevel, 4);
959 OS.EmitValue(FilterOrNull, 4);
960 OS.EmitValue(ExceptOrFinally, 4);
961
962 // The next state unwinds to this state.
963 EnclosingLevel = CurState;
964 CurState++;
965 }
966 }
967 }
815 assert(!FuncInfo.SEHUnwindMap.empty());
816 for (SEHUnwindMapEntry &UME : FuncInfo.SEHUnwindMap) {
817 MCSymbol *ExceptOrFinally =
818 UME.Handler.get()->getSymbol();
819 // -1 is usually the base state for "unwind to caller", but for
820 // _except_handler4 it's -2. Do that replacement here if necessary.
821 int ToState = UME.ToState == -1 ? BaseState : UME.ToState;
822 OS.EmitIntValue(ToState, 4); // ToState
823 OS.EmitValue(create32bitRef(UME.Filter), 4); // Filter
824 OS.EmitValue(create32bitRef(ExceptOrFinally), 4); // Except/Finally
825 }
826 }
11021102 // The donothing intrinsic does, well, nothing.
11031103 case Intrinsic::donothing:
11041104 return true;
1105 case Intrinsic::eh_actions: {
1106 unsigned ResultReg = getRegForValue(UndefValue::get(II->getType()));
1107 if (!ResultReg)
1108 return false;
1109 updateValueMap(II, ResultReg);
1110 return true;
1111 }
11121105 case Intrinsic::dbg_declare: {
11131106 const DbgDeclareInst *DI = cast(II);
11141107 assert(DI->getVariable() && "Missing variable");
282282 if (!isFuncletEHPersonality(Personality))
283283 return;
284284
285 if (Personality == EHPersonality::MSVC_Win64SEH ||
286 Personality == EHPersonality::MSVC_X86SEH) {
287 addSEHHandlersForLPads(LPads);
288 }
289
290285 // Calculate state numbers if we haven't already.
291286 WinEHFuncInfo &EHInfo = MMI.getWinEHFuncInfo(&fn);
292287 const Function *WinEHParentFn = MMI.getWinEHParent(&fn);
312307 H.Handler = MBBMap[BB];
313308 }
314309 }
315 for (WinEHUnwindMapEntry &UME : EHInfo.UnwindMap)
310 for (CxxUnwindMapEntry &UME : EHInfo.CxxUnwindMap)
316311 if (UME.Cleanup)
317312 if (const auto *BB = dyn_cast(UME.Cleanup.get()))
318313 UME.Cleanup = MBBMap[BB];
340335 for (const LandingPadInst *LP : LPads) {
341336 MachineBasicBlock *LPadMBB = MBBMap[LP->getParent()];
342337 MMI.addWinEHState(LPadMBB, EHInfo.EHPadStateMap[LP]);
343 }
344 }
345 }
346
347 void FunctionLoweringInfo::addSEHHandlersForLPads(
348 ArrayRef LPads) {
349 MachineModuleInfo &MMI = MF->getMMI();
350
351 // Iterate over all landing pads with llvm.eh.actions calls.
352 for (const LandingPadInst *LP : LPads) {
353 const IntrinsicInst *ActionsCall =
354 dyn_cast(LP->getNextNode());
355 if (!ActionsCall ||
356 ActionsCall->getIntrinsicID() != Intrinsic::eh_actions)
357 continue;
358
359 // Parse the llvm.eh.actions call we found.
360 MachineBasicBlock *LPadMBB = MBBMap[LP->getParent()];
361 SmallVector, 4> Actions;
362 parseEHActions(ActionsCall, Actions);
363
364 // Iterate EH actions from most to least precedence, which means
365 // iterating in reverse.
366 for (auto I = Actions.rbegin(), E = Actions.rend(); I != E; ++I) {
367 ActionHandler *Action = I->get();
368 if (auto *CH = dyn_cast(Action)) {
369 const auto *Filter =
370 dyn_cast(CH->getSelector()->stripPointerCasts());
371 assert((Filter || CH->getSelector()->isNullValue()) &&
372 "expected function or catch-all");
373 const auto *RecoverBA =
374 cast(CH->getHandlerBlockOrFunc());
375 MMI.addSEHCatchHandler(LPadMBB, Filter, RecoverBA);
376 } else {
377 assert(isa(Action));
378 const auto *Fini = cast(Action->getHandlerBlockOrFunc());
379 MMI.addSEHCleanupHandler(LPadMBB, Fini);
380 }
381338 }
382339 }
383340 }
51515151 }
51525152 case Intrinsic::clear_cache:
51535153 return TLI.getClearCacheBuiltinName();
5154 case Intrinsic::eh_actions:
5155 setValue(&I, DAG.getUNDEF(TLI.getPointerTy(DAG.getDataLayout())));
5156 return nullptr;
51575154 case Intrinsic::donothing:
51585155 // ignore
51595156 return nullptr;
52355232 SDValue Add = DAG.getNode(ISD::ADD, sdl, PtrVT, FPVal, OffsetVal);
52365233 setValue(&I, Add);
52375234
5238 return nullptr;
5239 }
5240 case Intrinsic::eh_begincatch:
5241 case Intrinsic::eh_endcatch:
5242 llvm_unreachable("begin/end catch intrinsics not lowered in codegen");
5243 case Intrinsic::eh_exceptioncode_old: {
5244 unsigned Reg = TLI.getExceptionPointerRegister();
5245 assert(Reg && "cannot get exception code on this platform");
5246 MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
5247 const TargetRegisterClass *PtrRC = TLI.getRegClassFor(PtrVT);
5248 assert(FuncInfo.MBB->isEHPad() && "eh.exceptioncode in non-lpad");
5249 unsigned VReg = FuncInfo.MBB->addLiveIn(Reg, PtrRC);
5250 SDValue N =
5251 DAG.getCopyFromReg(DAG.getEntryNode(), getCurSDLoc(), VReg, PtrVT);
5252 N = DAG.getZExtOrTrunc(N, getCurSDLoc(), MVT::i32);
5253 setValue(&I, N);
52545235 return nullptr;
52555236 }
52565237
971971 const MCInstrDesc &II = TII->get(TargetOpcode::EH_LABEL);
972972 BuildMI(*MBB, FuncInfo->InsertPt, SDB->getCurDebugLoc(), II)
973973 .addSym(Label);
974
975 // If this personality function uses funclets, we need to split the landing
976 // pad into several BBs.
977 const Constant *Personality = MF->getFunction()->getPersonalityFn();
978 if (const auto *PF = dyn_cast(Personality->stripPointerCasts()))
979 MF->getMMI().addPersonality(PF);
980 EHPersonality PersonalityType = classifyEHPersonality(Personality);
981
982 if (isFuncletEHPersonality(PersonalityType)) {
983 SmallVector ClauseBBs;
984 const IntrinsicInst *ActionsCall =
985 dyn_cast(LLVMBB->getFirstInsertionPt());
986 // Get all invoke BBs that unwind to this landingpad.
987 SmallVector InvokeBBs(MBB->pred_begin(),
988 MBB->pred_end());
989 if (ActionsCall && ActionsCall->getIntrinsicID() == Intrinsic::eh_actions) {
990 // If this is a call to llvm.eh.actions followed by indirectbr, then we've
991 // run WinEHPrepare, and we should remove this block from the machine CFG.
992 // Mark the targets of the indirectbr as landingpads instead.
993 for (const BasicBlock *LLVMSucc : successors(LLVMBB)) {
994 MachineBasicBlock *ClauseBB = FuncInfo->MBBMap[LLVMSucc];
995 // Add the edge from the invoke to the clause.
996 for (MachineBasicBlock *InvokeBB : InvokeBBs)
997 InvokeBB->addSuccessor(ClauseBB);
998
999 // Mark the clause as a landing pad or MI passes will delete it.
1000 ClauseBB->setIsEHPad();
1001 }
1002 }
1003
1004 // Remove the edge from the invoke to the lpad.
1005 for (MachineBasicBlock *InvokeBB : InvokeBBs)
1006 InvokeBB->removeSuccessor(MBB);
1007
1008 // Don't select instructions for the landingpad.
1009 return false;
1010 }
1011974
1012975 // Mark exception register as live in.
1013976 if (unsigned Reg = TLI->getExceptionPointerRegister())
6262
6363 namespace {
6464
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> FrameVarInfoMap;
71
72 // TinyPtrVector cannot hold nullptr, so we need our own sentinel that isn't
73 // quite null.
74 AllocaInst *getCatchObjectSentinel() {
75 return static_cast(nullptr) + 1;
76 }
77
78 typedef SmallSet VisitedBlockSet;
79
80 class LandingPadActions;
81 class LandingPadMap;
82
83 typedef DenseMap CatchHandlerMapTy;
84 typedef DenseMap CleanupHandlerMapTy;
85
8665 class WinEHPrepare : public FunctionPass {
8766 public:
8867 static char ID; // Pass identification, replacement for typeid.
10382 }
10483
10584 private:
106 bool prepareExceptionHandlers(Function &F,
107 SmallVectorImpl &LPads);
108 void identifyEHBlocks(Function &F, SmallVectorImpl &LPads);
109 void promoteLandingPadValues(LandingPadInst *LPad);
110 void demoteValuesLiveAcrossHandlers(Function &F,
111 SmallVectorImpl &LPads);
112 void findSEHEHReturnPoints(Function &F,
113 SetVector &EHReturnBlocks);
114 void findCXXEHReturnPoints(Function &F,
115 SetVector &EHReturnBlocks);
116 void getPossibleReturnTargets(Function *ParentF, Function *HandlerF,
117 SetVector &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);
128
129 void mapLandingPadBlocks(LandingPadInst *LPad, LandingPadActions &Actions);
130 CatchHandler *findCatchHandler(BasicBlock *BB, BasicBlock *&NextBB,
131 VisitedBlockSet &VisitedBlocks);
132 void findCleanupHandlers(LandingPadActions &Actions, BasicBlock *StartBB,
133 BasicBlock *EndBB);
134
135 void processSEHCatchHandler(CatchHandler *Handler, BasicBlock *StartBB);
13685 void insertPHIStores(PHINode *OriginalPHI, AllocaInst *SpillSlot);
13786 void
13887 insertPHIStore(BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot,
158107 Triple TheTriple;
159108
160109 // All fields are reset by runOnFunction.
161 DominatorTree *DT = nullptr;
162 const TargetLibraryInfo *LibInfo = nullptr;
163110 EHPersonality Personality = EHPersonality::Unknown;
164 CatchHandlerMapTy CatchHandlerMap;
165 CleanupHandlerMapTy CleanupHandlerMap;
166 DenseMap LPadMaps;
167 SmallPtrSet NormalBlocks;
168 SmallPtrSet EHBlocks;
169 SetVector EHReturnBlocks;
170
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 NestedLPtoOriginalLP;
178
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 LPadTargetBlocks;
191
192 // Map from outlined handler to call to parent local address. Only used for
193 // 32-bit EH.
194 DenseMap HandlerToParentFP;
195
196 AllocaInst *SEHExceptionCodeSlot = nullptr;
197111
198112 std::map> BlockColors;
199113 std::map> FuncletBlocks;
200114 std::map> FuncletChildren;
201115 };
202116
203 class WinEHFrameVariableMaterializer : public ValueMaterializer {
204 public:
205 WinEHFrameVariableMaterializer(Function *OutlinedFn, Value *ParentFP,
206 FrameVarInfoMap &FrameVarInfo);
207 ~WinEHFrameVariableMaterializer() override {}
208
209 Value *materializeValueFor(Value *V) override;
210
211 void escapeCatchObject(Value *V);
212
213 private:
214 FrameVarInfoMap &FrameVarInfo;
215 IRBuilder<> Builder;
216 };
217
218 class LandingPadMap {
219 public:
220 LandingPadMap() : OriginLPad(nullptr) {}
221 void mapLandingPad(const LandingPadInst *LPad);
222
223 bool isInitialized() { return OriginLPad != nullptr; }
224
225 bool isOriginLandingPadBlock(const BasicBlock *BB) const;
226 bool isLandingPadSpecificInst(const Instruction *Inst) const;
227
228 void remapEHValues(ValueToValueMapTy &VMap, Value *EHPtrValue,
229 Value *SelectorValue) const;
230
231 private:
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 ExtractedEHPtrs;
236 TinyPtrVector ExtractedSelectors;
237 };
238
239 class WinEHCloningDirectorBase : public CloningDirector {
240 public:
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) {}
247
248 CloningAction handleInstruction(ValueToValueMapTy &VMap,
249 const Instruction *Inst,
250 BasicBlock *NewBB) override;
251
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;
276
277 ValueMaterializer *getValueMaterializer() override { return &Materializer; }
278
279 protected:
280 WinEHFrameVariableMaterializer Materializer;
281 Type *SelectorIDType;
282 Type *Int8PtrType;
283 LandingPadMap &LPadMap;
284
285 /// The value representing the parent frame pointer.
286 Value *ParentFP;
287 };
288
289 class WinEHCatchDirector : public WinEHCloningDirectorBase {
290 public:
291 WinEHCatchDirector(
292 Function *CatchFn, Value *ParentFP, Value *Selector,
293 FrameVarInfoMap &VarInfo, LandingPadMap &LPadMap,
294 DenseMap &NestedLPads,
295 DominatorTree *DT, SmallPtrSetImpl &EHBlocks)
296 : WinEHCloningDirectorBase(CatchFn, ParentFP, VarInfo, LPadMap),
297 CurrentSelector(Selector->stripPointerCasts()),
298 ExceptionObjectVar(nullptr), NestedLPtoOriginalLP(NestedLPads),
299 DT(DT), EHBlocks(EHBlocks) {}
300
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;
321
322 Value *getExceptionVar() { return ExceptionObjectVar; }
323 TinyPtrVector &getReturnTargets() { return ReturnTargets; }
324
325 private:
326 Value *CurrentSelector;
327
328 Value *ExceptionObjectVar;
329 TinyPtrVector ReturnTargets;
330
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 &NestedLPtoOriginalLP;
334 DominatorTree *DT;
335 SmallPtrSetImpl &EHBlocks;
336 };
337
338 class WinEHCleanupDirector : public WinEHCloningDirectorBase {
339 public:
340 WinEHCleanupDirector(Function *CleanupFn, Value *ParentFP,
341 FrameVarInfoMap &VarInfo, LandingPadMap &LPadMap)
342 : WinEHCloningDirectorBase(CleanupFn, ParentFP, VarInfo,
343 LPadMap) {}
344
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;
365 };
366
367 class LandingPadActions {
368 public:
369 LandingPadActions() : HasCleanupHandlers(false) {}
370
371 void insertCatchHandler(CatchHandler *Action) { Actions.push_back(Action); }
372 void insertCleanupHandler(CleanupHandler *Action) {
373 Actions.push_back(Action);
374 HasCleanupHandlers = true;
375 }
376
377 bool includesCleanup() const { return HasCleanupHandlers; }
378
379 SmallVectorImpl &actions() { return Actions; }
380 SmallVectorImpl::iterator begin() { return Actions.begin(); }
381 SmallVectorImpl::iterator end() { return Actions.end(); }
382
383 private:
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 Actions;
388 bool HasCleanupHandlers;
389 };
390
391117 } // end anonymous namespace
392118
393119 char WinEHPrepare::ID = 0;
398124 return new WinEHPrepare(TM);
399125 }
400126
401 static bool
402 findExceptionalConstructs(Function &Fn,
403 SmallVectorImpl &LPads,
404 SmallVectorImpl &Resumes,
405 SmallVectorImpl &EntryBlocks) {
406 bool ForExplicitEH = false;
127 static void findFuncletEntryPoints(Function &Fn,
128 SmallVectorImpl &EntryBlocks) {
129 EntryBlocks.push_back(&Fn.getEntryBlock());
407130 for (BasicBlock &BB : Fn) {
408131 Instruction *First = BB.getFirstNonPHI();
409 if (auto *LP = dyn_cast(First)) {
410 LPads.push_back(LP);
411 } else if (First->isEHPad()) {
412 if (!ForExplicitEH)
413 EntryBlocks.push_back(&Fn.getEntryBlock());
414 if (!isa(First) && !isa(First))
415 EntryBlocks.push_back(&BB);
416 ForExplicitEH = true;
417 }
418 if (auto *Resume = dyn_cast(BB.getTerminator()))
419 Resumes.push_back(Resume);
420 }
421 return ForExplicitEH;
132 if (!First->isEHPad())
133 continue;
134 assert(!isa(First) &&
135 "landingpad cannot be used with funclet EH personality");
136 // Find EH pad blocks that represent funclet start points.
137 if (!isa(First) && !isa(First))
138 EntryBlocks.push_back(&BB);
139 }
422140 }
423141
424142 bool WinEHPrepare::runOnFunction(Function &Fn) {
441159 // not.
442160 removeUnreachableBlocks(Fn);
443161
444 SmallVector LPads;
445 SmallVector Resumes;
446162 SmallVector EntryBlocks;
447 bool ForExplicitEH =
448 findExceptionalConstructs(Fn, LPads, Resumes, EntryBlocks);
449
450 if (ForExplicitEH)
451 return prepareExplicitEH(Fn, EntryBlocks);
452
453 // No need to prepare functions that lack landing pads.
454 if (LPads.empty())
455 return false;
456
457 DT = &getAnalysis().getDomTree();
458 LibInfo = &getAnalysis().getTLI();
459
460 // If there were any landing pads, prepareExceptionHandlers will make changes.
461 prepareExceptionHandlers(Fn, LPads);
462 return true;
163 findFuncletEntryPoints(Fn, EntryBlocks);
164 return prepareExplicitEH(Fn, EntryBlocks);
463165 }
464166
465167 bool WinEHPrepare::doFinalization(Module &M) { return false; }
469171 AU.addRequired();
470172 }
471173
472 static bool isSelectorDispatch(BasicBlock *BB, BasicBlock *&CatchHandler,
473 Constant *&Selector, BasicBlock *&NextBB);
474
475 // Finds blocks reachable from the starting set Worklist. Does not follow unwind
476 // edges or blocks listed in StopPoints.
477 static void findReachableBlocks(SmallPtrSetImpl &ReachableBBs,
478 SetVector &Worklist,
479 const SetVector *StopPoints) {
480 while (!Worklist.empty()) {
481 BasicBlock *BB = Worklist.pop_back_val();
482
483 // Don't cross blocks that we should stop at.
484 if (StopPoints && StopPoints->count(BB))
485 continue;
486
487 if (!ReachableBBs.insert(BB).second)
488 continue; // Already visited.
489
490 // Don't follow unwind edges of invokes.
491 if (auto *II = dyn_cast(BB->getTerminator())) {
492 Worklist.insert(II->getNormalDest());
493 continue;
494 }
495
496 // Otherwise, follow all successors.
497 Worklist.insert(succ_begin(BB), succ_end(BB));
498 }
499 }
500
501 // Attempt to find an instruction where a block can be split before
502 // a call to llvm.eh.begincatch and its operands. If the block
503 // begins with the begincatch call or one of its adjacent operands
504 // the block will not be split.
505 static Instruction *findBeginCatchSplitPoint(BasicBlock *BB,
506 IntrinsicInst *II) {
507 // If the begincatch call is already the first instruction in the block,
508 // don't split.
509 Instruction *FirstNonPHI = BB->getFirstNonPHI();
510 if (II == FirstNonPHI)
511 return nullptr;
512
513 // If either operand is in the same basic block as the instruction and
514 // isn't used by another instruction before the begincatch call, include it
515 // in the split block.
516 auto *Op0 = dyn_cast(II->getOperand(0));
517 auto *Op1 = dyn_cast(II->getOperand(1));
518
519 Instruction *I = II->getPrevNode();
520 Instruction *LastI = II;
521
522 while (I == Op0 || I == Op1) {
523 // If the block begins with one of the operands and there are no other
524 // instructions between the operand and the begincatch call, don't split.
525 if (I == FirstNonPHI)
526 return nullptr;
527
528 LastI = I;
529 I = I->getPrevNode();
530 }
531
532 // If there is at least one instruction in the block before the begincatch
533 // call and its operands, split the block at either the begincatch or
534 // its operand.
535 return LastI;
536 }
537
538 /// Find all points where exceptional control rejoins normal control flow via
539 /// llvm.eh.endcatch. Add them to the normal bb reachability worklist.
540 void WinEHPrepare::findCXXEHReturnPoints(
541 Function &F, SetVector &EHReturnBlocks) {
542 for (auto BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI) {
543 BasicBlock *BB = &*BBI;
544 for (Instruction &I : *BB) {
545 if (match(&I, m_Intrinsic())) {
546 Instruction *SplitPt =
547 findBeginCatchSplitPoint(BB, cast(&I));
548 if (SplitPt) {
549 // Split the block before the llvm.eh.begincatch call to allow
550 // cleanup and catch code to be distinguished later.
551 // Do not update BBI because we still need to process the
552 // portion of the block that we are splitting off.
553 SplitBlock(BB, SplitPt, DT);
554 break;
555 }
556 }
557 if (match(&I, m_Intrinsic())) {
558 // Split the block after the call to llvm.eh.endcatch if there is
559 // anything other than an unconditional branch, or if the successor
560 // starts with a phi.
561 auto *Br = dyn_cast(I.getNextNode());
562 if (!Br || !Br->isUnconditional() ||
563 isa(Br->getSuccessor(0)->begin())) {
564 DEBUG(dbgs() << "splitting block " << BB->getName()
565 << " with llvm.eh.endcatch\n");
566 BBI = SplitBlock(BB, I.getNextNode(), DT)->getIterator();
567 }
568 // The next BB is normal control flow.
569 EHReturnBlocks.insert(BB->getTerminator()->getSuccessor(0));
570 break;
571 }
572 }
573 }
574 }
575
576 static bool isCatchAllLandingPad(const BasicBlock *BB) {
577 const LandingPadInst *LP = BB->getLandingPadInst();
578 if (!LP)
579 return false;
580 unsigned N = LP->getNumClauses();
581 return (N > 0 && LP->isCatch(N - 1) &&
582 isa(LP->getClause(N - 1)));
583 }
584
585 /// Find all points where exceptions control rejoins normal control flow via
586 /// selector dispatch.
587 void WinEHPrepare::findSEHEHReturnPoints(
588 Function &F, SetVector &EHReturnBlocks) {
589 for (auto BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI) {
590 BasicBlock *BB = &*BBI;
591 // If the landingpad is a catch-all, treat the whole lpad as if it is
592 // reachable from normal control flow.
593 // FIXME: This is imprecise. We need a better way of identifying where a
594 // catch-all starts and cleanups stop. As far as LLVM is concerned, there
595 // is no difference.
596 if (isCatchAllLandingPad(BB)) {
597 EHReturnBlocks.insert(BB);
598 continue;
599 }
600
601 BasicBlock *CatchHandler;
602 BasicBlock *NextBB;
603 Constant *Selector;
604 if (isSelectorDispatch(BB, CatchHandler, Selector, NextBB)) {
605 // Split the edge if there are multiple predecessors. This creates a place
606 // where we can insert EH recovery code.
607 if (!CatchHandler->getSinglePredecessor()) {
608 DEBUG(dbgs() << "splitting EH return edge from " << BB->getName()
609 << " to " << CatchHandler->getName() << '\n');
610 CatchHandler = SplitCriticalEdge(
611 BB, std::find(succ_begin(BB), succ_end(BB), CatchHandler));
612 BBI = CatchHandler->getIterator();
613 }
614 EHReturnBlocks.insert(CatchHandler);
615 }
616 }
617 }
618
619 void WinEHPrepare::identifyEHBlocks(Function &F,
620 SmallVectorImpl &LPads) {
621 DEBUG(dbgs() << "Demoting values live across exception handlers in function "
622 << F.getName() << '\n');
623
624 // Build a set of all non-exceptional blocks and exceptional blocks.
625 // - Non-exceptional blocks are blocks reachable from the entry block while
626 // not following invoke unwind edges.
627 // - Exceptional blocks are blocks reachable from landingpads. Analysis does
628 // not follow llvm.eh.endcatch blocks, which mark a transition from
629 // exceptional to normal control.
630
631 if (Personality == EHPersonality::MSVC_CXX)
632 findCXXEHReturnPoints(F, EHReturnBlocks);
633 else
634 findSEHEHReturnPoints(F, EHReturnBlocks);
635
636 DEBUG({
637 dbgs() << "identified the following blocks as EH return points:\n";
638 for (BasicBlock *BB : EHReturnBlocks)
639 dbgs() << " " << BB->getName() << '\n';
640 });
641
642 // Join points should not have phis at this point, unless they are a
643 // landingpad, in which case we will demote their phis later.
644 #ifndef NDEBUG
645 for (BasicBlock *BB : EHReturnBlocks)
646 assert((BB->isLandingPad() || !isa(BB->begin())) &&
647 "non-lpad EH return block has phi");
648 #endif
649
650 // Normal blocks are the blocks reachable from the entry block and all EH
651 // return points.
652 SetVector Worklist;
653 Worklist = EHReturnBlocks;
654 Worklist.insert(&F.getEntryBlock());
655 findReachableBlocks(NormalBlocks, Worklist, nullptr);
656 DEBUG({
657 dbgs() << "marked the following blocks as normal:\n";
658 for (BasicBlock *BB : NormalBlocks)
659 dbgs() << " " << BB->getName() << '\n';
660 });
661
662 // Exceptional blocks are the blocks reachable from landingpads that don't
663 // cross EH return points.
664 Worklist.clear();
665 for (auto *LPI : LPads)
666 Worklist.insert(LPI->getParent());
667 findReachableBlocks(EHBlocks, Worklist, &EHReturnBlocks);
668 DEBUG({
669 dbgs() << "marked the following blocks as exceptional:\n";
670 for (BasicBlock *BB : EHBlocks)
671 dbgs() << " " << BB->getName() << '\n';
672 });
673
674 }
675
676 /// Ensure that all values live into and out of exception handlers are stored
677 /// in memory.
678 /// FIXME: This falls down when values are defined in one handler and live into
679 /// another handler. For example, a cleanup defines a value used only by a
680 /// catch handler.
681 void WinEHPrepare::demoteValuesLiveAcrossHandlers(
682 Function &F, SmallVectorImpl &LPads) {
683 DEBUG(dbgs() << "Demoting values live across exception handlers in function "
684 << F.getName() << '\n');
685
686 // identifyEHBlocks() should have been called before this function.
687 assert(!NormalBlocks.empty());
688
689 // Try to avoid demoting EH pointer and selector values. They get in the way
690 // of our pattern matching.
691 SmallPtrSet EHVals;
692 for (BasicBlock &BB : F) {
693 LandingPadInst *LP = BB.getLandingPadInst();
694 if (!LP)
695 continue;
696 EHVals.insert(LP);
697 for (User *U : LP->users()) {
698 auto *EI = dyn_cast(U);
699 if (!EI)
700 continue;
701 EHVals.insert(EI);
702 for (User *U2 : EI->users()) {
703 if (auto *PN = dyn_cast(U2))
704 EHVals.insert(PN);
705 }
706 }
707 }
708
709 SetVector ArgsToDemote;
710 SetVector InstrsToDemote;
711 for (BasicBlock &BB : F) {
712 bool IsNormalBB = NormalBlocks.count(&BB);
713 bool IsEHBB = EHBlocks.count(&BB);
714 if (!IsNormalBB && !IsEHBB)
715 continue; // Blocks that are neither normal nor EH are unreachable.
716 for (Instruction &I : BB) {
717 for (Value *Op : I.operands()) {
718 // Don't demote static allocas, constants, and labels.
719 if (isa(Op) || isa(Op) || isa(Op))
720 continue;
721 auto *AI = dyn_cast(Op);
722 if (AI && AI->isStaticAlloca())
723 continue;
724
725 if (auto *Arg = dyn_cast(Op)) {
726 if (IsEHBB) {
727 DEBUG(dbgs() << "Demoting argument " << *Arg
728 << " used by EH instr: " << I << "\n");
729 ArgsToDemote.insert(Arg);
730 }
731 continue;
732 }
733
734 // Don't demote EH values.
735 auto *OpI = cast(Op);
736 if (EHVals.count(OpI))
737 continue;
738
739 BasicBlock *OpBB = OpI->getParent();
740 // If a value is produced and consumed in the same BB, we don't need to
741 // demote it.
742 if (OpBB == &BB)
743 continue;
744 bool IsOpNormalBB = NormalBlocks.count(OpBB);
745 bool IsOpEHBB = EHBlocks.count(OpBB);
746 if (IsNormalBB != IsOpNormalBB || IsEHBB != IsOpEHBB) {
747 DEBUG({
748 dbgs() << "Demoting instruction live in-out from EH:\n";
749 dbgs() << "Instr: " << *OpI << '\n';
750 dbgs() << "User: " << I << '\n';
751 });
752 InstrsToDemote.insert(OpI);
753 }
754 }
755 }
756 }
757
758 // Demote values live into and out of handlers.
759 // FIXME: This demotion is inefficient. We should insert spills at the point
760 // of definition, insert one reload in each handler that uses the value, and
761 // insert reloads in the BB used to rejoin normal control flow.
762 Instruction *AllocaInsertPt = &*F.getEntryBlock().getFirstInsertionPt();
763 for (Instruction *I : InstrsToDemote)
764 DemoteRegToStack(*I, false, AllocaInsertPt);
765
766 // Demote arguments separately, and only for uses in EH blocks.
767 for (Argument *Arg : ArgsToDemote) {
768 auto *Slot = new AllocaInst(Arg->getType(), nullptr,
769 Arg->getName() + ".reg2mem", AllocaInsertPt);
770 SmallVector Users(Arg->user_begin(), Arg->user_end());
771 for (User *U : Users) {
772 auto *I = dyn_cast(U);
773 if (I && EHBlocks.count(I->getParent())) {
774 auto *Reload = new LoadInst(Slot, Arg->getName() + ".reload", false, I);
775 U->replaceUsesOfWith(Arg, Reload);
776 }
777 }
778 new StoreInst(Arg, Slot, AllocaInsertPt);
779 }
780
781 // Demote landingpad phis, as the landingpad will be removed from the machine
782 // CFG.
783 for (LandingPadInst *LPI : LPads) {
784 BasicBlock *BB = LPI->getParent();
785 while (auto *Phi = dyn_cast(BB->begin()))
786 DemotePHIToStack(Phi, AllocaInsertPt);
787 }
788
789 DEBUG(dbgs() << "Demoted " << InstrsToDemote.size() << " instructions and "
790 << ArgsToDemote.size() << " arguments for WinEHPrepare\n\n");
791 }
792
793 bool WinEHPrepare::prepareExceptionHandlers(
794 Function &F, SmallVectorImpl &LPads) {
795 // Don't run on functions that are already prepared.
796 for (LandingPadInst *LPad : LPads) {
797 BasicBlock *LPadBB = LPad->getParent();
798 for (Instruction &Inst : *LPadBB)
799 if (match(&Inst, m_Intrinsic()))
800 return false;
801 }
802
803 identifyEHBlocks(F, LPads);
804 demoteValuesLiveAcrossHandlers(F, LPads);
805
806 // These containers are used to re-map frame variables that are used in
807 // outlined catch and cleanup handlers. They will be populated as the
808 // handlers are outlined.
809 FrameVarInfoMap FrameVarInfo;
810
811 bool HandlersOutlined = false;
812
813 Module *M = F.getParent();
814 LLVMContext &Context = M->getContext();
815
816 // Create a new function to receive the handler contents.
817 PointerType *Int8PtrType = Type::getInt8PtrTy(Context);
818 Type *Int32Type = Type::getInt32Ty(Context);
819 Function *ActionIntrin = Intrinsic::getDeclaration(M, Intrinsic::eh_actions);
820
821 if (isAsynchronousEHPersonality(Personality)) {
822 // FIXME: Switch the ehptr type to i32 and then switch this.
823 SEHExceptionCodeSlot =
824 new AllocaInst(Int8PtrType, nullptr, "seh_exception_code",
825 &*F.getEntryBlock().getFirstInsertionPt());
826 }
827
828 // In order to handle the case where one outlined catch handler returns
829 // to a block within another outlined catch handler that would otherwise
830 // be unreachable, we need to outline the nested landing pad before we
831 // outline the landing pad which encloses it.
832 if (!isAsynchronousEHPersonality(Personality))
833 std::sort(LPads.begin(), LPads.end(),
834 [this](LandingPadInst *const &L, LandingPadInst *const &R) {
835 return DT->properlyDominates(R->getParent(), L->getParent());
836 });
837
838 // This container stores the llvm.eh.recover and IndirectBr instructions
839 // that make up the body of each landing pad after it has been outlined.
840 // We need to defer the population of the target list for the indirectbr
841 // until all landing pads have been outlined so that we can handle the
842 // case of blocks in the target that are reached only from nested
843 // landing pads.
844 SmallVector, 4> LPadImpls;
845
846 for (LandingPadInst *LPad : LPads) {
847 // Look for evidence that this landingpad has already been processed.
848 bool LPadHasActionList = false;
849 BasicBlock *LPadBB = LPad->getParent();
850 for (Instruction &Inst : *LPadBB) {
851 if (match(&Inst, m_Intrinsic())) {
852 LPadHasActionList = true;
853 break;
854 }
855 }
856
857 // If we've already outlined the handlers for this landingpad,
858 // there's nothing more to do here.
859 if (LPadHasActionList)
860 continue;
861
862 // If either of the values in the aggregate returned by the landing pad is
863 // extracted and stored to memory, promote the stored value to a register.
864 promoteLandingPadValues(LPad);
865
866 LandingPadActions Actions;
867 mapLandingPadBlocks(LPad, Actions);
868
869 HandlersOutlined |= !Actions.actions().empty();
870 for (ActionHandler *Action : Actions) {
871 if (Action->hasBeenProcessed())
872 continue;
873 BasicBlock *StartBB = Action->getStartBlock();
874
875 // SEH doesn't do any outlining for catches. Instead, pass the handler
876 // basic block addr to llvm.eh.actions and list the block as a return
877 // target.
878 if (isAsynchronousEHPersonality(Personality)) {
879 if (auto *CatchAction = dyn_cast(Action)) {
880 processSEHCatchHandler(CatchAction, StartBB);
881 continue;
882 }
883 }
884
885 outlineHandler(Action, &F, LPad, StartBB, FrameVarInfo);
886 }
887
888 // Split the block after the landingpad instruction so that it is just a
889 // call to llvm.eh.actions followed by indirectbr.
890 assert(!isa(LPadBB->begin()) && "lpad phi not removed");
891 SplitBlock(LPadBB, LPad->getNextNode(), DT);
892 // Erase the branch inserted by the split so we can insert indirectbr.
893 LPadBB->getTerminator()->eraseFromParent();
894
895 // Replace all extracted values with undef and ultimately replace the
896 // landingpad with undef.
897 SmallVector SEHCodeUses;
898 SmallVector EHUndefs;
899 for (User *U : LPad->users()) {
900 auto *E = dyn_cast(U);
901 if (!E)
902 continue;
903 assert(E->getNumIndices() == 1 &&
904 "Unexpected operation: extracting both landing pad values");
905 unsigned Idx = *E->idx_begin();
906 assert((Idx == 0 || Idx == 1) && "unexpected index");
907 if (Idx == 0 && isAsynchronousEHPersonality(Personality))
908 SEHCodeUses.push_back(E);
909 else
910 EHUndefs.push_back(E);
911 }
912 for (Instruction *E : EHUndefs) {
913 E->replaceAllUsesWith(UndefValue::get(E->getType()));
914 E->eraseFromParent();
915 }
916 LPad->replaceAllUsesWith(UndefValue::get(LPad->getType()));
917
918 // Rewrite uses of the exception pointer to loads of an alloca.
919 while (!SEHCodeUses.empty()) {
920 Instruction *E = SEHCodeUses.pop_back_val();
921 SmallVector Uses;
922 for (Use &U : E->uses())
923 Uses.push_back(&U);
924 for (Use *U : Uses) {
925 auto *I = cast(U->getUser());
926 if (isa(I))
927 continue;
928 if (auto *Phi = dyn_cast(I))
929 SEHCodeUses.push_back(Phi);
930 else
931 U->set(new LoadInst(SEHExceptionCodeSlot, "sehcode", false, I));
932 }
933 E->replaceAllUsesWith(UndefValue::get(E->getType()));
934 E->eraseFromParent();
935 }
936
937 // Add a call to describe the actions for this landing pad.
938 std::vector ActionArgs;
939 for (ActionHandler *Action : Actions) {
940 // Action codes from docs are: 0 cleanup, 1 catch.
941 if (auto *CatchAction = dyn_cast(Action)) {
942 ActionArgs.push_back(ConstantInt::get(Int32Type, 1));
943 ActionArgs.push_back(CatchAction->getSelector());
944 // Find the frame escape index of the exception object alloca in the
945 // parent.
946 int FrameEscapeIdx = -1;
947 Value *EHObj = const_cast(CatchAction->getExceptionVar());
948 if (EHObj && !isa(EHObj)) {
949 auto I = FrameVarInfo.find(EHObj);
950 assert(I != FrameVarInfo.end() &&
951 "failed to map llvm.eh.begincatch var");
952 FrameEscapeIdx = std::distance(FrameVarInfo.begin(), I);
953 }
954 ActionArgs.push_back(ConstantInt::get(Int32Type, FrameEscapeIdx));
955 } else {
956 ActionArgs.push_back(ConstantInt::get(Int32Type, 0));
957 }
958 ActionArgs.push_back(Action->getHandlerBlockOrFunc());
959 }
960 CallInst *Recover =
961 CallInst::Create(ActionIntrin, ActionArgs, "recover", LPadBB);
962
963 SetVector ReturnTargets;
964 for (ActionHandler *Action : Actions) {
965 if (auto *CatchAction = dyn_cast(Action)) {
966 const auto &CatchTargets = CatchAction->getReturnTargets();
967 ReturnTargets.insert(CatchTargets.begin(), CatchTargets.end());
968 }
969 }
970 IndirectBrInst *Branch =
971 IndirectBrInst::Create(Recover, ReturnTargets.size(), LPadBB);
972 for (BasicBlock *Target : ReturnTargets)
973 Branch->addDestination(Target);
974
975 if (!isAsynchronousEHPersonality(Personality)) {
976 // C++ EH must repopulate the targets later to handle the case of
977 // targets that are reached indirectly through nested landing pads.
978 LPadImpls.push_back(std::make_pair(Recover, Branch));
979 }
980
981 } // End for each landingpad
982
983 // If nothing got outlined, there is no more processing to be done.
984 if (!HandlersOutlined)
985 return false;
986
987 // Replace any nested landing pad stubs with the correct action handler.
988 // This must be done before we remove unreachable blocks because it
989 // cleans up references to outlined blocks that will be deleted.
990 for (auto &LPadPair : NestedLPtoOriginalLP)
991 completeNestedLandingPad(&F, LPadPair.first, LPadPair.second, FrameVarInfo);
992 NestedLPtoOriginalLP.clear();
993
994 // Update the indirectbr instructions' target lists if necessary.
995 SetVector CheckedTargets;
996 SmallVector, 4> ActionList;
997 for (auto &LPadImplPair : LPadImpls) {
998 IntrinsicInst *Recover = cast(LPadImplPair.first);
999 IndirectBrInst *Branch = LPadImplPair.second;
1000
1001 // Get a list of handlers called by
1002 parseEHActions(Recover, ActionList);
1003
1004 // Add an indirect branch listing possible successors of the catch handlers.
1005 SetVector ReturnTargets;
1006 for (const auto &Action : ActionList) {
1007 if (auto *CA = dyn_cast(Action.get())) {
1008 Function *Handler = cast(CA->getHandlerBlockOrFunc());
1009 getPossibleReturnTargets(&F, Handler, ReturnTargets);
1010 }
1011 }
1012 ActionList.clear();
1013 // Clear any targets we already knew about.
1014 for (unsigned int I = 0, E = Branch->getNumDestinations(); I < E; ++I) {
1015 BasicBlock *KnownTarget = Branch->getDestination(I);
1016 if (ReturnTargets.count(KnownTarget))
1017 ReturnTargets.remove(KnownTarget);
1018 }
1019 for (BasicBlock *Target : ReturnTargets) {
1020 Branch->addDestination(Target);
1021 // The target may be a block that we excepted to get pruned.
1022 // If it is, it may contain a call to llvm.eh.endcatch.
1023 if (CheckedTargets.insert(Target)) {
1024 // Earlier preparations guarantee that all calls to llvm.eh.endcatch
1025 // will be followed by an unconditional branch.
1026 auto *Br = dyn_cast(Target->getTerminator());
1027 if (Br && Br->isUnconditional() &&
1028 Br != Target->getFirstNonPHIOrDbgOrLifetime()) {
1029 Instruction *Prev = Br->getPrevNode();
1030 if (match(cast(Prev), m_Intrinsic()))
1031 Prev->eraseFromParent();
1032 }
1033 }
1034 }
1035 }
1036 LPadImpls.clear();
1037
1038 F.addFnAttr("wineh-parent", F.getName());
1039
1040 // Delete any blocks that were only used by handlers that were outlined above.
1041 removeUnreachableBlocks(F);
1042
1043 BasicBlock *Entry = &F.getEntryBlock();
1044 IRBuilder<> Builder(F.getParent()->getContext());
1045 Builder.SetInsertPoint(Entry, Entry->getFirstInsertionPt());
1046
1047 Function *FrameEscapeFn =
1048 Intrinsic::getDeclaration(M, Intrinsic::localescape);
1049 Function *RecoverFrameFn =
1050 Intrinsic::getDeclaration(M, Intrinsic::localrecover);
1051 SmallVector AllocasToEscape;
1052
1053 // Scan the entry block for an existing call to llvm.localescape. We need to
1054 // keep escaping those objects.
1055 for (Instruction &I : F.front()) {
1056 auto *II = dyn_cast(&I);
1057 if (II && II->getIntrinsicID() == Intrinsic::localescape) {
1058 auto Args = II->arg_operands();
1059 AllocasToEscape.append(Args.begin(), Args.end());
1060 II->eraseFromParent();
1061 break;
1062 }
1063 }
1064
1065 // Finally, replace all of the temporary allocas for frame variables used in
1066 // the outlined handlers with calls to llvm.localrecover.
1067 for (auto &VarInfoEntry : FrameVarInfo) {
1068 Value *ParentVal = VarInfoEntry.first;
1069 TinyPtrVector &Allocas = VarInfoEntry.second;
1070 AllocaInst *ParentAlloca = cast(ParentVal);
1071
1072 // FIXME: We should try to sink unescaped allocas from the parent frame into
1073 // the child frame. If the alloca is escaped, we have to use the lifetime
1074 // markers to ensure that the alloca is only live within the child frame.
1075
1076 // Add this alloca to the list of things to escape.
1077 AllocasToEscape.push_back(ParentAlloca);
1078
1079 // Next replace all outlined allocas that are mapped to it.
1080 for (AllocaInst *TempAlloca : Allocas) {
1081 if (TempAlloca == getCatchObjectSentinel())
1082 continue; // Skip catch parameter sentinels.
1083 Function *HandlerFn = TempAlloca->getParent()->getParent();
1084 llvm::Value *FP = HandlerToParentFP[HandlerFn];
1085 assert(FP);
1086
1087 // FIXME: Sink this localrecover into the blocks where it is used.
1088 Builder.SetInsertPoint(TempAlloca);
1089 Builder.SetCurrentDebugLocation(TempAlloca->getDebugLoc());
1090 Value *RecoverArgs[] = {
1091 Builder.CreateBitCast(&F, Int8PtrType, ""), FP,
1092 llvm::ConstantInt::get(Int32Type, AllocasToEscape.size() - 1)};
1093 Instruction *RecoveredAlloca =
1094 Builder.CreateCall(RecoverFrameFn, RecoverArgs);
1095
1096 // Add a pointer bitcast if the alloca wasn't an i8.
1097 if (RecoveredAlloca->getType() != TempAlloca->getType()) {
1098 RecoveredAlloca->setName(Twine(TempAlloca->getName()) + ".i8");
1099 RecoveredAlloca = cast(
1100 Builder.CreateBitCast(RecoveredAlloca, TempAlloca->getType()));
1101 }
1102 TempAlloca->replaceAllUsesWith(RecoveredAlloca);
1103 TempAlloca->removeFromParent();
1104 RecoveredAlloca->takeName(TempAlloca);
1105 delete TempAlloca;
1106 }
1107 } // End for each FrameVarInfo entry.
1108
1109 // Insert 'call void (...)* @llvm.localescape(...)' at the end of the entry
1110 // block.
1111 Builder.SetInsertPoint(&F.getEntryBlock().back());
1112 Builder.CreateCall(FrameEscapeFn, AllocasToEscape);
1113
1114 if (SEHExceptionCodeSlot) {
1115 if (isAllocaPromotable(SEHExceptionCodeSlot)) {
1116 SmallPtrSet UserBlocks;
1117 for (User *U : SEHExceptionCodeSlot->users()) {
1118 if (auto *Inst = dyn_cast(U))
1119 UserBlocks.insert(Inst->getParent());
1120 }
1121 PromoteMemToReg(SEHExceptionCodeSlot, *DT);
1122 // After the promotion, kill off dead instructions.
1123 for (BasicBlock *BB : UserBlocks)
1124 SimplifyInstructionsInBlock(BB, LibInfo);
1125 }
1126 }
1127
1128 // Clean up the handler action maps we created for this function
1129 DeleteContainerSeconds(CatchHandlerMap);
1130 CatchHandlerMap.clear();
1131 DeleteContainerSeconds(CleanupHandlerMap);
1132 CleanupHandlerMap.clear();
1133 HandlerToParentFP.clear();
1134 DT = nullptr;
1135 LibInfo = nullptr;
1136 SEHExceptionCodeSlot = nullptr;
1137 EHBlocks.clear();
1138 NormalBlocks.clear();
1139 EHReturnBlocks.clear();
1140
1141 return HandlersOutlined;
1142 }
1143
1144 void WinEHPrepare::promoteLandingPadValues(LandingPadInst *LPad) {
1145 // If the return values of the landing pad instruction are extracted and
1146 // stored to memory, we want to promote the store locations to reg values.
1147 SmallVector EHAllocas;
1148
1149 // The landingpad instruction returns an aggregate value. Typically, its
1150 // value will be passed to a pair of extract value instructions and the
1151 // results of those extracts are often passed to store instructions.
1152 // In unoptimized code the stored value will often be loaded and then stored
1153 // again.
1154 for (auto *U : LPad->users()) {
1155 ExtractValueInst *Extract = dyn_cast(U);
1156 if (!Extract)
1157 continue;
1158
1159 for (auto *EU : Extract->users()) {
1160 if (auto *Store = dyn_cast(EU)) {
1161 auto *AV = cast(Store->getPointerOperand());
1162 EHAllocas.push_back(AV);
1163 }
1164 }
1165 }
1166
1167 // We can't do this without a dominator tree.
1168 assert(DT);
1169
1170 if (!EHAllocas.empty()) {
1171 PromoteMemToReg(EHAllocas, *DT);
1172 EHAllocas.clear();
1173 }
1174
1175 // After promotion, some extracts may be trivially dead. Remove them.
1176 SmallVector Users(LPad->user_begin(), LPad->user_end());
1177 for (auto *U : Users)
1178 RecursivelyDeleteTriviallyDeadInstructions(U);
1179 }
1180
1181 void WinEHPrepare::getPossibleReturnTargets(Function *ParentF,
1182 Function *HandlerF,
1183 SetVector &Targets) {
1184 for (BasicBlock &BB : *HandlerF) {
1185 // If the handler contains landing pads, check for any
1186 // handlers that may return directly to a block in the
1187 // parent function.
1188 if (auto *LPI = BB.getLandingPadInst()) {
1189 IntrinsicInst *Recover = cast(LPI->getNextNode());
1190 SmallVector, 4> ActionList;
1191 parseEHActions(Recover, ActionList);
1192 for (const auto &Action : ActionList) {
1193 if (auto *CH = dyn_cast(Action.get())) {
1194 Function *NestedF = cast(CH->getHandlerBlockOrFunc());
1195 getPossibleReturnTargets(ParentF, NestedF, Targets);
1196 }
1197 }
1198 }
1199
1200 auto *Ret = dyn_cast(BB.getTerminator());
1201 if (!Ret)
1202 continue;
1203
1204 // Handler functions must always return a block address.
1205 BlockAddress *BA = cast(Ret->getReturnValue());
1206
1207 // If this is the handler for a nested landing pad, the
1208 // return address may have been remapped to a block in the
1209 // parent handler. We're not interested in those.
1210 if (BA->getFunction() != ParentF)
1211 continue;
1212
1213 Targets.insert(BA->getBasicBlock());
1214 }
1215 }
1216
1217 void WinEHPrepare::completeNestedLandingPad(Function *ParentFn,
1218 LandingPadInst *OutlinedLPad,
1219 const LandingPadInst *OriginalLPad,
1220 FrameVarInfoMap &FrameVarInfo) {
1221 // Get the nested block and erase the unreachable instruction that was
1222 // temporarily inserted as its terminator.
1223 LLVMContext &Context = ParentFn->getContext();
1224 BasicBlock *OutlinedBB = OutlinedLPad->getParent();
1225 // If the nested landing pad was outlined before the landing pad that enclosed
1226 // it, it will already be in outlined form. In that case, we just need to see
1227 // if the returns and the enclosing branch instruction need to be updated.
1228 IndirectBrInst *Branch =
1229 dyn_cast(OutlinedBB->getTerminator());
1230 if (!Branch) {
1231 // If the landing pad wasn't in outlined form, it should be a stub with
1232 // an unreachable terminator.
1233 assert(isa(OutlinedBB->getTerminator()));
1234 OutlinedBB->getTerminator()->eraseFromParent();
1235 // That should leave OutlinedLPad as the last instruction in its block.
1236 assert(&OutlinedBB->back() == OutlinedLPad);
1237 }
1238
1239 // The original landing pad will have already had its action intrinsic
1240 // built by the outlining loop. We need to clone that into the outlined
1241 // location. It may also be necessary to add references to the exception
1242 // variables to the outlined handler in which this landing pad is nested
1243 // and remap return instructions in the nested handlers that should return
1244 // to an address in the outlined handler.
1245 Function *OutlinedHandlerFn = OutlinedBB->getParent();
1246 BasicBlock::const_iterator II = OriginalLPad->getIterator();
1247 ++II;
1248 // The instruction after the landing pad should now be a call to eh.actions.
1249 const Instruction *Recover = &*II;
1250 const IntrinsicInst *EHActions = cast(Recover);
1251
1252 // Remap the return target in the nested handler.
1253 SmallVector ActionTargets;
1254 SmallVector, 4> ActionList;
1255 parseEHActions(EHActions, ActionList);
1256 for (const auto &Action : ActionList) {
1257 auto *Catch = dyn_cast(Action.get());
1258 if (!Catch)
1259 continue;
1260 // The dyn_cast to function here selects C++ catch handlers and skips
1261 // SEH catch handlers.
1262 auto *Handler = dyn_cast(Catch->getHandlerBlockOrFunc());
1263 if (!Handler)
1264 continue;
1265 // Visit all the return instructions, looking for places that return
1266 // to a location within OutlinedHandlerFn.
1267 for (BasicBlock &NestedHandlerBB : *Handler) {
1268 auto *Ret = dyn_cast(NestedHandlerBB.getTerminator());
1269 if (!Ret)
1270 continue;
1271
1272 // Handler functions must always return a block address.
1273 BlockAddress *BA = cast(Ret->getReturnValue());
1274 // The original target will have been in the main parent function,
1275 // but if it is the address of a block that has been outlined, it
1276 // should be a block that was outlined into OutlinedHandlerFn.
1277 assert(BA->getFunction() == ParentFn);
1278
1279 // Ignore targets that aren't part of an outlined handler function.
1280 if (!LPadTargetBlocks.count(BA->getBasicBlock()))
1281 continue;
1282
1283 // If the return value is the address ofF a block that we
1284 // previously outlined into the parent handler function, replace
1285 // the return instruction and add the mapped target to the list
1286 // of possible return addresses.
1287 BasicBlock *MappedBB = LPadTargetBlocks[BA->getBasicBlock()];
1288 assert(MappedBB->getParent() == OutlinedHandlerFn);
1289 BlockAddress *NewBA = BlockAddress::get(OutlinedHandlerFn, MappedBB);
1290 Ret->eraseFromParent();
1291 ReturnInst::Create(Context, NewBA, &NestedHandlerBB);
1292 ActionTargets.push_back(NewBA);
1293 }
1294 }
1295 ActionList.clear();
1296
1297 if (Branch) {
1298 // If the landing pad was already in outlined form, just update its targets.
1299 for (unsigned int I = Branch->getNumDestinations(); I > 0; --I)
1300 Branch->removeDestination(I);
1301 // Add the previously collected action targets.
1302 for (auto *Target : ActionTargets)
1303 Branch->addDestination(Target->getBasicBlock());
1304 } else {
1305 // If the landing pad was previously stubbed out, fill in its outlined form.
1306 IntrinsicInst *NewEHActions = cast(EHActions->clone());
1307 OutlinedBB->getInstList().push_back(NewEHActions);
1308
1309 // Insert an indirect branch into the outlined landing pad BB.
1310 IndirectBrInst *IBr = IndirectBrInst::Create(NewEHActions, 0, OutlinedBB);
1311 // Add the previously collected action targets.
1312 for (auto *Target : ActionTargets)
1313 IBr->addDestination(Target->getBasicBlock());
1314 }
1315 }
1316
1317 // This function examines a block to determine whether the block ends with a
1318 // conditional branch to a catch handler based on a selector comparison.
1319 // This function is used both by the WinEHPrepare::findSelectorComparison() and
1320 // WinEHCleanupDirector::handleTypeIdFor().
1321 static bool isSelectorDispatch(BasicBlock *BB, BasicBlock *&CatchHandler,
1322 Constant *&Selector, BasicBlock *&NextBB) {
1323 ICmpInst::Predicate Pred;
1324 BasicBlock *TBB, *FBB;
1325 Value *LHS, *RHS;
1326
1327 if (!match(BB->getTerminator(),
1328 m_Br(m_ICmp(Pred, m_Value(LHS), m_Value(RHS)), TBB, FBB)))
1329 return false;
1330
1331 if (!match(LHS,
1332 m_Intrinsic(m_Constant(Selector))) &&
1333 !match(RHS, m_Intrinsic(m_Constant(Selector))))
1334 return false;
1335
1336 if (Pred == CmpInst::ICMP_EQ) {
1337 CatchHandler = TBB;
1338 NextBB = FBB;
1339 return true;
1340 }
1341
1342 if (Pred == CmpInst::ICMP_NE) {
1343 CatchHandler = FBB;
1344 NextBB = TBB;
1345 return true;
1346 }
1347
1348 return false;
1349 }
1350
1351 static bool isCatchBlock(BasicBlock *BB) {
1352 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg()->getIterator(),
1353 IE = BB->end();
1354 II != IE; ++II) {
1355 if (match(cast(II), m_Intrinsic()))
1356 return true;
1357 }
1358 return false;
1359 }
1360
1361 static BasicBlock *createStubLandingPad(Function *Handler) {
1362 // FIXME: Finish this!
1363 LLVMContext &Context = Handler->getContext();
1364 BasicBlock *StubBB = BasicBlock::Create(Context, "stub");
1365 Handler->getBasicBlockList().push_back(StubBB);
1366 IRBuilder<> Builder(StubBB);
1367 LandingPadInst *LPad = Builder.CreateLandingPad(
1368 llvm::StructType::get(Type::getInt8PtrTy(Context),
1369 Type::getInt32Ty(Context), nullptr),
1370 0);
1371 // Insert a call to llvm.eh.actions so that we don't try to outline this lpad.
1372 Function *ActionIntrin =
1373 Intrinsic::getDeclaration(Handler->getParent(), Intrinsic::eh_actions);
1374 Builder.CreateCall(ActionIntrin, {}, "recover");
1375 LPad->setCleanup(true);
1376 Builder.CreateUnreachable();
1377 return StubBB;
1378 }
1379
1380 // Cycles through the blocks in an outlined handler function looking for an
1381 // invoke instruction and inserts an invoke of llvm.donothing with an empty
1382 // landing pad if none is found. The code that generates the .xdata tables for
1383 // the handler needs at least one landing pad to identify the parent function's
1384 // personality.
1385 void WinEHPrepare::addStubInvokeToHandlerIfNeeded(Function *Handler) {
1386 ReturnInst *Ret = nullptr;
1387 UnreachableInst *Unreached = nullptr;
1388 for (BasicBlock &BB : *Handler) {
1389 TerminatorInst *Terminator = BB.getTerminator();
1390 // If we find an invoke, there is nothing to be done.
1391 auto *II = dyn_cast(Terminator);
1392 if (II)
1393 return;
1394 // If we've already recorded a return instruction, keep looking for invokes.
1395 if (!Ret)
1396 Ret = dyn_cast(Terminator);
1397 // If we haven't recorded an unreachable instruction, try this terminator.
1398 if (!Unreached)
1399 Unreached = dyn_cast(Terminator);
1400 }
1401
1402 // If we got this far, the handler contains no invokes. We should have seen
1403 // at least one return or unreachable instruction. We'll insert an invoke of
1404 // llvm.donothing ahead of that instruction.
1405 assert(Ret || Unreached);
1406 TerminatorInst *Term;
1407 if (Ret)
1408 Term = Ret;
1409 else
1410 Term = Unreached;
1411 BasicBlock *OldRetBB = Term->getParent();
1412 BasicBlock *NewRetBB = SplitBlock(OldRetBB, Term, DT);
1413 // SplitBlock adds an unconditional branch instruction at the end of the
1414 // parent block. We want to replace that with an invoke call, so we can
1415 // erase it now.
1416 OldRetBB->getTerminator()->eraseFromParent();
1417 BasicBlock *StubLandingPad = createStubLandingPad(Handler);
1418 Function *F =
1419 Intrinsic::getDeclaration(Handler->getParent(), Intrinsic::donothing);
1420 InvokeInst::Create(F, NewRetBB, StubLandingPad, None, "", OldRetBB);
1421 }
1422
1423 // FIXME: Consider sinking this into lib/Target/X86 somehow. TargetLowering
1424 // usually doesn't build LLVM IR, so that's probably the wrong place.
1425 Function *WinEHPrepare::createHandlerFunc(Function *ParentFn, Type *RetTy,
1426 const Twine &Name, Module *M,
1427 Value *&ParentFP) {
1428 // x64 uses a two-argument prototype where the parent FP is the second
1429 // argument. x86 uses no arguments, just the incoming EBP value.
1430 LLVMContext &Context = M->getContext();
1431 Type *Int8PtrType = Type::getInt8PtrTy(Context);
1432 FunctionType *FnType;
1433 if (TheTriple.getArch() == Triple::x86_64) {
1434 Type *ArgTys[2] = {Int8PtrType, Int8PtrType};
1435 FnType = FunctionType::get(RetTy, ArgTys, false);
1436 } else {
1437 FnType = FunctionType::get(RetTy, None, false);
1438 }
1439
1440 Function *Handler =
1441 Function::Create(FnType, GlobalVariable::InternalLinkage, Name, M);
1442 BasicBlock *Entry = BasicBlock::Create(Context, "entry");
1443 Handler->getBasicBlockList().push_front(Entry);
1444 if (TheTriple.getArch() == Triple::x86_64) {
1445 ParentFP = &(Handler->getArgumentList().back());
1446 } else {
1447 assert(M);
1448 Function *FrameAddressFn =
1449 Intrinsic::getDeclaration(M, Intrinsic::frameaddress);
1450 Function *RecoverFPFn =
1451 Intrinsic::getDeclaration(M, Intrinsic::x86_seh_recoverfp);
1452 IRBuilder<> Builder(&Handler->getEntryBlock());
1453 Value *EBP =
1454 Builder.CreateCall(FrameAddressFn, {Builder.getInt32(1)}, "ebp");
1455 Value *ParentI8Fn = Builder.CreateBitCast(ParentFn, Int8PtrType);
1456 ParentFP = Builder.CreateCall(RecoverFPFn, {ParentI8Fn, EBP});
1457 }
1458 return Handler;
1459 }
1460
1461 bool WinEHPrepare::outlineHandler(ActionHandler *Action, Function *SrcFn,
1462 LandingPadInst *LPad, BasicBlock *StartBB,
1463 FrameVarInfoMap &VarInfo) {
1464 Module *M = SrcFn->getParent();
1465 LLVMContext &Context = M->getContext();
1466 Type *Int8PtrType = Type::getInt8PtrTy(Context);
1467
1468 // Create a new function to receive the handler contents.
1469 Value *ParentFP;
1470 Function *Handler;
1471 if (Action->getType() == Catch) {
1472 Handler = createHandlerFunc(SrcFn, Int8PtrType, SrcFn->getName() + ".catch", M,
1473 ParentFP);
1474 } else {
1475 Handler = createHandlerFunc(SrcFn, Type::getVoidTy(Context),
1476 SrcFn->getName() + ".cleanup", M, ParentFP);
1477 }
1478 Handler->setPersonalityFn(SrcFn->getPersonalityFn());
1479 HandlerToParentFP[Handler] = ParentFP;
1480 Handler->addFnAttr("wineh-parent", SrcFn->getName());
1481 BasicBlock *Entry = &Handler->getEntryBlock();
1482
1483 // Generate a standard prolog to setup the frame recovery structure.
1484 IRBuilder<> Builder(Context);
1485 Builder.SetInsertPoint(Entry);
1486 Builder.SetCurrentDebugLocation(LPad->getDebugLoc());
1487
1488 std::unique_ptr Director;
1489
1490 ValueToValueMapTy VMap;
1491
1492 LandingPadMap &LPadMap = LPadMaps[LPad];
1493 if (!LPadMap.isInitialized())
1494 LPadMap.mapLandingPad(LPad);
1495 if (auto *CatchAction = dyn_cast(Action)) {
1496 Constant *Sel = CatchAction->getSelector();
1497 Director.reset(new WinEHCatchDirector(Handler, ParentFP, Sel, VarInfo,
1498 LPadMap, NestedLPtoOriginalLP, DT,
1499 EHBlocks));
1500 LPadMap.remapEHValues(VMap, UndefValue::get(Int8PtrType),
1501 ConstantInt::get(Type::getInt32Ty(Context), 1));
1502 } else {
1503 Director.reset(
1504 new WinEHCleanupDirector(Handler, ParentFP, VarInfo, LPadMap));
1505 LPadMap.remapEHValues(VMap, UndefValue::get(Int8PtrType),
1506 UndefValue::get(Type::getInt32Ty(Context)));
1507 }
1508
1509 SmallVector Returns;
1510 ClonedCodeInfo OutlinedFunctionInfo;
1511
1512 // If the start block contains PHI nodes, we need to map them.
1513 BasicBlock::iterator II = StartBB->begin();
1514 while (auto *PN = dyn_cast(II)) {
1515 bool Mapped = false;
1516 // Look for PHI values that we have already mapped (such as the selector).
1517 for (Value *Val : PN->incoming_values()) {
1518 if (VMap.count(Val)) {
1519 VMap[PN] = VMap[Val];
1520 Mapped = true;
1521 }
1522 }
1523 // If we didn't find a match for this value, map it as an undef.
1524 if (!Mapped) {
1525 VMap[PN] = UndefValue::get(PN->getType());
1526 }
1527 ++II;
1528 }
1529
1530 // The landing pad value may be used by PHI nodes. It will ultimately be
1531 // eliminated, but we need it in the map for intermediate handling.
1532 VMap[LPad] = UndefValue::get(LPad->getType());
1533
1534 // Skip over PHIs and, if applicable, landingpad instructions.
1535 II = StartBB->getFirstInsertionPt();
1536
1537 CloneAndPruneIntoFromInst(Handler, SrcFn, &*II, VMap,
1538 /*ModuleLevelChanges=*/false, Returns, "",
1539 &OutlinedFunctionInfo, Director.get());
1540
1541 // Move all the instructions in the cloned "entry" block into our entry block.
1542 // Depending on how the parent function was laid out, the block that will
1543 // correspond to the outlined entry block may not be the first block in the
1544 // list. We can recognize it, however, as the cloned block which has no
1545 // predecessors. Any other block wouldn't have been cloned if it didn't
1546 // have a predecessor which was also cloned.
1547 Function::iterator ClonedIt = std::next(Function::iterator(Entry));
1548 while (!pred_empty(&*ClonedIt))
1549 ++ClonedIt;
1550 assert(ClonedIt != Entry->getParent()->end());
1551 BasicBlock *ClonedEntryBB = &*ClonedIt;
1552 Entry->getInstList().splice(Entry->end(), ClonedEntryBB->getInstList());
1553 ClonedEntryBB->eraseFromParent();
1554
1555 // Make sure we can identify the handler's personality later.
1556 addStubInvokeToHandlerIfNeeded(Handler);
1557
1558 if (auto *CatchAction = dyn_cast(Action)) {
1559 WinEHCatchDirector *CatchDirector =
1560 reinterpret_cast(Director.get());
1561 CatchAction->setExceptionVar(CatchDirector->getExceptionVar());
1562 CatchAction->setReturnTargets(CatchDirector->getReturnTargets());
1563
1564 // Look for blocks that are not part of the landing pad that we just
1565 // outlined but terminate with a call to llvm.eh.endcatch and a
1566 // branch to a block that is in the handler we just outlined.
1567 // These blocks will be part of a nested landing pad that intends to
1568 // return to an address in this handler. This case is best handled
1569 // after both landing pads have been outlined, so for now we'll just
1570 // save the association of the blocks in LPadTargetBlocks. The
1571 // return instructions which are created from these branches will be
1572 // replaced after all landing pads have been outlined.
1573 for (const auto MapEntry : VMap) {
1574 // VMap maps all values and blocks that were just cloned, but dead
1575 // blocks which were pruned will map to nullptr.
1576 if (!isa(MapEntry.first) || MapEntry.second == nullptr)
1577 continue;
1578 const BasicBlock *MappedBB = cast(MapEntry.first);
1579 for (auto *Pred : predecessors(const_cast(MappedBB))) {
1580 auto *Branch = dyn_cast(Pred->getTerminator());
1581 if (!Branch || !Branch->isUnconditional() || Pred->size() <= 1)
1582 continue;
1583 BasicBlock::iterator II =
1584 const_cast(Branch)->getIterator();
1585 --II;
1586 if (match(cast(II), m_Intrinsic())) {
1587 // This would indicate that a nested landing pad wants to return
1588 // to a block that is outlined into two different handlers.
1589 assert(!LPadTargetBlocks.count(MappedBB));
1590 LPadTargetBlocks[MappedBB] = cast(MapEntry.second);
1591 }
1592 }
1593 }
1594 } // End if (CatchAction)
1595
1596 Action->setHandlerBlockOrFunc(Handler);
1597
1598 return true;
1599 }
1600
1601 /// This BB must end in a selector dispatch. All we need to do is pass the
1602 /// handler block to llvm.eh.actions and list it as a possible indirectbr
1603 /// target.
1604 void WinEHPrepare::processSEHCatchHandler(CatchHandler *CatchAction,
1605 BasicBlock *StartBB) {
1606 BasicBlock *HandlerBB;
1607 BasicBlock *NextBB;
1608 Constant *Selector;
1609 bool Res = isSelectorDispatch(StartBB, HandlerBB, Selector, NextBB);
1610 if (Res) {
1611 // If this was EH dispatch, this must be a conditional branch to the handler
1612 // block.
1613 // FIXME: Handle instructions in the dispatch block. Currently we drop them,
1614 // leading to crashes if some optimization hoists stuff here.
1615 assert(CatchAction->getSelector() && HandlerBB &&
1616 "expected catch EH dispatch");
1617 } else {
1618 // This must be a catch-all. Split the block after the landingpad.
1619 assert(CatchAction->getSelector()->isNullValue() && "expected catch-all");
1620 HandlerBB = SplitBlock(StartBB, &*StartBB->getFirstInsertionPt(), DT);
1621 }
1622 IRBuilder<> Builder(&*HandlerBB->getFirstInsertionPt());
1623 Function *EHCodeFn = Intrinsic::getDeclaration(
1624 StartBB->getParent()->getParent(), Intrinsic::eh_exceptioncode_old);
1625 Value *Code = Builder.CreateCall(EHCodeFn, {}, "sehcode");
1626 Code = Builder.CreateIntToPtr(Code, SEHExceptionCodeSlot->getAllocatedType());
1627 Builder.CreateStore(Code, SEHExceptionCodeSlot);
1628 CatchAction->setHandlerBlockOrFunc(BlockAddress::get(HandlerBB));
1629 TinyPtrVector Targets(HandlerBB);
1630 CatchAction->setReturnTargets(Targets);
1631 }
1632
1633 void LandingPadMap::mapLandingPad(const LandingPadInst *LPad) {
1634 // Each instance of this class should only ever be used to map a single
1635 // landing pad.
1636 assert(OriginLPad == nullptr || OriginLPad == LPad);
1637
1638 // If the landing pad has already been mapped, there's nothing more to do.
1639 if (OriginLPad == LPad)
1640 return;
1641
1642 OriginLPad = LPad;
1643
1644 // The landingpad instruction returns an aggregate value. Typically, its
1645 // value will be passed to a pair of extract value instructions and the
1646 // results of those extracts will have been promoted to reg values before
1647 // this routine is called.
1648 for (auto *U : LPad->users()) {
1649 const ExtractValueInst *Extract = dyn_cast(U);
1650 if (!Extract)
1651 continue;
1652 assert(Extract->getNumIndices() == 1 &&
1653 "Unexpected operation: extracting both landing pad values");
1654 unsigned int Idx = *(Extract->idx_begin());
1655 assert((Idx == 0 || Idx == 1) &&
1656 "Unexpected operation: extracting an unknown landing pad element");
1657 if (Idx == 0) {
1658 ExtractedEHPtrs.push_back(Extract);
1659 } else if (Idx == 1) {
1660 ExtractedSelectors.push_back(Extract);
1661 }
1662 }
1663 }
1664
1665 bool LandingPadMap::isOriginLandingPadBlock(const BasicBlock *BB) const {
1666 return BB->getLandingPadInst() == OriginLPad;
1667 }
1668
1669 bool LandingPadMap::isLandingPadSpecificInst(const Instruction *Inst) const {
1670 if (Inst == OriginLPad)
1671 return true;
1672 for (auto *Extract : ExtractedEHPtrs) {
1673 if (Inst == Extract)
1674 return true;
1675 }
1676 for (auto *Extract : ExtractedSelectors) {
1677 if (Inst == Extract)
1678 return true;
1679 }
1680 return false;
1681 }
1682
1683 void LandingPadMap::remapEHValues(ValueToValueMapTy &VMap, Value *EHPtrValue,
1684 Value *SelectorValue) const {
1685 // Remap all landing pad extract instructions to the specified values.
1686 for (auto *Extract : ExtractedEHPtrs)
1687 VMap[Extract] = EHPtrValue;
1688 for (auto *Extract : ExtractedSelectors)
1689 VMap[Extract] = SelectorValue;
1690 }
1691
1692 static bool isLocalAddressCall(const Value *V) {
1693 return match(const_cast(V), m_Intrinsic());
1694 }
1695
1696 CloningDirector::CloningAction WinEHCloningDirectorBase::handleInstruction(
1697 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1698 // If this is one of the boilerplate landing pad instructions, skip it.
1699 // The instruction will have already been remapped in VMap.
1700 if (LPadMap.isLandingPadSpecificInst(Inst))
1701 return CloningDirector::SkipInstruction;
1702
1703 // Nested landing pads that have not already been outlined will be cloned as
1704 // stubs, with just the landingpad instruction and an unreachable instruction.
1705 // When all landingpads have been outlined, we'll replace this with the
1706 // llvm.eh.actions call and indirect branch created when the landing pad was
1707 // outlined.
1708 if (auto *LPad = dyn_cast(Inst)) {
1709 return handleLandingPad(VMap, LPad, NewBB);
1710 }
1711
1712 // Nested landing pads that have already been outlined will be cloned in their
1713 // outlined form, but we need to intercept the ibr instruction to filter out
1714 // targets that do not return to the handler we are outlining.
1715 if (auto *IBr = dyn_cast(Inst)) {
1716 return handleIndirectBr(VMap, IBr, NewBB);
1717 }
1718
1719 if (auto *Invoke = dyn_cast(Inst))
1720 return handleInvoke(VMap, Invoke, NewBB);
1721
1722 if (auto *Resume = dyn_cast(Inst))
1723 return handleResume(VMap, Resume, NewBB);
1724
1725 if (auto *Cmp = dyn_cast(Inst))
1726 return handleCompare(VMap, Cmp, NewBB);
1727
1728 if (match(Inst, m_Intrinsic()))
1729 return handleBeginCatch(VMap, Inst, NewBB);
1730 if (match(Inst, m_Intrinsic()))
1731 return handleEndCatch(VMap, Inst, NewBB);
1732 if (match(Inst, m_Intrinsic()))
1733 return handleTypeIdFor(VMap, Inst, NewBB);
1734
1735 // When outlining llvm.localaddress(), remap that to the second argument,
1736 // which is the FP of the parent.
1737 if (isLocalAddressCall(Inst)) {
1738 VMap[Inst] = ParentFP;
1739 return CloningDirector::SkipInstruction;
1740 }
1741
1742 // Continue with the default cloning behavior.
1743 return CloningDirector::CloneInstruction;
1744 }
1745
1746 CloningDirector::CloningAction WinEHCatchDirector::handleLandingPad(
1747 ValueToValueMapTy &VMap, const LandingPadInst *LPad, BasicBlock *NewBB) {
1748 // If the instruction after the landing pad is a call to llvm.eh.actions
1749 // the landing pad has already been outlined. In this case, we should
1750 // clone it because it may return to a block in the handler we are
1751 // outlining now that would otherwise be unreachable. The landing pads
1752 // are sorted before outlining begins to enable this case to work
1753 // properly.
1754 const Instruction *NextI = LPad->getNextNode();
1755 if (match(NextI, m_Intrinsic()))
1756 return CloningDirector::CloneInstruction;
1757
1758 // If the landing pad hasn't been outlined yet, the landing pad we are
1759 // outlining now does not dominate it and so it cannot return to a block
1760 // in this handler. In that case, we can just insert a stub landing
1761 // pad now and patch it up later.
1762 Instruction *NewInst = LPad->clone();
1763 if (LPad->hasName())
1764 NewInst->setName(LPad->getName());
1765 // Save this correlation for later processing.
1766 NestedLPtoOriginalLP[cast(NewInst)] = LPad;
1767 VMap[LPad] = NewInst;
1768 BasicBlock::InstListType &InstList = NewBB->getInstList();
1769 InstList.push_back(NewInst);
1770 InstList.push_back(new UnreachableInst(NewBB->getContext()));
1771 return CloningDirector::StopCloningBB;
1772 }
1773
1774 CloningDirector::CloningAction WinEHCatchDirector::handleBeginCatch(
1775 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1776 // The argument to the call is some form of the first element of the
1777 // landingpad aggregate value, but that doesn't matter. It isn't used
1778 // here.
1779 // The second argument is an outparameter where the exception object will be
1780 // stored. Typically the exception object is a scalar, but it can be an
1781 // aggregate when catching by value.
1782 // FIXME: Leave something behind to indicate where the exception object lives
1783 // for this handler. Should it be part of llvm.eh.actions?
1784 assert(ExceptionObjectVar == nullptr && "Multiple calls to "
1785 "llvm.eh.begincatch found while "
1786 "outlining catch handler.");
1787 ExceptionObjectVar = Inst->getOperand(1)->stripPointerCasts();
1788 if (isa(ExceptionObjectVar))
1789 return CloningDirector::SkipInstruction;
1790 assert(cast(ExceptionObjectVar)->isStaticAlloca() &&
1791 "catch parameter is not static alloca");
1792 Materializer.escapeCatchObject(ExceptionObjectVar);
1793 return CloningDirector::SkipInstruction;
1794 }
1795
1796 CloningDirector::CloningAction
1797 WinEHCatchDirector::handleEndCatch(ValueToValueMapTy &VMap,
1798 const Instruction *Inst, BasicBlock *NewBB) {
1799 auto *IntrinCall = dyn_cast(Inst);
1800 // It might be interesting to track whether or not we are inside a catch
1801 // function, but that might make the algorithm more brittle than it needs
1802 // to be.
1803
1804 // The end catch call can occur in one of two places: either in a
1805 // landingpad block that is part of the catch handlers exception mechanism,
1806 // or at the end of the catch block. However, a catch-all handler may call
1807 // end catch from the original landing pad. If the call occurs in a nested
1808 // landing pad block, we must skip it and continue so that the landing pad
1809 // gets cloned.
1810 auto *ParentBB = IntrinCall->getParent();
1811 if (ParentBB->isLandingPad() && !LPadMap.isOriginLandingPadBlock(ParentBB))
1812 return CloningDirector::SkipInstruction;
1813
1814 // If an end catch occurs anywhere else we want to terminate the handler
1815 // with a return to the code that follows the endcatch call. If the
1816 // next instruction is not an unconditional branch, we need to split the
1817 // block to provide a clear target for the return instruction.
1818 BasicBlock *ContinueBB;
1819 auto Next = std::next(BasicBlock::const_iterator(IntrinCall));
1820 const BranchInst *Branch = dyn_cast(Next);
1821 if (!Branch || !Branch->isUnconditional()) {
1822 // We're interrupting the cloning process at this location, so the
1823 // const_cast we're doing here will not cause a problem.
1824 ContinueBB = SplitBlock(const_cast(ParentBB),
1825 const_cast(cast(Next)));
1826 } else {
1827 ContinueBB = Branch->getSuccessor(0);
1828 }
1829
1830 ReturnInst::Create(NewBB->getContext(), BlockAddress::get(ContinueBB), NewBB);
1831 ReturnTargets.push_back(ContinueBB);
1832
1833 // We just added a terminator to the cloned block.
1834 // Tell the caller to stop processing the current basic block so that
1835 // the branch instruction will be skipped.
1836 return CloningDirector::StopCloningBB;
1837 }
1838
1839 CloningDirector::CloningAction WinEHCatchDirector::handleTypeIdFor(
1840 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1841 auto *IntrinCall = dyn_cast(Inst);
1842 Value *Selector = IntrinCall->getArgOperand(0)->stripPointerCasts();
1843 // This causes a replacement that will collapse the landing pad CFG based
1844 // on the filter function we intend to match.
1845 if (Selector == CurrentSelector)
1846 VMap[Inst] = ConstantInt::get(SelectorIDType, 1);
1847 else
1848 VMap[Inst] = ConstantInt::get(SelectorIDType, 0);
1849 // Tell the caller not to clone this instruction.
1850 return CloningDirector::SkipInstruction;
1851 }
1852
1853 CloningDirector::CloningAction WinEHCatchDirector::handleIndirectBr(
1854 ValueToValueMapTy &VMap,
1855 const IndirectBrInst *IBr,
1856 BasicBlock *NewBB) {
1857 // If this indirect branch is not part of a landing pad block, just clone it.
1858 const BasicBlock *ParentBB = IBr->getParent();
1859 if (!ParentBB->isLandingPad())
1860 return CloningDirector::CloneInstruction;
1861
1862 // If it is part of a landing pad, we want to filter out target blocks
1863 // that are not part of the handler we are outlining.
1864 const LandingPadInst *LPad = ParentBB->getLandingPadInst();
1865
1866 // Save this correlation for later processing.
1867 NestedLPtoOriginalLP[cast(VMap[LPad])] = LPad;
1868
1869 // We should only get here for landing pads that have already been outlined.
1870 assert(match(LPad->getNextNode(), m_Intrinsic()));
1871
1872 // Copy the indirectbr, but only include targets that were previously
1873 // identified as EH blocks and are dominated by the nested landing pad.
1874 SetVector ReturnTargets;
1875 for (int I = 0, E = IBr->getNumDestinations(); I < E; ++I) {
1876 auto *TargetBB = IBr->getDestination(I);
1877 if (EHBlocks.count(const_cast(TargetBB)) &&
1878 DT->dominates(ParentBB, TargetBB)) {
1879 DEBUG(dbgs() << " Adding destination " << TargetBB->getName() << "\n");
1880 ReturnTargets.insert(TargetBB);
1881 }
1882 }
1883 IndirectBrInst *NewBranch =
1884 IndirectBrInst::Create(const_cast(IBr->getAddress()),
1885 ReturnTargets.size(), NewBB);
1886 for (auto *Target : ReturnTargets)
1887 NewBranch->addDestination(const_cast(Target));
1888
1889 // The operands and targets of the branch instruction are remapped later
1890 // because it is a terminator. Tell the cloning code to clone the
1891 // blocks we just added to the target list.
1892 return CloningDirector::CloneSuccessors;
1893 }
1894
1895 CloningDirector::CloningAction
1896 WinEHCatchDirector::handleInvoke(ValueToValueMapTy &VMap,
1897 const InvokeInst *Invoke, BasicBlock *NewBB) {
1898 return CloningDirector::CloneInstruction;
1899 }
1900
1901 CloningDirector::CloningAction
1902 WinEHCatchDirector::handleResume(ValueToValueMapTy &VMap,
1903 const ResumeInst *Resume, BasicBlock *NewBB) {
1904 // Resume instructions shouldn't be reachable from catch handlers.
1905 // We still need to handle it, but it will be pruned.
1906 BasicBlock::InstListType &InstList = NewBB->getInstList();
1907 InstList.push_back(new UnreachableInst(NewBB->getContext()));
1908 return CloningDirector::StopCloningBB;
1909 }
1910
1911 CloningDirector::CloningAction
1912 WinEHCatchDirector::handleCompare(ValueToValueMapTy &VMap,
1913 const CmpInst *Compare, BasicBlock *NewBB) {
1914 const IntrinsicInst *IntrinCall = nullptr;
1915 if (match(Compare->getOperand(0), m_Intrinsic())) {
1916 IntrinCall = dyn_cast(Compare->getOperand(0));
1917 } else if (match(Compare->getOperand(1),
1918 m_Intrinsic())) {
1919 IntrinCall = dyn_cast(Compare->getOperand(1));
1920 }
1921 if (IntrinCall) {
1922 Value *Selector = IntrinCall->getArgOperand(0)->stripPointerCasts();
1923 // This causes a replacement that will collapse the landing pad CFG based
1924 // on the filter function we intend to match.
1925 if (Selector == CurrentSelector->stripPointerCasts()) {
1926 VMap[Compare] = ConstantInt::get(SelectorIDType, 1);
1927 } else {
1928 VMap[Compare] = ConstantInt::get(SelectorIDType, 0);
1929 }
1930 return CloningDirector::SkipInstruction;
1931 }
1932 return CloningDirector::CloneInstruction;
1933 }
1934
1935 CloningDirector::CloningAction WinEHCleanupDirector::handleLandingPad(
1936 ValueToValueMapTy &VMap, const LandingPadInst *LPad, BasicBlock *NewBB) {
1937 // The MS runtime will terminate the process if an exception occurs in a
1938 // cleanup handler, so we shouldn't encounter landing pads in the actual
1939 // cleanup code, but they may appear in catch blocks. Depending on where
1940 // we started cloning we may see one, but it will get dropped during dead
1941 // block pruning.
1942 Instruction *NewInst = new UnreachableInst(NewBB->getContext());
1943 VMap[LPad] = NewInst;
1944 BasicBlock::InstListType &InstList = NewBB->getInstList();
1945 InstList.push_back(NewInst);
1946 return CloningDirector::StopCloningBB;
1947 }
1948
1949 CloningDirector::CloningAction WinEHCleanupDirector::handleBeginCatch(
1950 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1951 // Cleanup code may flow into catch blocks or the catch block may be part
1952 // of a branch that will be optimized away. We'll insert a return
1953 // instruction now, but it may be pruned before the cloning process is
1954 // complete.
1955 ReturnInst::Create(NewBB->getContext(), nullptr, NewBB);
1956 return CloningDirector::StopCloningBB;
1957 }
1958
1959 CloningDirector::CloningAction WinEHCleanupDirector::handleEndCatch(
1960 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1961 // Cleanup handlers nested within catch handlers may begin with a call to
1962 // eh.endcatch. We can just ignore that instruction.
1963 return CloningDirector::SkipInstruction;
1964 }
1965
1966 CloningDirector::CloningAction WinEHCleanupDirector::handleTypeIdFor(
1967 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1968 // If we encounter a selector comparison while cloning a cleanup handler,
1969 // we want to stop cloning immediately. Anything after the dispatch
1970 // will be outlined into a different handler.
1971 BasicBlock *CatchHandler;
1972 Constant *Selector;
1973 BasicBlock *NextBB;
1974 if (isSelectorDispatch(const_cast(Inst->getParent()),
1975 CatchHandler, Selector, NextBB)) {
1976 ReturnInst::Create(NewBB->getContext(), nullptr, NewBB);
1977 return CloningDirector::StopCloningBB;
1978 }
1979 // If eg.typeid.for is called for any other reason, it can be ignored.
1980 VMap[Inst] = ConstantInt::get(SelectorIDType, 0);
1981 return CloningDirector::SkipInstruction;
1982 }
1983
1984 CloningDirector::CloningAction WinEHCleanupDirector::handleIndirectBr(
1985 ValueToValueMapTy &VMap,
1986 const IndirectBrInst *IBr,
1987 BasicBlock *NewBB) {
1988 // No special handling is required for cleanup cloning.
1989 return CloningDirector::CloneInstruction;
1990 }
1991
1992 CloningDirector::CloningAction WinEHCleanupDirector::handleInvoke(
1993 ValueToValueMapTy &VMap, const InvokeInst *Invoke, BasicBlock *NewBB) {
1994 // All invokes in cleanup handlers can be replaced with calls.
1995 SmallVector CallArgs(Invoke->op_begin(), Invoke->op_end() - 3);
1996 // Insert a normal call instruction...
1997 CallInst *NewCall =
1998 CallInst::Create(const_cast(Invoke->getCalledValue()), CallArgs,
1999 Invoke->getName(), NewBB);
2000 NewCall->setCallingConv(Invoke->getCallingConv());
2001 NewCall->setAttributes(Invoke->getAttributes());
2002 NewCall->setDebugLoc(Invoke->getDebugLoc());
2003 VMap[Invoke] = NewCall;
2004
2005 // Remap the operands.
2006 llvm::RemapInstruction(NewCall, VMap, RF_None, nullptr, &Materializer);
2007
2008 // Insert an unconditional branch to the normal destination.
2009 BranchInst::Create(Invoke->getNormalDest(), NewBB);
2010
2011 // The unwind destination won't be cloned into the new function, so
2012 // we don't need to clean up its phi nodes.
2013
2014 // We just added a terminator to the cloned block.
2015 // Tell the caller to stop processing the current basic block.
2016 return CloningDirector::CloneSuccessors;
2017 }
2018
2019 CloningDirector::CloningAction WinEHCleanupDirector::handleResume(
2020 ValueToValueMapTy &VMap, const ResumeInst *Resume, BasicBlock *NewBB) {
2021 ReturnInst::Create(NewBB->getContext(), nullptr, NewBB);
2022
2023 // We just added a terminator to the cloned block.
2024 // Tell the caller to stop processing the current basic block so that
2025 // the branch instruction will be skipped.
2026 return CloningDirector::StopCloningBB;
2027 }
2028
2029 CloningDirector::CloningAction
2030 WinEHCleanupDirector::handleCompare(ValueToValueMapTy &VMap,
2031 const CmpInst *Compare, BasicBlock *NewBB) {
2032 if (match(Compare->getOperand(0), m_Intrinsic()) ||
2033 match(Compare->getOperand(1), m_Intrinsic())) {
2034 VMap[Compare] = ConstantInt::get(SelectorIDType, 1);
2035 return CloningDirector::SkipInstruction;
2036 }
2037 return CloningDirector::CloneInstruction;
2038 }
2039
2040 WinEHFrameVariableMaterializer::WinEHFrameVariableMaterializer(
2041 Function *OutlinedFn, Value *ParentFP, FrameVarInfoMap &FrameVarInfo)
2042 : FrameVarInfo(FrameVarInfo), Builder(OutlinedFn->getContext()) {
2043 BasicBlock *EntryBB = &OutlinedFn->getEntryBlock();
2044
2045 // New allocas should be inserted in the entry block, but after the parent FP
2046 // is established if it is an instruction.
2047 BasicBlock::iterator InsertPoint = EntryBB->getFirstInsertionPt();
2048 if (auto *FPInst = dyn_cast(ParentFP))
2049 InsertPoint = std::next(FPInst->getIterator());
2050 Builder.SetInsertPoint(EntryBB, InsertPoint);
2051 }
2052
2053 Value *WinEHFrameVariableMaterializer::materializeValueFor(Value *V) {
2054 // If we're asked to materialize a static alloca, we temporarily create an
2055 // alloca in the outlined function and add this to the FrameVarInfo map. When
2056 // all the outlining is complete, we'll replace these temporary allocas with
2057 // calls to llvm.localrecover.
2058 if (auto *AV = dyn_cast(V)) {
2059 assert(AV->isStaticAlloca() &&
2060 "cannot materialize un-demoted dynamic alloca");
2061 AllocaInst *NewAlloca = dyn_cast(AV->clone());
2062 Builder.Insert(NewAlloca, AV->getName());
2063 FrameVarInfo[AV].push_back(NewAlloca);
2064 return NewAlloca;
2065 }
2066
2067 if (isa(V) || isa(V)) {
2068 Function *Parent = isa(V)
2069 ? cast(V)->getParent()->getParent()
2070 : cast(V)->getParent();
2071 errs()
2072 << "Failed to demote instruction used in exception handler of function "
2073 << GlobalValue::getRealLinkageName(Parent->getName()) << ":\n";
2074 errs() << " " << *V << '\n';
2075 report_fatal_error("WinEHPrepare failed to demote instruction");
2076 }
2077
2078 // Don't materialize other values.
2079 return nullptr;
2080 }
2081
2082 void WinEHFrameVariableMaterializer::escapeCatchObject(Value *V) {
2083 // Catch parameter objects have to live in the parent frame. When we see a use
2084 // of a catch parameter, add a sentinel to the multimap to indicate that it's
2085 // used from another handler. This will prevent us from trying to sink the
2086 // alloca into the handler and ensure that the catch parameter is present in
2087 // the call to llvm.localescape.
2088 FrameVarInfo[V].push_back(getCatchObjectSentinel());
2089 }
2090
2091 // This function maps the catch and cleanup handlers that are reachable from the
2092 // specified landing pad. The landing pad sequence will have this basic shape:
2093 //
2094 //
2095 //
2096 //
2097 //
2098 //
2099 //
2100 //
2101 // ...
2102 //
2103 // Any of the cleanup slots may be absent. The cleanup slots may be occupied by
2104 // any arbitrary control flow, but all paths through the cleanup code must
2105 // eventually reach the next selector comparison and no path can skip to a
2106 // different selector comparisons, though some paths may terminate abnormally.
2107 // Therefore, we will use a depth first search from the start of any given
2108 // cleanup block and stop searching when we find the next selector comparison.
2109 //
2110 // If the landingpad instruction does not have a catch clause, we will assume
2111 // that any instructions other than selector comparisons and catch handlers can
2112 // be ignored. In practice, these will only be the boilerplate instructions.
2113 //
2114 // The catch handlers may also have any control structure, but we are only
2115 // interested in the start of the catch handlers, so we don't need to actually
2116 // follow the flow of the catch handlers. The start of the catch handlers can
2117 // be located from the compare instructions, but they can be skipped in the
2118 // flow by following the contrary branch.
2119 void WinEHPrepare::mapLandingPadBlocks(LandingPadInst *LPad,
2120 LandingPadActions &Actions) {
2121 unsigned int NumClauses = LPad->getNumClauses();
2122 unsigned int HandlersFound = 0;
2123 BasicBlock *BB = LPad->getParent();
2124
2125 DEBUG(dbgs() << "Mapping landing pad: " << BB->getName() << "\n");
2126
2127 if (NumClauses == 0) {
2128 findCleanupHandlers(Actions, BB, nullptr);
2129 return;
2130 }
2131
2132 VisitedBlockSet VisitedBlocks;
2133
2134 while (HandlersFound != NumClauses) {
2135 BasicBlock *NextBB = nullptr;
2136
2137 // Skip over filter clauses.
2138 if (LPad->isFilter(HandlersFound)) {
2139 ++HandlersFound;
2140 continue;
2141 }
2142
2143 // See if the clause we're looking for is a catch-all.
2144 // If so, the catch begins immediately.
2145 Constant *ExpectedSelector =
2146 LPad->getClause(HandlersFound)->stripPointerCasts();
2147 if (isa(ExpectedSelector)) {
2148 // The catch all must occur last.
2149 assert(HandlersFound == NumClauses - 1);
2150
2151 // There can be additional selector dispatches in the call chain that we
2152 // need to ignore.
2153 BasicBlock *CatchBlock = nullptr;
2154 Constant *Selector;
2155 while (BB && isSelectorDispatch(BB, CatchBlock, Selector, NextBB)) {
2156 DEBUG(dbgs() << " Found extra catch dispatch in block "
2157 << CatchBlock->getName() << "\n");
2158 BB = NextBB;
2159 }
2160
2161 // Add the catch handler to the action list.
2162 CatchHandler *Action = nullptr;
2163 if (CatchHandlerMap.count(BB) && CatchHandlerMap[BB] != nullptr) {
2164 // If the CatchHandlerMap already has an entry for this BB, re-use it.
2165 Action = CatchHandlerMap[BB];
2166 assert(Action->getSelector() == ExpectedSelector);
2167 } else {
2168 // We don't expect a selector dispatch, but there may be a call to
2169 // llvm.eh.begincatch, which separates catch handling code from
2170 // cleanup code in the same control flow. This call looks for the
2171 // begincatch intrinsic.
2172 Action = findCatchHandler(BB, NextBB, VisitedBlocks);
2173 if (Action) {
2174 // For C++ EH, check if there is any interesting cleanup code before
2175 // we begin the catch. This is important because cleanups cannot
2176 // rethrow exceptions but code called from catches can. For SEH, it
2177 // isn't important if some finally code before a catch-all is executed
2178 // out of line or after recovering from the exception.
2179 if (Personality == EHPersonality::MSVC_CXX)
2180 findCleanupHandlers(Actions, BB, BB);
2181 } else {
2182 // If an action was not found, it means that the control flows
2183 // directly into the catch-all handler and there is no cleanup code.
2184 // That's an expected situation and we must create a catch action.
2185 // Since this is a catch-all handler, the selector won't actually
2186 // appear in the code anywhere. ExpectedSelector here is the constant
2187 // null ptr that we got from the landing pad instruction.
2188 Action = new CatchHandler(BB, ExpectedSelector, nullptr);
2189 CatchHandlerMap[BB] = Action;
2190 }
2191 }
2192 Actions.insertCatchHandler(Action);
2193 DEBUG(dbgs() << " Catch all handler at block " << BB->getName() << "\n");
2194 ++HandlersFound;
2195
2196 // Once we reach a catch-all, don't expect to hit a resume instruction.
2197 BB = nullptr;
2198 break;
2199 }
2200
2201 CatchHandler *CatchAction = findCatchHandler(BB, NextBB, VisitedBlocks);
2202 assert(CatchAction);
2203
2204 // See if there is any interesting code executed before the dispatch.
2205 findCleanupHandlers(Actions, BB, CatchAction->getStartBlock());
2206
2207 // When the source program contains multiple nested try blocks the catch
2208 // handlers can get strung together in such a way that we can encounter
2209 // a dispatch for a selector that we've already had a handler for.
2210 if (CatchAction->getSelector()->stripPointerCasts() == ExpectedSelector) {
2211 ++HandlersFound;
2212
2213 // Add the catch handler to the action list.
2214 DEBUG(dbgs() << " Found catch dispatch in block "
2215 << CatchAction->getStartBlock()->getName() << "\n");
2216 Actions.insertCatchHandler(CatchAction);
2217 } else {
2218 // Under some circumstances optimized IR will flow unconditionally into a
2219 // handler block without checking the selector. This can only happen if
2220 // the landing pad has a catch-all handler and the handler for the
2221 // preceding catch clause is identical to the catch-call handler
2222 // (typically an empty catch). In this case, the handler must be shared
2223 // by all remaining clauses.
2224 if (isa(
2225 CatchAction->getSelector()->stripPointerCasts())) {
2226 DEBUG(dbgs() << " Applying early catch-all handler in block "
2227 << CatchAction->getStartBlock()->getName()
2228 << " to all remaining clauses.\n");
2229 Actions.insertCatchHandler(CatchAction);
2230 return;
2231 }
2232
2233 DEBUG(dbgs() << " Found extra catch dispatch in block "
2234 << CatchAction->getStartBlock()->getName() << "\n");
2235 }
2236
2237 // Move on to the block after the catch handler.
2238 BB = NextBB;
2239 }
2240
2241 // If we didn't wind up in a catch-all, see if there is any interesting code
2242 // executed before the resume.
2243 findCleanupHandlers(Actions, BB, BB);
2244
2245 // It's possible that some optimization moved code into a landingpad that
2246 // wasn't
2247 // previously being used for cleanup. If that happens, we need to execute
2248 // that
2249 // extra code from a cleanup handler.
2250 if (Actions.includesCleanup() && !LPad->isCleanup())
2251 LPad->setCleanup(true);
2252 }
2253
2254 // This function searches starting with the input block for the next
2255 // block that terminates with a branch whose condition is based on a selector
2256 // comparison. This may be the input block. See the mapLandingPadBlocks
2257 // comments for a discussion of control flow assumptions.
2258 //
2259 CatchHandler *WinEHPrepare::findCatchHandler(BasicBlock *BB,
2260 BasicBlock *&NextBB,
2261 VisitedBlockSet &VisitedBlocks) {
2262 // See if we've already found a catch handler use it.
2263 // Call count() first to avoid creating a null entry for blocks
2264 // we haven't seen before.
2265 if (CatchHandlerMap.count(BB) && CatchHandlerMap[BB] != nullptr) {
2266 CatchHandler *Action = cast(CatchHandlerMap[BB]);
2267 NextBB = Action->getNextBB();
2268 return Action;
2269 }
2270
2271 // VisitedBlocks applies only to the current search. We still
2272 // need to consider blocks that we've visited while mapping other
2273 // landing pads.
2274 VisitedBlocks.insert(BB);
2275
2276 BasicBlock *CatchBlock = nullptr;
2277 Constant *Selector = nullptr;
2278
2279 // If this is the first time we've visited this block from any landing pad
2280 // look to see if it is a selector dispatch block.
2281 if (!CatchHandlerMap.count(BB)) {
2282 if (isSelectorDispatch(BB, CatchBlock, Selector, NextBB)) {
2283 CatchHandler *Action = new CatchHandler(BB, Selector, NextBB);
2284 CatchHandlerMap[BB] = Action;
2285 return Action;
2286 }
2287 // If we encounter a block containing an llvm.eh.begincatch before we
2288 // find a selector dispatch block, the handler is assumed to be
2289 // reached unconditionally. This happens for catch-all blocks, but
2290 // it can also happen for other catch handlers that have been combined
2291 // with the catch-all handler during optimization.
2292 if (isCatchBlock(BB)) {
2293 PointerType *Int8PtrTy = Type::getInt8PtrTy(BB->getContext());
2294 Constant *NullSelector = ConstantPointerNull::get(Int8PtrTy);
2295 CatchHandler *Action = new CatchHandler(BB, NullSelector, nullptr);
2296 CatchHandlerMap[BB] = Action;
2297 return Action;
2298 }
2299 }
2300
2301 // Visit each successor, looking for the dispatch.
2302 // FIXME: We expect to find the dispatch quickly, so this will probably
2303 // work better as a breadth first search.
2304 for (BasicBlock *Succ : successors(BB)) {
2305 if (VisitedBlocks.count(Succ))
2306 continue;
2307
2308 CatchHandler *Action = findCatchHandler(Succ, NextBB, VisitedBlocks);
2309 if (Action)
2310 return Action;
2311 }
2312 return nullptr;
2313 }
2314
2315 // These are helper functions to combine repeated code from findCleanupHandlers.
2316 static void createCleanupHandler(LandingPadActions &Actions,
2317 CleanupHandlerMapTy &CleanupHandlerMap,
2318 BasicBlock *BB) {
2319 CleanupHandler *Action = new CleanupHandler(BB);
2320 CleanupHandlerMap[BB] = Action;
2321 Actions.insertCleanupHandler(Action);
2322 DEBUG(dbgs() << " Found cleanup code in block "
2323 << Action->getStartBlock()->getName() << "\n");
2324 }
2325
2326 static CallSite matchOutlinedFinallyCall(BasicBlock *BB,
2327 Instruction *MaybeCall) {
2328 // Look for finally blocks that Clang has already outlined for us.
2329 // %fp = call i8* @llvm.localaddress()
2330 // call void @"fin$parent"(iN 1, i8* %fp)
2331 if (isLocalAddressCall(MaybeCall) && MaybeCall != BB->getTerminator())
2332 MaybeCall = MaybeCall->getNextNode();
2333 CallSite FinallyCall(MaybeCall);
2334 if (!FinallyCall || FinallyCall.arg_size() != 2)
2335 return CallSite();
2336 if (!match(FinallyCall.getArgument(0), m_SpecificInt(1)))
2337 return CallSite();
2338 if (!isLocalAddressCall(FinallyCall.getArgument(1)))
2339 return CallSite();
2340 return FinallyCall;
2341 }
2342
2343 static BasicBlock *followSingleUnconditionalBranches(BasicBlock *BB) {
2344 // Skip single ubr blocks.
2345 while (BB->getFirstNonPHIOrDbg() == BB->getTerminator()) {
2346 auto *Br = dyn_cast(BB->getTerminator());
2347 if (Br && Br->isUnconditional())
2348 BB = Br->getSuccessor(0);
2349 else
2350 return BB;
2351 }
2352 return BB;
2353 }
2354
2355 // This function searches starting with the input block for the next block that
2356 // contains code that is not part of a catch handler and would not be eliminated
2357 // during handler outlining.
2358 //
2359 void WinEHPrepare::findCleanupHandlers(LandingPadActions &Actions,
2360 BasicBlock *StartBB, BasicBlock *EndBB) {
2361 // Here we will skip over the following:
2362 //
2363 // landing pad prolog:
2364 //
2365 // Unconditional branches
2366 //
2367 // Selector dispatch
2368 //
2369 // Resume pattern
2370 //
2371 // Anything else marks the start of an interesting block
2372
2373 BasicBlock *BB = StartBB;
2374 // Anything other than an unconditional branch will kick us out of this loop
2375 // one way or another.
2376 while (BB) {
2377 BB = followSingleUnconditionalBranches(BB);
2378 // If we've already scanned this block, don't scan it again. If it is
2379 // a cleanup block, there will be an action in the CleanupHandlerMap.
2380 // If we've scanned it and it is not a cleanup block, there will be a
2381 // nullptr in the CleanupHandlerMap. If we have not scanned it, there will
2382 // be no entry in the CleanupHandlerMap. We must call count() first to
2383 // avoid creating a null entry for blocks we haven't scanned.
2384 if (CleanupHandlerMap.count(BB)) {
2385 if (auto *Action = CleanupHandlerMap[BB]) {
2386 Actions.insertCleanupHandler(Action);
2387 DEBUG(dbgs() << " Found cleanup code in block "
2388 << Action->getStartBlock()->getName() << "\n");
2389 // FIXME: This cleanup might chain into another, and we need to discover
2390 // that.
2391 return;
2392 } else {
2393 // Here we handle the case where the cleanup handler map contains a
2394 // value for this block but the value is a nullptr. This means that
2395 // we have previously analyzed the block and determined that it did
2396 // not contain any cleanup code. Based on the earlier analysis, we
2397 // know the block must end in either an unconditional branch, a
2398 // resume or a conditional branch that is predicated on a comparison
2399 // with a selector. Either the resume or the selector dispatch
2400 // would terminate the search for cleanup code, so the unconditional
2401 // branch is the only case for which we might need to continue
2402 // searching.
2403 BasicBlock *SuccBB = followSingleUnconditionalBranches(BB);
2404 if (SuccBB == BB || SuccBB == EndBB)
2405 return;
2406 BB = SuccBB;
2407 continue;
2408 }
2409 }
2410
2411 // Create an entry in the cleanup handler map for this block. Initially
2412 // we create an entry that says this isn't a cleanup block. If we find
2413 // cleanup code, the caller will replace this entry.
2414 CleanupHandlerMap[BB] = nullptr;
2415
2416 TerminatorInst *Terminator = BB->getTerminator();
2417
2418 // Landing pad blocks have extra instructions we need to accept.
2419 LandingPadMap *LPadMap = nullptr;
2420 if (BB->isLandingPad()) {
2421 LandingPadInst *LPad = BB->getLandingPadInst();
2422 LPadMap = &LPadMaps[LPad];
2423 if (!LPadMap->isInitialized())
2424 LPadMap->mapLandingPad(LPad);
2425 }
2426
2427 // Look for the bare resume pattern:
2428 // %lpad.val1 = insertvalue { i8*, i32 } undef, i8* %exn, 0
2429 // %lpad.val2 = insertvalue { i8*, i32 } %lpad.val1, i32 %sel, 1
2430 // resume { i8*, i32 } %lpad.val2
2431 if (auto *Resume = dyn_cast(Terminator)) {
2432 InsertValueInst *Insert1 = nullptr;
2433 InsertValueInst *Insert2 = nullptr;
2434 Value *ResumeVal = Resume->getOperand(0);
2435 // If the resume value isn't a phi or landingpad value, it should be a
2436 // series of insertions. Identify them so we can avoid them when scanning
2437 // for cleanups.
2438 if (!isa(ResumeVal) && !isa(ResumeVal)) {
2439 Insert2 = dyn_cast(ResumeVal);
2440 if (!Insert2)
2441 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
2442 Insert1 = dyn_cast(Insert2->getAggregateOperand());
2443 if (!Insert1)
2444 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
2445 }
2446 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg()->getIterator(),
2447 IE = BB->end();
2448 II != IE; ++II) {
2449 Instruction *Inst = &*II;
2450 if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst))
2451 continue;
2452 if (Inst == Insert1 || Inst == Insert2 || Inst == Resume)
2453 continue;
2454 if (!Inst->hasOneUse() ||
2455 (Inst->user_back() != Insert1 && Inst->user_back() != Insert2)) {
2456 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
2457 }
2458 }
2459 return;
2460 }
2461
2462 BranchInst *Branch = dyn_cast(Terminator);
2463 if (Branch && Branch->isConditional()) {
2464 // Look for the selector dispatch.
2465 // %2 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIf to i8*))
2466 // %matches = icmp eq i32 %sel, %2
2467 // br i1 %matches, label %catch14, label %eh.resume
2468 CmpInst *Compare = dyn_cast(Branch->getCondition());
2469 if (!Compare || !Compare->isEquality())
2470 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
2471 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg()->getIterator(),
2472 IE = BB->end();
2473 II != IE; ++II) {
2474 Instruction *Inst = &*II;
2475 if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst))
2476 continue;
2477 if (Inst == Compare || Inst == Branch)
2478 continue;
2479 if (match(Inst, m_Intrinsic()))
2480 continue;
2481 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
2482 }
2483 // The selector dispatch block should always terminate our search.
2484 assert(BB == EndBB);
2485 return;
2486 }
2487
2488 if (isAsynchronousEHPersonality(Personality)) {
2489 // If this is a landingpad block, split the block at the first non-landing
2490 // pad instruction.
2491 Instruction *MaybeCall = BB->getFirstNonPHIOrDbg();
2492 if (LPadMap) {
2493 while (MaybeCall != BB->getTerminator() &&
2494 LPadMap->isLandingPadSpecificInst(MaybeCall))
2495 MaybeCall = MaybeCall->getNextNode();
2496 }
2497
2498 // Look for outlined finally calls on x64, since those happen to match the
2499 // prototype provided by the runtime.
2500 if (TheTriple.getArch() == Triple::x86_64) {
2501 if (CallSite FinallyCall = matchOutlinedFinallyCall(BB, MaybeCall)) {
2502 Function *Fin = FinallyCall.getCalledFunction();
2503 assert(Fin && "outlined finally call should be direct");
2504 auto *Action = new CleanupHandler(BB);
2505 Action->setHandlerBlockOrFunc(Fin);
2506 Actions.insertCleanupHandler(Action);
2507 CleanupHandlerMap[BB] = Action;
2508 DEBUG(dbgs() << " Found frontend-outlined finally call to "
2509 << Fin->getName() << " in block "
2510 << Action->getStartBlock()->getName() << "\n");
2511
2512 // Split the block if there were more interesting instructions and
2513 // look for finally calls in the normal successor block.
2514 BasicBlock *SuccBB = BB;
2515 if (FinallyCall.getInstruction() != BB->getTerminator() &&
2516 FinallyCall.getInstruction()->getNextNode() !=
2517 BB->getTerminator()) {
2518 SuccBB =
2519 SplitBlock(BB, FinallyCall.getInstruction()->getNextNode(), DT);
2520 } else {
2521 if (FinallyCall.isInvoke()) {
2522 SuccBB = cast(FinallyCall.getInstruction())
2523 ->getNormalDest();
2524 } else {
2525 SuccBB = BB->getUniqueSuccessor();
2526 assert(SuccBB &&
2527 "splitOutlinedFinallyCalls didn't insert a branch");
2528 }
2529 }
2530 BB = SuccBB;
2531 if (BB == EndBB)
2532 return;
2533 continue;
2534 }
2535 }
2536 }
2537
2538 // Anything else is either a catch block or interesting cleanup code.
2539 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg()->getIterator(),
2540 IE = BB->end();
2541 II != IE; ++II) {
2542 Instruction *Inst = &*II;
2543 if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst))
2544 continue;
2545 // Unconditional branches fall through to this loop.
2546 if (Inst == Branch)
2547 continue;
2548 // If this is a catch block, there is no cleanup code to be found.
2549 if (match(Inst, m_Intrinsic()))
2550 return;
2551 // If this a nested landing pad, it may contain an endcatch call.
2552 if (match(Inst, m_Intrinsic()))
2553 return;
2554 // Anything else makes this interesting cleanup code.
2555 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
2556 }
2557
2558 // Only unconditional branches in empty blocks should get this far.
2559 assert(Branch && Branch->isUnconditional());
2560 if (BB == EndBB)
2561 return;
2562 BB = Branch->getSuccessor(0);
2563 }
2564 }
2565
2566 // This is a public function, declared in WinEHFuncInfo.h and is also
2567 // referenced by WinEHNumbering in FunctionLoweringInfo.cpp.
2568 void llvm::parseEHActions(
2569 const IntrinsicInst *II,
2570 SmallVectorImpl> &Actions) {
2571 assert(II->getIntrinsicID() == Intrinsic::eh_actions &&
2572 "attempted to parse non eh.actions intrinsic");
2573 for (unsigned I = 0, E = II->getNumArgOperands(); I != E;) {
2574 uint64_t ActionKind =
2575 cast(II->getArgOperand(I))->getZExtValue();
2576 if (ActionKind == /*catch=*/1) {
2577 auto *Selector = cast(II->getArgOperand(I + 1));
2578 ConstantInt *EHObjIndex = cast(II->getArgOperand(I + 2));
2579 int64_t EHObjIndexVal = EHObjIndex->getSExtValue();
2580 Constant *Handler = cast(II->getArgOperand(I + 3));
2581 I += 4;
2582 auto CH = make_unique(/*BB=*/nullptr, Selector,
2583 /*NextBB=*/nullptr);
2584 CH->setHandlerBlockOrFunc(Handler);
2585 CH->setExceptionVarIndex(EHObjIndexVal);
2586 Actions.push_back(std::move(CH));
2587 } else if (ActionKind == 0) {
2588 Constant *Handler = cast(II->getArgOperand(I + 1));
2589 I += 2;
2590 auto CH = make_unique(/*BB=*/nullptr);
2591 CH->setHandlerBlockOrFunc(Handler);
2592 Actions.push_back(std::move(CH));
2593 } else {
2594 llvm_unreachable("Expected either a catch or cleanup handler!");
2595 }
2596 }
2597 std::reverse(Actions.begin(), Actions.end());
2598 }
2599
2600174 static int addUnwindMapEntry(WinEHFuncInfo &FuncInfo, int ToState,
2601175 const Value *V) {
2602 WinEHUnwindMapEntry UME;
176 CxxUnwindMapEntry UME;
2603177 UME.ToState = ToState;
2604178 UME.Cleanup = V;
2605 FuncInfo.UnwindMap.push_back(UME);
179 FuncInfo.CxxUnwindMap.push_back(UME);
2606180 return FuncInfo.getLastStateNumber();
2607181 }
2608182
3119693
3120694 void llvm::calculateCatchReturnSuccessorColors(const Function *Fn,
3121695 WinEHFuncInfo &FuncInfo) {
3122 SmallVector LPads;
3123 SmallVector Resumes;
3124696 SmallVector EntryBlocks;
3125697 // colorFunclets needs the set of EntryBlocks, get them using
3126 // findExceptionalConstructs.
3127 bool ForExplicitEH = findExceptionalConstructs(const_cast(*Fn),
3128 LPads, Resumes, EntryBlocks);
3129 if (!ForExplicitEH)
3130 return;
698 // findFuncletEntryPoints.
699 findFuncletEntryPoints(const_cast(*Fn), EntryBlocks);
3131700
3132701 std::map> BlockColors;
3133702 std::map> FuncletBlocks;
428428 }
429429
430430 void WinEHStatePass::addCXXStateStores(Function &F, WinEHFuncInfo &FuncInfo) {
431 calculateWinCXXEHStateNumbers(&F, FuncInfo);
432
433 // The base state for the parent is -1.
434 addStateStoresToFunclet(RegNode, FuncInfo, F, -1);
435
436431 // Set up RegNodeEscapeIndex
437432 int RegNodeEscapeIndex = escapeRegNode(F);
438433 FuncInfo.EHRegNodeEscapeIndex = RegNodeEscapeIndex;
434
435 calculateWinCXXEHStateNumbers(&F, FuncInfo);
436 addStateStoresToFunclet(RegNode, FuncInfo, F, -1);
437 }
438
439 /// Assign every distinct landingpad a unique state number for SEH. Unlike C++
440 /// EH, we can use this very simple algorithm while C++ EH cannot because catch
441 /// handlers aren't outlined and the runtime doesn't have to figure out which
442 /// catch handler frame to unwind to.
443 void WinEHStatePass::addSEHStateStores(Function &F, WinEHFuncInfo &FuncInfo) {
444 // Remember and return the index that we used. We save it in WinEHFuncInfo so
445 // that we can lower llvm.x86.seh.recoverfp later in filter functions without
446 // too much trouble.
447 int RegNodeEscapeIndex = escapeRegNode(F);
448 FuncInfo.EHRegNodeEscapeIndex = RegNodeEscapeIndex;
449
450 calculateSEHStateNumbers(&F, FuncInfo);
451 addStateStoresToFunclet(RegNode, FuncInfo, F, -1);
439452 }
440453
441454 /// Escape RegNode so that we can access it from child handlers. Find the call
502515 }
503516 }
504517
505 /// Assign every distinct landingpad a unique state number for SEH. Unlike C++
506 /// EH, we can use this very simple algorithm while C++ EH cannot because catch
507 /// handlers aren't outlined and the runtime doesn't have to figure out which
508 /// catch handler frame to unwind to.
509 /// FIXME: __finally blocks are outlined, so this approach may break down there.
510 void WinEHStatePass::addSEHStateStores(Function &F, WinEHFuncInfo &FuncInfo) {
511 // Remember and return the index that we used. We save it in WinEHFuncInfo so
512 // that we can lower llvm.x86.seh.recoverfp later in filter functions without
513 // too much trouble.
514 int RegNodeEscapeIndex = escapeRegNode(F);
515 FuncInfo.EHRegNodeEscapeIndex = RegNodeEscapeIndex;
516
517 // If this funciton uses the new EH IR, use the explicit state numbering
518 // algorithm and return early.
519 bool UsesLPads = false;
520 for (BasicBlock &BB : F) {
521 if (BB.isLandingPad()) {
522 UsesLPads = true;
523 break;
524 }
525 }
526 if (!UsesLPads) {
527 calculateSEHStateNumbers(&F, FuncInfo);
528 addStateStoresToFunclet(RegNode, FuncInfo, F, -1);
529 return;
530 }
531 // FIXME: Delete the rest of this code and clean things up when new EH is
532 // done.
533
534 // Iterate all the instructions and emit state number stores.
535 int CurState = 0;
536 SmallPtrSet ExceptBlocks;
537 for (BasicBlock &BB : F) {
538 for (auto I = BB.begin(), E = BB.end(); I != E; ++I) {
539 if (auto *CI = dyn_cast(I)) {
540 auto *Intrin = dyn_cast(CI);
541 if (Intrin) {
542 // Calls that "don't throw" are considered to be able to throw asynch
543 // exceptions, but intrinsics cannot.
544 continue;
545 }
546 insertStateNumberStore(RegNode, CI, -1);
547 } else if (auto *II = dyn_cast(I)) {
548 // Look up the state number of the landingpad this unwinds to.
549 LandingPadInst *LPI = II->getUnwindDest()->getLandingPadInst();
550 auto InsertionPair =
551 FuncInfo.EHPadStateMap.insert(std::make_pair(LPI, CurState));
552 auto Iter = InsertionPair.first;
553 int &State = Iter->second;
554 bool Inserted = InsertionPair.second;
555 if (Inserted) {
556 // Each action consumes a state number.
557 auto *EHActions = cast(LPI->getNextNode());
558 SmallVector, 4> ActionList;
559 parseEHActions(EHActions, ActionList);
560 assert(!ActionList.empty());
561 CurState += ActionList.size();
562 State += ActionList.size() - 1;
563
564 // Remember all the __except block targets.
565 for (auto &Handler : ActionList) {
566 if (auto *CH = dyn_cast(Handler.get())) {
567 auto *BA = cast(CH->getHandlerBlockOrFunc());
568 #ifndef NDEBUG
569 for (BasicBlock *Pred : predecessors(BA->getBasicBlock()))
570 assert(Pred->isLandingPad() &&
571 "WinEHPrepare failed to split block");
572 #endif
573 ExceptBlocks.insert(BA->getBasicBlock());
574 }
575 }
576 }
577 insertStateNumberStore(RegNode, II, State);
578 }
579 }
580 }
581
582 // Insert llvm.x86.seh.restoreframe() into each __except block.
583 Function *RestoreFrame =
584 Intrinsic::getDeclaration(TheModule, Intrinsic::x86_seh_restoreframe);
585 for (BasicBlock *ExceptBB : ExceptBlocks) {
586 IRBuilder<> Builder(ExceptBB->begin());
587 Builder.CreateCall(RestoreFrame, {});
588 }
589 }
590
591518 void WinEHStatePass::insertStateNumberStore(Value *ParentRegNode,
592519 Instruction *IP, int State) {
593520 IRBuilder<> Builder(IP);
+0
-278
test/Analysis/Lint/cppeh-catch-intrinsics.ll less more
None ; RUN: opt -lint -disable-output < %s 2>&1 | FileCheck %s
1
2 ; This test is meant to prove that the Verifier is able to identify a variety
3 ; of errors with the llvm.eh.begincatch and llvm.eh.endcatch intrinsics.
4 ; See cppeh-catch-intrinsics-clean for correct uses.
5
6 target triple = "x86_64-pc-windows-msvc"
7
8 declare void @llvm.eh.begincatch(i8*, i8*)
9
10 declare void @llvm.eh.endcatch()
11
12 @_ZTIi = external constant i8*
13
14 ; Function Attrs: uwtable
15 define void @test_missing_endcatch() personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
16 ; CHECK: Some paths from llvm.eh.begincatch may not reach llvm.eh.endcatch
17 ; CHECK-NEXT: call void @llvm.eh.begincatch(i8* %exn, i8* null)
18 entry:
19 invoke void @_Z9may_throwv()
20 to label %try.cont unwind label %lpad
21
22 lpad: ; preds = %entry
23 %0 = landingpad { i8*, i32 }
24 catch i8* bitcast (i8** @_ZTIi to i8*)
25 %exn = extractvalue { i8*, i32 } %0, 0
26 %sel = extractvalue { i8*, i32 } %0, 1
27 %1 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
28 %matches = icmp eq i32 %sel, %1
29 br i1 %matches, label %catch, label %eh.resume
30
31 catch: ; preds = %lpad
32 call void @llvm.eh.begincatch(i8* %exn, i8* null)
33 call void @_Z10handle_intv()
34 br label %invoke.cont2
35
36 invoke.cont2: ; preds = %catch
37 br label %try.cont
38
39 try.cont: ; preds = %invoke.cont2, %entry
40 ret void
41
42 eh.resume: ; preds = %catch.dispatch
43 resume { i8*, i32 } %0
44 }
45
46 ; Function Attrs: uwtable
47 define void @test_missing_begincatch() personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
48 ; CHECK: llvm.eh.endcatch may be reachable without passing llvm.eh.begincatch
49 ; CHECK-NEXT: call void @llvm.eh.endcatch()
50 entry:
51 invoke void @_Z9may_throwv()
52 to label %try.cont unwind label %lpad
53
54 lpad: ; preds = %entry
55 %0 = landingpad { i8*, i32 }
56 catch i8* bitcast (i8** @_ZTIi to i8*)
57 %exn = extractvalue { i8*, i32 } %0, 0
58 %sel = extractvalue { i8*, i32 } %0, 1
59 %1 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
60 %matches = icmp eq i32 %sel, %1
61 br i1 %matches, label %catch, label %eh.resume
62
63 catch: ; preds = %lpad
64 call void @_Z10handle_intv()
65 br label %invoke.cont2