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