1 //===-- DeadArgumentElimination.cpp - Eliminate dead arguments ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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 return values 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 or return values.
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/SmallVector.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/Support/Compiler.h"
38 STATISTIC(NumArgumentsEliminated, "Number of unread args removed");
39 STATISTIC(NumRetValsEliminated , "Number of unused return values removed");
42 /// DAE - The dead argument elimination pass.
44 class VISIBILITY_HIDDEN DAE : public ModulePass {
47 /// Struct that represent either a (part of a) return value or a function
48 /// argument. Used so that arguments and return values can be used
51 RetOrArg(const Function* F, unsigned Idx, bool IsArg) : F(F), Idx(Idx), IsArg(IsArg) {}
56 /// Make RetOrArg comparable, so we can put it into a map
57 bool operator<(const RetOrArg &O) const {
60 else if (Idx != O.Idx)
63 return IsArg < O.IsArg;
66 /// Make RetOrArg comparable, so we can easily iterate the multimap
67 bool operator==(const RetOrArg &O) const {
68 return F == O.F && Idx == O.Idx && IsArg == O.IsArg;
72 /// Liveness enum - During our initial pass over the program, we determine
73 /// that things are either definately alive, definately dead, or in need of
74 /// interprocedural analysis (MaybeLive).
76 enum Liveness { Live, MaybeLive, Dead };
78 /// Convenience wrapper
79 RetOrArg CreateRet(const Function *F, unsigned Idx) { return RetOrArg(F, Idx, false); }
80 /// Convenience wrapper
81 RetOrArg CreateArg(const Function *F, unsigned Idx) { return RetOrArg(F, Idx, true); }
83 typedef std::multimap<RetOrArg, RetOrArg> UseMap;
84 /// This map maps a return value or argument to all return values or
85 /// arguments it uses.
86 /// For example (indices are left out for clarity):
87 /// - Uses[ret F] = ret G
88 /// This means that F calls G, and F returns the value returned by G.
89 /// - Uses[arg F] = ret G
90 /// This means that some function calls G and passes its result as an
92 /// - Uses[ret F] = arg F
93 /// This means that F returns one of its own arguments.
94 /// - Uses[arg F] = arg G
95 /// This means that G calls F and passes one of its own (G's) arguments
99 typedef std::set<RetOrArg> LiveSet;
101 /// This set contains all values that have been determined to be live
104 typedef SmallVector<RetOrArg, 5> UseVector;
107 static char ID; // Pass identification, replacement for typeid
108 DAE() : ModulePass((intptr_t)&ID) {}
109 bool runOnModule(Module &M);
111 virtual bool ShouldHackArguments() const { return false; }
114 Liveness IsMaybeLive(RetOrArg Use, UseVector &MaybeLiveUses);
115 Liveness SurveyUse(Value::use_iterator U, UseVector &MaybeLiveUses, unsigned RetValNum = 0);
116 Liveness SurveyUses(Value *V, UseVector &MaybeLiveUses);
118 void SurveyFunction(Function &F);
119 void MarkValue(const RetOrArg &RA, Liveness L, const UseVector &MaybeLiveUses);
120 void MarkLive(RetOrArg RA);
121 bool RemoveDeadStuffFromFunction(Function *F);
122 bool DeleteDeadVarargs(Function &Fn);
128 static RegisterPass<DAE>
129 X("deadargelim", "Dead Argument Elimination");
132 /// DAH - DeadArgumentHacking pass - Same as dead argument elimination, but
133 /// deletes arguments to functions which are external. This is only for use
135 struct DAH : public DAE {
137 virtual bool ShouldHackArguments() const { return true; }
142 static RegisterPass<DAH>
143 Y("deadarghaX0r", "Dead Argument Hacking (BUGPOINT USE ONLY; DO NOT USE)");
145 /// createDeadArgEliminationPass - This pass removes arguments from functions
146 /// which are not used by the body of the function.
148 ModulePass *llvm::createDeadArgEliminationPass() { return new DAE(); }
149 ModulePass *llvm::createDeadArgHackingPass() { return new DAH(); }
151 /// DeleteDeadVarargs - If this is an function that takes a ... list, and if
152 /// llvm.vastart is never called, the varargs list is dead for the function.
153 bool DAE::DeleteDeadVarargs(Function &Fn) {
154 assert(Fn.getFunctionType()->isVarArg() && "Function isn't varargs!");
155 if (Fn.isDeclaration() || !Fn.hasInternalLinkage()) return false;
157 // Ensure that the function is only directly called.
158 for (Value::use_iterator I = Fn.use_begin(), E = Fn.use_end(); I != E; ++I) {
159 // If this use is anything other than a call site, give up.
160 CallSite CS = CallSite::get(*I);
161 Instruction *TheCall = CS.getInstruction();
162 if (!TheCall) return false; // Not a direct call site?
164 // The addr of this function is passed to the call.
165 if (I.getOperandNo() != 0) return false;
168 // Okay, we know we can transform this function if safe. Scan its body
169 // looking for calls to llvm.vastart.
170 for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
171 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
172 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
173 if (II->getIntrinsicID() == Intrinsic::vastart)
179 // If we get here, there are no calls to llvm.vastart in the function body,
180 // remove the "..." and adjust all the calls.
182 // Start by computing a new prototype for the function, which is the same as
183 // the old function, but doesn't have isVarArg set.
184 const FunctionType *FTy = Fn.getFunctionType();
185 std::vector<const Type*> Params(FTy->param_begin(), FTy->param_end());
186 FunctionType *NFTy = FunctionType::get(FTy->getReturnType(), Params, false);
187 unsigned NumArgs = Params.size();
189 // Create the new function body and insert it into the module...
190 Function *NF = Function::Create(NFTy, Fn.getLinkage());
191 NF->copyAttributesFrom(&Fn);
192 Fn.getParent()->getFunctionList().insert(&Fn, NF);
195 // Loop over all of the callers of the function, transforming the call sites
196 // to pass in a smaller number of arguments into the new function.
198 std::vector<Value*> Args;
199 while (!Fn.use_empty()) {
200 CallSite CS = CallSite::get(Fn.use_back());
201 Instruction *Call = CS.getInstruction();
203 // Pass all the same arguments.
204 Args.assign(CS.arg_begin(), CS.arg_begin()+NumArgs);
206 // Drop any attributes that were on the vararg arguments.
207 PAListPtr PAL = CS.getParamAttrs();
208 if (!PAL.isEmpty() && PAL.getSlot(PAL.getNumSlots() - 1).Index > NumArgs) {
209 SmallVector<ParamAttrsWithIndex, 8> ParamAttrsVec;
210 for (unsigned i = 0; PAL.getSlot(i).Index <= NumArgs; ++i)
211 ParamAttrsVec.push_back(PAL.getSlot(i));
212 PAL = PAListPtr::get(ParamAttrsVec.begin(), ParamAttrsVec.end());
216 if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
217 New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),
218 Args.begin(), Args.end(), "", Call);
219 cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
220 cast<InvokeInst>(New)->setParamAttrs(PAL);
222 New = CallInst::Create(NF, Args.begin(), Args.end(), "", Call);
223 cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
224 cast<CallInst>(New)->setParamAttrs(PAL);
225 if (cast<CallInst>(Call)->isTailCall())
226 cast<CallInst>(New)->setTailCall();
230 if (!Call->use_empty())
231 Call->replaceAllUsesWith(New);
235 // Finally, remove the old call from the program, reducing the use-count of
237 Call->eraseFromParent();
240 // Since we have now created the new function, splice the body of the old
241 // function right into the new function, leaving the old rotting hulk of the
243 NF->getBasicBlockList().splice(NF->begin(), Fn.getBasicBlockList());
245 // Loop over the argument list, transfering uses of the old arguments over to
246 // the new arguments, also transfering over the names as well. While we're at
247 // it, remove the dead arguments from the DeadArguments list.
249 for (Function::arg_iterator I = Fn.arg_begin(), E = Fn.arg_end(),
250 I2 = NF->arg_begin(); I != E; ++I, ++I2) {
251 // Move the name and users over to the new version.
252 I->replaceAllUsesWith(I2);
256 // Finally, nuke the old function.
257 Fn.eraseFromParent();
261 /// Convenience function that returns the number of return values. It returns 0
262 /// for void functions and 1 for functions not returning a struct. It returns
263 /// the number of struct elements for functions returning a struct.
264 static unsigned NumRetVals(const Function *F) {
265 if (F->getReturnType() == Type::VoidTy)
267 else if (const StructType *STy = dyn_cast<StructType>(F->getReturnType()))
268 return STy->getNumElements();
273 /// IsMaybeAlive - This checks Use for liveness. If Use is live, returns Live,
274 /// else returns MaybeLive. Also, adds Use to MaybeLiveUses in the latter case.
275 DAE::Liveness DAE::IsMaybeLive(RetOrArg Use, UseVector &MaybeLiveUses) {
276 // We're live if our use is already marked as live
277 if (LiveValues.count(Use))
280 // We're maybe live otherwise, but remember that we must become live if
282 MaybeLiveUses.push_back(Use);
287 /// SurveyUse - This looks at a single use of an argument or return value
288 /// and determines if it should be alive or not. Adds this use to MaybeLiveUses
289 /// if it causes the used value to become MaybeAlive.
291 /// RetValNum is the return value number to use when this use is used in a
292 /// return instruction. This is used in the recursion, you should always leave
294 DAE::Liveness DAE::SurveyUse(Value::use_iterator U, UseVector &MaybeLiveUses, unsigned RetValNum) {
296 if (ReturnInst *RI = dyn_cast<ReturnInst>(V)) {
297 // The value is returned from another function. It's only live when the
298 // caller's return value is live
299 RetOrArg Use = CreateRet(RI->getParent()->getParent(), RetValNum);
300 // We might be live, depending on the liveness of Use
301 return IsMaybeLive(Use, MaybeLiveUses);
303 if (InsertValueInst *IV = dyn_cast<InsertValueInst>(V)) {
304 if (U.getOperandNo() != InsertValueInst::getAggregateOperandIndex() && IV->hasIndices())
305 // The use we are examining is inserted into an aggregate. Our liveness
306 // depends on all uses of that aggregate, but if it is used as a return
307 // value, only index at which we were inserted counts.
308 RetValNum = *IV->idx_begin();
310 // Note that if we are used as the aggregate operand to the insertvalue,
311 // we don't change RetValNum, but do survey all our uses.
313 Liveness Result = Dead;
314 for (Value::use_iterator I = IV->use_begin(),
315 E = V->use_end(); I != E; ++I) {
316 Result = SurveyUse(I, MaybeLiveUses, RetValNum);
322 CallSite CS = CallSite::get(V);
323 if (CS.getInstruction()) {
324 Function *F = CS.getCalledFunction();
326 // Used in a direct call
328 // Check for vararg. Do - 1 to skip the first operand to call (the
330 if (U.getOperandNo() - 1 >= F->getFunctionType()->getNumParams())
331 // The value is passed in through a vararg! Must be live.
334 // Value passed to a normal call. It's only live when the corresponding
335 // argument (operand number - 1 to skip the function pointer operand) to
336 // the called function turns out live
337 RetOrArg Use = CreateArg(F, U.getOperandNo() - 1);
338 return IsMaybeLive(Use, MaybeLiveUses);
340 // Used in any other way? Value must be live.
344 // Used in any other way? Value must be live.
348 /// SurveyUses - This looks at all the uses of the given return value
349 /// (possibly a partial return value from a function returning a struct).
350 /// Returns the Liveness deduced from the uses of this value.
352 /// Adds all uses that cause the result to be MaybeLive to MaybeLiveRetUses.
353 DAE::Liveness DAE::SurveyUses(Value *V, UseVector &MaybeLiveUses) {
354 // Assume it's dead (which will only hold if there are no uses at all..)
355 Liveness Result = Dead;
357 for (Value::use_iterator I = V->use_begin(),
358 E = V->use_end(); I != E; ++I) {
359 Result = SurveyUse(I, MaybeLiveUses);
366 // SurveyFunction - This performs the initial survey of the specified function,
367 // checking out whether or not it uses any of its incoming arguments or whether
368 // any callers use the return value. This fills in the
369 // (Dead|MaybeLive|Live)(Arguments|RetVal) sets.
371 // We consider arguments of non-internal functions to be intrinsically alive as
372 // well as arguments to functions which have their "address taken".
374 void DAE::SurveyFunction(Function &F) {
375 bool FunctionIntrinsicallyLive = false;
376 unsigned RetCount = NumRetVals(&F);
377 // Assume all return values are dead
378 typedef SmallVector<Liveness, 5> RetVals;
379 RetVals RetValLiveness(RetCount, Dead);
381 // These vectors maps each return value to the uses that make it MaybeLive, so
382 // we can add those to the MaybeLiveRetVals list if the return value
383 // really turns out to be MaybeLive. Initializes to RetCount empty vectors
384 typedef SmallVector<UseVector, 5> RetUses;
385 // Intialized to a list of RetCount empty lists
386 RetUses MaybeLiveRetUses(RetCount);
388 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
389 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()))
390 if (RI->getNumOperands() != 0 && RI->getOperand(0)->getType() != F.getFunctionType()->getReturnType()) {
391 // We don't support old style multiple return values
392 FunctionIntrinsicallyLive = true;
396 if (!F.hasInternalLinkage() && (!ShouldHackArguments() || F.isIntrinsic()))
397 FunctionIntrinsicallyLive = true;
399 if (!FunctionIntrinsicallyLive) {
400 DOUT << "DAE - Inspecting callers for fn: " << F.getName() << "\n";
401 // Keep track of the number of live retvals, so we can skip checks once all
402 // of them turn out to be live.
403 unsigned NumLiveRetVals = 0;
404 const Type *STy = dyn_cast<StructType>(F.getReturnType());
405 // Loop all uses of the function
406 for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I) {
407 // If the function is PASSED IN as an argument, its address has been taken
408 if (I.getOperandNo() != 0) {
409 FunctionIntrinsicallyLive = true;
413 // If this use is anything other than a call site, the function is alive.
414 CallSite CS = CallSite::get(*I);
415 Instruction *TheCall = CS.getInstruction();
416 if (!TheCall) { // Not a direct call site?
417 FunctionIntrinsicallyLive = true;
421 // If we end up here, we are looking at a direct call to our function.
423 // Now, check how our return value(s) is/are used in this caller. Don't
424 // bother checking return values if all of them are live already
425 if (NumLiveRetVals != RetCount) {
427 // Check all uses of the return value
428 for (Value::use_iterator I = TheCall->use_begin(),
429 E = TheCall->use_end(); I != E; ++I) {
430 ExtractValueInst *Ext = dyn_cast<ExtractValueInst>(*I);
431 if (Ext && Ext->hasIndices()) {
432 // This use uses a part of our return value, survey the uses of that
433 // part and store the results for this index only.
434 unsigned Idx = *Ext->idx_begin();
435 if (RetValLiveness[Idx] != Live) {
436 RetValLiveness[Idx] = SurveyUses(Ext, MaybeLiveRetUses[Idx]);
437 if (RetValLiveness[Idx] == Live)
441 // Used by something else than extractvalue. Mark all
442 // return values as live.
443 for (unsigned i = 0; i != RetCount; ++i )
444 RetValLiveness[i] = Live;
445 NumLiveRetVals = RetCount;
450 // Single return value
451 RetValLiveness[0] = SurveyUses(TheCall, MaybeLiveRetUses[0]);
452 if (RetValLiveness[0] == Live)
453 NumLiveRetVals = RetCount;
458 if (FunctionIntrinsicallyLive) {
459 DOUT << "DAE - Intrinsically live fn: " << F.getName() << "\n";
460 // Mark all arguments as live
462 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
464 MarkLive(CreateArg(&F, i));
465 // Mark all return values as live
467 for (unsigned i = 0, e = RetValLiveness.size(); i != e; ++i)
468 MarkLive(CreateRet(&F, i));
472 // Now we've inspected all callers, record the liveness of our return values.
473 for (unsigned i = 0, e = RetValLiveness.size(); i != e; ++i) {
474 RetOrArg Ret = CreateRet(&F, i);
475 // Mark the result down
476 MarkValue(Ret, RetValLiveness[i], MaybeLiveRetUses[i]);
478 DOUT << "DAE - Inspecting args for fn: " << F.getName() << "\n";
480 // Now, check all of our arguments
482 UseVector MaybeLiveArgUses;
483 for (Function::arg_iterator AI = F.arg_begin(),
484 E = F.arg_end(); AI != E; ++AI, ++i) {
485 // See what the effect of this use is (recording any uses that cause
486 // MaybeLive in MaybeLiveArgUses)
487 Liveness Result = SurveyUses(AI, MaybeLiveArgUses);
488 RetOrArg Arg = CreateArg(&F, i);
489 // Mark the result down
490 MarkValue(Arg, Result, MaybeLiveArgUses);
491 // Clear the vector again for the next iteration
492 MaybeLiveArgUses.clear();
496 /// MarkValue - This function marks the liveness of RA depending on L. If L is
497 /// MaybeLive, it also records any uses in MaybeLiveUses such that RA will be
498 /// marked live if any use in MaybeLiveUses gets marked live later on.
499 void DAE::MarkValue(const RetOrArg &RA, Liveness L, const UseVector &MaybeLiveUses) {
501 case Live: MarkLive(RA); break;
504 // Note any uses of this value, so this return value can be
505 // marked live whenever one of the uses becomes live.
506 UseMap::iterator Where = Uses.begin();
507 for (UseVector::const_iterator UI = MaybeLiveUses.begin(),
508 UE = MaybeLiveUses.end(); UI != UE; ++UI)
509 Where = Uses.insert(Where, UseMap::value_type(*UI, RA));
516 /// MarkLive - Mark the given return value or argument as live. Additionally,
517 /// mark any values that are used by this value (according to Uses) live as
519 void DAE::MarkLive(RetOrArg RA) {
520 if (!LiveValues.insert(RA).second)
521 return; // We were already marked Live
524 DOUT << "DAE - Marking argument " << RA.Idx << " to function " << RA.F->getNameStart() << " live\n";
526 DOUT << "DAE - Marking return value " << RA.Idx << " of function " << RA.F->getNameStart() << " live\n";
528 // We don't use upper_bound (or equal_range) here, because our recursive call
529 // to ourselves is likely to mark the upper_bound (which is the first value
530 // not belonging to RA) to become erased and the iterator invalidated.
531 UseMap::iterator Begin = Uses.lower_bound(RA);
532 UseMap::iterator E = Uses.end();
534 for (I = Begin; I != E && I->first == RA; ++I)
537 // Erase RA from the Uses map (from the lower bound to wherever we ended up
539 Uses.erase(Begin, I);
542 // RemoveDeadStuffFromFunction - Remove any arguments and return values from F
543 // that are not in LiveValues. This function is a noop for any Function created
544 // by this function before, or any function that was not inspected for liveness.
545 // specified by the DeadArguments list. Transform the function and all of the
546 // callees of the function to not have these arguments.
548 bool DAE::RemoveDeadStuffFromFunction(Function *F) {
549 // Quick exit path for external functions
550 if (!F->hasInternalLinkage() && (!ShouldHackArguments() || F->isIntrinsic()))
553 // Start by computing a new prototype for the function, which is the same as
554 // the old function, but has fewer arguments and a different return type.
555 const FunctionType *FTy = F->getFunctionType();
556 std::vector<const Type*> Params;
558 // Set up to build a new list of parameter attributes
559 SmallVector<ParamAttrsWithIndex, 8> ParamAttrsVec;
560 const PAListPtr &PAL = F->getParamAttrs();
562 // The existing function return attributes.
563 ParameterAttributes RAttrs = PAL.getParamAttrs(0);
566 // Find out the new return value
568 const Type *RetTy = FTy->getReturnType();
570 unsigned RetCount = NumRetVals(F);
571 // Explicitely track if anything changed, for debugging
572 bool Changed = false;
573 // -1 means unused, other numbers are the new index
574 SmallVector<int, 5> NewRetIdxs(RetCount, -1);
575 std::vector<const Type*> RetTypes;
576 if (RetTy != Type::VoidTy) {
577 const StructType *STy = dyn_cast<StructType>(RetTy);
579 // Look at each of the original return values individually
580 for (unsigned i = 0; i != RetCount; ++i) {
581 RetOrArg Ret = CreateRet(F, i);
582 if (LiveValues.erase(Ret)) {
583 RetTypes.push_back(STy->getElementType(i));
584 NewRetIdxs[i] = RetTypes.size() - 1;
586 ++NumRetValsEliminated;
587 DOUT << "DAE - Removing return value " << i << " from " << F->getNameStart() << "\n";
592 // We used to return a single value
593 if (LiveValues.erase(CreateRet(F, 0))) {
594 RetTypes.push_back(RetTy);
597 DOUT << "DAE - Removing return value from " << F->getNameStart() << "\n";
598 ++NumRetValsEliminated;
601 if (RetTypes.size() > 1 || (STy && STy->getNumElements() == RetTypes.size()))
602 // More than one return type? Return a struct with them. Also, if we used
603 // to return a struct and didn't change the number of return values,
604 // return a struct again. This prevents chaning {something} into something
606 // Make the new struct packed if we used to return a packed struct
608 NRetTy = StructType::get(RetTypes, STy->isPacked());
609 else if (RetTypes.size() == 1)
610 // One return type? Just a simple value then, but only if we didn't use to
611 // return a struct with that simple value before.
612 NRetTy = RetTypes.front();
613 else if (RetTypes.size() == 0)
614 // No return types? Make it void, but only if we didn't use to return {}
615 NRetTy = Type::VoidTy;
617 NRetTy = Type::VoidTy;
620 // Remove any incompatible attributes
621 RAttrs &= ~ParamAttr::typeIncompatible(NRetTy);
623 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(0, RAttrs));
625 // Remember which arguments are still alive
626 SmallVector<bool, 10> ArgAlive(FTy->getNumParams(), false);
627 // Construct the new parameter list from non-dead arguments. Also construct
628 // a new set of parameter attributes to correspond. Skip the first parameter
629 // attribute, since that belongs to the return value.
631 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
633 RetOrArg Arg = CreateArg(F, i);
634 if (LiveValues.erase(Arg)) {
635 Params.push_back(I->getType());
638 // Get the original parameter attributes (skipping the first one, that is
639 // for the return value
640 if (ParameterAttributes Attrs = PAL.getParamAttrs(i + 1))
641 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Params.size(), Attrs));
643 ++NumArgumentsEliminated;
644 DOUT << "DAE - Removing argument " << i << " (" << I->getNameStart() << ") from " << F->getNameStart() << "\n";
649 // Reconstruct the ParamAttrsList based on the vector we constructed.
650 PAListPtr NewPAL = PAListPtr::get(ParamAttrsVec.begin(), ParamAttrsVec.end());
652 // Work around LLVM bug PR56: the CWriter cannot emit varargs functions which
653 // have zero fixed arguments.
655 // Not that we apply this hack for a vararg fuction that does not have any
656 // arguments anymore, but did have them before (so don't bother fixing
657 // functions that were already broken wrt CWriter).
658 bool ExtraArgHack = false;
659 if (Params.empty() && FTy->isVarArg() && FTy->getNumParams() != 0) {
661 Params.push_back(Type::Int32Ty);
664 // Create the new function type based on the recomputed parameters.
665 FunctionType *NFTy = FunctionType::get(NRetTy, Params, FTy->isVarArg());
671 // The function type is only allowed to be different if we actually left out
672 // an argument or return value
673 assert(Changed && "Function type changed while no arguments or retrurn values were removed!");
675 // Create the new function body and insert it into the module...
676 Function *NF = Function::Create(NFTy, F->getLinkage());
677 NF->copyAttributesFrom(F);
678 NF->setParamAttrs(NewPAL);
679 // Insert the new function before the old function, so we won't be processing
681 F->getParent()->getFunctionList().insert(F, NF);
684 // Loop over all of the callers of the function, transforming the call sites
685 // to pass in a smaller number of arguments into the new function.
687 std::vector<Value*> Args;
688 while (!F->use_empty()) {
689 CallSite CS = CallSite::get(F->use_back());
690 Instruction *Call = CS.getInstruction();
692 ParamAttrsVec.clear();
693 const PAListPtr &CallPAL = CS.getParamAttrs();
695 // The call return attributes.
696 ParameterAttributes RAttrs = CallPAL.getParamAttrs(0);
697 // Adjust in case the function was changed to return void.
698 RAttrs &= ~ParamAttr::typeIncompatible(NF->getReturnType());
700 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(0, RAttrs));
702 // Declare these outside of the loops, so we can reuse them for the second
703 // loop, which loops the varargs
704 CallSite::arg_iterator I = CS.arg_begin();
706 // Loop over those operands, corresponding to the normal arguments to the
707 // original function, and add those that are still alive.
708 for (unsigned e = FTy->getNumParams(); i != e; ++I, ++i)
711 // Get original parameter attributes, but skip return attributes
712 if (ParameterAttributes Attrs = CallPAL.getParamAttrs(i + 1))
713 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Args.size(), Attrs));
717 Args.push_back(UndefValue::get(Type::Int32Ty));
719 // Push any varargs arguments on the list. Don't forget their attributes.
720 for (CallSite::arg_iterator E = CS.arg_end(); I != E; ++I, ++i) {
722 if (ParameterAttributes Attrs = CallPAL.getParamAttrs(i + 1))
723 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Args.size(), Attrs));
726 // Reconstruct the ParamAttrsList based on the vector we constructed.
727 PAListPtr NewCallPAL = PAListPtr::get(ParamAttrsVec.begin(),
728 ParamAttrsVec.end());
731 if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
732 New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),
733 Args.begin(), Args.end(), "", Call);
734 cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
735 cast<InvokeInst>(New)->setParamAttrs(NewCallPAL);
737 New = CallInst::Create(NF, Args.begin(), Args.end(), "", Call);
738 cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
739 cast<CallInst>(New)->setParamAttrs(NewCallPAL);
740 if (cast<CallInst>(Call)->isTailCall())
741 cast<CallInst>(New)->setTailCall();
745 if (!Call->use_empty()) {
746 if (New->getType() == Call->getType()) {
747 // Return type not changed? Just replace users then
748 Call->replaceAllUsesWith(New);
750 } else if (New->getType() == Type::VoidTy) {
751 // Our return value has uses, but they will get removed later on.
752 // Replace by null for now.
753 Call->replaceAllUsesWith(Constant::getNullValue(Call->getType()));
755 assert(isa<StructType>(RetTy) && "Return type changed, but not into a"
756 "void. The old return type must have"
758 // The original return value was a struct, update all uses (which are
759 // all extractvalue instructions).
760 for (Value::use_iterator I = Call->use_begin(), E = Call->use_end();
762 assert(isa<ExtractValueInst>(*I) && "Return value not only used by extractvalue?");
763 ExtractValueInst *EV = cast<ExtractValueInst>(*I);
764 // Increment now, since we're about to throw away this use.
766 assert(EV->hasIndices() && "Return value used by extractvalue without indices?");
767 unsigned Idx = *EV->idx_begin();
768 if (NewRetIdxs[Idx] != -1) {
769 if (RetTypes.size() > 1) {
770 // We're still returning a struct, create a new extractvalue
771 // instruction with the first index updated
772 std::vector<unsigned> NewIdxs(EV->idx_begin(), EV->idx_end());
773 NewIdxs[0] = NewRetIdxs[Idx];
774 Value *NEV = ExtractValueInst::Create(New, NewIdxs.begin(), NewIdxs.end(), "retval", EV);
775 EV->replaceAllUsesWith(NEV);
776 EV->eraseFromParent();
778 // We are now only returning a simple value, remove the
780 EV->replaceAllUsesWith(New);
781 EV->eraseFromParent();
784 // Value unused, replace uses by null for now, they will get removed
786 EV->replaceAllUsesWith(Constant::getNullValue(EV->getType()));
787 EV->eraseFromParent();
794 // Finally, remove the old call from the program, reducing the use-count of
796 Call->eraseFromParent();
799 // Since we have now created the new function, splice the body of the old
800 // function right into the new function, leaving the old rotting hulk of the
802 NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());
804 // Loop over the argument list, transfering uses of the old arguments over to
805 // the new arguments, also transfering over the names as well.
807 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(),
808 I2 = NF->arg_begin(); I != E; ++I, ++i)
810 // If this is a live argument, move the name and users over to the new
812 I->replaceAllUsesWith(I2);
816 // If this argument is dead, replace any uses of it with null constants
817 // (these are guaranteed to become unused later on)
818 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
821 // If we change the return value of the function we must rewrite any return
822 // instructions. Check this now.
823 if (F->getReturnType() != NF->getReturnType())
824 for (Function::iterator BB = NF->begin(), E = NF->end(); BB != E; ++BB)
825 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
828 if (NFTy->getReturnType() == Type::VoidTy) {
831 assert (isa<StructType>(RetTy));
832 // The original return value was a struct, insert
833 // extractvalue/insertvalue chains to extract only the values we need
834 // to return and insert them into our new result.
835 // This does generate messy code, but we'll let it to instcombine to
837 Value *OldRet = RI->getOperand(0);
838 // Start out building up our return value from undef
839 RetVal = llvm::UndefValue::get(NRetTy);
840 for (unsigned i = 0; i != RetCount; ++i)
841 if (NewRetIdxs[i] != -1) {
842 ExtractValueInst *EV = ExtractValueInst::Create(OldRet, i, "newret", RI);
843 if (RetTypes.size() > 1) {
844 // We're still returning a struct, so reinsert the value into
845 // our new return value at the new index
847 RetVal = InsertValueInst::Create(RetVal, EV, NewRetIdxs[i], "oldret");
849 // We are now only returning a simple value, so just return the
855 // Replace the return instruction with one returning the new return
856 // value (possibly 0 if we became void).
857 ReturnInst::Create(RetVal, RI);
858 BB->getInstList().erase(RI);
861 // Now that the old function is dead, delete it.
862 F->eraseFromParent();
867 bool DAE::runOnModule(Module &M) {
868 bool Changed = false;
869 // First pass: Do a simple check to see if any functions can have their "..."
870 // removed. We can do this if they never call va_start. This loop cannot be
871 // fused with the next loop, because deleting a function invalidates
872 // information computed while surveying other functions.
873 DOUT << "DAE - Deleting dead varargs\n";
874 for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
876 if (F.getFunctionType()->isVarArg())
877 Changed |= DeleteDeadVarargs(F);
880 // Second phase:loop through the module, determining which arguments are live.
881 // We assume all arguments are dead unless proven otherwise (allowing us to
882 // determine that dead arguments passed into recursive functions are dead).
884 DOUT << "DAE - Determining liveness\n";
885 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
888 // Now, remove all dead arguments and return values from each function in
890 for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
891 // Increment now, because the function will probably get removed (ie
892 // replaced by a new one)
894 Changed |= RemoveDeadStuffFromFunction(F);