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