Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 1 | //===- LowerInvoke.cpp - Eliminate Invoke & Unwind instructions -----------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 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. |
| 7 | // |
| 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 |
| 21 | // support to the program at making the 'invoke' instruction really expensive. |
| 22 | // It basically inserts setjmp/longjmp calls to emulate the exception handling |
| 23 | // as necessary. |
| 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" |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 44 | #include "Support/Statistic.h" |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 45 | #include "Support/CommandLine.h" |
| 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 | f1d0d35 | 2004-02-09 22:48:47 +0000 | [diff] [blame] | 58 | Constant *AbortMessageInit; |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 59 | Value *AbortMessage; |
| 60 | unsigned AbortMessageLength; |
| 61 | |
| 62 | // Used for expensive EH support. |
| 63 | const Type *JBLinkTy; |
| 64 | GlobalVariable *JBListHead; |
| 65 | Function *SetJmpFn, *LongJmpFn; |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 66 | public: |
| 67 | bool doInitialization(Module &M); |
| 68 | bool runOnFunction(Function &F); |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 69 | private: |
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 | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 119 | PointerType::get(JmpBufTy), 0); |
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), |
| 122 | Type::IntTy, 0); |
| 123 | |
| 124 | // The abort message for expensive EH support tells the user that the |
| 125 | // program 'unwound' without an 'invoke' instruction. |
| 126 | Constant *Msg = |
| 127 | ConstantArray::get("ERROR: Exception thrown, but not caught!\n"); |
| 128 | AbortMessageLength = Msg->getNumOperands()-1; // don't include \0 |
Chris Lattner | f1d0d35 | 2004-02-09 22:48:47 +0000 | [diff] [blame] | 129 | AbortMessageInit = Msg; |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 130 | |
| 131 | GlobalVariable *MsgGV = M.getGlobalVariable("abort.msg", Msg->getType()); |
| 132 | if (MsgGV && (!MsgGV->hasInitializer() || MsgGV->getInitializer() != Msg)) |
| 133 | MsgGV = 0; |
Chris Lattner | f1d0d35 | 2004-02-09 22:48:47 +0000 | [diff] [blame] | 134 | |
| 135 | if (MsgGV) { |
| 136 | std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::LongTy)); |
| 137 | AbortMessage = |
Reid Spencer | 518310c | 2004-07-18 00:44:37 +0000 | [diff] [blame] | 138 | ConstantExpr::getGetElementPtr(MsgGV, GEPIdx); |
Chris Lattner | f1d0d35 | 2004-02-09 22:48:47 +0000 | [diff] [blame] | 139 | } |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 140 | |
| 141 | } else { |
| 142 | // The abort message for cheap EH support tells the user that EH is not |
| 143 | // enabled. |
| 144 | Constant *Msg = |
| 145 | ConstantArray::get("Exception handler needed, but not enabled. Recompile" |
| 146 | " program with -enable-correct-eh-support.\n"); |
| 147 | AbortMessageLength = Msg->getNumOperands()-1; // don't include \0 |
Chris Lattner | f1d0d35 | 2004-02-09 22:48:47 +0000 | [diff] [blame] | 148 | AbortMessageInit = Msg; |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 149 | |
| 150 | GlobalVariable *MsgGV = M.getGlobalVariable("abort.msg", Msg->getType()); |
| 151 | if (MsgGV && (!MsgGV->hasInitializer() || MsgGV->getInitializer() != Msg)) |
| 152 | MsgGV = 0; |
| 153 | |
Chris Lattner | f1d0d35 | 2004-02-09 22:48:47 +0000 | [diff] [blame] | 154 | if (MsgGV) { |
| 155 | std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::LongTy)); |
| 156 | AbortMessage = |
Reid Spencer | 518310c | 2004-07-18 00:44:37 +0000 | [diff] [blame] | 157 | ConstantExpr::getGetElementPtr(MsgGV, GEPIdx); |
Chris Lattner | f1d0d35 | 2004-02-09 22:48:47 +0000 | [diff] [blame] | 158 | } |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // We need the 'write' and 'abort' functions for both models. |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 162 | AbortFn = M.getOrInsertFunction("abort", Type::VoidTy, 0); |
Chris Lattner | 501825e | 2004-02-08 22:14:44 +0000 | [diff] [blame] | 163 | |
| 164 | // Unfortunately, 'write' can end up being prototyped in several different |
| 165 | // 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] | 166 | // 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] | 167 | // of a write prototype, because we don't know that the funcresolve pass will |
| 168 | // run after us. If there is a definition of a write function, but it's not |
| 169 | // suitable for our uses, we just don't emit write calls. If there is no |
| 170 | // write prototype at all, we just add one. |
| 171 | if (Function *WF = M.getNamedFunction("write")) { |
| 172 | if (WF->getFunctionType()->getNumParams() > 3 || |
| 173 | WF->getFunctionType()->isVarArg()) |
| 174 | WriteFn = WF; |
| 175 | else |
| 176 | WriteFn = 0; |
| 177 | } else { |
| 178 | WriteFn = M.getOrInsertFunction("write", Type::VoidTy, Type::IntTy, |
| 179 | VoidPtrTy, Type::IntTy, 0); |
| 180 | } |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 181 | return true; |
| 182 | } |
| 183 | |
Chris Lattner | 501825e | 2004-02-08 22:14:44 +0000 | [diff] [blame] | 184 | void LowerInvoke::writeAbortMessage(Instruction *IB) { |
| 185 | if (WriteFn) { |
Chris Lattner | f1d0d35 | 2004-02-09 22:48:47 +0000 | [diff] [blame] | 186 | if (!AbortMessage) { |
| 187 | GlobalVariable *MsgGV = new GlobalVariable(AbortMessageInit->getType(), |
| 188 | true, |
| 189 | GlobalValue::InternalLinkage, |
| 190 | AbortMessageInit, "abort.msg", |
| 191 | WriteFn->getParent()); |
| 192 | std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::LongTy)); |
| 193 | AbortMessage = |
Reid Spencer | 518310c | 2004-07-18 00:44:37 +0000 | [diff] [blame] | 194 | ConstantExpr::getGetElementPtr(MsgGV, GEPIdx); |
Chris Lattner | f1d0d35 | 2004-02-09 22:48:47 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Chris Lattner | 501825e | 2004-02-08 22:14:44 +0000 | [diff] [blame] | 197 | // These are the arguments we WANT... |
| 198 | std::vector<Value*> Args; |
| 199 | Args.push_back(ConstantInt::get(Type::IntTy, 2)); |
| 200 | Args.push_back(AbortMessage); |
| 201 | Args.push_back(ConstantInt::get(Type::IntTy, AbortMessageLength)); |
| 202 | |
| 203 | // If the actual declaration of write disagrees, insert casts as |
| 204 | // appropriate. |
| 205 | const FunctionType *FT = WriteFn->getFunctionType(); |
| 206 | unsigned NumArgs = FT->getNumParams(); |
| 207 | for (unsigned i = 0; i != 3; ++i) |
| 208 | if (i < NumArgs && FT->getParamType(i) != Args[i]->getType()) |
| 209 | Args[i] = ConstantExpr::getCast(cast<Constant>(Args[i]), |
| 210 | FT->getParamType(i)); |
| 211 | |
| 212 | new CallInst(WriteFn, Args, "", IB); |
| 213 | } |
| 214 | } |
| 215 | |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 216 | bool LowerInvoke::insertCheapEHSupport(Function &F) { |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 217 | bool Changed = false; |
Chris Lattner | dead993 | 2003-12-10 20:22:42 +0000 | [diff] [blame] | 218 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 219 | if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) { |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 220 | // Insert a normal call instruction... |
| 221 | std::string Name = II->getName(); II->setName(""); |
| 222 | Value *NewCall = new CallInst(II->getCalledValue(), |
| 223 | std::vector<Value*>(II->op_begin()+3, |
| 224 | II->op_end()), Name,II); |
| 225 | II->replaceAllUsesWith(NewCall); |
| 226 | |
Chris Lattner | dead993 | 2003-12-10 20:22:42 +0000 | [diff] [blame] | 227 | // Insert an unconditional branch to the normal destination. |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 228 | new BranchInst(II->getNormalDest(), II); |
| 229 | |
Chris Lattner | dead993 | 2003-12-10 20:22:42 +0000 | [diff] [blame] | 230 | // Remove any PHI node entries from the exception destination. |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 231 | II->getUnwindDest()->removePredecessor(BB); |
Chris Lattner | dead993 | 2003-12-10 20:22:42 +0000 | [diff] [blame] | 232 | |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 233 | // Remove the invoke instruction now. |
Chris Lattner | dead993 | 2003-12-10 20:22:42 +0000 | [diff] [blame] | 234 | BB->getInstList().erase(II); |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 235 | |
| 236 | ++NumLowered; Changed = true; |
Chris Lattner | dead993 | 2003-12-10 20:22:42 +0000 | [diff] [blame] | 237 | } else if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) { |
Chris Lattner | e1c0930 | 2004-02-08 07:30:29 +0000 | [diff] [blame] | 238 | // Insert a new call to write(2, AbortMessage, AbortMessageLength); |
Chris Lattner | 501825e | 2004-02-08 22:14:44 +0000 | [diff] [blame] | 239 | writeAbortMessage(UI); |
Chris Lattner | e1c0930 | 2004-02-08 07:30:29 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 241 | // Insert a call to abort() |
| 242 | new CallInst(AbortFn, std::vector<Value*>(), "", UI); |
| 243 | |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 244 | // Insert a return instruction. This really should be a "barrier", as it |
| 245 | // is unreachable. |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 246 | new ReturnInst(F.getReturnType() == Type::VoidTy ? 0 : |
| 247 | Constant::getNullValue(F.getReturnType()), UI); |
| 248 | |
| 249 | // Remove the unwind instruction now. |
Chris Lattner | dead993 | 2003-12-10 20:22:42 +0000 | [diff] [blame] | 250 | BB->getInstList().erase(UI); |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 251 | |
| 252 | ++NumLowered; Changed = true; |
| 253 | } |
| 254 | return Changed; |
| 255 | } |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 256 | |
| 257 | bool LowerInvoke::insertExpensiveEHSupport(Function &F) { |
| 258 | bool Changed = false; |
| 259 | |
| 260 | // If a function uses invoke, we have an alloca for the jump buffer. |
| 261 | AllocaInst *JmpBuf = 0; |
| 262 | |
| 263 | // If this function contains an unwind instruction, two blocks get added: one |
| 264 | // to actually perform the longjmp, and one to terminate the program if there |
| 265 | // is no handler. |
| 266 | BasicBlock *UnwindBlock = 0, *TermBlock = 0; |
| 267 | std::vector<LoadInst*> JBPtrs; |
| 268 | |
| 269 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 270 | if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) { |
| 271 | if (JmpBuf == 0) |
| 272 | JmpBuf = new AllocaInst(JBLinkTy, 0, "jblink", F.begin()->begin()); |
| 273 | |
| 274 | // On the entry to the invoke, we must install our JmpBuf as the top of |
| 275 | // the stack. |
| 276 | LoadInst *OldEntry = new LoadInst(JBListHead, "oldehlist", II); |
| 277 | |
| 278 | // Store this old value as our 'next' field, and store our alloca as the |
| 279 | // current jblist. |
| 280 | std::vector<Value*> Idx; |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 281 | Idx.push_back(Constant::getNullValue(Type::IntTy)); |
| 282 | Idx.push_back(ConstantUInt::get(Type::UIntTy, 0)); |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 283 | Value *NextFieldPtr = new GetElementPtrInst(JmpBuf, Idx, "NextField", II); |
| 284 | new StoreInst(OldEntry, NextFieldPtr, II); |
| 285 | new StoreInst(JmpBuf, JBListHead, II); |
| 286 | |
| 287 | // Call setjmp, passing in the address of the jmpbuffer. |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 288 | Idx[1] = ConstantUInt::get(Type::UIntTy, 1); |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 289 | Value *JmpBufPtr = new GetElementPtrInst(JmpBuf, Idx, "TheJmpBuf", II); |
| 290 | Value *SJRet = new CallInst(SetJmpFn, JmpBufPtr, "sjret", II); |
| 291 | |
| 292 | // Compare the return value to zero. |
| 293 | Value *IsNormal = BinaryOperator::create(Instruction::SetEQ, SJRet, |
| 294 | Constant::getNullValue(SJRet->getType()), |
| 295 | "notunwind", II); |
| 296 | // Create the receiver block if there is a critical edge to the normal |
| 297 | // destination. |
| 298 | SplitCriticalEdge(II, 0, this); |
| 299 | Instruction *InsertLoc = II->getNormalDest()->begin(); |
| 300 | |
| 301 | // Insert a normal call instruction on the normal execution path. |
| 302 | std::string Name = II->getName(); II->setName(""); |
| 303 | Value *NewCall = new CallInst(II->getCalledValue(), |
| 304 | std::vector<Value*>(II->op_begin()+3, |
| 305 | II->op_end()), Name, |
| 306 | InsertLoc); |
| 307 | II->replaceAllUsesWith(NewCall); |
| 308 | |
| 309 | // If we got this far, then no exception was thrown and we can pop our |
| 310 | // jmpbuf entry off. |
| 311 | new StoreInst(OldEntry, JBListHead, InsertLoc); |
| 312 | |
| 313 | // Now we change the invoke into a branch instruction. |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 314 | new BranchInst(II->getNormalDest(), II->getUnwindDest(), IsNormal, II); |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 315 | |
| 316 | // Remove the InvokeInst now. |
| 317 | BB->getInstList().erase(II); |
| 318 | ++NumLowered; Changed = true; |
| 319 | |
| 320 | } else if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) { |
| 321 | if (UnwindBlock == 0) { |
| 322 | // Create two new blocks, the unwind block and the terminate block. Add |
| 323 | // them at the end of the function because they are not hot. |
| 324 | UnwindBlock = new BasicBlock("unwind", &F); |
| 325 | TermBlock = new BasicBlock("unwinderror", &F); |
| 326 | |
| 327 | // Insert return instructions. These really should be "barrier"s, as |
| 328 | // they are unreachable. |
| 329 | new ReturnInst(F.getReturnType() == Type::VoidTy ? 0 : |
| 330 | Constant::getNullValue(F.getReturnType()), UnwindBlock); |
| 331 | new ReturnInst(F.getReturnType() == Type::VoidTy ? 0 : |
| 332 | Constant::getNullValue(F.getReturnType()), TermBlock); |
| 333 | } |
| 334 | |
| 335 | // Load the JBList, if it's null, then there was no catch! |
| 336 | LoadInst *Ptr = new LoadInst(JBListHead, "ehlist", UI); |
| 337 | Value *NotNull = BinaryOperator::create(Instruction::SetNE, Ptr, |
| 338 | Constant::getNullValue(Ptr->getType()), |
| 339 | "notnull", UI); |
| 340 | new BranchInst(UnwindBlock, TermBlock, NotNull, UI); |
| 341 | |
| 342 | // Remember the loaded value so we can insert the PHI node as needed. |
| 343 | JBPtrs.push_back(Ptr); |
| 344 | |
| 345 | // Remove the UnwindInst now. |
| 346 | BB->getInstList().erase(UI); |
| 347 | ++NumLowered; Changed = true; |
| 348 | } |
| 349 | |
| 350 | // If an unwind instruction was inserted, we need to set up the Unwind and |
| 351 | // term blocks. |
| 352 | if (UnwindBlock) { |
| 353 | // In the unwind block, we know that the pointer coming in on the JBPtrs |
| 354 | // list are non-null. |
| 355 | Instruction *RI = UnwindBlock->getTerminator(); |
| 356 | |
| 357 | Value *RecPtr; |
| 358 | if (JBPtrs.size() == 1) |
| 359 | RecPtr = JBPtrs[0]; |
| 360 | else { |
| 361 | // If there is more than one unwind in this function, make a PHI node to |
| 362 | // merge in all of the loaded values. |
| 363 | PHINode *PN = new PHINode(JBPtrs[0]->getType(), "jbptrs", RI); |
| 364 | for (unsigned i = 0, e = JBPtrs.size(); i != e; ++i) |
| 365 | PN->addIncoming(JBPtrs[i], JBPtrs[i]->getParent()); |
| 366 | RecPtr = PN; |
| 367 | } |
| 368 | |
| 369 | // Now that we have a pointer to the whole record, remove the entry from the |
| 370 | // JBList. |
| 371 | std::vector<Value*> Idx; |
| 372 | Idx.push_back(Constant::getNullValue(Type::LongTy)); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 373 | Idx.push_back(ConstantUInt::get(Type::UIntTy, 0)); |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 374 | Value *NextFieldPtr = new GetElementPtrInst(RecPtr, Idx, "NextField", RI); |
| 375 | Value *NextRec = new LoadInst(NextFieldPtr, "NextRecord", RI); |
| 376 | new StoreInst(NextRec, JBListHead, RI); |
| 377 | |
| 378 | // Now that we popped the top of the JBList, get a pointer to the jmpbuf and |
| 379 | // longjmp. |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 380 | Idx[1] = ConstantUInt::get(Type::UIntTy, 1); |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 381 | Idx[0] = new GetElementPtrInst(RecPtr, Idx, "JmpBuf", RI); |
| 382 | Idx[1] = ConstantInt::get(Type::IntTy, 1); |
| 383 | new CallInst(LongJmpFn, Idx, "", RI); |
| 384 | |
| 385 | // Now we set up the terminate block. |
| 386 | RI = TermBlock->getTerminator(); |
| 387 | |
| 388 | // Insert a new call to write(2, AbortMessage, AbortMessageLength); |
Chris Lattner | 501825e | 2004-02-08 22:14:44 +0000 | [diff] [blame] | 389 | writeAbortMessage(RI); |
| 390 | |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 391 | // Insert a call to abort() |
| 392 | new CallInst(AbortFn, std::vector<Value*>(), "", RI); |
| 393 | } |
| 394 | |
| 395 | return Changed; |
| 396 | } |
| 397 | |
| 398 | bool LowerInvoke::runOnFunction(Function &F) { |
| 399 | if (ExpensiveEHSupport) |
| 400 | return insertExpensiveEHSupport(F); |
| 401 | else |
| 402 | return insertCheapEHSupport(F); |
| 403 | } |