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