ArgumentPromotion.cpp revision 7db5a6df78749bf0cd37870fcefe08b8849e38e6
1//===-- ArgumentPromotion.cpp - Promote 'by reference' arguments ----------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source License. See LICENSE.TXT for details. 7// 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 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 16// code like the STL. 17// 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 20// case. 21// 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. 25// 26//===----------------------------------------------------------------------===// 27 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" 41#include <set> 42using namespace llvm; 43 44namespace { 45 Statistic<> NumArgumentsPromoted("argpromotion", 46 "Number of pointer arguments promoted"); 47 Statistic<> NumArgumentsDead("argpromotion", 48 "Number of dead pointer args eliminated"); 49 50 /// ArgPromotion - The 'by reference' to 'by value' argument promotion pass. 51 /// 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; 58 public: 59 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 60 AU.addRequired<AliasAnalysis>(); 61 AU.addRequired<TargetData>(); 62 } 63 64 virtual bool run(Module &M); 65 private: 66 bool PromoteArguments(Function *F); 67 bool isSafeToPromoteArgument(Argument *Arg) const; 68 void DoPromotion(Function *F, std::vector<Argument*> &ArgsToPromote); 69 }; 70 71 RegisterOpt<ArgPromotion> X("argpromotion", 72 "Promote 'by reference' arguments to scalars"); 73} 74 75Pass *llvm::createArgumentPromotionPass() { 76 return new ArgPromotion(); 77} 78 79bool ArgPromotion::run(Module &M) { 80 bool Changed = false; 81 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 82 if (I->hasInternalLinkage()) { 83 WorkList.insert(I); 84 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; 89 ++UI) 90 if ((CPR = dyn_cast<ConstantPointerRef>(*UI))) 91 break; // Found one! 92 if (CPR) { 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)) { 97 Changed = true; 98 TheUser->replaceUsesOfWith(CPR, I); 99 } else { 100 // We won't be able to eliminate all users. :( 101 WorkList.erase(I); // Minor efficiency win. 102 break; 103 } 104 } 105 106 // If we nuked all users of the CPR, kill the CPR now! 107 if (CPR->use_empty()) { 108 CPR->destroyConstant(); 109 Changed = true; 110 } 111 } 112 } 113 114 while (!WorkList.empty()) { 115 Function *F = *WorkList.begin(); 116 WorkList.erase(WorkList.begin()); 117 118 if (PromoteArguments(F)) // Attempt to promote an argument. 119 Changed = true; // Remember that we changed something. 120 } 121 122 return Changed; 123} 124 125 126bool ArgPromotion::PromoteArguments(Function *F) { 127 assert(F->hasInternalLinkage() && "We can only process internal functions!"); 128 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; 135 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(); 139 UI != E; ++UI) { 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 143 // an argument. 144 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end(); 145 AI != E; ++AI) 146 if (*AI == F) return false; // Passing the function address in! 147 } else { 148 return false; // Cannot promote an indirect call! 149 } 150 } 151 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(); 158 } 159 160 // No promotable pointer arguments. 161 if (PointerArgs.empty()) return false; 162 163 // Okay, promote all of the arguments are rewrite the callees! 164 DoPromotion(F, PointerArgs); 165 return true; 166} 167 168bool 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(); 172 UI != E; ++UI) 173 if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) { 174 if (LI->isVolatile()) return false; // Don't hack volatile loads 175 Loads.push_back(LI); 176 } else 177 return false; 178 179 if (Loads.empty()) return true; // No users, dead argument. 180 181 const Type *LoadTy = cast<PointerType>(Arg->getType())->getElementType(); 182 unsigned LoadSize = getAnalysis<TargetData>().getTypeSize(LoadTy); 183 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(); 188 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; 192 193 AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); 194 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 197 // the load itself. 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! 202 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 205 // loading block. 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)) 210 return false; 211 } 212 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 215 // transformation! 216 return true; 217} 218 219 220void ArgPromotion::DoPromotion(Function *F, std::vector<Argument*> &Args2Prom) { 221 std::set<Argument*> ArgsToPromote(Args2Prom.begin(), Args2Prom.end()); 222 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; 227 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; 234 } else { 235 ++NumArgumentsDead; 236 } 237 238 const Type *RetTy = FTy->getReturnType(); 239 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()) { 244 ExtraArgHack = true; 245 Params.push_back(Type::IntTy); 246 } 247 FunctionType *NFTy = FunctionType::get(RetTy, Params, FTy->isVarArg()); 248 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); 252 253 // Loop over all of the callers of the function, transforming the call sites 254 // to pass in the loaded pointers. 255 // 256 std::vector<Value*> Args; 257 while (!F->use_empty()) { 258 CallSite CS = CallSite::get(F->use_back()); 259 Instruction *Call = CS.getInstruction(); 260 261 // Make sure the caller of this function is revisited. 262 if (Call->getParent()->getParent()->hasInternalLinkage()) 263 WorkList.insert(Call->getParent()->getParent()); 264 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)); 273 } 274 275 if (ExtraArgHack) 276 Args.push_back(Constant::getNullValue(Type::IntTy)); 277 278 // Push any varargs arguments on the list 279 for (; AI != CS.arg_end(); ++AI) 280 Args.push_back(*AI); 281 282 Instruction *New; 283 if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) { 284 New = new InvokeInst(NF, II->getNormalDest(), II->getUnwindDest(), 285 Args, "", Call); 286 } else { 287 New = new CallInst(NF, Args, "", Call); 288 } 289 Args.clear(); 290 291 if (!Call->use_empty()) { 292 Call->replaceAllUsesWith(New); 293 std::string Name = Call->getName(); 294 Call->setName(""); 295 New->setName(Name); 296 } 297 298 // Finally, remove the old call from the program, reducing the use-count of 299 // F. 300 Call->getParent()->getInstList().erase(Call); 301 } 302 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 305 // function empty. 306 NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList()); 307 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. 310 // 311 for (Function::aiterator I = F->abegin(), E = F->aend(), I2 = NF->abegin(); 312 I != E; ++I) 313 if (!ArgsToPromote.count(I)) { 314 // If this is an unmodified argument, move the name and users over to the 315 // new version. 316 I->replaceAllUsesWith(I2); 317 I2->setName(I->getName()); 318 ++I2; 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 322 // added. 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); 330 } 331 ++I2; 332 } 333 334 // Now that the old function is dead, delete it. 335 F->getParent()->getFunctionList().erase(F); 336} 337