blob: 9b3efe0962bff19603ba9374753f7f3c61eb0ba1 [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());
Chris Lattner4af90ab2006-09-18 07:02:31 +0000166 NF->setCallingConv(Fn.getCallingConv());
Duncan Sandsdc024672007-11-27 13:23:08 +0000167 NF->setParamAttrs(Fn.getParamAttrs());
Gordon Henriksen194c90e2007-12-25 22:16:06 +0000168 if (Fn.hasCollector())
169 NF->setCollector(Fn.getCollector());
Chris Lattner4af90ab2006-09-18 07:02:31 +0000170 Fn.getParent()->getFunctionList().insert(&Fn, NF);
Chris Lattner046800a2007-02-11 01:08:35 +0000171 NF->takeName(&Fn);
Duncan Sands110c8352007-12-21 19:16:16 +0000172
Chris Lattner4af90ab2006-09-18 07:02:31 +0000173 // Loop over all of the callers of the function, transforming the call sites
174 // to pass in a smaller number of arguments into the new function.
175 //
176 std::vector<Value*> Args;
177 while (!Fn.use_empty()) {
178 CallSite CS = CallSite::get(Fn.use_back());
179 Instruction *Call = CS.getInstruction();
Duncan Sands110c8352007-12-21 19:16:16 +0000180
Chris Lattnera0bc7fc2007-10-18 18:49:29 +0000181 // Pass all the same arguments.
Chris Lattner4af90ab2006-09-18 07:02:31 +0000182 Args.assign(CS.arg_begin(), CS.arg_begin()+NumArgs);
Duncan Sands110c8352007-12-21 19:16:16 +0000183
Duncan Sandsbfc5ae62008-01-11 23:13:45 +0000184 // Drop any attributes that were on the vararg arguments.
Chris Lattner58d74912008-03-12 17:45:29 +0000185 PAListPtr PAL = CS.getParamAttrs();
186 if (!PAL.isEmpty() && PAL.getSlot(PAL.getNumSlots() - 1).Index > NumArgs) {
187 SmallVector<ParamAttrsWithIndex, 8> ParamAttrsVec;
188 for (unsigned i = 0; PAL.getSlot(i).Index <= NumArgs; ++i)
189 ParamAttrsVec.push_back(PAL.getSlot(i));
190 PAL = PAListPtr::get(ParamAttrsVec.begin(), ParamAttrsVec.end());
Duncan Sandsbfc5ae62008-01-11 23:13:45 +0000191 }
192
Chris Lattner4af90ab2006-09-18 07:02:31 +0000193 Instruction *New;
194 if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
Gabor Greif051a9502008-04-06 20:25:17 +0000195 New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),
196 Args.begin(), Args.end(), "", Call);
Chris Lattner4af90ab2006-09-18 07:02:31 +0000197 cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
Duncan Sandsbfc5ae62008-01-11 23:13:45 +0000198 cast<InvokeInst>(New)->setParamAttrs(PAL);
Chris Lattner4af90ab2006-09-18 07:02:31 +0000199 } else {
Gabor Greif051a9502008-04-06 20:25:17 +0000200 New = CallInst::Create(NF, Args.begin(), Args.end(), "", Call);
Chris Lattner4af90ab2006-09-18 07:02:31 +0000201 cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
Duncan Sandsbfc5ae62008-01-11 23:13:45 +0000202 cast<CallInst>(New)->setParamAttrs(PAL);
Chris Lattner4af90ab2006-09-18 07:02:31 +0000203 if (cast<CallInst>(Call)->isTailCall())
204 cast<CallInst>(New)->setTailCall();
205 }
206 Args.clear();
Duncan Sands110c8352007-12-21 19:16:16 +0000207
Chris Lattner4af90ab2006-09-18 07:02:31 +0000208 if (!Call->use_empty())
Chris Lattnera0bc7fc2007-10-18 18:49:29 +0000209 Call->replaceAllUsesWith(New);
Duncan Sands110c8352007-12-21 19:16:16 +0000210
Chris Lattner046800a2007-02-11 01:08:35 +0000211 New->takeName(Call);
Duncan Sands110c8352007-12-21 19:16:16 +0000212
Chris Lattner4af90ab2006-09-18 07:02:31 +0000213 // Finally, remove the old call from the program, reducing the use-count of
214 // F.
Chris Lattnera0bc7fc2007-10-18 18:49:29 +0000215 Call->eraseFromParent();
Chris Lattner4af90ab2006-09-18 07:02:31 +0000216 }
Duncan Sands110c8352007-12-21 19:16:16 +0000217
Chris Lattner4af90ab2006-09-18 07:02:31 +0000218 // Since we have now created the new function, splice the body of the old
219 // function right into the new function, leaving the old rotting hulk of the
220 // function empty.
221 NF->getBasicBlockList().splice(NF->begin(), Fn.getBasicBlockList());
Duncan Sands110c8352007-12-21 19:16:16 +0000222
Chris Lattner4af90ab2006-09-18 07:02:31 +0000223 // Loop over the argument list, transfering uses of the old arguments over to
224 // the new arguments, also transfering over the names as well. While we're at
225 // it, remove the dead arguments from the DeadArguments list.
226 //
227 for (Function::arg_iterator I = Fn.arg_begin(), E = Fn.arg_end(),
228 I2 = NF->arg_begin(); I != E; ++I, ++I2) {
229 // Move the name and users over to the new version.
230 I->replaceAllUsesWith(I2);
Chris Lattner046800a2007-02-11 01:08:35 +0000231 I2->takeName(I);
Chris Lattner4af90ab2006-09-18 07:02:31 +0000232 }
Duncan Sands110c8352007-12-21 19:16:16 +0000233
Chris Lattner4af90ab2006-09-18 07:02:31 +0000234 // Finally, nuke the old function.
235 Fn.eraseFromParent();
236 return true;
237}
238
239
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000240static inline bool CallPassesValueThoughVararg(Instruction *Call,
241 const Value *Arg) {
242 CallSite CS = CallSite::get(Call);
243 const Type *CalledValueTy = CS.getCalledValue()->getType();
244 const Type *FTy = cast<PointerType>(CalledValueTy)->getElementType();
245 unsigned NumFixedArgs = cast<FunctionType>(FTy)->getNumParams();
246 for (CallSite::arg_iterator AI = CS.arg_begin()+NumFixedArgs;
247 AI != CS.arg_end(); ++AI)
248 if (AI->get() == Arg)
249 return true;
Chris Lattner08227e42003-06-17 22:21:05 +0000250 return false;
251}
252
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000253// getArgumentLiveness - Inspect an argument, determining if is known Live
Chris Lattner08227e42003-06-17 22:21:05 +0000254// (used in a computation), MaybeLive (only passed as an argument to a call), or
255// Dead (not used).
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000256DAE::Liveness DAE::getArgumentLiveness(const Argument &A) {
Duncan Sandsdc024672007-11-27 13:23:08 +0000257 const Function *F = A.getParent();
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000258
259 // If this is the return value of a struct function, it's not really dead.
Devang Patel41e23972008-03-03 21:46:28 +0000260 if (F->hasStructRetAttr() && &*(F->arg_begin()) == &A)
Chris Lattner92044ce2006-06-27 21:05:04 +0000261 return Live;
262
263 if (A.use_empty()) // First check, directly dead?
264 return Dead;
Chris Lattner08227e42003-06-17 22:21:05 +0000265
266 // Scan through all of the uses, looking for non-argument passing uses.
267 for (Value::use_const_iterator I = A.use_begin(), E = A.use_end(); I!=E;++I) {
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000268 // Return instructions do not immediately effect liveness.
269 if (isa<ReturnInst>(*I))
270 continue;
271
Chris Lattner08227e42003-06-17 22:21:05 +0000272 CallSite CS = CallSite::get(const_cast<User*>(*I));
273 if (!CS.getInstruction()) {
274 // If its used by something that is not a call or invoke, it's alive!
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000275 return Live;
Chris Lattner08227e42003-06-17 22:21:05 +0000276 }
277 // If it's an indirect call, mark it alive...
278 Function *Callee = CS.getCalledFunction();
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000279 if (!Callee) return Live;
Chris Lattner08227e42003-06-17 22:21:05 +0000280
Chris Lattner97f4b662003-06-18 16:25:51 +0000281 // Check to see if it's passed through a va_arg area: if so, we cannot
282 // remove it.
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000283 if (CallPassesValueThoughVararg(CS.getInstruction(), &A))
284 return Live; // If passed through va_arg area, we cannot remove it
Chris Lattner08227e42003-06-17 22:21:05 +0000285 }
286
287 return MaybeLive; // It must be used, but only as argument to a function
288}
289
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000290
291// SurveyFunction - This performs the initial survey of the specified function,
292// checking out whether or not it uses any of its incoming arguments or whether
293// any callers use the return value. This fills in the
294// (Dead|MaybeLive|Live)(Arguments|RetVal) sets.
Chris Lattner08227e42003-06-17 22:21:05 +0000295//
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000296// We consider arguments of non-internal functions to be intrinsically alive as
297// well as arguments to functions which have their "address taken".
298//
299void DAE::SurveyFunction(Function &F) {
300 bool FunctionIntrinsicallyLive = false;
301 Liveness RetValLiveness = F.getReturnType() == Type::VoidTy ? Live : Dead;
302
Chris Lattnerb6e06312003-11-05 21:53:41 +0000303 if (!F.hasInternalLinkage() &&
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000304 (!ShouldHackArguments() || F.isIntrinsic()))
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000305 FunctionIntrinsicallyLive = true;
Misha Brukmanfd939082005-04-21 23:48:37 +0000306 else
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000307 for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I) {
308 // If this use is anything other than a call site, the function is alive.
309 CallSite CS = CallSite::get(*I);
310 Instruction *TheCall = CS.getInstruction();
311 if (!TheCall) { // Not a direct call site?
312 FunctionIntrinsicallyLive = true;
313 break;
314 }
315
316 // Check to see if the return value is used...
317 if (RetValLiveness != Live)
318 for (Value::use_iterator I = TheCall->use_begin(),
319 E = TheCall->use_end(); I != E; ++I)
320 if (isa<ReturnInst>(cast<Instruction>(*I))) {
321 RetValLiveness = MaybeLive;
322 } else if (isa<CallInst>(cast<Instruction>(*I)) ||
323 isa<InvokeInst>(cast<Instruction>(*I))) {
324 if (CallPassesValueThoughVararg(cast<Instruction>(*I), TheCall) ||
325 !CallSite::get(cast<Instruction>(*I)).getCalledFunction()) {
326 RetValLiveness = Live;
327 break;
328 } else {
329 RetValLiveness = MaybeLive;
330 }
331 } else {
332 RetValLiveness = Live;
333 break;
334 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000335
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000336 // If the function is PASSED IN as an argument, its address has been taken
337 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
338 AI != E; ++AI)
339 if (AI->get() == &F) {
340 FunctionIntrinsicallyLive = true;
341 break;
342 }
343 if (FunctionIntrinsicallyLive) break;
344 }
345
346 if (FunctionIntrinsicallyLive) {
Bill Wendling0a81aac2006-11-26 10:02:32 +0000347 DOUT << " Intrinsically live fn: " << F.getName() << "\n";
Chris Lattner19bdc032005-05-06 05:34:40 +0000348 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
349 AI != E; ++AI)
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000350 LiveArguments.insert(AI);
351 LiveRetVal.insert(&F);
352 return;
353 }
354
355 switch (RetValLiveness) {
356 case Live: LiveRetVal.insert(&F); break;
357 case MaybeLive: MaybeLiveRetVal.insert(&F); break;
358 case Dead: DeadRetVal.insert(&F); break;
359 }
360
Bill Wendling0a81aac2006-11-26 10:02:32 +0000361 DOUT << " Inspecting args for fn: " << F.getName() << "\n";
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000362
363 // If it is not intrinsically alive, we know that all users of the
364 // function are call sites. Mark all of the arguments live which are
365 // directly used, and keep track of all of the call sites of this function
366 // if there are any arguments we assume that are dead.
367 //
368 bool AnyMaybeLiveArgs = false;
Chris Lattner19bdc032005-05-06 05:34:40 +0000369 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
370 AI != E; ++AI)
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000371 switch (getArgumentLiveness(*AI)) {
372 case Live:
Bill Wendling0a81aac2006-11-26 10:02:32 +0000373 DOUT << " Arg live by use: " << AI->getName() << "\n";
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000374 LiveArguments.insert(AI);
375 break;
376 case Dead:
Bill Wendling0a81aac2006-11-26 10:02:32 +0000377 DOUT << " Arg definitely dead: " << AI->getName() <<"\n";
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000378 DeadArguments.insert(AI);
379 break;
380 case MaybeLive:
Bill Wendling0a81aac2006-11-26 10:02:32 +0000381 DOUT << " Arg only passed to calls: " << AI->getName() << "\n";
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000382 AnyMaybeLiveArgs = true;
383 MaybeLiveArguments.insert(AI);
384 break;
385 }
386
387 // If there are any "MaybeLive" arguments, we need to check callees of
388 // this function when/if they become alive. Record which functions are
389 // callees...
390 if (AnyMaybeLiveArgs || RetValLiveness == MaybeLive)
391 for (Value::use_iterator I = F.use_begin(), E = F.use_end();
392 I != E; ++I) {
393 if (AnyMaybeLiveArgs)
394 CallSites.insert(std::make_pair(&F, CallSite::get(*I)));
395
396 if (RetValLiveness == MaybeLive)
397 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
398 UI != E; ++UI)
399 InstructionsToInspect.push_back(cast<Instruction>(*UI));
400 }
401}
402
403// isMaybeLiveArgumentNowLive - Check to see if Arg is alive. At this point, we
404// know that the only uses of Arg are to be passed in as an argument to a
405// function call or return. Check to see if the formal argument passed in is in
406// the LiveArguments set. If so, return true.
407//
408bool DAE::isMaybeLiveArgumentNowLive(Argument *Arg) {
Chris Lattner08227e42003-06-17 22:21:05 +0000409 for (Value::use_iterator I = Arg->use_begin(), E = Arg->use_end(); I!=E; ++I){
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000410 if (isa<ReturnInst>(*I)) {
411 if (LiveRetVal.count(Arg->getParent())) return true;
412 continue;
413 }
414
Chris Lattner08227e42003-06-17 22:21:05 +0000415 CallSite CS = CallSite::get(*I);
416
417 // We know that this can only be used for direct calls...
Chris Lattnerd6d0d8c2003-11-02 02:06:27 +0000418 Function *Callee = CS.getCalledFunction();
Chris Lattner08227e42003-06-17 22:21:05 +0000419
420 // Loop over all of the arguments (because Arg may be passed into the call
421 // multiple times) and check to see if any are now alive...
422 CallSite::arg_iterator CSAI = CS.arg_begin();
Chris Lattnere4d5c442005-03-15 04:54:21 +0000423 for (Function::arg_iterator AI = Callee->arg_begin(), E = Callee->arg_end();
Chris Lattner08227e42003-06-17 22:21:05 +0000424 AI != E; ++AI, ++CSAI)
425 // If this is the argument we are looking for, check to see if it's alive
426 if (*CSAI == Arg && LiveArguments.count(AI))
427 return true;
428 }
429 return false;
430}
431
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000432/// MarkArgumentLive - The MaybeLive argument 'Arg' is now known to be alive.
433/// Mark it live in the specified sets and recursively mark arguments in callers
434/// live that are needed to pass in a value.
435///
436void DAE::MarkArgumentLive(Argument *Arg) {
437 std::set<Argument*>::iterator It = MaybeLiveArguments.lower_bound(Arg);
438 if (It == MaybeLiveArguments.end() || *It != Arg) return;
Misha Brukmanfd939082005-04-21 23:48:37 +0000439
Bill Wendling0a81aac2006-11-26 10:02:32 +0000440 DOUT << " MaybeLive argument now live: " << Arg->getName() <<"\n";
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000441 MaybeLiveArguments.erase(It);
Chris Lattner08227e42003-06-17 22:21:05 +0000442 LiveArguments.insert(Arg);
Misha Brukmanfd939082005-04-21 23:48:37 +0000443
Chris Lattner08227e42003-06-17 22:21:05 +0000444 // Loop over all of the call sites of the function, making any arguments
445 // passed in to provide a value for this argument live as necessary.
446 //
447 Function *Fn = Arg->getParent();
Chris Lattnere4d5c442005-03-15 04:54:21 +0000448 unsigned ArgNo = std::distance(Fn->arg_begin(), Function::arg_iterator(Arg));
Chris Lattner08227e42003-06-17 22:21:05 +0000449
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000450 std::multimap<Function*, CallSite>::iterator I = CallSites.lower_bound(Fn);
Chris Lattner08227e42003-06-17 22:21:05 +0000451 for (; I != CallSites.end() && I->first == Fn; ++I) {
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000452 CallSite CS = I->second;
453 Value *ArgVal = *(CS.arg_begin()+ArgNo);
454 if (Argument *ActualArg = dyn_cast<Argument>(ArgVal)) {
455 MarkArgumentLive(ActualArg);
456 } else {
457 // If the value passed in at this call site is a return value computed by
458 // some other call site, make sure to mark the return value at the other
459 // call site as being needed.
460 CallSite ArgCS = CallSite::get(ArgVal);
461 if (ArgCS.getInstruction())
462 if (Function *Fn = ArgCS.getCalledFunction())
463 MarkRetValLive(Fn);
464 }
465 }
466}
467
468/// MarkArgumentLive - The MaybeLive return value for the specified function is
469/// now known to be alive. Propagate this fact to the return instructions which
470/// produce it.
471void DAE::MarkRetValLive(Function *F) {
472 assert(F && "Shame shame, we can't have null pointers here!");
473
474 // Check to see if we already knew it was live
475 std::set<Function*>::iterator I = MaybeLiveRetVal.lower_bound(F);
476 if (I == MaybeLiveRetVal.end() || *I != F) return; // It's already alive!
477
Bill Wendling0a81aac2006-11-26 10:02:32 +0000478 DOUT << " MaybeLive retval now live: " << F->getName() << "\n";
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000479
480 MaybeLiveRetVal.erase(I);
481 LiveRetVal.insert(F); // It is now known to be live!
482
483 // Loop over all of the functions, noticing that the return value is now live.
484 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
485 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()))
486 MarkReturnInstArgumentLive(RI);
487}
488
489void DAE::MarkReturnInstArgumentLive(ReturnInst *RI) {
490 Value *Op = RI->getOperand(0);
491 if (Argument *A = dyn_cast<Argument>(Op)) {
492 MarkArgumentLive(A);
493 } else if (CallInst *CI = dyn_cast<CallInst>(Op)) {
494 if (Function *F = CI->getCalledFunction())
495 MarkRetValLive(F);
496 } else if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) {
497 if (Function *F = II->getCalledFunction())
498 MarkRetValLive(F);
Chris Lattner08227e42003-06-17 22:21:05 +0000499 }
500}
501
502// RemoveDeadArgumentsFromFunction - We know that F has dead arguments, as
503// specified by the DeadArguments list. Transform the function and all of the
504// callees of the function to not have these arguments.
505//
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000506void DAE::RemoveDeadArgumentsFromFunction(Function *F) {
Chris Lattner08227e42003-06-17 22:21:05 +0000507 // Start by computing a new prototype for the function, which is the same as
508 // the old function, but has fewer arguments.
509 const FunctionType *FTy = F->getFunctionType();
510 std::vector<const Type*> Params;
511
Duncan Sandsdc024672007-11-27 13:23:08 +0000512 // Set up to build a new list of parameter attributes
Chris Lattner58d74912008-03-12 17:45:29 +0000513 SmallVector<ParamAttrsWithIndex, 8> ParamAttrsVec;
514 const PAListPtr &PAL = F->getParamAttrs();
Chris Lattner08227e42003-06-17 22:21:05 +0000515
Duncan Sands110c8352007-12-21 19:16:16 +0000516 // The existing function return attributes.
Chris Lattner58d74912008-03-12 17:45:29 +0000517 ParameterAttributes RAttrs = PAL.getParamAttrs(0);
Duncan Sands110c8352007-12-21 19:16:16 +0000518
519 // Make the function return void if the return value is dead.
520 const Type *RetTy = FTy->getReturnType();
521 if (DeadRetVal.count(F)) {
522 RetTy = Type::VoidTy;
Duncan Sands6c3470e2008-01-07 17:16:06 +0000523 RAttrs &= ~ParamAttr::typeIncompatible(RetTy);
Duncan Sands110c8352007-12-21 19:16:16 +0000524 DeadRetVal.erase(F);
525 }
526
527 if (RAttrs)
528 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(0, RAttrs));
529
Duncan Sandsdc024672007-11-27 13:23:08 +0000530 // Construct the new parameter list from non-dead arguments. Also construct
531 // a new set of parameter attributes to correspond.
532 unsigned index = 1;
533 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E;
534 ++I, ++index)
535 if (!DeadArguments.count(I)) {
536 Params.push_back(I->getType());
Chris Lattner58d74912008-03-12 17:45:29 +0000537
538 if (ParameterAttributes Attrs = PAL.getParamAttrs(index))
Duncan Sands110c8352007-12-21 19:16:16 +0000539 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Params.size(), Attrs));
Duncan Sandsdc024672007-11-27 13:23:08 +0000540 }
541
542 // Reconstruct the ParamAttrsList based on the vector we constructed.
Chris Lattner58d74912008-03-12 17:45:29 +0000543 PAListPtr NewPAL = PAListPtr::get(ParamAttrsVec.begin(), ParamAttrsVec.end());
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000544
Chris Lattnerff5bf9c2003-10-23 17:44:53 +0000545 // Work around LLVM bug PR56: the CWriter cannot emit varargs functions which
546 // have zero fixed arguments.
547 //
Chris Lattnerff5bf9c2003-10-23 17:44:53 +0000548 bool ExtraArgHack = false;
549 if (Params.empty() && FTy->isVarArg()) {
550 ExtraArgHack = true;
Reid Spencerc5b206b2006-12-31 05:48:39 +0000551 Params.push_back(Type::Int32Ty);
Chris Lattnerff5bf9c2003-10-23 17:44:53 +0000552 }
553
Duncan Sandsdc024672007-11-27 13:23:08 +0000554 // Create the new function type based on the recomputed parameters.
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000555 FunctionType *NFTy = FunctionType::get(RetTy, Params, FTy->isVarArg());
556
Chris Lattner08227e42003-06-17 22:21:05 +0000557 // Create the new function body and insert it into the module...
Gabor Greif051a9502008-04-06 20:25:17 +0000558 Function *NF = Function::Create(NFTy, F->getLinkage());
Chris Lattnerf201dbc2005-05-09 01:05:50 +0000559 NF->setCallingConv(F->getCallingConv());
Chris Lattner58d74912008-03-12 17:45:29 +0000560 NF->setParamAttrs(NewPAL);
Gordon Henriksen194c90e2007-12-25 22:16:06 +0000561 if (F->hasCollector())
562 NF->setCollector(F->getCollector());
Chris Lattner08227e42003-06-17 22:21:05 +0000563 F->getParent()->getFunctionList().insert(F, NF);
Chris Lattner046800a2007-02-11 01:08:35 +0000564 NF->takeName(F);
Chris Lattner08227e42003-06-17 22:21:05 +0000565
566 // Loop over all of the callers of the function, transforming the call sites
567 // to pass in a smaller number of arguments into the new function.
568 //
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000569 std::vector<Value*> Args;
Chris Lattner08227e42003-06-17 22:21:05 +0000570 while (!F->use_empty()) {
571 CallSite CS = CallSite::get(F->use_back());
572 Instruction *Call = CS.getInstruction();
Duncan Sands110c8352007-12-21 19:16:16 +0000573 ParamAttrsVec.clear();
Chris Lattner58d74912008-03-12 17:45:29 +0000574 const PAListPtr &CallPAL = CS.getParamAttrs();
Duncan Sands110c8352007-12-21 19:16:16 +0000575
576 // The call return attributes.
Chris Lattner58d74912008-03-12 17:45:29 +0000577 ParameterAttributes RAttrs = CallPAL.getParamAttrs(0);
Duncan Sands110c8352007-12-21 19:16:16 +0000578 // Adjust in case the function was changed to return void.
Duncan Sands6c3470e2008-01-07 17:16:06 +0000579 RAttrs &= ~ParamAttr::typeIncompatible(NF->getReturnType());
Duncan Sands110c8352007-12-21 19:16:16 +0000580 if (RAttrs)
581 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(0, RAttrs));
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000582
Chris Lattner08227e42003-06-17 22:21:05 +0000583 // Loop over the operands, deleting dead ones...
584 CallSite::arg_iterator AI = CS.arg_begin();
Duncan Sands110c8352007-12-21 19:16:16 +0000585 index = 1;
Chris Lattner19bdc032005-05-06 05:34:40 +0000586 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
Duncan Sands110c8352007-12-21 19:16:16 +0000587 I != E; ++I, ++AI, ++index)
588 if (!DeadArguments.count(I)) { // Remove operands for dead arguments
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000589 Args.push_back(*AI);
Chris Lattner58d74912008-03-12 17:45:29 +0000590 if (ParameterAttributes Attrs = CallPAL.getParamAttrs(index))
Duncan Sands110c8352007-12-21 19:16:16 +0000591 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Args.size(), Attrs));
592 }
593
Chris Lattnerff5bf9c2003-10-23 17:44:53 +0000594 if (ExtraArgHack)
Reid Spencerc5b206b2006-12-31 05:48:39 +0000595 Args.push_back(UndefValue::get(Type::Int32Ty));
Chris Lattnerff5bf9c2003-10-23 17:44:53 +0000596
Evan Chengb2fc2a32008-01-17 04:18:54 +0000597 // Push any varargs arguments on the list. Don't forget their attributes.
598 for (; AI != CS.arg_end(); ++AI) {
Chris Lattnerff5bf9c2003-10-23 17:44:53 +0000599 Args.push_back(*AI);
Chris Lattner58d74912008-03-12 17:45:29 +0000600 if (ParameterAttributes Attrs = CallPAL.getParamAttrs(index++))
Evan Chengb2fc2a32008-01-17 04:18:54 +0000601 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Args.size(), Attrs));
602 }
603
604 // Reconstruct the ParamAttrsList based on the vector we constructed.
Chris Lattner58d74912008-03-12 17:45:29 +0000605 PAListPtr NewCallPAL = PAListPtr::get(ParamAttrsVec.begin(),
606 ParamAttrsVec.end());
Chris Lattnerff5bf9c2003-10-23 17:44:53 +0000607
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000608 Instruction *New;
609 if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
Gabor Greif051a9502008-04-06 20:25:17 +0000610 New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),
611 Args.begin(), Args.end(), "", Call);
Chris Lattnerf201dbc2005-05-09 01:05:50 +0000612 cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
Chris Lattner58d74912008-03-12 17:45:29 +0000613 cast<InvokeInst>(New)->setParamAttrs(NewCallPAL);
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000614 } else {
Gabor Greif051a9502008-04-06 20:25:17 +0000615 New = CallInst::Create(NF, Args.begin(), Args.end(), "", Call);
Chris Lattnerf201dbc2005-05-09 01:05:50 +0000616 cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
Chris Lattner58d74912008-03-12 17:45:29 +0000617 cast<CallInst>(New)->setParamAttrs(NewCallPAL);
Chris Lattner1430ef12005-05-06 06:46:58 +0000618 if (cast<CallInst>(Call)->isTailCall())
619 cast<CallInst>(New)->setTailCall();
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000620 }
621 Args.clear();
622
623 if (!Call->use_empty()) {
624 if (New->getType() == Type::VoidTy)
625 Call->replaceAllUsesWith(Constant::getNullValue(Call->getType()));
626 else {
627 Call->replaceAllUsesWith(New);
Chris Lattner046800a2007-02-11 01:08:35 +0000628 New->takeName(Call);
Chris Lattner08227e42003-06-17 22:21:05 +0000629 }
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000630 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000631
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000632 // Finally, remove the old call from the program, reducing the use-count of
633 // F.
634 Call->getParent()->getInstList().erase(Call);
Chris Lattner08227e42003-06-17 22:21:05 +0000635 }
636
637 // Since we have now created the new function, splice the body of the old
638 // function right into the new function, leaving the old rotting hulk of the
639 // function empty.
640 NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());
641
642 // Loop over the argument list, transfering uses of the old arguments over to
643 // the new arguments, also transfering over the names as well. While we're at
644 // it, remove the dead arguments from the DeadArguments list.
645 //
Chris Lattner19bdc032005-05-06 05:34:40 +0000646 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(),
647 I2 = NF->arg_begin();
Chris Lattner08227e42003-06-17 22:21:05 +0000648 I != E; ++I)
649 if (!DeadArguments.count(I)) {
650 // If this is a live argument, move the name and users over to the new
651 // version.
652 I->replaceAllUsesWith(I2);
Chris Lattner046800a2007-02-11 01:08:35 +0000653 I2->takeName(I);
Chris Lattner08227e42003-06-17 22:21:05 +0000654 ++I2;
655 } else {
656 // If this argument is dead, replace any uses of it with null constants
657 // (these are guaranteed to only be operands to call instructions which
658 // will later be simplified).
659 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
660 DeadArguments.erase(I);
661 }
662
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000663 // If we change the return value of the function we must rewrite any return
664 // instructions. Check this now.
665 if (F->getReturnType() != NF->getReturnType())
666 for (Function::iterator BB = NF->begin(), E = NF->end(); BB != E; ++BB)
667 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
Gabor Greif051a9502008-04-06 20:25:17 +0000668 ReturnInst::Create(0, RI);
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000669 BB->getInstList().erase(RI);
670 }
671
Chris Lattner08227e42003-06-17 22:21:05 +0000672 // Now that the old function is dead, delete it.
673 F->getParent()->getFunctionList().erase(F);
674}
675
Chris Lattnerb12914b2004-09-20 04:48:05 +0000676bool DAE::runOnModule(Module &M) {
Chris Lattner701bc422007-11-15 06:10:55 +0000677 bool Changed = false;
678 // First pass: Do a simple check to see if any functions can have their "..."
679 // removed. We can do this if they never call va_start. This loop cannot be
680 // fused with the next loop, because deleting a function invalidates
681 // information computed while surveying other functions.
682 DOUT << "DAE - Deleting dead varargs\n";
683 for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
684 Function &F = *I++;
685 if (F.getFunctionType()->isVarArg())
686 Changed |= DeleteDeadVarargs(F);
687 }
688
689 // Second phase:loop through the module, determining which arguments are live.
Chris Lattner08227e42003-06-17 22:21:05 +0000690 // We assume all arguments are dead unless proven otherwise (allowing us to
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000691 // determine that dead arguments passed into recursive functions are dead).
Chris Lattner08227e42003-06-17 22:21:05 +0000692 //
Bill Wendling0a81aac2006-11-26 10:02:32 +0000693 DOUT << "DAE - Determining liveness\n";
Chris Lattner701bc422007-11-15 06:10:55 +0000694 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
695 SurveyFunction(*I);
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000696
697 // Loop over the instructions to inspect, propagating liveness among arguments
698 // and return values which are MaybeLive.
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000699 while (!InstructionsToInspect.empty()) {
700 Instruction *I = InstructionsToInspect.back();
701 InstructionsToInspect.pop_back();
Misha Brukmanfd939082005-04-21 23:48:37 +0000702
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000703 if (ReturnInst *RI = dyn_cast<ReturnInst>(I)) {
704 // For return instructions, we just have to check to see if the return
705 // value for the current function is known now to be alive. If so, any
706 // arguments used by it are now alive, and any call instruction return
707 // value is alive as well.
708 if (LiveRetVal.count(RI->getParent()->getParent()))
709 MarkReturnInstArgumentLive(RI);
710
Chris Lattner08227e42003-06-17 22:21:05 +0000711 } else {
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000712 CallSite CS = CallSite::get(I);
713 assert(CS.getInstruction() && "Unknown instruction for the I2I list!");
Chris Lattner08227e42003-06-17 22:21:05 +0000714
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000715 Function *Callee = CS.getCalledFunction();
Misha Brukmanfd939082005-04-21 23:48:37 +0000716
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000717 // If we found a call or invoke instruction on this list, that means that
718 // an argument of the function is a call instruction. If the argument is
719 // live, then the return value of the called instruction is now live.
Chris Lattner08227e42003-06-17 22:21:05 +0000720 //
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000721 CallSite::arg_iterator AI = CS.arg_begin(); // ActualIterator
Chris Lattner19bdc032005-05-06 05:34:40 +0000722 for (Function::arg_iterator FI = Callee->arg_begin(),
723 E = Callee->arg_end(); FI != E; ++AI, ++FI) {
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000724 // If this argument is another call...
725 CallSite ArgCS = CallSite::get(*AI);
726 if (ArgCS.getInstruction() && LiveArguments.count(FI))
727 if (Function *Callee = ArgCS.getCalledFunction())
728 MarkRetValLive(Callee);
729 }
Chris Lattner08227e42003-06-17 22:21:05 +0000730 }
731 }
732
733 // Now we loop over all of the MaybeLive arguments, promoting them to be live
734 // arguments if one of the calls that uses the arguments to the calls they are
735 // passed into requires them to be live. Of course this could make other
736 // arguments live, so process callers recursively.
737 //
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000738 // Because elements can be removed from the MaybeLiveArguments set, copy it to
739 // a temporary vector.
Chris Lattner08227e42003-06-17 22:21:05 +0000740 //
741 std::vector<Argument*> TmpArgList(MaybeLiveArguments.begin(),
742 MaybeLiveArguments.end());
743 for (unsigned i = 0, e = TmpArgList.size(); i != e; ++i) {
744 Argument *MLA = TmpArgList[i];
745 if (MaybeLiveArguments.count(MLA) &&
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000746 isMaybeLiveArgumentNowLive(MLA))
747 MarkArgumentLive(MLA);
Chris Lattner08227e42003-06-17 22:21:05 +0000748 }
749
750 // Recover memory early...
751 CallSites.clear();
752
753 // At this point, we know that all arguments in DeadArguments and
754 // MaybeLiveArguments are dead. If the two sets are empty, there is nothing
755 // to do.
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000756 if (MaybeLiveArguments.empty() && DeadArguments.empty() &&
757 MaybeLiveRetVal.empty() && DeadRetVal.empty())
Chris Lattner701bc422007-11-15 06:10:55 +0000758 return Changed;
Misha Brukmanfd939082005-04-21 23:48:37 +0000759
Chris Lattner08227e42003-06-17 22:21:05 +0000760 // Otherwise, compact into one set, and start eliminating the arguments from
761 // the functions.
762 DeadArguments.insert(MaybeLiveArguments.begin(), MaybeLiveArguments.end());
763 MaybeLiveArguments.clear();
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000764 DeadRetVal.insert(MaybeLiveRetVal.begin(), MaybeLiveRetVal.end());
765 MaybeLiveRetVal.clear();
766
767 LiveArguments.clear();
768 LiveRetVal.clear();
Chris Lattner08227e42003-06-17 22:21:05 +0000769
770 NumArgumentsEliminated += DeadArguments.size();
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000771 NumRetValsEliminated += DeadRetVal.size();
Chris Lattner08227e42003-06-17 22:21:05 +0000772 while (!DeadArguments.empty())
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000773 RemoveDeadArgumentsFromFunction((*DeadArguments.begin())->getParent());
774
775 while (!DeadRetVal.empty())
776 RemoveDeadArgumentsFromFunction(*DeadRetVal.begin());
Chris Lattner08227e42003-06-17 22:21:05 +0000777 return true;
778}