1 //===-- ArgumentPromotion.cpp - Promote 'by reference' arguments ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
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 we can prove, through the use of alias analysis, that that an
13 // argument is *only* loaded, then we can pass the value into the function
14 // instead of the address of the value. This can cause recursive simplification
15 // of code, and lead to the elimination of allocas, especially in C++ template
18 // Note that this transformation could also be done for arguments that are only
19 // stored to (returning the value instead), but we do not currently handle that
22 // Note that we should be able to promote pointers to structures that are only
23 // loaded from as well. The danger is creating way to many arguments, so this
24 // transformation should be limited to 3 element structs or something.
26 //===----------------------------------------------------------------------===//
28 #include "llvm/Transforms/IPO.h"
29 #include "llvm/Constants.h"
30 #include "llvm/DerivedTypes.h"
31 #include "llvm/Module.h"
32 #include "llvm/Pass.h"
33 #include "llvm/Instructions.h"
34 #include "llvm/Analysis/AliasAnalysis.h"
35 #include "llvm/Target/TargetData.h"
36 #include "llvm/Support/CallSite.h"
37 #include "llvm/Support/CFG.h"
38 #include "Support/Debug.h"
39 #include "Support/DepthFirstIterator.h"
40 #include "Support/Statistic.h"
45 Statistic<> NumArgumentsPromoted("argpromotion",
46 "Number of pointer arguments promoted");
47 Statistic<> NumArgumentsDead("argpromotion",
48 "Number of dead pointer args eliminated");
50 /// ArgPromotion - The 'by reference' to 'by value' argument promotion pass.
52 class ArgPromotion : public Pass {
53 // WorkList - The set of internal functions that we have yet to process. As
54 // we eliminate arguments from a function, we push all callers into this set
55 // so that the by reference argument can be bubbled out as far as possible.
56 // This set contains only internal functions.
57 std::set<Function*> WorkList;
59 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
60 AU.addRequired<AliasAnalysis>();
61 AU.addRequired<TargetData>();
64 virtual bool run(Module &M);
66 bool PromoteArguments(Function *F);
67 bool isSafeToPromoteArgument(Argument *Arg) const;
68 void DoPromotion(Function *F, std::vector<Argument*> &ArgsToPromote);
71 RegisterOpt<ArgPromotion> X("argpromotion",
72 "Promote 'by reference' arguments to scalars");
75 Pass *llvm::createArgumentPromotionPass() {
76 return new ArgPromotion();
79 bool ArgPromotion::run(Module &M) {
81 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
82 if (I->hasInternalLinkage()) {
85 // If there are any constant pointer refs pointing to this function,
86 // eliminate them now if possible.
87 ConstantPointerRef *CPR = 0;
88 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;
90 if ((CPR = dyn_cast<ConstantPointerRef>(*UI)))
93 // See if we can transform all users to use the function directly.
94 while (!CPR->use_empty()) {
95 User *TheUser = CPR->use_back();
96 if (!isa<Constant>(TheUser) && !isa<GlobalVariable>(TheUser)) {
98 TheUser->replaceUsesOfWith(CPR, I);
100 // We won't be able to eliminate all users. :(
101 WorkList.erase(I); // Minor efficiency win.
106 // If we nuked all users of the CPR, kill the CPR now!
107 if (CPR->use_empty()) {
108 CPR->destroyConstant();
114 while (!WorkList.empty()) {
115 Function *F = *WorkList.begin();
116 WorkList.erase(WorkList.begin());
118 if (PromoteArguments(F)) // Attempt to promote an argument.
119 Changed = true; // Remember that we changed something.
126 bool ArgPromotion::PromoteArguments(Function *F) {
127 assert(F->hasInternalLinkage() && "We can only process internal functions!");
129 // First check: see if there are any pointer arguments! If not, quick exit.
130 std::vector<Argument*> PointerArgs;
131 for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
132 if (isa<PointerType>(I->getType()))
133 PointerArgs.push_back(I);
134 if (PointerArgs.empty()) return false;
136 // Second check: make sure that all callers are direct callers. We can't
137 // transform functions that have indirect callers.
138 for (Value::use_iterator UI = F->use_begin(), E = F->use_end();
140 CallSite CS = CallSite::get(*UI);
141 if (Instruction *I = CS.getInstruction()) {
142 // Ensure that this call site is CALLING the function, not passing it as
144 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
146 if (*AI == F) return false; // Passing the function address in!
148 return false; // Cannot promote an indirect call!
152 // Check to see which arguments are promotable. If an argument is not
153 // promotable, remove it from the PointerArgs vector.
154 for (unsigned i = 0; i != PointerArgs.size(); ++i)
155 if (!isSafeToPromoteArgument(PointerArgs[i])) {
156 std::swap(PointerArgs[i--], PointerArgs.back());
157 PointerArgs.pop_back();
160 // No promotable pointer arguments.
161 if (PointerArgs.empty()) return false;
163 // Okay, promote all of the arguments are rewrite the callees!
164 DoPromotion(F, PointerArgs);
168 bool ArgPromotion::isSafeToPromoteArgument(Argument *Arg) const {
169 // We can only promote this argument if all of the uses are loads...
170 std::vector<LoadInst*> Loads;
171 for (Value::use_iterator UI = Arg->use_begin(), E = Arg->use_end();
173 if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
174 if (LI->isVolatile()) return false; // Don't hack volatile loads
179 if (Loads.empty()) return true; // No users, dead argument.
181 const Type *LoadTy = cast<PointerType>(Arg->getType())->getElementType();
182 unsigned LoadSize = getAnalysis<TargetData>().getTypeSize(LoadTy);
184 // Okay, now we know that the argument is only used by load instructions.
185 // Check to see if the pointer is guaranteed to not be modified from entry of
186 // the function to each of the load instructions.
187 Function &F = *Arg->getParent();
189 // Because there could be several/many load instructions, remember which
190 // blocks we know to be transparent to the load.
191 std::set<BasicBlock*> TranspBlocks;
193 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
195 for (unsigned i = 0, e = Loads.size(); i != e; ++i) {
196 // Check to see if the load is invalidated from the start of the block to
198 LoadInst *Load = Loads[i];
199 BasicBlock *BB = Load->getParent();
200 if (AA.canInstructionRangeModify(BB->front(), *Load, Arg, LoadSize))
201 return false; // Pointer is invalidated!
203 // Now check every path from the entry block to the load for transparency.
204 // To do this, we perform a depth first search on the inverse CFG from the
206 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
207 for (idf_ext_iterator<BasicBlock*> I = idf_ext_begin(*PI, TranspBlocks),
208 E = idf_ext_end(*PI, TranspBlocks); I != E; ++I)
209 if (AA.canBasicBlockModify(**I, Arg, LoadSize))
213 // If the path from the entry of the function to each load is free of
214 // instructions that potentially invalidate the load, we can make the
220 void ArgPromotion::DoPromotion(Function *F, std::vector<Argument*> &Args2Prom) {
221 std::set<Argument*> ArgsToPromote(Args2Prom.begin(), Args2Prom.end());
223 // Start by computing a new prototype for the function, which is the same as
224 // the old function, but has modified arguments.
225 const FunctionType *FTy = F->getFunctionType();
226 std::vector<const Type*> Params;
228 for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
229 if (!ArgsToPromote.count(I)) {
230 Params.push_back(I->getType());
231 } else if (!I->use_empty()) {
232 Params.push_back(cast<PointerType>(I->getType())->getElementType());
233 ++NumArgumentsPromoted;
238 const Type *RetTy = FTy->getReturnType();
240 // Work around LLVM bug PR56: the CWriter cannot emit varargs functions which
241 // have zero fixed arguments.
242 bool ExtraArgHack = false;
243 if (Params.empty() && FTy->isVarArg()) {
245 Params.push_back(Type::IntTy);
247 FunctionType *NFTy = FunctionType::get(RetTy, Params, FTy->isVarArg());
249 // Create the new function body and insert it into the module...
250 Function *NF = new Function(NFTy, F->getLinkage(), F->getName());
251 F->getParent()->getFunctionList().insert(F, NF);
253 // Loop over all of the callers of the function, transforming the call sites
254 // to pass in the loaded pointers.
256 std::vector<Value*> Args;
257 while (!F->use_empty()) {
258 CallSite CS = CallSite::get(F->use_back());
259 Instruction *Call = CS.getInstruction();
261 // Make sure the caller of this function is revisited.
262 if (Call->getParent()->getParent()->hasInternalLinkage())
263 WorkList.insert(Call->getParent()->getParent());
265 // Loop over the operands, deleting dead ones...
266 CallSite::arg_iterator AI = CS.arg_begin();
267 for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I, ++AI)
268 if (!ArgsToPromote.count(I))
269 Args.push_back(*AI); // Unmodified argument
270 else if (!I->use_empty()) {
271 // Non-dead instruction
272 Args.push_back(new LoadInst(*AI, (*AI)->getName()+".val", Call));
276 Args.push_back(Constant::getNullValue(Type::IntTy));
278 // Push any varargs arguments on the list
279 for (; AI != CS.arg_end(); ++AI)
283 if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
284 New = new InvokeInst(NF, II->getNormalDest(), II->getUnwindDest(),
287 New = new CallInst(NF, Args, "", Call);
291 if (!Call->use_empty()) {
292 Call->replaceAllUsesWith(New);
293 std::string Name = Call->getName();
298 // Finally, remove the old call from the program, reducing the use-count of
300 Call->getParent()->getInstList().erase(Call);
303 // Since we have now created the new function, splice the body of the old
304 // function right into the new function, leaving the old rotting hulk of the
306 NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());
308 // Loop over the argument list, transfering uses of the old arguments over to
309 // the new arguments, also transfering over the names as well.
311 for (Function::aiterator I = F->abegin(), E = F->aend(), I2 = NF->abegin();
313 if (!ArgsToPromote.count(I)) {
314 // If this is an unmodified argument, move the name and users over to the
316 I->replaceAllUsesWith(I2);
317 I2->setName(I->getName());
319 } else if (!I->use_empty()) {
320 // Otherwise, if we promoted this argument, then all users are load
321 // instructions, and all loads should be using the new argument that we
323 DEBUG(std::cerr << "*** Promoted argument '" << I->getName()
324 << "' of function '" << F->getName() << "'\n");
325 I2->setName(I->getName()+".val");
326 while (!I->use_empty()) {
327 LoadInst *LI = cast<LoadInst>(I->use_back());
328 LI->replaceAllUsesWith(I2);
329 LI->getParent()->getInstList().erase(LI);
332 // If we inserted a new pointer type, it's possible that IT could be
334 if (isa<PointerType>(I2->getType()))
339 // Now that the old function is dead, delete it.
340 F->getParent()->getFunctionList().erase(F);