GlobalsModRef.cpp revision 3b27d68c6af2582df0962557f1fe5c3f70f46e3f
13b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner//===- GlobalsModRef.cpp - Simple Mod/Ref Analysis for Globals ------------===// 22b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman// 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. 72b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman// 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 173b27d68c6af2582df0962557f1fe5c3f70f46e3fChris Lattner#define DEBUG_TYPE "globalsmodref-aa" 183b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include "llvm/Analysis/Passes.h" 193b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include "llvm/Module.h" 203b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include "llvm/Pass.h" 213b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include "llvm/Instructions.h" 223b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include "llvm/Constants.h" 23ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner#include "llvm/DerivedTypes.h" 243b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include "llvm/Analysis/AliasAnalysis.h" 253b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include "llvm/Analysis/CallGraph.h" 26fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner#include "llvm/Support/InstIterator.h" 27551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/CommandLine.h" 28551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/Statistic.h" 29551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/SCCIterator.h" 303b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include <set> 313b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnerusing namespace llvm; 323b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 333b27d68c6af2582df0962557f1fe5c3f70f46e3fChris LattnerSTATISTIC(NumNonAddrTakenGlobalVars, 343b27d68c6af2582df0962557f1fe5c3f70f46e3fChris Lattner "Number of global vars without address taken"); 353b27d68c6af2582df0962557f1fe5c3f70f46e3fChris LattnerSTATISTIC(NumNonAddrTakenFunctions,"Number of functions without address taken"); 363b27d68c6af2582df0962557f1fe5c3f70f46e3fChris LattnerSTATISTIC(NumNoMemFunctions, "Number of functions that do not access memory"); 373b27d68c6af2582df0962557f1fe5c3f70f46e3fChris LattnerSTATISTIC(NumReadMemFunctions, "Number of functions that only read memory"); 383b27d68c6af2582df0962557f1fe5c3f70f46e3fChris LattnerSTATISTIC(NumIndirectGlobalVars, "Number of indirect global objects"); 393b27d68c6af2582df0962557f1fe5c3f70f46e3fChris Lattner 403b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnernamespace { 41fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// FunctionRecord - One instance of this structure is stored for every 42fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// function in the program. Later, the entries for these functions are 43fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// removed if the function is found to call an external function (in which 44fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// case we know nothing about it. 45fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner struct FunctionRecord { 46fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// GlobalInfo - Maintain mod/ref info for all of the globals without 47fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// addresses taken that are read or written (transitively) by this 48fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// function. 49fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner std::map<GlobalValue*, unsigned> GlobalInfo; 50fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner 51fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner unsigned getInfoForGlobal(GlobalValue *GV) const { 52fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner std::map<GlobalValue*, unsigned>::const_iterator I = GlobalInfo.find(GV); 53fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (I != GlobalInfo.end()) 54fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner return I->second; 55fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner return 0; 56fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner } 572b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman 58fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// FunctionEffect - Capture whether or not this function reads or writes to 59fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// ANY memory. If not, we can do a lot of aggressive analysis on it. 60fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner unsigned FunctionEffect; 61105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner 62105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner FunctionRecord() : FunctionEffect(0) {} 63fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner }; 643b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 65fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// GlobalsModRef - The actual analysis pass. 66b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner class GlobalsModRef : public ModulePass, public AliasAnalysis { 67fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// NonAddressTakenGlobals - The globals that do not have their addresses 68fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// taken. 69fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner std::set<GlobalValue*> NonAddressTakenGlobals; 703b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 71ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner /// IndirectGlobals - The memory pointed to by this global is known to be 72ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner /// 'owned' by the global. 73ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner std::set<GlobalValue*> IndirectGlobals; 74ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner 75ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner /// AllocsForIndirectGlobals - If an instruction allocates memory for an 76ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner /// indirect global, this map indicates which one. 77ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner std::map<Value*, GlobalValue*> AllocsForIndirectGlobals; 78ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner 793b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner /// FunctionInfo - For each function, keep track of what globals are 803b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner /// modified or read. 81fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner std::map<Function*, FunctionRecord> FunctionInfo; 823b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 833b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner public: 84b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner bool runOnModule(Module &M) { 853b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner InitializeAliasAnalysis(this); // set up super class 863b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner AnalyzeGlobals(M); // find non-addr taken globals 873b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner AnalyzeCallGraph(getAnalysis<CallGraph>(), M); // Propagate on CG 883b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner return false; 893b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } 903b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 913b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner virtual void getAnalysisUsage(AnalysisUsage &AU) const { 923b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner AliasAnalysis::getAnalysisUsage(AU); 933b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner AU.addRequired<CallGraph>(); 943b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner AU.setPreservesAll(); // Does not transform code 953b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } 963b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 973b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner //------------------------------------------------ 983b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner // Implement the AliasAnalysis API 992b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman // 1003b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner AliasResult alias(const Value *V1, unsigned V1Size, 1013b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner const Value *V2, unsigned V2Size); 1023b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size); 1034a7ebfa4111305ea22fc753d4f029eed88149662Reid Spencer ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) { 1044a7ebfa4111305ea22fc753d4f029eed88149662Reid Spencer return AliasAnalysis::getModRefInfo(CS1,CS2); 1054a7ebfa4111305ea22fc753d4f029eed88149662Reid Spencer } 1063b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner bool hasNoModRefInfoForCalls() const { return false; } 1073b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 1080af024c5d00c2a2d0e35ba33f4aaf4ed3fadfae2Chris Lattner /// getModRefBehavior - Return the behavior of the specified function if 1090af024c5d00c2a2d0e35ba33f4aaf4ed3fadfae2Chris Lattner /// called from the specified call site. The call site may be null in which 1100af024c5d00c2a2d0e35ba33f4aaf4ed3fadfae2Chris Lattner /// case the most generic behavior of this function should be returned. 11141925f87d3e7fc9eb2b583267b99012dcb2774a0Chris Lattner virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS, 11241925f87d3e7fc9eb2b583267b99012dcb2774a0Chris Lattner std::vector<PointerAccessInfo> *Info) { 113fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (FunctionRecord *FR = getFunctionInfo(F)) 114fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (FR->FunctionEffect == 0) 1150af024c5d00c2a2d0e35ba33f4aaf4ed3fadfae2Chris Lattner return DoesNotAccessMemory; 116dedf2bd5a34dac25e4245f58bb902ced6b64edd9Misha Brukman else if ((FR->FunctionEffect & Mod) == 0) 117dedf2bd5a34dac25e4245f58bb902ced6b64edd9Misha Brukman return OnlyReadsMemory; 1182b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman return AliasAnalysis::getModRefBehavior(F, CS, Info); 119fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner } 120fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner 1213b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner virtual void deleteValue(Value *V); 1223b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner virtual void copyValue(Value *From, Value *To); 1233b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 1243b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner private: 125fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// getFunctionInfo - Return the function info for the function, or null if 126fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// the function calls an external function (in which case we don't have 127fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner /// anything useful to say about it). 128fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner FunctionRecord *getFunctionInfo(Function *F) { 129fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner std::map<Function*, FunctionRecord>::iterator I = FunctionInfo.find(F); 130fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (I != FunctionInfo.end()) 131fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner return &I->second; 132fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner return 0; 133fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner } 134fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner 1353b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner void AnalyzeGlobals(Module &M); 1363b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner void AnalyzeCallGraph(CallGraph &CG, Module &M); 137fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner void AnalyzeSCC(std::vector<CallGraphNode *> &SCC); 138ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner bool AnalyzeUsesOfPointer(Value *V, std::vector<Function*> &Readers, 139ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner std::vector<Function*> &Writers, 140ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner GlobalValue *OkayStoreDest = 0); 141ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner bool AnalyzeIndirectGlobalMemory(GlobalValue *GV); 1423b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner }; 1432b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman 1447f8897f22e88271cfa114998a4d6088e7c8e8e11Chris Lattner RegisterPass<GlobalsModRef> X("globalsmodref-aa", 1457f8897f22e88271cfa114998a4d6088e7c8e8e11Chris Lattner "Simple mod/ref analysis for globals"); 146a5370172b64bed5daf8e2869d7bf7cb52f80d6b7Chris Lattner RegisterAnalysisGroup<AliasAnalysis> Y(X); 1473b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner} 1483b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 1493b04a8ac455568f9b98ab7e9a43076260cb737aeChris LattnerPass *llvm::createGlobalsModRefPass() { return new GlobalsModRef(); } 1503b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 151ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// getUnderlyingObject - This traverses the use chain to figure out what object 152ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// the specified value points to. If the value points to, or is derived from, 153ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// a global object, return it. 154ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattnerstatic Value *getUnderlyingObject(Value *V) { 155ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (!isa<PointerType>(V->getType())) return V; 156ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner 157ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // If we are at some type of object... return it. 158ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV; 159ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner 160ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // Traverse through different addressing mechanisms. 161ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (Instruction *I = dyn_cast<Instruction>(V)) { 1623da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer if (isa<BitCastInst>(I) || isa<GetElementPtrInst>(I)) 163ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner return getUnderlyingObject(I->getOperand(0)); 164ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { 1653da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer if (CE->getOpcode() == Instruction::BitCast || 166ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner CE->getOpcode() == Instruction::GetElementPtr) 167ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner return getUnderlyingObject(CE->getOperand(0)); 168ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner } 169ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner 170ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // Othewise, we don't know what this is, return it as the base pointer. 171ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner return V; 172ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner} 1733b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 174ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// AnalyzeGlobals - Scan through the users of all of the internal 1753b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// GlobalValue's in the program. If none of them have their "Address taken" 1763b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// (really, their address passed to something nontrivial), record this fact, 1773b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// and record the functions that they are used directly in. 1783b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnervoid GlobalsModRef::AnalyzeGlobals(Module &M) { 1793b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner std::vector<Function*> Readers, Writers; 1803b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 1813b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (I->hasInternalLinkage()) { 182ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (!AnalyzeUsesOfPointer(I, Readers, Writers)) { 183fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner // Remember that we are tracking this global. 184fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner NonAddressTakenGlobals.insert(I); 1853b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner ++NumNonAddrTakenFunctions; 1863b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } 1873b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner Readers.clear(); Writers.clear(); 1883b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } 1893b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 190f5eaf3c62c7b2f2da1f85ac411c4f13bcbf131f1Chris Lattner for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 191f5eaf3c62c7b2f2da1f85ac411c4f13bcbf131f1Chris Lattner I != E; ++I) 1923b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (I->hasInternalLinkage()) { 193ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (!AnalyzeUsesOfPointer(I, Readers, Writers)) { 1943b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner // Remember that we are tracking this global, and the mod/ref fns 195fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner NonAddressTakenGlobals.insert(I); 196fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner for (unsigned i = 0, e = Readers.size(); i != e; ++i) 197fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner FunctionInfo[Readers[i]].GlobalInfo[I] |= Ref; 198fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner 199fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (!I->isConstant()) // No need to keep track of writers to constants 200fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner for (unsigned i = 0, e = Writers.size(); i != e; ++i) 201fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner FunctionInfo[Writers[i]].GlobalInfo[I] |= Mod; 2023b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner ++NumNonAddrTakenGlobalVars; 203ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner 204ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // If this global holds a pointer type, see if it is an indirect global. 205ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (isa<PointerType>(I->getType()->getElementType()) && 206ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner AnalyzeIndirectGlobalMemory(I)) 207ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner ++NumIndirectGlobalVars; 2083b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } 2093b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner Readers.clear(); Writers.clear(); 2103b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } 2113b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner} 2123b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 213ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// AnalyzeUsesOfPointer - Look at all of the users of the specified pointer. 214ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// If this is used by anything complex (i.e., the address escapes), return 215ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// true. Also, while we are at it, keep track of those functions that read and 216ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// write to the value. 217ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// 218ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// If OkayStoreDest is non-null, stores into this global are allowed. 219ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattnerbool GlobalsModRef::AnalyzeUsesOfPointer(Value *V, 220ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner std::vector<Function*> &Readers, 221ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner std::vector<Function*> &Writers, 222ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner GlobalValue *OkayStoreDest) { 223fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (!isa<PointerType>(V->getType())) return true; 2243b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 2253b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI) 2263b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) { 2273b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner Readers.push_back(LI->getParent()->getParent()); 2283b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { 229ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (V == SI->getOperand(1)) { 230ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner Writers.push_back(SI->getParent()->getParent()); 231ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner } else if (SI->getOperand(1) != OkayStoreDest) { 232ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner return true; // Storing the pointer 233ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner } 2343b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) { 235ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (AnalyzeUsesOfPointer(GEP, Readers, Writers)) return true; 2363b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } else if (CallInst *CI = dyn_cast<CallInst>(*UI)) { 2373b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner // Make sure that this is just the function being called, not that it is 2383b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner // passing into the function. 2393b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i) 2403b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (CI->getOperand(i) == V) return true; 2413b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) { 2423b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner // Make sure that this is just the function being called, not that it is 2433b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner // passing into the function. 2443b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner for (unsigned i = 3, e = II->getNumOperands(); i != e; ++i) 2453b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (II->getOperand(i) == V) return true; 2463b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(*UI)) { 2473da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer if (CE->getOpcode() == Instruction::GetElementPtr || 2483da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer CE->getOpcode() == Instruction::BitCast) { 249ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (AnalyzeUsesOfPointer(CE, Readers, Writers)) 2503b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner return true; 2513b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } else { 2523b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner return true; 2532b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman } 254ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner } else if (SetCondInst *SCI = dyn_cast<SetCondInst>(*UI)) { 255ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (!isa<ConstantPointerNull>(SCI->getOperand(1))) 256ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner return true; // Allow comparison against null. 257ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner } else if (FreeInst *F = dyn_cast<FreeInst>(*UI)) { 258ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner Writers.push_back(F->getParent()->getParent()); 2593b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } else { 2603b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner return true; 2613b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } 2623b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner return false; 2633b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner} 2643b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 265ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// AnalyzeIndirectGlobalMemory - We found an non-address-taken global variable 266ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// which holds a pointer type. See if the global always points to non-aliased 267ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// heap memory: that is, all initializers of the globals are allocations, and 268ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// those allocations have no use other than initialization of the global. 269ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// Further, all loads out of GV must directly use the memory, not store the 270ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// pointer somewhere. If this is true, we consider the memory pointed to by 271ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// GV to be owned by GV and can disambiguate other pointers from it. 272ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattnerbool GlobalsModRef::AnalyzeIndirectGlobalMemory(GlobalValue *GV) { 273ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // Keep track of values related to the allocation of the memory, f.e. the 274ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // value produced by the malloc call and any casts. 275ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner std::vector<Value*> AllocRelatedValues; 276ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner 277ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // Walk the user list of the global. If we find anything other than a direct 278ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // load or store, bail out. 279ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner for (Value::use_iterator I = GV->use_begin(), E = GV->use_end(); I != E; ++I){ 280ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (LoadInst *LI = dyn_cast<LoadInst>(*I)) { 281ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // The pointer loaded from the global can only be used in simple ways: 282ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // we allow addressing of it and loading storing to it. We do *not* allow 283ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // storing the loaded pointer somewhere else or passing to a function. 284ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner std::vector<Function*> ReadersWriters; 285ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (AnalyzeUsesOfPointer(LI, ReadersWriters, ReadersWriters)) 286ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner return false; // Loaded pointer escapes. 287ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // TODO: Could try some IP mod/ref of the loaded pointer. 288ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner } else if (StoreInst *SI = dyn_cast<StoreInst>(*I)) { 289ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // Storing the global itself. 290ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (SI->getOperand(0) == GV) return false; 291ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner 292ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // If storing the null pointer, ignore it. 293ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (isa<ConstantPointerNull>(SI->getOperand(0))) 294ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner continue; 295ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner 296ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // Check the value being stored. 297ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner Value *Ptr = getUnderlyingObject(SI->getOperand(0)); 298ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner 299ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (isa<MallocInst>(Ptr)) { 300ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // Okay, easy case. 301ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner } else if (CallInst *CI = dyn_cast<CallInst>(Ptr)) { 302ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner Function *F = CI->getCalledFunction(); 303ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (!F || !F->isExternal()) return false; // Too hard to analyze. 304ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (F->getName() != "calloc") return false; // Not calloc. 305ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner } else { 306ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner return false; // Too hard to analyze. 307ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner } 308ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner 309ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // Analyze all uses of the allocation. If any of them are used in a 310ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // non-simple way (e.g. stored to another global) bail out. 311ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner std::vector<Function*> ReadersWriters; 312ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (AnalyzeUsesOfPointer(Ptr, ReadersWriters, ReadersWriters, GV)) 313ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner return false; // Loaded pointer escapes. 314ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner 315ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // Remember that this allocation is related to the indirect global. 316ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner AllocRelatedValues.push_back(Ptr); 317ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner } else { 318ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // Something complex, bail out. 319ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner return false; 320ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner } 321ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner } 322ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner 323ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // Okay, this is an indirect global. Remember all of the allocations for 324ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // this global in AllocsForIndirectGlobals. 325ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner while (!AllocRelatedValues.empty()) { 326ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV; 327ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner AllocRelatedValues.pop_back(); 328ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner } 329ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner IndirectGlobals.insert(GV); 330ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner return true; 331ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner} 332ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner 3333b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// AnalyzeCallGraph - At this point, we know the functions where globals are 3343b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// immediately stored to and read from. Propagate this information up the call 335fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner/// graph to all callers and compute the mod/ref info for all memory for each 3362b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman/// function. 3373b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnervoid GlobalsModRef::AnalyzeCallGraph(CallGraph &CG, Module &M) { 3383b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner // We do a bottom-up SCC traversal of the call graph. In other words, we 3393b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner // visit all callees before callers (leaf-first). 340fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner for (scc_iterator<CallGraph*> I = scc_begin(&CG), E = scc_end(&CG); I!=E; ++I) 341105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner if ((*I).size() != 1) { 342fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner AnalyzeSCC(*I); 343105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner } else if (Function *F = (*I)[0]->getFunction()) { 344105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner if (!F->isExternal()) { 345105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner // Nonexternal function. 346105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner AnalyzeSCC(*I); 347105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner } else { 348105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner // Otherwise external function. Handle intrinsics and other special 349105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner // cases here. 350105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner if (getAnalysis<AliasAnalysis>().doesNotAccessMemory(F)) 351105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner // If it does not access memory, process the function, causing us to 352105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner // realize it doesn't do anything (the body is empty). 353105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner AnalyzeSCC(*I); 354105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner else { 355105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner // Otherwise, don't process it. This will cause us to conservatively 356105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner // assume the worst. 357105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner } 358105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner } 359105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner } else { 360105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner // Do not process the external node, assume the worst. 361105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner } 362fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner} 363fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner 364fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattnervoid GlobalsModRef::AnalyzeSCC(std::vector<CallGraphNode *> &SCC) { 365fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner assert(!SCC.empty() && "SCC with no functions?"); 366fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner FunctionRecord &FR = FunctionInfo[SCC[0]->getFunction()]; 367fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner 368fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner bool CallsExternal = false; 369fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner unsigned FunctionEffect = 0; 370fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner 371fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner // Collect the mod/ref properties due to called functions. We only compute 372fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner // one mod-ref set 373fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner for (unsigned i = 0, e = SCC.size(); i != e && !CallsExternal; ++i) 374fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end(); 375fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner CI != E; ++CI) 376d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner if (Function *Callee = CI->second->getFunction()) { 377fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (FunctionRecord *CalleeFR = getFunctionInfo(Callee)) { 378fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner // Propagate function effect up. 379fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner FunctionEffect |= CalleeFR->FunctionEffect; 380fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner 381fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner // Incorporate callee's effects on globals into our info. 382fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner for (std::map<GlobalValue*, unsigned>::iterator GI = 383fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner CalleeFR->GlobalInfo.begin(), E = CalleeFR->GlobalInfo.end(); 384fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner GI != E; ++GI) 385fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner FR.GlobalInfo[GI->first] |= GI->second; 3863b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 3873b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } else { 388ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner // Okay, if we can't say anything about it, maybe some other alias 389ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner // analysis can. 390ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner ModRefBehavior MRB = 391ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner AliasAnalysis::getModRefBehavior(Callee, CallSite()); 392ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner if (MRB != DoesNotAccessMemory) { 39362da315c859eb20e0ad38c034f71901ac6558d4dChris Lattner // FIXME: could make this more aggressive for functions that just 39462da315c859eb20e0ad38c034f71901ac6558d4dChris Lattner // read memory. We should just say they read all globals. 39562da315c859eb20e0ad38c034f71901ac6558d4dChris Lattner CallsExternal = true; 39662da315c859eb20e0ad38c034f71901ac6558d4dChris Lattner break; 397ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner } 3983b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } 399fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner } else { 400fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner CallsExternal = true; 401fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner break; 4023b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } 403fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner 404fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner // If this SCC calls an external function, we can't say anything about it, so 405fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner // remove all SCC functions from the FunctionInfo map. 406fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (CallsExternal) { 4073b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner for (unsigned i = 0, e = SCC.size(); i != e; ++i) 408fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner FunctionInfo.erase(SCC[i]->getFunction()); 409fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner return; 4103b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner } 4112b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman 412fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner // Otherwise, unless we already know that this function mod/refs memory, scan 413fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner // the function bodies to see if there are any explicit loads or stores. 414fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (FunctionEffect != ModRef) { 415fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner for (unsigned i = 0, e = SCC.size(); i != e && FunctionEffect != ModRef;++i) 416fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner for (inst_iterator II = inst_begin(SCC[i]->getFunction()), 4172b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman E = inst_end(SCC[i]->getFunction()); 418fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner II != E && FunctionEffect != ModRef; ++II) 419fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (isa<LoadInst>(*II)) 420fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner FunctionEffect |= Ref; 421fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner else if (isa<StoreInst>(*II)) 422fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner FunctionEffect |= Mod; 4233fe4d3cb5bd5ea7948faeed8451b61380d928808Chris Lattner else if (isa<MallocInst>(*II) || isa<FreeInst>(*II)) 4243fe4d3cb5bd5ea7948faeed8451b61380d928808Chris Lattner FunctionEffect |= ModRef; 425fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner } 426fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner 427fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if ((FunctionEffect & Mod) == 0) 428fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner ++NumReadMemFunctions; 429fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (FunctionEffect == 0) 430fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner ++NumNoMemFunctions; 431fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner FR.FunctionEffect = FunctionEffect; 432fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner 433fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner // Finally, now that we know the full effect on this SCC, clone the 434fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner // information to each function in the SCC. 435fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner for (unsigned i = 1, e = SCC.size(); i != e; ++i) 436fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner FunctionInfo[SCC[i]->getFunction()] = FR; 4373b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner} 4383b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 4393b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 4403b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 4413b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// alias - If one of the pointers is to a global that we are tracking, and the 4423b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// other is some random pointer, we know there cannot be an alias, because the 4433b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// address of the global isn't taken. 4443b04a8ac455568f9b98ab7e9a43076260cb737aeChris LattnerAliasAnalysis::AliasResult 4453b04a8ac455568f9b98ab7e9a43076260cb737aeChris LattnerGlobalsModRef::alias(const Value *V1, unsigned V1Size, 4463b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner const Value *V2, unsigned V2Size) { 447ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // Get the base object these pointers point to. 448ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner Value *UV1 = getUnderlyingObject(const_cast<Value*>(V1)); 449ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner Value *UV2 = getUnderlyingObject(const_cast<Value*>(V2)); 450ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner 451ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // If either of the underlying values is a global, they may be non-addr-taken 452ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // globals, which we can answer queries about. 453ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner GlobalValue *GV1 = dyn_cast<GlobalValue>(UV1); 454ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner GlobalValue *GV2 = dyn_cast<GlobalValue>(UV2); 455ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (GV1 || GV2) { 456ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // If the global's address is taken, pretend we don't know it's a pointer to 457ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // the global. 458ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (GV1 && !NonAddressTakenGlobals.count(GV1)) GV1 = 0; 459ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (GV2 && !NonAddressTakenGlobals.count(GV2)) GV2 = 0; 460ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner 461ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // If the the two pointers are derived from two different non-addr-taken 462ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // globals, or if one is and the other isn't, we know these can't alias. 463ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if ((GV1 || GV2) && GV1 != GV2) 464ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner return NoAlias; 465ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner 466ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // Otherwise if they are both derived from the same addr-taken global, we 467ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // can't know the two accesses don't overlap. 468ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner } 469ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner 470ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // These pointers may be based on the memory owned by an indirect global. If 471ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // so, we may be able to handle this. First check to see if the base pointer 472ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // is a direct load from an indirect global. 473ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner GV1 = GV2 = 0; 474ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (LoadInst *LI = dyn_cast<LoadInst>(UV1)) 475ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0))) 476ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (IndirectGlobals.count(GV)) 477ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner GV1 = GV; 478ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (LoadInst *LI = dyn_cast<LoadInst>(UV2)) 479ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0))) 480ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (IndirectGlobals.count(GV)) 481ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner GV2 = GV; 482ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner 483ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // These pointers may also be from an allocation for the indirect global. If 484ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // so, also handle them. 485ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (AllocsForIndirectGlobals.count(UV1)) 486ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner GV1 = AllocsForIndirectGlobals[UV1]; 487ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (AllocsForIndirectGlobals.count(UV2)) 488ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner GV2 = AllocsForIndirectGlobals[UV2]; 489ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner 490ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // Now that we know whether the two pointers are related to indirect globals, 491ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // use this to disambiguate the pointers. If either pointer is based on an 492ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // indirect global and if they are not both based on the same indirect global, 493ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // they cannot alias. 4943b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if ((GV1 || GV2) && GV1 != GV2) 4953b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner return NoAlias; 496ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner 4973b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner return AliasAnalysis::alias(V1, V1Size, V2, V2Size); 4983b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner} 4993b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 5003b04a8ac455568f9b98ab7e9a43076260cb737aeChris LattnerAliasAnalysis::ModRefResult 5013b04a8ac455568f9b98ab7e9a43076260cb737aeChris LattnerGlobalsModRef::getModRefInfo(CallSite CS, Value *P, unsigned Size) { 5023b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner unsigned Known = ModRef; 5033b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 5043b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner // If we are asking for mod/ref info of a direct call with a pointer to a 505fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner // global we are tracking, return information if we have it. 506ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (GlobalValue *GV = dyn_cast<GlobalValue>(getUnderlyingObject(P))) 5073b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (GV->hasInternalLinkage()) 508fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (Function *F = CS.getCalledFunction()) 509fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (NonAddressTakenGlobals.count(GV)) 510fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner if (FunctionRecord *FR = getFunctionInfo(F)) 511fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner Known = FR->getInfoForGlobal(GV); 5123b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 5133b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner if (Known == NoModRef) 5143b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner return NoModRef; // No need to query other mod/ref analyses 5153b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner return ModRefResult(Known & AliasAnalysis::getModRefInfo(CS, P, Size)); 5163b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner} 5173b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 5183b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 5193b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner//===----------------------------------------------------------------------===// 5203b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner// Methods to update the analysis as a result of the client transformation. 5213b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner// 5223b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnervoid GlobalsModRef::deleteValue(Value *V) { 523ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 524ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (NonAddressTakenGlobals.erase(GV)) { 525ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // This global might be an indirect global. If so, remove it and remove 526ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // any AllocRelatedValues for it. 527ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (IndirectGlobals.erase(GV)) { 528ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // Remove any entries in AllocsForIndirectGlobals for this global. 529ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner for (std::map<Value*, GlobalValue*>::iterator 530ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner I = AllocsForIndirectGlobals.begin(), 531ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner E = AllocsForIndirectGlobals.end(); I != E; ) { 532ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner if (I->second == GV) { 533ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner AllocsForIndirectGlobals.erase(I++); 534ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner } else { 535ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner ++I; 536ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner } 537ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner } 538ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner } 539ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner } 540ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner } 541ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner 542ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // Otherwise, if this is an allocation related to an indirect global, remove 543ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner // it. 544ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner AllocsForIndirectGlobals.erase(V); 5453b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner} 5463b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner 5473b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnervoid GlobalsModRef::copyValue(Value *From, Value *To) { 5483b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner} 549