SjLjEHPrepare.cpp 19 KB

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