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