blob: 4fbd43c6f4dcdcf92429fe98ee38b963b96a00cd [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"
Duraid Madina2a0013f2006-09-04 06:21:35 +000048#include "llvm/Target/TargetLowering.h"
Chris Lattner6d784572004-02-08 19:53:56 +000049#include <csetjmp>
Chris Lattnerdead9932003-12-10 20:22:42 +000050using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000051
Chris Lattner86e44452003-10-05 19:14:42 +000052namespace {
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000053 Statistic NumInvokes("lowerinvoke", "Number of invokes replaced");
54 Statistic NumUnwinds("lowerinvoke", "Number of unwinds replaced");
55 Statistic NumSpilled("lowerinvoke",
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +000056 "Number of registers live across unwind edges");
Chris Lattner99cca7d2004-03-01 01:12:13 +000057 cl::opt<bool> ExpensiveEHSupport("enable-correct-eh-support",
Chris Lattner6d784572004-02-08 19:53:56 +000058 cl::desc("Make the -lowerinvoke pass insert expensive, but correct, EH code"));
Chris Lattner86e44452003-10-05 19:14:42 +000059
Chris Lattnerf4b54612006-06-28 22:08:15 +000060 class VISIBILITY_HIDDEN LowerInvoke : public FunctionPass {
Chris Lattner6d784572004-02-08 19:53:56 +000061 // Used for both models.
Chris Lattnere1c09302004-02-08 07:30:29 +000062 Function *WriteFn;
Chris Lattner86e44452003-10-05 19:14:42 +000063 Function *AbortFn;
Chris Lattner6d784572004-02-08 19:53:56 +000064 Value *AbortMessage;
65 unsigned AbortMessageLength;
66
67 // Used for expensive EH support.
68 const Type *JBLinkTy;
69 GlobalVariable *JBListHead;
70 Function *SetJmpFn, *LongJmpFn;
Duraid Madina2a0013f2006-09-04 06:21:35 +000071
72 // We peek in TLI to grab the target's jmp_buf size and alignment
73 const TargetLowering *TLI;
74
Chris Lattner86e44452003-10-05 19:14:42 +000075 public:
Duraid Madina2a0013f2006-09-04 06:21:35 +000076 LowerInvoke(const TargetLowering *tli = NULL) : TLI(tli) { }
Chris Lattner86e44452003-10-05 19:14:42 +000077 bool doInitialization(Module &M);
78 bool runOnFunction(Function &F);
Chris Lattnered96fe82006-05-17 21:05:27 +000079
80 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
81 // This is a cluster of orthogonal Transforms
82 AU.addPreservedID(PromoteMemoryToRegisterID);
83 AU.addPreservedID(LowerSelectID);
84 AU.addPreservedID(LowerSwitchID);
85 AU.addPreservedID(LowerAllocationsID);
86 }
87
Chris Lattner6d784572004-02-08 19:53:56 +000088 private:
Chris Lattner0c3b3902004-11-13 19:07:32 +000089 void createAbortMessage();
Chris Lattner501825e2004-02-08 22:14:44 +000090 void writeAbortMessage(Instruction *IB);
Chris Lattner6d784572004-02-08 19:53:56 +000091 bool insertCheapEHSupport(Function &F);
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +000092 void splitLiveRangesLiveAcrossInvokes(std::vector<InvokeInst*> &Invokes);
93 void rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo,
94 AllocaInst *InvokeNum, SwitchInst *CatchSwitch);
Chris Lattner6d784572004-02-08 19:53:56 +000095 bool insertExpensiveEHSupport(Function &F);
Chris Lattner86e44452003-10-05 19:14:42 +000096 };
97
Chris Lattner7f8897f2006-08-27 22:42:52 +000098 RegisterPass<LowerInvoke>
Chris Lattner86e44452003-10-05 19:14:42 +000099 X("lowerinvoke", "Lower invoke and unwind, for unwindless code generators");
100}
101
Chris Lattnercefc18e2004-02-13 16:16:16 +0000102const PassInfo *llvm::LowerInvokePassID = X.getPassInfo();
103
Brian Gaeked0fde302003-11-11 22:41:34 +0000104// Public Interface To the LowerInvoke pass.
Duraid Madina2a0013f2006-09-04 06:21:35 +0000105FunctionPass *llvm::createLowerInvokePass(const TargetLowering *TLI) {
106 return new LowerInvoke(TLI);
Nate Begeman14b05292005-11-05 09:21:28 +0000107}
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.
Chris Lattner97d2dbd2006-09-05 17:48:07 +0000116 unsigned JBSize = TLI ? TLI->getJumpBufSize() : 0;
117 JBSize = JBSize ? JBSize : 200;
118 const Type *JmpBufTy = ArrayType::get(VoidPtrTy, JBSize);
Chris Lattnere1c09302004-02-08 07:30:29 +0000119
Chris Lattner6d784572004-02-08 19:53:56 +0000120 { // The type is recursive, so use a type holder.
121 std::vector<const Type*> Elements;
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000122 Elements.push_back(JmpBufTy);
Chris Lattner6d784572004-02-08 19:53:56 +0000123 OpaqueType *OT = OpaqueType::get();
124 Elements.push_back(PointerType::get(OT));
Chris Lattner6d784572004-02-08 19:53:56 +0000125 PATypeHolder JBLType(StructType::get(Elements));
126 OT->refineAbstractTypeTo(JBLType.get()); // Complete the cycle.
127 JBLinkTy = JBLType.get();
Chris Lattner11b9be52004-05-28 05:02:13 +0000128 M.addTypeName("llvm.sjljeh.jmpbufty", JBLinkTy);
Chris Lattner6d784572004-02-08 19:53:56 +0000129 }
130
131 const Type *PtrJBList = PointerType::get(JBLinkTy);
132
133 // Now that we've done that, insert the jmpbuf list head global, unless it
134 // already exists.
Chris Lattner97d2dbd2006-09-05 17:48:07 +0000135 if (!(JBListHead = M.getGlobalVariable("llvm.sjljeh.jblist", PtrJBList))) {
Chris Lattner6d784572004-02-08 19:53:56 +0000136 JBListHead = new GlobalVariable(PtrJBList, false,
137 GlobalValue::LinkOnceLinkage,
138 Constant::getNullValue(PtrJBList),
139 "llvm.sjljeh.jblist", &M);
Chris Lattner97d2dbd2006-09-05 17:48:07 +0000140 }
Chris Lattner860a1612004-02-15 22:24:27 +0000141 SetJmpFn = M.getOrInsertFunction("llvm.setjmp", Type::IntTy,
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000142 PointerType::get(JmpBufTy), (Type *)0);
Chris Lattner860a1612004-02-15 22:24:27 +0000143 LongJmpFn = M.getOrInsertFunction("llvm.longjmp", Type::VoidTy,
Chris Lattner6d784572004-02-08 19:53:56 +0000144 PointerType::get(JmpBufTy),
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000145 Type::IntTy, (Type *)0);
Chris Lattner6d784572004-02-08 19:53:56 +0000146 }
147
148 // We need the 'write' and 'abort' functions for both models.
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000149 AbortFn = M.getOrInsertFunction("abort", Type::VoidTy, (Type *)0);
Chris Lattner501825e2004-02-08 22:14:44 +0000150
151 // Unfortunately, 'write' can end up being prototyped in several different
152 // ways. If the user defines a three (or more) operand function named 'write'
Misha Brukmanb9806e02004-02-08 22:27:33 +0000153 // we will use their prototype. We _do not_ want to insert another instance
Chris Lattner501825e2004-02-08 22:14:44 +0000154 // of a write prototype, because we don't know that the funcresolve pass will
155 // run after us. If there is a definition of a write function, but it's not
156 // suitable for our uses, we just don't emit write calls. If there is no
157 // write prototype at all, we just add one.
158 if (Function *WF = M.getNamedFunction("write")) {
159 if (WF->getFunctionType()->getNumParams() > 3 ||
160 WF->getFunctionType()->isVarArg())
161 WriteFn = WF;
162 else
163 WriteFn = 0;
164 } else {
165 WriteFn = M.getOrInsertFunction("write", Type::VoidTy, Type::IntTy,
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000166 VoidPtrTy, Type::IntTy, (Type *)0);
Chris Lattner501825e2004-02-08 22:14:44 +0000167 }
Chris Lattner86e44452003-10-05 19:14:42 +0000168 return true;
169}
170
Chris Lattner0c3b3902004-11-13 19:07:32 +0000171void LowerInvoke::createAbortMessage() {
172 Module &M = *WriteFn->getParent();
173 if (ExpensiveEHSupport) {
174 // The abort message for expensive EH support tells the user that the
175 // program 'unwound' without an 'invoke' instruction.
176 Constant *Msg =
177 ConstantArray::get("ERROR: Exception thrown, but not caught!\n");
178 AbortMessageLength = Msg->getNumOperands()-1; // don't include \0
Misha Brukmanfd939082005-04-21 23:48:37 +0000179
Chris Lattner0c3b3902004-11-13 19:07:32 +0000180 GlobalVariable *MsgGV = new GlobalVariable(Msg->getType(), true,
181 GlobalValue::InternalLinkage,
182 Msg, "abortmsg", &M);
Chris Lattner1381dd82005-05-13 06:10:12 +0000183 std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::IntTy));
Chris Lattner0c3b3902004-11-13 19:07:32 +0000184 AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, GEPIdx);
185 } else {
186 // The abort message for cheap EH support tells the user that EH is not
187 // enabled.
188 Constant *Msg =
189 ConstantArray::get("Exception handler needed, but not enabled. Recompile"
190 " program with -enable-correct-eh-support.\n");
191 AbortMessageLength = Msg->getNumOperands()-1; // don't include \0
192
193 GlobalVariable *MsgGV = new GlobalVariable(Msg->getType(), true,
194 GlobalValue::InternalLinkage,
195 Msg, "abortmsg", &M);
Chris Lattner1381dd82005-05-13 06:10:12 +0000196 std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::IntTy));
Chris Lattner0c3b3902004-11-13 19:07:32 +0000197 AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, GEPIdx);
198 }
199}
200
201
Chris Lattner501825e2004-02-08 22:14:44 +0000202void LowerInvoke::writeAbortMessage(Instruction *IB) {
203 if (WriteFn) {
Chris Lattner0c3b3902004-11-13 19:07:32 +0000204 if (AbortMessage == 0) createAbortMessage();
205
Chris Lattner501825e2004-02-08 22:14:44 +0000206 // These are the arguments we WANT...
207 std::vector<Value*> Args;
208 Args.push_back(ConstantInt::get(Type::IntTy, 2));
209 Args.push_back(AbortMessage);
210 Args.push_back(ConstantInt::get(Type::IntTy, AbortMessageLength));
211
212 // If the actual declaration of write disagrees, insert casts as
213 // appropriate.
214 const FunctionType *FT = WriteFn->getFunctionType();
215 unsigned NumArgs = FT->getNumParams();
216 for (unsigned i = 0; i != 3; ++i)
217 if (i < NumArgs && FT->getParamType(i) != Args[i]->getType())
Reid Spencer4da49122006-12-12 05:05:00 +0000218 if (Args[i]->getType()->isInteger())
219 Args[i] = ConstantExpr::getIntegerCast(cast<Constant>(Args[i]),
220 FT->getParamType(i), true);
221 else
222 Args[i] = ConstantExpr::getBitCast(cast<Constant>(Args[i]),
223 FT->getParamType(i));
Chris Lattner501825e2004-02-08 22:14:44 +0000224
Chris Lattnera9e92112005-05-06 06:48:21 +0000225 (new CallInst(WriteFn, Args, "", IB))->setTailCall();
Chris Lattner501825e2004-02-08 22:14:44 +0000226 }
227}
228
Chris Lattner6d784572004-02-08 19:53:56 +0000229bool LowerInvoke::insertCheapEHSupport(Function &F) {
Chris Lattner86e44452003-10-05 19:14:42 +0000230 bool Changed = false;
Chris Lattnerdead9932003-12-10 20:22:42 +0000231 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
232 if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
Chris Lattner86e44452003-10-05 19:14:42 +0000233 // Insert a normal call instruction...
234 std::string Name = II->getName(); II->setName("");
Chris Lattnerefd91682005-05-13 06:27:02 +0000235 CallInst *NewCall = new CallInst(II->getCalledValue(),
236 std::vector<Value*>(II->op_begin()+3,
237 II->op_end()), Name, II);
238 NewCall->setCallingConv(II->getCallingConv());
Chris Lattner86e44452003-10-05 19:14:42 +0000239 II->replaceAllUsesWith(NewCall);
Misha Brukmanfd939082005-04-21 23:48:37 +0000240
Chris Lattnerdead9932003-12-10 20:22:42 +0000241 // Insert an unconditional branch to the normal destination.
Chris Lattner86e44452003-10-05 19:14:42 +0000242 new BranchInst(II->getNormalDest(), II);
243
Chris Lattnerdead9932003-12-10 20:22:42 +0000244 // Remove any PHI node entries from the exception destination.
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000245 II->getUnwindDest()->removePredecessor(BB);
Chris Lattnerdead9932003-12-10 20:22:42 +0000246
Chris Lattner86e44452003-10-05 19:14:42 +0000247 // Remove the invoke instruction now.
Chris Lattnerdead9932003-12-10 20:22:42 +0000248 BB->getInstList().erase(II);
Chris Lattner86e44452003-10-05 19:14:42 +0000249
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000250 ++NumInvokes; Changed = true;
Chris Lattnerdead9932003-12-10 20:22:42 +0000251 } else if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
Chris Lattnere1c09302004-02-08 07:30:29 +0000252 // Insert a new call to write(2, AbortMessage, AbortMessageLength);
Chris Lattner501825e2004-02-08 22:14:44 +0000253 writeAbortMessage(UI);
Chris Lattnere1c09302004-02-08 07:30:29 +0000254
Chris Lattner86e44452003-10-05 19:14:42 +0000255 // Insert a call to abort()
Chris Lattnera9e92112005-05-06 06:48:21 +0000256 (new CallInst(AbortFn, std::vector<Value*>(), "", UI))->setTailCall();
Chris Lattner86e44452003-10-05 19:14:42 +0000257
Chris Lattner6d784572004-02-08 19:53:56 +0000258 // Insert a return instruction. This really should be a "barrier", as it
259 // is unreachable.
Chris Lattner86e44452003-10-05 19:14:42 +0000260 new ReturnInst(F.getReturnType() == Type::VoidTy ? 0 :
261 Constant::getNullValue(F.getReturnType()), UI);
262
263 // Remove the unwind instruction now.
Chris Lattnerdead9932003-12-10 20:22:42 +0000264 BB->getInstList().erase(UI);
Chris Lattner86e44452003-10-05 19:14:42 +0000265
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000266 ++NumUnwinds; Changed = true;
Chris Lattner86e44452003-10-05 19:14:42 +0000267 }
268 return Changed;
269}
Chris Lattner6d784572004-02-08 19:53:56 +0000270
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000271/// rewriteExpensiveInvoke - Insert code and hack the function to replace the
272/// specified invoke instruction with a call.
273void LowerInvoke::rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo,
274 AllocaInst *InvokeNum,
275 SwitchInst *CatchSwitch) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000276 ConstantInt *InvokeNoC = ConstantInt::get(Type::UIntTy, InvokeNo);
Chris Lattner6d784572004-02-08 19:53:56 +0000277
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000278 // Insert a store of the invoke num before the invoke and store zero into the
279 // location afterward.
280 new StoreInst(InvokeNoC, InvokeNum, true, II); // volatile
Chris Lattner93e50ce2005-09-29 17:44:20 +0000281
282 BasicBlock::iterator NI = II->getNormalDest()->begin();
283 while (isa<PHINode>(NI)) ++NI;
284 // nonvolatile.
285 new StoreInst(Constant::getNullValue(Type::UIntTy), InvokeNum, false, NI);
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000286
287 // Add a switch case to our unwind block.
288 CatchSwitch->addCase(InvokeNoC, II->getUnwindDest());
289
290 // Insert a normal call instruction.
291 std::string Name = II->getName(); II->setName("");
292 CallInst *NewCall = new CallInst(II->getCalledValue(),
293 std::vector<Value*>(II->op_begin()+3,
294 II->op_end()), Name,
295 II);
296 NewCall->setCallingConv(II->getCallingConv());
297 II->replaceAllUsesWith(NewCall);
298
299 // Replace the invoke with an uncond branch.
300 new BranchInst(II->getNormalDest(), NewCall->getParent());
301 II->eraseFromParent();
302}
Chris Lattner6d784572004-02-08 19:53:56 +0000303
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000304/// MarkBlocksLiveIn - Insert BB and all of its predescessors into LiveBBs until
305/// we reach blocks we've already seen.
306static void MarkBlocksLiveIn(BasicBlock *BB, std::set<BasicBlock*> &LiveBBs) {
307 if (!LiveBBs.insert(BB).second) return; // already been here.
308
309 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
310 MarkBlocksLiveIn(*PI, LiveBBs);
311}
Chris Lattner6d784572004-02-08 19:53:56 +0000312
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000313// First thing we need to do is scan the whole function for values that are
314// live across unwind edges. Each value that is live across an unwind edge
315// we spill into a stack location, guaranteeing that there is nothing live
316// across the unwind edge. This process also splits all critical edges
317// coming out of invoke's.
318void LowerInvoke::
319splitLiveRangesLiveAcrossInvokes(std::vector<InvokeInst*> &Invokes) {
320 // First step, split all critical edges from invoke instructions.
321 for (unsigned i = 0, e = Invokes.size(); i != e; ++i) {
322 InvokeInst *II = Invokes[i];
323 SplitCriticalEdge(II, 0, this);
324 SplitCriticalEdge(II, 1, this);
325 assert(!isa<PHINode>(II->getNormalDest()) &&
326 !isa<PHINode>(II->getUnwindDest()) &&
327 "critical edge splitting left single entry phi nodes?");
Chris Lattner6d784572004-02-08 19:53:56 +0000328 }
329
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000330 Function *F = Invokes.back()->getParent()->getParent();
331
332 // To avoid having to handle incoming arguments specially, we lower each arg
Reid Spencer3da59db2006-11-27 01:05:10 +0000333 // to a copy instruction in the entry block. This ensures that the argument
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000334 // value itself cannot be live across the entry block.
335 BasicBlock::iterator AfterAllocaInsertPt = F->begin()->begin();
336 while (isa<AllocaInst>(AfterAllocaInsertPt) &&
337 isa<ConstantInt>(cast<AllocaInst>(AfterAllocaInsertPt)->getArraySize()))
338 ++AfterAllocaInsertPt;
339 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
340 AI != E; ++AI) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000341 // This is always a no-op cast because we're casting AI to AI->getType() so
342 // src and destination types are identical. BitCast is the only possibility.
343 CastInst *NC = new BitCastInst(
344 AI, AI->getType(), AI->getName()+".tmp", AfterAllocaInsertPt);
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000345 AI->replaceAllUsesWith(NC);
Reid Spencer3da59db2006-11-27 01:05:10 +0000346 // Normally its is forbidden to replace a CastInst's operand because it
347 // could cause the opcode to reflect an illegal conversion. However, we're
348 // replacing it here with the same value it was constructed with to simply
349 // make NC its user.
350 NC->setOperand(0, AI);
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000351 }
352
353 // Finally, scan the code looking for instructions with bad live ranges.
354 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
355 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ++II) {
356 // Ignore obvious cases we don't have to handle. In particular, most
357 // instructions either have no uses or only have a single use inside the
358 // current block. Ignore them quickly.
359 Instruction *Inst = II;
360 if (Inst->use_empty()) continue;
361 if (Inst->hasOneUse() &&
362 cast<Instruction>(Inst->use_back())->getParent() == BB &&
363 !isa<PHINode>(Inst->use_back())) continue;
364
Chris Lattner45313712005-09-27 21:33:12 +0000365 // If this is an alloca in the entry block, it's not a real register
366 // value.
367 if (AllocaInst *AI = dyn_cast<AllocaInst>(Inst))
368 if (isa<ConstantInt>(AI->getArraySize()) && BB == F->begin())
369 continue;
370
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000371 // Avoid iterator invalidation by copying users to a temporary vector.
372 std::vector<Instruction*> Users;
373 for (Value::use_iterator UI = Inst->use_begin(), E = Inst->use_end();
374 UI != E; ++UI) {
375 Instruction *User = cast<Instruction>(*UI);
376 if (User->getParent() != BB || isa<PHINode>(User))
377 Users.push_back(User);
378 }
379
380 // Scan all of the uses and see if the live range is live across an unwind
381 // edge. If we find a use live across an invoke edge, create an alloca
382 // and spill the value.
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000383 std::set<InvokeInst*> InvokesWithStoreInserted;
384
385 // Find all of the blocks that this value is live in.
386 std::set<BasicBlock*> LiveBBs;
387 LiveBBs.insert(Inst->getParent());
388 while (!Users.empty()) {
389 Instruction *U = Users.back();
390 Users.pop_back();
391
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000392 if (!isa<PHINode>(U)) {
393 MarkBlocksLiveIn(U->getParent(), LiveBBs);
394 } else {
395 // Uses for a PHI node occur in their predecessor block.
396 PHINode *PN = cast<PHINode>(U);
397 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
398 if (PN->getIncomingValue(i) == Inst)
399 MarkBlocksLiveIn(PN->getIncomingBlock(i), LiveBBs);
400 }
401 }
402
403 // Now that we know all of the blocks that this thing is live in, see if
404 // it includes any of the unwind locations.
405 bool NeedsSpill = false;
406 for (unsigned i = 0, e = Invokes.size(); i != e; ++i) {
407 BasicBlock *UnwindBlock = Invokes[i]->getUnwindDest();
408 if (UnwindBlock != BB && LiveBBs.count(UnwindBlock)) {
409 NeedsSpill = true;
410 }
411 }
412
413 // If we decided we need a spill, do it.
414 if (NeedsSpill) {
415 ++NumSpilled;
416 DemoteRegToStack(*Inst, true);
417 }
418 }
419}
420
421bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
422 std::vector<ReturnInst*> Returns;
423 std::vector<UnwindInst*> Unwinds;
424 std::vector<InvokeInst*> Invokes;
425
426 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
427 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
428 // Remember all return instructions in case we insert an invoke into this
429 // function.
430 Returns.push_back(RI);
431 } else if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
432 Invokes.push_back(II);
433 } else if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
434 Unwinds.push_back(UI);
435 }
436
437 if (Unwinds.empty() && Invokes.empty()) return false;
438
439 NumInvokes += Invokes.size();
440 NumUnwinds += Unwinds.size();
Chris Lattner5b3c7022005-09-27 22:44:59 +0000441
442 // TODO: This is not an optimal way to do this. In particular, this always
443 // inserts setjmp calls into the entries of functions with invoke instructions
444 // even though there are possibly paths through the function that do not
445 // execute any invokes. In particular, for functions with early exits, e.g.
446 // the 'addMove' method in hexxagon, it would be nice to not have to do the
447 // setjmp stuff on the early exit path. This requires a bit of dataflow, but
448 // would not be too hard to do.
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000449
450 // If we have an invoke instruction, insert a setjmp that dominates all
451 // invokes. After the setjmp, use a cond branch that goes to the original
452 // code path on zero, and to a designated 'catch' block of nonzero.
453 Value *OldJmpBufPtr = 0;
454 if (!Invokes.empty()) {
455 // First thing we need to do is scan the whole function for values that are
456 // live across unwind edges. Each value that is live across an unwind edge
457 // we spill into a stack location, guaranteeing that there is nothing live
458 // across the unwind edge. This process also splits all critical edges
459 // coming out of invoke's.
460 splitLiveRangesLiveAcrossInvokes(Invokes);
461
462 BasicBlock *EntryBB = F.begin();
463
464 // Create an alloca for the incoming jump buffer ptr and the new jump buffer
465 // that needs to be restored on all exits from the function. This is an
466 // alloca because the value needs to be live across invokes.
Chris Lattner97d2dbd2006-09-05 17:48:07 +0000467 unsigned Align = TLI ? TLI->getJumpBufAlignment() : 0;
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000468 AllocaInst *JmpBuf =
Chris Lattner97d2dbd2006-09-05 17:48:07 +0000469 new AllocaInst(JBLinkTy, 0, Align, "jblink", F.begin()->begin());
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000470
471 std::vector<Value*> Idx;
472 Idx.push_back(Constant::getNullValue(Type::IntTy));
Reid Spencerb83eb642006-10-20 07:07:24 +0000473 Idx.push_back(ConstantInt::get(Type::UIntTy, 1));
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000474 OldJmpBufPtr = new GetElementPtrInst(JmpBuf, Idx, "OldBuf",
475 EntryBB->getTerminator());
476
477 // Copy the JBListHead to the alloca.
478 Value *OldBuf = new LoadInst(JBListHead, "oldjmpbufptr", true,
479 EntryBB->getTerminator());
480 new StoreInst(OldBuf, OldJmpBufPtr, true, EntryBB->getTerminator());
481
482 // Add the new jumpbuf to the list.
483 new StoreInst(JmpBuf, JBListHead, true, EntryBB->getTerminator());
484
485 // Create the catch block. The catch block is basically a big switch
486 // statement that goes to all of the invoke catch blocks.
487 BasicBlock *CatchBB = new BasicBlock("setjmp.catch", &F);
488
489 // Create an alloca which keeps track of which invoke is currently
490 // executing. For normal calls it contains zero.
491 AllocaInst *InvokeNum = new AllocaInst(Type::UIntTy, 0, "invokenum",
492 EntryBB->begin());
493 new StoreInst(ConstantInt::get(Type::UIntTy, 0), InvokeNum, true,
494 EntryBB->getTerminator());
495
496 // Insert a load in the Catch block, and a switch on its value. By default,
497 // we go to a block that just does an unwind (which is the correct action
498 // for a standard call).
499 BasicBlock *UnwindBB = new BasicBlock("unwindbb", &F);
500 Unwinds.push_back(new UnwindInst(UnwindBB));
501
502 Value *CatchLoad = new LoadInst(InvokeNum, "invoke.num", true, CatchBB);
503 SwitchInst *CatchSwitch =
504 new SwitchInst(CatchLoad, UnwindBB, Invokes.size(), CatchBB);
505
506 // Now that things are set up, insert the setjmp call itself.
507
508 // Split the entry block to insert the conditional branch for the setjmp.
509 BasicBlock *ContBlock = EntryBB->splitBasicBlock(EntryBB->getTerminator(),
510 "setjmp.cont");
511
Reid Spencerb83eb642006-10-20 07:07:24 +0000512 Idx[1] = ConstantInt::get(Type::UIntTy, 0);
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000513 Value *JmpBufPtr = new GetElementPtrInst(JmpBuf, Idx, "TheJmpBuf",
514 EntryBB->getTerminator());
515 Value *SJRet = new CallInst(SetJmpFn, JmpBufPtr, "sjret",
516 EntryBB->getTerminator());
517
518 // Compare the return value to zero.
519 Value *IsNormal = BinaryOperator::createSetEQ(SJRet,
520 Constant::getNullValue(SJRet->getType()),
521 "notunwind", EntryBB->getTerminator());
522 // Nuke the uncond branch.
523 EntryBB->getTerminator()->eraseFromParent();
524
525 // Put in a new condbranch in its place.
526 new BranchInst(ContBlock, CatchBB, IsNormal, EntryBB);
527
528 // At this point, we are all set up, rewrite each invoke instruction.
529 for (unsigned i = 0, e = Invokes.size(); i != e; ++i)
530 rewriteExpensiveInvoke(Invokes[i], i+1, InvokeNum, CatchSwitch);
531 }
532
533 // We know that there is at least one unwind.
534
535 // Create three new blocks, the block to load the jmpbuf ptr and compare
536 // against null, the block to do the longjmp, and the error block for if it
537 // is null. Add them at the end of the function because they are not hot.
538 BasicBlock *UnwindHandler = new BasicBlock("dounwind", &F);
539 BasicBlock *UnwindBlock = new BasicBlock("unwind", &F);
540 BasicBlock *TermBlock = new BasicBlock("unwinderror", &F);
541
542 // If this function contains an invoke, restore the old jumpbuf ptr.
543 Value *BufPtr;
544 if (OldJmpBufPtr) {
545 // Before the return, insert a copy from the saved value to the new value.
546 BufPtr = new LoadInst(OldJmpBufPtr, "oldjmpbufptr", UnwindHandler);
547 new StoreInst(BufPtr, JBListHead, UnwindHandler);
548 } else {
549 BufPtr = new LoadInst(JBListHead, "ehlist", UnwindHandler);
550 }
551
552 // Load the JBList, if it's null, then there was no catch!
553 Value *NotNull = BinaryOperator::createSetNE(BufPtr,
554 Constant::getNullValue(BufPtr->getType()),
555 "notnull", UnwindHandler);
556 new BranchInst(UnwindBlock, TermBlock, NotNull, UnwindHandler);
557
558 // Create the block to do the longjmp.
559 // Get a pointer to the jmpbuf and longjmp.
560 std::vector<Value*> Idx;
561 Idx.push_back(Constant::getNullValue(Type::IntTy));
Reid Spencerb83eb642006-10-20 07:07:24 +0000562 Idx.push_back(ConstantInt::get(Type::UIntTy, 0));
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +0000563 Idx[0] = new GetElementPtrInst(BufPtr, Idx, "JmpBuf", UnwindBlock);
564 Idx[1] = ConstantInt::get(Type::IntTy, 1);
565 new CallInst(LongJmpFn, Idx, "", UnwindBlock);
566 new UnreachableInst(UnwindBlock);
567
568 // Set up the term block ("throw without a catch").
569 new UnreachableInst(TermBlock);
570
571 // Insert a new call to write(2, AbortMessage, AbortMessageLength);
572 writeAbortMessage(TermBlock->getTerminator());
573
574 // Insert a call to abort()
575 (new CallInst(AbortFn, std::vector<Value*>(), "",
576 TermBlock->getTerminator()))->setTailCall();
577
578
579 // Replace all unwinds with a branch to the unwind handler.
580 for (unsigned i = 0, e = Unwinds.size(); i != e; ++i) {
581 new BranchInst(UnwindHandler, Unwinds[i]);
582 Unwinds[i]->eraseFromParent();
583 }
584
585 // Finally, for any returns from this function, if this function contains an
586 // invoke, restore the old jmpbuf pointer to its input value.
587 if (OldJmpBufPtr) {
588 for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
589 ReturnInst *R = Returns[i];
590
591 // Before the return, insert a copy from the saved value to the new value.
592 Value *OldBuf = new LoadInst(OldJmpBufPtr, "oldjmpbufptr", true, R);
593 new StoreInst(OldBuf, JBListHead, true, R);
594 }
595 }
596
597 return true;
Chris Lattner6d784572004-02-08 19:53:56 +0000598}
599
600bool LowerInvoke::runOnFunction(Function &F) {
601 if (ExpensiveEHSupport)
602 return insertExpensiveEHSupport(F);
603 else
604 return insertCheapEHSupport(F);
605}