1 //===- SjLjEHPass.cpp - Eliminate Invoke & Unwind instructions -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This transformation is designed for use by code generators which use SjLj
11 // based exception handling.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "sjljehprepare"
16 #include "llvm/Transforms/Scalar.h"
17 #include "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Intrinsics.h"
21 #include "llvm/LLVMContext.h"
22 #include "llvm/Module.h"
23 #include "llvm/Pass.h"
24 #include "llvm/CodeGen/Passes.h"
25 #include "llvm/Target/TargetData.h"
26 #include "llvm/Target/TargetLowering.h"
27 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
28 #include "llvm/Transforms/Utils/Local.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/IRBuilder.h"
32 #include "llvm/ADT/DenseMap.h"
33 #include "llvm/ADT/SmallVector.h"
34 #include "llvm/ADT/Statistic.h"
38 static cl::opt<bool> DisableOldSjLjEH("disable-old-sjlj-eh", cl::Hidden,
39 cl::desc("Disable the old SjLj EH preparation pass"));
41 STATISTIC(NumInvokes, "Number of invokes replaced");
42 STATISTIC(NumUnwinds, "Number of unwinds replaced");
43 STATISTIC(NumSpilled, "Number of registers live across unwind edges");
46 class SjLjEHPass : public FunctionPass {
47 const TargetLowering *TLI;
48 Type *FunctionContextTy;
50 Constant *UnregisterFn;
51 Constant *BuiltinSetjmpFn;
52 Constant *FrameAddrFn;
53 Constant *StackAddrFn;
54 Constant *StackRestoreFn;
58 Constant *ExceptionFn;
60 Constant *DispatchSetupFn;
62 DenseMap<InvokeInst*, BasicBlock*> LPadSuccMap;
64 static char ID; // Pass identification, replacement for typeid
65 explicit SjLjEHPass(const TargetLowering *tli = NULL)
66 : FunctionPass(ID), TLI(tli) { }
67 bool doInitialization(Module &M);
68 bool runOnFunction(Function &F);
70 virtual void getAnalysisUsage(AnalysisUsage &AU) const {}
71 const char *getPassName() const {
72 return "SJLJ Exception Handling preparation";
76 bool setupEntryBlockAndCallSites(Function &F);
77 void setupFunctionContext(Function &F, ArrayRef<LandingPadInst*> LPads);
79 void insertCallSiteStore(Instruction *I, int Number, Value *CallSite);
80 void markInvokeCallSite(InvokeInst *II, int InvokeNo, Value *CallSite,
81 SwitchInst *CatchSwitch);
82 void splitLiveRangesAcrossInvokes(SmallVector<InvokeInst*,16> &Invokes);
83 void splitLandingPad(InvokeInst *II);
84 bool insertSjLjEHSupport(Function &F);
86 } // end anonymous namespace
88 char SjLjEHPass::ID = 0;
90 // Public Interface To the SjLjEHPass pass.
91 FunctionPass *llvm::createSjLjEHPass(const TargetLowering *TLI) {
92 return new SjLjEHPass(TLI);
94 // doInitialization - Set up decalarations and types needed to process
96 bool SjLjEHPass::doInitialization(Module &M) {
97 // Build the function context structure.
98 // builtin_setjmp uses a five word jbuf
99 Type *VoidPtrTy = Type::getInt8PtrTy(M.getContext());
100 Type *Int32Ty = Type::getInt32Ty(M.getContext());
102 StructType::get(VoidPtrTy, // __prev
103 Int32Ty, // call_site
104 ArrayType::get(Int32Ty, 4), // __data
105 VoidPtrTy, // __personality
107 ArrayType::get(VoidPtrTy, 5), // __jbuf
109 RegisterFn = M.getOrInsertFunction("_Unwind_SjLj_Register",
110 Type::getVoidTy(M.getContext()),
111 PointerType::getUnqual(FunctionContextTy),
114 M.getOrInsertFunction("_Unwind_SjLj_Unregister",
115 Type::getVoidTy(M.getContext()),
116 PointerType::getUnqual(FunctionContextTy),
118 FrameAddrFn = Intrinsic::getDeclaration(&M, Intrinsic::frameaddress);
119 StackAddrFn = Intrinsic::getDeclaration(&M, Intrinsic::stacksave);
120 StackRestoreFn = Intrinsic::getDeclaration(&M, Intrinsic::stackrestore);
121 BuiltinSetjmpFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_sjlj_setjmp);
122 LSDAAddrFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_sjlj_lsda);
123 SelectorFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_selector);
124 ExceptionFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_exception);
125 CallSiteFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_sjlj_callsite);
127 = Intrinsic::getDeclaration(&M, Intrinsic::eh_sjlj_dispatch_setup);
133 /// insertCallSiteStore - Insert a store of the call-site value to the
135 void SjLjEHPass::insertCallSiteStore(Instruction *I, int Number,
137 ConstantInt *CallSiteNoC = ConstantInt::get(Type::getInt32Ty(I->getContext()),
139 // Insert a store of the call-site number
140 new StoreInst(CallSiteNoC, CallSite, true, I); // volatile
143 /// splitLandingPad - Split a landing pad. This takes considerable care because
144 /// of PHIs and other nasties. The problem is that the jump table needs to jump
145 /// to the landing pad block. However, the landing pad block can be jumped to
146 /// only by an invoke instruction. So we clone the landingpad instruction into
147 /// its own basic block, have the invoke jump to there. The landingpad
148 /// instruction's basic block's successor is now the target for the jump table.
150 /// But because of PHI nodes, we need to create another basic block for the jump
151 /// table to jump to. This is definitely a hack, because the values for the PHI
152 /// nodes may not be defined on the edge from the jump table. But that's okay,
153 /// because the jump table is simply a construct to mimic what is happening in
154 /// the CFG. So the values are mysteriously there, even though there is no value
155 /// for the PHI from the jump table's edge (hence calling this a hack).
156 void SjLjEHPass::splitLandingPad(InvokeInst *II) {
157 SmallVector<BasicBlock*, 2> NewBBs;
158 SplitLandingPadPredecessors(II->getUnwindDest(), II->getParent(),
159 ".1", ".2", this, NewBBs);
161 // Create an empty block so that the jump table has something to jump to
162 // which doesn't have any PHI nodes.
163 BasicBlock *LPad = NewBBs[0];
164 BasicBlock *Succ = *succ_begin(LPad);
165 BasicBlock *JumpTo = BasicBlock::Create(II->getContext(), "jt.land",
166 LPad->getParent(), Succ);
167 LPad->getTerminator()->eraseFromParent();
168 BranchInst::Create(JumpTo, LPad);
169 BranchInst::Create(Succ, JumpTo);
170 LPadSuccMap[II] = JumpTo;
172 for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
173 PHINode *PN = cast<PHINode>(I);
174 Value *Val = PN->removeIncomingValue(LPad, false);
175 PN->addIncoming(Val, JumpTo);
179 /// markInvokeCallSite - Insert code to mark the call_site for this invoke
180 void SjLjEHPass::markInvokeCallSite(InvokeInst *II, int InvokeNo,
182 SwitchInst *CatchSwitch) {
183 ConstantInt *CallSiteNoC= ConstantInt::get(Type::getInt32Ty(II->getContext()),
185 // The runtime comes back to the dispatcher with the call_site - 1 in
186 // the context. Odd, but there it is.
187 ConstantInt *SwitchValC = ConstantInt::get(Type::getInt32Ty(II->getContext()),
190 // If the unwind edge has phi nodes, split the edge.
191 if (isa<PHINode>(II->getUnwindDest()->begin())) {
192 // FIXME: New EH - This if-condition will be always true in the new scheme.
193 if (II->getUnwindDest()->isLandingPad())
196 SplitCriticalEdge(II, 1, this);
198 // If there are any phi nodes left, they must have a single predecessor.
199 while (PHINode *PN = dyn_cast<PHINode>(II->getUnwindDest()->begin())) {
200 PN->replaceAllUsesWith(PN->getIncomingValue(0));
201 PN->eraseFromParent();
205 // Insert the store of the call site value
206 insertCallSiteStore(II, InvokeNo, CallSite);
208 // Record the call site value for the back end so it stays associated with
210 CallInst::Create(CallSiteFn, CallSiteNoC, "", II);
212 // Add a switch case to our unwind block.
213 if (BasicBlock *SuccBB = LPadSuccMap[II]) {
214 CatchSwitch->addCase(SwitchValC, SuccBB);
216 CatchSwitch->addCase(SwitchValC, II->getUnwindDest());
219 // We still want this to look like an invoke so we emit the LSDA properly,
220 // so we don't transform the invoke into a call here.
223 /// MarkBlocksLiveIn - Insert BB and all of its predescessors into LiveBBs until
224 /// we reach blocks we've already seen.
225 static void MarkBlocksLiveIn(BasicBlock *BB, std::set<BasicBlock*> &LiveBBs) {
226 if (!LiveBBs.insert(BB).second) return; // already been here.
228 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
229 MarkBlocksLiveIn(*PI, LiveBBs);
232 /// splitLiveRangesAcrossInvokes - Each value that is live across an unwind edge
233 /// we spill into a stack location, guaranteeing that there is nothing live
234 /// across the unwind edge. This process also splits all critical edges
235 /// coming out of invoke's.
236 /// FIXME: Move this function to a common utility file (Local.cpp?) so
237 /// both SjLj and LowerInvoke can use it.
239 splitLiveRangesAcrossInvokes(SmallVector<InvokeInst*,16> &Invokes) {
240 // First step, split all critical edges from invoke instructions.
241 for (unsigned i = 0, e = Invokes.size(); i != e; ++i) {
242 InvokeInst *II = Invokes[i];
243 SplitCriticalEdge(II, 0, this);
245 // FIXME: New EH - This if-condition will be always true in the new scheme.
246 if (II->getUnwindDest()->isLandingPad())
249 SplitCriticalEdge(II, 1, this);
251 assert(!isa<PHINode>(II->getNormalDest()) &&
252 !isa<PHINode>(II->getUnwindDest()) &&
253 "Critical edge splitting left single entry phi nodes?");
256 Function *F = Invokes.back()->getParent()->getParent();
258 // To avoid having to handle incoming arguments specially, we lower each arg
259 // to a copy instruction in the entry block. This ensures that the argument
260 // value itself cannot be live across the entry block.
261 BasicBlock::iterator AfterAllocaInsertPt = F->begin()->begin();
262 while (isa<AllocaInst>(AfterAllocaInsertPt) &&
263 isa<ConstantInt>(cast<AllocaInst>(AfterAllocaInsertPt)->getArraySize()))
264 ++AfterAllocaInsertPt;
265 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
267 Type *Ty = AI->getType();
268 // Aggregate types can't be cast, but are legal argument types, so we have
269 // to handle them differently. We use an extract/insert pair as a
270 // lightweight method to achieve the same goal.
271 if (isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) {
272 Instruction *EI = ExtractValueInst::Create(AI, 0, "",AfterAllocaInsertPt);
273 Instruction *NI = InsertValueInst::Create(AI, EI, 0);
275 AI->replaceAllUsesWith(NI);
276 // Set the operand of the instructions back to the AllocaInst.
277 EI->setOperand(0, AI);
278 NI->setOperand(0, AI);
280 // This is always a no-op cast because we're casting AI to AI->getType()
281 // so src and destination types are identical. BitCast is the only
283 CastInst *NC = new BitCastInst(
284 AI, AI->getType(), AI->getName()+".tmp", AfterAllocaInsertPt);
285 AI->replaceAllUsesWith(NC);
286 // Set the operand of the cast instruction back to the AllocaInst.
287 // Normally it's forbidden to replace a CastInst's operand because it
288 // could cause the opcode to reflect an illegal conversion. However,
289 // we're replacing it here with the same value it was constructed with.
290 // We do this because the above replaceAllUsesWith() clobbered the
291 // operand, but we want this one to remain.
292 NC->setOperand(0, AI);
296 // Finally, scan the code looking for instructions with bad live ranges.
297 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
298 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ++II) {
299 // Ignore obvious cases we don't have to handle. In particular, most
300 // instructions either have no uses or only have a single use inside the
301 // current block. Ignore them quickly.
302 Instruction *Inst = II;
303 if (Inst->use_empty()) continue;
304 if (Inst->hasOneUse() &&
305 cast<Instruction>(Inst->use_back())->getParent() == BB &&
306 !isa<PHINode>(Inst->use_back())) continue;
308 // If this is an alloca in the entry block, it's not a real register
310 if (AllocaInst *AI = dyn_cast<AllocaInst>(Inst))
311 if (isa<ConstantInt>(AI->getArraySize()) && BB == F->begin())
314 // Avoid iterator invalidation by copying users to a temporary vector.
315 SmallVector<Instruction*,16> Users;
316 for (Value::use_iterator UI = Inst->use_begin(), E = Inst->use_end();
318 Instruction *User = cast<Instruction>(*UI);
319 if (User->getParent() != BB || isa<PHINode>(User))
320 Users.push_back(User);
323 // Find all of the blocks that this value is live in.
324 std::set<BasicBlock*> LiveBBs;
325 LiveBBs.insert(Inst->getParent());
326 while (!Users.empty()) {
327 Instruction *U = Users.back();
330 if (!isa<PHINode>(U)) {
331 MarkBlocksLiveIn(U->getParent(), LiveBBs);
333 // Uses for a PHI node occur in their predecessor block.
334 PHINode *PN = cast<PHINode>(U);
335 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
336 if (PN->getIncomingValue(i) == Inst)
337 MarkBlocksLiveIn(PN->getIncomingBlock(i), LiveBBs);
341 // Now that we know all of the blocks that this thing is live in, see if
342 // it includes any of the unwind locations.
343 bool NeedsSpill = false;
344 for (unsigned i = 0, e = Invokes.size(); i != e; ++i) {
345 BasicBlock *UnwindBlock = Invokes[i]->getUnwindDest();
346 if (UnwindBlock != BB && LiveBBs.count(UnwindBlock)) {
351 // If we decided we need a spill, do it.
352 // FIXME: Spilling this way is overkill, as it forces all uses of
353 // the value to be reloaded from the stack slot, even those that aren't
354 // in the unwind blocks. We should be more selective.
357 DemoteRegToStack(*Inst, true);
362 /// CreateLandingPadLoad - Load the exception handling values and insert them
363 /// into a structure.
364 static Instruction *CreateLandingPadLoad(Function &F, Value *ExnAddr,
366 BasicBlock::iterator InsertPt) {
367 Value *Exn = new LoadInst(ExnAddr, "exn", false,
369 Type *Ty = Type::getInt8PtrTy(F.getContext());
370 Exn = CastInst::Create(Instruction::IntToPtr, Exn, Ty, "", InsertPt);
371 Value *Sel = new LoadInst(SelAddr, "sel", false, InsertPt);
373 Ty = StructType::get(Exn->getType(), Sel->getType(), NULL);
374 InsertValueInst *LPadVal = InsertValueInst::Create(llvm::UndefValue::get(Ty),
376 "lpad.val", InsertPt);
377 return InsertValueInst::Create(LPadVal, Sel, 1, "lpad.val", InsertPt);
380 /// ReplaceLandingPadVal - Replace the landingpad instruction's value with a
381 /// load from the stored values (via CreateLandingPadLoad). This looks through
382 /// PHI nodes, and removes them if they are dead.
383 static void ReplaceLandingPadVal(Function &F, Instruction *Inst, Value *ExnAddr,
385 if (Inst->use_empty()) return;
387 while (!Inst->use_empty()) {
388 Instruction *I = cast<Instruction>(Inst->use_back());
390 if (PHINode *PN = dyn_cast<PHINode>(I)) {
391 ReplaceLandingPadVal(F, PN, ExnAddr, SelAddr);
392 if (PN->use_empty()) PN->eraseFromParent();
396 I->replaceUsesOfWith(Inst, CreateLandingPadLoad(F, ExnAddr, SelAddr, I));
400 bool SjLjEHPass::insertSjLjEHSupport(Function &F) {
401 SmallVector<ReturnInst*,16> Returns;
402 SmallVector<UnwindInst*,16> Unwinds;
403 SmallVector<InvokeInst*,16> Invokes;
405 // Look through the terminators of the basic blocks to find invokes, returns
407 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
408 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
409 // Remember all return instructions in case we insert an invoke into this
411 Returns.push_back(RI);
412 } else if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
413 Invokes.push_back(II);
414 } else if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
415 Unwinds.push_back(UI);
419 NumInvokes += Invokes.size();
420 NumUnwinds += Unwinds.size();
422 // If we don't have any invokes, there's nothing to do.
423 if (Invokes.empty()) return false;
425 // Find the eh.selector.*, eh.exception and alloca calls.
427 // Remember any allocas() that aren't in the entry block, as the
428 // jmpbuf saved SP will need to be updated for them.
430 // We'll use the first eh.selector to determine the right personality
431 // function to use. For SJLJ, we always use the same personality for the
432 // whole function, not on a per-selector basis.
433 // FIXME: That's a bit ugly. Better way?
434 SmallVector<CallInst*,16> EH_Selectors;
435 SmallVector<CallInst*,16> EH_Exceptions;
436 SmallVector<Instruction*,16> JmpbufUpdatePoints;
438 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
439 // Note: Skip the entry block since there's nothing there that interests
440 // us. eh.selector and eh.exception shouldn't ever be there, and we
441 // want to disregard any allocas that are there.
443 // FIXME: This is awkward. The new EH scheme won't need to skip the entry
445 if (BB == F.begin()) {
446 if (InvokeInst *II = dyn_cast<InvokeInst>(F.begin()->getTerminator())) {
447 // FIXME: This will be always non-NULL in the new EH.
448 if (LandingPadInst *LPI = II->getUnwindDest()->getLandingPadInst())
449 if (!PersonalityFn) PersonalityFn = LPI->getPersonalityFn();
455 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
456 if (CallInst *CI = dyn_cast<CallInst>(I)) {
457 if (CI->getCalledFunction() == SelectorFn) {
458 if (!PersonalityFn) PersonalityFn = CI->getArgOperand(1);
459 EH_Selectors.push_back(CI);
460 } else if (CI->getCalledFunction() == ExceptionFn) {
461 EH_Exceptions.push_back(CI);
462 } else if (CI->getCalledFunction() == StackRestoreFn) {
463 JmpbufUpdatePoints.push_back(CI);
465 } else if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
466 JmpbufUpdatePoints.push_back(AI);
467 } else if (InvokeInst *II = dyn_cast<InvokeInst>(I)) {
468 // FIXME: This will be always non-NULL in the new EH.
469 if (LandingPadInst *LPI = II->getUnwindDest()->getLandingPadInst())
470 if (!PersonalityFn) PersonalityFn = LPI->getPersonalityFn();
475 // If we don't have any eh.selector calls, we can't determine the personality
476 // function. Without a personality function, we can't process exceptions.
477 if (!PersonalityFn) return false;
479 // We have invokes, so we need to add register/unregister calls to get this
480 // function onto the global unwind stack.
482 // First thing we need to do is scan the whole function for values that are
483 // live across unwind edges. Each value that is live across an unwind edge we
484 // spill into a stack location, guaranteeing that there is nothing live across
485 // the unwind edge. This process also splits all critical edges coming out of
487 splitLiveRangesAcrossInvokes(Invokes);
490 SmallVector<LandingPadInst*, 16> LandingPads;
491 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
492 if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()))
493 // FIXME: This will be always non-NULL in the new EH.
494 if (LandingPadInst *LPI = II->getUnwindDest()->getLandingPadInst())
495 LandingPads.push_back(LPI);
499 BasicBlock *EntryBB = F.begin();
500 // Create an alloca for the incoming jump buffer ptr and the new jump buffer
501 // that needs to be restored on all exits from the function. This is an
502 // alloca because the value needs to be added to the global context list.
503 unsigned Align = 4; // FIXME: Should be a TLI check?
504 AllocaInst *FunctionContext =
505 new AllocaInst(FunctionContextTy, 0, Align,
506 "fcn_context", F.begin()->begin());
509 Type *Int32Ty = Type::getInt32Ty(F.getContext());
510 Value *Zero = ConstantInt::get(Int32Ty, 0);
511 // We need to also keep around a reference to the call_site field
513 Idxs[1] = ConstantInt::get(Int32Ty, 1);
514 CallSite = GetElementPtrInst::Create(FunctionContext, Idxs, "call_site",
515 EntryBB->getTerminator());
517 // The exception selector comes back in context->data[1]
518 Idxs[1] = ConstantInt::get(Int32Ty, 2);
519 Value *FCData = GetElementPtrInst::Create(FunctionContext, Idxs, "fc_data",
520 EntryBB->getTerminator());
521 Idxs[1] = ConstantInt::get(Int32Ty, 1);
522 Value *SelectorAddr = GetElementPtrInst::Create(FCData, Idxs,
524 EntryBB->getTerminator());
525 // The exception value comes back in context->data[0]
527 Value *ExceptionAddr = GetElementPtrInst::Create(FCData, Idxs,
529 EntryBB->getTerminator());
531 // The result of the eh.selector call will be replaced with a a reference to
532 // the selector value returned in the function context. We leave the selector
533 // itself so the EH analysis later can use it.
534 for (int i = 0, e = EH_Selectors.size(); i < e; ++i) {
535 CallInst *I = EH_Selectors[i];
536 Value *SelectorVal = new LoadInst(SelectorAddr, "select_val", true, I);
537 I->replaceAllUsesWith(SelectorVal);
540 // eh.exception calls are replaced with references to the proper location in
541 // the context. Unlike eh.selector, the eh.exception calls are removed
543 for (int i = 0, e = EH_Exceptions.size(); i < e; ++i) {
544 CallInst *I = EH_Exceptions[i];
545 // Possible for there to be duplicates, so check to make sure the
546 // instruction hasn't already been removed.
547 if (!I->getParent()) continue;
548 Value *Val = new LoadInst(ExceptionAddr, "exception", true, I);
549 Type *Ty = Type::getInt8PtrTy(F.getContext());
550 Val = CastInst::Create(Instruction::IntToPtr, Val, Ty, "", I);
552 I->replaceAllUsesWith(Val);
553 I->eraseFromParent();
556 for (unsigned i = 0, e = LandingPads.size(); i != e; ++i)
557 ReplaceLandingPadVal(F, LandingPads[i], ExceptionAddr, SelectorAddr);
559 // The entry block changes to have the eh.sjlj.setjmp, with a conditional
560 // branch to a dispatch block for non-zero returns. If we return normally,
561 // we're not handling an exception and just register the function context and
564 // Create the dispatch block. The dispatch block is basically a big switch
565 // statement that goes to all of the invoke landing pads.
566 BasicBlock *DispatchBlock =
567 BasicBlock::Create(F.getContext(), "eh.sjlj.setjmp.catch", &F);
569 // Insert a load of the callsite in the dispatch block, and a switch on its
570 // value. By default, we issue a trap statement.
571 BasicBlock *TrapBlock =
572 BasicBlock::Create(F.getContext(), "trapbb", &F);
573 CallInst::Create(Intrinsic::getDeclaration(F.getParent(), Intrinsic::trap),
575 new UnreachableInst(F.getContext(), TrapBlock);
577 Value *DispatchLoad = new LoadInst(CallSite, "invoke.num", true,
579 SwitchInst *DispatchSwitch =
580 SwitchInst::Create(DispatchLoad, TrapBlock, Invokes.size(),
582 // Split the entry block to insert the conditional branch for the setjmp.
583 BasicBlock *ContBlock = EntryBB->splitBasicBlock(EntryBB->getTerminator(),
584 "eh.sjlj.setjmp.cont");
586 // Populate the Function Context
588 // 2. Personality function address
589 // 3. jmpbuf (save SP, FP and call eh.sjlj.setjmp)
593 Idxs[1] = ConstantInt::get(Int32Ty, 4);
594 Value *LSDAFieldPtr =
595 GetElementPtrInst::Create(FunctionContext, Idxs, "lsda_gep",
596 EntryBB->getTerminator());
597 Value *LSDA = CallInst::Create(LSDAAddrFn, "lsda_addr",
598 EntryBB->getTerminator());
599 new StoreInst(LSDA, LSDAFieldPtr, true, EntryBB->getTerminator());
601 Idxs[1] = ConstantInt::get(Int32Ty, 3);
602 Value *PersonalityFieldPtr =
603 GetElementPtrInst::Create(FunctionContext, Idxs, "lsda_gep",
604 EntryBB->getTerminator());
605 new StoreInst(PersonalityFn, PersonalityFieldPtr, true,
606 EntryBB->getTerminator());
608 // Save the frame pointer.
609 Idxs[1] = ConstantInt::get(Int32Ty, 5);
611 = GetElementPtrInst::Create(FunctionContext, Idxs, "jbuf_gep",
612 EntryBB->getTerminator());
613 Idxs[1] = ConstantInt::get(Int32Ty, 0);
615 GetElementPtrInst::Create(JBufPtr, Idxs, "jbuf_fp_gep",
616 EntryBB->getTerminator());
618 Value *Val = CallInst::Create(FrameAddrFn,
619 ConstantInt::get(Int32Ty, 0),
621 EntryBB->getTerminator());
622 new StoreInst(Val, FramePtr, true, EntryBB->getTerminator());
624 // Save the stack pointer.
625 Idxs[1] = ConstantInt::get(Int32Ty, 2);
627 GetElementPtrInst::Create(JBufPtr, Idxs, "jbuf_sp_gep",
628 EntryBB->getTerminator());
630 Val = CallInst::Create(StackAddrFn, "sp", EntryBB->getTerminator());
631 new StoreInst(Val, StackPtr, true, EntryBB->getTerminator());
633 // Call the setjmp instrinsic. It fills in the rest of the jmpbuf.
635 CastInst::Create(Instruction::BitCast, JBufPtr,
636 Type::getInt8PtrTy(F.getContext()), "",
637 EntryBB->getTerminator());
638 Value *DispatchVal = CallInst::Create(BuiltinSetjmpFn, SetjmpArg,
640 EntryBB->getTerminator());
642 // Add a call to dispatch_setup after the setjmp call. This is expanded to any
643 // target-specific setup that needs to be done.
644 CallInst::Create(DispatchSetupFn, DispatchVal, "", EntryBB->getTerminator());
646 // check the return value of the setjmp. non-zero goes to dispatcher.
647 Value *IsNormal = new ICmpInst(EntryBB->getTerminator(),
648 ICmpInst::ICMP_EQ, DispatchVal, Zero,
650 // Nuke the uncond branch.
651 EntryBB->getTerminator()->eraseFromParent();
653 // Put in a new condbranch in its place.
654 BranchInst::Create(ContBlock, DispatchBlock, IsNormal, EntryBB);
656 // Register the function context and make sure it's known to not throw
658 CallInst::Create(RegisterFn, FunctionContext, "",
659 ContBlock->getTerminator());
660 Register->setDoesNotThrow();
662 // At this point, we are all set up, update the invoke instructions to mark
663 // their call_site values, and fill in the dispatch switch accordingly.
664 for (unsigned i = 0, e = Invokes.size(); i != e; ++i)
665 markInvokeCallSite(Invokes[i], i+1, CallSite, DispatchSwitch);
667 // Mark call instructions that aren't nounwind as no-action (call_site ==
668 // -1). Skip the entry block, as prior to then, no function context has been
669 // created for this function and any unexpected exceptions thrown will go
670 // directly to the caller's context, which is what we want anyway, so no need
671 // to do anything here.
672 for (Function::iterator BB = F.begin(), E = F.end(); ++BB != E;) {
673 for (BasicBlock::iterator I = BB->begin(), end = BB->end(); I != end; ++I)
674 if (CallInst *CI = dyn_cast<CallInst>(I)) {
675 // Ignore calls to the EH builtins (eh.selector, eh.exception)
676 Constant *Callee = CI->getCalledFunction();
677 if (Callee != SelectorFn && Callee != ExceptionFn
678 && !CI->doesNotThrow())
679 insertCallSiteStore(CI, -1, CallSite);
680 } else if (ResumeInst *RI = dyn_cast<ResumeInst>(I)) {
681 insertCallSiteStore(RI, -1, CallSite);
685 // Replace all unwinds with a branch to the unwind handler.
686 // ??? Should this ever happen with sjlj exceptions?
687 for (unsigned i = 0, e = Unwinds.size(); i != e; ++i) {
688 BranchInst::Create(TrapBlock, Unwinds[i]);
689 Unwinds[i]->eraseFromParent();
692 // Following any allocas not in the entry block, update the saved SP in the
693 // jmpbuf to the new value.
694 for (unsigned i = 0, e = JmpbufUpdatePoints.size(); i != e; ++i) {
695 Instruction *AI = JmpbufUpdatePoints[i];
696 Instruction *StackAddr = CallInst::Create(StackAddrFn, "sp");
697 StackAddr->insertAfter(AI);
698 Instruction *StoreStackAddr = new StoreInst(StackAddr, StackPtr, true);
699 StoreStackAddr->insertAfter(StackAddr);
702 // Finally, for any returns from this function, if this function contains an
703 // invoke, add a call to unregister the function context.
704 for (unsigned i = 0, e = Returns.size(); i != e; ++i)
705 CallInst::Create(UnregisterFn, FunctionContext, "", Returns[i]);
710 /// setupFunctionContext - Allocate the function context on the stack and fill
711 /// it with all of the data that we know at this point.
712 void SjLjEHPass::setupFunctionContext(Function &F,
713 ArrayRef<LandingPadInst*> LPads) {
714 BasicBlock *EntryBB = F.begin();
716 // Create an alloca for the incoming jump buffer ptr and the new jump buffer
717 // that needs to be restored on all exits from the function. This is an alloca
718 // because the value needs to be added to the global context list.
720 TLI->getTargetData()->getPrefTypeAlignment(FunctionContextTy);
721 AllocaInst *FuncCtx =
722 new AllocaInst(FunctionContextTy, 0, Align, "fn_context", EntryBB->begin());
724 // Store a pointer to the function context so that the back-end will know
725 // where to look for it.
726 CallInst::Create(Intrinsic::getDeclaration(F.getParent(),
727 Intrinsic::eh_sjlj_functioncontext),
728 FuncCtx, "", EntryBB->getTerminator());
730 // Fill in the function context structure.
732 Type *Int32Ty = Type::getInt32Ty(F.getContext());
733 Value *Zero = ConstantInt::get(Int32Ty, 0);
734 Value *One = ConstantInt::get(Int32Ty, 1);
736 // Keep around a reference to the call_site field.
739 CallSite = GetElementPtrInst::Create(FuncCtx, Idxs, "call_site",
740 EntryBB->getTerminator());
742 // Reference the __data field.
743 Idxs[1] = ConstantInt::get(Int32Ty, 2);
744 Value *FCData = GetElementPtrInst::Create(FuncCtx, Idxs, "__data",
745 EntryBB->getTerminator());
747 // The exception value comes back in context->__data[0].
749 Value *ExceptionAddr = GetElementPtrInst::Create(FCData, Idxs,
751 EntryBB->getTerminator());
753 // The exception selector comes back in context->__data[1].
755 Value *SelectorAddr = GetElementPtrInst::Create(FCData, Idxs,
757 EntryBB->getTerminator());
759 for (unsigned I = 0, E = LPads.size(); I != E; ++I) {
760 LandingPadInst *LPI = LPads[I];
761 IRBuilder<> Builder(LPI->getParent()->getFirstInsertionPt());
763 Value *ExnVal = Builder.CreateLoad(ExceptionAddr, true, "exn_val");
764 ExnVal = Builder.CreateIntToPtr(ExnVal, Type::getInt8PtrTy(F.getContext()));
765 Value *SelVal = Builder.CreateLoad(SelectorAddr, true, "exn_selector_val");
767 Type *LPadType = LPI->getType();
768 Value *LPadVal = UndefValue::get(LPadType);
769 LPadVal = Builder.CreateInsertValue(LPadVal, ExnVal, 0, "lpad.val");
770 LPadVal = Builder.CreateInsertValue(LPadVal, SelVal, 1, "lpad.val");
772 LPI->replaceAllUsesWith(LPadVal);
775 // Personality function
776 Idxs[1] = ConstantInt::get(Int32Ty, 3);
778 PersonalityFn = LPads[0]->getPersonalityFn();
779 Value *PersonalityFieldPtr =
780 GetElementPtrInst::Create(FuncCtx, Idxs, "pers_fn_gep",
781 EntryBB->getTerminator());
782 new StoreInst(PersonalityFn, PersonalityFieldPtr, true,
783 EntryBB->getTerminator());
786 Idxs[1] = ConstantInt::get(Int32Ty, 4);
787 Value *LSDAFieldPtr =
788 GetElementPtrInst::Create(FuncCtx, Idxs, "lsda_gep",
789 EntryBB->getTerminator());
790 Value *LSDA = CallInst::Create(LSDAAddrFn, "lsda_addr",
791 EntryBB->getTerminator());
792 new StoreInst(LSDA, LSDAFieldPtr, true, EntryBB->getTerminator());
794 // Get a reference to the jump buffer.
795 Idxs[1] = ConstantInt::get(Int32Ty, 5);
797 GetElementPtrInst::Create(FuncCtx, Idxs, "jbuf_gep",
798 EntryBB->getTerminator());
801 GetElementPtrInst::Create(JBufPtr, Idxs, "jbuf_fp_gep",
802 EntryBB->getTerminator());
804 // Save the frame pointer.
805 Value *Val = CallInst::Create(FrameAddrFn,
806 ConstantInt::get(Int32Ty, 0),
808 EntryBB->getTerminator());
809 new StoreInst(Val, FramePtr, true, EntryBB->getTerminator());
811 // Save the stack pointer.
812 Idxs[1] = ConstantInt::get(Int32Ty, 2);
814 GetElementPtrInst::Create(JBufPtr, Idxs, "jbuf_sp_gep",
815 EntryBB->getTerminator());
817 Val = CallInst::Create(StackAddrFn, "sp", EntryBB->getTerminator());
818 new StoreInst(Val, StackPtr, true, EntryBB->getTerminator());
820 // Call the setjmp instrinsic. It fills in the rest of the jmpbuf.
822 CastInst::Create(Instruction::BitCast, JBufPtr,
823 Type::getInt8PtrTy(F.getContext()), "",
824 EntryBB->getTerminator());
825 Value *DispatchVal = CallInst::Create(BuiltinSetjmpFn, SetjmpArg,
827 EntryBB->getTerminator());
829 // Add a call to dispatch_setup after the setjmp call. This is expanded to any
830 // target-specific setup that needs to be done.
831 CallInst::Create(DispatchSetupFn, DispatchVal, "", EntryBB->getTerminator());
834 /// setupEntryBlockAndCallSites - Setup the entry block by creating and filling
835 /// the function context and marking the call sites with the appropriate
836 /// values. These values are used by the DWARF EH emitter.
837 bool SjLjEHPass::setupEntryBlockAndCallSites(Function &F) {
838 SmallVector<InvokeInst*, 16> Invokes;
839 SmallVector<LandingPadInst*, 16> LPads;
841 // Look through the terminators of the basic blocks to find invokes.
842 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
843 if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
844 Invokes.push_back(II);
845 LPads.push_back(II->getUnwindDest()->getLandingPadInst());
848 if (Invokes.empty()) return false;
850 setupFunctionContext(F, LPads);
852 // At this point, we are all set up, update the invoke instructions to mark
853 // their call_site values, and fill in the dispatch switch accordingly.
854 for (unsigned I = 0, E = Invokes.size(); I != E; ++I) {
855 insertCallSiteStore(Invokes[I], I + 1, CallSite);
857 ConstantInt *CallSiteNum =
858 ConstantInt::get(Type::getInt32Ty(F.getContext()), I + 1);
860 // Record the call site value for the back end so it stays associated with
862 CallInst::Create(CallSiteFn, CallSiteNum, "", Invokes[I]);
865 // Mark call instructions that aren't nounwind as no-action (call_site ==
866 // -1). Skip the entry block, as prior to then, no function context has been
867 // created for this function and any unexpected exceptions thrown will go
868 // directly to the caller's context, which is what we want anyway, so no need
869 // to do anything here.
870 for (Function::iterator BB = F.begin(), E = F.end(); ++BB != E;) {
871 for (BasicBlock::iterator I = BB->begin(), end = BB->end(); I != end; ++I)
872 if (CallInst *CI = dyn_cast<CallInst>(I)) {
873 if (!CI->doesNotThrow())
874 insertCallSiteStore(CI, -1, CallSite);
875 } else if (ResumeInst *RI = dyn_cast<ResumeInst>(I)) {
876 insertCallSiteStore(RI, -1, CallSite);
883 bool SjLjEHPass::runOnFunction(Function &F) {
885 if (!DisableOldSjLjEH)
886 Res = insertSjLjEHSupport(F);
888 Res = setupEntryBlockAndCallSites(F);