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" |
Chris Lattner | f4e6c3a | 2005-09-27 21:18:17 +0000 | [diff] [blame] | 44 | #include "llvm/Transforms/Utils/Local.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 45 | #include "llvm/ADT/Statistic.h" |
| 46 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Compiler.h" |
Duraid Madina | 2a0013f | 2006-09-04 06:21:35 +0000 | [diff] [blame] | 48 | #include "llvm/Target/TargetLowering.h" |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 49 | #include <csetjmp> |
Chris Lattner | dead993 | 2003-12-10 20:22:42 +0000 | [diff] [blame] | 50 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 52 | namespace { |
Chris Lattner | f4e6c3a | 2005-09-27 21:18:17 +0000 | [diff] [blame] | 53 | Statistic<> NumInvokes("lowerinvoke", "Number of invokes replaced"); |
| 54 | Statistic<> NumUnwinds("lowerinvoke", "Number of unwinds replaced"); |
| 55 | Statistic<> NumSpilled("lowerinvoke", |
| 56 | "Number of registers live across unwind edges"); |
Chris Lattner | 99cca7d | 2004-03-01 01:12:13 +0000 | [diff] [blame] | 57 | cl::opt<bool> ExpensiveEHSupport("enable-correct-eh-support", |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 58 | cl::desc("Make the -lowerinvoke pass insert expensive, but correct, EH code")); |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 59 | |
Chris Lattner | f4b5461 | 2006-06-28 22:08:15 +0000 | [diff] [blame] | 60 | class VISIBILITY_HIDDEN LowerInvoke : public FunctionPass { |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 61 | // Used for both models. |
Chris Lattner | e1c0930 | 2004-02-08 07:30:29 +0000 | [diff] [blame] | 62 | Function *WriteFn; |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 63 | Function *AbortFn; |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 64 | Value *AbortMessage; |
| 65 | unsigned AbortMessageLength; |
| 66 | |
| 67 | // Used for expensive EH support. |
| 68 | const Type *JBLinkTy; |
| 69 | GlobalVariable *JBListHead; |
| 70 | Function *SetJmpFn, *LongJmpFn; |
Duraid Madina | 2a0013f | 2006-09-04 06:21:35 +0000 | [diff] [blame] | 71 | |
| 72 | // We peek in TLI to grab the target's jmp_buf size and alignment |
| 73 | const TargetLowering *TLI; |
| 74 | |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 75 | public: |
Duraid Madina | 2a0013f | 2006-09-04 06:21:35 +0000 | [diff] [blame] | 76 | LowerInvoke(const TargetLowering *tli = NULL) : TLI(tli) { } |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 77 | bool doInitialization(Module &M); |
| 78 | bool runOnFunction(Function &F); |
Chris Lattner | ed96fe8 | 2006-05-17 21:05:27 +0000 | [diff] [blame] | 79 | |
| 80 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 81 | // This is a cluster of orthogonal Transforms |
| 82 | AU.addPreservedID(PromoteMemoryToRegisterID); |
| 83 | AU.addPreservedID(LowerSelectID); |
| 84 | AU.addPreservedID(LowerSwitchID); |
| 85 | AU.addPreservedID(LowerAllocationsID); |
| 86 | } |
| 87 | |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 88 | private: |
Chris Lattner | 0c3b390 | 2004-11-13 19:07:32 +0000 | [diff] [blame] | 89 | void createAbortMessage(); |
Chris Lattner | 501825e | 2004-02-08 22:14:44 +0000 | [diff] [blame] | 90 | void writeAbortMessage(Instruction *IB); |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 91 | bool insertCheapEHSupport(Function &F); |
Chris Lattner | f4e6c3a | 2005-09-27 21:18:17 +0000 | [diff] [blame] | 92 | void splitLiveRangesLiveAcrossInvokes(std::vector<InvokeInst*> &Invokes); |
| 93 | void rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo, |
| 94 | AllocaInst *InvokeNum, SwitchInst *CatchSwitch); |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 95 | bool insertExpensiveEHSupport(Function &F); |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 98 | RegisterPass<LowerInvoke> |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 99 | X("lowerinvoke", "Lower invoke and unwind, for unwindless code generators"); |
| 100 | } |
| 101 | |
Chris Lattner | cefc18e | 2004-02-13 16:16:16 +0000 | [diff] [blame] | 102 | const PassInfo *llvm::LowerInvokePassID = X.getPassInfo(); |
| 103 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 104 | // Public Interface To the LowerInvoke pass. |
Duraid Madina | 2a0013f | 2006-09-04 06:21:35 +0000 | [diff] [blame] | 105 | FunctionPass *llvm::createLowerInvokePass(const TargetLowering *TLI) { |
| 106 | return new LowerInvoke(TLI); |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 107 | } |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 108 | |
| 109 | // doInitialization - Make sure that there is a prototype for abort in the |
| 110 | // current module. |
| 111 | bool LowerInvoke::doInitialization(Module &M) { |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 112 | const Type *VoidPtrTy = PointerType::get(Type::SByteTy); |
Chris Lattner | f1d0d35 | 2004-02-09 22:48:47 +0000 | [diff] [blame] | 113 | AbortMessage = 0; |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 114 | if (ExpensiveEHSupport) { |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 115 | // Insert a type for the linked list of jump buffers. |
Chris Lattner | 97d2dbd | 2006-09-05 17:48:07 +0000 | [diff] [blame] | 116 | unsigned JBSize = TLI ? TLI->getJumpBufSize() : 0; |
| 117 | JBSize = JBSize ? JBSize : 200; |
| 118 | const Type *JmpBufTy = ArrayType::get(VoidPtrTy, JBSize); |
Chris Lattner | e1c0930 | 2004-02-08 07:30:29 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 120 | { // The type is recursive, so use a type holder. |
| 121 | std::vector<const Type*> Elements; |
Chris Lattner | f4e6c3a | 2005-09-27 21:18:17 +0000 | [diff] [blame] | 122 | Elements.push_back(JmpBufTy); |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 123 | OpaqueType *OT = OpaqueType::get(); |
| 124 | Elements.push_back(PointerType::get(OT)); |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 125 | PATypeHolder JBLType(StructType::get(Elements)); |
| 126 | OT->refineAbstractTypeTo(JBLType.get()); // Complete the cycle. |
| 127 | JBLinkTy = JBLType.get(); |
Chris Lattner | 11b9be5 | 2004-05-28 05:02:13 +0000 | [diff] [blame] | 128 | M.addTypeName("llvm.sjljeh.jmpbufty", JBLinkTy); |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | const Type *PtrJBList = PointerType::get(JBLinkTy); |
| 132 | |
| 133 | // Now that we've done that, insert the jmpbuf list head global, unless it |
| 134 | // already exists. |
Chris Lattner | 97d2dbd | 2006-09-05 17:48:07 +0000 | [diff] [blame] | 135 | if (!(JBListHead = M.getGlobalVariable("llvm.sjljeh.jblist", PtrJBList))) { |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 136 | JBListHead = new GlobalVariable(PtrJBList, false, |
| 137 | GlobalValue::LinkOnceLinkage, |
| 138 | Constant::getNullValue(PtrJBList), |
| 139 | "llvm.sjljeh.jblist", &M); |
Chris Lattner | 97d2dbd | 2006-09-05 17:48:07 +0000 | [diff] [blame] | 140 | } |
Chris Lattner | 860a161 | 2004-02-15 22:24:27 +0000 | [diff] [blame] | 141 | SetJmpFn = M.getOrInsertFunction("llvm.setjmp", Type::IntTy, |
Jeff Cohen | 66c5fd6 | 2005-10-23 04:37:20 +0000 | [diff] [blame] | 142 | PointerType::get(JmpBufTy), (Type *)0); |
Chris Lattner | 860a161 | 2004-02-15 22:24:27 +0000 | [diff] [blame] | 143 | LongJmpFn = M.getOrInsertFunction("llvm.longjmp", Type::VoidTy, |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 144 | PointerType::get(JmpBufTy), |
Jeff Cohen | 66c5fd6 | 2005-10-23 04:37:20 +0000 | [diff] [blame] | 145 | Type::IntTy, (Type *)0); |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // We need the 'write' and 'abort' functions for both models. |
Jeff Cohen | 66c5fd6 | 2005-10-23 04:37:20 +0000 | [diff] [blame] | 149 | AbortFn = M.getOrInsertFunction("abort", Type::VoidTy, (Type *)0); |
Chris Lattner | 501825e | 2004-02-08 22:14:44 +0000 | [diff] [blame] | 150 | |
| 151 | // Unfortunately, 'write' can end up being prototyped in several different |
| 152 | // 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] | 153 | // 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] | 154 | // of a write prototype, because we don't know that the funcresolve pass will |
| 155 | // run after us. If there is a definition of a write function, but it's not |
| 156 | // suitable for our uses, we just don't emit write calls. If there is no |
| 157 | // write prototype at all, we just add one. |
| 158 | if (Function *WF = M.getNamedFunction("write")) { |
| 159 | if (WF->getFunctionType()->getNumParams() > 3 || |
| 160 | WF->getFunctionType()->isVarArg()) |
| 161 | WriteFn = WF; |
| 162 | else |
| 163 | WriteFn = 0; |
| 164 | } else { |
| 165 | WriteFn = M.getOrInsertFunction("write", Type::VoidTy, Type::IntTy, |
Jeff Cohen | 66c5fd6 | 2005-10-23 04:37:20 +0000 | [diff] [blame] | 166 | VoidPtrTy, Type::IntTy, (Type *)0); |
Chris Lattner | 501825e | 2004-02-08 22:14:44 +0000 | [diff] [blame] | 167 | } |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 168 | return true; |
| 169 | } |
| 170 | |
Chris Lattner | 0c3b390 | 2004-11-13 19:07:32 +0000 | [diff] [blame] | 171 | void LowerInvoke::createAbortMessage() { |
| 172 | Module &M = *WriteFn->getParent(); |
| 173 | if (ExpensiveEHSupport) { |
| 174 | // The abort message for expensive EH support tells the user that the |
| 175 | // program 'unwound' without an 'invoke' instruction. |
| 176 | Constant *Msg = |
| 177 | ConstantArray::get("ERROR: Exception thrown, but not caught!\n"); |
| 178 | AbortMessageLength = Msg->getNumOperands()-1; // don't include \0 |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 179 | |
Chris Lattner | 0c3b390 | 2004-11-13 19:07:32 +0000 | [diff] [blame] | 180 | GlobalVariable *MsgGV = new GlobalVariable(Msg->getType(), true, |
| 181 | GlobalValue::InternalLinkage, |
| 182 | Msg, "abortmsg", &M); |
Chris Lattner | 1381dd8 | 2005-05-13 06:10:12 +0000 | [diff] [blame] | 183 | std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::IntTy)); |
Chris Lattner | 0c3b390 | 2004-11-13 19:07:32 +0000 | [diff] [blame] | 184 | AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, GEPIdx); |
| 185 | } else { |
| 186 | // The abort message for cheap EH support tells the user that EH is not |
| 187 | // enabled. |
| 188 | Constant *Msg = |
| 189 | ConstantArray::get("Exception handler needed, but not enabled. Recompile" |
| 190 | " program with -enable-correct-eh-support.\n"); |
| 191 | AbortMessageLength = Msg->getNumOperands()-1; // don't include \0 |
| 192 | |
| 193 | GlobalVariable *MsgGV = new GlobalVariable(Msg->getType(), true, |
| 194 | GlobalValue::InternalLinkage, |
| 195 | Msg, "abortmsg", &M); |
Chris Lattner | 1381dd8 | 2005-05-13 06:10:12 +0000 | [diff] [blame] | 196 | std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::IntTy)); |
Chris Lattner | 0c3b390 | 2004-11-13 19:07:32 +0000 | [diff] [blame] | 197 | AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, GEPIdx); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | |
Chris Lattner | 501825e | 2004-02-08 22:14:44 +0000 | [diff] [blame] | 202 | void LowerInvoke::writeAbortMessage(Instruction *IB) { |
| 203 | if (WriteFn) { |
Chris Lattner | 0c3b390 | 2004-11-13 19:07:32 +0000 | [diff] [blame] | 204 | if (AbortMessage == 0) createAbortMessage(); |
| 205 | |
Chris Lattner | 501825e | 2004-02-08 22:14:44 +0000 | [diff] [blame] | 206 | // These are the arguments we WANT... |
| 207 | std::vector<Value*> Args; |
| 208 | Args.push_back(ConstantInt::get(Type::IntTy, 2)); |
| 209 | Args.push_back(AbortMessage); |
| 210 | Args.push_back(ConstantInt::get(Type::IntTy, AbortMessageLength)); |
| 211 | |
| 212 | // If the actual declaration of write disagrees, insert casts as |
| 213 | // appropriate. |
| 214 | const FunctionType *FT = WriteFn->getFunctionType(); |
| 215 | unsigned NumArgs = FT->getNumParams(); |
| 216 | for (unsigned i = 0; i != 3; ++i) |
| 217 | if (i < NumArgs && FT->getParamType(i) != Args[i]->getType()) |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 218 | Args[i] = ConstantExpr::getCast(cast<Constant>(Args[i]), |
Chris Lattner | 501825e | 2004-02-08 22:14:44 +0000 | [diff] [blame] | 219 | FT->getParamType(i)); |
| 220 | |
Chris Lattner | a9e9211 | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 221 | (new CallInst(WriteFn, Args, "", IB))->setTailCall(); |
Chris Lattner | 501825e | 2004-02-08 22:14:44 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 225 | bool LowerInvoke::insertCheapEHSupport(Function &F) { |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 226 | bool Changed = false; |
Chris Lattner | dead993 | 2003-12-10 20:22:42 +0000 | [diff] [blame] | 227 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 228 | if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) { |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 229 | // Insert a normal call instruction... |
| 230 | std::string Name = II->getName(); II->setName(""); |
Chris Lattner | efd9168 | 2005-05-13 06:27:02 +0000 | [diff] [blame] | 231 | CallInst *NewCall = new CallInst(II->getCalledValue(), |
| 232 | std::vector<Value*>(II->op_begin()+3, |
| 233 | II->op_end()), Name, II); |
| 234 | NewCall->setCallingConv(II->getCallingConv()); |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 235 | II->replaceAllUsesWith(NewCall); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 236 | |
Chris Lattner | dead993 | 2003-12-10 20:22:42 +0000 | [diff] [blame] | 237 | // Insert an unconditional branch to the normal destination. |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 238 | new BranchInst(II->getNormalDest(), II); |
| 239 | |
Chris Lattner | dead993 | 2003-12-10 20:22:42 +0000 | [diff] [blame] | 240 | // Remove any PHI node entries from the exception destination. |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 241 | II->getUnwindDest()->removePredecessor(BB); |
Chris Lattner | dead993 | 2003-12-10 20:22:42 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 243 | // Remove the invoke instruction now. |
Chris Lattner | dead993 | 2003-12-10 20:22:42 +0000 | [diff] [blame] | 244 | BB->getInstList().erase(II); |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 245 | |
Chris Lattner | f4e6c3a | 2005-09-27 21:18:17 +0000 | [diff] [blame] | 246 | ++NumInvokes; Changed = true; |
Chris Lattner | dead993 | 2003-12-10 20:22:42 +0000 | [diff] [blame] | 247 | } else if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) { |
Chris Lattner | e1c0930 | 2004-02-08 07:30:29 +0000 | [diff] [blame] | 248 | // Insert a new call to write(2, AbortMessage, AbortMessageLength); |
Chris Lattner | 501825e | 2004-02-08 22:14:44 +0000 | [diff] [blame] | 249 | writeAbortMessage(UI); |
Chris Lattner | e1c0930 | 2004-02-08 07:30:29 +0000 | [diff] [blame] | 250 | |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 251 | // Insert a call to abort() |
Chris Lattner | a9e9211 | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 252 | (new CallInst(AbortFn, std::vector<Value*>(), "", UI))->setTailCall(); |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 253 | |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 254 | // Insert a return instruction. This really should be a "barrier", as it |
| 255 | // is unreachable. |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 256 | new ReturnInst(F.getReturnType() == Type::VoidTy ? 0 : |
| 257 | Constant::getNullValue(F.getReturnType()), UI); |
| 258 | |
| 259 | // Remove the unwind instruction now. |
Chris Lattner | dead993 | 2003-12-10 20:22:42 +0000 | [diff] [blame] | 260 | BB->getInstList().erase(UI); |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 261 | |
Chris Lattner | f4e6c3a | 2005-09-27 21:18:17 +0000 | [diff] [blame] | 262 | ++NumUnwinds; Changed = true; |
Chris Lattner | 86e4445 | 2003-10-05 19:14:42 +0000 | [diff] [blame] | 263 | } |
| 264 | return Changed; |
| 265 | } |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 266 | |
Chris Lattner | f4e6c3a | 2005-09-27 21:18:17 +0000 | [diff] [blame] | 267 | /// rewriteExpensiveInvoke - Insert code and hack the function to replace the |
| 268 | /// specified invoke instruction with a call. |
| 269 | void LowerInvoke::rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo, |
| 270 | AllocaInst *InvokeNum, |
| 271 | SwitchInst *CatchSwitch) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 272 | ConstantInt *InvokeNoC = ConstantInt::get(Type::UIntTy, InvokeNo); |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 273 | |
Chris Lattner | f4e6c3a | 2005-09-27 21:18:17 +0000 | [diff] [blame] | 274 | // Insert a store of the invoke num before the invoke and store zero into the |
| 275 | // location afterward. |
| 276 | new StoreInst(InvokeNoC, InvokeNum, true, II); // volatile |
Chris Lattner | 93e50ce | 2005-09-29 17:44:20 +0000 | [diff] [blame] | 277 | |
| 278 | BasicBlock::iterator NI = II->getNormalDest()->begin(); |
| 279 | while (isa<PHINode>(NI)) ++NI; |
| 280 | // nonvolatile. |
| 281 | new StoreInst(Constant::getNullValue(Type::UIntTy), InvokeNum, false, NI); |
Chris Lattner | f4e6c3a | 2005-09-27 21:18:17 +0000 | [diff] [blame] | 282 | |
| 283 | // Add a switch case to our unwind block. |
| 284 | CatchSwitch->addCase(InvokeNoC, II->getUnwindDest()); |
| 285 | |
| 286 | // Insert a normal call instruction. |
| 287 | std::string Name = II->getName(); II->setName(""); |
| 288 | CallInst *NewCall = new CallInst(II->getCalledValue(), |
| 289 | std::vector<Value*>(II->op_begin()+3, |
| 290 | II->op_end()), Name, |
| 291 | II); |
| 292 | NewCall->setCallingConv(II->getCallingConv()); |
| 293 | II->replaceAllUsesWith(NewCall); |
| 294 | |
| 295 | // Replace the invoke with an uncond branch. |
| 296 | new BranchInst(II->getNormalDest(), NewCall->getParent()); |
| 297 | II->eraseFromParent(); |
| 298 | } |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 299 | |
Chris Lattner | f4e6c3a | 2005-09-27 21:18:17 +0000 | [diff] [blame] | 300 | /// MarkBlocksLiveIn - Insert BB and all of its predescessors into LiveBBs until |
| 301 | /// we reach blocks we've already seen. |
| 302 | static void MarkBlocksLiveIn(BasicBlock *BB, std::set<BasicBlock*> &LiveBBs) { |
| 303 | if (!LiveBBs.insert(BB).second) return; // already been here. |
| 304 | |
| 305 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) |
| 306 | MarkBlocksLiveIn(*PI, LiveBBs); |
| 307 | } |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 308 | |
Chris Lattner | f4e6c3a | 2005-09-27 21:18:17 +0000 | [diff] [blame] | 309 | // First thing we need to do is scan the whole function for values that are |
| 310 | // live across unwind edges. Each value that is live across an unwind edge |
| 311 | // we spill into a stack location, guaranteeing that there is nothing live |
| 312 | // across the unwind edge. This process also splits all critical edges |
| 313 | // coming out of invoke's. |
| 314 | void LowerInvoke:: |
| 315 | splitLiveRangesLiveAcrossInvokes(std::vector<InvokeInst*> &Invokes) { |
| 316 | // First step, split all critical edges from invoke instructions. |
| 317 | for (unsigned i = 0, e = Invokes.size(); i != e; ++i) { |
| 318 | InvokeInst *II = Invokes[i]; |
| 319 | SplitCriticalEdge(II, 0, this); |
| 320 | SplitCriticalEdge(II, 1, this); |
| 321 | assert(!isa<PHINode>(II->getNormalDest()) && |
| 322 | !isa<PHINode>(II->getUnwindDest()) && |
| 323 | "critical edge splitting left single entry phi nodes?"); |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Chris Lattner | f4e6c3a | 2005-09-27 21:18:17 +0000 | [diff] [blame] | 326 | Function *F = Invokes.back()->getParent()->getParent(); |
| 327 | |
| 328 | // To avoid having to handle incoming arguments specially, we lower each arg |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame^] | 329 | // to a copy instruction in the entry block. This ensures that the argument |
Chris Lattner | f4e6c3a | 2005-09-27 21:18:17 +0000 | [diff] [blame] | 330 | // value itself cannot be live across the entry block. |
| 331 | BasicBlock::iterator AfterAllocaInsertPt = F->begin()->begin(); |
| 332 | while (isa<AllocaInst>(AfterAllocaInsertPt) && |
| 333 | isa<ConstantInt>(cast<AllocaInst>(AfterAllocaInsertPt)->getArraySize())) |
| 334 | ++AfterAllocaInsertPt; |
| 335 | for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); |
| 336 | AI != E; ++AI) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame^] | 337 | // This is always a no-op cast because we're casting AI to AI->getType() so |
| 338 | // src and destination types are identical. BitCast is the only possibility. |
| 339 | CastInst *NC = new BitCastInst( |
| 340 | AI, AI->getType(), AI->getName()+".tmp", AfterAllocaInsertPt); |
Chris Lattner | f4e6c3a | 2005-09-27 21:18:17 +0000 | [diff] [blame] | 341 | AI->replaceAllUsesWith(NC); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame^] | 342 | // Normally its is forbidden to replace a CastInst's operand because it |
| 343 | // could cause the opcode to reflect an illegal conversion. However, we're |
| 344 | // replacing it here with the same value it was constructed with to simply |
| 345 | // make NC its user. |
| 346 | NC->setOperand(0, AI); |
Chris Lattner | f4e6c3a | 2005-09-27 21:18:17 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | // Finally, scan the code looking for instructions with bad live ranges. |
| 350 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 351 | for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ++II) { |
| 352 | // Ignore obvious cases we don't have to handle. In particular, most |
| 353 | // instructions either have no uses or only have a single use inside the |
| 354 | // current block. Ignore them quickly. |
| 355 | Instruction *Inst = II; |
| 356 | if (Inst->use_empty()) continue; |
| 357 | if (Inst->hasOneUse() && |
| 358 | cast<Instruction>(Inst->use_back())->getParent() == BB && |
| 359 | !isa<PHINode>(Inst->use_back())) continue; |
| 360 | |
Chris Lattner | 4531371 | 2005-09-27 21:33:12 +0000 | [diff] [blame] | 361 | // If this is an alloca in the entry block, it's not a real register |
| 362 | // value. |
| 363 | if (AllocaInst *AI = dyn_cast<AllocaInst>(Inst)) |
| 364 | if (isa<ConstantInt>(AI->getArraySize()) && BB == F->begin()) |
| 365 | continue; |
| 366 | |
Chris Lattner | f4e6c3a | 2005-09-27 21:18:17 +0000 | [diff] [blame] | 367 | // Avoid iterator invalidation by copying users to a temporary vector. |
| 368 | std::vector<Instruction*> Users; |
| 369 | for (Value::use_iterator UI = Inst->use_begin(), E = Inst->use_end(); |
| 370 | UI != E; ++UI) { |
| 371 | Instruction *User = cast<Instruction>(*UI); |
| 372 | if (User->getParent() != BB || isa<PHINode>(User)) |
| 373 | Users.push_back(User); |
| 374 | } |
| 375 | |
| 376 | // Scan all of the uses and see if the live range is live across an unwind |
| 377 | // edge. If we find a use live across an invoke edge, create an alloca |
| 378 | // and spill the value. |
Chris Lattner | f4e6c3a | 2005-09-27 21:18:17 +0000 | [diff] [blame] | 379 | std::set<InvokeInst*> InvokesWithStoreInserted; |
| 380 | |
| 381 | // Find all of the blocks that this value is live in. |
| 382 | std::set<BasicBlock*> LiveBBs; |
| 383 | LiveBBs.insert(Inst->getParent()); |
| 384 | while (!Users.empty()) { |
| 385 | Instruction *U = Users.back(); |
| 386 | Users.pop_back(); |
| 387 | |
Chris Lattner | f4e6c3a | 2005-09-27 21:18:17 +0000 | [diff] [blame] | 388 | if (!isa<PHINode>(U)) { |
| 389 | MarkBlocksLiveIn(U->getParent(), LiveBBs); |
| 390 | } else { |
| 391 | // Uses for a PHI node occur in their predecessor block. |
| 392 | PHINode *PN = cast<PHINode>(U); |
| 393 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 394 | if (PN->getIncomingValue(i) == Inst) |
| 395 | MarkBlocksLiveIn(PN->getIncomingBlock(i), LiveBBs); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | // Now that we know all of the blocks that this thing is live in, see if |
| 400 | // it includes any of the unwind locations. |
| 401 | bool NeedsSpill = false; |
| 402 | for (unsigned i = 0, e = Invokes.size(); i != e; ++i) { |
| 403 | BasicBlock *UnwindBlock = Invokes[i]->getUnwindDest(); |
| 404 | if (UnwindBlock != BB && LiveBBs.count(UnwindBlock)) { |
| 405 | NeedsSpill = true; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | // If we decided we need a spill, do it. |
| 410 | if (NeedsSpill) { |
| 411 | ++NumSpilled; |
| 412 | DemoteRegToStack(*Inst, true); |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | bool LowerInvoke::insertExpensiveEHSupport(Function &F) { |
| 418 | std::vector<ReturnInst*> Returns; |
| 419 | std::vector<UnwindInst*> Unwinds; |
| 420 | std::vector<InvokeInst*> Invokes; |
| 421 | |
| 422 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 423 | if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) { |
| 424 | // Remember all return instructions in case we insert an invoke into this |
| 425 | // function. |
| 426 | Returns.push_back(RI); |
| 427 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) { |
| 428 | Invokes.push_back(II); |
| 429 | } else if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) { |
| 430 | Unwinds.push_back(UI); |
| 431 | } |
| 432 | |
| 433 | if (Unwinds.empty() && Invokes.empty()) return false; |
| 434 | |
| 435 | NumInvokes += Invokes.size(); |
| 436 | NumUnwinds += Unwinds.size(); |
Chris Lattner | 5b3c702 | 2005-09-27 22:44:59 +0000 | [diff] [blame] | 437 | |
| 438 | // TODO: This is not an optimal way to do this. In particular, this always |
| 439 | // inserts setjmp calls into the entries of functions with invoke instructions |
| 440 | // even though there are possibly paths through the function that do not |
| 441 | // execute any invokes. In particular, for functions with early exits, e.g. |
| 442 | // the 'addMove' method in hexxagon, it would be nice to not have to do the |
| 443 | // setjmp stuff on the early exit path. This requires a bit of dataflow, but |
| 444 | // would not be too hard to do. |
Chris Lattner | f4e6c3a | 2005-09-27 21:18:17 +0000 | [diff] [blame] | 445 | |
| 446 | // If we have an invoke instruction, insert a setjmp that dominates all |
| 447 | // invokes. After the setjmp, use a cond branch that goes to the original |
| 448 | // code path on zero, and to a designated 'catch' block of nonzero. |
| 449 | Value *OldJmpBufPtr = 0; |
| 450 | if (!Invokes.empty()) { |
| 451 | // First thing we need to do is scan the whole function for values that are |
| 452 | // live across unwind edges. Each value that is live across an unwind edge |
| 453 | // we spill into a stack location, guaranteeing that there is nothing live |
| 454 | // across the unwind edge. This process also splits all critical edges |
| 455 | // coming out of invoke's. |
| 456 | splitLiveRangesLiveAcrossInvokes(Invokes); |
| 457 | |
| 458 | BasicBlock *EntryBB = F.begin(); |
| 459 | |
| 460 | // Create an alloca for the incoming jump buffer ptr and the new jump buffer |
| 461 | // that needs to be restored on all exits from the function. This is an |
| 462 | // alloca because the value needs to be live across invokes. |
Chris Lattner | 97d2dbd | 2006-09-05 17:48:07 +0000 | [diff] [blame] | 463 | unsigned Align = TLI ? TLI->getJumpBufAlignment() : 0; |
Chris Lattner | f4e6c3a | 2005-09-27 21:18:17 +0000 | [diff] [blame] | 464 | AllocaInst *JmpBuf = |
Chris Lattner | 97d2dbd | 2006-09-05 17:48:07 +0000 | [diff] [blame] | 465 | new AllocaInst(JBLinkTy, 0, Align, "jblink", F.begin()->begin()); |
Chris Lattner | f4e6c3a | 2005-09-27 21:18:17 +0000 | [diff] [blame] | 466 | |
| 467 | std::vector<Value*> Idx; |
| 468 | Idx.push_back(Constant::getNullValue(Type::IntTy)); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 469 | Idx.push_back(ConstantInt::get(Type::UIntTy, 1)); |
Chris Lattner | f4e6c3a | 2005-09-27 21:18:17 +0000 | [diff] [blame] | 470 | OldJmpBufPtr = new GetElementPtrInst(JmpBuf, Idx, "OldBuf", |
| 471 | EntryBB->getTerminator()); |
| 472 | |
| 473 | // Copy the JBListHead to the alloca. |
| 474 | Value *OldBuf = new LoadInst(JBListHead, "oldjmpbufptr", true, |
| 475 | EntryBB->getTerminator()); |
| 476 | new StoreInst(OldBuf, OldJmpBufPtr, true, EntryBB->getTerminator()); |
| 477 | |
| 478 | // Add the new jumpbuf to the list. |
| 479 | new StoreInst(JmpBuf, JBListHead, true, EntryBB->getTerminator()); |
| 480 | |
| 481 | // Create the catch block. The catch block is basically a big switch |
| 482 | // statement that goes to all of the invoke catch blocks. |
| 483 | BasicBlock *CatchBB = new BasicBlock("setjmp.catch", &F); |
| 484 | |
| 485 | // Create an alloca which keeps track of which invoke is currently |
| 486 | // executing. For normal calls it contains zero. |
| 487 | AllocaInst *InvokeNum = new AllocaInst(Type::UIntTy, 0, "invokenum", |
| 488 | EntryBB->begin()); |
| 489 | new StoreInst(ConstantInt::get(Type::UIntTy, 0), InvokeNum, true, |
| 490 | EntryBB->getTerminator()); |
| 491 | |
| 492 | // Insert a load in the Catch block, and a switch on its value. By default, |
| 493 | // we go to a block that just does an unwind (which is the correct action |
| 494 | // for a standard call). |
| 495 | BasicBlock *UnwindBB = new BasicBlock("unwindbb", &F); |
| 496 | Unwinds.push_back(new UnwindInst(UnwindBB)); |
| 497 | |
| 498 | Value *CatchLoad = new LoadInst(InvokeNum, "invoke.num", true, CatchBB); |
| 499 | SwitchInst *CatchSwitch = |
| 500 | new SwitchInst(CatchLoad, UnwindBB, Invokes.size(), CatchBB); |
| 501 | |
| 502 | // Now that things are set up, insert the setjmp call itself. |
| 503 | |
| 504 | // Split the entry block to insert the conditional branch for the setjmp. |
| 505 | BasicBlock *ContBlock = EntryBB->splitBasicBlock(EntryBB->getTerminator(), |
| 506 | "setjmp.cont"); |
| 507 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 508 | Idx[1] = ConstantInt::get(Type::UIntTy, 0); |
Chris Lattner | f4e6c3a | 2005-09-27 21:18:17 +0000 | [diff] [blame] | 509 | Value *JmpBufPtr = new GetElementPtrInst(JmpBuf, Idx, "TheJmpBuf", |
| 510 | EntryBB->getTerminator()); |
| 511 | Value *SJRet = new CallInst(SetJmpFn, JmpBufPtr, "sjret", |
| 512 | EntryBB->getTerminator()); |
| 513 | |
| 514 | // Compare the return value to zero. |
| 515 | Value *IsNormal = BinaryOperator::createSetEQ(SJRet, |
| 516 | Constant::getNullValue(SJRet->getType()), |
| 517 | "notunwind", EntryBB->getTerminator()); |
| 518 | // Nuke the uncond branch. |
| 519 | EntryBB->getTerminator()->eraseFromParent(); |
| 520 | |
| 521 | // Put in a new condbranch in its place. |
| 522 | new BranchInst(ContBlock, CatchBB, IsNormal, EntryBB); |
| 523 | |
| 524 | // At this point, we are all set up, rewrite each invoke instruction. |
| 525 | for (unsigned i = 0, e = Invokes.size(); i != e; ++i) |
| 526 | rewriteExpensiveInvoke(Invokes[i], i+1, InvokeNum, CatchSwitch); |
| 527 | } |
| 528 | |
| 529 | // We know that there is at least one unwind. |
| 530 | |
| 531 | // Create three new blocks, the block to load the jmpbuf ptr and compare |
| 532 | // against null, the block to do the longjmp, and the error block for if it |
| 533 | // is null. Add them at the end of the function because they are not hot. |
| 534 | BasicBlock *UnwindHandler = new BasicBlock("dounwind", &F); |
| 535 | BasicBlock *UnwindBlock = new BasicBlock("unwind", &F); |
| 536 | BasicBlock *TermBlock = new BasicBlock("unwinderror", &F); |
| 537 | |
| 538 | // If this function contains an invoke, restore the old jumpbuf ptr. |
| 539 | Value *BufPtr; |
| 540 | if (OldJmpBufPtr) { |
| 541 | // Before the return, insert a copy from the saved value to the new value. |
| 542 | BufPtr = new LoadInst(OldJmpBufPtr, "oldjmpbufptr", UnwindHandler); |
| 543 | new StoreInst(BufPtr, JBListHead, UnwindHandler); |
| 544 | } else { |
| 545 | BufPtr = new LoadInst(JBListHead, "ehlist", UnwindHandler); |
| 546 | } |
| 547 | |
| 548 | // Load the JBList, if it's null, then there was no catch! |
| 549 | Value *NotNull = BinaryOperator::createSetNE(BufPtr, |
| 550 | Constant::getNullValue(BufPtr->getType()), |
| 551 | "notnull", UnwindHandler); |
| 552 | new BranchInst(UnwindBlock, TermBlock, NotNull, UnwindHandler); |
| 553 | |
| 554 | // Create the block to do the longjmp. |
| 555 | // Get a pointer to the jmpbuf and longjmp. |
| 556 | std::vector<Value*> Idx; |
| 557 | Idx.push_back(Constant::getNullValue(Type::IntTy)); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 558 | Idx.push_back(ConstantInt::get(Type::UIntTy, 0)); |
Chris Lattner | f4e6c3a | 2005-09-27 21:18:17 +0000 | [diff] [blame] | 559 | Idx[0] = new GetElementPtrInst(BufPtr, Idx, "JmpBuf", UnwindBlock); |
| 560 | Idx[1] = ConstantInt::get(Type::IntTy, 1); |
| 561 | new CallInst(LongJmpFn, Idx, "", UnwindBlock); |
| 562 | new UnreachableInst(UnwindBlock); |
| 563 | |
| 564 | // Set up the term block ("throw without a catch"). |
| 565 | new UnreachableInst(TermBlock); |
| 566 | |
| 567 | // Insert a new call to write(2, AbortMessage, AbortMessageLength); |
| 568 | writeAbortMessage(TermBlock->getTerminator()); |
| 569 | |
| 570 | // Insert a call to abort() |
| 571 | (new CallInst(AbortFn, std::vector<Value*>(), "", |
| 572 | TermBlock->getTerminator()))->setTailCall(); |
| 573 | |
| 574 | |
| 575 | // Replace all unwinds with a branch to the unwind handler. |
| 576 | for (unsigned i = 0, e = Unwinds.size(); i != e; ++i) { |
| 577 | new BranchInst(UnwindHandler, Unwinds[i]); |
| 578 | Unwinds[i]->eraseFromParent(); |
| 579 | } |
| 580 | |
| 581 | // Finally, for any returns from this function, if this function contains an |
| 582 | // invoke, restore the old jmpbuf pointer to its input value. |
| 583 | if (OldJmpBufPtr) { |
| 584 | for (unsigned i = 0, e = Returns.size(); i != e; ++i) { |
| 585 | ReturnInst *R = Returns[i]; |
| 586 | |
| 587 | // Before the return, insert a copy from the saved value to the new value. |
| 588 | Value *OldBuf = new LoadInst(OldJmpBufPtr, "oldjmpbufptr", true, R); |
| 589 | new StoreInst(OldBuf, JBListHead, true, R); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | return true; |
Chris Lattner | 6d78457 | 2004-02-08 19:53:56 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | bool LowerInvoke::runOnFunction(Function &F) { |
| 597 | if (ExpensiveEHSupport) |
| 598 | return insertExpensiveEHSupport(F); |
| 599 | else |
| 600 | return insertCheapEHSupport(F); |
| 601 | } |