Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 1 | //===- SjLjEHPass.cpp - Eliminate Invoke & Unwind instructions -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This transformation is designed for use by code generators which use SjLj |
| 11 | // based exception handling. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "sjljehprepare" |
| 16 | #include "llvm/Transforms/Scalar.h" |
| 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/DerivedTypes.h" |
| 19 | #include "llvm/Instructions.h" |
| 20 | #include "llvm/Intrinsics.h" |
| 21 | #include "llvm/LLVMContext.h" |
| 22 | #include "llvm/Module.h" |
| 23 | #include "llvm/Pass.h" |
| 24 | #include "llvm/CodeGen/Passes.h" |
Benjamin Kramer | f788854 | 2010-11-06 11:45:59 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetLowering.h" |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 27 | #include "llvm/Transforms/Utils/Local.h" |
Bill Wendling | d36b3e3 | 2011-08-22 18:44:49 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Debug.h" |
Bill Wendling | aef508d | 2011-08-22 23:38:40 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/DenseMap.h" |
Bill Wendling | d36b3e3 | 2011-08-22 18:44:49 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallVector.h" |
| 31 | #include "llvm/ADT/Statistic.h" |
Benjamin Kramer | f788854 | 2010-11-06 11:45:59 +0000 | [diff] [blame] | 32 | #include <set> |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 33 | using namespace llvm; |
| 34 | |
| 35 | STATISTIC(NumInvokes, "Number of invokes replaced"); |
| 36 | STATISTIC(NumUnwinds, "Number of unwinds replaced"); |
| 37 | STATISTIC(NumSpilled, "Number of registers live across unwind edges"); |
| 38 | |
| 39 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 40 | class SjLjEHPass : public FunctionPass { |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 41 | const TargetLowering *TLI; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 42 | Type *FunctionContextTy; |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 43 | Constant *RegisterFn; |
| 44 | Constant *UnregisterFn; |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 45 | Constant *BuiltinSetjmpFn; |
| 46 | Constant *FrameAddrFn; |
Jim Grosbach | 0798edd | 2010-05-27 23:49:24 +0000 | [diff] [blame] | 47 | Constant *StackAddrFn; |
| 48 | Constant *StackRestoreFn; |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 49 | Constant *LSDAAddrFn; |
| 50 | Value *PersonalityFn; |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 51 | Constant *SelectorFn; |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 52 | Constant *ExceptionFn; |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 53 | Constant *CallSiteFn; |
Jim Grosbach | e4ad387 | 2010-10-19 23:27:08 +0000 | [diff] [blame] | 54 | Constant *DispatchSetupFn; |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 55 | Value *CallSite; |
Bill Wendling | aef508d | 2011-08-22 23:38:40 +0000 | [diff] [blame] | 56 | DenseMap<InvokeInst*, BasicBlock*> LPadSuccMap; |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 57 | public: |
| 58 | static char ID; // Pass identification, replacement for typeid |
| 59 | explicit SjLjEHPass(const TargetLowering *tli = NULL) |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 60 | : FunctionPass(ID), TLI(tli) { } |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 61 | bool doInitialization(Module &M); |
| 62 | bool runOnFunction(Function &F); |
| 63 | |
Bill Wendling | d36b3e3 | 2011-08-22 18:44:49 +0000 | [diff] [blame] | 64 | virtual void getAnalysisUsage(AnalysisUsage &AU) const {} |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 65 | const char *getPassName() const { |
| 66 | return "SJLJ Exception Handling preparation"; |
| 67 | } |
| 68 | |
| 69 | private: |
Jim Grosbach | b58a59b | 2010-03-04 22:07:46 +0000 | [diff] [blame] | 70 | void insertCallSiteStore(Instruction *I, int Number, Value *CallSite); |
| 71 | void markInvokeCallSite(InvokeInst *II, int InvokeNo, Value *CallSite, |
Jim Grosbach | 0bb61c5 | 2009-08-31 01:35:03 +0000 | [diff] [blame] | 72 | SwitchInst *CatchSwitch); |
Jim Grosbach | 288694f | 2010-06-15 18:53:34 +0000 | [diff] [blame] | 73 | void splitLiveRangesAcrossInvokes(SmallVector<InvokeInst*,16> &Invokes); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 74 | bool insertSjLjEHSupport(Function &F); |
| 75 | }; |
| 76 | } // end anonymous namespace |
| 77 | |
| 78 | char SjLjEHPass::ID = 0; |
| 79 | |
| 80 | // Public Interface To the SjLjEHPass pass. |
| 81 | FunctionPass *llvm::createSjLjEHPass(const TargetLowering *TLI) { |
| 82 | return new SjLjEHPass(TLI); |
| 83 | } |
Jim Grosbach | a235d13 | 2009-08-23 18:13:48 +0000 | [diff] [blame] | 84 | // doInitialization - Set up decalarations and types needed to process |
| 85 | // exceptions. |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 86 | bool SjLjEHPass::doInitialization(Module &M) { |
| 87 | // Build the function context structure. |
| 88 | // builtin_setjmp uses a five word jbuf |
Jay Foad | 5fdd6c8 | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 89 | Type *VoidPtrTy = Type::getInt8PtrTy(M.getContext()); |
| 90 | Type *Int32Ty = Type::getInt32Ty(M.getContext()); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 91 | FunctionContextTy = |
Chris Lattner | b231866 | 2011-06-18 22:48:56 +0000 | [diff] [blame] | 92 | StructType::get(VoidPtrTy, // __prev |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 93 | Int32Ty, // call_site |
| 94 | ArrayType::get(Int32Ty, 4), // __data |
| 95 | VoidPtrTy, // __personality |
| 96 | VoidPtrTy, // __lsda |
| 97 | ArrayType::get(VoidPtrTy, 5), // __jbuf |
| 98 | NULL); |
| 99 | RegisterFn = M.getOrInsertFunction("_Unwind_SjLj_Register", |
| 100 | Type::getVoidTy(M.getContext()), |
| 101 | PointerType::getUnqual(FunctionContextTy), |
| 102 | (Type *)0); |
| 103 | UnregisterFn = |
| 104 | M.getOrInsertFunction("_Unwind_SjLj_Unregister", |
| 105 | Type::getVoidTy(M.getContext()), |
| 106 | PointerType::getUnqual(FunctionContextTy), |
| 107 | (Type *)0); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 108 | FrameAddrFn = Intrinsic::getDeclaration(&M, Intrinsic::frameaddress); |
Jim Grosbach | 0798edd | 2010-05-27 23:49:24 +0000 | [diff] [blame] | 109 | StackAddrFn = Intrinsic::getDeclaration(&M, Intrinsic::stacksave); |
| 110 | StackRestoreFn = Intrinsic::getDeclaration(&M, Intrinsic::stackrestore); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 111 | BuiltinSetjmpFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_sjlj_setjmp); |
| 112 | LSDAAddrFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_sjlj_lsda); |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 113 | SelectorFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_selector); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 114 | ExceptionFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_exception); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 115 | CallSiteFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_sjlj_callsite); |
Jim Grosbach | e4ad387 | 2010-10-19 23:27:08 +0000 | [diff] [blame] | 116 | DispatchSetupFn |
| 117 | = Intrinsic::getDeclaration(&M, Intrinsic::eh_sjlj_dispatch_setup); |
Jim Grosbach | a235d13 | 2009-08-23 18:13:48 +0000 | [diff] [blame] | 118 | PersonalityFn = 0; |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 119 | |
| 120 | return true; |
| 121 | } |
| 122 | |
Jim Grosbach | b58a59b | 2010-03-04 22:07:46 +0000 | [diff] [blame] | 123 | /// insertCallSiteStore - Insert a store of the call-site value to the |
| 124 | /// function context |
| 125 | void SjLjEHPass::insertCallSiteStore(Instruction *I, int Number, |
| 126 | Value *CallSite) { |
| 127 | ConstantInt *CallSiteNoC = ConstantInt::get(Type::getInt32Ty(I->getContext()), |
| 128 | Number); |
| 129 | // Insert a store of the call-site number |
| 130 | new StoreInst(CallSiteNoC, CallSite, true, I); // volatile |
| 131 | } |
| 132 | |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 133 | /// markInvokeCallSite - Insert code to mark the call_site for this invoke |
Jim Grosbach | b58a59b | 2010-03-04 22:07:46 +0000 | [diff] [blame] | 134 | void SjLjEHPass::markInvokeCallSite(InvokeInst *II, int InvokeNo, |
Jim Grosbach | 0bb61c5 | 2009-08-31 01:35:03 +0000 | [diff] [blame] | 135 | Value *CallSite, |
| 136 | SwitchInst *CatchSwitch) { |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 137 | ConstantInt *CallSiteNoC= ConstantInt::get(Type::getInt32Ty(II->getContext()), |
Jim Grosbach | b58a59b | 2010-03-04 22:07:46 +0000 | [diff] [blame] | 138 | InvokeNo); |
Jim Grosbach | 0bb61c5 | 2009-08-31 01:35:03 +0000 | [diff] [blame] | 139 | // The runtime comes back to the dispatcher with the call_site - 1 in |
| 140 | // the context. Odd, but there it is. |
| 141 | ConstantInt *SwitchValC = ConstantInt::get(Type::getInt32Ty(II->getContext()), |
| 142 | InvokeNo - 1); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 143 | |
| 144 | // If the unwind edge has phi nodes, split the edge. |
| 145 | if (isa<PHINode>(II->getUnwindDest()->begin())) { |
Bill Wendling | 51fb91c | 2011-08-26 21:18:55 +0000 | [diff] [blame^] | 146 | // FIXME: New EH - This if-condition will be always true in the new scheme. |
| 147 | if (II->getUnwindDest()->isLandingPad()) { |
| 148 | if (isCriticalEdge(II, 1)) { |
| 149 | SmallVector<BasicBlock*, 2> NewBBs; |
| 150 | SplitLandingPadPredecessors(II->getUnwindDest(), II->getParent(), |
| 151 | ".1", ".2", this, NewBBs); |
| 152 | LPadSuccMap[II] = *succ_begin(NewBBs[0]); |
| 153 | } else { |
| 154 | LPadSuccMap[II] = II->getUnwindDest(); |
| 155 | } |
| 156 | } else { |
| 157 | SplitCriticalEdge(II, 1, this); |
| 158 | } |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 159 | |
| 160 | // If there are any phi nodes left, they must have a single predecessor. |
| 161 | while (PHINode *PN = dyn_cast<PHINode>(II->getUnwindDest()->begin())) { |
| 162 | PN->replaceAllUsesWith(PN->getIncomingValue(0)); |
| 163 | PN->eraseFromParent(); |
| 164 | } |
| 165 | } |
| 166 | |
Jim Grosbach | b58a59b | 2010-03-04 22:07:46 +0000 | [diff] [blame] | 167 | // Insert the store of the call site value |
| 168 | insertCallSiteStore(II, InvokeNo, CallSite); |
| 169 | |
| 170 | // Record the call site value for the back end so it stays associated with |
| 171 | // the invoke. |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 172 | CallInst::Create(CallSiteFn, CallSiteNoC, "", II); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 173 | |
Jim Grosbach | 0bb61c5 | 2009-08-31 01:35:03 +0000 | [diff] [blame] | 174 | // Add a switch case to our unwind block. |
Bill Wendling | aef508d | 2011-08-22 23:38:40 +0000 | [diff] [blame] | 175 | if (BasicBlock *SuccBB = LPadSuccMap[II]) { |
| 176 | CatchSwitch->addCase(SwitchValC, SuccBB); |
| 177 | } else { |
| 178 | CatchSwitch->addCase(SwitchValC, II->getUnwindDest()); |
| 179 | } |
| 180 | |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 181 | // We still want this to look like an invoke so we emit the LSDA properly, |
| 182 | // so we don't transform the invoke into a call here. |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /// MarkBlocksLiveIn - Insert BB and all of its predescessors into LiveBBs until |
| 186 | /// we reach blocks we've already seen. |
| 187 | static void MarkBlocksLiveIn(BasicBlock *BB, std::set<BasicBlock*> &LiveBBs) { |
| 188 | if (!LiveBBs.insert(BB).second) return; // already been here. |
| 189 | |
| 190 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) |
| 191 | MarkBlocksLiveIn(*PI, LiveBBs); |
| 192 | } |
| 193 | |
Jim Grosbach | 606f3d6 | 2009-08-17 21:40:03 +0000 | [diff] [blame] | 194 | /// splitLiveRangesAcrossInvokes - Each value that is live across an unwind edge |
| 195 | /// we spill into a stack location, guaranteeing that there is nothing live |
| 196 | /// across the unwind edge. This process also splits all critical edges |
| 197 | /// coming out of invoke's. |
Jim Grosbach | 2f3257e | 2010-06-01 18:06:35 +0000 | [diff] [blame] | 198 | /// FIXME: Move this function to a common utility file (Local.cpp?) so |
| 199 | /// both SjLj and LowerInvoke can use it. |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 200 | void SjLjEHPass:: |
Jim Grosbach | 288694f | 2010-06-15 18:53:34 +0000 | [diff] [blame] | 201 | splitLiveRangesAcrossInvokes(SmallVector<InvokeInst*,16> &Invokes) { |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 202 | // First step, split all critical edges from invoke instructions. |
| 203 | for (unsigned i = 0, e = Invokes.size(); i != e; ++i) { |
| 204 | InvokeInst *II = Invokes[i]; |
| 205 | SplitCriticalEdge(II, 0, this); |
Bill Wendling | aef508d | 2011-08-22 23:38:40 +0000 | [diff] [blame] | 206 | |
| 207 | // FIXME: New EH - This if-condition will be always true in the new scheme. |
| 208 | if (II->getUnwindDest()->isLandingPad()) { |
Bill Wendling | 51fb91c | 2011-08-26 21:18:55 +0000 | [diff] [blame^] | 209 | if (isCriticalEdge(II, 1)) { |
| 210 | SmallVector<BasicBlock*, 2> NewBBs; |
| 211 | SplitLandingPadPredecessors(II->getUnwindDest(), II->getParent(), |
| 212 | ".1", ".2", this, NewBBs); |
| 213 | LPadSuccMap[II] = *succ_begin(NewBBs[0]); |
| 214 | } else { |
| 215 | LPadSuccMap[II] = II->getUnwindDest(); |
| 216 | } |
Bill Wendling | aef508d | 2011-08-22 23:38:40 +0000 | [diff] [blame] | 217 | } else { |
| 218 | SplitCriticalEdge(II, 1, this); |
| 219 | } |
| 220 | |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 221 | assert(!isa<PHINode>(II->getNormalDest()) && |
| 222 | !isa<PHINode>(II->getUnwindDest()) && |
Bill Wendling | d36b3e3 | 2011-08-22 18:44:49 +0000 | [diff] [blame] | 223 | "Critical edge splitting left single entry phi nodes?"); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | Function *F = Invokes.back()->getParent()->getParent(); |
| 227 | |
| 228 | // To avoid having to handle incoming arguments specially, we lower each arg |
| 229 | // to a copy instruction in the entry block. This ensures that the argument |
| 230 | // value itself cannot be live across the entry block. |
| 231 | BasicBlock::iterator AfterAllocaInsertPt = F->begin()->begin(); |
| 232 | while (isa<AllocaInst>(AfterAllocaInsertPt) && |
| 233 | isa<ConstantInt>(cast<AllocaInst>(AfterAllocaInsertPt)->getArraySize())) |
| 234 | ++AfterAllocaInsertPt; |
| 235 | for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); |
| 236 | AI != E; ++AI) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 237 | Type *Ty = AI->getType(); |
Jim Grosbach | e70fc8e | 2010-06-30 22:20:38 +0000 | [diff] [blame] | 238 | // Aggregate types can't be cast, but are legal argument types, so we have |
Jim Grosbach | dc58b25 | 2010-06-01 18:04:09 +0000 | [diff] [blame] | 239 | // to handle them differently. We use an extract/insert pair as a |
| 240 | // lightweight method to achieve the same goal. |
Jim Grosbach | e70fc8e | 2010-06-30 22:20:38 +0000 | [diff] [blame] | 241 | if (isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) { |
| 242 | Instruction *EI = ExtractValueInst::Create(AI, 0, "",AfterAllocaInsertPt); |
Jim Grosbach | dc58b25 | 2010-06-01 18:04:09 +0000 | [diff] [blame] | 243 | Instruction *NI = InsertValueInst::Create(AI, EI, 0); |
| 244 | NI->insertAfter(EI); |
| 245 | AI->replaceAllUsesWith(NI); |
Jim Grosbach | e70fc8e | 2010-06-30 22:20:38 +0000 | [diff] [blame] | 246 | // Set the operand of the instructions back to the AllocaInst. |
Jim Grosbach | dc58b25 | 2010-06-01 18:04:09 +0000 | [diff] [blame] | 247 | EI->setOperand(0, AI); |
| 248 | NI->setOperand(0, AI); |
| 249 | } else { |
| 250 | // This is always a no-op cast because we're casting AI to AI->getType() |
| 251 | // so src and destination types are identical. BitCast is the only |
| 252 | // possibility. |
| 253 | CastInst *NC = new BitCastInst( |
| 254 | AI, AI->getType(), AI->getName()+".tmp", AfterAllocaInsertPt); |
| 255 | AI->replaceAllUsesWith(NC); |
| 256 | // Set the operand of the cast instruction back to the AllocaInst. |
| 257 | // Normally it's forbidden to replace a CastInst's operand because it |
| 258 | // could cause the opcode to reflect an illegal conversion. However, |
| 259 | // we're replacing it here with the same value it was constructed with. |
| 260 | // We do this because the above replaceAllUsesWith() clobbered the |
| 261 | // operand, but we want this one to remain. |
| 262 | NC->setOperand(0, AI); |
| 263 | } |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | // Finally, scan the code looking for instructions with bad live ranges. |
| 267 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 268 | for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ++II) { |
| 269 | // Ignore obvious cases we don't have to handle. In particular, most |
| 270 | // instructions either have no uses or only have a single use inside the |
| 271 | // current block. Ignore them quickly. |
| 272 | Instruction *Inst = II; |
| 273 | if (Inst->use_empty()) continue; |
| 274 | if (Inst->hasOneUse() && |
| 275 | cast<Instruction>(Inst->use_back())->getParent() == BB && |
| 276 | !isa<PHINode>(Inst->use_back())) continue; |
| 277 | |
| 278 | // If this is an alloca in the entry block, it's not a real register |
| 279 | // value. |
| 280 | if (AllocaInst *AI = dyn_cast<AllocaInst>(Inst)) |
| 281 | if (isa<ConstantInt>(AI->getArraySize()) && BB == F->begin()) |
| 282 | continue; |
| 283 | |
| 284 | // Avoid iterator invalidation by copying users to a temporary vector. |
Jim Grosbach | 606f3d6 | 2009-08-17 21:40:03 +0000 | [diff] [blame] | 285 | SmallVector<Instruction*,16> Users; |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 286 | for (Value::use_iterator UI = Inst->use_begin(), E = Inst->use_end(); |
| 287 | UI != E; ++UI) { |
| 288 | Instruction *User = cast<Instruction>(*UI); |
| 289 | if (User->getParent() != BB || isa<PHINode>(User)) |
| 290 | Users.push_back(User); |
| 291 | } |
| 292 | |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 293 | // Find all of the blocks that this value is live in. |
| 294 | std::set<BasicBlock*> LiveBBs; |
| 295 | LiveBBs.insert(Inst->getParent()); |
| 296 | while (!Users.empty()) { |
| 297 | Instruction *U = Users.back(); |
| 298 | Users.pop_back(); |
| 299 | |
| 300 | if (!isa<PHINode>(U)) { |
| 301 | MarkBlocksLiveIn(U->getParent(), LiveBBs); |
| 302 | } else { |
| 303 | // Uses for a PHI node occur in their predecessor block. |
| 304 | PHINode *PN = cast<PHINode>(U); |
| 305 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 306 | if (PN->getIncomingValue(i) == Inst) |
| 307 | MarkBlocksLiveIn(PN->getIncomingBlock(i), LiveBBs); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | // Now that we know all of the blocks that this thing is live in, see if |
| 312 | // it includes any of the unwind locations. |
| 313 | bool NeedsSpill = false; |
| 314 | for (unsigned i = 0, e = Invokes.size(); i != e; ++i) { |
| 315 | BasicBlock *UnwindBlock = Invokes[i]->getUnwindDest(); |
| 316 | if (UnwindBlock != BB && LiveBBs.count(UnwindBlock)) { |
| 317 | NeedsSpill = true; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | // If we decided we need a spill, do it. |
Jim Grosbach | 956352e | 2010-06-16 18:45:08 +0000 | [diff] [blame] | 322 | // FIXME: Spilling this way is overkill, as it forces all uses of |
| 323 | // the value to be reloaded from the stack slot, even those that aren't |
| 324 | // in the unwind blocks. We should be more selective. |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 325 | if (NeedsSpill) { |
| 326 | ++NumSpilled; |
| 327 | DemoteRegToStack(*Inst, true); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
Bill Wendling | aef508d | 2011-08-22 23:38:40 +0000 | [diff] [blame] | 332 | /// CreateLandingPadLoad - Load the exception handling values and insert them |
| 333 | /// into a structure. |
| 334 | static Instruction *CreateLandingPadLoad(Function &F, Value *ExnAddr, |
| 335 | Value *SelAddr, |
| 336 | BasicBlock::iterator InsertPt) { |
| 337 | Value *Exn = new LoadInst(ExnAddr, "exn", false, |
| 338 | InsertPt); |
| 339 | Type *Ty = Type::getInt8PtrTy(F.getContext()); |
| 340 | Exn = CastInst::Create(Instruction::IntToPtr, Exn, Ty, "", InsertPt); |
| 341 | Value *Sel = new LoadInst(SelAddr, "sel", false, InsertPt); |
| 342 | |
| 343 | Ty = StructType::get(Exn->getType(), Sel->getType(), NULL); |
| 344 | InsertValueInst *LPadVal = InsertValueInst::Create(llvm::UndefValue::get(Ty), |
| 345 | Exn, 0, |
| 346 | "lpad.val", InsertPt); |
| 347 | return InsertValueInst::Create(LPadVal, Sel, 1, "lpad.val", InsertPt); |
| 348 | } |
| 349 | |
| 350 | /// ReplaceLandingPadVal - Replace the landingpad instruction's value with a |
| 351 | /// load from the stored values (via CreateLandingPadLoad). This looks through |
| 352 | /// PHI nodes, and removes them if they are dead. |
| 353 | static void ReplaceLandingPadVal(Function &F, Instruction *Inst, Value *ExnAddr, |
| 354 | Value *SelAddr) { |
| 355 | if (Inst->use_empty()) return; |
| 356 | |
| 357 | while (!Inst->use_empty()) { |
| 358 | Instruction *I = cast<Instruction>(Inst->use_back()); |
| 359 | |
| 360 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
| 361 | ReplaceLandingPadVal(F, PN, ExnAddr, SelAddr); |
| 362 | if (PN->use_empty()) PN->eraseFromParent(); |
| 363 | continue; |
| 364 | } |
| 365 | |
Bill Wendling | fc8713f | 2011-08-23 22:55:03 +0000 | [diff] [blame] | 366 | I->replaceUsesOfWith(Inst, CreateLandingPadLoad(F, ExnAddr, SelAddr, I)); |
Bill Wendling | aef508d | 2011-08-22 23:38:40 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 370 | bool SjLjEHPass::insertSjLjEHSupport(Function &F) { |
Jim Grosbach | 606f3d6 | 2009-08-17 21:40:03 +0000 | [diff] [blame] | 371 | SmallVector<ReturnInst*,16> Returns; |
| 372 | SmallVector<UnwindInst*,16> Unwinds; |
| 373 | SmallVector<InvokeInst*,16> Invokes; |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 374 | |
| 375 | // Look through the terminators of the basic blocks to find invokes, returns |
Jim Grosbach | b58a59b | 2010-03-04 22:07:46 +0000 | [diff] [blame] | 376 | // and unwinds. |
| 377 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 378 | if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) { |
| 379 | // Remember all return instructions in case we insert an invoke into this |
| 380 | // function. |
| 381 | Returns.push_back(RI); |
| 382 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) { |
| 383 | Invokes.push_back(II); |
| 384 | } else if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) { |
| 385 | Unwinds.push_back(UI); |
| 386 | } |
Jim Grosbach | b58a59b | 2010-03-04 22:07:46 +0000 | [diff] [blame] | 387 | } |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 388 | |
| 389 | NumInvokes += Invokes.size(); |
| 390 | NumUnwinds += Unwinds.size(); |
| 391 | |
| 392 | // If we don't have any invokes, there's nothing to do. |
| 393 | if (Invokes.empty()) return false; |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 394 | |
Jim Grosbach | 0798edd | 2010-05-27 23:49:24 +0000 | [diff] [blame] | 395 | // Find the eh.selector.*, eh.exception and alloca calls. |
| 396 | // |
| 397 | // Remember any allocas() that aren't in the entry block, as the |
| 398 | // jmpbuf saved SP will need to be updated for them. |
| 399 | // |
| 400 | // We'll use the first eh.selector to determine the right personality |
| 401 | // function to use. For SJLJ, we always use the same personality for the |
| 402 | // whole function, not on a per-selector basis. |
Jim Grosbach | a235d13 | 2009-08-23 18:13:48 +0000 | [diff] [blame] | 403 | // FIXME: That's a bit ugly. Better way? |
| 404 | SmallVector<CallInst*,16> EH_Selectors; |
| 405 | SmallVector<CallInst*,16> EH_Exceptions; |
Jim Grosbach | 0798edd | 2010-05-27 23:49:24 +0000 | [diff] [blame] | 406 | SmallVector<Instruction*,16> JmpbufUpdatePoints; |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 407 | |
Bill Wendling | cfcccef | 2011-08-23 22:20:16 +0000 | [diff] [blame] | 408 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { |
| 409 | // Note: Skip the entry block since there's nothing there that interests |
| 410 | // us. eh.selector and eh.exception shouldn't ever be there, and we |
| 411 | // want to disregard any allocas that are there. |
| 412 | // |
| 413 | // FIXME: This is awkward. The new EH scheme won't need to skip the entry |
| 414 | // block. |
| 415 | if (BB == F.begin()) { |
| 416 | if (InvokeInst *II = dyn_cast<InvokeInst>(F.begin()->getTerminator())) { |
| 417 | // FIXME: This will be always non-NULL in the new EH. |
| 418 | if (LandingPadInst *LPI = II->getUnwindDest()->getLandingPadInst()) |
| 419 | if (!PersonalityFn) PersonalityFn = LPI->getPersonalityFn(); |
| 420 | } |
| 421 | |
| 422 | continue; |
| 423 | } |
| 424 | |
Jim Grosbach | a235d13 | 2009-08-23 18:13:48 +0000 | [diff] [blame] | 425 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
| 426 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 427 | if (CI->getCalledFunction() == SelectorFn) { |
Gabor Greif | 45c097f | 2010-06-25 09:36:23 +0000 | [diff] [blame] | 428 | if (!PersonalityFn) PersonalityFn = CI->getArgOperand(1); |
Jim Grosbach | a235d13 | 2009-08-23 18:13:48 +0000 | [diff] [blame] | 429 | EH_Selectors.push_back(CI); |
| 430 | } else if (CI->getCalledFunction() == ExceptionFn) { |
| 431 | EH_Exceptions.push_back(CI); |
Jim Grosbach | 0798edd | 2010-05-27 23:49:24 +0000 | [diff] [blame] | 432 | } else if (CI->getCalledFunction() == StackRestoreFn) { |
| 433 | JmpbufUpdatePoints.push_back(CI); |
Jim Grosbach | a235d13 | 2009-08-23 18:13:48 +0000 | [diff] [blame] | 434 | } |
Jim Grosbach | 0798edd | 2010-05-27 23:49:24 +0000 | [diff] [blame] | 435 | } else if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) { |
| 436 | JmpbufUpdatePoints.push_back(AI); |
Bill Wendling | aef508d | 2011-08-22 23:38:40 +0000 | [diff] [blame] | 437 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(I)) { |
| 438 | // FIXME: This will be always non-NULL in the new EH. |
| 439 | if (LandingPadInst *LPI = II->getUnwindDest()->getLandingPadInst()) |
| 440 | if (!PersonalityFn) PersonalityFn = LPI->getPersonalityFn(); |
Jim Grosbach | a235d13 | 2009-08-23 18:13:48 +0000 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | } |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 444 | |
Jim Grosbach | a235d13 | 2009-08-23 18:13:48 +0000 | [diff] [blame] | 445 | // If we don't have any eh.selector calls, we can't determine the personality |
| 446 | // function. Without a personality function, we can't process exceptions. |
| 447 | if (!PersonalityFn) return false; |
| 448 | |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 449 | // We have invokes, so we need to add register/unregister calls to get this |
| 450 | // function onto the global unwind stack. |
| 451 | // |
| 452 | // First thing we need to do is scan the whole function for values that are |
| 453 | // live across unwind edges. Each value that is live across an unwind edge we |
| 454 | // spill into a stack location, guaranteeing that there is nothing live across |
| 455 | // the unwind edge. This process also splits all critical edges coming out of |
| 456 | // invoke's. |
| 457 | splitLiveRangesAcrossInvokes(Invokes); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 458 | |
Bill Wendling | aef508d | 2011-08-22 23:38:40 +0000 | [diff] [blame] | 459 | |
| 460 | SmallVector<LandingPadInst*, 16> LandingPads; |
| 461 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { |
| 462 | if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) |
| 463 | // FIXME: This will be always non-NULL in the new EH. |
| 464 | if (LandingPadInst *LPI = II->getUnwindDest()->getLandingPadInst()) |
| 465 | LandingPads.push_back(LPI); |
| 466 | } |
| 467 | |
| 468 | |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 469 | BasicBlock *EntryBB = F.begin(); |
| 470 | // Create an alloca for the incoming jump buffer ptr and the new jump buffer |
| 471 | // that needs to be restored on all exits from the function. This is an |
| 472 | // alloca because the value needs to be added to the global context list. |
| 473 | unsigned Align = 4; // FIXME: Should be a TLI check? |
| 474 | AllocaInst *FunctionContext = |
| 475 | new AllocaInst(FunctionContextTy, 0, Align, |
| 476 | "fcn_context", F.begin()->begin()); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 477 | |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 478 | Value *Idxs[2]; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 479 | Type *Int32Ty = Type::getInt32Ty(F.getContext()); |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 480 | Value *Zero = ConstantInt::get(Int32Ty, 0); |
| 481 | // We need to also keep around a reference to the call_site field |
| 482 | Idxs[0] = Zero; |
| 483 | Idxs[1] = ConstantInt::get(Int32Ty, 1); |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 484 | CallSite = GetElementPtrInst::Create(FunctionContext, Idxs, "call_site", |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 485 | EntryBB->getTerminator()); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 486 | |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 487 | // The exception selector comes back in context->data[1] |
| 488 | Idxs[1] = ConstantInt::get(Int32Ty, 2); |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 489 | Value *FCData = GetElementPtrInst::Create(FunctionContext, Idxs, "fc_data", |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 490 | EntryBB->getTerminator()); |
| 491 | Idxs[1] = ConstantInt::get(Int32Ty, 1); |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 492 | Value *SelectorAddr = GetElementPtrInst::Create(FCData, Idxs, |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 493 | "exc_selector_gep", |
| 494 | EntryBB->getTerminator()); |
| 495 | // The exception value comes back in context->data[0] |
| 496 | Idxs[1] = Zero; |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 497 | Value *ExceptionAddr = GetElementPtrInst::Create(FCData, Idxs, |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 498 | "exception_gep", |
| 499 | EntryBB->getTerminator()); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 500 | |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 501 | // The result of the eh.selector call will be replaced with a a reference to |
| 502 | // the selector value returned in the function context. We leave the selector |
| 503 | // itself so the EH analysis later can use it. |
| 504 | for (int i = 0, e = EH_Selectors.size(); i < e; ++i) { |
| 505 | CallInst *I = EH_Selectors[i]; |
| 506 | Value *SelectorVal = new LoadInst(SelectorAddr, "select_val", true, I); |
| 507 | I->replaceAllUsesWith(SelectorVal); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 510 | // eh.exception calls are replaced with references to the proper location in |
| 511 | // the context. Unlike eh.selector, the eh.exception calls are removed |
| 512 | // entirely. |
| 513 | for (int i = 0, e = EH_Exceptions.size(); i < e; ++i) { |
| 514 | CallInst *I = EH_Exceptions[i]; |
| 515 | // Possible for there to be duplicates, so check to make sure the |
| 516 | // instruction hasn't already been removed. |
| 517 | if (!I->getParent()) continue; |
| 518 | Value *Val = new LoadInst(ExceptionAddr, "exception", true, I); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 519 | Type *Ty = Type::getInt8PtrTy(F.getContext()); |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 520 | Val = CastInst::Create(Instruction::IntToPtr, Val, Ty, "", I); |
| 521 | |
| 522 | I->replaceAllUsesWith(Val); |
| 523 | I->eraseFromParent(); |
| 524 | } |
| 525 | |
Bill Wendling | aef508d | 2011-08-22 23:38:40 +0000 | [diff] [blame] | 526 | for (unsigned i = 0, e = LandingPads.size(); i != e; ++i) |
| 527 | ReplaceLandingPadVal(F, LandingPads[i], ExceptionAddr, SelectorAddr); |
| 528 | |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 529 | // The entry block changes to have the eh.sjlj.setjmp, with a conditional |
| 530 | // branch to a dispatch block for non-zero returns. If we return normally, |
| 531 | // we're not handling an exception and just register the function context and |
| 532 | // continue. |
| 533 | |
| 534 | // Create the dispatch block. The dispatch block is basically a big switch |
| 535 | // statement that goes to all of the invoke landing pads. |
| 536 | BasicBlock *DispatchBlock = |
| 537 | BasicBlock::Create(F.getContext(), "eh.sjlj.setjmp.catch", &F); |
| 538 | |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 539 | // Insert a load of the callsite in the dispatch block, and a switch on its |
Bill Wendling | cc88595 | 2011-04-11 21:32:34 +0000 | [diff] [blame] | 540 | // value. By default, we issue a trap statement. |
| 541 | BasicBlock *TrapBlock = |
| 542 | BasicBlock::Create(F.getContext(), "trapbb", &F); |
| 543 | CallInst::Create(Intrinsic::getDeclaration(F.getParent(), Intrinsic::trap), |
| 544 | "", TrapBlock); |
| 545 | new UnreachableInst(F.getContext(), TrapBlock); |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 546 | |
| 547 | Value *DispatchLoad = new LoadInst(CallSite, "invoke.num", true, |
| 548 | DispatchBlock); |
| 549 | SwitchInst *DispatchSwitch = |
Bill Wendling | cc88595 | 2011-04-11 21:32:34 +0000 | [diff] [blame] | 550 | SwitchInst::Create(DispatchLoad, TrapBlock, Invokes.size(), |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 551 | DispatchBlock); |
| 552 | // Split the entry block to insert the conditional branch for the setjmp. |
| 553 | BasicBlock *ContBlock = EntryBB->splitBasicBlock(EntryBB->getTerminator(), |
| 554 | "eh.sjlj.setjmp.cont"); |
| 555 | |
| 556 | // Populate the Function Context |
| 557 | // 1. LSDA address |
| 558 | // 2. Personality function address |
| 559 | // 3. jmpbuf (save SP, FP and call eh.sjlj.setjmp) |
| 560 | |
| 561 | // LSDA address |
| 562 | Idxs[0] = Zero; |
| 563 | Idxs[1] = ConstantInt::get(Int32Ty, 4); |
| 564 | Value *LSDAFieldPtr = |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 565 | GetElementPtrInst::Create(FunctionContext, Idxs, "lsda_gep", |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 566 | EntryBB->getTerminator()); |
| 567 | Value *LSDA = CallInst::Create(LSDAAddrFn, "lsda_addr", |
| 568 | EntryBB->getTerminator()); |
| 569 | new StoreInst(LSDA, LSDAFieldPtr, true, EntryBB->getTerminator()); |
| 570 | |
| 571 | Idxs[1] = ConstantInt::get(Int32Ty, 3); |
| 572 | Value *PersonalityFieldPtr = |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 573 | GetElementPtrInst::Create(FunctionContext, Idxs, "lsda_gep", |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 574 | EntryBB->getTerminator()); |
| 575 | new StoreInst(PersonalityFn, PersonalityFieldPtr, true, |
| 576 | EntryBB->getTerminator()); |
| 577 | |
| 578 | // Save the frame pointer. |
| 579 | Idxs[1] = ConstantInt::get(Int32Ty, 5); |
| 580 | Value *JBufPtr |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 581 | = GetElementPtrInst::Create(FunctionContext, Idxs, "jbuf_gep", |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 582 | EntryBB->getTerminator()); |
| 583 | Idxs[1] = ConstantInt::get(Int32Ty, 0); |
| 584 | Value *FramePtr = |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 585 | GetElementPtrInst::Create(JBufPtr, Idxs, "jbuf_fp_gep", |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 586 | EntryBB->getTerminator()); |
| 587 | |
| 588 | Value *Val = CallInst::Create(FrameAddrFn, |
| 589 | ConstantInt::get(Int32Ty, 0), |
| 590 | "fp", |
| 591 | EntryBB->getTerminator()); |
| 592 | new StoreInst(Val, FramePtr, true, EntryBB->getTerminator()); |
| 593 | |
| 594 | // Save the stack pointer. |
| 595 | Idxs[1] = ConstantInt::get(Int32Ty, 2); |
| 596 | Value *StackPtr = |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 597 | GetElementPtrInst::Create(JBufPtr, Idxs, "jbuf_sp_gep", |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 598 | EntryBB->getTerminator()); |
| 599 | |
| 600 | Val = CallInst::Create(StackAddrFn, "sp", EntryBB->getTerminator()); |
| 601 | new StoreInst(Val, StackPtr, true, EntryBB->getTerminator()); |
| 602 | |
| 603 | // Call the setjmp instrinsic. It fills in the rest of the jmpbuf. |
| 604 | Value *SetjmpArg = |
| 605 | CastInst::Create(Instruction::BitCast, JBufPtr, |
| 606 | Type::getInt8PtrTy(F.getContext()), "", |
| 607 | EntryBB->getTerminator()); |
| 608 | Value *DispatchVal = CallInst::Create(BuiltinSetjmpFn, SetjmpArg, |
| 609 | "dispatch", |
| 610 | EntryBB->getTerminator()); |
Bill Wendling | f05b1dc | 2011-04-05 01:37:43 +0000 | [diff] [blame] | 611 | |
| 612 | // Add a call to dispatch_setup after the setjmp call. This is expanded to any |
| 613 | // target-specific setup that needs to be done. |
Bill Wendling | 61512ba | 2011-05-11 01:11:55 +0000 | [diff] [blame] | 614 | CallInst::Create(DispatchSetupFn, DispatchVal, "", EntryBB->getTerminator()); |
Bill Wendling | f05b1dc | 2011-04-05 01:37:43 +0000 | [diff] [blame] | 615 | |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 616 | // check the return value of the setjmp. non-zero goes to dispatcher. |
| 617 | Value *IsNormal = new ICmpInst(EntryBB->getTerminator(), |
| 618 | ICmpInst::ICMP_EQ, DispatchVal, Zero, |
| 619 | "notunwind"); |
| 620 | // Nuke the uncond branch. |
| 621 | EntryBB->getTerminator()->eraseFromParent(); |
| 622 | |
| 623 | // Put in a new condbranch in its place. |
| 624 | BranchInst::Create(ContBlock, DispatchBlock, IsNormal, EntryBB); |
| 625 | |
| 626 | // Register the function context and make sure it's known to not throw |
| 627 | CallInst *Register = |
| 628 | CallInst::Create(RegisterFn, FunctionContext, "", |
| 629 | ContBlock->getTerminator()); |
| 630 | Register->setDoesNotThrow(); |
| 631 | |
| 632 | // At this point, we are all set up, update the invoke instructions to mark |
| 633 | // their call_site values, and fill in the dispatch switch accordingly. |
| 634 | for (unsigned i = 0, e = Invokes.size(); i != e; ++i) |
| 635 | markInvokeCallSite(Invokes[i], i+1, CallSite, DispatchSwitch); |
| 636 | |
| 637 | // Mark call instructions that aren't nounwind as no-action (call_site == |
| 638 | // -1). Skip the entry block, as prior to then, no function context has been |
| 639 | // created for this function and any unexpected exceptions thrown will go |
| 640 | // directly to the caller's context, which is what we want anyway, so no need |
| 641 | // to do anything here. |
| 642 | for (Function::iterator BB = F.begin(), E = F.end(); ++BB != E;) { |
| 643 | for (BasicBlock::iterator I = BB->begin(), end = BB->end(); I != end; ++I) |
| 644 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 645 | // Ignore calls to the EH builtins (eh.selector, eh.exception) |
| 646 | Constant *Callee = CI->getCalledFunction(); |
| 647 | if (Callee != SelectorFn && Callee != ExceptionFn |
| 648 | && !CI->doesNotThrow()) |
| 649 | insertCallSiteStore(CI, -1, CallSite); |
Bill Wendling | 3ae96d6 | 2011-08-24 00:00:23 +0000 | [diff] [blame] | 650 | } else if (ResumeInst *RI = dyn_cast<ResumeInst>(I)) { |
| 651 | insertCallSiteStore(RI, -1, CallSite); |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | |
| 655 | // Replace all unwinds with a branch to the unwind handler. |
| 656 | // ??? Should this ever happen with sjlj exceptions? |
| 657 | for (unsigned i = 0, e = Unwinds.size(); i != e; ++i) { |
Bill Wendling | cc88595 | 2011-04-11 21:32:34 +0000 | [diff] [blame] | 658 | BranchInst::Create(TrapBlock, Unwinds[i]); |
Bill Wendling | 8d90b71 | 2011-01-07 02:54:45 +0000 | [diff] [blame] | 659 | Unwinds[i]->eraseFromParent(); |
| 660 | } |
| 661 | |
| 662 | // Following any allocas not in the entry block, update the saved SP in the |
| 663 | // jmpbuf to the new value. |
| 664 | for (unsigned i = 0, e = JmpbufUpdatePoints.size(); i != e; ++i) { |
| 665 | Instruction *AI = JmpbufUpdatePoints[i]; |
| 666 | Instruction *StackAddr = CallInst::Create(StackAddrFn, "sp"); |
| 667 | StackAddr->insertAfter(AI); |
| 668 | Instruction *StoreStackAddr = new StoreInst(StackAddr, StackPtr, true); |
| 669 | StoreStackAddr->insertAfter(StackAddr); |
| 670 | } |
| 671 | |
| 672 | // Finally, for any returns from this function, if this function contains an |
| 673 | // invoke, add a call to unregister the function context. |
| 674 | for (unsigned i = 0, e = Returns.size(); i != e; ++i) |
| 675 | CallInst::Create(UnregisterFn, FunctionContext, "", Returns[i]); |
| 676 | |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 677 | return true; |
| 678 | } |
| 679 | |
| 680 | bool SjLjEHPass::runOnFunction(Function &F) { |
| 681 | bool Res = insertSjLjEHSupport(F); |
| 682 | return Res; |
| 683 | } |