blob: 2f075ba30dc99c1b278fff0d89e0cf52b71560c0 [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
13// pass also deletes dead arguments 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
16// interprocedural passes, which add possibly-dead arguments.
17//
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 {
Chris Lattnerc3afd9b2003-10-23 03:48:17 +000045 /// Liveness enum - During our initial pass over the program, we determine
46 /// that things are either definately alive, definately dead, or in need of
47 /// interprocedural analysis (MaybeLive).
48 ///
49 enum Liveness { Live, MaybeLive, Dead };
50
51 /// LiveArguments, MaybeLiveArguments, DeadArguments - These sets contain
52 /// all of the arguments in the program. The Dead set contains arguments
53 /// which are completely dead (never used in the function). The MaybeLive
54 /// set contains arguments which are only passed into other function calls,
55 /// thus may be live and may be dead. The Live set contains arguments which
56 /// are known to be alive.
57 ///
58 std::set<Argument*> DeadArguments, MaybeLiveArguments, LiveArguments;
59
60 /// DeadRetVal, MaybeLiveRetVal, LifeRetVal - These sets contain all of the
61 /// functions in the program. The Dead set contains functions whose return
62 /// value is known to be dead. The MaybeLive set contains functions whose
63 /// return values are only used by return instructions, and the Live set
64 /// contains functions whose return values are used, functions that are
65 /// external, and functions that already return void.
66 ///
67 std::set<Function*> DeadRetVal, MaybeLiveRetVal, LiveRetVal;
68
69 /// InstructionsToInspect - As we mark arguments and return values
70 /// MaybeLive, we keep track of which instructions could make the values
71 /// live here. Once the entire program has had the return value and
72 /// arguments analyzed, this set is scanned to promote the MaybeLive objects
73 /// to be Live if they really are used.
74 std::vector<Instruction*> InstructionsToInspect;
75
76 /// CallSites - Keep track of the call sites of functions that have
77 /// MaybeLive arguments or return values.
78 std::multimap<Function*, CallSite> CallSites;
79
80 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000081 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000082 DAE() : ModulePass((intptr_t)&ID) {}
Chris Lattnerb12914b2004-09-20 04:48:05 +000083 bool runOnModule(Module &M);
Chris Lattner9b2a14b2003-06-25 04:12:49 +000084
Chris Lattnerfdcc3ac2003-11-05 21:43:02 +000085 virtual bool ShouldHackArguments() const { return false; }
86
Chris Lattner9b2a14b2003-06-25 04:12:49 +000087 private:
Chris Lattnerc3afd9b2003-10-23 03:48:17 +000088 Liveness getArgumentLiveness(const Argument &A);
89 bool isMaybeLiveArgumentNowLive(Argument *Arg);
90
Chris Lattner4af90ab2006-09-18 07:02:31 +000091 bool DeleteDeadVarargs(Function &Fn);
Chris Lattnerc3afd9b2003-10-23 03:48:17 +000092 void SurveyFunction(Function &Fn);
93
94 void MarkArgumentLive(Argument *Arg);
95 void MarkRetValLive(Function *F);
96 void MarkReturnInstArgumentLive(ReturnInst *RI);
Misha Brukmanfd939082005-04-21 23:48:37 +000097
Chris Lattnerc3afd9b2003-10-23 03:48:17 +000098 void RemoveDeadArgumentsFromFunction(Function *F);
Chris Lattner08227e42003-06-17 22:21:05 +000099 };
Dan Gohman844731a2008-05-13 00:00:25 +0000100}
Chris Lattnerfdcc3ac2003-11-05 21:43:02 +0000101
Dan Gohman844731a2008-05-13 00:00:25 +0000102char DAE::ID = 0;
103static RegisterPass<DAE>
104X("deadargelim", "Dead Argument Elimination");
105
106namespace {
Chris Lattnerfdcc3ac2003-11-05 21:43:02 +0000107 /// DAH - DeadArgumentHacking pass - Same as dead argument elimination, but
108 /// deletes arguments to functions which are external. This is only for use
109 /// by bugpoint.
110 struct DAH : public DAE {
Devang Patel19974732007-05-03 01:11:54 +0000111 static char ID;
Chris Lattnerfdcc3ac2003-11-05 21:43:02 +0000112 virtual bool ShouldHackArguments() const { return true; }
113 };
Chris Lattner08227e42003-06-17 22:21:05 +0000114}
115
Dan Gohman844731a2008-05-13 00:00:25 +0000116char DAH::ID = 0;
117static RegisterPass<DAH>
118Y("deadarghaX0r", "Dead Argument Hacking (BUGPOINT USE ONLY; DO NOT USE)");
119
Chris Lattner9b2a14b2003-06-25 04:12:49 +0000120/// createDeadArgEliminationPass - This pass removes arguments from functions
Chris Lattnerfdcc3ac2003-11-05 21:43:02 +0000121/// which are not used by the body of the function.
Chris Lattner9b2a14b2003-06-25 04:12:49 +0000122///
Chris Lattnerb12914b2004-09-20 04:48:05 +0000123ModulePass *llvm::createDeadArgEliminationPass() { return new DAE(); }
124ModulePass *llvm::createDeadArgHackingPass() { return new DAH(); }
Chris Lattner08227e42003-06-17 22:21:05 +0000125
Chris Lattner4af90ab2006-09-18 07:02:31 +0000126/// DeleteDeadVarargs - If this is an function that takes a ... list, and if
127/// llvm.vastart is never called, the varargs list is dead for the function.
128bool DAE::DeleteDeadVarargs(Function &Fn) {
129 assert(Fn.getFunctionType()->isVarArg() && "Function isn't varargs!");
Reid Spencer5cbf9852007-01-30 20:08:39 +0000130 if (Fn.isDeclaration() || !Fn.hasInternalLinkage()) return false;
Duncan Sands110c8352007-12-21 19:16:16 +0000131
Chris Lattner4af90ab2006-09-18 07:02:31 +0000132 // Ensure that the function is only directly called.
133 for (Value::use_iterator I = Fn.use_begin(), E = Fn.use_end(); I != E; ++I) {
134 // If this use is anything other than a call site, give up.
135 CallSite CS = CallSite::get(*I);
136 Instruction *TheCall = CS.getInstruction();
137 if (!TheCall) return false; // Not a direct call site?
Duncan Sands110c8352007-12-21 19:16:16 +0000138
Chris Lattner4af90ab2006-09-18 07:02:31 +0000139 // The addr of this function is passed to the call.
140 if (I.getOperandNo() != 0) return false;
141 }
Duncan Sands110c8352007-12-21 19:16:16 +0000142
Chris Lattner4af90ab2006-09-18 07:02:31 +0000143 // Okay, we know we can transform this function if safe. Scan its body
144 // looking for calls to llvm.vastart.
145 for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
146 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
147 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
148 if (II->getIntrinsicID() == Intrinsic::vastart)
149 return false;
150 }
151 }
152 }
Duncan Sands110c8352007-12-21 19:16:16 +0000153
Chris Lattner4af90ab2006-09-18 07:02:31 +0000154 // If we get here, there are no calls to llvm.vastart in the function body,
155 // remove the "..." and adjust all the calls.
Duncan Sands110c8352007-12-21 19:16:16 +0000156
Chris Lattner4af90ab2006-09-18 07:02:31 +0000157 // Start by computing a new prototype for the function, which is the same as
158 // the old function, but has fewer arguments.
159 const FunctionType *FTy = Fn.getFunctionType();
160 std::vector<const Type*> Params(FTy->param_begin(), FTy->param_end());
161 FunctionType *NFTy = FunctionType::get(FTy->getReturnType(), Params, false);
162 unsigned NumArgs = Params.size();
Duncan Sands110c8352007-12-21 19:16:16 +0000163
Chris Lattner4af90ab2006-09-18 07:02:31 +0000164 // Create the new function body and insert it into the module...
Gabor Greif051a9502008-04-06 20:25:17 +0000165 Function *NF = Function::Create(NFTy, Fn.getLinkage());
Duncan Sands28c3cff2008-05-26 19:58:59 +0000166 NF->copyAttributesFrom(&Fn);
Chris Lattner4af90ab2006-09-18 07:02:31 +0000167 Fn.getParent()->getFunctionList().insert(&Fn, NF);
Chris Lattner046800a2007-02-11 01:08:35 +0000168 NF->takeName(&Fn);
Duncan Sands110c8352007-12-21 19:16:16 +0000169
Chris Lattner4af90ab2006-09-18 07:02:31 +0000170 // Loop over all of the callers of the function, transforming the call sites
171 // to pass in a smaller number of arguments into the new function.
172 //
173 std::vector<Value*> Args;
174 while (!Fn.use_empty()) {
175 CallSite CS = CallSite::get(Fn.use_back());
176 Instruction *Call = CS.getInstruction();
Duncan Sands110c8352007-12-21 19:16:16 +0000177
Chris Lattnera0bc7fc2007-10-18 18:49:29 +0000178 // Pass all the same arguments.
Chris Lattner4af90ab2006-09-18 07:02:31 +0000179 Args.assign(CS.arg_begin(), CS.arg_begin()+NumArgs);
Duncan Sands110c8352007-12-21 19:16:16 +0000180
Duncan Sandsbfc5ae62008-01-11 23:13:45 +0000181 // Drop any attributes that were on the vararg arguments.
Chris Lattner58d74912008-03-12 17:45:29 +0000182 PAListPtr PAL = CS.getParamAttrs();
183 if (!PAL.isEmpty() && PAL.getSlot(PAL.getNumSlots() - 1).Index > NumArgs) {
184 SmallVector<ParamAttrsWithIndex, 8> ParamAttrsVec;
185 for (unsigned i = 0; PAL.getSlot(i).Index <= NumArgs; ++i)
186 ParamAttrsVec.push_back(PAL.getSlot(i));
187 PAL = PAListPtr::get(ParamAttrsVec.begin(), ParamAttrsVec.end());
Duncan Sandsbfc5ae62008-01-11 23:13:45 +0000188 }
189
Chris Lattner4af90ab2006-09-18 07:02:31 +0000190 Instruction *New;
191 if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
Gabor Greif051a9502008-04-06 20:25:17 +0000192 New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),
193 Args.begin(), Args.end(), "", Call);
Chris Lattner4af90ab2006-09-18 07:02:31 +0000194 cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
Duncan Sandsbfc5ae62008-01-11 23:13:45 +0000195 cast<InvokeInst>(New)->setParamAttrs(PAL);
Chris Lattner4af90ab2006-09-18 07:02:31 +0000196 } else {
Gabor Greif051a9502008-04-06 20:25:17 +0000197 New = CallInst::Create(NF, Args.begin(), Args.end(), "", Call);
Chris Lattner4af90ab2006-09-18 07:02:31 +0000198 cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
Duncan Sandsbfc5ae62008-01-11 23:13:45 +0000199 cast<CallInst>(New)->setParamAttrs(PAL);
Chris Lattner4af90ab2006-09-18 07:02:31 +0000200 if (cast<CallInst>(Call)->isTailCall())
201 cast<CallInst>(New)->setTailCall();
202 }
203 Args.clear();
Duncan Sands110c8352007-12-21 19:16:16 +0000204
Chris Lattner4af90ab2006-09-18 07:02:31 +0000205 if (!Call->use_empty())
Chris Lattnera0bc7fc2007-10-18 18:49:29 +0000206 Call->replaceAllUsesWith(New);
Duncan Sands110c8352007-12-21 19:16:16 +0000207
Chris Lattner046800a2007-02-11 01:08:35 +0000208 New->takeName(Call);
Duncan Sands110c8352007-12-21 19:16:16 +0000209
Chris Lattner4af90ab2006-09-18 07:02:31 +0000210 // Finally, remove the old call from the program, reducing the use-count of
211 // F.
Chris Lattnera0bc7fc2007-10-18 18:49:29 +0000212 Call->eraseFromParent();
Chris Lattner4af90ab2006-09-18 07:02:31 +0000213 }
Duncan Sands110c8352007-12-21 19:16:16 +0000214
Chris Lattner4af90ab2006-09-18 07:02:31 +0000215 // Since we have now created the new function, splice the body of the old
216 // function right into the new function, leaving the old rotting hulk of the
217 // function empty.
218 NF->getBasicBlockList().splice(NF->begin(), Fn.getBasicBlockList());
Duncan Sands110c8352007-12-21 19:16:16 +0000219
Chris Lattner4af90ab2006-09-18 07:02:31 +0000220 // Loop over the argument list, transfering uses of the old arguments over to
221 // the new arguments, also transfering over the names as well. While we're at
222 // it, remove the dead arguments from the DeadArguments list.
223 //
224 for (Function::arg_iterator I = Fn.arg_begin(), E = Fn.arg_end(),
225 I2 = NF->arg_begin(); I != E; ++I, ++I2) {
226 // Move the name and users over to the new version.
227 I->replaceAllUsesWith(I2);
Chris Lattner046800a2007-02-11 01:08:35 +0000228 I2->takeName(I);
Chris Lattner4af90ab2006-09-18 07:02:31 +0000229 }
Duncan Sands110c8352007-12-21 19:16:16 +0000230
Chris Lattner4af90ab2006-09-18 07:02:31 +0000231 // Finally, nuke the old function.
232 Fn.eraseFromParent();
233 return true;
234}
235
236
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000237static inline bool CallPassesValueThoughVararg(Instruction *Call,
238 const Value *Arg) {
239 CallSite CS = CallSite::get(Call);
240 const Type *CalledValueTy = CS.getCalledValue()->getType();
241 const Type *FTy = cast<PointerType>(CalledValueTy)->getElementType();
242 unsigned NumFixedArgs = cast<FunctionType>(FTy)->getNumParams();
243 for (CallSite::arg_iterator AI = CS.arg_begin()+NumFixedArgs;
244 AI != CS.arg_end(); ++AI)
245 if (AI->get() == Arg)
246 return true;
Chris Lattner08227e42003-06-17 22:21:05 +0000247 return false;
248}
249
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000250// getArgumentLiveness - Inspect an argument, determining if is known Live
Chris Lattner08227e42003-06-17 22:21:05 +0000251// (used in a computation), MaybeLive (only passed as an argument to a call), or
252// Dead (not used).
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000253DAE::Liveness DAE::getArgumentLiveness(const Argument &A) {
Duncan Sandsdc024672007-11-27 13:23:08 +0000254 const Function *F = A.getParent();
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000255
256 // If this is the return value of a struct function, it's not really dead.
Devang Patel41e23972008-03-03 21:46:28 +0000257 if (F->hasStructRetAttr() && &*(F->arg_begin()) == &A)
Chris Lattner92044ce2006-06-27 21:05:04 +0000258 return Live;
259
260 if (A.use_empty()) // First check, directly dead?
261 return Dead;
Chris Lattner08227e42003-06-17 22:21:05 +0000262
263 // Scan through all of the uses, looking for non-argument passing uses.
264 for (Value::use_const_iterator I = A.use_begin(), E = A.use_end(); I!=E;++I) {
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000265 // Return instructions do not immediately effect liveness.
266 if (isa<ReturnInst>(*I))
267 continue;
268
Chris Lattner08227e42003-06-17 22:21:05 +0000269 CallSite CS = CallSite::get(const_cast<User*>(*I));
270 if (!CS.getInstruction()) {
271 // If its used by something that is not a call or invoke, it's alive!
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000272 return Live;
Chris Lattner08227e42003-06-17 22:21:05 +0000273 }
274 // If it's an indirect call, mark it alive...
275 Function *Callee = CS.getCalledFunction();
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000276 if (!Callee) return Live;
Chris Lattner08227e42003-06-17 22:21:05 +0000277
Chris Lattner97f4b662003-06-18 16:25:51 +0000278 // Check to see if it's passed through a va_arg area: if so, we cannot
279 // remove it.
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000280 if (CallPassesValueThoughVararg(CS.getInstruction(), &A))
281 return Live; // If passed through va_arg area, we cannot remove it
Chris Lattner08227e42003-06-17 22:21:05 +0000282 }
283
284 return MaybeLive; // It must be used, but only as argument to a function
285}
286
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000287
288// SurveyFunction - This performs the initial survey of the specified function,
289// checking out whether or not it uses any of its incoming arguments or whether
290// any callers use the return value. This fills in the
291// (Dead|MaybeLive|Live)(Arguments|RetVal) sets.
Chris Lattner08227e42003-06-17 22:21:05 +0000292//
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000293// We consider arguments of non-internal functions to be intrinsically alive as
294// well as arguments to functions which have their "address taken".
295//
296void DAE::SurveyFunction(Function &F) {
297 bool FunctionIntrinsicallyLive = false;
298 Liveness RetValLiveness = F.getReturnType() == Type::VoidTy ? Live : Dead;
299
Chris Lattnerb6e06312003-11-05 21:53:41 +0000300 if (!F.hasInternalLinkage() &&
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000301 (!ShouldHackArguments() || F.isIntrinsic()))
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000302 FunctionIntrinsicallyLive = true;
Misha Brukmanfd939082005-04-21 23:48:37 +0000303 else
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000304 for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I) {
305 // If this use is anything other than a call site, the function is alive.
306 CallSite CS = CallSite::get(*I);
307 Instruction *TheCall = CS.getInstruction();
308 if (!TheCall) { // Not a direct call site?
309 FunctionIntrinsicallyLive = true;
310 break;
311 }
312
313 // Check to see if the return value is used...
314 if (RetValLiveness != Live)
315 for (Value::use_iterator I = TheCall->use_begin(),
316 E = TheCall->use_end(); I != E; ++I)
317 if (isa<ReturnInst>(cast<Instruction>(*I))) {
318 RetValLiveness = MaybeLive;
319 } else if (isa<CallInst>(cast<Instruction>(*I)) ||
320 isa<InvokeInst>(cast<Instruction>(*I))) {
321 if (CallPassesValueThoughVararg(cast<Instruction>(*I), TheCall) ||
322 !CallSite::get(cast<Instruction>(*I)).getCalledFunction()) {
323 RetValLiveness = Live;
324 break;
325 } else {
326 RetValLiveness = MaybeLive;
327 }
328 } else {
329 RetValLiveness = Live;
330 break;
331 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000332
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000333 // If the function is PASSED IN as an argument, its address has been taken
Matthijs Kooijman227c27d2008-06-04 16:57:50 +0000334
335 if (CS.hasArgument(&F)) {
336 FunctionIntrinsicallyLive = true;
337 break;
338 }
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000339 }
340
341 if (FunctionIntrinsicallyLive) {
Bill Wendling0a81aac2006-11-26 10:02:32 +0000342 DOUT << " Intrinsically live fn: " << F.getName() << "\n";
Chris Lattner19bdc032005-05-06 05:34:40 +0000343 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
344 AI != E; ++AI)
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000345 LiveArguments.insert(AI);
346 LiveRetVal.insert(&F);
347 return;
348 }
349
350 switch (RetValLiveness) {
351 case Live: LiveRetVal.insert(&F); break;
352 case MaybeLive: MaybeLiveRetVal.insert(&F); break;
353 case Dead: DeadRetVal.insert(&F); break;
354 }
355
Bill Wendling0a81aac2006-11-26 10:02:32 +0000356 DOUT << " Inspecting args for fn: " << F.getName() << "\n";
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000357
358 // If it is not intrinsically alive, we know that all users of the
359 // function are call sites. Mark all of the arguments live which are
360 // directly used, and keep track of all of the call sites of this function
361 // if there are any arguments we assume that are dead.
362 //
363 bool AnyMaybeLiveArgs = false;
Chris Lattner19bdc032005-05-06 05:34:40 +0000364 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
365 AI != E; ++AI)
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000366 switch (getArgumentLiveness(*AI)) {
367 case Live:
Bill Wendling0a81aac2006-11-26 10:02:32 +0000368 DOUT << " Arg live by use: " << AI->getName() << "\n";
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000369 LiveArguments.insert(AI);
370 break;
371 case Dead:
Bill Wendling0a81aac2006-11-26 10:02:32 +0000372 DOUT << " Arg definitely dead: " << AI->getName() <<"\n";
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000373 DeadArguments.insert(AI);
374 break;
375 case MaybeLive:
Bill Wendling0a81aac2006-11-26 10:02:32 +0000376 DOUT << " Arg only passed to calls: " << AI->getName() << "\n";
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000377 AnyMaybeLiveArgs = true;
378 MaybeLiveArguments.insert(AI);
379 break;
380 }
381
382 // If there are any "MaybeLive" arguments, we need to check callees of
383 // this function when/if they become alive. Record which functions are
384 // callees...
385 if (AnyMaybeLiveArgs || RetValLiveness == MaybeLive)
386 for (Value::use_iterator I = F.use_begin(), E = F.use_end();
387 I != E; ++I) {
388 if (AnyMaybeLiveArgs)
389 CallSites.insert(std::make_pair(&F, CallSite::get(*I)));
390
391 if (RetValLiveness == MaybeLive)
392 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
393 UI != E; ++UI)
394 InstructionsToInspect.push_back(cast<Instruction>(*UI));
395 }
396}
397
398// isMaybeLiveArgumentNowLive - Check to see if Arg is alive. At this point, we
399// know that the only uses of Arg are to be passed in as an argument to a
400// function call or return. Check to see if the formal argument passed in is in
401// the LiveArguments set. If so, return true.
402//
403bool DAE::isMaybeLiveArgumentNowLive(Argument *Arg) {
Chris Lattner08227e42003-06-17 22:21:05 +0000404 for (Value::use_iterator I = Arg->use_begin(), E = Arg->use_end(); I!=E; ++I){
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000405 if (isa<ReturnInst>(*I)) {
406 if (LiveRetVal.count(Arg->getParent())) return true;
407 continue;
408 }
409
Chris Lattner08227e42003-06-17 22:21:05 +0000410 CallSite CS = CallSite::get(*I);
411
412 // We know that this can only be used for direct calls...
Chris Lattnerd6d0d8c2003-11-02 02:06:27 +0000413 Function *Callee = CS.getCalledFunction();
Chris Lattner08227e42003-06-17 22:21:05 +0000414
415 // Loop over all of the arguments (because Arg may be passed into the call
416 // multiple times) and check to see if any are now alive...
417 CallSite::arg_iterator CSAI = CS.arg_begin();
Chris Lattnere4d5c442005-03-15 04:54:21 +0000418 for (Function::arg_iterator AI = Callee->arg_begin(), E = Callee->arg_end();
Chris Lattner08227e42003-06-17 22:21:05 +0000419 AI != E; ++AI, ++CSAI)
420 // If this is the argument we are looking for, check to see if it's alive
421 if (*CSAI == Arg && LiveArguments.count(AI))
422 return true;
423 }
424 return false;
425}
426
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000427/// MarkArgumentLive - The MaybeLive argument 'Arg' is now known to be alive.
428/// Mark it live in the specified sets and recursively mark arguments in callers
429/// live that are needed to pass in a value.
430///
431void DAE::MarkArgumentLive(Argument *Arg) {
432 std::set<Argument*>::iterator It = MaybeLiveArguments.lower_bound(Arg);
433 if (It == MaybeLiveArguments.end() || *It != Arg) return;
Misha Brukmanfd939082005-04-21 23:48:37 +0000434
Bill Wendling0a81aac2006-11-26 10:02:32 +0000435 DOUT << " MaybeLive argument now live: " << Arg->getName() <<"\n";
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000436 MaybeLiveArguments.erase(It);
Chris Lattner08227e42003-06-17 22:21:05 +0000437 LiveArguments.insert(Arg);
Misha Brukmanfd939082005-04-21 23:48:37 +0000438
Chris Lattner08227e42003-06-17 22:21:05 +0000439 // Loop over all of the call sites of the function, making any arguments
440 // passed in to provide a value for this argument live as necessary.
441 //
442 Function *Fn = Arg->getParent();
Chris Lattnere4d5c442005-03-15 04:54:21 +0000443 unsigned ArgNo = std::distance(Fn->arg_begin(), Function::arg_iterator(Arg));
Chris Lattner08227e42003-06-17 22:21:05 +0000444
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000445 std::multimap<Function*, CallSite>::iterator I = CallSites.lower_bound(Fn);
Chris Lattner08227e42003-06-17 22:21:05 +0000446 for (; I != CallSites.end() && I->first == Fn; ++I) {
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000447 CallSite CS = I->second;
448 Value *ArgVal = *(CS.arg_begin()+ArgNo);
449 if (Argument *ActualArg = dyn_cast<Argument>(ArgVal)) {
450 MarkArgumentLive(ActualArg);
451 } else {
452 // If the value passed in at this call site is a return value computed by
453 // some other call site, make sure to mark the return value at the other
454 // call site as being needed.
455 CallSite ArgCS = CallSite::get(ArgVal);
456 if (ArgCS.getInstruction())
457 if (Function *Fn = ArgCS.getCalledFunction())
458 MarkRetValLive(Fn);
459 }
460 }
461}
462
463/// MarkArgumentLive - The MaybeLive return value for the specified function is
464/// now known to be alive. Propagate this fact to the return instructions which
465/// produce it.
466void DAE::MarkRetValLive(Function *F) {
467 assert(F && "Shame shame, we can't have null pointers here!");
468
469 // Check to see if we already knew it was live
470 std::set<Function*>::iterator I = MaybeLiveRetVal.lower_bound(F);
471 if (I == MaybeLiveRetVal.end() || *I != F) return; // It's already alive!
472
Bill Wendling0a81aac2006-11-26 10:02:32 +0000473 DOUT << " MaybeLive retval now live: " << F->getName() << "\n";
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000474
475 MaybeLiveRetVal.erase(I);
476 LiveRetVal.insert(F); // It is now known to be live!
477
478 // Loop over all of the functions, noticing that the return value is now live.
479 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
480 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()))
481 MarkReturnInstArgumentLive(RI);
482}
483
484void DAE::MarkReturnInstArgumentLive(ReturnInst *RI) {
485 Value *Op = RI->getOperand(0);
486 if (Argument *A = dyn_cast<Argument>(Op)) {
487 MarkArgumentLive(A);
488 } else if (CallInst *CI = dyn_cast<CallInst>(Op)) {
489 if (Function *F = CI->getCalledFunction())
490 MarkRetValLive(F);
491 } else if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) {
492 if (Function *F = II->getCalledFunction())
493 MarkRetValLive(F);
Chris Lattner08227e42003-06-17 22:21:05 +0000494 }
495}
496
497// RemoveDeadArgumentsFromFunction - We know that F has dead arguments, as
498// specified by the DeadArguments list. Transform the function and all of the
499// callees of the function to not have these arguments.
500//
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000501void DAE::RemoveDeadArgumentsFromFunction(Function *F) {
Chris Lattner08227e42003-06-17 22:21:05 +0000502 // Start by computing a new prototype for the function, which is the same as
503 // the old function, but has fewer arguments.
504 const FunctionType *FTy = F->getFunctionType();
505 std::vector<const Type*> Params;
506
Duncan Sandsdc024672007-11-27 13:23:08 +0000507 // Set up to build a new list of parameter attributes
Chris Lattner58d74912008-03-12 17:45:29 +0000508 SmallVector<ParamAttrsWithIndex, 8> ParamAttrsVec;
509 const PAListPtr &PAL = F->getParamAttrs();
Chris Lattner08227e42003-06-17 22:21:05 +0000510
Duncan Sands110c8352007-12-21 19:16:16 +0000511 // The existing function return attributes.
Chris Lattner58d74912008-03-12 17:45:29 +0000512 ParameterAttributes RAttrs = PAL.getParamAttrs(0);
Duncan Sands110c8352007-12-21 19:16:16 +0000513
514 // Make the function return void if the return value is dead.
515 const Type *RetTy = FTy->getReturnType();
516 if (DeadRetVal.count(F)) {
517 RetTy = Type::VoidTy;
Duncan Sands6c3470e2008-01-07 17:16:06 +0000518 RAttrs &= ~ParamAttr::typeIncompatible(RetTy);
Duncan Sands110c8352007-12-21 19:16:16 +0000519 DeadRetVal.erase(F);
520 }
521
522 if (RAttrs)
523 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(0, RAttrs));
524
Duncan Sandsdc024672007-11-27 13:23:08 +0000525 // Construct the new parameter list from non-dead arguments. Also construct
526 // a new set of parameter attributes to correspond.
527 unsigned index = 1;
528 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E;
529 ++I, ++index)
530 if (!DeadArguments.count(I)) {
531 Params.push_back(I->getType());
Chris Lattner58d74912008-03-12 17:45:29 +0000532
533 if (ParameterAttributes Attrs = PAL.getParamAttrs(index))
Duncan Sands110c8352007-12-21 19:16:16 +0000534 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Params.size(), Attrs));
Duncan Sandsdc024672007-11-27 13:23:08 +0000535 }
536
537 // Reconstruct the ParamAttrsList based on the vector we constructed.
Chris Lattner58d74912008-03-12 17:45:29 +0000538 PAListPtr NewPAL = PAListPtr::get(ParamAttrsVec.begin(), ParamAttrsVec.end());
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000539
Chris Lattnerff5bf9c2003-10-23 17:44:53 +0000540 // Work around LLVM bug PR56: the CWriter cannot emit varargs functions which
541 // have zero fixed arguments.
542 //
Chris Lattnerff5bf9c2003-10-23 17:44:53 +0000543 bool ExtraArgHack = false;
544 if (Params.empty() && FTy->isVarArg()) {
545 ExtraArgHack = true;
Reid Spencerc5b206b2006-12-31 05:48:39 +0000546 Params.push_back(Type::Int32Ty);
Chris Lattnerff5bf9c2003-10-23 17:44:53 +0000547 }
548
Duncan Sandsdc024672007-11-27 13:23:08 +0000549 // Create the new function type based on the recomputed parameters.
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000550 FunctionType *NFTy = FunctionType::get(RetTy, Params, FTy->isVarArg());
551
Chris Lattner08227e42003-06-17 22:21:05 +0000552 // Create the new function body and insert it into the module...
Gabor Greif051a9502008-04-06 20:25:17 +0000553 Function *NF = Function::Create(NFTy, F->getLinkage());
Duncan Sands28c3cff2008-05-26 19:58:59 +0000554 NF->copyAttributesFrom(F);
Chris Lattner58d74912008-03-12 17:45:29 +0000555 NF->setParamAttrs(NewPAL);
Chris Lattner08227e42003-06-17 22:21:05 +0000556 F->getParent()->getFunctionList().insert(F, NF);
Chris Lattner046800a2007-02-11 01:08:35 +0000557 NF->takeName(F);
Chris Lattner08227e42003-06-17 22:21:05 +0000558
559 // Loop over all of the callers of the function, transforming the call sites
560 // to pass in a smaller number of arguments into the new function.
561 //
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000562 std::vector<Value*> Args;
Chris Lattner08227e42003-06-17 22:21:05 +0000563 while (!F->use_empty()) {
564 CallSite CS = CallSite::get(F->use_back());
565 Instruction *Call = CS.getInstruction();
Duncan Sands110c8352007-12-21 19:16:16 +0000566 ParamAttrsVec.clear();
Chris Lattner58d74912008-03-12 17:45:29 +0000567 const PAListPtr &CallPAL = CS.getParamAttrs();
Duncan Sands110c8352007-12-21 19:16:16 +0000568
569 // The call return attributes.
Chris Lattner58d74912008-03-12 17:45:29 +0000570 ParameterAttributes RAttrs = CallPAL.getParamAttrs(0);
Duncan Sands110c8352007-12-21 19:16:16 +0000571 // Adjust in case the function was changed to return void.
Duncan Sands6c3470e2008-01-07 17:16:06 +0000572 RAttrs &= ~ParamAttr::typeIncompatible(NF->getReturnType());
Duncan Sands110c8352007-12-21 19:16:16 +0000573 if (RAttrs)
574 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(0, RAttrs));
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000575
Chris Lattner08227e42003-06-17 22:21:05 +0000576 // Loop over the operands, deleting dead ones...
577 CallSite::arg_iterator AI = CS.arg_begin();
Duncan Sands110c8352007-12-21 19:16:16 +0000578 index = 1;
Chris Lattner19bdc032005-05-06 05:34:40 +0000579 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
Duncan Sands110c8352007-12-21 19:16:16 +0000580 I != E; ++I, ++AI, ++index)
581 if (!DeadArguments.count(I)) { // Remove operands for dead arguments
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000582 Args.push_back(*AI);
Chris Lattner58d74912008-03-12 17:45:29 +0000583 if (ParameterAttributes Attrs = CallPAL.getParamAttrs(index))
Duncan Sands110c8352007-12-21 19:16:16 +0000584 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Args.size(), Attrs));
585 }
586
Chris Lattnerff5bf9c2003-10-23 17:44:53 +0000587 if (ExtraArgHack)
Reid Spencerc5b206b2006-12-31 05:48:39 +0000588 Args.push_back(UndefValue::get(Type::Int32Ty));
Chris Lattnerff5bf9c2003-10-23 17:44:53 +0000589
Evan Chengb2fc2a32008-01-17 04:18:54 +0000590 // Push any varargs arguments on the list. Don't forget their attributes.
591 for (; AI != CS.arg_end(); ++AI) {
Chris Lattnerff5bf9c2003-10-23 17:44:53 +0000592 Args.push_back(*AI);
Chris Lattner58d74912008-03-12 17:45:29 +0000593 if (ParameterAttributes Attrs = CallPAL.getParamAttrs(index++))
Evan Chengb2fc2a32008-01-17 04:18:54 +0000594 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Args.size(), Attrs));
595 }
596
597 // Reconstruct the ParamAttrsList based on the vector we constructed.
Chris Lattner58d74912008-03-12 17:45:29 +0000598 PAListPtr NewCallPAL = PAListPtr::get(ParamAttrsVec.begin(),
599 ParamAttrsVec.end());
Chris Lattnerff5bf9c2003-10-23 17:44:53 +0000600
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000601 Instruction *New;
602 if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
Gabor Greif051a9502008-04-06 20:25:17 +0000603 New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),
604 Args.begin(), Args.end(), "", Call);
Chris Lattnerf201dbc2005-05-09 01:05:50 +0000605 cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
Chris Lattner58d74912008-03-12 17:45:29 +0000606 cast<InvokeInst>(New)->setParamAttrs(NewCallPAL);
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000607 } else {
Gabor Greif051a9502008-04-06 20:25:17 +0000608 New = CallInst::Create(NF, Args.begin(), Args.end(), "", Call);
Chris Lattnerf201dbc2005-05-09 01:05:50 +0000609 cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
Chris Lattner58d74912008-03-12 17:45:29 +0000610 cast<CallInst>(New)->setParamAttrs(NewCallPAL);
Chris Lattner1430ef12005-05-06 06:46:58 +0000611 if (cast<CallInst>(Call)->isTailCall())
612 cast<CallInst>(New)->setTailCall();
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000613 }
614 Args.clear();
615
616 if (!Call->use_empty()) {
617 if (New->getType() == Type::VoidTy)
618 Call->replaceAllUsesWith(Constant::getNullValue(Call->getType()));
619 else {
620 Call->replaceAllUsesWith(New);
Chris Lattner046800a2007-02-11 01:08:35 +0000621 New->takeName(Call);
Chris Lattner08227e42003-06-17 22:21:05 +0000622 }
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000623 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000624
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000625 // Finally, remove the old call from the program, reducing the use-count of
626 // F.
Matthijs Kooijman494661c2008-05-30 12:35:46 +0000627 Call->eraseFromParent();
Chris Lattner08227e42003-06-17 22:21:05 +0000628 }
629
630 // Since we have now created the new function, splice the body of the old
631 // function right into the new function, leaving the old rotting hulk of the
632 // function empty.
633 NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());
634
635 // Loop over the argument list, transfering uses of the old arguments over to
636 // the new arguments, also transfering over the names as well. While we're at
637 // it, remove the dead arguments from the DeadArguments list.
638 //
Chris Lattner19bdc032005-05-06 05:34:40 +0000639 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(),
640 I2 = NF->arg_begin();
Chris Lattner08227e42003-06-17 22:21:05 +0000641 I != E; ++I)
642 if (!DeadArguments.count(I)) {
643 // If this is a live argument, move the name and users over to the new
644 // version.
645 I->replaceAllUsesWith(I2);
Chris Lattner046800a2007-02-11 01:08:35 +0000646 I2->takeName(I);
Chris Lattner08227e42003-06-17 22:21:05 +0000647 ++I2;
648 } else {
649 // If this argument is dead, replace any uses of it with null constants
650 // (these are guaranteed to only be operands to call instructions which
651 // will later be simplified).
652 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
653 DeadArguments.erase(I);
654 }
655
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000656 // If we change the return value of the function we must rewrite any return
657 // instructions. Check this now.
658 if (F->getReturnType() != NF->getReturnType())
659 for (Function::iterator BB = NF->begin(), E = NF->end(); BB != E; ++BB)
660 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
Gabor Greif051a9502008-04-06 20:25:17 +0000661 ReturnInst::Create(0, RI);
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000662 BB->getInstList().erase(RI);
663 }
664
Chris Lattner08227e42003-06-17 22:21:05 +0000665 // Now that the old function is dead, delete it.
Matthijs Kooijman494661c2008-05-30 12:35:46 +0000666 F->eraseFromParent();
Chris Lattner08227e42003-06-17 22:21:05 +0000667}
668
Chris Lattnerb12914b2004-09-20 04:48:05 +0000669bool DAE::runOnModule(Module &M) {
Chris Lattner701bc422007-11-15 06:10:55 +0000670 bool Changed = false;
671 // First pass: Do a simple check to see if any functions can have their "..."
672 // removed. We can do this if they never call va_start. This loop cannot be
673 // fused with the next loop, because deleting a function invalidates
674 // information computed while surveying other functions.
675 DOUT << "DAE - Deleting dead varargs\n";
676 for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
677 Function &F = *I++;
678 if (F.getFunctionType()->isVarArg())
679 Changed |= DeleteDeadVarargs(F);
680 }
681
682 // Second phase:loop through the module, determining which arguments are live.
Chris Lattner08227e42003-06-17 22:21:05 +0000683 // We assume all arguments are dead unless proven otherwise (allowing us to
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000684 // determine that dead arguments passed into recursive functions are dead).
Chris Lattner08227e42003-06-17 22:21:05 +0000685 //
Bill Wendling0a81aac2006-11-26 10:02:32 +0000686 DOUT << "DAE - Determining liveness\n";
Chris Lattner701bc422007-11-15 06:10:55 +0000687 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
688 SurveyFunction(*I);
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000689
690 // Loop over the instructions to inspect, propagating liveness among arguments
691 // and return values which are MaybeLive.
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000692 while (!InstructionsToInspect.empty()) {
693 Instruction *I = InstructionsToInspect.back();
694 InstructionsToInspect.pop_back();
Misha Brukmanfd939082005-04-21 23:48:37 +0000695
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000696 if (ReturnInst *RI = dyn_cast<ReturnInst>(I)) {
697 // For return instructions, we just have to check to see if the return
698 // value for the current function is known now to be alive. If so, any
699 // arguments used by it are now alive, and any call instruction return
700 // value is alive as well.
701 if (LiveRetVal.count(RI->getParent()->getParent()))
702 MarkReturnInstArgumentLive(RI);
703
Chris Lattner08227e42003-06-17 22:21:05 +0000704 } else {
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000705 CallSite CS = CallSite::get(I);
706 assert(CS.getInstruction() && "Unknown instruction for the I2I list!");
Chris Lattner08227e42003-06-17 22:21:05 +0000707
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000708 Function *Callee = CS.getCalledFunction();
Misha Brukmanfd939082005-04-21 23:48:37 +0000709
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000710 // If we found a call or invoke instruction on this list, that means that
711 // an argument of the function is a call instruction. If the argument is
712 // live, then the return value of the called instruction is now live.
Chris Lattner08227e42003-06-17 22:21:05 +0000713 //
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000714 CallSite::arg_iterator AI = CS.arg_begin(); // ActualIterator
Chris Lattner19bdc032005-05-06 05:34:40 +0000715 for (Function::arg_iterator FI = Callee->arg_begin(),
716 E = Callee->arg_end(); FI != E; ++AI, ++FI) {
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000717 // If this argument is another call...
718 CallSite ArgCS = CallSite::get(*AI);
719 if (ArgCS.getInstruction() && LiveArguments.count(FI))
720 if (Function *Callee = ArgCS.getCalledFunction())
721 MarkRetValLive(Callee);
722 }
Chris Lattner08227e42003-06-17 22:21:05 +0000723 }
724 }
725
726 // Now we loop over all of the MaybeLive arguments, promoting them to be live
727 // arguments if one of the calls that uses the arguments to the calls they are
728 // passed into requires them to be live. Of course this could make other
729 // arguments live, so process callers recursively.
730 //
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000731 // Because elements can be removed from the MaybeLiveArguments set, copy it to
732 // a temporary vector.
Chris Lattner08227e42003-06-17 22:21:05 +0000733 //
734 std::vector<Argument*> TmpArgList(MaybeLiveArguments.begin(),
735 MaybeLiveArguments.end());
736 for (unsigned i = 0, e = TmpArgList.size(); i != e; ++i) {
737 Argument *MLA = TmpArgList[i];
738 if (MaybeLiveArguments.count(MLA) &&
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000739 isMaybeLiveArgumentNowLive(MLA))
740 MarkArgumentLive(MLA);
Chris Lattner08227e42003-06-17 22:21:05 +0000741 }
742
743 // Recover memory early...
744 CallSites.clear();
745
746 // At this point, we know that all arguments in DeadArguments and
747 // MaybeLiveArguments are dead. If the two sets are empty, there is nothing
748 // to do.
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000749 if (MaybeLiveArguments.empty() && DeadArguments.empty() &&
750 MaybeLiveRetVal.empty() && DeadRetVal.empty())
Chris Lattner701bc422007-11-15 06:10:55 +0000751 return Changed;
Misha Brukmanfd939082005-04-21 23:48:37 +0000752
Chris Lattner08227e42003-06-17 22:21:05 +0000753 // Otherwise, compact into one set, and start eliminating the arguments from
754 // the functions.
755 DeadArguments.insert(MaybeLiveArguments.begin(), MaybeLiveArguments.end());
756 MaybeLiveArguments.clear();
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000757 DeadRetVal.insert(MaybeLiveRetVal.begin(), MaybeLiveRetVal.end());
758 MaybeLiveRetVal.clear();
759
760 LiveArguments.clear();
761 LiveRetVal.clear();
Chris Lattner08227e42003-06-17 22:21:05 +0000762
763 NumArgumentsEliminated += DeadArguments.size();
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000764 NumRetValsEliminated += DeadRetVal.size();
Chris Lattner08227e42003-06-17 22:21:05 +0000765 while (!DeadArguments.empty())
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000766 RemoveDeadArgumentsFromFunction((*DeadArguments.begin())->getParent());
767
768 while (!DeadRetVal.empty())
769 RemoveDeadArgumentsFromFunction(*DeadRetVal.begin());
Chris Lattner08227e42003-06-17 22:21:05 +0000770 return true;
771}