blob: 331152aeafae930e1afe50e14bfbe25ab5887eca [file] [log] [blame]
Chris Lattner08227e42003-06-17 22:21:05 +00001//===-- DeadArgumentElimination.cpp - Eliminate dead arguments ------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner08227e42003-06-17 22:21:05 +00009//
10// This pass deletes dead arguments from internal functions. Dead argument
11// elimination removes arguments which are directly dead, as well as arguments
Chris Lattnerc3afd9b2003-10-23 03:48:17 +000012// only passed into function calls as dead arguments of other functions. This
Matthijs Kooijmanca85d652008-06-18 11:12:53 +000013// pass also deletes dead return values in a similar way.
Chris Lattner08227e42003-06-17 22:21:05 +000014//
15// This pass is often useful as a cleanup pass to run after aggressive
Matthijs Kooijmanca85d652008-06-18 11:12:53 +000016// interprocedural passes, which add possibly-dead arguments or return values.
Chris Lattner08227e42003-06-17 22:21:05 +000017//
18//===----------------------------------------------------------------------===//
19
Chris Lattner543a0272005-06-24 16:00:46 +000020#define DEBUG_TYPE "deadargelim"
Chris Lattner08227e42003-06-17 22:21:05 +000021#include "llvm/Transforms/IPO.h"
Chris Lattner92044ce2006-06-27 21:05:04 +000022#include "llvm/CallingConv.h"
23#include "llvm/Constant.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/Instructions.h"
Chris Lattner4af90ab2006-09-18 07:02:31 +000026#include "llvm/IntrinsicInst.h"
Chris Lattner08227e42003-06-17 22:21:05 +000027#include "llvm/Module.h"
28#include "llvm/Pass.h"
Chris Lattner08227e42003-06-17 22:21:05 +000029#include "llvm/Support/CallSite.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000030#include "llvm/Support/Debug.h"
Chris Lattner58d74912008-03-12 17:45:29 +000031#include "llvm/ADT/SmallVector.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000032#include "llvm/ADT/Statistic.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000033#include "llvm/Support/Compiler.h"
Dan Gohmanc9235d22008-03-21 23:51:57 +000034#include <map>
Chris Lattner08227e42003-06-17 22:21:05 +000035#include <set>
Chris Lattner1e2385b2003-11-21 21:54:22 +000036using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000037
Chris Lattner86453c52006-12-19 22:09:18 +000038STATISTIC(NumArgumentsEliminated, "Number of unread args removed");
39STATISTIC(NumRetValsEliminated , "Number of unused return values removed");
Chris Lattner08227e42003-06-17 22:21:05 +000040
Chris Lattner86453c52006-12-19 22:09:18 +000041namespace {
Chris Lattnerc3afd9b2003-10-23 03:48:17 +000042 /// DAE - The dead argument elimination pass.
43 ///
Reid Spencer9133fe22007-02-05 23:32:05 +000044 class VISIBILITY_HIDDEN DAE : public ModulePass {
Matthijs Kooijmanca85d652008-06-18 11:12:53 +000045 public:
46
47 /// Struct that represent either a (part of a) return value or a function
48 /// argument. Used so that arguments and return values can be used
49 /// interchangably.
50 struct RetOrArg {
51 RetOrArg(const Function* F, unsigned Idx, bool IsArg) : F(F), Idx(Idx), IsArg(IsArg) {}
52 const Function *F;
53 unsigned Idx;
54 bool IsArg;
55
56 /// Make RetOrArg comparable, so we can put it into a map
57 bool operator<(const RetOrArg &O) const {
58 if (F != O.F)
59 return F < O.F;
60 else if (Idx != O.Idx)
61 return Idx < O.Idx;
62 else
63 return IsArg < O.IsArg;
64 }
65 };
66
Chris Lattnerc3afd9b2003-10-23 03:48:17 +000067 /// Liveness enum - During our initial pass over the program, we determine
68 /// that things are either definately alive, definately dead, or in need of
69 /// interprocedural analysis (MaybeLive).
70 ///
71 enum Liveness { Live, MaybeLive, Dead };
72
Matthijs Kooijmanca85d652008-06-18 11:12:53 +000073 /// Convenience wrapper
74 RetOrArg CreateRet(const Function *F, unsigned Idx) { return RetOrArg(F, Idx, false); }
75 /// Convenience wrapper
76 RetOrArg CreateArg(const Function *F, unsigned Idx) { return RetOrArg(F, Idx, true); }
Chris Lattnerc3afd9b2003-10-23 03:48:17 +000077
Matthijs Kooijmanca85d652008-06-18 11:12:53 +000078 typedef std::multimap<RetOrArg, RetOrArg> UseMap;
79 /// This map maps a return value or argument to all return values or
80 /// arguments it uses.
81 /// For example (indices are left out for clarity):
82 /// - Uses[ret F] = ret G
83 /// This means that F calls G, and F returns the value returned by G.
84 /// - Uses[arg F] = ret G
85 /// This means that some function calls G and passes its result as an
86 /// argument to F.
87 /// - Uses[ret F] = arg F
88 /// This means that F returns one of its own arguments.
89 /// - Uses[arg F] = arg G
90 /// This means that G calls F and passes one of its own (G's) arguments
91 /// directly to F.
92 UseMap Uses;
Chris Lattnerc3afd9b2003-10-23 03:48:17 +000093
Matthijs Kooijmanca85d652008-06-18 11:12:53 +000094 typedef std::set<RetOrArg> LiveSet;
Chris Lattnerc3afd9b2003-10-23 03:48:17 +000095
Matthijs Kooijmanca85d652008-06-18 11:12:53 +000096 /// This set contains all values that have been determined to be live
97 LiveSet LiveValues;
98
99 typedef SmallVector<RetOrArg, 5> UseVector;
100
101 /// This is the set of functions that have been inspected. Since LiveValues
102 /// keeps a list of live values for inspected functions only, this way we
103 /// can prevent uninspected functions becoming completely dead.
104 std::set<Function*> InspectedFunctions;
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000105
106 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +0000107 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +0000108 DAE() : ModulePass((intptr_t)&ID) {}
Chris Lattnerb12914b2004-09-20 04:48:05 +0000109 bool runOnModule(Module &M);
Chris Lattner9b2a14b2003-06-25 04:12:49 +0000110
Chris Lattnerfdcc3ac2003-11-05 21:43:02 +0000111 virtual bool ShouldHackArguments() const { return false; }
112
Chris Lattner9b2a14b2003-06-25 04:12:49 +0000113 private:
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000114 Liveness IsMaybeLive(RetOrArg Use, UseVector &MaybeLiveUses);
115 Liveness SurveyUse(Value::use_iterator U, UseVector &MaybeLiveUses, unsigned RetValNum = 0);
116 Liveness SurveyUses(Value *V, UseVector &MaybeLiveUses);
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000117
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000118 void SurveyFunction(Function &F);
119 void MarkValue(const RetOrArg &RA, Liveness L, const UseVector &MaybeLiveUses);
120 void MarkLive(RetOrArg RA);
121 bool RemoveDeadStuffFromFunction(Function *F);
Chris Lattner4af90ab2006-09-18 07:02:31 +0000122 bool DeleteDeadVarargs(Function &Fn);
Chris Lattner08227e42003-06-17 22:21:05 +0000123 };
Dan Gohman844731a2008-05-13 00:00:25 +0000124}
Chris Lattnerfdcc3ac2003-11-05 21:43:02 +0000125
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000126
Dan Gohman844731a2008-05-13 00:00:25 +0000127char DAE::ID = 0;
128static RegisterPass<DAE>
129X("deadargelim", "Dead Argument Elimination");
130
131namespace {
Chris Lattnerfdcc3ac2003-11-05 21:43:02 +0000132 /// DAH - DeadArgumentHacking pass - Same as dead argument elimination, but
133 /// deletes arguments to functions which are external. This is only for use
134 /// by bugpoint.
135 struct DAH : public DAE {
Devang Patel19974732007-05-03 01:11:54 +0000136 static char ID;
Chris Lattnerfdcc3ac2003-11-05 21:43:02 +0000137 virtual bool ShouldHackArguments() const { return true; }
138 };
Chris Lattner08227e42003-06-17 22:21:05 +0000139}
140
Dan Gohman844731a2008-05-13 00:00:25 +0000141char DAH::ID = 0;
142static RegisterPass<DAH>
143Y("deadarghaX0r", "Dead Argument Hacking (BUGPOINT USE ONLY; DO NOT USE)");
144
Chris Lattner9b2a14b2003-06-25 04:12:49 +0000145/// createDeadArgEliminationPass - This pass removes arguments from functions
Chris Lattnerfdcc3ac2003-11-05 21:43:02 +0000146/// which are not used by the body of the function.
Chris Lattner9b2a14b2003-06-25 04:12:49 +0000147///
Chris Lattnerb12914b2004-09-20 04:48:05 +0000148ModulePass *llvm::createDeadArgEliminationPass() { return new DAE(); }
149ModulePass *llvm::createDeadArgHackingPass() { return new DAH(); }
Chris Lattner08227e42003-06-17 22:21:05 +0000150
Chris Lattner4af90ab2006-09-18 07:02:31 +0000151/// DeleteDeadVarargs - If this is an function that takes a ... list, and if
152/// llvm.vastart is never called, the varargs list is dead for the function.
153bool DAE::DeleteDeadVarargs(Function &Fn) {
154 assert(Fn.getFunctionType()->isVarArg() && "Function isn't varargs!");
Reid Spencer5cbf9852007-01-30 20:08:39 +0000155 if (Fn.isDeclaration() || !Fn.hasInternalLinkage()) return false;
Duncan Sands110c8352007-12-21 19:16:16 +0000156
Chris Lattner4af90ab2006-09-18 07:02:31 +0000157 // Ensure that the function is only directly called.
158 for (Value::use_iterator I = Fn.use_begin(), E = Fn.use_end(); I != E; ++I) {
159 // If this use is anything other than a call site, give up.
160 CallSite CS = CallSite::get(*I);
161 Instruction *TheCall = CS.getInstruction();
162 if (!TheCall) return false; // Not a direct call site?
Duncan Sands110c8352007-12-21 19:16:16 +0000163
Chris Lattner4af90ab2006-09-18 07:02:31 +0000164 // The addr of this function is passed to the call.
165 if (I.getOperandNo() != 0) return false;
166 }
Duncan Sands110c8352007-12-21 19:16:16 +0000167
Chris Lattner4af90ab2006-09-18 07:02:31 +0000168 // Okay, we know we can transform this function if safe. Scan its body
169 // looking for calls to llvm.vastart.
170 for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
171 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
172 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
173 if (II->getIntrinsicID() == Intrinsic::vastart)
174 return false;
175 }
176 }
177 }
Duncan Sands110c8352007-12-21 19:16:16 +0000178
Chris Lattner4af90ab2006-09-18 07:02:31 +0000179 // If we get here, there are no calls to llvm.vastart in the function body,
180 // remove the "..." and adjust all the calls.
Duncan Sands110c8352007-12-21 19:16:16 +0000181
Chris Lattner4af90ab2006-09-18 07:02:31 +0000182 // Start by computing a new prototype for the function, which is the same as
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000183 // the old function, but doesn't have isVarArg set.
Chris Lattner4af90ab2006-09-18 07:02:31 +0000184 const FunctionType *FTy = Fn.getFunctionType();
185 std::vector<const Type*> Params(FTy->param_begin(), FTy->param_end());
186 FunctionType *NFTy = FunctionType::get(FTy->getReturnType(), Params, false);
187 unsigned NumArgs = Params.size();
Duncan Sands110c8352007-12-21 19:16:16 +0000188
Chris Lattner4af90ab2006-09-18 07:02:31 +0000189 // Create the new function body and insert it into the module...
Gabor Greif051a9502008-04-06 20:25:17 +0000190 Function *NF = Function::Create(NFTy, Fn.getLinkage());
Duncan Sands28c3cff2008-05-26 19:58:59 +0000191 NF->copyAttributesFrom(&Fn);
Chris Lattner4af90ab2006-09-18 07:02:31 +0000192 Fn.getParent()->getFunctionList().insert(&Fn, NF);
Chris Lattner046800a2007-02-11 01:08:35 +0000193 NF->takeName(&Fn);
Duncan Sands110c8352007-12-21 19:16:16 +0000194
Chris Lattner4af90ab2006-09-18 07:02:31 +0000195 // Loop over all of the callers of the function, transforming the call sites
196 // to pass in a smaller number of arguments into the new function.
197 //
198 std::vector<Value*> Args;
199 while (!Fn.use_empty()) {
200 CallSite CS = CallSite::get(Fn.use_back());
201 Instruction *Call = CS.getInstruction();
Duncan Sands110c8352007-12-21 19:16:16 +0000202
Chris Lattnera0bc7fc2007-10-18 18:49:29 +0000203 // Pass all the same arguments.
Chris Lattner4af90ab2006-09-18 07:02:31 +0000204 Args.assign(CS.arg_begin(), CS.arg_begin()+NumArgs);
Duncan Sands110c8352007-12-21 19:16:16 +0000205
Duncan Sandsbfc5ae62008-01-11 23:13:45 +0000206 // Drop any attributes that were on the vararg arguments.
Chris Lattner58d74912008-03-12 17:45:29 +0000207 PAListPtr PAL = CS.getParamAttrs();
208 if (!PAL.isEmpty() && PAL.getSlot(PAL.getNumSlots() - 1).Index > NumArgs) {
209 SmallVector<ParamAttrsWithIndex, 8> ParamAttrsVec;
210 for (unsigned i = 0; PAL.getSlot(i).Index <= NumArgs; ++i)
211 ParamAttrsVec.push_back(PAL.getSlot(i));
212 PAL = PAListPtr::get(ParamAttrsVec.begin(), ParamAttrsVec.end());
Duncan Sandsbfc5ae62008-01-11 23:13:45 +0000213 }
214
Chris Lattner4af90ab2006-09-18 07:02:31 +0000215 Instruction *New;
216 if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
Gabor Greif051a9502008-04-06 20:25:17 +0000217 New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),
218 Args.begin(), Args.end(), "", Call);
Chris Lattner4af90ab2006-09-18 07:02:31 +0000219 cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
Duncan Sandsbfc5ae62008-01-11 23:13:45 +0000220 cast<InvokeInst>(New)->setParamAttrs(PAL);
Chris Lattner4af90ab2006-09-18 07:02:31 +0000221 } else {
Gabor Greif051a9502008-04-06 20:25:17 +0000222 New = CallInst::Create(NF, Args.begin(), Args.end(), "", Call);
Chris Lattner4af90ab2006-09-18 07:02:31 +0000223 cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
Duncan Sandsbfc5ae62008-01-11 23:13:45 +0000224 cast<CallInst>(New)->setParamAttrs(PAL);
Chris Lattner4af90ab2006-09-18 07:02:31 +0000225 if (cast<CallInst>(Call)->isTailCall())
226 cast<CallInst>(New)->setTailCall();
227 }
228 Args.clear();
Duncan Sands110c8352007-12-21 19:16:16 +0000229
Chris Lattner4af90ab2006-09-18 07:02:31 +0000230 if (!Call->use_empty())
Chris Lattnera0bc7fc2007-10-18 18:49:29 +0000231 Call->replaceAllUsesWith(New);
Duncan Sands110c8352007-12-21 19:16:16 +0000232
Chris Lattner046800a2007-02-11 01:08:35 +0000233 New->takeName(Call);
Duncan Sands110c8352007-12-21 19:16:16 +0000234
Chris Lattner4af90ab2006-09-18 07:02:31 +0000235 // Finally, remove the old call from the program, reducing the use-count of
236 // F.
Chris Lattnera0bc7fc2007-10-18 18:49:29 +0000237 Call->eraseFromParent();
Chris Lattner4af90ab2006-09-18 07:02:31 +0000238 }
Duncan Sands110c8352007-12-21 19:16:16 +0000239
Chris Lattner4af90ab2006-09-18 07:02:31 +0000240 // Since we have now created the new function, splice the body of the old
241 // function right into the new function, leaving the old rotting hulk of the
242 // function empty.
243 NF->getBasicBlockList().splice(NF->begin(), Fn.getBasicBlockList());
Duncan Sands110c8352007-12-21 19:16:16 +0000244
Chris Lattner4af90ab2006-09-18 07:02:31 +0000245 // Loop over the argument list, transfering uses of the old arguments over to
246 // the new arguments, also transfering over the names as well. While we're at
247 // it, remove the dead arguments from the DeadArguments list.
248 //
249 for (Function::arg_iterator I = Fn.arg_begin(), E = Fn.arg_end(),
250 I2 = NF->arg_begin(); I != E; ++I, ++I2) {
251 // Move the name and users over to the new version.
252 I->replaceAllUsesWith(I2);
Chris Lattner046800a2007-02-11 01:08:35 +0000253 I2->takeName(I);
Chris Lattner4af90ab2006-09-18 07:02:31 +0000254 }
Duncan Sands110c8352007-12-21 19:16:16 +0000255
Chris Lattner4af90ab2006-09-18 07:02:31 +0000256 // Finally, nuke the old function.
257 Fn.eraseFromParent();
258 return true;
259}
260
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000261/// Convenience function that returns the number of return values. It returns 0
262/// for void functions and 1 for functions not returning a struct. It returns
263/// the number of struct elements for functions returning a struct.
264static unsigned NumRetVals(const Function *F) {
265 if (F->getReturnType() == Type::VoidTy)
266 return 0;
267 else if (const StructType *STy = dyn_cast<StructType>(F->getReturnType()))
268 return STy->getNumElements();
269 else
270 return 1;
Chris Lattner08227e42003-06-17 22:21:05 +0000271}
272
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000273/// IsMaybeAlive - This checks Use for liveness. If Use is live, returns Live,
274/// else returns MaybeLive. Also, adds Use to MaybeLiveUses in the latter case.
275DAE::Liveness DAE::IsMaybeLive(RetOrArg Use, UseVector &MaybeLiveUses) {
276 // We're live if our use is already marked as live
277 if (LiveValues.count(Use))
Chris Lattner92044ce2006-06-27 21:05:04 +0000278 return Live;
Chris Lattner08227e42003-06-17 22:21:05 +0000279
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000280 // We're maybe live otherwise, but remember that we must become live if
281 // Use becomes live.
282 MaybeLiveUses.push_back(Use);
283 return MaybeLive;
Chris Lattner08227e42003-06-17 22:21:05 +0000284}
285
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000286
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000287/// SurveyUse - This looks at a single use of an argument or return value
288/// and determines if it should be alive or not. Adds this use to MaybeLiveUses
289/// if it causes the used value to become MaybeAlive.
290///
291/// RetValNum is the return value number to use when this use is used in a
292/// return instruction. This is used in the recursion, you should always leave
293/// it at 0.
294DAE::Liveness DAE::SurveyUse(Value::use_iterator U, UseVector &MaybeLiveUses, unsigned RetValNum) {
295 Value *V = *U;
296 if (ReturnInst *RI = dyn_cast<ReturnInst>(V)) {
297 // The value is returned from another function. It's only live when the
298 // caller's return value is live
299 RetOrArg Use = CreateRet(RI->getParent()->getParent(), RetValNum);
300 // We might be live, depending on the liveness of Use
301 return IsMaybeLive(Use, MaybeLiveUses);
302 }
303 if (InsertValueInst *IV = dyn_cast<InsertValueInst>(V)) {
304 if (U.getOperandNo() != InsertValueInst::getAggregateOperandIndex() && IV->hasIndices())
305 // The use we are examining is inserted into an aggregate. Our liveness
306 // depends on all uses of that aggregate, but if it is used as a return
307 // value, only index at which we were inserted counts.
308 RetValNum = *IV->idx_begin();
309
310 // Note that if we are used as the aggregate operand to the insertvalue,
311 // we don't change RetValNum, but do survey all our uses.
312
313 Liveness Result = Dead;
314 for (Value::use_iterator I = IV->use_begin(),
315 E = V->use_end(); I != E; ++I) {
316 Result = SurveyUse(I, MaybeLiveUses, RetValNum);
317 if (Result == Live)
318 break;
319 }
320 return Result;
321 }
322 CallSite CS = CallSite::get(V);
323 if (CS.getInstruction()) {
324 Function *F = CS.getCalledFunction();
325 if (F) {
326 // Used in a direct call
327
328 // Check for vararg. Do - 1 to skip the first operand to call (the
329 // function itself).
330 if (U.getOperandNo() - 1 >= F->getFunctionType()->getNumParams())
331 // The value is passed in through a vararg! Must be live.
332 return Live;
333
334 // Value passed to a normal call. It's only live when the corresponding
335 // argument (operand number - 1 to skip the function pointer operand) to
336 // the called function turns out live
337 RetOrArg Use = CreateArg(F, U.getOperandNo() - 1);
338 return IsMaybeLive(Use, MaybeLiveUses);
339 } else {
340 // Used in any other way? Value must be live.
341 return Live;
342 }
343 }
344 // Used in any other way? Value must be live.
345 return Live;
346}
347
348/// SurveyUses - This looks at all the uses of the given return value
349/// (possibly a partial return value from a function returning a struct).
350/// Returns the Liveness deduced from the uses of this value.
351///
352/// Adds all uses that cause the result to be MaybeLive to MaybeLiveRetUses.
353DAE::Liveness DAE::SurveyUses(Value *V, UseVector &MaybeLiveUses) {
354 // Assume it's dead (which will only hold if there are no uses at all..)
355 Liveness Result = Dead;
356 // Check each use
357 for (Value::use_iterator I = V->use_begin(),
358 E = V->use_end(); I != E; ++I) {
359 Result = SurveyUse(I, MaybeLiveUses);
360 if (Result == Live)
361 break;
362 }
363 return Result;
364}
365
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000366// SurveyFunction - This performs the initial survey of the specified function,
367// checking out whether or not it uses any of its incoming arguments or whether
368// any callers use the return value. This fills in the
369// (Dead|MaybeLive|Live)(Arguments|RetVal) sets.
Chris Lattner08227e42003-06-17 22:21:05 +0000370//
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000371// We consider arguments of non-internal functions to be intrinsically alive as
372// well as arguments to functions which have their "address taken".
373//
374void DAE::SurveyFunction(Function &F) {
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000375 InspectedFunctions.insert(&F);
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000376 bool FunctionIntrinsicallyLive = false;
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000377 unsigned RetCount = NumRetVals(&F);
378 // Assume all return values are dead
379 typedef SmallVector<Liveness, 5> RetVals;
380 RetVals RetValLiveness(RetCount, Dead);
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000381
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000382 // These vectors maps each return value to the uses that make it MaybeLive, so
383 // we can add those to the MaybeLiveRetVals list if the return value
384 // really turns out to be MaybeLive. Initializes to RetCount empty vectors
385 typedef SmallVector<UseVector, 5> RetUses;
386 // Intialized to a list of RetCount empty lists
387 RetUses MaybeLiveRetUses(RetCount);
388
389 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
390 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()))
391 if (RI->getNumOperands() != 0 && RI->getOperand(0)->getType() != F.getFunctionType()->getReturnType()) {
392 // We don't support old style multiple return values
393 FunctionIntrinsicallyLive = true;
394 break;
395 }
396 if (!F.hasInternalLinkage() && (!ShouldHackArguments() || F.isIntrinsic()))
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000397 FunctionIntrinsicallyLive = true;
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000398 if (!FunctionIntrinsicallyLive) {
399 DOUT << "DAE - Inspecting callers for fn: " << F.getName() << "\n";
400 // Keep track of the number of live retvals, so we can skip checks once all
401 // of them turn out to be live.
402 unsigned NumLiveRetVals = 0;
403 const Type *STy = dyn_cast<StructType>(F.getReturnType());
404 // Loop all uses of the function
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000405 for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I) {
Matthijs Kooijman41335412008-06-05 08:34:25 +0000406 // If the function is PASSED IN as an argument, its address has been taken
407 if (I.getOperandNo() != 0) {
408 FunctionIntrinsicallyLive = true;
409 break;
410 }
411
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000412 // If this use is anything other than a call site, the function is alive.
413 CallSite CS = CallSite::get(*I);
414 Instruction *TheCall = CS.getInstruction();
415 if (!TheCall) { // Not a direct call site?
416 FunctionIntrinsicallyLive = true;
417 break;
418 }
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000419
420 // If we end up here, we are looking at a direct call to our function.
421
422 // Now, check how our return value(s) is/are used in this caller. Don't
423 // bother checking return values if all of them are live already
424 if (NumLiveRetVals != RetCount) {
425 if (STy) {
426 // Check all uses of the return value
427 for (Value::use_iterator I = TheCall->use_begin(),
428 E = TheCall->use_end(); I != E; ++I) {
429 ExtractValueInst *Ext = dyn_cast<ExtractValueInst>(*I);
430 if (Ext && Ext->hasIndices()) {
431 // This use uses a part of our return value, survey the uses of that
432 // part and store the results for this index only.
433 unsigned Idx = *Ext->idx_begin();
434 if (RetValLiveness[Idx] != Live) {
435 RetValLiveness[Idx] = SurveyUses(Ext, MaybeLiveRetUses[Idx]);
436 if (RetValLiveness[Idx] == Live)
437 NumLiveRetVals++;
438 }
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000439 } else {
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000440 // Used by something else than extractvalue. Mark all
441 // return values as live.
442 for (unsigned i = 0; i != RetCount; ++i )
443 RetValLiveness[i] = Live;
444 NumLiveRetVals = RetCount;
445 break;
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000446 }
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000447 }
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000448 } else {
449 // Single return value
450 RetValLiveness[0] = SurveyUses(TheCall, MaybeLiveRetUses[0]);
451 if (RetValLiveness[0] == Live)
452 NumLiveRetVals = RetCount;
453 }
454 }
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000455 }
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000456 }
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000457 if (FunctionIntrinsicallyLive) {
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000458 DOUT << "DAE - Intrinsically live fn: " << F.getName() << "\n";
459 // Mark all arguments as live
460 unsigned i = 0;
Chris Lattner19bdc032005-05-06 05:34:40 +0000461 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000462 AI != E; ++AI, ++i)
463 MarkLive(CreateArg(&F, i));
464 // Mark all return values as live
465 i = 0;
466 for (unsigned i = 0, e = RetValLiveness.size(); i != e; ++i)
467 MarkLive(CreateRet(&F, i));
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000468 return;
469 }
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000470
471 // Now we've inspected all callers, record the liveness of our return values.
472 for (unsigned i = 0, e = RetValLiveness.size(); i != e; ++i) {
473 RetOrArg Ret = CreateRet(&F, i);
474 // Mark the result down
475 MarkValue(Ret, RetValLiveness[i], MaybeLiveRetUses[i]);
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000476 }
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000477 DOUT << "DAE - Inspecting args for fn: " << F.getName() << "\n";
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000478
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000479 // Now, check all of our arguments
480 unsigned i = 0;
481 UseVector MaybeLiveArgUses;
482 for (Function::arg_iterator AI = F.arg_begin(),
483 E = F.arg_end(); AI != E; ++AI, ++i) {
484 // See what the effect of this use is (recording any uses that cause
485 // MaybeLive in MaybeLiveArgUses)
486 Liveness Result = SurveyUses(AI, MaybeLiveArgUses);
487 RetOrArg Arg = CreateArg(&F, i);
488 // Mark the result down
489 MarkValue(Arg, Result, MaybeLiveArgUses);
490 // Clear the vector again for the next iteration
491 MaybeLiveArgUses.clear();
492 }
493}
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000494
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000495/// MarkValue - This function marks the liveness of RA depending on L. If L is
496/// MaybeLive, it also records any uses in MaybeLiveUses such that RA will be
497/// marked live if any use in MaybeLiveUses gets marked live later on.
498void DAE::MarkValue(const RetOrArg &RA, Liveness L, const UseVector &MaybeLiveUses) {
499 switch (L) {
500 case Live: MarkLive(RA); break;
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000501 case MaybeLive:
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000502 {
503 // Note any uses of this value, so this return value can be
504 // marked live whenever one of the uses becomes live.
505 UseMap::iterator Where = Uses.begin();
506 for (UseVector::const_iterator UI = MaybeLiveUses.begin(),
507 UE = MaybeLiveUses.end(); UI != UE; ++UI)
508 Where = Uses.insert(Where, UseMap::value_type(*UI, RA));
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000509 break;
510 }
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000511 case Dead: break;
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000512 }
513}
514
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000515/// MarkLive - Mark the given return value or argument as live. Additionally,
516/// mark any values that are used by this value (according to Uses) live as
517/// well.
518void DAE::MarkLive(RetOrArg RA) {
519 if (!LiveValues.insert(RA).second)
520 return; // We were already marked Live
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000521
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000522 if (RA.IsArg)
523 DOUT << "DAE - Marking argument " << RA.Idx << " to function " << RA.F->getNameStart() << " live\n";
524 else
525 DOUT << "DAE - Marking return value " << RA.Idx << " of function " << RA.F->getNameStart() << " live\n";
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000526
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000527 std::pair<UseMap::iterator, UseMap::iterator> Range = Uses.equal_range(RA);
528 UseMap::iterator E = Range.second;
529 UseMap::iterator I = Range.first;
530 for (; I != E; ++I)
531 MarkLive(I->second);
532 // Erase RA from the Uses map (from the lower bound to wherever we ended up
533 // after the loop).
534 Uses.erase(Range.first, Range.second);
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000535}
536
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000537// RemoveDeadStuffFromFunction - Remove any arguments and return values from F
538// that are not in LiveValues. This function is a noop for any Function created
539// by this function before, or any function that was not inspected for liveness.
Chris Lattner08227e42003-06-17 22:21:05 +0000540// specified by the DeadArguments list. Transform the function and all of the
541// callees of the function to not have these arguments.
542//
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000543bool DAE::RemoveDeadStuffFromFunction(Function *F) {
544 // Don't process functions we didn't inspect (such as external functions, or
545 // functions that we've newly created).
546 if (!InspectedFunctions.count(F))
547 return false;
548
Chris Lattner08227e42003-06-17 22:21:05 +0000549 // Start by computing a new prototype for the function, which is the same as
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000550 // the old function, but has fewer arguments and a different return type.
Chris Lattner08227e42003-06-17 22:21:05 +0000551 const FunctionType *FTy = F->getFunctionType();
552 std::vector<const Type*> Params;
553
Duncan Sandsdc024672007-11-27 13:23:08 +0000554 // Set up to build a new list of parameter attributes
Chris Lattner58d74912008-03-12 17:45:29 +0000555 SmallVector<ParamAttrsWithIndex, 8> ParamAttrsVec;
556 const PAListPtr &PAL = F->getParamAttrs();
Chris Lattner08227e42003-06-17 22:21:05 +0000557
Duncan Sands110c8352007-12-21 19:16:16 +0000558 // The existing function return attributes.
Chris Lattner58d74912008-03-12 17:45:29 +0000559 ParameterAttributes RAttrs = PAL.getParamAttrs(0);
Duncan Sands110c8352007-12-21 19:16:16 +0000560
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000561
562 // Find out the new return value
563
Duncan Sands110c8352007-12-21 19:16:16 +0000564 const Type *RetTy = FTy->getReturnType();
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000565 const Type *NRetTy;
566 unsigned RetCount = NumRetVals(F);
567 // -1 means unused, other numbers are the new index
568 SmallVector<int, 5> NewRetIdxs(RetCount, -1);
569 std::vector<const Type*> RetTypes;
570 if (RetTy != Type::VoidTy) {
571 const StructType *STy = dyn_cast<StructType>(RetTy);
572 if (STy)
573 // Look at each of the original return values individually
574 for (unsigned i = 0; i != RetCount; ++i) {
575 RetOrArg Ret = CreateRet(F, i);
576 if (LiveValues.erase(Ret)) {
577 RetTypes.push_back(STy->getElementType(i));
578 NewRetIdxs[i] = RetTypes.size() - 1;
579 } else {
580 ++NumRetValsEliminated;
581 DOUT << "DAE - Removing return value " << i << " from " << F->getNameStart() << "\n";
582 }
583 }
584 else
585 // We used to return a single value
586 if (LiveValues.erase(CreateRet(F, 0))) {
587 RetTypes.push_back(RetTy);
588 NewRetIdxs[0] = 0;
589 } else {
590 DOUT << "DAE - Removing return value from " << F->getNameStart() << "\n";
591 ++NumRetValsEliminated;
592 }
593 if (RetTypes.size() == 0)
594 // No return types? Make it void
595 NRetTy = Type::VoidTy;
596 else if (RetTypes.size() == 1)
597 // One return type? Just a simple value then
598 NRetTy = RetTypes.front();
599 else
600 // More return types? Return a struct with them
601 NRetTy = StructType::get(RetTypes);
602 } else {
603 NRetTy = Type::VoidTy;
Duncan Sands110c8352007-12-21 19:16:16 +0000604 }
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000605
606 // Remove any incompatible attributes
607 RAttrs &= ~ParamAttr::typeIncompatible(NRetTy);
Duncan Sands110c8352007-12-21 19:16:16 +0000608 if (RAttrs)
609 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(0, RAttrs));
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000610
611 // Remember which arguments are still alive
612 SmallVector<bool, 10> ArgAlive(FTy->getNumParams(), false);
Duncan Sandsdc024672007-11-27 13:23:08 +0000613 // Construct the new parameter list from non-dead arguments. Also construct
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000614 // a new set of parameter attributes to correspond. Skip the first parameter
615 // attribute, since that belongs to the return value.
616 unsigned i = 0;
617 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
618 I != E; ++I, ++i) {
619 RetOrArg Arg = CreateArg(F, i);
620 if (LiveValues.erase(Arg)) {
Duncan Sandsdc024672007-11-27 13:23:08 +0000621 Params.push_back(I->getType());
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000622 ArgAlive[i] = true;
Chris Lattner58d74912008-03-12 17:45:29 +0000623
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000624 // Get the original parameter attributes (skipping the first one, that is
625 // for the return value
626 if (ParameterAttributes Attrs = PAL.getParamAttrs(i + 1))
Duncan Sands110c8352007-12-21 19:16:16 +0000627 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Params.size(), Attrs));
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000628 } else {
629 ++NumArgumentsEliminated;
630 DOUT << "DAE - Removing argument " << i << " (" << I->getNameStart() << ") from " << F->getNameStart() << "\n";
Duncan Sandsdc024672007-11-27 13:23:08 +0000631 }
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000632 }
Duncan Sandsdc024672007-11-27 13:23:08 +0000633
634 // Reconstruct the ParamAttrsList based on the vector we constructed.
Chris Lattner58d74912008-03-12 17:45:29 +0000635 PAListPtr NewPAL = PAListPtr::get(ParamAttrsVec.begin(), ParamAttrsVec.end());
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000636
Chris Lattnerff5bf9c2003-10-23 17:44:53 +0000637 // Work around LLVM bug PR56: the CWriter cannot emit varargs functions which
638 // have zero fixed arguments.
639 //
Chris Lattnerff5bf9c2003-10-23 17:44:53 +0000640 bool ExtraArgHack = false;
641 if (Params.empty() && FTy->isVarArg()) {
642 ExtraArgHack = true;
Reid Spencerc5b206b2006-12-31 05:48:39 +0000643 Params.push_back(Type::Int32Ty);
Chris Lattnerff5bf9c2003-10-23 17:44:53 +0000644 }
645
Duncan Sandsdc024672007-11-27 13:23:08 +0000646 // Create the new function type based on the recomputed parameters.
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000647 FunctionType *NFTy = FunctionType::get(NRetTy, Params, FTy->isVarArg());
648
649 // No change?
650 if (NFTy == FTy)
651 return false;
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000652
Chris Lattner08227e42003-06-17 22:21:05 +0000653 // Create the new function body and insert it into the module...
Gabor Greif051a9502008-04-06 20:25:17 +0000654 Function *NF = Function::Create(NFTy, F->getLinkage());
Duncan Sands28c3cff2008-05-26 19:58:59 +0000655 NF->copyAttributesFrom(F);
Chris Lattner58d74912008-03-12 17:45:29 +0000656 NF->setParamAttrs(NewPAL);
Chris Lattner08227e42003-06-17 22:21:05 +0000657 F->getParent()->getFunctionList().insert(F, NF);
Chris Lattner046800a2007-02-11 01:08:35 +0000658 NF->takeName(F);
Chris Lattner08227e42003-06-17 22:21:05 +0000659
660 // Loop over all of the callers of the function, transforming the call sites
661 // to pass in a smaller number of arguments into the new function.
662 //
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000663 std::vector<Value*> Args;
Chris Lattner08227e42003-06-17 22:21:05 +0000664 while (!F->use_empty()) {
665 CallSite CS = CallSite::get(F->use_back());
666 Instruction *Call = CS.getInstruction();
Duncan Sands110c8352007-12-21 19:16:16 +0000667 ParamAttrsVec.clear();
Chris Lattner58d74912008-03-12 17:45:29 +0000668 const PAListPtr &CallPAL = CS.getParamAttrs();
Duncan Sands110c8352007-12-21 19:16:16 +0000669
670 // The call return attributes.
Chris Lattner58d74912008-03-12 17:45:29 +0000671 ParameterAttributes RAttrs = CallPAL.getParamAttrs(0);
Duncan Sands110c8352007-12-21 19:16:16 +0000672 // Adjust in case the function was changed to return void.
Duncan Sands6c3470e2008-01-07 17:16:06 +0000673 RAttrs &= ~ParamAttr::typeIncompatible(NF->getReturnType());
Duncan Sands110c8352007-12-21 19:16:16 +0000674 if (RAttrs)
675 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(0, RAttrs));
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000676
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000677 // Declare these outside of the loops, so we can reuse them for the second
678 // loop, which loops the varargs
679 CallSite::arg_iterator I = CS.arg_begin();
680 unsigned i = 0;
681 // Loop over those operands, corresponding to the normal arguments to the
682 // original function, and add those that are still alive.
683 for (unsigned e = FTy->getNumParams(); i != e; ++I, ++i)
684 if (ArgAlive[i]) {
685 Args.push_back(*I);
686 // Get original parameter attributes, but skip return attributes
687 if (ParameterAttributes Attrs = CallPAL.getParamAttrs(i + 1))
Duncan Sands110c8352007-12-21 19:16:16 +0000688 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Args.size(), Attrs));
689 }
690
Chris Lattnerff5bf9c2003-10-23 17:44:53 +0000691 if (ExtraArgHack)
Reid Spencerc5b206b2006-12-31 05:48:39 +0000692 Args.push_back(UndefValue::get(Type::Int32Ty));
Chris Lattnerff5bf9c2003-10-23 17:44:53 +0000693
Evan Chengb2fc2a32008-01-17 04:18:54 +0000694 // Push any varargs arguments on the list. Don't forget their attributes.
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000695 for (CallSite::arg_iterator E = CS.arg_end(); I != E; ++I, ++i) {
696 Args.push_back(*I);
697 if (ParameterAttributes Attrs = CallPAL.getParamAttrs(i + 1))
Evan Chengb2fc2a32008-01-17 04:18:54 +0000698 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Args.size(), Attrs));
699 }
700
701 // Reconstruct the ParamAttrsList based on the vector we constructed.
Chris Lattner58d74912008-03-12 17:45:29 +0000702 PAListPtr NewCallPAL = PAListPtr::get(ParamAttrsVec.begin(),
703 ParamAttrsVec.end());
Chris Lattnerff5bf9c2003-10-23 17:44:53 +0000704
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000705 Instruction *New;
706 if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
Gabor Greif051a9502008-04-06 20:25:17 +0000707 New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),
708 Args.begin(), Args.end(), "", Call);
Chris Lattnerf201dbc2005-05-09 01:05:50 +0000709 cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
Chris Lattner58d74912008-03-12 17:45:29 +0000710 cast<InvokeInst>(New)->setParamAttrs(NewCallPAL);
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000711 } else {
Gabor Greif051a9502008-04-06 20:25:17 +0000712 New = CallInst::Create(NF, Args.begin(), Args.end(), "", Call);
Chris Lattnerf201dbc2005-05-09 01:05:50 +0000713 cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
Chris Lattner58d74912008-03-12 17:45:29 +0000714 cast<CallInst>(New)->setParamAttrs(NewCallPAL);
Chris Lattner1430ef12005-05-06 06:46:58 +0000715 if (cast<CallInst>(Call)->isTailCall())
716 cast<CallInst>(New)->setTailCall();
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000717 }
718 Args.clear();
719
720 if (!Call->use_empty()) {
721 if (New->getType() == Type::VoidTy)
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000722 // Our return value was unused, replace by null for now, uses will get
723 // removed later on
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000724 Call->replaceAllUsesWith(Constant::getNullValue(Call->getType()));
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000725 else if (isa<StructType>(RetTy)) {
726 // The original return value was a struct, update all uses (which are
727 // all extractvalue instructions).
728 for (Value::use_iterator I = Call->use_begin(), E = Call->use_end();
729 I != E;) {
730 assert(isa<ExtractValueInst>(*I) && "Return value not only used by extractvalue?");
731 ExtractValueInst *EV = cast<ExtractValueInst>(*I);
732 // Increment now, since we're about to throw away this use.
733 ++I;
734 assert(EV->hasIndices() && "Return value used by extractvalue without indices?");
735 unsigned Idx = *EV->idx_begin();
736 if (NewRetIdxs[Idx] != -1) {
737 if (RetTypes.size() > 1) {
738 // We're still returning a struct, create a new extractvalue
739 // instruction with the first index updated
740 std::vector<unsigned> NewIdxs(EV->idx_begin(), EV->idx_end());
741 NewIdxs[0] = NewRetIdxs[Idx];
742 Value *NEV = ExtractValueInst::Create(New, NewIdxs.begin(), NewIdxs.end(), "retval", EV);
743 EV->replaceAllUsesWith(NEV);
744 EV->eraseFromParent();
745 } else {
746 // We are now only returning a simple value, remove the
747 // extractvalue
748 EV->replaceAllUsesWith(New);
749 EV->eraseFromParent();
750 }
751 } else {
752 // Value unused, replace uses by null for now, they will get removed
753 // later on
754 EV->replaceAllUsesWith(Constant::getNullValue(EV->getType()));
755 EV->eraseFromParent();
756 }
757 }
758 New->takeName(Call);
759 } else {
760 // The original function had a single return value
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000761 Call->replaceAllUsesWith(New);
Chris Lattner046800a2007-02-11 01:08:35 +0000762 New->takeName(Call);
Chris Lattner08227e42003-06-17 22:21:05 +0000763 }
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000764 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000765
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000766 // Finally, remove the old call from the program, reducing the use-count of
767 // F.
Matthijs Kooijman494661c2008-05-30 12:35:46 +0000768 Call->eraseFromParent();
Chris Lattner08227e42003-06-17 22:21:05 +0000769 }
770
771 // Since we have now created the new function, splice the body of the old
772 // function right into the new function, leaving the old rotting hulk of the
773 // function empty.
774 NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());
775
776 // Loop over the argument list, transfering uses of the old arguments over to
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000777 // the new arguments, also transfering over the names as well.
778 i = 0;
Chris Lattner19bdc032005-05-06 05:34:40 +0000779 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(),
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000780 I2 = NF->arg_begin(); I != E; ++I, ++i)
781 if (ArgAlive[i]) {
Chris Lattner08227e42003-06-17 22:21:05 +0000782 // If this is a live argument, move the name and users over to the new
783 // version.
784 I->replaceAllUsesWith(I2);
Chris Lattner046800a2007-02-11 01:08:35 +0000785 I2->takeName(I);
Chris Lattner08227e42003-06-17 22:21:05 +0000786 ++I2;
787 } else {
788 // If this argument is dead, replace any uses of it with null constants
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000789 // (these are guaranteed to become unused later on)
Chris Lattner08227e42003-06-17 22:21:05 +0000790 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
Chris Lattner08227e42003-06-17 22:21:05 +0000791 }
792
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000793 // If we change the return value of the function we must rewrite any return
794 // instructions. Check this now.
795 if (F->getReturnType() != NF->getReturnType())
796 for (Function::iterator BB = NF->begin(), E = NF->end(); BB != E; ++BB)
797 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000798 Value *RetVal;
799
800 if (NFTy->getReturnType() == Type::VoidTy) {
801 RetVal = 0;
802 } else {
803 assert (isa<StructType>(RetTy));
804 // The original return value was a struct, insert
805 // extractvalue/insertvalue chains to extract only the values we need
806 // to return and insert them into our new result.
807 // This does generate messy code, but we'll let it to instcombine to
808 // clean that up
809 Value *OldRet = RI->getOperand(0);
810 // Start out building up our return value from undef
811 RetVal = llvm::UndefValue::get(NRetTy);
812 for (unsigned i = 0; i != RetCount; ++i)
813 if (NewRetIdxs[i] != -1) {
814 ExtractValueInst *EV = ExtractValueInst::Create(OldRet, i, "newret", RI);
815 if (RetTypes.size() > 1) {
816 // We're still returning a struct, so reinsert the value into
817 // our new return value at the new index
818
819 RetVal = InsertValueInst::Create(RetVal, EV, NewRetIdxs[i], "oldret");
820 } else {
821 // We are now only returning a simple value, so just return the
822 // extracted value
823 RetVal = EV;
824 }
825 }
826 }
827 // Replace the return instruction with one returning the new return
828 // value (possibly 0 if we became void).
829 ReturnInst::Create(RetVal, RI);
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000830 BB->getInstList().erase(RI);
831 }
832
Chris Lattner08227e42003-06-17 22:21:05 +0000833 // Now that the old function is dead, delete it.
Matthijs Kooijman494661c2008-05-30 12:35:46 +0000834 F->eraseFromParent();
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000835
836 return true;
Chris Lattner08227e42003-06-17 22:21:05 +0000837}
838
Chris Lattnerb12914b2004-09-20 04:48:05 +0000839bool DAE::runOnModule(Module &M) {
Chris Lattner701bc422007-11-15 06:10:55 +0000840 bool Changed = false;
841 // First pass: Do a simple check to see if any functions can have their "..."
842 // removed. We can do this if they never call va_start. This loop cannot be
843 // fused with the next loop, because deleting a function invalidates
844 // information computed while surveying other functions.
845 DOUT << "DAE - Deleting dead varargs\n";
846 for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
847 Function &F = *I++;
848 if (F.getFunctionType()->isVarArg())
849 Changed |= DeleteDeadVarargs(F);
850 }
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000851
Chris Lattner701bc422007-11-15 06:10:55 +0000852 // Second phase:loop through the module, determining which arguments are live.
Chris Lattner08227e42003-06-17 22:21:05 +0000853 // We assume all arguments are dead unless proven otherwise (allowing us to
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000854 // determine that dead arguments passed into recursive functions are dead).
Chris Lattner08227e42003-06-17 22:21:05 +0000855 //
Bill Wendling0a81aac2006-11-26 10:02:32 +0000856 DOUT << "DAE - Determining liveness\n";
Chris Lattner701bc422007-11-15 06:10:55 +0000857 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
858 SurveyFunction(*I);
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000859
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000860 // Now, remove all dead arguments and return values from each function in
861 // turn
862 for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
863 // Increment now, because the function will probably get removed (ie
864 // replaced by a new one)
865 Function *F = I++;
866 Changed |= RemoveDeadStuffFromFunction(F);
Chris Lattner08227e42003-06-17 22:21:05 +0000867 }
868
Matthijs Kooijmanca85d652008-06-18 11:12:53 +0000869 return Changed;
Chris Lattner08227e42003-06-17 22:21:05 +0000870}