1 //===-- ArgumentPromotion.cpp - Promote by-reference arguments ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass promotes "by reference" arguments to be "by value" arguments. In
11 // practice, this means looking for internal functions that have pointer
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
14 // 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).
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 // by default 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! This limit is can be configured or
25 // Note that this transformation could also be done for arguments that are only
26 // 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.
30 //===----------------------------------------------------------------------===//
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"
52 STATISTIC(NumArgumentsPromoted , "Number of pointer arguments promoted");
53 STATISTIC(NumAggregatesPromoted, "Number of aggregate arguments promoted");
54 STATISTIC(NumByValArgsPromoted , "Number of byval arguments promoted");
55 STATISTIC(NumArgumentsDead , "Number of dead pointer args eliminated");
58 /// ArgPromotion - The 'by reference' to 'by value' argument promotion pass.
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);
67 virtual bool runOnSCC(const std::vector<CallGraphNode *> &SCC);
68 static char ID; // Pass identification, replacement for typeid
69 ArgPromotion(unsigned maxElements = 3) : CallGraphSCCPass((intptr_t)&ID),
70 maxElements(maxElements) {}
73 bool PromoteArguments(CallGraphNode *CGN);
74 bool isSafeToPromoteArgument(Argument *Arg, bool isByVal) const;
75 Function *DoPromotion(Function *F,
76 SmallPtrSet<Argument*, 8> &ArgsToPromote,
77 SmallPtrSet<Argument*, 8> &ByValArgsToTransform);
78 /// The maximum number of elements to expand, or 0 for unlimited.
83 char ArgPromotion::ID = 0;
84 static RegisterPass<ArgPromotion>
85 X("argpromotion", "Promote 'by reference' arguments to scalars");
87 Pass *llvm::createArgumentPromotionPass(unsigned maxElements) {
88 return new ArgPromotion(maxElements);
91 bool ArgPromotion::runOnSCC(const std::vector<CallGraphNode *> &SCC) {
92 bool Changed = false, LocalChange;
94 do { // Iterate until we stop promoting from this SCC.
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);
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.
110 bool ArgPromotion::PromoteArguments(CallGraphNode *CGN) {
111 Function *F = CGN->getFunction();
113 // Make sure that it is local to this module.
114 if (!F || !F->hasInternalLinkage()) return false;
116 // First check: see if there are any pointer arguments! If not, quick exit.
117 SmallVector<std::pair<Argument*, unsigned>, 16> PointerArgs;
119 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
120 I != E; ++I, ++ArgNo)
121 if (isa<PointerType>(I->getType()))
122 PointerArgs.push_back(std::pair<Argument*, unsigned>(I, ArgNo));
123 if (PointerArgs.empty()) return false;
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();
129 CallSite CS = CallSite::get(*UI);
130 if (!CS.getInstruction()) // "Taking the address" of the function
133 // Ensure that this call site is CALLING the function, not passing it as
135 if (UI.getOperandNo() != 0)
139 // Check to see which arguments are promotable. If an argument is promotable,
140 // add it to ArgsToPromote.
141 SmallPtrSet<Argument*, 8> ArgsToPromote;
142 SmallPtrSet<Argument*, 8> ByValArgsToTransform;
143 for (unsigned i = 0; i != PointerArgs.size(); ++i) {
144 bool isByVal = F->paramHasAttr(PointerArgs[i].second+1, ParamAttr::ByVal);
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;
150 const Type *AgTy = cast<PointerType>(PtrArg->getType())->getElementType();
151 if (const StructType *STy = dyn_cast<StructType>(AgTy))
152 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";
157 // If all the elements are first class types, we can promote it.
158 bool AllSimple = true;
159 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
160 if (!STy->getElementType(i)->isFirstClassType()) {
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.
169 ByValArgsToTransform.insert(PtrArg);
175 // Otherwise, see if we can promote the pointer to its value.
176 if (isSafeToPromoteArgument(PtrArg, isByVal))
177 ArgsToPromote.insert(PtrArg);
180 // No promotable pointer arguments.
181 if (ArgsToPromote.empty() && ByValArgsToTransform.empty()) return false;
183 Function *NewF = DoPromotion(F, ArgsToPromote, ByValArgsToTransform);
185 // Update the call graph to know that the function has been transformed.
186 getAnalysis<CallGraph>().changeFunction(F, NewF);
190 /// IsAlwaysValidPointer - Return true if the specified pointer is always legal
192 static bool IsAlwaysValidPointer(Value *V) {
193 if (isa<AllocaInst>(V) || isa<GlobalVariable>(V)) return true;
194 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V))
195 return IsAlwaysValidPointer(GEP->getOperand(0));
196 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
197 if (CE->getOpcode() == Instruction::GetElementPtr)
198 return IsAlwaysValidPointer(CE->getOperand(0));
203 /// AllCalleesPassInValidPointerForArgument - Return true if we can prove that
204 /// all callees pass in a valid pointer for the specified function argument.
205 static bool AllCalleesPassInValidPointerForArgument(Argument *Arg) {
206 Function *Callee = Arg->getParent();
208 unsigned ArgNo = std::distance(Callee->arg_begin(),
209 Function::arg_iterator(Arg));
211 // Look at all call sites of the function. At this pointer we know we only
212 // have direct callees.
213 for (Value::use_iterator UI = Callee->use_begin(), E = Callee->use_end();
215 CallSite CS = CallSite::get(*UI);
216 assert(CS.getInstruction() && "Should only have direct calls!");
218 if (!IsAlwaysValidPointer(CS.getArgument(ArgNo)))
225 /// isSafeToPromoteArgument - As you might guess from the name of this method,
226 /// it checks to see if it is both safe and useful to promote the argument.
227 /// This method limits promotion of aggregates to only promote up to three
228 /// elements of the aggregate in order to avoid exploding the number of
229 /// arguments passed in.
230 bool ArgPromotion::isSafeToPromoteArgument(Argument *Arg, bool isByVal) const {
231 // We can only promote this argument if all of the uses are loads, or are GEP
232 // instructions (with constant indices) that are subsequently loaded.
234 // We can also only promote the load if we can guarantee that it will happen.
235 // Promoting a load causes the load to be unconditionally executed in the
236 // caller, so we can't turn a conditional load into an unconditional load in
238 bool SafeToUnconditionallyLoad = false;
239 if (isByVal) // ByVal arguments are always safe to load from.
240 SafeToUnconditionallyLoad = true;
242 BasicBlock *EntryBlock = Arg->getParent()->begin();
243 SmallVector<LoadInst*, 16> Loads;
244 std::vector<SmallVector<ConstantInt*, 8> > GEPIndices;
245 for (Value::use_iterator UI = Arg->use_begin(), E = Arg->use_end();
247 if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
248 if (LI->isVolatile()) return false; // Don't hack volatile loads
251 // If this load occurs in the entry block, then the pointer is
252 // unconditionally loaded.
253 SafeToUnconditionallyLoad |= LI->getParent() == EntryBlock;
254 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) {
255 if (GEP->use_empty()) {
256 // Dead GEP's cause trouble later. Just remove them if we run into
258 getAnalysis<AliasAnalysis>().deleteValue(GEP);
259 GEP->eraseFromParent();
260 return isSafeToPromoteArgument(Arg, isByVal);
262 // Ensure that all of the indices are constants.
263 SmallVector<ConstantInt*, 8> Operands;
264 for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i)
265 if (ConstantInt *C = dyn_cast<ConstantInt>(GEP->getOperand(i)))
266 Operands.push_back(C);
268 return false; // Not a constant operand GEP!
270 // Ensure that the only users of the GEP are load instructions.
271 for (Value::use_iterator UI = GEP->use_begin(), E = GEP->use_end();
273 if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
274 if (LI->isVolatile()) return false; // Don't hack volatile loads
277 // If this load occurs in the entry block, then the pointer is
278 // unconditionally loaded.
279 SafeToUnconditionallyLoad |= LI->getParent() == EntryBlock;
284 // See if there is already a GEP with these indices. If not, check to
285 // make sure that we aren't promoting too many elements. If so, nothing
287 if (std::find(GEPIndices.begin(), GEPIndices.end(), Operands) ==
289 if (maxElements > 0 && GEPIndices.size() == maxElements) {
290 DOUT << "argpromotion disable promoting argument '"
291 << Arg->getName() << "' because it would require adding more "
292 << "than " << maxElements << " arguments to the function.\n";
293 // We limit aggregate promotion to only promoting up to a fixed number
294 // of elements of the aggregate.
297 GEPIndices.push_back(Operands);
300 return false; // Not a load or a GEP.
303 if (Loads.empty()) return true; // No users, this is a dead argument.
305 // If we decide that we want to promote this argument, the value is going to
306 // be unconditionally loaded in all callees. This is only safe to do if the
307 // pointer was going to be unconditionally loaded anyway (i.e. there is a load
308 // of the pointer in the entry block of the function) or if we can prove that
309 // all pointers passed in are always to legal locations (for example, no null
310 // pointers are passed in, no pointers to free'd memory, etc).
311 if (!SafeToUnconditionallyLoad &&
312 !AllCalleesPassInValidPointerForArgument(Arg))
313 return false; // Cannot prove that this is safe!!
315 // Okay, now we know that the argument is only used by load instructions and
316 // it is safe to unconditionally load the pointer. Use alias analysis to
317 // check to see if the pointer is guaranteed to not be modified from entry of
318 // the function to each of the load instructions.
320 // Because there could be several/many load instructions, remember which
321 // blocks we know to be transparent to the load.
322 SmallPtrSet<BasicBlock*, 16> TranspBlocks;
324 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
325 TargetData &TD = getAnalysis<TargetData>();
327 for (unsigned i = 0, e = Loads.size(); i != e; ++i) {
328 // Check to see if the load is invalidated from the start of the block to
330 LoadInst *Load = Loads[i];
331 BasicBlock *BB = Load->getParent();
333 const PointerType *LoadTy =
334 cast<PointerType>(Load->getOperand(0)->getType());
335 unsigned LoadSize = (unsigned)TD.getTypeStoreSize(LoadTy->getElementType());
337 if (AA.canInstructionRangeModify(BB->front(), *Load, Arg, LoadSize))
338 return false; // Pointer is invalidated!
340 // Now check every path from the entry block to the load for transparency.
341 // To do this, we perform a depth first search on the inverse CFG from the
343 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
344 for (idf_ext_iterator<BasicBlock*, SmallPtrSet<BasicBlock*, 16> >
345 I = idf_ext_begin(*PI, TranspBlocks),
346 E = idf_ext_end(*PI, TranspBlocks); I != E; ++I)
347 if (AA.canBasicBlockModify(**I, Arg, LoadSize))
351 // If the path from the entry of the function to each load is free of
352 // instructions that potentially invalidate the load, we can make the
358 /// GEPIdxComparator - Provide a strong ordering for GEP indices. All Value*
359 /// elements are instances of ConstantInt.
361 struct GEPIdxComparator {
362 bool operator()(const std::vector<Value*> &LHS,
363 const std::vector<Value*> &RHS) const {
365 for (; idx < LHS.size() && idx < RHS.size(); ++idx) {
366 if (LHS[idx] != RHS[idx]) {
367 return cast<ConstantInt>(LHS[idx])->getZExtValue() <
368 cast<ConstantInt>(RHS[idx])->getZExtValue();
372 // Return less than if we ran out of stuff in LHS and we didn't run out of
374 return idx == LHS.size() && idx != RHS.size();
380 /// DoPromotion - This method actually performs the promotion of the specified
381 /// arguments, and returns the new function. At this point, we know that it's
383 Function *ArgPromotion::DoPromotion(Function *F,
384 SmallPtrSet<Argument*, 8> &ArgsToPromote,
385 SmallPtrSet<Argument*, 8> &ByValArgsToTransform) {
387 // Start by computing a new prototype for the function, which is the same as
388 // the old function, but has modified arguments.
389 const FunctionType *FTy = F->getFunctionType();
390 std::vector<const Type*> Params;
392 typedef std::set<std::vector<Value*>, GEPIdxComparator> ScalarizeTable;
394 // ScalarizedElements - If we are promoting a pointer that has elements
395 // accessed out of it, keep track of which elements are accessed so that we
396 // can add one argument for each.
398 // Arguments that are directly loaded will have a zero element value here, to
399 // handle cases where there are both a direct load and GEP accesses.
401 std::map<Argument*, ScalarizeTable> ScalarizedElements;
403 // OriginalLoads - Keep track of a representative load instruction from the
404 // original function so that we can tell the alias analysis implementation
405 // what the new GEP/Load instructions we are inserting look like.
406 std::map<std::vector<Value*>, LoadInst*> OriginalLoads;
408 // ParamAttrs - Keep track of the parameter attributes for the arguments
409 // that we are *not* promoting. For the ones that we do promote, the parameter
410 // attributes are lost
411 SmallVector<ParamAttrsWithIndex, 8> ParamAttrsVec;
412 const PAListPtr &PAL = F->getParamAttrs();
414 // Add any return attributes.
415 if (ParameterAttributes attrs = PAL.getParamAttrs(0))
416 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(0, attrs));
418 unsigned ArgIndex = 1;
419 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E;
421 if (ByValArgsToTransform.count(I)) {
422 // Just add all the struct element types.
423 const Type *AgTy = cast<PointerType>(I->getType())->getElementType();
424 const StructType *STy = cast<StructType>(AgTy);
425 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
426 Params.push_back(STy->getElementType(i));
427 ++NumByValArgsPromoted;
428 } else if (!ArgsToPromote.count(I)) {
429 Params.push_back(I->getType());
430 if (ParameterAttributes attrs = PAL.getParamAttrs(ArgIndex))
431 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Params.size(), attrs));
432 } else if (I->use_empty()) {
435 // Okay, this is being promoted. Check to see if there are any GEP uses
437 ScalarizeTable &ArgIndices = ScalarizedElements[I];
438 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;
440 Instruction *User = cast<Instruction>(*UI);
441 assert(isa<LoadInst>(User) || isa<GetElementPtrInst>(User));
442 std::vector<Value*> Indices(User->op_begin()+1, User->op_end());
443 ArgIndices.insert(Indices);
445 if (LoadInst *L = dyn_cast<LoadInst>(User))
448 OrigLoad = cast<LoadInst>(User->use_back());
449 OriginalLoads[Indices] = OrigLoad;
452 // Add a parameter to the function for each element passed in.
453 for (ScalarizeTable::iterator SI = ArgIndices.begin(),
454 E = ArgIndices.end(); SI != E; ++SI)
455 Params.push_back(GetElementPtrInst::getIndexedType(I->getType(),
459 if (ArgIndices.size() == 1 && ArgIndices.begin()->empty())
460 ++NumArgumentsPromoted;
462 ++NumAggregatesPromoted;
466 const Type *RetTy = FTy->getReturnType();
468 // Work around LLVM bug PR56: the CWriter cannot emit varargs functions which
469 // have zero fixed arguments.
470 bool ExtraArgHack = false;
471 if (Params.empty() && FTy->isVarArg()) {
473 Params.push_back(Type::Int32Ty);
476 // Construct the new function type using the new arguments.
477 FunctionType *NFTy = FunctionType::get(RetTy, Params, FTy->isVarArg());
479 // Create the new function body and insert it into the module...
480 Function *NF = Function::Create(NFTy, F->getLinkage(), F->getName());
481 NF->setCallingConv(F->getCallingConv());
483 // Recompute the parameter attributes list based on the new arguments for
485 NF->setParamAttrs(PAListPtr::get(ParamAttrsVec.begin(), ParamAttrsVec.end()));
486 ParamAttrsVec.clear();
488 if (F->hasCollector())
489 NF->setCollector(F->getCollector());
490 F->getParent()->getFunctionList().insert(F, NF);
493 // Get the alias analysis information that we need to update to reflect our
495 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
497 // Loop over all of the callers of the function, transforming the call sites
498 // to pass in the loaded pointers.
500 SmallVector<Value*, 16> Args;
501 while (!F->use_empty()) {
502 CallSite CS = CallSite::get(F->use_back());
503 Instruction *Call = CS.getInstruction();
504 const PAListPtr &CallPAL = CS.getParamAttrs();
506 // Add any return attributes.
507 if (ParameterAttributes attrs = CallPAL.getParamAttrs(0))
508 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(0, attrs));
510 // Loop over the operands, inserting GEP and loads in the caller as
512 CallSite::arg_iterator AI = CS.arg_begin();
514 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
515 I != E; ++I, ++AI, ++ArgIndex)
516 if (!ArgsToPromote.count(I) && !ByValArgsToTransform.count(I)) {
517 Args.push_back(*AI); // Unmodified argument
519 if (ParameterAttributes Attrs = CallPAL.getParamAttrs(ArgIndex))
520 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Args.size(), Attrs));
522 } 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);
529 Value *Idx = GetElementPtrInst::Create(*AI, Idxs, Idxs+2,
530 (*AI)->getName()+"."+utostr(i),
532 // TODO: Tell AA about the new values?
533 Args.push_back(new LoadInst(Idx, Idx->getName()+".val", Call));
535 } else if (!I->use_empty()) {
536 // 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) {
541 LoadInst *OrigLoad = OriginalLoads[*SI];
543 V = GetElementPtrInst::Create(V, SI->begin(), SI->end(),
544 V->getName()+".idx", Call);
545 AA.copyValue(OrigLoad->getOperand(0), V);
547 Args.push_back(new LoadInst(V, V->getName()+".val", Call));
548 AA.copyValue(OrigLoad, Args.back());
553 Args.push_back(Constant::getNullValue(Type::Int32Ty));
555 // Push any varargs arguments on the list
556 for (; AI != CS.arg_end(); ++AI, ++ArgIndex) {
558 if (ParameterAttributes Attrs = CallPAL.getParamAttrs(ArgIndex))
559 ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Args.size(), Attrs));
563 if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
564 New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),
565 Args.begin(), Args.end(), "", Call);
566 cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
567 cast<InvokeInst>(New)->setParamAttrs(PAListPtr::get(ParamAttrsVec.begin(),
568 ParamAttrsVec.end()));
570 New = CallInst::Create(NF, Args.begin(), Args.end(), "", Call);
571 cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
572 cast<CallInst>(New)->setParamAttrs(PAListPtr::get(ParamAttrsVec.begin(),
573 ParamAttrsVec.end()));
574 if (cast<CallInst>(Call)->isTailCall())
575 cast<CallInst>(New)->setTailCall();
578 ParamAttrsVec.clear();
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);
584 if (!Call->use_empty()) {
585 Call->replaceAllUsesWith(New);
589 // Finally, remove the old call from the program, reducing the use-count of
591 Call->eraseFromParent();
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
597 NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());
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.
602 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(),
603 I2 = NF->arg_begin(); I != E; ++I) {
604 if (!ArgsToPromote.count(I) && !ByValArgsToTransform.count(I)) {
605 // If this is an unmodified argument, move the name and users over to the
607 I->replaceAllUsesWith(I2);
609 AA.replaceWithNewValue(I, I2);
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();
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 };
625 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
626 Idxs[1] = ConstantInt::get(Type::Int32Ty, i);
627 Value *Idx = GetElementPtrInst::Create(TheAlloca, Idxs, Idxs+2,
628 TheAlloca->getName()+"."+utostr(i),
630 I2->setName(I->getName()+"."+utostr(i));
631 new StoreInst(I2++, Idx, InsertPt);
634 // Anything that used the arg should now use the alloca.
635 I->replaceAllUsesWith(TheAlloca);
636 TheAlloca->takeName(I);
637 AA.replaceWithNewValue(I, TheAlloca);
641 if (I->use_empty()) {
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
649 ScalarizeTable &ArgIndices = ScalarizedElements[I];
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";
662 GetElementPtrInst *GEP = cast<GetElementPtrInst>(I->use_back());
663 std::vector<Value*> Operands(GEP->op_begin()+1, GEP->op_end());
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??");
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);
677 TheArg->setName(NewName+".val");
679 DOUT << "*** Promoted agg argument '" << TheArg->getName()
680 << "' of function '" << F->getName() << "'\n";
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();
691 GEP->eraseFromParent();
695 // Increment I2 past all of the arguments added for this promoted pointer.
696 for (unsigned i = 0, e = ArgIndices.size(); i != e; ++i)
700 // Notify the alias analysis implementation that we inserted a new argument.
702 AA.copyValue(Constant::getNullValue(Type::Int32Ty), NF->arg_begin());
705 // Tell the alias analysis that the old function is about to disappear.
706 AA.replaceWithNewValue(F, NF);
708 // Now that the old function is dead, delete it.
709 F->eraseFromParent();