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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "dwarfehprepare"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/IntrinsicInst.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
2010-03-27 00:41:30 +01:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Analysis/Dominators.h"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2010-01-21 00:03:55 +01:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2010-06-25 13:25:30 +02:00
|
|
|
#include "llvm/Support/CallSite.h"
|
2009-05-22 22:36:31 +02:00
|
|
|
#include "llvm/Target/TargetLowering.h"
|
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2010-09-03 10:31:48 +02:00
|
|
|
#include "llvm/Transforms/Utils/SSAUpdater.h"
|
2009-05-22 22:36:31 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
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 {
|
2010-04-19 21:05:59 +02:00
|
|
|
const TargetMachine *TM;
|
2009-05-22 22:36:31 +02:00
|
|
|
const TargetLowering *TLI;
|
|
|
|
|
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;
|
|
|
|
|
2011-11-08 00:36:48 +01:00
|
|
|
bool InsertUnwindResumeCalls(Function &Fn);
|
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.
|
2010-08-31 11:05:06 +02:00
|
|
|
DwarfEHPrepare(const TargetMachine *tm) :
|
2010-08-06 20:33:48 +02:00
|
|
|
FunctionPass(ID), TM(tm), TLI(TM->getTargetLowering()),
|
2011-11-08 00:36:48 +01:00
|
|
|
RewindFunction(0) {
|
2010-10-19 19:21:58 +02:00
|
|
|
initializeDominatorTreePass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2009-05-22 22:36:31 +02:00
|
|
|
|
|
|
|
virtual bool runOnFunction(Function &Fn);
|
|
|
|
|
2011-11-08 00:36:48 +01:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const { }
|
2009-10-29 01:37:35 +01:00
|
|
|
|
2009-05-22 22:36:31 +02:00
|
|
|
const char *getPassName() const {
|
|
|
|
return "Exception handling preparation";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
char DwarfEHPrepare::ID = 0;
|
|
|
|
|
2010-08-31 11:05:06 +02:00
|
|
|
FunctionPass *llvm::createDwarfEHPass(const TargetMachine *tm) {
|
|
|
|
return new DwarfEHPrepare(tm);
|
2009-05-22 22:36:31 +02:00
|
|
|
}
|
|
|
|
|
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-26 01:48:11 +02:00
|
|
|
bool UsesNewEH = false;
|
2011-08-17 21:48:49 +02:00
|
|
|
SmallVector<ResumeInst*, 16> Resumes;
|
2011-11-08 00:36:48 +01:00
|
|
|
for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
|
2011-08-26 01:48:11 +02: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 21:48:49 +02:00
|
|
|
|
|
|
|
if (Resumes.empty())
|
2011-08-26 01:48:11 +02:00
|
|
|
return UsesNewEH;
|
2011-08-17 21:48:49 +02: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-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.
|
2011-11-08 00:36:48 +01:00
|
|
|
LLVMContext &Ctx = Fn.getContext();
|
|
|
|
BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn);
|
2011-08-17 21:48:49 +02:00
|
|
|
PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), Resumes.size(),
|
|
|
|
"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;
|
|
|
|
BranchInst::Create(UnwindBB, RI->getParent());
|
|
|
|
ExtractValueInst *ExnObj = ExtractValueInst::Create(RI->getOperand(0),
|
|
|
|
0, "exn.obj", RI);
|
|
|
|
PN->addIncoming(ExnObj, RI->getParent());
|
|
|
|
RI->eraseFromParent();
|
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) {
|
2011-11-08 00:36:48 +01:00
|
|
|
bool Changed = InsertUnwindResumeCalls(Fn);
|
2009-05-22 22:36:31 +02:00
|
|
|
return Changed;
|
|
|
|
}
|