1193323Sed//===- GlobalsModRef.cpp - Simple Mod/Ref Analysis for Globals ------------===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This simple pass provides alias and mod/ref information for global values 11193323Sed// that do not have their address taken, and keeps track of whether functions 12193323Sed// read or write memory (are "pure"). For this simple (but very common) case, 13193323Sed// we can provide pretty accurate and useful information. 14193323Sed// 15193323Sed//===----------------------------------------------------------------------===// 16193323Sed 17193323Sed#define DEBUG_TYPE "globalsmodref-aa" 18193323Sed#include "llvm/Analysis/Passes.h" 19249423Sdim#include "llvm/ADT/SCCIterator.h" 20249423Sdim#include "llvm/ADT/Statistic.h" 21193323Sed#include "llvm/Analysis/AliasAnalysis.h" 22193323Sed#include "llvm/Analysis/CallGraph.h" 23198892Srdivacky#include "llvm/Analysis/MemoryBuiltins.h" 24218893Sdim#include "llvm/Analysis/ValueTracking.h" 25249423Sdim#include "llvm/IR/Constants.h" 26249423Sdim#include "llvm/IR/DerivedTypes.h" 27249423Sdim#include "llvm/IR/Instructions.h" 28249423Sdim#include "llvm/IR/IntrinsicInst.h" 29249423Sdim#include "llvm/IR/Module.h" 30249423Sdim#include "llvm/Pass.h" 31193323Sed#include "llvm/Support/CommandLine.h" 32193323Sed#include "llvm/Support/InstIterator.h" 33193323Sed#include <set> 34193323Sedusing namespace llvm; 35193323Sed 36193323SedSTATISTIC(NumNonAddrTakenGlobalVars, 37193323Sed "Number of global vars without address taken"); 38193323SedSTATISTIC(NumNonAddrTakenFunctions,"Number of functions without address taken"); 39193323SedSTATISTIC(NumNoMemFunctions, "Number of functions that do not access memory"); 40193323SedSTATISTIC(NumReadMemFunctions, "Number of functions that only read memory"); 41193323SedSTATISTIC(NumIndirectGlobalVars, "Number of indirect global objects"); 42193323Sed 43193323Sednamespace { 44193323Sed /// FunctionRecord - One instance of this structure is stored for every 45193323Sed /// function in the program. Later, the entries for these functions are 46193323Sed /// removed if the function is found to call an external function (in which 47193323Sed /// case we know nothing about it. 48198892Srdivacky struct FunctionRecord { 49193323Sed /// GlobalInfo - Maintain mod/ref info for all of the globals without 50193323Sed /// addresses taken that are read or written (transitively) by this 51193323Sed /// function. 52212904Sdim std::map<const GlobalValue*, unsigned> GlobalInfo; 53193323Sed 54193323Sed /// MayReadAnyGlobal - May read global variables, but it is not known which. 55193323Sed bool MayReadAnyGlobal; 56193323Sed 57212904Sdim unsigned getInfoForGlobal(const GlobalValue *GV) const { 58193323Sed unsigned Effect = MayReadAnyGlobal ? AliasAnalysis::Ref : 0; 59212904Sdim std::map<const GlobalValue*, unsigned>::const_iterator I = 60212904Sdim GlobalInfo.find(GV); 61193323Sed if (I != GlobalInfo.end()) 62193323Sed Effect |= I->second; 63193323Sed return Effect; 64193323Sed } 65193323Sed 66193323Sed /// FunctionEffect - Capture whether or not this function reads or writes to 67193323Sed /// ANY memory. If not, we can do a lot of aggressive analysis on it. 68193323Sed unsigned FunctionEffect; 69193323Sed 70193323Sed FunctionRecord() : MayReadAnyGlobal (false), FunctionEffect(0) {} 71193323Sed }; 72193323Sed 73193323Sed /// GlobalsModRef - The actual analysis pass. 74198892Srdivacky class GlobalsModRef : public ModulePass, public AliasAnalysis { 75193323Sed /// NonAddressTakenGlobals - The globals that do not have their addresses 76193323Sed /// taken. 77212904Sdim std::set<const GlobalValue*> NonAddressTakenGlobals; 78193323Sed 79193323Sed /// IndirectGlobals - The memory pointed to by this global is known to be 80193323Sed /// 'owned' by the global. 81212904Sdim std::set<const GlobalValue*> IndirectGlobals; 82193323Sed 83193323Sed /// AllocsForIndirectGlobals - If an instruction allocates memory for an 84193323Sed /// indirect global, this map indicates which one. 85212904Sdim std::map<const Value*, const GlobalValue*> AllocsForIndirectGlobals; 86193323Sed 87193323Sed /// FunctionInfo - For each function, keep track of what globals are 88193323Sed /// modified or read. 89212904Sdim std::map<const Function*, FunctionRecord> FunctionInfo; 90193323Sed 91193323Sed public: 92193323Sed static char ID; 93218893Sdim GlobalsModRef() : ModulePass(ID) { 94218893Sdim initializeGlobalsModRefPass(*PassRegistry::getPassRegistry()); 95218893Sdim } 96193323Sed 97193323Sed bool runOnModule(Module &M) { 98193323Sed InitializeAliasAnalysis(this); // set up super class 99193323Sed AnalyzeGlobals(M); // find non-addr taken globals 100193323Sed AnalyzeCallGraph(getAnalysis<CallGraph>(), M); // Propagate on CG 101193323Sed return false; 102193323Sed } 103193323Sed 104193323Sed virtual void getAnalysisUsage(AnalysisUsage &AU) const { 105193323Sed AliasAnalysis::getAnalysisUsage(AU); 106193323Sed AU.addRequired<CallGraph>(); 107193323Sed AU.setPreservesAll(); // Does not transform code 108193323Sed } 109193323Sed 110193323Sed //------------------------------------------------ 111193323Sed // Implement the AliasAnalysis API 112193323Sed // 113218893Sdim AliasResult alias(const Location &LocA, const Location &LocB); 114212904Sdim ModRefResult getModRefInfo(ImmutableCallSite CS, 115218893Sdim const Location &Loc); 116212904Sdim ModRefResult getModRefInfo(ImmutableCallSite CS1, 117212904Sdim ImmutableCallSite CS2) { 118212904Sdim return AliasAnalysis::getModRefInfo(CS1, CS2); 119193323Sed } 120193323Sed 121193323Sed /// getModRefBehavior - Return the behavior of the specified function if 122193323Sed /// called from the specified call site. The call site may be null in which 123193323Sed /// case the most generic behavior of this function should be returned. 124212904Sdim ModRefBehavior getModRefBehavior(const Function *F) { 125218893Sdim ModRefBehavior Min = UnknownModRefBehavior; 126218893Sdim 127193323Sed if (FunctionRecord *FR = getFunctionInfo(F)) { 128193323Sed if (FR->FunctionEffect == 0) 129218893Sdim Min = DoesNotAccessMemory; 130193323Sed else if ((FR->FunctionEffect & Mod) == 0) 131218893Sdim Min = OnlyReadsMemory; 132193323Sed } 133218893Sdim 134218893Sdim return ModRefBehavior(AliasAnalysis::getModRefBehavior(F) & Min); 135193323Sed } 136193323Sed 137193323Sed /// getModRefBehavior - Return the behavior of the specified function if 138193323Sed /// called from the specified call site. The call site may be null in which 139193323Sed /// case the most generic behavior of this function should be returned. 140212904Sdim ModRefBehavior getModRefBehavior(ImmutableCallSite CS) { 141218893Sdim ModRefBehavior Min = UnknownModRefBehavior; 142218893Sdim 143218893Sdim if (const Function* F = CS.getCalledFunction()) 144218893Sdim if (FunctionRecord *FR = getFunctionInfo(F)) { 145218893Sdim if (FR->FunctionEffect == 0) 146218893Sdim Min = DoesNotAccessMemory; 147218893Sdim else if ((FR->FunctionEffect & Mod) == 0) 148218893Sdim Min = OnlyReadsMemory; 149218893Sdim } 150218893Sdim 151218893Sdim return ModRefBehavior(AliasAnalysis::getModRefBehavior(CS) & Min); 152193323Sed } 153193323Sed 154193323Sed virtual void deleteValue(Value *V); 155193323Sed virtual void copyValue(Value *From, Value *To); 156218893Sdim virtual void addEscapingUse(Use &U); 157193323Sed 158202878Srdivacky /// getAdjustedAnalysisPointer - This method is used when a pass implements 159202878Srdivacky /// an analysis interface through multiple inheritance. If needed, it 160202878Srdivacky /// should override this to adjust the this pointer as needed for the 161202878Srdivacky /// specified pass info. 162212904Sdim virtual void *getAdjustedAnalysisPointer(AnalysisID PI) { 163212904Sdim if (PI == &AliasAnalysis::ID) 164202878Srdivacky return (AliasAnalysis*)this; 165202878Srdivacky return this; 166202878Srdivacky } 167202878Srdivacky 168193323Sed private: 169193323Sed /// getFunctionInfo - Return the function info for the function, or null if 170193323Sed /// we don't have anything useful to say about it. 171212904Sdim FunctionRecord *getFunctionInfo(const Function *F) { 172212904Sdim std::map<const Function*, FunctionRecord>::iterator I = 173212904Sdim FunctionInfo.find(F); 174193323Sed if (I != FunctionInfo.end()) 175193323Sed return &I->second; 176193323Sed return 0; 177193323Sed } 178193323Sed 179193323Sed void AnalyzeGlobals(Module &M); 180193323Sed void AnalyzeCallGraph(CallGraph &CG, Module &M); 181193323Sed bool AnalyzeUsesOfPointer(Value *V, std::vector<Function*> &Readers, 182193323Sed std::vector<Function*> &Writers, 183193323Sed GlobalValue *OkayStoreDest = 0); 184193323Sed bool AnalyzeIndirectGlobalMemory(GlobalValue *GV); 185193323Sed }; 186193323Sed} 187193323Sed 188193323Sedchar GlobalsModRef::ID = 0; 189218893SdimINITIALIZE_AG_PASS_BEGIN(GlobalsModRef, AliasAnalysis, 190212904Sdim "globalsmodref-aa", "Simple mod/ref analysis for globals", 191218893Sdim false, true, false) 192263508SdimINITIALIZE_PASS_DEPENDENCY(CallGraph) 193218893SdimINITIALIZE_AG_PASS_END(GlobalsModRef, AliasAnalysis, 194218893Sdim "globalsmodref-aa", "Simple mod/ref analysis for globals", 195218893Sdim false, true, false) 196193323Sed 197193323SedPass *llvm::createGlobalsModRefPass() { return new GlobalsModRef(); } 198193323Sed 199193323Sed/// AnalyzeGlobals - Scan through the users of all of the internal 200193323Sed/// GlobalValue's in the program. If none of them have their "address taken" 201193323Sed/// (really, their address passed to something nontrivial), record this fact, 202193323Sed/// and record the functions that they are used directly in. 203193323Sedvoid GlobalsModRef::AnalyzeGlobals(Module &M) { 204193323Sed std::vector<Function*> Readers, Writers; 205193323Sed for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 206193323Sed if (I->hasLocalLinkage()) { 207193323Sed if (!AnalyzeUsesOfPointer(I, Readers, Writers)) { 208193323Sed // Remember that we are tracking this global. 209193323Sed NonAddressTakenGlobals.insert(I); 210193323Sed ++NumNonAddrTakenFunctions; 211193323Sed } 212193323Sed Readers.clear(); Writers.clear(); 213193323Sed } 214193323Sed 215193323Sed for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 216193323Sed I != E; ++I) 217193323Sed if (I->hasLocalLinkage()) { 218193323Sed if (!AnalyzeUsesOfPointer(I, Readers, Writers)) { 219193323Sed // Remember that we are tracking this global, and the mod/ref fns 220193323Sed NonAddressTakenGlobals.insert(I); 221193323Sed 222193323Sed for (unsigned i = 0, e = Readers.size(); i != e; ++i) 223193323Sed FunctionInfo[Readers[i]].GlobalInfo[I] |= Ref; 224193323Sed 225193323Sed if (!I->isConstant()) // No need to keep track of writers to constants 226193323Sed for (unsigned i = 0, e = Writers.size(); i != e; ++i) 227193323Sed FunctionInfo[Writers[i]].GlobalInfo[I] |= Mod; 228193323Sed ++NumNonAddrTakenGlobalVars; 229193323Sed 230193323Sed // If this global holds a pointer type, see if it is an indirect global. 231204642Srdivacky if (I->getType()->getElementType()->isPointerTy() && 232193323Sed AnalyzeIndirectGlobalMemory(I)) 233193323Sed ++NumIndirectGlobalVars; 234193323Sed } 235193323Sed Readers.clear(); Writers.clear(); 236193323Sed } 237193323Sed} 238193323Sed 239193323Sed/// AnalyzeUsesOfPointer - Look at all of the users of the specified pointer. 240193323Sed/// If this is used by anything complex (i.e., the address escapes), return 241193323Sed/// true. Also, while we are at it, keep track of those functions that read and 242193323Sed/// write to the value. 243193323Sed/// 244193323Sed/// If OkayStoreDest is non-null, stores into this global are allowed. 245193323Sedbool GlobalsModRef::AnalyzeUsesOfPointer(Value *V, 246193323Sed std::vector<Function*> &Readers, 247193323Sed std::vector<Function*> &Writers, 248193323Sed GlobalValue *OkayStoreDest) { 249204642Srdivacky if (!V->getType()->isPointerTy()) return true; 250193323Sed 251210299Sed for (Value::use_iterator UI = V->use_begin(), E=V->use_end(); UI != E; ++UI) { 252210299Sed User *U = *UI; 253210299Sed if (LoadInst *LI = dyn_cast<LoadInst>(U)) { 254193323Sed Readers.push_back(LI->getParent()->getParent()); 255210299Sed } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) { 256193323Sed if (V == SI->getOperand(1)) { 257193323Sed Writers.push_back(SI->getParent()->getParent()); 258193323Sed } else if (SI->getOperand(1) != OkayStoreDest) { 259193323Sed return true; // Storing the pointer 260193323Sed } 261210299Sed } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) { 262193323Sed if (AnalyzeUsesOfPointer(GEP, Readers, Writers)) return true; 263210299Sed } else if (BitCastInst *BCI = dyn_cast<BitCastInst>(U)) { 264198090Srdivacky if (AnalyzeUsesOfPointer(BCI, Readers, Writers, OkayStoreDest)) 265198090Srdivacky return true; 266243830Sdim } else if (isFreeCall(U, TLI)) { 267210299Sed Writers.push_back(cast<Instruction>(U)->getParent()->getParent()); 268210299Sed } else if (CallInst *CI = dyn_cast<CallInst>(U)) { 269193323Sed // Make sure that this is just the function being called, not that it is 270193323Sed // passing into the function. 271210299Sed for (unsigned i = 0, e = CI->getNumArgOperands(); i != e; ++i) 272210299Sed if (CI->getArgOperand(i) == V) return true; 273210299Sed } else if (InvokeInst *II = dyn_cast<InvokeInst>(U)) { 274193323Sed // Make sure that this is just the function being called, not that it is 275193323Sed // passing into the function. 276210299Sed for (unsigned i = 0, e = II->getNumArgOperands(); i != e; ++i) 277210299Sed if (II->getArgOperand(i) == V) return true; 278210299Sed } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) { 279193323Sed if (CE->getOpcode() == Instruction::GetElementPtr || 280193323Sed CE->getOpcode() == Instruction::BitCast) { 281193323Sed if (AnalyzeUsesOfPointer(CE, Readers, Writers)) 282193323Sed return true; 283193323Sed } else { 284193323Sed return true; 285193323Sed } 286210299Sed } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(U)) { 287193323Sed if (!isa<ConstantPointerNull>(ICI->getOperand(1))) 288193323Sed return true; // Allow comparison against null. 289193323Sed } else { 290193323Sed return true; 291193323Sed } 292210299Sed } 293210299Sed 294193323Sed return false; 295193323Sed} 296193323Sed 297193323Sed/// AnalyzeIndirectGlobalMemory - We found an non-address-taken global variable 298193323Sed/// which holds a pointer type. See if the global always points to non-aliased 299193323Sed/// heap memory: that is, all initializers of the globals are allocations, and 300193323Sed/// those allocations have no use other than initialization of the global. 301193323Sed/// Further, all loads out of GV must directly use the memory, not store the 302193323Sed/// pointer somewhere. If this is true, we consider the memory pointed to by 303193323Sed/// GV to be owned by GV and can disambiguate other pointers from it. 304193323Sedbool GlobalsModRef::AnalyzeIndirectGlobalMemory(GlobalValue *GV) { 305193323Sed // Keep track of values related to the allocation of the memory, f.e. the 306193323Sed // value produced by the malloc call and any casts. 307193323Sed std::vector<Value*> AllocRelatedValues; 308193323Sed 309193323Sed // Walk the user list of the global. If we find anything other than a direct 310193323Sed // load or store, bail out. 311193323Sed for (Value::use_iterator I = GV->use_begin(), E = GV->use_end(); I != E; ++I){ 312210299Sed User *U = *I; 313210299Sed if (LoadInst *LI = dyn_cast<LoadInst>(U)) { 314193323Sed // The pointer loaded from the global can only be used in simple ways: 315193323Sed // we allow addressing of it and loading storing to it. We do *not* allow 316193323Sed // storing the loaded pointer somewhere else or passing to a function. 317193323Sed std::vector<Function*> ReadersWriters; 318193323Sed if (AnalyzeUsesOfPointer(LI, ReadersWriters, ReadersWriters)) 319193323Sed return false; // Loaded pointer escapes. 320193323Sed // TODO: Could try some IP mod/ref of the loaded pointer. 321210299Sed } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) { 322193323Sed // Storing the global itself. 323193323Sed if (SI->getOperand(0) == GV) return false; 324193323Sed 325193323Sed // If storing the null pointer, ignore it. 326193323Sed if (isa<ConstantPointerNull>(SI->getOperand(0))) 327193323Sed continue; 328193323Sed 329193323Sed // Check the value being stored. 330218893Sdim Value *Ptr = GetUnderlyingObject(SI->getOperand(0)); 331193323Sed 332243830Sdim if (!isAllocLikeFn(Ptr, TLI)) 333193323Sed return false; // Too hard to analyze. 334193323Sed 335193323Sed // Analyze all uses of the allocation. If any of them are used in a 336193323Sed // non-simple way (e.g. stored to another global) bail out. 337193323Sed std::vector<Function*> ReadersWriters; 338193323Sed if (AnalyzeUsesOfPointer(Ptr, ReadersWriters, ReadersWriters, GV)) 339193323Sed return false; // Loaded pointer escapes. 340193323Sed 341193323Sed // Remember that this allocation is related to the indirect global. 342193323Sed AllocRelatedValues.push_back(Ptr); 343193323Sed } else { 344193323Sed // Something complex, bail out. 345193323Sed return false; 346193323Sed } 347193323Sed } 348193323Sed 349193323Sed // Okay, this is an indirect global. Remember all of the allocations for 350193323Sed // this global in AllocsForIndirectGlobals. 351193323Sed while (!AllocRelatedValues.empty()) { 352193323Sed AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV; 353193323Sed AllocRelatedValues.pop_back(); 354193323Sed } 355193323Sed IndirectGlobals.insert(GV); 356193323Sed return true; 357193323Sed} 358193323Sed 359193323Sed/// AnalyzeCallGraph - At this point, we know the functions where globals are 360193323Sed/// immediately stored to and read from. Propagate this information up the call 361193323Sed/// graph to all callers and compute the mod/ref info for all memory for each 362193323Sed/// function. 363193323Sedvoid GlobalsModRef::AnalyzeCallGraph(CallGraph &CG, Module &M) { 364193323Sed // We do a bottom-up SCC traversal of the call graph. In other words, we 365193323Sed // visit all callees before callers (leaf-first). 366193323Sed for (scc_iterator<CallGraph*> I = scc_begin(&CG), E = scc_end(&CG); I != E; 367193323Sed ++I) { 368193323Sed std::vector<CallGraphNode *> &SCC = *I; 369193323Sed assert(!SCC.empty() && "SCC with no functions?"); 370193323Sed 371193323Sed if (!SCC[0]->getFunction()) { 372193323Sed // Calls externally - can't say anything useful. Remove any existing 373193323Sed // function records (may have been created when scanning globals). 374193323Sed for (unsigned i = 0, e = SCC.size(); i != e; ++i) 375193323Sed FunctionInfo.erase(SCC[i]->getFunction()); 376193323Sed continue; 377193323Sed } 378193323Sed 379193323Sed FunctionRecord &FR = FunctionInfo[SCC[0]->getFunction()]; 380193323Sed 381193323Sed bool KnowNothing = false; 382193323Sed unsigned FunctionEffect = 0; 383193323Sed 384193323Sed // Collect the mod/ref properties due to called functions. We only compute 385193323Sed // one mod-ref set. 386193323Sed for (unsigned i = 0, e = SCC.size(); i != e && !KnowNothing; ++i) { 387193323Sed Function *F = SCC[i]->getFunction(); 388193323Sed if (!F) { 389193323Sed KnowNothing = true; 390193323Sed break; 391193323Sed } 392193323Sed 393193323Sed if (F->isDeclaration()) { 394193323Sed // Try to get mod/ref behaviour from function attributes. 395193323Sed if (F->doesNotAccessMemory()) { 396193323Sed // Can't do better than that! 397193323Sed } else if (F->onlyReadsMemory()) { 398193323Sed FunctionEffect |= Ref; 399193323Sed if (!F->isIntrinsic()) 400193323Sed // This function might call back into the module and read a global - 401193323Sed // consider every global as possibly being read by this function. 402193323Sed FR.MayReadAnyGlobal = true; 403193323Sed } else { 404193323Sed FunctionEffect |= ModRef; 405193323Sed // Can't say anything useful unless it's an intrinsic - they don't 406193323Sed // read or write global variables of the kind considered here. 407193323Sed KnowNothing = !F->isIntrinsic(); 408193323Sed } 409193323Sed continue; 410193323Sed } 411193323Sed 412193323Sed for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end(); 413193323Sed CI != E && !KnowNothing; ++CI) 414193323Sed if (Function *Callee = CI->second->getFunction()) { 415193323Sed if (FunctionRecord *CalleeFR = getFunctionInfo(Callee)) { 416193323Sed // Propagate function effect up. 417193323Sed FunctionEffect |= CalleeFR->FunctionEffect; 418193323Sed 419193323Sed // Incorporate callee's effects on globals into our info. 420212904Sdim for (std::map<const GlobalValue*, unsigned>::iterator GI = 421193323Sed CalleeFR->GlobalInfo.begin(), E = CalleeFR->GlobalInfo.end(); 422193323Sed GI != E; ++GI) 423193323Sed FR.GlobalInfo[GI->first] |= GI->second; 424193323Sed FR.MayReadAnyGlobal |= CalleeFR->MayReadAnyGlobal; 425193323Sed } else { 426193323Sed // Can't say anything about it. However, if it is inside our SCC, 427193323Sed // then nothing needs to be done. 428193323Sed CallGraphNode *CalleeNode = CG[Callee]; 429193323Sed if (std::find(SCC.begin(), SCC.end(), CalleeNode) == SCC.end()) 430193323Sed KnowNothing = true; 431193323Sed } 432193323Sed } else { 433193323Sed KnowNothing = true; 434193323Sed } 435193323Sed } 436193323Sed 437193323Sed // If we can't say anything useful about this SCC, remove all SCC functions 438193323Sed // from the FunctionInfo map. 439193323Sed if (KnowNothing) { 440193323Sed for (unsigned i = 0, e = SCC.size(); i != e; ++i) 441193323Sed FunctionInfo.erase(SCC[i]->getFunction()); 442193323Sed continue; 443193323Sed } 444193323Sed 445193323Sed // Scan the function bodies for explicit loads or stores. 446193323Sed for (unsigned i = 0, e = SCC.size(); i != e && FunctionEffect != ModRef;++i) 447193323Sed for (inst_iterator II = inst_begin(SCC[i]->getFunction()), 448193323Sed E = inst_end(SCC[i]->getFunction()); 449193323Sed II != E && FunctionEffect != ModRef; ++II) 450239462Sdim if (LoadInst *LI = dyn_cast<LoadInst>(&*II)) { 451193323Sed FunctionEffect |= Ref; 452239462Sdim if (LI->isVolatile()) 453193323Sed // Volatile loads may have side-effects, so mark them as writing 454193323Sed // memory (for example, a flag inside the processor). 455193323Sed FunctionEffect |= Mod; 456239462Sdim } else if (StoreInst *SI = dyn_cast<StoreInst>(&*II)) { 457193323Sed FunctionEffect |= Mod; 458239462Sdim if (SI->isVolatile()) 459193323Sed // Treat volatile stores as reading memory somewhere. 460193323Sed FunctionEffect |= Ref; 461243830Sdim } else if (isAllocationFn(&*II, TLI) || isFreeCall(&*II, TLI)) { 462193323Sed FunctionEffect |= ModRef; 463234353Sdim } else if (IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(&*II)) { 464234353Sdim // The callgraph doesn't include intrinsic calls. 465234353Sdim Function *Callee = Intrinsic->getCalledFunction(); 466234353Sdim ModRefBehavior Behaviour = AliasAnalysis::getModRefBehavior(Callee); 467234353Sdim FunctionEffect |= (Behaviour & ModRef); 468193323Sed } 469193323Sed 470193323Sed if ((FunctionEffect & Mod) == 0) 471193323Sed ++NumReadMemFunctions; 472193323Sed if (FunctionEffect == 0) 473193323Sed ++NumNoMemFunctions; 474193323Sed FR.FunctionEffect = FunctionEffect; 475193323Sed 476193323Sed // Finally, now that we know the full effect on this SCC, clone the 477193323Sed // information to each function in the SCC. 478193323Sed for (unsigned i = 1, e = SCC.size(); i != e; ++i) 479193323Sed FunctionInfo[SCC[i]->getFunction()] = FR; 480193323Sed } 481193323Sed} 482193323Sed 483193323Sed 484193323Sed 485193323Sed/// alias - If one of the pointers is to a global that we are tracking, and the 486193323Sed/// other is some random pointer, we know there cannot be an alias, because the 487193323Sed/// address of the global isn't taken. 488193323SedAliasAnalysis::AliasResult 489218893SdimGlobalsModRef::alias(const Location &LocA, 490218893Sdim const Location &LocB) { 491193323Sed // Get the base object these pointers point to. 492218893Sdim const Value *UV1 = GetUnderlyingObject(LocA.Ptr); 493218893Sdim const Value *UV2 = GetUnderlyingObject(LocB.Ptr); 494193323Sed 495193323Sed // If either of the underlying values is a global, they may be non-addr-taken 496193323Sed // globals, which we can answer queries about. 497212904Sdim const GlobalValue *GV1 = dyn_cast<GlobalValue>(UV1); 498212904Sdim const GlobalValue *GV2 = dyn_cast<GlobalValue>(UV2); 499193323Sed if (GV1 || GV2) { 500193323Sed // If the global's address is taken, pretend we don't know it's a pointer to 501193323Sed // the global. 502193323Sed if (GV1 && !NonAddressTakenGlobals.count(GV1)) GV1 = 0; 503193323Sed if (GV2 && !NonAddressTakenGlobals.count(GV2)) GV2 = 0; 504193323Sed 505203954Srdivacky // If the two pointers are derived from two different non-addr-taken 506193323Sed // globals, or if one is and the other isn't, we know these can't alias. 507193323Sed if ((GV1 || GV2) && GV1 != GV2) 508193323Sed return NoAlias; 509193323Sed 510193323Sed // Otherwise if they are both derived from the same addr-taken global, we 511193323Sed // can't know the two accesses don't overlap. 512193323Sed } 513193323Sed 514193323Sed // These pointers may be based on the memory owned by an indirect global. If 515193323Sed // so, we may be able to handle this. First check to see if the base pointer 516193323Sed // is a direct load from an indirect global. 517193323Sed GV1 = GV2 = 0; 518212904Sdim if (const LoadInst *LI = dyn_cast<LoadInst>(UV1)) 519193323Sed if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0))) 520193323Sed if (IndirectGlobals.count(GV)) 521193323Sed GV1 = GV; 522212904Sdim if (const LoadInst *LI = dyn_cast<LoadInst>(UV2)) 523212904Sdim if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0))) 524193323Sed if (IndirectGlobals.count(GV)) 525193323Sed GV2 = GV; 526193323Sed 527193323Sed // These pointers may also be from an allocation for the indirect global. If 528193323Sed // so, also handle them. 529193323Sed if (AllocsForIndirectGlobals.count(UV1)) 530193323Sed GV1 = AllocsForIndirectGlobals[UV1]; 531193323Sed if (AllocsForIndirectGlobals.count(UV2)) 532193323Sed GV2 = AllocsForIndirectGlobals[UV2]; 533193323Sed 534193323Sed // Now that we know whether the two pointers are related to indirect globals, 535193323Sed // use this to disambiguate the pointers. If either pointer is based on an 536193323Sed // indirect global and if they are not both based on the same indirect global, 537193323Sed // they cannot alias. 538193323Sed if ((GV1 || GV2) && GV1 != GV2) 539193323Sed return NoAlias; 540193323Sed 541218893Sdim return AliasAnalysis::alias(LocA, LocB); 542193323Sed} 543193323Sed 544193323SedAliasAnalysis::ModRefResult 545212904SdimGlobalsModRef::getModRefInfo(ImmutableCallSite CS, 546218893Sdim const Location &Loc) { 547193323Sed unsigned Known = ModRef; 548193323Sed 549193323Sed // If we are asking for mod/ref info of a direct call with a pointer to a 550193323Sed // global we are tracking, return information if we have it. 551218893Sdim if (const GlobalValue *GV = 552218893Sdim dyn_cast<GlobalValue>(GetUnderlyingObject(Loc.Ptr))) 553193323Sed if (GV->hasLocalLinkage()) 554212904Sdim if (const Function *F = CS.getCalledFunction()) 555193323Sed if (NonAddressTakenGlobals.count(GV)) 556212904Sdim if (const FunctionRecord *FR = getFunctionInfo(F)) 557193323Sed Known = FR->getInfoForGlobal(GV); 558193323Sed 559193323Sed if (Known == NoModRef) 560193323Sed return NoModRef; // No need to query other mod/ref analyses 561218893Sdim return ModRefResult(Known & AliasAnalysis::getModRefInfo(CS, Loc)); 562193323Sed} 563193323Sed 564193323Sed 565193323Sed//===----------------------------------------------------------------------===// 566193323Sed// Methods to update the analysis as a result of the client transformation. 567193323Sed// 568193323Sedvoid GlobalsModRef::deleteValue(Value *V) { 569193323Sed if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 570193323Sed if (NonAddressTakenGlobals.erase(GV)) { 571193323Sed // This global might be an indirect global. If so, remove it and remove 572193323Sed // any AllocRelatedValues for it. 573193323Sed if (IndirectGlobals.erase(GV)) { 574193323Sed // Remove any entries in AllocsForIndirectGlobals for this global. 575212904Sdim for (std::map<const Value*, const GlobalValue*>::iterator 576193323Sed I = AllocsForIndirectGlobals.begin(), 577193323Sed E = AllocsForIndirectGlobals.end(); I != E; ) { 578193323Sed if (I->second == GV) { 579193323Sed AllocsForIndirectGlobals.erase(I++); 580193323Sed } else { 581193323Sed ++I; 582193323Sed } 583193323Sed } 584193323Sed } 585193323Sed } 586193323Sed } 587193323Sed 588193323Sed // Otherwise, if this is an allocation related to an indirect global, remove 589193323Sed // it. 590193323Sed AllocsForIndirectGlobals.erase(V); 591193323Sed 592193323Sed AliasAnalysis::deleteValue(V); 593193323Sed} 594193323Sed 595193323Sedvoid GlobalsModRef::copyValue(Value *From, Value *To) { 596193323Sed AliasAnalysis::copyValue(From, To); 597193323Sed} 598218893Sdim 599218893Sdimvoid GlobalsModRef::addEscapingUse(Use &U) { 600218893Sdim // For the purposes of this analysis, it is conservatively correct to treat 601218893Sdim // a newly escaping value equivalently to a deleted one. We could perhaps 602218893Sdim // be more precise by processing the new use and attempting to update our 603221345Sdim // saved analysis results to accommodate it. 604218893Sdim deleteValue(U); 605218893Sdim 606218893Sdim AliasAnalysis::addEscapingUse(U); 607218893Sdim} 608