//===- LowerSetJmp.cpp - Code pertaining to lowering set/long jumps -------===//
-//
+//
// The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
-//
+//
//===----------------------------------------------------------------------===//
//
// This file implements the lowering of setjmp and longjmp to use the
// pass invokable via the "opt" command at will.
//===----------------------------------------------------------------------===//
+#define DEBUG_TYPE "lowersetjmp"
#include "llvm/Transforms/IPO.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/ADT/VectorExtras.h"
using namespace llvm;
-namespace {
- Statistic<> LongJmpsTransformed("lowersetjmp",
- "Number of longjmps transformed");
- Statistic<> SetJmpsTransformed("lowersetjmp",
- "Number of setjmps transformed");
- Statistic<> CallsTransformed("lowersetjmp",
- "Number of calls invokified");
- Statistic<> InvokesTransformed("lowersetjmp",
- "Number of invokes modified");
+STATISTIC(LongJmpsTransformed, "Number of longjmps transformed");
+STATISTIC(SetJmpsTransformed , "Number of setjmps transformed");
+STATISTIC(CallsTransformed , "Number of calls invokified");
+STATISTIC(InvokesTransformed , "Number of invokes modified");
+namespace {
//===--------------------------------------------------------------------===//
- // LowerSetJmp pass implementation. This is subclassed from the "Pass"
- // class because it works on a module as a whole, not a function at a
- // time.
-
- class LowerSetJmp : public Pass,
+ // LowerSetJmp pass implementation.
+ class LowerSetJmp : public ModulePass,
public InstVisitor<LowerSetJmp> {
// LLVM library functions...
Function* InitSJMap; // __llvm_sjljeh_init_setjmpmap
void visitReturnInst(ReturnInst& RI);
void visitUnwindInst(UnwindInst& UI);
- bool run(Module& M);
+ bool runOnModule(Module& M);
bool doInitialization(Module& M);
};
- RegisterOpt<LowerSetJmp> X("lowersetjmp", "Lower Set Jump");
+ RegisterPass<LowerSetJmp> X("lowersetjmp", "Lower Set Jump");
} // end anonymous namespace
// run - Run the transformation on the program. We grab the function
// prototypes for longjmp and setjmp. If they are used in the program,
// then we can go directly to the places they're at and transform them.
-bool LowerSetJmp::run(Module& M)
-{
+bool LowerSetJmp::runOnModule(Module& M) {
bool Changed = false;
// These are what the functions are called.
// void __llvm_sjljeh_init_setjmpmap(void**)
InitSJMap = M.getOrInsertFunction("__llvm_sjljeh_init_setjmpmap",
- Type::VoidTy, SBPPTy, 0);
+ Type::VoidTy, SBPPTy, (Type *)0);
// void __llvm_sjljeh_destroy_setjmpmap(void**)
DestroySJMap = M.getOrInsertFunction("__llvm_sjljeh_destroy_setjmpmap",
- Type::VoidTy, SBPPTy, 0);
+ Type::VoidTy, SBPPTy, (Type *)0);
// void __llvm_sjljeh_add_setjmp_to_map(void**, void*, unsigned)
AddSJToMap = M.getOrInsertFunction("__llvm_sjljeh_add_setjmp_to_map",
Type::VoidTy, SBPPTy, SBPTy,
- Type::UIntTy, 0);
+ Type::UIntTy, (Type *)0);
// void __llvm_sjljeh_throw_longjmp(int*, int)
ThrowLongJmp = M.getOrInsertFunction("__llvm_sjljeh_throw_longjmp",
- Type::VoidTy, SBPTy, Type::IntTy, 0);
+ Type::VoidTy, SBPTy, Type::IntTy,
+ (Type *)0);
// unsigned __llvm_sjljeh_try_catching_longjmp_exception(void **)
TryCatchLJ =
M.getOrInsertFunction("__llvm_sjljeh_try_catching_longjmp_exception",
- Type::UIntTy, SBPPTy, 0);
+ Type::UIntTy, SBPPTy, (Type *)0);
// bool __llvm_sjljeh_is_longjmp_exception()
IsLJException = M.getOrInsertFunction("__llvm_sjljeh_is_longjmp_exception",
- Type::BoolTy, 0);
+ Type::BoolTy, (Type *)0);
// int __llvm_sjljeh_get_longjmp_value()
GetLJValue = M.getOrInsertFunction("__llvm_sjljeh_get_longjmp_value",
- Type::IntTy, 0);
+ Type::IntTy, (Type *)0);
return true;
}
// "llvm.{setjmp,longjmp}" functions and none of the setjmp/longjmp error
// handling functions (beginning with __llvm_sjljeh_...they don't throw
// exceptions).
-bool LowerSetJmp::IsTransformableFunction(const std::string& Name)
-{
+bool LowerSetJmp::IsTransformableFunction(const std::string& Name) {
std::string SJLJEh("__llvm_sjljeh");
if (Name.size() > SJLJEh.size())
// same parameters as "longjmp", except that the buffer is cast to a
// char*. It returns "void", so it doesn't need to replace any of
// Inst's uses and doesn't get a name.
- CastInst* CI = new CastInst(Inst->getOperand(1), SBPTy, "LJBuf", Inst);
+ CastInst* CI =
+ new BitCastInst(Inst->getOperand(1), SBPTy, "LJBuf", Inst);
new CallInst(ThrowLongJmp, make_vector<Value*>(CI, Inst->getOperand(2), 0),
"", Inst);
else
new UnwindInst(Inst);
- // Remove all insts after the branch/unwind inst.
- Inst->getParent()->getInstList().erase(Inst,
- Inst->getParent()->getInstList().end());
+ // Remove all insts after the branch/unwind inst. Go from back to front to
+ // avoid replaceAllUsesWith if possible.
+ BasicBlock *BB = Inst->getParent();
+ Instruction *Removed;
+ do {
+ Removed = &BB->back();
+ // If the removed instructions have any users, replace them now.
+ if (!Removed->use_empty())
+ Removed->replaceAllUsesWith(UndefValue::get(Removed->getType()));
+ Removed->eraseFromParent();
+ } while (Removed != Inst);
++LongJmpsTransformed;
}
CallInst(TryCatchLJ, make_vector<Value*>(GetSetJmpMap(Func), 0), "SJNum");
DecisionBBIL.push_back(SJNum);
- SwitchInst* SI = new SwitchInst(SJNum, Rethrow, DecisionBB);
+ SwitchInst* SI = new SwitchInst(SJNum, Rethrow, 0, DecisionBB);
return SwitchValMap[Func] = SwitchValuePair(SI, LJVal);
}
// Add this setjmp to the setjmp map.
const Type* SBPTy = PointerType::get(Type::SByteTy);
- CastInst* BufPtr = new CastInst(Inst->getOperand(1), SBPTy, "SBJmpBuf", Inst);
+ CastInst* BufPtr =
+ new BitCastInst(Inst->getOperand(1), SBPTy, "SBJmpBuf", Inst);
new CallInst(AddSJToMap,
make_vector<Value*>(GetSetJmpMap(Func), BufPtr,
- ConstantUInt::get(Type::UIntTy,
+ ConstantInt::get(Type::UIntTy,
SetJmpIDMap[Func]++), 0),
"", Inst);
// instructions after the call.
for (BasicBlock::iterator I = ++BasicBlock::iterator(Inst), E = ABlock->end();
I != E; ++I)
- InstrsAfterCall.insert(I);
+ InstrsAfterCall.insert(I);
for (BasicBlock::iterator II = ABlock->begin();
II != BasicBlock::iterator(Inst); ++II)
BasicBlock* SetJmpContBlock = ABlock->splitBasicBlock(Inst);
assert(SetJmpContBlock && "Couldn't split setjmp BB!!");
- SetJmpContBlock->setName("SetJmpContBlock");
+ SetJmpContBlock->setName(ABlock->getName()+"SetJmpCont");
+
+ // Add the SetJmpContBlock to the set of blocks reachable from a setjmp.
+ DFSBlocks.insert(SetJmpContBlock);
// This PHI node will be in the new block created from the
// splitBasicBlock call.
// Add the case for this setjmp's number...
SwitchValuePair SVP = GetSJSwitch(Func, GetRethrowBB(Func));
- SVP.first->addCase(ConstantUInt::get(Type::UIntTy, SetJmpIDMap[Func] - 1),
+ SVP.first->addCase(ConstantInt::get(Type::UIntTy, SetJmpIDMap[Func] - 1),
SetJmpContBlock);
// Value coming from the handling of the exception.
BasicBlock* NewBB = OldBB->splitBasicBlock(CI);
assert(NewBB && "Couldn't split BB of \"call\" instruction!!");
+ DFSBlocks.insert(NewBB);
NewBB->setName("Call2Invoke");
Function* Func = OldBB->getParent();
std::vector<Value*> Params(CI.op_begin() + 1, CI.op_end());
InvokeInst* II = new
InvokeInst(CI.getCalledValue(), NewBB, PrelimBBMap[Func],
- Params, CI.getName(), Term);
+ Params, CI.getName(), Term);
// Replace the old call inst with the invoke inst and remove the call.
CI.replaceAllUsesWith(II);
// If not reachable from a setjmp call, don't transform.
if (!DFSBlocks.count(BB)) return;
- BasicBlock* NormalBB = II.getNormalDest();
BasicBlock* ExceptBB = II.getUnwindDest();
Function* Func = BB->getParent();
// visitReturnInst - We want to destroy the setjmp map upon exit from the
// function.
-void LowerSetJmp::visitReturnInst(ReturnInst& RI)
-{
+void LowerSetJmp::visitReturnInst(ReturnInst &RI) {
Function* Func = RI.getParent()->getParent();
new CallInst(DestroySJMap, make_vector<Value*>(GetSetJmpMap(Func), 0),
"", &RI);
// visitUnwindInst - We want to destroy the setjmp map upon exit from the
// function.
-void LowerSetJmp::visitUnwindInst(UnwindInst& UI)
-{
+void LowerSetJmp::visitUnwindInst(UnwindInst &UI) {
Function* Func = UI.getParent()->getParent();
new CallInst(DestroySJMap, make_vector<Value*>(GetSetJmpMap(Func), 0),
"", &UI);
}
-Pass* llvm::createLowerSetJmpPass()
-{
+ModulePass *llvm::createLowerSetJmpPass() {
return new LowerSetJmp();
}