blob: 37fb46d8ab39487f47eb90d090739279d4b66b69 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris 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"
22#include "llvm/Module.h"
23#include "llvm/Pass.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/Constant.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000026#include "llvm/Instructions.h"
Chris Lattner08227e42003-06-17 22:21:05 +000027#include "llvm/Support/CallSite.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000028#include "llvm/Support/Debug.h"
29#include "llvm/ADT/Statistic.h"
30#include "llvm/ADT/iterator"
Chris Lattnerdac58ad2006-01-22 23:32:06 +000031#include <iostream>
Chris Lattner08227e42003-06-17 22:21:05 +000032#include <set>
Chris Lattner1e2385b2003-11-21 21:54:22 +000033using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattner08227e42003-06-17 22:21:05 +000035namespace {
Chris Lattnerc3afd9b2003-10-23 03:48:17 +000036 Statistic<> NumArgumentsEliminated("deadargelim",
37 "Number of unread args removed");
38 Statistic<> NumRetValsEliminated("deadargelim",
39 "Number of unused return values removed");
Chris Lattner08227e42003-06-17 22:21:05 +000040
Chris Lattnerc3afd9b2003-10-23 03:48:17 +000041 /// DAE - The dead argument elimination pass.
42 ///
Chris Lattnerb12914b2004-09-20 04:48:05 +000043 class DAE : public ModulePass {
Chris Lattnerc3afd9b2003-10-23 03:48:17 +000044 /// Liveness enum - During our initial pass over the program, we determine
45 /// that things are either definately alive, definately dead, or in need of
46 /// interprocedural analysis (MaybeLive).
47 ///
48 enum Liveness { Live, MaybeLive, Dead };
49
50 /// LiveArguments, MaybeLiveArguments, DeadArguments - These sets contain
51 /// all of the arguments in the program. The Dead set contains arguments
52 /// which are completely dead (never used in the function). The MaybeLive
53 /// set contains arguments which are only passed into other function calls,
54 /// thus may be live and may be dead. The Live set contains arguments which
55 /// are known to be alive.
56 ///
57 std::set<Argument*> DeadArguments, MaybeLiveArguments, LiveArguments;
58
59 /// DeadRetVal, MaybeLiveRetVal, LifeRetVal - These sets contain all of the
60 /// functions in the program. The Dead set contains functions whose return
61 /// value is known to be dead. The MaybeLive set contains functions whose
62 /// return values are only used by return instructions, and the Live set
63 /// contains functions whose return values are used, functions that are
64 /// external, and functions that already return void.
65 ///
66 std::set<Function*> DeadRetVal, MaybeLiveRetVal, LiveRetVal;
67
68 /// InstructionsToInspect - As we mark arguments and return values
69 /// MaybeLive, we keep track of which instructions could make the values
70 /// live here. Once the entire program has had the return value and
71 /// arguments analyzed, this set is scanned to promote the MaybeLive objects
72 /// to be Live if they really are used.
73 std::vector<Instruction*> InstructionsToInspect;
74
75 /// CallSites - Keep track of the call sites of functions that have
76 /// MaybeLive arguments or return values.
77 std::multimap<Function*, CallSite> CallSites;
78
79 public:
Chris Lattnerb12914b2004-09-20 04:48:05 +000080 bool runOnModule(Module &M);
Chris Lattner9b2a14b2003-06-25 04:12:49 +000081
Chris Lattnerfdcc3ac2003-11-05 21:43:02 +000082 virtual bool ShouldHackArguments() const { return false; }
83
Chris Lattner9b2a14b2003-06-25 04:12:49 +000084 private:
Chris Lattnerc3afd9b2003-10-23 03:48:17 +000085 Liveness getArgumentLiveness(const Argument &A);
86 bool isMaybeLiveArgumentNowLive(Argument *Arg);
87
88 void SurveyFunction(Function &Fn);
89
90 void MarkArgumentLive(Argument *Arg);
91 void MarkRetValLive(Function *F);
92 void MarkReturnInstArgumentLive(ReturnInst *RI);
Misha Brukmanfd939082005-04-21 23:48:37 +000093
Chris Lattnerc3afd9b2003-10-23 03:48:17 +000094 void RemoveDeadArgumentsFromFunction(Function *F);
Chris Lattner08227e42003-06-17 22:21:05 +000095 };
96 RegisterOpt<DAE> X("deadargelim", "Dead Argument Elimination");
Chris Lattnerfdcc3ac2003-11-05 21:43:02 +000097
98 /// DAH - DeadArgumentHacking pass - Same as dead argument elimination, but
99 /// deletes arguments to functions which are external. This is only for use
100 /// by bugpoint.
101 struct DAH : public DAE {
102 virtual bool ShouldHackArguments() const { return true; }
103 };
Chris Lattnerb6e06312003-11-05 21:53:41 +0000104 RegisterPass<DAH> Y("deadarghaX0r",
Brian Gaeke09ca4112004-02-02 19:32:27 +0000105 "Dead Argument Hacking (BUGPOINT USE ONLY; DO NOT USE)");
Chris Lattner08227e42003-06-17 22:21:05 +0000106}
107
Chris Lattner9b2a14b2003-06-25 04:12:49 +0000108/// createDeadArgEliminationPass - This pass removes arguments from functions
Chris Lattnerfdcc3ac2003-11-05 21:43:02 +0000109/// which are not used by the body of the function.
Chris Lattner9b2a14b2003-06-25 04:12:49 +0000110///
Chris Lattnerb12914b2004-09-20 04:48:05 +0000111ModulePass *llvm::createDeadArgEliminationPass() { return new DAE(); }
112ModulePass *llvm::createDeadArgHackingPass() { return new DAH(); }
Chris Lattner08227e42003-06-17 22:21:05 +0000113
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000114static inline bool CallPassesValueThoughVararg(Instruction *Call,
115 const Value *Arg) {
116 CallSite CS = CallSite::get(Call);
117 const Type *CalledValueTy = CS.getCalledValue()->getType();
118 const Type *FTy = cast<PointerType>(CalledValueTy)->getElementType();
119 unsigned NumFixedArgs = cast<FunctionType>(FTy)->getNumParams();
120 for (CallSite::arg_iterator AI = CS.arg_begin()+NumFixedArgs;
121 AI != CS.arg_end(); ++AI)
122 if (AI->get() == Arg)
123 return true;
Chris Lattner08227e42003-06-17 22:21:05 +0000124 return false;
125}
126
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000127// getArgumentLiveness - Inspect an argument, determining if is known Live
Chris Lattner08227e42003-06-17 22:21:05 +0000128// (used in a computation), MaybeLive (only passed as an argument to a call), or
129// Dead (not used).
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000130DAE::Liveness DAE::getArgumentLiveness(const Argument &A) {
Chris Lattner08227e42003-06-17 22:21:05 +0000131 if (A.use_empty()) return Dead; // First check, directly dead?
132
133 // Scan through all of the uses, looking for non-argument passing uses.
134 for (Value::use_const_iterator I = A.use_begin(), E = A.use_end(); I!=E;++I) {
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000135 // Return instructions do not immediately effect liveness.
136 if (isa<ReturnInst>(*I))
137 continue;
138
Chris Lattner08227e42003-06-17 22:21:05 +0000139 CallSite CS = CallSite::get(const_cast<User*>(*I));
140 if (!CS.getInstruction()) {
141 // If its used by something that is not a call or invoke, it's alive!
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000142 return Live;
Chris Lattner08227e42003-06-17 22:21:05 +0000143 }
144 // If it's an indirect call, mark it alive...
145 Function *Callee = CS.getCalledFunction();
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000146 if (!Callee) return Live;
Chris Lattner08227e42003-06-17 22:21:05 +0000147
Chris Lattner97f4b662003-06-18 16:25:51 +0000148 // Check to see if it's passed through a va_arg area: if so, we cannot
149 // remove it.
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000150 if (CallPassesValueThoughVararg(CS.getInstruction(), &A))
151 return Live; // If passed through va_arg area, we cannot remove it
Chris Lattner08227e42003-06-17 22:21:05 +0000152 }
153
154 return MaybeLive; // It must be used, but only as argument to a function
155}
156
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000157
158// SurveyFunction - This performs the initial survey of the specified function,
159// checking out whether or not it uses any of its incoming arguments or whether
160// any callers use the return value. This fills in the
161// (Dead|MaybeLive|Live)(Arguments|RetVal) sets.
Chris Lattner08227e42003-06-17 22:21:05 +0000162//
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000163// We consider arguments of non-internal functions to be intrinsically alive as
164// well as arguments to functions which have their "address taken".
165//
166void DAE::SurveyFunction(Function &F) {
167 bool FunctionIntrinsicallyLive = false;
168 Liveness RetValLiveness = F.getReturnType() == Type::VoidTy ? Live : Dead;
169
Chris Lattnerb6e06312003-11-05 21:53:41 +0000170 if (!F.hasInternalLinkage() &&
171 (!ShouldHackArguments() || F.getIntrinsicID()))
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000172 FunctionIntrinsicallyLive = true;
Misha Brukmanfd939082005-04-21 23:48:37 +0000173 else
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000174 for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I) {
175 // If this use is anything other than a call site, the function is alive.
176 CallSite CS = CallSite::get(*I);
177 Instruction *TheCall = CS.getInstruction();
178 if (!TheCall) { // Not a direct call site?
179 FunctionIntrinsicallyLive = true;
180 break;
181 }
182
183 // Check to see if the return value is used...
184 if (RetValLiveness != Live)
185 for (Value::use_iterator I = TheCall->use_begin(),
186 E = TheCall->use_end(); I != E; ++I)
187 if (isa<ReturnInst>(cast<Instruction>(*I))) {
188 RetValLiveness = MaybeLive;
189 } else if (isa<CallInst>(cast<Instruction>(*I)) ||
190 isa<InvokeInst>(cast<Instruction>(*I))) {
191 if (CallPassesValueThoughVararg(cast<Instruction>(*I), TheCall) ||
192 !CallSite::get(cast<Instruction>(*I)).getCalledFunction()) {
193 RetValLiveness = Live;
194 break;
195 } else {
196 RetValLiveness = MaybeLive;
197 }
198 } else {
199 RetValLiveness = Live;
200 break;
201 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000202
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000203 // If the function is PASSED IN as an argument, its address has been taken
204 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
205 AI != E; ++AI)
206 if (AI->get() == &F) {
207 FunctionIntrinsicallyLive = true;
208 break;
209 }
210 if (FunctionIntrinsicallyLive) break;
211 }
212
213 if (FunctionIntrinsicallyLive) {
214 DEBUG(std::cerr << " Intrinsically live fn: " << F.getName() << "\n");
Chris Lattner19bdc032005-05-06 05:34:40 +0000215 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
216 AI != E; ++AI)
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000217 LiveArguments.insert(AI);
218 LiveRetVal.insert(&F);
219 return;
220 }
221
222 switch (RetValLiveness) {
223 case Live: LiveRetVal.insert(&F); break;
224 case MaybeLive: MaybeLiveRetVal.insert(&F); break;
225 case Dead: DeadRetVal.insert(&F); break;
226 }
227
228 DEBUG(std::cerr << " Inspecting args for fn: " << F.getName() << "\n");
229
230 // If it is not intrinsically alive, we know that all users of the
231 // function are call sites. Mark all of the arguments live which are
232 // directly used, and keep track of all of the call sites of this function
233 // if there are any arguments we assume that are dead.
234 //
235 bool AnyMaybeLiveArgs = false;
Chris Lattner19bdc032005-05-06 05:34:40 +0000236 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
237 AI != E; ++AI)
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000238 switch (getArgumentLiveness(*AI)) {
239 case Live:
240 DEBUG(std::cerr << " Arg live by use: " << AI->getName() << "\n");
241 LiveArguments.insert(AI);
242 break;
243 case Dead:
244 DEBUG(std::cerr << " Arg definitely dead: " <<AI->getName()<<"\n");
245 DeadArguments.insert(AI);
246 break;
247 case MaybeLive:
248 DEBUG(std::cerr << " Arg only passed to calls: "
249 << AI->getName() << "\n");
250 AnyMaybeLiveArgs = true;
251 MaybeLiveArguments.insert(AI);
252 break;
253 }
254
255 // If there are any "MaybeLive" arguments, we need to check callees of
256 // this function when/if they become alive. Record which functions are
257 // callees...
258 if (AnyMaybeLiveArgs || RetValLiveness == MaybeLive)
259 for (Value::use_iterator I = F.use_begin(), E = F.use_end();
260 I != E; ++I) {
261 if (AnyMaybeLiveArgs)
262 CallSites.insert(std::make_pair(&F, CallSite::get(*I)));
263
264 if (RetValLiveness == MaybeLive)
265 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
266 UI != E; ++UI)
267 InstructionsToInspect.push_back(cast<Instruction>(*UI));
268 }
269}
270
271// isMaybeLiveArgumentNowLive - Check to see if Arg is alive. At this point, we
272// know that the only uses of Arg are to be passed in as an argument to a
273// function call or return. Check to see if the formal argument passed in is in
274// the LiveArguments set. If so, return true.
275//
276bool DAE::isMaybeLiveArgumentNowLive(Argument *Arg) {
Chris Lattner08227e42003-06-17 22:21:05 +0000277 for (Value::use_iterator I = Arg->use_begin(), E = Arg->use_end(); I!=E; ++I){
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000278 if (isa<ReturnInst>(*I)) {
279 if (LiveRetVal.count(Arg->getParent())) return true;
280 continue;
281 }
282
Chris Lattner08227e42003-06-17 22:21:05 +0000283 CallSite CS = CallSite::get(*I);
284
285 // We know that this can only be used for direct calls...
Chris Lattnerd6d0d8c2003-11-02 02:06:27 +0000286 Function *Callee = CS.getCalledFunction();
Chris Lattner08227e42003-06-17 22:21:05 +0000287
288 // Loop over all of the arguments (because Arg may be passed into the call
289 // multiple times) and check to see if any are now alive...
290 CallSite::arg_iterator CSAI = CS.arg_begin();
Chris Lattnere4d5c442005-03-15 04:54:21 +0000291 for (Function::arg_iterator AI = Callee->arg_begin(), E = Callee->arg_end();
Chris Lattner08227e42003-06-17 22:21:05 +0000292 AI != E; ++AI, ++CSAI)
293 // If this is the argument we are looking for, check to see if it's alive
294 if (*CSAI == Arg && LiveArguments.count(AI))
295 return true;
296 }
297 return false;
298}
299
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000300/// MarkArgumentLive - The MaybeLive argument 'Arg' is now known to be alive.
301/// Mark it live in the specified sets and recursively mark arguments in callers
302/// live that are needed to pass in a value.
303///
304void DAE::MarkArgumentLive(Argument *Arg) {
305 std::set<Argument*>::iterator It = MaybeLiveArguments.lower_bound(Arg);
306 if (It == MaybeLiveArguments.end() || *It != Arg) return;
Misha Brukmanfd939082005-04-21 23:48:37 +0000307
Chris Lattner08227e42003-06-17 22:21:05 +0000308 DEBUG(std::cerr << " MaybeLive argument now live: " << Arg->getName()<<"\n");
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000309 MaybeLiveArguments.erase(It);
Chris Lattner08227e42003-06-17 22:21:05 +0000310 LiveArguments.insert(Arg);
Misha Brukmanfd939082005-04-21 23:48:37 +0000311
Chris Lattner08227e42003-06-17 22:21:05 +0000312 // Loop over all of the call sites of the function, making any arguments
313 // passed in to provide a value for this argument live as necessary.
314 //
315 Function *Fn = Arg->getParent();
Chris Lattnere4d5c442005-03-15 04:54:21 +0000316 unsigned ArgNo = std::distance(Fn->arg_begin(), Function::arg_iterator(Arg));
Chris Lattner08227e42003-06-17 22:21:05 +0000317
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000318 std::multimap<Function*, CallSite>::iterator I = CallSites.lower_bound(Fn);
Chris Lattner08227e42003-06-17 22:21:05 +0000319 for (; I != CallSites.end() && I->first == Fn; ++I) {
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000320 CallSite CS = I->second;
321 Value *ArgVal = *(CS.arg_begin()+ArgNo);
322 if (Argument *ActualArg = dyn_cast<Argument>(ArgVal)) {
323 MarkArgumentLive(ActualArg);
324 } else {
325 // If the value passed in at this call site is a return value computed by
326 // some other call site, make sure to mark the return value at the other
327 // call site as being needed.
328 CallSite ArgCS = CallSite::get(ArgVal);
329 if (ArgCS.getInstruction())
330 if (Function *Fn = ArgCS.getCalledFunction())
331 MarkRetValLive(Fn);
332 }
333 }
334}
335
336/// MarkArgumentLive - The MaybeLive return value for the specified function is
337/// now known to be alive. Propagate this fact to the return instructions which
338/// produce it.
339void DAE::MarkRetValLive(Function *F) {
340 assert(F && "Shame shame, we can't have null pointers here!");
341
342 // Check to see if we already knew it was live
343 std::set<Function*>::iterator I = MaybeLiveRetVal.lower_bound(F);
344 if (I == MaybeLiveRetVal.end() || *I != F) return; // It's already alive!
345
346 DEBUG(std::cerr << " MaybeLive retval now live: " << F->getName() << "\n");
347
348 MaybeLiveRetVal.erase(I);
349 LiveRetVal.insert(F); // It is now known to be live!
350
351 // Loop over all of the functions, noticing that the return value is now live.
352 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
353 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()))
354 MarkReturnInstArgumentLive(RI);
355}
356
357void DAE::MarkReturnInstArgumentLive(ReturnInst *RI) {
358 Value *Op = RI->getOperand(0);
359 if (Argument *A = dyn_cast<Argument>(Op)) {
360 MarkArgumentLive(A);
361 } else if (CallInst *CI = dyn_cast<CallInst>(Op)) {
362 if (Function *F = CI->getCalledFunction())
363 MarkRetValLive(F);
364 } else if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) {
365 if (Function *F = II->getCalledFunction())
366 MarkRetValLive(F);
Chris Lattner08227e42003-06-17 22:21:05 +0000367 }
368}
369
370// RemoveDeadArgumentsFromFunction - We know that F has dead arguments, as
371// specified by the DeadArguments list. Transform the function and all of the
372// callees of the function to not have these arguments.
373//
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000374void DAE::RemoveDeadArgumentsFromFunction(Function *F) {
Chris Lattner08227e42003-06-17 22:21:05 +0000375 // Start by computing a new prototype for the function, which is the same as
376 // the old function, but has fewer arguments.
377 const FunctionType *FTy = F->getFunctionType();
378 std::vector<const Type*> Params;
379
Chris Lattnere4d5c442005-03-15 04:54:21 +0000380 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
Chris Lattner08227e42003-06-17 22:21:05 +0000381 if (!DeadArguments.count(I))
382 Params.push_back(I->getType());
383
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000384 const Type *RetTy = FTy->getReturnType();
385 if (DeadRetVal.count(F)) {
386 RetTy = Type::VoidTy;
387 DeadRetVal.erase(F);
388 }
389
Chris Lattnerff5bf9c2003-10-23 17:44:53 +0000390 // Work around LLVM bug PR56: the CWriter cannot emit varargs functions which
391 // have zero fixed arguments.
392 //
393 // FIXME: once this bug is fixed in the CWriter, this hack should be removed.
394 //
395 bool ExtraArgHack = false;
396 if (Params.empty() && FTy->isVarArg()) {
397 ExtraArgHack = true;
398 Params.push_back(Type::IntTy);
399 }
400
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000401 FunctionType *NFTy = FunctionType::get(RetTy, Params, FTy->isVarArg());
402
Chris Lattner08227e42003-06-17 22:21:05 +0000403 // Create the new function body and insert it into the module...
Chris Lattner9b2a14b2003-06-25 04:12:49 +0000404 Function *NF = new Function(NFTy, F->getLinkage(), F->getName());
Chris Lattnerf201dbc2005-05-09 01:05:50 +0000405 NF->setCallingConv(F->getCallingConv());
Chris Lattner08227e42003-06-17 22:21:05 +0000406 F->getParent()->getFunctionList().insert(F, NF);
407
408 // Loop over all of the callers of the function, transforming the call sites
409 // to pass in a smaller number of arguments into the new function.
410 //
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000411 std::vector<Value*> Args;
Chris Lattner08227e42003-06-17 22:21:05 +0000412 while (!F->use_empty()) {
413 CallSite CS = CallSite::get(F->use_back());
414 Instruction *Call = CS.getInstruction();
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000415
Chris Lattner08227e42003-06-17 22:21:05 +0000416 // Loop over the operands, deleting dead ones...
417 CallSite::arg_iterator AI = CS.arg_begin();
Chris Lattner19bdc032005-05-06 05:34:40 +0000418 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
419 I != E; ++I, ++AI)
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000420 if (!DeadArguments.count(I)) // Remove operands for dead arguments
421 Args.push_back(*AI);
422
Chris Lattnerff5bf9c2003-10-23 17:44:53 +0000423 if (ExtraArgHack)
424 Args.push_back(Constant::getNullValue(Type::IntTy));
425
426 // Push any varargs arguments on the list
427 for (; AI != CS.arg_end(); ++AI)
428 Args.push_back(*AI);
429
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000430 Instruction *New;
431 if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000432 New = new InvokeInst(NF, II->getNormalDest(), II->getUnwindDest(),
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000433 Args, "", Call);
Chris Lattnerf201dbc2005-05-09 01:05:50 +0000434 cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000435 } else {
436 New = new CallInst(NF, Args, "", Call);
Chris Lattnerf201dbc2005-05-09 01:05:50 +0000437 cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
Chris Lattner1430ef12005-05-06 06:46:58 +0000438 if (cast<CallInst>(Call)->isTailCall())
439 cast<CallInst>(New)->setTailCall();
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000440 }
441 Args.clear();
442
443 if (!Call->use_empty()) {
444 if (New->getType() == Type::VoidTy)
445 Call->replaceAllUsesWith(Constant::getNullValue(Call->getType()));
446 else {
447 Call->replaceAllUsesWith(New);
448 std::string Name = Call->getName();
449 Call->setName("");
450 New->setName(Name);
Chris Lattner08227e42003-06-17 22:21:05 +0000451 }
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000452 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000453
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000454 // Finally, remove the old call from the program, reducing the use-count of
455 // F.
456 Call->getParent()->getInstList().erase(Call);
Chris Lattner08227e42003-06-17 22:21:05 +0000457 }
458
459 // Since we have now created the new function, splice the body of the old
460 // function right into the new function, leaving the old rotting hulk of the
461 // function empty.
462 NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());
463
464 // Loop over the argument list, transfering uses of the old arguments over to
465 // the new arguments, also transfering over the names as well. While we're at
466 // it, remove the dead arguments from the DeadArguments list.
467 //
Chris Lattner19bdc032005-05-06 05:34:40 +0000468 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(),
469 I2 = NF->arg_begin();
Chris Lattner08227e42003-06-17 22:21:05 +0000470 I != E; ++I)
471 if (!DeadArguments.count(I)) {
472 // If this is a live argument, move the name and users over to the new
473 // version.
474 I->replaceAllUsesWith(I2);
475 I2->setName(I->getName());
476 ++I2;
477 } else {
478 // If this argument is dead, replace any uses of it with null constants
479 // (these are guaranteed to only be operands to call instructions which
480 // will later be simplified).
481 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
482 DeadArguments.erase(I);
483 }
484
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000485 // If we change the return value of the function we must rewrite any return
486 // instructions. Check this now.
487 if (F->getReturnType() != NF->getReturnType())
488 for (Function::iterator BB = NF->begin(), E = NF->end(); BB != E; ++BB)
489 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
490 new ReturnInst(0, RI);
491 BB->getInstList().erase(RI);
492 }
493
Chris Lattner08227e42003-06-17 22:21:05 +0000494 // Now that the old function is dead, delete it.
495 F->getParent()->getFunctionList().erase(F);
496}
497
Chris Lattnerb12914b2004-09-20 04:48:05 +0000498bool DAE::runOnModule(Module &M) {
Chris Lattner08227e42003-06-17 22:21:05 +0000499 // First phase: loop through the module, determining which arguments are live.
500 // We assume all arguments are dead unless proven otherwise (allowing us to
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000501 // determine that dead arguments passed into recursive functions are dead).
Chris Lattner08227e42003-06-17 22:21:05 +0000502 //
Chris Lattner08227e42003-06-17 22:21:05 +0000503 DEBUG(std::cerr << "DAE - Determining liveness\n");
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000504 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
505 SurveyFunction(*I);
506
507 // Loop over the instructions to inspect, propagating liveness among arguments
508 // and return values which are MaybeLive.
509
510 while (!InstructionsToInspect.empty()) {
511 Instruction *I = InstructionsToInspect.back();
512 InstructionsToInspect.pop_back();
Misha Brukmanfd939082005-04-21 23:48:37 +0000513
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000514 if (ReturnInst *RI = dyn_cast<ReturnInst>(I)) {
515 // For return instructions, we just have to check to see if the return
516 // value for the current function is known now to be alive. If so, any
517 // arguments used by it are now alive, and any call instruction return
518 // value is alive as well.
519 if (LiveRetVal.count(RI->getParent()->getParent()))
520 MarkReturnInstArgumentLive(RI);
521
Chris Lattner08227e42003-06-17 22:21:05 +0000522 } else {
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000523 CallSite CS = CallSite::get(I);
524 assert(CS.getInstruction() && "Unknown instruction for the I2I list!");
Chris Lattner08227e42003-06-17 22:21:05 +0000525
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000526 Function *Callee = CS.getCalledFunction();
Misha Brukmanfd939082005-04-21 23:48:37 +0000527
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000528 // If we found a call or invoke instruction on this list, that means that
529 // an argument of the function is a call instruction. If the argument is
530 // live, then the return value of the called instruction is now live.
Chris Lattner08227e42003-06-17 22:21:05 +0000531 //
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000532 CallSite::arg_iterator AI = CS.arg_begin(); // ActualIterator
Chris Lattner19bdc032005-05-06 05:34:40 +0000533 for (Function::arg_iterator FI = Callee->arg_begin(),
534 E = Callee->arg_end(); FI != E; ++AI, ++FI) {
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000535 // If this argument is another call...
536 CallSite ArgCS = CallSite::get(*AI);
537 if (ArgCS.getInstruction() && LiveArguments.count(FI))
538 if (Function *Callee = ArgCS.getCalledFunction())
539 MarkRetValLive(Callee);
540 }
Chris Lattner08227e42003-06-17 22:21:05 +0000541 }
542 }
543
544 // Now we loop over all of the MaybeLive arguments, promoting them to be live
545 // arguments if one of the calls that uses the arguments to the calls they are
546 // passed into requires them to be live. Of course this could make other
547 // arguments live, so process callers recursively.
548 //
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000549 // Because elements can be removed from the MaybeLiveArguments set, copy it to
550 // a temporary vector.
Chris Lattner08227e42003-06-17 22:21:05 +0000551 //
552 std::vector<Argument*> TmpArgList(MaybeLiveArguments.begin(),
553 MaybeLiveArguments.end());
554 for (unsigned i = 0, e = TmpArgList.size(); i != e; ++i) {
555 Argument *MLA = TmpArgList[i];
556 if (MaybeLiveArguments.count(MLA) &&
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000557 isMaybeLiveArgumentNowLive(MLA))
558 MarkArgumentLive(MLA);
Chris Lattner08227e42003-06-17 22:21:05 +0000559 }
560
561 // Recover memory early...
562 CallSites.clear();
563
564 // At this point, we know that all arguments in DeadArguments and
565 // MaybeLiveArguments are dead. If the two sets are empty, there is nothing
566 // to do.
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000567 if (MaybeLiveArguments.empty() && DeadArguments.empty() &&
568 MaybeLiveRetVal.empty() && DeadRetVal.empty())
Chris Lattner08227e42003-06-17 22:21:05 +0000569 return false;
Misha Brukmanfd939082005-04-21 23:48:37 +0000570
Chris Lattner08227e42003-06-17 22:21:05 +0000571 // Otherwise, compact into one set, and start eliminating the arguments from
572 // the functions.
573 DeadArguments.insert(MaybeLiveArguments.begin(), MaybeLiveArguments.end());
574 MaybeLiveArguments.clear();
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000575 DeadRetVal.insert(MaybeLiveRetVal.begin(), MaybeLiveRetVal.end());
576 MaybeLiveRetVal.clear();
577
578 LiveArguments.clear();
579 LiveRetVal.clear();
Chris Lattner08227e42003-06-17 22:21:05 +0000580
581 NumArgumentsEliminated += DeadArguments.size();
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000582 NumRetValsEliminated += DeadRetVal.size();
Chris Lattner08227e42003-06-17 22:21:05 +0000583 while (!DeadArguments.empty())
Chris Lattnerc3afd9b2003-10-23 03:48:17 +0000584 RemoveDeadArgumentsFromFunction((*DeadArguments.begin())->getParent());
585
586 while (!DeadRetVal.empty())
587 RemoveDeadArgumentsFromFunction(*DeadRetVal.begin());
Chris Lattner08227e42003-06-17 22:21:05 +0000588 return true;
589}