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