blob: 89a939ffcbcf73a43b04445688981a7c1053e869 [file] [log] [blame]
Chris Lattner86e44452003-10-05 19:14:42 +00001//===- LowerInvoke.cpp - Eliminate Invoke & Unwind instructions -----------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner86e44452003-10-05 19:14:42 +00009//
10// This transformation is designed for use by code generators which do not yet
Chris Lattner6d784572004-02-08 19:53:56 +000011// support stack unwinding. This pass supports two models of exception handling
12// lowering, the 'cheap' support and the 'expensive' support.
13//
14// 'Cheap' exception handling support gives the program the ability to execute
15// any program which does not "throw an exception", by turning 'invoke'
16// instructions into calls and by turning 'unwind' instructions into calls to
17// abort(). If the program does dynamically use the unwind instruction, the
18// program will print a message then abort.
19//
20// 'Expensive' exception handling support gives the full exception handling
John Criswellfe3706a2005-05-02 14:47:42 +000021// support to the program at the cost of making the 'invoke' instruction
22// really expensive. It basically inserts setjmp/longjmp calls to emulate the
23// exception handling as necessary.
Chris Lattner6d784572004-02-08 19:53:56 +000024//
25// Because the 'expensive' support slows down programs a lot, and EH is only
26// used for a subset of the programs, it must be specifically enabled by an
27// option.
Chris Lattner86e44452003-10-05 19:14:42 +000028//
Chris Lattner0e28eca2004-03-31 22:00:30 +000029// Note that after this pass runs the CFG is not entirely accurate (exceptional
30// control flow edges are not correct anymore) so only very simple things should
31// be done after the lowerinvoke pass has run (like generation of native code).
32// This should not be used as a general purpose "my LLVM-to-LLVM pass doesn't
33// support the invoke instruction yet" lowering pass.
34//
Chris Lattner86e44452003-10-05 19:14:42 +000035//===----------------------------------------------------------------------===//
36
37#include "llvm/Transforms/Scalar.h"
Chris Lattnere1c09302004-02-08 07:30:29 +000038#include "llvm/Constants.h"
39#include "llvm/DerivedTypes.h"
Chris Lattner6d784572004-02-08 19:53:56 +000040#include "llvm/Instructions.h"
Chris Lattnere1c09302004-02-08 07:30:29 +000041#include "llvm/Module.h"
Chris Lattner86e44452003-10-05 19:14:42 +000042#include "llvm/Pass.h"
Chris Lattner6d784572004-02-08 19:53:56 +000043#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +000044#include "llvm/Transforms/Utils/Local.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000045#include "llvm/ADT/Statistic.h"
46#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000047#include "llvm/Support/Compiler.h"
Chris Lattner6d784572004-02-08 19:53:56 +000048#include <csetjmp>
Chris Lattnerdead9932003-12-10 20:22:42 +000049using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000050
Chris Lattner86e44452003-10-05 19:14:42 +000051namespace {
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +000052 Statistic<> NumInvokes("lowerinvoke", "Number of invokes replaced");
53 Statistic<> NumUnwinds("lowerinvoke", "Number of unwinds replaced");
54 Statistic<> NumSpilled("lowerinvoke",
55 "Number of registers live across unwind edges");
Chris Lattner99cca7d2004-03-01 01:12:13 +000056 cl::opt<bool> ExpensiveEHSupport("enable-correct-eh-support",
Chris Lattner6d784572004-02-08 19:53:56 +000057 cl::desc("Make the -lowerinvoke pass insert expensive, but correct, EH code"));
Chris Lattner86e44452003-10-05 19:14:42 +000058
Chris Lattnerf4b54612006-06-28 22:08:15 +000059 class VISIBILITY_HIDDEN LowerInvoke : public FunctionPass {
Chris Lattner6d784572004-02-08 19:53:56 +000060 // Used for both models.
Chris Lattnere1c09302004-02-08 07:30:29 +000061 Function *WriteFn;
Chris Lattner86e44452003-10-05 19:14:42 +000062 Function *AbortFn;
Chris Lattner6d784572004-02-08 19:53:56 +000063 Value *AbortMessage;
64 unsigned AbortMessageLength;
65
66 // Used for expensive EH support.
67 const Type *JBLinkTy;
68 GlobalVariable *JBListHead;
69 Function *SetJmpFn, *LongJmpFn;
Chris Lattner86e44452003-10-05 19:14:42 +000070 public:
Nate Begeman14b05292005-11-05 09:21:28 +000071 LowerInvoke(unsigned Size = 200, unsigned Align = 0) : JumpBufSize(Size),
72 JumpBufAlign(Align) {}
Chris Lattner86e44452003-10-05 19:14:42 +000073 bool doInitialization(Module &M);
74 bool runOnFunction(Function &F);
Chris Lattnered96fe82006-05-17 21:05:27 +000075
76 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
77 // This is a cluster of orthogonal Transforms
78 AU.addPreservedID(PromoteMemoryToRegisterID);
79 AU.addPreservedID(LowerSelectID);
80 AU.addPreservedID(LowerSwitchID);
81 AU.addPreservedID(LowerAllocationsID);
82 }
83
Chris Lattner6d784572004-02-08 19:53:56 +000084 private:
Chris Lattner0c3b3902004-11-13 19:07:32 +000085 void createAbortMessage();
Chris Lattner501825e2004-02-08 22:14:44 +000086 void writeAbortMessage(Instruction *IB);
Chris Lattner6d784572004-02-08 19:53:56 +000087 bool insertCheapEHSupport(Function &F);
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +000088 void splitLiveRangesLiveAcrossInvokes(std::vector<InvokeInst*> &Invokes);
89 void rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo,
90 AllocaInst *InvokeNum, SwitchInst *CatchSwitch);
Chris Lattner6d784572004-02-08 19:53:56 +000091 bool insertExpensiveEHSupport(Function &F);
Nate Begeman14b05292005-11-05 09:21:28 +000092
93 unsigned JumpBufSize;
94 unsigned JumpBufAlign;
Chris Lattner86e44452003-10-05 19:14:42 +000095 };
96
97 RegisterOpt<LowerInvoke>
98 X("lowerinvoke", "Lower invoke and unwind, for unwindless code generators");
99}
100
Chris Lattnercefc18e2004-02-13 16:16:16 +0000101const PassInfo *llvm::LowerInvokePassID = X.getPassInfo();
102
Brian Gaeked0fde302003-11-11 22:41:34 +0000103// Public Interface To the LowerInvoke pass.
Nate Begeman14b05292005-11-05 09:21:28 +0000104FunctionPass *llvm::createLowerInvokePass(unsigned JumpBufSize,
105 unsigned JumpBufAlign) {
106 return new LowerInvoke(JumpBufSize, JumpBufAlign);
107}
Chris Lattner86e44452003-10-05 19:14:42 +0000108
109// doInitialization - Make sure that there is a prototype for abort in the
110// current module.
111bool LowerInvoke::doInitialization(Module &M) {
Chris Lattner6d784572004-02-08 19:53:56 +0000112 const Type *VoidPtrTy = PointerType::get(Type::SByteTy);
Chris Lattnerf1d0d352004-02-09 22:48:47 +0000113 AbortMessage = 0;
Chris Lattner6d784572004-02-08 19:53:56 +0000114 if (ExpensiveEHSupport) {
Nate Begeman14b05292005-11-05 09:21:28 +0000115 // Insert a type for the linked list of jump buffers.
116 const Type *JmpBufTy = ArrayType::get(VoidPtrTy, JumpBufSize);
Chris Lattnere1c09302004-02-08 07:30:29 +0000117
Chris Lattner6d784572004-02-08 19:53:56 +0000118 { // The type is recursive, so use a type holder.
119 std::vector<const Type*> Elements;
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000120 Elements.push_back(JmpBufTy);
Chris Lattner6d784572004-02-08 19:53:56 +0000121 OpaqueType *OT = OpaqueType::get();
122 Elements.push_back(PointerType::get(OT));
Chris Lattner6d784572004-02-08 19:53:56 +0000123 PATypeHolder JBLType(StructType::get(Elements));
124 OT->refineAbstractTypeTo(JBLType.get()); // Complete the cycle.
125 JBLinkTy = JBLType.get();
Chris Lattner11b9be52004-05-28 05:02:13 +0000126 M.addTypeName("llvm.sjljeh.jmpbufty", JBLinkTy);
Chris Lattner6d784572004-02-08 19:53:56 +0000127 }
128
129 const Type *PtrJBList = PointerType::get(JBLinkTy);
130
131 // Now that we've done that, insert the jmpbuf list head global, unless it
132 // already exists.
133 if (!(JBListHead = M.getGlobalVariable("llvm.sjljeh.jblist", PtrJBList)))
134 JBListHead = new GlobalVariable(PtrJBList, false,
135 GlobalValue::LinkOnceLinkage,
136 Constant::getNullValue(PtrJBList),
137 "llvm.sjljeh.jblist", &M);
Chris Lattner860a1612004-02-15 22:24:27 +0000138 SetJmpFn = M.getOrInsertFunction("llvm.setjmp", Type::IntTy,
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000139 PointerType::get(JmpBufTy), (Type *)0);
Chris Lattner860a1612004-02-15 22:24:27 +0000140 LongJmpFn = M.getOrInsertFunction("llvm.longjmp", Type::VoidTy,
Chris Lattner6d784572004-02-08 19:53:56 +0000141 PointerType::get(JmpBufTy),
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000142 Type::IntTy, (Type *)0);
Chris Lattner6d784572004-02-08 19:53:56 +0000143 }
144
145 // We need the 'write' and 'abort' functions for both models.
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000146 AbortFn = M.getOrInsertFunction("abort", Type::VoidTy, (Type *)0);
Chris Lattner501825e2004-02-08 22:14:44 +0000147
148 // Unfortunately, 'write' can end up being prototyped in several different
149 // ways. If the user defines a three (or more) operand function named 'write'
Misha Brukmanb9806e02004-02-08 22:27:33 +0000150 // we will use their prototype. We _do not_ want to insert another instance
Chris Lattner501825e2004-02-08 22:14:44 +0000151 // of a write prototype, because we don't know that the funcresolve pass will
152 // run after us. If there is a definition of a write function, but it's not
153 // suitable for our uses, we just don't emit write calls. If there is no
154 // write prototype at all, we just add one.
155 if (Function *WF = M.getNamedFunction("write")) {
156 if (WF->getFunctionType()->getNumParams() > 3 ||
157 WF->getFunctionType()->isVarArg())
158 WriteFn = WF;
159 else
160 WriteFn = 0;
161 } else {
162 WriteFn = M.getOrInsertFunction("write", Type::VoidTy, Type::IntTy,
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000163 VoidPtrTy, Type::IntTy, (Type *)0);
Chris Lattner501825e2004-02-08 22:14:44 +0000164 }
Chris Lattner86e44452003-10-05 19:14:42 +0000165 return true;
166}
167
Chris Lattner0c3b3902004-11-13 19:07:32 +0000168void LowerInvoke::createAbortMessage() {
169 Module &M = *WriteFn->getParent();
170 if (ExpensiveEHSupport) {
171 // The abort message for expensive EH support tells the user that the
172 // program 'unwound' without an 'invoke' instruction.
173 Constant *Msg =
174 ConstantArray::get("ERROR: Exception thrown, but not caught!\n");
175 AbortMessageLength = Msg->getNumOperands()-1; // don't include \0
Misha Brukmanfd939082005-04-21 23:48:37 +0000176
Chris Lattner0c3b3902004-11-13 19:07:32 +0000177 GlobalVariable *MsgGV = new GlobalVariable(Msg->getType(), true,
178 GlobalValue::InternalLinkage,
179 Msg, "abortmsg", &M);
Chris Lattner1381dd82005-05-13 06:10:12 +0000180 std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::IntTy));
Chris Lattner0c3b3902004-11-13 19:07:32 +0000181 AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, GEPIdx);
182 } else {
183 // The abort message for cheap EH support tells the user that EH is not
184 // enabled.
185 Constant *Msg =
186 ConstantArray::get("Exception handler needed, but not enabled. Recompile"
187 " program with -enable-correct-eh-support.\n");
188 AbortMessageLength = Msg->getNumOperands()-1; // don't include \0
189
190 GlobalVariable *MsgGV = new GlobalVariable(Msg->getType(), true,
191 GlobalValue::InternalLinkage,
192 Msg, "abortmsg", &M);
Chris Lattner1381dd82005-05-13 06:10:12 +0000193 std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::IntTy));
Chris Lattner0c3b3902004-11-13 19:07:32 +0000194 AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, GEPIdx);
195 }
196}
197
198
Chris Lattner501825e2004-02-08 22:14:44 +0000199void LowerInvoke::writeAbortMessage(Instruction *IB) {
200 if (WriteFn) {
Chris Lattner0c3b3902004-11-13 19:07:32 +0000201 if (AbortMessage == 0) createAbortMessage();
202
Chris Lattner501825e2004-02-08 22:14:44 +0000203 // These are the arguments we WANT...
204 std::vector<Value*> Args;
205 Args.push_back(ConstantInt::get(Type::IntTy, 2));
206 Args.push_back(AbortMessage);
207 Args.push_back(ConstantInt::get(Type::IntTy, AbortMessageLength));
208
209 // If the actual declaration of write disagrees, insert casts as
210 // appropriate.
211 const FunctionType *FT = WriteFn->getFunctionType();
212 unsigned NumArgs = FT->getNumParams();
213 for (unsigned i = 0; i != 3; ++i)
214 if (i < NumArgs && FT->getParamType(i) != Args[i]->getType())
Misha Brukmanfd939082005-04-21 23:48:37 +0000215 Args[i] = ConstantExpr::getCast(cast<Constant>(Args[i]),
Chris Lattner501825e2004-02-08 22:14:44 +0000216 FT->getParamType(i));
217
Chris Lattnera9e92112005-05-06 06:48:21 +0000218 (new CallInst(WriteFn, Args, "", IB))->setTailCall();
Chris Lattner501825e2004-02-08 22:14:44 +0000219 }
220}
221
Chris Lattner6d784572004-02-08 19:53:56 +0000222bool LowerInvoke::insertCheapEHSupport(Function &F) {
Chris Lattner86e44452003-10-05 19:14:42 +0000223 bool Changed = false;
Chris Lattnerdead9932003-12-10 20:22:42 +0000224 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
225 if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
Chris Lattner86e44452003-10-05 19:14:42 +0000226 // Insert a normal call instruction...
227 std::string Name = II->getName(); II->setName("");
Chris Lattnerefd91682005-05-13 06:27:02 +0000228 CallInst *NewCall = new CallInst(II->getCalledValue(),
229 std::vector<Value*>(II->op_begin()+3,
230 II->op_end()), Name, II);
231 NewCall->setCallingConv(II->getCallingConv());
Chris Lattner86e44452003-10-05 19:14:42 +0000232 II->replaceAllUsesWith(NewCall);
Misha Brukmanfd939082005-04-21 23:48:37 +0000233
Chris Lattnerdead9932003-12-10 20:22:42 +0000234 // Insert an unconditional branch to the normal destination.
Chris Lattner86e44452003-10-05 19:14:42 +0000235 new BranchInst(II->getNormalDest(), II);
236
Chris Lattnerdead9932003-12-10 20:22:42 +0000237 // Remove any PHI node entries from the exception destination.
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000238 II->getUnwindDest()->removePredecessor(BB);
Chris Lattnerdead9932003-12-10 20:22:42 +0000239
Chris Lattner86e44452003-10-05 19:14:42 +0000240 // Remove the invoke instruction now.
Chris Lattnerdead9932003-12-10 20:22:42 +0000241 BB->getInstList().erase(II);
Chris Lattner86e44452003-10-05 19:14:42 +0000242
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000243 ++NumInvokes; Changed = true;
Chris Lattnerdead9932003-12-10 20:22:42 +0000244 } else if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
Chris Lattnere1c09302004-02-08 07:30:29 +0000245 // Insert a new call to write(2, AbortMessage, AbortMessageLength);
Chris Lattner501825e2004-02-08 22:14:44 +0000246 writeAbortMessage(UI);
Chris Lattnere1c09302004-02-08 07:30:29 +0000247
Chris Lattner86e44452003-10-05 19:14:42 +0000248 // Insert a call to abort()
Chris Lattnera9e92112005-05-06 06:48:21 +0000249 (new CallInst(AbortFn, std::vector<Value*>(), "", UI))->setTailCall();
Chris Lattner86e44452003-10-05 19:14:42 +0000250
Chris Lattner6d784572004-02-08 19:53:56 +0000251 // Insert a return instruction. This really should be a "barrier", as it
252 // is unreachable.
Chris Lattner86e44452003-10-05 19:14:42 +0000253 new ReturnInst(F.getReturnType() == Type::VoidTy ? 0 :
254 Constant::getNullValue(F.getReturnType()), UI);
255
256 // Remove the unwind instruction now.
Chris Lattnerdead9932003-12-10 20:22:42 +0000257 BB->getInstList().erase(UI);
Chris Lattner86e44452003-10-05 19:14:42 +0000258
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000259 ++NumUnwinds; Changed = true;
Chris Lattner86e44452003-10-05 19:14:42 +0000260 }
261 return Changed;
262}
Chris Lattner6d784572004-02-08 19:53:56 +0000263
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000264/// rewriteExpensiveInvoke - Insert code and hack the function to replace the
265/// specified invoke instruction with a call.
266void LowerInvoke::rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo,
267 AllocaInst *InvokeNum,
268 SwitchInst *CatchSwitch) {
269 ConstantUInt *InvokeNoC = ConstantUInt::get(Type::UIntTy, InvokeNo);
Chris Lattner6d784572004-02-08 19:53:56 +0000270
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000271 // Insert a store of the invoke num before the invoke and store zero into the
272 // location afterward.
273 new StoreInst(InvokeNoC, InvokeNum, true, II); // volatile
Chris Lattner93e50ce2005-09-29 17:44:20 +0000274
275 BasicBlock::iterator NI = II->getNormalDest()->begin();
276 while (isa<PHINode>(NI)) ++NI;
277 // nonvolatile.
278 new StoreInst(Constant::getNullValue(Type::UIntTy), InvokeNum, false, NI);
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000279
280 // Add a switch case to our unwind block.
281 CatchSwitch->addCase(InvokeNoC, II->getUnwindDest());
282
283 // Insert a normal call instruction.
284 std::string Name = II->getName(); II->setName("");
285 CallInst *NewCall = new CallInst(II->getCalledValue(),
286 std::vector<Value*>(II->op_begin()+3,
287 II->op_end()), Name,
288 II);
289 NewCall->setCallingConv(II->getCallingConv());
290 II->replaceAllUsesWith(NewCall);
291
292 // Replace the invoke with an uncond branch.
293 new BranchInst(II->getNormalDest(), NewCall->getParent());
294 II->eraseFromParent();
295}
Chris Lattner6d784572004-02-08 19:53:56 +0000296
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000297/// MarkBlocksLiveIn - Insert BB and all of its predescessors into LiveBBs until
298/// we reach blocks we've already seen.
299static void MarkBlocksLiveIn(BasicBlock *BB, std::set<BasicBlock*> &LiveBBs) {
300 if (!LiveBBs.insert(BB).second) return; // already been here.
301
302 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
303 MarkBlocksLiveIn(*PI, LiveBBs);
304}
Chris Lattner6d784572004-02-08 19:53:56 +0000305
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000306// First thing we need to do is scan the whole function for values that are
307// live across unwind edges. Each value that is live across an unwind edge
308// we spill into a stack location, guaranteeing that there is nothing live
309// across the unwind edge. This process also splits all critical edges
310// coming out of invoke's.
311void LowerInvoke::
312splitLiveRangesLiveAcrossInvokes(std::vector<InvokeInst*> &Invokes) {
313 // First step, split all critical edges from invoke instructions.
314 for (unsigned i = 0, e = Invokes.size(); i != e; ++i) {
315 InvokeInst *II = Invokes[i];
316 SplitCriticalEdge(II, 0, this);
317 SplitCriticalEdge(II, 1, this);
318 assert(!isa<PHINode>(II->getNormalDest()) &&
319 !isa<PHINode>(II->getUnwindDest()) &&
320 "critical edge splitting left single entry phi nodes?");
Chris Lattner6d784572004-02-08 19:53:56 +0000321 }
322
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000323 Function *F = Invokes.back()->getParent()->getParent();
324
325 // To avoid having to handle incoming arguments specially, we lower each arg
326 // to a copy instruction in the entry block. This ensure that the argument
327 // value itself cannot be live across the entry block.
328 BasicBlock::iterator AfterAllocaInsertPt = F->begin()->begin();
329 while (isa<AllocaInst>(AfterAllocaInsertPt) &&
330 isa<ConstantInt>(cast<AllocaInst>(AfterAllocaInsertPt)->getArraySize()))
331 ++AfterAllocaInsertPt;
332 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
333 AI != E; ++AI) {
334 CastInst *NC = new CastInst(AI, AI->getType(), AI->getName()+".tmp",
335 AfterAllocaInsertPt);
336 AI->replaceAllUsesWith(NC);
337 NC->setOperand(0, AI);
338 }
339
340 // Finally, scan the code looking for instructions with bad live ranges.
341 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
342 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ++II) {
343 // Ignore obvious cases we don't have to handle. In particular, most
344 // instructions either have no uses or only have a single use inside the
345 // current block. Ignore them quickly.
346 Instruction *Inst = II;
347 if (Inst->use_empty()) continue;
348 if (Inst->hasOneUse() &&
349 cast<Instruction>(Inst->use_back())->getParent() == BB &&
350 !isa<PHINode>(Inst->use_back())) continue;
351
Chris Lattner45313712005-09-27 21:33:12 +0000352 // If this is an alloca in the entry block, it's not a real register
353 // value.
354 if (AllocaInst *AI = dyn_cast<AllocaInst>(Inst))
355 if (isa<ConstantInt>(AI->getArraySize()) && BB == F->begin())
356 continue;
357
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000358 // Avoid iterator invalidation by copying users to a temporary vector.
359 std::vector<Instruction*> Users;
360 for (Value::use_iterator UI = Inst->use_begin(), E = Inst->use_end();
361 UI != E; ++UI) {
362 Instruction *User = cast<Instruction>(*UI);
363 if (User->getParent() != BB || isa<PHINode>(User))
364 Users.push_back(User);
365 }
366
367 // Scan all of the uses and see if the live range is live across an unwind
368 // edge. If we find a use live across an invoke edge, create an alloca
369 // and spill the value.
370 AllocaInst *SpillLoc = 0;
371 std::set<InvokeInst*> InvokesWithStoreInserted;
372
373 // Find all of the blocks that this value is live in.
374 std::set<BasicBlock*> LiveBBs;
375 LiveBBs.insert(Inst->getParent());
376 while (!Users.empty()) {
377 Instruction *U = Users.back();
378 Users.pop_back();
379
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000380 if (!isa<PHINode>(U)) {
381 MarkBlocksLiveIn(U->getParent(), LiveBBs);
382 } else {
383 // Uses for a PHI node occur in their predecessor block.
384 PHINode *PN = cast<PHINode>(U);
385 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
386 if (PN->getIncomingValue(i) == Inst)
387 MarkBlocksLiveIn(PN->getIncomingBlock(i), LiveBBs);
388 }
389 }
390
391 // Now that we know all of the blocks that this thing is live in, see if
392 // it includes any of the unwind locations.
393 bool NeedsSpill = false;
394 for (unsigned i = 0, e = Invokes.size(); i != e; ++i) {
395 BasicBlock *UnwindBlock = Invokes[i]->getUnwindDest();
396 if (UnwindBlock != BB && LiveBBs.count(UnwindBlock)) {
397 NeedsSpill = true;
398 }
399 }
400
401 // If we decided we need a spill, do it.
402 if (NeedsSpill) {
403 ++NumSpilled;
404 DemoteRegToStack(*Inst, true);
405 }
406 }
407}
408
409bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
410 std::vector<ReturnInst*> Returns;
411 std::vector<UnwindInst*> Unwinds;
412 std::vector<InvokeInst*> Invokes;
413
414 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
415 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
416 // Remember all return instructions in case we insert an invoke into this
417 // function.
418 Returns.push_back(RI);
419 } else if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
420 Invokes.push_back(II);
421 } else if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
422 Unwinds.push_back(UI);
423 }
424
425 if (Unwinds.empty() && Invokes.empty()) return false;
426
427 NumInvokes += Invokes.size();
428 NumUnwinds += Unwinds.size();
Chris Lattner5b3c7022005-09-27 22:44:59 +0000429
430 // TODO: This is not an optimal way to do this. In particular, this always
431 // inserts setjmp calls into the entries of functions with invoke instructions
432 // even though there are possibly paths through the function that do not
433 // execute any invokes. In particular, for functions with early exits, e.g.
434 // the 'addMove' method in hexxagon, it would be nice to not have to do the
435 // setjmp stuff on the early exit path. This requires a bit of dataflow, but
436 // would not be too hard to do.
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000437
438 // If we have an invoke instruction, insert a setjmp that dominates all
439 // invokes. After the setjmp, use a cond branch that goes to the original
440 // code path on zero, and to a designated 'catch' block of nonzero.
441 Value *OldJmpBufPtr = 0;
442 if (!Invokes.empty()) {
443 // First thing we need to do is scan the whole function for values that are
444 // live across unwind edges. Each value that is live across an unwind edge
445 // we spill into a stack location, guaranteeing that there is nothing live
446 // across the unwind edge. This process also splits all critical edges
447 // coming out of invoke's.
448 splitLiveRangesLiveAcrossInvokes(Invokes);
449
450 BasicBlock *EntryBB = F.begin();
451
452 // Create an alloca for the incoming jump buffer ptr and the new jump buffer
453 // that needs to be restored on all exits from the function. This is an
454 // alloca because the value needs to be live across invokes.
455 AllocaInst *JmpBuf =
Nate Begeman14b05292005-11-05 09:21:28 +0000456 new AllocaInst(JBLinkTy, 0, JumpBufAlign, "jblink", F.begin()->begin());
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000457
458 std::vector<Value*> Idx;
459 Idx.push_back(Constant::getNullValue(Type::IntTy));
460 Idx.push_back(ConstantUInt::get(Type::UIntTy, 1));
461 OldJmpBufPtr = new GetElementPtrInst(JmpBuf, Idx, "OldBuf",
462 EntryBB->getTerminator());
463
464 // Copy the JBListHead to the alloca.
465 Value *OldBuf = new LoadInst(JBListHead, "oldjmpbufptr", true,
466 EntryBB->getTerminator());
467 new StoreInst(OldBuf, OldJmpBufPtr, true, EntryBB->getTerminator());
468
469 // Add the new jumpbuf to the list.
470 new StoreInst(JmpBuf, JBListHead, true, EntryBB->getTerminator());
471
472 // Create the catch block. The catch block is basically a big switch
473 // statement that goes to all of the invoke catch blocks.
474 BasicBlock *CatchBB = new BasicBlock("setjmp.catch", &F);
475
476 // Create an alloca which keeps track of which invoke is currently
477 // executing. For normal calls it contains zero.
478 AllocaInst *InvokeNum = new AllocaInst(Type::UIntTy, 0, "invokenum",
479 EntryBB->begin());
480 new StoreInst(ConstantInt::get(Type::UIntTy, 0), InvokeNum, true,
481 EntryBB->getTerminator());
482
483 // Insert a load in the Catch block, and a switch on its value. By default,
484 // we go to a block that just does an unwind (which is the correct action
485 // for a standard call).
486 BasicBlock *UnwindBB = new BasicBlock("unwindbb", &F);
487 Unwinds.push_back(new UnwindInst(UnwindBB));
488
489 Value *CatchLoad = new LoadInst(InvokeNum, "invoke.num", true, CatchBB);
490 SwitchInst *CatchSwitch =
491 new SwitchInst(CatchLoad, UnwindBB, Invokes.size(), CatchBB);
492
493 // Now that things are set up, insert the setjmp call itself.
494
495 // Split the entry block to insert the conditional branch for the setjmp.
496 BasicBlock *ContBlock = EntryBB->splitBasicBlock(EntryBB->getTerminator(),
497 "setjmp.cont");
498
499 Idx[1] = ConstantUInt::get(Type::UIntTy, 0);
500 Value *JmpBufPtr = new GetElementPtrInst(JmpBuf, Idx, "TheJmpBuf",
501 EntryBB->getTerminator());
502 Value *SJRet = new CallInst(SetJmpFn, JmpBufPtr, "sjret",
503 EntryBB->getTerminator());
504
505 // Compare the return value to zero.
506 Value *IsNormal = BinaryOperator::createSetEQ(SJRet,
507 Constant::getNullValue(SJRet->getType()),
508 "notunwind", EntryBB->getTerminator());
509 // Nuke the uncond branch.
510 EntryBB->getTerminator()->eraseFromParent();
511
512 // Put in a new condbranch in its place.
513 new BranchInst(ContBlock, CatchBB, IsNormal, EntryBB);
514
515 // At this point, we are all set up, rewrite each invoke instruction.
516 for (unsigned i = 0, e = Invokes.size(); i != e; ++i)
517 rewriteExpensiveInvoke(Invokes[i], i+1, InvokeNum, CatchSwitch);
518 }
519
520 // We know that there is at least one unwind.
521
522 // Create three new blocks, the block to load the jmpbuf ptr and compare
523 // against null, the block to do the longjmp, and the error block for if it
524 // is null. Add them at the end of the function because they are not hot.
525 BasicBlock *UnwindHandler = new BasicBlock("dounwind", &F);
526 BasicBlock *UnwindBlock = new BasicBlock("unwind", &F);
527 BasicBlock *TermBlock = new BasicBlock("unwinderror", &F);
528
529 // If this function contains an invoke, restore the old jumpbuf ptr.
530 Value *BufPtr;
531 if (OldJmpBufPtr) {
532 // Before the return, insert a copy from the saved value to the new value.
533 BufPtr = new LoadInst(OldJmpBufPtr, "oldjmpbufptr", UnwindHandler);
534 new StoreInst(BufPtr, JBListHead, UnwindHandler);
535 } else {
536 BufPtr = new LoadInst(JBListHead, "ehlist", UnwindHandler);
537 }
538
539 // Load the JBList, if it's null, then there was no catch!
540 Value *NotNull = BinaryOperator::createSetNE(BufPtr,
541 Constant::getNullValue(BufPtr->getType()),
542 "notnull", UnwindHandler);
543 new BranchInst(UnwindBlock, TermBlock, NotNull, UnwindHandler);
544
545 // Create the block to do the longjmp.
546 // Get a pointer to the jmpbuf and longjmp.
547 std::vector<Value*> Idx;
548 Idx.push_back(Constant::getNullValue(Type::IntTy));
549 Idx.push_back(ConstantUInt::get(Type::UIntTy, 0));
550 Idx[0] = new GetElementPtrInst(BufPtr, Idx, "JmpBuf", UnwindBlock);
551 Idx[1] = ConstantInt::get(Type::IntTy, 1);
552 new CallInst(LongJmpFn, Idx, "", UnwindBlock);
553 new UnreachableInst(UnwindBlock);
554
555 // Set up the term block ("throw without a catch").
556 new UnreachableInst(TermBlock);
557
558 // Insert a new call to write(2, AbortMessage, AbortMessageLength);
559 writeAbortMessage(TermBlock->getTerminator());
560
561 // Insert a call to abort()
562 (new CallInst(AbortFn, std::vector<Value*>(), "",
563 TermBlock->getTerminator()))->setTailCall();
564
565
566 // Replace all unwinds with a branch to the unwind handler.
567 for (unsigned i = 0, e = Unwinds.size(); i != e; ++i) {
568 new BranchInst(UnwindHandler, Unwinds[i]);
569 Unwinds[i]->eraseFromParent();
570 }
571
572 // Finally, for any returns from this function, if this function contains an
573 // invoke, restore the old jmpbuf pointer to its input value.
574 if (OldJmpBufPtr) {
575 for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
576 ReturnInst *R = Returns[i];
577
578 // Before the return, insert a copy from the saved value to the new value.
579 Value *OldBuf = new LoadInst(OldJmpBufPtr, "oldjmpbufptr", true, R);
580 new StoreInst(OldBuf, JBListHead, true, R);
581 }
582 }
583
584 return true;
Chris Lattner6d784572004-02-08 19:53:56 +0000585}
586
587bool LowerInvoke::runOnFunction(Function &F) {
588 if (ExpensiveEHSupport)
589 return insertExpensiveEHSupport(F);
590 else
591 return insertCheapEHSupport(F);
592}