2003-09-15 04:56:27 +00:00
|
|
|
//===- LowerSetJmp.cpp - Code pertaining to lowering set/long jumps -------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-09-15 04:56:27 +00:00
|
|
|
//
|
|
|
|
// This file implements the lowering of setjmp and longjmp to use the
|
2003-09-15 05:43:05 +00:00
|
|
|
// LLVM invoke and unwind instructions as necessary.
|
2003-09-15 04:56:27 +00:00
|
|
|
//
|
|
|
|
// Lowering of longjmp is fairly trivial. We replace the call with a
|
|
|
|
// call to the LLVM library function "__llvm_sjljeh_throw_longjmp()".
|
|
|
|
// This unwinds the stack for us calling all of the destructors for
|
|
|
|
// objects allocated on the stack.
|
|
|
|
//
|
|
|
|
// At a setjmp call, the basic block is split and the setjmp removed.
|
|
|
|
// The calls in a function that have a setjmp are converted to invoke
|
|
|
|
// where the except part checks to see if it's a longjmp exception and,
|
|
|
|
// if so, if it's handled in the function. If it is, then it gets the
|
|
|
|
// value returned by the longjmp and goes to where the basic block was
|
|
|
|
// split. Invoke instructions are handled in a similar fashion with the
|
|
|
|
// original except block being executed if it isn't a longjmp except
|
|
|
|
// that is handled by that function.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// FIXME: This pass doesn't deal with PHI statements just yet. That is,
|
|
|
|
// we expect this to occur before SSAification is done. This would seem
|
|
|
|
// to make sense, but in general, it might be a good idea to make this
|
|
|
|
// pass invokable via the "opt" command at will.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-19 22:09:18 +00:00
|
|
|
#define DEBUG_TYPE "lowersetjmp"
|
2003-11-21 21:54:22 +00:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2003-09-15 04:56:27 +00:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/Intrinsics.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
2003-10-13 00:57:16 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
2007-02-05 23:32:05 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2003-09-15 04:56:27 +00:00
|
|
|
#include "llvm/Support/InstVisitor.h"
|
2004-03-14 02:13:38 +00:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/ADT/VectorExtras.h"
|
2007-08-01 03:43:44 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2008-03-21 23:51:57 +00:00
|
|
|
#include <map>
|
2003-11-21 21:54:22 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2006-12-19 22:09:18 +00:00
|
|
|
STATISTIC(LongJmpsTransformed, "Number of longjmps transformed");
|
|
|
|
STATISTIC(SetJmpsTransformed , "Number of setjmps transformed");
|
|
|
|
STATISTIC(CallsTransformed , "Number of calls invokified");
|
|
|
|
STATISTIC(InvokesTransformed , "Number of invokes modified");
|
2003-09-15 04:56:27 +00:00
|
|
|
|
2006-12-19 22:09:18 +00:00
|
|
|
namespace {
|
2003-09-15 04:56:27 +00:00
|
|
|
//===--------------------------------------------------------------------===//
|
2004-10-07 06:00:24 +00:00
|
|
|
// LowerSetJmp pass implementation.
|
2007-02-05 23:32:05 +00:00
|
|
|
class VISIBILITY_HIDDEN LowerSetJmp : public ModulePass,
|
2003-09-15 04:56:27 +00:00
|
|
|
public InstVisitor<LowerSetJmp> {
|
|
|
|
// LLVM library functions...
|
2007-01-07 06:59:47 +00:00
|
|
|
Constant *InitSJMap; // __llvm_sjljeh_init_setjmpmap
|
|
|
|
Constant *DestroySJMap; // __llvm_sjljeh_destroy_setjmpmap
|
|
|
|
Constant *AddSJToMap; // __llvm_sjljeh_add_setjmp_to_map
|
|
|
|
Constant *ThrowLongJmp; // __llvm_sjljeh_throw_longjmp
|
|
|
|
Constant *TryCatchLJ; // __llvm_sjljeh_try_catching_longjmp_exception
|
|
|
|
Constant *IsLJException; // __llvm_sjljeh_is_longjmp_exception
|
|
|
|
Constant *GetLJValue; // __llvm_sjljeh_get_longjmp_value
|
2003-09-15 04:56:27 +00:00
|
|
|
|
|
|
|
typedef std::pair<SwitchInst*, CallInst*> SwitchValuePair;
|
|
|
|
|
2003-10-13 00:57:16 +00:00
|
|
|
// Keep track of those basic blocks reachable via a depth-first search of
|
|
|
|
// the CFG from a setjmp call. We only need to transform those "call" and
|
|
|
|
// "invoke" instructions that are reachable from the setjmp call site.
|
|
|
|
std::set<BasicBlock*> DFSBlocks;
|
|
|
|
|
2003-09-15 04:56:27 +00:00
|
|
|
// The setjmp map is going to hold information about which setjmps
|
|
|
|
// were called (each setjmp gets its own number) and with which
|
|
|
|
// buffer it was called.
|
|
|
|
std::map<Function*, AllocaInst*> SJMap;
|
|
|
|
|
|
|
|
// The rethrow basic block map holds the basic block to branch to if
|
|
|
|
// the exception isn't handled in the current function and needs to
|
|
|
|
// be rethrown.
|
|
|
|
std::map<const Function*, BasicBlock*> RethrowBBMap;
|
|
|
|
|
|
|
|
// The preliminary basic block map holds a basic block that grabs the
|
|
|
|
// exception and determines if it's handled by the current function.
|
|
|
|
std::map<const Function*, BasicBlock*> PrelimBBMap;
|
|
|
|
|
|
|
|
// The switch/value map holds a switch inst/call inst pair. The
|
|
|
|
// switch inst controls which handler (if any) gets called and the
|
|
|
|
// value is the value returned to that handler by the call to
|
|
|
|
// __llvm_sjljeh_get_longjmp_value.
|
|
|
|
std::map<const Function*, SwitchValuePair> SwitchValMap;
|
|
|
|
|
|
|
|
// A map of which setjmps we've seen so far in a function.
|
|
|
|
std::map<const Function*, unsigned> SetJmpIDMap;
|
|
|
|
|
|
|
|
AllocaInst* GetSetJmpMap(Function* Func);
|
|
|
|
BasicBlock* GetRethrowBB(Function* Func);
|
|
|
|
SwitchValuePair GetSJSwitch(Function* Func, BasicBlock* Rethrow);
|
|
|
|
|
|
|
|
void TransformLongJmpCall(CallInst* Inst);
|
|
|
|
void TransformSetJmpCall(CallInst* Inst);
|
|
|
|
|
|
|
|
bool IsTransformableFunction(const std::string& Name);
|
|
|
|
public:
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2007-05-01 21:15:47 +00:00
|
|
|
LowerSetJmp() : ModulePass((intptr_t)&ID) {}
|
|
|
|
|
2003-09-15 04:56:27 +00:00
|
|
|
void visitCallInst(CallInst& CI);
|
|
|
|
void visitInvokeInst(InvokeInst& II);
|
|
|
|
void visitReturnInst(ReturnInst& RI);
|
|
|
|
void visitUnwindInst(UnwindInst& UI);
|
|
|
|
|
2004-09-20 04:48:05 +00:00
|
|
|
bool runOnModule(Module& M);
|
2003-09-15 04:56:27 +00:00
|
|
|
bool doInitialization(Module& M);
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char LowerSetJmp::ID = 0;
|
|
|
|
static RegisterPass<LowerSetJmp> X("lowersetjmp", "Lower Set Jump");
|
|
|
|
|
2003-09-15 04:56:27 +00:00
|
|
|
// 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.
|
2004-09-20 04:48:05 +00:00
|
|
|
bool LowerSetJmp::runOnModule(Module& M) {
|
2003-09-15 04:56:27 +00:00
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
// These are what the functions are called.
|
2007-02-05 21:19:13 +00:00
|
|
|
Function* SetJmp = M.getFunction("llvm.setjmp");
|
|
|
|
Function* LongJmp = M.getFunction("llvm.longjmp");
|
2003-09-15 04:56:27 +00:00
|
|
|
|
|
|
|
// This program doesn't have longjmp and setjmp calls.
|
|
|
|
if ((!LongJmp || LongJmp->use_empty()) &&
|
|
|
|
(!SetJmp || SetJmp->use_empty())) return false;
|
|
|
|
|
|
|
|
// Initialize some values and functions we'll need to transform the
|
|
|
|
// setjmp/longjmp functions.
|
|
|
|
doInitialization(M);
|
|
|
|
|
2003-10-13 00:57:16 +00:00
|
|
|
if (SetJmp) {
|
|
|
|
for (Value::use_iterator B = SetJmp->use_begin(), E = SetJmp->use_end();
|
|
|
|
B != E; ++B) {
|
2003-10-13 01:02:33 +00:00
|
|
|
BasicBlock* BB = cast<Instruction>(*B)->getParent();
|
2003-10-13 16:49:21 +00:00
|
|
|
for (df_ext_iterator<BasicBlock*> I = df_ext_begin(BB, DFSBlocks),
|
|
|
|
E = df_ext_end(BB, DFSBlocks); I != E; ++I)
|
2003-10-13 16:44:50 +00:00
|
|
|
/* empty */;
|
2003-10-13 00:57:16 +00:00
|
|
|
}
|
|
|
|
|
2003-09-15 04:56:27 +00:00
|
|
|
while (!SetJmp->use_empty()) {
|
|
|
|
assert(isa<CallInst>(SetJmp->use_back()) &&
|
|
|
|
"User of setjmp intrinsic not a call?");
|
|
|
|
TransformSetJmpCall(cast<CallInst>(SetJmp->use_back()));
|
|
|
|
Changed = true;
|
|
|
|
}
|
2003-10-13 00:57:16 +00:00
|
|
|
}
|
2003-09-15 04:56:27 +00:00
|
|
|
|
|
|
|
if (LongJmp)
|
|
|
|
while (!LongJmp->use_empty()) {
|
|
|
|
assert(isa<CallInst>(LongJmp->use_back()) &&
|
|
|
|
"User of longjmp intrinsic not a call?");
|
|
|
|
TransformLongJmpCall(cast<CallInst>(LongJmp->use_back()));
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now go through the affected functions and convert calls and invokes
|
|
|
|
// to new invokes...
|
|
|
|
for (std::map<Function*, AllocaInst*>::iterator
|
|
|
|
B = SJMap.begin(), E = SJMap.end(); B != E; ++B) {
|
|
|
|
Function* F = B->first;
|
|
|
|
for (Function::iterator BB = F->begin(), BE = F->end(); BB != BE; ++BB)
|
|
|
|
for (BasicBlock::iterator IB = BB->begin(), IE = BB->end(); IB != IE; ) {
|
|
|
|
visit(*IB++);
|
|
|
|
if (IB != BB->end() && IB->getParent() != BB)
|
|
|
|
break; // The next instruction got moved to a different block!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-13 00:57:16 +00:00
|
|
|
DFSBlocks.clear();
|
2003-09-15 04:56:27 +00:00
|
|
|
SJMap.clear();
|
|
|
|
RethrowBBMap.clear();
|
|
|
|
PrelimBBMap.clear();
|
|
|
|
SwitchValMap.clear();
|
|
|
|
SetJmpIDMap.clear();
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// doInitialization - For the lower long/setjmp pass, this ensures that a
|
|
|
|
// module contains a declaration for the intrisic functions we are going
|
|
|
|
// to call to convert longjmp and setjmp calls.
|
|
|
|
//
|
|
|
|
// This function is always successful, unless it isn't.
|
|
|
|
bool LowerSetJmp::doInitialization(Module& M)
|
|
|
|
{
|
2007-12-17 01:12:55 +00:00
|
|
|
const Type *SBPTy = PointerType::getUnqual(Type::Int8Ty);
|
|
|
|
const Type *SBPPTy = PointerType::getUnqual(SBPTy);
|
2003-09-15 04:56:27 +00:00
|
|
|
|
|
|
|
// N.B. See llvm/runtime/GCCLibraries/libexception/SJLJ-Exception.h for
|
|
|
|
// a description of the following library functions.
|
|
|
|
|
|
|
|
// void __llvm_sjljeh_init_setjmpmap(void**)
|
|
|
|
InitSJMap = M.getOrInsertFunction("__llvm_sjljeh_init_setjmpmap",
|
2005-10-23 04:37:20 +00:00
|
|
|
Type::VoidTy, SBPPTy, (Type *)0);
|
2003-09-15 04:56:27 +00:00
|
|
|
// void __llvm_sjljeh_destroy_setjmpmap(void**)
|
|
|
|
DestroySJMap = M.getOrInsertFunction("__llvm_sjljeh_destroy_setjmpmap",
|
2005-10-23 04:37:20 +00:00
|
|
|
Type::VoidTy, SBPPTy, (Type *)0);
|
2003-09-15 04:56:27 +00:00
|
|
|
|
|
|
|
// void __llvm_sjljeh_add_setjmp_to_map(void**, void*, unsigned)
|
|
|
|
AddSJToMap = M.getOrInsertFunction("__llvm_sjljeh_add_setjmp_to_map",
|
|
|
|
Type::VoidTy, SBPPTy, SBPTy,
|
2006-12-31 05:48:39 +00:00
|
|
|
Type::Int32Ty, (Type *)0);
|
2003-09-15 04:56:27 +00:00
|
|
|
|
|
|
|
// void __llvm_sjljeh_throw_longjmp(int*, int)
|
|
|
|
ThrowLongJmp = M.getOrInsertFunction("__llvm_sjljeh_throw_longjmp",
|
2006-12-31 05:48:39 +00:00
|
|
|
Type::VoidTy, SBPTy, Type::Int32Ty,
|
2005-10-23 04:37:20 +00:00
|
|
|
(Type *)0);
|
2003-09-15 04:56:27 +00:00
|
|
|
|
|
|
|
// unsigned __llvm_sjljeh_try_catching_longjmp_exception(void **)
|
|
|
|
TryCatchLJ =
|
|
|
|
M.getOrInsertFunction("__llvm_sjljeh_try_catching_longjmp_exception",
|
2006-12-31 05:48:39 +00:00
|
|
|
Type::Int32Ty, SBPPTy, (Type *)0);
|
2003-09-15 04:56:27 +00:00
|
|
|
|
|
|
|
// bool __llvm_sjljeh_is_longjmp_exception()
|
|
|
|
IsLJException = M.getOrInsertFunction("__llvm_sjljeh_is_longjmp_exception",
|
2007-01-11 18:21:29 +00:00
|
|
|
Type::Int1Ty, (Type *)0);
|
2003-09-15 04:56:27 +00:00
|
|
|
|
|
|
|
// int __llvm_sjljeh_get_longjmp_value()
|
|
|
|
GetLJValue = M.getOrInsertFunction("__llvm_sjljeh_get_longjmp_value",
|
2006-12-31 05:48:39 +00:00
|
|
|
Type::Int32Ty, (Type *)0);
|
2003-09-15 04:56:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsTransformableFunction - Return true if the function name isn't one
|
|
|
|
// of the ones we don't want transformed. Currently, don't transform any
|
|
|
|
// "llvm.{setjmp,longjmp}" functions and none of the setjmp/longjmp error
|
|
|
|
// handling functions (beginning with __llvm_sjljeh_...they don't throw
|
|
|
|
// exceptions).
|
2005-04-21 16:46:46 +00:00
|
|
|
bool LowerSetJmp::IsTransformableFunction(const std::string& Name) {
|
2003-09-15 04:56:27 +00:00
|
|
|
std::string SJLJEh("__llvm_sjljeh");
|
|
|
|
|
2003-09-15 05:43:05 +00:00
|
|
|
if (Name.size() > SJLJEh.size())
|
|
|
|
return std::string(Name.begin(), Name.begin() + SJLJEh.size()) != SJLJEh;
|
2003-09-15 04:56:27 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TransformLongJmpCall - Transform a longjmp call into a call to the
|
|
|
|
// internal __llvm_sjljeh_throw_longjmp function. It then takes care of
|
|
|
|
// throwing the exception for us.
|
|
|
|
void LowerSetJmp::TransformLongJmpCall(CallInst* Inst)
|
|
|
|
{
|
2007-12-17 01:12:55 +00:00
|
|
|
const Type* SBPTy = PointerType::getUnqual(Type::Int8Ty);
|
2003-09-15 04:56:27 +00:00
|
|
|
|
|
|
|
// Create the call to "__llvm_sjljeh_throw_longjmp". This takes the
|
|
|
|
// 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.
|
2006-11-27 01:05:10 +00:00
|
|
|
CastInst* CI =
|
|
|
|
new BitCastInst(Inst->getOperand(1), SBPTy, "LJBuf", Inst);
|
2007-08-01 03:43:44 +00:00
|
|
|
SmallVector<Value *, 2> Args;
|
|
|
|
Args.push_back(CI);
|
|
|
|
Args.push_back(Inst->getOperand(2));
|
2008-04-06 20:25:17 +00:00
|
|
|
CallInst::Create(ThrowLongJmp, Args.begin(), Args.end(), "", Inst);
|
2003-09-15 04:56:27 +00:00
|
|
|
|
|
|
|
SwitchValuePair& SVP = SwitchValMap[Inst->getParent()->getParent()];
|
|
|
|
|
|
|
|
// If the function has a setjmp call in it (they are transformed first)
|
|
|
|
// we should branch to the basic block that determines if this longjmp
|
|
|
|
// is applicable here. Otherwise, issue an unwind.
|
|
|
|
if (SVP.first)
|
2008-04-06 20:25:17 +00:00
|
|
|
BranchInst::Create(SVP.first->getParent(), Inst);
|
2003-09-15 04:56:27 +00:00
|
|
|
else
|
|
|
|
new UnwindInst(Inst);
|
|
|
|
|
2005-05-05 15:47:43 +00:00
|
|
|
// 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);
|
2003-09-15 04:56:27 +00:00
|
|
|
|
|
|
|
++LongJmpsTransformed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSetJmpMap - Retrieve (create and initialize, if necessary) the
|
|
|
|
// setjmp map. This map is going to hold information about which setjmps
|
|
|
|
// were called (each setjmp gets its own number) and with which buffer it
|
|
|
|
// was called. There can be only one!
|
|
|
|
AllocaInst* LowerSetJmp::GetSetJmpMap(Function* Func)
|
|
|
|
{
|
|
|
|
if (SJMap[Func]) return SJMap[Func];
|
|
|
|
|
|
|
|
// Insert the setjmp map initialization before the first instruction in
|
|
|
|
// the function.
|
2003-09-20 14:39:18 +00:00
|
|
|
Instruction* Inst = Func->getEntryBlock().begin();
|
2003-09-15 04:56:27 +00:00
|
|
|
assert(Inst && "Couldn't find even ONE instruction in entry block!");
|
|
|
|
|
|
|
|
// Fill in the alloca and call to initialize the SJ map.
|
2007-12-17 01:12:55 +00:00
|
|
|
const Type *SBPTy = PointerType::getUnqual(Type::Int8Ty);
|
2003-09-15 04:56:27 +00:00
|
|
|
AllocaInst* Map = new AllocaInst(SBPTy, 0, "SJMap", Inst);
|
2008-04-06 20:25:17 +00:00
|
|
|
CallInst::Create(InitSJMap, Map, "", Inst);
|
2003-09-15 04:56:27 +00:00
|
|
|
return SJMap[Func] = Map;
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRethrowBB - Only one rethrow basic block is needed per function.
|
|
|
|
// If this is a longjmp exception but not handled in this block, this BB
|
|
|
|
// performs the rethrow.
|
|
|
|
BasicBlock* LowerSetJmp::GetRethrowBB(Function* Func)
|
|
|
|
{
|
|
|
|
if (RethrowBBMap[Func]) return RethrowBBMap[Func];
|
|
|
|
|
|
|
|
// The basic block we're going to jump to if we need to rethrow the
|
|
|
|
// exception.
|
2008-04-06 20:25:17 +00:00
|
|
|
BasicBlock* Rethrow = BasicBlock::Create("RethrowExcept", Func);
|
2003-09-15 04:56:27 +00:00
|
|
|
|
|
|
|
// Fill in the "Rethrow" BB with a call to rethrow the exception. This
|
|
|
|
// is the last instruction in the BB since at this point the runtime
|
|
|
|
// should exit this function and go to the next function.
|
2003-11-20 18:25:24 +00:00
|
|
|
new UnwindInst(Rethrow);
|
2003-09-15 04:56:27 +00:00
|
|
|
return RethrowBBMap[Func] = Rethrow;
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSJSwitch - Return the switch statement that controls which handler
|
|
|
|
// (if any) gets called and the value returned to that handler.
|
|
|
|
LowerSetJmp::SwitchValuePair LowerSetJmp::GetSJSwitch(Function* Func,
|
|
|
|
BasicBlock* Rethrow)
|
|
|
|
{
|
|
|
|
if (SwitchValMap[Func].first) return SwitchValMap[Func];
|
|
|
|
|
2008-04-06 20:25:17 +00:00
|
|
|
BasicBlock* LongJmpPre = BasicBlock::Create("LongJmpBlkPre", Func);
|
2003-09-15 04:56:27 +00:00
|
|
|
|
|
|
|
// Keep track of the preliminary basic block for some of the other
|
|
|
|
// transformations.
|
|
|
|
PrelimBBMap[Func] = LongJmpPre;
|
|
|
|
|
|
|
|
// Grab the exception.
|
2008-06-19 17:53:32 +00:00
|
|
|
CallInst* Cond = CallInst::Create(IsLJException, "IsLJExcept", LongJmpPre);
|
2003-09-15 04:56:27 +00:00
|
|
|
|
|
|
|
// The "decision basic block" gets the number associated with the
|
|
|
|
// setjmp call returning to switch on and the value returned by
|
|
|
|
// longjmp.
|
2008-04-06 20:25:17 +00:00
|
|
|
BasicBlock* DecisionBB = BasicBlock::Create("LJDecisionBB", Func);
|
2003-09-15 04:56:27 +00:00
|
|
|
|
2008-04-06 20:25:17 +00:00
|
|
|
BranchInst::Create(DecisionBB, Rethrow, Cond, LongJmpPre);
|
2003-09-15 04:56:27 +00:00
|
|
|
|
|
|
|
// Fill in the "decision" basic block.
|
2008-06-19 17:53:32 +00:00
|
|
|
CallInst* LJVal = CallInst::Create(GetLJValue, "LJVal", DecisionBB);
|
|
|
|
CallInst* SJNum = CallInst::Create(TryCatchLJ, GetSetJmpMap(Func), "SJNum",
|
|
|
|
DecisionBB);
|
2003-09-15 04:56:27 +00:00
|
|
|
|
2008-04-06 20:25:17 +00:00
|
|
|
SwitchInst* SI = SwitchInst::Create(SJNum, Rethrow, 0, DecisionBB);
|
2003-09-15 04:56:27 +00:00
|
|
|
return SwitchValMap[Func] = SwitchValuePair(SI, LJVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TransformSetJmpCall - The setjmp call is a bit trickier to transform.
|
|
|
|
// We're going to convert all setjmp calls to nops. Then all "call" and
|
|
|
|
// "invoke" instructions in the function are converted to "invoke" where
|
|
|
|
// the "except" branch is used when returning from a longjmp call.
|
|
|
|
void LowerSetJmp::TransformSetJmpCall(CallInst* Inst)
|
|
|
|
{
|
|
|
|
BasicBlock* ABlock = Inst->getParent();
|
|
|
|
Function* Func = ABlock->getParent();
|
|
|
|
|
|
|
|
// Add this setjmp to the setjmp map.
|
2007-12-17 01:12:55 +00:00
|
|
|
const Type* SBPTy = PointerType::getUnqual(Type::Int8Ty);
|
2006-11-27 01:05:10 +00:00
|
|
|
CastInst* BufPtr =
|
|
|
|
new BitCastInst(Inst->getOperand(1), SBPTy, "SBJmpBuf", Inst);
|
2007-02-13 02:10:56 +00:00
|
|
|
std::vector<Value*> Args =
|
|
|
|
make_vector<Value*>(GetSetJmpMap(Func), BufPtr,
|
|
|
|
ConstantInt::get(Type::Int32Ty,
|
|
|
|
SetJmpIDMap[Func]++), 0);
|
2008-04-06 20:25:17 +00:00
|
|
|
CallInst::Create(AddSJToMap, Args.begin(), Args.end(), "", Inst);
|
2003-09-15 04:56:27 +00:00
|
|
|
|
2003-11-06 19:18:49 +00:00
|
|
|
// We are guaranteed that there are no values live across basic blocks
|
|
|
|
// (because we are "not in SSA form" yet), but there can still be values live
|
|
|
|
// in basic blocks. Because of this, splitting the setjmp block can cause
|
|
|
|
// values above the setjmp to not dominate uses which are after the setjmp
|
|
|
|
// call. For all of these occasions, we must spill the value to the stack.
|
|
|
|
//
|
|
|
|
std::set<Instruction*> InstrsAfterCall;
|
|
|
|
|
|
|
|
// The call is probably very close to the end of the basic block, for the
|
|
|
|
// common usage pattern of: 'if (setjmp(...))', so keep track of the
|
|
|
|
// instructions after the call.
|
|
|
|
for (BasicBlock::iterator I = ++BasicBlock::iterator(Inst), E = ABlock->end();
|
|
|
|
I != E; ++I)
|
2005-04-21 23:48:37 +00:00
|
|
|
InstrsAfterCall.insert(I);
|
2003-11-06 19:18:49 +00:00
|
|
|
|
|
|
|
for (BasicBlock::iterator II = ABlock->begin();
|
|
|
|
II != BasicBlock::iterator(Inst); ++II)
|
|
|
|
// Loop over all of the uses of instruction. If any of them are after the
|
|
|
|
// call, "spill" the value to the stack.
|
|
|
|
for (Value::use_iterator UI = II->use_begin(), E = II->use_end();
|
|
|
|
UI != E; ++UI)
|
|
|
|
if (cast<Instruction>(*UI)->getParent() != ABlock ||
|
|
|
|
InstrsAfterCall.count(cast<Instruction>(*UI))) {
|
|
|
|
DemoteRegToStack(*II);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
InstrsAfterCall.clear();
|
|
|
|
|
2003-09-15 04:56:27 +00:00
|
|
|
// Change the setjmp call into a branch statement. We'll remove the
|
|
|
|
// setjmp call in a little bit. No worries.
|
|
|
|
BasicBlock* SetJmpContBlock = ABlock->splitBasicBlock(Inst);
|
|
|
|
assert(SetJmpContBlock && "Couldn't split setjmp BB!!");
|
|
|
|
|
2005-04-21 16:46:46 +00:00
|
|
|
SetJmpContBlock->setName(ABlock->getName()+"SetJmpCont");
|
|
|
|
|
|
|
|
// Add the SetJmpContBlock to the set of blocks reachable from a setjmp.
|
|
|
|
DFSBlocks.insert(SetJmpContBlock);
|
2003-09-15 04:56:27 +00:00
|
|
|
|
|
|
|
// This PHI node will be in the new block created from the
|
|
|
|
// splitBasicBlock call.
|
2008-04-06 20:25:17 +00:00
|
|
|
PHINode* PHI = PHINode::Create(Type::Int32Ty, "SetJmpReturn", Inst);
|
2003-09-15 04:56:27 +00:00
|
|
|
|
|
|
|
// Coming from a call to setjmp, the return is 0.
|
2006-12-31 05:48:39 +00:00
|
|
|
PHI->addIncoming(ConstantInt::getNullValue(Type::Int32Ty), ABlock);
|
2003-09-15 04:56:27 +00:00
|
|
|
|
|
|
|
// Add the case for this setjmp's number...
|
|
|
|
SwitchValuePair SVP = GetSJSwitch(Func, GetRethrowBB(Func));
|
2006-12-31 05:48:39 +00:00
|
|
|
SVP.first->addCase(ConstantInt::get(Type::Int32Ty, SetJmpIDMap[Func] - 1),
|
2003-09-15 04:56:27 +00:00
|
|
|
SetJmpContBlock);
|
|
|
|
|
|
|
|
// Value coming from the handling of the exception.
|
|
|
|
PHI->addIncoming(SVP.second, SVP.second->getParent());
|
|
|
|
|
|
|
|
// Replace all uses of this instruction with the PHI node created by
|
|
|
|
// the eradication of setjmp.
|
|
|
|
Inst->replaceAllUsesWith(PHI);
|
2008-06-21 22:08:46 +00:00
|
|
|
Inst->eraseFromParent();
|
2003-09-15 04:56:27 +00:00
|
|
|
|
|
|
|
++SetJmpsTransformed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// visitCallInst - This converts all LLVM call instructions into invoke
|
|
|
|
// instructions. The except part of the invoke goes to the "LongJmpBlkPre"
|
|
|
|
// that grabs the exception and proceeds to determine if it's a longjmp
|
|
|
|
// exception or not.
|
|
|
|
void LowerSetJmp::visitCallInst(CallInst& CI)
|
|
|
|
{
|
|
|
|
if (CI.getCalledFunction())
|
|
|
|
if (!IsTransformableFunction(CI.getCalledFunction()->getName()) ||
|
|
|
|
CI.getCalledFunction()->isIntrinsic()) return;
|
|
|
|
|
|
|
|
BasicBlock* OldBB = CI.getParent();
|
2003-10-13 00:57:16 +00:00
|
|
|
|
|
|
|
// If not reachable from a setjmp call, don't transform.
|
|
|
|
if (!DFSBlocks.count(OldBB)) return;
|
|
|
|
|
2003-09-15 04:56:27 +00:00
|
|
|
BasicBlock* NewBB = OldBB->splitBasicBlock(CI);
|
|
|
|
assert(NewBB && "Couldn't split BB of \"call\" instruction!!");
|
2005-06-15 22:49:30 +00:00
|
|
|
DFSBlocks.insert(NewBB);
|
2003-09-15 04:56:27 +00:00
|
|
|
NewBB->setName("Call2Invoke");
|
|
|
|
|
2003-10-13 01:02:33 +00:00
|
|
|
Function* Func = OldBB->getParent();
|
2003-09-15 04:56:27 +00:00
|
|
|
|
|
|
|
// Construct the new "invoke" instruction.
|
|
|
|
TerminatorInst* Term = OldBB->getTerminator();
|
|
|
|
std::vector<Value*> Params(CI.op_begin() + 1, CI.op_end());
|
2008-04-06 20:25:17 +00:00
|
|
|
InvokeInst* II =
|
|
|
|
InvokeInst::Create(CI.getCalledValue(), NewBB, PrelimBBMap[Func],
|
|
|
|
Params.begin(), Params.end(), CI.getName(), Term);
|
2007-11-27 13:23:08 +00:00
|
|
|
II->setCallingConv(CI.getCallingConv());
|
|
|
|
II->setParamAttrs(CI.getParamAttrs());
|
2003-09-15 04:56:27 +00:00
|
|
|
|
|
|
|
// Replace the old call inst with the invoke inst and remove the call.
|
|
|
|
CI.replaceAllUsesWith(II);
|
2008-06-21 22:08:46 +00:00
|
|
|
CI.eraseFromParent();
|
2003-09-15 04:56:27 +00:00
|
|
|
|
|
|
|
// The old terminator is useless now that we have the invoke inst.
|
2008-06-21 22:08:46 +00:00
|
|
|
Term->eraseFromParent();
|
2003-10-28 23:14:59 +00:00
|
|
|
++CallsTransformed;
|
2003-09-15 04:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// visitInvokeInst - Converting the "invoke" instruction is fairly
|
|
|
|
// straight-forward. The old exception part is replaced by a query asking
|
|
|
|
// if this is a longjmp exception. If it is, then it goes to the longjmp
|
|
|
|
// exception blocks. Otherwise, control is passed the old exception.
|
|
|
|
void LowerSetJmp::visitInvokeInst(InvokeInst& II)
|
|
|
|
{
|
|
|
|
if (II.getCalledFunction())
|
|
|
|
if (!IsTransformableFunction(II.getCalledFunction()->getName()) ||
|
|
|
|
II.getCalledFunction()->isIntrinsic()) return;
|
|
|
|
|
2003-10-13 00:57:16 +00:00
|
|
|
BasicBlock* BB = II.getParent();
|
|
|
|
|
|
|
|
// If not reachable from a setjmp call, don't transform.
|
|
|
|
if (!DFSBlocks.count(BB)) return;
|
2003-09-15 04:56:27 +00:00
|
|
|
|
2004-02-08 21:44:31 +00:00
|
|
|
BasicBlock* ExceptBB = II.getUnwindDest();
|
2003-09-15 04:56:27 +00:00
|
|
|
|
2003-10-13 01:02:33 +00:00
|
|
|
Function* Func = BB->getParent();
|
2008-04-06 20:25:17 +00:00
|
|
|
BasicBlock* NewExceptBB = BasicBlock::Create("InvokeExcept", Func);
|
2003-09-15 04:56:27 +00:00
|
|
|
|
|
|
|
// If this is a longjmp exception, then branch to the preliminary BB of
|
|
|
|
// the longjmp exception handling. Otherwise, go to the old exception.
|
2008-06-19 17:53:32 +00:00
|
|
|
CallInst* IsLJExcept = CallInst::Create(IsLJException, "IsLJExcept",
|
|
|
|
NewExceptBB);
|
2003-09-15 04:56:27 +00:00
|
|
|
|
2008-04-06 20:25:17 +00:00
|
|
|
BranchInst::Create(PrelimBBMap[Func], ExceptBB, IsLJExcept, NewExceptBB);
|
2003-09-15 04:56:27 +00:00
|
|
|
|
2004-02-08 21:44:31 +00:00
|
|
|
II.setUnwindDest(NewExceptBB);
|
2003-10-28 23:14:59 +00:00
|
|
|
++InvokesTransformed;
|
2003-09-15 04:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// visitReturnInst - We want to destroy the setjmp map upon exit from the
|
|
|
|
// function.
|
2004-09-20 04:48:05 +00:00
|
|
|
void LowerSetJmp::visitReturnInst(ReturnInst &RI) {
|
2003-09-15 04:56:27 +00:00
|
|
|
Function* Func = RI.getParent()->getParent();
|
2008-04-06 20:25:17 +00:00
|
|
|
CallInst::Create(DestroySJMap, GetSetJmpMap(Func), "", &RI);
|
2003-09-15 04:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// visitUnwindInst - We want to destroy the setjmp map upon exit from the
|
|
|
|
// function.
|
2004-09-20 04:48:05 +00:00
|
|
|
void LowerSetJmp::visitUnwindInst(UnwindInst &UI) {
|
2003-09-15 04:56:27 +00:00
|
|
|
Function* Func = UI.getParent()->getParent();
|
2008-04-06 20:25:17 +00:00
|
|
|
CallInst::Create(DestroySJMap, GetSetJmpMap(Func), "", &UI);
|
2003-09-15 04:56:27 +00:00
|
|
|
}
|
|
|
|
|
2004-09-20 04:48:05 +00:00
|
|
|
ModulePass *llvm::createLowerSetJmpPass() {
|
2003-09-15 04:56:27 +00:00
|
|
|
return new LowerSetJmp();
|
|
|
|
}
|
2003-11-11 22:41:34 +00:00
|
|
|
|