2009-05-22 20:36:31 +00: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-26 23:41:30 +00:00
|
|
|
// generation. Required if using dwarf exception handling.
|
2009-05-22 20:36:31 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "dwarfehprepare"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Analysis/Dominators.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2009-05-22 20:36:31 +00:00
|
|
|
#include "llvm/Pass.h"
|
2010-06-25 11:25:30 +00:00
|
|
|
#include "llvm/Support/CallSite.h"
|
2009-05-22 20:36:31 +00:00
|
|
|
#include "llvm/Target/TargetLowering.h"
|
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2010-09-03 08:31:48 +00:00
|
|
|
#include "llvm/Transforms/Utils/SSAUpdater.h"
|
2009-05-22 20:36:31 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2011-11-07 23:36:48 +00:00
|
|
|
STATISTIC(NumResumesLowered, "Number of resume calls lowered");
|
2009-05-22 20:36:31 +00:00
|
|
|
|
|
|
|
namespace {
|
2009-10-25 06:33:48 +00:00
|
|
|
class DwarfEHPrepare : public FunctionPass {
|
2010-04-19 19:05:59 +00:00
|
|
|
const TargetMachine *TM;
|
2013-01-11 20:05:37 +00:00
|
|
|
const TargetLoweringBase *TLI;
|
2009-05-22 20:36:31 +00:00
|
|
|
|
2011-11-07 23:36:48 +00:00
|
|
|
// RewindFunction - _Unwind_Resume or the target equivalent.
|
2009-05-22 20:36:31 +00:00
|
|
|
Constant *RewindFunction;
|
|
|
|
|
2011-11-07 23:36:48 +00:00
|
|
|
bool InsertUnwindResumeCalls(Function &Fn);
|
2012-05-17 17:59:51 +00:00
|
|
|
Value *GetExceptionObject(ResumeInst *RI);
|
2010-06-12 02:34:29 +00:00
|
|
|
|
2009-05-22 20:36:31 +00:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid.
|
2010-08-31 09:05:06 +00:00
|
|
|
DwarfEHPrepare(const TargetMachine *tm) :
|
2010-08-06 18:33:48 +00:00
|
|
|
FunctionPass(ID), TM(tm), TLI(TM->getTargetLowering()),
|
2011-11-07 23:36:48 +00:00
|
|
|
RewindFunction(0) {
|
2010-10-19 17:21:58 +00:00
|
|
|
initializeDominatorTreePass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2009-05-22 20:36:31 +00:00
|
|
|
|
|
|
|
virtual bool runOnFunction(Function &Fn);
|
|
|
|
|
2011-11-07 23:36:48 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const { }
|
2009-10-29 00:37:35 +00:00
|
|
|
|
2009-05-22 20:36:31 +00:00
|
|
|
const char *getPassName() const {
|
|
|
|
return "Exception handling preparation";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
char DwarfEHPrepare::ID = 0;
|
|
|
|
|
2010-08-31 09:05:06 +00:00
|
|
|
FunctionPass *llvm::createDwarfEHPass(const TargetMachine *tm) {
|
|
|
|
return new DwarfEHPrepare(tm);
|
2009-05-22 20:36:31 +00:00
|
|
|
}
|
|
|
|
|
2012-01-28 01:17:56 +00: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 17:59:51 +00:00
|
|
|
Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) {
|
2012-01-28 01:17:56 +00:00
|
|
|
Value *V = RI->getOperand(0);
|
2012-05-17 17:59:51 +00:00
|
|
|
Value *ExnObj = 0;
|
2012-01-28 01:17:56 +00:00
|
|
|
InsertValueInst *SelIVI = dyn_cast<InsertValueInst>(V);
|
|
|
|
LoadInst *SelLoad = 0;
|
|
|
|
InsertValueInst *ExcIVI = 0;
|
|
|
|
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 17:59:51 +00:00
|
|
|
ExnObj = ExcIVI->getOperand(1);
|
2012-01-28 01:17:56 +00: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) {
|
|
|
|
if (SelIVI->getNumUses() == 0)
|
|
|
|
SelIVI->eraseFromParent();
|
|
|
|
if (ExcIVI->getNumUses() == 0)
|
|
|
|
ExcIVI->eraseFromParent();
|
|
|
|
if (SelLoad && SelLoad->getNumUses() == 0)
|
|
|
|
SelLoad->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ExnObj;
|
|
|
|
}
|
|
|
|
|
2011-08-17 19:48:49 +00:00
|
|
|
/// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present
|
|
|
|
/// into calls to the appropriate _Unwind_Resume function.
|
2011-11-07 23:36:48 +00:00
|
|
|
bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) {
|
2011-08-25 23:48:11 +00:00
|
|
|
bool UsesNewEH = false;
|
2011-08-17 19:48:49 +00:00
|
|
|
SmallVector<ResumeInst*, 16> Resumes;
|
2011-11-07 23:36:48 +00:00
|
|
|
for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
|
2011-08-25 23:48:11 +00:00
|
|
|
TerminatorInst *TI = I->getTerminator();
|
|
|
|
if (ResumeInst *RI = dyn_cast<ResumeInst>(TI))
|
|
|
|
Resumes.push_back(RI);
|
|
|
|
else if (InvokeInst *II = dyn_cast<InvokeInst>(TI))
|
|
|
|
UsesNewEH = II->getUnwindDest()->isLandingPad();
|
|
|
|
}
|
2011-08-17 19:48:49 +00:00
|
|
|
|
|
|
|
if (Resumes.empty())
|
2011-08-25 23:48:11 +00:00
|
|
|
return UsesNewEH;
|
2011-08-17 19:48:49 +00:00
|
|
|
|
|
|
|
// Find the rewind function if we didn't already.
|
|
|
|
if (!RewindFunction) {
|
|
|
|
LLVMContext &Ctx = Resumes[0]->getContext();
|
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
|
|
|
|
Type::getInt8PtrTy(Ctx), false);
|
|
|
|
const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME);
|
2011-11-07 23:36:48 +00:00
|
|
|
RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy);
|
2011-08-17 19:48:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the basic block where the _Unwind_Resume call will live.
|
2011-11-07 23:36:48 +00:00
|
|
|
LLVMContext &Ctx = Fn.getContext();
|
2012-01-28 01:17:56 +00:00
|
|
|
unsigned ResumesSize = Resumes.size();
|
|
|
|
|
|
|
|
if (ResumesSize == 1) {
|
|
|
|
// 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 17:59:51 +00:00
|
|
|
Value *ExnObj = GetExceptionObject(RI);
|
2012-01-28 01:17:56 +00: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-07 23:36:48 +00:00
|
|
|
BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn);
|
2012-01-28 01:17:56 +00:00
|
|
|
PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesSize,
|
2011-08-17 19:48:49 +00:00
|
|
|
"exn.obj", UnwindBB);
|
|
|
|
|
|
|
|
// Extract the exception object from the ResumeInst and add it to the PHI node
|
|
|
|
// that feeds the _Unwind_Resume call.
|
|
|
|
for (SmallVectorImpl<ResumeInst*>::iterator
|
|
|
|
I = Resumes.begin(), E = Resumes.end(); I != E; ++I) {
|
|
|
|
ResumeInst *RI = *I;
|
2012-01-28 01:17:56 +00:00
|
|
|
BasicBlock *Parent = RI->getParent();
|
|
|
|
BranchInst::Create(UnwindBB, Parent);
|
2012-01-28 01:10:01 +00:00
|
|
|
|
2012-05-17 17:59:51 +00:00
|
|
|
Value *ExnObj = GetExceptionObject(RI);
|
2012-01-28 01:17:56 +00:00
|
|
|
PN->addIncoming(ExnObj, Parent);
|
2012-01-20 00:53:28 +00:00
|
|
|
|
2011-11-07 23:36:48 +00:00
|
|
|
++NumResumesLowered;
|
2011-08-17 19:48:49 +00: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 20:36:31 +00:00
|
|
|
bool DwarfEHPrepare::runOnFunction(Function &Fn) {
|
2011-11-07 23:36:48 +00:00
|
|
|
bool Changed = InsertUnwindResumeCalls(Fn);
|
2009-05-22 20:36:31 +00:00
|
|
|
return Changed;
|
|
|
|
}
|