llvm.org GIT mirror llvm / c3afd9b
Make this pass substantially stronger by having it delete dead return values as well as arguments. Now it can delete arguments and return values which are only passed into other arguments or are returned, if they are dead. This causes it to delete several hundred extra args/retvals from the C++ hello world program, shrinking it by about 2K. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@9398 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 15 years ago
1 changed file(s) with 356 addition(s) and 129 deletion(s). Raw diff Collapse all Expand all
88 //
99 // This pass deletes dead arguments from internal functions. Dead argument
1010 // elimination removes arguments which are directly dead, as well as arguments
11 // only passed into function calls as dead arguments of other functions.
11 // only passed into function calls as dead arguments of other functions. This
12 // pass also deletes dead arguments in a similar way.
1213 //
1314 // This pass is often useful as a cleanup pass to run after aggressive
1415 // interprocedural passes, which add possibly-dead arguments.
2930 #include
3031
3132 namespace {
32 Statistic<> NumArgumentsEliminated("deadargelim", "Number of args removed");
33
34 struct DAE : public Pass {
33 Statistic<> NumArgumentsEliminated("deadargelim",
34 "Number of unread args removed");
35 Statistic<> NumRetValsEliminated("deadargelim",
36 "Number of unused return values removed");
37
38 /// DAE - The dead argument elimination pass.
39 ///
40 class DAE : public Pass {
41 /// DeleteFromExternalFunctions - Bugpoint sets this flag to indicate that
42 /// it is safe to hack apart functions without internal linkage.
43 bool DeleteFromExternalFunctions;
44
45 /// Liveness enum - During our initial pass over the program, we determine
46 /// that things are either definately alive, definately dead, or in need of
47 /// interprocedural analysis (MaybeLive).
48 ///
49 enum Liveness { Live, MaybeLive, Dead };
50
51 /// LiveArguments, MaybeLiveArguments, DeadArguments - These sets contain
52 /// all of the arguments in the program. The Dead set contains arguments
53 /// which are completely dead (never used in the function). The MaybeLive
54 /// set contains arguments which are only passed into other function calls,
55 /// thus may be live and may be dead. The Live set contains arguments which
56 /// are known to be alive.
57 ///
58 std::set DeadArguments, MaybeLiveArguments, LiveArguments;
59
60 /// DeadRetVal, MaybeLiveRetVal, LifeRetVal - These sets contain all of the
61 /// functions in the program. The Dead set contains functions whose return
62 /// value is known to be dead. The MaybeLive set contains functions whose
63 /// return values are only used by return instructions, and the Live set
64 /// contains functions whose return values are used, functions that are
65 /// external, and functions that already return void.
66 ///
67 std::set DeadRetVal, MaybeLiveRetVal, LiveRetVal;
68
69 /// InstructionsToInspect - As we mark arguments and return values
70 /// MaybeLive, we keep track of which instructions could make the values
71 /// live here. Once the entire program has had the return value and
72 /// arguments analyzed, this set is scanned to promote the MaybeLive objects
73 /// to be Live if they really are used.
74 std::vector InstructionsToInspect;
75
76 /// CallSites - Keep track of the call sites of functions that have
77 /// MaybeLive arguments or return values.
78 std::multimap CallSites;
79
80 public:
3581 DAE(bool DFEF = false) : DeleteFromExternalFunctions(DFEF) {}
3682 bool run(Module &M);
3783
3884 private:
39 bool DeleteFromExternalFunctions;
40 bool FunctionArgumentsIntrinsicallyAlive(const Function &F);
41 void RemoveDeadArgumentsFromFunction(Function *F,
42 std::set &DeadArguments);
85 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);
93
94 void RemoveDeadArgumentsFromFunction(Function *F);
4395 };
4496 RegisterOpt X("deadargelim", "Dead Argument Elimination");
4597 }
54106 return new DAE(DeleteFromExternalFunctions);
55107 }
56108
57
58 // FunctionArgumentsIntrinsicallyAlive - Return true if the arguments of the
59 // specified function are intrinsically alive.
60 //
61 // We consider arguments of non-internal functions to be intrinsically alive as
62 // well as arguments to functions which have their "address taken".
63 //
64 bool DAE::FunctionArgumentsIntrinsicallyAlive(const Function &F) {
65 if (!F.hasInternalLinkage() && !DeleteFromExternalFunctions) return true;
66
67 for (Value::use_const_iterator I = F.use_begin(), E = F.use_end(); I!=E; ++I){
68 // If this use is anything other than a call site, the function is alive.
69 CallSite CS = CallSite::get(const_cast(*I));
70 if (!CS.getInstruction()) return true; // Not a valid call site?
71
72 // If the function is PASSED IN as an argument, its address has been taken
73 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end(); AI != E;
74 ++AI)
75 if (AI->get() == &F) return true;
76 }
109 static inline bool CallPassesValueThoughVararg(Instruction *Call,
110 const Value *Arg) {
111 CallSite CS = CallSite::get(Call);
112 const Type *CalledValueTy = CS.getCalledValue()->getType();
113 const Type *FTy = cast(CalledValueTy)->getElementType();
114 unsigned NumFixedArgs = cast(FTy)->getNumParams();
115 for (CallSite::arg_iterator AI = CS.arg_begin()+NumFixedArgs;
116 AI != CS.arg_end(); ++AI)
117 if (AI->get() == Arg)
118 return true;
77119 return false;
78120 }
79121
80 namespace {
81 enum ArgumentLiveness { Alive, MaybeLive, Dead };
82 }
83
84 // getArgumentLiveness - Inspect an argument, determining if is known Alive
122 // getArgumentLiveness - Inspect an argument, determining if is known Live
85123 // (used in a computation), MaybeLive (only passed as an argument to a call), or
86124 // Dead (not used).
87 static ArgumentLiveness getArgumentLiveness(const Argument &A) {
125 DAE::Liveness DAE::getArgumentLiveness(const Argument &A) {
88126 if (A.use_empty()) return Dead; // First check, directly dead?
89127
90128 // Scan through all of the uses, looking for non-argument passing uses.
91129 for (Value::use_const_iterator I = A.use_begin(), E = A.use_end(); I!=E;++I) {
130 // Return instructions do not immediately effect liveness.
131 if (isa(*I))
132 continue;
133
92134 CallSite CS = CallSite::get(const_cast(*I));
93135 if (!CS.getInstruction()) {
94136 // If its used by something that is not a call or invoke, it's alive!
95 return Alive;
137 return Live;
96138 }
97139 // If it's an indirect call, mark it alive...
98140 Function *Callee = CS.getCalledFunction();
99 if (!Callee) return Alive;
141 if (!Callee) return Live;
100142
101143 // Check to see if it's passed through a va_arg area: if so, we cannot
102144 // remove it.
103 unsigned NumFixedArgs = Callee->getFunctionType()->getNumParams();
104 for (CallSite::arg_iterator AI = CS.arg_begin()+NumFixedArgs;
105 AI != CS.arg_end(); ++AI)
106 if (AI->get() == &A) // If passed through va_arg area, we cannot remove it
107 return Alive;
145 if (CallPassesValueThoughVararg(CS.getInstruction(), &A))
146 return Live; // If passed through va_arg area, we cannot remove it
108147 }
109148
110149 return MaybeLive; // It must be used, but only as argument to a function
111150 }
112151
113 // isMaybeLiveArgumentNowAlive - Check to see if Arg is alive. At this point,
114 // we know that the only uses of Arg are to be passed in as an argument to a
115 // function call. Check to see if the formal argument passed in is in the
116 // LiveArguments set. If so, return true.
117 //
118 static bool isMaybeLiveArgumentNowAlive(Argument *Arg,
119 const std::set &LiveArguments) {
152
153 // SurveyFunction - This performs the initial survey of the specified function,
154 // checking out whether or not it uses any of its incoming arguments or whether
155 // any callers use the return value. This fills in the
156 // (Dead|MaybeLive|Live)(Arguments|RetVal) sets.
157 //
158 // We consider arguments of non-internal functions to be intrinsically alive as
159 // well as arguments to functions which have their "address taken".
160 //
161 void DAE::SurveyFunction(Function &F) {
162 bool FunctionIntrinsicallyLive = false;
163 Liveness RetValLiveness = F.getReturnType() == Type::VoidTy ? Live : Dead;
164
165 if (!F.hasInternalLinkage() && !DeleteFromExternalFunctions)
166 FunctionIntrinsicallyLive = true;
167 else
168 for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I) {
169 // If this use is anything other than a call site, the function is alive.
170 CallSite CS = CallSite::get(*I);
171 Instruction *TheCall = CS.getInstruction();
172 if (!TheCall) { // Not a direct call site?
173 FunctionIntrinsicallyLive = true;
174 break;
175 }
176
177 // Check to see if the return value is used...
178 if (RetValLiveness != Live)
179 for (Value::use_iterator I = TheCall->use_begin(),
180 E = TheCall->use_end(); I != E; ++I)
181 if (isa(cast(*I))) {
182 RetValLiveness = MaybeLive;
183 } else if (isa(cast(*I)) ||
184 isa(cast(*I))) {
185 if (CallPassesValueThoughVararg(cast(*I), TheCall) ||
186 !CallSite::get(cast(*I)).getCalledFunction()) {
187 RetValLiveness = Live;
188 break;
189 } else {
190 RetValLiveness = MaybeLive;
191 }
192 } else {
193 RetValLiveness = Live;
194 break;
195 }
196
197 // If the function is PASSED IN as an argument, its address has been taken
198 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
199 AI != E; ++AI)
200 if (AI->get() == &F) {
201 FunctionIntrinsicallyLive = true;
202 break;
203 }
204 if (FunctionIntrinsicallyLive) break;
205 }
206
207 if (FunctionIntrinsicallyLive) {
208 DEBUG(std::cerr << " Intrinsically live fn: " << F.getName() << "\n");
209 for (Function::aiterator AI = F.abegin(), E = F.aend(); AI != E; ++AI)
210 LiveArguments.insert(AI);
211 LiveRetVal.insert(&F);
212 return;
213 }
214
215 switch (RetValLiveness) {
216 case Live: LiveRetVal.insert(&F); break;
217 case MaybeLive: MaybeLiveRetVal.insert(&F); break;
218 case Dead: DeadRetVal.insert(&F); break;
219 }
220
221 DEBUG(std::cerr << " Inspecting args for fn: " << F.getName() << "\n");
222
223 // If it is not intrinsically alive, we know that all users of the
224 // function are call sites. Mark all of the arguments live which are
225 // directly used, and keep track of all of the call sites of this function
226 // if there are any arguments we assume that are dead.
227 //
228 bool AnyMaybeLiveArgs = false;
229 for (Function::aiterator AI = F.abegin(), E = F.aend(); AI != E; ++AI)
230 switch (getArgumentLiveness(*AI)) {
231 case Live:
232 DEBUG(std::cerr << " Arg live by use: " << AI->getName() << "\n");
233 LiveArguments.insert(AI);
234 break;
235 case Dead:
236 DEBUG(std::cerr << " Arg definitely dead: " <getName()<<"\n");
237 DeadArguments.insert(AI);
238 break;
239 case MaybeLive:
240 DEBUG(std::cerr << " Arg only passed to calls: "
241 << AI->getName() << "\n");
242 AnyMaybeLiveArgs = true;
243 MaybeLiveArguments.insert(AI);
244 break;
245 }
246
247 // If there are any "MaybeLive" arguments, we need to check callees of
248 // this function when/if they become alive. Record which functions are
249 // callees...
250 if (AnyMaybeLiveArgs || RetValLiveness == MaybeLive)
251 for (Value::use_iterator I = F.use_begin(), E = F.use_end();
252 I != E; ++I) {
253 if (AnyMaybeLiveArgs)
254 CallSites.insert(std::make_pair(&F, CallSite::get(*I)));
255
256 if (RetValLiveness == MaybeLive)
257 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
258 UI != E; ++UI)
259 InstructionsToInspect.push_back(cast(*UI));
260 }
261 }
262
263 // isMaybeLiveArgumentNowLive - Check to see if Arg is alive. At this point, we
264 // know that the only uses of Arg are to be passed in as an argument to a
265 // function call or return. Check to see if the formal argument passed in is in
266 // the LiveArguments set. If so, return true.
267 //
268 bool DAE::isMaybeLiveArgumentNowLive(Argument *Arg) {
120269 for (Value::use_iterator I = Arg->use_begin(), E = Arg->use_end(); I!=E; ++I){
270 if (isa(*I)) {
271 if (LiveRetVal.count(Arg->getParent())) return true;
272 continue;
273 }
274
121275 CallSite CS = CallSite::get(*I);
122276
123277 // We know that this can only be used for direct calls...
135289 return false;
136290 }
137291
138 // MarkArgumentLive - The MaybeLive argument 'Arg' is now known to be alive.
139 // Mark it live in the specified sets and recursively mark arguments in callers
140 // live that are needed to pass in a value.
141 //
142 static void MarkArgumentLive(Argument *Arg,
143 std::set &MaybeLiveArguments,
144 std::set &LiveArguments,
145 const std::multimap &CallSites) {
292 /// MarkArgumentLive - The MaybeLive argument 'Arg' is now known to be alive.
293 /// Mark it live in the specified sets and recursively mark arguments in callers
294 /// live that are needed to pass in a value.
295 ///
296 void DAE::MarkArgumentLive(Argument *Arg) {
297 std::set::iterator It = MaybeLiveArguments.lower_bound(Arg);
298 if (It == MaybeLiveArguments.end() || *It != Arg) return;
299
146300 DEBUG(std::cerr << " MaybeLive argument now live: " << Arg->getName()<<"\n");
147 assert(MaybeLiveArguments.count(Arg) && !LiveArguments.count(Arg) &&
148 "Arg not MaybeLive?");
149 MaybeLiveArguments.erase(Arg);
301 MaybeLiveArguments.erase(It);
150302 LiveArguments.insert(Arg);
151303
152304 // Loop over all of the call sites of the function, making any arguments
155307 Function *Fn = Arg->getParent();
156308 unsigned ArgNo = std::distance(Fn->abegin(), Function::aiterator(Arg));
157309
158 std::multimap::const_iterator I =
159 CallSites.lower_bound(Fn);
310 std::multimap::iterator I = CallSites.lower_bound(Fn);
160311 for (; I != CallSites.end() && I->first == Fn; ++I) {
161 const CallSite &CS = I->second;
162 if (Argument *ActualArg = dyn_cast(*(CS.arg_begin()+ArgNo)))
163 if (MaybeLiveArguments.count(ActualArg))
164 MarkArgumentLive(ActualArg, MaybeLiveArguments, LiveArguments,
165 CallSites);
312 CallSite CS = I->second;
313 Value *ArgVal = *(CS.arg_begin()+ArgNo);
314 if (Argument *ActualArg = dyn_cast(ArgVal)) {
315 MarkArgumentLive(ActualArg);
316 } else {
317 // If the value passed in at this call site is a return value computed by
318 // some other call site, make sure to mark the return value at the other
319 // call site as being needed.
320 CallSite ArgCS = CallSite::get(ArgVal);
321 if (ArgCS.getInstruction())
322 if (Function *Fn = ArgCS.getCalledFunction())
323 MarkRetValLive(Fn);
324 }
325 }
326 }
327
328 /// MarkArgumentLive - The MaybeLive return value for the specified function is
329 /// now known to be alive. Propagate this fact to the return instructions which
330 /// produce it.
331 void DAE::MarkRetValLive(Function *F) {
332 assert(F && "Shame shame, we can't have null pointers here!");
333
334 // Check to see if we already knew it was live
335 std::set::iterator I = MaybeLiveRetVal.lower_bound(F);
336 if (I == MaybeLiveRetVal.end() || *I != F) return; // It's already alive!
337
338 DEBUG(std::cerr << " MaybeLive retval now live: " << F->getName() << "\n");
339
340 MaybeLiveRetVal.erase(I);
341 LiveRetVal.insert(F); // It is now known to be live!
342
343 // Loop over all of the functions, noticing that the return value is now live.
344 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
345 if (ReturnInst *RI = dyn_cast(BB->getTerminator()))
346 MarkReturnInstArgumentLive(RI);
347 }
348
349 void DAE::MarkReturnInstArgumentLive(ReturnInst *RI) {
350 Value *Op = RI->getOperand(0);
351 if (Argument *A = dyn_cast(Op)) {
352 MarkArgumentLive(A);
353 } else if (CallInst *CI = dyn_cast(Op)) {
354 if (Function *F = CI->getCalledFunction())
355 MarkRetValLive(F);
356 } else if (InvokeInst *II = dyn_cast(Op)) {
357 if (Function *F = II->getCalledFunction())
358 MarkRetValLive(F);
166359 }
167360 }
168361
170363 // specified by the DeadArguments list. Transform the function and all of the
171364 // callees of the function to not have these arguments.
172365 //
173 void DAE::RemoveDeadArgumentsFromFunction(Function *F,
174 std::set &DeadArguments){
366 void DAE::RemoveDeadArgumentsFromFunction(Function *F) {
175367 // Start by computing a new prototype for the function, which is the same as
176368 // the old function, but has fewer arguments.
177369 const FunctionType *FTy = F->getFunctionType();
181373 if (!DeadArguments.count(I))
182374 Params.push_back(I->getType());
183375
184 FunctionType *NFTy = FunctionType::get(FTy->getReturnType(), Params,
185 FTy->isVarArg());
186
376 const Type *RetTy = FTy->getReturnType();
377 if (DeadRetVal.count(F)) {
378 RetTy = Type::VoidTy;
379 DeadRetVal.erase(F);
380 }
381
382 FunctionType *NFTy = FunctionType::get(RetTy, Params, FTy->isVarArg());
383
187384 // Create the new function body and insert it into the module...
188385 Function *NF = new Function(NFTy, F->getLinkage(), F->getName());
189386 F->getParent()->getFunctionList().insert(F, NF);
191388 // Loop over all of the callers of the function, transforming the call sites
192389 // to pass in a smaller number of arguments into the new function.
193390 //
391 std::vector Args;
194392 while (!F->use_empty()) {
195393 CallSite CS = CallSite::get(F->use_back());
196394 Instruction *Call = CS.getInstruction();
197 CS.setCalledFunction(NF); // Reduce the uses count of F
198
395
199396 // Loop over the operands, deleting dead ones...
200397 CallSite::arg_iterator AI = CS.arg_begin();
201 for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
202 if (DeadArguments.count(I)) { // Remove operands for dead arguments
203 AI = Call->op_erase(AI);
204 } else {
205 ++AI; // Leave live operands alone...
398 for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I, ++AI)
399 if (!DeadArguments.count(I)) // Remove operands for dead arguments
400 Args.push_back(*AI);
401
402 Instruction *New;
403 if (InvokeInst *II = dyn_cast(Call)) {
404 New = new InvokeInst(NF, II->getNormalDest(), II->getExceptionalDest(),
405 Args, "", Call);
406 } else {
407 New = new CallInst(NF, Args, "", Call);
408 }
409 Args.clear();
410
411 if (!Call->use_empty()) {
412 if (New->getType() == Type::VoidTy)
413 Call->replaceAllUsesWith(Constant::getNullValue(Call->getType()));
414 else {
415 Call->replaceAllUsesWith(New);
416 std::string Name = Call->getName();
417 Call->setName("");
418 New->setName(Name);
206419 }
420 }
421
422 // Finally, remove the old call from the program, reducing the use-count of
423 // F.
424 Call->getParent()->getInstList().erase(Call);
207425 }
208426
209427 // Since we have now created the new function, splice the body of the old
231449 DeadArguments.erase(I);
232450 }
233451
452 // If we change the return value of the function we must rewrite any return
453 // instructions. Check this now.
454 if (F->getReturnType() != NF->getReturnType())
455 for (Function::iterator BB = NF->begin(), E = NF->end(); BB != E; ++BB)
456 if (ReturnInst *RI = dyn_cast(BB->getTerminator())) {
457 new ReturnInst(0, RI);
458 BB->getInstList().erase(RI);
459 }
460
234461 // Now that the old function is dead, delete it.
235462 F->getParent()->getFunctionList().erase(F);
236463 }
240467 // We assume all arguments are dead unless proven otherwise (allowing us to
241468 // determine that dead arguments passed into recursive functions are dead).
242469 //
243 std::set LiveArguments, MaybeLiveArguments, DeadArguments;
244 std::multimap CallSites;
245
246470 DEBUG(std::cerr << "DAE - Determining liveness\n");
247 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
248 Function &Fn = *I;
249 // If the function is intrinsically alive, just mark the arguments alive.
250 if (FunctionArgumentsIntrinsicallyAlive(Fn)) {
251 for (Function::aiterator AI = Fn.abegin(), E = Fn.aend(); AI != E; ++AI)
252 LiveArguments.insert(AI);
253 DEBUG(std::cerr << " Args intrinsically live for fn: " << Fn.getName()
254 << "\n");
471 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
472 SurveyFunction(*I);
473
474 // Loop over the instructions to inspect, propagating liveness among arguments
475 // and return values which are MaybeLive.
476
477 while (!InstructionsToInspect.empty()) {
478 Instruction *I = InstructionsToInspect.back();
479 InstructionsToInspect.pop_back();
480
481 if (ReturnInst *RI = dyn_cast(I)) {
482 // For return instructions, we just have to check to see if the return
483 // value for the current function is known now to be alive. If so, any
484 // arguments used by it are now alive, and any call instruction return
485 // value is alive as well.
486 if (LiveRetVal.count(RI->getParent()->getParent()))
487 MarkReturnInstArgumentLive(RI);
488
255489 } else {
256 DEBUG(std::cerr << " Inspecting args for fn: " << Fn.getName() << "\n");
257
258 // If it is not intrinsically alive, we know that all users of the
259 // function are call sites. Mark all of the arguments live which are
260 // directly used, and keep track of all of the call sites of this function
261 // if there are any arguments we assume that are dead.
490 CallSite CS = CallSite::get(I);
491 assert(CS.getInstruction() && "Unknown instruction for the I2I list!");
492
493 Function *Callee = CS.getCalledFunction();
494
495 // If we found a call or invoke instruction on this list, that means that
496 // an argument of the function is a call instruction. If the argument is
497 // live, then the return value of the called instruction is now live.
262498 //
263 bool AnyMaybeLiveArgs = false;
264 for (Function::aiterator AI = Fn.abegin(), E = Fn.aend(); AI != E; ++AI)
265 switch (getArgumentLiveness(*AI)) {
266 case Alive:
267 DEBUG(std::cerr << " Arg live by use: " << AI->getName() << "\n");
268 LiveArguments.insert(AI);
269 break;
270 case Dead:
271 DEBUG(std::cerr << " Arg definitely dead: " <getName()<<"\n");
272 DeadArguments.insert(AI);
273 break;
274 case MaybeLive:
275 DEBUG(std::cerr << " Arg only passed to calls: "
276 << AI->getName() << "\n");
277 AnyMaybeLiveArgs = true;
278 MaybeLiveArguments.insert(AI);
279 break;
280 }
281
282 // If there are any "MaybeLive" arguments, we need to check callees of
283 // this function when/if they become alive. Record which functions are
284 // callees...
285 if (AnyMaybeLiveArgs)
286 for (Value::use_iterator I = Fn.use_begin(), E = Fn.use_end();
287 I != E; ++I)
288 CallSites.insert(std::make_pair(&Fn, CallSite::get(*I)));
499 CallSite::arg_iterator AI = CS.arg_begin(); // ActualIterator
500 for (Function::aiterator FI = Callee->abegin(), E = Callee->aend();
501 FI != E; ++AI, ++FI) {
502 // If this argument is another call...
503 CallSite ArgCS = CallSite::get(*AI);
504 if (ArgCS.getInstruction() && LiveArguments.count(FI))
505 if (Function *Callee = ArgCS.getCalledFunction())
506 MarkRetValLive(Callee);
507 }
289508 }
290509 }
291510
294513 // passed into requires them to be live. Of course this could make other
295514 // arguments live, so process callers recursively.
296515 //
297 // Because elements can be removed from the MaybeLiveArguments list, copy it
298 // to a temporary vector.
516 // Because elements can be removed from the MaybeLiveArguments set, copy it to
517 // a temporary vector.
299518 //
300519 std::vector TmpArgList(MaybeLiveArguments.begin(),
301520 MaybeLiveArguments.end());
302521 for (unsigned i = 0, e = TmpArgList.size(); i != e; ++i) {
303522 Argument *MLA = TmpArgList[i];
304523 if (MaybeLiveArguments.count(MLA) &&
305 isMaybeLiveArgumentNowAlive(MLA, LiveArguments)) {
306 MarkArgumentLive(MLA, MaybeLiveArguments, LiveArguments, CallSites);
307 }
524 isMaybeLiveArgumentNowLive(MLA))
525 MarkArgumentLive(MLA);
308526 }
309527
310528 // Recover memory early...
313531 // At this point, we know that all arguments in DeadArguments and
314532 // MaybeLiveArguments are dead. If the two sets are empty, there is nothing
315533 // to do.
316 if (MaybeLiveArguments.empty() && DeadArguments.empty())
534 if (MaybeLiveArguments.empty() && DeadArguments.empty() &&
535 MaybeLiveRetVal.empty() && DeadRetVal.empty())
317536 return false;
318537
319538 // Otherwise, compact into one set, and start eliminating the arguments from
320539 // the functions.
321540 DeadArguments.insert(MaybeLiveArguments.begin(), MaybeLiveArguments.end());
322541 MaybeLiveArguments.clear();
542 DeadRetVal.insert(MaybeLiveRetVal.begin(), MaybeLiveRetVal.end());
543 MaybeLiveRetVal.clear();
544
545 LiveArguments.clear();
546 LiveRetVal.clear();
323547
324548 NumArgumentsEliminated += DeadArguments.size();
549 NumRetValsEliminated += DeadRetVal.size();
325550 while (!DeadArguments.empty())
326 RemoveDeadArgumentsFromFunction((*DeadArguments.begin())->getParent(),
327 DeadArguments);
551 RemoveDeadArgumentsFromFunction((*DeadArguments.begin())->getParent());
552
553 while (!DeadRetVal.empty())
554 RemoveDeadArgumentsFromFunction(*DeadRetVal.begin());
328555 return true;
329556 }