//===----------------------------------------------------------------------===//
#include "llvm/CodeGen/Passes.h"
+#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/MapVector.h"
+#include "llvm/ADT/STLExtras.h"
#include "llvm/Analysis/CFG.h"
-#include "llvm/Analysis/LibCallSemantics.h"
+#include "llvm/Analysis/EHPersonalities.h"
+#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/WinEHFuncInfo.h"
+#include "llvm/IR/Verifier.h"
+#include "llvm/MC/MCSymbol.h"
#include "llvm/Pass.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
cl::init(false));
namespace {
-
+
class WinEHPrepare : public FunctionPass {
public:
static char ID; // Pass identification, replacement for typeid.
AllocaInst *insertPHILoads(PHINode *PN, Function &F);
void replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
DenseMap<BasicBlock *, Value *> &Loads, Function &F);
- void demoteNonlocalUses(Value *V, std::set<BasicBlock *> &ColorsForBB,
- Function &F);
- bool prepareExplicitEH(Function &F,
- SmallVectorImpl<BasicBlock *> &EntryBlocks);
- void replaceTerminatePadWithCleanup(Function &F);
- void colorFunclets(Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks);
+ bool prepareExplicitEH(Function &F);
+ void colorFunclets(Function &F);
+
void demotePHIsOnFunclets(Function &F);
- void demoteUsesBetweenFunclets(Function &F);
- void demoteArgumentUses(Function &F);
- void cloneCommonBlocks(Function &F,
- SmallVectorImpl<BasicBlock *> &EntryBlocks);
- void removeImplausibleTerminators(Function &F);
+ void cloneCommonBlocks(Function &F);
+ void removeImplausibleInstructions(Function &F);
void cleanupPreparedFunclets(Function &F);
void verifyPreparedFunclets(Function &F);
// All fields are reset by runOnFunction.
EHPersonality Personality = EHPersonality::Unknown;
- std::map<BasicBlock *, std::set<BasicBlock *>> BlockColors;
- std::map<BasicBlock *, std::set<BasicBlock *>> FuncletBlocks;
- std::map<BasicBlock *, std::set<BasicBlock *>> FuncletChildren;
+ DenseMap<BasicBlock *, ColorVector> BlockColors;
+ MapVector<BasicBlock *, std::vector<BasicBlock *>> FuncletBlocks;
};
} // end anonymous namespace
return new WinEHPrepare(TM);
}
-static void findFuncletEntryPoints(Function &Fn,
- SmallVectorImpl<BasicBlock *> &EntryBlocks) {
- EntryBlocks.push_back(&Fn.getEntryBlock());
- for (BasicBlock &BB : Fn) {
- Instruction *First = BB.getFirstNonPHI();
- if (!First->isEHPad())
- continue;
- assert(!isa<LandingPadInst>(First) &&
- "landingpad cannot be used with funclet EH personality");
- // Find EH pad blocks that represent funclet start points.
- if (!isa<CatchEndPadInst>(First) && !isa<CleanupEndPadInst>(First))
- EntryBlocks.push_back(&BB);
- }
-}
-
bool WinEHPrepare::runOnFunction(Function &Fn) {
if (!Fn.hasPersonalityFn())
return false;
if (!isFuncletEHPersonality(Personality))
return false;
- // Remove unreachable blocks. It is not valuable to assign them a color and
- // their existence can trick us into thinking values are alive when they are
- // not.
- removeUnreachableBlocks(Fn);
-
- SmallVector<BasicBlock *, 4> EntryBlocks;
- findFuncletEntryPoints(Fn, EntryBlocks);
- return prepareExplicitEH(Fn, EntryBlocks);
+ return prepareExplicitEH(Fn);
}
bool WinEHPrepare::doFinalization(Module &M) { return false; }
HT.TypeDescriptor = cast<GlobalVariable>(TypeInfo->stripPointerCasts());
HT.Adjectives = cast<ConstantInt>(CPI->getArgOperand(1))->getZExtValue();
HT.Handler = CPI->getParent();
- if (isa<ConstantPointerNull>(CPI->getArgOperand(2)))
- HT.CatchObj.Alloca = nullptr;
+ if (auto *AI =
+ dyn_cast<AllocaInst>(CPI->getArgOperand(2)->stripPointerCasts()))
+ HT.CatchObj.Alloca = AI;
else
- HT.CatchObj.Alloca = cast<AllocaInst>(CPI->getArgOperand(2));
+ HT.CatchObj.Alloca = nullptr;
TBME.HandlerArray.push_back(HT);
}
FuncInfo.TryBlockMap.push_back(TBME);
}
-static const CatchPadInst *getSingleCatchPadPredecessor(const BasicBlock *BB) {
- for (const BasicBlock *PredBlock : predecessors(BB))
- if (auto *CPI = dyn_cast<CatchPadInst>(PredBlock->getFirstNonPHI()))
- return CPI;
+static BasicBlock *getCleanupRetUnwindDest(const CleanupPadInst *CleanupPad) {
+ for (const User *U : CleanupPad->users())
+ if (const auto *CRI = dyn_cast<CleanupReturnInst>(U))
+ return CRI->getUnwindDest();
return nullptr;
}
-/// Find all the catchpads that feed directly into the catchendpad. Frontends
-/// using this personality should ensure that each catchendpad and catchpad has
-/// one or zero catchpad predecessors.
-///
-/// The following C++ generates the IR after it:
-/// try {
-/// } catch (A) {
-/// } catch (B) {
-/// }
-///
-/// IR:
-/// %catchpad.A
-/// catchpad [i8* A typeinfo]
-/// to label %catch.A unwind label %catchpad.B
-/// %catchpad.B
-/// catchpad [i8* B typeinfo]
-/// to label %catch.B unwind label %endcatches
-/// %endcatches
-/// catchendblock unwind to caller
-static void
-findCatchPadsForCatchEndPad(const BasicBlock *CatchEndBB,
- SmallVectorImpl<const CatchPadInst *> &Handlers) {
- const CatchPadInst *CPI = getSingleCatchPadPredecessor(CatchEndBB);
- while (CPI) {
- Handlers.push_back(CPI);
- CPI = getSingleCatchPadPredecessor(CPI->getParent());
+static void calculateStateNumbersForInvokes(const Function *Fn,
+ WinEHFuncInfo &FuncInfo) {
+ auto *F = const_cast<Function *>(Fn);
+ DenseMap<BasicBlock *, ColorVector> BlockColors = colorEHFunclets(*F);
+ for (BasicBlock &BB : *F) {
+ auto *II = dyn_cast<InvokeInst>(BB.getTerminator());
+ if (!II)
+ continue;
+
+ auto &BBColors = BlockColors[&BB];
+ assert(BBColors.size() == 1 && "multi-color BB not removed by preparation");
+ BasicBlock *FuncletEntryBB = BBColors.front();
+
+ BasicBlock *FuncletUnwindDest;
+ auto *FuncletPad =
+ dyn_cast<FuncletPadInst>(FuncletEntryBB->getFirstNonPHI());
+ assert(FuncletPad || FuncletEntryBB == &Fn->getEntryBlock());
+ if (!FuncletPad)
+ FuncletUnwindDest = nullptr;
+ else if (auto *CatchPad = dyn_cast<CatchPadInst>(FuncletPad))
+ FuncletUnwindDest = CatchPad->getCatchSwitch()->getUnwindDest();
+ else if (auto *CleanupPad = dyn_cast<CleanupPadInst>(FuncletPad))
+ FuncletUnwindDest = getCleanupRetUnwindDest(CleanupPad);
+ else
+ llvm_unreachable("unexpected funclet pad!");
+
+ BasicBlock *InvokeUnwindDest = II->getUnwindDest();
+ int BaseState = -1;
+ if (FuncletUnwindDest == InvokeUnwindDest) {
+ auto BaseStateI = FuncInfo.FuncletBaseStateMap.find(FuncletPad);
+ if (BaseStateI != FuncInfo.FuncletBaseStateMap.end())
+ BaseState = BaseStateI->second;
+ }
+
+ if (BaseState != -1) {
+ FuncInfo.InvokeStateMap[II] = BaseState;
+ } else {
+ Instruction *PadInst = InvokeUnwindDest->getFirstNonPHI();
+ assert(FuncInfo.EHPadStateMap.count(PadInst) && "EH Pad has no state!");
+ FuncInfo.InvokeStateMap[II] = FuncInfo.EHPadStateMap[PadInst];
+ }
}
- // We've pushed these back into reverse source order. Reverse them to get
- // the list back into source order.
- std::reverse(Handlers.begin(), Handlers.end());
}
// Given BB which ends in an unwind edge, return the EHPad that this BB belongs
// to. If the unwind edge came from an invoke, return null.
-static const BasicBlock *getEHPadFromPredecessor(const BasicBlock *BB) {
+static const BasicBlock *getEHPadFromPredecessor(const BasicBlock *BB,
+ Value *ParentPad) {
const TerminatorInst *TI = BB->getTerminator();
if (isa<InvokeInst>(TI))
return nullptr;
- if (TI->isEHPad())
+ if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(TI)) {
+ if (CatchSwitch->getParentPad() != ParentPad)
+ return nullptr;
return BB;
- return cast<CleanupReturnInst>(TI)->getCleanupPad()->getParent();
+ }
+ assert(!TI->isEHPad() && "unexpected EHPad!");
+ auto *CleanupPad = cast<CleanupReturnInst>(TI)->getCleanupPad();
+ if (CleanupPad->getParentPad() != ParentPad)
+ return nullptr;
+ return CleanupPad->getParent();
}
-static void calculateExplicitCXXStateNumbers(WinEHFuncInfo &FuncInfo,
- const BasicBlock &BB,
- int ParentState) {
- assert(BB.isEHPad());
- const Instruction *FirstNonPHI = BB.getFirstNonPHI();
- // All catchpad instructions will be handled when we process their
- // respective catchendpad instruction.
- if (isa<CatchPadInst>(FirstNonPHI))
- return;
+static void calculateCXXStateNumbers(WinEHFuncInfo &FuncInfo,
+ const Instruction *FirstNonPHI,
+ int ParentState) {
+ const BasicBlock *BB = FirstNonPHI->getParent();
+ assert(BB->isEHPad() && "not a funclet!");
+
+ if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(FirstNonPHI)) {
+ assert(FuncInfo.EHPadStateMap.count(CatchSwitch) == 0 &&
+ "shouldn't revist catch funclets!");
- if (isa<CatchEndPadInst>(FirstNonPHI)) {
SmallVector<const CatchPadInst *, 2> Handlers;
- findCatchPadsForCatchEndPad(&BB, Handlers);
- const BasicBlock *FirstTryPad = Handlers.front()->getParent();
+ for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
+ auto *CatchPad = cast<CatchPadInst>(CatchPadBB->getFirstNonPHI());
+ Handlers.push_back(CatchPad);
+ }
int TryLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr);
- FuncInfo.EHPadStateMap[Handlers.front()] = TryLow;
- for (const BasicBlock *PredBlock : predecessors(FirstTryPad))
- if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
- calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, TryLow);
+ FuncInfo.EHPadStateMap[CatchSwitch] = TryLow;
+ for (const BasicBlock *PredBlock : predecessors(BB))
+ if ((PredBlock = getEHPadFromPredecessor(PredBlock,
+ CatchSwitch->getParentPad())))
+ calculateCXXStateNumbers(FuncInfo, PredBlock->getFirstNonPHI(),
+ TryLow);
int CatchLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr);
// catchpads are separate funclets in C++ EH due to the way rethrow works.
- // In SEH, they aren't, so no invokes will unwind to the catchendpad.
- FuncInfo.EHPadStateMap[FirstNonPHI] = CatchLow;
int TryHigh = CatchLow - 1;
- for (const BasicBlock *PredBlock : predecessors(&BB))
- if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
- calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, CatchLow);
+ for (const auto *CatchPad : Handlers) {
+ FuncInfo.FuncletBaseStateMap[CatchPad] = CatchLow;
+ for (const User *U : CatchPad->users()) {
+ const auto *UserI = cast<Instruction>(U);
+ if (auto *InnerCatchSwitch = dyn_cast<CatchSwitchInst>(UserI))
+ if (InnerCatchSwitch->getUnwindDest() == CatchSwitch->getUnwindDest())
+ calculateCXXStateNumbers(FuncInfo, UserI, CatchLow);
+ if (auto *InnerCleanupPad = dyn_cast<CleanupPadInst>(UserI))
+ if (getCleanupRetUnwindDest(InnerCleanupPad) ==
+ CatchSwitch->getUnwindDest())
+ calculateCXXStateNumbers(FuncInfo, UserI, CatchLow);
+ }
+ }
int CatchHigh = FuncInfo.getLastStateNumber();
addTryBlockMapEntry(FuncInfo, TryLow, TryHigh, CatchHigh, Handlers);
- DEBUG(dbgs() << "TryLow[" << FirstTryPad->getName() << "]: " << TryLow
- << '\n');
- DEBUG(dbgs() << "TryHigh[" << FirstTryPad->getName() << "]: " << TryHigh
- << '\n');
- DEBUG(dbgs() << "CatchHigh[" << FirstTryPad->getName() << "]: " << CatchHigh
+ DEBUG(dbgs() << "TryLow[" << BB->getName() << "]: " << TryLow << '\n');
+ DEBUG(dbgs() << "TryHigh[" << BB->getName() << "]: " << TryHigh << '\n');
+ DEBUG(dbgs() << "CatchHigh[" << BB->getName() << "]: " << CatchHigh
<< '\n');
- } else if (isa<CleanupPadInst>(FirstNonPHI)) {
- // A cleanup can have multiple exits; don't re-process after the first.
- if (FuncInfo.EHPadStateMap.count(FirstNonPHI))
+ } else {
+ auto *CleanupPad = cast<CleanupPadInst>(FirstNonPHI);
+
+ // It's possible for a cleanup to be visited twice: it might have multiple
+ // cleanupret instructions.
+ if (FuncInfo.EHPadStateMap.count(CleanupPad))
return;
- int CleanupState = addUnwindMapEntry(FuncInfo, ParentState, &BB);
- FuncInfo.EHPadStateMap[FirstNonPHI] = CleanupState;
+
+ int CleanupState = addUnwindMapEntry(FuncInfo, ParentState, BB);
+ FuncInfo.EHPadStateMap[CleanupPad] = CleanupState;
DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB "
- << BB.getName() << '\n');
- for (const BasicBlock *PredBlock : predecessors(&BB))
- if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
- calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, CleanupState);
- } else if (auto *CEPI = dyn_cast<CleanupEndPadInst>(FirstNonPHI)) {
- // Propagate ParentState to the cleanuppad in case it doesn't have
- // any cleanuprets.
- BasicBlock *CleanupBlock = CEPI->getCleanupPad()->getParent();
- calculateExplicitCXXStateNumbers(FuncInfo, *CleanupBlock, ParentState);
- // Anything unwinding through CleanupEndPadInst is in ParentState.
- FuncInfo.EHPadStateMap[FirstNonPHI] = ParentState;
- for (const BasicBlock *PredBlock : predecessors(&BB))
- if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
- calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, ParentState);
- } else if (isa<TerminatePadInst>(FirstNonPHI)) {
- report_fatal_error("Not yet implemented!");
- } else {
- llvm_unreachable("unexpected EH Pad!");
+ << BB->getName() << '\n');
+ for (const BasicBlock *PredBlock : predecessors(BB)) {
+ if ((PredBlock = getEHPadFromPredecessor(PredBlock,
+ CleanupPad->getParentPad()))) {
+ calculateCXXStateNumbers(FuncInfo, PredBlock->getFirstNonPHI(),
+ CleanupState);
+ }
+ }
+ for (const User *U : CleanupPad->users()) {
+ const auto *UserI = cast<Instruction>(U);
+ if (UserI->isEHPad())
+ report_fatal_error("Cleanup funclets for the MSVC++ personality cannot "
+ "contain exceptional actions");
+ }
}
}
return FuncInfo.SEHUnwindMap.size() - 1;
}
-static void calculateExplicitSEHStateNumbers(WinEHFuncInfo &FuncInfo,
- const BasicBlock &BB,
- int ParentState) {
- assert(BB.isEHPad());
- const Instruction *FirstNonPHI = BB.getFirstNonPHI();
- // All catchpad instructions will be handled when we process their
- // respective catchendpad instruction.
- if (isa<CatchPadInst>(FirstNonPHI))
- return;
+static void calculateSEHStateNumbers(WinEHFuncInfo &FuncInfo,
+ const Instruction *FirstNonPHI,
+ int ParentState) {
+ const BasicBlock *BB = FirstNonPHI->getParent();
+ assert(BB->isEHPad() && "no a funclet!");
+
+ if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(FirstNonPHI)) {
+ assert(FuncInfo.EHPadStateMap.count(CatchSwitch) == 0 &&
+ "shouldn't revist catch funclets!");
- if (isa<CatchEndPadInst>(FirstNonPHI)) {
// Extract the filter function and the __except basic block and create a
// state for them.
- SmallVector<const CatchPadInst *, 1> Handlers;
- findCatchPadsForCatchEndPad(&BB, Handlers);
- assert(Handlers.size() == 1 &&
+ assert(CatchSwitch->getNumHandlers() == 1 &&
"SEH doesn't have multiple handlers per __try");
- const CatchPadInst *CPI = Handlers.front();
- const BasicBlock *CatchPadBB = CPI->getParent();
+ const auto *CatchPad =
+ cast<CatchPadInst>((*CatchSwitch->handler_begin())->getFirstNonPHI());
+ const BasicBlock *CatchPadBB = CatchPad->getParent();
const Constant *FilterOrNull =
- cast<Constant>(CPI->getArgOperand(0)->stripPointerCasts());
+ cast<Constant>(CatchPad->getArgOperand(0)->stripPointerCasts());
const Function *Filter = dyn_cast<Function>(FilterOrNull);
assert((Filter || FilterOrNull->isNullValue()) &&
"unexpected filter value");
int TryState = addSEHExcept(FuncInfo, ParentState, Filter, CatchPadBB);
// Everything in the __try block uses TryState as its parent state.
- FuncInfo.EHPadStateMap[CPI] = TryState;
+ FuncInfo.EHPadStateMap[CatchSwitch] = TryState;
DEBUG(dbgs() << "Assigning state #" << TryState << " to BB "
<< CatchPadBB->getName() << '\n');
- for (const BasicBlock *PredBlock : predecessors(CatchPadBB))
- if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
- calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, TryState);
+ for (const BasicBlock *PredBlock : predecessors(BB))
+ if ((PredBlock = getEHPadFromPredecessor(PredBlock,
+ CatchSwitch->getParentPad())))
+ calculateSEHStateNumbers(FuncInfo, PredBlock->getFirstNonPHI(),
+ TryState);
// Everything in the __except block unwinds to ParentState, just like code
// outside the __try.
- FuncInfo.EHPadStateMap[FirstNonPHI] = ParentState;
- DEBUG(dbgs() << "Assigning state #" << ParentState << " to BB "
- << BB.getName() << '\n');
- for (const BasicBlock *PredBlock : predecessors(&BB))
- if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
- calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, ParentState);
- } else if (isa<CleanupPadInst>(FirstNonPHI)) {
- // A cleanup can have multiple exits; don't re-process after the first.
- if (FuncInfo.EHPadStateMap.count(FirstNonPHI))
+ for (const User *U : CatchPad->users()) {
+ const auto *UserI = cast<Instruction>(U);
+ if (auto *InnerCatchSwitch = dyn_cast<CatchSwitchInst>(UserI))
+ if (InnerCatchSwitch->getUnwindDest() == CatchSwitch->getUnwindDest())
+ calculateSEHStateNumbers(FuncInfo, UserI, ParentState);
+ if (auto *InnerCleanupPad = dyn_cast<CleanupPadInst>(UserI))
+ if (getCleanupRetUnwindDest(InnerCleanupPad) ==
+ CatchSwitch->getUnwindDest())
+ calculateSEHStateNumbers(FuncInfo, UserI, ParentState);
+ }
+ } else {
+ auto *CleanupPad = cast<CleanupPadInst>(FirstNonPHI);
+
+ // It's possible for a cleanup to be visited twice: it might have multiple
+ // cleanupret instructions.
+ if (FuncInfo.EHPadStateMap.count(CleanupPad))
return;
- int CleanupState = addSEHFinally(FuncInfo, ParentState, &BB);
- FuncInfo.EHPadStateMap[FirstNonPHI] = CleanupState;
+
+ int CleanupState = addSEHFinally(FuncInfo, ParentState, BB);
+ FuncInfo.EHPadStateMap[CleanupPad] = CleanupState;
DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB "
- << BB.getName() << '\n');
- for (const BasicBlock *PredBlock : predecessors(&BB))
- if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
- calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, CleanupState);
- } else if (auto *CEPI = dyn_cast<CleanupEndPadInst>(FirstNonPHI)) {
- // Propagate ParentState to the cleanuppad in case it doesn't have
- // any cleanuprets.
- BasicBlock *CleanupBlock = CEPI->getCleanupPad()->getParent();
- calculateExplicitSEHStateNumbers(FuncInfo, *CleanupBlock, ParentState);
- // Anything unwinding through CleanupEndPadInst is in ParentState.
- FuncInfo.EHPadStateMap[FirstNonPHI] = ParentState;
- DEBUG(dbgs() << "Assigning state #" << ParentState << " to BB "
- << BB.getName() << '\n');
- for (const BasicBlock *PredBlock : predecessors(&BB))
- if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
- calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, ParentState);
- } else if (isa<TerminatePadInst>(FirstNonPHI)) {
- report_fatal_error("Not yet implemented!");
- } else {
- llvm_unreachable("unexpected EH Pad!");
+ << BB->getName() << '\n');
+ for (const BasicBlock *PredBlock : predecessors(BB))
+ if ((PredBlock =
+ getEHPadFromPredecessor(PredBlock, CleanupPad->getParentPad())))
+ calculateSEHStateNumbers(FuncInfo, PredBlock->getFirstNonPHI(),
+ CleanupState);
+ for (const User *U : CleanupPad->users()) {
+ const auto *UserI = cast<Instruction>(U);
+ if (UserI->isEHPad())
+ report_fatal_error("Cleanup funclets for the SEH personality cannot "
+ "contain exceptional actions");
+ }
}
}
-/// Check if the EH Pad unwinds to caller. Cleanups are a little bit of a
-/// special case because we have to look at the cleanupret instruction that uses
-/// the cleanuppad.
-static bool doesEHPadUnwindToCaller(const Instruction *EHPad) {
- auto *CPI = dyn_cast<CleanupPadInst>(EHPad);
- if (!CPI)
- return EHPad->mayThrow();
-
- // This cleanup does not return or unwind, so we say it unwinds to caller.
- if (CPI->use_empty())
- return true;
-
- const Instruction *User = CPI->user_back();
- if (auto *CRI = dyn_cast<CleanupReturnInst>(User))
- return CRI->unwindsToCaller();
- return cast<CleanupEndPadInst>(User)->unwindsToCaller();
+static bool isTopLevelPadForMSVC(const Instruction *EHPad) {
+ if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(EHPad))
+ return isa<ConstantTokenNone>(CatchSwitch->getParentPad()) &&
+ CatchSwitch->unwindsToCaller();
+ if (auto *CleanupPad = dyn_cast<CleanupPadInst>(EHPad))
+ return isa<ConstantTokenNone>(CleanupPad->getParentPad()) &&
+ getCleanupRetUnwindDest(CleanupPad) == nullptr;
+ if (isa<CatchPadInst>(EHPad))
+ return false;
+ llvm_unreachable("unexpected EHPad!");
}
void llvm::calculateSEHStateNumbers(const Function *Fn,
return;
for (const BasicBlock &BB : *Fn) {
- if (!BB.isEHPad() || !doesEHPadUnwindToCaller(BB.getFirstNonPHI()))
+ if (!BB.isEHPad())
+ continue;
+ const Instruction *FirstNonPHI = BB.getFirstNonPHI();
+ if (!isTopLevelPadForMSVC(FirstNonPHI))
continue;
- calculateExplicitSEHStateNumbers(FuncInfo, BB, -1);
+ ::calculateSEHStateNumbers(FuncInfo, FirstNonPHI, -1);
}
+
+ calculateStateNumbersForInvokes(Fn, FuncInfo);
}
void llvm::calculateWinCXXEHStateNumbers(const Function *Fn,
for (const BasicBlock &BB : *Fn) {
if (!BB.isEHPad())
continue;
- if (BB.isLandingPad())
- report_fatal_error("MSVC C++ EH cannot use landingpads");
const Instruction *FirstNonPHI = BB.getFirstNonPHI();
- if (!doesEHPadUnwindToCaller(FirstNonPHI))
+ if (!isTopLevelPadForMSVC(FirstNonPHI))
continue;
- calculateExplicitCXXStateNumbers(FuncInfo, BB, -1);
+ calculateCXXStateNumbers(FuncInfo, FirstNonPHI, -1);
}
+
+ calculateStateNumbersForInvokes(Fn, FuncInfo);
}
-static int addClrEHHandler(WinEHFuncInfo &FuncInfo, int ParentState,
- ClrHandlerType HandlerType, uint32_t TypeToken,
- const BasicBlock *Handler) {
+static int addClrEHHandler(WinEHFuncInfo &FuncInfo, int HandlerParentState,
+ int TryParentState, ClrHandlerType HandlerType,
+ uint32_t TypeToken, const BasicBlock *Handler) {
ClrEHUnwindMapEntry Entry;
- Entry.Parent = ParentState;
+ Entry.HandlerParentState = HandlerParentState;
+ Entry.TryParentState = TryParentState;
Entry.Handler = Handler;
Entry.HandlerType = HandlerType;
Entry.TypeToken = TypeToken;
if (!FuncInfo.EHPadStateMap.empty())
return;
+ // This numbering assigns one state number to each catchpad and cleanuppad.
+ // It also computes two tree-like relations over states:
+ // 1) Each state has a "HandlerParentState", which is the state of the next
+ // outer handler enclosing this state's handler (same as nearest ancestor
+ // per the ParentPad linkage on EH pads, but skipping over catchswitches).
+ // 2) Each state has a "TryParentState", which:
+ // a) for a catchpad that's not the last handler on its catchswitch, is
+ // the state of the next catchpad on that catchswitch
+ // b) for all other pads, is the state of the pad whose try region is the
+ // next outer try region enclosing this state's try region. The "try
+ // regions are not present as such in the IR, but will be inferred
+ // based on the placement of invokes and pads which reach each other
+ // by exceptional exits
+ // Catchswitches do not get their own states, but each gets mapped to the
+ // state of its first catchpad.
+
+ // Step one: walk down from outermost to innermost funclets, assigning each
+ // catchpad and cleanuppad a state number. Add an entry to the
+ // ClrEHUnwindMap for each state, recording its HandlerParentState and
+ // handler attributes. Record the TryParentState as well for each catchpad
+ // that's not the last on its catchswitch, but initialize all other entries'
+ // TryParentStates to a sentinel -1 value that the next pass will update.
+
+ // Seed a worklist with pads that have no parent.
SmallVector<std::pair<const Instruction *, int>, 8> Worklist;
-
- // Each pad needs to be able to refer to its parent, so scan the function
- // looking for top-level handlers and seed the worklist with them.
for (const BasicBlock &BB : *Fn) {
- if (!BB.isEHPad())
- continue;
- if (BB.isLandingPad())
- report_fatal_error("CoreCLR EH cannot use landingpads");
const Instruction *FirstNonPHI = BB.getFirstNonPHI();
- if (!doesEHPadUnwindToCaller(FirstNonPHI))
+ const Value *ParentPad;
+ if (const auto *CPI = dyn_cast<CleanupPadInst>(FirstNonPHI))
+ ParentPad = CPI->getParentPad();
+ else if (const auto *CSI = dyn_cast<CatchSwitchInst>(FirstNonPHI))
+ ParentPad = CSI->getParentPad();
+ else
continue;
- // queue this with sentinel parent state -1 to mean unwind to caller.
- Worklist.emplace_back(FirstNonPHI, -1);
+ if (isa<ConstantTokenNone>(ParentPad))
+ Worklist.emplace_back(FirstNonPHI, -1);
}
+ // Use the worklist to visit all pads, from outer to inner. Record
+ // HandlerParentState for all pads. Record TryParentState only for catchpads
+ // that aren't the last on their catchswitch (setting all other entries'
+ // TryParentStates to an initial value of -1). This loop is also responsible
+ // for setting the EHPadStateMap entry for all catchpads, cleanuppads, and
+ // catchswitches.
while (!Worklist.empty()) {
const Instruction *Pad;
- int ParentState;
- std::tie(Pad, ParentState) = Worklist.pop_back_val();
-
- int PredState;
- if (const CleanupEndPadInst *EndPad = dyn_cast<CleanupEndPadInst>(Pad)) {
- FuncInfo.EHPadStateMap[EndPad] = ParentState;
- // Queue the cleanuppad, in case it doesn't have a cleanupret.
- Worklist.emplace_back(EndPad->getCleanupPad(), ParentState);
- // Preds of the endpad should get the parent state.
- PredState = ParentState;
- } else if (const CleanupPadInst *Cleanup = dyn_cast<CleanupPadInst>(Pad)) {
- // A cleanup can have multiple exits; don't re-process after the first.
- if (FuncInfo.EHPadStateMap.count(Pad))
- continue;
- // CoreCLR personality uses arity to distinguish faults from finallies.
- const BasicBlock *PadBlock = Cleanup->getParent();
+ int HandlerParentState;
+ std::tie(Pad, HandlerParentState) = Worklist.pop_back_val();
+
+ if (const auto *Cleanup = dyn_cast<CleanupPadInst>(Pad)) {
+ // Create the entry for this cleanup with the appropriate handler
+ // properties. Finaly and fault handlers are distinguished by arity.
ClrHandlerType HandlerType =
- (Cleanup->getNumOperands() ? ClrHandlerType::Fault
- : ClrHandlerType::Finally);
- int NewState =
- addClrEHHandler(FuncInfo, ParentState, HandlerType, 0, PadBlock);
- FuncInfo.EHPadStateMap[Cleanup] = NewState;
- // Propagate the new state to all preds of the cleanup
- PredState = NewState;
- } else if (const CatchEndPadInst *EndPad = dyn_cast<CatchEndPadInst>(Pad)) {
- FuncInfo.EHPadStateMap[EndPad] = ParentState;
- // Preds of the endpad should get the parent state.
- PredState = ParentState;
- } else if (const CatchPadInst *Catch = dyn_cast<CatchPadInst>(Pad)) {
- const BasicBlock *PadBlock = Catch->getParent();
- uint32_t TypeToken = static_cast<uint32_t>(
- cast<ConstantInt>(Catch->getArgOperand(0))->getZExtValue());
- int NewState = addClrEHHandler(FuncInfo, ParentState,
- ClrHandlerType::Catch, TypeToken, PadBlock);
- FuncInfo.EHPadStateMap[Catch] = NewState;
- // Preds of the catch get its state
- PredState = NewState;
+ (Cleanup->getNumArgOperands() ? ClrHandlerType::Fault
+ : ClrHandlerType::Finally);
+ int CleanupState = addClrEHHandler(FuncInfo, HandlerParentState, -1,
+ HandlerType, 0, Pad->getParent());
+ // Queue any child EH pads on the worklist.
+ for (const User *U : Cleanup->users())
+ if (const auto *I = dyn_cast<Instruction>(U))
+ if (I->isEHPad())
+ Worklist.emplace_back(I, CleanupState);
+ // Remember this pad's state.
+ FuncInfo.EHPadStateMap[Cleanup] = CleanupState;
} else {
- llvm_unreachable("Unexpected EH pad");
- }
-
- // Queue all predecessors with the given state
- for (const BasicBlock *Pred : predecessors(Pad->getParent())) {
- if ((Pred = getEHPadFromPredecessor(Pred)))
- Worklist.emplace_back(Pred->getFirstNonPHI(), PredState);
+ // Walk the handlers of this catchswitch in reverse order since all but
+ // the last need to set the following one as its TryParentState.
+ const auto *CatchSwitch = cast<CatchSwitchInst>(Pad);
+ int CatchState = -1, FollowerState = -1;
+ SmallVector<const BasicBlock *, 4> CatchBlocks(CatchSwitch->handlers());
+ for (auto CBI = CatchBlocks.rbegin(), CBE = CatchBlocks.rend();
+ CBI != CBE; ++CBI, FollowerState = CatchState) {
+ const BasicBlock *CatchBlock = *CBI;
+ // Create the entry for this catch with the appropriate handler
+ // properties.
+ const auto *Catch = cast<CatchPadInst>(CatchBlock->getFirstNonPHI());
+ uint32_t TypeToken = static_cast<uint32_t>(
+ cast<ConstantInt>(Catch->getArgOperand(0))->getZExtValue());
+ CatchState =
+ addClrEHHandler(FuncInfo, HandlerParentState, FollowerState,
+ ClrHandlerType::Catch, TypeToken, CatchBlock);
+ // Queue any child EH pads on the worklist.
+ for (const User *U : Catch->users())
+ if (const auto *I = dyn_cast<Instruction>(U))
+ if (I->isEHPad())
+ Worklist.emplace_back(I, CatchState);
+ // Remember this catch's state.
+ FuncInfo.EHPadStateMap[Catch] = CatchState;
+ }
+ // Associate the catchswitch with the state of its first catch.
+ assert(CatchSwitch->getNumHandlers());
+ FuncInfo.EHPadStateMap[CatchSwitch] = CatchState;
}
}
-}
-
-void WinEHPrepare::replaceTerminatePadWithCleanup(Function &F) {
- if (Personality != EHPersonality::MSVC_CXX)
- return;
- for (BasicBlock &BB : F) {
- Instruction *First = BB.getFirstNonPHI();
- auto *TPI = dyn_cast<TerminatePadInst>(First);
- if (!TPI)
- continue;
-
- if (TPI->getNumArgOperands() != 1)
- report_fatal_error(
- "Expected a unary terminatepad for MSVC C++ personalities!");
-
- auto *TerminateFn = dyn_cast<Function>(TPI->getArgOperand(0));
- if (!TerminateFn)
- report_fatal_error("Function operand expected in terminatepad for MSVC "
- "C++ personalities!");
-
- // Insert the cleanuppad instruction.
- auto *CPI = CleanupPadInst::Create(
- BB.getContext(), {}, Twine("terminatepad.for.", BB.getName()), &BB);
- // Insert the call to the terminate instruction.
- auto *CallTerminate = CallInst::Create(TerminateFn, {}, &BB);
- CallTerminate->setDoesNotThrow();
- CallTerminate->setDoesNotReturn();
- CallTerminate->setCallingConv(TerminateFn->getCallingConv());
+ // Step two: record the TryParentState of each state. For cleanuppads that
+ // don't have cleanuprets, we may need to infer this from their child pads,
+ // so visit pads in descendant-most to ancestor-most order.
+ for (auto Entry = FuncInfo.ClrEHUnwindMap.rbegin(),
+ End = FuncInfo.ClrEHUnwindMap.rend();
+ Entry != End; ++Entry) {
+ const Instruction *Pad =
+ Entry->Handler.get<const BasicBlock *>()->getFirstNonPHI();
+ // For most pads, the TryParentState is the state associated with the
+ // unwind dest of exceptional exits from it.
+ const BasicBlock *UnwindDest;
+ if (const auto *Catch = dyn_cast<CatchPadInst>(Pad)) {
+ // If a catch is not the last in its catchswitch, its TryParentState is
+ // the state associated with the next catch in the switch, even though
+ // that's not the unwind dest of exceptions escaping the catch. Those
+ // cases were already assigned a TryParentState in the first pass, so
+ // skip them.
+ if (Entry->TryParentState != -1)
+ continue;
+ // Otherwise, get the unwind dest from the catchswitch.
+ UnwindDest = Catch->getCatchSwitch()->getUnwindDest();
+ } else {
+ const auto *Cleanup = cast<CleanupPadInst>(Pad);
+ UnwindDest = nullptr;
+ for (const User *U : Cleanup->users()) {
+ if (auto *CleanupRet = dyn_cast<CleanupReturnInst>(U)) {
+ // Common and unambiguous case -- cleanupret indicates cleanup's
+ // unwind dest.
+ UnwindDest = CleanupRet->getUnwindDest();
+ break;
+ }
- // Insert a new terminator for the cleanuppad using the same successor as
- // the terminatepad.
- CleanupReturnInst::Create(CPI, TPI->getUnwindDest(), &BB);
+ // Get an unwind dest for the user
+ const BasicBlock *UserUnwindDest = nullptr;
+ if (auto *Invoke = dyn_cast<InvokeInst>(U)) {
+ UserUnwindDest = Invoke->getUnwindDest();
+ } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(U)) {
+ UserUnwindDest = CatchSwitch->getUnwindDest();
+ } else if (auto *ChildCleanup = dyn_cast<CleanupPadInst>(U)) {
+ int UserState = FuncInfo.EHPadStateMap[ChildCleanup];
+ int UserUnwindState =
+ FuncInfo.ClrEHUnwindMap[UserState].TryParentState;
+ if (UserUnwindState != -1)
+ UserUnwindDest = FuncInfo.ClrEHUnwindMap[UserUnwindState]
+ .Handler.get<const BasicBlock *>();
+ }
- // Let's remove the terminatepad now that we've inserted the new
- // instructions.
- TPI->eraseFromParent();
- }
-}
+ // Not having an unwind dest for this user might indicate that it
+ // doesn't unwind, so can't be taken as proof that the cleanup itself
+ // may unwind to caller (see e.g. SimplifyUnreachable and
+ // RemoveUnwindEdge).
+ if (!UserUnwindDest)
+ continue;
-static void
-colorFunclets(Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks,
- std::map<BasicBlock *, std::set<BasicBlock *>> &BlockColors,
- std::map<BasicBlock *, std::set<BasicBlock *>> &FuncletBlocks,
- std::map<BasicBlock *, std::set<BasicBlock *>> &FuncletChildren) {
- SmallVector<std::pair<BasicBlock *, BasicBlock *>, 16> Worklist;
- BasicBlock *EntryBlock = &F.getEntryBlock();
-
- // Build up the color map, which maps each block to its set of 'colors'.
- // For any block B, the "colors" of B are the set of funclets F (possibly
- // including a root "funclet" representing the main function), such that
- // F will need to directly contain B or a copy of B (where the term "directly
- // contain" is used to distinguish from being "transitively contained" in
- // a nested funclet).
- // Use a CFG walk driven by a worklist of (block, color) pairs. The "color"
- // sets attached during this processing to a block which is the entry of some
- // funclet F is actually the set of F's parents -- i.e. the union of colors
- // of all predecessors of F's entry. For all other blocks, the color sets
- // are as defined above. A post-pass fixes up the block color map to reflect
- // the same sense of "color" for funclet entries as for other blocks.
-
- Worklist.push_back({EntryBlock, EntryBlock});
+ // Now we have an unwind dest for the user, but we need to see if it
+ // unwinds all the way out of the cleanup or if it stays within it.
+ const Instruction *UserUnwindPad = UserUnwindDest->getFirstNonPHI();
+ const Value *UserUnwindParent;
+ if (auto *CSI = dyn_cast<CatchSwitchInst>(UserUnwindPad))
+ UserUnwindParent = CSI->getParentPad();
+ else
+ UserUnwindParent =
+ cast<CleanupPadInst>(UserUnwindPad)->getParentPad();
+
+ // The unwind stays within the cleanup iff it targets a child of the
+ // cleanup.
+ if (UserUnwindParent == Cleanup)
+ continue;
- while (!Worklist.empty()) {
- BasicBlock *Visiting;
- BasicBlock *Color;
- std::tie(Visiting, Color) = Worklist.pop_back_val();
- Instruction *VisitingHead = Visiting->getFirstNonPHI();
- if (VisitingHead->isEHPad() && !isa<CatchEndPadInst>(VisitingHead) &&
- !isa<CleanupEndPadInst>(VisitingHead)) {
- // Mark this as a funclet head as a member of itself.
- FuncletBlocks[Visiting].insert(Visiting);
- // Queue exits (i.e. successors of rets/endpads) with the parent color.
- // Skip any exits that are catchendpads, since the parent color must then
- // represent one of the catches chained to that catchendpad, but the
- // catchendpad should get the color of the common parent of all its
- // chained catches (i.e. the grandparent color of the current pad).
- // We don't need to worry abou catchendpads going unvisited, since the
- // catches chained to them must have unwind edges to them by which we will
- // visit them.
- for (User *U : VisitingHead->users()) {
- if (auto *Exit = dyn_cast<TerminatorInst>(U)) {
- for (BasicBlock *Succ : successors(Exit->getParent()))
- if (!isa<CatchEndPadInst>(*Succ->getFirstNonPHI()))
- if (BlockColors[Succ].insert(Color).second)
- Worklist.push_back({Succ, Color});
- }
- }
- // Handle CatchPad specially since its successors need different colors.
- if (CatchPadInst *CatchPad = dyn_cast<CatchPadInst>(VisitingHead)) {
- // Visit the normal successor with the color of the new EH pad, and
- // visit the unwind successor with the color of the parent.
- BasicBlock *NormalSucc = CatchPad->getNormalDest();
- if (BlockColors[NormalSucc].insert(Visiting).second) {
- Worklist.push_back({NormalSucc, Visiting});
- }
- BasicBlock *UnwindSucc = CatchPad->getUnwindDest();
- if (BlockColors[UnwindSucc].insert(Color).second) {
- Worklist.push_back({UnwindSucc, Color});
- }
- continue;
+ // This unwind exits the cleanup, so its dest is the cleanup's dest.
+ UnwindDest = UserUnwindDest;
+ break;
}
- // Switch color to the current node, except for terminate pads which
- // have no bodies and only unwind successors and so need their successors
- // visited with the color of the parent.
- if (!isa<TerminatePadInst>(VisitingHead))
- Color = Visiting;
- } else {
- // Note that this is a member of the given color.
- FuncletBlocks[Color].insert(Visiting);
}
- TerminatorInst *Terminator = Visiting->getTerminator();
- if (isa<CleanupReturnInst>(Terminator) ||
- isa<CatchReturnInst>(Terminator) ||
- isa<CleanupEndPadInst>(Terminator)) {
- // These blocks' successors have already been queued with the parent
- // color.
- continue;
- }
- for (BasicBlock *Succ : successors(Visiting)) {
- if (isa<CatchEndPadInst>(Succ->getFirstNonPHI())) {
- // The catchendpad needs to be visited with the parent's color, not
- // the current color. This will happen in the code above that visits
- // any catchpad unwind successor with the parent color, so we can
- // safely skip this successor here.
- continue;
- }
- if (BlockColors[Succ].insert(Color).second) {
- Worklist.push_back({Succ, Color});
- }
+ // Record the state of the unwind dest as the TryParentState.
+ int UnwindDestState;
+
+ // If UnwindDest is null at this point, either the pad in question can
+ // be exited by unwind to caller, or it cannot be exited by unwind. In
+ // either case, reporting such cases as unwinding to caller is correct.
+ // This can lead to EH tables that "look strange" -- if this pad's is in
+ // a parent funclet which has other children that do unwind to an enclosing
+ // pad, the try region for this pad will be missing the "duplicate" EH
+ // clause entries that you'd expect to see covering the whole parent. That
+ // should be benign, since the unwind never actually happens. If it were
+ // an issue, we could add a subsequent pass that pushes unwind dests down
+ // from parents that have them to children that appear to unwind to caller.
+ if (!UnwindDest) {
+ UnwindDestState = -1;
+ } else {
+ UnwindDestState = FuncInfo.EHPadStateMap[UnwindDest->getFirstNonPHI()];
}
- }
- // The processing above actually accumulated the parent set for this
- // funclet into the color set for its entry; use the parent set to
- // populate the children map, and reset the color set to include just
- // the funclet itself (no instruction can target a funclet entry except on
- // that transitions to the child funclet).
- for (BasicBlock *FuncletEntry : EntryBlocks) {
- std::set<BasicBlock *> &ColorMapItem = BlockColors[FuncletEntry];
- for (BasicBlock *Parent : ColorMapItem)
- FuncletChildren[Parent].insert(FuncletEntry);
- ColorMapItem.clear();
- ColorMapItem.insert(FuncletEntry);
+ Entry->TryParentState = UnwindDestState;
}
-}
-void WinEHPrepare::colorFunclets(Function &F,
- SmallVectorImpl<BasicBlock *> &EntryBlocks) {
- ::colorFunclets(F, EntryBlocks, BlockColors, FuncletBlocks, FuncletChildren);
+ // Step three: transfer information from pads to invokes.
+ calculateStateNumbersForInvokes(Fn, FuncInfo);
}
-void llvm::calculateCatchReturnSuccessorColors(const Function *Fn,
- WinEHFuncInfo &FuncInfo) {
- SmallVector<BasicBlock *, 4> EntryBlocks;
- // colorFunclets needs the set of EntryBlocks, get them using
- // findFuncletEntryPoints.
- findFuncletEntryPoints(const_cast<Function &>(*Fn), EntryBlocks);
-
- std::map<BasicBlock *, std::set<BasicBlock *>> BlockColors;
- std::map<BasicBlock *, std::set<BasicBlock *>> FuncletBlocks;
- std::map<BasicBlock *, std::set<BasicBlock *>> FuncletChildren;
- // Figure out which basic blocks belong to which funclets.
- colorFunclets(const_cast<Function &>(*Fn), EntryBlocks, BlockColors,
- FuncletBlocks, FuncletChildren);
-
- // We need to find the catchret successors. To do this, we must first find
- // all the catchpad funclets.
- for (auto &Funclet : FuncletBlocks) {
- // Figure out what kind of funclet we are looking at; We only care about
- // catchpads.
- BasicBlock *FuncletPadBB = Funclet.first;
- Instruction *FirstNonPHI = FuncletPadBB->getFirstNonPHI();
- auto *CatchPad = dyn_cast<CatchPadInst>(FirstNonPHI);
- if (!CatchPad)
- continue;
+void WinEHPrepare::colorFunclets(Function &F) {
+ BlockColors = colorEHFunclets(F);
- // The users of a catchpad are always catchrets.
- for (User *Exit : CatchPad->users()) {
- auto *CatchReturn = dyn_cast<CatchReturnInst>(Exit);
- if (!CatchReturn)
- continue;
- BasicBlock *CatchRetSuccessor = CatchReturn->getSuccessor();
- std::set<BasicBlock *> &SuccessorColors = BlockColors[CatchRetSuccessor];
- assert(SuccessorColors.size() == 1 && "Expected BB to be monochrome!");
- BasicBlock *Color = *SuccessorColors.begin();
- if (auto *CPI = dyn_cast<CatchPadInst>(Color->getFirstNonPHI()))
- Color = CPI->getNormalDest();
- // Record the catchret successor's funclet membership.
- FuncInfo.CatchRetSuccessorColorMap[CatchReturn] = Color;
- }
+ // Invert the map from BB to colors to color to BBs.
+ for (BasicBlock &BB : F) {
+ ColorVector &Colors = BlockColors[&BB];
+ for (BasicBlock *Color : Colors)
+ FuncletBlocks[Color].push_back(&BB);
}
}
}
}
-void WinEHPrepare::demoteUsesBetweenFunclets(Function &F) {
- // Turn all inter-funclet uses of a Value into loads and stores.
- for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) {
- BasicBlock *BB = &*FI++;
- std::set<BasicBlock *> &ColorsForBB = BlockColors[BB];
- for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
- Instruction *I = &*BI++;
- // Funclets are permitted to use static allocas.
- if (auto *AI = dyn_cast<AllocaInst>(I))
- if (AI->isStaticAlloca())
- continue;
-
- demoteNonlocalUses(I, ColorsForBB, F);
- }
- }
-}
-
-void WinEHPrepare::demoteArgumentUses(Function &F) {
- // Also demote function parameters used in funclets.
- std::set<BasicBlock *> &ColorsForEntry = BlockColors[&F.getEntryBlock()];
- for (Argument &Arg : F.args())
- demoteNonlocalUses(&Arg, ColorsForEntry, F);
-}
-
-void WinEHPrepare::cloneCommonBlocks(
- Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks) {
+void WinEHPrepare::cloneCommonBlocks(Function &F) {
// We need to clone all blocks which belong to multiple funclets. Values are
// remapped throughout the funclet to propogate both the new instructions
// *and* the new basic blocks themselves.
- for (BasicBlock *FuncletPadBB : EntryBlocks) {
- std::set<BasicBlock *> &BlocksInFunclet = FuncletBlocks[FuncletPadBB];
+ for (auto &Funclets : FuncletBlocks) {
+ BasicBlock *FuncletPadBB = Funclets.first;
+ std::vector<BasicBlock *> &BlocksInFunclet = Funclets.second;
+ Value *FuncletToken;
+ if (FuncletPadBB == &F.getEntryBlock())
+ FuncletToken = ConstantTokenNone::get(F.getContext());
+ else
+ FuncletToken = FuncletPadBB->getFirstNonPHI();
- std::map<BasicBlock *, BasicBlock *> Orig2Clone;
+ std::vector<std::pair<BasicBlock *, BasicBlock *>> Orig2Clone;
ValueToValueMapTy VMap;
for (BasicBlock *BB : BlocksInFunclet) {
- std::set<BasicBlock *> &ColorsForBB = BlockColors[BB];
+ ColorVector &ColorsForBB = BlockColors[BB];
// We don't need to do anything if the block is monochromatic.
size_t NumColorsForBB = ColorsForBB.size();
if (NumColorsForBB == 1)
continue;
+ DEBUG_WITH_TYPE("winehprepare-coloring",
+ dbgs() << " Cloning block \'" << BB->getName()
+ << "\' for funclet \'" << FuncletPadBB->getName()
+ << "\'.\n");
+
// Create a new basic block and copy instructions into it!
BasicBlock *CBB =
CloneBasicBlock(BB, VMap, Twine(".for.", FuncletPadBB->getName()));
VMap[BB] = CBB;
// Record delta operations that we need to perform to our color mappings.
- Orig2Clone[BB] = CBB;
+ Orig2Clone.emplace_back(BB, CBB);
}
// If nothing was cloned, we're done cloning in this funclet.
BasicBlock *OldBlock = BBMapping.first;
BasicBlock *NewBlock = BBMapping.second;
- BlocksInFunclet.insert(NewBlock);
- BlockColors[NewBlock].insert(FuncletPadBB);
-
- BlocksInFunclet.erase(OldBlock);
- BlockColors[OldBlock].erase(FuncletPadBB);
+ BlocksInFunclet.push_back(NewBlock);
+ ColorVector &NewColors = BlockColors[NewBlock];
+ assert(NewColors.empty() && "A new block should only have one color!");
+ NewColors.push_back(FuncletPadBB);
+
+ DEBUG_WITH_TYPE("winehprepare-coloring",
+ dbgs() << " Assigned color \'" << FuncletPadBB->getName()
+ << "\' to block \'" << NewBlock->getName()
+ << "\'.\n");
+
+ BlocksInFunclet.erase(
+ std::remove(BlocksInFunclet.begin(), BlocksInFunclet.end(), OldBlock),
+ BlocksInFunclet.end());
+ ColorVector &OldColors = BlockColors[OldBlock];
+ OldColors.erase(
+ std::remove(OldColors.begin(), OldColors.end(), FuncletPadBB),
+ OldColors.end());
+
+ DEBUG_WITH_TYPE("winehprepare-coloring",
+ dbgs() << " Removed color \'" << FuncletPadBB->getName()
+ << "\' from block \'" << OldBlock->getName()
+ << "\'.\n");
}
// Loop over all of the instructions in this funclet, fixing up operand
RemapInstruction(&I, VMap,
RF_IgnoreMissingEntries | RF_NoModuleLevelChanges);
+ // Catchrets targeting cloned blocks need to be updated separately from
+ // the loop above because they are not in the current funclet.
+ SmallVector<CatchReturnInst *, 2> FixupCatchrets;
+ for (auto &BBMapping : Orig2Clone) {
+ BasicBlock *OldBlock = BBMapping.first;
+ BasicBlock *NewBlock = BBMapping.second;
+
+ FixupCatchrets.clear();
+ for (BasicBlock *Pred : predecessors(OldBlock))
+ if (auto *CatchRet = dyn_cast<CatchReturnInst>(Pred->getTerminator()))
+ if (CatchRet->getParentPad() == FuncletToken)
+ FixupCatchrets.push_back(CatchRet);
+
+ for (CatchReturnInst *CatchRet : FixupCatchrets)
+ CatchRet->setSuccessor(NewBlock);
+ }
+
+ auto UpdatePHIOnClonedBlock = [&](PHINode *PN, bool IsForOldBlock) {
+ unsigned NumPreds = PN->getNumIncomingValues();
+ for (unsigned PredIdx = 0, PredEnd = NumPreds; PredIdx != PredEnd;
+ ++PredIdx) {
+ BasicBlock *IncomingBlock = PN->getIncomingBlock(PredIdx);
+ bool EdgeTargetsFunclet;
+ if (auto *CRI =
+ dyn_cast<CatchReturnInst>(IncomingBlock->getTerminator())) {
+ EdgeTargetsFunclet = (CRI->getParentPad() == FuncletToken);
+ } else {
+ ColorVector &IncomingColors = BlockColors[IncomingBlock];
+ assert(!IncomingColors.empty() && "Block not colored!");
+ assert((IncomingColors.size() == 1 ||
+ llvm::all_of(IncomingColors,
+ [&](BasicBlock *Color) {
+ return Color != FuncletPadBB;
+ })) &&
+ "Cloning should leave this funclet's blocks monochromatic");
+ EdgeTargetsFunclet = (IncomingColors.front() == FuncletPadBB);
+ }
+ if (IsForOldBlock != EdgeTargetsFunclet)
+ continue;
+ PN->removeIncomingValue(IncomingBlock, /*DeletePHIIfEmpty=*/false);
+ // Revisit the next entry.
+ --PredIdx;
+ --PredEnd;
+ }
+ };
+
+ for (auto &BBMapping : Orig2Clone) {
+ BasicBlock *OldBlock = BBMapping.first;
+ BasicBlock *NewBlock = BBMapping.second;
+ for (Instruction &OldI : *OldBlock) {
+ auto *OldPN = dyn_cast<PHINode>(&OldI);
+ if (!OldPN)
+ break;
+ UpdatePHIOnClonedBlock(OldPN, /*IsForOldBlock=*/true);
+ }
+ for (Instruction &NewI : *NewBlock) {
+ auto *NewPN = dyn_cast<PHINode>(&NewI);
+ if (!NewPN)
+ break;
+ UpdatePHIOnClonedBlock(NewPN, /*IsForOldBlock=*/false);
+ }
+ }
+
// Check to see if SuccBB has PHI nodes. If so, we need to add entries to
// the PHI nodes for NewBB now.
for (auto &BBMapping : Orig2Clone) {
for (Use &U : OldI->uses()) {
Instruction *UserI = cast<Instruction>(U.getUser());
BasicBlock *UserBB = UserI->getParent();
- std::set<BasicBlock *> &ColorsForUserBB = BlockColors[UserBB];
+ ColorVector &ColorsForUserBB = BlockColors[UserBB];
assert(!ColorsForUserBB.empty());
if (ColorsForUserBB.size() > 1 ||
*ColorsForUserBB.begin() != FuncletPadBB)
}
}
-void WinEHPrepare::removeImplausibleTerminators(Function &F) {
+void WinEHPrepare::removeImplausibleInstructions(Function &F) {
// Remove implausible terminators and replace them with UnreachableInst.
for (auto &Funclet : FuncletBlocks) {
BasicBlock *FuncletPadBB = Funclet.first;
- std::set<BasicBlock *> &BlocksInFunclet = Funclet.second;
+ std::vector<BasicBlock *> &BlocksInFunclet = Funclet.second;
Instruction *FirstNonPHI = FuncletPadBB->getFirstNonPHI();
- auto *CatchPad = dyn_cast<CatchPadInst>(FirstNonPHI);
- auto *CleanupPad = dyn_cast<CleanupPadInst>(FirstNonPHI);
+ auto *FuncletPad = dyn_cast<FuncletPadInst>(FirstNonPHI);
+ auto *CatchPad = dyn_cast_or_null<CatchPadInst>(FuncletPad);
+ auto *CleanupPad = dyn_cast_or_null<CleanupPadInst>(FuncletPad);
for (BasicBlock *BB : BlocksInFunclet) {
+ for (Instruction &I : *BB) {
+ CallSite CS(&I);
+ if (!CS)
+ continue;
+
+ Value *FuncletBundleOperand = nullptr;
+ if (auto BU = CS.getOperandBundle(LLVMContext::OB_funclet))
+ FuncletBundleOperand = BU->Inputs.front();
+
+ if (FuncletBundleOperand == FuncletPad)
+ continue;
+
+ // Skip call sites which are nounwind intrinsics.
+ auto *CalledFn =
+ dyn_cast<Function>(CS.getCalledValue()->stripPointerCasts());
+ if (CalledFn && CalledFn->isIntrinsic() && CS.doesNotThrow())
+ continue;
+
+ // This call site was not part of this funclet, remove it.
+ if (CS.isInvoke()) {
+ // Remove the unwind edge if it was an invoke.
+ removeUnwindEdge(BB);
+ // Get a pointer to the new call.
+ BasicBlock::iterator CallI =
+ std::prev(BB->getTerminator()->getIterator());
+ auto *CI = cast<CallInst>(&*CallI);
+ changeToUnreachable(CI, /*UseLLVMTrap=*/false);
+ } else {
+ changeToUnreachable(&I, /*UseLLVMTrap=*/false);
+ }
+
+ // There are no more instructions in the block (except for unreachable),
+ // we are done.
+ break;
+ }
+
TerminatorInst *TI = BB->getTerminator();
// CatchPadInst and CleanupPadInst can't transfer control to a ReturnInst.
- bool IsUnreachableRet = isa<ReturnInst>(TI) && (CatchPad || CleanupPad);
+ bool IsUnreachableRet = isa<ReturnInst>(TI) && FuncletPad;
// The token consumed by a CatchReturnInst must match the funclet token.
bool IsUnreachableCatchret = false;
if (auto *CRI = dyn_cast<CatchReturnInst>(TI))
bool IsUnreachableCleanupret = false;
if (auto *CRI = dyn_cast<CleanupReturnInst>(TI))
IsUnreachableCleanupret = CRI->getCleanupPad() != CleanupPad;
- // The token consumed by a CleanupEndPadInst must match the funclet token.
- bool IsUnreachableCleanupendpad = false;
- if (auto *CEPI = dyn_cast<CleanupEndPadInst>(TI))
- IsUnreachableCleanupendpad = CEPI->getCleanupPad() != CleanupPad;
if (IsUnreachableRet || IsUnreachableCatchret ||
- IsUnreachableCleanupret || IsUnreachableCleanupendpad) {
- // Loop through all of our successors and make sure they know that one
- // of their predecessors is going away.
- for (BasicBlock *SuccBB : TI->successors())
- SuccBB->removePredecessor(BB);
-
- if (IsUnreachableCleanupendpad) {
- // We can't simply replace a cleanupendpad with unreachable, because
- // its predecessor edges are EH edges and unreachable is not an EH
- // pad. Change all predecessors to the "unwind to caller" form.
- for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
- PI != PE;) {
- BasicBlock *Pred = *PI++;
- removeUnwindEdge(Pred);
- }
+ IsUnreachableCleanupret) {
+ changeToUnreachable(TI, /*UseLLVMTrap=*/false);
+ } else if (isa<InvokeInst>(TI)) {
+ if (Personality == EHPersonality::MSVC_CXX && CleanupPad) {
+ // Invokes within a cleanuppad for the MSVC++ personality never
+ // transfer control to their unwind edge: the personality will
+ // terminate the program.
+ removeUnwindEdge(BB);
}
-
- new UnreachableInst(BB->getContext(), TI);
- TI->eraseFromParent();
}
- // FIXME: Check for invokes/cleanuprets/cleanupendpads which unwind to
- // implausible catchendpads (i.e. catchendpad not in immediate parent
- // funclet).
}
}
}
}
void WinEHPrepare::verifyPreparedFunclets(Function &F) {
- // Recolor the CFG to verify that all is well.
for (BasicBlock &BB : F) {
size_t NumColors = BlockColors[&BB].size();
assert(NumColors == 1 && "Expected monochromatic BB!");
report_fatal_error("Uncolored BB!");
if (NumColors > 1)
report_fatal_error("Multicolor BB!");
- if (!DisableDemotion) {
- bool EHPadHasPHI = BB.isEHPad() && isa<PHINode>(BB.begin());
- assert(!EHPadHasPHI && "EH Pad still has a PHI!");
- if (EHPadHasPHI)
- report_fatal_error("EH Pad still has a PHI!");
- }
+ assert((DisableDemotion || !(BB.isEHPad() && isa<PHINode>(BB.begin()))) &&
+ "EH Pad still has a PHI!");
}
}
-bool WinEHPrepare::prepareExplicitEH(
- Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks) {
- replaceTerminatePadWithCleanup(F);
+bool WinEHPrepare::prepareExplicitEH(Function &F) {
+ // Remove unreachable blocks. It is not valuable to assign them a color and
+ // their existence can trick us into thinking values are alive when they are
+ // not.
+ removeUnreachableBlocks(F);
// Determine which blocks are reachable from which funclet entries.
- colorFunclets(F, EntryBlocks);
+ colorFunclets(F);
- if (!DisableDemotion) {
- demotePHIsOnFunclets(F);
+ cloneCommonBlocks(F);
- demoteUsesBetweenFunclets(F);
-
- demoteArgumentUses(F);
- }
-
- cloneCommonBlocks(F, EntryBlocks);
+ if (!DisableDemotion)
+ demotePHIsOnFunclets(F);
if (!DisableCleanups) {
- removeImplausibleTerminators(F);
+ DEBUG(verifyFunction(F));
+ removeImplausibleInstructions(F);
+ DEBUG(verifyFunction(F));
cleanupPreparedFunclets(F);
}
- verifyPreparedFunclets(F);
+ DEBUG(verifyPreparedFunclets(F));
+ // Recolor the CFG to verify that all is well.
+ DEBUG(colorFunclets(F));
+ DEBUG(verifyPreparedFunclets(F));
BlockColors.clear();
FuncletBlocks.clear();
- FuncletChildren.clear();
return true;
}
AllocaInst *WinEHPrepare::insertPHILoads(PHINode *PN, Function &F) {
BasicBlock *PHIBlock = PN->getParent();
AllocaInst *SpillSlot = nullptr;
+ Instruction *EHPad = PHIBlock->getFirstNonPHI();
- if (isa<CleanupPadInst>(PHIBlock->getFirstNonPHI())) {
- // Insert a load in place of the PHI and replace all uses.
+ if (!isa<TerminatorInst>(EHPad)) {
+ // If the EHPad isn't a terminator, then we can insert a load in this block
+ // that will dominate all uses.
SpillSlot = new AllocaInst(PN->getType(), nullptr,
Twine(PN->getName(), ".wineh.spillslot"),
&F.getEntryBlock().front());
return SpillSlot;
}
+ // Otherwise, we have a PHI on a terminator EHPad, and we give up and insert
+ // loads of the slot before every use.
DenseMap<BasicBlock *, Value *> Loads;
for (Value::use_iterator UI = PN->use_begin(), UE = PN->use_end();
UI != UE;) {
Use &U = *UI++;
auto *UsingInst = cast<Instruction>(U.getUser());
- BasicBlock *UsingBB = UsingInst->getParent();
- if (UsingBB->isEHPad()) {
+ if (isa<PHINode>(UsingInst) && UsingInst->getParent()->isEHPad()) {
// Use is on an EH pad phi. Leave it alone; we'll insert loads and
// stores for it separately.
- assert(isa<PHINode>(UsingInst));
continue;
}
replaceUseWithLoad(PN, U, SpillSlot, Loads, F);
SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist) {
if (PredBlock->isEHPad() &&
- !isa<CleanupPadInst>(PredBlock->getFirstNonPHI())) {
+ isa<TerminatorInst>(PredBlock->getFirstNonPHI())) {
// Pred is unsplittable, so we need to queue it on the worklist.
Worklist.push_back({PredBlock, PredVal});
return;
new StoreInst(PredVal, SpillSlot, PredBlock->getTerminator());
}
-// TODO: Share loads for same-funclet uses (requires dominators if funclets
-// aren't properly nested).
-void WinEHPrepare::demoteNonlocalUses(Value *V,
- std::set<BasicBlock *> &ColorsForBB,
- Function &F) {
- // Tokens can only be used non-locally due to control flow involving
- // unreachable edges. Don't try to demote the token usage, we'll simply
- // delete the cloned user later.
- if (isa<CatchPadInst>(V) || isa<CleanupPadInst>(V))
- return;
-
- DenseMap<BasicBlock *, Value *> Loads;
- AllocaInst *SpillSlot = nullptr;
- for (Value::use_iterator UI = V->use_begin(), UE = V->use_end(); UI != UE;) {
- Use &U = *UI++;
- auto *UsingInst = cast<Instruction>(U.getUser());
- BasicBlock *UsingBB = UsingInst->getParent();
-
- // Is the Use inside a block which is colored the same as the Def?
- // If so, we don't need to escape the Def because we will clone
- // ourselves our own private copy.
- std::set<BasicBlock *> &ColorsForUsingBB = BlockColors[UsingBB];
- if (ColorsForUsingBB == ColorsForBB)
- continue;
-
- replaceUseWithLoad(V, U, SpillSlot, Loads, F);
- }
- if (SpillSlot) {
- // Insert stores of the computed value into the stack slot.
- // We have to be careful if I is an invoke instruction,
- // because we can't insert the store AFTER the terminator instruction.
- BasicBlock::iterator InsertPt;
- if (isa<Argument>(V)) {
- InsertPt = F.getEntryBlock().getTerminator()->getIterator();
- } else if (isa<TerminatorInst>(V)) {
- auto *II = cast<InvokeInst>(V);
- // We cannot demote invoke instructions to the stack if their normal
- // edge is critical. Therefore, split the critical edge and create a
- // basic block into which the store can be inserted.
- if (!II->getNormalDest()->getSinglePredecessor()) {
- unsigned SuccNum =
- GetSuccessorNumber(II->getParent(), II->getNormalDest());
- assert(isCriticalEdge(II, SuccNum) && "Expected a critical edge!");
- BasicBlock *NewBlock = SplitCriticalEdge(II, SuccNum);
- assert(NewBlock && "Unable to split critical edge.");
- // Update the color mapping for the newly split edge.
- std::set<BasicBlock *> &ColorsForUsingBB = BlockColors[II->getParent()];
- BlockColors[NewBlock] = ColorsForUsingBB;
- for (BasicBlock *FuncletPad : ColorsForUsingBB)
- FuncletBlocks[FuncletPad].insert(NewBlock);
- }
- InsertPt = II->getNormalDest()->getFirstInsertionPt();
- } else {
- InsertPt = cast<Instruction>(V)->getIterator();
- ++InsertPt;
- // Don't insert before PHI nodes or EH pad instrs.
- for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt)
- ;
- }
- new StoreInst(V, SpillSlot, &*InsertPt);
- }
-}
-
void WinEHPrepare::replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
DenseMap<BasicBlock *, Value *> &Loads,
Function &F) {
Goto->setSuccessor(0, PHIBlock);
CatchRet->setSuccessor(NewBlock);
// Update the color mapping for the newly split edge.
- std::set<BasicBlock *> &ColorsForPHIBlock = BlockColors[PHIBlock];
+ ColorVector &ColorsForPHIBlock = BlockColors[PHIBlock];
BlockColors[NewBlock] = ColorsForPHIBlock;
for (BasicBlock *FuncletPad : ColorsForPHIBlock)
- FuncletBlocks[FuncletPad].insert(NewBlock);
+ FuncletBlocks[FuncletPad].push_back(NewBlock);
// Treat the new block as incoming for load insertion.
IncomingBlock = NewBlock;
}
}
}
-void WinEHFuncInfo::addIPToStateRange(const BasicBlock *PadBB,
+void WinEHFuncInfo::addIPToStateRange(const InvokeInst *II,
MCSymbol *InvokeBegin,
MCSymbol *InvokeEnd) {
- assert(PadBB->isEHPad() && EHPadStateMap.count(PadBB->getFirstNonPHI()) &&
- "should get EH pad BB with precomputed state");
- InvokeToStateMap[InvokeBegin] =
- std::make_pair(EHPadStateMap[PadBB->getFirstNonPHI()], InvokeEnd);
+ assert(InvokeStateMap.count(II) &&
+ "should get invoke with precomputed state");
+ LabelToStateMap[InvokeBegin] = std::make_pair(InvokeStateMap[II], InvokeEnd);
}
+
+WinEHFuncInfo::WinEHFuncInfo() {}