Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 1 | //===-- DeadArgumentElimination.cpp - Eliminate dead arguments ------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 9 | // |
| 10 | // This pass deletes dead arguments from internal functions. Dead argument |
| 11 | // elimination removes arguments which are directly dead, as well as arguments |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 12 | // only passed into function calls as dead arguments of other functions. This |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 13 | // pass also deletes dead return values in a similar way. |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 14 | // |
| 15 | // This pass is often useful as a cleanup pass to run after aggressive |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 16 | // interprocedural passes, which add possibly-dead arguments or return values. |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Chris Lattner | 543a027 | 2005-06-24 16:00:46 +0000 | [diff] [blame] | 20 | #define DEBUG_TYPE "deadargelim" |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 21 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | 92044ce | 2006-06-27 21:05:04 +0000 | [diff] [blame] | 22 | #include "llvm/CallingConv.h" |
| 23 | #include "llvm/Constant.h" |
| 24 | #include "llvm/DerivedTypes.h" |
| 25 | #include "llvm/Instructions.h" |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 26 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 27 | #include "llvm/Module.h" |
| 28 | #include "llvm/Pass.h" |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CallSite.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Debug.h" |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallVector.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/Statistic.h" |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Compiler.h" |
Dan Gohman | c9235d2 | 2008-03-21 23:51:57 +0000 | [diff] [blame] | 34 | #include <map> |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 35 | #include <set> |
Chris Lattner | 1e2385b | 2003-11-21 21:54:22 +0000 | [diff] [blame] | 36 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 38 | STATISTIC(NumArgumentsEliminated, "Number of unread args removed"); |
| 39 | STATISTIC(NumRetValsEliminated , "Number of unused return values removed"); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 41 | namespace { |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 42 | /// DAE - The dead argument elimination pass. |
| 43 | /// |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 44 | class VISIBILITY_HIDDEN DAE : public ModulePass { |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 45 | public: |
| 46 | |
| 47 | /// Struct that represent either a (part of a) return value or a function |
| 48 | /// argument. Used so that arguments and return values can be used |
| 49 | /// interchangably. |
| 50 | struct RetOrArg { |
| 51 | RetOrArg(const Function* F, unsigned Idx, bool IsArg) : F(F), Idx(Idx), IsArg(IsArg) {} |
| 52 | const Function *F; |
| 53 | unsigned Idx; |
| 54 | bool IsArg; |
| 55 | |
| 56 | /// Make RetOrArg comparable, so we can put it into a map |
| 57 | bool operator<(const RetOrArg &O) const { |
| 58 | if (F != O.F) |
| 59 | return F < O.F; |
| 60 | else if (Idx != O.Idx) |
| 61 | return Idx < O.Idx; |
| 62 | else |
| 63 | return IsArg < O.IsArg; |
| 64 | } |
| 65 | |
| 66 | /// Make RetOrArg comparable, so we can easily iterate the multimap |
| 67 | bool operator==(const RetOrArg &O) const { |
| 68 | return F == O.F && Idx == O.Idx && IsArg == O.IsArg; |
| 69 | } |
| 70 | }; |
| 71 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 72 | /// Liveness enum - During our initial pass over the program, we determine |
| 73 | /// that things are either definately alive, definately dead, or in need of |
| 74 | /// interprocedural analysis (MaybeLive). |
| 75 | /// |
| 76 | enum Liveness { Live, MaybeLive, Dead }; |
| 77 | |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 78 | /// Convenience wrapper |
| 79 | RetOrArg CreateRet(const Function *F, unsigned Idx) { return RetOrArg(F, Idx, false); } |
| 80 | /// Convenience wrapper |
| 81 | RetOrArg CreateArg(const Function *F, unsigned Idx) { return RetOrArg(F, Idx, true); } |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 82 | |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 83 | typedef std::multimap<RetOrArg, RetOrArg> UseMap; |
| 84 | /// This map maps a return value or argument to all return values or |
| 85 | /// arguments it uses. |
| 86 | /// For example (indices are left out for clarity): |
| 87 | /// - Uses[ret F] = ret G |
| 88 | /// This means that F calls G, and F returns the value returned by G. |
| 89 | /// - Uses[arg F] = ret G |
| 90 | /// This means that some function calls G and passes its result as an |
| 91 | /// argument to F. |
| 92 | /// - Uses[ret F] = arg F |
| 93 | /// This means that F returns one of its own arguments. |
| 94 | /// - Uses[arg F] = arg G |
| 95 | /// This means that G calls F and passes one of its own (G's) arguments |
| 96 | /// directly to F. |
| 97 | UseMap Uses; |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 98 | |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 99 | typedef std::set<RetOrArg> LiveSet; |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 100 | |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 101 | /// This set contains all values that have been determined to be live |
| 102 | LiveSet LiveValues; |
| 103 | |
| 104 | typedef SmallVector<RetOrArg, 5> UseVector; |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 105 | |
| 106 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 107 | static char ID; // Pass identification, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 108 | DAE() : ModulePass((intptr_t)&ID) {} |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 109 | bool runOnModule(Module &M); |
Chris Lattner | 9b2a14b | 2003-06-25 04:12:49 +0000 | [diff] [blame] | 110 | |
Chris Lattner | fdcc3ac | 2003-11-05 21:43:02 +0000 | [diff] [blame] | 111 | virtual bool ShouldHackArguments() const { return false; } |
| 112 | |
Chris Lattner | 9b2a14b | 2003-06-25 04:12:49 +0000 | [diff] [blame] | 113 | private: |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 114 | Liveness IsMaybeLive(RetOrArg Use, UseVector &MaybeLiveUses); |
| 115 | Liveness SurveyUse(Value::use_iterator U, UseVector &MaybeLiveUses, unsigned RetValNum = 0); |
| 116 | Liveness SurveyUses(Value *V, UseVector &MaybeLiveUses); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 117 | |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 118 | void SurveyFunction(Function &F); |
| 119 | void MarkValue(const RetOrArg &RA, Liveness L, const UseVector &MaybeLiveUses); |
| 120 | void MarkLive(RetOrArg RA); |
| 121 | bool RemoveDeadStuffFromFunction(Function *F); |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 122 | bool DeleteDeadVarargs(Function &Fn); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 123 | }; |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 124 | } |
Chris Lattner | fdcc3ac | 2003-11-05 21:43:02 +0000 | [diff] [blame] | 125 | |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 126 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 127 | char DAE::ID = 0; |
| 128 | static RegisterPass<DAE> |
| 129 | X("deadargelim", "Dead Argument Elimination"); |
| 130 | |
| 131 | namespace { |
Chris Lattner | fdcc3ac | 2003-11-05 21:43:02 +0000 | [diff] [blame] | 132 | /// DAH - DeadArgumentHacking pass - Same as dead argument elimination, but |
| 133 | /// deletes arguments to functions which are external. This is only for use |
| 134 | /// by bugpoint. |
| 135 | struct DAH : public DAE { |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 136 | static char ID; |
Chris Lattner | fdcc3ac | 2003-11-05 21:43:02 +0000 | [diff] [blame] | 137 | virtual bool ShouldHackArguments() const { return true; } |
| 138 | }; |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 141 | char DAH::ID = 0; |
| 142 | static RegisterPass<DAH> |
| 143 | Y("deadarghaX0r", "Dead Argument Hacking (BUGPOINT USE ONLY; DO NOT USE)"); |
| 144 | |
Chris Lattner | 9b2a14b | 2003-06-25 04:12:49 +0000 | [diff] [blame] | 145 | /// createDeadArgEliminationPass - This pass removes arguments from functions |
Chris Lattner | fdcc3ac | 2003-11-05 21:43:02 +0000 | [diff] [blame] | 146 | /// which are not used by the body of the function. |
Chris Lattner | 9b2a14b | 2003-06-25 04:12:49 +0000 | [diff] [blame] | 147 | /// |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 148 | ModulePass *llvm::createDeadArgEliminationPass() { return new DAE(); } |
| 149 | ModulePass *llvm::createDeadArgHackingPass() { return new DAH(); } |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 150 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 151 | /// DeleteDeadVarargs - If this is an function that takes a ... list, and if |
| 152 | /// llvm.vastart is never called, the varargs list is dead for the function. |
| 153 | bool DAE::DeleteDeadVarargs(Function &Fn) { |
| 154 | assert(Fn.getFunctionType()->isVarArg() && "Function isn't varargs!"); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 155 | if (Fn.isDeclaration() || !Fn.hasInternalLinkage()) return false; |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 156 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 157 | // Ensure that the function is only directly called. |
| 158 | for (Value::use_iterator I = Fn.use_begin(), E = Fn.use_end(); I != E; ++I) { |
| 159 | // If this use is anything other than a call site, give up. |
| 160 | CallSite CS = CallSite::get(*I); |
| 161 | Instruction *TheCall = CS.getInstruction(); |
| 162 | if (!TheCall) return false; // Not a direct call site? |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 163 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 164 | // The addr of this function is passed to the call. |
| 165 | if (I.getOperandNo() != 0) return false; |
| 166 | } |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 168 | // Okay, we know we can transform this function if safe. Scan its body |
| 169 | // looking for calls to llvm.vastart. |
| 170 | for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) { |
| 171 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
| 172 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
| 173 | if (II->getIntrinsicID() == Intrinsic::vastart) |
| 174 | return false; |
| 175 | } |
| 176 | } |
| 177 | } |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 178 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 179 | // If we get here, there are no calls to llvm.vastart in the function body, |
| 180 | // remove the "..." and adjust all the calls. |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 181 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 182 | // Start by computing a new prototype for the function, which is the same as |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 183 | // the old function, but doesn't have isVarArg set. |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 184 | const FunctionType *FTy = Fn.getFunctionType(); |
| 185 | std::vector<const Type*> Params(FTy->param_begin(), FTy->param_end()); |
| 186 | FunctionType *NFTy = FunctionType::get(FTy->getReturnType(), Params, false); |
| 187 | unsigned NumArgs = Params.size(); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 188 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 189 | // Create the new function body and insert it into the module... |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 190 | Function *NF = Function::Create(NFTy, Fn.getLinkage()); |
Duncan Sands | 28c3cff | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 191 | NF->copyAttributesFrom(&Fn); |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 192 | Fn.getParent()->getFunctionList().insert(&Fn, NF); |
Chris Lattner | 046800a | 2007-02-11 01:08:35 +0000 | [diff] [blame] | 193 | NF->takeName(&Fn); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 194 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 195 | // Loop over all of the callers of the function, transforming the call sites |
| 196 | // to pass in a smaller number of arguments into the new function. |
| 197 | // |
| 198 | std::vector<Value*> Args; |
| 199 | while (!Fn.use_empty()) { |
| 200 | CallSite CS = CallSite::get(Fn.use_back()); |
| 201 | Instruction *Call = CS.getInstruction(); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 202 | |
Chris Lattner | a0bc7fc | 2007-10-18 18:49:29 +0000 | [diff] [blame] | 203 | // Pass all the same arguments. |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 204 | Args.assign(CS.arg_begin(), CS.arg_begin()+NumArgs); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 205 | |
Duncan Sands | bfc5ae6 | 2008-01-11 23:13:45 +0000 | [diff] [blame] | 206 | // Drop any attributes that were on the vararg arguments. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 207 | PAListPtr PAL = CS.getParamAttrs(); |
| 208 | if (!PAL.isEmpty() && PAL.getSlot(PAL.getNumSlots() - 1).Index > NumArgs) { |
| 209 | SmallVector<ParamAttrsWithIndex, 8> ParamAttrsVec; |
| 210 | for (unsigned i = 0; PAL.getSlot(i).Index <= NumArgs; ++i) |
| 211 | ParamAttrsVec.push_back(PAL.getSlot(i)); |
| 212 | PAL = PAListPtr::get(ParamAttrsVec.begin(), ParamAttrsVec.end()); |
Duncan Sands | bfc5ae6 | 2008-01-11 23:13:45 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 215 | Instruction *New; |
| 216 | if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 217 | New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(), |
| 218 | Args.begin(), Args.end(), "", Call); |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 219 | cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv()); |
Duncan Sands | bfc5ae6 | 2008-01-11 23:13:45 +0000 | [diff] [blame] | 220 | cast<InvokeInst>(New)->setParamAttrs(PAL); |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 221 | } else { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 222 | New = CallInst::Create(NF, Args.begin(), Args.end(), "", Call); |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 223 | cast<CallInst>(New)->setCallingConv(CS.getCallingConv()); |
Duncan Sands | bfc5ae6 | 2008-01-11 23:13:45 +0000 | [diff] [blame] | 224 | cast<CallInst>(New)->setParamAttrs(PAL); |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 225 | if (cast<CallInst>(Call)->isTailCall()) |
| 226 | cast<CallInst>(New)->setTailCall(); |
| 227 | } |
| 228 | Args.clear(); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 229 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 230 | if (!Call->use_empty()) |
Chris Lattner | a0bc7fc | 2007-10-18 18:49:29 +0000 | [diff] [blame] | 231 | Call->replaceAllUsesWith(New); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 232 | |
Chris Lattner | 046800a | 2007-02-11 01:08:35 +0000 | [diff] [blame] | 233 | New->takeName(Call); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 234 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 235 | // Finally, remove the old call from the program, reducing the use-count of |
| 236 | // F. |
Chris Lattner | a0bc7fc | 2007-10-18 18:49:29 +0000 | [diff] [blame] | 237 | Call->eraseFromParent(); |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 238 | } |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 239 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 240 | // Since we have now created the new function, splice the body of the old |
| 241 | // function right into the new function, leaving the old rotting hulk of the |
| 242 | // function empty. |
| 243 | NF->getBasicBlockList().splice(NF->begin(), Fn.getBasicBlockList()); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 244 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 245 | // Loop over the argument list, transfering uses of the old arguments over to |
| 246 | // the new arguments, also transfering over the names as well. While we're at |
| 247 | // it, remove the dead arguments from the DeadArguments list. |
| 248 | // |
| 249 | for (Function::arg_iterator I = Fn.arg_begin(), E = Fn.arg_end(), |
| 250 | I2 = NF->arg_begin(); I != E; ++I, ++I2) { |
| 251 | // Move the name and users over to the new version. |
| 252 | I->replaceAllUsesWith(I2); |
Chris Lattner | 046800a | 2007-02-11 01:08:35 +0000 | [diff] [blame] | 253 | I2->takeName(I); |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 254 | } |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 255 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 256 | // Finally, nuke the old function. |
| 257 | Fn.eraseFromParent(); |
| 258 | return true; |
| 259 | } |
| 260 | |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 261 | /// Convenience function that returns the number of return values. It returns 0 |
| 262 | /// for void functions and 1 for functions not returning a struct. It returns |
| 263 | /// the number of struct elements for functions returning a struct. |
| 264 | static unsigned NumRetVals(const Function *F) { |
| 265 | if (F->getReturnType() == Type::VoidTy) |
| 266 | return 0; |
| 267 | else if (const StructType *STy = dyn_cast<StructType>(F->getReturnType())) |
| 268 | return STy->getNumElements(); |
| 269 | else |
| 270 | return 1; |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 273 | /// IsMaybeAlive - This checks Use for liveness. If Use is live, returns Live, |
| 274 | /// else returns MaybeLive. Also, adds Use to MaybeLiveUses in the latter case. |
| 275 | DAE::Liveness DAE::IsMaybeLive(RetOrArg Use, UseVector &MaybeLiveUses) { |
| 276 | // We're live if our use is already marked as live |
| 277 | if (LiveValues.count(Use)) |
Chris Lattner | 92044ce | 2006-06-27 21:05:04 +0000 | [diff] [blame] | 278 | return Live; |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 279 | |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 280 | // We're maybe live otherwise, but remember that we must become live if |
| 281 | // Use becomes live. |
| 282 | MaybeLiveUses.push_back(Use); |
| 283 | return MaybeLive; |
Matthijs Kooijman | ca85d65 | 2008-06-18 11:12:53 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Owen Anderson | bb3761c | 2008-06-18 17:32:16 +0000 | [diff] [blame] | 286 | |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 287 | /// SurveyUse - This looks at a single use of an argument or return value |
| 288 | /// and determines if it should be alive or not. Adds this use to MaybeLiveUses |
| 289 | /// if it causes the used value to become MaybeAlive. |
| 290 | /// |
| 291 | /// RetValNum is the return value number to use when this use is used in a |
| 292 | /// return instruction. This is used in the recursion, you should always leave |
| 293 | /// it at 0. |
| 294 | DAE::Liveness DAE::SurveyUse(Value::use_iterator U, UseVector &MaybeLiveUses, unsigned RetValNum) { |
| 295 | Value *V = *U; |
| 296 | if (ReturnInst *RI = dyn_cast<ReturnInst>(V)) { |
| 297 | // The value is returned from another function. It's only live when the |
| 298 | // caller's return value is live |
| 299 | RetOrArg Use = CreateRet(RI->getParent()->getParent(), RetValNum); |
| 300 | // We might be live, depending on the liveness of Use |
| 301 | return IsMaybeLive(Use, MaybeLiveUses); |
| 302 | } |
| 303 | if (InsertValueInst *IV = dyn_cast<InsertValueInst>(V)) { |
| 304 | if (U.getOperandNo() != InsertValueInst::getAggregateOperandIndex() && IV->hasIndices()) |
| 305 | // The use we are examining is inserted into an aggregate. Our liveness |
| 306 | // depends on all uses of that aggregate, but if it is used as a return |
| 307 | // value, only index at which we were inserted counts. |
| 308 | RetValNum = *IV->idx_begin(); |
| 309 | |
| 310 | // Note that if we are used as the aggregate operand to the insertvalue, |
| 311 | // we don't change RetValNum, but do survey all our uses. |
| 312 | |
| 313 | Liveness Result = Dead; |
| 314 | for (Value::use_iterator I = IV->use_begin(), |
| 315 | E = V->use_end(); I != E; ++I) { |
| 316 | Result = SurveyUse(I, MaybeLiveUses, RetValNum); |
| 317 | if (Result == Live) |
| 318 | break; |
| 319 | } |
| 320 | return Result; |
| 321 | } |
| 322 | CallSite CS = CallSite::get(V); |
| 323 | if (CS.getInstruction()) { |
| 324 | Function *F = CS.getCalledFunction(); |
| 325 | if (F) { |
| 326 | // Used in a direct call |
| 327 | |
| 328 | // Check for vararg. Do - 1 to skip the first operand to call (the |
| 329 | // function itself). |
| 330 | if (U.getOperandNo() - 1 >= F->getFunctionType()->getNumParams()) |
| 331 | // The value is passed in through a vararg! Must be live. |
| 332 | return Live; |
| 333 | |
| 334 | // Value passed to a normal call. It's only live when the corresponding |
| 335 | // argument (operand number - 1 to skip the function pointer operand) to |
| 336 | // the called function turns out live |
| 337 | RetOrArg Use = CreateArg(F, U.getOperandNo() - 1); |
| 338 | return IsMaybeLive(Use, MaybeLiveUses); |
| 339 | } else { |
| 340 | // Used in any other way? Value must be live. |
| 341 | return Live; |
| 342 | } |
| 343 | } |
| 344 | // Used in any other way? Value must be live. |
| 345 | return Live; |
| 346 | } |
| 347 | |
| 348 | /// SurveyUses - This looks at all the uses of the given return value |
| 349 | /// (possibly a partial return value from a function returning a struct). |
| 350 | /// Returns the Liveness deduced from the uses of this value. |
| 351 | /// |
| 352 | /// Adds all uses that cause the result to be MaybeLive to MaybeLiveRetUses. |
| 353 | DAE::Liveness DAE::SurveyUses(Value *V, UseVector &MaybeLiveUses) { |
| 354 | // Assume it's dead (which will only hold if there are no uses at all..) |
| 355 | Liveness Result = Dead; |
| 356 | // Check each use |
| 357 | for (Value::use_iterator I = V->use_begin(), |
| 358 | E = V->use_end(); I != E; ++I) { |
| 359 | Result = SurveyUse(I, MaybeLiveUses); |
| 360 | if (Result == Live) |
| 361 | break; |
| 362 | } |
| 363 | return Result; |
| 364 | } |
| 365 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 366 | // SurveyFunction - This performs the initial survey of the specified function, |
| 367 | // checking out whether or not it uses any of its incoming arguments or whether |
| 368 | // any callers use the return value. This fills in the |
| 369 | // (Dead|MaybeLive|Live)(Arguments|RetVal) sets. |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 370 | // |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 371 | // We consider arguments of non-internal functions to be intrinsically alive as |
| 372 | // well as arguments to functions which have their "address taken". |
| 373 | // |
| 374 | void DAE::SurveyFunction(Function &F) { |
| 375 | bool FunctionIntrinsicallyLive = false; |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 376 | unsigned RetCount = NumRetVals(&F); |
| 377 | // Assume all return values are dead |
| 378 | typedef SmallVector<Liveness, 5> RetVals; |
| 379 | RetVals RetValLiveness(RetCount, Dead); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 380 | |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 381 | // These vectors maps each return value to the uses that make it MaybeLive, so |
| 382 | // we can add those to the MaybeLiveRetVals list if the return value |
| 383 | // really turns out to be MaybeLive. Initializes to RetCount empty vectors |
| 384 | typedef SmallVector<UseVector, 5> RetUses; |
| 385 | // Intialized to a list of RetCount empty lists |
| 386 | RetUses MaybeLiveRetUses(RetCount); |
| 387 | |
| 388 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 389 | if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) |
| 390 | if (RI->getNumOperands() != 0 && RI->getOperand(0)->getType() != F.getFunctionType()->getReturnType()) { |
| 391 | // We don't support old style multiple return values |
| 392 | FunctionIntrinsicallyLive = true; |
| 393 | break; |
| 394 | } |
| 395 | |
| 396 | if (!F.hasInternalLinkage() && (!ShouldHackArguments() || F.isIntrinsic())) |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 397 | FunctionIntrinsicallyLive = true; |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 398 | |
| 399 | if (!FunctionIntrinsicallyLive) { |
| 400 | DOUT << "DAE - Inspecting callers for fn: " << F.getName() << "\n"; |
| 401 | // Keep track of the number of live retvals, so we can skip checks once all |
| 402 | // of them turn out to be live. |
| 403 | unsigned NumLiveRetVals = 0; |
| 404 | const Type *STy = dyn_cast<StructType>(F.getReturnType()); |
| 405 | // Loop all uses of the function |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 406 | for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I) { |
Matthijs Kooijman | 4133541 | 2008-06-05 08:34:25 +0000 | [diff] [blame] | 407 | // If the function is PASSED IN as an argument, its address has been taken |
| 408 | if (I.getOperandNo() != 0) { |
| 409 | FunctionIntrinsicallyLive = true; |
| 410 | break; |
| 411 | } |
| 412 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 413 | // If this use is anything other than a call site, the function is alive. |
| 414 | CallSite CS = CallSite::get(*I); |
| 415 | Instruction *TheCall = CS.getInstruction(); |
| 416 | if (!TheCall) { // Not a direct call site? |
| 417 | FunctionIntrinsicallyLive = true; |
| 418 | break; |
| 419 | } |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 420 | |
| 421 | // If we end up here, we are looking at a direct call to our function. |
| 422 | |
| 423 | // Now, check how our return value(s) is/are used in this caller. Don't |
| 424 | // bother checking return values if all of them are live already |
| 425 | if (NumLiveRetVals != RetCount) { |
| 426 | if (STy) { |
| 427 | // Check all uses of the return value |
| 428 | for (Value::use_iterator I = TheCall->use_begin(), |
| 429 | E = TheCall->use_end(); I != E; ++I) { |
| 430 | ExtractValueInst *Ext = dyn_cast<ExtractValueInst>(*I); |
| 431 | if (Ext && Ext->hasIndices()) { |
| 432 | // This use uses a part of our return value, survey the uses of that |
| 433 | // part and store the results for this index only. |
| 434 | unsigned Idx = *Ext->idx_begin(); |
| 435 | if (RetValLiveness[Idx] != Live) { |
| 436 | RetValLiveness[Idx] = SurveyUses(Ext, MaybeLiveRetUses[Idx]); |
| 437 | if (RetValLiveness[Idx] == Live) |
| 438 | NumLiveRetVals++; |
| 439 | } |
Owen Anderson | bb3761c | 2008-06-18 17:32:16 +0000 | [diff] [blame] | 440 | } else { |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 441 | // Used by something else than extractvalue. Mark all |
| 442 | // return values as live. |
| 443 | for (unsigned i = 0; i != RetCount; ++i ) |
| 444 | RetValLiveness[i] = Live; |
| 445 | NumLiveRetVals = RetCount; |
| 446 | break; |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 447 | } |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 448 | } |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 449 | } else { |
| 450 | // Single return value |
| 451 | RetValLiveness[0] = SurveyUses(TheCall, MaybeLiveRetUses[0]); |
| 452 | if (RetValLiveness[0] == Live) |
| 453 | NumLiveRetVals = RetCount; |
| 454 | } |
| 455 | } |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 456 | } |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 457 | } |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 458 | if (FunctionIntrinsicallyLive) { |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 459 | DOUT << "DAE - Intrinsically live fn: " << F.getName() << "\n"; |
| 460 | // Mark all arguments as live |
| 461 | unsigned i = 0; |
Chris Lattner | 19bdc03 | 2005-05-06 05:34:40 +0000 | [diff] [blame] | 462 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 463 | AI != E; ++AI, ++i) |
| 464 | MarkLive(CreateArg(&F, i)); |
| 465 | // Mark all return values as live |
| 466 | i = 0; |
| 467 | for (unsigned i = 0, e = RetValLiveness.size(); i != e; ++i) |
| 468 | MarkLive(CreateRet(&F, i)); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 469 | return; |
| 470 | } |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 471 | |
| 472 | // Now we've inspected all callers, record the liveness of our return values. |
| 473 | for (unsigned i = 0, e = RetValLiveness.size(); i != e; ++i) { |
| 474 | RetOrArg Ret = CreateRet(&F, i); |
| 475 | // Mark the result down |
| 476 | MarkValue(Ret, RetValLiveness[i], MaybeLiveRetUses[i]); |
Matthijs Kooijman | ca85d65 | 2008-06-18 11:12:53 +0000 | [diff] [blame] | 477 | } |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 478 | DOUT << "DAE - Inspecting args for fn: " << F.getName() << "\n"; |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 479 | |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 480 | // Now, check all of our arguments |
| 481 | unsigned i = 0; |
| 482 | UseVector MaybeLiveArgUses; |
| 483 | for (Function::arg_iterator AI = F.arg_begin(), |
| 484 | E = F.arg_end(); AI != E; ++AI, ++i) { |
| 485 | // See what the effect of this use is (recording any uses that cause |
| 486 | // MaybeLive in MaybeLiveArgUses) |
| 487 | Liveness Result = SurveyUses(AI, MaybeLiveArgUses); |
| 488 | RetOrArg Arg = CreateArg(&F, i); |
| 489 | // Mark the result down |
| 490 | MarkValue(Arg, Result, MaybeLiveArgUses); |
| 491 | // Clear the vector again for the next iteration |
| 492 | MaybeLiveArgUses.clear(); |
| 493 | } |
| 494 | } |
Owen Anderson | bb3761c | 2008-06-18 17:32:16 +0000 | [diff] [blame] | 495 | |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 496 | /// MarkValue - This function marks the liveness of RA depending on L. If L is |
| 497 | /// MaybeLive, it also records any uses in MaybeLiveUses such that RA will be |
| 498 | /// marked live if any use in MaybeLiveUses gets marked live later on. |
| 499 | void DAE::MarkValue(const RetOrArg &RA, Liveness L, const UseVector &MaybeLiveUses) { |
| 500 | switch (L) { |
| 501 | case Live: MarkLive(RA); break; |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 502 | case MaybeLive: |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 503 | { |
| 504 | // Note any uses of this value, so this return value can be |
| 505 | // marked live whenever one of the uses becomes live. |
| 506 | UseMap::iterator Where = Uses.begin(); |
| 507 | for (UseVector::const_iterator UI = MaybeLiveUses.begin(), |
| 508 | UE = MaybeLiveUses.end(); UI != UE; ++UI) |
| 509 | Where = Uses.insert(Where, UseMap::value_type(*UI, RA)); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 510 | break; |
| 511 | } |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 512 | case Dead: break; |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 516 | /// MarkLive - Mark the given return value or argument as live. Additionally, |
| 517 | /// mark any values that are used by this value (according to Uses) live as |
| 518 | /// well. |
| 519 | void DAE::MarkLive(RetOrArg RA) { |
| 520 | if (!LiveValues.insert(RA).second) |
| 521 | return; // We were already marked Live |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 522 | |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 523 | if (RA.IsArg) |
| 524 | DOUT << "DAE - Marking argument " << RA.Idx << " to function " << RA.F->getNameStart() << " live\n"; |
| 525 | else |
| 526 | DOUT << "DAE - Marking return value " << RA.Idx << " of function " << RA.F->getNameStart() << " live\n"; |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 527 | |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 528 | // We don't use upper_bound (or equal_range) here, because our recursive call |
| 529 | // to ourselves is likely to mark the upper_bound (which is the first value |
| 530 | // not belonging to RA) to become erased and the iterator invalidated. |
| 531 | UseMap::iterator Begin = Uses.lower_bound(RA); |
| 532 | UseMap::iterator E = Uses.end(); |
| 533 | UseMap::iterator I; |
| 534 | for (I = Begin; I != E && I->first == RA; ++I) |
| 535 | MarkLive(I->second); |
Owen Anderson | bb3761c | 2008-06-18 17:32:16 +0000 | [diff] [blame] | 536 | |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 537 | // Erase RA from the Uses map (from the lower bound to wherever we ended up |
| 538 | // after the loop). |
| 539 | Uses.erase(Begin, I); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 542 | // RemoveDeadStuffFromFunction - Remove any arguments and return values from F |
| 543 | // that are not in LiveValues. This function is a noop for any Function created |
| 544 | // by this function before, or any function that was not inspected for liveness. |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 545 | // specified by the DeadArguments list. Transform the function and all of the |
| 546 | // callees of the function to not have these arguments. |
| 547 | // |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 548 | bool DAE::RemoveDeadStuffFromFunction(Function *F) { |
| 549 | // Quick exit path for external functions |
| 550 | if (!F->hasInternalLinkage() && (!ShouldHackArguments() || F->isIntrinsic())) |
| 551 | return false; |
| 552 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 553 | // Start by computing a new prototype for the function, which is the same as |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 554 | // the old function, but has fewer arguments and a different return type. |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 555 | const FunctionType *FTy = F->getFunctionType(); |
| 556 | std::vector<const Type*> Params; |
| 557 | |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 558 | // Set up to build a new list of parameter attributes |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 559 | SmallVector<ParamAttrsWithIndex, 8> ParamAttrsVec; |
| 560 | const PAListPtr &PAL = F->getParamAttrs(); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 561 | |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 562 | // The existing function return attributes. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 563 | ParameterAttributes RAttrs = PAL.getParamAttrs(0); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 564 | |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 565 | |
| 566 | // Find out the new return value |
| 567 | |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 568 | const Type *RetTy = FTy->getReturnType(); |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 569 | const Type *NRetTy; |
| 570 | unsigned RetCount = NumRetVals(F); |
Matthijs Kooijman | 9cb6ec2 | 2008-06-20 14:28:52 +0000 | [diff] [blame^] | 571 | // Explicitely track if anything changed, for debugging |
| 572 | bool Changed = false; |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 573 | // -1 means unused, other numbers are the new index |
| 574 | SmallVector<int, 5> NewRetIdxs(RetCount, -1); |
| 575 | std::vector<const Type*> RetTypes; |
| 576 | if (RetTy != Type::VoidTy) { |
| 577 | const StructType *STy = dyn_cast<StructType>(RetTy); |
| 578 | if (STy) |
| 579 | // Look at each of the original return values individually |
| 580 | for (unsigned i = 0; i != RetCount; ++i) { |
| 581 | RetOrArg Ret = CreateRet(F, i); |
| 582 | if (LiveValues.erase(Ret)) { |
| 583 | RetTypes.push_back(STy->getElementType(i)); |
| 584 | NewRetIdxs[i] = RetTypes.size() - 1; |
| 585 | } else { |
| 586 | ++NumRetValsEliminated; |
Matthijs Kooijman | 9cb6ec2 | 2008-06-20 14:28:52 +0000 | [diff] [blame^] | 587 | DOUT << "DAE - Removing return value " << i << " from " << F->getNameStart() << "\n"; |
| 588 | Changed = true; |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | else |
| 592 | // We used to return a single value |
| 593 | if (LiveValues.erase(CreateRet(F, 0))) { |
| 594 | RetTypes.push_back(RetTy); |
| 595 | NewRetIdxs[0] = 0; |
| 596 | } else { |
| 597 | DOUT << "DAE - Removing return value from " << F->getNameStart() << "\n"; |
| 598 | ++NumRetValsEliminated; |
Matthijs Kooijman | 9cb6ec2 | 2008-06-20 14:28:52 +0000 | [diff] [blame^] | 599 | Changed = true; |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 600 | } |
| 601 | if (RetTypes.size() == 0) |
| 602 | // No return types? Make it void |
| 603 | NRetTy = Type::VoidTy; |
| 604 | else if (RetTypes.size() == 1) |
| 605 | // One return type? Just a simple value then |
| 606 | NRetTy = RetTypes.front(); |
| 607 | else |
| 608 | // More return types? Return a struct with them |
| 609 | NRetTy = StructType::get(RetTypes); |
| 610 | } else { |
| 611 | NRetTy = Type::VoidTy; |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 612 | } |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 613 | |
| 614 | // Remove any incompatible attributes |
| 615 | RAttrs &= ~ParamAttr::typeIncompatible(NRetTy); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 616 | if (RAttrs) |
| 617 | ParamAttrsVec.push_back(ParamAttrsWithIndex::get(0, RAttrs)); |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 618 | |
| 619 | // Remember which arguments are still alive |
| 620 | SmallVector<bool, 10> ArgAlive(FTy->getNumParams(), false); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 621 | // Construct the new parameter list from non-dead arguments. Also construct |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 622 | // a new set of parameter attributes to correspond. Skip the first parameter |
| 623 | // attribute, since that belongs to the return value. |
| 624 | unsigned i = 0; |
| 625 | for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 626 | I != E; ++I, ++i) { |
| 627 | RetOrArg Arg = CreateArg(F, i); |
| 628 | if (LiveValues.erase(Arg)) { |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 629 | Params.push_back(I->getType()); |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 630 | ArgAlive[i] = true; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 631 | |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 632 | // Get the original parameter attributes (skipping the first one, that is |
| 633 | // for the return value |
| 634 | if (ParameterAttributes Attrs = PAL.getParamAttrs(i + 1)) |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 635 | ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Params.size(), Attrs)); |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 636 | } else { |
| 637 | ++NumArgumentsEliminated; |
| 638 | DOUT << "DAE - Removing argument " << i << " (" << I->getNameStart() << ") from " << F->getNameStart() << "\n"; |
Matthijs Kooijman | 9cb6ec2 | 2008-06-20 14:28:52 +0000 | [diff] [blame^] | 639 | Changed = true; |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 640 | } |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 641 | } |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 642 | |
| 643 | // Reconstruct the ParamAttrsList based on the vector we constructed. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 644 | PAListPtr NewPAL = PAListPtr::get(ParamAttrsVec.begin(), ParamAttrsVec.end()); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 645 | |
Chris Lattner | ff5bf9c | 2003-10-23 17:44:53 +0000 | [diff] [blame] | 646 | // Work around LLVM bug PR56: the CWriter cannot emit varargs functions which |
| 647 | // have zero fixed arguments. |
| 648 | // |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 649 | // Not that we apply this hack for a vararg fuction that does not have any |
| 650 | // arguments anymore, but did have them before (so don't bother fixing |
| 651 | // functions that were already broken wrt CWriter). |
Chris Lattner | ff5bf9c | 2003-10-23 17:44:53 +0000 | [diff] [blame] | 652 | bool ExtraArgHack = false; |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 653 | if (Params.empty() && FTy->isVarArg() && FTy->getNumParams() != 0) { |
Chris Lattner | ff5bf9c | 2003-10-23 17:44:53 +0000 | [diff] [blame] | 654 | ExtraArgHack = true; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 655 | Params.push_back(Type::Int32Ty); |
Chris Lattner | ff5bf9c | 2003-10-23 17:44:53 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 658 | // Create the new function type based on the recomputed parameters. |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 659 | FunctionType *NFTy = FunctionType::get(NRetTy, Params, FTy->isVarArg()); |
| 660 | |
| 661 | // No change? |
| 662 | if (NFTy == FTy) |
| 663 | return false; |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 664 | |
Matthijs Kooijman | 9cb6ec2 | 2008-06-20 14:28:52 +0000 | [diff] [blame^] | 665 | // The function type is only allowed to be different if we actually left out |
| 666 | // an argument or return value |
| 667 | assert(Changed && "Function type changed while no arguments or retrurn values were removed!"); |
| 668 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 669 | // Create the new function body and insert it into the module... |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 670 | Function *NF = Function::Create(NFTy, F->getLinkage()); |
Duncan Sands | 28c3cff | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 671 | NF->copyAttributesFrom(F); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 672 | NF->setParamAttrs(NewPAL); |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 673 | // Insert the new function before the old function, so we won't be processing |
| 674 | // it again |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 675 | F->getParent()->getFunctionList().insert(F, NF); |
Chris Lattner | 046800a | 2007-02-11 01:08:35 +0000 | [diff] [blame] | 676 | NF->takeName(F); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 677 | |
| 678 | // Loop over all of the callers of the function, transforming the call sites |
| 679 | // to pass in a smaller number of arguments into the new function. |
| 680 | // |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 681 | std::vector<Value*> Args; |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 682 | while (!F->use_empty()) { |
| 683 | CallSite CS = CallSite::get(F->use_back()); |
| 684 | Instruction *Call = CS.getInstruction(); |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 685 | |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 686 | ParamAttrsVec.clear(); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 687 | const PAListPtr &CallPAL = CS.getParamAttrs(); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 688 | |
| 689 | // The call return attributes. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 690 | ParameterAttributes RAttrs = CallPAL.getParamAttrs(0); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 691 | // Adjust in case the function was changed to return void. |
Duncan Sands | 6c3470e | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 692 | RAttrs &= ~ParamAttr::typeIncompatible(NF->getReturnType()); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 693 | if (RAttrs) |
| 694 | ParamAttrsVec.push_back(ParamAttrsWithIndex::get(0, RAttrs)); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 695 | |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 696 | // Declare these outside of the loops, so we can reuse them for the second |
| 697 | // loop, which loops the varargs |
| 698 | CallSite::arg_iterator I = CS.arg_begin(); |
| 699 | unsigned i = 0; |
| 700 | // Loop over those operands, corresponding to the normal arguments to the |
| 701 | // original function, and add those that are still alive. |
| 702 | for (unsigned e = FTy->getNumParams(); i != e; ++I, ++i) |
| 703 | if (ArgAlive[i]) { |
| 704 | Args.push_back(*I); |
| 705 | // Get original parameter attributes, but skip return attributes |
| 706 | if (ParameterAttributes Attrs = CallPAL.getParamAttrs(i + 1)) |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 707 | ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Args.size(), Attrs)); |
| 708 | } |
| 709 | |
Chris Lattner | ff5bf9c | 2003-10-23 17:44:53 +0000 | [diff] [blame] | 710 | if (ExtraArgHack) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 711 | Args.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | ff5bf9c | 2003-10-23 17:44:53 +0000 | [diff] [blame] | 712 | |
Evan Cheng | b2fc2a3 | 2008-01-17 04:18:54 +0000 | [diff] [blame] | 713 | // Push any varargs arguments on the list. Don't forget their attributes. |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 714 | for (CallSite::arg_iterator E = CS.arg_end(); I != E; ++I, ++i) { |
| 715 | Args.push_back(*I); |
| 716 | if (ParameterAttributes Attrs = CallPAL.getParamAttrs(i + 1)) |
Evan Cheng | b2fc2a3 | 2008-01-17 04:18:54 +0000 | [diff] [blame] | 717 | ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Args.size(), Attrs)); |
| 718 | } |
| 719 | |
| 720 | // Reconstruct the ParamAttrsList based on the vector we constructed. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 721 | PAListPtr NewCallPAL = PAListPtr::get(ParamAttrsVec.begin(), |
| 722 | ParamAttrsVec.end()); |
Chris Lattner | ff5bf9c | 2003-10-23 17:44:53 +0000 | [diff] [blame] | 723 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 724 | Instruction *New; |
| 725 | if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 726 | New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(), |
| 727 | Args.begin(), Args.end(), "", Call); |
Chris Lattner | f201dbc | 2005-05-09 01:05:50 +0000 | [diff] [blame] | 728 | cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv()); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 729 | cast<InvokeInst>(New)->setParamAttrs(NewCallPAL); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 730 | } else { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 731 | New = CallInst::Create(NF, Args.begin(), Args.end(), "", Call); |
Chris Lattner | f201dbc | 2005-05-09 01:05:50 +0000 | [diff] [blame] | 732 | cast<CallInst>(New)->setCallingConv(CS.getCallingConv()); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 733 | cast<CallInst>(New)->setParamAttrs(NewCallPAL); |
Chris Lattner | 1430ef1 | 2005-05-06 06:46:58 +0000 | [diff] [blame] | 734 | if (cast<CallInst>(Call)->isTailCall()) |
| 735 | cast<CallInst>(New)->setTailCall(); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 736 | } |
| 737 | Args.clear(); |
| 738 | |
| 739 | if (!Call->use_empty()) { |
| 740 | if (New->getType() == Type::VoidTy) |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 741 | // Our return value was unused, replace by null for now, uses will get |
| 742 | // removed later on |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 743 | Call->replaceAllUsesWith(Constant::getNullValue(Call->getType())); |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 744 | else if (isa<StructType>(RetTy)) { |
| 745 | // The original return value was a struct, update all uses (which are |
| 746 | // all extractvalue instructions). |
| 747 | for (Value::use_iterator I = Call->use_begin(), E = Call->use_end(); |
| 748 | I != E;) { |
| 749 | assert(isa<ExtractValueInst>(*I) && "Return value not only used by extractvalue?"); |
| 750 | ExtractValueInst *EV = cast<ExtractValueInst>(*I); |
| 751 | // Increment now, since we're about to throw away this use. |
| 752 | ++I; |
| 753 | assert(EV->hasIndices() && "Return value used by extractvalue without indices?"); |
| 754 | unsigned Idx = *EV->idx_begin(); |
| 755 | if (NewRetIdxs[Idx] != -1) { |
| 756 | if (RetTypes.size() > 1) { |
| 757 | // We're still returning a struct, create a new extractvalue |
| 758 | // instruction with the first index updated |
| 759 | std::vector<unsigned> NewIdxs(EV->idx_begin(), EV->idx_end()); |
| 760 | NewIdxs[0] = NewRetIdxs[Idx]; |
| 761 | Value *NEV = ExtractValueInst::Create(New, NewIdxs.begin(), NewIdxs.end(), "retval", EV); |
| 762 | EV->replaceAllUsesWith(NEV); |
| 763 | EV->eraseFromParent(); |
| 764 | } else { |
| 765 | // We are now only returning a simple value, remove the |
| 766 | // extractvalue |
| 767 | EV->replaceAllUsesWith(New); |
| 768 | EV->eraseFromParent(); |
| 769 | } |
| 770 | } else { |
| 771 | // Value unused, replace uses by null for now, they will get removed |
| 772 | // later on |
| 773 | EV->replaceAllUsesWith(Constant::getNullValue(EV->getType())); |
| 774 | EV->eraseFromParent(); |
| 775 | } |
| 776 | } |
| 777 | New->takeName(Call); |
| 778 | } else { |
| 779 | // The original function had a single return value |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 780 | Call->replaceAllUsesWith(New); |
Chris Lattner | 046800a | 2007-02-11 01:08:35 +0000 | [diff] [blame] | 781 | New->takeName(Call); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 782 | } |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 783 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 784 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 785 | // Finally, remove the old call from the program, reducing the use-count of |
| 786 | // F. |
Matthijs Kooijman | 494661c | 2008-05-30 12:35:46 +0000 | [diff] [blame] | 787 | Call->eraseFromParent(); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | // Since we have now created the new function, splice the body of the old |
| 791 | // function right into the new function, leaving the old rotting hulk of the |
| 792 | // function empty. |
| 793 | NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList()); |
| 794 | |
| 795 | // Loop over the argument list, transfering uses of the old arguments over to |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 796 | // the new arguments, also transfering over the names as well. |
| 797 | i = 0; |
Chris Lattner | 19bdc03 | 2005-05-06 05:34:40 +0000 | [diff] [blame] | 798 | for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(), |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 799 | I2 = NF->arg_begin(); I != E; ++I, ++i) |
| 800 | if (ArgAlive[i]) { |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 801 | // If this is a live argument, move the name and users over to the new |
| 802 | // version. |
| 803 | I->replaceAllUsesWith(I2); |
Chris Lattner | 046800a | 2007-02-11 01:08:35 +0000 | [diff] [blame] | 804 | I2->takeName(I); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 805 | ++I2; |
| 806 | } else { |
| 807 | // If this argument is dead, replace any uses of it with null constants |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 808 | // (these are guaranteed to become unused later on) |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 809 | I->replaceAllUsesWith(Constant::getNullValue(I->getType())); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 810 | } |
| 811 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 812 | // If we change the return value of the function we must rewrite any return |
| 813 | // instructions. Check this now. |
| 814 | if (F->getReturnType() != NF->getReturnType()) |
| 815 | for (Function::iterator BB = NF->begin(), E = NF->end(); BB != E; ++BB) |
| 816 | if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) { |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 817 | Value *RetVal; |
| 818 | |
| 819 | if (NFTy->getReturnType() == Type::VoidTy) { |
| 820 | RetVal = 0; |
| 821 | } else { |
| 822 | assert (isa<StructType>(RetTy)); |
| 823 | // The original return value was a struct, insert |
| 824 | // extractvalue/insertvalue chains to extract only the values we need |
| 825 | // to return and insert them into our new result. |
| 826 | // This does generate messy code, but we'll let it to instcombine to |
| 827 | // clean that up |
| 828 | Value *OldRet = RI->getOperand(0); |
| 829 | // Start out building up our return value from undef |
| 830 | RetVal = llvm::UndefValue::get(NRetTy); |
| 831 | for (unsigned i = 0; i != RetCount; ++i) |
| 832 | if (NewRetIdxs[i] != -1) { |
| 833 | ExtractValueInst *EV = ExtractValueInst::Create(OldRet, i, "newret", RI); |
| 834 | if (RetTypes.size() > 1) { |
| 835 | // We're still returning a struct, so reinsert the value into |
| 836 | // our new return value at the new index |
| 837 | |
| 838 | RetVal = InsertValueInst::Create(RetVal, EV, NewRetIdxs[i], "oldret"); |
| 839 | } else { |
| 840 | // We are now only returning a simple value, so just return the |
| 841 | // extracted value |
| 842 | RetVal = EV; |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | // Replace the return instruction with one returning the new return |
| 847 | // value (possibly 0 if we became void). |
| 848 | ReturnInst::Create(RetVal, RI); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 849 | BB->getInstList().erase(RI); |
| 850 | } |
| 851 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 852 | // Now that the old function is dead, delete it. |
Matthijs Kooijman | 494661c | 2008-05-30 12:35:46 +0000 | [diff] [blame] | 853 | F->eraseFromParent(); |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 854 | |
| 855 | return true; |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 858 | bool DAE::runOnModule(Module &M) { |
Chris Lattner | 701bc42 | 2007-11-15 06:10:55 +0000 | [diff] [blame] | 859 | bool Changed = false; |
| 860 | // First pass: Do a simple check to see if any functions can have their "..." |
| 861 | // removed. We can do this if they never call va_start. This loop cannot be |
| 862 | // fused with the next loop, because deleting a function invalidates |
| 863 | // information computed while surveying other functions. |
| 864 | DOUT << "DAE - Deleting dead varargs\n"; |
| 865 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ) { |
| 866 | Function &F = *I++; |
| 867 | if (F.getFunctionType()->isVarArg()) |
| 868 | Changed |= DeleteDeadVarargs(F); |
| 869 | } |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 870 | |
Chris Lattner | 701bc42 | 2007-11-15 06:10:55 +0000 | [diff] [blame] | 871 | // Second phase:loop through the module, determining which arguments are live. |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 872 | // We assume all arguments are dead unless proven otherwise (allowing us to |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 873 | // determine that dead arguments passed into recursive functions are dead). |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 874 | // |
Bill Wendling | 0a81aac | 2006-11-26 10:02:32 +0000 | [diff] [blame] | 875 | DOUT << "DAE - Determining liveness\n"; |
Chris Lattner | 701bc42 | 2007-11-15 06:10:55 +0000 | [diff] [blame] | 876 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 877 | SurveyFunction(*I); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 878 | |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 879 | // Now, remove all dead arguments and return values from each function in |
| 880 | // turn |
| 881 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ) { |
| 882 | // Increment now, because the function will probably get removed (ie |
| 883 | // replaced by a new one) |
| 884 | Function *F = I++; |
| 885 | Changed |= RemoveDeadStuffFromFunction(F); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Matthijs Kooijman | c95e44b | 2008-06-20 09:36:16 +0000 | [diff] [blame] | 888 | return Changed; |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 889 | } |