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