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