Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 1 | //===-- DeadArgumentElimination.cpp - Eliminate dead arguments ------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 9 | // |
| 10 | // This pass deletes dead arguments from internal functions. Dead argument |
| 11 | // elimination removes arguments which are directly dead, as well as arguments |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 12 | // only passed into function calls as dead arguments of other functions. This |
| 13 | // pass also deletes dead arguments in a similar way. |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 14 | // |
| 15 | // This pass is often useful as a cleanup pass to run after aggressive |
| 16 | // interprocedural passes, which add possibly-dead arguments. |
| 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Chris Lattner | 543a027 | 2005-06-24 16:00:46 +0000 | [diff] [blame] | 20 | #define DEBUG_TYPE "deadargelim" |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 21 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | 92044ce | 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 | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 26 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 27 | #include "llvm/Module.h" |
| 28 | #include "llvm/Pass.h" |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CallSite.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Debug.h" |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallVector.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/Statistic.h" |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Compiler.h" |
Dan Gohman | c9235d2 | 2008-03-21 23:51:57 +0000 | [diff] [blame] | 34 | #include <map> |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 35 | #include <set> |
Chris Lattner | 1e2385b | 2003-11-21 21:54:22 +0000 | [diff] [blame] | 36 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 38 | STATISTIC(NumArgumentsEliminated, "Number of unread args removed"); |
| 39 | STATISTIC(NumRetValsEliminated , "Number of unused return values removed"); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 41 | namespace { |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 42 | /// DAE - The dead argument elimination pass. |
| 43 | /// |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 44 | class VISIBILITY_HIDDEN DAE : public ModulePass { |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 45 | /// Liveness enum - During our initial pass over the program, we determine |
| 46 | /// that things are either definately alive, definately dead, or in need of |
| 47 | /// interprocedural analysis (MaybeLive). |
| 48 | /// |
| 49 | enum Liveness { Live, MaybeLive, Dead }; |
| 50 | |
| 51 | /// LiveArguments, MaybeLiveArguments, DeadArguments - These sets contain |
| 52 | /// all of the arguments in the program. The Dead set contains arguments |
| 53 | /// which are completely dead (never used in the function). The MaybeLive |
| 54 | /// set contains arguments which are only passed into other function calls, |
| 55 | /// thus may be live and may be dead. The Live set contains arguments which |
| 56 | /// are known to be alive. |
| 57 | /// |
| 58 | std::set<Argument*> DeadArguments, MaybeLiveArguments, LiveArguments; |
| 59 | |
| 60 | /// DeadRetVal, MaybeLiveRetVal, LifeRetVal - These sets contain all of the |
| 61 | /// functions in the program. The Dead set contains functions whose return |
| 62 | /// value is known to be dead. The MaybeLive set contains functions whose |
| 63 | /// return values are only used by return instructions, and the Live set |
| 64 | /// contains functions whose return values are used, functions that are |
| 65 | /// external, and functions that already return void. |
| 66 | /// |
| 67 | std::set<Function*> DeadRetVal, MaybeLiveRetVal, LiveRetVal; |
| 68 | |
| 69 | /// InstructionsToInspect - As we mark arguments and return values |
| 70 | /// MaybeLive, we keep track of which instructions could make the values |
| 71 | /// live here. Once the entire program has had the return value and |
| 72 | /// arguments analyzed, this set is scanned to promote the MaybeLive objects |
| 73 | /// to be Live if they really are used. |
| 74 | std::vector<Instruction*> InstructionsToInspect; |
| 75 | |
| 76 | /// CallSites - Keep track of the call sites of functions that have |
| 77 | /// MaybeLive arguments or return values. |
| 78 | std::multimap<Function*, CallSite> CallSites; |
| 79 | |
| 80 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 81 | static char ID; // Pass identification, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 82 | DAE() : ModulePass((intptr_t)&ID) {} |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 83 | bool runOnModule(Module &M); |
Chris Lattner | 9b2a14b | 2003-06-25 04:12:49 +0000 | [diff] [blame] | 84 | |
Chris Lattner | fdcc3ac | 2003-11-05 21:43:02 +0000 | [diff] [blame] | 85 | virtual bool ShouldHackArguments() const { return false; } |
| 86 | |
Chris Lattner | 9b2a14b | 2003-06-25 04:12:49 +0000 | [diff] [blame] | 87 | private: |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 88 | Liveness getArgumentLiveness(const Argument &A); |
| 89 | bool isMaybeLiveArgumentNowLive(Argument *Arg); |
| 90 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 91 | bool DeleteDeadVarargs(Function &Fn); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 92 | void SurveyFunction(Function &Fn); |
| 93 | |
| 94 | void MarkArgumentLive(Argument *Arg); |
| 95 | void MarkRetValLive(Function *F); |
| 96 | void MarkReturnInstArgumentLive(ReturnInst *RI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 97 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 98 | void RemoveDeadArgumentsFromFunction(Function *F); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 99 | }; |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 100 | char DAE::ID = 0; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 101 | RegisterPass<DAE> X("deadargelim", "Dead Argument Elimination"); |
Chris Lattner | fdcc3ac | 2003-11-05 21:43:02 +0000 | [diff] [blame] | 102 | |
| 103 | /// DAH - DeadArgumentHacking pass - Same as dead argument elimination, but |
| 104 | /// deletes arguments to functions which are external. This is only for use |
| 105 | /// by bugpoint. |
| 106 | struct DAH : public DAE { |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 107 | static char ID; |
Chris Lattner | fdcc3ac | 2003-11-05 21:43:02 +0000 | [diff] [blame] | 108 | virtual bool ShouldHackArguments() const { return true; } |
| 109 | }; |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 110 | char DAH::ID = 0; |
Chris Lattner | b6e0631 | 2003-11-05 21:53:41 +0000 | [diff] [blame] | 111 | RegisterPass<DAH> Y("deadarghaX0r", |
Brian Gaeke | 09ca411 | 2004-02-02 19:32:27 +0000 | [diff] [blame] | 112 | "Dead Argument Hacking (BUGPOINT USE ONLY; DO NOT USE)"); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Chris Lattner | 9b2a14b | 2003-06-25 04:12:49 +0000 | [diff] [blame] | 115 | /// createDeadArgEliminationPass - This pass removes arguments from functions |
Chris Lattner | fdcc3ac | 2003-11-05 21:43:02 +0000 | [diff] [blame] | 116 | /// which are not used by the body of the function. |
Chris Lattner | 9b2a14b | 2003-06-25 04:12:49 +0000 | [diff] [blame] | 117 | /// |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 118 | ModulePass *llvm::createDeadArgEliminationPass() { return new DAE(); } |
| 119 | ModulePass *llvm::createDeadArgHackingPass() { return new DAH(); } |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 121 | /// DeleteDeadVarargs - If this is an function that takes a ... list, and if |
| 122 | /// llvm.vastart is never called, the varargs list is dead for the function. |
| 123 | bool DAE::DeleteDeadVarargs(Function &Fn) { |
| 124 | assert(Fn.getFunctionType()->isVarArg() && "Function isn't varargs!"); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 125 | if (Fn.isDeclaration() || !Fn.hasInternalLinkage()) return false; |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 126 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 127 | // Ensure that the function is only directly called. |
| 128 | for (Value::use_iterator I = Fn.use_begin(), E = Fn.use_end(); I != E; ++I) { |
| 129 | // If this use is anything other than a call site, give up. |
| 130 | CallSite CS = CallSite::get(*I); |
| 131 | Instruction *TheCall = CS.getInstruction(); |
| 132 | if (!TheCall) return false; // Not a direct call site? |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 134 | // The addr of this function is passed to the call. |
| 135 | if (I.getOperandNo() != 0) return false; |
| 136 | } |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 137 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 138 | // Okay, we know we can transform this function if safe. Scan its body |
| 139 | // looking for calls to llvm.vastart. |
| 140 | for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) { |
| 141 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
| 142 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
| 143 | if (II->getIntrinsicID() == Intrinsic::vastart) |
| 144 | return false; |
| 145 | } |
| 146 | } |
| 147 | } |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 148 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 149 | // If we get here, there are no calls to llvm.vastart in the function body, |
| 150 | // remove the "..." and adjust all the calls. |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 151 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 152 | // Start by computing a new prototype for the function, which is the same as |
| 153 | // the old function, but has fewer arguments. |
| 154 | const FunctionType *FTy = Fn.getFunctionType(); |
| 155 | std::vector<const Type*> Params(FTy->param_begin(), FTy->param_end()); |
| 156 | FunctionType *NFTy = FunctionType::get(FTy->getReturnType(), Params, false); |
| 157 | unsigned NumArgs = Params.size(); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 158 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 159 | // Create the new function body and insert it into the module... |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame^] | 160 | Function *NF = Function::Create(NFTy, Fn.getLinkage()); |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 161 | NF->setCallingConv(Fn.getCallingConv()); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 162 | NF->setParamAttrs(Fn.getParamAttrs()); |
Gordon Henriksen | 194c90e | 2007-12-25 22:16:06 +0000 | [diff] [blame] | 163 | if (Fn.hasCollector()) |
| 164 | NF->setCollector(Fn.getCollector()); |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 165 | Fn.getParent()->getFunctionList().insert(&Fn, NF); |
Chris Lattner | 046800a | 2007-02-11 01:08:35 +0000 | [diff] [blame] | 166 | NF->takeName(&Fn); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 168 | // Loop over all of the callers of the function, transforming the call sites |
| 169 | // to pass in a smaller number of arguments into the new function. |
| 170 | // |
| 171 | std::vector<Value*> Args; |
| 172 | while (!Fn.use_empty()) { |
| 173 | CallSite CS = CallSite::get(Fn.use_back()); |
| 174 | Instruction *Call = CS.getInstruction(); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 175 | |
Chris Lattner | a0bc7fc | 2007-10-18 18:49:29 +0000 | [diff] [blame] | 176 | // Pass all the same arguments. |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 177 | Args.assign(CS.arg_begin(), CS.arg_begin()+NumArgs); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 178 | |
Duncan Sands | bfc5ae6 | 2008-01-11 23:13:45 +0000 | [diff] [blame] | 179 | // Drop any attributes that were on the vararg arguments. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 180 | PAListPtr PAL = CS.getParamAttrs(); |
| 181 | if (!PAL.isEmpty() && PAL.getSlot(PAL.getNumSlots() - 1).Index > NumArgs) { |
| 182 | SmallVector<ParamAttrsWithIndex, 8> ParamAttrsVec; |
| 183 | for (unsigned i = 0; PAL.getSlot(i).Index <= NumArgs; ++i) |
| 184 | ParamAttrsVec.push_back(PAL.getSlot(i)); |
| 185 | PAL = PAListPtr::get(ParamAttrsVec.begin(), ParamAttrsVec.end()); |
Duncan Sands | bfc5ae6 | 2008-01-11 23:13:45 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 188 | Instruction *New; |
| 189 | if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame^] | 190 | New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(), |
| 191 | Args.begin(), Args.end(), "", Call); |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 192 | cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv()); |
Duncan Sands | bfc5ae6 | 2008-01-11 23:13:45 +0000 | [diff] [blame] | 193 | cast<InvokeInst>(New)->setParamAttrs(PAL); |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 194 | } else { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame^] | 195 | New = CallInst::Create(NF, Args.begin(), Args.end(), "", Call); |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 196 | cast<CallInst>(New)->setCallingConv(CS.getCallingConv()); |
Duncan Sands | bfc5ae6 | 2008-01-11 23:13:45 +0000 | [diff] [blame] | 197 | cast<CallInst>(New)->setParamAttrs(PAL); |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 198 | if (cast<CallInst>(Call)->isTailCall()) |
| 199 | cast<CallInst>(New)->setTailCall(); |
| 200 | } |
| 201 | Args.clear(); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 202 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 203 | if (!Call->use_empty()) |
Chris Lattner | a0bc7fc | 2007-10-18 18:49:29 +0000 | [diff] [blame] | 204 | Call->replaceAllUsesWith(New); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 046800a | 2007-02-11 01:08:35 +0000 | [diff] [blame] | 206 | New->takeName(Call); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 208 | // Finally, remove the old call from the program, reducing the use-count of |
| 209 | // F. |
Chris Lattner | a0bc7fc | 2007-10-18 18:49:29 +0000 | [diff] [blame] | 210 | Call->eraseFromParent(); |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 211 | } |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 212 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 213 | // Since we have now created the new function, splice the body of the old |
| 214 | // function right into the new function, leaving the old rotting hulk of the |
| 215 | // function empty. |
| 216 | NF->getBasicBlockList().splice(NF->begin(), Fn.getBasicBlockList()); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 217 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 218 | // Loop over the argument list, transfering uses of the old arguments over to |
| 219 | // the new arguments, also transfering over the names as well. While we're at |
| 220 | // it, remove the dead arguments from the DeadArguments list. |
| 221 | // |
| 222 | for (Function::arg_iterator I = Fn.arg_begin(), E = Fn.arg_end(), |
| 223 | I2 = NF->arg_begin(); I != E; ++I, ++I2) { |
| 224 | // Move the name and users over to the new version. |
| 225 | I->replaceAllUsesWith(I2); |
Chris Lattner | 046800a | 2007-02-11 01:08:35 +0000 | [diff] [blame] | 226 | I2->takeName(I); |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 227 | } |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 4af90ab | 2006-09-18 07:02:31 +0000 | [diff] [blame] | 229 | // Finally, nuke the old function. |
| 230 | Fn.eraseFromParent(); |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 235 | static inline bool CallPassesValueThoughVararg(Instruction *Call, |
| 236 | const Value *Arg) { |
| 237 | CallSite CS = CallSite::get(Call); |
| 238 | const Type *CalledValueTy = CS.getCalledValue()->getType(); |
| 239 | const Type *FTy = cast<PointerType>(CalledValueTy)->getElementType(); |
| 240 | unsigned NumFixedArgs = cast<FunctionType>(FTy)->getNumParams(); |
| 241 | for (CallSite::arg_iterator AI = CS.arg_begin()+NumFixedArgs; |
| 242 | AI != CS.arg_end(); ++AI) |
| 243 | if (AI->get() == Arg) |
| 244 | return true; |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 245 | return false; |
| 246 | } |
| 247 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 248 | // getArgumentLiveness - Inspect an argument, determining if is known Live |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 249 | // (used in a computation), MaybeLive (only passed as an argument to a call), or |
| 250 | // Dead (not used). |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 251 | DAE::Liveness DAE::getArgumentLiveness(const Argument &A) { |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 252 | const Function *F = A.getParent(); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 253 | |
| 254 | // If this is the return value of a struct function, it's not really dead. |
Devang Patel | 41e2397 | 2008-03-03 21:46:28 +0000 | [diff] [blame] | 255 | if (F->hasStructRetAttr() && &*(F->arg_begin()) == &A) |
Chris Lattner | 92044ce | 2006-06-27 21:05:04 +0000 | [diff] [blame] | 256 | return Live; |
| 257 | |
| 258 | if (A.use_empty()) // First check, directly dead? |
| 259 | return Dead; |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 260 | |
| 261 | // Scan through all of the uses, looking for non-argument passing uses. |
| 262 | 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] | 263 | // Return instructions do not immediately effect liveness. |
| 264 | if (isa<ReturnInst>(*I)) |
| 265 | continue; |
| 266 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 267 | CallSite CS = CallSite::get(const_cast<User*>(*I)); |
| 268 | if (!CS.getInstruction()) { |
| 269 | // 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] | 270 | return Live; |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 271 | } |
| 272 | // If it's an indirect call, mark it alive... |
| 273 | Function *Callee = CS.getCalledFunction(); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 274 | if (!Callee) return Live; |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 97f4b66 | 2003-06-18 16:25:51 +0000 | [diff] [blame] | 276 | // Check to see if it's passed through a va_arg area: if so, we cannot |
| 277 | // remove it. |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 278 | if (CallPassesValueThoughVararg(CS.getInstruction(), &A)) |
| 279 | return Live; // If passed through va_arg area, we cannot remove it |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | return MaybeLive; // It must be used, but only as argument to a function |
| 283 | } |
| 284 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 285 | |
| 286 | // SurveyFunction - This performs the initial survey of the specified function, |
| 287 | // checking out whether or not it uses any of its incoming arguments or whether |
| 288 | // any callers use the return value. This fills in the |
| 289 | // (Dead|MaybeLive|Live)(Arguments|RetVal) sets. |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 290 | // |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 291 | // We consider arguments of non-internal functions to be intrinsically alive as |
| 292 | // well as arguments to functions which have their "address taken". |
| 293 | // |
| 294 | void DAE::SurveyFunction(Function &F) { |
| 295 | bool FunctionIntrinsicallyLive = false; |
| 296 | Liveness RetValLiveness = F.getReturnType() == Type::VoidTy ? Live : Dead; |
| 297 | |
Chris Lattner | b6e0631 | 2003-11-05 21:53:41 +0000 | [diff] [blame] | 298 | if (!F.hasInternalLinkage() && |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 299 | (!ShouldHackArguments() || F.isIntrinsic())) |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 300 | FunctionIntrinsicallyLive = true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 301 | else |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 302 | for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I) { |
| 303 | // If this use is anything other than a call site, the function is alive. |
| 304 | CallSite CS = CallSite::get(*I); |
| 305 | Instruction *TheCall = CS.getInstruction(); |
| 306 | if (!TheCall) { // Not a direct call site? |
| 307 | FunctionIntrinsicallyLive = true; |
| 308 | break; |
| 309 | } |
| 310 | |
| 311 | // Check to see if the return value is used... |
| 312 | if (RetValLiveness != Live) |
| 313 | for (Value::use_iterator I = TheCall->use_begin(), |
| 314 | E = TheCall->use_end(); I != E; ++I) |
| 315 | if (isa<ReturnInst>(cast<Instruction>(*I))) { |
| 316 | RetValLiveness = MaybeLive; |
| 317 | } else if (isa<CallInst>(cast<Instruction>(*I)) || |
| 318 | isa<InvokeInst>(cast<Instruction>(*I))) { |
| 319 | if (CallPassesValueThoughVararg(cast<Instruction>(*I), TheCall) || |
| 320 | !CallSite::get(cast<Instruction>(*I)).getCalledFunction()) { |
| 321 | RetValLiveness = Live; |
| 322 | break; |
| 323 | } else { |
| 324 | RetValLiveness = MaybeLive; |
| 325 | } |
| 326 | } else { |
| 327 | RetValLiveness = Live; |
| 328 | break; |
| 329 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 330 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 331 | // If the function is PASSED IN as an argument, its address has been taken |
| 332 | for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end(); |
| 333 | AI != E; ++AI) |
| 334 | if (AI->get() == &F) { |
| 335 | FunctionIntrinsicallyLive = true; |
| 336 | break; |
| 337 | } |
| 338 | if (FunctionIntrinsicallyLive) break; |
| 339 | } |
| 340 | |
| 341 | if (FunctionIntrinsicallyLive) { |
Bill Wendling | 0a81aac | 2006-11-26 10:02:32 +0000 | [diff] [blame] | 342 | DOUT << " Intrinsically live fn: " << F.getName() << "\n"; |
Chris Lattner | 19bdc03 | 2005-05-06 05:34:40 +0000 | [diff] [blame] | 343 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); |
| 344 | AI != E; ++AI) |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 345 | LiveArguments.insert(AI); |
| 346 | LiveRetVal.insert(&F); |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | switch (RetValLiveness) { |
| 351 | case Live: LiveRetVal.insert(&F); break; |
| 352 | case MaybeLive: MaybeLiveRetVal.insert(&F); break; |
| 353 | case Dead: DeadRetVal.insert(&F); break; |
| 354 | } |
| 355 | |
Bill Wendling | 0a81aac | 2006-11-26 10:02:32 +0000 | [diff] [blame] | 356 | DOUT << " Inspecting args for fn: " << F.getName() << "\n"; |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 357 | |
| 358 | // If it is not intrinsically alive, we know that all users of the |
| 359 | // function are call sites. Mark all of the arguments live which are |
| 360 | // directly used, and keep track of all of the call sites of this function |
| 361 | // if there are any arguments we assume that are dead. |
| 362 | // |
| 363 | bool AnyMaybeLiveArgs = false; |
Chris Lattner | 19bdc03 | 2005-05-06 05:34:40 +0000 | [diff] [blame] | 364 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); |
| 365 | AI != E; ++AI) |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 366 | switch (getArgumentLiveness(*AI)) { |
| 367 | case Live: |
Bill Wendling | 0a81aac | 2006-11-26 10:02:32 +0000 | [diff] [blame] | 368 | DOUT << " Arg live by use: " << AI->getName() << "\n"; |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 369 | LiveArguments.insert(AI); |
| 370 | break; |
| 371 | case Dead: |
Bill Wendling | 0a81aac | 2006-11-26 10:02:32 +0000 | [diff] [blame] | 372 | DOUT << " Arg definitely dead: " << AI->getName() <<"\n"; |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 373 | DeadArguments.insert(AI); |
| 374 | break; |
| 375 | case MaybeLive: |
Bill Wendling | 0a81aac | 2006-11-26 10:02:32 +0000 | [diff] [blame] | 376 | DOUT << " Arg only passed to calls: " << AI->getName() << "\n"; |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 377 | AnyMaybeLiveArgs = true; |
| 378 | MaybeLiveArguments.insert(AI); |
| 379 | break; |
| 380 | } |
| 381 | |
| 382 | // If there are any "MaybeLive" arguments, we need to check callees of |
| 383 | // this function when/if they become alive. Record which functions are |
| 384 | // callees... |
| 385 | if (AnyMaybeLiveArgs || RetValLiveness == MaybeLive) |
| 386 | for (Value::use_iterator I = F.use_begin(), E = F.use_end(); |
| 387 | I != E; ++I) { |
| 388 | if (AnyMaybeLiveArgs) |
| 389 | CallSites.insert(std::make_pair(&F, CallSite::get(*I))); |
| 390 | |
| 391 | if (RetValLiveness == MaybeLive) |
| 392 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 393 | UI != E; ++UI) |
| 394 | InstructionsToInspect.push_back(cast<Instruction>(*UI)); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | // isMaybeLiveArgumentNowLive - Check to see if Arg is alive. At this point, we |
| 399 | // know that the only uses of Arg are to be passed in as an argument to a |
| 400 | // function call or return. Check to see if the formal argument passed in is in |
| 401 | // the LiveArguments set. If so, return true. |
| 402 | // |
| 403 | bool DAE::isMaybeLiveArgumentNowLive(Argument *Arg) { |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 404 | 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] | 405 | if (isa<ReturnInst>(*I)) { |
| 406 | if (LiveRetVal.count(Arg->getParent())) return true; |
| 407 | continue; |
| 408 | } |
| 409 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 410 | CallSite CS = CallSite::get(*I); |
| 411 | |
| 412 | // We know that this can only be used for direct calls... |
Chris Lattner | d6d0d8c | 2003-11-02 02:06:27 +0000 | [diff] [blame] | 413 | Function *Callee = CS.getCalledFunction(); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 414 | |
| 415 | // Loop over all of the arguments (because Arg may be passed into the call |
| 416 | // multiple times) and check to see if any are now alive... |
| 417 | CallSite::arg_iterator CSAI = CS.arg_begin(); |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 418 | for (Function::arg_iterator AI = Callee->arg_begin(), E = Callee->arg_end(); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 419 | AI != E; ++AI, ++CSAI) |
| 420 | // If this is the argument we are looking for, check to see if it's alive |
| 421 | if (*CSAI == Arg && LiveArguments.count(AI)) |
| 422 | return true; |
| 423 | } |
| 424 | return false; |
| 425 | } |
| 426 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 427 | /// MarkArgumentLive - The MaybeLive argument 'Arg' is now known to be alive. |
| 428 | /// Mark it live in the specified sets and recursively mark arguments in callers |
| 429 | /// live that are needed to pass in a value. |
| 430 | /// |
| 431 | void DAE::MarkArgumentLive(Argument *Arg) { |
| 432 | std::set<Argument*>::iterator It = MaybeLiveArguments.lower_bound(Arg); |
| 433 | if (It == MaybeLiveArguments.end() || *It != Arg) return; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 434 | |
Bill Wendling | 0a81aac | 2006-11-26 10:02:32 +0000 | [diff] [blame] | 435 | DOUT << " MaybeLive argument now live: " << Arg->getName() <<"\n"; |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 436 | MaybeLiveArguments.erase(It); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 437 | LiveArguments.insert(Arg); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 438 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 439 | // Loop over all of the call sites of the function, making any arguments |
| 440 | // passed in to provide a value for this argument live as necessary. |
| 441 | // |
| 442 | Function *Fn = Arg->getParent(); |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 443 | unsigned ArgNo = std::distance(Fn->arg_begin(), Function::arg_iterator(Arg)); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 444 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 445 | std::multimap<Function*, CallSite>::iterator I = CallSites.lower_bound(Fn); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 446 | for (; I != CallSites.end() && I->first == Fn; ++I) { |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 447 | CallSite CS = I->second; |
| 448 | Value *ArgVal = *(CS.arg_begin()+ArgNo); |
| 449 | if (Argument *ActualArg = dyn_cast<Argument>(ArgVal)) { |
| 450 | MarkArgumentLive(ActualArg); |
| 451 | } else { |
| 452 | // If the value passed in at this call site is a return value computed by |
| 453 | // some other call site, make sure to mark the return value at the other |
| 454 | // call site as being needed. |
| 455 | CallSite ArgCS = CallSite::get(ArgVal); |
| 456 | if (ArgCS.getInstruction()) |
| 457 | if (Function *Fn = ArgCS.getCalledFunction()) |
| 458 | MarkRetValLive(Fn); |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | /// MarkArgumentLive - The MaybeLive return value for the specified function is |
| 464 | /// now known to be alive. Propagate this fact to the return instructions which |
| 465 | /// produce it. |
| 466 | void DAE::MarkRetValLive(Function *F) { |
| 467 | assert(F && "Shame shame, we can't have null pointers here!"); |
| 468 | |
| 469 | // Check to see if we already knew it was live |
| 470 | std::set<Function*>::iterator I = MaybeLiveRetVal.lower_bound(F); |
| 471 | if (I == MaybeLiveRetVal.end() || *I != F) return; // It's already alive! |
| 472 | |
Bill Wendling | 0a81aac | 2006-11-26 10:02:32 +0000 | [diff] [blame] | 473 | DOUT << " MaybeLive retval now live: " << F->getName() << "\n"; |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 474 | |
| 475 | MaybeLiveRetVal.erase(I); |
| 476 | LiveRetVal.insert(F); // It is now known to be live! |
| 477 | |
| 478 | // Loop over all of the functions, noticing that the return value is now live. |
| 479 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 480 | if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) |
| 481 | MarkReturnInstArgumentLive(RI); |
| 482 | } |
| 483 | |
| 484 | void DAE::MarkReturnInstArgumentLive(ReturnInst *RI) { |
| 485 | Value *Op = RI->getOperand(0); |
| 486 | if (Argument *A = dyn_cast<Argument>(Op)) { |
| 487 | MarkArgumentLive(A); |
| 488 | } else if (CallInst *CI = dyn_cast<CallInst>(Op)) { |
| 489 | if (Function *F = CI->getCalledFunction()) |
| 490 | MarkRetValLive(F); |
| 491 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) { |
| 492 | if (Function *F = II->getCalledFunction()) |
| 493 | MarkRetValLive(F); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
| 497 | // RemoveDeadArgumentsFromFunction - We know that F has dead arguments, as |
| 498 | // specified by the DeadArguments list. Transform the function and all of the |
| 499 | // callees of the function to not have these arguments. |
| 500 | // |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 501 | void DAE::RemoveDeadArgumentsFromFunction(Function *F) { |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 502 | // Start by computing a new prototype for the function, which is the same as |
| 503 | // the old function, but has fewer arguments. |
| 504 | const FunctionType *FTy = F->getFunctionType(); |
| 505 | std::vector<const Type*> Params; |
| 506 | |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 507 | // Set up to build a new list of parameter attributes |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 508 | SmallVector<ParamAttrsWithIndex, 8> ParamAttrsVec; |
| 509 | const PAListPtr &PAL = F->getParamAttrs(); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 510 | |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 511 | // The existing function return attributes. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 512 | ParameterAttributes RAttrs = PAL.getParamAttrs(0); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 513 | |
| 514 | // Make the function return void if the return value is dead. |
| 515 | const Type *RetTy = FTy->getReturnType(); |
| 516 | if (DeadRetVal.count(F)) { |
| 517 | RetTy = Type::VoidTy; |
Duncan Sands | 6c3470e | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 518 | RAttrs &= ~ParamAttr::typeIncompatible(RetTy); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 519 | DeadRetVal.erase(F); |
| 520 | } |
| 521 | |
| 522 | if (RAttrs) |
| 523 | ParamAttrsVec.push_back(ParamAttrsWithIndex::get(0, RAttrs)); |
| 524 | |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 525 | // Construct the new parameter list from non-dead arguments. Also construct |
| 526 | // a new set of parameter attributes to correspond. |
| 527 | unsigned index = 1; |
| 528 | for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; |
| 529 | ++I, ++index) |
| 530 | if (!DeadArguments.count(I)) { |
| 531 | Params.push_back(I->getType()); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 532 | |
| 533 | if (ParameterAttributes Attrs = PAL.getParamAttrs(index)) |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 534 | ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Params.size(), Attrs)); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | // Reconstruct the ParamAttrsList based on the vector we constructed. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 538 | PAListPtr NewPAL = PAListPtr::get(ParamAttrsVec.begin(), ParamAttrsVec.end()); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 539 | |
Chris Lattner | ff5bf9c | 2003-10-23 17:44:53 +0000 | [diff] [blame] | 540 | // Work around LLVM bug PR56: the CWriter cannot emit varargs functions which |
| 541 | // have zero fixed arguments. |
| 542 | // |
Chris Lattner | ff5bf9c | 2003-10-23 17:44:53 +0000 | [diff] [blame] | 543 | bool ExtraArgHack = false; |
| 544 | if (Params.empty() && FTy->isVarArg()) { |
| 545 | ExtraArgHack = true; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 546 | Params.push_back(Type::Int32Ty); |
Chris Lattner | ff5bf9c | 2003-10-23 17:44:53 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 549 | // Create the new function type based on the recomputed parameters. |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 550 | FunctionType *NFTy = FunctionType::get(RetTy, Params, FTy->isVarArg()); |
| 551 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 552 | // Create the new function body and insert it into the module... |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame^] | 553 | Function *NF = Function::Create(NFTy, F->getLinkage()); |
Chris Lattner | f201dbc | 2005-05-09 01:05:50 +0000 | [diff] [blame] | 554 | NF->setCallingConv(F->getCallingConv()); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 555 | NF->setParamAttrs(NewPAL); |
Gordon Henriksen | 194c90e | 2007-12-25 22:16:06 +0000 | [diff] [blame] | 556 | if (F->hasCollector()) |
| 557 | NF->setCollector(F->getCollector()); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 558 | F->getParent()->getFunctionList().insert(F, NF); |
Chris Lattner | 046800a | 2007-02-11 01:08:35 +0000 | [diff] [blame] | 559 | NF->takeName(F); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 560 | |
| 561 | // Loop over all of the callers of the function, transforming the call sites |
| 562 | // to pass in a smaller number of arguments into the new function. |
| 563 | // |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 564 | std::vector<Value*> Args; |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 565 | while (!F->use_empty()) { |
| 566 | CallSite CS = CallSite::get(F->use_back()); |
| 567 | Instruction *Call = CS.getInstruction(); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 568 | ParamAttrsVec.clear(); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 569 | const PAListPtr &CallPAL = CS.getParamAttrs(); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 570 | |
| 571 | // The call return attributes. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 572 | ParameterAttributes RAttrs = CallPAL.getParamAttrs(0); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 573 | // Adjust in case the function was changed to return void. |
Duncan Sands | 6c3470e | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 574 | RAttrs &= ~ParamAttr::typeIncompatible(NF->getReturnType()); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 575 | if (RAttrs) |
| 576 | ParamAttrsVec.push_back(ParamAttrsWithIndex::get(0, RAttrs)); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 577 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 578 | // Loop over the operands, deleting dead ones... |
| 579 | CallSite::arg_iterator AI = CS.arg_begin(); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 580 | index = 1; |
Chris Lattner | 19bdc03 | 2005-05-06 05:34:40 +0000 | [diff] [blame] | 581 | for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 582 | I != E; ++I, ++AI, ++index) |
| 583 | if (!DeadArguments.count(I)) { // Remove operands for dead arguments |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 584 | Args.push_back(*AI); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 585 | if (ParameterAttributes Attrs = CallPAL.getParamAttrs(index)) |
Duncan Sands | 110c835 | 2007-12-21 19:16:16 +0000 | [diff] [blame] | 586 | ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Args.size(), Attrs)); |
| 587 | } |
| 588 | |
Chris Lattner | ff5bf9c | 2003-10-23 17:44:53 +0000 | [diff] [blame] | 589 | if (ExtraArgHack) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 590 | Args.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | ff5bf9c | 2003-10-23 17:44:53 +0000 | [diff] [blame] | 591 | |
Evan Cheng | b2fc2a3 | 2008-01-17 04:18:54 +0000 | [diff] [blame] | 592 | // Push any varargs arguments on the list. Don't forget their attributes. |
| 593 | for (; AI != CS.arg_end(); ++AI) { |
Chris Lattner | ff5bf9c | 2003-10-23 17:44:53 +0000 | [diff] [blame] | 594 | Args.push_back(*AI); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 595 | if (ParameterAttributes Attrs = CallPAL.getParamAttrs(index++)) |
Evan Cheng | b2fc2a3 | 2008-01-17 04:18:54 +0000 | [diff] [blame] | 596 | ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Args.size(), Attrs)); |
| 597 | } |
| 598 | |
| 599 | // Reconstruct the ParamAttrsList based on the vector we constructed. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 600 | PAListPtr NewCallPAL = PAListPtr::get(ParamAttrsVec.begin(), |
| 601 | ParamAttrsVec.end()); |
Chris Lattner | ff5bf9c | 2003-10-23 17:44:53 +0000 | [diff] [blame] | 602 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 603 | Instruction *New; |
| 604 | if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame^] | 605 | New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(), |
| 606 | Args.begin(), Args.end(), "", Call); |
Chris Lattner | f201dbc | 2005-05-09 01:05:50 +0000 | [diff] [blame] | 607 | cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv()); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 608 | cast<InvokeInst>(New)->setParamAttrs(NewCallPAL); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 609 | } else { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame^] | 610 | New = CallInst::Create(NF, Args.begin(), Args.end(), "", Call); |
Chris Lattner | f201dbc | 2005-05-09 01:05:50 +0000 | [diff] [blame] | 611 | cast<CallInst>(New)->setCallingConv(CS.getCallingConv()); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 612 | cast<CallInst>(New)->setParamAttrs(NewCallPAL); |
Chris Lattner | 1430ef1 | 2005-05-06 06:46:58 +0000 | [diff] [blame] | 613 | if (cast<CallInst>(Call)->isTailCall()) |
| 614 | cast<CallInst>(New)->setTailCall(); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 615 | } |
| 616 | Args.clear(); |
| 617 | |
| 618 | if (!Call->use_empty()) { |
| 619 | if (New->getType() == Type::VoidTy) |
| 620 | Call->replaceAllUsesWith(Constant::getNullValue(Call->getType())); |
| 621 | else { |
| 622 | Call->replaceAllUsesWith(New); |
Chris Lattner | 046800a | 2007-02-11 01:08:35 +0000 | [diff] [blame] | 623 | New->takeName(Call); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 624 | } |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 625 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 626 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 627 | // Finally, remove the old call from the program, reducing the use-count of |
| 628 | // F. |
| 629 | Call->getParent()->getInstList().erase(Call); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | // Since we have now created the new function, splice the body of the old |
| 633 | // function right into the new function, leaving the old rotting hulk of the |
| 634 | // function empty. |
| 635 | NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList()); |
| 636 | |
| 637 | // Loop over the argument list, transfering uses of the old arguments over to |
| 638 | // the new arguments, also transfering over the names as well. While we're at |
| 639 | // it, remove the dead arguments from the DeadArguments list. |
| 640 | // |
Chris Lattner | 19bdc03 | 2005-05-06 05:34:40 +0000 | [diff] [blame] | 641 | for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(), |
| 642 | I2 = NF->arg_begin(); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 643 | I != E; ++I) |
| 644 | if (!DeadArguments.count(I)) { |
| 645 | // If this is a live argument, move the name and users over to the new |
| 646 | // version. |
| 647 | I->replaceAllUsesWith(I2); |
Chris Lattner | 046800a | 2007-02-11 01:08:35 +0000 | [diff] [blame] | 648 | I2->takeName(I); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 649 | ++I2; |
| 650 | } else { |
| 651 | // If this argument is dead, replace any uses of it with null constants |
| 652 | // (these are guaranteed to only be operands to call instructions which |
| 653 | // will later be simplified). |
| 654 | I->replaceAllUsesWith(Constant::getNullValue(I->getType())); |
| 655 | DeadArguments.erase(I); |
| 656 | } |
| 657 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 658 | // If we change the return value of the function we must rewrite any return |
| 659 | // instructions. Check this now. |
| 660 | if (F->getReturnType() != NF->getReturnType()) |
| 661 | for (Function::iterator BB = NF->begin(), E = NF->end(); BB != E; ++BB) |
| 662 | if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame^] | 663 | ReturnInst::Create(0, RI); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 664 | BB->getInstList().erase(RI); |
| 665 | } |
| 666 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 667 | // Now that the old function is dead, delete it. |
| 668 | F->getParent()->getFunctionList().erase(F); |
| 669 | } |
| 670 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 671 | bool DAE::runOnModule(Module &M) { |
Chris Lattner | 701bc42 | 2007-11-15 06:10:55 +0000 | [diff] [blame] | 672 | bool Changed = false; |
| 673 | // First pass: Do a simple check to see if any functions can have their "..." |
| 674 | // removed. We can do this if they never call va_start. This loop cannot be |
| 675 | // fused with the next loop, because deleting a function invalidates |
| 676 | // information computed while surveying other functions. |
| 677 | DOUT << "DAE - Deleting dead varargs\n"; |
| 678 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ) { |
| 679 | Function &F = *I++; |
| 680 | if (F.getFunctionType()->isVarArg()) |
| 681 | Changed |= DeleteDeadVarargs(F); |
| 682 | } |
| 683 | |
| 684 | // Second phase:loop through the module, determining which arguments are live. |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 685 | // We assume all arguments are dead unless proven otherwise (allowing us to |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 686 | // determine that dead arguments passed into recursive functions are dead). |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 687 | // |
Bill Wendling | 0a81aac | 2006-11-26 10:02:32 +0000 | [diff] [blame] | 688 | DOUT << "DAE - Determining liveness\n"; |
Chris Lattner | 701bc42 | 2007-11-15 06:10:55 +0000 | [diff] [blame] | 689 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 690 | SurveyFunction(*I); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 691 | |
| 692 | // Loop over the instructions to inspect, propagating liveness among arguments |
| 693 | // and return values which are MaybeLive. |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 694 | while (!InstructionsToInspect.empty()) { |
| 695 | Instruction *I = InstructionsToInspect.back(); |
| 696 | InstructionsToInspect.pop_back(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 697 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 698 | if (ReturnInst *RI = dyn_cast<ReturnInst>(I)) { |
| 699 | // For return instructions, we just have to check to see if the return |
| 700 | // value for the current function is known now to be alive. If so, any |
| 701 | // arguments used by it are now alive, and any call instruction return |
| 702 | // value is alive as well. |
| 703 | if (LiveRetVal.count(RI->getParent()->getParent())) |
| 704 | MarkReturnInstArgumentLive(RI); |
| 705 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 706 | } else { |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 707 | CallSite CS = CallSite::get(I); |
| 708 | assert(CS.getInstruction() && "Unknown instruction for the I2I list!"); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 709 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 710 | Function *Callee = CS.getCalledFunction(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 711 | |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 712 | // If we found a call or invoke instruction on this list, that means that |
| 713 | // an argument of the function is a call instruction. If the argument is |
| 714 | // live, then the return value of the called instruction is now live. |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 715 | // |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 716 | CallSite::arg_iterator AI = CS.arg_begin(); // ActualIterator |
Chris Lattner | 19bdc03 | 2005-05-06 05:34:40 +0000 | [diff] [blame] | 717 | for (Function::arg_iterator FI = Callee->arg_begin(), |
| 718 | E = Callee->arg_end(); FI != E; ++AI, ++FI) { |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 719 | // If this argument is another call... |
| 720 | CallSite ArgCS = CallSite::get(*AI); |
| 721 | if (ArgCS.getInstruction() && LiveArguments.count(FI)) |
| 722 | if (Function *Callee = ArgCS.getCalledFunction()) |
| 723 | MarkRetValLive(Callee); |
| 724 | } |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | |
| 728 | // Now we loop over all of the MaybeLive arguments, promoting them to be live |
| 729 | // arguments if one of the calls that uses the arguments to the calls they are |
| 730 | // passed into requires them to be live. Of course this could make other |
| 731 | // arguments live, so process callers recursively. |
| 732 | // |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 733 | // Because elements can be removed from the MaybeLiveArguments set, copy it to |
| 734 | // a temporary vector. |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 735 | // |
| 736 | std::vector<Argument*> TmpArgList(MaybeLiveArguments.begin(), |
| 737 | MaybeLiveArguments.end()); |
| 738 | for (unsigned i = 0, e = TmpArgList.size(); i != e; ++i) { |
| 739 | Argument *MLA = TmpArgList[i]; |
| 740 | if (MaybeLiveArguments.count(MLA) && |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 741 | isMaybeLiveArgumentNowLive(MLA)) |
| 742 | MarkArgumentLive(MLA); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | // Recover memory early... |
| 746 | CallSites.clear(); |
| 747 | |
| 748 | // At this point, we know that all arguments in DeadArguments and |
| 749 | // MaybeLiveArguments are dead. If the two sets are empty, there is nothing |
| 750 | // to do. |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 751 | if (MaybeLiveArguments.empty() && DeadArguments.empty() && |
| 752 | MaybeLiveRetVal.empty() && DeadRetVal.empty()) |
Chris Lattner | 701bc42 | 2007-11-15 06:10:55 +0000 | [diff] [blame] | 753 | return Changed; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 754 | |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 755 | // Otherwise, compact into one set, and start eliminating the arguments from |
| 756 | // the functions. |
| 757 | DeadArguments.insert(MaybeLiveArguments.begin(), MaybeLiveArguments.end()); |
| 758 | MaybeLiveArguments.clear(); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 759 | DeadRetVal.insert(MaybeLiveRetVal.begin(), MaybeLiveRetVal.end()); |
| 760 | MaybeLiveRetVal.clear(); |
| 761 | |
| 762 | LiveArguments.clear(); |
| 763 | LiveRetVal.clear(); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 764 | |
| 765 | NumArgumentsEliminated += DeadArguments.size(); |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 766 | NumRetValsEliminated += DeadRetVal.size(); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 767 | while (!DeadArguments.empty()) |
Chris Lattner | c3afd9b | 2003-10-23 03:48:17 +0000 | [diff] [blame] | 768 | RemoveDeadArgumentsFromFunction((*DeadArguments.begin())->getParent()); |
| 769 | |
| 770 | while (!DeadRetVal.empty()) |
| 771 | RemoveDeadArgumentsFromFunction(*DeadRetVal.begin()); |
Chris Lattner | 08227e4 | 2003-06-17 22:21:05 +0000 | [diff] [blame] | 772 | return true; |
| 773 | } |