SjLjEHPrepare.cpp revision 2b6bd7ba5819d8c73c89291c6de89a86dbbffe9c
1//===- SjLjEHPass.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
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"
35#include <set>
36using namespace llvm;
37
38static cl::opt<bool> DisableOldSjLjEH("disable-old-sjlj-eh", cl::Hidden,
39    cl::desc("Disable the old SjLj EH preparation pass"));
40
41STATISTIC(NumInvokes, "Number of invokes replaced");
42STATISTIC(NumUnwinds, "Number of unwinds replaced");
43STATISTIC(NumSpilled, "Number of registers live across unwind edges");
44
45namespace {
46  class SjLjEHPass : public FunctionPass {
47    const TargetLowering *TLI;
48    Type *FunctionContextTy;
49    Constant *RegisterFn;
50    Constant *UnregisterFn;
51    Constant *BuiltinSetjmpFn;
52    Constant *FrameAddrFn;
53    Constant *StackAddrFn;
54    Constant *StackRestoreFn;
55    Constant *LSDAAddrFn;
56    Value *PersonalityFn;
57    Constant *SelectorFn;
58    Constant *ExceptionFn;
59    Constant *CallSiteFn;
60    Constant *DispatchSetupFn;
61    Value *CallSite;
62    DenseMap<InvokeInst*, BasicBlock*> LPadSuccMap;
63  public:
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);
69
70    virtual void getAnalysisUsage(AnalysisUsage &AU) const {}
71    const char *getPassName() const {
72      return "SJLJ Exception Handling preparation";
73    }
74
75  private:
76    bool setupEntryBlockAndCallSites(Function &F);
77    void setupFunctionContext(Function &F, ArrayRef<LandingPadInst*> LPads);
78
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);
85  };
86} // end anonymous namespace
87
88char SjLjEHPass::ID = 0;
89
90// Public Interface To the SjLjEHPass pass.
91FunctionPass *llvm::createSjLjEHPass(const TargetLowering *TLI) {
92  return new SjLjEHPass(TLI);
93}
94// doInitialization - Set up decalarations and types needed to process
95// exceptions.
96bool 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());
101  FunctionContextTy =
102    StructType::get(VoidPtrTy,                        // __prev
103                    Int32Ty,                          // call_site
104                    ArrayType::get(Int32Ty, 4),       // __data
105                    VoidPtrTy,                        // __personality
106                    VoidPtrTy,                        // __lsda
107                    ArrayType::get(VoidPtrTy, 5),     // __jbuf
108                    NULL);
109  RegisterFn = M.getOrInsertFunction("_Unwind_SjLj_Register",
110                                     Type::getVoidTy(M.getContext()),
111                                     PointerType::getUnqual(FunctionContextTy),
112                                     (Type *)0);
113  UnregisterFn =
114    M.getOrInsertFunction("_Unwind_SjLj_Unregister",
115                          Type::getVoidTy(M.getContext()),
116                          PointerType::getUnqual(FunctionContextTy),
117                          (Type *)0);
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);
126  DispatchSetupFn
127    = Intrinsic::getDeclaration(&M, Intrinsic::eh_sjlj_dispatch_setup);
128  PersonalityFn = 0;
129
130  return true;
131}
132
133/// insertCallSiteStore - Insert a store of the call-site value to the
134/// function context
135void SjLjEHPass::insertCallSiteStore(Instruction *I, int Number,
136                                     Value *CallSite) {
137  ConstantInt *CallSiteNoC = ConstantInt::get(Type::getInt32Ty(I->getContext()),
138                                              Number);
139  // Insert a store of the call-site number
140  new StoreInst(CallSiteNoC, CallSite, true, I);  // volatile
141}
142
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.
149///
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).
156void SjLjEHPass::splitLandingPad(InvokeInst *II) {
157  SmallVector<BasicBlock*, 2> NewBBs;
158  SplitLandingPadPredecessors(II->getUnwindDest(), II->getParent(),
159                              ".1", ".2", this, NewBBs);
160
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;
171
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);
176  }
177}
178
179/// markInvokeCallSite - Insert code to mark the call_site for this invoke
180void SjLjEHPass::markInvokeCallSite(InvokeInst *II, int InvokeNo,
181                                    Value *CallSite,
182                                    SwitchInst *CatchSwitch) {
183  ConstantInt *CallSiteNoC= ConstantInt::get(Type::getInt32Ty(II->getContext()),
184                                              InvokeNo);
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()),
188                                             InvokeNo - 1);
189
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())
194      splitLandingPad(II);
195    else
196      SplitCriticalEdge(II, 1, this);
197
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();
202    }
203  }
204
205  // Insert the store of the call site value
206  insertCallSiteStore(II, InvokeNo, CallSite);
207
208  // Record the call site value for the back end so it stays associated with
209  // the invoke.
210  CallInst::Create(CallSiteFn, CallSiteNoC, "", II);
211
212  // Add a switch case to our unwind block.
213  if (BasicBlock *SuccBB = LPadSuccMap[II]) {
214    CatchSwitch->addCase(SwitchValC, SuccBB);
215  } else {
216    CatchSwitch->addCase(SwitchValC, II->getUnwindDest());
217  }
218
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.
221}
222
223/// MarkBlocksLiveIn - Insert BB and all of its predescessors into LiveBBs until
224/// we reach blocks we've already seen.
225static void MarkBlocksLiveIn(BasicBlock *BB, std::set<BasicBlock*> &LiveBBs) {
226  if (!LiveBBs.insert(BB).second) return; // already been here.
227
228  for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
229    MarkBlocksLiveIn(*PI, LiveBBs);
230}
231
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.
238void SjLjEHPass::
239splitLiveRangesAcrossInvokes(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);
244
245    // FIXME: New EH - This if-condition will be always true in the new scheme.
246    if (II->getUnwindDest()->isLandingPad())
247      splitLandingPad(II);
248    else
249      SplitCriticalEdge(II, 1, this);
250
251    assert(!isa<PHINode>(II->getNormalDest()) &&
252           !isa<PHINode>(II->getUnwindDest()) &&
253           "Critical edge splitting left single entry phi nodes?");
254  }
255
256  Function *F = Invokes.back()->getParent()->getParent();
257
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();
266       AI != E; ++AI) {
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);
274      NI->insertAfter(EI);
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);
279    } else {
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
282      // possibility.
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);
293    }
294  }
295
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;
307
308      // If this is an alloca in the entry block, it's not a real register
309      // value.
310      if (AllocaInst *AI = dyn_cast<AllocaInst>(Inst))
311        if (isa<ConstantInt>(AI->getArraySize()) && BB == F->begin())
312          continue;
313
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();
317           UI != E; ++UI) {
318        Instruction *User = cast<Instruction>(*UI);
319        if (User->getParent() != BB || isa<PHINode>(User))
320          Users.push_back(User);
321      }
322
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();
328        Users.pop_back();
329
330        if (!isa<PHINode>(U)) {
331          MarkBlocksLiveIn(U->getParent(), LiveBBs);
332        } else {
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);
338        }
339      }
340
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)) {
347          NeedsSpill = true;
348        }
349      }
350
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.
355      if (NeedsSpill) {
356        ++NumSpilled;
357        DemoteRegToStack(*Inst, true);
358      }
359    }
360}
361
362/// CreateLandingPadLoad - Load the exception handling values and insert them
363/// into a structure.
364static Instruction *CreateLandingPadLoad(Function &F, Value *ExnAddr,
365                                         Value *SelAddr,
366                                         BasicBlock::iterator InsertPt) {
367  Value *Exn = new LoadInst(ExnAddr, "exn", false,
368                            InsertPt);
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);
372
373  Ty = StructType::get(Exn->getType(), Sel->getType(), NULL);
374  InsertValueInst *LPadVal = InsertValueInst::Create(llvm::UndefValue::get(Ty),
375                                                     Exn, 0,
376                                                     "lpad.val", InsertPt);
377  return InsertValueInst::Create(LPadVal, Sel, 1, "lpad.val", InsertPt);
378}
379
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.
383static void ReplaceLandingPadVal(Function &F, Instruction *Inst, Value *ExnAddr,
384                                 Value *SelAddr) {
385  if (Inst->use_empty()) return;
386
387  while (!Inst->use_empty()) {
388    Instruction *I = cast<Instruction>(Inst->use_back());
389
390    if (PHINode *PN = dyn_cast<PHINode>(I)) {
391      ReplaceLandingPadVal(F, PN, ExnAddr, SelAddr);
392      if (PN->use_empty()) PN->eraseFromParent();
393      continue;
394    }
395
396    I->replaceUsesOfWith(Inst, CreateLandingPadLoad(F, ExnAddr, SelAddr, I));
397  }
398}
399
400bool SjLjEHPass::insertSjLjEHSupport(Function &F) {
401  SmallVector<ReturnInst*,16> Returns;
402  SmallVector<UnwindInst*,16> Unwinds;
403  SmallVector<InvokeInst*,16> Invokes;
404
405  // Look through the terminators of the basic blocks to find invokes, returns
406  // and unwinds.
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
410      // function.
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);
416    }
417  }
418
419  NumInvokes += Invokes.size();
420  NumUnwinds += Unwinds.size();
421
422  // If we don't have any invokes, there's nothing to do.
423  if (Invokes.empty()) return false;
424
425  // Find the eh.selector.*, eh.exception and alloca calls.
426  //
427  // Remember any allocas() that aren't in the entry block, as the
428  // jmpbuf saved SP will need to be updated for them.
429  //
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;
437
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.
442    //
443    // FIXME: This is awkward. The new EH scheme won't need to skip the entry
444    //        block.
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();
450      }
451
452      continue;
453    }
454
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);
464        }
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();
471      }
472    }
473  }
474
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;
478
479  // We have invokes, so we need to add register/unregister calls to get this
480  // function onto the global unwind stack.
481  //
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
486  // invoke's.
487  splitLiveRangesAcrossInvokes(Invokes);
488
489
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);
496  }
497
498
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());
507
508  Value *Idxs[2];
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
512  Idxs[0] = Zero;
513  Idxs[1] = ConstantInt::get(Int32Ty, 1);
514  CallSite = GetElementPtrInst::Create(FunctionContext, Idxs, "call_site",
515                                       EntryBB->getTerminator());
516
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,
523                                                  "exc_selector_gep",
524                                                  EntryBB->getTerminator());
525  // The exception value comes back in context->data[0]
526  Idxs[1] = Zero;
527  Value *ExceptionAddr = GetElementPtrInst::Create(FCData, Idxs,
528                                                   "exception_gep",
529                                                   EntryBB->getTerminator());
530
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);
538  }
539
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
542  // entirely.
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);
551
552    I->replaceAllUsesWith(Val);
553    I->eraseFromParent();
554  }
555
556  for (unsigned i = 0, e = LandingPads.size(); i != e; ++i)
557    ReplaceLandingPadVal(F, LandingPads[i], ExceptionAddr, SelectorAddr);
558
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
562  // continue.
563
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);
568
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),
574                   "", TrapBlock);
575  new UnreachableInst(F.getContext(), TrapBlock);
576
577  Value *DispatchLoad = new LoadInst(CallSite, "invoke.num", true,
578                                     DispatchBlock);
579  SwitchInst *DispatchSwitch =
580    SwitchInst::Create(DispatchLoad, TrapBlock, Invokes.size(),
581                       DispatchBlock);
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");
585
586  // Populate the Function Context
587  //   1. LSDA address
588  //   2. Personality function address
589  //   3. jmpbuf (save SP, FP and call eh.sjlj.setjmp)
590
591  // LSDA address
592  Idxs[0] = Zero;
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());
600
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());
607
608  // Save the frame pointer.
609  Idxs[1] = ConstantInt::get(Int32Ty, 5);
610  Value *JBufPtr
611    = GetElementPtrInst::Create(FunctionContext, Idxs, "jbuf_gep",
612                                EntryBB->getTerminator());
613  Idxs[1] = ConstantInt::get(Int32Ty, 0);
614  Value *FramePtr =
615    GetElementPtrInst::Create(JBufPtr, Idxs, "jbuf_fp_gep",
616                              EntryBB->getTerminator());
617
618  Value *Val = CallInst::Create(FrameAddrFn,
619                                ConstantInt::get(Int32Ty, 0),
620                                "fp",
621                                EntryBB->getTerminator());
622  new StoreInst(Val, FramePtr, true, EntryBB->getTerminator());
623
624  // Save the stack pointer.
625  Idxs[1] = ConstantInt::get(Int32Ty, 2);
626  Value *StackPtr =
627    GetElementPtrInst::Create(JBufPtr, Idxs, "jbuf_sp_gep",
628                              EntryBB->getTerminator());
629
630  Val = CallInst::Create(StackAddrFn, "sp", EntryBB->getTerminator());
631  new StoreInst(Val, StackPtr, true, EntryBB->getTerminator());
632
633  // Call the setjmp instrinsic. It fills in the rest of the jmpbuf.
634  Value *SetjmpArg =
635    CastInst::Create(Instruction::BitCast, JBufPtr,
636                     Type::getInt8PtrTy(F.getContext()), "",
637                     EntryBB->getTerminator());
638  Value *DispatchVal = CallInst::Create(BuiltinSetjmpFn, SetjmpArg,
639                                        "dispatch",
640                                        EntryBB->getTerminator());
641
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());
645
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,
649                                 "notunwind");
650  // Nuke the uncond branch.
651  EntryBB->getTerminator()->eraseFromParent();
652
653  // Put in a new condbranch in its place.
654  BranchInst::Create(ContBlock, DispatchBlock, IsNormal, EntryBB);
655
656  // Register the function context and make sure it's known to not throw
657  CallInst *Register =
658    CallInst::Create(RegisterFn, FunctionContext, "",
659                     ContBlock->getTerminator());
660  Register->setDoesNotThrow();
661
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);
666
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);
682      }
683  }
684
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();
690  }
691
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);
700  }
701
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]);
706
707  return true;
708}
709
710/// setupFunctionContext - Allocate the function context on the stack and fill
711/// it with all of the data that we know at this point.
712void SjLjEHPass::setupFunctionContext(Function &F,
713                                      ArrayRef<LandingPadInst*> LPads) {
714  BasicBlock *EntryBB = F.begin();
715
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.
719  unsigned Align =
720    TLI->getTargetData()->getPrefTypeAlignment(FunctionContextTy);
721  AllocaInst *FuncCtx =
722    new AllocaInst(FunctionContextTy, 0, Align, "fn_context", EntryBB->begin());
723
724  // Fill in the function context structure.
725  Value *Idxs[2];
726  Type *Int32Ty = Type::getInt32Ty(F.getContext());
727  Value *Zero = ConstantInt::get(Int32Ty, 0);
728  Value *One = ConstantInt::get(Int32Ty, 1);
729
730  // Keep around a reference to the call_site field.
731  Idxs[0] = Zero;
732  Idxs[1] = One;
733  CallSite = GetElementPtrInst::Create(FuncCtx, Idxs, "call_site",
734                                       EntryBB->getTerminator());
735
736  // Reference the __data field.
737  Idxs[1] = ConstantInt::get(Int32Ty, 2);
738  Value *FCData = GetElementPtrInst::Create(FuncCtx, Idxs, "__data",
739                                            EntryBB->getTerminator());
740
741  // The exception value comes back in context->__data[0].
742  Idxs[1] = Zero;
743  Value *ExceptionAddr = GetElementPtrInst::Create(FCData, Idxs,
744                                                   "exception_gep",
745                                                   EntryBB->getTerminator());
746
747  // The exception selector comes back in context->__data[1].
748  Idxs[1] = One;
749  Value *SelectorAddr = GetElementPtrInst::Create(FCData, Idxs,
750                                                  "exn_selector_gep",
751                                                  EntryBB->getTerminator());
752
753  for (unsigned I = 0, E = LPads.size(); I != E; ++I) {
754    LandingPadInst *LPI = LPads[I];
755    IRBuilder<> Builder(LPI->getParent()->getFirstInsertionPt());
756
757    Value *ExnVal = Builder.CreateLoad(ExceptionAddr, true, "exn_val");
758    ExnVal = Builder.CreateIntToPtr(ExnVal, Type::getInt8PtrTy(F.getContext()));
759    Value *SelVal = Builder.CreateLoad(SelectorAddr, true, "exn_selector_val");
760
761    Type *LPadType = LPI->getType();
762    Value *LPadVal = UndefValue::get(LPadType);
763    LPadVal = Builder.CreateInsertValue(LPadVal, ExnVal, 0, "lpad.val");
764    LPadVal = Builder.CreateInsertValue(LPadVal, SelVal, 1, "lpad.val");
765
766    LPI->replaceAllUsesWith(LPadVal);
767  }
768
769  // Personality function
770  Idxs[1] = ConstantInt::get(Int32Ty, 3);
771  if (!PersonalityFn)
772    PersonalityFn = LPads[0]->getPersonalityFn();
773  Value *PersonalityFieldPtr =
774    GetElementPtrInst::Create(FuncCtx, Idxs, "pers_fn_gep",
775                              EntryBB->getTerminator());
776  new StoreInst(PersonalityFn, PersonalityFieldPtr, true,
777                EntryBB->getTerminator());
778
779  // LSDA address
780  Idxs[1] = ConstantInt::get(Int32Ty, 4);
781  Value *LSDAFieldPtr =
782    GetElementPtrInst::Create(FuncCtx, Idxs, "lsda_gep",
783                              EntryBB->getTerminator());
784  Value *LSDA = CallInst::Create(LSDAAddrFn, "lsda_addr",
785                                 EntryBB->getTerminator());
786  new StoreInst(LSDA, LSDAFieldPtr, true, EntryBB->getTerminator());
787
788  // Get a reference to the jump buffer.
789  Idxs[1] = ConstantInt::get(Int32Ty, 5);
790  Value *JBufPtr =
791    GetElementPtrInst::Create(FuncCtx, Idxs, "jbuf_gep",
792                              EntryBB->getTerminator());
793  Idxs[1] = Zero;
794  Value *FramePtr =
795    GetElementPtrInst::Create(JBufPtr, Idxs, "jbuf_fp_gep",
796                              EntryBB->getTerminator());
797
798  // Save the frame pointer.
799  Value *Val = CallInst::Create(FrameAddrFn,
800                                ConstantInt::get(Int32Ty, 0),
801                                "fp",
802                                EntryBB->getTerminator());
803  new StoreInst(Val, FramePtr, true, EntryBB->getTerminator());
804
805  // Save the stack pointer.
806  Idxs[1] = ConstantInt::get(Int32Ty, 2);
807  Value *StackPtr =
808    GetElementPtrInst::Create(JBufPtr, Idxs, "jbuf_sp_gep",
809                              EntryBB->getTerminator());
810
811  Val = CallInst::Create(StackAddrFn, "sp", EntryBB->getTerminator());
812  new StoreInst(Val, StackPtr, true, EntryBB->getTerminator());
813
814  // Call the setjmp instrinsic. It fills in the rest of the jmpbuf.
815  Value *SetjmpArg =
816    CastInst::Create(Instruction::BitCast, JBufPtr,
817                     Type::getInt8PtrTy(F.getContext()), "",
818                     EntryBB->getTerminator());
819  Value *DispatchVal = CallInst::Create(BuiltinSetjmpFn, SetjmpArg,
820                                        "dispatch",
821                                        EntryBB->getTerminator());
822
823  // Add a call to dispatch_setup after the setjmp call. This is expanded to any
824  // target-specific setup that needs to be done.
825  CallInst::Create(DispatchSetupFn, DispatchVal, "", EntryBB->getTerminator());
826}
827
828/// setupEntryBlockAndCallSites - Setup the entry block by creating and filling
829/// the function context and marking the call sites with the appropriate
830/// values. These values are used by the DWARF EH emitter.
831bool SjLjEHPass::setupEntryBlockAndCallSites(Function &F) {
832  SmallVector<InvokeInst*,     16> Invokes;
833  SmallVector<LandingPadInst*, 16> LPads;
834
835  // Look through the terminators of the basic blocks to find invokes.
836  for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
837    if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
838      Invokes.push_back(II);
839      LPads.push_back(II->getUnwindDest()->getLandingPadInst());
840    }
841
842  if (Invokes.empty()) return false;
843
844  setupFunctionContext(F, LPads);
845
846  // At this point, we are all set up, update the invoke instructions to mark
847  // their call_site values, and fill in the dispatch switch accordingly.
848  for (unsigned I = 0, E = Invokes.size(); I != E; ++I) {
849    insertCallSiteStore(Invokes[I], I + 1, CallSite);
850
851    ConstantInt *CallSiteNum =
852      ConstantInt::get(Type::getInt32Ty(F.getContext()), I + 1);
853
854    // Record the call site value for the back end so it stays associated with
855    // the invoke.
856    CallInst::Create(CallSiteFn, CallSiteNum, "", Invokes[I]);
857  }
858
859  // Mark call instructions that aren't nounwind as no-action (call_site ==
860  // -1). Skip the entry block, as prior to then, no function context has been
861  // created for this function and any unexpected exceptions thrown will go
862  // directly to the caller's context, which is what we want anyway, so no need
863  // to do anything here.
864  for (Function::iterator BB = F.begin(), E = F.end(); ++BB != E;) {
865    for (BasicBlock::iterator I = BB->begin(), end = BB->end(); I != end; ++I)
866      if (CallInst *CI = dyn_cast<CallInst>(I)) {
867        if (!CI->doesNotThrow())
868          insertCallSiteStore(CI, -1, CallSite);
869      } else if (ResumeInst *RI = dyn_cast<ResumeInst>(I)) {
870        insertCallSiteStore(RI, -1, CallSite);
871      }
872  }
873
874  return true;
875}
876
877bool SjLjEHPass::runOnFunction(Function &F) {
878  bool Res = false;
879  if (!DisableOldSjLjEH)
880    Res = insertSjLjEHSupport(F);
881  else
882    Res = setupEntryBlockAndCallSites(F);
883  return Res;
884}
885