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