Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 1 | //===-- ArgumentPromotion.cpp - Promote by-reference arguments ------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +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 | // |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass promotes "by reference" arguments to be "by value" arguments. In |
| 11 | // practice, this means looking for internal functions that have pointer |
Chris Lattner | 5065b24 | 2004-09-17 03:58:39 +0000 | [diff] [blame] | 12 | // arguments. If we can prove, through the use of alias analysis, that an |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 13 | // argument is *only* loaded, then we can pass the value into the function |
| 14 | // instead of the address of the value. This can cause recursive simplification |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 15 | // of code and lead to the elimination of allocas (especially in C++ template |
| 16 | // code like the STL). |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 17 | // |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 18 | // This pass also handles aggregate arguments that are passed into a function, |
| 19 | // scalarizing them if the elements of the aggregate are only loaded. Note that |
| 20 | // we refuse to scalarize aggregates which would require passing in more than |
| 21 | // three operands to the function, because we don't want to pass thousands of |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 22 | // operands for a large array or structure! |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 23 | // |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 24 | // Note that this transformation could also be done for arguments that are only |
| 25 | // stored to (returning the value instead), but we do not currently handle that |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 26 | // case. This case would be best handled when and if we start supporting |
| 27 | // multiple return values from functions. |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 28 | // |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 31 | #define DEBUG_TYPE "argpromotion" |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 32 | #include "llvm/Transforms/IPO.h" |
| 33 | #include "llvm/Constants.h" |
| 34 | #include "llvm/DerivedTypes.h" |
| 35 | #include "llvm/Module.h" |
Chris Lattner | 37b6c4f | 2004-09-18 00:34:13 +0000 | [diff] [blame] | 36 | #include "llvm/CallGraphSCCPass.h" |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 37 | #include "llvm/Instructions.h" |
| 38 | #include "llvm/Analysis/AliasAnalysis.h" |
Chris Lattner | 37b6c4f | 2004-09-18 00:34:13 +0000 | [diff] [blame] | 39 | #include "llvm/Analysis/CallGraph.h" |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 40 | #include "llvm/Target/TargetData.h" |
| 41 | #include "llvm/Support/CallSite.h" |
| 42 | #include "llvm/Support/CFG.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Debug.h" |
| 44 | #include "llvm/ADT/DepthFirstIterator.h" |
| 45 | #include "llvm/ADT/Statistic.h" |
| 46 | #include "llvm/ADT/StringExtras.h" |
Reid Spencer | 557ab15 | 2007-02-05 23:32:05 +0000 | [diff] [blame^] | 47 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 48 | #include <set> |
| 49 | using namespace llvm; |
| 50 | |
Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 51 | STATISTIC(NumArgumentsPromoted , "Number of pointer arguments promoted"); |
| 52 | STATISTIC(NumAggregatesPromoted, "Number of aggregate arguments promoted"); |
| 53 | STATISTIC(NumArgumentsDead , "Number of dead pointer args eliminated"); |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 55 | namespace { |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 56 | /// ArgPromotion - The 'by reference' to 'by value' argument promotion pass. |
| 57 | /// |
Reid Spencer | 557ab15 | 2007-02-05 23:32:05 +0000 | [diff] [blame^] | 58 | struct VISIBILITY_HIDDEN ArgPromotion : public CallGraphSCCPass { |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 59 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 60 | AU.addRequired<AliasAnalysis>(); |
| 61 | AU.addRequired<TargetData>(); |
Chris Lattner | 37b6c4f | 2004-09-18 00:34:13 +0000 | [diff] [blame] | 62 | CallGraphSCCPass::getAnalysisUsage(AU); |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Chris Lattner | 37b6c4f | 2004-09-18 00:34:13 +0000 | [diff] [blame] | 65 | virtual bool runOnSCC(const std::vector<CallGraphNode *> &SCC); |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 66 | private: |
Chris Lattner | 37b6c4f | 2004-09-18 00:34:13 +0000 | [diff] [blame] | 67 | bool PromoteArguments(CallGraphNode *CGN); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 68 | bool isSafeToPromoteArgument(Argument *Arg) const; |
Chris Lattner | 37b6c4f | 2004-09-18 00:34:13 +0000 | [diff] [blame] | 69 | Function *DoPromotion(Function *F, std::vector<Argument*> &ArgsToPromote); |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
Chris Lattner | c2d3d31 | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 72 | RegisterPass<ArgPromotion> X("argpromotion", |
| 73 | "Promote 'by reference' arguments to scalars"); |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Devang Patel | 13058a5 | 2007-01-26 00:47:38 +0000 | [diff] [blame] | 76 | Pass *llvm::createArgumentPromotionPass() { |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 77 | return new ArgPromotion(); |
| 78 | } |
| 79 | |
Chris Lattner | 37b6c4f | 2004-09-18 00:34:13 +0000 | [diff] [blame] | 80 | bool ArgPromotion::runOnSCC(const std::vector<CallGraphNode *> &SCC) { |
| 81 | bool Changed = false, LocalChange; |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 9864df9 | 2004-09-19 01:05:16 +0000 | [diff] [blame] | 83 | do { // Iterate until we stop promoting from this SCC. |
Chris Lattner | 37b6c4f | 2004-09-18 00:34:13 +0000 | [diff] [blame] | 84 | LocalChange = false; |
| 85 | // Attempt to promote arguments from all functions in this SCC. |
| 86 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) |
| 87 | LocalChange |= PromoteArguments(SCC[i]); |
| 88 | Changed |= LocalChange; // Remember that we changed something. |
| 89 | } while (LocalChange); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 91 | return Changed; |
| 92 | } |
| 93 | |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 94 | /// PromoteArguments - This method checks the specified function to see if there |
| 95 | /// are any promotable arguments and if it is safe to promote the function (for |
| 96 | /// example, all callers are direct). If safe to promote some arguments, it |
| 97 | /// calls the DoPromotion method. |
| 98 | /// |
Chris Lattner | 37b6c4f | 2004-09-18 00:34:13 +0000 | [diff] [blame] | 99 | bool ArgPromotion::PromoteArguments(CallGraphNode *CGN) { |
| 100 | Function *F = CGN->getFunction(); |
| 101 | |
| 102 | // Make sure that it is local to this module. |
| 103 | if (!F || !F->hasInternalLinkage()) return false; |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 104 | |
| 105 | // First check: see if there are any pointer arguments! If not, quick exit. |
| 106 | std::vector<Argument*> PointerArgs; |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 107 | for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 108 | if (isa<PointerType>(I->getType())) |
| 109 | PointerArgs.push_back(I); |
| 110 | if (PointerArgs.empty()) return false; |
| 111 | |
| 112 | // Second check: make sure that all callers are direct callers. We can't |
| 113 | // transform functions that have indirect callers. |
| 114 | for (Value::use_iterator UI = F->use_begin(), E = F->use_end(); |
Chris Lattner | 64b8d69 | 2004-03-07 22:43:27 +0000 | [diff] [blame] | 115 | UI != E; ++UI) { |
| 116 | CallSite CS = CallSite::get(*UI); |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 117 | if (!CS.getInstruction()) // "Taking the address" of the function |
| 118 | return false; |
| 119 | |
| 120 | // Ensure that this call site is CALLING the function, not passing it as |
| 121 | // an argument. |
| 122 | for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end(); |
| 123 | AI != E; ++AI) |
| 124 | if (*AI == F) return false; // Passing the function address in! |
Chris Lattner | 64b8d69 | 2004-03-07 22:43:27 +0000 | [diff] [blame] | 125 | } |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 126 | |
| 127 | // Check to see which arguments are promotable. If an argument is not |
| 128 | // promotable, remove it from the PointerArgs vector. |
| 129 | for (unsigned i = 0; i != PointerArgs.size(); ++i) |
| 130 | if (!isSafeToPromoteArgument(PointerArgs[i])) { |
| 131 | std::swap(PointerArgs[i--], PointerArgs.back()); |
| 132 | PointerArgs.pop_back(); |
| 133 | } |
| 134 | |
| 135 | // No promotable pointer arguments. |
| 136 | if (PointerArgs.empty()) return false; |
| 137 | |
| 138 | // Okay, promote all of the arguments are rewrite the callees! |
Chris Lattner | 37b6c4f | 2004-09-18 00:34:13 +0000 | [diff] [blame] | 139 | Function *NewF = DoPromotion(F, PointerArgs); |
| 140 | |
| 141 | // Update the call graph to know that the old function is gone. |
| 142 | getAnalysis<CallGraph>().changeFunction(F, NewF); |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 143 | return true; |
| 144 | } |
| 145 | |
Chris Lattner | 244031d | 2004-11-13 23:31:34 +0000 | [diff] [blame] | 146 | /// IsAlwaysValidPointer - Return true if the specified pointer is always legal |
| 147 | /// to load. |
| 148 | static bool IsAlwaysValidPointer(Value *V) { |
| 149 | if (isa<AllocaInst>(V) || isa<GlobalVariable>(V)) return true; |
| 150 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) |
| 151 | return IsAlwaysValidPointer(GEP->getOperand(0)); |
| 152 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 153 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 154 | return IsAlwaysValidPointer(CE->getOperand(0)); |
| 155 | |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | /// AllCalleesPassInValidPointerForArgument - Return true if we can prove that |
| 160 | /// all callees pass in a valid pointer for the specified function argument. |
| 161 | static bool AllCalleesPassInValidPointerForArgument(Argument *Arg) { |
| 162 | Function *Callee = Arg->getParent(); |
| 163 | |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 164 | unsigned ArgNo = std::distance(Callee->arg_begin(), Function::arg_iterator(Arg)); |
Chris Lattner | 244031d | 2004-11-13 23:31:34 +0000 | [diff] [blame] | 165 | |
| 166 | // Look at all call sites of the function. At this pointer we know we only |
| 167 | // have direct callees. |
| 168 | for (Value::use_iterator UI = Callee->use_begin(), E = Callee->use_end(); |
| 169 | UI != E; ++UI) { |
| 170 | CallSite CS = CallSite::get(*UI); |
| 171 | assert(CS.getInstruction() && "Should only have direct calls!"); |
| 172 | |
| 173 | if (!IsAlwaysValidPointer(CS.getArgument(ArgNo))) |
| 174 | return false; |
| 175 | } |
| 176 | return true; |
| 177 | } |
| 178 | |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 179 | |
| 180 | /// isSafeToPromoteArgument - As you might guess from the name of this method, |
| 181 | /// it checks to see if it is both safe and useful to promote the argument. |
| 182 | /// This method limits promotion of aggregates to only promote up to three |
| 183 | /// elements of the aggregate in order to avoid exploding the number of |
| 184 | /// arguments passed in. |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 185 | bool ArgPromotion::isSafeToPromoteArgument(Argument *Arg) const { |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 186 | // We can only promote this argument if all of the uses are loads, or are GEP |
| 187 | // instructions (with constant indices) that are subsequently loaded. |
Chris Lattner | 244031d | 2004-11-13 23:31:34 +0000 | [diff] [blame] | 188 | bool HasLoadInEntryBlock = false; |
| 189 | BasicBlock *EntryBlock = Arg->getParent()->begin(); |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 190 | std::vector<LoadInst*> Loads; |
Chris Lattner | 1c676f7 | 2004-06-21 00:07:58 +0000 | [diff] [blame] | 191 | std::vector<std::vector<ConstantInt*> > GEPIndices; |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 192 | for (Value::use_iterator UI = Arg->use_begin(), E = Arg->use_end(); |
| 193 | UI != E; ++UI) |
| 194 | if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) { |
| 195 | if (LI->isVolatile()) return false; // Don't hack volatile loads |
| 196 | Loads.push_back(LI); |
Chris Lattner | 244031d | 2004-11-13 23:31:34 +0000 | [diff] [blame] | 197 | HasLoadInEntryBlock |= LI->getParent() == EntryBlock; |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 198 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) { |
| 199 | if (GEP->use_empty()) { |
| 200 | // Dead GEP's cause trouble later. Just remove them if we run into |
| 201 | // them. |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 202 | getAnalysis<AliasAnalysis>().deleteValue(GEP); |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 203 | GEP->getParent()->getInstList().erase(GEP); |
| 204 | return isSafeToPromoteArgument(Arg); |
| 205 | } |
| 206 | // Ensure that all of the indices are constants. |
Chris Lattner | 1c676f7 | 2004-06-21 00:07:58 +0000 | [diff] [blame] | 207 | std::vector<ConstantInt*> Operands; |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 208 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i) |
Chris Lattner | 1c676f7 | 2004-06-21 00:07:58 +0000 | [diff] [blame] | 209 | if (ConstantInt *C = dyn_cast<ConstantInt>(GEP->getOperand(i))) |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 210 | Operands.push_back(C); |
| 211 | else |
| 212 | return false; // Not a constant operand GEP! |
| 213 | |
| 214 | // Ensure that the only users of the GEP are load instructions. |
| 215 | for (Value::use_iterator UI = GEP->use_begin(), E = GEP->use_end(); |
| 216 | UI != E; ++UI) |
| 217 | if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) { |
| 218 | if (LI->isVolatile()) return false; // Don't hack volatile loads |
| 219 | Loads.push_back(LI); |
Chris Lattner | 244031d | 2004-11-13 23:31:34 +0000 | [diff] [blame] | 220 | HasLoadInEntryBlock |= LI->getParent() == EntryBlock; |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 221 | } else { |
| 222 | return false; |
| 223 | } |
| 224 | |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 225 | // See if there is already a GEP with these indices. If not, check to |
| 226 | // make sure that we aren't promoting too many elements. If so, nothing |
| 227 | // to do. |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 228 | if (std::find(GEPIndices.begin(), GEPIndices.end(), Operands) == |
| 229 | GEPIndices.end()) { |
| 230 | if (GEPIndices.size() == 3) { |
Bill Wendling | 8f13b5c | 2006-11-26 10:02:32 +0000 | [diff] [blame] | 231 | DOUT << "argpromotion disable promoting argument '" |
| 232 | << Arg->getName() << "' because it would require adding more " |
| 233 | << "than 3 arguments to the function.\n"; |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 234 | // We limit aggregate promotion to only promoting up to three elements |
| 235 | // of the aggregate. |
| 236 | return false; |
| 237 | } |
| 238 | GEPIndices.push_back(Operands); |
| 239 | } |
| 240 | } else { |
| 241 | return false; // Not a load or a GEP. |
| 242 | } |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 243 | |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 244 | if (Loads.empty()) return true; // No users, this is a dead argument. |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 245 | |
Chris Lattner | 244031d | 2004-11-13 23:31:34 +0000 | [diff] [blame] | 246 | // If we decide that we want to promote this argument, the value is going to |
| 247 | // be unconditionally loaded in all callees. This is only safe to do if the |
| 248 | // pointer was going to be unconditionally loaded anyway (i.e. there is a load |
| 249 | // of the pointer in the entry block of the function) or if we can prove that |
| 250 | // all pointers passed in are always to legal locations (for example, no null |
| 251 | // pointers are passed in, no pointers to free'd memory, etc). |
Evan Cheng | ff510a5 | 2006-10-03 07:26:07 +0000 | [diff] [blame] | 252 | if (!HasLoadInEntryBlock && !AllCalleesPassInValidPointerForArgument(Arg)) |
Chris Lattner | 244031d | 2004-11-13 23:31:34 +0000 | [diff] [blame] | 253 | return false; // Cannot prove that this is safe!! |
| 254 | |
| 255 | // Okay, now we know that the argument is only used by load instructions and |
| 256 | // it is safe to unconditionally load the pointer. Use alias analysis to |
| 257 | // check to see if the pointer is guaranteed to not be modified from entry of |
| 258 | // the function to each of the load instructions. |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 259 | |
| 260 | // Because there could be several/many load instructions, remember which |
| 261 | // blocks we know to be transparent to the load. |
| 262 | std::set<BasicBlock*> TranspBlocks; |
Owen Anderson | edadd3f | 2006-09-15 05:22:51 +0000 | [diff] [blame] | 263 | |
| 264 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 265 | TargetData &TD = getAnalysis<TargetData>(); |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 266 | |
| 267 | for (unsigned i = 0, e = Loads.size(); i != e; ++i) { |
| 268 | // Check to see if the load is invalidated from the start of the block to |
| 269 | // the load itself. |
| 270 | LoadInst *Load = Loads[i]; |
| 271 | BasicBlock *BB = Load->getParent(); |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 272 | |
| 273 | const PointerType *LoadTy = |
| 274 | cast<PointerType>(Load->getOperand(0)->getType()); |
Chris Lattner | 46fa04b | 2005-01-08 19:45:31 +0000 | [diff] [blame] | 275 | unsigned LoadSize = (unsigned)TD.getTypeSize(LoadTy->getElementType()); |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 276 | |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 277 | if (AA.canInstructionRangeModify(BB->front(), *Load, Arg, LoadSize)) |
| 278 | return false; // Pointer is invalidated! |
| 279 | |
| 280 | // Now check every path from the entry block to the load for transparency. |
| 281 | // To do this, we perform a depth first search on the inverse CFG from the |
| 282 | // loading block. |
| 283 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) |
| 284 | for (idf_ext_iterator<BasicBlock*> I = idf_ext_begin(*PI, TranspBlocks), |
| 285 | E = idf_ext_end(*PI, TranspBlocks); I != E; ++I) |
| 286 | if (AA.canBasicBlockModify(**I, Arg, LoadSize)) |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | // If the path from the entry of the function to each load is free of |
| 291 | // instructions that potentially invalidate the load, we can make the |
| 292 | // transformation! |
| 293 | return true; |
| 294 | } |
| 295 | |
Chris Lattner | 1c676f7 | 2004-06-21 00:07:58 +0000 | [diff] [blame] | 296 | namespace { |
| 297 | /// GEPIdxComparator - Provide a strong ordering for GEP indices. All Value* |
| 298 | /// elements are instances of ConstantInt. |
| 299 | /// |
| 300 | struct GEPIdxComparator { |
| 301 | bool operator()(const std::vector<Value*> &LHS, |
| 302 | const std::vector<Value*> &RHS) const { |
| 303 | unsigned idx = 0; |
| 304 | for (; idx < LHS.size() && idx < RHS.size(); ++idx) { |
| 305 | if (LHS[idx] != RHS[idx]) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 306 | return cast<ConstantInt>(LHS[idx])->getZExtValue() < |
| 307 | cast<ConstantInt>(RHS[idx])->getZExtValue(); |
Chris Lattner | 1c676f7 | 2004-06-21 00:07:58 +0000 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
| 311 | // Return less than if we ran out of stuff in LHS and we didn't run out of |
| 312 | // stuff in RHS. |
| 313 | return idx == LHS.size() && idx != RHS.size(); |
| 314 | } |
| 315 | }; |
| 316 | } |
| 317 | |
| 318 | |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 319 | /// DoPromotion - This method actually performs the promotion of the specified |
Chris Lattner | 37b6c4f | 2004-09-18 00:34:13 +0000 | [diff] [blame] | 320 | /// arguments, and returns the new function. At this point, we know that it's |
| 321 | /// safe to do so. |
| 322 | Function *ArgPromotion::DoPromotion(Function *F, |
| 323 | std::vector<Argument*> &Args2Prom) { |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 324 | std::set<Argument*> ArgsToPromote(Args2Prom.begin(), Args2Prom.end()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 325 | |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 326 | // Start by computing a new prototype for the function, which is the same as |
| 327 | // the old function, but has modified arguments. |
| 328 | const FunctionType *FTy = F->getFunctionType(); |
| 329 | std::vector<const Type*> Params; |
| 330 | |
Chris Lattner | 1c676f7 | 2004-06-21 00:07:58 +0000 | [diff] [blame] | 331 | typedef std::set<std::vector<Value*>, GEPIdxComparator> ScalarizeTable; |
| 332 | |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 333 | // ScalarizedElements - If we are promoting a pointer that has elements |
| 334 | // accessed out of it, keep track of which elements are accessed so that we |
| 335 | // can add one argument for each. |
| 336 | // |
| 337 | // Arguments that are directly loaded will have a zero element value here, to |
| 338 | // handle cases where there are both a direct load and GEP accesses. |
| 339 | // |
Chris Lattner | 1c676f7 | 2004-06-21 00:07:58 +0000 | [diff] [blame] | 340 | std::map<Argument*, ScalarizeTable> ScalarizedElements; |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 341 | |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 342 | // OriginalLoads - Keep track of a representative load instruction from the |
| 343 | // original function so that we can tell the alias analysis implementation |
| 344 | // what the new GEP/Load instructions we are inserting look like. |
| 345 | std::map<std::vector<Value*>, LoadInst*> OriginalLoads; |
| 346 | |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 347 | for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 348 | if (!ArgsToPromote.count(I)) { |
| 349 | Params.push_back(I->getType()); |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 350 | } else if (I->use_empty()) { |
| 351 | ++NumArgumentsDead; |
| 352 | } else { |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 353 | // Okay, this is being promoted. Check to see if there are any GEP uses |
| 354 | // of the argument. |
Chris Lattner | 1c676f7 | 2004-06-21 00:07:58 +0000 | [diff] [blame] | 355 | ScalarizeTable &ArgIndices = ScalarizedElements[I]; |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 356 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; |
| 357 | ++UI) { |
| 358 | Instruction *User = cast<Instruction>(*UI); |
Owen Anderson | edadd3f | 2006-09-15 05:22:51 +0000 | [diff] [blame] | 359 | assert(isa<LoadInst>(User) || isa<GetElementPtrInst>(User)); |
| 360 | std::vector<Value*> Indices(User->op_begin()+1, User->op_end()); |
| 361 | ArgIndices.insert(Indices); |
| 362 | LoadInst *OrigLoad; |
| 363 | if (LoadInst *L = dyn_cast<LoadInst>(User)) |
| 364 | OrigLoad = L; |
| 365 | else |
| 366 | OrigLoad = cast<LoadInst>(User->use_back()); |
| 367 | OriginalLoads[Indices] = OrigLoad; |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | // Add a parameter to the function for each element passed in. |
Chris Lattner | 1c676f7 | 2004-06-21 00:07:58 +0000 | [diff] [blame] | 371 | for (ScalarizeTable::iterator SI = ArgIndices.begin(), |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 372 | E = ArgIndices.end(); SI != E; ++SI) |
| 373 | Params.push_back(GetElementPtrInst::getIndexedType(I->getType(), *SI)); |
| 374 | |
| 375 | if (ArgIndices.size() == 1 && ArgIndices.begin()->empty()) |
| 376 | ++NumArgumentsPromoted; |
| 377 | else |
| 378 | ++NumAggregatesPromoted; |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | const Type *RetTy = FTy->getReturnType(); |
| 382 | |
| 383 | // Work around LLVM bug PR56: the CWriter cannot emit varargs functions which |
| 384 | // have zero fixed arguments. |
| 385 | bool ExtraArgHack = false; |
| 386 | if (Params.empty() && FTy->isVarArg()) { |
| 387 | ExtraArgHack = true; |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 388 | Params.push_back(Type::Int32Ty); |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 389 | } |
| 390 | FunctionType *NFTy = FunctionType::get(RetTy, Params, FTy->isVarArg()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 391 | |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 392 | // Create the new function body and insert it into the module... |
| 393 | Function *NF = new Function(NFTy, F->getLinkage(), F->getName()); |
Chris Lattner | d0525a2 | 2005-05-09 01:05:50 +0000 | [diff] [blame] | 394 | NF->setCallingConv(F->getCallingConv()); |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 395 | F->getParent()->getFunctionList().insert(F, NF); |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 396 | |
| 397 | // Get the alias analysis information that we need to update to reflect our |
| 398 | // changes. |
| 399 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
| 400 | |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 401 | // Loop over all of the callers of the function, transforming the call sites |
| 402 | // to pass in the loaded pointers. |
| 403 | // |
| 404 | std::vector<Value*> Args; |
| 405 | while (!F->use_empty()) { |
| 406 | CallSite CS = CallSite::get(F->use_back()); |
| 407 | Instruction *Call = CS.getInstruction(); |
| 408 | |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 409 | // Loop over the operands, inserting GEP and loads in the caller as |
| 410 | // appropriate. |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 411 | CallSite::arg_iterator AI = CS.arg_begin(); |
Chris Lattner | d0525a2 | 2005-05-09 01:05:50 +0000 | [diff] [blame] | 412 | for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 413 | I != E; ++I, ++AI) |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 414 | if (!ArgsToPromote.count(I)) |
| 415 | Args.push_back(*AI); // Unmodified argument |
| 416 | else if (!I->use_empty()) { |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 417 | // Non-dead argument: insert GEPs and loads as appropriate. |
Chris Lattner | 1c676f7 | 2004-06-21 00:07:58 +0000 | [diff] [blame] | 418 | ScalarizeTable &ArgIndices = ScalarizedElements[I]; |
| 419 | for (ScalarizeTable::iterator SI = ArgIndices.begin(), |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 420 | E = ArgIndices.end(); SI != E; ++SI) { |
| 421 | Value *V = *AI; |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 422 | LoadInst *OrigLoad = OriginalLoads[*SI]; |
| 423 | if (!SI->empty()) { |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 424 | V = new GetElementPtrInst(V, *SI, V->getName()+".idx", Call); |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 425 | AA.copyValue(OrigLoad->getOperand(0), V); |
| 426 | } |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 427 | Args.push_back(new LoadInst(V, V->getName()+".val", Call)); |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 428 | AA.copyValue(OrigLoad, Args.back()); |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 429 | } |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | if (ExtraArgHack) |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 433 | Args.push_back(Constant::getNullValue(Type::Int32Ty)); |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 434 | |
| 435 | // Push any varargs arguments on the list |
| 436 | for (; AI != CS.arg_end(); ++AI) |
| 437 | Args.push_back(*AI); |
| 438 | |
| 439 | Instruction *New; |
| 440 | if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) { |
| 441 | New = new InvokeInst(NF, II->getNormalDest(), II->getUnwindDest(), |
| 442 | Args, "", Call); |
Chris Lattner | d0525a2 | 2005-05-09 01:05:50 +0000 | [diff] [blame] | 443 | cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv()); |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 444 | } else { |
| 445 | New = new CallInst(NF, Args, "", Call); |
Chris Lattner | d0525a2 | 2005-05-09 01:05:50 +0000 | [diff] [blame] | 446 | cast<CallInst>(New)->setCallingConv(CS.getCallingConv()); |
Chris Lattner | 324d2ee | 2005-05-06 06:46:58 +0000 | [diff] [blame] | 447 | if (cast<CallInst>(Call)->isTailCall()) |
| 448 | cast<CallInst>(New)->setTailCall(); |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 449 | } |
| 450 | Args.clear(); |
| 451 | |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 452 | // Update the alias analysis implementation to know that we are replacing |
| 453 | // the old call with a new one. |
| 454 | AA.replaceWithNewValue(Call, New); |
| 455 | |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 456 | if (!Call->use_empty()) { |
| 457 | Call->replaceAllUsesWith(New); |
| 458 | std::string Name = Call->getName(); |
| 459 | Call->setName(""); |
| 460 | New->setName(Name); |
| 461 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 462 | |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 463 | // Finally, remove the old call from the program, reducing the use-count of |
| 464 | // F. |
| 465 | Call->getParent()->getInstList().erase(Call); |
| 466 | } |
| 467 | |
| 468 | // Since we have now created the new function, splice the body of the old |
| 469 | // function right into the new function, leaving the old rotting hulk of the |
| 470 | // function empty. |
| 471 | NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList()); |
| 472 | |
| 473 | // Loop over the argument list, transfering uses of the old arguments over to |
| 474 | // the new arguments, also transfering over the names as well. |
| 475 | // |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 476 | for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(), I2 = NF->arg_begin(); |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 477 | I != E; ++I) |
| 478 | if (!ArgsToPromote.count(I)) { |
| 479 | // If this is an unmodified argument, move the name and users over to the |
| 480 | // new version. |
| 481 | I->replaceAllUsesWith(I2); |
| 482 | I2->setName(I->getName()); |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 483 | AA.replaceWithNewValue(I, I2); |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 484 | ++I2; |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 485 | } else if (I->use_empty()) { |
| 486 | AA.deleteValue(I); |
| 487 | } else { |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 488 | // Otherwise, if we promoted this argument, then all users are load |
| 489 | // instructions, and all loads should be using the new argument that we |
| 490 | // added. |
Chris Lattner | 1c676f7 | 2004-06-21 00:07:58 +0000 | [diff] [blame] | 491 | ScalarizeTable &ArgIndices = ScalarizedElements[I]; |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 492 | |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 493 | while (!I->use_empty()) { |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 494 | if (LoadInst *LI = dyn_cast<LoadInst>(I->use_back())) { |
| 495 | assert(ArgIndices.begin()->empty() && |
| 496 | "Load element should sort to front!"); |
| 497 | I2->setName(I->getName()+".val"); |
| 498 | LI->replaceAllUsesWith(I2); |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 499 | AA.replaceWithNewValue(LI, I2); |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 500 | LI->getParent()->getInstList().erase(LI); |
Bill Wendling | 8f13b5c | 2006-11-26 10:02:32 +0000 | [diff] [blame] | 501 | DOUT << "*** Promoted load of argument '" << I->getName() |
| 502 | << "' in function '" << F->getName() << "'\n"; |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 503 | } else { |
| 504 | GetElementPtrInst *GEP = cast<GetElementPtrInst>(I->use_back()); |
| 505 | std::vector<Value*> Operands(GEP->op_begin()+1, GEP->op_end()); |
| 506 | |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 507 | Function::arg_iterator TheArg = I2; |
Chris Lattner | 1c676f7 | 2004-06-21 00:07:58 +0000 | [diff] [blame] | 508 | for (ScalarizeTable::iterator It = ArgIndices.begin(); |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 509 | *It != Operands; ++It, ++TheArg) { |
| 510 | assert(It != ArgIndices.end() && "GEP not handled??"); |
| 511 | } |
| 512 | |
| 513 | std::string NewName = I->getName(); |
| 514 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 515 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Operands[i])) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 516 | NewName += "."+itostr((int64_t)CI->getZExtValue()); |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 517 | else |
| 518 | NewName += ".x"; |
| 519 | TheArg->setName(NewName+".val"); |
| 520 | |
Bill Wendling | 8f13b5c | 2006-11-26 10:02:32 +0000 | [diff] [blame] | 521 | DOUT << "*** Promoted agg argument '" << TheArg->getName() |
| 522 | << "' of function '" << F->getName() << "'\n"; |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 523 | |
| 524 | // All of the uses must be load instructions. Replace them all with |
| 525 | // the argument specified by ArgNo. |
| 526 | while (!GEP->use_empty()) { |
| 527 | LoadInst *L = cast<LoadInst>(GEP->use_back()); |
| 528 | L->replaceAllUsesWith(TheArg); |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 529 | AA.replaceWithNewValue(L, TheArg); |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 530 | L->getParent()->getInstList().erase(L); |
| 531 | } |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 532 | AA.deleteValue(GEP); |
Chris Lattner | fe6f2e3 | 2004-03-08 01:04:36 +0000 | [diff] [blame] | 533 | GEP->getParent()->getInstList().erase(GEP); |
| 534 | } |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 535 | } |
Chris Lattner | cc544e5 | 2004-03-07 22:52:53 +0000 | [diff] [blame] | 536 | |
Chris Lattner | 37b6c4f | 2004-09-18 00:34:13 +0000 | [diff] [blame] | 537 | // Increment I2 past all of the arguments added for this promoted pointer. |
| 538 | for (unsigned i = 0, e = ArgIndices.size(); i != e; ++i) |
| 539 | ++I2; |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 542 | // Notify the alias analysis implementation that we inserted a new argument. |
| 543 | if (ExtraArgHack) |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 544 | AA.copyValue(Constant::getNullValue(Type::Int32Ty), NF->arg_begin()); |
Chris Lattner | 254f8f8 | 2004-05-23 21:21:17 +0000 | [diff] [blame] | 545 | |
| 546 | |
| 547 | // Tell the alias analysis that the old function is about to disappear. |
| 548 | AA.replaceWithNewValue(F, NF); |
| 549 | |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 550 | // Now that the old function is dead, delete it. |
| 551 | F->getParent()->getFunctionList().erase(F); |
Chris Lattner | 37b6c4f | 2004-09-18 00:34:13 +0000 | [diff] [blame] | 552 | return NF; |
Chris Lattner | 483ae01 | 2004-03-07 21:29:54 +0000 | [diff] [blame] | 553 | } |