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/CallingConv.h"
23 #include "llvm/Constant.h"
24 #include "llvm/DerivedTypes.h"
25 #include "llvm/Instructions.h"
26 #include "llvm/Module.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Support/CallSite.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/ADT/Statistic.h"
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 this is the return value of a csret function, it's not really dead.
132 if (A.getParent()->getCallingConv() == CallingConv::CSRet &&
133 &*A.getParent()->arg_begin() == &A)
136 if (A.use_empty()) // First check, directly dead?
139 // Scan through all of the uses, looking for non-argument passing uses.
140 for (Value::use_const_iterator I = A.use_begin(), E = A.use_end(); I!=E;++I) {
141 // Return instructions do not immediately effect liveness.
142 if (isa<ReturnInst>(*I))
145 CallSite CS = CallSite::get(const_cast<User*>(*I));
146 if (!CS.getInstruction()) {
147 // If its used by something that is not a call or invoke, it's alive!
150 // If it's an indirect call, mark it alive...
151 Function *Callee = CS.getCalledFunction();
152 if (!Callee) return Live;
154 // Check to see if it's passed through a va_arg area: if so, we cannot
156 if (CallPassesValueThoughVararg(CS.getInstruction(), &A))
157 return Live; // If passed through va_arg area, we cannot remove it
160 return MaybeLive; // It must be used, but only as argument to a function
164 // SurveyFunction - This performs the initial survey of the specified function,
165 // checking out whether or not it uses any of its incoming arguments or whether
166 // any callers use the return value. This fills in the
167 // (Dead|MaybeLive|Live)(Arguments|RetVal) sets.
169 // We consider arguments of non-internal functions to be intrinsically alive as
170 // well as arguments to functions which have their "address taken".
172 void DAE::SurveyFunction(Function &F) {
173 bool FunctionIntrinsicallyLive = false;
174 Liveness RetValLiveness = F.getReturnType() == Type::VoidTy ? Live : Dead;
176 if (!F.hasInternalLinkage() &&
177 (!ShouldHackArguments() || F.getIntrinsicID()))
178 FunctionIntrinsicallyLive = true;
180 for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I) {
181 // If this use is anything other than a call site, the function is alive.
182 CallSite CS = CallSite::get(*I);
183 Instruction *TheCall = CS.getInstruction();
184 if (!TheCall) { // Not a direct call site?
185 FunctionIntrinsicallyLive = true;
189 // Check to see if the return value is used...
190 if (RetValLiveness != Live)
191 for (Value::use_iterator I = TheCall->use_begin(),
192 E = TheCall->use_end(); I != E; ++I)
193 if (isa<ReturnInst>(cast<Instruction>(*I))) {
194 RetValLiveness = MaybeLive;
195 } else if (isa<CallInst>(cast<Instruction>(*I)) ||
196 isa<InvokeInst>(cast<Instruction>(*I))) {
197 if (CallPassesValueThoughVararg(cast<Instruction>(*I), TheCall) ||
198 !CallSite::get(cast<Instruction>(*I)).getCalledFunction()) {
199 RetValLiveness = Live;
202 RetValLiveness = MaybeLive;
205 RetValLiveness = Live;
209 // If the function is PASSED IN as an argument, its address has been taken
210 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
212 if (AI->get() == &F) {
213 FunctionIntrinsicallyLive = true;
216 if (FunctionIntrinsicallyLive) break;
219 if (FunctionIntrinsicallyLive) {
220 DEBUG(std::cerr << " Intrinsically live fn: " << F.getName() << "\n");
221 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
223 LiveArguments.insert(AI);
224 LiveRetVal.insert(&F);
228 switch (RetValLiveness) {
229 case Live: LiveRetVal.insert(&F); break;
230 case MaybeLive: MaybeLiveRetVal.insert(&F); break;
231 case Dead: DeadRetVal.insert(&F); break;
234 DEBUG(std::cerr << " Inspecting args for fn: " << F.getName() << "\n");
236 // If it is not intrinsically alive, we know that all users of the
237 // function are call sites. Mark all of the arguments live which are
238 // directly used, and keep track of all of the call sites of this function
239 // if there are any arguments we assume that are dead.
241 bool AnyMaybeLiveArgs = false;
242 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
244 switch (getArgumentLiveness(*AI)) {
246 DEBUG(std::cerr << " Arg live by use: " << AI->getName() << "\n");
247 LiveArguments.insert(AI);
250 DEBUG(std::cerr << " Arg definitely dead: " <<AI->getName()<<"\n");
251 DeadArguments.insert(AI);
254 DEBUG(std::cerr << " Arg only passed to calls: "
255 << AI->getName() << "\n");
256 AnyMaybeLiveArgs = true;
257 MaybeLiveArguments.insert(AI);
261 // If there are any "MaybeLive" arguments, we need to check callees of
262 // this function when/if they become alive. Record which functions are
264 if (AnyMaybeLiveArgs || RetValLiveness == MaybeLive)
265 for (Value::use_iterator I = F.use_begin(), E = F.use_end();
267 if (AnyMaybeLiveArgs)
268 CallSites.insert(std::make_pair(&F, CallSite::get(*I)));
270 if (RetValLiveness == MaybeLive)
271 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
273 InstructionsToInspect.push_back(cast<Instruction>(*UI));
277 // isMaybeLiveArgumentNowLive - Check to see if Arg is alive. At this point, we
278 // know that the only uses of Arg are to be passed in as an argument to a
279 // function call or return. Check to see if the formal argument passed in is in
280 // the LiveArguments set. If so, return true.
282 bool DAE::isMaybeLiveArgumentNowLive(Argument *Arg) {
283 for (Value::use_iterator I = Arg->use_begin(), E = Arg->use_end(); I!=E; ++I){
284 if (isa<ReturnInst>(*I)) {
285 if (LiveRetVal.count(Arg->getParent())) return true;
289 CallSite CS = CallSite::get(*I);
291 // We know that this can only be used for direct calls...
292 Function *Callee = CS.getCalledFunction();
294 // Loop over all of the arguments (because Arg may be passed into the call
295 // multiple times) and check to see if any are now alive...
296 CallSite::arg_iterator CSAI = CS.arg_begin();
297 for (Function::arg_iterator AI = Callee->arg_begin(), E = Callee->arg_end();
298 AI != E; ++AI, ++CSAI)
299 // If this is the argument we are looking for, check to see if it's alive
300 if (*CSAI == Arg && LiveArguments.count(AI))
306 /// MarkArgumentLive - The MaybeLive argument 'Arg' is now known to be alive.
307 /// Mark it live in the specified sets and recursively mark arguments in callers
308 /// live that are needed to pass in a value.
310 void DAE::MarkArgumentLive(Argument *Arg) {
311 std::set<Argument*>::iterator It = MaybeLiveArguments.lower_bound(Arg);
312 if (It == MaybeLiveArguments.end() || *It != Arg) return;
314 DEBUG(std::cerr << " MaybeLive argument now live: " << Arg->getName()<<"\n");
315 MaybeLiveArguments.erase(It);
316 LiveArguments.insert(Arg);
318 // Loop over all of the call sites of the function, making any arguments
319 // passed in to provide a value for this argument live as necessary.
321 Function *Fn = Arg->getParent();
322 unsigned ArgNo = std::distance(Fn->arg_begin(), Function::arg_iterator(Arg));
324 std::multimap<Function*, CallSite>::iterator I = CallSites.lower_bound(Fn);
325 for (; I != CallSites.end() && I->first == Fn; ++I) {
326 CallSite CS = I->second;
327 Value *ArgVal = *(CS.arg_begin()+ArgNo);
328 if (Argument *ActualArg = dyn_cast<Argument>(ArgVal)) {
329 MarkArgumentLive(ActualArg);
331 // If the value passed in at this call site is a return value computed by
332 // some other call site, make sure to mark the return value at the other
333 // call site as being needed.
334 CallSite ArgCS = CallSite::get(ArgVal);
335 if (ArgCS.getInstruction())
336 if (Function *Fn = ArgCS.getCalledFunction())
342 /// MarkArgumentLive - The MaybeLive return value for the specified function is
343 /// now known to be alive. Propagate this fact to the return instructions which
345 void DAE::MarkRetValLive(Function *F) {
346 assert(F && "Shame shame, we can't have null pointers here!");
348 // Check to see if we already knew it was live
349 std::set<Function*>::iterator I = MaybeLiveRetVal.lower_bound(F);
350 if (I == MaybeLiveRetVal.end() || *I != F) return; // It's already alive!
352 DEBUG(std::cerr << " MaybeLive retval now live: " << F->getName() << "\n");
354 MaybeLiveRetVal.erase(I);
355 LiveRetVal.insert(F); // It is now known to be live!
357 // Loop over all of the functions, noticing that the return value is now live.
358 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
359 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()))
360 MarkReturnInstArgumentLive(RI);
363 void DAE::MarkReturnInstArgumentLive(ReturnInst *RI) {
364 Value *Op = RI->getOperand(0);
365 if (Argument *A = dyn_cast<Argument>(Op)) {
367 } else if (CallInst *CI = dyn_cast<CallInst>(Op)) {
368 if (Function *F = CI->getCalledFunction())
370 } else if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) {
371 if (Function *F = II->getCalledFunction())
376 // RemoveDeadArgumentsFromFunction - We know that F has dead arguments, as
377 // specified by the DeadArguments list. Transform the function and all of the
378 // callees of the function to not have these arguments.
380 void DAE::RemoveDeadArgumentsFromFunction(Function *F) {
381 // Start by computing a new prototype for the function, which is the same as
382 // the old function, but has fewer arguments.
383 const FunctionType *FTy = F->getFunctionType();
384 std::vector<const Type*> Params;
386 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
387 if (!DeadArguments.count(I))
388 Params.push_back(I->getType());
390 const Type *RetTy = FTy->getReturnType();
391 if (DeadRetVal.count(F)) {
392 RetTy = Type::VoidTy;
396 // Work around LLVM bug PR56: the CWriter cannot emit varargs functions which
397 // have zero fixed arguments.
399 // FIXME: once this bug is fixed in the CWriter, this hack should be removed.
401 bool ExtraArgHack = false;
402 if (Params.empty() && FTy->isVarArg()) {
404 Params.push_back(Type::IntTy);
407 FunctionType *NFTy = FunctionType::get(RetTy, Params, FTy->isVarArg());
409 // Create the new function body and insert it into the module...
410 Function *NF = new Function(NFTy, F->getLinkage(), F->getName());
411 NF->setCallingConv(F->getCallingConv());
412 F->getParent()->getFunctionList().insert(F, NF);
414 // Loop over all of the callers of the function, transforming the call sites
415 // to pass in a smaller number of arguments into the new function.
417 std::vector<Value*> Args;
418 while (!F->use_empty()) {
419 CallSite CS = CallSite::get(F->use_back());
420 Instruction *Call = CS.getInstruction();
422 // Loop over the operands, deleting dead ones...
423 CallSite::arg_iterator AI = CS.arg_begin();
424 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
426 if (!DeadArguments.count(I)) // Remove operands for dead arguments
430 Args.push_back(Constant::getNullValue(Type::IntTy));
432 // Push any varargs arguments on the list
433 for (; AI != CS.arg_end(); ++AI)
437 if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
438 New = new InvokeInst(NF, II->getNormalDest(), II->getUnwindDest(),
440 cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
442 New = new CallInst(NF, Args, "", Call);
443 cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
444 if (cast<CallInst>(Call)->isTailCall())
445 cast<CallInst>(New)->setTailCall();
449 if (!Call->use_empty()) {
450 if (New->getType() == Type::VoidTy)
451 Call->replaceAllUsesWith(Constant::getNullValue(Call->getType()));
453 Call->replaceAllUsesWith(New);
454 std::string Name = Call->getName();
460 // Finally, remove the old call from the program, reducing the use-count of
462 Call->getParent()->getInstList().erase(Call);
465 // Since we have now created the new function, splice the body of the old
466 // function right into the new function, leaving the old rotting hulk of the
468 NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());
470 // Loop over the argument list, transfering uses of the old arguments over to
471 // the new arguments, also transfering over the names as well. While we're at
472 // it, remove the dead arguments from the DeadArguments list.
474 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(),
475 I2 = NF->arg_begin();
477 if (!DeadArguments.count(I)) {
478 // If this is a live argument, move the name and users over to the new
480 I->replaceAllUsesWith(I2);
481 I2->setName(I->getName());
484 // If this argument is dead, replace any uses of it with null constants
485 // (these are guaranteed to only be operands to call instructions which
486 // will later be simplified).
487 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
488 DeadArguments.erase(I);
491 // If we change the return value of the function we must rewrite any return
492 // instructions. Check this now.
493 if (F->getReturnType() != NF->getReturnType())
494 for (Function::iterator BB = NF->begin(), E = NF->end(); BB != E; ++BB)
495 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
496 new ReturnInst(0, RI);
497 BB->getInstList().erase(RI);
500 // Now that the old function is dead, delete it.
501 F->getParent()->getFunctionList().erase(F);
504 bool DAE::runOnModule(Module &M) {
505 // First phase: loop through the module, determining which arguments are live.
506 // We assume all arguments are dead unless proven otherwise (allowing us to
507 // determine that dead arguments passed into recursive functions are dead).
509 DEBUG(std::cerr << "DAE - Determining liveness\n");
510 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
513 // Loop over the instructions to inspect, propagating liveness among arguments
514 // and return values which are MaybeLive.
516 while (!InstructionsToInspect.empty()) {
517 Instruction *I = InstructionsToInspect.back();
518 InstructionsToInspect.pop_back();
520 if (ReturnInst *RI = dyn_cast<ReturnInst>(I)) {
521 // For return instructions, we just have to check to see if the return
522 // value for the current function is known now to be alive. If so, any
523 // arguments used by it are now alive, and any call instruction return
524 // value is alive as well.
525 if (LiveRetVal.count(RI->getParent()->getParent()))
526 MarkReturnInstArgumentLive(RI);
529 CallSite CS = CallSite::get(I);
530 assert(CS.getInstruction() && "Unknown instruction for the I2I list!");
532 Function *Callee = CS.getCalledFunction();
534 // If we found a call or invoke instruction on this list, that means that
535 // an argument of the function is a call instruction. If the argument is
536 // live, then the return value of the called instruction is now live.
538 CallSite::arg_iterator AI = CS.arg_begin(); // ActualIterator
539 for (Function::arg_iterator FI = Callee->arg_begin(),
540 E = Callee->arg_end(); FI != E; ++AI, ++FI) {
541 // If this argument is another call...
542 CallSite ArgCS = CallSite::get(*AI);
543 if (ArgCS.getInstruction() && LiveArguments.count(FI))
544 if (Function *Callee = ArgCS.getCalledFunction())
545 MarkRetValLive(Callee);
550 // Now we loop over all of the MaybeLive arguments, promoting them to be live
551 // arguments if one of the calls that uses the arguments to the calls they are
552 // passed into requires them to be live. Of course this could make other
553 // arguments live, so process callers recursively.
555 // Because elements can be removed from the MaybeLiveArguments set, copy it to
556 // a temporary vector.
558 std::vector<Argument*> TmpArgList(MaybeLiveArguments.begin(),
559 MaybeLiveArguments.end());
560 for (unsigned i = 0, e = TmpArgList.size(); i != e; ++i) {
561 Argument *MLA = TmpArgList[i];
562 if (MaybeLiveArguments.count(MLA) &&
563 isMaybeLiveArgumentNowLive(MLA))
564 MarkArgumentLive(MLA);
567 // Recover memory early...
570 // At this point, we know that all arguments in DeadArguments and
571 // MaybeLiveArguments are dead. If the two sets are empty, there is nothing
573 if (MaybeLiveArguments.empty() && DeadArguments.empty() &&
574 MaybeLiveRetVal.empty() && DeadRetVal.empty())
577 // Otherwise, compact into one set, and start eliminating the arguments from
579 DeadArguments.insert(MaybeLiveArguments.begin(), MaybeLiveArguments.end());
580 MaybeLiveArguments.clear();
581 DeadRetVal.insert(MaybeLiveRetVal.begin(), MaybeLiveRetVal.end());
582 MaybeLiveRetVal.clear();
584 LiveArguments.clear();
587 NumArgumentsEliminated += DeadArguments.size();
588 NumRetValsEliminated += DeadRetVal.size();
589 while (!DeadArguments.empty())
590 RemoveDeadArgumentsFromFunction((*DeadArguments.begin())->getParent());
592 while (!DeadRetVal.empty())
593 RemoveDeadArgumentsFromFunction(*DeadRetVal.begin());