1 //===-- DeadArgumentElimination.cpp - Eliminate dead arguments ------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This pass deletes dead arguments from internal functions. Dead argument
11 // elimination removes arguments which are directly dead, as well as arguments
12 // only passed into function calls as dead arguments of other functions. This
13 // pass also deletes dead arguments in a similar way.
15 // This pass is often useful as a cleanup pass to run after aggressive
16 // interprocedural passes, which add possibly-dead arguments.
18 //===----------------------------------------------------------------------===//
20 #define DEBUG_TYPE "deadargelim"
21 #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"
26 #include "llvm/Instructions.h"
27 #include "llvm/Support/CallSite.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/ADT/Statistic.h"
30 #include "llvm/ADT/iterator"
36 Statistic<> NumArgumentsEliminated("deadargelim",
37 "Number of unread args removed");
38 Statistic<> NumRetValsEliminated("deadargelim",
39 "Number of unused return values removed");
41 /// DAE - The dead argument elimination pass.
43 class DAE : public ModulePass {
44 /// 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).
48 enum Liveness { Live, MaybeLive, Dead };
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.
57 std::set<Argument*> DeadArguments, MaybeLiveArguments, LiveArguments;
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.
66 std::set<Function*> DeadRetVal, MaybeLiveRetVal, LiveRetVal;
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;
75 /// CallSites - Keep track of the call sites of functions that have
76 /// MaybeLive arguments or return values.
77 std::multimap<Function*, CallSite> CallSites;
80 bool runOnModule(Module &M);
82 virtual bool ShouldHackArguments() const { return false; }
85 Liveness getArgumentLiveness(const Argument &A);
86 bool isMaybeLiveArgumentNowLive(Argument *Arg);
88 void SurveyFunction(Function &Fn);
90 void MarkArgumentLive(Argument *Arg);
91 void MarkRetValLive(Function *F);
92 void MarkReturnInstArgumentLive(ReturnInst *RI);
94 void RemoveDeadArgumentsFromFunction(Function *F);
96 RegisterOpt<DAE> X("deadargelim", "Dead Argument Elimination");
98 /// DAH - DeadArgumentHacking pass - Same as dead argument elimination, but
99 /// deletes arguments to functions which are external. This is only for use
101 struct DAH : public DAE {
102 virtual bool ShouldHackArguments() const { return true; }
104 RegisterPass<DAH> Y("deadarghaX0r",
105 "Dead Argument Hacking (BUGPOINT USE ONLY; DO NOT USE)");
108 /// createDeadArgEliminationPass - This pass removes arguments from functions
109 /// which are not used by the body of the function.
111 ModulePass *llvm::createDeadArgEliminationPass() { return new DAE(); }
112 ModulePass *llvm::createDeadArgHackingPass() { return new DAH(); }
114 static inline bool CallPassesValueThoughVararg(Instruction *Call,
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)
127 // getArgumentLiveness - Inspect an argument, determining if is known Live
128 // (used in a computation), MaybeLive (only passed as an argument to a call), or
130 DAE::Liveness DAE::getArgumentLiveness(const Argument &A) {
131 if (A.use_empty()) return Dead; // First check, directly dead?
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) {
135 // Return instructions do not immediately effect liveness.
136 if (isa<ReturnInst>(*I))
139 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!
144 // If it's an indirect call, mark it alive...
145 Function *Callee = CS.getCalledFunction();
146 if (!Callee) return Live;
148 // Check to see if it's passed through a va_arg area: if so, we cannot
150 if (CallPassesValueThoughVararg(CS.getInstruction(), &A))
151 return Live; // If passed through va_arg area, we cannot remove it
154 return MaybeLive; // It must be used, but only as argument to a function
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.
163 // We consider arguments of non-internal functions to be intrinsically alive as
164 // well as arguments to functions which have their "address taken".
166 void DAE::SurveyFunction(Function &F) {
167 bool FunctionIntrinsicallyLive = false;
168 Liveness RetValLiveness = F.getReturnType() == Type::VoidTy ? Live : Dead;
170 if (!F.hasInternalLinkage() &&
171 (!ShouldHackArguments() || F.getIntrinsicID()))
172 FunctionIntrinsicallyLive = true;
174 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;
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;
196 RetValLiveness = MaybeLive;
199 RetValLiveness = Live;
203 // 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();
206 if (AI->get() == &F) {
207 FunctionIntrinsicallyLive = true;
210 if (FunctionIntrinsicallyLive) break;
213 if (FunctionIntrinsicallyLive) {
214 DEBUG(std::cerr << " Intrinsically live fn: " << F.getName() << "\n");
215 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
217 LiveArguments.insert(AI);
218 LiveRetVal.insert(&F);
222 switch (RetValLiveness) {
223 case Live: LiveRetVal.insert(&F); break;
224 case MaybeLive: MaybeLiveRetVal.insert(&F); break;
225 case Dead: DeadRetVal.insert(&F); break;
228 DEBUG(std::cerr << " Inspecting args for fn: " << F.getName() << "\n");
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.
235 bool AnyMaybeLiveArgs = false;
236 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
238 switch (getArgumentLiveness(*AI)) {
240 DEBUG(std::cerr << " Arg live by use: " << AI->getName() << "\n");
241 LiveArguments.insert(AI);
244 DEBUG(std::cerr << " Arg definitely dead: " <<AI->getName()<<"\n");
245 DeadArguments.insert(AI);
248 DEBUG(std::cerr << " Arg only passed to calls: "
249 << AI->getName() << "\n");
250 AnyMaybeLiveArgs = true;
251 MaybeLiveArguments.insert(AI);
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
258 if (AnyMaybeLiveArgs || RetValLiveness == MaybeLive)
259 for (Value::use_iterator I = F.use_begin(), E = F.use_end();
261 if (AnyMaybeLiveArgs)
262 CallSites.insert(std::make_pair(&F, CallSite::get(*I)));
264 if (RetValLiveness == MaybeLive)
265 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
267 InstructionsToInspect.push_back(cast<Instruction>(*UI));
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.
276 bool DAE::isMaybeLiveArgumentNowLive(Argument *Arg) {
277 for (Value::use_iterator I = Arg->use_begin(), E = Arg->use_end(); I!=E; ++I){
278 if (isa<ReturnInst>(*I)) {
279 if (LiveRetVal.count(Arg->getParent())) return true;
283 CallSite CS = CallSite::get(*I);
285 // We know that this can only be used for direct calls...
286 Function *Callee = CS.getCalledFunction();
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();
291 for (Function::arg_iterator AI = Callee->arg_begin(), E = Callee->arg_end();
292 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))
300 /// 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.
304 void DAE::MarkArgumentLive(Argument *Arg) {
305 std::set<Argument*>::iterator It = MaybeLiveArguments.lower_bound(Arg);
306 if (It == MaybeLiveArguments.end() || *It != Arg) return;
308 DEBUG(std::cerr << " MaybeLive argument now live: " << Arg->getName()<<"\n");
309 MaybeLiveArguments.erase(It);
310 LiveArguments.insert(Arg);
312 // 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.
315 Function *Fn = Arg->getParent();
316 unsigned ArgNo = std::distance(Fn->arg_begin(), Function::arg_iterator(Arg));
318 std::multimap<Function*, CallSite>::iterator I = CallSites.lower_bound(Fn);
319 for (; I != CallSites.end() && I->first == Fn; ++I) {
320 CallSite CS = I->second;
321 Value *ArgVal = *(CS.arg_begin()+ArgNo);
322 if (Argument *ActualArg = dyn_cast<Argument>(ArgVal)) {
323 MarkArgumentLive(ActualArg);
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())
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
339 void DAE::MarkRetValLive(Function *F) {
340 assert(F && "Shame shame, we can't have null pointers here!");
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!
346 DEBUG(std::cerr << " MaybeLive retval now live: " << F->getName() << "\n");
348 MaybeLiveRetVal.erase(I);
349 LiveRetVal.insert(F); // It is now known to be live!
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);
357 void DAE::MarkReturnInstArgumentLive(ReturnInst *RI) {
358 Value *Op = RI->getOperand(0);
359 if (Argument *A = dyn_cast<Argument>(Op)) {
361 } else if (CallInst *CI = dyn_cast<CallInst>(Op)) {
362 if (Function *F = CI->getCalledFunction())
364 } else if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) {
365 if (Function *F = II->getCalledFunction())
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.
374 void DAE::RemoveDeadArgumentsFromFunction(Function *F) {
375 // 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;
380 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
381 if (!DeadArguments.count(I))
382 Params.push_back(I->getType());
384 const Type *RetTy = FTy->getReturnType();
385 if (DeadRetVal.count(F)) {
386 RetTy = Type::VoidTy;
390 // Work around LLVM bug PR56: the CWriter cannot emit varargs functions which
391 // have zero fixed arguments.
393 // FIXME: once this bug is fixed in the CWriter, this hack should be removed.
395 bool ExtraArgHack = false;
396 if (Params.empty() && FTy->isVarArg()) {
398 Params.push_back(Type::IntTy);
401 FunctionType *NFTy = FunctionType::get(RetTy, Params, FTy->isVarArg());
403 // Create the new function body and insert it into the module...
404 Function *NF = new Function(NFTy, F->getLinkage(), F->getName());
405 NF->setCallingConv(F->getCallingConv());
406 F->getParent()->getFunctionList().insert(F, NF);
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.
411 std::vector<Value*> Args;
412 while (!F->use_empty()) {
413 CallSite CS = CallSite::get(F->use_back());
414 Instruction *Call = CS.getInstruction();
416 // Loop over the operands, deleting dead ones...
417 CallSite::arg_iterator AI = CS.arg_begin();
418 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
420 if (!DeadArguments.count(I)) // Remove operands for dead arguments
424 Args.push_back(Constant::getNullValue(Type::IntTy));
426 // Push any varargs arguments on the list
427 for (; AI != CS.arg_end(); ++AI)
431 if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
432 New = new InvokeInst(NF, II->getNormalDest(), II->getUnwindDest(),
434 cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
436 New = new CallInst(NF, Args, "", Call);
437 cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
438 if (cast<CallInst>(Call)->isTailCall())
439 cast<CallInst>(New)->setTailCall();
443 if (!Call->use_empty()) {
444 if (New->getType() == Type::VoidTy)
445 Call->replaceAllUsesWith(Constant::getNullValue(Call->getType()));
447 Call->replaceAllUsesWith(New);
448 std::string Name = Call->getName();
454 // Finally, remove the old call from the program, reducing the use-count of
456 Call->getParent()->getInstList().erase(Call);
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
462 NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());
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.
468 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(),
469 I2 = NF->arg_begin();
471 if (!DeadArguments.count(I)) {
472 // If this is a live argument, move the name and users over to the new
474 I->replaceAllUsesWith(I2);
475 I2->setName(I->getName());
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);
485 // 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);
494 // Now that the old function is dead, delete it.
495 F->getParent()->getFunctionList().erase(F);
498 bool DAE::runOnModule(Module &M) {
499 // First phase: loop through the module, determining which arguments are live.
500 // We assume all arguments are dead unless proven otherwise (allowing us to
501 // determine that dead arguments passed into recursive functions are dead).
503 DEBUG(std::cerr << "DAE - Determining liveness\n");
504 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
507 // Loop over the instructions to inspect, propagating liveness among arguments
508 // and return values which are MaybeLive.
510 while (!InstructionsToInspect.empty()) {
511 Instruction *I = InstructionsToInspect.back();
512 InstructionsToInspect.pop_back();
514 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);
523 CallSite CS = CallSite::get(I);
524 assert(CS.getInstruction() && "Unknown instruction for the I2I list!");
526 Function *Callee = CS.getCalledFunction();
528 // 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.
532 CallSite::arg_iterator AI = CS.arg_begin(); // ActualIterator
533 for (Function::arg_iterator FI = Callee->arg_begin(),
534 E = Callee->arg_end(); FI != E; ++AI, ++FI) {
535 // 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);
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.
549 // Because elements can be removed from the MaybeLiveArguments set, copy it to
550 // a temporary vector.
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) &&
557 isMaybeLiveArgumentNowLive(MLA))
558 MarkArgumentLive(MLA);
561 // Recover memory early...
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
567 if (MaybeLiveArguments.empty() && DeadArguments.empty() &&
568 MaybeLiveRetVal.empty() && DeadRetVal.empty())
571 // Otherwise, compact into one set, and start eliminating the arguments from
573 DeadArguments.insert(MaybeLiveArguments.begin(), MaybeLiveArguments.end());
574 MaybeLiveArguments.clear();
575 DeadRetVal.insert(MaybeLiveRetVal.begin(), MaybeLiveRetVal.end());
576 MaybeLiveRetVal.clear();
578 LiveArguments.clear();
581 NumArgumentsEliminated += DeadArguments.size();
582 NumRetValsEliminated += DeadRetVal.size();
583 while (!DeadArguments.empty())
584 RemoveDeadArgumentsFromFunction((*DeadArguments.begin())->getParent());
586 while (!DeadRetVal.empty())
587 RemoveDeadArgumentsFromFunction(*DeadRetVal.begin());