Bill Wendling | eabae1d | 2012-03-13 20:04:21 +0000 | [diff] [blame] | 1 | //===- SjLjEHPrepare.cpp - Eliminate Invoke & Unwind instructions ---------===// |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 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" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/Passes.h" |
| 17 | #include "llvm/ADT/DenseMap.h" |
| 18 | #include "llvm/ADT/SetVector.h" |
| 19 | #include "llvm/ADT/SmallPtrSet.h" |
| 20 | #include "llvm/ADT/SmallVector.h" |
| 21 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Constants.h" |
| 23 | #include "llvm/IR/DataLayout.h" |
| 24 | #include "llvm/IR/DerivedTypes.h" |
| 25 | #include "llvm/IR/IRBuilder.h" |
| 26 | #include "llvm/IR/Instructions.h" |
| 27 | #include "llvm/IR/Intrinsics.h" |
| 28 | #include "llvm/IR/LLVMContext.h" |
| 29 | #include "llvm/IR/Module.h" |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 30 | #include "llvm/Pass.h" |
Chandler Carruth | 06cb8ed | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 31 | #include "llvm/Support/CommandLine.h" |
| 32 | #include "llvm/Support/Debug.h" |
| 33 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 06cb8ed | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetLowering.h" |
| 35 | #include "llvm/Transforms/Scalar.h" |
| 36 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 37 | #include "llvm/Transforms/Utils/Local.h" |
Benjamin Kramer | f788854 | 2010-11-06 11:45:59 +0000 | [diff] [blame] | 38 | #include <set> |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | |
| 41 | STATISTIC(NumInvokes, "Number of invokes replaced"); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 42 | STATISTIC(NumSpilled, "Number of registers live across unwind edges"); |
| 43 | |
| 44 | namespace { |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 45 | class SjLjEHPrepare : public FunctionPass { |
| 46 | const TargetMachine *TM; |
| 47 | Type *FunctionContextTy; |
| 48 | Constant *RegisterFn; |
| 49 | Constant *UnregisterFn; |
| 50 | Constant *BuiltinSetjmpFn; |
| 51 | Constant *FrameAddrFn; |
| 52 | Constant *StackAddrFn; |
| 53 | Constant *StackRestoreFn; |
| 54 | Constant *LSDAAddrFn; |
| 55 | Value *PersonalityFn; |
| 56 | Constant *CallSiteFn; |
| 57 | Constant *FuncCtxFn; |
| 58 | AllocaInst *FuncCtx; |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 59 | |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 60 | public: |
| 61 | static char ID; // Pass identification, replacement for typeid |
| 62 | explicit SjLjEHPrepare(const TargetMachine *TM) : FunctionPass(ID), TM(TM) {} |
| 63 | bool doInitialization(Module &M); |
| 64 | bool runOnFunction(Function &F); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 65 | |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 66 | virtual void getAnalysisUsage(AnalysisUsage &AU) const {} |
| 67 | const char *getPassName() const { |
| 68 | return "SJLJ Exception Handling preparation"; |
| 69 | } |
| 70 | |
| 71 | private: |
| 72 | bool setupEntryBlockAndCallSites(Function &F); |
| 73 | void substituteLPadValues(LandingPadInst *LPI, Value *ExnVal, Value *SelVal); |
| 74 | Value *setupFunctionContext(Function &F, ArrayRef<LandingPadInst *> LPads); |
| 75 | void lowerIncomingArguments(Function &F); |
| 76 | void lowerAcrossUnwindEdges(Function &F, ArrayRef<InvokeInst *> Invokes); |
| 77 | void insertCallSiteStore(Instruction *I, int Number); |
| 78 | }; |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 79 | } // end anonymous namespace |
| 80 | |
Bill Wendling | eabae1d | 2012-03-13 20:04:21 +0000 | [diff] [blame] | 81 | char SjLjEHPrepare::ID = 0; |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 82 | |
Bill Wendling | eabae1d | 2012-03-13 20:04:21 +0000 | [diff] [blame] | 83 | // Public Interface To the SjLjEHPrepare pass. |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 84 | FunctionPass *llvm::createSjLjEHPreparePass(const TargetMachine *TM) { |
| 85 | return new SjLjEHPrepare(TM); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 86 | } |
Jim Grosbach | a235d13 | 2009-08-23 18:13:48 +0000 | [diff] [blame] | 87 | // doInitialization - Set up decalarations and types needed to process |
| 88 | // exceptions. |
Bill Wendling | eabae1d | 2012-03-13 20:04:21 +0000 | [diff] [blame] | 89 | bool SjLjEHPrepare::doInitialization(Module &M) { |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 90 | // Build the function context structure. |
| 91 | // builtin_setjmp uses a five word jbuf |
Jay Foad | 5fdd6c8 | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 92 | Type *VoidPtrTy = Type::getInt8PtrTy(M.getContext()); |
| 93 | Type *Int32Ty = Type::getInt32Ty(M.getContext()); |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 94 | FunctionContextTy = StructType::get(VoidPtrTy, // __prev |
| 95 | Int32Ty, // call_site |
| 96 | ArrayType::get(Int32Ty, 4), // __data |
| 97 | VoidPtrTy, // __personality |
| 98 | VoidPtrTy, // __lsda |
| 99 | ArrayType::get(VoidPtrTy, 5), // __jbuf |
| 100 | NULL); |
| 101 | RegisterFn = M.getOrInsertFunction( |
| 102 | "_Unwind_SjLj_Register", Type::getVoidTy(M.getContext()), |
| 103 | PointerType::getUnqual(FunctionContextTy), (Type *)0); |
| 104 | UnregisterFn = M.getOrInsertFunction( |
| 105 | "_Unwind_SjLj_Unregister", Type::getVoidTy(M.getContext()), |
| 106 | PointerType::getUnqual(FunctionContextTy), (Type *)0); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 107 | FrameAddrFn = Intrinsic::getDeclaration(&M, Intrinsic::frameaddress); |
Jim Grosbach | 0798edd | 2010-05-27 23:49:24 +0000 | [diff] [blame] | 108 | StackAddrFn = Intrinsic::getDeclaration(&M, Intrinsic::stacksave); |
Bob Wilson | 20c918d | 2011-11-16 07:12:00 +0000 | [diff] [blame] | 109 | StackRestoreFn = Intrinsic::getDeclaration(&M, Intrinsic::stackrestore); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 110 | BuiltinSetjmpFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_sjlj_setjmp); |
| 111 | LSDAAddrFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_sjlj_lsda); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 112 | CallSiteFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_sjlj_callsite); |
Bill Wendling | cc8cf97 | 2011-09-28 21:56:53 +0000 | [diff] [blame] | 113 | FuncCtxFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_sjlj_functioncontext); |
Jim Grosbach | a235d13 | 2009-08-23 18:13:48 +0000 | [diff] [blame] | 114 | PersonalityFn = 0; |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 115 | |
| 116 | return true; |
| 117 | } |
| 118 | |
Jim Grosbach | b58a59b | 2010-03-04 22:07:46 +0000 | [diff] [blame] | 119 | /// insertCallSiteStore - Insert a store of the call-site value to the |
| 120 | /// function context |
Bill Wendling | eabae1d | 2012-03-13 20:04:21 +0000 | [diff] [blame] | 121 | void SjLjEHPrepare::insertCallSiteStore(Instruction *I, int Number) { |
Bill Wendling | 4cc4666 | 2012-01-27 02:02:24 +0000 | [diff] [blame] | 122 | IRBuilder<> Builder(I); |
| 123 | |
| 124 | // Get a reference to the call_site field. |
| 125 | Type *Int32Ty = Type::getInt32Ty(I->getContext()); |
| 126 | Value *Zero = ConstantInt::get(Int32Ty, 0); |
| 127 | Value *One = ConstantInt::get(Int32Ty, 1); |
| 128 | Value *Idxs[2] = { Zero, One }; |
| 129 | Value *CallSite = Builder.CreateGEP(FuncCtx, Idxs, "call_site"); |
| 130 | |
| 131 | // Insert a store of the call-site number |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 132 | ConstantInt *CallSiteNoC = |
| 133 | ConstantInt::get(Type::getInt32Ty(I->getContext()), Number); |
| 134 | Builder.CreateStore(CallSiteNoC, CallSite, true /*volatile*/); |
Jim Grosbach | b58a59b | 2010-03-04 22:07:46 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Chad Rosier | bace5da | 2012-03-16 01:04:00 +0000 | [diff] [blame] | 137 | /// MarkBlocksLiveIn - Insert BB and all of its predescessors into LiveBBs until |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 138 | /// we reach blocks we've already seen. |
Chad Rosier | bace5da | 2012-03-16 01:04:00 +0000 | [diff] [blame] | 139 | static void MarkBlocksLiveIn(BasicBlock *BB, |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 140 | SmallPtrSet<BasicBlock *, 64> &LiveBBs) { |
| 141 | if (!LiveBBs.insert(BB)) |
| 142 | return; // already been here. |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 143 | |
Chad Rosier | bace5da | 2012-03-16 01:04:00 +0000 | [diff] [blame] | 144 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) |
| 145 | MarkBlocksLiveIn(*PI, LiveBBs); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Bill Wendling | 69fdcd7 | 2011-12-14 22:45:33 +0000 | [diff] [blame] | 148 | /// substituteLPadValues - Substitute the values returned by the landingpad |
| 149 | /// instruction with those returned by the personality function. |
Bill Wendling | eabae1d | 2012-03-13 20:04:21 +0000 | [diff] [blame] | 150 | void SjLjEHPrepare::substituteLPadValues(LandingPadInst *LPI, Value *ExnVal, |
| 151 | Value *SelVal) { |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 152 | SmallVector<Value *, 8> UseWorkList(LPI->use_begin(), LPI->use_end()); |
Bill Wendling | 69fdcd7 | 2011-12-14 22:45:33 +0000 | [diff] [blame] | 153 | while (!UseWorkList.empty()) { |
| 154 | Value *Val = UseWorkList.pop_back_val(); |
| 155 | ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(Val); |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 156 | if (!EVI) |
| 157 | continue; |
| 158 | if (EVI->getNumIndices() != 1) |
| 159 | continue; |
Bill Wendling | 69fdcd7 | 2011-12-14 22:45:33 +0000 | [diff] [blame] | 160 | if (*EVI->idx_begin() == 0) |
| 161 | EVI->replaceAllUsesWith(ExnVal); |
| 162 | else if (*EVI->idx_begin() == 1) |
| 163 | EVI->replaceAllUsesWith(SelVal); |
| 164 | if (EVI->getNumUses() == 0) |
| 165 | EVI->eraseFromParent(); |
| 166 | } |
| 167 | |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 168 | if (LPI->getNumUses() == 0) |
| 169 | return; |
Bill Wendling | 69fdcd7 | 2011-12-14 22:45:33 +0000 | [diff] [blame] | 170 | |
| 171 | // There are still some uses of LPI. Construct an aggregate with the exception |
| 172 | // values and replace the LPI with that aggregate. |
| 173 | Type *LPadType = LPI->getType(); |
| 174 | Value *LPadVal = UndefValue::get(LPadType); |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 175 | IRBuilder<> Builder( |
| 176 | llvm::next(BasicBlock::iterator(cast<Instruction>(SelVal)))); |
Bill Wendling | 69fdcd7 | 2011-12-14 22:45:33 +0000 | [diff] [blame] | 177 | LPadVal = Builder.CreateInsertValue(LPadVal, ExnVal, 0, "lpad.val"); |
| 178 | LPadVal = Builder.CreateInsertValue(LPadVal, SelVal, 1, "lpad.val"); |
| 179 | |
| 180 | LPI->replaceAllUsesWith(LPadVal); |
| 181 | } |
| 182 | |
Bill Wendling | 2b6bd7b | 2011-09-28 03:14:05 +0000 | [diff] [blame] | 183 | /// setupFunctionContext - Allocate the function context on the stack and fill |
| 184 | /// it with all of the data that we know at this point. |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 185 | Value *SjLjEHPrepare::setupFunctionContext(Function &F, |
| 186 | ArrayRef<LandingPadInst *> LPads) { |
Bill Wendling | 2b6bd7b | 2011-09-28 03:14:05 +0000 | [diff] [blame] | 187 | BasicBlock *EntryBB = F.begin(); |
| 188 | |
| 189 | // Create an alloca for the incoming jump buffer ptr and the new jump buffer |
| 190 | // that needs to be restored on all exits from the function. This is an alloca |
| 191 | // because the value needs to be added to the global context list. |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 192 | const TargetLowering *TLI = TM->getTargetLowering(); |
Bill Wendling | 2b6bd7b | 2011-09-28 03:14:05 +0000 | [diff] [blame] | 193 | unsigned Align = |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 194 | TLI->getDataLayout()->getPrefTypeAlignment(FunctionContextTy); |
| 195 | FuncCtx = new AllocaInst(FunctionContextTy, 0, Align, "fn_context", |
| 196 | EntryBB->begin()); |
Bill Wendling | 2b6bd7b | 2011-09-28 03:14:05 +0000 | [diff] [blame] | 197 | |
| 198 | // Fill in the function context structure. |
Bill Wendling | 2b6bd7b | 2011-09-28 03:14:05 +0000 | [diff] [blame] | 199 | for (unsigned I = 0, E = LPads.size(); I != E; ++I) { |
| 200 | LandingPadInst *LPI = LPads[I]; |
| 201 | IRBuilder<> Builder(LPI->getParent()->getFirstInsertionPt()); |
| 202 | |
Bill Wendling | 4cc4666 | 2012-01-27 02:02:24 +0000 | [diff] [blame] | 203 | // Reference the __data field. |
Benjamin Kramer | f68b87f | 2012-09-03 12:27:43 +0000 | [diff] [blame] | 204 | Value *FCData = Builder.CreateConstGEP2_32(FuncCtx, 0, 2, "__data"); |
Bill Wendling | 4cc4666 | 2012-01-27 02:02:24 +0000 | [diff] [blame] | 205 | |
| 206 | // The exception values come back in context->__data[0]. |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 207 | Value *ExceptionAddr = |
| 208 | Builder.CreateConstGEP2_32(FCData, 0, 0, "exception_gep"); |
Bill Wendling | 2b6bd7b | 2011-09-28 03:14:05 +0000 | [diff] [blame] | 209 | Value *ExnVal = Builder.CreateLoad(ExceptionAddr, true, "exn_val"); |
Benjamin Kramer | f68b87f | 2012-09-03 12:27:43 +0000 | [diff] [blame] | 210 | ExnVal = Builder.CreateIntToPtr(ExnVal, Builder.getInt8PtrTy()); |
Bill Wendling | 4cc4666 | 2012-01-27 02:02:24 +0000 | [diff] [blame] | 211 | |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 212 | Value *SelectorAddr = |
| 213 | Builder.CreateConstGEP2_32(FCData, 0, 1, "exn_selector_gep"); |
Bill Wendling | 2b6bd7b | 2011-09-28 03:14:05 +0000 | [diff] [blame] | 214 | Value *SelVal = Builder.CreateLoad(SelectorAddr, true, "exn_selector_val"); |
| 215 | |
Bill Wendling | 69fdcd7 | 2011-12-14 22:45:33 +0000 | [diff] [blame] | 216 | substituteLPadValues(LPI, ExnVal, SelVal); |
Bill Wendling | 2b6bd7b | 2011-09-28 03:14:05 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // Personality function |
Benjamin Kramer | f68b87f | 2012-09-03 12:27:43 +0000 | [diff] [blame] | 220 | IRBuilder<> Builder(EntryBB->getTerminator()); |
Bill Wendling | 2b6bd7b | 2011-09-28 03:14:05 +0000 | [diff] [blame] | 221 | if (!PersonalityFn) |
| 222 | PersonalityFn = LPads[0]->getPersonalityFn(); |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 223 | Value *PersonalityFieldPtr = |
| 224 | Builder.CreateConstGEP2_32(FuncCtx, 0, 3, "pers_fn_gep"); |
| 225 | Builder.CreateStore( |
| 226 | Builder.CreateBitCast(PersonalityFn, Builder.getInt8PtrTy()), |
| 227 | PersonalityFieldPtr, /*isVolatile=*/true); |
Bill Wendling | 2b6bd7b | 2011-09-28 03:14:05 +0000 | [diff] [blame] | 228 | |
| 229 | // LSDA address |
Benjamin Kramer | f68b87f | 2012-09-03 12:27:43 +0000 | [diff] [blame] | 230 | Value *LSDA = Builder.CreateCall(LSDAAddrFn, "lsda_addr"); |
| 231 | Value *LSDAFieldPtr = Builder.CreateConstGEP2_32(FuncCtx, 0, 4, "lsda_gep"); |
| 232 | Builder.CreateStore(LSDA, LSDAFieldPtr, /*isVolatile=*/true); |
Bill Wendling | 2b6bd7b | 2011-09-28 03:14:05 +0000 | [diff] [blame] | 233 | |
Bill Wendling | 631d117 | 2011-10-03 21:15:28 +0000 | [diff] [blame] | 234 | return FuncCtx; |
Bill Wendling | cc8cf97 | 2011-09-28 21:56:53 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Bill Wendling | d5d1700 | 2011-10-08 00:56:47 +0000 | [diff] [blame] | 237 | /// lowerIncomingArguments - To avoid having to handle incoming arguments |
| 238 | /// specially, we lower each arg to a copy instruction in the entry block. This |
| 239 | /// ensures that the argument value itself cannot be live out of the entry |
| 240 | /// block. |
Bill Wendling | eabae1d | 2012-03-13 20:04:21 +0000 | [diff] [blame] | 241 | void SjLjEHPrepare::lowerIncomingArguments(Function &F) { |
Bill Wendling | d5d1700 | 2011-10-08 00:56:47 +0000 | [diff] [blame] | 242 | BasicBlock::iterator AfterAllocaInsPt = F.begin()->begin(); |
| 243 | while (isa<AllocaInst>(AfterAllocaInsPt) && |
| 244 | isa<ConstantInt>(cast<AllocaInst>(AfterAllocaInsPt)->getArraySize())) |
| 245 | ++AfterAllocaInsPt; |
| 246 | |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 247 | for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end(); AI != AE; |
| 248 | ++AI) { |
Bill Wendling | d5d1700 | 2011-10-08 00:56:47 +0000 | [diff] [blame] | 249 | Type *Ty = AI->getType(); |
| 250 | |
| 251 | // Aggregate types can't be cast, but are legal argument types, so we have |
| 252 | // to handle them differently. We use an extract/insert pair as a |
| 253 | // lightweight method to achieve the same goal. |
| 254 | if (isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) { |
| 255 | Instruction *EI = ExtractValueInst::Create(AI, 0, "", AfterAllocaInsPt); |
| 256 | Instruction *NI = InsertValueInst::Create(AI, EI, 0); |
| 257 | NI->insertAfter(EI); |
| 258 | AI->replaceAllUsesWith(NI); |
| 259 | |
| 260 | // Set the operand of the instructions back to the AllocaInst. |
| 261 | EI->setOperand(0, AI); |
| 262 | NI->setOperand(0, AI); |
| 263 | } else { |
| 264 | // This is always a no-op cast because we're casting AI to AI->getType() |
| 265 | // so src and destination types are identical. BitCast is the only |
| 266 | // possibility. |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 267 | CastInst *NC = new BitCastInst(AI, AI->getType(), AI->getName() + ".tmp", |
| 268 | AfterAllocaInsPt); |
Bill Wendling | d5d1700 | 2011-10-08 00:56:47 +0000 | [diff] [blame] | 269 | AI->replaceAllUsesWith(NC); |
| 270 | |
| 271 | // Set the operand of the cast instruction back to the AllocaInst. |
| 272 | // Normally it's forbidden to replace a CastInst's operand because it |
| 273 | // could cause the opcode to reflect an illegal conversion. However, we're |
| 274 | // replacing it here with the same value it was constructed with. We do |
| 275 | // this because the above replaceAllUsesWith() clobbered the operand, but |
| 276 | // we want this one to remain. |
| 277 | NC->setOperand(0, AI); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /// lowerAcrossUnwindEdges - Find all variables which are alive across an unwind |
| 283 | /// edge and spill them. |
Bill Wendling | eabae1d | 2012-03-13 20:04:21 +0000 | [diff] [blame] | 284 | void SjLjEHPrepare::lowerAcrossUnwindEdges(Function &F, |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 285 | ArrayRef<InvokeInst *> Invokes) { |
Bill Wendling | d5d1700 | 2011-10-08 00:56:47 +0000 | [diff] [blame] | 286 | // Finally, scan the code looking for instructions with bad live ranges. |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 287 | for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB) { |
| 288 | for (BasicBlock::iterator II = BB->begin(), IIE = BB->end(); II != IIE; |
| 289 | ++II) { |
Bill Wendling | d5d1700 | 2011-10-08 00:56:47 +0000 | [diff] [blame] | 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; |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 294 | if (Inst->use_empty()) |
| 295 | continue; |
Bill Wendling | d5d1700 | 2011-10-08 00:56:47 +0000 | [diff] [blame] | 296 | if (Inst->hasOneUse() && |
| 297 | cast<Instruction>(Inst->use_back())->getParent() == BB && |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 298 | !isa<PHINode>(Inst->use_back())) |
| 299 | continue; |
Bill Wendling | d5d1700 | 2011-10-08 00:56:47 +0000 | [diff] [blame] | 300 | |
| 301 | // If this is an alloca in the entry block, it's not a real register |
| 302 | // value. |
| 303 | if (AllocaInst *AI = dyn_cast<AllocaInst>(Inst)) |
| 304 | if (isa<ConstantInt>(AI->getArraySize()) && BB == F.begin()) |
| 305 | continue; |
| 306 | |
| 307 | // Avoid iterator invalidation by copying users to a temporary vector. |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 308 | SmallVector<Instruction *, 16> Users; |
| 309 | for (Value::use_iterator UI = Inst->use_begin(), E = Inst->use_end(); |
| 310 | UI != E; ++UI) { |
Bill Wendling | d5d1700 | 2011-10-08 00:56:47 +0000 | [diff] [blame] | 311 | Instruction *User = cast<Instruction>(*UI); |
| 312 | if (User->getParent() != BB || isa<PHINode>(User)) |
| 313 | Users.push_back(User); |
| 314 | } |
| 315 | |
| 316 | // Find all of the blocks that this value is live in. |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 317 | SmallPtrSet<BasicBlock *, 64> LiveBBs; |
Chad Rosier | bace5da | 2012-03-16 01:04:00 +0000 | [diff] [blame] | 318 | LiveBBs.insert(Inst->getParent()); |
Bill Wendling | d5d1700 | 2011-10-08 00:56:47 +0000 | [diff] [blame] | 319 | while (!Users.empty()) { |
Chad Rosier | bace5da | 2012-03-16 01:04:00 +0000 | [diff] [blame] | 320 | Instruction *U = Users.back(); |
| 321 | Users.pop_back(); |
Bill Wendling | d5d1700 | 2011-10-08 00:56:47 +0000 | [diff] [blame] | 322 | |
Chad Rosier | bace5da | 2012-03-16 01:04:00 +0000 | [diff] [blame] | 323 | if (!isa<PHINode>(U)) { |
| 324 | MarkBlocksLiveIn(U->getParent(), LiveBBs); |
| 325 | } else { |
Bill Wendling | d5d1700 | 2011-10-08 00:56:47 +0000 | [diff] [blame] | 326 | // Uses for a PHI node occur in their predecessor block. |
Chad Rosier | bace5da | 2012-03-16 01:04:00 +0000 | [diff] [blame] | 327 | PHINode *PN = cast<PHINode>(U); |
Bill Wendling | d5d1700 | 2011-10-08 00:56:47 +0000 | [diff] [blame] | 328 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 329 | if (PN->getIncomingValue(i) == Inst) |
Chad Rosier | bace5da | 2012-03-16 01:04:00 +0000 | [diff] [blame] | 330 | MarkBlocksLiveIn(PN->getIncomingBlock(i), LiveBBs); |
Bill Wendling | d5d1700 | 2011-10-08 00:56:47 +0000 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
Chad Rosier | bace5da | 2012-03-16 01:04:00 +0000 | [diff] [blame] | 334 | // Now that we know all of the blocks that this thing is live in, see if |
| 335 | // it includes any of the unwind locations. |
| 336 | bool NeedsSpill = false; |
| 337 | for (unsigned i = 0, e = Invokes.size(); i != e; ++i) { |
| 338 | BasicBlock *UnwindBlock = Invokes[i]->getUnwindDest(); |
| 339 | if (UnwindBlock != BB && LiveBBs.count(UnwindBlock)) { |
| 340 | DEBUG(dbgs() << "SJLJ Spill: " << *Inst << " around " |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 341 | << UnwindBlock->getName() << "\n"); |
Chad Rosier | bace5da | 2012-03-16 01:04:00 +0000 | [diff] [blame] | 342 | NeedsSpill = true; |
| 343 | break; |
Bill Wendling | 30442f9 | 2012-03-14 07:28:01 +0000 | [diff] [blame] | 344 | } |
Bill Wendling | 9eb5f17 | 2012-03-12 20:19:41 +0000 | [diff] [blame] | 345 | } |
Bill Wendling | 0ad5612 | 2011-10-21 22:08:56 +0000 | [diff] [blame] | 346 | |
Chad Rosier | bace5da | 2012-03-16 01:04:00 +0000 | [diff] [blame] | 347 | // If we decided we need a spill, do it. |
| 348 | // FIXME: Spilling this way is overkill, as it forces all uses of |
| 349 | // the value to be reloaded from the stack slot, even those that aren't |
| 350 | // in the unwind blocks. We should be more selective. |
| 351 | if (NeedsSpill) { |
| 352 | DemoteRegToStack(*Inst, true); |
| 353 | ++NumSpilled; |
| 354 | } |
Bill Wendling | 30442f9 | 2012-03-14 07:28:01 +0000 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
Bill Wendling | 0ad5612 | 2011-10-21 22:08:56 +0000 | [diff] [blame] | 358 | // Go through the landing pads and remove any PHIs there. |
| 359 | for (unsigned i = 0, e = Invokes.size(); i != e; ++i) { |
| 360 | BasicBlock *UnwindBlock = Invokes[i]->getUnwindDest(); |
| 361 | LandingPadInst *LPI = UnwindBlock->getLandingPadInst(); |
| 362 | |
| 363 | // Place PHIs into a set to avoid invalidating the iterator. |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 364 | SmallPtrSet<PHINode *, 8> PHIsToDemote; |
| 365 | for (BasicBlock::iterator PN = UnwindBlock->begin(); isa<PHINode>(PN); ++PN) |
Bill Wendling | 0ad5612 | 2011-10-21 22:08:56 +0000 | [diff] [blame] | 366 | PHIsToDemote.insert(cast<PHINode>(PN)); |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 367 | if (PHIsToDemote.empty()) |
| 368 | continue; |
Bill Wendling | 0ad5612 | 2011-10-21 22:08:56 +0000 | [diff] [blame] | 369 | |
| 370 | // Demote the PHIs to the stack. |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 371 | for (SmallPtrSet<PHINode *, 8>::iterator I = PHIsToDemote.begin(), |
| 372 | E = PHIsToDemote.end(); |
| 373 | I != E; ++I) |
Bill Wendling | 0ad5612 | 2011-10-21 22:08:56 +0000 | [diff] [blame] | 374 | DemotePHIToStack(*I); |
| 375 | |
| 376 | // Move the landingpad instruction back to the top of the landing pad block. |
| 377 | LPI->moveBefore(UnwindBlock->begin()); |
| 378 | } |
Bill Wendling | d5d1700 | 2011-10-08 00:56:47 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Bill Wendling | cc8cf97 | 2011-09-28 21:56:53 +0000 | [diff] [blame] | 381 | /// setupEntryBlockAndCallSites - Setup the entry block by creating and filling |
| 382 | /// the function context and marking the call sites with the appropriate |
| 383 | /// values. These values are used by the DWARF EH emitter. |
Bill Wendling | eabae1d | 2012-03-13 20:04:21 +0000 | [diff] [blame] | 384 | bool SjLjEHPrepare::setupEntryBlockAndCallSites(Function &F) { |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 385 | SmallVector<ReturnInst *, 16> Returns; |
| 386 | SmallVector<InvokeInst *, 16> Invokes; |
| 387 | SmallSetVector<LandingPadInst *, 16> LPads; |
Bill Wendling | cc8cf97 | 2011-09-28 21:56:53 +0000 | [diff] [blame] | 388 | |
| 389 | // Look through the terminators of the basic blocks to find invokes. |
| 390 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 391 | if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) { |
Bill Wendling | e36b47e | 2013-03-08 02:21:08 +0000 | [diff] [blame] | 392 | if (Function *Callee = II->getCalledFunction()) |
| 393 | if (Callee->isIntrinsic() && |
| 394 | Callee->getIntrinsicID() == Intrinsic::donothing) { |
| 395 | // Remove the NOP invoke. |
| 396 | BranchInst::Create(II->getNormalDest(), II); |
| 397 | II->eraseFromParent(); |
| 398 | continue; |
| 399 | } |
| 400 | |
Bill Wendling | cc8cf97 | 2011-09-28 21:56:53 +0000 | [diff] [blame] | 401 | Invokes.push_back(II); |
Bob Wilson | f1b41dd | 2011-11-16 07:57:21 +0000 | [diff] [blame] | 402 | LPads.insert(II->getUnwindDest()->getLandingPadInst()); |
Bill Wendling | cc8cf97 | 2011-09-28 21:56:53 +0000 | [diff] [blame] | 403 | } else if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) { |
| 404 | Returns.push_back(RI); |
| 405 | } |
| 406 | |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 407 | if (Invokes.empty()) |
| 408 | return false; |
Bill Wendling | cc8cf97 | 2011-09-28 21:56:53 +0000 | [diff] [blame] | 409 | |
Bill Wendling | d2dae0c | 2011-10-24 17:12:36 +0000 | [diff] [blame] | 410 | NumInvokes += Invokes.size(); |
| 411 | |
Bill Wendling | d5d1700 | 2011-10-08 00:56:47 +0000 | [diff] [blame] | 412 | lowerIncomingArguments(F); |
| 413 | lowerAcrossUnwindEdges(F, Invokes); |
| 414 | |
Bob Wilson | f1b41dd | 2011-11-16 07:57:21 +0000 | [diff] [blame] | 415 | Value *FuncCtx = |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 416 | setupFunctionContext(F, makeArrayRef(LPads.begin(), LPads.end())); |
Bill Wendling | cc8cf97 | 2011-09-28 21:56:53 +0000 | [diff] [blame] | 417 | BasicBlock *EntryBB = F.begin(); |
Benjamin Kramer | f68b87f | 2012-09-03 12:27:43 +0000 | [diff] [blame] | 418 | IRBuilder<> Builder(EntryBB->getTerminator()); |
Bill Wendling | 631d117 | 2011-10-03 21:15:28 +0000 | [diff] [blame] | 419 | |
| 420 | // Get a reference to the jump buffer. |
Benjamin Kramer | f68b87f | 2012-09-03 12:27:43 +0000 | [diff] [blame] | 421 | Value *JBufPtr = Builder.CreateConstGEP2_32(FuncCtx, 0, 5, "jbuf_gep"); |
Bill Wendling | 2b6bd7b | 2011-09-28 03:14:05 +0000 | [diff] [blame] | 422 | |
Bill Wendling | 631d117 | 2011-10-03 21:15:28 +0000 | [diff] [blame] | 423 | // Save the frame pointer. |
Benjamin Kramer | f68b87f | 2012-09-03 12:27:43 +0000 | [diff] [blame] | 424 | Value *FramePtr = Builder.CreateConstGEP2_32(JBufPtr, 0, 0, "jbuf_fp_gep"); |
Bill Wendling | 631d117 | 2011-10-03 21:15:28 +0000 | [diff] [blame] | 425 | |
Benjamin Kramer | f68b87f | 2012-09-03 12:27:43 +0000 | [diff] [blame] | 426 | Value *Val = Builder.CreateCall(FrameAddrFn, Builder.getInt32(0), "fp"); |
| 427 | Builder.CreateStore(Val, FramePtr, /*isVolatile=*/true); |
Bill Wendling | 631d117 | 2011-10-03 21:15:28 +0000 | [diff] [blame] | 428 | |
| 429 | // Save the stack pointer. |
Benjamin Kramer | f68b87f | 2012-09-03 12:27:43 +0000 | [diff] [blame] | 430 | Value *StackPtr = Builder.CreateConstGEP2_32(JBufPtr, 0, 2, "jbuf_sp_gep"); |
Bill Wendling | 631d117 | 2011-10-03 21:15:28 +0000 | [diff] [blame] | 431 | |
Benjamin Kramer | f68b87f | 2012-09-03 12:27:43 +0000 | [diff] [blame] | 432 | Val = Builder.CreateCall(StackAddrFn, "sp"); |
| 433 | Builder.CreateStore(Val, StackPtr, /*isVolatile=*/true); |
Bill Wendling | 2b6bd7b | 2011-09-28 03:14:05 +0000 | [diff] [blame] | 434 | |
| 435 | // Call the setjmp instrinsic. It fills in the rest of the jmpbuf. |
Benjamin Kramer | f68b87f | 2012-09-03 12:27:43 +0000 | [diff] [blame] | 436 | Value *SetjmpArg = Builder.CreateBitCast(JBufPtr, Builder.getInt8PtrTy()); |
| 437 | Builder.CreateCall(BuiltinSetjmpFn, SetjmpArg); |
Bill Wendling | 2b6bd7b | 2011-09-28 03:14:05 +0000 | [diff] [blame] | 438 | |
Bill Wendling | cc8cf97 | 2011-09-28 21:56:53 +0000 | [diff] [blame] | 439 | // Store a pointer to the function context so that the back-end will know |
| 440 | // where to look for it. |
Benjamin Kramer | f68b87f | 2012-09-03 12:27:43 +0000 | [diff] [blame] | 441 | Value *FuncCtxArg = Builder.CreateBitCast(FuncCtx, Builder.getInt8PtrTy()); |
| 442 | Builder.CreateCall(FuncCtxFn, FuncCtxArg); |
Bill Wendling | 2b6bd7b | 2011-09-28 03:14:05 +0000 | [diff] [blame] | 443 | |
| 444 | // At this point, we are all set up, update the invoke instructions to mark |
Bill Wendling | 2130ab0 | 2011-10-05 22:04:08 +0000 | [diff] [blame] | 445 | // their call_site values. |
Bill Wendling | 2b6bd7b | 2011-09-28 03:14:05 +0000 | [diff] [blame] | 446 | for (unsigned I = 0, E = Invokes.size(); I != E; ++I) { |
Bill Wendling | 4cc4666 | 2012-01-27 02:02:24 +0000 | [diff] [blame] | 447 | insertCallSiteStore(Invokes[I], I + 1); |
Bill Wendling | 2b6bd7b | 2011-09-28 03:14:05 +0000 | [diff] [blame] | 448 | |
| 449 | ConstantInt *CallSiteNum = |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 450 | ConstantInt::get(Type::getInt32Ty(F.getContext()), I + 1); |
Bill Wendling | 2b6bd7b | 2011-09-28 03:14:05 +0000 | [diff] [blame] | 451 | |
| 452 | // Record the call site value for the back end so it stays associated with |
| 453 | // the invoke. |
| 454 | CallInst::Create(CallSiteFn, CallSiteNum, "", Invokes[I]); |
| 455 | } |
| 456 | |
| 457 | // Mark call instructions that aren't nounwind as no-action (call_site == |
| 458 | // -1). Skip the entry block, as prior to then, no function context has been |
| 459 | // created for this function and any unexpected exceptions thrown will go |
| 460 | // directly to the caller's context, which is what we want anyway, so no need |
| 461 | // to do anything here. |
Bill Wendling | da7e6a9 | 2011-10-04 00:16:40 +0000 | [diff] [blame] | 462 | for (Function::iterator BB = F.begin(), E = F.end(); ++BB != E;) |
Bill Wendling | 2b6bd7b | 2011-09-28 03:14:05 +0000 | [diff] [blame] | 463 | for (BasicBlock::iterator I = BB->begin(), end = BB->end(); I != end; ++I) |
| 464 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 465 | if (!CI->doesNotThrow()) |
Bill Wendling | 4cc4666 | 2012-01-27 02:02:24 +0000 | [diff] [blame] | 466 | insertCallSiteStore(CI, -1); |
Bill Wendling | 2b6bd7b | 2011-09-28 03:14:05 +0000 | [diff] [blame] | 467 | } else if (ResumeInst *RI = dyn_cast<ResumeInst>(I)) { |
Bill Wendling | 4cc4666 | 2012-01-27 02:02:24 +0000 | [diff] [blame] | 468 | insertCallSiteStore(RI, -1); |
Bill Wendling | 2b6bd7b | 2011-09-28 03:14:05 +0000 | [diff] [blame] | 469 | } |
Bill Wendling | 2b6bd7b | 2011-09-28 03:14:05 +0000 | [diff] [blame] | 470 | |
Bill Wendling | cc8cf97 | 2011-09-28 21:56:53 +0000 | [diff] [blame] | 471 | // Register the function context and make sure it's known to not throw |
Bill Wendling | 54e8249 | 2013-09-23 20:57:47 +0000 | [diff] [blame] | 472 | CallInst *Register = |
| 473 | CallInst::Create(RegisterFn, FuncCtx, "", EntryBB->getTerminator()); |
Bill Wendling | cc8cf97 | 2011-09-28 21:56:53 +0000 | [diff] [blame] | 474 | Register->setDoesNotThrow(); |
| 475 | |
Bob Wilson | 20c918d | 2011-11-16 07:12:00 +0000 | [diff] [blame] | 476 | // Following any allocas not in the entry block, update the saved SP in the |
| 477 | // jmpbuf to the new value. |
| 478 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { |
| 479 | if (BB == F.begin()) |
| 480 | continue; |
| 481 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
| 482 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 483 | if (CI->getCalledFunction() != StackRestoreFn) |
| 484 | continue; |
| 485 | } else if (!isa<AllocaInst>(I)) { |
| 486 | continue; |
| 487 | } |
| 488 | Instruction *StackAddr = CallInst::Create(StackAddrFn, "sp"); |
| 489 | StackAddr->insertAfter(I); |
| 490 | Instruction *StoreStackAddr = new StoreInst(StackAddr, StackPtr, true); |
| 491 | StoreStackAddr->insertAfter(StackAddr); |
| 492 | } |
| 493 | } |
| 494 | |
Bill Wendling | cc8cf97 | 2011-09-28 21:56:53 +0000 | [diff] [blame] | 495 | // Finally, for any returns from this function, if this function contains an |
| 496 | // invoke, add a call to unregister the function context. |
| 497 | for (unsigned I = 0, E = Returns.size(); I != E; ++I) |
Bill Wendling | 631d117 | 2011-10-03 21:15:28 +0000 | [diff] [blame] | 498 | CallInst::Create(UnregisterFn, FuncCtx, "", Returns[I]); |
Bill Wendling | cc8cf97 | 2011-09-28 21:56:53 +0000 | [diff] [blame] | 499 | |
Bill Wendling | 2b6bd7b | 2011-09-28 03:14:05 +0000 | [diff] [blame] | 500 | return true; |
| 501 | } |
| 502 | |
Bill Wendling | eabae1d | 2012-03-13 20:04:21 +0000 | [diff] [blame] | 503 | bool SjLjEHPrepare::runOnFunction(Function &F) { |
Bill Wendling | d2dae0c | 2011-10-24 17:12:36 +0000 | [diff] [blame] | 504 | bool Res = setupEntryBlockAndCallSites(F); |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 505 | return Res; |
| 506 | } |