Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 1 | //===-- DeadArgumentElimination.cpp - Eliminate dead arguments ------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 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. |
| 7 | // |
| 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 | |
| 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 | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 25 | #include "llvm/Instructions.h" |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CallSite.h" |
Reid Spencer | 551ccae | 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 | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 30 | #include <set> |
Chris Lattner | 1e2385b | 2003-11-21 21:54:22 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 32 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 33 | namespace { |
Chris Lattner | c3afd9b | 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 | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 38 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 39 | /// DAE - The dead argument elimination pass. |
| 40 | /// |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 41 | class DAE : public ModulePass { |
Chris Lattner | c3afd9b | 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 | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 78 | bool runOnModule(Module &M); |
Chris Lattner | 9b2a14b | 2003-06-25 04:12:49 +0000 | [diff] [blame] | 79 | |
Chris Lattner | fdcc3ac | 2003-11-05 21:43:02 +0000 | [diff] [blame] | 80 | virtual bool ShouldHackArguments() const { return false; } |
| 81 | |
Chris Lattner | 9b2a14b | 2003-06-25 04:12:49 +0000 | [diff] [blame] | 82 | private: |
Chris Lattner | c3afd9b | 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); |
| 91 | |
| 92 | void RemoveDeadArgumentsFromFunction(Function *F); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 93 | }; |
| 94 | RegisterOpt<DAE> X("deadargelim", "Dead Argument Elimination"); |
Chris Lattner | fdcc3ac | 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 | b6e0631 | 2003-11-05 21:53:41 +0000 | [diff] [blame] | 102 | RegisterPass<DAH> Y("deadarghaX0r", |
Brian Gaeke | 09ca411 | 2004-02-02 19:32:27 +0000 | [diff] [blame] | 103 | "Dead Argument Hacking (BUGPOINT USE ONLY; DO NOT USE)"); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Chris Lattner | 9b2a14b | 2003-06-25 04:12:49 +0000 | [diff] [blame] | 106 | /// createDeadArgEliminationPass - This pass removes arguments from functions |
Chris Lattner | fdcc3ac | 2003-11-05 21:43:02 +0000 | [diff] [blame] | 107 | /// which are not used by the body of the function. |
Chris Lattner | 9b2a14b | 2003-06-25 04:12:49 +0000 | [diff] [blame] | 108 | /// |
Chris Lattner | b12914b | 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 | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 111 | |
Chris Lattner | c3afd9b | 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 | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 122 | return false; |
| 123 | } |
| 124 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 125 | // getArgumentLiveness - Inspect an argument, determining if is known Live |
Chris Lattner | 08227e4 | 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 | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 128 | DAE::Liveness DAE::getArgumentLiveness(const Argument &A) { |
Chris Lattner | 08227e4 | 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 | c3afd9b | 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 | 08227e4 | 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 | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 140 | return Live; |
Chris Lattner | 08227e4 | 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 | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 144 | if (!Callee) return Live; |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 145 | |
Chris Lattner | 97f4b66 | 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 | c3afd9b | 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 | 08227e4 | 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 | c3afd9b | 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 | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 160 | // |
Chris Lattner | c3afd9b | 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 | b6e0631 | 2003-11-05 21:53:41 +0000 | [diff] [blame] | 168 | if (!F.hasInternalLinkage() && |
| 169 | (!ShouldHackArguments() || F.getIntrinsicID())) |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 170 | FunctionIntrinsicallyLive = true; |
| 171 | else |
| 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 | } |
| 200 | |
| 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 | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame^] | 213 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E; ++AI) |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 214 | LiveArguments.insert(AI); |
| 215 | LiveRetVal.insert(&F); |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | switch (RetValLiveness) { |
| 220 | case Live: LiveRetVal.insert(&F); break; |
| 221 | case MaybeLive: MaybeLiveRetVal.insert(&F); break; |
| 222 | case Dead: DeadRetVal.insert(&F); break; |
| 223 | } |
| 224 | |
| 225 | DEBUG(std::cerr << " Inspecting args for fn: " << F.getName() << "\n"); |
| 226 | |
| 227 | // If it is not intrinsically alive, we know that all users of the |
| 228 | // function are call sites. Mark all of the arguments live which are |
| 229 | // directly used, and keep track of all of the call sites of this function |
| 230 | // if there are any arguments we assume that are dead. |
| 231 | // |
| 232 | bool AnyMaybeLiveArgs = false; |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame^] | 233 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E; ++AI) |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 234 | switch (getArgumentLiveness(*AI)) { |
| 235 | case Live: |
| 236 | DEBUG(std::cerr << " Arg live by use: " << AI->getName() << "\n"); |
| 237 | LiveArguments.insert(AI); |
| 238 | break; |
| 239 | case Dead: |
| 240 | DEBUG(std::cerr << " Arg definitely dead: " <<AI->getName()<<"\n"); |
| 241 | DeadArguments.insert(AI); |
| 242 | break; |
| 243 | case MaybeLive: |
| 244 | DEBUG(std::cerr << " Arg only passed to calls: " |
| 245 | << AI->getName() << "\n"); |
| 246 | AnyMaybeLiveArgs = true; |
| 247 | MaybeLiveArguments.insert(AI); |
| 248 | break; |
| 249 | } |
| 250 | |
| 251 | // If there are any "MaybeLive" arguments, we need to check callees of |
| 252 | // this function when/if they become alive. Record which functions are |
| 253 | // callees... |
| 254 | if (AnyMaybeLiveArgs || RetValLiveness == MaybeLive) |
| 255 | for (Value::use_iterator I = F.use_begin(), E = F.use_end(); |
| 256 | I != E; ++I) { |
| 257 | if (AnyMaybeLiveArgs) |
| 258 | CallSites.insert(std::make_pair(&F, CallSite::get(*I))); |
| 259 | |
| 260 | if (RetValLiveness == MaybeLive) |
| 261 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 262 | UI != E; ++UI) |
| 263 | InstructionsToInspect.push_back(cast<Instruction>(*UI)); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | // isMaybeLiveArgumentNowLive - Check to see if Arg is alive. At this point, we |
| 268 | // know that the only uses of Arg are to be passed in as an argument to a |
| 269 | // function call or return. Check to see if the formal argument passed in is in |
| 270 | // the LiveArguments set. If so, return true. |
| 271 | // |
| 272 | bool DAE::isMaybeLiveArgumentNowLive(Argument *Arg) { |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 273 | 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] | 274 | if (isa<ReturnInst>(*I)) { |
| 275 | if (LiveRetVal.count(Arg->getParent())) return true; |
| 276 | continue; |
| 277 | } |
| 278 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 279 | CallSite CS = CallSite::get(*I); |
| 280 | |
| 281 | // We know that this can only be used for direct calls... |
Chris Lattner | d6d0d8c | 2003-11-02 02:06:27 +0000 | [diff] [blame] | 282 | Function *Callee = CS.getCalledFunction(); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 283 | |
| 284 | // Loop over all of the arguments (because Arg may be passed into the call |
| 285 | // multiple times) and check to see if any are now alive... |
| 286 | CallSite::arg_iterator CSAI = CS.arg_begin(); |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame^] | 287 | for (Function::arg_iterator AI = Callee->arg_begin(), E = Callee->arg_end(); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 288 | AI != E; ++AI, ++CSAI) |
| 289 | // If this is the argument we are looking for, check to see if it's alive |
| 290 | if (*CSAI == Arg && LiveArguments.count(AI)) |
| 291 | return true; |
| 292 | } |
| 293 | return false; |
| 294 | } |
| 295 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 296 | /// MarkArgumentLive - The MaybeLive argument 'Arg' is now known to be alive. |
| 297 | /// Mark it live in the specified sets and recursively mark arguments in callers |
| 298 | /// live that are needed to pass in a value. |
| 299 | /// |
| 300 | void DAE::MarkArgumentLive(Argument *Arg) { |
| 301 | std::set<Argument*>::iterator It = MaybeLiveArguments.lower_bound(Arg); |
| 302 | if (It == MaybeLiveArguments.end() || *It != Arg) return; |
| 303 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 304 | DEBUG(std::cerr << " MaybeLive argument now live: " << Arg->getName()<<"\n"); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 305 | MaybeLiveArguments.erase(It); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 306 | LiveArguments.insert(Arg); |
| 307 | |
| 308 | // Loop over all of the call sites of the function, making any arguments |
| 309 | // passed in to provide a value for this argument live as necessary. |
| 310 | // |
| 311 | Function *Fn = Arg->getParent(); |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame^] | 312 | unsigned ArgNo = std::distance(Fn->arg_begin(), Function::arg_iterator(Arg)); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 313 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 314 | std::multimap<Function*, CallSite>::iterator I = CallSites.lower_bound(Fn); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 315 | for (; I != CallSites.end() && I->first == Fn; ++I) { |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 316 | CallSite CS = I->second; |
| 317 | Value *ArgVal = *(CS.arg_begin()+ArgNo); |
| 318 | if (Argument *ActualArg = dyn_cast<Argument>(ArgVal)) { |
| 319 | MarkArgumentLive(ActualArg); |
| 320 | } else { |
| 321 | // If the value passed in at this call site is a return value computed by |
| 322 | // some other call site, make sure to mark the return value at the other |
| 323 | // call site as being needed. |
| 324 | CallSite ArgCS = CallSite::get(ArgVal); |
| 325 | if (ArgCS.getInstruction()) |
| 326 | if (Function *Fn = ArgCS.getCalledFunction()) |
| 327 | MarkRetValLive(Fn); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /// MarkArgumentLive - The MaybeLive return value for the specified function is |
| 333 | /// now known to be alive. Propagate this fact to the return instructions which |
| 334 | /// produce it. |
| 335 | void DAE::MarkRetValLive(Function *F) { |
| 336 | assert(F && "Shame shame, we can't have null pointers here!"); |
| 337 | |
| 338 | // Check to see if we already knew it was live |
| 339 | std::set<Function*>::iterator I = MaybeLiveRetVal.lower_bound(F); |
| 340 | if (I == MaybeLiveRetVal.end() || *I != F) return; // It's already alive! |
| 341 | |
| 342 | DEBUG(std::cerr << " MaybeLive retval now live: " << F->getName() << "\n"); |
| 343 | |
| 344 | MaybeLiveRetVal.erase(I); |
| 345 | LiveRetVal.insert(F); // It is now known to be live! |
| 346 | |
| 347 | // Loop over all of the functions, noticing that the return value is now live. |
| 348 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 349 | if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) |
| 350 | MarkReturnInstArgumentLive(RI); |
| 351 | } |
| 352 | |
| 353 | void DAE::MarkReturnInstArgumentLive(ReturnInst *RI) { |
| 354 | Value *Op = RI->getOperand(0); |
| 355 | if (Argument *A = dyn_cast<Argument>(Op)) { |
| 356 | MarkArgumentLive(A); |
| 357 | } else if (CallInst *CI = dyn_cast<CallInst>(Op)) { |
| 358 | if (Function *F = CI->getCalledFunction()) |
| 359 | MarkRetValLive(F); |
| 360 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) { |
| 361 | if (Function *F = II->getCalledFunction()) |
| 362 | MarkRetValLive(F); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
| 366 | // RemoveDeadArgumentsFromFunction - We know that F has dead arguments, as |
| 367 | // specified by the DeadArguments list. Transform the function and all of the |
| 368 | // callees of the function to not have these arguments. |
| 369 | // |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 370 | void DAE::RemoveDeadArgumentsFromFunction(Function *F) { |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 371 | // Start by computing a new prototype for the function, which is the same as |
| 372 | // the old function, but has fewer arguments. |
| 373 | const FunctionType *FTy = F->getFunctionType(); |
| 374 | std::vector<const Type*> Params; |
| 375 | |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame^] | 376 | 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] | 377 | if (!DeadArguments.count(I)) |
| 378 | Params.push_back(I->getType()); |
| 379 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 380 | const Type *RetTy = FTy->getReturnType(); |
| 381 | if (DeadRetVal.count(F)) { |
| 382 | RetTy = Type::VoidTy; |
| 383 | DeadRetVal.erase(F); |
| 384 | } |
| 385 | |
Chris Lattner | ff5bf9c | 2003-10-23 17:44:53 +0000 | [diff] [blame] | 386 | // Work around LLVM bug PR56: the CWriter cannot emit varargs functions which |
| 387 | // have zero fixed arguments. |
| 388 | // |
| 389 | // FIXME: once this bug is fixed in the CWriter, this hack should be removed. |
| 390 | // |
| 391 | bool ExtraArgHack = false; |
| 392 | if (Params.empty() && FTy->isVarArg()) { |
| 393 | ExtraArgHack = true; |
| 394 | Params.push_back(Type::IntTy); |
| 395 | } |
| 396 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 397 | FunctionType *NFTy = FunctionType::get(RetTy, Params, FTy->isVarArg()); |
| 398 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 399 | // Create the new function body and insert it into the module... |
Chris Lattner | 9b2a14b | 2003-06-25 04:12:49 +0000 | [diff] [blame] | 400 | Function *NF = new Function(NFTy, F->getLinkage(), F->getName()); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 401 | F->getParent()->getFunctionList().insert(F, NF); |
| 402 | |
| 403 | // Loop over all of the callers of the function, transforming the call sites |
| 404 | // to pass in a smaller number of arguments into the new function. |
| 405 | // |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 406 | std::vector<Value*> Args; |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 407 | while (!F->use_empty()) { |
| 408 | CallSite CS = CallSite::get(F->use_back()); |
| 409 | Instruction *Call = CS.getInstruction(); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 410 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 411 | // Loop over the operands, deleting dead ones... |
| 412 | CallSite::arg_iterator AI = CS.arg_begin(); |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame^] | 413 | for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I, ++AI) |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 414 | if (!DeadArguments.count(I)) // Remove operands for dead arguments |
| 415 | Args.push_back(*AI); |
| 416 | |
Chris Lattner | ff5bf9c | 2003-10-23 17:44:53 +0000 | [diff] [blame] | 417 | if (ExtraArgHack) |
| 418 | Args.push_back(Constant::getNullValue(Type::IntTy)); |
| 419 | |
| 420 | // Push any varargs arguments on the list |
| 421 | for (; AI != CS.arg_end(); ++AI) |
| 422 | Args.push_back(*AI); |
| 423 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 424 | Instruction *New; |
| 425 | if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) { |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 426 | New = new InvokeInst(NF, II->getNormalDest(), II->getUnwindDest(), |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 427 | Args, "", Call); |
| 428 | } else { |
| 429 | New = new CallInst(NF, Args, "", Call); |
| 430 | } |
| 431 | Args.clear(); |
| 432 | |
| 433 | if (!Call->use_empty()) { |
| 434 | if (New->getType() == Type::VoidTy) |
| 435 | Call->replaceAllUsesWith(Constant::getNullValue(Call->getType())); |
| 436 | else { |
| 437 | Call->replaceAllUsesWith(New); |
| 438 | std::string Name = Call->getName(); |
| 439 | Call->setName(""); |
| 440 | New->setName(Name); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 441 | } |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | // Finally, remove the old call from the program, reducing the use-count of |
| 445 | // F. |
| 446 | Call->getParent()->getInstList().erase(Call); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | // Since we have now created the new function, splice the body of the old |
| 450 | // function right into the new function, leaving the old rotting hulk of the |
| 451 | // function empty. |
| 452 | NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList()); |
| 453 | |
| 454 | // Loop over the argument list, transfering uses of the old arguments over to |
| 455 | // the new arguments, also transfering over the names as well. While we're at |
| 456 | // it, remove the dead arguments from the DeadArguments list. |
| 457 | // |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame^] | 458 | for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(), I2 = NF->arg_begin(); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 459 | I != E; ++I) |
| 460 | if (!DeadArguments.count(I)) { |
| 461 | // If this is a live argument, move the name and users over to the new |
| 462 | // version. |
| 463 | I->replaceAllUsesWith(I2); |
| 464 | I2->setName(I->getName()); |
| 465 | ++I2; |
| 466 | } else { |
| 467 | // If this argument is dead, replace any uses of it with null constants |
| 468 | // (these are guaranteed to only be operands to call instructions which |
| 469 | // will later be simplified). |
| 470 | I->replaceAllUsesWith(Constant::getNullValue(I->getType())); |
| 471 | DeadArguments.erase(I); |
| 472 | } |
| 473 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 474 | // If we change the return value of the function we must rewrite any return |
| 475 | // instructions. Check this now. |
| 476 | if (F->getReturnType() != NF->getReturnType()) |
| 477 | for (Function::iterator BB = NF->begin(), E = NF->end(); BB != E; ++BB) |
| 478 | if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) { |
| 479 | new ReturnInst(0, RI); |
| 480 | BB->getInstList().erase(RI); |
| 481 | } |
| 482 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 483 | // Now that the old function is dead, delete it. |
| 484 | F->getParent()->getFunctionList().erase(F); |
| 485 | } |
| 486 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 487 | bool DAE::runOnModule(Module &M) { |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 488 | // First phase: loop through the module, determining which arguments are live. |
| 489 | // We assume all arguments are dead unless proven otherwise (allowing us to |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 490 | // determine that dead arguments passed into recursive functions are dead). |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 491 | // |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 492 | DEBUG(std::cerr << "DAE - Determining liveness\n"); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 493 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 494 | SurveyFunction(*I); |
| 495 | |
| 496 | // Loop over the instructions to inspect, propagating liveness among arguments |
| 497 | // and return values which are MaybeLive. |
| 498 | |
| 499 | while (!InstructionsToInspect.empty()) { |
| 500 | Instruction *I = InstructionsToInspect.back(); |
| 501 | InstructionsToInspect.pop_back(); |
| 502 | |
| 503 | if (ReturnInst *RI = dyn_cast<ReturnInst>(I)) { |
| 504 | // For return instructions, we just have to check to see if the return |
| 505 | // value for the current function is known now to be alive. If so, any |
| 506 | // arguments used by it are now alive, and any call instruction return |
| 507 | // value is alive as well. |
| 508 | if (LiveRetVal.count(RI->getParent()->getParent())) |
| 509 | MarkReturnInstArgumentLive(RI); |
| 510 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 511 | } else { |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 512 | CallSite CS = CallSite::get(I); |
| 513 | assert(CS.getInstruction() && "Unknown instruction for the I2I list!"); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 514 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 515 | Function *Callee = CS.getCalledFunction(); |
| 516 | |
| 517 | // If we found a call or invoke instruction on this list, that means that |
| 518 | // an argument of the function is a call instruction. If the argument is |
| 519 | // live, then the return value of the called instruction is now live. |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 520 | // |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 521 | CallSite::arg_iterator AI = CS.arg_begin(); // ActualIterator |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame^] | 522 | for (Function::arg_iterator FI = Callee->arg_begin(), E = Callee->arg_end(); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 523 | FI != E; ++AI, ++FI) { |
| 524 | // If this argument is another call... |
| 525 | CallSite ArgCS = CallSite::get(*AI); |
| 526 | if (ArgCS.getInstruction() && LiveArguments.count(FI)) |
| 527 | if (Function *Callee = ArgCS.getCalledFunction()) |
| 528 | MarkRetValLive(Callee); |
| 529 | } |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
| 533 | // Now we loop over all of the MaybeLive arguments, promoting them to be live |
| 534 | // arguments if one of the calls that uses the arguments to the calls they are |
| 535 | // passed into requires them to be live. Of course this could make other |
| 536 | // arguments live, so process callers recursively. |
| 537 | // |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 538 | // Because elements can be removed from the MaybeLiveArguments set, copy it to |
| 539 | // a temporary vector. |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 540 | // |
| 541 | std::vector<Argument*> TmpArgList(MaybeLiveArguments.begin(), |
| 542 | MaybeLiveArguments.end()); |
| 543 | for (unsigned i = 0, e = TmpArgList.size(); i != e; ++i) { |
| 544 | Argument *MLA = TmpArgList[i]; |
| 545 | if (MaybeLiveArguments.count(MLA) && |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 546 | isMaybeLiveArgumentNowLive(MLA)) |
| 547 | MarkArgumentLive(MLA); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | // Recover memory early... |
| 551 | CallSites.clear(); |
| 552 | |
| 553 | // At this point, we know that all arguments in DeadArguments and |
| 554 | // MaybeLiveArguments are dead. If the two sets are empty, there is nothing |
| 555 | // to do. |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 556 | if (MaybeLiveArguments.empty() && DeadArguments.empty() && |
| 557 | MaybeLiveRetVal.empty() && DeadRetVal.empty()) |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 558 | return false; |
| 559 | |
| 560 | // Otherwise, compact into one set, and start eliminating the arguments from |
| 561 | // the functions. |
| 562 | DeadArguments.insert(MaybeLiveArguments.begin(), MaybeLiveArguments.end()); |
| 563 | MaybeLiveArguments.clear(); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 564 | DeadRetVal.insert(MaybeLiveRetVal.begin(), MaybeLiveRetVal.end()); |
| 565 | MaybeLiveRetVal.clear(); |
| 566 | |
| 567 | LiveArguments.clear(); |
| 568 | LiveRetVal.clear(); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 569 | |
| 570 | NumArgumentsEliminated += DeadArguments.size(); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 571 | NumRetValsEliminated += DeadRetVal.size(); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 572 | while (!DeadArguments.empty()) |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 573 | RemoveDeadArgumentsFromFunction((*DeadArguments.begin())->getParent()); |
| 574 | |
| 575 | while (!DeadRetVal.empty()) |
| 576 | RemoveDeadArgumentsFromFunction(*DeadRetVal.begin()); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 577 | return true; |
| 578 | } |