LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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 #define DEBUG_TYPE "globalsmodref-aa"
00018 #include "llvm/Analysis/Passes.h"
00019 #include "llvm/Module.h"
00020 #include "llvm/Pass.h"
00021 #include "llvm/Instructions.h"
00022 #include "llvm/Constants.h"
00023 #include "llvm/DerivedTypes.h"
00024 #include "llvm/Analysis/AliasAnalysis.h"
00025 #include "llvm/Analysis/CallGraph.h"
00026 #include "llvm/Support/Compiler.h"
00027 #include "llvm/Support/CommandLine.h"
00028 #include "llvm/Support/InstIterator.h"
00029 #include "llvm/ADT/Statistic.h"
00030 #include "llvm/ADT/SCCIterator.h"
00031 #include <set>
00032 using namespace llvm;
00033 
00034 STATISTIC(NumNonAddrTakenGlobalVars,
00035           "Number of global vars without address taken");
00036 STATISTIC(NumNonAddrTakenFunctions,"Number of functions without address taken");
00037 STATISTIC(NumNoMemFunctions, "Number of functions that do not access memory");
00038 STATISTIC(NumReadMemFunctions, "Number of functions that only read memory");
00039 STATISTIC(NumIndirectGlobalVars, "Number of indirect global objects");
00040 
00041 namespace {
00042   /// FunctionRecord - One instance of this structure is stored for every
00043   /// function in the program.  Later, the entries for these functions are
00044   /// removed if the function is found to call an external function (in which
00045   /// case we know nothing about it.
00046   struct VISIBILITY_HIDDEN FunctionRecord {
00047     /// GlobalInfo - Maintain mod/ref info for all of the globals without
00048     /// addresses taken that are read or written (transitively) by this
00049     /// function.
00050     std::map<GlobalValue*, unsigned> GlobalInfo;
00051 
00052     unsigned getInfoForGlobal(GlobalValue *GV) const {
00053       std::map<GlobalValue*, unsigned>::const_iterator I = GlobalInfo.find(GV);
00054       if (I != GlobalInfo.end())
00055         return I->second;
00056       return 0;
00057     }
00058 
00059     /// FunctionEffect - Capture whether or not this function reads or writes to
00060     /// ANY memory.  If not, we can do a lot of aggressive analysis on it.
00061     unsigned FunctionEffect;
00062 
00063     FunctionRecord() : FunctionEffect(0) {}
00064   };
00065 
00066   /// GlobalsModRef - The actual analysis pass.
00067   class VISIBILITY_HIDDEN GlobalsModRef 
00068       : public ModulePass, public AliasAnalysis {
00069     /// NonAddressTakenGlobals - The globals that do not have their addresses
00070     /// taken.
00071     std::set<GlobalValue*> NonAddressTakenGlobals;
00072 
00073     /// IndirectGlobals - The memory pointed to by this global is known to be
00074     /// 'owned' by the global.
00075     std::set<GlobalValue*> IndirectGlobals;
00076     
00077     /// AllocsForIndirectGlobals - If an instruction allocates memory for an
00078     /// indirect global, this map indicates which one.
00079     std::map<Value*, GlobalValue*> AllocsForIndirectGlobals;
00080     
00081     /// FunctionInfo - For each function, keep track of what globals are
00082     /// modified or read.
00083     std::map<Function*, FunctionRecord> FunctionInfo;
00084 
00085   public:
00086     static char ID;
00087     GlobalsModRef() : ModulePass((intptr_t)&ID) {}
00088 
00089     bool runOnModule(Module &M) {
00090       InitializeAliasAnalysis(this);                 // set up super class
00091       AnalyzeGlobals(M);                          // find non-addr taken globals
00092       AnalyzeCallGraph(getAnalysis<CallGraph>(), M); // Propagate on CG
00093       return false;
00094     }
00095 
00096     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00097       AliasAnalysis::getAnalysisUsage(AU);
00098       AU.addRequired<CallGraph>();
00099       AU.setPreservesAll();                         // Does not transform code
00100     }
00101 
00102     //------------------------------------------------
00103     // Implement the AliasAnalysis API
00104     //
00105     AliasResult alias(const Value *V1, unsigned V1Size,
00106                       const Value *V2, unsigned V2Size);
00107     ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
00108     ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
00109       return AliasAnalysis::getModRefInfo(CS1,CS2);
00110     }
00111     bool hasNoModRefInfoForCalls() const { return false; }
00112 
00113     /// getModRefBehavior - Return the behavior of the specified function if
00114     /// called from the specified call site.  The call site may be null in which
00115     /// case the most generic behavior of this function should be returned.
00116     virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS,
00117                                          std::vector<PointerAccessInfo> *Info) {
00118       if (FunctionRecord *FR = getFunctionInfo(F)) {
00119         if (FR->FunctionEffect == 0)
00120           return DoesNotAccessMemory;
00121         else if ((FR->FunctionEffect & Mod) == 0)
00122           return OnlyReadsMemory;
00123       }
00124       return AliasAnalysis::getModRefBehavior(F, CS, Info);
00125     }
00126 
00127     virtual void deleteValue(Value *V);
00128     virtual void copyValue(Value *From, Value *To);
00129 
00130   private:
00131     /// getFunctionInfo - Return the function info for the function, or null if
00132     /// the function calls an external function (in which case we don't have
00133     /// anything useful to say about it).
00134     FunctionRecord *getFunctionInfo(Function *F) {
00135       std::map<Function*, FunctionRecord>::iterator I = FunctionInfo.find(F);
00136       if (I != FunctionInfo.end())
00137         return &I->second;
00138       return 0;
00139     }
00140 
00141     void AnalyzeGlobals(Module &M);
00142     void AnalyzeCallGraph(CallGraph &CG, Module &M);
00143     void AnalyzeSCC(std::vector<CallGraphNode *> &SCC);
00144     bool AnalyzeUsesOfPointer(Value *V, std::vector<Function*> &Readers,
00145                               std::vector<Function*> &Writers,
00146                               GlobalValue *OkayStoreDest = 0);
00147     bool AnalyzeIndirectGlobalMemory(GlobalValue *GV);
00148   };
00149 }
00150 
00151 char GlobalsModRef::ID = 0;
00152 static RegisterPass<GlobalsModRef>
00153 X("globalsmodref-aa", "Simple mod/ref analysis for globals", false, true);
00154 static RegisterAnalysisGroup<AliasAnalysis> Y(X);
00155 
00156 Pass *llvm::createGlobalsModRefPass() { return new GlobalsModRef(); }
00157 
00158 /// getUnderlyingObject - This traverses the use chain to figure out what object
00159 /// the specified value points to.  If the value points to, or is derived from,
00160 /// a global object, return it.
00161 static Value *getUnderlyingObject(Value *V) {
00162   if (!isa<PointerType>(V->getType())) return V;
00163   
00164   // If we are at some type of object... return it.
00165   if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV;
00166   
00167   // Traverse through different addressing mechanisms.
00168   if (Instruction *I = dyn_cast<Instruction>(V)) {
00169     if (isa<BitCastInst>(I) || isa<GetElementPtrInst>(I))
00170       return getUnderlyingObject(I->getOperand(0));
00171   } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
00172     if (CE->getOpcode() == Instruction::BitCast || 
00173         CE->getOpcode() == Instruction::GetElementPtr)
00174       return getUnderlyingObject(CE->getOperand(0));
00175   }
00176   
00177   // Othewise, we don't know what this is, return it as the base pointer.
00178   return V;
00179 }
00180 
00181 /// AnalyzeGlobals - Scan through the users of all of the internal
00182 /// GlobalValue's in the program.  If none of them have their "Address taken"
00183 /// (really, their address passed to something nontrivial), record this fact,
00184 /// and record the functions that they are used directly in.
00185 void GlobalsModRef::AnalyzeGlobals(Module &M) {
00186   std::vector<Function*> Readers, Writers;
00187   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
00188     if (I->hasInternalLinkage()) {
00189       if (!AnalyzeUsesOfPointer(I, Readers, Writers)) {
00190         // Remember that we are tracking this global.
00191         NonAddressTakenGlobals.insert(I);
00192         ++NumNonAddrTakenFunctions;
00193       }
00194       Readers.clear(); Writers.clear();
00195     }
00196 
00197   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
00198        I != E; ++I)
00199     if (I->hasInternalLinkage()) {
00200       if (!AnalyzeUsesOfPointer(I, Readers, Writers)) {
00201         // Remember that we are tracking this global, and the mod/ref fns
00202         NonAddressTakenGlobals.insert(I);
00203         for (unsigned i = 0, e = Readers.size(); i != e; ++i)
00204           FunctionInfo[Readers[i]].GlobalInfo[I] |= Ref;
00205 
00206         if (!I->isConstant())  // No need to keep track of writers to constants
00207           for (unsigned i = 0, e = Writers.size(); i != e; ++i)
00208             FunctionInfo[Writers[i]].GlobalInfo[I] |= Mod;
00209         ++NumNonAddrTakenGlobalVars;
00210         
00211         // If this global holds a pointer type, see if it is an indirect global.
00212         if (isa<PointerType>(I->getType()->getElementType()) &&
00213             AnalyzeIndirectGlobalMemory(I))
00214           ++NumIndirectGlobalVars;
00215       }
00216       Readers.clear(); Writers.clear();
00217     }
00218 }
00219 
00220 /// AnalyzeUsesOfPointer - Look at all of the users of the specified pointer.
00221 /// If this is used by anything complex (i.e., the address escapes), return
00222 /// true.  Also, while we are at it, keep track of those functions that read and
00223 /// write to the value.
00224 ///
00225 /// If OkayStoreDest is non-null, stores into this global are allowed.
00226 bool GlobalsModRef::AnalyzeUsesOfPointer(Value *V,
00227                                          std::vector<Function*> &Readers,
00228                                          std::vector<Function*> &Writers,
00229                                          GlobalValue *OkayStoreDest) {
00230   if (!isa<PointerType>(V->getType())) return true;
00231 
00232   for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI)
00233     if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
00234       Readers.push_back(LI->getParent()->getParent());
00235     } else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
00236       if (V == SI->getOperand(1)) {
00237         Writers.push_back(SI->getParent()->getParent());
00238       } else if (SI->getOperand(1) != OkayStoreDest) {
00239         return true;  // Storing the pointer
00240       }
00241     } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) {
00242       if (AnalyzeUsesOfPointer(GEP, Readers, Writers)) return true;
00243     } else if (CallInst *CI = dyn_cast<CallInst>(*UI)) {
00244       // Make sure that this is just the function being called, not that it is
00245       // passing into the function.
00246       for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i)
00247         if (CI->getOperand(i) == V) return true;
00248     } else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) {
00249       // Make sure that this is just the function being called, not that it is
00250       // passing into the function.
00251       for (unsigned i = 3, e = II->getNumOperands(); i != e; ++i)
00252         if (II->getOperand(i) == V) return true;
00253     } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(*UI)) {
00254       if (CE->getOpcode() == Instruction::GetElementPtr || 
00255           CE->getOpcode() == Instruction::BitCast) {
00256         if (AnalyzeUsesOfPointer(CE, Readers, Writers))
00257           return true;
00258       } else {
00259         return true;
00260       }
00261     } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(*UI)) {
00262       if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
00263         return true;  // Allow comparison against null.
00264     } else if (FreeInst *F = dyn_cast<FreeInst>(*UI)) {
00265       Writers.push_back(F->getParent()->getParent());
00266     } else {
00267       return true;
00268     }
00269   return false;
00270 }
00271 
00272 /// AnalyzeIndirectGlobalMemory - We found an non-address-taken global variable
00273 /// which holds a pointer type.  See if the global always points to non-aliased
00274 /// heap memory: that is, all initializers of the globals are allocations, and
00275 /// those allocations have no use other than initialization of the global.
00276 /// Further, all loads out of GV must directly use the memory, not store the
00277 /// pointer somewhere.  If this is true, we consider the memory pointed to by
00278 /// GV to be owned by GV and can disambiguate other pointers from it.
00279 bool GlobalsModRef::AnalyzeIndirectGlobalMemory(GlobalValue *GV) {
00280   // Keep track of values related to the allocation of the memory, f.e. the
00281   // value produced by the malloc call and any casts.
00282   std::vector<Value*> AllocRelatedValues;
00283   
00284   // Walk the user list of the global.  If we find anything other than a direct
00285   // load or store, bail out.
00286   for (Value::use_iterator I = GV->use_begin(), E = GV->use_end(); I != E; ++I){
00287     if (LoadInst *LI = dyn_cast<LoadInst>(*I)) {
00288       // The pointer loaded from the global can only be used in simple ways:
00289       // we allow addressing of it and loading storing to it.  We do *not* allow
00290       // storing the loaded pointer somewhere else or passing to a function.
00291       std::vector<Function*> ReadersWriters;
00292       if (AnalyzeUsesOfPointer(LI, ReadersWriters, ReadersWriters))
00293         return false;  // Loaded pointer escapes.
00294       // TODO: Could try some IP mod/ref of the loaded pointer.
00295     } else if (StoreInst *SI = dyn_cast<StoreInst>(*I)) {
00296       // Storing the global itself.
00297       if (SI->getOperand(0) == GV) return false;
00298       
00299       // If storing the null pointer, ignore it.
00300       if (isa<ConstantPointerNull>(SI->getOperand(0)))
00301         continue;
00302       
00303       // Check the value being stored.
00304       Value *Ptr = getUnderlyingObject(SI->getOperand(0));
00305 
00306       if (isa<MallocInst>(Ptr)) {
00307         // Okay, easy case.
00308       } else if (CallInst *CI = dyn_cast<CallInst>(Ptr)) {
00309         Function *F = CI->getCalledFunction();
00310         if (!F || !F->isDeclaration()) return false;     // Too hard to analyze.
00311         if (F->getName() != "calloc") return false;   // Not calloc.
00312       } else {
00313         return false;  // Too hard to analyze.
00314       }
00315       
00316       // Analyze all uses of the allocation.  If any of them are used in a
00317       // non-simple way (e.g. stored to another global) bail out.
00318       std::vector<Function*> ReadersWriters;
00319       if (AnalyzeUsesOfPointer(Ptr, ReadersWriters, ReadersWriters, GV))
00320         return false;  // Loaded pointer escapes.
00321 
00322       // Remember that this allocation is related to the indirect global.
00323       AllocRelatedValues.push_back(Ptr);
00324     } else {
00325       // Something complex, bail out.
00326       return false;
00327     }
00328   }
00329   
00330   // Okay, this is an indirect global.  Remember all of the allocations for
00331   // this global in AllocsForIndirectGlobals.
00332   while (!AllocRelatedValues.empty()) {
00333     AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV;
00334     AllocRelatedValues.pop_back();
00335   }
00336   IndirectGlobals.insert(GV);
00337   return true;
00338 }
00339 
00340 /// AnalyzeCallGraph - At this point, we know the functions where globals are
00341 /// immediately stored to and read from.  Propagate this information up the call
00342 /// graph to all callers and compute the mod/ref info for all memory for each
00343 /// function.
00344 void GlobalsModRef::AnalyzeCallGraph(CallGraph &CG, Module &M) {
00345   // We do a bottom-up SCC traversal of the call graph.  In other words, we
00346   // visit all callees before callers (leaf-first).
00347   for (scc_iterator<CallGraph*> I = scc_begin(&CG), E = scc_end(&CG); I!=E; ++I)
00348     if ((*I).size() != 1) {
00349       AnalyzeSCC(*I);
00350     } else if (Function *F = (*I)[0]->getFunction()) {
00351       if (!F->isDeclaration()) {
00352         // Nonexternal function.
00353         AnalyzeSCC(*I);
00354       } else {
00355         // Otherwise external function.  Handle intrinsics and other special
00356         // cases here.
00357         if (getAnalysis<AliasAnalysis>().doesNotAccessMemory(F))
00358           // If it does not access memory, process the function, causing us to
00359           // realize it doesn't do anything (the body is empty).
00360           AnalyzeSCC(*I);
00361         else {
00362           // Otherwise, don't process it.  This will cause us to conservatively
00363           // assume the worst.
00364         }
00365       }
00366     } else {
00367       // Do not process the external node, assume the worst.
00368     }
00369 }
00370 
00371 void GlobalsModRef::AnalyzeSCC(std::vector<CallGraphNode *> &SCC) {
00372   assert(!SCC.empty() && "SCC with no functions?");
00373   FunctionRecord &FR = FunctionInfo[SCC[0]->getFunction()];
00374 
00375   bool CallsExternal = 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 && !CallsExternal; ++i)
00381     for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end();
00382          CI != E; ++CI)
00383       if (Function *Callee = CI->second->getFunction()) {
00384         if (FunctionRecord *CalleeFR = getFunctionInfo(Callee)) {
00385           // Propagate function effect up.
00386           FunctionEffect |= CalleeFR->FunctionEffect;
00387 
00388           // Incorporate callee's effects on globals into our info.
00389           for (std::map<GlobalValue*, unsigned>::iterator GI =
00390                  CalleeFR->GlobalInfo.begin(), E = CalleeFR->GlobalInfo.end();
00391                GI != E; ++GI)
00392             FR.GlobalInfo[GI->first] |= GI->second;
00393 
00394         } else {
00395           // Okay, if we can't say anything about it, maybe some other alias
00396           // analysis can.
00397           ModRefBehavior MRB =
00398             AliasAnalysis::getModRefBehavior(Callee);
00399           if (MRB != DoesNotAccessMemory) {
00400             // FIXME: could make this more aggressive for functions that just
00401             // read memory.  We should just say they read all globals.
00402             CallsExternal = true;
00403             break;
00404           }
00405         }
00406       } else {
00407         CallsExternal = true;
00408         break;
00409       }
00410 
00411   // If this SCC calls an external function, we can't say anything about it, so
00412   // remove all SCC functions from the FunctionInfo map.
00413   if (CallsExternal) {
00414     for (unsigned i = 0, e = SCC.size(); i != e; ++i)
00415       FunctionInfo.erase(SCC[i]->getFunction());
00416     return;
00417   }
00418 
00419   // Otherwise, unless we already know that this function mod/refs memory, scan
00420   // the function bodies to see if there are any explicit loads or stores.
00421   if (FunctionEffect != ModRef) {
00422     for (unsigned i = 0, e = SCC.size(); i != e && FunctionEffect != ModRef;++i)
00423       for (inst_iterator II = inst_begin(SCC[i]->getFunction()),
00424              E = inst_end(SCC[i]->getFunction());
00425            II != E && FunctionEffect != ModRef; ++II)
00426         if (isa<LoadInst>(*II))
00427           FunctionEffect |= Ref;
00428         else if (isa<StoreInst>(*II))
00429           FunctionEffect |= Mod;
00430         else if (isa<MallocInst>(*II) || isa<FreeInst>(*II))
00431           FunctionEffect |= ModRef;
00432   }
00433 
00434   if ((FunctionEffect & Mod) == 0)
00435     ++NumReadMemFunctions;
00436   if (FunctionEffect == 0)
00437     ++NumNoMemFunctions;
00438   FR.FunctionEffect = FunctionEffect;
00439 
00440   // Finally, now that we know the full effect on this SCC, clone the
00441   // information to each function in the SCC.
00442   for (unsigned i = 1, e = SCC.size(); i != e; ++i)
00443     FunctionInfo[SCC[i]->getFunction()] = FR;
00444 }
00445 
00446 
00447 
00448 /// alias - If one of the pointers is to a global that we are tracking, and the
00449 /// other is some random pointer, we know there cannot be an alias, because the
00450 /// address of the global isn't taken.
00451 AliasAnalysis::AliasResult
00452 GlobalsModRef::alias(const Value *V1, unsigned V1Size,
00453                      const Value *V2, unsigned V2Size) {
00454   // Get the base object these pointers point to.
00455   Value *UV1 = getUnderlyingObject(const_cast<Value*>(V1));
00456   Value *UV2 = getUnderlyingObject(const_cast<Value*>(V2));
00457   
00458   // If either of the underlying values is a global, they may be non-addr-taken
00459   // globals, which we can answer queries about.
00460   GlobalValue *GV1 = dyn_cast<GlobalValue>(UV1);
00461   GlobalValue *GV2 = dyn_cast<GlobalValue>(UV2);
00462   if (GV1 || GV2) {
00463     // If the global's address is taken, pretend we don't know it's a pointer to
00464     // the global.
00465     if (GV1 && !NonAddressTakenGlobals.count(GV1)) GV1 = 0;
00466     if (GV2 && !NonAddressTakenGlobals.count(GV2)) GV2 = 0;
00467 
00468     // If the the two pointers are derived from two different non-addr-taken
00469     // globals, or if one is and the other isn't, we know these can't alias.
00470     if ((GV1 || GV2) && GV1 != GV2)
00471       return NoAlias;
00472 
00473     // Otherwise if they are both derived from the same addr-taken global, we
00474     // can't know the two accesses don't overlap.
00475   }
00476   
00477   // These pointers may be based on the memory owned by an indirect global.  If
00478   // so, we may be able to handle this.  First check to see if the base pointer
00479   // is a direct load from an indirect global.
00480   GV1 = GV2 = 0;
00481   if (LoadInst *LI = dyn_cast<LoadInst>(UV1))
00482     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
00483       if (IndirectGlobals.count(GV))
00484         GV1 = GV;
00485   if (LoadInst *LI = dyn_cast<LoadInst>(UV2))
00486     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
00487       if (IndirectGlobals.count(GV))
00488         GV2 = GV;
00489   
00490   // These pointers may also be from an allocation for the indirect global.  If
00491   // so, also handle them.
00492   if (AllocsForIndirectGlobals.count(UV1))
00493     GV1 = AllocsForIndirectGlobals[UV1];
00494   if (AllocsForIndirectGlobals.count(UV2))
00495     GV2 = AllocsForIndirectGlobals[UV2];
00496   
00497   // Now that we know whether the two pointers are related to indirect globals,
00498   // use this to disambiguate the pointers.  If either pointer is based on an
00499   // indirect global and if they are not both based on the same indirect global,
00500   // they cannot alias.
00501   if ((GV1 || GV2) && GV1 != GV2)
00502     return NoAlias;
00503   
00504   return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
00505 }
00506 
00507 AliasAnalysis::ModRefResult
00508 GlobalsModRef::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
00509   unsigned Known = ModRef;
00510 
00511   // If we are asking for mod/ref info of a direct call with a pointer to a
00512   // global we are tracking, return information if we have it.
00513   if (GlobalValue *GV = dyn_cast<GlobalValue>(getUnderlyingObject(P)))
00514     if (GV->hasInternalLinkage())
00515       if (Function *F = CS.getCalledFunction())
00516         if (NonAddressTakenGlobals.count(GV))
00517           if (FunctionRecord *FR = getFunctionInfo(F))
00518             Known = FR->getInfoForGlobal(GV);
00519 
00520   if (Known == NoModRef)
00521     return NoModRef; // No need to query other mod/ref analyses
00522   return ModRefResult(Known & AliasAnalysis::getModRefInfo(CS, P, Size));
00523 }
00524 
00525 
00526 //===----------------------------------------------------------------------===//
00527 // Methods to update the analysis as a result of the client transformation.
00528 //
00529 void GlobalsModRef::deleteValue(Value *V) {
00530   if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
00531     if (NonAddressTakenGlobals.erase(GV)) {
00532       // This global might be an indirect global.  If so, remove it and remove
00533       // any AllocRelatedValues for it.
00534       if (IndirectGlobals.erase(GV)) {
00535         // Remove any entries in AllocsForIndirectGlobals for this global.
00536         for (std::map<Value*, GlobalValue*>::iterator
00537              I = AllocsForIndirectGlobals.begin(),
00538              E = AllocsForIndirectGlobals.end(); I != E; ) {
00539           if (I->second == GV) {
00540             AllocsForIndirectGlobals.erase(I++);
00541           } else {
00542             ++I;
00543           }
00544         }
00545       }
00546     }
00547   }
00548   
00549   // Otherwise, if this is an allocation related to an indirect global, remove
00550   // it.
00551   AllocsForIndirectGlobals.erase(V);
00552   
00553   AliasAnalysis::deleteValue(V);
00554 }
00555 
00556 void GlobalsModRef::copyValue(Value *From, Value *To) {
00557   AliasAnalysis::copyValue(From, To);
00558 }



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.