GlobalsModRef.cpp revision 193323
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" 19193323Sed#include "llvm/Module.h" 20193323Sed#include "llvm/Pass.h" 21193323Sed#include "llvm/Instructions.h" 22193323Sed#include "llvm/Constants.h" 23193323Sed#include "llvm/DerivedTypes.h" 24193323Sed#include "llvm/Analysis/AliasAnalysis.h" 25193323Sed#include "llvm/Analysis/CallGraph.h" 26193323Sed#include "llvm/Support/Compiler.h" 27193323Sed#include "llvm/Support/CommandLine.h" 28193323Sed#include "llvm/Support/InstIterator.h" 29193323Sed#include "llvm/ADT/Statistic.h" 30193323Sed#include "llvm/ADT/SCCIterator.h" 31193323Sed#include <set> 32193323Sedusing namespace llvm; 33193323Sed 34193323SedSTATISTIC(NumNonAddrTakenGlobalVars, 35193323Sed "Number of global vars without address taken"); 36193323SedSTATISTIC(NumNonAddrTakenFunctions,"Number of functions without address taken"); 37193323SedSTATISTIC(NumNoMemFunctions, "Number of functions that do not access memory"); 38193323SedSTATISTIC(NumReadMemFunctions, "Number of functions that only read memory"); 39193323SedSTATISTIC(NumIndirectGlobalVars, "Number of indirect global objects"); 40193323Sed 41193323Sednamespace { 42193323Sed /// FunctionRecord - One instance of this structure is stored for every 43193323Sed /// function in the program. Later, the entries for these functions are 44193323Sed /// removed if the function is found to call an external function (in which 45193323Sed /// case we know nothing about it. 46193323Sed struct VISIBILITY_HIDDEN FunctionRecord { 47193323Sed /// GlobalInfo - Maintain mod/ref info for all of the globals without 48193323Sed /// addresses taken that are read or written (transitively) by this 49193323Sed /// function. 50193323Sed std::map<GlobalValue*, unsigned> GlobalInfo; 51193323Sed 52193323Sed /// MayReadAnyGlobal - May read global variables, but it is not known which. 53193323Sed bool MayReadAnyGlobal; 54193323Sed 55193323Sed unsigned getInfoForGlobal(GlobalValue *GV) const { 56193323Sed unsigned Effect = MayReadAnyGlobal ? AliasAnalysis::Ref : 0; 57193323Sed std::map<GlobalValue*, unsigned>::const_iterator I = GlobalInfo.find(GV); 58193323Sed if (I != GlobalInfo.end()) 59193323Sed Effect |= I->second; 60193323Sed return Effect; 61193323Sed } 62193323Sed 63193323Sed /// FunctionEffect - Capture whether or not this function reads or writes to 64193323Sed /// ANY memory. If not, we can do a lot of aggressive analysis on it. 65193323Sed unsigned FunctionEffect; 66193323Sed 67193323Sed FunctionRecord() : MayReadAnyGlobal (false), FunctionEffect(0) {} 68193323Sed }; 69193323Sed 70193323Sed /// GlobalsModRef - The actual analysis pass. 71193323Sed class VISIBILITY_HIDDEN GlobalsModRef 72193323Sed : public ModulePass, public AliasAnalysis { 73193323Sed /// NonAddressTakenGlobals - The globals that do not have their addresses 74193323Sed /// taken. 75193323Sed std::set<GlobalValue*> NonAddressTakenGlobals; 76193323Sed 77193323Sed /// IndirectGlobals - The memory pointed to by this global is known to be 78193323Sed /// 'owned' by the global. 79193323Sed std::set<GlobalValue*> IndirectGlobals; 80193323Sed 81193323Sed /// AllocsForIndirectGlobals - If an instruction allocates memory for an 82193323Sed /// indirect global, this map indicates which one. 83193323Sed std::map<Value*, GlobalValue*> AllocsForIndirectGlobals; 84193323Sed 85193323Sed /// FunctionInfo - For each function, keep track of what globals are 86193323Sed /// modified or read. 87193323Sed std::map<Function*, FunctionRecord> FunctionInfo; 88193323Sed 89193323Sed public: 90193323Sed static char ID; 91193323Sed GlobalsModRef() : ModulePass(&ID) {} 92193323Sed 93193323Sed bool runOnModule(Module &M) { 94193323Sed InitializeAliasAnalysis(this); // set up super class 95193323Sed AnalyzeGlobals(M); // find non-addr taken globals 96193323Sed AnalyzeCallGraph(getAnalysis<CallGraph>(), M); // Propagate on CG 97193323Sed return false; 98193323Sed } 99193323Sed 100193323Sed virtual void getAnalysisUsage(AnalysisUsage &AU) const { 101193323Sed AliasAnalysis::getAnalysisUsage(AU); 102193323Sed AU.addRequired<CallGraph>(); 103193323Sed AU.setPreservesAll(); // Does not transform code 104193323Sed } 105193323Sed 106193323Sed //------------------------------------------------ 107193323Sed // Implement the AliasAnalysis API 108193323Sed // 109193323Sed AliasResult alias(const Value *V1, unsigned V1Size, 110193323Sed const Value *V2, unsigned V2Size); 111193323Sed ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size); 112193323Sed ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) { 113193323Sed return AliasAnalysis::getModRefInfo(CS1,CS2); 114193323Sed } 115193323Sed bool hasNoModRefInfoForCalls() const { return false; } 116193323Sed 117193323Sed /// getModRefBehavior - Return the behavior of the specified function if 118193323Sed /// called from the specified call site. The call site may be null in which 119193323Sed /// case the most generic behavior of this function should be returned. 120193323Sed ModRefBehavior getModRefBehavior(Function *F, 121193323Sed std::vector<PointerAccessInfo> *Info) { 122193323Sed if (FunctionRecord *FR = getFunctionInfo(F)) { 123193323Sed if (FR->FunctionEffect == 0) 124193323Sed return DoesNotAccessMemory; 125193323Sed else if ((FR->FunctionEffect & Mod) == 0) 126193323Sed return OnlyReadsMemory; 127193323Sed } 128193323Sed return AliasAnalysis::getModRefBehavior(F, Info); 129193323Sed } 130193323Sed 131193323Sed /// getModRefBehavior - Return the behavior of the specified function if 132193323Sed /// called from the specified call site. The call site may be null in which 133193323Sed /// case the most generic behavior of this function should be returned. 134193323Sed ModRefBehavior getModRefBehavior(CallSite CS, 135193323Sed std::vector<PointerAccessInfo> *Info) { 136193323Sed Function* F = CS.getCalledFunction(); 137193323Sed if (!F) return AliasAnalysis::getModRefBehavior(CS, Info); 138193323Sed if (FunctionRecord *FR = getFunctionInfo(F)) { 139193323Sed if (FR->FunctionEffect == 0) 140193323Sed return DoesNotAccessMemory; 141193323Sed else if ((FR->FunctionEffect & Mod) == 0) 142193323Sed return OnlyReadsMemory; 143193323Sed } 144193323Sed return AliasAnalysis::getModRefBehavior(CS, Info); 145193323Sed } 146193323Sed 147193323Sed virtual void deleteValue(Value *V); 148193323Sed virtual void copyValue(Value *From, Value *To); 149193323Sed 150193323Sed private: 151193323Sed /// getFunctionInfo - Return the function info for the function, or null if 152193323Sed /// we don't have anything useful to say about it. 153193323Sed FunctionRecord *getFunctionInfo(Function *F) { 154193323Sed std::map<Function*, FunctionRecord>::iterator I = FunctionInfo.find(F); 155193323Sed if (I != FunctionInfo.end()) 156193323Sed return &I->second; 157193323Sed return 0; 158193323Sed } 159193323Sed 160193323Sed void AnalyzeGlobals(Module &M); 161193323Sed void AnalyzeCallGraph(CallGraph &CG, Module &M); 162193323Sed bool AnalyzeUsesOfPointer(Value *V, std::vector<Function*> &Readers, 163193323Sed std::vector<Function*> &Writers, 164193323Sed GlobalValue *OkayStoreDest = 0); 165193323Sed bool AnalyzeIndirectGlobalMemory(GlobalValue *GV); 166193323Sed }; 167193323Sed} 168193323Sed 169193323Sedchar GlobalsModRef::ID = 0; 170193323Sedstatic RegisterPass<GlobalsModRef> 171193323SedX("globalsmodref-aa", "Simple mod/ref analysis for globals", false, true); 172193323Sedstatic RegisterAnalysisGroup<AliasAnalysis> Y(X); 173193323Sed 174193323SedPass *llvm::createGlobalsModRefPass() { return new GlobalsModRef(); } 175193323Sed 176193323Sed/// AnalyzeGlobals - Scan through the users of all of the internal 177193323Sed/// GlobalValue's in the program. If none of them have their "address taken" 178193323Sed/// (really, their address passed to something nontrivial), record this fact, 179193323Sed/// and record the functions that they are used directly in. 180193323Sedvoid GlobalsModRef::AnalyzeGlobals(Module &M) { 181193323Sed std::vector<Function*> Readers, Writers; 182193323Sed for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 183193323Sed if (I->hasLocalLinkage()) { 184193323Sed if (!AnalyzeUsesOfPointer(I, Readers, Writers)) { 185193323Sed // Remember that we are tracking this global. 186193323Sed NonAddressTakenGlobals.insert(I); 187193323Sed ++NumNonAddrTakenFunctions; 188193323Sed } 189193323Sed Readers.clear(); Writers.clear(); 190193323Sed } 191193323Sed 192193323Sed for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 193193323Sed I != E; ++I) 194193323Sed if (I->hasLocalLinkage()) { 195193323Sed if (!AnalyzeUsesOfPointer(I, Readers, Writers)) { 196193323Sed // Remember that we are tracking this global, and the mod/ref fns 197193323Sed NonAddressTakenGlobals.insert(I); 198193323Sed 199193323Sed for (unsigned i = 0, e = Readers.size(); i != e; ++i) 200193323Sed FunctionInfo[Readers[i]].GlobalInfo[I] |= Ref; 201193323Sed 202193323Sed if (!I->isConstant()) // No need to keep track of writers to constants 203193323Sed for (unsigned i = 0, e = Writers.size(); i != e; ++i) 204193323Sed FunctionInfo[Writers[i]].GlobalInfo[I] |= Mod; 205193323Sed ++NumNonAddrTakenGlobalVars; 206193323Sed 207193323Sed // If this global holds a pointer type, see if it is an indirect global. 208193323Sed if (isa<PointerType>(I->getType()->getElementType()) && 209193323Sed AnalyzeIndirectGlobalMemory(I)) 210193323Sed ++NumIndirectGlobalVars; 211193323Sed } 212193323Sed Readers.clear(); Writers.clear(); 213193323Sed } 214193323Sed} 215193323Sed 216193323Sed/// AnalyzeUsesOfPointer - Look at all of the users of the specified pointer. 217193323Sed/// If this is used by anything complex (i.e., the address escapes), return 218193323Sed/// true. Also, while we are at it, keep track of those functions that read and 219193323Sed/// write to the value. 220193323Sed/// 221193323Sed/// If OkayStoreDest is non-null, stores into this global are allowed. 222193323Sedbool GlobalsModRef::AnalyzeUsesOfPointer(Value *V, 223193323Sed std::vector<Function*> &Readers, 224193323Sed std::vector<Function*> &Writers, 225193323Sed GlobalValue *OkayStoreDest) { 226193323Sed if (!isa<PointerType>(V->getType())) return true; 227193323Sed 228193323Sed for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI) 229193323Sed if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) { 230193323Sed Readers.push_back(LI->getParent()->getParent()); 231193323Sed } else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { 232193323Sed if (V == SI->getOperand(1)) { 233193323Sed Writers.push_back(SI->getParent()->getParent()); 234193323Sed } else if (SI->getOperand(1) != OkayStoreDest) { 235193323Sed return true; // Storing the pointer 236193323Sed } 237193323Sed } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) { 238193323Sed if (AnalyzeUsesOfPointer(GEP, Readers, Writers)) return true; 239193323Sed } else if (CallInst *CI = dyn_cast<CallInst>(*UI)) { 240193323Sed // Make sure that this is just the function being called, not that it is 241193323Sed // passing into the function. 242193323Sed for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i) 243193323Sed if (CI->getOperand(i) == V) return true; 244193323Sed } else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) { 245193323Sed // Make sure that this is just the function being called, not that it is 246193323Sed // passing into the function. 247193323Sed for (unsigned i = 3, e = II->getNumOperands(); i != e; ++i) 248193323Sed if (II->getOperand(i) == V) return true; 249193323Sed } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(*UI)) { 250193323Sed if (CE->getOpcode() == Instruction::GetElementPtr || 251193323Sed CE->getOpcode() == Instruction::BitCast) { 252193323Sed if (AnalyzeUsesOfPointer(CE, Readers, Writers)) 253193323Sed return true; 254193323Sed } else { 255193323Sed return true; 256193323Sed } 257193323Sed } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(*UI)) { 258193323Sed if (!isa<ConstantPointerNull>(ICI->getOperand(1))) 259193323Sed return true; // Allow comparison against null. 260193323Sed } else if (FreeInst *F = dyn_cast<FreeInst>(*UI)) { 261193323Sed Writers.push_back(F->getParent()->getParent()); 262193323Sed } else { 263193323Sed return true; 264193323Sed } 265193323Sed return false; 266193323Sed} 267193323Sed 268193323Sed/// AnalyzeIndirectGlobalMemory - We found an non-address-taken global variable 269193323Sed/// which holds a pointer type. See if the global always points to non-aliased 270193323Sed/// heap memory: that is, all initializers of the globals are allocations, and 271193323Sed/// those allocations have no use other than initialization of the global. 272193323Sed/// Further, all loads out of GV must directly use the memory, not store the 273193323Sed/// pointer somewhere. If this is true, we consider the memory pointed to by 274193323Sed/// GV to be owned by GV and can disambiguate other pointers from it. 275193323Sedbool GlobalsModRef::AnalyzeIndirectGlobalMemory(GlobalValue *GV) { 276193323Sed // Keep track of values related to the allocation of the memory, f.e. the 277193323Sed // value produced by the malloc call and any casts. 278193323Sed std::vector<Value*> AllocRelatedValues; 279193323Sed 280193323Sed // Walk the user list of the global. If we find anything other than a direct 281193323Sed // load or store, bail out. 282193323Sed for (Value::use_iterator I = GV->use_begin(), E = GV->use_end(); I != E; ++I){ 283193323Sed if (LoadInst *LI = dyn_cast<LoadInst>(*I)) { 284193323Sed // The pointer loaded from the global can only be used in simple ways: 285193323Sed // we allow addressing of it and loading storing to it. We do *not* allow 286193323Sed // storing the loaded pointer somewhere else or passing to a function. 287193323Sed std::vector<Function*> ReadersWriters; 288193323Sed if (AnalyzeUsesOfPointer(LI, ReadersWriters, ReadersWriters)) 289193323Sed return false; // Loaded pointer escapes. 290193323Sed // TODO: Could try some IP mod/ref of the loaded pointer. 291193323Sed } else if (StoreInst *SI = dyn_cast<StoreInst>(*I)) { 292193323Sed // Storing the global itself. 293193323Sed if (SI->getOperand(0) == GV) return false; 294193323Sed 295193323Sed // If storing the null pointer, ignore it. 296193323Sed if (isa<ConstantPointerNull>(SI->getOperand(0))) 297193323Sed continue; 298193323Sed 299193323Sed // Check the value being stored. 300193323Sed Value *Ptr = SI->getOperand(0)->getUnderlyingObject(); 301193323Sed 302193323Sed if (isa<MallocInst>(Ptr)) { 303193323Sed // Okay, easy case. 304193323Sed } else if (CallInst *CI = dyn_cast<CallInst>(Ptr)) { 305193323Sed Function *F = CI->getCalledFunction(); 306193323Sed if (!F || !F->isDeclaration()) return false; // Too hard to analyze. 307193323Sed if (F->getName() != "calloc") return false; // Not calloc. 308193323Sed } else { 309193323Sed return false; // Too hard to analyze. 310193323Sed } 311193323Sed 312193323Sed // Analyze all uses of the allocation. If any of them are used in a 313193323Sed // non-simple way (e.g. stored to another global) bail out. 314193323Sed std::vector<Function*> ReadersWriters; 315193323Sed if (AnalyzeUsesOfPointer(Ptr, ReadersWriters, ReadersWriters, GV)) 316193323Sed return false; // Loaded pointer escapes. 317193323Sed 318193323Sed // Remember that this allocation is related to the indirect global. 319193323Sed AllocRelatedValues.push_back(Ptr); 320193323Sed } else { 321193323Sed // Something complex, bail out. 322193323Sed return false; 323193323Sed } 324193323Sed } 325193323Sed 326193323Sed // Okay, this is an indirect global. Remember all of the allocations for 327193323Sed // this global in AllocsForIndirectGlobals. 328193323Sed while (!AllocRelatedValues.empty()) { 329193323Sed AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV; 330193323Sed AllocRelatedValues.pop_back(); 331193323Sed } 332193323Sed IndirectGlobals.insert(GV); 333193323Sed return true; 334193323Sed} 335193323Sed 336193323Sed/// AnalyzeCallGraph - At this point, we know the functions where globals are 337193323Sed/// immediately stored to and read from. Propagate this information up the call 338193323Sed/// graph to all callers and compute the mod/ref info for all memory for each 339193323Sed/// function. 340193323Sedvoid GlobalsModRef::AnalyzeCallGraph(CallGraph &CG, Module &M) { 341193323Sed // We do a bottom-up SCC traversal of the call graph. In other words, we 342193323Sed // visit all callees before callers (leaf-first). 343193323Sed for (scc_iterator<CallGraph*> I = scc_begin(&CG), E = scc_end(&CG); I != E; 344193323Sed ++I) { 345193323Sed std::vector<CallGraphNode *> &SCC = *I; 346193323Sed assert(!SCC.empty() && "SCC with no functions?"); 347193323Sed 348193323Sed if (!SCC[0]->getFunction()) { 349193323Sed // Calls externally - can't say anything useful. Remove any existing 350193323Sed // function records (may have been created when scanning globals). 351193323Sed for (unsigned i = 0, e = SCC.size(); i != e; ++i) 352193323Sed FunctionInfo.erase(SCC[i]->getFunction()); 353193323Sed continue; 354193323Sed } 355193323Sed 356193323Sed FunctionRecord &FR = FunctionInfo[SCC[0]->getFunction()]; 357193323Sed 358193323Sed bool KnowNothing = false; 359193323Sed unsigned FunctionEffect = 0; 360193323Sed 361193323Sed // Collect the mod/ref properties due to called functions. We only compute 362193323Sed // one mod-ref set. 363193323Sed for (unsigned i = 0, e = SCC.size(); i != e && !KnowNothing; ++i) { 364193323Sed Function *F = SCC[i]->getFunction(); 365193323Sed if (!F) { 366193323Sed KnowNothing = true; 367193323Sed break; 368193323Sed } 369193323Sed 370193323Sed if (F->isDeclaration()) { 371193323Sed // Try to get mod/ref behaviour from function attributes. 372193323Sed if (F->doesNotAccessMemory()) { 373193323Sed // Can't do better than that! 374193323Sed } else if (F->onlyReadsMemory()) { 375193323Sed FunctionEffect |= Ref; 376193323Sed if (!F->isIntrinsic()) 377193323Sed // This function might call back into the module and read a global - 378193323Sed // consider every global as possibly being read by this function. 379193323Sed FR.MayReadAnyGlobal = true; 380193323Sed } else { 381193323Sed FunctionEffect |= ModRef; 382193323Sed // Can't say anything useful unless it's an intrinsic - they don't 383193323Sed // read or write global variables of the kind considered here. 384193323Sed KnowNothing = !F->isIntrinsic(); 385193323Sed } 386193323Sed continue; 387193323Sed } 388193323Sed 389193323Sed for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end(); 390193323Sed CI != E && !KnowNothing; ++CI) 391193323Sed if (Function *Callee = CI->second->getFunction()) { 392193323Sed if (FunctionRecord *CalleeFR = getFunctionInfo(Callee)) { 393193323Sed // Propagate function effect up. 394193323Sed FunctionEffect |= CalleeFR->FunctionEffect; 395193323Sed 396193323Sed // Incorporate callee's effects on globals into our info. 397193323Sed for (std::map<GlobalValue*, unsigned>::iterator GI = 398193323Sed CalleeFR->GlobalInfo.begin(), E = CalleeFR->GlobalInfo.end(); 399193323Sed GI != E; ++GI) 400193323Sed FR.GlobalInfo[GI->first] |= GI->second; 401193323Sed FR.MayReadAnyGlobal |= CalleeFR->MayReadAnyGlobal; 402193323Sed } else { 403193323Sed // Can't say anything about it. However, if it is inside our SCC, 404193323Sed // then nothing needs to be done. 405193323Sed CallGraphNode *CalleeNode = CG[Callee]; 406193323Sed if (std::find(SCC.begin(), SCC.end(), CalleeNode) == SCC.end()) 407193323Sed KnowNothing = true; 408193323Sed } 409193323Sed } else { 410193323Sed KnowNothing = true; 411193323Sed } 412193323Sed } 413193323Sed 414193323Sed // If we can't say anything useful about this SCC, remove all SCC functions 415193323Sed // from the FunctionInfo map. 416193323Sed if (KnowNothing) { 417193323Sed for (unsigned i = 0, e = SCC.size(); i != e; ++i) 418193323Sed FunctionInfo.erase(SCC[i]->getFunction()); 419193323Sed continue; 420193323Sed } 421193323Sed 422193323Sed // Scan the function bodies for explicit loads or stores. 423193323Sed for (unsigned i = 0, e = SCC.size(); i != e && FunctionEffect != ModRef;++i) 424193323Sed for (inst_iterator II = inst_begin(SCC[i]->getFunction()), 425193323Sed E = inst_end(SCC[i]->getFunction()); 426193323Sed II != E && FunctionEffect != ModRef; ++II) 427193323Sed if (isa<LoadInst>(*II)) { 428193323Sed FunctionEffect |= Ref; 429193323Sed if (cast<LoadInst>(*II).isVolatile()) 430193323Sed // Volatile loads may have side-effects, so mark them as writing 431193323Sed // memory (for example, a flag inside the processor). 432193323Sed FunctionEffect |= Mod; 433193323Sed } else if (isa<StoreInst>(*II)) { 434193323Sed FunctionEffect |= Mod; 435193323Sed if (cast<StoreInst>(*II).isVolatile()) 436193323Sed // Treat volatile stores as reading memory somewhere. 437193323Sed FunctionEffect |= Ref; 438193323Sed } else if (isa<MallocInst>(*II) || isa<FreeInst>(*II)) { 439193323Sed FunctionEffect |= ModRef; 440193323Sed } 441193323Sed 442193323Sed if ((FunctionEffect & Mod) == 0) 443193323Sed ++NumReadMemFunctions; 444193323Sed if (FunctionEffect == 0) 445193323Sed ++NumNoMemFunctions; 446193323Sed FR.FunctionEffect = FunctionEffect; 447193323Sed 448193323Sed // Finally, now that we know the full effect on this SCC, clone the 449193323Sed // information to each function in the SCC. 450193323Sed for (unsigned i = 1, e = SCC.size(); i != e; ++i) 451193323Sed FunctionInfo[SCC[i]->getFunction()] = FR; 452193323Sed } 453193323Sed} 454193323Sed 455193323Sed 456193323Sed 457193323Sed/// alias - If one of the pointers is to a global that we are tracking, and the 458193323Sed/// other is some random pointer, we know there cannot be an alias, because the 459193323Sed/// address of the global isn't taken. 460193323SedAliasAnalysis::AliasResult 461193323SedGlobalsModRef::alias(const Value *V1, unsigned V1Size, 462193323Sed const Value *V2, unsigned V2Size) { 463193323Sed // Get the base object these pointers point to. 464193323Sed Value *UV1 = const_cast<Value*>(V1->getUnderlyingObject()); 465193323Sed Value *UV2 = const_cast<Value*>(V2->getUnderlyingObject()); 466193323Sed 467193323Sed // If either of the underlying values is a global, they may be non-addr-taken 468193323Sed // globals, which we can answer queries about. 469193323Sed GlobalValue *GV1 = dyn_cast<GlobalValue>(UV1); 470193323Sed GlobalValue *GV2 = dyn_cast<GlobalValue>(UV2); 471193323Sed if (GV1 || GV2) { 472193323Sed // If the global's address is taken, pretend we don't know it's a pointer to 473193323Sed // the global. 474193323Sed if (GV1 && !NonAddressTakenGlobals.count(GV1)) GV1 = 0; 475193323Sed if (GV2 && !NonAddressTakenGlobals.count(GV2)) GV2 = 0; 476193323Sed 477193323Sed // If the the two pointers are derived from two different non-addr-taken 478193323Sed // globals, or if one is and the other isn't, we know these can't alias. 479193323Sed if ((GV1 || GV2) && GV1 != GV2) 480193323Sed return NoAlias; 481193323Sed 482193323Sed // Otherwise if they are both derived from the same addr-taken global, we 483193323Sed // can't know the two accesses don't overlap. 484193323Sed } 485193323Sed 486193323Sed // These pointers may be based on the memory owned by an indirect global. If 487193323Sed // so, we may be able to handle this. First check to see if the base pointer 488193323Sed // is a direct load from an indirect global. 489193323Sed GV1 = GV2 = 0; 490193323Sed if (LoadInst *LI = dyn_cast<LoadInst>(UV1)) 491193323Sed if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0))) 492193323Sed if (IndirectGlobals.count(GV)) 493193323Sed GV1 = GV; 494193323Sed if (LoadInst *LI = dyn_cast<LoadInst>(UV2)) 495193323Sed if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0))) 496193323Sed if (IndirectGlobals.count(GV)) 497193323Sed GV2 = GV; 498193323Sed 499193323Sed // These pointers may also be from an allocation for the indirect global. If 500193323Sed // so, also handle them. 501193323Sed if (AllocsForIndirectGlobals.count(UV1)) 502193323Sed GV1 = AllocsForIndirectGlobals[UV1]; 503193323Sed if (AllocsForIndirectGlobals.count(UV2)) 504193323Sed GV2 = AllocsForIndirectGlobals[UV2]; 505193323Sed 506193323Sed // Now that we know whether the two pointers are related to indirect globals, 507193323Sed // use this to disambiguate the pointers. If either pointer is based on an 508193323Sed // indirect global and if they are not both based on the same indirect global, 509193323Sed // they cannot alias. 510193323Sed if ((GV1 || GV2) && GV1 != GV2) 511193323Sed return NoAlias; 512193323Sed 513193323Sed return AliasAnalysis::alias(V1, V1Size, V2, V2Size); 514193323Sed} 515193323Sed 516193323SedAliasAnalysis::ModRefResult 517193323SedGlobalsModRef::getModRefInfo(CallSite CS, Value *P, unsigned Size) { 518193323Sed unsigned Known = ModRef; 519193323Sed 520193323Sed // If we are asking for mod/ref info of a direct call with a pointer to a 521193323Sed // global we are tracking, return information if we have it. 522193323Sed if (GlobalValue *GV = dyn_cast<GlobalValue>(P->getUnderlyingObject())) 523193323Sed if (GV->hasLocalLinkage()) 524193323Sed if (Function *F = CS.getCalledFunction()) 525193323Sed if (NonAddressTakenGlobals.count(GV)) 526193323Sed if (FunctionRecord *FR = getFunctionInfo(F)) 527193323Sed Known = FR->getInfoForGlobal(GV); 528193323Sed 529193323Sed if (Known == NoModRef) 530193323Sed return NoModRef; // No need to query other mod/ref analyses 531193323Sed return ModRefResult(Known & AliasAnalysis::getModRefInfo(CS, P, Size)); 532193323Sed} 533193323Sed 534193323Sed 535193323Sed//===----------------------------------------------------------------------===// 536193323Sed// Methods to update the analysis as a result of the client transformation. 537193323Sed// 538193323Sedvoid GlobalsModRef::deleteValue(Value *V) { 539193323Sed if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 540193323Sed if (NonAddressTakenGlobals.erase(GV)) { 541193323Sed // This global might be an indirect global. If so, remove it and remove 542193323Sed // any AllocRelatedValues for it. 543193323Sed if (IndirectGlobals.erase(GV)) { 544193323Sed // Remove any entries in AllocsForIndirectGlobals for this global. 545193323Sed for (std::map<Value*, GlobalValue*>::iterator 546193323Sed I = AllocsForIndirectGlobals.begin(), 547193323Sed E = AllocsForIndirectGlobals.end(); I != E; ) { 548193323Sed if (I->second == GV) { 549193323Sed AllocsForIndirectGlobals.erase(I++); 550193323Sed } else { 551193323Sed ++I; 552193323Sed } 553193323Sed } 554193323Sed } 555193323Sed } 556193323Sed } 557193323Sed 558193323Sed // Otherwise, if this is an allocation related to an indirect global, remove 559193323Sed // it. 560193323Sed AllocsForIndirectGlobals.erase(V); 561193323Sed 562193323Sed AliasAnalysis::deleteValue(V); 563193323Sed} 564193323Sed 565193323Sedvoid GlobalsModRef::copyValue(Value *From, Value *To) { 566193323Sed AliasAnalysis::copyValue(From, To); 567193323Sed} 568