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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source 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 |
| 13 | // pass also deletes dead arguments 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 |
| 16 | // interprocedural passes, which add possibly-dead arguments. |
| 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" |
| 22 | #include "llvm/Module.h" |
| 23 | #include "llvm/Pass.h" |
| 24 | #include "llvm/DerivedTypes.h" |
| 25 | #include "llvm/Constant.h" |
Misha Brukman | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 26 | #include "llvm/Instructions.h" |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CallSite.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Debug.h" |
| 29 | #include "llvm/ADT/Statistic.h" |
| 30 | #include "llvm/ADT/iterator" |
Chris Lattner | dac58ad | 2006-01-22 23:32:06 +0000 | [diff] [blame^] | 31 | #include <iostream> |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 32 | #include <set> |
Chris Lattner | 1e2385b | 2003-11-21 21:54:22 +0000 | [diff] [blame] | 33 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 35 | namespace { |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 36 | Statistic<> NumArgumentsEliminated("deadargelim", |
| 37 | "Number of unread args removed"); |
| 38 | Statistic<> NumRetValsEliminated("deadargelim", |
| 39 | "Number of unused return values removed"); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 40 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 41 | /// DAE - The dead argument elimination pass. |
| 42 | /// |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 43 | class DAE : public ModulePass { |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 44 | /// Liveness enum - During our initial pass over the program, we determine |
| 45 | /// that things are either definately alive, definately dead, or in need of |
| 46 | /// interprocedural analysis (MaybeLive). |
| 47 | /// |
| 48 | enum Liveness { Live, MaybeLive, Dead }; |
| 49 | |
| 50 | /// LiveArguments, MaybeLiveArguments, DeadArguments - These sets contain |
| 51 | /// all of the arguments in the program. The Dead set contains arguments |
| 52 | /// which are completely dead (never used in the function). The MaybeLive |
| 53 | /// set contains arguments which are only passed into other function calls, |
| 54 | /// thus may be live and may be dead. The Live set contains arguments which |
| 55 | /// are known to be alive. |
| 56 | /// |
| 57 | std::set<Argument*> DeadArguments, MaybeLiveArguments, LiveArguments; |
| 58 | |
| 59 | /// DeadRetVal, MaybeLiveRetVal, LifeRetVal - These sets contain all of the |
| 60 | /// functions in the program. The Dead set contains functions whose return |
| 61 | /// value is known to be dead. The MaybeLive set contains functions whose |
| 62 | /// return values are only used by return instructions, and the Live set |
| 63 | /// contains functions whose return values are used, functions that are |
| 64 | /// external, and functions that already return void. |
| 65 | /// |
| 66 | std::set<Function*> DeadRetVal, MaybeLiveRetVal, LiveRetVal; |
| 67 | |
| 68 | /// InstructionsToInspect - As we mark arguments and return values |
| 69 | /// MaybeLive, we keep track of which instructions could make the values |
| 70 | /// live here. Once the entire program has had the return value and |
| 71 | /// arguments analyzed, this set is scanned to promote the MaybeLive objects |
| 72 | /// to be Live if they really are used. |
| 73 | std::vector<Instruction*> InstructionsToInspect; |
| 74 | |
| 75 | /// CallSites - Keep track of the call sites of functions that have |
| 76 | /// MaybeLive arguments or return values. |
| 77 | std::multimap<Function*, CallSite> CallSites; |
| 78 | |
| 79 | public: |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 80 | bool runOnModule(Module &M); |
Chris Lattner | 9b2a14b | 2003-06-25 04:12:49 +0000 | [diff] [blame] | 81 | |
Chris Lattner | fdcc3ac | 2003-11-05 21:43:02 +0000 | [diff] [blame] | 82 | virtual bool ShouldHackArguments() const { return false; } |
| 83 | |
Chris Lattner | 9b2a14b | 2003-06-25 04:12:49 +0000 | [diff] [blame] | 84 | private: |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 85 | Liveness getArgumentLiveness(const Argument &A); |
| 86 | bool isMaybeLiveArgumentNowLive(Argument *Arg); |
| 87 | |
| 88 | void SurveyFunction(Function &Fn); |
| 89 | |
| 90 | void MarkArgumentLive(Argument *Arg); |
| 91 | void MarkRetValLive(Function *F); |
| 92 | void MarkReturnInstArgumentLive(ReturnInst *RI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 93 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 94 | void RemoveDeadArgumentsFromFunction(Function *F); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 95 | }; |
| 96 | RegisterOpt<DAE> X("deadargelim", "Dead Argument Elimination"); |
Chris Lattner | fdcc3ac | 2003-11-05 21:43:02 +0000 | [diff] [blame] | 97 | |
| 98 | /// DAH - DeadArgumentHacking pass - Same as dead argument elimination, but |
| 99 | /// deletes arguments to functions which are external. This is only for use |
| 100 | /// by bugpoint. |
| 101 | struct DAH : public DAE { |
| 102 | virtual bool ShouldHackArguments() const { return true; } |
| 103 | }; |
Chris Lattner | b6e0631 | 2003-11-05 21:53:41 +0000 | [diff] [blame] | 104 | RegisterPass<DAH> Y("deadarghaX0r", |
Brian Gaeke | 09ca411 | 2004-02-02 19:32:27 +0000 | [diff] [blame] | 105 | "Dead Argument Hacking (BUGPOINT USE ONLY; DO NOT USE)"); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Chris Lattner | 9b2a14b | 2003-06-25 04:12:49 +0000 | [diff] [blame] | 108 | /// createDeadArgEliminationPass - This pass removes arguments from functions |
Chris Lattner | fdcc3ac | 2003-11-05 21:43:02 +0000 | [diff] [blame] | 109 | /// which are not used by the body of the function. |
Chris Lattner | 9b2a14b | 2003-06-25 04:12:49 +0000 | [diff] [blame] | 110 | /// |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 111 | ModulePass *llvm::createDeadArgEliminationPass() { return new DAE(); } |
| 112 | ModulePass *llvm::createDeadArgHackingPass() { return new DAH(); } |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 113 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 114 | static inline bool CallPassesValueThoughVararg(Instruction *Call, |
| 115 | const Value *Arg) { |
| 116 | CallSite CS = CallSite::get(Call); |
| 117 | const Type *CalledValueTy = CS.getCalledValue()->getType(); |
| 118 | const Type *FTy = cast<PointerType>(CalledValueTy)->getElementType(); |
| 119 | unsigned NumFixedArgs = cast<FunctionType>(FTy)->getNumParams(); |
| 120 | for (CallSite::arg_iterator AI = CS.arg_begin()+NumFixedArgs; |
| 121 | AI != CS.arg_end(); ++AI) |
| 122 | if (AI->get() == Arg) |
| 123 | return true; |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 124 | return false; |
| 125 | } |
| 126 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 127 | // getArgumentLiveness - Inspect an argument, determining if is known Live |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 128 | // (used in a computation), MaybeLive (only passed as an argument to a call), or |
| 129 | // Dead (not used). |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 130 | DAE::Liveness DAE::getArgumentLiveness(const Argument &A) { |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 131 | if (A.use_empty()) return Dead; // First check, directly dead? |
| 132 | |
| 133 | // Scan through all of the uses, looking for non-argument passing uses. |
| 134 | for (Value::use_const_iterator I = A.use_begin(), E = A.use_end(); I!=E;++I) { |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 135 | // Return instructions do not immediately effect liveness. |
| 136 | if (isa<ReturnInst>(*I)) |
| 137 | continue; |
| 138 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 139 | CallSite CS = CallSite::get(const_cast<User*>(*I)); |
| 140 | if (!CS.getInstruction()) { |
| 141 | // If its used by something that is not a call or invoke, it's alive! |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 142 | return Live; |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 143 | } |
| 144 | // If it's an indirect call, mark it alive... |
| 145 | Function *Callee = CS.getCalledFunction(); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 146 | if (!Callee) return Live; |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 147 | |
Chris Lattner | 97f4b66 | 2003-06-18 16:25:51 +0000 | [diff] [blame] | 148 | // Check to see if it's passed through a va_arg area: if so, we cannot |
| 149 | // remove it. |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 150 | if (CallPassesValueThoughVararg(CS.getInstruction(), &A)) |
| 151 | return Live; // If passed through va_arg area, we cannot remove it |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | return MaybeLive; // It must be used, but only as argument to a function |
| 155 | } |
| 156 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 157 | |
| 158 | // SurveyFunction - This performs the initial survey of the specified function, |
| 159 | // checking out whether or not it uses any of its incoming arguments or whether |
| 160 | // any callers use the return value. This fills in the |
| 161 | // (Dead|MaybeLive|Live)(Arguments|RetVal) sets. |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 162 | // |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 163 | // We consider arguments of non-internal functions to be intrinsically alive as |
| 164 | // well as arguments to functions which have their "address taken". |
| 165 | // |
| 166 | void DAE::SurveyFunction(Function &F) { |
| 167 | bool FunctionIntrinsicallyLive = false; |
| 168 | Liveness RetValLiveness = F.getReturnType() == Type::VoidTy ? Live : Dead; |
| 169 | |
Chris Lattner | b6e0631 | 2003-11-05 21:53:41 +0000 | [diff] [blame] | 170 | if (!F.hasInternalLinkage() && |
| 171 | (!ShouldHackArguments() || F.getIntrinsicID())) |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 172 | FunctionIntrinsicallyLive = true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 173 | else |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 174 | for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I) { |
| 175 | // If this use is anything other than a call site, the function is alive. |
| 176 | CallSite CS = CallSite::get(*I); |
| 177 | Instruction *TheCall = CS.getInstruction(); |
| 178 | if (!TheCall) { // Not a direct call site? |
| 179 | FunctionIntrinsicallyLive = true; |
| 180 | break; |
| 181 | } |
| 182 | |
| 183 | // Check to see if the return value is used... |
| 184 | if (RetValLiveness != Live) |
| 185 | for (Value::use_iterator I = TheCall->use_begin(), |
| 186 | E = TheCall->use_end(); I != E; ++I) |
| 187 | if (isa<ReturnInst>(cast<Instruction>(*I))) { |
| 188 | RetValLiveness = MaybeLive; |
| 189 | } else if (isa<CallInst>(cast<Instruction>(*I)) || |
| 190 | isa<InvokeInst>(cast<Instruction>(*I))) { |
| 191 | if (CallPassesValueThoughVararg(cast<Instruction>(*I), TheCall) || |
| 192 | !CallSite::get(cast<Instruction>(*I)).getCalledFunction()) { |
| 193 | RetValLiveness = Live; |
| 194 | break; |
| 195 | } else { |
| 196 | RetValLiveness = MaybeLive; |
| 197 | } |
| 198 | } else { |
| 199 | RetValLiveness = Live; |
| 200 | break; |
| 201 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 202 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 203 | // If the function is PASSED IN as an argument, its address has been taken |
| 204 | for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end(); |
| 205 | AI != E; ++AI) |
| 206 | if (AI->get() == &F) { |
| 207 | FunctionIntrinsicallyLive = true; |
| 208 | break; |
| 209 | } |
| 210 | if (FunctionIntrinsicallyLive) break; |
| 211 | } |
| 212 | |
| 213 | if (FunctionIntrinsicallyLive) { |
| 214 | DEBUG(std::cerr << " Intrinsically live fn: " << F.getName() << "\n"); |
Chris Lattner | 19bdc03 | 2005-05-06 05:34:40 +0000 | [diff] [blame] | 215 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); |
| 216 | AI != E; ++AI) |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 217 | LiveArguments.insert(AI); |
| 218 | LiveRetVal.insert(&F); |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | switch (RetValLiveness) { |
| 223 | case Live: LiveRetVal.insert(&F); break; |
| 224 | case MaybeLive: MaybeLiveRetVal.insert(&F); break; |
| 225 | case Dead: DeadRetVal.insert(&F); break; |
| 226 | } |
| 227 | |
| 228 | DEBUG(std::cerr << " Inspecting args for fn: " << F.getName() << "\n"); |
| 229 | |
| 230 | // If it is not intrinsically alive, we know that all users of the |
| 231 | // function are call sites. Mark all of the arguments live which are |
| 232 | // directly used, and keep track of all of the call sites of this function |
| 233 | // if there are any arguments we assume that are dead. |
| 234 | // |
| 235 | bool AnyMaybeLiveArgs = false; |
Chris Lattner | 19bdc03 | 2005-05-06 05:34:40 +0000 | [diff] [blame] | 236 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); |
| 237 | AI != E; ++AI) |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 238 | switch (getArgumentLiveness(*AI)) { |
| 239 | case Live: |
| 240 | DEBUG(std::cerr << " Arg live by use: " << AI->getName() << "\n"); |
| 241 | LiveArguments.insert(AI); |
| 242 | break; |
| 243 | case Dead: |
| 244 | DEBUG(std::cerr << " Arg definitely dead: " <<AI->getName()<<"\n"); |
| 245 | DeadArguments.insert(AI); |
| 246 | break; |
| 247 | case MaybeLive: |
| 248 | DEBUG(std::cerr << " Arg only passed to calls: " |
| 249 | << AI->getName() << "\n"); |
| 250 | AnyMaybeLiveArgs = true; |
| 251 | MaybeLiveArguments.insert(AI); |
| 252 | break; |
| 253 | } |
| 254 | |
| 255 | // If there are any "MaybeLive" arguments, we need to check callees of |
| 256 | // this function when/if they become alive. Record which functions are |
| 257 | // callees... |
| 258 | if (AnyMaybeLiveArgs || RetValLiveness == MaybeLive) |
| 259 | for (Value::use_iterator I = F.use_begin(), E = F.use_end(); |
| 260 | I != E; ++I) { |
| 261 | if (AnyMaybeLiveArgs) |
| 262 | CallSites.insert(std::make_pair(&F, CallSite::get(*I))); |
| 263 | |
| 264 | if (RetValLiveness == MaybeLive) |
| 265 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 266 | UI != E; ++UI) |
| 267 | InstructionsToInspect.push_back(cast<Instruction>(*UI)); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | // isMaybeLiveArgumentNowLive - Check to see if Arg is alive. At this point, we |
| 272 | // know that the only uses of Arg are to be passed in as an argument to a |
| 273 | // function call or return. Check to see if the formal argument passed in is in |
| 274 | // the LiveArguments set. If so, return true. |
| 275 | // |
| 276 | bool DAE::isMaybeLiveArgumentNowLive(Argument *Arg) { |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 277 | for (Value::use_iterator I = Arg->use_begin(), E = Arg->use_end(); I!=E; ++I){ |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 278 | if (isa<ReturnInst>(*I)) { |
| 279 | if (LiveRetVal.count(Arg->getParent())) return true; |
| 280 | continue; |
| 281 | } |
| 282 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 283 | CallSite CS = CallSite::get(*I); |
| 284 | |
| 285 | // We know that this can only be used for direct calls... |
Chris Lattner | d6d0d8c | 2003-11-02 02:06:27 +0000 | [diff] [blame] | 286 | Function *Callee = CS.getCalledFunction(); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 287 | |
| 288 | // Loop over all of the arguments (because Arg may be passed into the call |
| 289 | // multiple times) and check to see if any are now alive... |
| 290 | CallSite::arg_iterator CSAI = CS.arg_begin(); |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 291 | for (Function::arg_iterator AI = Callee->arg_begin(), E = Callee->arg_end(); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 292 | AI != E; ++AI, ++CSAI) |
| 293 | // If this is the argument we are looking for, check to see if it's alive |
| 294 | if (*CSAI == Arg && LiveArguments.count(AI)) |
| 295 | return true; |
| 296 | } |
| 297 | return false; |
| 298 | } |
| 299 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 300 | /// MarkArgumentLive - The MaybeLive argument 'Arg' is now known to be alive. |
| 301 | /// Mark it live in the specified sets and recursively mark arguments in callers |
| 302 | /// live that are needed to pass in a value. |
| 303 | /// |
| 304 | void DAE::MarkArgumentLive(Argument *Arg) { |
| 305 | std::set<Argument*>::iterator It = MaybeLiveArguments.lower_bound(Arg); |
| 306 | if (It == MaybeLiveArguments.end() || *It != Arg) return; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 307 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 308 | DEBUG(std::cerr << " MaybeLive argument now live: " << Arg->getName()<<"\n"); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 309 | MaybeLiveArguments.erase(It); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 310 | LiveArguments.insert(Arg); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 311 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 312 | // Loop over all of the call sites of the function, making any arguments |
| 313 | // passed in to provide a value for this argument live as necessary. |
| 314 | // |
| 315 | Function *Fn = Arg->getParent(); |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 316 | unsigned ArgNo = std::distance(Fn->arg_begin(), Function::arg_iterator(Arg)); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 317 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 318 | std::multimap<Function*, CallSite>::iterator I = CallSites.lower_bound(Fn); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 319 | for (; I != CallSites.end() && I->first == Fn; ++I) { |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 320 | CallSite CS = I->second; |
| 321 | Value *ArgVal = *(CS.arg_begin()+ArgNo); |
| 322 | if (Argument *ActualArg = dyn_cast<Argument>(ArgVal)) { |
| 323 | MarkArgumentLive(ActualArg); |
| 324 | } else { |
| 325 | // If the value passed in at this call site is a return value computed by |
| 326 | // some other call site, make sure to mark the return value at the other |
| 327 | // call site as being needed. |
| 328 | CallSite ArgCS = CallSite::get(ArgVal); |
| 329 | if (ArgCS.getInstruction()) |
| 330 | if (Function *Fn = ArgCS.getCalledFunction()) |
| 331 | MarkRetValLive(Fn); |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | /// MarkArgumentLive - The MaybeLive return value for the specified function is |
| 337 | /// now known to be alive. Propagate this fact to the return instructions which |
| 338 | /// produce it. |
| 339 | void DAE::MarkRetValLive(Function *F) { |
| 340 | assert(F && "Shame shame, we can't have null pointers here!"); |
| 341 | |
| 342 | // Check to see if we already knew it was live |
| 343 | std::set<Function*>::iterator I = MaybeLiveRetVal.lower_bound(F); |
| 344 | if (I == MaybeLiveRetVal.end() || *I != F) return; // It's already alive! |
| 345 | |
| 346 | DEBUG(std::cerr << " MaybeLive retval now live: " << F->getName() << "\n"); |
| 347 | |
| 348 | MaybeLiveRetVal.erase(I); |
| 349 | LiveRetVal.insert(F); // It is now known to be live! |
| 350 | |
| 351 | // Loop over all of the functions, noticing that the return value is now live. |
| 352 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 353 | if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) |
| 354 | MarkReturnInstArgumentLive(RI); |
| 355 | } |
| 356 | |
| 357 | void DAE::MarkReturnInstArgumentLive(ReturnInst *RI) { |
| 358 | Value *Op = RI->getOperand(0); |
| 359 | if (Argument *A = dyn_cast<Argument>(Op)) { |
| 360 | MarkArgumentLive(A); |
| 361 | } else if (CallInst *CI = dyn_cast<CallInst>(Op)) { |
| 362 | if (Function *F = CI->getCalledFunction()) |
| 363 | MarkRetValLive(F); |
| 364 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) { |
| 365 | if (Function *F = II->getCalledFunction()) |
| 366 | MarkRetValLive(F); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | // RemoveDeadArgumentsFromFunction - We know that F has dead arguments, as |
| 371 | // specified by the DeadArguments list. Transform the function and all of the |
| 372 | // callees of the function to not have these arguments. |
| 373 | // |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 374 | void DAE::RemoveDeadArgumentsFromFunction(Function *F) { |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 375 | // Start by computing a new prototype for the function, which is the same as |
| 376 | // the old function, but has fewer arguments. |
| 377 | const FunctionType *FTy = F->getFunctionType(); |
| 378 | std::vector<const Type*> Params; |
| 379 | |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 380 | for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 381 | if (!DeadArguments.count(I)) |
| 382 | Params.push_back(I->getType()); |
| 383 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 384 | const Type *RetTy = FTy->getReturnType(); |
| 385 | if (DeadRetVal.count(F)) { |
| 386 | RetTy = Type::VoidTy; |
| 387 | DeadRetVal.erase(F); |
| 388 | } |
| 389 | |
Chris Lattner | ff5bf9c | 2003-10-23 17:44:53 +0000 | [diff] [blame] | 390 | // Work around LLVM bug PR56: the CWriter cannot emit varargs functions which |
| 391 | // have zero fixed arguments. |
| 392 | // |
| 393 | // FIXME: once this bug is fixed in the CWriter, this hack should be removed. |
| 394 | // |
| 395 | bool ExtraArgHack = false; |
| 396 | if (Params.empty() && FTy->isVarArg()) { |
| 397 | ExtraArgHack = true; |
| 398 | Params.push_back(Type::IntTy); |
| 399 | } |
| 400 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 401 | FunctionType *NFTy = FunctionType::get(RetTy, Params, FTy->isVarArg()); |
| 402 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 403 | // Create the new function body and insert it into the module... |
Chris Lattner | 9b2a14b | 2003-06-25 04:12:49 +0000 | [diff] [blame] | 404 | Function *NF = new Function(NFTy, F->getLinkage(), F->getName()); |
Chris Lattner | f201dbc | 2005-05-09 01:05:50 +0000 | [diff] [blame] | 405 | NF->setCallingConv(F->getCallingConv()); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 406 | F->getParent()->getFunctionList().insert(F, NF); |
| 407 | |
| 408 | // Loop over all of the callers of the function, transforming the call sites |
| 409 | // to pass in a smaller number of arguments into the new function. |
| 410 | // |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 411 | std::vector<Value*> Args; |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 412 | while (!F->use_empty()) { |
| 413 | CallSite CS = CallSite::get(F->use_back()); |
| 414 | Instruction *Call = CS.getInstruction(); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 415 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 416 | // Loop over the operands, deleting dead ones... |
| 417 | CallSite::arg_iterator AI = CS.arg_begin(); |
Chris Lattner | 19bdc03 | 2005-05-06 05:34:40 +0000 | [diff] [blame] | 418 | for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 419 | I != E; ++I, ++AI) |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 420 | if (!DeadArguments.count(I)) // Remove operands for dead arguments |
| 421 | Args.push_back(*AI); |
| 422 | |
Chris Lattner | ff5bf9c | 2003-10-23 17:44:53 +0000 | [diff] [blame] | 423 | if (ExtraArgHack) |
| 424 | Args.push_back(Constant::getNullValue(Type::IntTy)); |
| 425 | |
| 426 | // Push any varargs arguments on the list |
| 427 | for (; AI != CS.arg_end(); ++AI) |
| 428 | Args.push_back(*AI); |
| 429 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 430 | Instruction *New; |
| 431 | if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) { |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 432 | New = new InvokeInst(NF, II->getNormalDest(), II->getUnwindDest(), |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 433 | Args, "", Call); |
Chris Lattner | f201dbc | 2005-05-09 01:05:50 +0000 | [diff] [blame] | 434 | cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv()); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 435 | } else { |
| 436 | New = new CallInst(NF, Args, "", Call); |
Chris Lattner | f201dbc | 2005-05-09 01:05:50 +0000 | [diff] [blame] | 437 | cast<CallInst>(New)->setCallingConv(CS.getCallingConv()); |
Chris Lattner | 1430ef1 | 2005-05-06 06:46:58 +0000 | [diff] [blame] | 438 | if (cast<CallInst>(Call)->isTailCall()) |
| 439 | cast<CallInst>(New)->setTailCall(); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 440 | } |
| 441 | Args.clear(); |
| 442 | |
| 443 | if (!Call->use_empty()) { |
| 444 | if (New->getType() == Type::VoidTy) |
| 445 | Call->replaceAllUsesWith(Constant::getNullValue(Call->getType())); |
| 446 | else { |
| 447 | Call->replaceAllUsesWith(New); |
| 448 | std::string Name = Call->getName(); |
| 449 | Call->setName(""); |
| 450 | New->setName(Name); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 451 | } |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 452 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 453 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 454 | // Finally, remove the old call from the program, reducing the use-count of |
| 455 | // F. |
| 456 | Call->getParent()->getInstList().erase(Call); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | // Since we have now created the new function, splice the body of the old |
| 460 | // function right into the new function, leaving the old rotting hulk of the |
| 461 | // function empty. |
| 462 | NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList()); |
| 463 | |
| 464 | // Loop over the argument list, transfering uses of the old arguments over to |
| 465 | // the new arguments, also transfering over the names as well. While we're at |
| 466 | // it, remove the dead arguments from the DeadArguments list. |
| 467 | // |
Chris Lattner | 19bdc03 | 2005-05-06 05:34:40 +0000 | [diff] [blame] | 468 | for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(), |
| 469 | I2 = NF->arg_begin(); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 470 | I != E; ++I) |
| 471 | if (!DeadArguments.count(I)) { |
| 472 | // If this is a live argument, move the name and users over to the new |
| 473 | // version. |
| 474 | I->replaceAllUsesWith(I2); |
| 475 | I2->setName(I->getName()); |
| 476 | ++I2; |
| 477 | } else { |
| 478 | // If this argument is dead, replace any uses of it with null constants |
| 479 | // (these are guaranteed to only be operands to call instructions which |
| 480 | // will later be simplified). |
| 481 | I->replaceAllUsesWith(Constant::getNullValue(I->getType())); |
| 482 | DeadArguments.erase(I); |
| 483 | } |
| 484 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 485 | // If we change the return value of the function we must rewrite any return |
| 486 | // instructions. Check this now. |
| 487 | if (F->getReturnType() != NF->getReturnType()) |
| 488 | for (Function::iterator BB = NF->begin(), E = NF->end(); BB != E; ++BB) |
| 489 | if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) { |
| 490 | new ReturnInst(0, RI); |
| 491 | BB->getInstList().erase(RI); |
| 492 | } |
| 493 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 494 | // Now that the old function is dead, delete it. |
| 495 | F->getParent()->getFunctionList().erase(F); |
| 496 | } |
| 497 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 498 | bool DAE::runOnModule(Module &M) { |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 499 | // First phase: loop through the module, determining which arguments are live. |
| 500 | // We assume all arguments are dead unless proven otherwise (allowing us to |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 501 | // determine that dead arguments passed into recursive functions are dead). |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 502 | // |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 503 | DEBUG(std::cerr << "DAE - Determining liveness\n"); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 504 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 505 | SurveyFunction(*I); |
| 506 | |
| 507 | // Loop over the instructions to inspect, propagating liveness among arguments |
| 508 | // and return values which are MaybeLive. |
| 509 | |
| 510 | while (!InstructionsToInspect.empty()) { |
| 511 | Instruction *I = InstructionsToInspect.back(); |
| 512 | InstructionsToInspect.pop_back(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 513 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 514 | if (ReturnInst *RI = dyn_cast<ReturnInst>(I)) { |
| 515 | // For return instructions, we just have to check to see if the return |
| 516 | // value for the current function is known now to be alive. If so, any |
| 517 | // arguments used by it are now alive, and any call instruction return |
| 518 | // value is alive as well. |
| 519 | if (LiveRetVal.count(RI->getParent()->getParent())) |
| 520 | MarkReturnInstArgumentLive(RI); |
| 521 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 522 | } else { |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 523 | CallSite CS = CallSite::get(I); |
| 524 | assert(CS.getInstruction() && "Unknown instruction for the I2I list!"); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 525 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 526 | Function *Callee = CS.getCalledFunction(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 527 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 528 | // If we found a call or invoke instruction on this list, that means that |
| 529 | // an argument of the function is a call instruction. If the argument is |
| 530 | // live, then the return value of the called instruction is now live. |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 531 | // |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 532 | CallSite::arg_iterator AI = CS.arg_begin(); // ActualIterator |
Chris Lattner | 19bdc03 | 2005-05-06 05:34:40 +0000 | [diff] [blame] | 533 | for (Function::arg_iterator FI = Callee->arg_begin(), |
| 534 | E = Callee->arg_end(); FI != E; ++AI, ++FI) { |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 535 | // If this argument is another call... |
| 536 | CallSite ArgCS = CallSite::get(*AI); |
| 537 | if (ArgCS.getInstruction() && LiveArguments.count(FI)) |
| 538 | if (Function *Callee = ArgCS.getCalledFunction()) |
| 539 | MarkRetValLive(Callee); |
| 540 | } |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
| 544 | // Now we loop over all of the MaybeLive arguments, promoting them to be live |
| 545 | // arguments if one of the calls that uses the arguments to the calls they are |
| 546 | // passed into requires them to be live. Of course this could make other |
| 547 | // arguments live, so process callers recursively. |
| 548 | // |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 549 | // Because elements can be removed from the MaybeLiveArguments set, copy it to |
| 550 | // a temporary vector. |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 551 | // |
| 552 | std::vector<Argument*> TmpArgList(MaybeLiveArguments.begin(), |
| 553 | MaybeLiveArguments.end()); |
| 554 | for (unsigned i = 0, e = TmpArgList.size(); i != e; ++i) { |
| 555 | Argument *MLA = TmpArgList[i]; |
| 556 | if (MaybeLiveArguments.count(MLA) && |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 557 | isMaybeLiveArgumentNowLive(MLA)) |
| 558 | MarkArgumentLive(MLA); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | // Recover memory early... |
| 562 | CallSites.clear(); |
| 563 | |
| 564 | // At this point, we know that all arguments in DeadArguments and |
| 565 | // MaybeLiveArguments are dead. If the two sets are empty, there is nothing |
| 566 | // to do. |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 567 | if (MaybeLiveArguments.empty() && DeadArguments.empty() && |
| 568 | MaybeLiveRetVal.empty() && DeadRetVal.empty()) |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 569 | return false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 570 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 571 | // Otherwise, compact into one set, and start eliminating the arguments from |
| 572 | // the functions. |
| 573 | DeadArguments.insert(MaybeLiveArguments.begin(), MaybeLiveArguments.end()); |
| 574 | MaybeLiveArguments.clear(); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 575 | DeadRetVal.insert(MaybeLiveRetVal.begin(), MaybeLiveRetVal.end()); |
| 576 | MaybeLiveRetVal.clear(); |
| 577 | |
| 578 | LiveArguments.clear(); |
| 579 | LiveRetVal.clear(); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 580 | |
| 581 | NumArgumentsEliminated += DeadArguments.size(); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 582 | NumRetValsEliminated += DeadRetVal.size(); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 583 | while (!DeadArguments.empty()) |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 584 | RemoveDeadArgumentsFromFunction((*DeadArguments.begin())->getParent()); |
| 585 | |
| 586 | while (!DeadRetVal.empty()) |
| 587 | RemoveDeadArgumentsFromFunction(*DeadRetVal.begin()); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 588 | return true; |
| 589 | } |