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/IntrinsicInst.h"
27 #include "llvm/Module.h"
28 #include "llvm/Pass.h"
29 #include "llvm/Support/CallSite.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/ADT/Statistic.h"
32 #include "llvm/Support/Compiler.h"
36 STATISTIC(NumArgumentsEliminated, "Number of unread args removed");
37 STATISTIC(NumRetValsEliminated , "Number of unused return values removed");
40 /// DAE - The dead argument elimination pass.
42 class VISIBILITY_HIDDEN DAE : public ModulePass {
43 /// Liveness enum - During our initial pass over the program, we determine
44 /// that things are either definately alive, definately dead, or in need of
45 /// interprocedural analysis (MaybeLive).
47 enum Liveness { Live, MaybeLive, Dead };
49 /// LiveArguments, MaybeLiveArguments, DeadArguments - These sets contain
50 /// all of the arguments in the program. The Dead set contains arguments
51 /// which are completely dead (never used in the function). The MaybeLive
52 /// set contains arguments which are only passed into other function calls,
53 /// thus may be live and may be dead. The Live set contains arguments which
54 /// are known to be alive.
56 std::set<Argument*> DeadArguments, MaybeLiveArguments, LiveArguments;
58 /// DeadRetVal, MaybeLiveRetVal, LifeRetVal - These sets contain all of the
59 /// functions in the program. The Dead set contains functions whose return
60 /// value is known to be dead. The MaybeLive set contains functions whose
61 /// return values are only used by return instructions, and the Live set
62 /// contains functions whose return values are used, functions that are
63 /// external, and functions that already return void.
65 std::set<Function*> DeadRetVal, MaybeLiveRetVal, LiveRetVal;
67 /// InstructionsToInspect - As we mark arguments and return values
68 /// MaybeLive, we keep track of which instructions could make the values
69 /// live here. Once the entire program has had the return value and
70 /// arguments analyzed, this set is scanned to promote the MaybeLive objects
71 /// to be Live if they really are used.
72 std::vector<Instruction*> InstructionsToInspect;
74 /// CallSites - Keep track of the call sites of functions that have
75 /// MaybeLive arguments or return values.
76 std::multimap<Function*, CallSite> CallSites;
79 bool runOnModule(Module &M);
81 virtual bool ShouldHackArguments() const { return false; }
84 Liveness getArgumentLiveness(const Argument &A);
85 bool isMaybeLiveArgumentNowLive(Argument *Arg);
87 bool DeleteDeadVarargs(Function &Fn);
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 RegisterPass<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 /// DeleteDeadVarargs - If this is an function that takes a ... list, and if
115 /// llvm.vastart is never called, the varargs list is dead for the function.
116 bool DAE::DeleteDeadVarargs(Function &Fn) {
117 assert(Fn.getFunctionType()->isVarArg() && "Function isn't varargs!");
118 if (Fn.isDeclaration() || !Fn.hasInternalLinkage()) return false;
120 // Ensure that the function is only directly called.
121 for (Value::use_iterator I = Fn.use_begin(), E = Fn.use_end(); I != E; ++I) {
122 // If this use is anything other than a call site, give up.
123 CallSite CS = CallSite::get(*I);
124 Instruction *TheCall = CS.getInstruction();
125 if (!TheCall) return false; // Not a direct call site?
127 // The addr of this function is passed to the call.
128 if (I.getOperandNo() != 0) return false;
131 // Okay, we know we can transform this function if safe. Scan its body
132 // looking for calls to llvm.vastart.
133 for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
134 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
135 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
136 if (II->getIntrinsicID() == Intrinsic::vastart)
142 // If we get here, there are no calls to llvm.vastart in the function body,
143 // remove the "..." and adjust all the calls.
145 // Start by computing a new prototype for the function, which is the same as
146 // the old function, but has fewer arguments.
147 const FunctionType *FTy = Fn.getFunctionType();
148 std::vector<const Type*> Params(FTy->param_begin(), FTy->param_end());
149 FunctionType *NFTy = FunctionType::get(FTy->getReturnType(), Params, false);
150 unsigned NumArgs = Params.size();
152 // Create the new function body and insert it into the module...
153 Function *NF = new Function(NFTy, Fn.getLinkage());
154 NF->setCallingConv(Fn.getCallingConv());
155 Fn.getParent()->getFunctionList().insert(&Fn, NF);
158 // Loop over all of the callers of the function, transforming the call sites
159 // to pass in a smaller number of arguments into the new function.
161 std::vector<Value*> Args;
162 while (!Fn.use_empty()) {
163 CallSite CS = CallSite::get(Fn.use_back());
164 Instruction *Call = CS.getInstruction();
166 // Loop over the operands, dropping extraneous ones at the end of the list.
167 Args.assign(CS.arg_begin(), CS.arg_begin()+NumArgs);
170 if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
171 New = new InvokeInst(NF, II->getNormalDest(), II->getUnwindDest(),
173 cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
175 New = new CallInst(NF, Args, "", Call);
176 cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
177 if (cast<CallInst>(Call)->isTailCall())
178 cast<CallInst>(New)->setTailCall();
182 if (!Call->use_empty())
183 Call->replaceAllUsesWith(Constant::getNullValue(Call->getType()));
187 // Finally, remove the old call from the program, reducing the use-count of
189 Call->getParent()->getInstList().erase(Call);
192 // Since we have now created the new function, splice the body of the old
193 // function right into the new function, leaving the old rotting hulk of the
195 NF->getBasicBlockList().splice(NF->begin(), Fn.getBasicBlockList());
197 // Loop over the argument list, transfering uses of the old arguments over to
198 // the new arguments, also transfering over the names as well. While we're at
199 // it, remove the dead arguments from the DeadArguments list.
201 for (Function::arg_iterator I = Fn.arg_begin(), E = Fn.arg_end(),
202 I2 = NF->arg_begin(); I != E; ++I, ++I2) {
203 // Move the name and users over to the new version.
204 I->replaceAllUsesWith(I2);
208 // Finally, nuke the old function.
209 Fn.eraseFromParent();
214 static inline bool CallPassesValueThoughVararg(Instruction *Call,
216 CallSite CS = CallSite::get(Call);
217 const Type *CalledValueTy = CS.getCalledValue()->getType();
218 const Type *FTy = cast<PointerType>(CalledValueTy)->getElementType();
219 unsigned NumFixedArgs = cast<FunctionType>(FTy)->getNumParams();
220 for (CallSite::arg_iterator AI = CS.arg_begin()+NumFixedArgs;
221 AI != CS.arg_end(); ++AI)
222 if (AI->get() == Arg)
227 // getArgumentLiveness - Inspect an argument, determining if is known Live
228 // (used in a computation), MaybeLive (only passed as an argument to a call), or
230 DAE::Liveness DAE::getArgumentLiveness(const Argument &A) {
231 const FunctionType *FTy = A.getParent()->getFunctionType();
233 // If this is the return value of a struct function, it's not really dead.
234 if (FTy->isStructReturn() && &*A.getParent()->arg_begin() == &A)
237 if (A.use_empty()) // First check, directly dead?
240 // Scan through all of the uses, looking for non-argument passing uses.
241 for (Value::use_const_iterator I = A.use_begin(), E = A.use_end(); I!=E;++I) {
242 // Return instructions do not immediately effect liveness.
243 if (isa<ReturnInst>(*I))
246 CallSite CS = CallSite::get(const_cast<User*>(*I));
247 if (!CS.getInstruction()) {
248 // If its used by something that is not a call or invoke, it's alive!
251 // If it's an indirect call, mark it alive...
252 Function *Callee = CS.getCalledFunction();
253 if (!Callee) return Live;
255 // Check to see if it's passed through a va_arg area: if so, we cannot
257 if (CallPassesValueThoughVararg(CS.getInstruction(), &A))
258 return Live; // If passed through va_arg area, we cannot remove it
261 return MaybeLive; // It must be used, but only as argument to a function
265 // SurveyFunction - This performs the initial survey of the specified function,
266 // checking out whether or not it uses any of its incoming arguments or whether
267 // any callers use the return value. This fills in the
268 // (Dead|MaybeLive|Live)(Arguments|RetVal) sets.
270 // We consider arguments of non-internal functions to be intrinsically alive as
271 // well as arguments to functions which have their "address taken".
273 void DAE::SurveyFunction(Function &F) {
274 bool FunctionIntrinsicallyLive = false;
275 Liveness RetValLiveness = F.getReturnType() == Type::VoidTy ? Live : Dead;
277 if (!F.hasInternalLinkage() &&
278 (!ShouldHackArguments() || F.getIntrinsicID()))
279 FunctionIntrinsicallyLive = true;
281 for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I) {
282 // If this use is anything other than a call site, the function is alive.
283 CallSite CS = CallSite::get(*I);
284 Instruction *TheCall = CS.getInstruction();
285 if (!TheCall) { // Not a direct call site?
286 FunctionIntrinsicallyLive = true;
290 // Check to see if the return value is used...
291 if (RetValLiveness != Live)
292 for (Value::use_iterator I = TheCall->use_begin(),
293 E = TheCall->use_end(); I != E; ++I)
294 if (isa<ReturnInst>(cast<Instruction>(*I))) {
295 RetValLiveness = MaybeLive;
296 } else if (isa<CallInst>(cast<Instruction>(*I)) ||
297 isa<InvokeInst>(cast<Instruction>(*I))) {
298 if (CallPassesValueThoughVararg(cast<Instruction>(*I), TheCall) ||
299 !CallSite::get(cast<Instruction>(*I)).getCalledFunction()) {
300 RetValLiveness = Live;
303 RetValLiveness = MaybeLive;
306 RetValLiveness = Live;
310 // If the function is PASSED IN as an argument, its address has been taken
311 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
313 if (AI->get() == &F) {
314 FunctionIntrinsicallyLive = true;
317 if (FunctionIntrinsicallyLive) break;
320 if (FunctionIntrinsicallyLive) {
321 DOUT << " Intrinsically live fn: " << F.getName() << "\n";
322 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
324 LiveArguments.insert(AI);
325 LiveRetVal.insert(&F);
329 switch (RetValLiveness) {
330 case Live: LiveRetVal.insert(&F); break;
331 case MaybeLive: MaybeLiveRetVal.insert(&F); break;
332 case Dead: DeadRetVal.insert(&F); break;
335 DOUT << " Inspecting args for fn: " << F.getName() << "\n";
337 // If it is not intrinsically alive, we know that all users of the
338 // function are call sites. Mark all of the arguments live which are
339 // directly used, and keep track of all of the call sites of this function
340 // if there are any arguments we assume that are dead.
342 bool AnyMaybeLiveArgs = false;
343 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
345 switch (getArgumentLiveness(*AI)) {
347 DOUT << " Arg live by use: " << AI->getName() << "\n";
348 LiveArguments.insert(AI);
351 DOUT << " Arg definitely dead: " << AI->getName() <<"\n";
352 DeadArguments.insert(AI);
355 DOUT << " Arg only passed to calls: " << AI->getName() << "\n";
356 AnyMaybeLiveArgs = true;
357 MaybeLiveArguments.insert(AI);
361 // If there are any "MaybeLive" arguments, we need to check callees of
362 // this function when/if they become alive. Record which functions are
364 if (AnyMaybeLiveArgs || RetValLiveness == MaybeLive)
365 for (Value::use_iterator I = F.use_begin(), E = F.use_end();
367 if (AnyMaybeLiveArgs)
368 CallSites.insert(std::make_pair(&F, CallSite::get(*I)));
370 if (RetValLiveness == MaybeLive)
371 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
373 InstructionsToInspect.push_back(cast<Instruction>(*UI));
377 // isMaybeLiveArgumentNowLive - Check to see if Arg is alive. At this point, we
378 // know that the only uses of Arg are to be passed in as an argument to a
379 // function call or return. Check to see if the formal argument passed in is in
380 // the LiveArguments set. If so, return true.
382 bool DAE::isMaybeLiveArgumentNowLive(Argument *Arg) {
383 for (Value::use_iterator I = Arg->use_begin(), E = Arg->use_end(); I!=E; ++I){
384 if (isa<ReturnInst>(*I)) {
385 if (LiveRetVal.count(Arg->getParent())) return true;
389 CallSite CS = CallSite::get(*I);
391 // We know that this can only be used for direct calls...
392 Function *Callee = CS.getCalledFunction();
394 // Loop over all of the arguments (because Arg may be passed into the call
395 // multiple times) and check to see if any are now alive...
396 CallSite::arg_iterator CSAI = CS.arg_begin();
397 for (Function::arg_iterator AI = Callee->arg_begin(), E = Callee->arg_end();
398 AI != E; ++AI, ++CSAI)
399 // If this is the argument we are looking for, check to see if it's alive
400 if (*CSAI == Arg && LiveArguments.count(AI))
406 /// MarkArgumentLive - The MaybeLive argument 'Arg' is now known to be alive.
407 /// Mark it live in the specified sets and recursively mark arguments in callers
408 /// live that are needed to pass in a value.
410 void DAE::MarkArgumentLive(Argument *Arg) {
411 std::set<Argument*>::iterator It = MaybeLiveArguments.lower_bound(Arg);
412 if (It == MaybeLiveArguments.end() || *It != Arg) return;
414 DOUT << " MaybeLive argument now live: " << Arg->getName() <<"\n";
415 MaybeLiveArguments.erase(It);
416 LiveArguments.insert(Arg);
418 // Loop over all of the call sites of the function, making any arguments
419 // passed in to provide a value for this argument live as necessary.
421 Function *Fn = Arg->getParent();
422 unsigned ArgNo = std::distance(Fn->arg_begin(), Function::arg_iterator(Arg));
424 std::multimap<Function*, CallSite>::iterator I = CallSites.lower_bound(Fn);
425 for (; I != CallSites.end() && I->first == Fn; ++I) {
426 CallSite CS = I->second;
427 Value *ArgVal = *(CS.arg_begin()+ArgNo);
428 if (Argument *ActualArg = dyn_cast<Argument>(ArgVal)) {
429 MarkArgumentLive(ActualArg);
431 // If the value passed in at this call site is a return value computed by
432 // some other call site, make sure to mark the return value at the other
433 // call site as being needed.
434 CallSite ArgCS = CallSite::get(ArgVal);
435 if (ArgCS.getInstruction())
436 if (Function *Fn = ArgCS.getCalledFunction())
442 /// MarkArgumentLive - The MaybeLive return value for the specified function is
443 /// now known to be alive. Propagate this fact to the return instructions which
445 void DAE::MarkRetValLive(Function *F) {
446 assert(F && "Shame shame, we can't have null pointers here!");
448 // Check to see if we already knew it was live
449 std::set<Function*>::iterator I = MaybeLiveRetVal.lower_bound(F);
450 if (I == MaybeLiveRetVal.end() || *I != F) return; // It's already alive!
452 DOUT << " MaybeLive retval now live: " << F->getName() << "\n";
454 MaybeLiveRetVal.erase(I);
455 LiveRetVal.insert(F); // It is now known to be live!
457 // Loop over all of the functions, noticing that the return value is now live.
458 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
459 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()))
460 MarkReturnInstArgumentLive(RI);
463 void DAE::MarkReturnInstArgumentLive(ReturnInst *RI) {
464 Value *Op = RI->getOperand(0);
465 if (Argument *A = dyn_cast<Argument>(Op)) {
467 } else if (CallInst *CI = dyn_cast<CallInst>(Op)) {
468 if (Function *F = CI->getCalledFunction())
470 } else if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) {
471 if (Function *F = II->getCalledFunction())
476 // RemoveDeadArgumentsFromFunction - We know that F has dead arguments, as
477 // specified by the DeadArguments list. Transform the function and all of the
478 // callees of the function to not have these arguments.
480 void DAE::RemoveDeadArgumentsFromFunction(Function *F) {
481 // Start by computing a new prototype for the function, which is the same as
482 // the old function, but has fewer arguments.
483 const FunctionType *FTy = F->getFunctionType();
484 std::vector<const Type*> Params;
486 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
487 if (!DeadArguments.count(I))
488 Params.push_back(I->getType());
490 const Type *RetTy = FTy->getReturnType();
491 if (DeadRetVal.count(F)) {
492 RetTy = Type::VoidTy;
496 // Work around LLVM bug PR56: the CWriter cannot emit varargs functions which
497 // have zero fixed arguments.
499 bool ExtraArgHack = false;
500 if (Params.empty() && FTy->isVarArg()) {
502 Params.push_back(Type::Int32Ty);
505 FunctionType *NFTy = FunctionType::get(RetTy, Params, FTy->isVarArg());
507 // Create the new function body and insert it into the module...
508 Function *NF = new Function(NFTy, F->getLinkage());
509 NF->setCallingConv(F->getCallingConv());
510 F->getParent()->getFunctionList().insert(F, NF);
513 // Loop over all of the callers of the function, transforming the call sites
514 // to pass in a smaller number of arguments into the new function.
516 std::vector<Value*> Args;
517 while (!F->use_empty()) {
518 CallSite CS = CallSite::get(F->use_back());
519 Instruction *Call = CS.getInstruction();
521 // Loop over the operands, deleting dead ones...
522 CallSite::arg_iterator AI = CS.arg_begin();
523 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
525 if (!DeadArguments.count(I)) // Remove operands for dead arguments
529 Args.push_back(UndefValue::get(Type::Int32Ty));
531 // Push any varargs arguments on the list
532 for (; AI != CS.arg_end(); ++AI)
536 if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
537 New = new InvokeInst(NF, II->getNormalDest(), II->getUnwindDest(),
539 cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
541 New = new CallInst(NF, Args, "", Call);
542 cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
543 if (cast<CallInst>(Call)->isTailCall())
544 cast<CallInst>(New)->setTailCall();
548 if (!Call->use_empty()) {
549 if (New->getType() == Type::VoidTy)
550 Call->replaceAllUsesWith(Constant::getNullValue(Call->getType()));
552 Call->replaceAllUsesWith(New);
557 // Finally, remove the old call from the program, reducing the use-count of
559 Call->getParent()->getInstList().erase(Call);
562 // Since we have now created the new function, splice the body of the old
563 // function right into the new function, leaving the old rotting hulk of the
565 NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());
567 // Loop over the argument list, transfering uses of the old arguments over to
568 // the new arguments, also transfering over the names as well. While we're at
569 // it, remove the dead arguments from the DeadArguments list.
571 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(),
572 I2 = NF->arg_begin();
574 if (!DeadArguments.count(I)) {
575 // If this is a live argument, move the name and users over to the new
577 I->replaceAllUsesWith(I2);
581 // If this argument is dead, replace any uses of it with null constants
582 // (these are guaranteed to only be operands to call instructions which
583 // will later be simplified).
584 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
585 DeadArguments.erase(I);
588 // If we change the return value of the function we must rewrite any return
589 // instructions. Check this now.
590 if (F->getReturnType() != NF->getReturnType())
591 for (Function::iterator BB = NF->begin(), E = NF->end(); BB != E; ++BB)
592 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
593 new ReturnInst(0, RI);
594 BB->getInstList().erase(RI);
597 // Now that the old function is dead, delete it.
598 F->getParent()->getFunctionList().erase(F);
601 bool DAE::runOnModule(Module &M) {
602 // First phase: loop through the module, determining which arguments are live.
603 // We assume all arguments are dead unless proven otherwise (allowing us to
604 // determine that dead arguments passed into recursive functions are dead).
606 DOUT << "DAE - Determining liveness\n";
607 for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
609 if (F.getFunctionType()->isVarArg())
610 if (DeleteDeadVarargs(F))
616 // Loop over the instructions to inspect, propagating liveness among arguments
617 // and return values which are MaybeLive.
619 while (!InstructionsToInspect.empty()) {
620 Instruction *I = InstructionsToInspect.back();
621 InstructionsToInspect.pop_back();
623 if (ReturnInst *RI = dyn_cast<ReturnInst>(I)) {
624 // For return instructions, we just have to check to see if the return
625 // value for the current function is known now to be alive. If so, any
626 // arguments used by it are now alive, and any call instruction return
627 // value is alive as well.
628 if (LiveRetVal.count(RI->getParent()->getParent()))
629 MarkReturnInstArgumentLive(RI);
632 CallSite CS = CallSite::get(I);
633 assert(CS.getInstruction() && "Unknown instruction for the I2I list!");
635 Function *Callee = CS.getCalledFunction();
637 // If we found a call or invoke instruction on this list, that means that
638 // an argument of the function is a call instruction. If the argument is
639 // live, then the return value of the called instruction is now live.
641 CallSite::arg_iterator AI = CS.arg_begin(); // ActualIterator
642 for (Function::arg_iterator FI = Callee->arg_begin(),
643 E = Callee->arg_end(); FI != E; ++AI, ++FI) {
644 // If this argument is another call...
645 CallSite ArgCS = CallSite::get(*AI);
646 if (ArgCS.getInstruction() && LiveArguments.count(FI))
647 if (Function *Callee = ArgCS.getCalledFunction())
648 MarkRetValLive(Callee);
653 // Now we loop over all of the MaybeLive arguments, promoting them to be live
654 // arguments if one of the calls that uses the arguments to the calls they are
655 // passed into requires them to be live. Of course this could make other
656 // arguments live, so process callers recursively.
658 // Because elements can be removed from the MaybeLiveArguments set, copy it to
659 // a temporary vector.
661 std::vector<Argument*> TmpArgList(MaybeLiveArguments.begin(),
662 MaybeLiveArguments.end());
663 for (unsigned i = 0, e = TmpArgList.size(); i != e; ++i) {
664 Argument *MLA = TmpArgList[i];
665 if (MaybeLiveArguments.count(MLA) &&
666 isMaybeLiveArgumentNowLive(MLA))
667 MarkArgumentLive(MLA);
670 // Recover memory early...
673 // At this point, we know that all arguments in DeadArguments and
674 // MaybeLiveArguments are dead. If the two sets are empty, there is nothing
676 if (MaybeLiveArguments.empty() && DeadArguments.empty() &&
677 MaybeLiveRetVal.empty() && DeadRetVal.empty())
680 // Otherwise, compact into one set, and start eliminating the arguments from
682 DeadArguments.insert(MaybeLiveArguments.begin(), MaybeLiveArguments.end());
683 MaybeLiveArguments.clear();
684 DeadRetVal.insert(MaybeLiveRetVal.begin(), MaybeLiveRetVal.end());
685 MaybeLiveRetVal.clear();
687 LiveArguments.clear();
690 NumArgumentsEliminated += DeadArguments.size();
691 NumRetValsEliminated += DeadRetVal.size();
692 while (!DeadArguments.empty())
693 RemoveDeadArgumentsFromFunction((*DeadArguments.begin())->getParent());
695 while (!DeadRetVal.empty())
696 RemoveDeadArgumentsFromFunction(*DeadRetVal.begin());