1 //===- LowerInvoke.cpp - Eliminate Invoke & Unwind instructions -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This transformation is designed for use by code generators which do not yet
11 // support stack unwinding. This pass supports two models of exception handling
12 // lowering, the 'cheap' support and the 'expensive' support.
14 // 'Cheap' exception handling support gives the program the ability to execute
15 // any program which does not "throw an exception", by turning 'invoke'
16 // instructions into calls and by turning 'unwind' instructions into calls to
17 // abort(). If the program does dynamically use the unwind instruction, the
18 // program will print a message then abort.
20 // 'Expensive' exception handling support gives the full exception handling
21 // support to the program at the cost of making the 'invoke' instruction
22 // really expensive. It basically inserts setjmp/longjmp calls to emulate the
23 // exception handling as necessary.
25 // Because the 'expensive' support slows down programs a lot, and EH is only
26 // used for a subset of the programs, it must be specifically enabled by an
29 // Note that after this pass runs the CFG is not entirely accurate (exceptional
30 // control flow edges are not correct anymore) so only very simple things should
31 // be done after the lowerinvoke pass has run (like generation of native code).
32 // This should not be used as a general purpose "my LLVM-to-LLVM pass doesn't
33 // support the invoke instruction yet" lowering pass.
35 //===----------------------------------------------------------------------===//
37 #include "llvm/Transforms/Scalar.h"
38 #include "llvm/Constants.h"
39 #include "llvm/DerivedTypes.h"
40 #include "llvm/Instructions.h"
41 #include "llvm/Module.h"
42 #include "llvm/Pass.h"
43 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
44 #include "llvm/Transforms/Utils/Local.h"
45 #include "llvm/ADT/Statistic.h"
46 #include "llvm/Support/CommandLine.h"
51 Statistic<> NumInvokes("lowerinvoke", "Number of invokes replaced");
52 Statistic<> NumUnwinds("lowerinvoke", "Number of unwinds replaced");
53 Statistic<> NumSpilled("lowerinvoke",
54 "Number of registers live across unwind edges");
55 cl::opt<bool> ExpensiveEHSupport("enable-correct-eh-support",
56 cl::desc("Make the -lowerinvoke pass insert expensive, but correct, EH code"));
58 class LowerInvoke : public FunctionPass {
59 // Used for both models.
63 unsigned AbortMessageLength;
65 // Used for expensive EH support.
67 GlobalVariable *JBListHead;
68 Function *SetJmpFn, *LongJmpFn;
70 bool doInitialization(Module &M);
71 bool runOnFunction(Function &F);
74 void createAbortMessage();
75 void writeAbortMessage(Instruction *IB);
76 bool insertCheapEHSupport(Function &F);
77 void splitLiveRangesLiveAcrossInvokes(std::vector<InvokeInst*> &Invokes);
78 void rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo,
79 AllocaInst *InvokeNum, SwitchInst *CatchSwitch);
80 bool insertExpensiveEHSupport(Function &F);
83 RegisterOpt<LowerInvoke>
84 X("lowerinvoke", "Lower invoke and unwind, for unwindless code generators");
87 const PassInfo *llvm::LowerInvokePassID = X.getPassInfo();
89 // Public Interface To the LowerInvoke pass.
90 FunctionPass *llvm::createLowerInvokePass() { return new LowerInvoke(); }
92 // doInitialization - Make sure that there is a prototype for abort in the
94 bool LowerInvoke::doInitialization(Module &M) {
95 const Type *VoidPtrTy = PointerType::get(Type::SByteTy);
97 if (ExpensiveEHSupport) {
98 // Insert a type for the linked list of jump buffers. Unfortunately, we
99 // don't know the size of the target's setjmp buffer, so we make a guess.
100 // If this guess turns out to be too small, bad stuff could happen.
101 unsigned JmpBufSize = 200; // PPC has 192 words
102 assert(sizeof(jmp_buf) <= JmpBufSize*sizeof(void*) &&
103 "LowerInvoke doesn't know about targets with jmp_buf size > 200 words!");
104 const Type *JmpBufTy = ArrayType::get(VoidPtrTy, JmpBufSize);
106 { // The type is recursive, so use a type holder.
107 std::vector<const Type*> Elements;
108 Elements.push_back(JmpBufTy);
109 OpaqueType *OT = OpaqueType::get();
110 Elements.push_back(PointerType::get(OT));
111 PATypeHolder JBLType(StructType::get(Elements));
112 OT->refineAbstractTypeTo(JBLType.get()); // Complete the cycle.
113 JBLinkTy = JBLType.get();
114 M.addTypeName("llvm.sjljeh.jmpbufty", JBLinkTy);
117 const Type *PtrJBList = PointerType::get(JBLinkTy);
119 // Now that we've done that, insert the jmpbuf list head global, unless it
121 if (!(JBListHead = M.getGlobalVariable("llvm.sjljeh.jblist", PtrJBList)))
122 JBListHead = new GlobalVariable(PtrJBList, false,
123 GlobalValue::LinkOnceLinkage,
124 Constant::getNullValue(PtrJBList),
125 "llvm.sjljeh.jblist", &M);
126 SetJmpFn = M.getOrInsertFunction("llvm.setjmp", Type::IntTy,
127 PointerType::get(JmpBufTy), NULL);
128 LongJmpFn = M.getOrInsertFunction("llvm.longjmp", Type::VoidTy,
129 PointerType::get(JmpBufTy),
133 // We need the 'write' and 'abort' functions for both models.
134 AbortFn = M.getOrInsertFunction("abort", Type::VoidTy, NULL);
136 // Unfortunately, 'write' can end up being prototyped in several different
137 // ways. If the user defines a three (or more) operand function named 'write'
138 // we will use their prototype. We _do not_ want to insert another instance
139 // of a write prototype, because we don't know that the funcresolve pass will
140 // run after us. If there is a definition of a write function, but it's not
141 // suitable for our uses, we just don't emit write calls. If there is no
142 // write prototype at all, we just add one.
143 if (Function *WF = M.getNamedFunction("write")) {
144 if (WF->getFunctionType()->getNumParams() > 3 ||
145 WF->getFunctionType()->isVarArg())
150 WriteFn = M.getOrInsertFunction("write", Type::VoidTy, Type::IntTy,
151 VoidPtrTy, Type::IntTy, NULL);
156 void LowerInvoke::createAbortMessage() {
157 Module &M = *WriteFn->getParent();
158 if (ExpensiveEHSupport) {
159 // The abort message for expensive EH support tells the user that the
160 // program 'unwound' without an 'invoke' instruction.
162 ConstantArray::get("ERROR: Exception thrown, but not caught!\n");
163 AbortMessageLength = Msg->getNumOperands()-1; // don't include \0
165 GlobalVariable *MsgGV = new GlobalVariable(Msg->getType(), true,
166 GlobalValue::InternalLinkage,
167 Msg, "abortmsg", &M);
168 std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::IntTy));
169 AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, GEPIdx);
171 // The abort message for cheap EH support tells the user that EH is not
174 ConstantArray::get("Exception handler needed, but not enabled. Recompile"
175 " program with -enable-correct-eh-support.\n");
176 AbortMessageLength = Msg->getNumOperands()-1; // don't include \0
178 GlobalVariable *MsgGV = new GlobalVariable(Msg->getType(), true,
179 GlobalValue::InternalLinkage,
180 Msg, "abortmsg", &M);
181 std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::IntTy));
182 AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, GEPIdx);
187 void LowerInvoke::writeAbortMessage(Instruction *IB) {
189 if (AbortMessage == 0) createAbortMessage();
191 // These are the arguments we WANT...
192 std::vector<Value*> Args;
193 Args.push_back(ConstantInt::get(Type::IntTy, 2));
194 Args.push_back(AbortMessage);
195 Args.push_back(ConstantInt::get(Type::IntTy, AbortMessageLength));
197 // If the actual declaration of write disagrees, insert casts as
199 const FunctionType *FT = WriteFn->getFunctionType();
200 unsigned NumArgs = FT->getNumParams();
201 for (unsigned i = 0; i != 3; ++i)
202 if (i < NumArgs && FT->getParamType(i) != Args[i]->getType())
203 Args[i] = ConstantExpr::getCast(cast<Constant>(Args[i]),
204 FT->getParamType(i));
206 (new CallInst(WriteFn, Args, "", IB))->setTailCall();
210 bool LowerInvoke::insertCheapEHSupport(Function &F) {
211 bool Changed = false;
212 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
213 if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
214 // Insert a normal call instruction...
215 std::string Name = II->getName(); II->setName("");
216 CallInst *NewCall = new CallInst(II->getCalledValue(),
217 std::vector<Value*>(II->op_begin()+3,
218 II->op_end()), Name, II);
219 NewCall->setCallingConv(II->getCallingConv());
220 II->replaceAllUsesWith(NewCall);
222 // Insert an unconditional branch to the normal destination.
223 new BranchInst(II->getNormalDest(), II);
225 // Remove any PHI node entries from the exception destination.
226 II->getUnwindDest()->removePredecessor(BB);
228 // Remove the invoke instruction now.
229 BB->getInstList().erase(II);
231 ++NumInvokes; Changed = true;
232 } else if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
233 // Insert a new call to write(2, AbortMessage, AbortMessageLength);
234 writeAbortMessage(UI);
236 // Insert a call to abort()
237 (new CallInst(AbortFn, std::vector<Value*>(), "", UI))->setTailCall();
239 // Insert a return instruction. This really should be a "barrier", as it
241 new ReturnInst(F.getReturnType() == Type::VoidTy ? 0 :
242 Constant::getNullValue(F.getReturnType()), UI);
244 // Remove the unwind instruction now.
245 BB->getInstList().erase(UI);
247 ++NumUnwinds; Changed = true;
252 /// rewriteExpensiveInvoke - Insert code and hack the function to replace the
253 /// specified invoke instruction with a call.
254 void LowerInvoke::rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo,
255 AllocaInst *InvokeNum,
256 SwitchInst *CatchSwitch) {
257 ConstantUInt *InvokeNoC = ConstantUInt::get(Type::UIntTy, InvokeNo);
259 // Insert a store of the invoke num before the invoke and store zero into the
260 // location afterward.
261 new StoreInst(InvokeNoC, InvokeNum, true, II); // volatile
262 new StoreInst(Constant::getNullValue(Type::UIntTy), InvokeNum, false,
263 II->getNormalDest()->begin()); // nonvolatile.
265 // Add a switch case to our unwind block.
266 CatchSwitch->addCase(InvokeNoC, II->getUnwindDest());
268 // Insert a normal call instruction.
269 std::string Name = II->getName(); II->setName("");
270 CallInst *NewCall = new CallInst(II->getCalledValue(),
271 std::vector<Value*>(II->op_begin()+3,
274 NewCall->setCallingConv(II->getCallingConv());
275 II->replaceAllUsesWith(NewCall);
277 // Replace the invoke with an uncond branch.
278 new BranchInst(II->getNormalDest(), NewCall->getParent());
279 II->eraseFromParent();
282 /// MarkBlocksLiveIn - Insert BB and all of its predescessors into LiveBBs until
283 /// we reach blocks we've already seen.
284 static void MarkBlocksLiveIn(BasicBlock *BB, std::set<BasicBlock*> &LiveBBs) {
285 if (!LiveBBs.insert(BB).second) return; // already been here.
287 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
288 MarkBlocksLiveIn(*PI, LiveBBs);
291 // First thing we need to do is scan the whole function for values that are
292 // live across unwind edges. Each value that is live across an unwind edge
293 // we spill into a stack location, guaranteeing that there is nothing live
294 // across the unwind edge. This process also splits all critical edges
295 // coming out of invoke's.
297 splitLiveRangesLiveAcrossInvokes(std::vector<InvokeInst*> &Invokes) {
298 // First step, split all critical edges from invoke instructions.
299 for (unsigned i = 0, e = Invokes.size(); i != e; ++i) {
300 InvokeInst *II = Invokes[i];
301 SplitCriticalEdge(II, 0, this);
302 SplitCriticalEdge(II, 1, this);
303 assert(!isa<PHINode>(II->getNormalDest()) &&
304 !isa<PHINode>(II->getUnwindDest()) &&
305 "critical edge splitting left single entry phi nodes?");
308 Function *F = Invokes.back()->getParent()->getParent();
310 // To avoid having to handle incoming arguments specially, we lower each arg
311 // to a copy instruction in the entry block. This ensure that the argument
312 // value itself cannot be live across the entry block.
313 BasicBlock::iterator AfterAllocaInsertPt = F->begin()->begin();
314 while (isa<AllocaInst>(AfterAllocaInsertPt) &&
315 isa<ConstantInt>(cast<AllocaInst>(AfterAllocaInsertPt)->getArraySize()))
316 ++AfterAllocaInsertPt;
317 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
319 CastInst *NC = new CastInst(AI, AI->getType(), AI->getName()+".tmp",
320 AfterAllocaInsertPt);
321 AI->replaceAllUsesWith(NC);
322 NC->setOperand(0, AI);
325 // Finally, scan the code looking for instructions with bad live ranges.
326 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
327 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ++II) {
328 // Ignore obvious cases we don't have to handle. In particular, most
329 // instructions either have no uses or only have a single use inside the
330 // current block. Ignore them quickly.
331 Instruction *Inst = II;
332 if (Inst->use_empty()) continue;
333 if (Inst->hasOneUse() &&
334 cast<Instruction>(Inst->use_back())->getParent() == BB &&
335 !isa<PHINode>(Inst->use_back())) continue;
337 // If this is an alloca in the entry block, it's not a real register
339 if (AllocaInst *AI = dyn_cast<AllocaInst>(Inst))
340 if (isa<ConstantInt>(AI->getArraySize()) && BB == F->begin())
343 // Avoid iterator invalidation by copying users to a temporary vector.
344 std::vector<Instruction*> Users;
345 for (Value::use_iterator UI = Inst->use_begin(), E = Inst->use_end();
347 Instruction *User = cast<Instruction>(*UI);
348 if (User->getParent() != BB || isa<PHINode>(User))
349 Users.push_back(User);
352 // Scan all of the uses and see if the live range is live across an unwind
353 // edge. If we find a use live across an invoke edge, create an alloca
354 // and spill the value.
355 AllocaInst *SpillLoc = 0;
356 std::set<InvokeInst*> InvokesWithStoreInserted;
358 // Find all of the blocks that this value is live in.
359 std::set<BasicBlock*> LiveBBs;
360 LiveBBs.insert(Inst->getParent());
361 while (!Users.empty()) {
362 Instruction *U = Users.back();
365 BasicBlock *UseBlock;
366 if (!isa<PHINode>(U)) {
367 MarkBlocksLiveIn(U->getParent(), LiveBBs);
369 // Uses for a PHI node occur in their predecessor block.
370 PHINode *PN = cast<PHINode>(U);
371 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
372 if (PN->getIncomingValue(i) == Inst)
373 MarkBlocksLiveIn(PN->getIncomingBlock(i), LiveBBs);
377 // Now that we know all of the blocks that this thing is live in, see if
378 // it includes any of the unwind locations.
379 bool NeedsSpill = false;
380 for (unsigned i = 0, e = Invokes.size(); i != e; ++i) {
381 BasicBlock *UnwindBlock = Invokes[i]->getUnwindDest();
382 if (UnwindBlock != BB && LiveBBs.count(UnwindBlock)) {
387 // If we decided we need a spill, do it.
390 DemoteRegToStack(*Inst, true);
395 bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
396 std::vector<ReturnInst*> Returns;
397 std::vector<UnwindInst*> Unwinds;
398 std::vector<InvokeInst*> Invokes;
400 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
401 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
402 // Remember all return instructions in case we insert an invoke into this
404 Returns.push_back(RI);
405 } else if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
406 Invokes.push_back(II);
407 } else if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
408 Unwinds.push_back(UI);
411 if (Unwinds.empty() && Invokes.empty()) return false;
413 NumInvokes += Invokes.size();
414 NumUnwinds += Unwinds.size();
416 // If we have an invoke instruction, insert a setjmp that dominates all
417 // invokes. After the setjmp, use a cond branch that goes to the original
418 // code path on zero, and to a designated 'catch' block of nonzero.
419 Value *OldJmpBufPtr = 0;
420 if (!Invokes.empty()) {
421 // First thing we need to do is scan the whole function for values that are
422 // live across unwind edges. Each value that is live across an unwind edge
423 // we spill into a stack location, guaranteeing that there is nothing live
424 // across the unwind edge. This process also splits all critical edges
425 // coming out of invoke's.
426 splitLiveRangesLiveAcrossInvokes(Invokes);
428 BasicBlock *EntryBB = F.begin();
430 // Create an alloca for the incoming jump buffer ptr and the new jump buffer
431 // that needs to be restored on all exits from the function. This is an
432 // alloca because the value needs to be live across invokes.
434 new AllocaInst(JBLinkTy, 0, "jblink", F.begin()->begin());
436 std::vector<Value*> Idx;
437 Idx.push_back(Constant::getNullValue(Type::IntTy));
438 Idx.push_back(ConstantUInt::get(Type::UIntTy, 1));
439 OldJmpBufPtr = new GetElementPtrInst(JmpBuf, Idx, "OldBuf",
440 EntryBB->getTerminator());
442 // Copy the JBListHead to the alloca.
443 Value *OldBuf = new LoadInst(JBListHead, "oldjmpbufptr", true,
444 EntryBB->getTerminator());
445 new StoreInst(OldBuf, OldJmpBufPtr, true, EntryBB->getTerminator());
447 // Add the new jumpbuf to the list.
448 new StoreInst(JmpBuf, JBListHead, true, EntryBB->getTerminator());
450 // Create the catch block. The catch block is basically a big switch
451 // statement that goes to all of the invoke catch blocks.
452 BasicBlock *CatchBB = new BasicBlock("setjmp.catch", &F);
454 // Create an alloca which keeps track of which invoke is currently
455 // executing. For normal calls it contains zero.
456 AllocaInst *InvokeNum = new AllocaInst(Type::UIntTy, 0, "invokenum",
458 new StoreInst(ConstantInt::get(Type::UIntTy, 0), InvokeNum, true,
459 EntryBB->getTerminator());
461 // Insert a load in the Catch block, and a switch on its value. By default,
462 // we go to a block that just does an unwind (which is the correct action
463 // for a standard call).
464 BasicBlock *UnwindBB = new BasicBlock("unwindbb", &F);
465 Unwinds.push_back(new UnwindInst(UnwindBB));
467 Value *CatchLoad = new LoadInst(InvokeNum, "invoke.num", true, CatchBB);
468 SwitchInst *CatchSwitch =
469 new SwitchInst(CatchLoad, UnwindBB, Invokes.size(), CatchBB);
471 // Now that things are set up, insert the setjmp call itself.
473 // Split the entry block to insert the conditional branch for the setjmp.
474 BasicBlock *ContBlock = EntryBB->splitBasicBlock(EntryBB->getTerminator(),
477 Idx[1] = ConstantUInt::get(Type::UIntTy, 0);
478 Value *JmpBufPtr = new GetElementPtrInst(JmpBuf, Idx, "TheJmpBuf",
479 EntryBB->getTerminator());
480 Value *SJRet = new CallInst(SetJmpFn, JmpBufPtr, "sjret",
481 EntryBB->getTerminator());
483 // Compare the return value to zero.
484 Value *IsNormal = BinaryOperator::createSetEQ(SJRet,
485 Constant::getNullValue(SJRet->getType()),
486 "notunwind", EntryBB->getTerminator());
487 // Nuke the uncond branch.
488 EntryBB->getTerminator()->eraseFromParent();
490 // Put in a new condbranch in its place.
491 new BranchInst(ContBlock, CatchBB, IsNormal, EntryBB);
493 // At this point, we are all set up, rewrite each invoke instruction.
494 for (unsigned i = 0, e = Invokes.size(); i != e; ++i)
495 rewriteExpensiveInvoke(Invokes[i], i+1, InvokeNum, CatchSwitch);
498 // We know that there is at least one unwind.
500 // Create three new blocks, the block to load the jmpbuf ptr and compare
501 // against null, the block to do the longjmp, and the error block for if it
502 // is null. Add them at the end of the function because they are not hot.
503 BasicBlock *UnwindHandler = new BasicBlock("dounwind", &F);
504 BasicBlock *UnwindBlock = new BasicBlock("unwind", &F);
505 BasicBlock *TermBlock = new BasicBlock("unwinderror", &F);
507 // If this function contains an invoke, restore the old jumpbuf ptr.
510 // Before the return, insert a copy from the saved value to the new value.
511 BufPtr = new LoadInst(OldJmpBufPtr, "oldjmpbufptr", UnwindHandler);
512 new StoreInst(BufPtr, JBListHead, UnwindHandler);
514 BufPtr = new LoadInst(JBListHead, "ehlist", UnwindHandler);
517 // Load the JBList, if it's null, then there was no catch!
518 Value *NotNull = BinaryOperator::createSetNE(BufPtr,
519 Constant::getNullValue(BufPtr->getType()),
520 "notnull", UnwindHandler);
521 new BranchInst(UnwindBlock, TermBlock, NotNull, UnwindHandler);
523 // Create the block to do the longjmp.
524 // Get a pointer to the jmpbuf and longjmp.
525 std::vector<Value*> Idx;
526 Idx.push_back(Constant::getNullValue(Type::IntTy));
527 Idx.push_back(ConstantUInt::get(Type::UIntTy, 0));
528 Idx[0] = new GetElementPtrInst(BufPtr, Idx, "JmpBuf", UnwindBlock);
529 Idx[1] = ConstantInt::get(Type::IntTy, 1);
530 new CallInst(LongJmpFn, Idx, "", UnwindBlock);
531 new UnreachableInst(UnwindBlock);
533 // Set up the term block ("throw without a catch").
534 new UnreachableInst(TermBlock);
536 // Insert a new call to write(2, AbortMessage, AbortMessageLength);
537 writeAbortMessage(TermBlock->getTerminator());
539 // Insert a call to abort()
540 (new CallInst(AbortFn, std::vector<Value*>(), "",
541 TermBlock->getTerminator()))->setTailCall();
544 // Replace all unwinds with a branch to the unwind handler.
545 for (unsigned i = 0, e = Unwinds.size(); i != e; ++i) {
546 new BranchInst(UnwindHandler, Unwinds[i]);
547 Unwinds[i]->eraseFromParent();
550 // Finally, for any returns from this function, if this function contains an
551 // invoke, restore the old jmpbuf pointer to its input value.
553 for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
554 ReturnInst *R = Returns[i];
556 // Before the return, insert a copy from the saved value to the new value.
557 Value *OldBuf = new LoadInst(OldJmpBufPtr, "oldjmpbufptr", true, R);
558 new StoreInst(OldBuf, JBListHead, true, R);
565 bool LowerInvoke::runOnFunction(Function &F) {
566 if (ExpensiveEHSupport)
567 return insertExpensiveEHSupport(F);
569 return insertCheapEHSupport(F);