LLVM API Documentation

DeadArgumentElimination.cpp
Go to the documentation of this file.
00001 //===-- DeadArgumentElimination.cpp - Eliminate dead arguments ------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This pass deletes dead arguments from internal functions.  Dead argument
00011 // elimination removes arguments which are directly dead, as well as arguments
00012 // only passed into function calls as dead arguments of other functions.  This
00013 // pass also deletes dead return values in a similar way.
00014 //
00015 // This pass is often useful as a cleanup pass to run after aggressive
00016 // interprocedural passes, which add possibly-dead arguments or return values.
00017 //
00018 //===----------------------------------------------------------------------===//
00019 
00020 #define DEBUG_TYPE "deadargelim"
00021 #include "llvm/Transforms/IPO.h"
00022 #include "llvm/ADT/DenseMap.h"
00023 #include "llvm/ADT/SmallVector.h"
00024 #include "llvm/ADT/Statistic.h"
00025 #include "llvm/ADT/StringExtras.h"
00026 #include "llvm/DIBuilder.h"
00027 #include "llvm/DebugInfo.h"
00028 #include "llvm/IR/CallingConv.h"
00029 #include "llvm/IR/Constant.h"
00030 #include "llvm/IR/DerivedTypes.h"
00031 #include "llvm/IR/Instructions.h"
00032 #include "llvm/IR/IntrinsicInst.h"
00033 #include "llvm/IR/LLVMContext.h"
00034 #include "llvm/IR/Module.h"
00035 #include "llvm/Pass.h"
00036 #include "llvm/Support/CallSite.h"
00037 #include "llvm/Support/Debug.h"
00038 #include "llvm/Support/raw_ostream.h"
00039 #include <map>
00040 #include <set>
00041 using namespace llvm;
00042 
00043 STATISTIC(NumArgumentsEliminated, "Number of unread args removed");
00044 STATISTIC(NumRetValsEliminated  , "Number of unused return values removed");
00045 STATISTIC(NumArgumentsReplacedWithUndef, 
00046           "Number of unread args replaced with undef");
00047 namespace {
00048   /// DAE - The dead argument elimination pass.
00049   ///
00050   class DAE : public ModulePass {
00051   public:
00052 
00053     /// Struct that represents (part of) either a return value or a function
00054     /// argument.  Used so that arguments and return values can be used
00055     /// interchangeably.
00056     struct RetOrArg {
00057       RetOrArg(const Function *F, unsigned Idx, bool IsArg) : F(F), Idx(Idx),
00058                IsArg(IsArg) {}
00059       const Function *F;
00060       unsigned Idx;
00061       bool IsArg;
00062 
00063       /// Make RetOrArg comparable, so we can put it into a map.
00064       bool operator<(const RetOrArg &O) const {
00065         if (F != O.F)
00066           return F < O.F;
00067         else if (Idx != O.Idx)
00068           return Idx < O.Idx;
00069         else
00070           return IsArg < O.IsArg;
00071       }
00072 
00073       /// Make RetOrArg comparable, so we can easily iterate the multimap.
00074       bool operator==(const RetOrArg &O) const {
00075         return F == O.F && Idx == O.Idx && IsArg == O.IsArg;
00076       }
00077 
00078       std::string getDescription() const {
00079         return std::string((IsArg ? "Argument #" : "Return value #"))
00080                + utostr(Idx) + " of function " + F->getName().str();
00081       }
00082     };
00083 
00084     /// Liveness enum - During our initial pass over the program, we determine
00085     /// that things are either alive or maybe alive. We don't mark anything
00086     /// explicitly dead (even if we know they are), since anything not alive
00087     /// with no registered uses (in Uses) will never be marked alive and will
00088     /// thus become dead in the end.
00089     enum Liveness { Live, MaybeLive };
00090 
00091     /// Convenience wrapper
00092     RetOrArg CreateRet(const Function *F, unsigned Idx) {
00093       return RetOrArg(F, Idx, false);
00094     }
00095     /// Convenience wrapper
00096     RetOrArg CreateArg(const Function *F, unsigned Idx) {
00097       return RetOrArg(F, Idx, true);
00098     }
00099 
00100     typedef std::multimap<RetOrArg, RetOrArg> UseMap;
00101     /// This maps a return value or argument to any MaybeLive return values or
00102     /// arguments it uses. This allows the MaybeLive values to be marked live
00103     /// when any of its users is marked live.
00104     /// For example (indices are left out for clarity):
00105     ///  - Uses[ret F] = ret G
00106     ///    This means that F calls G, and F returns the value returned by G.
00107     ///  - Uses[arg F] = ret G
00108     ///    This means that some function calls G and passes its result as an
00109     ///    argument to F.
00110     ///  - Uses[ret F] = arg F
00111     ///    This means that F returns one of its own arguments.
00112     ///  - Uses[arg F] = arg G
00113     ///    This means that G calls F and passes one of its own (G's) arguments
00114     ///    directly to F.
00115     UseMap Uses;
00116 
00117     typedef std::set<RetOrArg> LiveSet;
00118     typedef std::set<const Function*> LiveFuncSet;
00119 
00120     /// This set contains all values that have been determined to be live.
00121     LiveSet LiveValues;
00122     /// This set contains all values that are cannot be changed in any way.
00123     LiveFuncSet LiveFunctions;
00124 
00125     typedef SmallVector<RetOrArg, 5> UseVector;
00126 
00127     // Map each LLVM function to corresponding metadata with debug info. If
00128     // the function is replaced with another one, we should patch the pointer
00129     // to LLVM function in metadata.
00130     // As the code generation for module is finished (and DIBuilder is
00131     // finalized) we assume that subprogram descriptors won't be changed, and
00132     // they are stored in map for short duration anyway.
00133     typedef DenseMap<Function*, DISubprogram> FunctionDIMap;
00134     FunctionDIMap FunctionDIs;
00135 
00136   protected:
00137     // DAH uses this to specify a different ID.
00138     explicit DAE(char &ID) : ModulePass(ID) {}
00139 
00140   public:
00141     static char ID; // Pass identification, replacement for typeid
00142     DAE() : ModulePass(ID) {
00143       initializeDAEPass(*PassRegistry::getPassRegistry());
00144     }
00145 
00146     bool runOnModule(Module &M);
00147 
00148     virtual bool ShouldHackArguments() const { return false; }
00149 
00150   private:
00151     Liveness MarkIfNotLive(RetOrArg Use, UseVector &MaybeLiveUses);
00152     Liveness SurveyUse(Value::const_use_iterator U, UseVector &MaybeLiveUses,
00153                        unsigned RetValNum = 0);
00154     Liveness SurveyUses(const Value *V, UseVector &MaybeLiveUses);
00155 
00156     void CollectFunctionDIs(Module &M);
00157     void SurveyFunction(const Function &F);
00158     void MarkValue(const RetOrArg &RA, Liveness L,
00159                    const UseVector &MaybeLiveUses);
00160     void MarkLive(const RetOrArg &RA);
00161     void MarkLive(const Function &F);
00162     void PropagateLiveness(const RetOrArg &RA);
00163     bool RemoveDeadStuffFromFunction(Function *F);
00164     bool DeleteDeadVarargs(Function &Fn);
00165     bool RemoveDeadArgumentsFromCallers(Function &Fn);
00166   };
00167 }
00168 
00169 
00170 char DAE::ID = 0;
00171 INITIALIZE_PASS(DAE, "deadargelim", "Dead Argument Elimination", false, false)
00172 
00173 namespace {
00174   /// DAH - DeadArgumentHacking pass - Same as dead argument elimination, but
00175   /// deletes arguments to functions which are external.  This is only for use
00176   /// by bugpoint.
00177   struct DAH : public DAE {
00178     static char ID;
00179     DAH() : DAE(ID) {}
00180 
00181     virtual bool ShouldHackArguments() const { return true; }
00182   };
00183 }
00184 
00185 char DAH::ID = 0;
00186 INITIALIZE_PASS(DAH, "deadarghaX0r", 
00187                 "Dead Argument Hacking (BUGPOINT USE ONLY; DO NOT USE)",
00188                 false, false)
00189 
00190 /// createDeadArgEliminationPass - This pass removes arguments from functions
00191 /// which are not used by the body of the function.
00192 ///
00193 ModulePass *llvm::createDeadArgEliminationPass() { return new DAE(); }
00194 ModulePass *llvm::createDeadArgHackingPass() { return new DAH(); }
00195 
00196 /// CollectFunctionDIs - Map each function in the module to its debug info
00197 /// descriptor.
00198 void DAE::CollectFunctionDIs(Module &M) {
00199   FunctionDIs.clear();
00200 
00201   for (Module::named_metadata_iterator I = M.named_metadata_begin(),
00202        E = M.named_metadata_end(); I != E; ++I) {
00203     NamedMDNode &NMD = *I;
00204     for (unsigned MDIndex = 0, MDNum = NMD.getNumOperands();
00205          MDIndex < MDNum; ++MDIndex) {
00206       MDNode *Node = NMD.getOperand(MDIndex);
00207       if (!DIDescriptor(Node).isCompileUnit())
00208         continue;
00209       DICompileUnit CU(Node);
00210       const DIArray &SPs = CU.getSubprograms();
00211       for (unsigned SPIndex = 0, SPNum = SPs.getNumElements();
00212            SPIndex < SPNum; ++SPIndex) {
00213         DISubprogram SP(SPs.getElement(SPIndex));
00214         if (!SP.Verify())
00215           continue;
00216         if (Function *F = SP.getFunction())
00217           FunctionDIs[F] = SP;
00218       }
00219     }
00220   }
00221 }
00222 
00223 /// DeleteDeadVarargs - If this is an function that takes a ... list, and if
00224 /// llvm.vastart is never called, the varargs list is dead for the function.
00225 bool DAE::DeleteDeadVarargs(Function &Fn) {
00226   assert(Fn.getFunctionType()->isVarArg() && "Function isn't varargs!");
00227   if (Fn.isDeclaration() || !Fn.hasLocalLinkage()) return false;
00228 
00229   // Ensure that the function is only directly called.
00230   if (Fn.hasAddressTaken())
00231     return false;
00232 
00233   // Okay, we know we can transform this function if safe.  Scan its body
00234   // looking for calls to llvm.vastart.
00235   for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
00236     for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
00237       if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
00238         if (II->getIntrinsicID() == Intrinsic::vastart)
00239           return false;
00240       }
00241     }
00242   }
00243 
00244   // If we get here, there are no calls to llvm.vastart in the function body,
00245   // remove the "..." and adjust all the calls.
00246 
00247   // Start by computing a new prototype for the function, which is the same as
00248   // the old function, but doesn't have isVarArg set.
00249   FunctionType *FTy = Fn.getFunctionType();
00250 
00251   std::vector<Type*> Params(FTy->param_begin(), FTy->param_end());
00252   FunctionType *NFTy = FunctionType::get(FTy->getReturnType(),
00253                                                 Params, false);
00254   unsigned NumArgs = Params.size();
00255 
00256   // Create the new function body and insert it into the module...
00257   Function *NF = Function::Create(NFTy, Fn.getLinkage());
00258   NF->copyAttributesFrom(&Fn);
00259   Fn.getParent()->getFunctionList().insert(&Fn, NF);
00260   NF->takeName(&Fn);
00261 
00262   // Loop over all of the callers of the function, transforming the call sites
00263   // to pass in a smaller number of arguments into the new function.
00264   //
00265   std::vector<Value*> Args;
00266   while (!Fn.use_empty()) {
00267     CallSite CS(Fn.use_back());
00268     Instruction *Call = CS.getInstruction();
00269 
00270     // Pass all the same arguments.
00271     Args.assign(CS.arg_begin(), CS.arg_begin() + NumArgs);
00272 
00273     // Drop any attributes that were on the vararg arguments.
00274     AttributeSet PAL = CS.getAttributes();
00275     if (!PAL.isEmpty() && PAL.getSlotIndex(PAL.getNumSlots() - 1) > NumArgs) {
00276       SmallVector<AttributeSet, 8> AttributesVec;
00277       for (unsigned i = 0; PAL.getSlotIndex(i) <= NumArgs; ++i)
00278         AttributesVec.push_back(PAL.getSlotAttributes(i));
00279       if (PAL.hasAttributes(AttributeSet::FunctionIndex))
00280         AttributesVec.push_back(AttributeSet::get(Fn.getContext(),
00281                                                   PAL.getFnAttributes()));
00282       PAL = AttributeSet::get(Fn.getContext(), AttributesVec);
00283     }
00284 
00285     Instruction *New;
00286     if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
00287       New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),
00288                                Args, "", Call);
00289       cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
00290       cast<InvokeInst>(New)->setAttributes(PAL);
00291     } else {
00292       New = CallInst::Create(NF, Args, "", Call);
00293       cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
00294       cast<CallInst>(New)->setAttributes(PAL);
00295       if (cast<CallInst>(Call)->isTailCall())
00296         cast<CallInst>(New)->setTailCall();
00297     }
00298     New->setDebugLoc(Call->getDebugLoc());
00299 
00300     Args.clear();
00301 
00302     if (!Call->use_empty())
00303       Call->replaceAllUsesWith(New);
00304 
00305     New->takeName(Call);
00306 
00307     // Finally, remove the old call from the program, reducing the use-count of
00308     // F.
00309     Call->eraseFromParent();
00310   }
00311 
00312   // Since we have now created the new function, splice the body of the old
00313   // function right into the new function, leaving the old rotting hulk of the
00314   // function empty.
00315   NF->getBasicBlockList().splice(NF->begin(), Fn.getBasicBlockList());
00316 
00317   // Loop over the argument list, transferring uses of the old arguments over to
00318   // the new arguments, also transferring over the names as well.  While we're at
00319   // it, remove the dead arguments from the DeadArguments list.
00320   //
00321   for (Function::arg_iterator I = Fn.arg_begin(), E = Fn.arg_end(),
00322        I2 = NF->arg_begin(); I != E; ++I, ++I2) {
00323     // Move the name and users over to the new version.
00324     I->replaceAllUsesWith(I2);
00325     I2->takeName(I);
00326   }
00327 
00328   // Patch the pointer to LLVM function in debug info descriptor.
00329   FunctionDIMap::iterator DI = FunctionDIs.find(&Fn);
00330   if (DI != FunctionDIs.end())
00331     DI->second.replaceFunction(NF);
00332 
00333   // Finally, nuke the old function.
00334   Fn.eraseFromParent();
00335   return true;
00336 }
00337 
00338 /// RemoveDeadArgumentsFromCallers - Checks if the given function has any 
00339 /// arguments that are unused, and changes the caller parameters to be undefined
00340 /// instead.
00341 bool DAE::RemoveDeadArgumentsFromCallers(Function &Fn)
00342 {
00343   if (Fn.isDeclaration() || Fn.mayBeOverridden())
00344     return false;
00345 
00346   // Functions with local linkage should already have been handled.
00347   if (Fn.hasLocalLinkage())
00348     return false;
00349 
00350   if (Fn.use_empty())
00351     return false;
00352 
00353   SmallVector<unsigned, 8> UnusedArgs;
00354   for (Function::arg_iterator I = Fn.arg_begin(), E = Fn.arg_end(); 
00355        I != E; ++I) {
00356     Argument *Arg = I;
00357 
00358     if (Arg->use_empty() && !Arg->hasByValAttr())
00359       UnusedArgs.push_back(Arg->getArgNo());
00360   }
00361 
00362   if (UnusedArgs.empty())
00363     return false;
00364 
00365   bool Changed = false;
00366 
00367   for (Function::use_iterator I = Fn.use_begin(), E = Fn.use_end(); 
00368        I != E; ++I) {
00369     CallSite CS(*I);
00370     if (!CS || !CS.isCallee(I))
00371       continue;
00372 
00373     // Now go through all unused args and replace them with "undef".
00374     for (unsigned I = 0, E = UnusedArgs.size(); I != E; ++I) {
00375       unsigned ArgNo = UnusedArgs[I];
00376 
00377       Value *Arg = CS.getArgument(ArgNo);
00378       CS.setArgument(ArgNo, UndefValue::get(Arg->getType()));
00379       ++NumArgumentsReplacedWithUndef;
00380       Changed = true;
00381     }
00382   }
00383 
00384   return Changed;
00385 }
00386 
00387 /// Convenience function that returns the number of return values. It returns 0
00388 /// for void functions and 1 for functions not returning a struct. It returns
00389 /// the number of struct elements for functions returning a struct.
00390 static unsigned NumRetVals(const Function *F) {
00391   if (F->getReturnType()->isVoidTy())
00392     return 0;
00393   else if (StructType *STy = dyn_cast<StructType>(F->getReturnType()))
00394     return STy->getNumElements();
00395   else
00396     return 1;
00397 }
00398 
00399 /// MarkIfNotLive - This checks Use for liveness in LiveValues. If Use is not
00400 /// live, it adds Use to the MaybeLiveUses argument. Returns the determined
00401 /// liveness of Use.
00402 DAE::Liveness DAE::MarkIfNotLive(RetOrArg Use, UseVector &MaybeLiveUses) {
00403   // We're live if our use or its Function is already marked as live.
00404   if (LiveFunctions.count(Use.F) || LiveValues.count(Use))
00405     return Live;
00406 
00407   // We're maybe live otherwise, but remember that we must become live if
00408   // Use becomes live.
00409   MaybeLiveUses.push_back(Use);
00410   return MaybeLive;
00411 }
00412 
00413 
00414 /// SurveyUse - This looks at a single use of an argument or return value
00415 /// and determines if it should be alive or not. Adds this use to MaybeLiveUses
00416 /// if it causes the used value to become MaybeLive.
00417 ///
00418 /// RetValNum is the return value number to use when this use is used in a
00419 /// return instruction. This is used in the recursion, you should always leave
00420 /// it at 0.
00421 DAE::Liveness DAE::SurveyUse(Value::const_use_iterator U,
00422                              UseVector &MaybeLiveUses, unsigned RetValNum) {
00423     const User *V = *U;
00424     if (const ReturnInst *RI = dyn_cast<ReturnInst>(V)) {
00425       // The value is returned from a function. It's only live when the
00426       // function's return value is live. We use RetValNum here, for the case
00427       // that U is really a use of an insertvalue instruction that uses the
00428       // original Use.
00429       RetOrArg Use = CreateRet(RI->getParent()->getParent(), RetValNum);
00430       // We might be live, depending on the liveness of Use.
00431       return MarkIfNotLive(Use, MaybeLiveUses);
00432     }
00433     if (const InsertValueInst *IV = dyn_cast<InsertValueInst>(V)) {
00434       if (U.getOperandNo() != InsertValueInst::getAggregateOperandIndex()
00435           && IV->hasIndices())
00436         // The use we are examining is inserted into an aggregate. Our liveness
00437         // depends on all uses of that aggregate, but if it is used as a return
00438         // value, only index at which we were inserted counts.
00439         RetValNum = *IV->idx_begin();
00440 
00441       // Note that if we are used as the aggregate operand to the insertvalue,
00442       // we don't change RetValNum, but do survey all our uses.
00443 
00444       Liveness Result = MaybeLive;
00445       for (Value::const_use_iterator I = IV->use_begin(),
00446            E = V->use_end(); I != E; ++I) {
00447         Result = SurveyUse(I, MaybeLiveUses, RetValNum);
00448         if (Result == Live)
00449           break;
00450       }
00451       return Result;
00452     }
00453 
00454     if (ImmutableCallSite CS = V) {
00455       const Function *F = CS.getCalledFunction();
00456       if (F) {
00457         // Used in a direct call.
00458 
00459         // Find the argument number. We know for sure that this use is an
00460         // argument, since if it was the function argument this would be an
00461         // indirect call and the we know can't be looking at a value of the
00462         // label type (for the invoke instruction).
00463         unsigned ArgNo = CS.getArgumentNo(U);
00464 
00465         if (ArgNo >= F->getFunctionType()->getNumParams())
00466           // The value is passed in through a vararg! Must be live.
00467           return Live;
00468 
00469         assert(CS.getArgument(ArgNo)
00470                == CS->getOperand(U.getOperandNo())
00471                && "Argument is not where we expected it");
00472 
00473         // Value passed to a normal call. It's only live when the corresponding
00474         // argument to the called function turns out live.
00475         RetOrArg Use = CreateArg(F, ArgNo);
00476         return MarkIfNotLive(Use, MaybeLiveUses);
00477       }
00478     }
00479     // Used in any other way? Value must be live.
00480     return Live;
00481 }
00482 
00483 /// SurveyUses - This looks at all the uses of the given value
00484 /// Returns the Liveness deduced from the uses of this value.
00485 ///
00486 /// Adds all uses that cause the result to be MaybeLive to MaybeLiveRetUses. If
00487 /// the result is Live, MaybeLiveUses might be modified but its content should
00488 /// be ignored (since it might not be complete).
00489 DAE::Liveness DAE::SurveyUses(const Value *V, UseVector &MaybeLiveUses) {
00490   // Assume it's dead (which will only hold if there are no uses at all..).
00491   Liveness Result = MaybeLive;
00492   // Check each use.
00493   for (Value::const_use_iterator I = V->use_begin(),
00494        E = V->use_end(); I != E; ++I) {
00495     Result = SurveyUse(I, MaybeLiveUses);
00496     if (Result == Live)
00497       break;
00498   }
00499   return Result;
00500 }
00501 
00502 // SurveyFunction - This performs the initial survey of the specified function,
00503 // checking out whether or not it uses any of its incoming arguments or whether
00504 // any callers use the return value.  This fills in the LiveValues set and Uses
00505 // map.
00506 //
00507 // We consider arguments of non-internal functions to be intrinsically alive as
00508 // well as arguments to functions which have their "address taken".
00509 //
00510 void DAE::SurveyFunction(const Function &F) {
00511   unsigned RetCount = NumRetVals(&F);
00512   // Assume all return values are dead
00513   typedef SmallVector<Liveness, 5> RetVals;
00514   RetVals RetValLiveness(RetCount, MaybeLive);
00515 
00516   typedef SmallVector<UseVector, 5> RetUses;
00517   // These vectors map each return value to the uses that make it MaybeLive, so
00518   // we can add those to the Uses map if the return value really turns out to be
00519   // MaybeLive. Initialized to a list of RetCount empty lists.
00520   RetUses MaybeLiveRetUses(RetCount);
00521 
00522   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
00523     if (const ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()))
00524       if (RI->getNumOperands() != 0 && RI->getOperand(0)->getType()
00525           != F.getFunctionType()->getReturnType()) {
00526         // We don't support old style multiple return values.
00527         MarkLive(F);
00528         return;
00529       }
00530 
00531   if (!F.hasLocalLinkage() && (!ShouldHackArguments() || F.isIntrinsic())) {
00532     MarkLive(F);
00533     return;
00534   }
00535 
00536   DEBUG(dbgs() << "DAE - Inspecting callers for fn: " << F.getName() << "\n");
00537   // Keep track of the number of live retvals, so we can skip checks once all
00538   // of them turn out to be live.
00539   unsigned NumLiveRetVals = 0;
00540   Type *STy = dyn_cast<StructType>(F.getReturnType());
00541   // Loop all uses of the function.
00542   for (Value::const_use_iterator I = F.use_begin(), E = F.use_end();
00543        I != E; ++I) {
00544     // If the function is PASSED IN as an argument, its address has been
00545     // taken.
00546     ImmutableCallSite CS(*I);
00547     if (!CS || !CS.isCallee(I)) {
00548       MarkLive(F);
00549       return;
00550     }
00551 
00552     // If this use is anything other than a call site, the function is alive.
00553     const Instruction *TheCall = CS.getInstruction();
00554     if (!TheCall) {   // Not a direct call site?
00555       MarkLive(F);
00556       return;
00557     }
00558 
00559     // If we end up here, we are looking at a direct call to our function.
00560 
00561     // Now, check how our return value(s) is/are used in this caller. Don't
00562     // bother checking return values if all of them are live already.
00563     if (NumLiveRetVals != RetCount) {
00564       if (STy) {
00565         // Check all uses of the return value.
00566         for (Value::const_use_iterator I = TheCall->use_begin(),
00567              E = TheCall->use_end(); I != E; ++I) {
00568           const ExtractValueInst *Ext = dyn_cast<ExtractValueInst>(*I);
00569           if (Ext && Ext->hasIndices()) {
00570             // This use uses a part of our return value, survey the uses of
00571             // that part and store the results for this index only.
00572             unsigned Idx = *Ext->idx_begin();
00573             if (RetValLiveness[Idx] != Live) {
00574               RetValLiveness[Idx] = SurveyUses(Ext, MaybeLiveRetUses[Idx]);
00575               if (RetValLiveness[Idx] == Live)
00576                 NumLiveRetVals++;
00577             }
00578           } else {
00579             // Used by something else than extractvalue. Mark all return
00580             // values as live.
00581             for (unsigned i = 0; i != RetCount; ++i )
00582               RetValLiveness[i] = Live;
00583             NumLiveRetVals = RetCount;
00584             break;
00585           }
00586         }
00587       } else {
00588         // Single return value
00589         RetValLiveness[0] = SurveyUses(TheCall, MaybeLiveRetUses[0]);
00590         if (RetValLiveness[0] == Live)
00591           NumLiveRetVals = RetCount;
00592       }
00593     }
00594   }
00595 
00596   // Now we've inspected all callers, record the liveness of our return values.
00597   for (unsigned i = 0; i != RetCount; ++i)
00598     MarkValue(CreateRet(&F, i), RetValLiveness[i], MaybeLiveRetUses[i]);
00599 
00600   DEBUG(dbgs() << "DAE - Inspecting args for fn: " << F.getName() << "\n");
00601 
00602   // Now, check all of our arguments.
00603   unsigned i = 0;
00604   UseVector MaybeLiveArgUses;
00605   for (Function::const_arg_iterator AI = F.arg_begin(),
00606        E = F.arg_end(); AI != E; ++AI, ++i) {
00607     // See what the effect of this use is (recording any uses that cause
00608     // MaybeLive in MaybeLiveArgUses).
00609     Liveness Result = SurveyUses(AI, MaybeLiveArgUses);
00610     // Mark the result.
00611     MarkValue(CreateArg(&F, i), Result, MaybeLiveArgUses);
00612     // Clear the vector again for the next iteration.
00613     MaybeLiveArgUses.clear();
00614   }
00615 }
00616 
00617 /// MarkValue - This function marks the liveness of RA depending on L. If L is
00618 /// MaybeLive, it also takes all uses in MaybeLiveUses and records them in Uses,
00619 /// such that RA will be marked live if any use in MaybeLiveUses gets marked
00620 /// live later on.
00621 void DAE::MarkValue(const RetOrArg &RA, Liveness L,
00622                     const UseVector &MaybeLiveUses) {
00623   switch (L) {
00624     case Live: MarkLive(RA); break;
00625     case MaybeLive:
00626     {
00627       // Note any uses of this value, so this return value can be
00628       // marked live whenever one of the uses becomes live.
00629       for (UseVector::const_iterator UI = MaybeLiveUses.begin(),
00630            UE = MaybeLiveUses.end(); UI != UE; ++UI)
00631         Uses.insert(std::make_pair(*UI, RA));
00632       break;
00633     }
00634   }
00635 }
00636 
00637 /// MarkLive - Mark the given Function as alive, meaning that it cannot be
00638 /// changed in any way. Additionally,
00639 /// mark any values that are used as this function's parameters or by its return
00640 /// values (according to Uses) live as well.
00641 void DAE::MarkLive(const Function &F) {
00642   DEBUG(dbgs() << "DAE - Intrinsically live fn: " << F.getName() << "\n");
00643   // Mark the function as live.
00644   LiveFunctions.insert(&F);
00645   // Mark all arguments as live.
00646   for (unsigned i = 0, e = F.arg_size(); i != e; ++i)
00647     PropagateLiveness(CreateArg(&F, i));
00648   // Mark all return values as live.
00649   for (unsigned i = 0, e = NumRetVals(&F); i != e; ++i)
00650     PropagateLiveness(CreateRet(&F, i));
00651 }
00652 
00653 /// MarkLive - Mark the given return value or argument as live. Additionally,
00654 /// mark any values that are used by this value (according to Uses) live as
00655 /// well.
00656 void DAE::MarkLive(const RetOrArg &RA) {
00657   if (LiveFunctions.count(RA.F))
00658     return; // Function was already marked Live.
00659 
00660   if (!LiveValues.insert(RA).second)
00661     return; // We were already marked Live.
00662 
00663   DEBUG(dbgs() << "DAE - Marking " << RA.getDescription() << " live\n");
00664   PropagateLiveness(RA);
00665 }
00666 
00667 /// PropagateLiveness - Given that RA is a live value, propagate it's liveness
00668 /// to any other values it uses (according to Uses).
00669 void DAE::PropagateLiveness(const RetOrArg &RA) {
00670   // We don't use upper_bound (or equal_range) here, because our recursive call
00671   // to ourselves is likely to cause the upper_bound (which is the first value
00672   // not belonging to RA) to become erased and the iterator invalidated.
00673   UseMap::iterator Begin = Uses.lower_bound(RA);
00674   UseMap::iterator E = Uses.end();
00675   UseMap::iterator I;
00676   for (I = Begin; I != E && I->first == RA; ++I)
00677     MarkLive(I->second);
00678 
00679   // Erase RA from the Uses map (from the lower bound to wherever we ended up
00680   // after the loop).
00681   Uses.erase(Begin, I);
00682 }
00683 
00684 // RemoveDeadStuffFromFunction - Remove any arguments and return values from F
00685 // that are not in LiveValues. Transform the function and all of the callees of
00686 // the function to not have these arguments and return values.
00687 //
00688 bool DAE::RemoveDeadStuffFromFunction(Function *F) {
00689   // Don't modify fully live functions
00690   if (LiveFunctions.count(F))
00691     return false;
00692 
00693   // Start by computing a new prototype for the function, which is the same as
00694   // the old function, but has fewer arguments and a different return type.
00695   FunctionType *FTy = F->getFunctionType();
00696   std::vector<Type*> Params;
00697 
00698   // Set up to build a new list of parameter attributes.
00699   SmallVector<AttributeSet, 8> AttributesVec;
00700   const AttributeSet &PAL = F->getAttributes();
00701 
00702   // Find out the new return value.
00703   Type *RetTy = FTy->getReturnType();
00704   Type *NRetTy = NULL;
00705   unsigned RetCount = NumRetVals(F);
00706 
00707   // -1 means unused, other numbers are the new index
00708   SmallVector<int, 5> NewRetIdxs(RetCount, -1);
00709   std::vector<Type*> RetTypes;
00710   if (RetTy->isVoidTy()) {
00711     NRetTy = RetTy;
00712   } else {
00713     StructType *STy = dyn_cast<StructType>(RetTy);
00714     if (STy)
00715       // Look at each of the original return values individually.
00716       for (unsigned i = 0; i != RetCount; ++i) {
00717         RetOrArg Ret = CreateRet(F, i);
00718         if (LiveValues.erase(Ret)) {
00719           RetTypes.push_back(STy->getElementType(i));
00720           NewRetIdxs[i] = RetTypes.size() - 1;
00721         } else {
00722           ++NumRetValsEliminated;
00723           DEBUG(dbgs() << "DAE - Removing return value " << i << " from "
00724                 << F->getName() << "\n");
00725         }
00726       }
00727     else
00728       // We used to return a single value.
00729       if (LiveValues.erase(CreateRet(F, 0))) {
00730         RetTypes.push_back(RetTy);
00731         NewRetIdxs[0] = 0;
00732       } else {
00733         DEBUG(dbgs() << "DAE - Removing return value from " << F->getName()
00734               << "\n");
00735         ++NumRetValsEliminated;
00736       }
00737     if (RetTypes.size() > 1)
00738       // More than one return type? Return a struct with them. Also, if we used
00739       // to return a struct and didn't change the number of return values,
00740       // return a struct again. This prevents changing {something} into
00741       // something and {} into void.
00742       // Make the new struct packed if we used to return a packed struct
00743       // already.
00744       NRetTy = StructType::get(STy->getContext(), RetTypes, STy->isPacked());
00745     else if (RetTypes.size() == 1)
00746       // One return type? Just a simple value then, but only if we didn't use to
00747       // return a struct with that simple value before.
00748       NRetTy = RetTypes.front();
00749     else if (RetTypes.size() == 0)
00750       // No return types? Make it void, but only if we didn't use to return {}.
00751       NRetTy = Type::getVoidTy(F->getContext());
00752   }
00753 
00754   assert(NRetTy && "No new return type found?");
00755 
00756   // The existing function return attributes.
00757   AttributeSet RAttrs = PAL.getRetAttributes();
00758 
00759   // Remove any incompatible attributes, but only if we removed all return
00760   // values. Otherwise, ensure that we don't have any conflicting attributes
00761   // here. Currently, this should not be possible, but special handling might be
00762   // required when new return value attributes are added.
00763   if (NRetTy->isVoidTy())
00764     RAttrs =
00765       AttributeSet::get(NRetTy->getContext(), AttributeSet::ReturnIndex,
00766                         AttrBuilder(RAttrs, AttributeSet::ReturnIndex).
00767          removeAttributes(AttributeFuncs::
00768                           typeIncompatible(NRetTy, AttributeSet::ReturnIndex),
00769                           AttributeSet::ReturnIndex));
00770   else
00771     assert(!AttrBuilder(RAttrs, AttributeSet::ReturnIndex).
00772              hasAttributes(AttributeFuncs::
00773                            typeIncompatible(NRetTy, AttributeSet::ReturnIndex),
00774                            AttributeSet::ReturnIndex) &&
00775            "Return attributes no longer compatible?");
00776 
00777   if (RAttrs.hasAttributes(AttributeSet::ReturnIndex))
00778     AttributesVec.push_back(AttributeSet::get(NRetTy->getContext(), RAttrs));
00779 
00780   // Remember which arguments are still alive.
00781   SmallVector<bool, 10> ArgAlive(FTy->getNumParams(), false);
00782   // Construct the new parameter list from non-dead arguments. Also construct
00783   // a new set of parameter attributes to correspond. Skip the first parameter
00784   // attribute, since that belongs to the return value.
00785   unsigned i = 0;
00786   for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
00787        I != E; ++I, ++i) {
00788     RetOrArg Arg = CreateArg(F, i);
00789     if (LiveValues.erase(Arg)) {
00790       Params.push_back(I->getType());
00791       ArgAlive[i] = true;
00792 
00793       // Get the original parameter attributes (skipping the first one, that is
00794       // for the return value.
00795       if (PAL.hasAttributes(i + 1)) {
00796         AttrBuilder B(PAL, i + 1);
00797         AttributesVec.
00798           push_back(AttributeSet::get(F->getContext(), Params.size(), B));
00799       }
00800     } else {
00801       ++NumArgumentsEliminated;
00802       DEBUG(dbgs() << "DAE - Removing argument " << i << " (" << I->getName()
00803             << ") from " << F->getName() << "\n");
00804     }
00805   }
00806 
00807   if (PAL.hasAttributes(AttributeSet::FunctionIndex))
00808     AttributesVec.push_back(AttributeSet::get(F->getContext(),
00809                                               PAL.getFnAttributes()));
00810 
00811   // Reconstruct the AttributesList based on the vector we constructed.
00812   AttributeSet NewPAL = AttributeSet::get(F->getContext(), AttributesVec);
00813 
00814   // Create the new function type based on the recomputed parameters.
00815   FunctionType *NFTy = FunctionType::get(NRetTy, Params, FTy->isVarArg());
00816 
00817   // No change?
00818   if (NFTy == FTy)
00819     return false;
00820 
00821   // Create the new function body and insert it into the module...
00822   Function *NF = Function::Create(NFTy, F->getLinkage());
00823   NF->copyAttributesFrom(F);
00824   NF->setAttributes(NewPAL);
00825   // Insert the new function before the old function, so we won't be processing
00826   // it again.
00827   F->getParent()->getFunctionList().insert(F, NF);
00828   NF->takeName(F);
00829 
00830   // Loop over all of the callers of the function, transforming the call sites
00831   // to pass in a smaller number of arguments into the new function.
00832   //
00833   std::vector<Value*> Args;
00834   while (!F->use_empty()) {
00835     CallSite CS(F->use_back());
00836     Instruction *Call = CS.getInstruction();
00837 
00838     AttributesVec.clear();
00839     const AttributeSet &CallPAL = CS.getAttributes();
00840 
00841     // The call return attributes.
00842     AttributeSet RAttrs = CallPAL.getRetAttributes();
00843 
00844     // Adjust in case the function was changed to return void.
00845     RAttrs =
00846       AttributeSet::get(NF->getContext(), AttributeSet::ReturnIndex,
00847                         AttrBuilder(RAttrs, AttributeSet::ReturnIndex).
00848         removeAttributes(AttributeFuncs::
00849                          typeIncompatible(NF->getReturnType(),
00850                                           AttributeSet::ReturnIndex),
00851                          AttributeSet::ReturnIndex));
00852     if (RAttrs.hasAttributes(AttributeSet::ReturnIndex))
00853       AttributesVec.push_back(AttributeSet::get(NF->getContext(), RAttrs));
00854 
00855     // Declare these outside of the loops, so we can reuse them for the second
00856     // loop, which loops the varargs.
00857     CallSite::arg_iterator I = CS.arg_begin();
00858     unsigned i = 0;
00859     // Loop over those operands, corresponding to the normal arguments to the
00860     // original function, and add those that are still alive.
00861     for (unsigned e = FTy->getNumParams(); i != e; ++I, ++i)
00862       if (ArgAlive[i]) {
00863         Args.push_back(*I);
00864         // Get original parameter attributes, but skip return attributes.
00865         if (CallPAL.hasAttributes(i + 1)) {
00866           AttrBuilder B(CallPAL, i + 1);
00867           AttributesVec.
00868             push_back(AttributeSet::get(F->getContext(), Args.size(), B));
00869         }
00870       }
00871 
00872     // Push any varargs arguments on the list. Don't forget their attributes.
00873     for (CallSite::arg_iterator E = CS.arg_end(); I != E; ++I, ++i) {
00874       Args.push_back(*I);
00875       if (CallPAL.hasAttributes(i + 1)) {
00876         AttrBuilder B(CallPAL, i + 1);
00877         AttributesVec.
00878           push_back(AttributeSet::get(F->getContext(), Args.size(), B));
00879       }
00880     }
00881 
00882     if (CallPAL.hasAttributes(AttributeSet::FunctionIndex))
00883       AttributesVec.push_back(AttributeSet::get(Call->getContext(),
00884                                                 CallPAL.getFnAttributes()));
00885 
00886     // Reconstruct the AttributesList based on the vector we constructed.
00887     AttributeSet NewCallPAL = AttributeSet::get(F->getContext(), AttributesVec);
00888 
00889     Instruction *New;
00890     if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
00891       New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),
00892                                Args, "", Call);
00893       cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
00894       cast<InvokeInst>(New)->setAttributes(NewCallPAL);
00895     } else {
00896       New = CallInst::Create(NF, Args, "", Call);
00897       cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
00898       cast<CallInst>(New)->setAttributes(NewCallPAL);
00899       if (cast<CallInst>(Call)->isTailCall())
00900         cast<CallInst>(New)->setTailCall();
00901     }
00902     New->setDebugLoc(Call->getDebugLoc());
00903 
00904     Args.clear();
00905 
00906     if (!Call->use_empty()) {
00907       if (New->getType() == Call->getType()) {
00908         // Return type not changed? Just replace users then.
00909         Call->replaceAllUsesWith(New);
00910         New->takeName(Call);
00911       } else if (New->getType()->isVoidTy()) {
00912         // Our return value has uses, but they will get removed later on.
00913         // Replace by null for now.
00914         if (!Call->getType()->isX86_MMXTy())
00915           Call->replaceAllUsesWith(Constant::getNullValue(Call->getType()));
00916       } else {
00917         assert(RetTy->isStructTy() &&
00918                "Return type changed, but not into a void. The old return type"
00919                " must have been a struct!");
00920         Instruction *InsertPt = Call;
00921         if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
00922           BasicBlock::iterator IP = II->getNormalDest()->begin();
00923           while (isa<PHINode>(IP)) ++IP;
00924           InsertPt = IP;
00925         }
00926 
00927         // We used to return a struct. Instead of doing smart stuff with all the
00928         // uses of this struct, we will just rebuild it using
00929         // extract/insertvalue chaining and let instcombine clean that up.
00930         //
00931         // Start out building up our return value from undef
00932         Value *RetVal = UndefValue::get(RetTy);
00933         for (unsigned i = 0; i != RetCount; ++i)
00934           if (NewRetIdxs[i] != -1) {
00935             Value *V;
00936             if (RetTypes.size() > 1)
00937               // We are still returning a struct, so extract the value from our
00938               // return value
00939               V = ExtractValueInst::Create(New, NewRetIdxs[i], "newret",
00940                                            InsertPt);
00941             else
00942               // We are now returning a single element, so just insert that
00943               V = New;
00944             // Insert the value at the old position
00945             RetVal = InsertValueInst::Create(RetVal, V, i, "oldret", InsertPt);
00946           }
00947         // Now, replace all uses of the old call instruction with the return
00948         // struct we built
00949         Call->replaceAllUsesWith(RetVal);
00950         New->takeName(Call);
00951       }
00952     }
00953 
00954     // Finally, remove the old call from the program, reducing the use-count of
00955     // F.
00956     Call->eraseFromParent();
00957   }
00958 
00959   // Since we have now created the new function, splice the body of the old
00960   // function right into the new function, leaving the old rotting hulk of the
00961   // function empty.
00962   NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());
00963 
00964   // Loop over the argument list, transferring uses of the old arguments over to
00965   // the new arguments, also transferring over the names as well.
00966   i = 0;
00967   for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(),
00968        I2 = NF->arg_begin(); I != E; ++I, ++i)
00969     if (ArgAlive[i]) {
00970       // If this is a live argument, move the name and users over to the new
00971       // version.
00972       I->replaceAllUsesWith(I2);
00973       I2->takeName(I);
00974       ++I2;
00975     } else {
00976       // If this argument is dead, replace any uses of it with null constants
00977       // (these are guaranteed to become unused later on).
00978       if (!I->getType()->isX86_MMXTy())
00979         I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
00980     }
00981 
00982   // If we change the return value of the function we must rewrite any return
00983   // instructions.  Check this now.
00984   if (F->getReturnType() != NF->getReturnType())
00985     for (Function::iterator BB = NF->begin(), E = NF->end(); BB != E; ++BB)
00986       if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
00987         Value *RetVal;
00988 
00989         if (NFTy->getReturnType()->isVoidTy()) {
00990           RetVal = 0;
00991         } else {
00992           assert (RetTy->isStructTy());
00993           // The original return value was a struct, insert
00994           // extractvalue/insertvalue chains to extract only the values we need
00995           // to return and insert them into our new result.
00996           // This does generate messy code, but we'll let it to instcombine to
00997           // clean that up.
00998           Value *OldRet = RI->getOperand(0);
00999           // Start out building up our return value from undef
01000           RetVal = UndefValue::get(NRetTy);
01001           for (unsigned i = 0; i != RetCount; ++i)
01002             if (NewRetIdxs[i] != -1) {
01003               ExtractValueInst *EV = ExtractValueInst::Create(OldRet, i,
01004                                                               "oldret", RI);
01005               if (RetTypes.size() > 1) {
01006                 // We're still returning a struct, so reinsert the value into
01007                 // our new return value at the new index
01008 
01009                 RetVal = InsertValueInst::Create(RetVal, EV, NewRetIdxs[i],
01010                                                  "newret", RI);
01011               } else {
01012                 // We are now only returning a simple value, so just return the
01013                 // extracted value.
01014                 RetVal = EV;
01015               }
01016             }
01017         }
01018         // Replace the return instruction with one returning the new return
01019         // value (possibly 0 if we became void).
01020         ReturnInst::Create(F->getContext(), RetVal, RI);
01021         BB->getInstList().erase(RI);
01022       }
01023 
01024   // Patch the pointer to LLVM function in debug info descriptor.
01025   FunctionDIMap::iterator DI = FunctionDIs.find(F);
01026   if (DI != FunctionDIs.end())
01027     DI->second.replaceFunction(NF);
01028 
01029   // Now that the old function is dead, delete it.
01030   F->eraseFromParent();
01031 
01032   return true;
01033 }
01034 
01035 bool DAE::runOnModule(Module &M) {
01036   bool Changed = false;
01037 
01038   // Collect debug info descriptors for functions.
01039   CollectFunctionDIs(M);
01040 
01041   // First pass: Do a simple check to see if any functions can have their "..."
01042   // removed.  We can do this if they never call va_start.  This loop cannot be
01043   // fused with the next loop, because deleting a function invalidates
01044   // information computed while surveying other functions.
01045   DEBUG(dbgs() << "DAE - Deleting dead varargs\n");
01046   for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
01047     Function &F = *I++;
01048     if (F.getFunctionType()->isVarArg())
01049       Changed |= DeleteDeadVarargs(F);
01050   }
01051 
01052   // Second phase:loop through the module, determining which arguments are live.
01053   // We assume all arguments are dead unless proven otherwise (allowing us to
01054   // determine that dead arguments passed into recursive functions are dead).
01055   //
01056   DEBUG(dbgs() << "DAE - Determining liveness\n");
01057   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
01058     SurveyFunction(*I);
01059 
01060   // Now, remove all dead arguments and return values from each function in
01061   // turn.
01062   for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
01063     // Increment now, because the function will probably get removed (ie.
01064     // replaced by a new one).
01065     Function *F = I++;
01066     Changed |= RemoveDeadStuffFromFunction(F);
01067   }
01068 
01069   // Finally, look for any unused parameters in functions with non-local
01070   // linkage and replace the passed in parameters with undef.
01071   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
01072     Function& F = *I;
01073 
01074     Changed |= RemoveDeadArgumentsFromCallers(F);
01075   }
01076 
01077   return Changed;
01078 }