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