Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 1 | //===- LowerInvoke.cpp - Eliminate Invoke & Unwind instructions -----------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 9 | // |
| 10 | // This transformation is designed for use by code generators which do not yet |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 11 | // support stack unwinding. This pass supports two models of exception handling |
| 12 | // lowering, the 'cheap' support and the 'expensive' support. |
| 13 | // |
| 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. |
| 19 | // |
| 20 | // 'Expensive' exception handling support gives the full exception handling |
John Criswell | fe3706a | 2005-05-02 14:47:42 +0000 | [diff] [blame] | 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. |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 24 | // |
| 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 |
| 27 | // option. |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 28 | // |
Chris Lattner | 0e28eca | 2004-03-31 22:00:30 +0000 | [diff] [blame] | 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. |
| 34 | // |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 35 | //===----------------------------------------------------------------------===// |
| 36 | |
| 37 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | e1c0930 | 2004-02-08 07:30:29 +0000 | [diff] [blame] | 38 | #include "llvm/Constants.h" |
| 39 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 40 | #include "llvm/Instructions.h" |
Chris Lattner | e1c0930 | 2004-02-08 07:30:29 +0000 | [diff] [blame] | 41 | #include "llvm/Module.h" |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 42 | #include "llvm/Pass.h" |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 43 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 44 | #include "llvm/ADT/Statistic.h" |
| 45 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 46 | #include <csetjmp> |
Chris Lattner | dead993 | 2003-12-10 20:22:42 +0000 | [diff] [blame] | 47 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 49 | namespace { |
| 50 | Statistic<> NumLowered("lowerinvoke", "Number of invoke & unwinds replaced"); |
Chris Lattner | 99cca7d | 2004-03-01 01:12:13 +0000 | [diff] [blame] | 51 | cl::opt<bool> ExpensiveEHSupport("enable-correct-eh-support", |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 52 | cl::desc("Make the -lowerinvoke pass insert expensive, but correct, EH code")); |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 53 | |
| 54 | class LowerInvoke : public FunctionPass { |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 55 | // Used for both models. |
Chris Lattner | e1c0930 | 2004-02-08 07:30:29 +0000 | [diff] [blame] | 56 | Function *WriteFn; |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 57 | Function *AbortFn; |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 58 | Value *AbortMessage; |
| 59 | unsigned AbortMessageLength; |
| 60 | |
| 61 | // Used for expensive EH support. |
| 62 | const Type *JBLinkTy; |
| 63 | GlobalVariable *JBListHead; |
| 64 | Function *SetJmpFn, *LongJmpFn; |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 65 | public: |
| 66 | bool doInitialization(Module &M); |
| 67 | bool runOnFunction(Function &F); |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 68 | private: |
Chris Lattner | 0c3b390 | 2004-11-13 19:07:32 +0000 | [diff] [blame] | 69 | void createAbortMessage(); |
Chris Lattner | 501825e | 2004-02-08 22:14:44 +0000 | [diff] [blame] | 70 | void writeAbortMessage(Instruction *IB); |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 71 | bool insertCheapEHSupport(Function &F); |
| 72 | bool insertExpensiveEHSupport(Function &F); |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | RegisterOpt<LowerInvoke> |
| 76 | X("lowerinvoke", "Lower invoke and unwind, for unwindless code generators"); |
| 77 | } |
| 78 | |
Chris Lattner | cefc18e | 2004-02-13 16:16:16 +0000 | [diff] [blame] | 79 | const PassInfo *llvm::LowerInvokePassID = X.getPassInfo(); |
| 80 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 81 | // Public Interface To the LowerInvoke pass. |
Chris Lattner | dead993 | 2003-12-10 20:22:42 +0000 | [diff] [blame] | 82 | FunctionPass *llvm::createLowerInvokePass() { return new LowerInvoke(); } |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 83 | |
| 84 | // doInitialization - Make sure that there is a prototype for abort in the |
| 85 | // current module. |
| 86 | bool LowerInvoke::doInitialization(Module &M) { |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 87 | const Type *VoidPtrTy = PointerType::get(Type::SByteTy); |
Chris Lattner | f1d0d35 | 2004-02-09 22:48:47 +0000 | [diff] [blame] | 88 | AbortMessage = 0; |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 89 | if (ExpensiveEHSupport) { |
| 90 | // Insert a type for the linked list of jump buffers. Unfortunately, we |
| 91 | // don't know the size of the target's setjmp buffer, so we make a guess. |
| 92 | // If this guess turns out to be too small, bad stuff could happen. |
| 93 | unsigned JmpBufSize = 200; // PPC has 192 words |
| 94 | assert(sizeof(jmp_buf) <= JmpBufSize*sizeof(void*) && |
| 95 | "LowerInvoke doesn't know about targets with jmp_buf size > 200 words!"); |
| 96 | const Type *JmpBufTy = ArrayType::get(VoidPtrTy, JmpBufSize); |
Chris Lattner | e1c0930 | 2004-02-08 07:30:29 +0000 | [diff] [blame] | 97 | |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 98 | { // The type is recursive, so use a type holder. |
| 99 | std::vector<const Type*> Elements; |
| 100 | OpaqueType *OT = OpaqueType::get(); |
| 101 | Elements.push_back(PointerType::get(OT)); |
| 102 | Elements.push_back(JmpBufTy); |
| 103 | PATypeHolder JBLType(StructType::get(Elements)); |
| 104 | OT->refineAbstractTypeTo(JBLType.get()); // Complete the cycle. |
| 105 | JBLinkTy = JBLType.get(); |
Chris Lattner | 11b9be5 | 2004-05-28 05:02:13 +0000 | [diff] [blame] | 106 | M.addTypeName("llvm.sjljeh.jmpbufty", JBLinkTy); |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | const Type *PtrJBList = PointerType::get(JBLinkTy); |
| 110 | |
| 111 | // Now that we've done that, insert the jmpbuf list head global, unless it |
| 112 | // already exists. |
| 113 | if (!(JBListHead = M.getGlobalVariable("llvm.sjljeh.jblist", PtrJBList))) |
| 114 | JBListHead = new GlobalVariable(PtrJBList, false, |
| 115 | GlobalValue::LinkOnceLinkage, |
| 116 | Constant::getNullValue(PtrJBList), |
| 117 | "llvm.sjljeh.jblist", &M); |
Chris Lattner | 860a161 | 2004-02-15 22:24:27 +0000 | [diff] [blame] | 118 | SetJmpFn = M.getOrInsertFunction("llvm.setjmp", Type::IntTy, |
Chris Lattner | 71fae10 | 2005-06-09 03:32:54 +0000 | [diff] [blame^] | 119 | PointerType::get(JmpBufTy), NULL); |
Chris Lattner | 860a161 | 2004-02-15 22:24:27 +0000 | [diff] [blame] | 120 | LongJmpFn = M.getOrInsertFunction("llvm.longjmp", Type::VoidTy, |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 121 | PointerType::get(JmpBufTy), |
Chris Lattner | 71fae10 | 2005-06-09 03:32:54 +0000 | [diff] [blame^] | 122 | Type::IntTy, NULL); |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // We need the 'write' and 'abort' functions for both models. |
Chris Lattner | 71fae10 | 2005-06-09 03:32:54 +0000 | [diff] [blame^] | 126 | AbortFn = M.getOrInsertFunction("abort", Type::VoidTy, NULL); |
Chris Lattner | 501825e | 2004-02-08 22:14:44 +0000 | [diff] [blame] | 127 | |
| 128 | // Unfortunately, 'write' can end up being prototyped in several different |
| 129 | // ways. If the user defines a three (or more) operand function named 'write' |
Misha Brukman | b9806e0 | 2004-02-08 22:27:33 +0000 | [diff] [blame] | 130 | // we will use their prototype. We _do not_ want to insert another instance |
Chris Lattner | 501825e | 2004-02-08 22:14:44 +0000 | [diff] [blame] | 131 | // of a write prototype, because we don't know that the funcresolve pass will |
| 132 | // run after us. If there is a definition of a write function, but it's not |
| 133 | // suitable for our uses, we just don't emit write calls. If there is no |
| 134 | // write prototype at all, we just add one. |
| 135 | if (Function *WF = M.getNamedFunction("write")) { |
| 136 | if (WF->getFunctionType()->getNumParams() > 3 || |
| 137 | WF->getFunctionType()->isVarArg()) |
| 138 | WriteFn = WF; |
| 139 | else |
| 140 | WriteFn = 0; |
| 141 | } else { |
| 142 | WriteFn = M.getOrInsertFunction("write", Type::VoidTy, Type::IntTy, |
Chris Lattner | 71fae10 | 2005-06-09 03:32:54 +0000 | [diff] [blame^] | 143 | VoidPtrTy, Type::IntTy, NULL); |
Chris Lattner | 501825e | 2004-02-08 22:14:44 +0000 | [diff] [blame] | 144 | } |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 145 | return true; |
| 146 | } |
| 147 | |
Chris Lattner | 0c3b390 | 2004-11-13 19:07:32 +0000 | [diff] [blame] | 148 | void LowerInvoke::createAbortMessage() { |
| 149 | Module &M = *WriteFn->getParent(); |
| 150 | if (ExpensiveEHSupport) { |
| 151 | // The abort message for expensive EH support tells the user that the |
| 152 | // program 'unwound' without an 'invoke' instruction. |
| 153 | Constant *Msg = |
| 154 | ConstantArray::get("ERROR: Exception thrown, but not caught!\n"); |
| 155 | AbortMessageLength = Msg->getNumOperands()-1; // don't include \0 |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 156 | |
Chris Lattner | 0c3b390 | 2004-11-13 19:07:32 +0000 | [diff] [blame] | 157 | GlobalVariable *MsgGV = new GlobalVariable(Msg->getType(), true, |
| 158 | GlobalValue::InternalLinkage, |
| 159 | Msg, "abortmsg", &M); |
Chris Lattner | 1381dd8 | 2005-05-13 06:10:12 +0000 | [diff] [blame] | 160 | std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::IntTy)); |
Chris Lattner | 0c3b390 | 2004-11-13 19:07:32 +0000 | [diff] [blame] | 161 | AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, GEPIdx); |
| 162 | } else { |
| 163 | // The abort message for cheap EH support tells the user that EH is not |
| 164 | // enabled. |
| 165 | Constant *Msg = |
| 166 | ConstantArray::get("Exception handler needed, but not enabled. Recompile" |
| 167 | " program with -enable-correct-eh-support.\n"); |
| 168 | AbortMessageLength = Msg->getNumOperands()-1; // don't include \0 |
| 169 | |
| 170 | GlobalVariable *MsgGV = new GlobalVariable(Msg->getType(), true, |
| 171 | GlobalValue::InternalLinkage, |
| 172 | Msg, "abortmsg", &M); |
Chris Lattner | 1381dd8 | 2005-05-13 06:10:12 +0000 | [diff] [blame] | 173 | std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::IntTy)); |
Chris Lattner | 0c3b390 | 2004-11-13 19:07:32 +0000 | [diff] [blame] | 174 | AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, GEPIdx); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | |
Chris Lattner | 501825e | 2004-02-08 22:14:44 +0000 | [diff] [blame] | 179 | void LowerInvoke::writeAbortMessage(Instruction *IB) { |
| 180 | if (WriteFn) { |
Chris Lattner | 0c3b390 | 2004-11-13 19:07:32 +0000 | [diff] [blame] | 181 | if (AbortMessage == 0) createAbortMessage(); |
| 182 | |
Chris Lattner | 501825e | 2004-02-08 22:14:44 +0000 | [diff] [blame] | 183 | // These are the arguments we WANT... |
| 184 | std::vector<Value*> Args; |
| 185 | Args.push_back(ConstantInt::get(Type::IntTy, 2)); |
| 186 | Args.push_back(AbortMessage); |
| 187 | Args.push_back(ConstantInt::get(Type::IntTy, AbortMessageLength)); |
| 188 | |
| 189 | // If the actual declaration of write disagrees, insert casts as |
| 190 | // appropriate. |
| 191 | const FunctionType *FT = WriteFn->getFunctionType(); |
| 192 | unsigned NumArgs = FT->getNumParams(); |
| 193 | for (unsigned i = 0; i != 3; ++i) |
| 194 | if (i < NumArgs && FT->getParamType(i) != Args[i]->getType()) |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 195 | Args[i] = ConstantExpr::getCast(cast<Constant>(Args[i]), |
Chris Lattner | 501825e | 2004-02-08 22:14:44 +0000 | [diff] [blame] | 196 | FT->getParamType(i)); |
| 197 | |
Chris Lattner | a9e9211 | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 198 | (new CallInst(WriteFn, Args, "", IB))->setTailCall(); |
Chris Lattner | 501825e | 2004-02-08 22:14:44 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 202 | bool LowerInvoke::insertCheapEHSupport(Function &F) { |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 203 | bool Changed = false; |
Chris Lattner | dead993 | 2003-12-10 20:22:42 +0000 | [diff] [blame] | 204 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 205 | if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) { |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 206 | // Insert a normal call instruction... |
| 207 | std::string Name = II->getName(); II->setName(""); |
Chris Lattner | efd9168 | 2005-05-13 06:27:02 +0000 | [diff] [blame] | 208 | CallInst *NewCall = new CallInst(II->getCalledValue(), |
| 209 | std::vector<Value*>(II->op_begin()+3, |
| 210 | II->op_end()), Name, II); |
| 211 | NewCall->setCallingConv(II->getCallingConv()); |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 212 | II->replaceAllUsesWith(NewCall); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 213 | |
Chris Lattner | dead993 | 2003-12-10 20:22:42 +0000 | [diff] [blame] | 214 | // Insert an unconditional branch to the normal destination. |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 215 | new BranchInst(II->getNormalDest(), II); |
| 216 | |
Chris Lattner | dead993 | 2003-12-10 20:22:42 +0000 | [diff] [blame] | 217 | // Remove any PHI node entries from the exception destination. |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 218 | II->getUnwindDest()->removePredecessor(BB); |
Chris Lattner | dead993 | 2003-12-10 20:22:42 +0000 | [diff] [blame] | 219 | |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 220 | // Remove the invoke instruction now. |
Chris Lattner | dead993 | 2003-12-10 20:22:42 +0000 | [diff] [blame] | 221 | BB->getInstList().erase(II); |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 222 | |
| 223 | ++NumLowered; Changed = true; |
Chris Lattner | dead993 | 2003-12-10 20:22:42 +0000 | [diff] [blame] | 224 | } else if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) { |
Chris Lattner | e1c0930 | 2004-02-08 07:30:29 +0000 | [diff] [blame] | 225 | // Insert a new call to write(2, AbortMessage, AbortMessageLength); |
Chris Lattner | 501825e | 2004-02-08 22:14:44 +0000 | [diff] [blame] | 226 | writeAbortMessage(UI); |
Chris Lattner | e1c0930 | 2004-02-08 07:30:29 +0000 | [diff] [blame] | 227 | |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 228 | // Insert a call to abort() |
Chris Lattner | a9e9211 | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 229 | (new CallInst(AbortFn, std::vector<Value*>(), "", UI))->setTailCall(); |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 230 | |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 231 | // Insert a return instruction. This really should be a "barrier", as it |
| 232 | // is unreachable. |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 233 | new ReturnInst(F.getReturnType() == Type::VoidTy ? 0 : |
| 234 | Constant::getNullValue(F.getReturnType()), UI); |
| 235 | |
| 236 | // Remove the unwind instruction now. |
Chris Lattner | dead993 | 2003-12-10 20:22:42 +0000 | [diff] [blame] | 237 | BB->getInstList().erase(UI); |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 238 | |
| 239 | ++NumLowered; Changed = true; |
| 240 | } |
| 241 | return Changed; |
| 242 | } |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 243 | |
| 244 | bool LowerInvoke::insertExpensiveEHSupport(Function &F) { |
| 245 | bool Changed = false; |
| 246 | |
| 247 | // If a function uses invoke, we have an alloca for the jump buffer. |
| 248 | AllocaInst *JmpBuf = 0; |
| 249 | |
| 250 | // If this function contains an unwind instruction, two blocks get added: one |
| 251 | // to actually perform the longjmp, and one to terminate the program if there |
| 252 | // is no handler. |
| 253 | BasicBlock *UnwindBlock = 0, *TermBlock = 0; |
| 254 | std::vector<LoadInst*> JBPtrs; |
| 255 | |
| 256 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 257 | if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) { |
| 258 | if (JmpBuf == 0) |
| 259 | JmpBuf = new AllocaInst(JBLinkTy, 0, "jblink", F.begin()->begin()); |
| 260 | |
| 261 | // On the entry to the invoke, we must install our JmpBuf as the top of |
| 262 | // the stack. |
| 263 | LoadInst *OldEntry = new LoadInst(JBListHead, "oldehlist", II); |
| 264 | |
| 265 | // Store this old value as our 'next' field, and store our alloca as the |
| 266 | // current jblist. |
| 267 | std::vector<Value*> Idx; |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 268 | Idx.push_back(Constant::getNullValue(Type::IntTy)); |
| 269 | Idx.push_back(ConstantUInt::get(Type::UIntTy, 0)); |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 270 | Value *NextFieldPtr = new GetElementPtrInst(JmpBuf, Idx, "NextField", II); |
| 271 | new StoreInst(OldEntry, NextFieldPtr, II); |
| 272 | new StoreInst(JmpBuf, JBListHead, II); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 273 | |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 274 | // Call setjmp, passing in the address of the jmpbuffer. |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 275 | Idx[1] = ConstantUInt::get(Type::UIntTy, 1); |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 276 | Value *JmpBufPtr = new GetElementPtrInst(JmpBuf, Idx, "TheJmpBuf", II); |
| 277 | Value *SJRet = new CallInst(SetJmpFn, JmpBufPtr, "sjret", II); |
| 278 | |
| 279 | // Compare the return value to zero. |
| 280 | Value *IsNormal = BinaryOperator::create(Instruction::SetEQ, SJRet, |
| 281 | Constant::getNullValue(SJRet->getType()), |
| 282 | "notunwind", II); |
| 283 | // Create the receiver block if there is a critical edge to the normal |
| 284 | // destination. |
| 285 | SplitCriticalEdge(II, 0, this); |
| 286 | Instruction *InsertLoc = II->getNormalDest()->begin(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 287 | |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 288 | // Insert a normal call instruction on the normal execution path. |
| 289 | std::string Name = II->getName(); II->setName(""); |
Chris Lattner | efd9168 | 2005-05-13 06:27:02 +0000 | [diff] [blame] | 290 | CallInst *NewCall = new CallInst(II->getCalledValue(), |
| 291 | std::vector<Value*>(II->op_begin()+3, |
| 292 | II->op_end()), Name, |
| 293 | InsertLoc); |
| 294 | NewCall->setCallingConv(II->getCallingConv()); |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 295 | II->replaceAllUsesWith(NewCall); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 296 | |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 297 | // If we got this far, then no exception was thrown and we can pop our |
| 298 | // jmpbuf entry off. |
| 299 | new StoreInst(OldEntry, JBListHead, InsertLoc); |
| 300 | |
| 301 | // Now we change the invoke into a branch instruction. |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 302 | new BranchInst(II->getNormalDest(), II->getUnwindDest(), IsNormal, II); |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 303 | |
| 304 | // Remove the InvokeInst now. |
| 305 | BB->getInstList().erase(II); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 306 | ++NumLowered; Changed = true; |
| 307 | |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 308 | } else if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) { |
| 309 | if (UnwindBlock == 0) { |
| 310 | // Create two new blocks, the unwind block and the terminate block. Add |
| 311 | // them at the end of the function because they are not hot. |
| 312 | UnwindBlock = new BasicBlock("unwind", &F); |
| 313 | TermBlock = new BasicBlock("unwinderror", &F); |
| 314 | |
| 315 | // Insert return instructions. These really should be "barrier"s, as |
| 316 | // they are unreachable. |
| 317 | new ReturnInst(F.getReturnType() == Type::VoidTy ? 0 : |
| 318 | Constant::getNullValue(F.getReturnType()), UnwindBlock); |
| 319 | new ReturnInst(F.getReturnType() == Type::VoidTy ? 0 : |
| 320 | Constant::getNullValue(F.getReturnType()), TermBlock); |
| 321 | } |
| 322 | |
| 323 | // Load the JBList, if it's null, then there was no catch! |
| 324 | LoadInst *Ptr = new LoadInst(JBListHead, "ehlist", UI); |
| 325 | Value *NotNull = BinaryOperator::create(Instruction::SetNE, Ptr, |
| 326 | Constant::getNullValue(Ptr->getType()), |
| 327 | "notnull", UI); |
| 328 | new BranchInst(UnwindBlock, TermBlock, NotNull, UI); |
| 329 | |
| 330 | // Remember the loaded value so we can insert the PHI node as needed. |
| 331 | JBPtrs.push_back(Ptr); |
| 332 | |
| 333 | // Remove the UnwindInst now. |
| 334 | BB->getInstList().erase(UI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 335 | ++NumLowered; Changed = true; |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | // If an unwind instruction was inserted, we need to set up the Unwind and |
| 339 | // term blocks. |
| 340 | if (UnwindBlock) { |
| 341 | // In the unwind block, we know that the pointer coming in on the JBPtrs |
| 342 | // list are non-null. |
| 343 | Instruction *RI = UnwindBlock->getTerminator(); |
| 344 | |
| 345 | Value *RecPtr; |
| 346 | if (JBPtrs.size() == 1) |
| 347 | RecPtr = JBPtrs[0]; |
| 348 | else { |
| 349 | // If there is more than one unwind in this function, make a PHI node to |
| 350 | // merge in all of the loaded values. |
| 351 | PHINode *PN = new PHINode(JBPtrs[0]->getType(), "jbptrs", RI); |
| 352 | for (unsigned i = 0, e = JBPtrs.size(); i != e; ++i) |
| 353 | PN->addIncoming(JBPtrs[i], JBPtrs[i]->getParent()); |
| 354 | RecPtr = PN; |
| 355 | } |
| 356 | |
| 357 | // Now that we have a pointer to the whole record, remove the entry from the |
| 358 | // JBList. |
| 359 | std::vector<Value*> Idx; |
Chris Lattner | 1381dd8 | 2005-05-13 06:10:12 +0000 | [diff] [blame] | 360 | Idx.push_back(Constant::getNullValue(Type::IntTy)); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 361 | Idx.push_back(ConstantUInt::get(Type::UIntTy, 0)); |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 362 | Value *NextFieldPtr = new GetElementPtrInst(RecPtr, Idx, "NextField", RI); |
| 363 | Value *NextRec = new LoadInst(NextFieldPtr, "NextRecord", RI); |
| 364 | new StoreInst(NextRec, JBListHead, RI); |
| 365 | |
| 366 | // Now that we popped the top of the JBList, get a pointer to the jmpbuf and |
| 367 | // longjmp. |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 368 | Idx[1] = ConstantUInt::get(Type::UIntTy, 1); |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 369 | Idx[0] = new GetElementPtrInst(RecPtr, Idx, "JmpBuf", RI); |
| 370 | Idx[1] = ConstantInt::get(Type::IntTy, 1); |
| 371 | new CallInst(LongJmpFn, Idx, "", RI); |
| 372 | |
| 373 | // Now we set up the terminate block. |
| 374 | RI = TermBlock->getTerminator(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 375 | |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 376 | // Insert a new call to write(2, AbortMessage, AbortMessageLength); |
Chris Lattner | 501825e | 2004-02-08 22:14:44 +0000 | [diff] [blame] | 377 | writeAbortMessage(RI); |
| 378 | |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 379 | // Insert a call to abort() |
Chris Lattner | a9e9211 | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 380 | (new CallInst(AbortFn, std::vector<Value*>(), "", RI))->setTailCall(); |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | return Changed; |
| 384 | } |
| 385 | |
| 386 | bool LowerInvoke::runOnFunction(Function &F) { |
| 387 | if (ExpensiveEHSupport) |
| 388 | return insertExpensiveEHSupport(F); |
| 389 | else |
| 390 | return insertCheapEHSupport(F); |
| 391 | } |