ArgumentPromotion.cpp revision 46f022a7f105211d5ea2c394e406d1943b80908c
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 757f8897f22e88271cfa114998a4d6088e7c8e8e11Chris Lattner RegisterPass<ArgPromotion> X("argpromotion", 767f8897f22e88271cfa114998a4d6088e7c8e8e11Chris 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 18246f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson/// AccessOccursOnPath - Returns true if and only if a load or GEP instruction 18346f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson/// on Pointer occurs in Path, or in every control-flow path that succeeds it. 18446f022a7f105211d5ea2c394e406d1943b80908cOwen Andersonbool AccessOccursOnPath(Argument* Arg) { 18546f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson std::vector<BasicBlock*> Worklist; 18646f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson Worklist.push_back(Arg->getParent()->begin()); 18746f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson 18846f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson std::set<BasicBlock*> Visited; 18946f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson 19046f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson while (!Worklist.empty()) { 19146f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson BasicBlock* BB = Worklist.back(); 19246f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson Worklist.pop_back(); 19346f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson Visited.insert(BB); 19446f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson 19546f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson bool ContainsAccess = false; 19646f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) 19746f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson if (isa<LoadInst>(I) || isa<GetElementPtrInst>(I)) { 19846f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson ContainsAccess = true; 19946f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson break; 20046f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson } 20146f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson 20246f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson if (ContainsAccess) continue; 20346f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson 20446f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson TerminatorInst* TI = BB->getTerminator(); 20546f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson if (isa<BranchInst>(TI) || isa<SwitchInst>(TI)) { 20646f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson for (unsigned i = 0; i < TI->getNumSuccessors(); ++i) 20746f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson if (!Visited.count(TI->getSuccessor(i))) 20846f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson Worklist.push_back(TI->getSuccessor(i)); 20946f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson } else { 21046f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson return false; 21146f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson } 21246f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson } 21346f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson 21446f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson return true; 21546f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson} 2169e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner 2179e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// isSafeToPromoteArgument - As you might guess from the name of this method, 2189e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// it checks to see if it is both safe and useful to promote the argument. 2199e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// This method limits promotion of aggregates to only promote up to three 2209e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// elements of the aggregate in order to avoid exploding the number of 2219e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// arguments passed in. 222ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattnerbool ArgPromotion::isSafeToPromoteArgument(Argument *Arg) const { 2239440db886627161a8413e823797569fc7b10beafChris Lattner // We can only promote this argument if all of the uses are loads, or are GEP 2249440db886627161a8413e823797569fc7b10beafChris Lattner // instructions (with constant indices) that are subsequently loaded. 22511a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner bool HasLoadInEntryBlock = false; 22611a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner BasicBlock *EntryBlock = Arg->getParent()->begin(); 227ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner std::vector<LoadInst*> Loads; 228beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner std::vector<std::vector<ConstantInt*> > GEPIndices; 229ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner for (Value::use_iterator UI = Arg->use_begin(), E = Arg->use_end(); 230ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner UI != E; ++UI) 231ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) { 232ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (LI->isVolatile()) return false; // Don't hack volatile loads 233ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Loads.push_back(LI); 23411a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner HasLoadInEntryBlock |= LI->getParent() == EntryBlock; 2359440db886627161a8413e823797569fc7b10beafChris Lattner } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) { 2369440db886627161a8413e823797569fc7b10beafChris Lattner if (GEP->use_empty()) { 2379440db886627161a8413e823797569fc7b10beafChris Lattner // Dead GEP's cause trouble later. Just remove them if we run into 2389440db886627161a8413e823797569fc7b10beafChris Lattner // them. 2399e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner getAnalysis<AliasAnalysis>().deleteValue(GEP); 2409440db886627161a8413e823797569fc7b10beafChris Lattner GEP->getParent()->getInstList().erase(GEP); 2419440db886627161a8413e823797569fc7b10beafChris Lattner return isSafeToPromoteArgument(Arg); 2429440db886627161a8413e823797569fc7b10beafChris Lattner } 2439440db886627161a8413e823797569fc7b10beafChris Lattner // Ensure that all of the indices are constants. 244beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner std::vector<ConstantInt*> Operands; 2459440db886627161a8413e823797569fc7b10beafChris Lattner for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i) 246beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner if (ConstantInt *C = dyn_cast<ConstantInt>(GEP->getOperand(i))) 2479440db886627161a8413e823797569fc7b10beafChris Lattner Operands.push_back(C); 2489440db886627161a8413e823797569fc7b10beafChris Lattner else 2499440db886627161a8413e823797569fc7b10beafChris Lattner return false; // Not a constant operand GEP! 2509440db886627161a8413e823797569fc7b10beafChris Lattner 2519440db886627161a8413e823797569fc7b10beafChris Lattner // Ensure that the only users of the GEP are load instructions. 2529440db886627161a8413e823797569fc7b10beafChris Lattner for (Value::use_iterator UI = GEP->use_begin(), E = GEP->use_end(); 2539440db886627161a8413e823797569fc7b10beafChris Lattner UI != E; ++UI) 2549440db886627161a8413e823797569fc7b10beafChris Lattner if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) { 2559440db886627161a8413e823797569fc7b10beafChris Lattner if (LI->isVolatile()) return false; // Don't hack volatile loads 2569440db886627161a8413e823797569fc7b10beafChris Lattner Loads.push_back(LI); 25711a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner HasLoadInEntryBlock |= LI->getParent() == EntryBlock; 2589440db886627161a8413e823797569fc7b10beafChris Lattner } else { 2599440db886627161a8413e823797569fc7b10beafChris Lattner return false; 2609440db886627161a8413e823797569fc7b10beafChris Lattner } 261ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 2629e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // See if there is already a GEP with these indices. If not, check to 2639e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // make sure that we aren't promoting too many elements. If so, nothing 2649e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // to do. 2659440db886627161a8413e823797569fc7b10beafChris Lattner if (std::find(GEPIndices.begin(), GEPIndices.end(), Operands) == 2669440db886627161a8413e823797569fc7b10beafChris Lattner GEPIndices.end()) { 2679440db886627161a8413e823797569fc7b10beafChris Lattner if (GEPIndices.size() == 3) { 2689e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner DEBUG(std::cerr << "argpromotion disable promoting argument '" 2699e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner << Arg->getName() << "' because it would require adding more " 2709e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner << "than 3 arguments to the function.\n"); 2719440db886627161a8413e823797569fc7b10beafChris Lattner // We limit aggregate promotion to only promoting up to three elements 2729440db886627161a8413e823797569fc7b10beafChris Lattner // of the aggregate. 2739440db886627161a8413e823797569fc7b10beafChris Lattner return false; 2749440db886627161a8413e823797569fc7b10beafChris Lattner } 2759440db886627161a8413e823797569fc7b10beafChris Lattner GEPIndices.push_back(Operands); 2769440db886627161a8413e823797569fc7b10beafChris Lattner } 2779440db886627161a8413e823797569fc7b10beafChris Lattner } else { 2789440db886627161a8413e823797569fc7b10beafChris Lattner return false; // Not a load or a GEP. 2799440db886627161a8413e823797569fc7b10beafChris Lattner } 280ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 2819e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner if (Loads.empty()) return true; // No users, this is a dead argument. 282ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 28311a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner // If we decide that we want to promote this argument, the value is going to 28411a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner // be unconditionally loaded in all callees. This is only safe to do if the 28511a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner // pointer was going to be unconditionally loaded anyway (i.e. there is a load 28611a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner // of the pointer in the entry block of the function) or if we can prove that 28711a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner // all pointers passed in are always to legal locations (for example, no null 28811a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner // pointers are passed in, no pointers to free'd memory, etc). 28946f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson if (!AccessOccursOnPath(Arg) && !AllCalleesPassInValidPointerForArgument(Arg)) 29011a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner return false; // Cannot prove that this is safe!! 29111a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner 29211a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner // Okay, now we know that the argument is only used by load instructions and 29311a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner // it is safe to unconditionally load the pointer. Use alias analysis to 29411a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner // check to see if the pointer is guaranteed to not be modified from entry of 29511a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner // the function to each of the load instructions. 296ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Function &F = *Arg->getParent(); 297ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 298ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Because there could be several/many load instructions, remember which 299ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // blocks we know to be transparent to the load. 300ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner std::set<BasicBlock*> TranspBlocks; 30146f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson 30246f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); 3039440db886627161a8413e823797569fc7b10beafChris Lattner TargetData &TD = getAnalysis<TargetData>(); 304ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 305ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner for (unsigned i = 0, e = Loads.size(); i != e; ++i) { 306ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Check to see if the load is invalidated from the start of the block to 307ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // the load itself. 308ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner LoadInst *Load = Loads[i]; 309ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner BasicBlock *BB = Load->getParent(); 3109440db886627161a8413e823797569fc7b10beafChris Lattner 3119440db886627161a8413e823797569fc7b10beafChris Lattner const PointerType *LoadTy = 3129440db886627161a8413e823797569fc7b10beafChris Lattner cast<PointerType>(Load->getOperand(0)->getType()); 3134d0801b243ebb05954ec2173b45ed9f892ef961aChris Lattner unsigned LoadSize = (unsigned)TD.getTypeSize(LoadTy->getElementType()); 3149440db886627161a8413e823797569fc7b10beafChris Lattner 315ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (AA.canInstructionRangeModify(BB->front(), *Load, Arg, LoadSize)) 316ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner return false; // Pointer is invalidated! 317ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 318ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Now check every path from the entry block to the load for transparency. 319ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // To do this, we perform a depth first search on the inverse CFG from the 320ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // loading block. 321ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) 322ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner for (idf_ext_iterator<BasicBlock*> I = idf_ext_begin(*PI, TranspBlocks), 323ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner E = idf_ext_end(*PI, TranspBlocks); I != E; ++I) 324ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (AA.canBasicBlockModify(**I, Arg, LoadSize)) 325ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner return false; 326ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner } 327ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 328ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // If the path from the entry of the function to each load is free of 329ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // instructions that potentially invalidate the load, we can make the 330ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // transformation! 331ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner return true; 332ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner} 333ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 334beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattnernamespace { 335beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner /// GEPIdxComparator - Provide a strong ordering for GEP indices. All Value* 336beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner /// elements are instances of ConstantInt. 337beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner /// 338beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner struct GEPIdxComparator { 339beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner bool operator()(const std::vector<Value*> &LHS, 340beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner const std::vector<Value*> &RHS) const { 341beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner unsigned idx = 0; 342beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner for (; idx < LHS.size() && idx < RHS.size(); ++idx) { 343beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner if (LHS[idx] != RHS[idx]) { 344fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman return cast<ConstantInt>(LHS[idx])->getRawValue() < 345beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner cast<ConstantInt>(RHS[idx])->getRawValue(); 346beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner } 347beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner } 348beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner 349beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner // Return less than if we ran out of stuff in LHS and we didn't run out of 350beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner // stuff in RHS. 351beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner return idx == LHS.size() && idx != RHS.size(); 352beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner } 353beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner }; 354beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner} 355beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner 356beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner 3579e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// DoPromotion - This method actually performs the promotion of the specified 3585eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner/// arguments, and returns the new function. At this point, we know that it's 3595eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner/// safe to do so. 3605eb6f6c829ddfe353f94623aa1009c72be930497Chris LattnerFunction *ArgPromotion::DoPromotion(Function *F, 3615eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner std::vector<Argument*> &Args2Prom) { 362ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner std::set<Argument*> ArgsToPromote(Args2Prom.begin(), Args2Prom.end()); 363fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 364ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Start by computing a new prototype for the function, which is the same as 365ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // the old function, but has modified arguments. 366ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner const FunctionType *FTy = F->getFunctionType(); 367ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner std::vector<const Type*> Params; 368ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 369beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner typedef std::set<std::vector<Value*>, GEPIdxComparator> ScalarizeTable; 370beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner 3719440db886627161a8413e823797569fc7b10beafChris Lattner // ScalarizedElements - If we are promoting a pointer that has elements 3729440db886627161a8413e823797569fc7b10beafChris Lattner // accessed out of it, keep track of which elements are accessed so that we 3739440db886627161a8413e823797569fc7b10beafChris Lattner // can add one argument for each. 3749440db886627161a8413e823797569fc7b10beafChris Lattner // 3759440db886627161a8413e823797569fc7b10beafChris Lattner // Arguments that are directly loaded will have a zero element value here, to 3769440db886627161a8413e823797569fc7b10beafChris Lattner // handle cases where there are both a direct load and GEP accesses. 3779440db886627161a8413e823797569fc7b10beafChris Lattner // 378beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner std::map<Argument*, ScalarizeTable> ScalarizedElements; 3799440db886627161a8413e823797569fc7b10beafChris Lattner 3809e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // OriginalLoads - Keep track of a representative load instruction from the 3819e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // original function so that we can tell the alias analysis implementation 3829e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // what the new GEP/Load instructions we are inserting look like. 3839e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner std::map<std::vector<Value*>, LoadInst*> OriginalLoads; 3849e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner 385e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) 386ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (!ArgsToPromote.count(I)) { 387ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Params.push_back(I->getType()); 3889e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner } else if (I->use_empty()) { 3899e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner ++NumArgumentsDead; 3909e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner } else { 3919440db886627161a8413e823797569fc7b10beafChris Lattner // Okay, this is being promoted. Check to see if there are any GEP uses 3929440db886627161a8413e823797569fc7b10beafChris Lattner // of the argument. 393beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner ScalarizeTable &ArgIndices = ScalarizedElements[I]; 3949440db886627161a8413e823797569fc7b10beafChris Lattner for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; 3959440db886627161a8413e823797569fc7b10beafChris Lattner ++UI) { 3969440db886627161a8413e823797569fc7b10beafChris Lattner Instruction *User = cast<Instruction>(*UI); 39746f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson assert(isa<LoadInst>(User) || isa<GetElementPtrInst>(User)); 39846f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson std::vector<Value*> Indices(User->op_begin()+1, User->op_end()); 39946f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson ArgIndices.insert(Indices); 40046f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson LoadInst *OrigLoad; 40146f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson if (LoadInst *L = dyn_cast<LoadInst>(User)) 40246f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson OrigLoad = L; 40346f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson else 40446f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson OrigLoad = cast<LoadInst>(User->use_back()); 40546f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson OriginalLoads[Indices] = OrigLoad; 4069440db886627161a8413e823797569fc7b10beafChris Lattner } 4079440db886627161a8413e823797569fc7b10beafChris Lattner 4089440db886627161a8413e823797569fc7b10beafChris Lattner // Add a parameter to the function for each element passed in. 409beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner for (ScalarizeTable::iterator SI = ArgIndices.begin(), 4109440db886627161a8413e823797569fc7b10beafChris Lattner E = ArgIndices.end(); SI != E; ++SI) 4119440db886627161a8413e823797569fc7b10beafChris Lattner Params.push_back(GetElementPtrInst::getIndexedType(I->getType(), *SI)); 4129440db886627161a8413e823797569fc7b10beafChris Lattner 4139440db886627161a8413e823797569fc7b10beafChris Lattner if (ArgIndices.size() == 1 && ArgIndices.begin()->empty()) 4149440db886627161a8413e823797569fc7b10beafChris Lattner ++NumArgumentsPromoted; 4159440db886627161a8413e823797569fc7b10beafChris Lattner else 4169440db886627161a8413e823797569fc7b10beafChris Lattner ++NumAggregatesPromoted; 417ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner } 418ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 419ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner const Type *RetTy = FTy->getReturnType(); 420ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 421ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Work around LLVM bug PR56: the CWriter cannot emit varargs functions which 422ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // have zero fixed arguments. 423ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner bool ExtraArgHack = false; 424ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (Params.empty() && FTy->isVarArg()) { 425ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner ExtraArgHack = true; 426ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Params.push_back(Type::IntTy); 427ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner } 428ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner FunctionType *NFTy = FunctionType::get(RetTy, Params, FTy->isVarArg()); 429fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 430ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Create the new function body and insert it into the module... 431ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Function *NF = new Function(NFTy, F->getLinkage(), F->getName()); 432f201dbc1a4a638659ec3916785196e2c204c7755Chris Lattner NF->setCallingConv(F->getCallingConv()); 433ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner F->getParent()->getFunctionList().insert(F, NF); 4349e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner 4359e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // Get the alias analysis information that we need to update to reflect our 4369e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // changes. 4379e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); 4389e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner 439ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Loop over all of the callers of the function, transforming the call sites 440ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // to pass in the loaded pointers. 441ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // 442ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner std::vector<Value*> Args; 443ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner while (!F->use_empty()) { 444ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner CallSite CS = CallSite::get(F->use_back()); 445ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Instruction *Call = CS.getInstruction(); 446ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 4479e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // Loop over the operands, inserting GEP and loads in the caller as 4489e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // appropriate. 449ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner CallSite::arg_iterator AI = CS.arg_begin(); 450f201dbc1a4a638659ec3916785196e2c204c7755Chris Lattner for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); 451f201dbc1a4a638659ec3916785196e2c204c7755Chris Lattner I != E; ++I, ++AI) 452ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (!ArgsToPromote.count(I)) 453ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Args.push_back(*AI); // Unmodified argument 454ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner else if (!I->use_empty()) { 4559e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // Non-dead argument: insert GEPs and loads as appropriate. 456beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner ScalarizeTable &ArgIndices = ScalarizedElements[I]; 457beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner for (ScalarizeTable::iterator SI = ArgIndices.begin(), 4589440db886627161a8413e823797569fc7b10beafChris Lattner E = ArgIndices.end(); SI != E; ++SI) { 4599440db886627161a8413e823797569fc7b10beafChris Lattner Value *V = *AI; 4609e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner LoadInst *OrigLoad = OriginalLoads[*SI]; 4619e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner if (!SI->empty()) { 4629440db886627161a8413e823797569fc7b10beafChris Lattner V = new GetElementPtrInst(V, *SI, V->getName()+".idx", Call); 4639e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner AA.copyValue(OrigLoad->getOperand(0), V); 4649e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner } 4659440db886627161a8413e823797569fc7b10beafChris Lattner Args.push_back(new LoadInst(V, V->getName()+".val", Call)); 4669e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner AA.copyValue(OrigLoad, Args.back()); 4679440db886627161a8413e823797569fc7b10beafChris Lattner } 468ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner } 469ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 470ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (ExtraArgHack) 471ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Args.push_back(Constant::getNullValue(Type::IntTy)); 472ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 473ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Push any varargs arguments on the list 474ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner for (; AI != CS.arg_end(); ++AI) 475ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Args.push_back(*AI); 476ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 477ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Instruction *New; 478ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) { 479ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner New = new InvokeInst(NF, II->getNormalDest(), II->getUnwindDest(), 480ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Args, "", Call); 481f201dbc1a4a638659ec3916785196e2c204c7755Chris Lattner cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv()); 482ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner } else { 483ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner New = new CallInst(NF, Args, "", Call); 484f201dbc1a4a638659ec3916785196e2c204c7755Chris Lattner cast<CallInst>(New)->setCallingConv(CS.getCallingConv()); 4851430ef134d36888b99d2e6fedd2b025882593538Chris Lattner if (cast<CallInst>(Call)->isTailCall()) 4861430ef134d36888b99d2e6fedd2b025882593538Chris Lattner cast<CallInst>(New)->setTailCall(); 487ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner } 488ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Args.clear(); 489ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 4909e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // Update the alias analysis implementation to know that we are replacing 4919e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // the old call with a new one. 4929e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner AA.replaceWithNewValue(Call, New); 4939e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner 494ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (!Call->use_empty()) { 495ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Call->replaceAllUsesWith(New); 496ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner std::string Name = Call->getName(); 497ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Call->setName(""); 498ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner New->setName(Name); 499ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner } 500fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 501ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Finally, remove the old call from the program, reducing the use-count of 502ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // F. 503ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner Call->getParent()->getInstList().erase(Call); 504ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner } 505ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 506ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Since we have now created the new function, splice the body of the old 507ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // function right into the new function, leaving the old rotting hulk of the 508ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // function empty. 509ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList()); 510ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 511ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Loop over the argument list, transfering uses of the old arguments over to 512ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // the new arguments, also transfering over the names as well. 513ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // 514e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(), I2 = NF->arg_begin(); 515ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner I != E; ++I) 516ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner if (!ArgsToPromote.count(I)) { 517ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // If this is an unmodified argument, move the name and users over to the 518ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // new version. 519ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner I->replaceAllUsesWith(I2); 520ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner I2->setName(I->getName()); 5219e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner AA.replaceWithNewValue(I, I2); 522ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner ++I2; 5239e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner } else if (I->use_empty()) { 5249e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner AA.deleteValue(I); 5259e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner } else { 526ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Otherwise, if we promoted this argument, then all users are load 527ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // instructions, and all loads should be using the new argument that we 528ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // added. 529beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner ScalarizeTable &ArgIndices = ScalarizedElements[I]; 5309440db886627161a8413e823797569fc7b10beafChris Lattner 531ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner while (!I->use_empty()) { 5329440db886627161a8413e823797569fc7b10beafChris Lattner if (LoadInst *LI = dyn_cast<LoadInst>(I->use_back())) { 5339440db886627161a8413e823797569fc7b10beafChris Lattner assert(ArgIndices.begin()->empty() && 5349440db886627161a8413e823797569fc7b10beafChris Lattner "Load element should sort to front!"); 5359440db886627161a8413e823797569fc7b10beafChris Lattner I2->setName(I->getName()+".val"); 5369440db886627161a8413e823797569fc7b10beafChris Lattner LI->replaceAllUsesWith(I2); 5379e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner AA.replaceWithNewValue(LI, I2); 5389440db886627161a8413e823797569fc7b10beafChris Lattner LI->getParent()->getInstList().erase(LI); 5399e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner DEBUG(std::cerr << "*** Promoted load of argument '" << I->getName() 5409e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner << "' in function '" << F->getName() << "'\n"); 5419440db886627161a8413e823797569fc7b10beafChris Lattner } else { 5429440db886627161a8413e823797569fc7b10beafChris Lattner GetElementPtrInst *GEP = cast<GetElementPtrInst>(I->use_back()); 5439440db886627161a8413e823797569fc7b10beafChris Lattner std::vector<Value*> Operands(GEP->op_begin()+1, GEP->op_end()); 5449440db886627161a8413e823797569fc7b10beafChris Lattner 5459440db886627161a8413e823797569fc7b10beafChris Lattner unsigned ArgNo = 0; 546e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner Function::arg_iterator TheArg = I2; 547beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner for (ScalarizeTable::iterator It = ArgIndices.begin(); 5489440db886627161a8413e823797569fc7b10beafChris Lattner *It != Operands; ++It, ++TheArg) { 5499440db886627161a8413e823797569fc7b10beafChris Lattner assert(It != ArgIndices.end() && "GEP not handled??"); 5509440db886627161a8413e823797569fc7b10beafChris Lattner } 5519440db886627161a8413e823797569fc7b10beafChris Lattner 5529440db886627161a8413e823797569fc7b10beafChris Lattner std::string NewName = I->getName(); 5539440db886627161a8413e823797569fc7b10beafChris Lattner for (unsigned i = 0, e = Operands.size(); i != e; ++i) 5549440db886627161a8413e823797569fc7b10beafChris Lattner if (ConstantInt *CI = dyn_cast<ConstantInt>(Operands[i])) 5559440db886627161a8413e823797569fc7b10beafChris Lattner NewName += "."+itostr((int64_t)CI->getRawValue()); 5569440db886627161a8413e823797569fc7b10beafChris Lattner else 5579440db886627161a8413e823797569fc7b10beafChris Lattner NewName += ".x"; 5589440db886627161a8413e823797569fc7b10beafChris Lattner TheArg->setName(NewName+".val"); 5599440db886627161a8413e823797569fc7b10beafChris Lattner 5609440db886627161a8413e823797569fc7b10beafChris Lattner DEBUG(std::cerr << "*** Promoted agg argument '" << TheArg->getName() 5619440db886627161a8413e823797569fc7b10beafChris Lattner << "' of function '" << F->getName() << "'\n"); 5629440db886627161a8413e823797569fc7b10beafChris Lattner 5639440db886627161a8413e823797569fc7b10beafChris Lattner // All of the uses must be load instructions. Replace them all with 5649440db886627161a8413e823797569fc7b10beafChris Lattner // the argument specified by ArgNo. 5659440db886627161a8413e823797569fc7b10beafChris Lattner while (!GEP->use_empty()) { 5669440db886627161a8413e823797569fc7b10beafChris Lattner LoadInst *L = cast<LoadInst>(GEP->use_back()); 5679440db886627161a8413e823797569fc7b10beafChris Lattner L->replaceAllUsesWith(TheArg); 5689e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner AA.replaceWithNewValue(L, TheArg); 5699440db886627161a8413e823797569fc7b10beafChris Lattner L->getParent()->getInstList().erase(L); 5709440db886627161a8413e823797569fc7b10beafChris Lattner } 5719e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner AA.deleteValue(GEP); 5729440db886627161a8413e823797569fc7b10beafChris Lattner GEP->getParent()->getInstList().erase(GEP); 5739440db886627161a8413e823797569fc7b10beafChris Lattner } 574ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner } 57586a734bd40857db9ef205234f3b58e550ee5959bChris Lattner 5765eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner // Increment I2 past all of the arguments added for this promoted pointer. 5775eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner for (unsigned i = 0, e = ArgIndices.size(); i != e; ++i) 5785eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner ++I2; 579ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner } 580ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner 5819e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // Notify the alias analysis implementation that we inserted a new argument. 5829e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner if (ExtraArgHack) 583e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner AA.copyValue(Constant::getNullValue(Type::IntTy), NF->arg_begin()); 5849e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner 5859e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner 5869e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner // Tell the alias analysis that the old function is about to disappear. 5879e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner AA.replaceWithNewValue(F, NF); 5889e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner 589ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner // Now that the old function is dead, delete it. 590ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner F->getParent()->getFunctionList().erase(F); 5915eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner return NF; 592ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner} 593