<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hi Matthijs,<div><br></div><div>This breaks llvm-gcc bootstrapping. Looks like it miscompiles gcc driver:</div><div><br></div><div><span class="Apple-style-span" style="font-family: -webkit-monospace; font-size: 11px; "><blockquote type="cite">/Volumes/SandBox/NightlyTest/llvm-gcc.obj/./gcc/xgcc -B/Volumes/SandBox/NightlyTest/llvm-gcc.obj/./gcc/ -B/Volumes/SandBox/NightlyTest/llvm-gcc.install/powerpc-apple-darwin9.2.2/bin/ -B/Volumes/SandBox/NightlyTest/llvm-gcc.install/powerpc-apple-darwin9.2.2/lib/ -isystem /Volumes/SandBox/NightlyTest/llvm-gcc.install/powerpc-apple-darwin9.2.2/include -isystem /Volumes/SandBox/NightlyTest/llvm-gcc.install/powerpc-apple-darwin9.2.2/sys-include -O2 -g -O2 -DIN_GCC -W -Wall -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition -isystem ./include -I. -I. -I../../llvm-gcc.src/gcc -I../../llvm-gcc.src/gcc/. -I../../llvm-gcc.src/gcc/../include -I./../intl -I../../llvm-gcc.src/gcc/../libcpp/include -I../../llvm-gcc.src/gcc/../libdecnumber -I../libdecnumber -I/Volumes/SandBox/NightlyTest/llvm.obj/include -I/Volumes/SandBox/NightlyTest/llvm.src/include -mlongcall \<br></blockquote><blockquote type="cite"><span class="Apple-tab-span" style="white-space: pre; "> </span> -c ../../llvm-gcc.src/gcc/config/darwin-crt2.c -o crt2.o<br></blockquote><blockquote type="cite">xgcc: error trying to exec 'cc1': execvp: No such file or directory<br></blockquote><blockquote type="cite">make[3]: *** [crt2.o] Error 1<br></blockquote><blockquote type="cite">make[3]: *** Waiting for unfinished jobs....<br></blockquote><blockquote type="cite">rm fsf-funding.pod gcov.pod gfdl.pod cpp.pod gpl.pod gcc.pod<br></blockquote><blockquote type="cite">make[2]: *** [all-stage2-gcc] Error 2<br></blockquote><blockquote type="cite">make[1]: *** [stage2-bubble] Error 2<br></blockquote><blockquote type="cite">make: *** [all] Error 2</blockquote><div><br></div>I'll back it out for now. Please take a look. Thanks,</span></div><div><font class="Apple-style-span" face="-webkit-monospace" size="3"><span class="Apple-style-span" style="font-size: 11px;"><br></span></font></div><div><span class="Apple-style-span" style="font-family: -webkit-monospace; font-size: 11px; ">Evan</span></div><div><span class="Apple-style-span" style="font-family: -webkit-monospace; font-size: 11px; "><br></span><div><div>On Jun 20, 2008, at 2:36 AM, Matthijs Kooijman wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div>Author: matthijs<br>Date: Fri Jun 20 04:36:16 2008<br>New Revision: 52532<br><br>URL: <a href="http://llvm.org/viewvc/llvm-project?rev=52532&view=rev">http://llvm.org/viewvc/llvm-project?rev=52532&view=rev</a><br>Log:<br>Recommit r52459, rewriting of the dead argument elimination pass.<br><br>This is a fixed version that no longer uses multimap::equal_range, which<br>resulted in a pointer invalidation problem.<br><br>Also, DAE::InspectedFunctions was not really necessary, so it got removed.<br><br>Lastly, this version no longer applies the extra arg hack on functions who did<br>not have any arguments to start with.<br><br>Added:<br> llvm/trunk/test/Transforms/DeadArgElim/multdeadretval.ll<br> - copied unchanged from r52459, llvm/trunk/test/Transforms/DeadArgElim/multdeadretval.ll<br>Modified:<br> llvm/trunk/lib/Transforms/IPO/DeadArgumentElimination.cpp<br><br>Modified: llvm/trunk/lib/Transforms/IPO/DeadArgumentElimination.cpp<br>URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/DeadArgumentElimination.cpp?rev=52532&r1=52531&r2=52532&view=diff">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/DeadArgumentElimination.cpp?rev=52532&r1=52531&r2=52532&view=diff</a><br><br>==============================================================================<br>--- llvm/trunk/lib/Transforms/IPO/DeadArgumentElimination.cpp (original)<br>+++ llvm/trunk/lib/Transforms/IPO/DeadArgumentElimination.cpp Fri Jun 20 04:36:16 2008<br>@@ -10,10 +10,10 @@<br> // This pass deletes dead arguments from internal functions. Dead argument<br> // elimination removes arguments which are directly dead, as well as arguments<br> // only passed into function calls as dead arguments of other functions. This<br>-// pass also deletes dead arguments in a similar way.<br>+// pass also deletes dead return values in a similar way.<br> //<br> // This pass is often useful as a cleanup pass to run after aggressive<br>-// interprocedural passes, which add possibly-dead arguments.<br>+// interprocedural passes, which add possibly-dead arguments or return values.<br> //<br> //===----------------------------------------------------------------------===//<br><br>@@ -42,40 +42,66 @@<br> /// DAE - The dead argument elimination pass.<br> ///<br> class VISIBILITY_HIDDEN DAE : public ModulePass {<br>+ public:<br>+<br>+ /// Struct that represent either a (part of a) return value or a function<br>+ /// argument. Used so that arguments and return values can be used<br>+ /// interchangably.<br>+ struct RetOrArg {<br>+ RetOrArg(const Function* F, unsigned Idx, bool IsArg) : F(F), Idx(Idx), IsArg(IsArg) {}<br>+ const Function *F;<br>+ unsigned Idx;<br>+ bool IsArg;<br>+ <br>+ /// Make RetOrArg comparable, so we can put it into a map<br>+ bool operator<(const RetOrArg &O) const {<br>+ if (F != O.F)<br>+ return F < O.F;<br>+ else if (Idx != O.Idx)<br>+ return Idx < O.Idx;<br>+ else<br>+ return IsArg < O.IsArg;<br>+ }<br>+<br>+ /// Make RetOrArg comparable, so we can easily iterate the multimap<br>+ bool operator==(const RetOrArg &O) const {<br>+ return F == O.F && Idx == O.Idx && IsArg == O.IsArg;<br>+ }<br>+ };<br>+ <br> /// Liveness enum - During our initial pass over the program, we determine<br> /// that things are either definately alive, definately dead, or in need of<br> /// interprocedural analysis (MaybeLive).<br> ///<br> enum Liveness { Live, MaybeLive, Dead };<br><br>- /// LiveArguments, MaybeLiveArguments, DeadArguments - These sets contain<br>- /// all of the arguments in the program. The Dead set contains arguments<br>- /// which are completely dead (never used in the function). The MaybeLive<br>- /// set contains arguments which are only passed into other function calls,<br>- /// thus may be live and may be dead. The Live set contains arguments which<br>- /// are known to be alive.<br>- ///<br>- std::set<Argument*> DeadArguments, MaybeLiveArguments, LiveArguments;<br>-<br>- /// DeadRetVal, MaybeLiveRetVal, LifeRetVal - These sets contain all of the<br>- /// functions in the program. The Dead set contains functions whose return<br>- /// value is known to be dead. The MaybeLive set contains functions whose<br>- /// return values are only used by return instructions, and the Live set<br>- /// contains functions whose return values are used, functions that are<br>- /// external, and functions that already return void.<br>- ///<br>- std::set<Function*> DeadRetVal, MaybeLiveRetVal, LiveRetVal;<br>-<br>- /// InstructionsToInspect - As we mark arguments and return values<br>- /// MaybeLive, we keep track of which instructions could make the values<br>- /// live here. Once the entire program has had the return value and<br>- /// arguments analyzed, this set is scanned to promote the MaybeLive objects<br>- /// to be Live if they really are used.<br>- std::vector<Instruction*> InstructionsToInspect;<br>-<br>- /// CallSites - Keep track of the call sites of functions that have<br>- /// MaybeLive arguments or return values.<br>- std::multimap<Function*, CallSite> CallSites;<br>+ /// Convenience wrapper<br>+ RetOrArg CreateRet(const Function *F, unsigned Idx) { return RetOrArg(F, Idx, false); }<br>+ /// Convenience wrapper<br>+ RetOrArg CreateArg(const Function *F, unsigned Idx) { return RetOrArg(F, Idx, true); }<br>+<br>+ typedef std::multimap<RetOrArg, RetOrArg> UseMap;<br>+ /// This map maps a return value or argument to all return values or<br>+ /// arguments it uses. <br>+ /// For example (indices are left out for clarity):<br>+ /// - Uses[ret F] = ret G<br>+ /// This means that F calls G, and F returns the value returned by G.<br>+ /// - Uses[arg F] = ret G<br>+ /// This means that some function calls G and passes its result as an<br>+ /// argument to F.<br>+ /// - Uses[ret F] = arg F<br>+ /// This means that F returns one of its own arguments.<br>+ /// - Uses[arg F] = arg G<br>+ /// This means that G calls F and passes one of its own (G's) arguments<br>+ /// directly to F.<br>+ UseMap Uses;<br>+<br>+ typedef std::set<RetOrArg> LiveSet;<br>+<br>+ /// This set contains all values that have been determined to be live<br>+ LiveSet LiveValues;<br>+ <br>+ typedef SmallVector<RetOrArg, 5> UseVector;<br><br> public:<br> static char ID; // Pass identification, replacement for typeid<br>@@ -85,20 +111,19 @@<br> virtual bool ShouldHackArguments() const { return false; }<br><br> private:<br>- Liveness getArgumentLiveness(const Argument &A);<br>- bool isMaybeLiveArgumentNowLive(Argument *Arg);<br>-<br>+ Liveness IsMaybeLive(RetOrArg Use, UseVector &MaybeLiveUses);<br>+ Liveness SurveyUse(Value::use_iterator U, UseVector &MaybeLiveUses, unsigned RetValNum = 0);<br>+ Liveness SurveyUses(Value *V, UseVector &MaybeLiveUses);<br>+<br>+ void SurveyFunction(Function &F);<br>+ void MarkValue(const RetOrArg &RA, Liveness L, const UseVector &MaybeLiveUses);<br>+ void MarkLive(RetOrArg RA);<br>+ bool RemoveDeadStuffFromFunction(Function *F);<br> bool DeleteDeadVarargs(Function &Fn);<br>- void SurveyFunction(Function &Fn);<br>-<br>- void MarkArgumentLive(Argument *Arg);<br>- void MarkRetValLive(Function *F);<br>- void MarkReturnInstArgumentLive(ReturnInst *RI);<br>-<br>- void RemoveDeadArgumentsFromFunction(Function *F);<br> };<br> }<br><br>+<br> char DAE::ID = 0;<br> static RegisterPass<DAE><br> X("deadargelim", "Dead Argument Elimination");<br>@@ -155,7 +180,7 @@<br> // remove the "..." and adjust all the calls.<br><br> // Start by computing a new prototype for the function, which is the same as<br>- // the old function, but has fewer arguments.<br>+ // the old function, but doesn't have isVarArg set.<br> const FunctionType *FTy = Fn.getFunctionType();<br> std::vector<const Type*> Params(FTy->param_begin(), FTy->param_end());<br> FunctionType *NFTy = FunctionType::get(FTy->getReturnType(), Params, false);<br>@@ -233,57 +258,110 @@<br> return true;<br> }<br><br>+/// Convenience function that returns the number of return values. It returns 0<br>+/// for void functions and 1 for functions not returning a struct. It returns<br>+/// the number of struct elements for functions returning a struct.<br>+static unsigned NumRetVals(const Function *F) {<br>+ if (F->getReturnType() == Type::VoidTy)<br>+ return 0;<br>+ else if (const StructType *STy = dyn_cast<StructType>(F->getReturnType()))<br>+ return STy->getNumElements();<br>+ else<br>+ return 1;<br>+}<br><br>-static inline bool CallPassesValueThoughVararg(Instruction *Call,<br>- const Value *Arg) {<br>- CallSite CS = CallSite::get(Call);<br>- const Type *CalledValueTy = CS.getCalledValue()->getType();<br>- const Type *FTy = cast<PointerType>(CalledValueTy)->getElementType();<br>- unsigned NumFixedArgs = cast<FunctionType>(FTy)->getNumParams();<br>- for (CallSite::arg_iterator AI = CS.arg_begin()+NumFixedArgs;<br>- AI != CS.arg_end(); ++AI)<br>- if (AI->get() == Arg)<br>- return true;<br>- return false;<br>-}<br>-<br>-// getArgumentLiveness - Inspect an argument, determining if is known Live<br>-// (used in a computation), MaybeLive (only passed as an argument to a call), or<br>-// Dead (not used).<br>-DAE::Liveness DAE::getArgumentLiveness(const Argument &A) {<br>- const Function *F = A.getParent();<br>- <br>- // If this is the return value of a struct function, it's not really dead.<br>- if (F->hasStructRetAttr() && &*(F->arg_begin()) == &A)<br>+/// IsMaybeAlive - This checks Use for liveness. If Use is live, returns Live,<br>+/// else returns MaybeLive. Also, adds Use to MaybeLiveUses in the latter case.<br>+DAE::Liveness DAE::IsMaybeLive(RetOrArg Use, UseVector &MaybeLiveUses) {<br>+ // We're live if our use is already marked as live<br>+ if (LiveValues.count(Use))<br> return Live;<br>- <br>- if (A.use_empty()) // First check, directly dead?<br>- return Dead;<br><br>- // Scan through all of the uses, looking for non-argument passing uses.<br>- for (Value::use_const_iterator I = A.use_begin(), E = A.use_end(); I!=E;++I) {<br>- // Return instructions do not immediately effect liveness.<br>- if (isa<ReturnInst>(*I))<br>- continue;<br>-<br>- CallSite CS = CallSite::get(const_cast<User*>(*I));<br>- if (!CS.getInstruction()) {<br>- // If its used by something that is not a call or invoke, it's alive!<br>- return Live;<br>- }<br>- // If it's an indirect call, mark it alive...<br>- Function *Callee = CS.getCalledFunction();<br>- if (!Callee) return Live;<br>-<br>- // Check to see if it's passed through a va_arg area: if so, we cannot<br>- // remove it.<br>- if (CallPassesValueThoughVararg(CS.getInstruction(), &A))<br>- return Live; // If passed through va_arg area, we cannot remove it<br>- }<br>+ // We're maybe live otherwise, but remember that we must become live if<br>+ // Use becomes live.<br>+ MaybeLiveUses.push_back(Use);<br>+ return MaybeLive;<br>+}<br>+<br>+<br>+/// SurveyUse - This looks at a single use of an argument or return value<br>+/// and determines if it should be alive or not. Adds this use to MaybeLiveUses<br>+/// if it causes the used value to become MaybeAlive.<br>+///<br>+/// RetValNum is the return value number to use when this use is used in a<br>+/// return instruction. This is used in the recursion, you should always leave<br>+/// it at 0.<br>+DAE::Liveness DAE::SurveyUse(Value::use_iterator U, UseVector &MaybeLiveUses, unsigned RetValNum) {<br>+ Value *V = *U;<br>+ if (ReturnInst *RI = dyn_cast<ReturnInst>(V)) {<br>+ // The value is returned from another function. It's only live when the<br>+ // caller's return value is live<br>+ RetOrArg Use = CreateRet(RI->getParent()->getParent(), RetValNum);<br>+ // We might be live, depending on the liveness of Use<br>+ return IsMaybeLive(Use, MaybeLiveUses);<br>+ } <br>+ if (InsertValueInst *IV = dyn_cast<InsertValueInst>(V)) {<br>+ if (U.getOperandNo() != InsertValueInst::getAggregateOperandIndex() && IV->hasIndices())<br>+ // The use we are examining is inserted into an aggregate. Our liveness<br>+ // depends on all uses of that aggregate, but if it is used as a return<br>+ // value, only index at which we were inserted counts.<br>+ RetValNum = *IV->idx_begin();<br><br>- return MaybeLive; // It must be used, but only as argument to a function<br>+ // Note that if we are used as the aggregate operand to the insertvalue,<br>+ // we don't change RetValNum, but do survey all our uses.<br>+ <br>+ Liveness Result = Dead;<br>+ for (Value::use_iterator I = IV->use_begin(),<br>+ E = V->use_end(); I != E; ++I) {<br>+ Result = SurveyUse(I, MaybeLiveUses, RetValNum);<br>+ if (Result == Live)<br>+ break;<br>+ }<br>+ return Result;<br>+ }<br>+ CallSite CS = CallSite::get(V);<br>+ if (CS.getInstruction()) {<br>+ Function *F = CS.getCalledFunction();<br>+ if (F) {<br>+ // Used in a direct call<br>+ <br>+ // Check for vararg. Do - 1 to skip the first operand to call (the<br>+ // function itself).<br>+ if (U.getOperandNo() - 1 >= F->getFunctionType()->getNumParams())<br>+ // The value is passed in through a vararg! Must be live.<br>+ return Live;<br>+<br>+ // Value passed to a normal call. It's only live when the corresponding<br>+ // argument (operand number - 1 to skip the function pointer operand) to<br>+ // the called function turns out live<br>+ RetOrArg Use = CreateArg(F, U.getOperandNo() - 1);<br>+ return IsMaybeLive(Use, MaybeLiveUses);<br>+ } else {<br>+ // Used in any other way? Value must be live.<br>+ return Live;<br>+ }<br>+ }<br>+ // Used in any other way? Value must be live.<br>+ return Live;<br> }<br><br>+/// SurveyUses - This looks at all the uses of the given return value<br>+/// (possibly a partial return value from a function returning a struct).<br>+/// Returns the Liveness deduced from the uses of this value.<br>+///<br>+/// Adds all uses that cause the result to be MaybeLive to MaybeLiveRetUses.<br>+DAE::Liveness DAE::SurveyUses(Value *V, UseVector &MaybeLiveUses) {<br>+ // Assume it's dead (which will only hold if there are no uses at all..)<br>+ Liveness Result = Dead;<br>+ // Check each use<br>+ for (Value::use_iterator I = V->use_begin(),<br>+ E = V->use_end(); I != E; ++I) {<br>+ Result = SurveyUse(I, MaybeLiveUses);<br>+ if (Result == Live)<br>+ break;<br>+ }<br>+ return Result;<br>+}<br><br> // SurveyFunction - This performs the initial survey of the specified function,<br> // checking out whether or not it uses any of its incoming arguments or whether<br>@@ -295,12 +373,36 @@<br> //<br> void DAE::SurveyFunction(Function &F) {<br> bool FunctionIntrinsicallyLive = false;<br>- Liveness RetValLiveness = F.getReturnType() == Type::VoidTy ? Live : Dead;<br>+ unsigned RetCount = NumRetVals(&F);<br>+ // Assume all return values are dead<br>+ typedef SmallVector<Liveness, 5> RetVals;<br>+ RetVals RetValLiveness(RetCount, Dead);<br>+<br>+ // These vectors maps each return value to the uses that make it MaybeLive, so<br>+ // we can add those to the MaybeLiveRetVals list if the return value<br>+ // really turns out to be MaybeLive. Initializes to RetCount empty vectors<br>+ typedef SmallVector<UseVector, 5> RetUses;<br>+ // Intialized to a list of RetCount empty lists<br>+ RetUses MaybeLiveRetUses(RetCount);<br>+ <br>+ for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)<br>+ if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()))<br>+ if (RI->getNumOperands() != 0 && RI->getOperand(0)->getType() != F.getFunctionType()->getReturnType()) {<br>+ // We don't support old style multiple return values<br>+ FunctionIntrinsicallyLive = true;<br>+ break;<br>+ }<br><br>- if (!F.hasInternalLinkage() &&<br>- (!ShouldHackArguments() || F.isIntrinsic()))<br>+ if (!F.hasInternalLinkage() && (!ShouldHackArguments() || F.isIntrinsic()))<br> FunctionIntrinsicallyLive = true;<br>- else<br>+<br>+ if (!FunctionIntrinsicallyLive) {<br>+ DOUT << "DAE - Inspecting callers for fn: " << F.getName() << "\n";<br>+ // Keep track of the number of live retvals, so we can skip checks once all<br>+ // of them turn out to be live.<br>+ unsigned NumLiveRetVals = 0;<br>+ const Type *STy = dyn_cast<StructType>(F.getReturnType());<br>+ // Loop all uses of the function<br> for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I) {<br> // If the function is PASSED IN as an argument, its address has been taken<br> if (I.getOperandNo() != 0) {<br>@@ -315,191 +417,141 @@<br> FunctionIntrinsicallyLive = true;<br> break;<br> }<br>-<br>- // Check to see if the return value is used...<br>- if (RetValLiveness != Live)<br>- for (Value::use_iterator I = TheCall->use_begin(),<br>- E = TheCall->use_end(); I != E; ++I)<br>- if (isa<ReturnInst>(cast<Instruction>(*I))) {<br>- RetValLiveness = MaybeLive;<br>- } else if (isa<CallInst>(cast<Instruction>(*I)) ||<br>- isa<InvokeInst>(cast<Instruction>(*I))) {<br>- if (CallPassesValueThoughVararg(cast<Instruction>(*I), TheCall) ||<br>- !CallSite::get(cast<Instruction>(*I)).getCalledFunction()) {<br>- RetValLiveness = Live;<br>- break;<br>+ <br>+ // If we end up here, we are looking at a direct call to our function.<br>+ <br>+ // Now, check how our return value(s) is/are used in this caller. Don't<br>+ // bother checking return values if all of them are live already<br>+ if (NumLiveRetVals != RetCount) { <br>+ if (STy) {<br>+ // Check all uses of the return value<br>+ for (Value::use_iterator I = TheCall->use_begin(),<br>+ E = TheCall->use_end(); I != E; ++I) {<br>+ ExtractValueInst *Ext = dyn_cast<ExtractValueInst>(*I);<br>+ if (Ext && Ext->hasIndices()) {<br>+ // This use uses a part of our return value, survey the uses of that<br>+ // part and store the results for this index only.<br>+ unsigned Idx = *Ext->idx_begin();<br>+ if (RetValLiveness[Idx] != Live) {<br>+ RetValLiveness[Idx] = SurveyUses(Ext, MaybeLiveRetUses[Idx]);<br>+ if (RetValLiveness[Idx] == Live)<br>+ NumLiveRetVals++;<br>+ }<br> } else {<br>- RetValLiveness = MaybeLive;<br>+ // Used by something else than extractvalue. Mark all<br>+ // return values as live.<br>+ for (unsigned i = 0; i != RetCount; ++i )<br>+ RetValLiveness[i] = Live;<br>+ NumLiveRetVals = RetCount;<br>+ break;<br> }<br>- } else {<br>- RetValLiveness = Live;<br>- break;<br> }<br>+ } else {<br>+ // Single return value<br>+ RetValLiveness[0] = SurveyUses(TheCall, MaybeLiveRetUses[0]);<br>+ if (RetValLiveness[0] == Live)<br>+ NumLiveRetVals = RetCount;<br>+ }<br>+ }<br> }<br>-<br>+ }<br> if (FunctionIntrinsicallyLive) {<br>- DOUT << " Intrinsically live fn: " << F.getName() << "\n";<br>+ DOUT << "DAE - Intrinsically live fn: " << F.getName() << "\n";<br>+ // Mark all arguments as live<br>+ unsigned i = 0;<br> for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();<br>- AI != E; ++AI)<br>- LiveArguments.insert(AI);<br>- LiveRetVal.insert(&F);<br>+ AI != E; ++AI, ++i)<br>+ MarkLive(CreateArg(&F, i));<br>+ // Mark all return values as live<br>+ i = 0;<br>+ for (unsigned i = 0, e = RetValLiveness.size(); i != e; ++i)<br>+ MarkLive(CreateRet(&F, i));<br> return;<br> }<br>-<br>- switch (RetValLiveness) {<br>- case Live: LiveRetVal.insert(&F); break;<br>- case MaybeLive: MaybeLiveRetVal.insert(&F); break;<br>- case Dead: DeadRetVal.insert(&F); break;<br>+ <br>+ // Now we've inspected all callers, record the liveness of our return values.<br>+ for (unsigned i = 0, e = RetValLiveness.size(); i != e; ++i) {<br>+ RetOrArg Ret = CreateRet(&F, i);<br>+ // Mark the result down<br>+ MarkValue(Ret, RetValLiveness[i], MaybeLiveRetUses[i]);<br>+ }<br>+ DOUT << "DAE - Inspecting args for fn: " << F.getName() << "\n";<br>+<br>+ // Now, check all of our arguments<br>+ unsigned i = 0;<br>+ UseVector MaybeLiveArgUses;<br>+ for (Function::arg_iterator AI = F.arg_begin(), <br>+ E = F.arg_end(); AI != E; ++AI, ++i) {<br>+ // See what the effect of this use is (recording any uses that cause<br>+ // MaybeLive in MaybeLiveArgUses)<br>+ Liveness Result = SurveyUses(AI, MaybeLiveArgUses);<br>+ RetOrArg Arg = CreateArg(&F, i);<br>+ // Mark the result down<br>+ MarkValue(Arg, Result, MaybeLiveArgUses);<br>+ // Clear the vector again for the next iteration<br>+ MaybeLiveArgUses.clear();<br> }<br>+}<br><br>- DOUT << " Inspecting args for fn: " << F.getName() << "\n";<br>-<br>- // If it is not intrinsically alive, we know that all users of the<br>- // function are call sites. Mark all of the arguments live which are<br>- // directly used, and keep track of all of the call sites of this function<br>- // if there are any arguments we assume that are dead.<br>- //<br>- bool AnyMaybeLiveArgs = false;<br>- for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();<br>- AI != E; ++AI)<br>- switch (getArgumentLiveness(*AI)) {<br>- case Live:<br>- DOUT << " Arg live by use: " << AI->getName() << "\n";<br>- LiveArguments.insert(AI);<br>- break;<br>- case Dead:<br>- DOUT << " Arg definitely dead: " << AI->getName() <<"\n";<br>- DeadArguments.insert(AI);<br>- break;<br>+/// MarkValue - This function marks the liveness of RA depending on L. If L is<br>+/// MaybeLive, it also records any uses in MaybeLiveUses such that RA will be<br>+/// marked live if any use in MaybeLiveUses gets marked live later on.<br>+void DAE::MarkValue(const RetOrArg &RA, Liveness L, const UseVector &MaybeLiveUses) {<br>+ switch (L) {<br>+ case Live: MarkLive(RA); break;<br> case MaybeLive:<br>- DOUT << " Arg only passed to calls: " << AI->getName() << "\n";<br>- AnyMaybeLiveArgs = true;<br>- MaybeLiveArguments.insert(AI);<br>+ {<br>+ // Note any uses of this value, so this return value can be<br>+ // marked live whenever one of the uses becomes live.<br>+ UseMap::iterator Where = Uses.begin();<br>+ for (UseVector::const_iterator UI = MaybeLiveUses.begin(), <br>+ UE = MaybeLiveUses.end(); UI != UE; ++UI)<br>+ Where = Uses.insert(Where, UseMap::value_type(*UI, RA));<br> break;<br> }<br>-<br>- // If there are any "MaybeLive" arguments, we need to check callees of<br>- // this function when/if they become alive. Record which functions are<br>- // callees...<br>- if (AnyMaybeLiveArgs || RetValLiveness == MaybeLive)<br>- for (Value::use_iterator I = F.use_begin(), E = F.use_end();<br>- I != E; ++I) {<br>- if (AnyMaybeLiveArgs)<br>- CallSites.insert(std::make_pair(&F, CallSite::get(*I)));<br>-<br>- if (RetValLiveness == MaybeLive)<br>- for (Value::use_iterator UI = I->use_begin(), E = I->use_end();<br>- UI != E; ++UI)<br>- InstructionsToInspect.push_back(cast<Instruction>(*UI));<br>- }<br>-}<br>-<br>-// isMaybeLiveArgumentNowLive - Check to see if Arg is alive. At this point, we<br>-// know that the only uses of Arg are to be passed in as an argument to a<br>-// function call or return. Check to see if the formal argument passed in is in<br>-// the LiveArguments set. If so, return true.<br>-//<br>-bool DAE::isMaybeLiveArgumentNowLive(Argument *Arg) {<br>- for (Value::use_iterator I = Arg->use_begin(), E = Arg->use_end(); I!=E; ++I){<br>- if (isa<ReturnInst>(*I)) {<br>- if (LiveRetVal.count(Arg->getParent())) return true;<br>- continue;<br>- }<br>-<br>- CallSite CS = CallSite::get(*I);<br>-<br>- // We know that this can only be used for direct calls...<br>- Function *Callee = CS.getCalledFunction();<br>-<br>- // Loop over all of the arguments (because Arg may be passed into the call<br>- // multiple times) and check to see if any are now alive...<br>- CallSite::arg_iterator CSAI = CS.arg_begin();<br>- for (Function::arg_iterator AI = Callee->arg_begin(), E = Callee->arg_end();<br>- AI != E; ++AI, ++CSAI)<br>- // If this is the argument we are looking for, check to see if it's alive<br>- if (*CSAI == Arg && LiveArguments.count(AI))<br>- return true;<br>- }<br>- return false;<br>-}<br>-<br>-/// MarkArgumentLive - The MaybeLive argument 'Arg' is now known to be alive.<br>-/// Mark it live in the specified sets and recursively mark arguments in callers<br>-/// live that are needed to pass in a value.<br>-///<br>-void DAE::MarkArgumentLive(Argument *Arg) {<br>- std::set<Argument*>::iterator It = MaybeLiveArguments.lower_bound(Arg);<br>- if (It == MaybeLiveArguments.end() || *It != Arg) return;<br>-<br>- DOUT << " MaybeLive argument now live: " << Arg->getName() <<"\n";<br>- MaybeLiveArguments.erase(It);<br>- LiveArguments.insert(Arg);<br>-<br>- // Loop over all of the call sites of the function, making any arguments<br>- // passed in to provide a value for this argument live as necessary.<br>- //<br>- Function *Fn = Arg->getParent();<br>- unsigned ArgNo = std::distance(Fn->arg_begin(), Function::arg_iterator(Arg));<br>-<br>- std::multimap<Function*, CallSite>::iterator I = CallSites.lower_bound(Fn);<br>- for (; I != CallSites.end() && I->first == Fn; ++I) {<br>- CallSite CS = I->second;<br>- Value *ArgVal = *(CS.arg_begin()+ArgNo);<br>- if (Argument *ActualArg = dyn_cast<Argument>(ArgVal)) {<br>- MarkArgumentLive(ActualArg);<br>- } else {<br>- // If the value passed in at this call site is a return value computed by<br>- // some other call site, make sure to mark the return value at the other<br>- // call site as being needed.<br>- CallSite ArgCS = CallSite::get(ArgVal);<br>- if (ArgCS.getInstruction())<br>- if (Function *Fn = ArgCS.getCalledFunction())<br>- MarkRetValLive(Fn);<br>- }<br>+ case Dead: break;<br> }<br> }<br><br>-/// MarkArgumentLive - The MaybeLive return value for the specified function is<br>-/// now known to be alive. Propagate this fact to the return instructions which<br>-/// produce it.<br>-void DAE::MarkRetValLive(Function *F) {<br>- assert(F && "Shame shame, we can't have null pointers here!");<br>-<br>- // Check to see if we already knew it was live<br>- std::set<Function*>::iterator I = MaybeLiveRetVal.lower_bound(F);<br>- if (I == MaybeLiveRetVal.end() || *I != F) return; // It's already alive!<br>-<br>- DOUT << " MaybeLive retval now live: " << F->getName() << "\n";<br>-<br>- MaybeLiveRetVal.erase(I);<br>- LiveRetVal.insert(F); // It is now known to be live!<br>+/// MarkLive - Mark the given return value or argument as live. Additionally,<br>+/// mark any values that are used by this value (according to Uses) live as<br>+/// well.<br>+void DAE::MarkLive(RetOrArg RA) {<br>+ if (!LiveValues.insert(RA).second)<br>+ return; // We were already marked Live<br><br>- // Loop over all of the functions, noticing that the return value is now live.<br>- for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)<br>- if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()))<br>- MarkReturnInstArgumentLive(RI);<br>-}<br>-<br>-void DAE::MarkReturnInstArgumentLive(ReturnInst *RI) {<br>- Value *Op = RI->getOperand(0);<br>- if (Argument *A = dyn_cast<Argument>(Op)) {<br>- MarkArgumentLive(A);<br>- } else if (CallInst *CI = dyn_cast<CallInst>(Op)) {<br>- if (Function *F = CI->getCalledFunction())<br>- MarkRetValLive(F);<br>- } else if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) {<br>- if (Function *F = II->getCalledFunction())<br>- MarkRetValLive(F);<br>- }<br>-}<br>+ if (RA.IsArg)<br>+ DOUT << "DAE - Marking argument " << RA.Idx << " to function " << RA.F->getNameStart() << " live\n";<br>+ else<br>+ DOUT << "DAE - Marking return value " << RA.Idx << " of function " << RA.F->getNameStart() << " live\n";<br><br>-// RemoveDeadArgumentsFromFunction - We know that F has dead arguments, as<br>+ // We don't use upper_bound (or equal_range) here, because our recursive call<br>+ // to ourselves is likely to mark the upper_bound (which is the first value<br>+ // not belonging to RA) to become erased and the iterator invalidated.<br>+ UseMap::iterator Begin = Uses.lower_bound(RA);<br>+ UseMap::iterator E = Uses.end();<br>+ UseMap::iterator I;<br>+ for (I = Begin; I != E && I->first == RA; ++I)<br>+ MarkLive(I->second);<br>+<br>+ // Erase RA from the Uses map (from the lower bound to wherever we ended up<br>+ // after the loop).<br>+ Uses.erase(Begin, I);<br>+}<br>+<br>+// RemoveDeadStuffFromFunction - Remove any arguments and return values from F<br>+// that are not in LiveValues. This function is a noop for any Function created<br>+// by this function before, or any function that was not inspected for liveness.<br> // specified by the DeadArguments list. Transform the function and all of the<br> // callees of the function to not have these arguments.<br> //<br>-void DAE::RemoveDeadArgumentsFromFunction(Function *F) {<br>+bool DAE::RemoveDeadStuffFromFunction(Function *F) {<br>+ // Quick exit path for external functions<br>+ if (!F->hasInternalLinkage() && (!ShouldHackArguments() || F->isIntrinsic()))<br>+ return false;<br>+<br> // Start by computing a new prototype for the function, which is the same as<br>- // the old function, but has fewer arguments.<br>+ // the old function, but has fewer arguments and a different return type.<br> const FunctionType *FTy = F->getFunctionType();<br> std::vector<const Type*> Params;<br><br>@@ -510,28 +562,78 @@<br> // The existing function return attributes.<br> ParameterAttributes RAttrs = PAL.getParamAttrs(0);<br><br>- // Make the function return void if the return value is dead.<br>+ <br>+ // Find out the new return value<br>+ <br> const Type *RetTy = FTy->getReturnType();<br>- if (DeadRetVal.count(F)) {<br>- RetTy = Type::VoidTy;<br>- RAttrs &= ~ParamAttr::typeIncompatible(RetTy);<br>- DeadRetVal.erase(F);<br>- }<br>-<br>+ const Type *NRetTy;<br>+ unsigned RetCount = NumRetVals(F);<br>+ // -1 means unused, other numbers are the new index<br>+ SmallVector<int, 5> NewRetIdxs(RetCount, -1);<br>+ std::vector<const Type*> RetTypes;<br>+ if (RetTy != Type::VoidTy) {<br>+ const StructType *STy = dyn_cast<StructType>(RetTy);<br>+ if (STy)<br>+ // Look at each of the original return values individually<br>+ for (unsigned i = 0; i != RetCount; ++i) {<br>+ RetOrArg Ret = CreateRet(F, i);<br>+ if (LiveValues.erase(Ret)) {<br>+ RetTypes.push_back(STy->getElementType(i));<br>+ NewRetIdxs[i] = RetTypes.size() - 1;<br>+ } else {<br>+ ++NumRetValsEliminated;<br>+ DOUT << "DAE - Removing return value " << i << " from " << F->getNameStart() << "\n";<br>+ }<br>+ }<br>+ else<br>+ // We used to return a single value<br>+ if (LiveValues.erase(CreateRet(F, 0))) {<br>+ RetTypes.push_back(RetTy);<br>+ NewRetIdxs[0] = 0;<br>+ } else {<br>+ DOUT << "DAE - Removing return value from " << F->getNameStart() << "\n";<br>+ ++NumRetValsEliminated;<br>+ } <br>+ if (RetTypes.size() == 0)<br>+ // No return types? Make it void<br>+ NRetTy = Type::VoidTy;<br>+ else if (RetTypes.size() == 1)<br>+ // One return type? Just a simple value then<br>+ NRetTy = RetTypes.front();<br>+ else<br>+ // More return types? Return a struct with them<br>+ NRetTy = StructType::get(RetTypes);<br>+ } else {<br>+ NRetTy = Type::VoidTy;<br>+ }<br>+ <br>+ // Remove any incompatible attributes<br>+ RAttrs &= ~ParamAttr::typeIncompatible(NRetTy);<br> if (RAttrs)<br> ParamAttrsVec.push_back(ParamAttrsWithIndex::get(0, RAttrs));<br>-<br>+ <br>+ // Remember which arguments are still alive<br>+ SmallVector<bool, 10> ArgAlive(FTy->getNumParams(), false);<br> // Construct the new parameter list from non-dead arguments. Also construct<br>- // a new set of parameter attributes to correspond.<br>- unsigned index = 1;<br>- for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E;<br>- ++I, ++index)<br>- if (!DeadArguments.count(I)) {<br>+ // a new set of parameter attributes to correspond. Skip the first parameter<br>+ // attribute, since that belongs to the return value.<br>+ unsigned i = 0;<br>+ for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();<br>+ I != E; ++I, ++i) {<br>+ RetOrArg Arg = CreateArg(F, i);<br>+ if (LiveValues.erase(Arg)) {<br> Params.push_back(I->getType());<br>+ ArgAlive[i] = true;<br><br>- if (ParameterAttributes Attrs = PAL.getParamAttrs(index))<br>+ // Get the original parameter attributes (skipping the first one, that is<br>+ // for the return value<br>+ if (ParameterAttributes Attrs = PAL.getParamAttrs(i + 1))<br> ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Params.size(), Attrs));<br>+ } else {<br>+ ++NumArgumentsEliminated;<br>+ DOUT << "DAE - Removing argument " << i << " (" << I->getNameStart() << ") from " << F->getNameStart() << "\n";<br> }<br>+ }<br><br> // Reconstruct the ParamAttrsList based on the vector we constructed.<br> PAListPtr NewPAL = PAListPtr::get(ParamAttrsVec.begin(), ParamAttrsVec.end());<br>@@ -539,19 +641,28 @@<br> // Work around LLVM bug PR56: the CWriter cannot emit varargs functions which<br> // have zero fixed arguments.<br> //<br>+ // Not that we apply this hack for a vararg fuction that does not have any<br>+ // arguments anymore, but did have them before (so don't bother fixing<br>+ // functions that were already broken wrt CWriter).<br> bool ExtraArgHack = false;<br>- if (Params.empty() && FTy->isVarArg()) {<br>+ if (Params.empty() && FTy->isVarArg() && FTy->getNumParams() != 0) {<br> ExtraArgHack = true;<br> Params.push_back(Type::Int32Ty);<br> }<br><br> // Create the new function type based on the recomputed parameters.<br>- FunctionType *NFTy = FunctionType::get(RetTy, Params, FTy->isVarArg());<br>+ FunctionType *NFTy = FunctionType::get(NRetTy, Params, FTy->isVarArg());<br>+ <br>+ // No change?<br>+ if (NFTy == FTy)<br>+ return false;<br><br> // Create the new function body and insert it into the module...<br> Function *NF = Function::Create(NFTy, F->getLinkage());<br> NF->copyAttributesFrom(F);<br> NF->setParamAttrs(NewPAL);<br>+ // Insert the new function before the old function, so we won't be processing<br>+ // it again<br> F->getParent()->getFunctionList().insert(F, NF);<br> NF->takeName(F);<br><br>@@ -562,6 +673,11 @@<br> while (!F->use_empty()) {<br> CallSite CS = CallSite::get(F->use_back());<br> Instruction *Call = CS.getInstruction();<br>+ if (!Call) {<br>+ DOUT << "Old: " << *FTy << "\n";<br>+ DOUT << "New: " << *NFTy << "\n";<br>+ }<br>+<br> ParamAttrsVec.clear();<br> const PAListPtr &CallPAL = CS.getParamAttrs();<br><br>@@ -572,14 +688,17 @@<br> if (RAttrs)<br> ParamAttrsVec.push_back(ParamAttrsWithIndex::get(0, RAttrs));<br><br>- // Loop over the operands, deleting dead ones...<br>- CallSite::arg_iterator AI = CS.arg_begin();<br>- index = 1;<br>- for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();<br>- I != E; ++I, ++AI, ++index)<br>- if (!DeadArguments.count(I)) { // Remove operands for dead arguments<br>- Args.push_back(*AI);<br>- if (ParameterAttributes Attrs = CallPAL.getParamAttrs(index))<br>+ // Declare these outside of the loops, so we can reuse them for the second<br>+ // loop, which loops the varargs<br>+ CallSite::arg_iterator I = CS.arg_begin();<br>+ unsigned i = 0; <br>+ // Loop over those operands, corresponding to the normal arguments to the<br>+ // original function, and add those that are still alive.<br>+ for (unsigned e = FTy->getNumParams(); i != e; ++I, ++i)<br>+ if (ArgAlive[i]) {<br>+ Args.push_back(*I);<br>+ // Get original parameter attributes, but skip return attributes<br>+ if (ParameterAttributes Attrs = CallPAL.getParamAttrs(i + 1))<br> ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Args.size(), Attrs));<br> }<br><br>@@ -587,9 +706,9 @@<br> Args.push_back(UndefValue::get(Type::Int32Ty));<br><br> // Push any varargs arguments on the list. Don't forget their attributes.<br>- for (; AI != CS.arg_end(); ++AI) {<br>- Args.push_back(*AI);<br>- if (ParameterAttributes Attrs = CallPAL.getParamAttrs(index++))<br>+ for (CallSite::arg_iterator E = CS.arg_end(); I != E; ++I, ++i) {<br>+ Args.push_back(*I);<br>+ if (ParameterAttributes Attrs = CallPAL.getParamAttrs(i + 1))<br> ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Args.size(), Attrs));<br> }<br><br>@@ -614,8 +733,45 @@<br><br> if (!Call->use_empty()) {<br> if (New->getType() == Type::VoidTy)<br>+ // Our return value was unused, replace by null for now, uses will get<br>+ // removed later on<br> Call->replaceAllUsesWith(Constant::getNullValue(Call->getType()));<br>- else {<br>+ else if (isa<StructType>(RetTy)) {<br>+ // The original return value was a struct, update all uses (which are<br>+ // all extractvalue instructions).<br>+ for (Value::use_iterator I = Call->use_begin(), E = Call->use_end();<br>+ I != E;) {<br>+ assert(isa<ExtractValueInst>(*I) && "Return value not only used by extractvalue?");<br>+ ExtractValueInst *EV = cast<ExtractValueInst>(*I);<br>+ // Increment now, since we're about to throw away this use.<br>+ ++I;<br>+ assert(EV->hasIndices() && "Return value used by extractvalue without indices?");<br>+ unsigned Idx = *EV->idx_begin();<br>+ if (NewRetIdxs[Idx] != -1) {<br>+ if (RetTypes.size() > 1) {<br>+ // We're still returning a struct, create a new extractvalue<br>+ // instruction with the first index updated<br>+ std::vector<unsigned> NewIdxs(EV->idx_begin(), EV->idx_end());<br>+ NewIdxs[0] = NewRetIdxs[Idx];<br>+ Value *NEV = ExtractValueInst::Create(New, NewIdxs.begin(), NewIdxs.end(), "retval", EV);<br>+ EV->replaceAllUsesWith(NEV);<br>+ EV->eraseFromParent();<br>+ } else {<br>+ // We are now only returning a simple value, remove the<br>+ // extractvalue<br>+ EV->replaceAllUsesWith(New);<br>+ EV->eraseFromParent();<br>+ }<br>+ } else {<br>+ // Value unused, replace uses by null for now, they will get removed<br>+ // later on<br>+ EV->replaceAllUsesWith(Constant::getNullValue(EV->getType()));<br>+ EV->eraseFromParent();<br>+ }<br>+ }<br>+ New->takeName(Call);<br>+ } else {<br>+ // The original function had a single return value<br> Call->replaceAllUsesWith(New);<br> New->takeName(Call);<br> }<br>@@ -632,13 +788,11 @@<br> NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());<br><br> // Loop over the argument list, transfering uses of the old arguments over to<br>- // the new arguments, also transfering over the names as well. While we're at<br>- // it, remove the dead arguments from the DeadArguments list.<br>- //<br>+ // the new arguments, also transfering over the names as well.<br>+ i = 0;<br> for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(),<br>- I2 = NF->arg_begin();<br>- I != E; ++I)<br>- if (!DeadArguments.count(I)) {<br>+ I2 = NF->arg_begin(); I != E; ++I, ++i)<br>+ if (ArgAlive[i]) {<br> // If this is a live argument, move the name and users over to the new<br> // version.<br> I->replaceAllUsesWith(I2);<br>@@ -646,10 +800,8 @@<br> ++I2;<br> } else {<br> // If this argument is dead, replace any uses of it with null constants<br>- // (these are guaranteed to only be operands to call instructions which<br>- // will later be simplified).<br>+ // (these are guaranteed to become unused later on)<br> I->replaceAllUsesWith(Constant::getNullValue(I->getType()));<br>- DeadArguments.erase(I);<br> }<br><br> // If we change the return value of the function we must rewrite any return<br>@@ -657,12 +809,45 @@<br> if (F->getReturnType() != NF->getReturnType())<br> for (Function::iterator BB = NF->begin(), E = NF->end(); BB != E; ++BB)<br> if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {<br>- ReturnInst::Create(0, RI);<br>+ Value *RetVal;<br>+<br>+ if (NFTy->getReturnType() == Type::VoidTy) {<br>+ RetVal = 0;<br>+ } else {<br>+ assert (isa<StructType>(RetTy));<br>+ // The original return value was a struct, insert<br>+ // extractvalue/insertvalue chains to extract only the values we need<br>+ // to return and insert them into our new result.<br>+ // This does generate messy code, but we'll let it to instcombine to<br>+ // clean that up<br>+ Value *OldRet = RI->getOperand(0);<br>+ // Start out building up our return value from undef<br>+ RetVal = llvm::UndefValue::get(NRetTy);<br>+ for (unsigned i = 0; i != RetCount; ++i)<br>+ if (NewRetIdxs[i] != -1) {<br>+ ExtractValueInst *EV = ExtractValueInst::Create(OldRet, i, "newret", RI);<br>+ if (RetTypes.size() > 1) {<br>+ // We're still returning a struct, so reinsert the value into<br>+ // our new return value at the new index<br>+<br>+ RetVal = InsertValueInst::Create(RetVal, EV, NewRetIdxs[i], "oldret");<br>+ } else {<br>+ // We are now only returning a simple value, so just return the<br>+ // extracted value<br>+ RetVal = EV;<br>+ }<br>+ } <br>+ } <br>+ // Replace the return instruction with one returning the new return<br>+ // value (possibly 0 if we became void).<br>+ ReturnInst::Create(RetVal, RI);<br> BB->getInstList().erase(RI);<br> }<br><br> // Now that the old function is dead, delete it.<br> F->eraseFromParent();<br>+<br>+ return true;<br> }<br><br> bool DAE::runOnModule(Module &M) {<br>@@ -677,7 +862,7 @@<br> if (F.getFunctionType()->isVarArg())<br> Changed |= DeleteDeadVarargs(F);<br> }<br>- <br>+<br> // Second phase:loop through the module, determining which arguments are live.<br> // We assume all arguments are dead unless proven otherwise (allowing us to<br> // determine that dead arguments passed into recursive functions are dead).<br>@@ -686,85 +871,14 @@<br> for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)<br> SurveyFunction(*I);<br><br>- // Loop over the instructions to inspect, propagating liveness among arguments<br>- // and return values which are MaybeLive.<br>- while (!InstructionsToInspect.empty()) {<br>- Instruction *I = InstructionsToInspect.back();<br>- InstructionsToInspect.pop_back();<br>-<br>- if (ReturnInst *RI = dyn_cast<ReturnInst>(I)) {<br>- // For return instructions, we just have to check to see if the return<br>- // value for the current function is known now to be alive. If so, any<br>- // arguments used by it are now alive, and any call instruction return<br>- // value is alive as well.<br>- if (LiveRetVal.count(RI->getParent()->getParent()))<br>- MarkReturnInstArgumentLive(RI);<br>-<br>- } else {<br>- CallSite CS = CallSite::get(I);<br>- assert(CS.getInstruction() && "Unknown instruction for the I2I list!");<br>-<br>- Function *Callee = CS.getCalledFunction();<br>-<br>- // If we found a call or invoke instruction on this list, that means that<br>- // an argument of the function is a call instruction. If the argument is<br>- // live, then the return value of the called instruction is now live.<br>- //<br>- CallSite::arg_iterator AI = CS.arg_begin(); // ActualIterator<br>- for (Function::arg_iterator FI = Callee->arg_begin(),<br>- E = Callee->arg_end(); FI != E; ++AI, ++FI) {<br>- // If this argument is another call...<br>- CallSite ArgCS = CallSite::get(*AI);<br>- if (ArgCS.getInstruction() && LiveArguments.count(FI))<br>- if (Function *Callee = ArgCS.getCalledFunction())<br>- MarkRetValLive(Callee);<br>- }<br>- }<br>+ // Now, remove all dead arguments and return values from each function in<br>+ // turn<br>+ for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {<br>+ // Increment now, because the function will probably get removed (ie<br>+ // replaced by a new one)<br>+ Function *F = I++;<br>+ Changed |= RemoveDeadStuffFromFunction(F);<br> }<br><br>- // Now we loop over all of the MaybeLive arguments, promoting them to be live<br>- // arguments if one of the calls that uses the arguments to the calls they are<br>- // passed into requires them to be live. Of course this could make other<br>- // arguments live, so process callers recursively.<br>- //<br>- // Because elements can be removed from the MaybeLiveArguments set, copy it to<br>- // a temporary vector.<br>- //<br>- std::vector<Argument*> TmpArgList(MaybeLiveArguments.begin(),<br>- MaybeLiveArguments.end());<br>- for (unsigned i = 0, e = TmpArgList.size(); i != e; ++i) {<br>- Argument *MLA = TmpArgList[i];<br>- if (MaybeLiveArguments.count(MLA) &&<br>- isMaybeLiveArgumentNowLive(MLA))<br>- MarkArgumentLive(MLA);<br>- }<br>-<br>- // Recover memory early...<br>- CallSites.clear();<br>-<br>- // At this point, we know that all arguments in DeadArguments and<br>- // MaybeLiveArguments are dead. If the two sets are empty, there is nothing<br>- // to do.<br>- if (MaybeLiveArguments.empty() && DeadArguments.empty() &&<br>- MaybeLiveRetVal.empty() && DeadRetVal.empty())<br>- return Changed;<br>-<br>- // Otherwise, compact into one set, and start eliminating the arguments from<br>- // the functions.<br>- DeadArguments.insert(MaybeLiveArguments.begin(), MaybeLiveArguments.end());<br>- MaybeLiveArguments.clear();<br>- DeadRetVal.insert(MaybeLiveRetVal.begin(), MaybeLiveRetVal.end());<br>- MaybeLiveRetVal.clear();<br>-<br>- LiveArguments.clear();<br>- LiveRetVal.clear();<br>-<br>- NumArgumentsEliminated += DeadArguments.size();<br>- NumRetValsEliminated += DeadRetVal.size();<br>- while (!DeadArguments.empty())<br>- RemoveDeadArgumentsFromFunction((*DeadArguments.begin())->getParent());<br>-<br>- while (!DeadRetVal.empty())<br>- RemoveDeadArgumentsFromFunction(*DeadRetVal.begin());<br>- return true;<br>+ return Changed;<br> }<br><br><br>_______________________________________________<br>llvm-commits mailing list<br><a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits<br></div></blockquote></div><br></div></body></html>