blob: 26ff8d703a22d1f0d28b3bcf215c2d0ae8f51132 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- ArgumentPromotion.cpp - Promote by-reference arguments ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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 Henriksen9adadb42007-10-26 03:03:51 +000012// 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
Dan Gohmanf17a25c2007-07-18 16:29:46 +000014// instead of the address of the value. This can cause recursive simplification
15// of code and lead to the elimination of allocas (especially in C++ template
16// code like the STL).
17//
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
Chris Lattner57e8a5b2008-04-19 19:50:01 +000020// by default it refuses to scalarize aggregates which would require passing in more than
Gordon Henriksen9adadb42007-10-26 03:03:51 +000021// three operands to the function, because passing thousands of operands for a
Chris Lattner57e8a5b2008-04-19 19:50:01 +000022// large array or structure is unprofitable! This limit is can be configured or
23// disabled, however.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024//
25// Note that this transformation could also be done for arguments that are only
Gordon Henriksen9adadb42007-10-26 03:03:51 +000026// stored to (returning the value instead), but does not currently. This case
27// would be best handled when and if LLVM begins supporting multiple return
28// values from functions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029//
30//===----------------------------------------------------------------------===//
31
32#define DEBUG_TYPE "argpromotion"
33#include "llvm/Transforms/IPO.h"
34#include "llvm/Constants.h"
35#include "llvm/DerivedTypes.h"
36#include "llvm/Module.h"
37#include "llvm/CallGraphSCCPass.h"
38#include "llvm/Instructions.h"
39#include "llvm/Analysis/AliasAnalysis.h"
40#include "llvm/Analysis/CallGraph.h"
41#include "llvm/Target/TargetData.h"
42#include "llvm/Support/CallSite.h"
43#include "llvm/Support/CFG.h"
44#include "llvm/Support/Debug.h"
45#include "llvm/ADT/DepthFirstIterator.h"
46#include "llvm/ADT/Statistic.h"
47#include "llvm/ADT/StringExtras.h"
48#include "llvm/Support/Compiler.h"
49#include <set>
50using namespace llvm;
51
52STATISTIC(NumArgumentsPromoted , "Number of pointer arguments promoted");
53STATISTIC(NumAggregatesPromoted, "Number of aggregate arguments promoted");
Chris Lattnera1dde2b2008-01-11 22:31:41 +000054STATISTIC(NumByValArgsPromoted , "Number of byval arguments promoted");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055STATISTIC(NumArgumentsDead , "Number of dead pointer args eliminated");
56
57namespace {
58 /// ArgPromotion - The 'by reference' to 'by value' argument promotion pass.
59 ///
60 struct VISIBILITY_HIDDEN ArgPromotion : public CallGraphSCCPass {
61 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
62 AU.addRequired<AliasAnalysis>();
63 AU.addRequired<TargetData>();
64 CallGraphSCCPass::getAnalysisUsage(AU);
65 }
66
67 virtual bool runOnSCC(const std::vector<CallGraphNode *> &SCC);
68 static char ID; // Pass identification, replacement for typeid
Nate Begeman285e2e42008-05-13 01:48:26 +000069 ArgPromotion(unsigned maxElements = 3) : CallGraphSCCPass((intptr_t)&ID),
70 maxElements(maxElements) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071
72 private:
73 bool PromoteArguments(CallGraphNode *CGN);
Chris Lattner09ddfda2008-01-11 19:20:39 +000074 bool isSafeToPromoteArgument(Argument *Arg, bool isByVal) const;
Chris Lattner7ec41402008-01-11 18:43:58 +000075 Function *DoPromotion(Function *F,
Chris Lattnera1dde2b2008-01-11 22:31:41 +000076 SmallPtrSet<Argument*, 8> &ArgsToPromote,
77 SmallPtrSet<Argument*, 8> &ByValArgsToTransform);
Matthijs Kooijmandb5f9592008-05-23 07:57:02 +000078 /// The maximum number of elements to expand, or 0 for unlimited.
79 unsigned maxElements;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081}
82
Dan Gohman089efff2008-05-13 00:00:25 +000083char ArgPromotion::ID = 0;
84static RegisterPass<ArgPromotion>
85X("argpromotion", "Promote 'by reference' arguments to scalars");
86
Chris Lattner57e8a5b2008-04-19 19:50:01 +000087Pass *llvm::createArgumentPromotionPass(unsigned maxElements) {
88 return new ArgPromotion(maxElements);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089}
90
91bool ArgPromotion::runOnSCC(const std::vector<CallGraphNode *> &SCC) {
92 bool Changed = false, LocalChange;
93
94 do { // Iterate until we stop promoting from this SCC.
95 LocalChange = false;
96 // Attempt to promote arguments from all functions in this SCC.
97 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
98 LocalChange |= PromoteArguments(SCC[i]);
99 Changed |= LocalChange; // Remember that we changed something.
100 } while (LocalChange);
101
102 return Changed;
103}
104
105/// PromoteArguments - This method checks the specified function to see if there
106/// are any promotable arguments and if it is safe to promote the function (for
107/// example, all callers are direct). If safe to promote some arguments, it
108/// calls the DoPromotion method.
109///
110bool ArgPromotion::PromoteArguments(CallGraphNode *CGN) {
111 Function *F = CGN->getFunction();
112
113 // Make sure that it is local to this module.
114 if (!F || !F->hasInternalLinkage()) return false;
115
116 // First check: see if there are any pointer arguments! If not, quick exit.
Chris Lattner09ddfda2008-01-11 19:20:39 +0000117 SmallVector<std::pair<Argument*, unsigned>, 16> PointerArgs;
118 unsigned ArgNo = 0;
119 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
120 I != E; ++I, ++ArgNo)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 if (isa<PointerType>(I->getType()))
Chris Lattner09ddfda2008-01-11 19:20:39 +0000122 PointerArgs.push_back(std::pair<Argument*, unsigned>(I, ArgNo));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 if (PointerArgs.empty()) return false;
124
125 // Second check: make sure that all callers are direct callers. We can't
126 // transform functions that have indirect callers.
127 for (Value::use_iterator UI = F->use_begin(), E = F->use_end();
128 UI != E; ++UI) {
129 CallSite CS = CallSite::get(*UI);
130 if (!CS.getInstruction()) // "Taking the address" of the function
131 return false;
132
133 // Ensure that this call site is CALLING the function, not passing it as
134 // an argument.
Chris Lattner16d8bdd2008-01-11 18:55:10 +0000135 if (UI.getOperandNo() != 0)
136 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 }
138
Chris Lattner09ddfda2008-01-11 19:20:39 +0000139 // Check to see which arguments are promotable. If an argument is promotable,
140 // add it to ArgsToPromote.
141 SmallPtrSet<Argument*, 8> ArgsToPromote;
Chris Lattnera1dde2b2008-01-11 22:31:41 +0000142 SmallPtrSet<Argument*, 8> ByValArgsToTransform;
Chris Lattner09ddfda2008-01-11 19:20:39 +0000143 for (unsigned i = 0; i != PointerArgs.size(); ++i) {
Chris Lattnera1dde2b2008-01-11 22:31:41 +0000144 bool isByVal = F->paramHasAttr(PointerArgs[i].second+1, ParamAttr::ByVal);
145
146 // If this is a byval argument, and if the aggregate type is small, just
147 // pass the elements, which is always safe.
148 Argument *PtrArg = PointerArgs[i].first;
149 if (isByVal) {
150 const Type *AgTy = cast<PointerType>(PtrArg->getType())->getElementType();
Duncan Sandsf6890712008-05-27 11:50:51 +0000151 if (const StructType *STy = dyn_cast<StructType>(AgTy)) {
Chris Lattner57e8a5b2008-04-19 19:50:01 +0000152 if (maxElements > 0 && STy->getNumElements() > maxElements) {
153 DOUT << "argpromotion disable promoting argument '"
154 << PtrArg->getName() << "' because it would require adding more "
155 << "than " << maxElements << " arguments to the function.\n";
156 } else {
Dan Gohman5e8fbc22008-05-23 00:17:26 +0000157 // If all the elements are single-value types, we can promote it.
Chris Lattnera1dde2b2008-01-11 22:31:41 +0000158 bool AllSimple = true;
159 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Dan Gohman5e8fbc22008-05-23 00:17:26 +0000160 if (!STy->getElementType(i)->isSingleValueType()) {
Chris Lattnera1dde2b2008-01-11 22:31:41 +0000161 AllSimple = false;
162 break;
163 }
164
165 // Safe to transform, don't even bother trying to "promote" it.
166 // Passing the elements as a scalar will allow scalarrepl to hack on
167 // the new alloca we introduce.
168 if (AllSimple) {
169 ByValArgsToTransform.insert(PtrArg);
170 continue;
171 }
172 }
Duncan Sandsf6890712008-05-27 11:50:51 +0000173 }
Chris Lattnera1dde2b2008-01-11 22:31:41 +0000174 }
175
176 // Otherwise, see if we can promote the pointer to its value.
177 if (isSafeToPromoteArgument(PtrArg, isByVal))
178 ArgsToPromote.insert(PtrArg);
Chris Lattner09ddfda2008-01-11 19:20:39 +0000179 }
180
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 // No promotable pointer arguments.
Chris Lattnera1dde2b2008-01-11 22:31:41 +0000182 if (ArgsToPromote.empty() && ByValArgsToTransform.empty()) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183
Chris Lattnera1dde2b2008-01-11 22:31:41 +0000184 Function *NewF = DoPromotion(F, ArgsToPromote, ByValArgsToTransform);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185
Duncan Sandsf5588dc2007-11-27 13:23:08 +0000186 // Update the call graph to know that the function has been transformed.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 getAnalysis<CallGraph>().changeFunction(F, NewF);
188 return true;
189}
190
191/// IsAlwaysValidPointer - Return true if the specified pointer is always legal
192/// to load.
193static bool IsAlwaysValidPointer(Value *V) {
194 if (isa<AllocaInst>(V) || isa<GlobalVariable>(V)) return true;
195 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V))
196 return IsAlwaysValidPointer(GEP->getOperand(0));
197 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
198 if (CE->getOpcode() == Instruction::GetElementPtr)
199 return IsAlwaysValidPointer(CE->getOperand(0));
200
201 return false;
202}
203
204/// AllCalleesPassInValidPointerForArgument - Return true if we can prove that
205/// all callees pass in a valid pointer for the specified function argument.
206static bool AllCalleesPassInValidPointerForArgument(Argument *Arg) {
207 Function *Callee = Arg->getParent();
208
209 unsigned ArgNo = std::distance(Callee->arg_begin(),
210 Function::arg_iterator(Arg));
211
212 // Look at all call sites of the function. At this pointer we know we only
213 // have direct callees.
214 for (Value::use_iterator UI = Callee->use_begin(), E = Callee->use_end();
215 UI != E; ++UI) {
216 CallSite CS = CallSite::get(*UI);
217 assert(CS.getInstruction() && "Should only have direct calls!");
218
219 if (!IsAlwaysValidPointer(CS.getArgument(ArgNo)))
220 return false;
221 }
222 return true;
223}
224
225
226/// isSafeToPromoteArgument - As you might guess from the name of this method,
227/// it checks to see if it is both safe and useful to promote the argument.
228/// This method limits promotion of aggregates to only promote up to three
229/// elements of the aggregate in order to avoid exploding the number of
230/// arguments passed in.
Chris Lattner09ddfda2008-01-11 19:20:39 +0000231bool ArgPromotion::isSafeToPromoteArgument(Argument *Arg, bool isByVal) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 // We can only promote this argument if all of the uses are loads, or are GEP
233 // instructions (with constant indices) that are subsequently loaded.
Chris Lattner41e0d202008-01-11 19:34:32 +0000234
235 // We can also only promote the load if we can guarantee that it will happen.
236 // Promoting a load causes the load to be unconditionally executed in the
237 // caller, so we can't turn a conditional load into an unconditional load in
238 // general.
239 bool SafeToUnconditionallyLoad = false;
240 if (isByVal) // ByVal arguments are always safe to load from.
241 SafeToUnconditionallyLoad = true;
242
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 BasicBlock *EntryBlock = Arg->getParent()->begin();
Chris Lattner7ec41402008-01-11 18:43:58 +0000244 SmallVector<LoadInst*, 16> Loads;
245 std::vector<SmallVector<ConstantInt*, 8> > GEPIndices;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 for (Value::use_iterator UI = Arg->use_begin(), E = Arg->use_end();
247 UI != E; ++UI)
248 if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
249 if (LI->isVolatile()) return false; // Don't hack volatile loads
250 Loads.push_back(LI);
Chris Lattner41e0d202008-01-11 19:34:32 +0000251
252 // If this load occurs in the entry block, then the pointer is
253 // unconditionally loaded.
254 SafeToUnconditionallyLoad |= LI->getParent() == EntryBlock;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) {
256 if (GEP->use_empty()) {
257 // Dead GEP's cause trouble later. Just remove them if we run into
258 // them.
259 getAnalysis<AliasAnalysis>().deleteValue(GEP);
Chris Lattner09ddfda2008-01-11 19:20:39 +0000260 GEP->eraseFromParent();
261 return isSafeToPromoteArgument(Arg, isByVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 }
263 // Ensure that all of the indices are constants.
Chris Lattner7ec41402008-01-11 18:43:58 +0000264 SmallVector<ConstantInt*, 8> Operands;
Gabor Greif20f03f52008-05-29 01:59:18 +0000265 for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end();
266 i != e; ++i)
267 if (ConstantInt *C = dyn_cast<ConstantInt>(*i))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 Operands.push_back(C);
269 else
270 return false; // Not a constant operand GEP!
271
272 // Ensure that the only users of the GEP are load instructions.
273 for (Value::use_iterator UI = GEP->use_begin(), E = GEP->use_end();
274 UI != E; ++UI)
275 if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
276 if (LI->isVolatile()) return false; // Don't hack volatile loads
277 Loads.push_back(LI);
Chris Lattner41e0d202008-01-11 19:34:32 +0000278
279 // If this load occurs in the entry block, then the pointer is
280 // unconditionally loaded.
281 SafeToUnconditionallyLoad |= LI->getParent() == EntryBlock;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282 } else {
283 return false;
284 }
285
286 // See if there is already a GEP with these indices. If not, check to
287 // make sure that we aren't promoting too many elements. If so, nothing
288 // to do.
289 if (std::find(GEPIndices.begin(), GEPIndices.end(), Operands) ==
290 GEPIndices.end()) {
Chris Lattner57e8a5b2008-04-19 19:50:01 +0000291 if (maxElements > 0 && GEPIndices.size() == maxElements) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292 DOUT << "argpromotion disable promoting argument '"
293 << Arg->getName() << "' because it would require adding more "
Chris Lattner57e8a5b2008-04-19 19:50:01 +0000294 << "than " << maxElements << " arguments to the function.\n";
295 // We limit aggregate promotion to only promoting up to a fixed number
Nate Begeman285e2e42008-05-13 01:48:26 +0000296 // of elements of the aggregate.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 return false;
298 }
299 GEPIndices.push_back(Operands);
300 }
301 } else {
302 return false; // Not a load or a GEP.
303 }
304
305 if (Loads.empty()) return true; // No users, this is a dead argument.
306
307 // If we decide that we want to promote this argument, the value is going to
308 // be unconditionally loaded in all callees. This is only safe to do if the
309 // pointer was going to be unconditionally loaded anyway (i.e. there is a load
310 // of the pointer in the entry block of the function) or if we can prove that
311 // all pointers passed in are always to legal locations (for example, no null
312 // pointers are passed in, no pointers to free'd memory, etc).
Chris Lattner41e0d202008-01-11 19:34:32 +0000313 if (!SafeToUnconditionallyLoad &&
314 !AllCalleesPassInValidPointerForArgument(Arg))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315 return false; // Cannot prove that this is safe!!
316
317 // Okay, now we know that the argument is only used by load instructions and
318 // it is safe to unconditionally load the pointer. Use alias analysis to
319 // check to see if the pointer is guaranteed to not be modified from entry of
320 // the function to each of the load instructions.
321
322 // Because there could be several/many load instructions, remember which
323 // blocks we know to be transparent to the load.
Chris Lattnerba799be2008-01-11 19:36:30 +0000324 SmallPtrSet<BasicBlock*, 16> TranspBlocks;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325
326 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
327 TargetData &TD = getAnalysis<TargetData>();
328
329 for (unsigned i = 0, e = Loads.size(); i != e; ++i) {
330 // Check to see if the load is invalidated from the start of the block to
331 // the load itself.
332 LoadInst *Load = Loads[i];
333 BasicBlock *BB = Load->getParent();
334
335 const PointerType *LoadTy =
336 cast<PointerType>(Load->getOperand(0)->getType());
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000337 unsigned LoadSize = (unsigned)TD.getTypeStoreSize(LoadTy->getElementType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338
339 if (AA.canInstructionRangeModify(BB->front(), *Load, Arg, LoadSize))
340 return false; // Pointer is invalidated!
341
342 // Now check every path from the entry block to the load for transparency.
343 // To do this, we perform a depth first search on the inverse CFG from the
344 // loading block.
345 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
Chris Lattnerba799be2008-01-11 19:36:30 +0000346 for (idf_ext_iterator<BasicBlock*, SmallPtrSet<BasicBlock*, 16> >
347 I = idf_ext_begin(*PI, TranspBlocks),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348 E = idf_ext_end(*PI, TranspBlocks); I != E; ++I)
349 if (AA.canBasicBlockModify(**I, Arg, LoadSize))
350 return false;
351 }
352
353 // If the path from the entry of the function to each load is free of
354 // instructions that potentially invalidate the load, we can make the
355 // transformation!
356 return true;
357}
358
359namespace {
360 /// GEPIdxComparator - Provide a strong ordering for GEP indices. All Value*
361 /// elements are instances of ConstantInt.
362 ///
363 struct GEPIdxComparator {
364 bool operator()(const std::vector<Value*> &LHS,
365 const std::vector<Value*> &RHS) const {
366 unsigned idx = 0;
367 for (; idx < LHS.size() && idx < RHS.size(); ++idx) {
368 if (LHS[idx] != RHS[idx]) {
369 return cast<ConstantInt>(LHS[idx])->getZExtValue() <
370 cast<ConstantInt>(RHS[idx])->getZExtValue();
371 }
372 }
373
374 // Return less than if we ran out of stuff in LHS and we didn't run out of
375 // stuff in RHS.
376 return idx == LHS.size() && idx != RHS.size();
377 }
378 };
379}
380
381
382/// DoPromotion - This method actually performs the promotion of the specified
383/// arguments, and returns the new function. At this point, we know that it's
384/// safe to do so.
385Function *ArgPromotion::DoPromotion(Function *F,
Chris Lattnera1dde2b2008-01-11 22:31:41 +0000386 SmallPtrSet<Argument*, 8> &ArgsToPromote,
387 SmallPtrSet<Argument*, 8> &ByValArgsToTransform) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000388
389 // Start by computing a new prototype for the function, which is the same as
390 // the old function, but has modified arguments.
391 const FunctionType *FTy = F->getFunctionType();
392 std::vector<const Type*> Params;
393
394 typedef std::set<std::vector<Value*>, GEPIdxComparator> ScalarizeTable;
395
396 // ScalarizedElements - If we are promoting a pointer that has elements
397 // accessed out of it, keep track of which elements are accessed so that we
398 // can add one argument for each.
399 //
400 // Arguments that are directly loaded will have a zero element value here, to
401 // handle cases where there are both a direct load and GEP accesses.
402 //
403 std::map<Argument*, ScalarizeTable> ScalarizedElements;
404
405 // OriginalLoads - Keep track of a representative load instruction from the
406 // original function so that we can tell the alias analysis implementation
407 // what the new GEP/Load instructions we are inserting look like.
408 std::map<std::vector<Value*>, LoadInst*> OriginalLoads;
409
Duncan Sandsf5588dc2007-11-27 13:23:08 +0000410 // ParamAttrs - Keep track of the parameter attributes for the arguments
411 // that we are *not* promoting. For the ones that we do promote, the parameter
412 // attributes are lost
Chris Lattner1c8733e2008-03-12 17:45:29 +0000413 SmallVector<ParamAttrsWithIndex, 8> ParamAttrsVec;
414 const PAListPtr &PAL = F->getParamAttrs();
Duncan Sandsf5588dc2007-11-27 13:23:08 +0000415
Duncan Sands23071512008-02-01 20:37:16 +0000416 // Add any return attributes.
Chris Lattner1c8733e2008-03-12 17:45:29 +0000417 if (ParameterAttributes attrs = PAL.getParamAttrs(0))
Duncan Sands23071512008-02-01 20:37:16 +0000418 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(0, attrs));
419
Chris Lattneraf5a54a2008-01-17 01:17:03 +0000420 unsigned ArgIndex = 1;
Duncan Sandsf5588dc2007-11-27 13:23:08 +0000421 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E;
Chris Lattneraf5a54a2008-01-17 01:17:03 +0000422 ++I, ++ArgIndex) {
Chris Lattnera1dde2b2008-01-11 22:31:41 +0000423 if (ByValArgsToTransform.count(I)) {
424 // Just add all the struct element types.
425 const Type *AgTy = cast<PointerType>(I->getType())->getElementType();
426 const StructType *STy = cast<StructType>(AgTy);
427 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
428 Params.push_back(STy->getElementType(i));
429 ++NumByValArgsPromoted;
430 } else if (!ArgsToPromote.count(I)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431 Params.push_back(I->getType());
Chris Lattner1c8733e2008-03-12 17:45:29 +0000432 if (ParameterAttributes attrs = PAL.getParamAttrs(ArgIndex))
Chris Lattnera1dde2b2008-01-11 22:31:41 +0000433 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Params.size(), attrs));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434 } else if (I->use_empty()) {
435 ++NumArgumentsDead;
436 } else {
437 // Okay, this is being promoted. Check to see if there are any GEP uses
438 // of the argument.
439 ScalarizeTable &ArgIndices = ScalarizedElements[I];
440 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;
441 ++UI) {
442 Instruction *User = cast<Instruction>(*UI);
443 assert(isa<LoadInst>(User) || isa<GetElementPtrInst>(User));
444 std::vector<Value*> Indices(User->op_begin()+1, User->op_end());
445 ArgIndices.insert(Indices);
446 LoadInst *OrigLoad;
447 if (LoadInst *L = dyn_cast<LoadInst>(User))
448 OrigLoad = L;
449 else
450 OrigLoad = cast<LoadInst>(User->use_back());
451 OriginalLoads[Indices] = OrigLoad;
452 }
453
454 // Add a parameter to the function for each element passed in.
455 for (ScalarizeTable::iterator SI = ArgIndices.begin(),
456 E = ArgIndices.end(); SI != E; ++SI)
457 Params.push_back(GetElementPtrInst::getIndexedType(I->getType(),
David Greene393be882007-09-04 15:46:09 +0000458 SI->begin(),
459 SI->end()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460
461 if (ArgIndices.size() == 1 && ArgIndices.begin()->empty())
462 ++NumArgumentsPromoted;
463 else
464 ++NumAggregatesPromoted;
465 }
Chris Lattnera1dde2b2008-01-11 22:31:41 +0000466 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467
468 const Type *RetTy = FTy->getReturnType();
469
470 // Work around LLVM bug PR56: the CWriter cannot emit varargs functions which
471 // have zero fixed arguments.
472 bool ExtraArgHack = false;
473 if (Params.empty() && FTy->isVarArg()) {
474 ExtraArgHack = true;
475 Params.push_back(Type::Int32Ty);
476 }
Duncan Sandsf5588dc2007-11-27 13:23:08 +0000477
478 // Construct the new function type using the new arguments.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000479 FunctionType *NFTy = FunctionType::get(RetTy, Params, FTy->isVarArg());
480
Duncan Sandsf5588dc2007-11-27 13:23:08 +0000481 // Create the new function body and insert it into the module...
Gabor Greifd6da1d02008-04-06 20:25:17 +0000482 Function *NF = Function::Create(NFTy, F->getLinkage(), F->getName());
Duncan Sands0cc90582008-05-26 19:58:59 +0000483 NF->copyAttributesFrom(F);
Chris Lattneraf5a54a2008-01-17 01:17:03 +0000484
485 // Recompute the parameter attributes list based on the new arguments for
486 // the function.
Chris Lattner1c8733e2008-03-12 17:45:29 +0000487 NF->setParamAttrs(PAListPtr::get(ParamAttrsVec.begin(), ParamAttrsVec.end()));
488 ParamAttrsVec.clear();
Duncan Sands0cc90582008-05-26 19:58:59 +0000489
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490 F->getParent()->getFunctionList().insert(F, NF);
Zhou Sheng7f161cc2008-03-20 08:05:05 +0000491 NF->takeName(F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492
493 // Get the alias analysis information that we need to update to reflect our
494 // changes.
495 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
496
497 // Loop over all of the callers of the function, transforming the call sites
498 // to pass in the loaded pointers.
499 //
Chris Lattneraf5a54a2008-01-17 01:17:03 +0000500 SmallVector<Value*, 16> Args;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501 while (!F->use_empty()) {
502 CallSite CS = CallSite::get(F->use_back());
503 Instruction *Call = CS.getInstruction();
Chris Lattner1c8733e2008-03-12 17:45:29 +0000504 const PAListPtr &CallPAL = CS.getParamAttrs();
Chris Lattneraf5a54a2008-01-17 01:17:03 +0000505
Duncan Sands23071512008-02-01 20:37:16 +0000506 // Add any return attributes.
Chris Lattner1c8733e2008-03-12 17:45:29 +0000507 if (ParameterAttributes attrs = CallPAL.getParamAttrs(0))
Duncan Sands23071512008-02-01 20:37:16 +0000508 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(0, attrs));
509
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000510 // Loop over the operands, inserting GEP and loads in the caller as
511 // appropriate.
512 CallSite::arg_iterator AI = CS.arg_begin();
Chris Lattneraf5a54a2008-01-17 01:17:03 +0000513 ArgIndex = 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000514 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
Chris Lattneraf5a54a2008-01-17 01:17:03 +0000515 I != E; ++I, ++AI, ++ArgIndex)
Chris Lattnera1dde2b2008-01-11 22:31:41 +0000516 if (!ArgsToPromote.count(I) && !ByValArgsToTransform.count(I)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 Args.push_back(*AI); // Unmodified argument
Chris Lattneraf5a54a2008-01-17 01:17:03 +0000518
Chris Lattner1c8733e2008-03-12 17:45:29 +0000519 if (ParameterAttributes Attrs = CallPAL.getParamAttrs(ArgIndex))
Chris Lattneraf5a54a2008-01-17 01:17:03 +0000520 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Args.size(), Attrs));
521
Chris Lattnera1dde2b2008-01-11 22:31:41 +0000522 } else if (ByValArgsToTransform.count(I)) {
523 // Emit a GEP and load for each element of the struct.
524 const Type *AgTy = cast<PointerType>(I->getType())->getElementType();
525 const StructType *STy = cast<StructType>(AgTy);
526 Value *Idxs[2] = { ConstantInt::get(Type::Int32Ty, 0), 0 };
527 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
528 Idxs[1] = ConstantInt::get(Type::Int32Ty, i);
Gabor Greifd6da1d02008-04-06 20:25:17 +0000529 Value *Idx = GetElementPtrInst::Create(*AI, Idxs, Idxs+2,
530 (*AI)->getName()+"."+utostr(i),
531 Call);
Chris Lattnera1dde2b2008-01-11 22:31:41 +0000532 // TODO: Tell AA about the new values?
533 Args.push_back(new LoadInst(Idx, Idx->getName()+".val", Call));
534 }
535 } else if (!I->use_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000536 // Non-dead argument: insert GEPs and loads as appropriate.
537 ScalarizeTable &ArgIndices = ScalarizedElements[I];
538 for (ScalarizeTable::iterator SI = ArgIndices.begin(),
539 E = ArgIndices.end(); SI != E; ++SI) {
540 Value *V = *AI;
541 LoadInst *OrigLoad = OriginalLoads[*SI];
542 if (!SI->empty()) {
Gabor Greifd6da1d02008-04-06 20:25:17 +0000543 V = GetElementPtrInst::Create(V, SI->begin(), SI->end(),
544 V->getName()+".idx", Call);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000545 AA.copyValue(OrigLoad->getOperand(0), V);
546 }
547 Args.push_back(new LoadInst(V, V->getName()+".val", Call));
548 AA.copyValue(OrigLoad, Args.back());
549 }
550 }
551
552 if (ExtraArgHack)
553 Args.push_back(Constant::getNullValue(Type::Int32Ty));
554
555 // Push any varargs arguments on the list
Chris Lattneraf5a54a2008-01-17 01:17:03 +0000556 for (; AI != CS.arg_end(); ++AI, ++ArgIndex) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000557 Args.push_back(*AI);
Chris Lattner1c8733e2008-03-12 17:45:29 +0000558 if (ParameterAttributes Attrs = CallPAL.getParamAttrs(ArgIndex))
Chris Lattneraf5a54a2008-01-17 01:17:03 +0000559 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Args.size(), Attrs));
560 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000561
562 Instruction *New;
563 if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
Gabor Greifd6da1d02008-04-06 20:25:17 +0000564 New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),
565 Args.begin(), Args.end(), "", Call);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566 cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
Chris Lattner1c8733e2008-03-12 17:45:29 +0000567 cast<InvokeInst>(New)->setParamAttrs(PAListPtr::get(ParamAttrsVec.begin(),
568 ParamAttrsVec.end()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000569 } else {
Gabor Greifd6da1d02008-04-06 20:25:17 +0000570 New = CallInst::Create(NF, Args.begin(), Args.end(), "", Call);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000571 cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
Chris Lattner1c8733e2008-03-12 17:45:29 +0000572 cast<CallInst>(New)->setParamAttrs(PAListPtr::get(ParamAttrsVec.begin(),
573 ParamAttrsVec.end()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574 if (cast<CallInst>(Call)->isTailCall())
575 cast<CallInst>(New)->setTailCall();
576 }
577 Args.clear();
Chris Lattneraf5a54a2008-01-17 01:17:03 +0000578 ParamAttrsVec.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000579
580 // Update the alias analysis implementation to know that we are replacing
581 // the old call with a new one.
582 AA.replaceWithNewValue(Call, New);
583
584 if (!Call->use_empty()) {
585 Call->replaceAllUsesWith(New);
586 New->takeName(Call);
587 }
588
589 // Finally, remove the old call from the program, reducing the use-count of
590 // F.
Chris Lattner09ddfda2008-01-11 19:20:39 +0000591 Call->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592 }
593
594 // Since we have now created the new function, splice the body of the old
595 // function right into the new function, leaving the old rotting hulk of the
596 // function empty.
597 NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());
598
599 // Loop over the argument list, transfering uses of the old arguments over to
600 // the new arguments, also transfering over the names as well.
601 //
602 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(),
Chris Lattnera1dde2b2008-01-11 22:31:41 +0000603 I2 = NF->arg_begin(); I != E; ++I) {
604 if (!ArgsToPromote.count(I) && !ByValArgsToTransform.count(I)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000605 // If this is an unmodified argument, move the name and users over to the
606 // new version.
607 I->replaceAllUsesWith(I2);
608 I2->takeName(I);
609 AA.replaceWithNewValue(I, I2);
610 ++I2;
Chris Lattnera1dde2b2008-01-11 22:31:41 +0000611 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 }
Chris Lattnera1dde2b2008-01-11 22:31:41 +0000613
614 if (ByValArgsToTransform.count(I)) {
615 // In the callee, we create an alloca, and store each of the new incoming
616 // arguments into the alloca.
617 Instruction *InsertPt = NF->begin()->begin();
618
619 // Just add all the struct element types.
620 const Type *AgTy = cast<PointerType>(I->getType())->getElementType();
621 Value *TheAlloca = new AllocaInst(AgTy, 0, "", InsertPt);
622 const StructType *STy = cast<StructType>(AgTy);
623 Value *Idxs[2] = { ConstantInt::get(Type::Int32Ty, 0), 0 };
624
625 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
626 Idxs[1] = ConstantInt::get(Type::Int32Ty, i);
Gabor Greifd6da1d02008-04-06 20:25:17 +0000627 Value *Idx = GetElementPtrInst::Create(TheAlloca, Idxs, Idxs+2,
628 TheAlloca->getName()+"."+utostr(i),
629 InsertPt);
Chris Lattnera1dde2b2008-01-11 22:31:41 +0000630 I2->setName(I->getName()+"."+utostr(i));
631 new StoreInst(I2++, Idx, InsertPt);
632 }
633
634 // Anything that used the arg should now use the alloca.
635 I->replaceAllUsesWith(TheAlloca);
636 TheAlloca->takeName(I);
637 AA.replaceWithNewValue(I, TheAlloca);
638 continue;
639 }
640
641 if (I->use_empty()) {
642 AA.deleteValue(I);
643 continue;
644 }
645
646 // Otherwise, if we promoted this argument, then all users are load
647 // instructions, and all loads should be using the new argument that we
648 // added.
649 ScalarizeTable &ArgIndices = ScalarizedElements[I];
650
651 while (!I->use_empty()) {
652 if (LoadInst *LI = dyn_cast<LoadInst>(I->use_back())) {
653 assert(ArgIndices.begin()->empty() &&
654 "Load element should sort to front!");
655 I2->setName(I->getName()+".val");
656 LI->replaceAllUsesWith(I2);
657 AA.replaceWithNewValue(LI, I2);
658 LI->eraseFromParent();
659 DOUT << "*** Promoted load of argument '" << I->getName()
660 << "' in function '" << F->getName() << "'\n";
661 } else {
662 GetElementPtrInst *GEP = cast<GetElementPtrInst>(I->use_back());
663 std::vector<Value*> Operands(GEP->op_begin()+1, GEP->op_end());
664
665 Function::arg_iterator TheArg = I2;
666 for (ScalarizeTable::iterator It = ArgIndices.begin();
667 *It != Operands; ++It, ++TheArg) {
668 assert(It != ArgIndices.end() && "GEP not handled??");
669 }
670
671 std::string NewName = I->getName();
672 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
673 if (ConstantInt *CI = dyn_cast<ConstantInt>(Operands[i]))
674 NewName += "." + CI->getValue().toStringUnsigned(10);
675 else
676 NewName += ".x";
677 TheArg->setName(NewName+".val");
678
679 DOUT << "*** Promoted agg argument '" << TheArg->getName()
680 << "' of function '" << F->getName() << "'\n";
681
682 // All of the uses must be load instructions. Replace them all with
683 // the argument specified by ArgNo.
684 while (!GEP->use_empty()) {
685 LoadInst *L = cast<LoadInst>(GEP->use_back());
686 L->replaceAllUsesWith(TheArg);
687 AA.replaceWithNewValue(L, TheArg);
688 L->eraseFromParent();
689 }
690 AA.deleteValue(GEP);
691 GEP->eraseFromParent();
692 }
693 }
694
695 // Increment I2 past all of the arguments added for this promoted pointer.
696 for (unsigned i = 0, e = ArgIndices.size(); i != e; ++i)
697 ++I2;
698 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000699
700 // Notify the alias analysis implementation that we inserted a new argument.
701 if (ExtraArgHack)
702 AA.copyValue(Constant::getNullValue(Type::Int32Ty), NF->arg_begin());
703
704
705 // Tell the alias analysis that the old function is about to disappear.
706 AA.replaceWithNewValue(F, NF);
707
708 // Now that the old function is dead, delete it.
Chris Lattner09ddfda2008-01-11 19:20:39 +0000709 F->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000710 return NF;
711}