LLVM API Documentation

GlobalsModRef.cpp
Go to the documentation of this file.
00001 //===- GlobalsModRef.cpp - Simple Mod/Ref Analysis for Globals ------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This simple pass provides alias and mod/ref information for global values
00011 // that do not have their address taken, and keeps track of whether functions
00012 // read or write memory (are "pure").  For this simple (but very common) case,
00013 // we can provide pretty accurate and useful information.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #include "llvm/Analysis/Passes.h"
00018 #include "llvm/ADT/SCCIterator.h"
00019 #include "llvm/ADT/Statistic.h"
00020 #include "llvm/Analysis/AliasAnalysis.h"
00021 #include "llvm/Analysis/CallGraph.h"
00022 #include "llvm/Analysis/MemoryBuiltins.h"
00023 #include "llvm/Analysis/ValueTracking.h"
00024 #include "llvm/IR/Constants.h"
00025 #include "llvm/IR/DerivedTypes.h"
00026 #include "llvm/IR/InstIterator.h"
00027 #include "llvm/IR/Instructions.h"
00028 #include "llvm/IR/IntrinsicInst.h"
00029 #include "llvm/IR/Module.h"
00030 #include "llvm/Pass.h"
00031 #include "llvm/Support/CommandLine.h"
00032 #include <set>
00033 using namespace llvm;
00034 
00035 #define DEBUG_TYPE "globalsmodref-aa"
00036 
00037 STATISTIC(NumNonAddrTakenGlobalVars,
00038           "Number of global vars without address taken");
00039 STATISTIC(NumNonAddrTakenFunctions,"Number of functions without address taken");
00040 STATISTIC(NumNoMemFunctions, "Number of functions that do not access memory");
00041 STATISTIC(NumReadMemFunctions, "Number of functions that only read memory");
00042 STATISTIC(NumIndirectGlobalVars, "Number of indirect global objects");
00043 
00044 namespace {
00045   /// FunctionRecord - One instance of this structure is stored for every
00046   /// function in the program.  Later, the entries for these functions are
00047   /// removed if the function is found to call an external function (in which
00048   /// case we know nothing about it.
00049   struct FunctionRecord {
00050     /// GlobalInfo - Maintain mod/ref info for all of the globals without
00051     /// addresses taken that are read or written (transitively) by this
00052     /// function.
00053     std::map<const GlobalValue*, unsigned> GlobalInfo;
00054 
00055     /// MayReadAnyGlobal - May read global variables, but it is not known which.
00056     bool MayReadAnyGlobal;
00057 
00058     unsigned getInfoForGlobal(const GlobalValue *GV) const {
00059       unsigned Effect = MayReadAnyGlobal ? AliasAnalysis::Ref : 0;
00060       std::map<const GlobalValue*, unsigned>::const_iterator I =
00061         GlobalInfo.find(GV);
00062       if (I != GlobalInfo.end())
00063         Effect |= I->second;
00064       return Effect;
00065     }
00066 
00067     /// FunctionEffect - Capture whether or not this function reads or writes to
00068     /// ANY memory.  If not, we can do a lot of aggressive analysis on it.
00069     unsigned FunctionEffect;
00070 
00071     FunctionRecord() : MayReadAnyGlobal (false), FunctionEffect(0) {}
00072   };
00073 
00074   /// GlobalsModRef - The actual analysis pass.
00075   class GlobalsModRef : public ModulePass, public AliasAnalysis {
00076     /// NonAddressTakenGlobals - The globals that do not have their addresses
00077     /// taken.
00078     std::set<const GlobalValue*> NonAddressTakenGlobals;
00079 
00080     /// IndirectGlobals - The memory pointed to by this global is known to be
00081     /// 'owned' by the global.
00082     std::set<const GlobalValue*> IndirectGlobals;
00083 
00084     /// AllocsForIndirectGlobals - If an instruction allocates memory for an
00085     /// indirect global, this map indicates which one.
00086     std::map<const Value*, const GlobalValue*> AllocsForIndirectGlobals;
00087 
00088     /// FunctionInfo - For each function, keep track of what globals are
00089     /// modified or read.
00090     std::map<const Function*, FunctionRecord> FunctionInfo;
00091 
00092   public:
00093     static char ID;
00094     GlobalsModRef() : ModulePass(ID) {
00095       initializeGlobalsModRefPass(*PassRegistry::getPassRegistry());
00096     }
00097 
00098     bool runOnModule(Module &M) override {
00099       InitializeAliasAnalysis(this);
00100 
00101       // Find non-addr taken globals.
00102       AnalyzeGlobals(M);
00103 
00104       // Propagate on CG.
00105       AnalyzeCallGraph(getAnalysis<CallGraphWrapperPass>().getCallGraph(), M);
00106       return false;
00107     }
00108 
00109     void getAnalysisUsage(AnalysisUsage &AU) const override {
00110       AliasAnalysis::getAnalysisUsage(AU);
00111       AU.addRequired<CallGraphWrapperPass>();
00112       AU.setPreservesAll();                         // Does not transform code
00113     }
00114 
00115     //------------------------------------------------
00116     // Implement the AliasAnalysis API
00117     //
00118     AliasResult alias(const Location &LocA, const Location &LocB) override;
00119     ModRefResult getModRefInfo(ImmutableCallSite CS,
00120                                const Location &Loc) override;
00121     ModRefResult getModRefInfo(ImmutableCallSite CS1,
00122                                ImmutableCallSite CS2) override {
00123       return AliasAnalysis::getModRefInfo(CS1, CS2);
00124     }
00125 
00126     /// getModRefBehavior - Return the behavior of the specified function if
00127     /// called from the specified call site.  The call site may be null in which
00128     /// case the most generic behavior of this function should be returned.
00129     ModRefBehavior getModRefBehavior(const Function *F) override {
00130       ModRefBehavior Min = UnknownModRefBehavior;
00131 
00132       if (FunctionRecord *FR = getFunctionInfo(F)) {
00133         if (FR->FunctionEffect == 0)
00134           Min = DoesNotAccessMemory;
00135         else if ((FR->FunctionEffect & Mod) == 0)
00136           Min = OnlyReadsMemory;
00137       }
00138 
00139       return ModRefBehavior(AliasAnalysis::getModRefBehavior(F) & Min);
00140     }
00141     
00142     /// getModRefBehavior - Return the behavior of the specified function if
00143     /// called from the specified call site.  The call site may be null in which
00144     /// case the most generic behavior of this function should be returned.
00145     ModRefBehavior getModRefBehavior(ImmutableCallSite CS) override {
00146       ModRefBehavior Min = UnknownModRefBehavior;
00147 
00148       if (const Function* F = CS.getCalledFunction())
00149         if (FunctionRecord *FR = getFunctionInfo(F)) {
00150           if (FR->FunctionEffect == 0)
00151             Min = DoesNotAccessMemory;
00152           else if ((FR->FunctionEffect & Mod) == 0)
00153             Min = OnlyReadsMemory;
00154         }
00155 
00156       return ModRefBehavior(AliasAnalysis::getModRefBehavior(CS) & Min);
00157     }
00158 
00159     void deleteValue(Value *V) override;
00160     void copyValue(Value *From, Value *To) override;
00161     void addEscapingUse(Use &U) override;
00162 
00163     /// getAdjustedAnalysisPointer - This method is used when a pass implements
00164     /// an analysis interface through multiple inheritance.  If needed, it
00165     /// should override this to adjust the this pointer as needed for the
00166     /// specified pass info.
00167     void *getAdjustedAnalysisPointer(AnalysisID PI) override {
00168       if (PI == &AliasAnalysis::ID)
00169         return (AliasAnalysis*)this;
00170       return this;
00171     }
00172     
00173   private:
00174     /// getFunctionInfo - Return the function info for the function, or null if
00175     /// we don't have anything useful to say about it.
00176     FunctionRecord *getFunctionInfo(const Function *F) {
00177       std::map<const Function*, FunctionRecord>::iterator I =
00178         FunctionInfo.find(F);
00179       if (I != FunctionInfo.end())
00180         return &I->second;
00181       return nullptr;
00182     }
00183 
00184     void AnalyzeGlobals(Module &M);
00185     void AnalyzeCallGraph(CallGraph &CG, Module &M);
00186     bool AnalyzeUsesOfPointer(Value *V, std::vector<Function*> &Readers,
00187                               std::vector<Function*> &Writers,
00188                               GlobalValue *OkayStoreDest = nullptr);
00189     bool AnalyzeIndirectGlobalMemory(GlobalValue *GV);
00190   };
00191 }
00192 
00193 char GlobalsModRef::ID = 0;
00194 INITIALIZE_AG_PASS_BEGIN(GlobalsModRef, AliasAnalysis,
00195                 "globalsmodref-aa", "Simple mod/ref analysis for globals",    
00196                 false, true, false)
00197 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
00198 INITIALIZE_AG_PASS_END(GlobalsModRef, AliasAnalysis,
00199                 "globalsmodref-aa", "Simple mod/ref analysis for globals",    
00200                 false, true, false)
00201 
00202 Pass *llvm::createGlobalsModRefPass() { return new GlobalsModRef(); }
00203 
00204 /// AnalyzeGlobals - Scan through the users of all of the internal
00205 /// GlobalValue's in the program.  If none of them have their "address taken"
00206 /// (really, their address passed to something nontrivial), record this fact,
00207 /// and record the functions that they are used directly in.
00208 void GlobalsModRef::AnalyzeGlobals(Module &M) {
00209   std::vector<Function*> Readers, Writers;
00210   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
00211     if (I->hasLocalLinkage()) {
00212       if (!AnalyzeUsesOfPointer(I, Readers, Writers)) {
00213         // Remember that we are tracking this global.
00214         NonAddressTakenGlobals.insert(I);
00215         ++NumNonAddrTakenFunctions;
00216       }
00217       Readers.clear(); Writers.clear();
00218     }
00219 
00220   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
00221        I != E; ++I)
00222     if (I->hasLocalLinkage()) {
00223       if (!AnalyzeUsesOfPointer(I, Readers, Writers)) {
00224         // Remember that we are tracking this global, and the mod/ref fns
00225         NonAddressTakenGlobals.insert(I);
00226 
00227         for (unsigned i = 0, e = Readers.size(); i != e; ++i)
00228           FunctionInfo[Readers[i]].GlobalInfo[I] |= Ref;
00229 
00230         if (!I->isConstant())  // No need to keep track of writers to constants
00231           for (unsigned i = 0, e = Writers.size(); i != e; ++i)
00232             FunctionInfo[Writers[i]].GlobalInfo[I] |= Mod;
00233         ++NumNonAddrTakenGlobalVars;
00234 
00235         // If this global holds a pointer type, see if it is an indirect global.
00236         if (I->getType()->getElementType()->isPointerTy() &&
00237             AnalyzeIndirectGlobalMemory(I))
00238           ++NumIndirectGlobalVars;
00239       }
00240       Readers.clear(); Writers.clear();
00241     }
00242 }
00243 
00244 /// AnalyzeUsesOfPointer - Look at all of the users of the specified pointer.
00245 /// If this is used by anything complex (i.e., the address escapes), return
00246 /// true.  Also, while we are at it, keep track of those functions that read and
00247 /// write to the value.
00248 ///
00249 /// If OkayStoreDest is non-null, stores into this global are allowed.
00250 bool GlobalsModRef::AnalyzeUsesOfPointer(Value *V,
00251                                          std::vector<Function*> &Readers,
00252                                          std::vector<Function*> &Writers,
00253                                          GlobalValue *OkayStoreDest) {
00254   if (!V->getType()->isPointerTy()) return true;
00255 
00256   for (Use &U : V->uses()) {
00257     User *I = U.getUser();
00258     if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
00259       Readers.push_back(LI->getParent()->getParent());
00260     } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
00261       if (V == SI->getOperand(1)) {
00262         Writers.push_back(SI->getParent()->getParent());
00263       } else if (SI->getOperand(1) != OkayStoreDest) {
00264         return true;  // Storing the pointer
00265       }
00266     } else if (Operator::getOpcode(I) == Instruction::GetElementPtr) {
00267       if (AnalyzeUsesOfPointer(I, Readers, Writers))
00268         return true;
00269     } else if (Operator::getOpcode(I) == Instruction::BitCast) {
00270       if (AnalyzeUsesOfPointer(I, Readers, Writers, OkayStoreDest))
00271         return true;
00272     } else if (CallSite CS = I) {
00273       // Make sure that this is just the function being called, not that it is
00274       // passing into the function.
00275       if (!CS.isCallee(&U)) {
00276         // Detect calls to free.
00277         if (isFreeCall(I, TLI))
00278           Writers.push_back(CS->getParent()->getParent());
00279         else
00280           return true; // Argument of an unknown call.
00281       }
00282     } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(I)) {
00283       if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
00284         return true;  // Allow comparison against null.
00285     } else {
00286       return true;
00287     }
00288   }
00289 
00290   return false;
00291 }
00292 
00293 /// AnalyzeIndirectGlobalMemory - We found an non-address-taken global variable
00294 /// which holds a pointer type.  See if the global always points to non-aliased
00295 /// heap memory: that is, all initializers of the globals are allocations, and
00296 /// those allocations have no use other than initialization of the global.
00297 /// Further, all loads out of GV must directly use the memory, not store the
00298 /// pointer somewhere.  If this is true, we consider the memory pointed to by
00299 /// GV to be owned by GV and can disambiguate other pointers from it.
00300 bool GlobalsModRef::AnalyzeIndirectGlobalMemory(GlobalValue *GV) {
00301   // Keep track of values related to the allocation of the memory, f.e. the
00302   // value produced by the malloc call and any casts.
00303   std::vector<Value*> AllocRelatedValues;
00304 
00305   // Walk the user list of the global.  If we find anything other than a direct
00306   // load or store, bail out.
00307   for (User *U : GV->users()) {
00308     if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
00309       // The pointer loaded from the global can only be used in simple ways:
00310       // we allow addressing of it and loading storing to it.  We do *not* allow
00311       // storing the loaded pointer somewhere else or passing to a function.
00312       std::vector<Function*> ReadersWriters;
00313       if (AnalyzeUsesOfPointer(LI, ReadersWriters, ReadersWriters))
00314         return false;  // Loaded pointer escapes.
00315       // TODO: Could try some IP mod/ref of the loaded pointer.
00316     } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
00317       // Storing the global itself.
00318       if (SI->getOperand(0) == GV) return false;
00319 
00320       // If storing the null pointer, ignore it.
00321       if (isa<ConstantPointerNull>(SI->getOperand(0)))
00322         continue;
00323 
00324       // Check the value being stored.
00325       Value *Ptr = GetUnderlyingObject(SI->getOperand(0));
00326 
00327       if (!isAllocLikeFn(Ptr, TLI))
00328         return false;  // Too hard to analyze.
00329 
00330       // Analyze all uses of the allocation.  If any of them are used in a
00331       // non-simple way (e.g. stored to another global) bail out.
00332       std::vector<Function*> ReadersWriters;
00333       if (AnalyzeUsesOfPointer(Ptr, ReadersWriters, ReadersWriters, GV))
00334         return false;  // Loaded pointer escapes.
00335 
00336       // Remember that this allocation is related to the indirect global.
00337       AllocRelatedValues.push_back(Ptr);
00338     } else {
00339       // Something complex, bail out.
00340       return false;
00341     }
00342   }
00343 
00344   // Okay, this is an indirect global.  Remember all of the allocations for
00345   // this global in AllocsForIndirectGlobals.
00346   while (!AllocRelatedValues.empty()) {
00347     AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV;
00348     AllocRelatedValues.pop_back();
00349   }
00350   IndirectGlobals.insert(GV);
00351   return true;
00352 }
00353 
00354 /// AnalyzeCallGraph - At this point, we know the functions where globals are
00355 /// immediately stored to and read from.  Propagate this information up the call
00356 /// graph to all callers and compute the mod/ref info for all memory for each
00357 /// function.
00358 void GlobalsModRef::AnalyzeCallGraph(CallGraph &CG, Module &M) {
00359   // We do a bottom-up SCC traversal of the call graph.  In other words, we
00360   // visit all callees before callers (leaf-first).
00361   for (scc_iterator<CallGraph*> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
00362     const std::vector<CallGraphNode *> &SCC = *I;
00363     assert(!SCC.empty() && "SCC with no functions?");
00364 
00365     if (!SCC[0]->getFunction()) {
00366       // Calls externally - can't say anything useful.  Remove any existing
00367       // function records (may have been created when scanning globals).
00368       for (unsigned i = 0, e = SCC.size(); i != e; ++i)
00369         FunctionInfo.erase(SCC[i]->getFunction());
00370       continue;
00371     }
00372 
00373     FunctionRecord &FR = FunctionInfo[SCC[0]->getFunction()];
00374 
00375     bool KnowNothing = false;
00376     unsigned FunctionEffect = 0;
00377 
00378     // Collect the mod/ref properties due to called functions.  We only compute
00379     // one mod-ref set.
00380     for (unsigned i = 0, e = SCC.size(); i != e && !KnowNothing; ++i) {
00381       Function *F = SCC[i]->getFunction();
00382       if (!F) {
00383         KnowNothing = true;
00384         break;
00385       }
00386 
00387       if (F->isDeclaration()) {
00388         // Try to get mod/ref behaviour from function attributes.
00389         if (F->doesNotAccessMemory()) {
00390           // Can't do better than that!
00391         } else if (F->onlyReadsMemory()) {
00392           FunctionEffect |= Ref;
00393           if (!F->isIntrinsic())
00394             // This function might call back into the module and read a global -
00395             // consider every global as possibly being read by this function.
00396             FR.MayReadAnyGlobal = true;
00397         } else {
00398           FunctionEffect |= ModRef;
00399           // Can't say anything useful unless it's an intrinsic - they don't
00400           // read or write global variables of the kind considered here.
00401           KnowNothing = !F->isIntrinsic();
00402         }
00403         continue;
00404       }
00405 
00406       for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end();
00407            CI != E && !KnowNothing; ++CI)
00408         if (Function *Callee = CI->second->getFunction()) {
00409           if (FunctionRecord *CalleeFR = getFunctionInfo(Callee)) {
00410             // Propagate function effect up.
00411             FunctionEffect |= CalleeFR->FunctionEffect;
00412 
00413             // Incorporate callee's effects on globals into our info.
00414             for (const auto &G : CalleeFR->GlobalInfo)
00415               FR.GlobalInfo[G.first] |= G.second;
00416             FR.MayReadAnyGlobal |= CalleeFR->MayReadAnyGlobal;
00417           } else {
00418             // Can't say anything about it.  However, if it is inside our SCC,
00419             // then nothing needs to be done.
00420             CallGraphNode *CalleeNode = CG[Callee];
00421             if (std::find(SCC.begin(), SCC.end(), CalleeNode) == SCC.end())
00422               KnowNothing = true;
00423           }
00424         } else {
00425           KnowNothing = true;
00426         }
00427     }
00428 
00429     // If we can't say anything useful about this SCC, remove all SCC functions
00430     // from the FunctionInfo map.
00431     if (KnowNothing) {
00432       for (unsigned i = 0, e = SCC.size(); i != e; ++i)
00433         FunctionInfo.erase(SCC[i]->getFunction());
00434       continue;
00435     }
00436 
00437     // Scan the function bodies for explicit loads or stores.
00438     for (unsigned i = 0, e = SCC.size(); i != e && FunctionEffect != ModRef;++i)
00439       for (inst_iterator II = inst_begin(SCC[i]->getFunction()),
00440              E = inst_end(SCC[i]->getFunction());
00441            II != E && FunctionEffect != ModRef; ++II)
00442         if (LoadInst *LI = dyn_cast<LoadInst>(&*II)) {
00443           FunctionEffect |= Ref;
00444           if (LI->isVolatile())
00445             // Volatile loads may have side-effects, so mark them as writing
00446             // memory (for example, a flag inside the processor).
00447             FunctionEffect |= Mod;
00448         } else if (StoreInst *SI = dyn_cast<StoreInst>(&*II)) {
00449           FunctionEffect |= Mod;
00450           if (SI->isVolatile())
00451             // Treat volatile stores as reading memory somewhere.
00452             FunctionEffect |= Ref;
00453         } else if (isAllocationFn(&*II, TLI) || isFreeCall(&*II, TLI)) {
00454           FunctionEffect |= ModRef;
00455         } else if (IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(&*II)) {
00456           // The callgraph doesn't include intrinsic calls.
00457           Function *Callee = Intrinsic->getCalledFunction();
00458           ModRefBehavior Behaviour = AliasAnalysis::getModRefBehavior(Callee);
00459           FunctionEffect |= (Behaviour & ModRef);
00460         }
00461 
00462     if ((FunctionEffect & Mod) == 0)
00463       ++NumReadMemFunctions;
00464     if (FunctionEffect == 0)
00465       ++NumNoMemFunctions;
00466     FR.FunctionEffect = FunctionEffect;
00467 
00468     // Finally, now that we know the full effect on this SCC, clone the
00469     // information to each function in the SCC.
00470     for (unsigned i = 1, e = SCC.size(); i != e; ++i)
00471       FunctionInfo[SCC[i]->getFunction()] = FR;
00472   }
00473 }
00474 
00475 
00476 
00477 /// alias - If one of the pointers is to a global that we are tracking, and the
00478 /// other is some random pointer, we know there cannot be an alias, because the
00479 /// address of the global isn't taken.
00480 AliasAnalysis::AliasResult
00481 GlobalsModRef::alias(const Location &LocA,
00482                      const Location &LocB) {
00483   // Get the base object these pointers point to.
00484   const Value *UV1 = GetUnderlyingObject(LocA.Ptr);
00485   const Value *UV2 = GetUnderlyingObject(LocB.Ptr);
00486 
00487   // If either of the underlying values is a global, they may be non-addr-taken
00488   // globals, which we can answer queries about.
00489   const GlobalValue *GV1 = dyn_cast<GlobalValue>(UV1);
00490   const GlobalValue *GV2 = dyn_cast<GlobalValue>(UV2);
00491   if (GV1 || GV2) {
00492     // If the global's address is taken, pretend we don't know it's a pointer to
00493     // the global.
00494     if (GV1 && !NonAddressTakenGlobals.count(GV1)) GV1 = nullptr;
00495     if (GV2 && !NonAddressTakenGlobals.count(GV2)) GV2 = nullptr;
00496 
00497     // If the two pointers are derived from two different non-addr-taken
00498     // globals, or if one is and the other isn't, we know these can't alias.
00499     if ((GV1 || GV2) && GV1 != GV2)
00500       return NoAlias;
00501 
00502     // Otherwise if they are both derived from the same addr-taken global, we
00503     // can't know the two accesses don't overlap.
00504   }
00505 
00506   // These pointers may be based on the memory owned by an indirect global.  If
00507   // so, we may be able to handle this.  First check to see if the base pointer
00508   // is a direct load from an indirect global.
00509   GV1 = GV2 = nullptr;
00510   if (const LoadInst *LI = dyn_cast<LoadInst>(UV1))
00511     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
00512       if (IndirectGlobals.count(GV))
00513         GV1 = GV;
00514   if (const LoadInst *LI = dyn_cast<LoadInst>(UV2))
00515     if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
00516       if (IndirectGlobals.count(GV))
00517         GV2 = GV;
00518 
00519   // These pointers may also be from an allocation for the indirect global.  If
00520   // so, also handle them.
00521   if (AllocsForIndirectGlobals.count(UV1))
00522     GV1 = AllocsForIndirectGlobals[UV1];
00523   if (AllocsForIndirectGlobals.count(UV2))
00524     GV2 = AllocsForIndirectGlobals[UV2];
00525 
00526   // Now that we know whether the two pointers are related to indirect globals,
00527   // use this to disambiguate the pointers.  If either pointer is based on an
00528   // indirect global and if they are not both based on the same indirect global,
00529   // they cannot alias.
00530   if ((GV1 || GV2) && GV1 != GV2)
00531     return NoAlias;
00532 
00533   return AliasAnalysis::alias(LocA, LocB);
00534 }
00535 
00536 AliasAnalysis::ModRefResult
00537 GlobalsModRef::getModRefInfo(ImmutableCallSite CS,
00538                              const Location &Loc) {
00539   unsigned Known = ModRef;
00540 
00541   // If we are asking for mod/ref info of a direct call with a pointer to a
00542   // global we are tracking, return information if we have it.
00543   if (const GlobalValue *GV =
00544         dyn_cast<GlobalValue>(GetUnderlyingObject(Loc.Ptr)))
00545     if (GV->hasLocalLinkage())
00546       if (const Function *F = CS.getCalledFunction())
00547         if (NonAddressTakenGlobals.count(GV))
00548           if (const FunctionRecord *FR = getFunctionInfo(F))
00549             Known = FR->getInfoForGlobal(GV);
00550 
00551   if (Known == NoModRef)
00552     return NoModRef; // No need to query other mod/ref analyses
00553   return ModRefResult(Known & AliasAnalysis::getModRefInfo(CS, Loc));
00554 }
00555 
00556 
00557 //===----------------------------------------------------------------------===//
00558 // Methods to update the analysis as a result of the client transformation.
00559 //
00560 void GlobalsModRef::deleteValue(Value *V) {
00561   if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
00562     if (NonAddressTakenGlobals.erase(GV)) {
00563       // This global might be an indirect global.  If so, remove it and remove
00564       // any AllocRelatedValues for it.
00565       if (IndirectGlobals.erase(GV)) {
00566         // Remove any entries in AllocsForIndirectGlobals for this global.
00567         for (std::map<const Value*, const GlobalValue*>::iterator
00568              I = AllocsForIndirectGlobals.begin(),
00569              E = AllocsForIndirectGlobals.end(); I != E; ) {
00570           if (I->second == GV) {
00571             AllocsForIndirectGlobals.erase(I++);
00572           } else {
00573             ++I;
00574           }
00575         }
00576       }
00577     }
00578   }
00579 
00580   // Otherwise, if this is an allocation related to an indirect global, remove
00581   // it.
00582   AllocsForIndirectGlobals.erase(V);
00583 
00584   AliasAnalysis::deleteValue(V);
00585 }
00586 
00587 void GlobalsModRef::copyValue(Value *From, Value *To) {
00588   AliasAnalysis::copyValue(From, To);
00589 }
00590 
00591 void GlobalsModRef::addEscapingUse(Use &U) {
00592   // For the purposes of this analysis, it is conservatively correct to treat
00593   // a newly escaping value equivalently to a deleted one.  We could perhaps
00594   // be more precise by processing the new use and attempting to update our
00595   // saved analysis results to accommodate it.
00596   deleteValue(U);
00597   
00598   AliasAnalysis::addEscapingUse(U);
00599 }