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" |
| 25 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 26 | #include "llvm/Transforms/Utils/Local.h" |
| 27 | #include "llvm/ADT/Statistic.h" |
Jim Grosbach | 606f3d6 | 2009-08-17 21:40:03 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallVector.h" |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CommandLine.h" |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Debug.h" |
| 31 | #include "llvm/Support/raw_ostream.h" |
| 32 | #include "llvm/Target/TargetLowering.h" |
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 | |
| 42 | const TargetLowering *TLI; |
| 43 | |
| 44 | const Type *FunctionContextTy; |
| 45 | Constant *RegisterFn; |
| 46 | Constant *UnregisterFn; |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 47 | Constant *BuiltinSetjmpFn; |
| 48 | Constant *FrameAddrFn; |
Jim Grosbach | 0798edd | 2010-05-27 23:49:24 +0000 | [diff] [blame] | 49 | Constant *StackAddrFn; |
| 50 | Constant *StackRestoreFn; |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 51 | Constant *LSDAAddrFn; |
| 52 | Value *PersonalityFn; |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 53 | Constant *SelectorFn; |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 54 | Constant *ExceptionFn; |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 55 | Constant *CallSiteFn; |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 56 | |
| 57 | Value *CallSite; |
| 58 | public: |
| 59 | static char ID; // Pass identification, replacement for typeid |
| 60 | explicit SjLjEHPass(const TargetLowering *tli = NULL) |
| 61 | : FunctionPass(&ID), TLI(tli) { } |
| 62 | bool doInitialization(Module &M); |
| 63 | bool runOnFunction(Function &F); |
| 64 | |
| 65 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { } |
| 66 | const char *getPassName() const { |
| 67 | return "SJLJ Exception Handling preparation"; |
| 68 | } |
| 69 | |
| 70 | private: |
Jim Grosbach | b58a59b | 2010-03-04 22:07:46 +0000 | [diff] [blame] | 71 | void insertCallSiteStore(Instruction *I, int Number, Value *CallSite); |
| 72 | void markInvokeCallSite(InvokeInst *II, int InvokeNo, Value *CallSite, |
Jim Grosbach | 0bb61c5 | 2009-08-31 01:35:03 +0000 | [diff] [blame] | 73 | SwitchInst *CatchSwitch); |
Jim Grosbach | 606f3d6 | 2009-08-17 21:40:03 +0000 | [diff] [blame] | 74 | void splitLiveRangesLiveAcrossInvokes(SmallVector<InvokeInst*,16> &Invokes); |
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 |
| 90 | const Type *VoidPtrTy = |
Duncan Sands | ac53a0b | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 91 | Type::getInt8PtrTy(M.getContext()); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 92 | const Type *Int32Ty = Type::getInt32Ty(M.getContext()); |
| 93 | FunctionContextTy = |
| 94 | StructType::get(M.getContext(), |
| 95 | VoidPtrTy, // __prev |
| 96 | Int32Ty, // call_site |
| 97 | ArrayType::get(Int32Ty, 4), // __data |
| 98 | VoidPtrTy, // __personality |
| 99 | VoidPtrTy, // __lsda |
| 100 | ArrayType::get(VoidPtrTy, 5), // __jbuf |
| 101 | NULL); |
| 102 | RegisterFn = M.getOrInsertFunction("_Unwind_SjLj_Register", |
| 103 | Type::getVoidTy(M.getContext()), |
| 104 | PointerType::getUnqual(FunctionContextTy), |
| 105 | (Type *)0); |
| 106 | UnregisterFn = |
| 107 | M.getOrInsertFunction("_Unwind_SjLj_Unregister", |
| 108 | Type::getVoidTy(M.getContext()), |
| 109 | PointerType::getUnqual(FunctionContextTy), |
| 110 | (Type *)0); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 111 | FrameAddrFn = Intrinsic::getDeclaration(&M, Intrinsic::frameaddress); |
Jim Grosbach | 0798edd | 2010-05-27 23:49:24 +0000 | [diff] [blame] | 112 | StackAddrFn = Intrinsic::getDeclaration(&M, Intrinsic::stacksave); |
| 113 | StackRestoreFn = Intrinsic::getDeclaration(&M, Intrinsic::stackrestore); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 114 | BuiltinSetjmpFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_sjlj_setjmp); |
| 115 | LSDAAddrFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_sjlj_lsda); |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 116 | SelectorFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_selector); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 117 | ExceptionFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_exception); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 118 | CallSiteFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_sjlj_callsite); |
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 | |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 134 | /// markInvokeCallSite - Insert code to mark the call_site for this invoke |
Jim Grosbach | b58a59b | 2010-03-04 22:07:46 +0000 | [diff] [blame] | 135 | void SjLjEHPass::markInvokeCallSite(InvokeInst *II, int InvokeNo, |
Jim Grosbach | 0bb61c5 | 2009-08-31 01:35:03 +0000 | [diff] [blame] | 136 | Value *CallSite, |
| 137 | SwitchInst *CatchSwitch) { |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 138 | ConstantInt *CallSiteNoC= ConstantInt::get(Type::getInt32Ty(II->getContext()), |
Jim Grosbach | b58a59b | 2010-03-04 22:07:46 +0000 | [diff] [blame] | 139 | InvokeNo); |
Jim Grosbach | 0bb61c5 | 2009-08-31 01:35:03 +0000 | [diff] [blame] | 140 | // The runtime comes back to the dispatcher with the call_site - 1 in |
| 141 | // the context. Odd, but there it is. |
| 142 | ConstantInt *SwitchValC = ConstantInt::get(Type::getInt32Ty(II->getContext()), |
| 143 | InvokeNo - 1); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 144 | |
| 145 | // If the unwind edge has phi nodes, split the edge. |
| 146 | if (isa<PHINode>(II->getUnwindDest()->begin())) { |
| 147 | SplitCriticalEdge(II, 1, this); |
| 148 | |
| 149 | // If there are any phi nodes left, they must have a single predecessor. |
| 150 | while (PHINode *PN = dyn_cast<PHINode>(II->getUnwindDest()->begin())) { |
| 151 | PN->replaceAllUsesWith(PN->getIncomingValue(0)); |
| 152 | PN->eraseFromParent(); |
| 153 | } |
| 154 | } |
| 155 | |
Jim Grosbach | b58a59b | 2010-03-04 22:07:46 +0000 | [diff] [blame] | 156 | // Insert the store of the call site value |
| 157 | insertCallSiteStore(II, InvokeNo, CallSite); |
| 158 | |
| 159 | // Record the call site value for the back end so it stays associated with |
| 160 | // the invoke. |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 161 | CallInst::Create(CallSiteFn, CallSiteNoC, "", II); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 162 | |
Jim Grosbach | 0bb61c5 | 2009-08-31 01:35:03 +0000 | [diff] [blame] | 163 | // Add a switch case to our unwind block. |
| 164 | CatchSwitch->addCase(SwitchValC, II->getUnwindDest()); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 165 | // We still want this to look like an invoke so we emit the LSDA properly, |
| 166 | // so we don't transform the invoke into a call here. |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /// MarkBlocksLiveIn - Insert BB and all of its predescessors into LiveBBs until |
| 170 | /// we reach blocks we've already seen. |
| 171 | static void MarkBlocksLiveIn(BasicBlock *BB, std::set<BasicBlock*> &LiveBBs) { |
| 172 | if (!LiveBBs.insert(BB).second) return; // already been here. |
| 173 | |
| 174 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) |
| 175 | MarkBlocksLiveIn(*PI, LiveBBs); |
| 176 | } |
| 177 | |
Jim Grosbach | 606f3d6 | 2009-08-17 21:40:03 +0000 | [diff] [blame] | 178 | /// splitLiveRangesAcrossInvokes - Each value that is live across an unwind edge |
| 179 | /// we spill into a stack location, guaranteeing that there is nothing live |
| 180 | /// across the unwind edge. This process also splits all critical edges |
| 181 | /// coming out of invoke's. |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 182 | void SjLjEHPass:: |
Jim Grosbach | 606f3d6 | 2009-08-17 21:40:03 +0000 | [diff] [blame] | 183 | splitLiveRangesLiveAcrossInvokes(SmallVector<InvokeInst*,16> &Invokes) { |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 184 | // First step, split all critical edges from invoke instructions. |
| 185 | for (unsigned i = 0, e = Invokes.size(); i != e; ++i) { |
| 186 | InvokeInst *II = Invokes[i]; |
| 187 | SplitCriticalEdge(II, 0, this); |
| 188 | SplitCriticalEdge(II, 1, this); |
| 189 | assert(!isa<PHINode>(II->getNormalDest()) && |
| 190 | !isa<PHINode>(II->getUnwindDest()) && |
| 191 | "critical edge splitting left single entry phi nodes?"); |
| 192 | } |
| 193 | |
| 194 | Function *F = Invokes.back()->getParent()->getParent(); |
| 195 | |
| 196 | // To avoid having to handle incoming arguments specially, we lower each arg |
| 197 | // to a copy instruction in the entry block. This ensures that the argument |
| 198 | // value itself cannot be live across the entry block. |
| 199 | BasicBlock::iterator AfterAllocaInsertPt = F->begin()->begin(); |
| 200 | while (isa<AllocaInst>(AfterAllocaInsertPt) && |
| 201 | isa<ConstantInt>(cast<AllocaInst>(AfterAllocaInsertPt)->getArraySize())) |
| 202 | ++AfterAllocaInsertPt; |
| 203 | for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); |
| 204 | AI != E; ++AI) { |
| 205 | // This is always a no-op cast because we're casting AI to AI->getType() so |
| 206 | // src and destination types are identical. BitCast is the only possibility. |
| 207 | CastInst *NC = new BitCastInst( |
| 208 | AI, AI->getType(), AI->getName()+".tmp", AfterAllocaInsertPt); |
| 209 | AI->replaceAllUsesWith(NC); |
| 210 | // Normally its is forbidden to replace a CastInst's operand because it |
| 211 | // could cause the opcode to reflect an illegal conversion. However, we're |
| 212 | // replacing it here with the same value it was constructed with to simply |
| 213 | // make NC its user. |
| 214 | NC->setOperand(0, AI); |
| 215 | } |
| 216 | |
| 217 | // Finally, scan the code looking for instructions with bad live ranges. |
| 218 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 219 | for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ++II) { |
| 220 | // Ignore obvious cases we don't have to handle. In particular, most |
| 221 | // instructions either have no uses or only have a single use inside the |
| 222 | // current block. Ignore them quickly. |
| 223 | Instruction *Inst = II; |
| 224 | if (Inst->use_empty()) continue; |
| 225 | if (Inst->hasOneUse() && |
| 226 | cast<Instruction>(Inst->use_back())->getParent() == BB && |
| 227 | !isa<PHINode>(Inst->use_back())) continue; |
| 228 | |
| 229 | // If this is an alloca in the entry block, it's not a real register |
| 230 | // value. |
| 231 | if (AllocaInst *AI = dyn_cast<AllocaInst>(Inst)) |
| 232 | if (isa<ConstantInt>(AI->getArraySize()) && BB == F->begin()) |
| 233 | continue; |
| 234 | |
| 235 | // Avoid iterator invalidation by copying users to a temporary vector. |
Jim Grosbach | 606f3d6 | 2009-08-17 21:40:03 +0000 | [diff] [blame] | 236 | SmallVector<Instruction*,16> Users; |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 237 | for (Value::use_iterator UI = Inst->use_begin(), E = Inst->use_end(); |
| 238 | UI != E; ++UI) { |
| 239 | Instruction *User = cast<Instruction>(*UI); |
| 240 | if (User->getParent() != BB || isa<PHINode>(User)) |
| 241 | Users.push_back(User); |
| 242 | } |
| 243 | |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 244 | // Find all of the blocks that this value is live in. |
| 245 | std::set<BasicBlock*> LiveBBs; |
| 246 | LiveBBs.insert(Inst->getParent()); |
| 247 | while (!Users.empty()) { |
| 248 | Instruction *U = Users.back(); |
| 249 | Users.pop_back(); |
| 250 | |
| 251 | if (!isa<PHINode>(U)) { |
| 252 | MarkBlocksLiveIn(U->getParent(), LiveBBs); |
| 253 | } else { |
| 254 | // Uses for a PHI node occur in their predecessor block. |
| 255 | PHINode *PN = cast<PHINode>(U); |
| 256 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 257 | if (PN->getIncomingValue(i) == Inst) |
| 258 | MarkBlocksLiveIn(PN->getIncomingBlock(i), LiveBBs); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | // Now that we know all of the blocks that this thing is live in, see if |
| 263 | // it includes any of the unwind locations. |
| 264 | bool NeedsSpill = false; |
| 265 | for (unsigned i = 0, e = Invokes.size(); i != e; ++i) { |
| 266 | BasicBlock *UnwindBlock = Invokes[i]->getUnwindDest(); |
| 267 | if (UnwindBlock != BB && LiveBBs.count(UnwindBlock)) { |
| 268 | NeedsSpill = true; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // If we decided we need a spill, do it. |
| 273 | if (NeedsSpill) { |
| 274 | ++NumSpilled; |
| 275 | DemoteRegToStack(*Inst, true); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | bool SjLjEHPass::insertSjLjEHSupport(Function &F) { |
Jim Grosbach | 606f3d6 | 2009-08-17 21:40:03 +0000 | [diff] [blame] | 281 | SmallVector<ReturnInst*,16> Returns; |
| 282 | SmallVector<UnwindInst*,16> Unwinds; |
| 283 | SmallVector<InvokeInst*,16> Invokes; |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 284 | |
| 285 | // Look through the terminators of the basic blocks to find invokes, returns |
Jim Grosbach | b58a59b | 2010-03-04 22:07:46 +0000 | [diff] [blame] | 286 | // and unwinds. |
| 287 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 288 | if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) { |
| 289 | // Remember all return instructions in case we insert an invoke into this |
| 290 | // function. |
| 291 | Returns.push_back(RI); |
| 292 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) { |
| 293 | Invokes.push_back(II); |
| 294 | } else if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) { |
| 295 | Unwinds.push_back(UI); |
| 296 | } |
Jim Grosbach | b58a59b | 2010-03-04 22:07:46 +0000 | [diff] [blame] | 297 | } |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 298 | // If we don't have any invokes or unwinds, there's nothing to do. |
| 299 | if (Unwinds.empty() && Invokes.empty()) return false; |
| 300 | |
Jim Grosbach | 0798edd | 2010-05-27 23:49:24 +0000 | [diff] [blame] | 301 | // Find the eh.selector.*, eh.exception and alloca calls. |
| 302 | // |
| 303 | // Remember any allocas() that aren't in the entry block, as the |
| 304 | // jmpbuf saved SP will need to be updated for them. |
| 305 | // |
| 306 | // We'll use the first eh.selector to determine the right personality |
| 307 | // function to use. For SJLJ, we always use the same personality for the |
| 308 | // whole function, not on a per-selector basis. |
Jim Grosbach | a235d13 | 2009-08-23 18:13:48 +0000 | [diff] [blame] | 309 | // FIXME: That's a bit ugly. Better way? |
| 310 | SmallVector<CallInst*,16> EH_Selectors; |
| 311 | SmallVector<CallInst*,16> EH_Exceptions; |
Jim Grosbach | 0798edd | 2010-05-27 23:49:24 +0000 | [diff] [blame] | 312 | SmallVector<Instruction*,16> JmpbufUpdatePoints; |
| 313 | // Note: Skip the entry block since there's nothing there that interests |
| 314 | // us. eh.selector and eh.exception shouldn't ever be there, and we |
| 315 | // want to disregard any allocas that are there. |
| 316 | for (Function::iterator BB = F.begin(), E = F.end(); ++BB != E;) { |
Jim Grosbach | a235d13 | 2009-08-23 18:13:48 +0000 | [diff] [blame] | 317 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
| 318 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 319 | if (CI->getCalledFunction() == SelectorFn) { |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 320 | if (!PersonalityFn) PersonalityFn = CI->getOperand(2); |
Jim Grosbach | a235d13 | 2009-08-23 18:13:48 +0000 | [diff] [blame] | 321 | EH_Selectors.push_back(CI); |
| 322 | } else if (CI->getCalledFunction() == ExceptionFn) { |
| 323 | EH_Exceptions.push_back(CI); |
Jim Grosbach | 0798edd | 2010-05-27 23:49:24 +0000 | [diff] [blame] | 324 | } else if (CI->getCalledFunction() == StackRestoreFn) { |
| 325 | JmpbufUpdatePoints.push_back(CI); |
Jim Grosbach | a235d13 | 2009-08-23 18:13:48 +0000 | [diff] [blame] | 326 | } |
Jim Grosbach | 0798edd | 2010-05-27 23:49:24 +0000 | [diff] [blame] | 327 | } else if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) { |
| 328 | JmpbufUpdatePoints.push_back(AI); |
Jim Grosbach | a235d13 | 2009-08-23 18:13:48 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | } |
| 332 | // If we don't have any eh.selector calls, we can't determine the personality |
| 333 | // function. Without a personality function, we can't process exceptions. |
| 334 | if (!PersonalityFn) return false; |
| 335 | |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 336 | NumInvokes += Invokes.size(); |
| 337 | NumUnwinds += Unwinds.size(); |
| 338 | |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 339 | if (!Invokes.empty()) { |
| 340 | // We have invokes, so we need to add register/unregister calls to get |
| 341 | // this function onto the global unwind stack. |
Jim Grosbach | 0bb61c5 | 2009-08-31 01:35:03 +0000 | [diff] [blame] | 342 | // |
| 343 | // First thing we need to do is scan the whole function for values that are |
| 344 | // live across unwind edges. Each value that is live across an unwind edge |
| 345 | // we spill into a stack location, guaranteeing that there is nothing live |
| 346 | // across the unwind edge. This process also splits all critical edges |
| 347 | // coming out of invoke's. |
| 348 | splitLiveRangesLiveAcrossInvokes(Invokes); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 349 | |
| 350 | BasicBlock *EntryBB = F.begin(); |
| 351 | // Create an alloca for the incoming jump buffer ptr and the new jump buffer |
| 352 | // that needs to be restored on all exits from the function. This is an |
| 353 | // alloca because the value needs to be added to the global context list. |
| 354 | unsigned Align = 4; // FIXME: Should be a TLI check? |
| 355 | AllocaInst *FunctionContext = |
| 356 | new AllocaInst(FunctionContextTy, 0, Align, |
| 357 | "fcn_context", F.begin()->begin()); |
| 358 | |
| 359 | Value *Idxs[2]; |
| 360 | const Type *Int32Ty = Type::getInt32Ty(F.getContext()); |
| 361 | Value *Zero = ConstantInt::get(Int32Ty, 0); |
| 362 | // We need to also keep around a reference to the call_site field |
| 363 | Idxs[0] = Zero; |
| 364 | Idxs[1] = ConstantInt::get(Int32Ty, 1); |
| 365 | CallSite = GetElementPtrInst::Create(FunctionContext, Idxs, Idxs+2, |
| 366 | "call_site", |
| 367 | EntryBB->getTerminator()); |
| 368 | |
| 369 | // The exception selector comes back in context->data[1] |
| 370 | Idxs[1] = ConstantInt::get(Int32Ty, 2); |
| 371 | Value *FCData = GetElementPtrInst::Create(FunctionContext, Idxs, Idxs+2, |
| 372 | "fc_data", |
| 373 | EntryBB->getTerminator()); |
| 374 | Idxs[1] = ConstantInt::get(Int32Ty, 1); |
| 375 | Value *SelectorAddr = GetElementPtrInst::Create(FCData, Idxs, Idxs+2, |
| 376 | "exc_selector_gep", |
| 377 | EntryBB->getTerminator()); |
| 378 | // The exception value comes back in context->data[0] |
| 379 | Idxs[1] = Zero; |
| 380 | Value *ExceptionAddr = GetElementPtrInst::Create(FCData, Idxs, Idxs+2, |
| 381 | "exception_gep", |
| 382 | EntryBB->getTerminator()); |
| 383 | |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 384 | // The result of the eh.selector call will be replaced with a |
| 385 | // a reference to the selector value returned in the function |
| 386 | // context. We leave the selector itself so the EH analysis later |
| 387 | // can use it. |
| 388 | for (int i = 0, e = EH_Selectors.size(); i < e; ++i) { |
| 389 | CallInst *I = EH_Selectors[i]; |
| 390 | Value *SelectorVal = new LoadInst(SelectorAddr, "select_val", true, I); |
| 391 | I->replaceAllUsesWith(SelectorVal); |
| 392 | } |
| 393 | // eh.exception calls are replaced with references to the proper |
| 394 | // location in the context. Unlike eh.selector, the eh.exception |
| 395 | // calls are removed entirely. |
| 396 | for (int i = 0, e = EH_Exceptions.size(); i < e; ++i) { |
| 397 | CallInst *I = EH_Exceptions[i]; |
| 398 | // Possible for there to be duplicates, so check to make sure |
| 399 | // the instruction hasn't already been removed. |
| 400 | if (!I->getParent()) continue; |
| 401 | Value *Val = new LoadInst(ExceptionAddr, "exception", true, I); |
Duncan Sands | ac53a0b | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 402 | const Type *Ty = Type::getInt8PtrTy(F.getContext()); |
Jim Grosbach | 606f3d6 | 2009-08-17 21:40:03 +0000 | [diff] [blame] | 403 | Val = CastInst::Create(Instruction::IntToPtr, Val, Ty, "", I); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 404 | |
| 405 | I->replaceAllUsesWith(Val); |
| 406 | I->eraseFromParent(); |
| 407 | } |
| 408 | |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 409 | // The entry block changes to have the eh.sjlj.setjmp, with a conditional |
| 410 | // branch to a dispatch block for non-zero returns. If we return normally, |
| 411 | // we're not handling an exception and just register the function context |
| 412 | // and continue. |
| 413 | |
| 414 | // Create the dispatch block. The dispatch block is basically a big switch |
| 415 | // statement that goes to all of the invoke landing pads. |
| 416 | BasicBlock *DispatchBlock = |
| 417 | BasicBlock::Create(F.getContext(), "eh.sjlj.setjmp.catch", &F); |
| 418 | |
| 419 | // Insert a load in the Catch block, and a switch on its value. By default, |
| 420 | // we go to a block that just does an unwind (which is the correct action |
| 421 | // for a standard call). |
Jim Grosbach | 03825f8 | 2010-01-15 00:32:47 +0000 | [diff] [blame] | 422 | BasicBlock *UnwindBlock = |
| 423 | BasicBlock::Create(F.getContext(), "unwindbb", &F); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 424 | Unwinds.push_back(new UnwindInst(F.getContext(), UnwindBlock)); |
| 425 | |
| 426 | Value *DispatchLoad = new LoadInst(CallSite, "invoke.num", true, |
| 427 | DispatchBlock); |
| 428 | SwitchInst *DispatchSwitch = |
Jim Grosbach | 03825f8 | 2010-01-15 00:32:47 +0000 | [diff] [blame] | 429 | SwitchInst::Create(DispatchLoad, UnwindBlock, Invokes.size(), |
| 430 | DispatchBlock); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 431 | // Split the entry block to insert the conditional branch for the setjmp. |
| 432 | BasicBlock *ContBlock = EntryBB->splitBasicBlock(EntryBB->getTerminator(), |
| 433 | "eh.sjlj.setjmp.cont"); |
| 434 | |
| 435 | // Populate the Function Context |
| 436 | // 1. LSDA address |
| 437 | // 2. Personality function address |
Jim Grosbach | 0798edd | 2010-05-27 23:49:24 +0000 | [diff] [blame] | 438 | // 3. jmpbuf (save SP, FP and call eh.sjlj.setjmp) |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 439 | |
| 440 | // LSDA address |
| 441 | Idxs[0] = Zero; |
| 442 | Idxs[1] = ConstantInt::get(Int32Ty, 4); |
| 443 | Value *LSDAFieldPtr = |
| 444 | GetElementPtrInst::Create(FunctionContext, Idxs, Idxs+2, |
| 445 | "lsda_gep", |
| 446 | EntryBB->getTerminator()); |
| 447 | Value *LSDA = CallInst::Create(LSDAAddrFn, "lsda_addr", |
| 448 | EntryBB->getTerminator()); |
| 449 | new StoreInst(LSDA, LSDAFieldPtr, true, EntryBB->getTerminator()); |
| 450 | |
| 451 | Idxs[1] = ConstantInt::get(Int32Ty, 3); |
| 452 | Value *PersonalityFieldPtr = |
| 453 | GetElementPtrInst::Create(FunctionContext, Idxs, Idxs+2, |
| 454 | "lsda_gep", |
| 455 | EntryBB->getTerminator()); |
| 456 | new StoreInst(PersonalityFn, PersonalityFieldPtr, true, |
| 457 | EntryBB->getTerminator()); |
| 458 | |
Jim Grosbach | 0798edd | 2010-05-27 23:49:24 +0000 | [diff] [blame] | 459 | // Save the frame pointer. |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 460 | Idxs[1] = ConstantInt::get(Int32Ty, 5); |
Jim Grosbach | 0798edd | 2010-05-27 23:49:24 +0000 | [diff] [blame] | 461 | Value *JBufPtr |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 462 | = GetElementPtrInst::Create(FunctionContext, Idxs, Idxs+2, |
| 463 | "jbuf_gep", |
| 464 | EntryBB->getTerminator()); |
| 465 | Idxs[1] = ConstantInt::get(Int32Ty, 0); |
Jim Grosbach | 0798edd | 2010-05-27 23:49:24 +0000 | [diff] [blame] | 466 | Value *FramePtr = |
| 467 | GetElementPtrInst::Create(JBufPtr, Idxs, Idxs+2, "jbuf_fp_gep", |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 468 | EntryBB->getTerminator()); |
| 469 | |
| 470 | Value *Val = CallInst::Create(FrameAddrFn, |
| 471 | ConstantInt::get(Int32Ty, 0), |
| 472 | "fp", |
| 473 | EntryBB->getTerminator()); |
Jim Grosbach | 0798edd | 2010-05-27 23:49:24 +0000 | [diff] [blame] | 474 | new StoreInst(Val, FramePtr, true, EntryBB->getTerminator()); |
| 475 | |
| 476 | // Save the stack pointer. |
| 477 | Idxs[1] = ConstantInt::get(Int32Ty, 2); |
| 478 | Value *StackPtr = |
| 479 | GetElementPtrInst::Create(JBufPtr, Idxs, Idxs+2, "jbuf_sp_gep", |
| 480 | EntryBB->getTerminator()); |
| 481 | |
| 482 | Val = CallInst::Create(StackAddrFn, "sp", EntryBB->getTerminator()); |
| 483 | new StoreInst(Val, StackPtr, true, EntryBB->getTerminator()); |
| 484 | |
| 485 | // Call the setjmp instrinsic. It fills in the rest of the jmpbuf. |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 486 | Value *SetjmpArg = |
Jim Grosbach | 0798edd | 2010-05-27 23:49:24 +0000 | [diff] [blame] | 487 | CastInst::Create(Instruction::BitCast, JBufPtr, |
Duncan Sands | ac53a0b | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 488 | Type::getInt8PtrTy(F.getContext()), "", |
| 489 | EntryBB->getTerminator()); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 490 | Value *DispatchVal = CallInst::Create(BuiltinSetjmpFn, SetjmpArg, |
| 491 | "dispatch", |
| 492 | EntryBB->getTerminator()); |
Jim Grosbach | 0798edd | 2010-05-27 23:49:24 +0000 | [diff] [blame] | 493 | // check the return value of the setjmp. non-zero goes to dispatcher. |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 494 | Value *IsNormal = new ICmpInst(EntryBB->getTerminator(), |
| 495 | ICmpInst::ICMP_EQ, DispatchVal, Zero, |
| 496 | "notunwind"); |
| 497 | // Nuke the uncond branch. |
| 498 | EntryBB->getTerminator()->eraseFromParent(); |
| 499 | |
| 500 | // Put in a new condbranch in its place. |
| 501 | BranchInst::Create(ContBlock, DispatchBlock, IsNormal, EntryBB); |
| 502 | |
| 503 | // Register the function context and make sure it's known to not throw |
| 504 | CallInst *Register = |
| 505 | CallInst::Create(RegisterFn, FunctionContext, "", |
| 506 | ContBlock->getTerminator()); |
| 507 | Register->setDoesNotThrow(); |
| 508 | |
Jim Grosbach | 0bb61c5 | 2009-08-31 01:35:03 +0000 | [diff] [blame] | 509 | // At this point, we are all set up, update the invoke instructions |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 510 | // to mark their call_site values, and fill in the dispatch switch |
| 511 | // accordingly. |
Jim Grosbach | f38a33c | 2010-01-21 20:10:22 +0000 | [diff] [blame] | 512 | for (unsigned i = 0, e = Invokes.size(); i != e; ++i) |
Jim Grosbach | 0bb61c5 | 2009-08-31 01:35:03 +0000 | [diff] [blame] | 513 | markInvokeCallSite(Invokes[i], i+1, CallSite, DispatchSwitch); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 514 | |
Jim Grosbach | b58a59b | 2010-03-04 22:07:46 +0000 | [diff] [blame] | 515 | // Mark call instructions that aren't nounwind as no-action |
| 516 | // (call_site == -1). Skip the entry block, as prior to then, no function |
| 517 | // context has been created for this function and any unexpected exceptions |
| 518 | // thrown will go directly to the caller's context, which is what we want |
| 519 | // anyway, so no need to do anything here. |
| 520 | for (Function::iterator BB = F.begin(), E = F.end(); ++BB != E;) { |
| 521 | for (BasicBlock::iterator I = BB->begin(), end = BB->end(); I != end; ++I) |
| 522 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 523 | // Ignore calls to the EH builtins (eh.selector, eh.exception) |
| 524 | Constant *Callee = CI->getCalledFunction(); |
| 525 | if (Callee != SelectorFn && Callee != ExceptionFn |
| 526 | && !CI->doesNotThrow()) |
| 527 | insertCallSiteStore(CI, -1, CallSite); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 528 | } |
Jim Grosbach | b58a59b | 2010-03-04 22:07:46 +0000 | [diff] [blame] | 529 | } |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 530 | |
| 531 | // Replace all unwinds with a branch to the unwind handler. |
| 532 | // ??? Should this ever happen with sjlj exceptions? |
| 533 | for (unsigned i = 0, e = Unwinds.size(); i != e; ++i) { |
| 534 | BranchInst::Create(UnwindBlock, Unwinds[i]); |
| 535 | Unwinds[i]->eraseFromParent(); |
| 536 | } |
| 537 | |
Jim Grosbach | 0798edd | 2010-05-27 23:49:24 +0000 | [diff] [blame] | 538 | // Following any allocas not in the entry block, update the saved SP |
| 539 | // in the jmpbuf to the new value. |
| 540 | for (unsigned i = 0, e = JmpbufUpdatePoints.size(); i != e; ++i) { |
| 541 | Instruction *AI = JmpbufUpdatePoints[i]; |
| 542 | Instruction *StackAddr = CallInst::Create(StackAddrFn, "sp"); |
| 543 | StackAddr->insertAfter(AI); |
| 544 | Instruction *StoreStackAddr = new StoreInst(StackAddr, StackPtr, true); |
| 545 | StoreStackAddr->insertAfter(StackAddr); |
| 546 | } |
| 547 | |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 548 | // Finally, for any returns from this function, if this function contains an |
| 549 | // invoke, add a call to unregister the function context. |
| 550 | for (unsigned i = 0, e = Returns.size(); i != e; ++i) |
| 551 | CallInst::Create(UnregisterFn, FunctionContext, "", Returns[i]); |
| 552 | } |
| 553 | |
| 554 | return true; |
| 555 | } |
| 556 | |
| 557 | bool SjLjEHPass::runOnFunction(Function &F) { |
| 558 | bool Res = insertSjLjEHSupport(F); |
| 559 | return Res; |
| 560 | } |