ArgumentPromotion.cpp revision dac58ad983c62b49629e1f2969f4e0a621167d63
19e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner//===-- ArgumentPromotion.cpp - Promote by-reference arguments ------------===// 2fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// 3ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner// The LLVM Compiler Infrastructure 4ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner// 5ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner// This file was developed by the LLVM research group and is distributed under 6ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner// the University of Illinois Open Source License. See LICENSE.TXT for details. 7fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// 8ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner//===----------------------------------------------------------------------===// 9ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner// 10ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner// This pass promotes "by reference" arguments to be "by value" arguments. In 11ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner// practice, this means looking for internal functions that have pointer 12ebeb0cba94e41b60f2095d4e7f09989f16233da4Chris Lattner// arguments. If we can prove, through the use of alias analysis, that an 13ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner// argument is *only* loaded, then we can pass the value into the function 14ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner// instead of the address of the value. This can cause recursive simplification 159e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner// of code and lead to the elimination of allocas (especially in C++ template 169e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner// code like the STL). 17ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner// 189440db886627161a8413e823797569fc7b10beafChris Lattner// This pass also handles aggregate arguments that are passed into a function, 199440db886627161a8413e823797569fc7b10beafChris Lattner// scalarizing them if the elements of the aggregate are only loaded. Note that 209440db886627161a8413e823797569fc7b10beafChris Lattner// we refuse to scalarize aggregates which would require passing in more than 219440db886627161a8413e823797569fc7b10beafChris Lattner// three operands to the function, because we don't want to pass thousands of 229e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner// operands for a large array or structure! 239440db886627161a8413e823797569fc7b10beafChris Lattner// 24ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner// Note that this transformation could also be done for arguments that are only 25ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner// stored to (returning the value instead), but we do not currently handle that 269440db886627161a8413e823797569fc7b10beafChris Lattner// case. This case would be best handled when and if we start supporting 279440db886627161a8413e823797569fc7b10beafChris Lattner// multiple return values from functions. 28ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner// 29ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner//===----------------------------------------------------------------------===// 30ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 319e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner#define DEBUG_TYPE "argpromotion" 32ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner#include "llvm/Transforms/IPO.h" 33ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner#include "llvm/Constants.h" 34ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner#include "llvm/DerivedTypes.h" 35ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner#include "llvm/Module.h" 365eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner#include "llvm/CallGraphSCCPass.h" 37ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner#include "llvm/Instructions.h" 38ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner#include "llvm/Analysis/AliasAnalysis.h" 395eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner#include "llvm/Analysis/CallGraph.h" 40ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner#include "llvm/Target/TargetData.h" 41ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner#include "llvm/Support/CallSite.h" 42ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner#include "llvm/Support/CFG.h" 43551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/Debug.h" 44551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/DepthFirstIterator.h" 45551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/Statistic.h" 46551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/StringExtras.h" 47dac58ad983c62b49629e1f2969f4e0a621167d63Chris Lattner#include <iostream> 48ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner#include <set> 49ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattnerusing namespace llvm; 50ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 51ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattnernamespace { 52ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Statistic<> NumArgumentsPromoted("argpromotion", 53ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner "Number of pointer arguments promoted"); 549440db886627161a8413e823797569fc7b10beafChris Lattner Statistic<> NumAggregatesPromoted("argpromotion", 559440db886627161a8413e823797569fc7b10beafChris Lattner "Number of aggregate arguments promoted"); 56ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Statistic<> NumArgumentsDead("argpromotion", 57ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner "Number of dead pointer args eliminated"); 58ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 59ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner /// ArgPromotion - The 'by reference' to 'by value' argument promotion pass. 60ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner /// 615eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner struct ArgPromotion : public CallGraphSCCPass { 62ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner virtual void getAnalysisUsage(AnalysisUsage &AU) const { 63ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner AU.addRequired<AliasAnalysis>(); 64ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner AU.addRequired<TargetData>(); 655eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner CallGraphSCCPass::getAnalysisUsage(AU); 66ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner } 67ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 685eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner virtual bool runOnSCC(const std::vector<CallGraphNode *> &SCC); 69ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner private: 705eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner bool PromoteArguments(CallGraphNode *CGN); 71fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman bool isSafeToPromoteArgument(Argument *Arg) const; 725eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner Function *DoPromotion(Function *F, std::vector<Argument*> &ArgsToPromote); 73ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner }; 74ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 75ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner RegisterOpt<ArgPromotion> X("argpromotion", 76ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner "Promote 'by reference' arguments to scalars"); 77ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner} 78ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 79b12914bfc0f76a7a48357162d5f4c39a1343e69bChris LattnerModulePass *llvm::createArgumentPromotionPass() { 80ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner return new ArgPromotion(); 81ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner} 82ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 835eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattnerbool ArgPromotion::runOnSCC(const std::vector<CallGraphNode *> &SCC) { 845eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner bool Changed = false, LocalChange; 85ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 86f5afcabff887c2e9023f0c69c44f1de15b5c4347Chris Lattner do { // Iterate until we stop promoting from this SCC. 875eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner LocalChange = false; 885eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner // Attempt to promote arguments from all functions in this SCC. 895eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner for (unsigned i = 0, e = SCC.size(); i != e; ++i) 905eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner LocalChange |= PromoteArguments(SCC[i]); 915eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner Changed |= LocalChange; // Remember that we changed something. 925eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner } while (LocalChange); 93fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 94ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner return Changed; 95ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner} 96ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 979e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// PromoteArguments - This method checks the specified function to see if there 989e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// are any promotable arguments and if it is safe to promote the function (for 999e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// example, all callers are direct). If safe to promote some arguments, it 1009e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// calls the DoPromotion method. 1019e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// 1025eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattnerbool ArgPromotion::PromoteArguments(CallGraphNode *CGN) { 1035eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner Function *F = CGN->getFunction(); 1045eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner 1055eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner // Make sure that it is local to this module. 1065eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner if (!F || !F->hasInternalLinkage()) return false; 107ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 108ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // First check: see if there are any pointer arguments! If not, quick exit. 109ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner std::vector<Argument*> PointerArgs; 110e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) 111ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (isa<PointerType>(I->getType())) 112ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner PointerArgs.push_back(I); 113ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (PointerArgs.empty()) return false; 114ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 115ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Second check: make sure that all callers are direct callers. We can't 116ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // transform functions that have indirect callers. 117ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner for (Value::use_iterator UI = F->use_begin(), E = F->use_end(); 1187db5a6df78749bf0cd37870fcefe08b8849e38e6Chris Lattner UI != E; ++UI) { 1197db5a6df78749bf0cd37870fcefe08b8849e38e6Chris Lattner CallSite CS = CallSite::get(*UI); 1209e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner if (!CS.getInstruction()) // "Taking the address" of the function 1219e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner return false; 1229e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner 1239e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // Ensure that this call site is CALLING the function, not passing it as 1249e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // an argument. 1259e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end(); 1269e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner AI != E; ++AI) 1279e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner if (*AI == F) return false; // Passing the function address in! 1287db5a6df78749bf0cd37870fcefe08b8849e38e6Chris Lattner } 129ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 130ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Check to see which arguments are promotable. If an argument is not 131ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // promotable, remove it from the PointerArgs vector. 132ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner for (unsigned i = 0; i != PointerArgs.size(); ++i) 133ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (!isSafeToPromoteArgument(PointerArgs[i])) { 134ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner std::swap(PointerArgs[i--], PointerArgs.back()); 135ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner PointerArgs.pop_back(); 136ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner } 137ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 138ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // No promotable pointer arguments. 139ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (PointerArgs.empty()) return false; 140ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 141ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Okay, promote all of the arguments are rewrite the callees! 1425eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner Function *NewF = DoPromotion(F, PointerArgs); 1435eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner 1445eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner // Update the call graph to know that the old function is gone. 1455eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner getAnalysis<CallGraph>().changeFunction(F, NewF); 146ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner return true; 147ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner} 148ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 14911a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner/// IsAlwaysValidPointer - Return true if the specified pointer is always legal 15011a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner/// to load. 15111a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattnerstatic bool IsAlwaysValidPointer(Value *V) { 15211a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner if (isa<AllocaInst>(V) || isa<GlobalVariable>(V)) return true; 15311a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) 15411a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner return IsAlwaysValidPointer(GEP->getOperand(0)); 15511a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) 15611a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner if (CE->getOpcode() == Instruction::GetElementPtr) 15711a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner return IsAlwaysValidPointer(CE->getOperand(0)); 15811a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner 15911a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner return false; 16011a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner} 16111a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner 16211a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner/// AllCalleesPassInValidPointerForArgument - Return true if we can prove that 16311a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner/// all callees pass in a valid pointer for the specified function argument. 16411a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattnerstatic bool AllCalleesPassInValidPointerForArgument(Argument *Arg) { 16511a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner Function *Callee = Arg->getParent(); 16611a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner 167e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner unsigned ArgNo = std::distance(Callee->arg_begin(), Function::arg_iterator(Arg)); 16811a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner 16911a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner // Look at all call sites of the function. At this pointer we know we only 17011a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner // have direct callees. 17111a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner for (Value::use_iterator UI = Callee->use_begin(), E = Callee->use_end(); 17211a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner UI != E; ++UI) { 17311a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner CallSite CS = CallSite::get(*UI); 17411a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner assert(CS.getInstruction() && "Should only have direct calls!"); 17511a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner 17611a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner if (!IsAlwaysValidPointer(CS.getArgument(ArgNo))) 17711a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner return false; 17811a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner } 17911a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner return true; 18011a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner} 18111a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner 1829e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner 1839e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// isSafeToPromoteArgument - As you might guess from the name of this method, 1849e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// it checks to see if it is both safe and useful to promote the argument. 1859e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// This method limits promotion of aggregates to only promote up to three 1869e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// elements of the aggregate in order to avoid exploding the number of 1879e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// arguments passed in. 188ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattnerbool ArgPromotion::isSafeToPromoteArgument(Argument *Arg) const { 1899440db886627161a8413e823797569fc7b10beafChris Lattner // We can only promote this argument if all of the uses are loads, or are GEP 1909440db886627161a8413e823797569fc7b10beafChris Lattner // instructions (with constant indices) that are subsequently loaded. 19111a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner bool HasLoadInEntryBlock = false; 19211a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner BasicBlock *EntryBlock = Arg->getParent()->begin(); 193ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner std::vector<LoadInst*> Loads; 194beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner std::vector<std::vector<ConstantInt*> > GEPIndices; 195ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner for (Value::use_iterator UI = Arg->use_begin(), E = Arg->use_end(); 196ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner UI != E; ++UI) 197ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) { 198ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (LI->isVolatile()) return false; // Don't hack volatile loads 199ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Loads.push_back(LI); 20011a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner HasLoadInEntryBlock |= LI->getParent() == EntryBlock; 2019440db886627161a8413e823797569fc7b10beafChris Lattner } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) { 2029440db886627161a8413e823797569fc7b10beafChris Lattner if (GEP->use_empty()) { 2039440db886627161a8413e823797569fc7b10beafChris Lattner // Dead GEP's cause trouble later. Just remove them if we run into 2049440db886627161a8413e823797569fc7b10beafChris Lattner // them. 2059e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner getAnalysis<AliasAnalysis>().deleteValue(GEP); 2069440db886627161a8413e823797569fc7b10beafChris Lattner GEP->getParent()->getInstList().erase(GEP); 2079440db886627161a8413e823797569fc7b10beafChris Lattner return isSafeToPromoteArgument(Arg); 2089440db886627161a8413e823797569fc7b10beafChris Lattner } 2099440db886627161a8413e823797569fc7b10beafChris Lattner // Ensure that all of the indices are constants. 210beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner std::vector<ConstantInt*> Operands; 2119440db886627161a8413e823797569fc7b10beafChris Lattner for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i) 212beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner if (ConstantInt *C = dyn_cast<ConstantInt>(GEP->getOperand(i))) 2139440db886627161a8413e823797569fc7b10beafChris Lattner Operands.push_back(C); 2149440db886627161a8413e823797569fc7b10beafChris Lattner else 2159440db886627161a8413e823797569fc7b10beafChris Lattner return false; // Not a constant operand GEP! 2169440db886627161a8413e823797569fc7b10beafChris Lattner 2179440db886627161a8413e823797569fc7b10beafChris Lattner // Ensure that the only users of the GEP are load instructions. 2189440db886627161a8413e823797569fc7b10beafChris Lattner for (Value::use_iterator UI = GEP->use_begin(), E = GEP->use_end(); 2199440db886627161a8413e823797569fc7b10beafChris Lattner UI != E; ++UI) 2209440db886627161a8413e823797569fc7b10beafChris Lattner if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) { 2219440db886627161a8413e823797569fc7b10beafChris Lattner if (LI->isVolatile()) return false; // Don't hack volatile loads 2229440db886627161a8413e823797569fc7b10beafChris Lattner Loads.push_back(LI); 22311a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner HasLoadInEntryBlock |= LI->getParent() == EntryBlock; 2249440db886627161a8413e823797569fc7b10beafChris Lattner } else { 2259440db886627161a8413e823797569fc7b10beafChris Lattner return false; 2269440db886627161a8413e823797569fc7b10beafChris Lattner } 227ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 2289e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // See if there is already a GEP with these indices. If not, check to 2299e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // make sure that we aren't promoting too many elements. If so, nothing 2309e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // to do. 2319440db886627161a8413e823797569fc7b10beafChris Lattner if (std::find(GEPIndices.begin(), GEPIndices.end(), Operands) == 2329440db886627161a8413e823797569fc7b10beafChris Lattner GEPIndices.end()) { 2339440db886627161a8413e823797569fc7b10beafChris Lattner if (GEPIndices.size() == 3) { 2349e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner DEBUG(std::cerr << "argpromotion disable promoting argument '" 2359e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner << Arg->getName() << "' because it would require adding more " 2369e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner << "than 3 arguments to the function.\n"); 2379440db886627161a8413e823797569fc7b10beafChris Lattner // We limit aggregate promotion to only promoting up to three elements 2389440db886627161a8413e823797569fc7b10beafChris Lattner // of the aggregate. 2399440db886627161a8413e823797569fc7b10beafChris Lattner return false; 2409440db886627161a8413e823797569fc7b10beafChris Lattner } 2419440db886627161a8413e823797569fc7b10beafChris Lattner GEPIndices.push_back(Operands); 2429440db886627161a8413e823797569fc7b10beafChris Lattner } 2439440db886627161a8413e823797569fc7b10beafChris Lattner } else { 2449440db886627161a8413e823797569fc7b10beafChris Lattner return false; // Not a load or a GEP. 2459440db886627161a8413e823797569fc7b10beafChris Lattner } 246ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 2479e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner if (Loads.empty()) return true; // No users, this is a dead argument. 248ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 24911a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner // If we decide that we want to promote this argument, the value is going to 25011a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner // be unconditionally loaded in all callees. This is only safe to do if the 25111a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner // pointer was going to be unconditionally loaded anyway (i.e. there is a load 25211a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner // of the pointer in the entry block of the function) or if we can prove that 25311a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner // all pointers passed in are always to legal locations (for example, no null 25411a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner // pointers are passed in, no pointers to free'd memory, etc). 25511a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner if (!HasLoadInEntryBlock && !AllCalleesPassInValidPointerForArgument(Arg)) 25611a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner return false; // Cannot prove that this is safe!! 25711a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner 25811a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner // Okay, now we know that the argument is only used by load instructions and 25911a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner // it is safe to unconditionally load the pointer. Use alias analysis to 26011a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner // check to see if the pointer is guaranteed to not be modified from entry of 26111a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner // the function to each of the load instructions. 262ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Function &F = *Arg->getParent(); 263ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 264ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Because there could be several/many load instructions, remember which 265ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // blocks we know to be transparent to the load. 266ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner std::set<BasicBlock*> TranspBlocks; 267ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 268ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); 2699440db886627161a8413e823797569fc7b10beafChris Lattner TargetData &TD = getAnalysis<TargetData>(); 270ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 271ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner for (unsigned i = 0, e = Loads.size(); i != e; ++i) { 272ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Check to see if the load is invalidated from the start of the block to 273ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // the load itself. 274ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner LoadInst *Load = Loads[i]; 275ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner BasicBlock *BB = Load->getParent(); 2769440db886627161a8413e823797569fc7b10beafChris Lattner 2779440db886627161a8413e823797569fc7b10beafChris Lattner const PointerType *LoadTy = 2789440db886627161a8413e823797569fc7b10beafChris Lattner cast<PointerType>(Load->getOperand(0)->getType()); 2794d0801b243ebb05954ec2173b45ed9f892ef961aChris Lattner unsigned LoadSize = (unsigned)TD.getTypeSize(LoadTy->getElementType()); 2809440db886627161a8413e823797569fc7b10beafChris Lattner 281ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (AA.canInstructionRangeModify(BB->front(), *Load, Arg, LoadSize)) 282ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner return false; // Pointer is invalidated! 283ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 284ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Now check every path from the entry block to the load for transparency. 285ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // To do this, we perform a depth first search on the inverse CFG from the 286ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // loading block. 287ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) 288ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner for (idf_ext_iterator<BasicBlock*> I = idf_ext_begin(*PI, TranspBlocks), 289ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner E = idf_ext_end(*PI, TranspBlocks); I != E; ++I) 290ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (AA.canBasicBlockModify(**I, Arg, LoadSize)) 291ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner return false; 292ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner } 293ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 294ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // If the path from the entry of the function to each load is free of 295ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // instructions that potentially invalidate the load, we can make the 296ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // transformation! 297ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner return true; 298ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner} 299ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 300beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattnernamespace { 301beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner /// GEPIdxComparator - Provide a strong ordering for GEP indices. All Value* 302beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner /// elements are instances of ConstantInt. 303beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner /// 304beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner struct GEPIdxComparator { 305beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner bool operator()(const std::vector<Value*> &LHS, 306beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner const std::vector<Value*> &RHS) const { 307beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner unsigned idx = 0; 308beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner for (; idx < LHS.size() && idx < RHS.size(); ++idx) { 309beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner if (LHS[idx] != RHS[idx]) { 310fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman return cast<ConstantInt>(LHS[idx])->getRawValue() < 311beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner cast<ConstantInt>(RHS[idx])->getRawValue(); 312beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner } 313beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner } 314beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner 315beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner // Return less than if we ran out of stuff in LHS and we didn't run out of 316beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner // stuff in RHS. 317beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner return idx == LHS.size() && idx != RHS.size(); 318beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner } 319beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner }; 320beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner} 321beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner 322beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner 3239e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// DoPromotion - This method actually performs the promotion of the specified 3245eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner/// arguments, and returns the new function. At this point, we know that it's 3255eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner/// safe to do so. 3265eb6f6c829ddfe353f94623aa1009c72be930497Chris LattnerFunction *ArgPromotion::DoPromotion(Function *F, 3275eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner std::vector<Argument*> &Args2Prom) { 328ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner std::set<Argument*> ArgsToPromote(Args2Prom.begin(), Args2Prom.end()); 329fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 330ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Start by computing a new prototype for the function, which is the same as 331ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // the old function, but has modified arguments. 332ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner const FunctionType *FTy = F->getFunctionType(); 333ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner std::vector<const Type*> Params; 334ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 335beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner typedef std::set<std::vector<Value*>, GEPIdxComparator> ScalarizeTable; 336beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner 3379440db886627161a8413e823797569fc7b10beafChris Lattner // ScalarizedElements - If we are promoting a pointer that has elements 3389440db886627161a8413e823797569fc7b10beafChris Lattner // accessed out of it, keep track of which elements are accessed so that we 3399440db886627161a8413e823797569fc7b10beafChris Lattner // can add one argument for each. 3409440db886627161a8413e823797569fc7b10beafChris Lattner // 3419440db886627161a8413e823797569fc7b10beafChris Lattner // Arguments that are directly loaded will have a zero element value here, to 3429440db886627161a8413e823797569fc7b10beafChris Lattner // handle cases where there are both a direct load and GEP accesses. 3439440db886627161a8413e823797569fc7b10beafChris Lattner // 344beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner std::map<Argument*, ScalarizeTable> ScalarizedElements; 3459440db886627161a8413e823797569fc7b10beafChris Lattner 3469e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // OriginalLoads - Keep track of a representative load instruction from the 3479e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // original function so that we can tell the alias analysis implementation 3489e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // what the new GEP/Load instructions we are inserting look like. 3499e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner std::map<std::vector<Value*>, LoadInst*> OriginalLoads; 3509e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner 351e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) 352ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (!ArgsToPromote.count(I)) { 353ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Params.push_back(I->getType()); 3549e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner } else if (I->use_empty()) { 3559e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner ++NumArgumentsDead; 3569e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner } else { 3579440db886627161a8413e823797569fc7b10beafChris Lattner // Okay, this is being promoted. Check to see if there are any GEP uses 3589440db886627161a8413e823797569fc7b10beafChris Lattner // of the argument. 359beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner ScalarizeTable &ArgIndices = ScalarizedElements[I]; 3609440db886627161a8413e823797569fc7b10beafChris Lattner for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; 3619440db886627161a8413e823797569fc7b10beafChris Lattner ++UI) { 3629440db886627161a8413e823797569fc7b10beafChris Lattner Instruction *User = cast<Instruction>(*UI); 3639440db886627161a8413e823797569fc7b10beafChris Lattner assert(isa<LoadInst>(User) || isa<GetElementPtrInst>(User)); 3649e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner std::vector<Value*> Indices(User->op_begin()+1, User->op_end()); 3659e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner ArgIndices.insert(Indices); 3669e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner LoadInst *OrigLoad; 3679e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner if (LoadInst *L = dyn_cast<LoadInst>(User)) 3689e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner OrigLoad = L; 3699e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner else 3709e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner OrigLoad = cast<LoadInst>(User->use_back()); 3719e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner OriginalLoads[Indices] = OrigLoad; 3729440db886627161a8413e823797569fc7b10beafChris Lattner } 3739440db886627161a8413e823797569fc7b10beafChris Lattner 3749440db886627161a8413e823797569fc7b10beafChris Lattner // Add a parameter to the function for each element passed in. 375beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner for (ScalarizeTable::iterator SI = ArgIndices.begin(), 3769440db886627161a8413e823797569fc7b10beafChris Lattner E = ArgIndices.end(); SI != E; ++SI) 3779440db886627161a8413e823797569fc7b10beafChris Lattner Params.push_back(GetElementPtrInst::getIndexedType(I->getType(), *SI)); 3789440db886627161a8413e823797569fc7b10beafChris Lattner 3799440db886627161a8413e823797569fc7b10beafChris Lattner if (ArgIndices.size() == 1 && ArgIndices.begin()->empty()) 3809440db886627161a8413e823797569fc7b10beafChris Lattner ++NumArgumentsPromoted; 3819440db886627161a8413e823797569fc7b10beafChris Lattner else 3829440db886627161a8413e823797569fc7b10beafChris Lattner ++NumAggregatesPromoted; 383ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner } 384ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 385ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner const Type *RetTy = FTy->getReturnType(); 386ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 387ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Work around LLVM bug PR56: the CWriter cannot emit varargs functions which 388ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // have zero fixed arguments. 389ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner bool ExtraArgHack = false; 390ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (Params.empty() && FTy->isVarArg()) { 391ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner ExtraArgHack = true; 392ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Params.push_back(Type::IntTy); 393ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner } 394ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner FunctionType *NFTy = FunctionType::get(RetTy, Params, FTy->isVarArg()); 395fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 396ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Create the new function body and insert it into the module... 397ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Function *NF = new Function(NFTy, F->getLinkage(), F->getName()); 398f201dbc1a4a638659ec3916785196e2c204c7755Chris Lattner NF->setCallingConv(F->getCallingConv()); 399ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner F->getParent()->getFunctionList().insert(F, NF); 4009e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner 4019e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // Get the alias analysis information that we need to update to reflect our 4029e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // changes. 4039e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); 4049e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner 405ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Loop over all of the callers of the function, transforming the call sites 406ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // to pass in the loaded pointers. 407ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // 408ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner std::vector<Value*> Args; 409ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner while (!F->use_empty()) { 410ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner CallSite CS = CallSite::get(F->use_back()); 411ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Instruction *Call = CS.getInstruction(); 412ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 4139e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // Loop over the operands, inserting GEP and loads in the caller as 4149e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // appropriate. 415ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner CallSite::arg_iterator AI = CS.arg_begin(); 416f201dbc1a4a638659ec3916785196e2c204c7755Chris Lattner for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); 417f201dbc1a4a638659ec3916785196e2c204c7755Chris Lattner I != E; ++I, ++AI) 418ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (!ArgsToPromote.count(I)) 419ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Args.push_back(*AI); // Unmodified argument 420ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner else if (!I->use_empty()) { 4219e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // Non-dead argument: insert GEPs and loads as appropriate. 422beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner ScalarizeTable &ArgIndices = ScalarizedElements[I]; 423beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner for (ScalarizeTable::iterator SI = ArgIndices.begin(), 4249440db886627161a8413e823797569fc7b10beafChris Lattner E = ArgIndices.end(); SI != E; ++SI) { 4259440db886627161a8413e823797569fc7b10beafChris Lattner Value *V = *AI; 4269e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner LoadInst *OrigLoad = OriginalLoads[*SI]; 4279e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner if (!SI->empty()) { 4289440db886627161a8413e823797569fc7b10beafChris Lattner V = new GetElementPtrInst(V, *SI, V->getName()+".idx", Call); 4299e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner AA.copyValue(OrigLoad->getOperand(0), V); 4309e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner } 4319440db886627161a8413e823797569fc7b10beafChris Lattner Args.push_back(new LoadInst(V, V->getName()+".val", Call)); 4329e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner AA.copyValue(OrigLoad, Args.back()); 4339440db886627161a8413e823797569fc7b10beafChris Lattner } 434ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner } 435ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 436ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (ExtraArgHack) 437ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Args.push_back(Constant::getNullValue(Type::IntTy)); 438ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 439ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Push any varargs arguments on the list 440ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner for (; AI != CS.arg_end(); ++AI) 441ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Args.push_back(*AI); 442ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 443ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Instruction *New; 444ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) { 445ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner New = new InvokeInst(NF, II->getNormalDest(), II->getUnwindDest(), 446ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Args, "", Call); 447f201dbc1a4a638659ec3916785196e2c204c7755Chris Lattner cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv()); 448ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner } else { 449ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner New = new CallInst(NF, Args, "", Call); 450f201dbc1a4a638659ec3916785196e2c204c7755Chris Lattner cast<CallInst>(New)->setCallingConv(CS.getCallingConv()); 4511430ef134d36888b99d2e6fedd2b025882593538Chris Lattner if (cast<CallInst>(Call)->isTailCall()) 4521430ef134d36888b99d2e6fedd2b025882593538Chris Lattner cast<CallInst>(New)->setTailCall(); 453ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner } 454ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Args.clear(); 455ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 4569e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // Update the alias analysis implementation to know that we are replacing 4579e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // the old call with a new one. 4589e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner AA.replaceWithNewValue(Call, New); 4599e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner 460ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (!Call->use_empty()) { 461ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Call->replaceAllUsesWith(New); 462ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner std::string Name = Call->getName(); 463ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Call->setName(""); 464ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner New->setName(Name); 465ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner } 466fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 467ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Finally, remove the old call from the program, reducing the use-count of 468ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // F. 469ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Call->getParent()->getInstList().erase(Call); 470ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner } 471ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 472ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Since we have now created the new function, splice the body of the old 473ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // function right into the new function, leaving the old rotting hulk of the 474ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // function empty. 475ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList()); 476ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 477ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Loop over the argument list, transfering uses of the old arguments over to 478ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // the new arguments, also transfering over the names as well. 479ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // 480e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(), I2 = NF->arg_begin(); 481ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner I != E; ++I) 482ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (!ArgsToPromote.count(I)) { 483ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // If this is an unmodified argument, move the name and users over to the 484ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // new version. 485ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner I->replaceAllUsesWith(I2); 486ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner I2->setName(I->getName()); 4879e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner AA.replaceWithNewValue(I, I2); 488ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner ++I2; 4899e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner } else if (I->use_empty()) { 4909e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner AA.deleteValue(I); 4919e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner } else { 492ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Otherwise, if we promoted this argument, then all users are load 493ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // instructions, and all loads should be using the new argument that we 494ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // added. 495beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner ScalarizeTable &ArgIndices = ScalarizedElements[I]; 4969440db886627161a8413e823797569fc7b10beafChris Lattner 497ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner while (!I->use_empty()) { 4989440db886627161a8413e823797569fc7b10beafChris Lattner if (LoadInst *LI = dyn_cast<LoadInst>(I->use_back())) { 4999440db886627161a8413e823797569fc7b10beafChris Lattner assert(ArgIndices.begin()->empty() && 5009440db886627161a8413e823797569fc7b10beafChris Lattner "Load element should sort to front!"); 5019440db886627161a8413e823797569fc7b10beafChris Lattner I2->setName(I->getName()+".val"); 5029440db886627161a8413e823797569fc7b10beafChris Lattner LI->replaceAllUsesWith(I2); 5039e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner AA.replaceWithNewValue(LI, I2); 5049440db886627161a8413e823797569fc7b10beafChris Lattner LI->getParent()->getInstList().erase(LI); 5059e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner DEBUG(std::cerr << "*** Promoted load of argument '" << I->getName() 5069e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner << "' in function '" << F->getName() << "'\n"); 5079440db886627161a8413e823797569fc7b10beafChris Lattner } else { 5089440db886627161a8413e823797569fc7b10beafChris Lattner GetElementPtrInst *GEP = cast<GetElementPtrInst>(I->use_back()); 5099440db886627161a8413e823797569fc7b10beafChris Lattner std::vector<Value*> Operands(GEP->op_begin()+1, GEP->op_end()); 5109440db886627161a8413e823797569fc7b10beafChris Lattner 5119440db886627161a8413e823797569fc7b10beafChris Lattner unsigned ArgNo = 0; 512e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner Function::arg_iterator TheArg = I2; 513beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner for (ScalarizeTable::iterator It = ArgIndices.begin(); 5149440db886627161a8413e823797569fc7b10beafChris Lattner *It != Operands; ++It, ++TheArg) { 5159440db886627161a8413e823797569fc7b10beafChris Lattner assert(It != ArgIndices.end() && "GEP not handled??"); 5169440db886627161a8413e823797569fc7b10beafChris Lattner } 5179440db886627161a8413e823797569fc7b10beafChris Lattner 5189440db886627161a8413e823797569fc7b10beafChris Lattner std::string NewName = I->getName(); 5199440db886627161a8413e823797569fc7b10beafChris Lattner for (unsigned i = 0, e = Operands.size(); i != e; ++i) 5209440db886627161a8413e823797569fc7b10beafChris Lattner if (ConstantInt *CI = dyn_cast<ConstantInt>(Operands[i])) 5219440db886627161a8413e823797569fc7b10beafChris Lattner NewName += "."+itostr((int64_t)CI->getRawValue()); 5229440db886627161a8413e823797569fc7b10beafChris Lattner else 5239440db886627161a8413e823797569fc7b10beafChris Lattner NewName += ".x"; 5249440db886627161a8413e823797569fc7b10beafChris Lattner TheArg->setName(NewName+".val"); 5259440db886627161a8413e823797569fc7b10beafChris Lattner 5269440db886627161a8413e823797569fc7b10beafChris Lattner DEBUG(std::cerr << "*** Promoted agg argument '" << TheArg->getName() 5279440db886627161a8413e823797569fc7b10beafChris Lattner << "' of function '" << F->getName() << "'\n"); 5289440db886627161a8413e823797569fc7b10beafChris Lattner 5299440db886627161a8413e823797569fc7b10beafChris Lattner // All of the uses must be load instructions. Replace them all with 5309440db886627161a8413e823797569fc7b10beafChris Lattner // the argument specified by ArgNo. 5319440db886627161a8413e823797569fc7b10beafChris Lattner while (!GEP->use_empty()) { 5329440db886627161a8413e823797569fc7b10beafChris Lattner LoadInst *L = cast<LoadInst>(GEP->use_back()); 5339440db886627161a8413e823797569fc7b10beafChris Lattner L->replaceAllUsesWith(TheArg); 5349e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner AA.replaceWithNewValue(L, TheArg); 5359440db886627161a8413e823797569fc7b10beafChris Lattner L->getParent()->getInstList().erase(L); 5369440db886627161a8413e823797569fc7b10beafChris Lattner } 5379e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner AA.deleteValue(GEP); 5389440db886627161a8413e823797569fc7b10beafChris Lattner GEP->getParent()->getInstList().erase(GEP); 5399440db886627161a8413e823797569fc7b10beafChris Lattner } 540ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner } 54186a734bd40857db9ef205234f3b58e550ee5959bChris Lattner 5425eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner // Increment I2 past all of the arguments added for this promoted pointer. 5435eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner for (unsigned i = 0, e = ArgIndices.size(); i != e; ++i) 5445eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner ++I2; 545ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner } 546ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 5479e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // Notify the alias analysis implementation that we inserted a new argument. 5489e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner if (ExtraArgHack) 549e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner AA.copyValue(Constant::getNullValue(Type::IntTy), NF->arg_begin()); 5509e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner 5519e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner 5529e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // Tell the alias analysis that the old function is about to disappear. 5539e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner AA.replaceWithNewValue(F, NF); 5549e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner 555ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Now that the old function is dead, delete it. 556ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner F->getParent()->getFunctionList().erase(F); 5575eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner return NF; 558ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner} 559