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