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