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