37 #define DEBUG_TYPE "lowerinvoke"
56 STATISTIC(NumInvokes,
"Number of invokes replaced");
57 STATISTIC(NumSpilled,
"Number of registers live across unwind edges");
60 cl::desc(
"Make the -lowerinvoke pass insert expensive, but correct, EH code"));
72 Constant *SetJmpFn, *LongJmpFn, *StackSaveFn, *StackRestoreFn;
73 bool useExpensiveEHSupport;
80 useExpensiveEHSupport(useExpensiveEHSupport) {
83 bool doInitialization(
Module &M);
95 void rewriteExpensiveInvoke(
InvokeInst *II,
unsigned InvokeNo,
98 bool insertExpensiveEHSupport(
Function &
F);
104 "Lower invoke and unwind, for unwindless code generators",
111 bool useExpensiveEHSupport) {
117 bool LowerInvoke::doInitialization(
Module &M) {
119 if (useExpensiveEHSupport) {
123 JBSize = JBSize ? JBSize : 200;
128 JBLinkTy->setBody(Elts);
138 "llvm.sjljeh.jblist");
142 #if defined(_MSC_VER) && defined(setjmp) && \
143 !defined(setjmp_undefined_for_msvc)
144 # pragma push_macro("setjmp")
146 # define setjmp_undefined_for_msvc
151 #if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)
153 # pragma pop_macro("setjmp")
154 # undef setjmp_undefined_for_msvc
168 bool LowerInvoke::insertCheapEHSupport(
Function &
F) {
169 bool Changed =
false;
171 if (
InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
176 NewCall->takeName(II);
177 NewCall->setCallingConv(II->getCallingConv());
178 NewCall->setAttributes(II->getAttributes());
179 NewCall->setDebugLoc(II->getDebugLoc());
180 II->replaceAllUsesWith(NewCall);
186 II->getUnwindDest()->removePredecessor(BB);
189 BB->getInstList().erase(II);
191 ++NumInvokes; Changed =
true;
198 void LowerInvoke::rewriteExpensiveInvoke(
InvokeInst *II,
unsigned InvokeNo,
211 PN->replaceAllUsesWith(PN->getIncomingValue(0));
212 PN->eraseFromParent();
218 new StoreInst(InvokeNoC, InvokeNum,
true, II);
223 new StoreInst(StackSaveRet, StackPtr,
true, II);
228 InvokeNum,
false, NI);
231 new LoadInst(StackPtr,
"stackptr.restore",
true,
242 NewCall->takeName(II);
256 if (!LiveBBs.insert(BB).second)
return;
270 for (
unsigned i = 0, e = Invokes.
size(); i != e; ++i) {
276 "critical edge splitting left single entry phi nodes?");
285 while (isa<AllocaInst>(AfterAllocaInsertPt) &&
286 isa<ConstantInt>(cast<AllocaInst>(AfterAllocaInsertPt)->getArraySize()))
287 ++AfterAllocaInsertPt;
290 Type *Ty = AI->getType();
294 if (isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) {
298 AI->replaceAllUsesWith(NI);
307 AI, AI->getType(), AI->getName()+
".tmp", AfterAllocaInsertPt);
329 !isa<PHINode>(Inst->
use_back()))
continue;
333 if (
AllocaInst *AI = dyn_cast<AllocaInst>(Inst))
334 if (isa<ConstantInt>(AI->getArraySize()) && BB == F->
begin())
342 if (User->
getParent() != BB || isa<PHINode>(User))
351 std::set<BasicBlock*> LiveBBs;
353 while (!Users.
empty()) {
357 if (!isa<PHINode>(U)) {
361 PHINode *PN = cast<PHINode>(U);
370 bool NeedsSpill =
false;
371 for (
unsigned i = 0, e = Invokes.
size(); i != e; ++i) {
372 BasicBlock *UnwindBlock = Invokes[i]->getUnwindDest();
373 if (UnwindBlock != BB && LiveBBs.count(UnwindBlock)) {
386 bool LowerInvoke::insertExpensiveEHSupport(
Function &F) {
392 if (
ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
396 }
else if (
InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
400 if (Invokes.
empty())
return false;
402 NumInvokes += Invokes.
size();
415 Value *OldJmpBufPtr = 0;
416 if (!Invokes.
empty()) {
422 splitLiveRangesLiveAcrossInvokes(Invokes);
433 "jblink", F.
begin()->begin());
441 Value *OldBuf =
new LoadInst(JBListHead,
"oldjmpbufptr",
true,
457 "stackptr", EntryBB->
begin());
462 "invokenum",EntryBB->
begin());
473 Value *CatchLoad =
new LoadInst(InvokeNum,
"invoke.num",
true, CatchBB);
504 for (
unsigned i = 0, e = Invokes.
size(); i != e; ++i)
505 rewriteExpensiveInvoke(Invokes[i], i+1, InvokeNum, StackPtr, CatchSwitch);
522 BufPtr =
new LoadInst(OldJmpBufPtr,
"oldjmpbufptr", UnwindHandler);
523 new StoreInst(BufPtr, JBListHead, UnwindHandler);
525 BufPtr =
new LoadInst(JBListHead,
"ehlist", UnwindHandler);
551 TermBlock->getTerminator())->setTailCall();
554 if (UnreachablePlaceholder) {
556 UnreachablePlaceholder->eraseFromParent();
562 for (
unsigned i = 0, e = Returns.
size(); i != e; ++i) {
566 Value *OldBuf =
new LoadInst(OldJmpBufPtr,
"oldjmpbufptr",
true, R);
567 new StoreInst(OldBuf, JBListHead,
true, R);
574 bool LowerInvoke::runOnFunction(
Function &F) {
575 if (useExpensiveEHSupport)
576 return insertExpensiveEHSupport(F);
578 return insertCheapEHSupport(F);
void push_back(const T &Elt)
AnalysisUsage & addPreserved()
static PassRegistry * getPassRegistry()
LLVMContext & getContext() const
The main container class for the LLVM Intermediate Representation.
BasicBlock * SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P=0, bool MergeIdenticalEdges=false, bool DontDeleteUselessPHIs=false, bool SplitLandingPads=false)
void addCase(ConstantInt *OnVal, BasicBlock *Dest)
static cl::opt< bool > ExpensiveEHSupport("enable-correct-eh-support", cl::desc("Make the -lowerinvoke pass insert expensive, but correct, EH code"))
AllocaInst * DemoteRegToStack(Instruction &X, bool VolatileLoads=false, Instruction *AllocaPoint=0)
const Function * getParent() const
Return the enclosing method, or null if none.
iv Induction Variable Users
const GlobalVariable * getGlobalVariable(StringRef Name, bool AllowInternal=false) const
static Constant * getNullValue(Type *Ty)
static void MarkBlocksLiveIn(BasicBlock *BB, std::set< BasicBlock * > &LiveBBs)
Base class of casting instructions.
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=0)
const Value * getCalledValue() const
unsigned getJumpBufSize() const
ID
LLVM Calling Convention Representation.
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
This class represents a no-op cast from one type to another.
AnalysisUsage & addPreservedID(const void *ID)
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
void replaceAllUsesWith(Value *V)
BasicBlock * getNormalDest() const
unsigned getNumIncomingValues() const
Constant * getOrInsertFunction(StringRef Name, FunctionType *T, AttributeSet AttributeList)
LLVM Basic Block Representation.
LLVM Constant Representation.
Interval::pred_iterator pred_begin(Interval *I)
const DebugLoc & getDebugLoc() const
getDebugLoc - Return the debug location for this node as a DebugLoc.
static Type * getVoidTy(LLVMContext &C)
BasicBlock * getIncomingBlock(unsigned i) const
Represent an integer comparison operator.
Interval::pred_iterator pred_end(Interval *I)
FunctionPass * createLowerInvokePass(const TargetMachine *TM=0, bool useExpensiveEHSupport=false)
LLVMContext & getContext() const
All values hold a context through their type.
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
CallingConv::ID getCallingConv() const
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static CallInst * Create(Value *Func, ArrayRef< Value * > Args, const Twine &NameStr="", Instruction *InsertBefore=0)
void initializeLowerInvokePass(PassRegistry &)
BasicBlock * getUnwindDest() const
static PointerType * getUnqual(Type *ElementType)
Class for constant integers.
Value * getIncomingValue(unsigned i) const
Keep one copy of function when linking (inline)
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
static GetElementPtrInst * Create(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=0)
void setOperand(unsigned i, Value *Val)
static cl::opt< AlignMode > Align(cl::desc("Load/store alignment support"), cl::Hidden, cl::init(DefaultAlign), cl::values(clEnumValN(DefaultAlign,"arm-default-align","Generate unaligned accesses only on hardware/OS ""combinations that are known to support them"), clEnumValN(StrictAlign,"arm-strict-align","Disallow all unaligned memory accesses"), clEnumValN(NoStrictAlign,"arm-no-strict-align","Allow unaligned memory accesses"), clEnumValEnd))
STATISTIC(NumInvokes,"Number of invokes replaced")
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", Instruction *InsertBefore=0)
const AttributeSet & getAttributes() const
static IntegerType * getInt32Ty(LLVMContext &C)
unsigned getJumpBufAlignment() const
void insertAfter(Instruction *InsertPos)
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=0, BasicBlock *InsertBefore=0)
Creates a new BasicBlock.
static ArrayType * get(Type *ElementType, uint64_t NumElements)
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
LLVM Value Representation.
static StructType * create(LLVMContext &Context, StringRef Name)
StructType::create - This creates an identified struct.
static const Function * getParent(const Value *V)
iterator getFirstInsertionPt()
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
static SwitchInst * Create(Value *Value, BasicBlock *Default, unsigned NumCases, Instruction *InsertBefore=0)
const BasicBlock * getParent() const
LLVMContext & getContext() const