blob: baa70b961b994b1bf1a11712edd181ebb2a1a123 [file] [log] [blame]
Jim Grosbach8b818d72009-08-17 16:41:22 +00001//===- 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"
Bill Wendling2b6bd7b2011-09-28 03:14:05 +000025#include "llvm/Target/TargetData.h"
Benjamin Kramerf7888542010-11-06 11:45:59 +000026#include "llvm/Target/TargetLowering.h"
Jim Grosbach8b818d72009-08-17 16:41:22 +000027#include "llvm/Transforms/Utils/BasicBlockUtils.h"
28#include "llvm/Transforms/Utils/Local.h"
Bill Wendling2b6bd7b2011-09-28 03:14:05 +000029#include "llvm/Support/CommandLine.h"
Bill Wendlingd36b3e32011-08-22 18:44:49 +000030#include "llvm/Support/Debug.h"
Bill Wendling2b6bd7b2011-09-28 03:14:05 +000031#include "llvm/Support/IRBuilder.h"
Bill Wendlingaef508d2011-08-22 23:38:40 +000032#include "llvm/ADT/DenseMap.h"
Bill Wendlingd36b3e32011-08-22 18:44:49 +000033#include "llvm/ADT/SmallVector.h"
34#include "llvm/ADT/Statistic.h"
Benjamin Kramerf7888542010-11-06 11:45:59 +000035#include <set>
Jim Grosbach8b818d72009-08-17 16:41:22 +000036using namespace llvm;
37
Bill Wendling2b6bd7b2011-09-28 03:14:05 +000038static cl::opt<bool> DisableOldSjLjEH("disable-old-sjlj-eh", cl::Hidden,
39 cl::desc("Disable the old SjLj EH preparation pass"));
40
Jim Grosbach8b818d72009-08-17 16:41:22 +000041STATISTIC(NumInvokes, "Number of invokes replaced");
42STATISTIC(NumUnwinds, "Number of unwinds replaced");
43STATISTIC(NumSpilled, "Number of registers live across unwind edges");
44
45namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000046 class SjLjEHPass : public FunctionPass {
Jim Grosbach8b818d72009-08-17 16:41:22 +000047 const TargetLowering *TLI;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000048 Type *FunctionContextTy;
Jim Grosbach8b818d72009-08-17 16:41:22 +000049 Constant *RegisterFn;
50 Constant *UnregisterFn;
Jim Grosbach8b818d72009-08-17 16:41:22 +000051 Constant *BuiltinSetjmpFn;
52 Constant *FrameAddrFn;
Jim Grosbach0798edd2010-05-27 23:49:24 +000053 Constant *StackAddrFn;
54 Constant *StackRestoreFn;
Jim Grosbach8b818d72009-08-17 16:41:22 +000055 Constant *LSDAAddrFn;
56 Value *PersonalityFn;
Duncan Sandsb01bbdc2009-10-14 16:11:37 +000057 Constant *SelectorFn;
Jim Grosbach8b818d72009-08-17 16:41:22 +000058 Constant *ExceptionFn;
Jim Grosbachca752c92010-01-28 01:45:32 +000059 Constant *CallSiteFn;
Jim Grosbache4ad3872010-10-19 23:27:08 +000060 Constant *DispatchSetupFn;
Jim Grosbach8b818d72009-08-17 16:41:22 +000061 Value *CallSite;
Bill Wendlingaef508d2011-08-22 23:38:40 +000062 DenseMap<InvokeInst*, BasicBlock*> LPadSuccMap;
Jim Grosbach8b818d72009-08-17 16:41:22 +000063 public:
64 static char ID; // Pass identification, replacement for typeid
65 explicit SjLjEHPass(const TargetLowering *tli = NULL)
Owen Anderson90c579d2010-08-06 18:33:48 +000066 : FunctionPass(ID), TLI(tli) { }
Jim Grosbach8b818d72009-08-17 16:41:22 +000067 bool doInitialization(Module &M);
68 bool runOnFunction(Function &F);
69
Bill Wendlingd36b3e32011-08-22 18:44:49 +000070 virtual void getAnalysisUsage(AnalysisUsage &AU) const {}
Jim Grosbach8b818d72009-08-17 16:41:22 +000071 const char *getPassName() const {
72 return "SJLJ Exception Handling preparation";
73 }
74
75 private:
Bill Wendling2b6bd7b2011-09-28 03:14:05 +000076 bool setupEntryBlockAndCallSites(Function &F);
77 void setupFunctionContext(Function &F, ArrayRef<LandingPadInst*> LPads);
78
Jim Grosbachb58a59b2010-03-04 22:07:46 +000079 void insertCallSiteStore(Instruction *I, int Number, Value *CallSite);
80 void markInvokeCallSite(InvokeInst *II, int InvokeNo, Value *CallSite,
Jim Grosbach0bb61c52009-08-31 01:35:03 +000081 SwitchInst *CatchSwitch);
Jim Grosbach288694f2010-06-15 18:53:34 +000082 void splitLiveRangesAcrossInvokes(SmallVector<InvokeInst*,16> &Invokes);
Bill Wendling36699152011-09-12 21:56:59 +000083 void splitLandingPad(InvokeInst *II);
Jim Grosbach8b818d72009-08-17 16:41:22 +000084 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}
Jim Grosbacha235d132009-08-23 18:13:48 +000094// doInitialization - Set up decalarations and types needed to process
95// exceptions.
Jim Grosbach8b818d72009-08-17 16:41:22 +000096bool SjLjEHPass::doInitialization(Module &M) {
97 // Build the function context structure.
98 // builtin_setjmp uses a five word jbuf
Jay Foad5fdd6c82011-07-12 14:06:48 +000099 Type *VoidPtrTy = Type::getInt8PtrTy(M.getContext());
100 Type *Int32Ty = Type::getInt32Ty(M.getContext());
Jim Grosbach8b818d72009-08-17 16:41:22 +0000101 FunctionContextTy =
Chris Lattnerb2318662011-06-18 22:48:56 +0000102 StructType::get(VoidPtrTy, // __prev
Jim Grosbach8b818d72009-08-17 16:41:22 +0000103 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);
Jim Grosbach8b818d72009-08-17 16:41:22 +0000118 FrameAddrFn = Intrinsic::getDeclaration(&M, Intrinsic::frameaddress);
Jim Grosbach0798edd2010-05-27 23:49:24 +0000119 StackAddrFn = Intrinsic::getDeclaration(&M, Intrinsic::stacksave);
120 StackRestoreFn = Intrinsic::getDeclaration(&M, Intrinsic::stackrestore);
Jim Grosbach8b818d72009-08-17 16:41:22 +0000121 BuiltinSetjmpFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_sjlj_setjmp);
122 LSDAAddrFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_sjlj_lsda);
Duncan Sandsb01bbdc2009-10-14 16:11:37 +0000123 SelectorFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_selector);
Jim Grosbach8b818d72009-08-17 16:41:22 +0000124 ExceptionFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_exception);
Jim Grosbachca752c92010-01-28 01:45:32 +0000125 CallSiteFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_sjlj_callsite);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000126 DispatchSetupFn
127 = Intrinsic::getDeclaration(&M, Intrinsic::eh_sjlj_dispatch_setup);
Jim Grosbacha235d132009-08-23 18:13:48 +0000128 PersonalityFn = 0;
Jim Grosbach8b818d72009-08-17 16:41:22 +0000129
130 return true;
131}
132
Jim Grosbachb58a59b2010-03-04 22:07:46 +0000133/// 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
Bill Wendling36699152011-09-12 21:56:59 +0000143/// 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
Jim Grosbach8b818d72009-08-17 16:41:22 +0000179/// markInvokeCallSite - Insert code to mark the call_site for this invoke
Jim Grosbachb58a59b2010-03-04 22:07:46 +0000180void SjLjEHPass::markInvokeCallSite(InvokeInst *II, int InvokeNo,
Jim Grosbach0bb61c52009-08-31 01:35:03 +0000181 Value *CallSite,
182 SwitchInst *CatchSwitch) {
Jim Grosbach8b818d72009-08-17 16:41:22 +0000183 ConstantInt *CallSiteNoC= ConstantInt::get(Type::getInt32Ty(II->getContext()),
Jim Grosbachb58a59b2010-03-04 22:07:46 +0000184 InvokeNo);
Jim Grosbach0bb61c52009-08-31 01:35:03 +0000185 // 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()),
Bill Wendling36699152011-09-12 21:56:59 +0000188 InvokeNo - 1);
Jim Grosbach8b818d72009-08-17 16:41:22 +0000189
190 // If the unwind edge has phi nodes, split the edge.
191 if (isa<PHINode>(II->getUnwindDest()->begin())) {
Bill Wendling51fb91c2011-08-26 21:18:55 +0000192 // FIXME: New EH - This if-condition will be always true in the new scheme.
Bill Wendling36699152011-09-12 21:56:59 +0000193 if (II->getUnwindDest()->isLandingPad())
194 splitLandingPad(II);
195 else
Bill Wendling51fb91c2011-08-26 21:18:55 +0000196 SplitCriticalEdge(II, 1, this);
Jim Grosbach8b818d72009-08-17 16:41:22 +0000197
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
Jim Grosbachb58a59b2010-03-04 22:07:46 +0000205 // 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.
Jim Grosbachca752c92010-01-28 01:45:32 +0000210 CallInst::Create(CallSiteFn, CallSiteNoC, "", II);
Jim Grosbach8b818d72009-08-17 16:41:22 +0000211
Jim Grosbach0bb61c52009-08-31 01:35:03 +0000212 // Add a switch case to our unwind block.
Bill Wendlingaef508d2011-08-22 23:38:40 +0000213 if (BasicBlock *SuccBB = LPadSuccMap[II]) {
214 CatchSwitch->addCase(SwitchValC, SuccBB);
215 } else {
216 CatchSwitch->addCase(SwitchValC, II->getUnwindDest());
217 }
218
Jim Grosbachca752c92010-01-28 01:45:32 +0000219 // 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.
Jim Grosbach8b818d72009-08-17 16:41:22 +0000221}
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
Jim Grosbach606f3d62009-08-17 21:40:03 +0000232/// 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.
Jim Grosbach2f3257e2010-06-01 18:06:35 +0000236/// FIXME: Move this function to a common utility file (Local.cpp?) so
237/// both SjLj and LowerInvoke can use it.
Jim Grosbach8b818d72009-08-17 16:41:22 +0000238void SjLjEHPass::
Jim Grosbach288694f2010-06-15 18:53:34 +0000239splitLiveRangesAcrossInvokes(SmallVector<InvokeInst*,16> &Invokes) {
Jim Grosbach8b818d72009-08-17 16:41:22 +0000240 // 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);
Bill Wendlingaef508d2011-08-22 23:38:40 +0000244
245 // FIXME: New EH - This if-condition will be always true in the new scheme.
Bill Wendling36699152011-09-12 21:56:59 +0000246 if (II->getUnwindDest()->isLandingPad())
247 splitLandingPad(II);
248 else
Bill Wendlingaef508d2011-08-22 23:38:40 +0000249 SplitCriticalEdge(II, 1, this);
Bill Wendlingaef508d2011-08-22 23:38:40 +0000250
Jim Grosbach8b818d72009-08-17 16:41:22 +0000251 assert(!isa<PHINode>(II->getNormalDest()) &&
252 !isa<PHINode>(II->getUnwindDest()) &&
Bill Wendlingd36b3e32011-08-22 18:44:49 +0000253 "Critical edge splitting left single entry phi nodes?");
Jim Grosbach8b818d72009-08-17 16:41:22 +0000254 }
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) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000267 Type *Ty = AI->getType();
Jim Grosbache70fc8e2010-06-30 22:20:38 +0000268 // Aggregate types can't be cast, but are legal argument types, so we have
Jim Grosbachdc58b252010-06-01 18:04:09 +0000269 // to handle them differently. We use an extract/insert pair as a
270 // lightweight method to achieve the same goal.
Jim Grosbache70fc8e2010-06-30 22:20:38 +0000271 if (isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) {
272 Instruction *EI = ExtractValueInst::Create(AI, 0, "",AfterAllocaInsertPt);
Jim Grosbachdc58b252010-06-01 18:04:09 +0000273 Instruction *NI = InsertValueInst::Create(AI, EI, 0);
274 NI->insertAfter(EI);
275 AI->replaceAllUsesWith(NI);
Jim Grosbache70fc8e2010-06-30 22:20:38 +0000276 // Set the operand of the instructions back to the AllocaInst.
Jim Grosbachdc58b252010-06-01 18:04:09 +0000277 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 }
Jim Grosbach8b818d72009-08-17 16:41:22 +0000294 }
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.
Jim Grosbach606f3d62009-08-17 21:40:03 +0000315 SmallVector<Instruction*,16> Users;
Jim Grosbach8b818d72009-08-17 16:41:22 +0000316 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
Jim Grosbach8b818d72009-08-17 16:41:22 +0000323 // 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.
Jim Grosbach956352e2010-06-16 18:45:08 +0000352 // 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.
Jim Grosbach8b818d72009-08-17 16:41:22 +0000355 if (NeedsSpill) {
356 ++NumSpilled;
357 DemoteRegToStack(*Inst, true);
358 }
359 }
360}
361
Bill Wendlingaef508d2011-08-22 23:38:40 +0000362/// 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
Bill Wendlingfc8713f2011-08-23 22:55:03 +0000396 I->replaceUsesOfWith(Inst, CreateLandingPadLoad(F, ExnAddr, SelAddr, I));
Bill Wendlingaef508d2011-08-22 23:38:40 +0000397 }
398}
399
Jim Grosbach8b818d72009-08-17 16:41:22 +0000400bool SjLjEHPass::insertSjLjEHSupport(Function &F) {
Jim Grosbach606f3d62009-08-17 21:40:03 +0000401 SmallVector<ReturnInst*,16> Returns;
402 SmallVector<UnwindInst*,16> Unwinds;
403 SmallVector<InvokeInst*,16> Invokes;
Jim Grosbach8b818d72009-08-17 16:41:22 +0000404
405 // Look through the terminators of the basic blocks to find invokes, returns
Jim Grosbachb58a59b2010-03-04 22:07:46 +0000406 // and unwinds.
407 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
Jim Grosbach8b818d72009-08-17 16:41:22 +0000408 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 }
Jim Grosbachb58a59b2010-03-04 22:07:46 +0000417 }
Bill Wendling8d90b712011-01-07 02:54:45 +0000418
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;
Jim Grosbach8b818d72009-08-17 16:41:22 +0000424
Jim Grosbach0798edd2010-05-27 23:49:24 +0000425 // 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.
Jim Grosbacha235d132009-08-23 18:13:48 +0000433 // FIXME: That's a bit ugly. Better way?
434 SmallVector<CallInst*,16> EH_Selectors;
435 SmallVector<CallInst*,16> EH_Exceptions;
Jim Grosbach0798edd2010-05-27 23:49:24 +0000436 SmallVector<Instruction*,16> JmpbufUpdatePoints;
Bill Wendling8d90b712011-01-07 02:54:45 +0000437
Bill Wendlingcfcccef2011-08-23 22:20:16 +0000438 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
Jim Grosbacha235d132009-08-23 18:13:48 +0000455 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
456 if (CallInst *CI = dyn_cast<CallInst>(I)) {
Duncan Sandsb01bbdc2009-10-14 16:11:37 +0000457 if (CI->getCalledFunction() == SelectorFn) {
Gabor Greif45c097f2010-06-25 09:36:23 +0000458 if (!PersonalityFn) PersonalityFn = CI->getArgOperand(1);
Jim Grosbacha235d132009-08-23 18:13:48 +0000459 EH_Selectors.push_back(CI);
460 } else if (CI->getCalledFunction() == ExceptionFn) {
461 EH_Exceptions.push_back(CI);
Jim Grosbach0798edd2010-05-27 23:49:24 +0000462 } else if (CI->getCalledFunction() == StackRestoreFn) {
463 JmpbufUpdatePoints.push_back(CI);
Jim Grosbacha235d132009-08-23 18:13:48 +0000464 }
Jim Grosbach0798edd2010-05-27 23:49:24 +0000465 } else if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
466 JmpbufUpdatePoints.push_back(AI);
Bill Wendlingaef508d2011-08-22 23:38:40 +0000467 } 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();
Jim Grosbacha235d132009-08-23 18:13:48 +0000471 }
472 }
473 }
Bill Wendling8d90b712011-01-07 02:54:45 +0000474
Jim Grosbacha235d132009-08-23 18:13:48 +0000475 // 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
Bill Wendling8d90b712011-01-07 02:54:45 +0000479 // 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);
Jim Grosbach8b818d72009-08-17 16:41:22 +0000488
Bill Wendlingaef508d2011-08-22 23:38:40 +0000489
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
Bill Wendling8d90b712011-01-07 02:54:45 +0000499 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());
Jim Grosbach8b818d72009-08-17 16:41:22 +0000507
Bill Wendling8d90b712011-01-07 02:54:45 +0000508 Value *Idxs[2];
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000509 Type *Int32Ty = Type::getInt32Ty(F.getContext());
Bill Wendling8d90b712011-01-07 02:54:45 +0000510 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);
Jay Foada9203102011-07-25 09:48:08 +0000514 CallSite = GetElementPtrInst::Create(FunctionContext, Idxs, "call_site",
Bill Wendling8d90b712011-01-07 02:54:45 +0000515 EntryBB->getTerminator());
Jim Grosbach8b818d72009-08-17 16:41:22 +0000516
Bill Wendling8d90b712011-01-07 02:54:45 +0000517 // The exception selector comes back in context->data[1]
518 Idxs[1] = ConstantInt::get(Int32Ty, 2);
Jay Foada9203102011-07-25 09:48:08 +0000519 Value *FCData = GetElementPtrInst::Create(FunctionContext, Idxs, "fc_data",
Bill Wendling8d90b712011-01-07 02:54:45 +0000520 EntryBB->getTerminator());
521 Idxs[1] = ConstantInt::get(Int32Ty, 1);
Jay Foada9203102011-07-25 09:48:08 +0000522 Value *SelectorAddr = GetElementPtrInst::Create(FCData, Idxs,
Bill Wendling8d90b712011-01-07 02:54:45 +0000523 "exc_selector_gep",
524 EntryBB->getTerminator());
525 // The exception value comes back in context->data[0]
526 Idxs[1] = Zero;
Jay Foada9203102011-07-25 09:48:08 +0000527 Value *ExceptionAddr = GetElementPtrInst::Create(FCData, Idxs,
Bill Wendling8d90b712011-01-07 02:54:45 +0000528 "exception_gep",
529 EntryBB->getTerminator());
Jim Grosbach8b818d72009-08-17 16:41:22 +0000530
Bill Wendling8d90b712011-01-07 02:54:45 +0000531 // 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);
Jim Grosbach8b818d72009-08-17 16:41:22 +0000538 }
539
Bill Wendling8d90b712011-01-07 02:54:45 +0000540 // 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);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000549 Type *Ty = Type::getInt8PtrTy(F.getContext());
Bill Wendling8d90b712011-01-07 02:54:45 +0000550 Val = CastInst::Create(Instruction::IntToPtr, Val, Ty, "", I);
551
552 I->replaceAllUsesWith(Val);
553 I->eraseFromParent();
554 }
555
Bill Wendlingaef508d2011-08-22 23:38:40 +0000556 for (unsigned i = 0, e = LandingPads.size(); i != e; ++i)
557 ReplaceLandingPadVal(F, LandingPads[i], ExceptionAddr, SelectorAddr);
558
Bill Wendling8d90b712011-01-07 02:54:45 +0000559 // 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
Bill Wendling8d90b712011-01-07 02:54:45 +0000569 // Insert a load of the callsite in the dispatch block, and a switch on its
Bill Wendlingcc885952011-04-11 21:32:34 +0000570 // 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);
Bill Wendling8d90b712011-01-07 02:54:45 +0000576
577 Value *DispatchLoad = new LoadInst(CallSite, "invoke.num", true,
578 DispatchBlock);
579 SwitchInst *DispatchSwitch =
Bill Wendlingcc885952011-04-11 21:32:34 +0000580 SwitchInst::Create(DispatchLoad, TrapBlock, Invokes.size(),
Bill Wendling8d90b712011-01-07 02:54:45 +0000581 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 =
Jay Foada9203102011-07-25 09:48:08 +0000595 GetElementPtrInst::Create(FunctionContext, Idxs, "lsda_gep",
Bill Wendling8d90b712011-01-07 02:54:45 +0000596 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 =
Jay Foada9203102011-07-25 09:48:08 +0000603 GetElementPtrInst::Create(FunctionContext, Idxs, "lsda_gep",
Bill Wendling8d90b712011-01-07 02:54:45 +0000604 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
Jay Foada9203102011-07-25 09:48:08 +0000611 = GetElementPtrInst::Create(FunctionContext, Idxs, "jbuf_gep",
Bill Wendling8d90b712011-01-07 02:54:45 +0000612 EntryBB->getTerminator());
613 Idxs[1] = ConstantInt::get(Int32Ty, 0);
614 Value *FramePtr =
Jay Foada9203102011-07-25 09:48:08 +0000615 GetElementPtrInst::Create(JBufPtr, Idxs, "jbuf_fp_gep",
Bill Wendling8d90b712011-01-07 02:54:45 +0000616 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 =
Jay Foada9203102011-07-25 09:48:08 +0000627 GetElementPtrInst::Create(JBufPtr, Idxs, "jbuf_sp_gep",
Bill Wendling8d90b712011-01-07 02:54:45 +0000628 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());
Bill Wendlingf05b1dc2011-04-05 01:37:43 +0000641
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.
Bill Wendling61512ba2011-05-11 01:11:55 +0000644 CallInst::Create(DispatchSetupFn, DispatchVal, "", EntryBB->getTerminator());
Bill Wendlingf05b1dc2011-04-05 01:37:43 +0000645
Bill Wendling8d90b712011-01-07 02:54:45 +0000646 // 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);
Bill Wendling3ae96d62011-08-24 00:00:23 +0000680 } else if (ResumeInst *RI = dyn_cast<ResumeInst>(I)) {
681 insertCallSiteStore(RI, -1, CallSite);
Bill Wendling8d90b712011-01-07 02:54:45 +0000682 }
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) {
Bill Wendlingcc885952011-04-11 21:32:34 +0000688 BranchInst::Create(TrapBlock, Unwinds[i]);
Bill Wendling8d90b712011-01-07 02:54:45 +0000689 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
Jim Grosbach8b818d72009-08-17 16:41:22 +0000707 return true;
708}
709
Bill Wendling2b6bd7b2011-09-28 03:14:05 +0000710/// 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
Jim Grosbach8b818d72009-08-17 16:41:22 +0000877bool SjLjEHPass::runOnFunction(Function &F) {
Bill Wendling2b6bd7b2011-09-28 03:14:05 +0000878 bool Res = false;
879 if (!DisableOldSjLjEH)
880 Res = insertSjLjEHSupport(F);
881 else
882 Res = setupEntryBlockAndCallSites(F);
Jim Grosbach8b818d72009-08-17 16:41:22 +0000883 return Res;
884}