2009-05-22 22:36:31 +02:00
|
|
|
//===-- DwarfEHPrepare - Prepare exception handling for code generation ---===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass mulches exception handling code into a form adapted to code
|
2010-03-27 00:41:30 +01:00
|
|
|
// generation. Required if using dwarf exception handling.
|
2009-05-22 22:36:31 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2015-03-09 23:45:16 +01:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2015-03-09 23:45:16 +01:00
|
|
|
#include "llvm/Analysis/CFG.h"
|
2015-12-03 00:06:39 +01:00
|
|
|
#include "llvm/Analysis/EHPersonalities.h"
|
2015-03-09 23:45:16 +01:00
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
|
|
|
#include "llvm/IR/Dominators.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2009-05-22 22:36:31 +02:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Target/TargetLowering.h"
|
2014-08-04 23:25:23 +02:00
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
2015-03-09 23:45:16 +01:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2009-05-22 22:36:31 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 04:02:50 +02:00
|
|
|
#define DEBUG_TYPE "dwarfehprepare"
|
|
|
|
|
2011-11-08 00:36:48 +01:00
|
|
|
STATISTIC(NumResumesLowered, "Number of resume calls lowered");
|
2009-05-22 22:36:31 +02:00
|
|
|
|
|
|
|
namespace {
|
2009-10-25 07:33:48 +01:00
|
|
|
class DwarfEHPrepare : public FunctionPass {
|
2013-06-19 22:51:24 +02:00
|
|
|
const TargetMachine *TM;
|
2009-05-22 22:36:31 +02:00
|
|
|
|
2011-11-08 00:36:48 +01:00
|
|
|
// RewindFunction - _Unwind_Resume or the target equivalent.
|
2009-05-22 22:36:31 +02:00
|
|
|
Constant *RewindFunction;
|
|
|
|
|
2015-03-09 23:45:16 +01:00
|
|
|
DominatorTree *DT;
|
|
|
|
const TargetLowering *TLI;
|
|
|
|
|
2011-11-08 00:36:48 +01:00
|
|
|
bool InsertUnwindResumeCalls(Function &Fn);
|
2012-05-17 19:59:51 +02:00
|
|
|
Value *GetExceptionObject(ResumeInst *RI);
|
2015-03-09 23:45:16 +01:00
|
|
|
size_t
|
|
|
|
pruneUnreachableResumes(Function &Fn,
|
|
|
|
SmallVectorImpl<ResumeInst *> &Resumes,
|
|
|
|
SmallVectorImpl<LandingPadInst *> &CleanupLPads);
|
2010-06-12 04:34:29 +02:00
|
|
|
|
2009-05-22 22:36:31 +02:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid.
|
2015-02-19 00:17:41 +01:00
|
|
|
|
|
|
|
// INITIALIZE_TM_PASS requires a default constructor, but it isn't used in
|
|
|
|
// practice.
|
2015-03-09 23:45:16 +01:00
|
|
|
DwarfEHPrepare()
|
|
|
|
: FunctionPass(ID), TM(nullptr), RewindFunction(nullptr), DT(nullptr),
|
|
|
|
TLI(nullptr) {}
|
2015-02-19 00:17:41 +01:00
|
|
|
|
2014-01-13 14:07:17 +01:00
|
|
|
DwarfEHPrepare(const TargetMachine *TM)
|
2015-03-09 23:45:16 +01:00
|
|
|
: FunctionPass(ID), TM(TM), RewindFunction(nullptr), DT(nullptr),
|
|
|
|
TLI(nullptr) {}
|
2009-05-22 22:36:31 +02:00
|
|
|
|
2014-03-07 10:26:03 +01:00
|
|
|
bool runOnFunction(Function &Fn) override;
|
2009-05-22 22:36:31 +02:00
|
|
|
|
2014-09-14 22:36:28 +02:00
|
|
|
bool doFinalization(Module &M) override {
|
|
|
|
RewindFunction = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-09 23:45:16 +01:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
|
|
|
|
2014-03-07 10:26:03 +01:00
|
|
|
const char *getPassName() const override {
|
2009-05-22 22:36:31 +02:00
|
|
|
return "Exception handling preparation";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
char DwarfEHPrepare::ID = 0;
|
2015-03-09 23:45:16 +01:00
|
|
|
INITIALIZE_TM_PASS_BEGIN(DwarfEHPrepare, "dwarfehprepare",
|
|
|
|
"Prepare DWARF exceptions", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
|
|
|
|
INITIALIZE_TM_PASS_END(DwarfEHPrepare, "dwarfehprepare",
|
|
|
|
"Prepare DWARF exceptions", false, false)
|
2009-05-22 22:36:31 +02:00
|
|
|
|
2013-06-19 22:51:24 +02:00
|
|
|
FunctionPass *llvm::createDwarfEHPass(const TargetMachine *TM) {
|
|
|
|
return new DwarfEHPrepare(TM);
|
2009-05-22 22:36:31 +02:00
|
|
|
}
|
|
|
|
|
2015-03-09 23:45:16 +01:00
|
|
|
void DwarfEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<TargetTransformInfoWrapperPass>();
|
|
|
|
AU.addRequired<DominatorTreeWrapperPass>();
|
|
|
|
}
|
|
|
|
|
2012-01-28 02:17:56 +01:00
|
|
|
/// GetExceptionObject - Return the exception object from the value passed into
|
|
|
|
/// the 'resume' instruction (typically an aggregate). Clean up any dead
|
|
|
|
/// instructions, including the 'resume' instruction.
|
2012-05-17 19:59:51 +02:00
|
|
|
Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) {
|
2012-01-28 02:17:56 +01:00
|
|
|
Value *V = RI->getOperand(0);
|
2014-04-14 02:51:57 +02:00
|
|
|
Value *ExnObj = nullptr;
|
2012-01-28 02:17:56 +01:00
|
|
|
InsertValueInst *SelIVI = dyn_cast<InsertValueInst>(V);
|
2014-04-14 02:51:57 +02:00
|
|
|
LoadInst *SelLoad = nullptr;
|
|
|
|
InsertValueInst *ExcIVI = nullptr;
|
2012-01-28 02:17:56 +01:00
|
|
|
bool EraseIVIs = false;
|
|
|
|
|
|
|
|
if (SelIVI) {
|
|
|
|
if (SelIVI->getNumIndices() == 1 && *SelIVI->idx_begin() == 1) {
|
|
|
|
ExcIVI = dyn_cast<InsertValueInst>(SelIVI->getOperand(0));
|
|
|
|
if (ExcIVI && isa<UndefValue>(ExcIVI->getOperand(0)) &&
|
|
|
|
ExcIVI->getNumIndices() == 1 && *ExcIVI->idx_begin() == 0) {
|
2012-05-17 19:59:51 +02:00
|
|
|
ExnObj = ExcIVI->getOperand(1);
|
2012-01-28 02:17:56 +01:00
|
|
|
SelLoad = dyn_cast<LoadInst>(SelIVI->getOperand(1));
|
|
|
|
EraseIVIs = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ExnObj)
|
|
|
|
ExnObj = ExtractValueInst::Create(RI->getOperand(0), 0, "exn.obj", RI);
|
|
|
|
|
|
|
|
RI->eraseFromParent();
|
|
|
|
|
|
|
|
if (EraseIVIs) {
|
2015-01-29 14:26:50 +01:00
|
|
|
if (SelIVI->use_empty())
|
2012-01-28 02:17:56 +01:00
|
|
|
SelIVI->eraseFromParent();
|
2015-01-29 14:26:50 +01:00
|
|
|
if (ExcIVI->use_empty())
|
2012-01-28 02:17:56 +01:00
|
|
|
ExcIVI->eraseFromParent();
|
2015-01-29 14:26:50 +01:00
|
|
|
if (SelLoad && SelLoad->use_empty())
|
2012-01-28 02:17:56 +01:00
|
|
|
SelLoad->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ExnObj;
|
|
|
|
}
|
|
|
|
|
2015-03-09 23:45:16 +01:00
|
|
|
/// Replace resumes that are not reachable from a cleanup landing pad with
|
|
|
|
/// unreachable and then simplify those blocks.
|
|
|
|
size_t DwarfEHPrepare::pruneUnreachableResumes(
|
|
|
|
Function &Fn, SmallVectorImpl<ResumeInst *> &Resumes,
|
|
|
|
SmallVectorImpl<LandingPadInst *> &CleanupLPads) {
|
|
|
|
BitVector ResumeReachable(Resumes.size());
|
|
|
|
size_t ResumeIndex = 0;
|
|
|
|
for (auto *RI : Resumes) {
|
|
|
|
for (auto *LP : CleanupLPads) {
|
|
|
|
if (isPotentiallyReachable(LP, RI, DT)) {
|
|
|
|
ResumeReachable.set(ResumeIndex);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++ResumeIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If everything is reachable, there is no change.
|
|
|
|
if (ResumeReachable.all())
|
|
|
|
return Resumes.size();
|
|
|
|
|
|
|
|
const TargetTransformInfo &TTI =
|
|
|
|
getAnalysis<TargetTransformInfoWrapperPass>().getTTI(Fn);
|
|
|
|
LLVMContext &Ctx = Fn.getContext();
|
|
|
|
|
|
|
|
// Otherwise, insert unreachable instructions and call simplifycfg.
|
|
|
|
size_t ResumesLeft = 0;
|
|
|
|
for (size_t I = 0, E = Resumes.size(); I < E; ++I) {
|
|
|
|
ResumeInst *RI = Resumes[I];
|
|
|
|
if (ResumeReachable[I]) {
|
|
|
|
Resumes[ResumesLeft++] = RI;
|
|
|
|
} else {
|
|
|
|
BasicBlock *BB = RI->getParent();
|
|
|
|
new UnreachableInst(Ctx, RI);
|
|
|
|
RI->eraseFromParent();
|
2015-03-10 03:37:25 +01:00
|
|
|
SimplifyCFG(BB, TTI, 1);
|
2015-03-09 23:45:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Resumes.resize(ResumesLeft);
|
|
|
|
return ResumesLeft;
|
|
|
|
}
|
|
|
|
|
2011-08-17 21:48:49 +02:00
|
|
|
/// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present
|
|
|
|
/// into calls to the appropriate _Unwind_Resume function.
|
2011-11-08 00:36:48 +01:00
|
|
|
bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) {
|
2011-08-17 21:48:49 +02:00
|
|
|
SmallVector<ResumeInst*, 16> Resumes;
|
2015-03-09 23:45:16 +01:00
|
|
|
SmallVector<LandingPadInst*, 16> CleanupLPads;
|
2015-01-29 14:26:50 +01:00
|
|
|
for (BasicBlock &BB : Fn) {
|
|
|
|
if (auto *RI = dyn_cast<ResumeInst>(BB.getTerminator()))
|
2011-08-26 01:48:11 +02:00
|
|
|
Resumes.push_back(RI);
|
2015-06-17 22:52:32 +02:00
|
|
|
if (auto *LP = BB.getLandingPadInst())
|
2015-03-09 23:45:16 +01:00
|
|
|
if (LP->isCleanup())
|
|
|
|
CleanupLPads.push_back(LP);
|
2011-08-26 01:48:11 +02:00
|
|
|
}
|
2011-08-17 21:48:49 +02:00
|
|
|
|
|
|
|
if (Resumes.empty())
|
2013-05-31 18:30:36 +02:00
|
|
|
return false;
|
2011-08-17 21:48:49 +02:00
|
|
|
|
2015-10-06 22:28:16 +02:00
|
|
|
// Check the personality, don't do anything if it's funclet-based.
|
2015-06-17 22:52:32 +02:00
|
|
|
EHPersonality Pers = classifyEHPersonality(Fn.getPersonalityFn());
|
2015-10-06 22:28:16 +02:00
|
|
|
if (isFuncletEHPersonality(Pers))
|
2015-06-17 22:52:32 +02:00
|
|
|
return false;
|
|
|
|
|
2015-02-20 03:15:36 +01:00
|
|
|
LLVMContext &Ctx = Fn.getContext();
|
2015-03-09 23:45:16 +01:00
|
|
|
|
|
|
|
size_t ResumesLeft = pruneUnreachableResumes(Fn, Resumes, CleanupLPads);
|
|
|
|
if (ResumesLeft == 0)
|
|
|
|
return true; // We pruned them all.
|
|
|
|
|
|
|
|
// Find the rewind function if we didn't already.
|
2011-08-17 21:48:49 +02:00
|
|
|
if (!RewindFunction) {
|
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
|
|
|
|
Type::getInt8PtrTy(Ctx), false);
|
|
|
|
const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME);
|
2011-11-08 00:36:48 +01:00
|
|
|
RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy);
|
2011-08-17 21:48:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the basic block where the _Unwind_Resume call will live.
|
2015-03-09 23:45:16 +01:00
|
|
|
if (ResumesLeft == 1) {
|
2012-01-28 02:17:56 +01:00
|
|
|
// Instead of creating a new BB and PHI node, just append the call to
|
|
|
|
// _Unwind_Resume to the end of the single resume block.
|
|
|
|
ResumeInst *RI = Resumes.front();
|
|
|
|
BasicBlock *UnwindBB = RI->getParent();
|
2012-05-17 19:59:51 +02:00
|
|
|
Value *ExnObj = GetExceptionObject(RI);
|
2012-01-28 02:17:56 +01:00
|
|
|
|
|
|
|
// Call the _Unwind_Resume function.
|
|
|
|
CallInst *CI = CallInst::Create(RewindFunction, ExnObj, "", UnwindBB);
|
|
|
|
CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
|
|
|
|
|
|
|
|
// We never expect _Unwind_Resume to return.
|
|
|
|
new UnreachableInst(Ctx, UnwindBB);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-08 00:36:48 +01:00
|
|
|
BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn);
|
2015-03-09 23:45:16 +01:00
|
|
|
PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesLeft,
|
2011-08-17 21:48:49 +02:00
|
|
|
"exn.obj", UnwindBB);
|
|
|
|
|
|
|
|
// Extract the exception object from the ResumeInst and add it to the PHI node
|
|
|
|
// that feeds the _Unwind_Resume call.
|
2015-01-29 14:26:50 +01:00
|
|
|
for (ResumeInst *RI : Resumes) {
|
2012-01-28 02:17:56 +01:00
|
|
|
BasicBlock *Parent = RI->getParent();
|
|
|
|
BranchInst::Create(UnwindBB, Parent);
|
2012-01-28 02:10:01 +01:00
|
|
|
|
2012-05-17 19:59:51 +02:00
|
|
|
Value *ExnObj = GetExceptionObject(RI);
|
2012-01-28 02:17:56 +01:00
|
|
|
PN->addIncoming(ExnObj, Parent);
|
2012-01-20 01:53:28 +01:00
|
|
|
|
2011-11-08 00:36:48 +01:00
|
|
|
++NumResumesLowered;
|
2011-08-17 21:48:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call the function.
|
|
|
|
CallInst *CI = CallInst::Create(RewindFunction, PN, "", UnwindBB);
|
|
|
|
CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
|
|
|
|
|
|
|
|
// We never expect _Unwind_Resume to return.
|
|
|
|
new UnreachableInst(Ctx, UnwindBB);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-05-22 22:36:31 +02:00
|
|
|
bool DwarfEHPrepare::runOnFunction(Function &Fn) {
|
2015-02-19 00:17:41 +01:00
|
|
|
assert(TM && "DWARF EH preparation requires a target machine");
|
2015-03-09 23:45:16 +01:00
|
|
|
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
|
|
|
TLI = TM->getSubtargetImpl(Fn)->getTargetLowering();
|
2011-11-08 00:36:48 +01:00
|
|
|
bool Changed = InsertUnwindResumeCalls(Fn);
|
2015-03-09 23:45:16 +01:00
|
|
|
DT = nullptr;
|
|
|
|
TLI = nullptr;
|
2009-05-22 22:36:31 +02:00
|
|
|
return Changed;
|
|
|
|
}
|