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