GlobalsModRef.cpp revision f5eaf3c62c7b2f2da1f85ac411c4f13bcbf131f1
13b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner//===- GlobalsModRef.cpp - Simple Mod/Ref Analysis for Globals ------------===// 23b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner// 33b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner// The LLVM Compiler Infrastructure 43b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner// 53b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner// This file was developed by the LLVM research group and is distributed under 63b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner// the University of Illinois Open Source License. See LICENSE.TXT for details. 73b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner// 83b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner//===----------------------------------------------------------------------===// 93b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner// 103b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner// This simple pass provides alias and mod/ref information for global values 11fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner// that do not have their address taken, and keeps track of whether functions 12fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner// read or write memory (are "pure"). For this simple (but very common) case, 13fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner// we can provide pretty accurate and useful information. 143b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner// 153b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner//===----------------------------------------------------------------------===// 163b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 173b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include "llvm/Analysis/Passes.h" 183b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include "llvm/Module.h" 193b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include "llvm/Pass.h" 203b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include "llvm/Instructions.h" 213b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include "llvm/Constants.h" 223b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include "llvm/Analysis/AliasAnalysis.h" 233b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include "llvm/Analysis/CallGraph.h" 24fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner#include "llvm/Support/InstIterator.h" 25551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/CommandLine.h" 26551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/Statistic.h" 27551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/SCCIterator.h" 283b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include <set> 293b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnerusing namespace llvm; 303b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 313b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnernamespace { 323b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner Statistic<> 333b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner NumNonAddrTakenGlobalVars("globalsmodref-aa", 343b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner "Number of global vars without address taken"); 353b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner Statistic<> 363b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner NumNonAddrTakenFunctions("globalsmodref-aa", 373b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner "Number of functions without address taken"); 38fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner Statistic<> 39fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner NumNoMemFunctions("globalsmodref-aa", 40fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner "Number of functions that do not access memory"); 41fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner Statistic<> 42fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner NumReadMemFunctions("globalsmodref-aa", 43fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner "Number of functions that only read memory"); 44fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner 45fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// FunctionRecord - One instance of this structure is stored for every 46fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// function in the program. Later, the entries for these functions are 47fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// removed if the function is found to call an external function (in which 48fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// case we know nothing about it. 49fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner struct FunctionRecord { 50fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// GlobalInfo - Maintain mod/ref info for all of the globals without 51fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// addresses taken that are read or written (transitively) by this 52fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// function. 53fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner std::map<GlobalValue*, unsigned> GlobalInfo; 54fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner 55fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner unsigned getInfoForGlobal(GlobalValue *GV) const { 56fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner std::map<GlobalValue*, unsigned>::const_iterator I = GlobalInfo.find(GV); 57fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (I != GlobalInfo.end()) 58fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner return I->second; 59fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner return 0; 60fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner } 61fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner 62fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// FunctionEffect - Capture whether or not this function reads or writes to 63fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// ANY memory. If not, we can do a lot of aggressive analysis on it. 64fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner unsigned FunctionEffect; 65105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner 66105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner FunctionRecord() : FunctionEffect(0) {} 67fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner }; 683b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 69fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// GlobalsModRef - The actual analysis pass. 70b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner class GlobalsModRef : public ModulePass, public AliasAnalysis { 71fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// NonAddressTakenGlobals - The globals that do not have their addresses 72fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// taken. 73fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner std::set<GlobalValue*> NonAddressTakenGlobals; 743b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 753b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner /// FunctionInfo - For each function, keep track of what globals are 763b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner /// modified or read. 77fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner std::map<Function*, FunctionRecord> FunctionInfo; 783b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 793b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner public: 80b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner bool runOnModule(Module &M) { 813b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner InitializeAliasAnalysis(this); // set up super class 823b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner AnalyzeGlobals(M); // find non-addr taken globals 833b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner AnalyzeCallGraph(getAnalysis<CallGraph>(), M); // Propagate on CG 843b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner return false; 853b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } 863b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 873b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner virtual void getAnalysisUsage(AnalysisUsage &AU) const { 883b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner AliasAnalysis::getAnalysisUsage(AU); 893b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner AU.addRequired<CallGraph>(); 903b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner AU.setPreservesAll(); // Does not transform code 913b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } 923b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 933b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner //------------------------------------------------ 943b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner // Implement the AliasAnalysis API 953b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner // 963b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner AliasResult alias(const Value *V1, unsigned V1Size, 973b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner const Value *V2, unsigned V2Size); 983b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size); 994a7ebfa4111305ea22fc753d4f029eed88149662Reid Spencer ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) { 1004a7ebfa4111305ea22fc753d4f029eed88149662Reid Spencer return AliasAnalysis::getModRefInfo(CS1,CS2); 1014a7ebfa4111305ea22fc753d4f029eed88149662Reid Spencer } 1023b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner bool hasNoModRefInfoForCalls() const { return false; } 1033b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 1040af024c5d00c2a2d0e35ba33f4aaf4ed3fadfae2Chris Lattner /// getModRefBehavior - Return the behavior of the specified function if 1050af024c5d00c2a2d0e35ba33f4aaf4ed3fadfae2Chris Lattner /// called from the specified call site. The call site may be null in which 1060af024c5d00c2a2d0e35ba33f4aaf4ed3fadfae2Chris Lattner /// case the most generic behavior of this function should be returned. 10741925f87d3e7fc9eb2b583267b99012dcb2774a0Chris Lattner virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS, 10841925f87d3e7fc9eb2b583267b99012dcb2774a0Chris Lattner std::vector<PointerAccessInfo> *Info) { 109fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (FunctionRecord *FR = getFunctionInfo(F)) 110fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (FR->FunctionEffect == 0) 1110af024c5d00c2a2d0e35ba33f4aaf4ed3fadfae2Chris Lattner return DoesNotAccessMemory; 1120af024c5d00c2a2d0e35ba33f4aaf4ed3fadfae2Chris Lattner else if ((FR->FunctionEffect & Mod) == 0) 1130af024c5d00c2a2d0e35ba33f4aaf4ed3fadfae2Chris Lattner return OnlyReadsMemory; 114ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner return AliasAnalysis::getModRefBehavior(F, CS, Info); 115fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner } 116fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner 1173b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner virtual void deleteValue(Value *V); 1183b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner virtual void copyValue(Value *From, Value *To); 1193b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 1203b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner private: 121fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// getFunctionInfo - Return the function info for the function, or null if 122fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// the function calls an external function (in which case we don't have 123fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// anything useful to say about it). 124fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner FunctionRecord *getFunctionInfo(Function *F) { 125fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner std::map<Function*, FunctionRecord>::iterator I = FunctionInfo.find(F); 126fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (I != FunctionInfo.end()) 127fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner return &I->second; 128fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner return 0; 129fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner } 130fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner 1313b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner void AnalyzeGlobals(Module &M); 1323b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner void AnalyzeCallGraph(CallGraph &CG, Module &M); 133fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner void AnalyzeSCC(std::vector<CallGraphNode *> &SCC); 1343b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner bool AnalyzeUsesOfGlobal(Value *V, std::vector<Function*> &Readers, 1353b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner std::vector<Function*> &Writers); 1363b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner }; 1373b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 1383b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner RegisterOpt<GlobalsModRef> X("globalsmodref-aa", 1393b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner "Simple mod/ref analysis for globals"); 1403b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner RegisterAnalysisGroup<AliasAnalysis, GlobalsModRef> Y; 1413b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner} 1423b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 1433b04a8ac455568f9b98ab7e9a43076260cb737aeChris LattnerPass *llvm::createGlobalsModRefPass() { return new GlobalsModRef(); } 1443b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 1453b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 1463b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// AnalyzeGlobalUses - Scan through the users of all of the internal 1473b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// GlobalValue's in the program. If none of them have their "Address taken" 1483b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// (really, their address passed to something nontrivial), record this fact, 1493b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// and record the functions that they are used directly in. 1503b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnervoid GlobalsModRef::AnalyzeGlobals(Module &M) { 1513b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner std::vector<Function*> Readers, Writers; 1523b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 1533b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (I->hasInternalLinkage()) { 1543b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (!AnalyzeUsesOfGlobal(I, Readers, Writers)) { 155fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner // Remember that we are tracking this global. 156fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner NonAddressTakenGlobals.insert(I); 1573b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner ++NumNonAddrTakenFunctions; 1583b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } 1593b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner Readers.clear(); Writers.clear(); 1603b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } 1613b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 162f5eaf3c62c7b2f2da1f85ac411c4f13bcbf131f1Chris Lattner for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 163f5eaf3c62c7b2f2da1f85ac411c4f13bcbf131f1Chris Lattner I != E; ++I) 1643b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (I->hasInternalLinkage()) { 1653b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (!AnalyzeUsesOfGlobal(I, Readers, Writers)) { 1663b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner // Remember that we are tracking this global, and the mod/ref fns 167fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner NonAddressTakenGlobals.insert(I); 168fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner for (unsigned i = 0, e = Readers.size(); i != e; ++i) 169fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner FunctionInfo[Readers[i]].GlobalInfo[I] |= Ref; 170fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner 171fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (!I->isConstant()) // No need to keep track of writers to constants 172fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner for (unsigned i = 0, e = Writers.size(); i != e; ++i) 173fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner FunctionInfo[Writers[i]].GlobalInfo[I] |= Mod; 1743b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner ++NumNonAddrTakenGlobalVars; 1753b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } 1763b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner Readers.clear(); Writers.clear(); 1773b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } 1783b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner} 1793b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 1803b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// AnalyzeUsesOfGlobal - Look at all of the users of the specified global value 1813b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// derived pointer. If this is used by anything complex (i.e., the address 1823b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// escapes), return true. Also, while we are at it, keep track of those 1833b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// functions that read and write to the value. 1843b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnerbool GlobalsModRef::AnalyzeUsesOfGlobal(Value *V, 1853b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner std::vector<Function*> &Readers, 1863b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner std::vector<Function*> &Writers) { 187fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (!isa<PointerType>(V->getType())) return true; 1883b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 1893b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI) 1903b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) { 1913b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner Readers.push_back(LI->getParent()->getParent()); 1923b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { 1933b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (V == SI->getOperand(0)) return true; // Storing the pointer 1943b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner Writers.push_back(SI->getParent()->getParent()); 1953b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) { 1963b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (AnalyzeUsesOfGlobal(GEP, Readers, Writers)) return true; 1973b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } else if (CallInst *CI = dyn_cast<CallInst>(*UI)) { 1983b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner // Make sure that this is just the function being called, not that it is 1993b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner // passing into the function. 2003b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i) 2013b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (CI->getOperand(i) == V) return true; 2023b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) { 2033b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner // Make sure that this is just the function being called, not that it is 2043b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner // passing into the function. 2053b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner for (unsigned i = 3, e = II->getNumOperands(); i != e; ++i) 2063b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (II->getOperand(i) == V) return true; 2073b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(*UI)) { 2083b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (CE->getOpcode() == Instruction::GetElementPtr || 2093b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner CE->getOpcode() == Instruction::Cast) { 2103b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (AnalyzeUsesOfGlobal(CE, Readers, Writers)) 2113b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner return true; 2123b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } else { 2133b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner return true; 2143b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } 215e840434755e2165ac20ec55e9d5ff3d2defac2d2Reid Spencer } else if (GlobalValue *GV = dyn_cast<GlobalValue>(*UI)) { 216e840434755e2165ac20ec55e9d5ff3d2defac2d2Reid Spencer if (AnalyzeUsesOfGlobal(GV, Readers, Writers)) return true; 2173b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } else { 2183b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner return true; 2193b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } 2203b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner return false; 2213b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner} 2223b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 2233b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// AnalyzeCallGraph - At this point, we know the functions where globals are 2243b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// immediately stored to and read from. Propagate this information up the call 225fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner/// graph to all callers and compute the mod/ref info for all memory for each 226fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner/// function. 2273b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnervoid GlobalsModRef::AnalyzeCallGraph(CallGraph &CG, Module &M) { 2283b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner // We do a bottom-up SCC traversal of the call graph. In other words, we 2293b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner // visit all callees before callers (leaf-first). 230fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner for (scc_iterator<CallGraph*> I = scc_begin(&CG), E = scc_end(&CG); I!=E; ++I) 231105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner if ((*I).size() != 1) { 232fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner AnalyzeSCC(*I); 233105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner } else if (Function *F = (*I)[0]->getFunction()) { 234105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner if (!F->isExternal()) { 235105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner // Nonexternal function. 236105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner AnalyzeSCC(*I); 237105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner } else { 238105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner // Otherwise external function. Handle intrinsics and other special 239105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner // cases here. 240105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner if (getAnalysis<AliasAnalysis>().doesNotAccessMemory(F)) 241105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner // If it does not access memory, process the function, causing us to 242105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner // realize it doesn't do anything (the body is empty). 243105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner AnalyzeSCC(*I); 244105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner else { 245105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner // Otherwise, don't process it. This will cause us to conservatively 246105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner // assume the worst. 247105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner } 248105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner } 249105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner } else { 250105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner // Do not process the external node, assume the worst. 251105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner } 252fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner} 253fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner 254fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattnervoid GlobalsModRef::AnalyzeSCC(std::vector<CallGraphNode *> &SCC) { 255fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner assert(!SCC.empty() && "SCC with no functions?"); 256fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner FunctionRecord &FR = FunctionInfo[SCC[0]->getFunction()]; 257fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner 258fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner bool CallsExternal = false; 259fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner unsigned FunctionEffect = 0; 260fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner 261fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner // Collect the mod/ref properties due to called functions. We only compute 262fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner // one mod-ref set 263fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner for (unsigned i = 0, e = SCC.size(); i != e && !CallsExternal; ++i) 264fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end(); 265fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner CI != E; ++CI) 266fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (Function *Callee = (*CI)->getFunction()) { 267fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (FunctionRecord *CalleeFR = getFunctionInfo(Callee)) { 268fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner // Propagate function effect up. 269fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner FunctionEffect |= CalleeFR->FunctionEffect; 270fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner 271fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner // Incorporate callee's effects on globals into our info. 272fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner for (std::map<GlobalValue*, unsigned>::iterator GI = 273fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner CalleeFR->GlobalInfo.begin(), E = CalleeFR->GlobalInfo.end(); 274fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner GI != E; ++GI) 275fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner FR.GlobalInfo[GI->first] |= GI->second; 2763b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 2773b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } else { 278ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner // Okay, if we can't say anything about it, maybe some other alias 279ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner // analysis can. 280ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner ModRefBehavior MRB = 281ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner AliasAnalysis::getModRefBehavior(Callee, CallSite()); 282ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner if (MRB != DoesNotAccessMemory) { 283ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner if (MRB == OnlyReadsMemory) { 284ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner // This reads memory, but we don't know what, just say that it 285ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner // reads all globals. 286ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner for (std::map<GlobalValue*, unsigned>::iterator 287ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner GI = CalleeFR->GlobalInfo.begin(), 288ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner E = CalleeFR->GlobalInfo.end(); 289ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner GI != E; ++GI) 290ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner FR.GlobalInfo[GI->first] |= Ref; 291ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner 292ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner } else { 293ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner CallsExternal = true; 294ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner break; 295ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner } 296ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner } 2973b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } 298fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner } else { 299fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner CallsExternal = true; 300fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner break; 3013b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } 302fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner 303fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner // If this SCC calls an external function, we can't say anything about it, so 304fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner // remove all SCC functions from the FunctionInfo map. 305fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (CallsExternal) { 3063b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner for (unsigned i = 0, e = SCC.size(); i != e; ++i) 307fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner FunctionInfo.erase(SCC[i]->getFunction()); 308fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner return; 3093b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } 310fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner 311fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner // Otherwise, unless we already know that this function mod/refs memory, scan 312fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner // the function bodies to see if there are any explicit loads or stores. 313fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (FunctionEffect != ModRef) { 314fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner for (unsigned i = 0, e = SCC.size(); i != e && FunctionEffect != ModRef;++i) 315fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner for (inst_iterator II = inst_begin(SCC[i]->getFunction()), 316fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner E = inst_end(SCC[i]->getFunction()); 317fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner II != E && FunctionEffect != ModRef; ++II) 318fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (isa<LoadInst>(*II)) 319fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner FunctionEffect |= Ref; 320fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner else if (isa<StoreInst>(*II)) 321fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner FunctionEffect |= Mod; 322fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner } 323fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner 324fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if ((FunctionEffect & Mod) == 0) 325fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner ++NumReadMemFunctions; 326fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (FunctionEffect == 0) 327fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner ++NumNoMemFunctions; 328fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner FR.FunctionEffect = FunctionEffect; 329fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner 330fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner // Finally, now that we know the full effect on this SCC, clone the 331fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner // information to each function in the SCC. 332fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner for (unsigned i = 1, e = SCC.size(); i != e; ++i) 333fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner FunctionInfo[SCC[i]->getFunction()] = FR; 3343b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner} 3353b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 3363b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 3373b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 3383b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// getUnderlyingObject - This traverses the use chain to figure out what object 3393b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// the specified value points to. If the value points to, or is derived from, 3403b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// a global object, return it. 3413b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnerstatic const GlobalValue *getUnderlyingObject(const Value *V) { 342fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (!isa<PointerType>(V->getType())) return 0; 3433b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 3443b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner // If we are at some type of object... return it. 3453b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV; 3463b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 3473b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner // Traverse through different addressing mechanisms... 3483b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (const Instruction *I = dyn_cast<Instruction>(V)) { 3493b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (isa<CastInst>(I) || isa<GetElementPtrInst>(I)) 3503b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner return getUnderlyingObject(I->getOperand(0)); 3513b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { 3523b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (CE->getOpcode() == Instruction::Cast || 3533b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner CE->getOpcode() == Instruction::GetElementPtr) 3543b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner return getUnderlyingObject(CE->getOperand(0)); 3553b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } 3563b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner return 0; 3573b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner} 3583b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 3593b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// alias - If one of the pointers is to a global that we are tracking, and the 3603b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// other is some random pointer, we know there cannot be an alias, because the 3613b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// address of the global isn't taken. 3623b04a8ac455568f9b98ab7e9a43076260cb737aeChris LattnerAliasAnalysis::AliasResult 3633b04a8ac455568f9b98ab7e9a43076260cb737aeChris LattnerGlobalsModRef::alias(const Value *V1, unsigned V1Size, 3643b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner const Value *V2, unsigned V2Size) { 3653b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner GlobalValue *GV1 = const_cast<GlobalValue*>(getUnderlyingObject(V1)); 3663b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner GlobalValue *GV2 = const_cast<GlobalValue*>(getUnderlyingObject(V2)); 3673b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 3683b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner // If the global's address is taken, pretend we don't know it's a pointer to 3693b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner // the global. 3703b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (GV1 && !NonAddressTakenGlobals.count(GV1)) GV1 = 0; 3713b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (GV2 && !NonAddressTakenGlobals.count(GV2)) GV2 = 0; 3723b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 3733b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if ((GV1 || GV2) && GV1 != GV2) 3743b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner return NoAlias; 3753b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 3763b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner return AliasAnalysis::alias(V1, V1Size, V2, V2Size); 3773b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner} 3783b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 3793b04a8ac455568f9b98ab7e9a43076260cb737aeChris LattnerAliasAnalysis::ModRefResult 3803b04a8ac455568f9b98ab7e9a43076260cb737aeChris LattnerGlobalsModRef::getModRefInfo(CallSite CS, Value *P, unsigned Size) { 3813b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner unsigned Known = ModRef; 3823b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 3833b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner // If we are asking for mod/ref info of a direct call with a pointer to a 384fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner // global we are tracking, return information if we have it. 3853b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (GlobalValue *GV = const_cast<GlobalValue*>(getUnderlyingObject(P))) 3863b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (GV->hasInternalLinkage()) 387fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (Function *F = CS.getCalledFunction()) 388fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (NonAddressTakenGlobals.count(GV)) 389fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (FunctionRecord *FR = getFunctionInfo(F)) 390fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner Known = FR->getInfoForGlobal(GV); 3913b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 3923b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (Known == NoModRef) 3933b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner return NoModRef; // No need to query other mod/ref analyses 3943b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner return ModRefResult(Known & AliasAnalysis::getModRefInfo(CS, P, Size)); 3953b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner} 3963b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 3973b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 3983b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner//===----------------------------------------------------------------------===// 3993b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner// Methods to update the analysis as a result of the client transformation. 4003b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner// 4013b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnervoid GlobalsModRef::deleteValue(Value *V) { 402fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) 403fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner NonAddressTakenGlobals.erase(GV); 4043b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner} 4053b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 4063b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnervoid GlobalsModRef::copyValue(Value *From, Value *To) { 4073b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner} 408