GlobalsModRef.cpp revision 1cee94f04111cfd7114979d6dfddce2669c9380d
13b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner//===- GlobalsModRef.cpp - Simple Mod/Ref Analysis for Globals ------------===//
22b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman//
33b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner//                     The LLVM Compiler Infrastructure
43b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
72b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman//
83b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner//===----------------------------------------------------------------------===//
93b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner//
103b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner// This simple pass provides alias and mod/ref information for global values
11fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner// that do not have their address taken, and keeps track of whether functions
12fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner// read or write memory (are "pure").  For this simple (but very common) case,
13fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner// we can provide pretty accurate and useful information.
143b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner//
153b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner//===----------------------------------------------------------------------===//
163b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
173b27d68c6af2582df0962557f1fe5c3f70f46e3fChris Lattner#define DEBUG_TYPE "globalsmodref-aa"
183b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include "llvm/Analysis/Passes.h"
193b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include "llvm/Module.h"
203b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include "llvm/Pass.h"
213b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include "llvm/Instructions.h"
223b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include "llvm/Constants.h"
23ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner#include "llvm/DerivedTypes.h"
243b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include "llvm/Analysis/AliasAnalysis.h"
253b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include "llvm/Analysis/CallGraph.h"
26d7d83db5f22d05e5b14b6b1d838668222113c83aReid Spencer#include "llvm/Support/Compiler.h"
27551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/CommandLine.h"
28d7d83db5f22d05e5b14b6b1d838668222113c83aReid Spencer#include "llvm/Support/InstIterator.h"
29551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/Statistic.h"
30551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/SCCIterator.h"
313b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include <set>
323b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnerusing namespace llvm;
333b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
343b27d68c6af2582df0962557f1fe5c3f70f46e3fChris LattnerSTATISTIC(NumNonAddrTakenGlobalVars,
353b27d68c6af2582df0962557f1fe5c3f70f46e3fChris Lattner          "Number of global vars without address taken");
363b27d68c6af2582df0962557f1fe5c3f70f46e3fChris LattnerSTATISTIC(NumNonAddrTakenFunctions,"Number of functions without address taken");
373b27d68c6af2582df0962557f1fe5c3f70f46e3fChris LattnerSTATISTIC(NumNoMemFunctions, "Number of functions that do not access memory");
383b27d68c6af2582df0962557f1fe5c3f70f46e3fChris LattnerSTATISTIC(NumReadMemFunctions, "Number of functions that only read memory");
393b27d68c6af2582df0962557f1fe5c3f70f46e3fChris LattnerSTATISTIC(NumIndirectGlobalVars, "Number of indirect global objects");
403b27d68c6af2582df0962557f1fe5c3f70f46e3fChris Lattner
413b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnernamespace {
42fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  /// FunctionRecord - One instance of this structure is stored for every
43fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  /// function in the program.  Later, the entries for these functions are
44fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  /// removed if the function is found to call an external function (in which
45fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  /// case we know nothing about it.
46d7d83db5f22d05e5b14b6b1d838668222113c83aReid Spencer  struct VISIBILITY_HIDDEN FunctionRecord {
47fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    /// GlobalInfo - Maintain mod/ref info for all of the globals without
48fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    /// addresses taken that are read or written (transitively) by this
49fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    /// function.
50fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    std::map<GlobalValue*, unsigned> GlobalInfo;
51fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner
52fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    unsigned getInfoForGlobal(GlobalValue *GV) const {
53fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner      std::map<GlobalValue*, unsigned>::const_iterator I = GlobalInfo.find(GV);
54fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner      if (I != GlobalInfo.end())
55fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        return I->second;
56fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner      return 0;
57fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    }
582b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman
59fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    /// FunctionEffect - Capture whether or not this function reads or writes to
60fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    /// ANY memory.  If not, we can do a lot of aggressive analysis on it.
61fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    unsigned FunctionEffect;
62105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner
63105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner    FunctionRecord() : FunctionEffect(0) {}
64fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  };
653b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
66fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  /// GlobalsModRef - The actual analysis pass.
67d7d83db5f22d05e5b14b6b1d838668222113c83aReid Spencer  class VISIBILITY_HIDDEN GlobalsModRef
68d7d83db5f22d05e5b14b6b1d838668222113c83aReid Spencer      : public ModulePass, public AliasAnalysis {
69fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    /// NonAddressTakenGlobals - The globals that do not have their addresses
70fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    /// taken.
71fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    std::set<GlobalValue*> NonAddressTakenGlobals;
723b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
73ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    /// IndirectGlobals - The memory pointed to by this global is known to be
74ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    /// 'owned' by the global.
75ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    std::set<GlobalValue*> IndirectGlobals;
76ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
77ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    /// AllocsForIndirectGlobals - If an instruction allocates memory for an
78ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    /// indirect global, this map indicates which one.
79ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    std::map<Value*, GlobalValue*> AllocsForIndirectGlobals;
80ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
813b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    /// FunctionInfo - For each function, keep track of what globals are
823b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    /// modified or read.
83fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    std::map<Function*, FunctionRecord> FunctionInfo;
843b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
853b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  public:
861997473cf72957d0e70322e2fe6fe2ab141c58a6Devang Patel    static char ID;
87794fd75c67a2cdc128d67342c6d88a504d186896Devang Patel    GlobalsModRef() : ModulePass((intptr_t)&ID) {}
88794fd75c67a2cdc128d67342c6d88a504d186896Devang Patel
891cee94f04111cfd7114979d6dfddce2669c9380dDevang Patel    /// isAnalysis - Return true if this pass is  implementing an analysis pass.
901cee94f04111cfd7114979d6dfddce2669c9380dDevang Patel    virtual bool isAnalysis() const { return true; }
911cee94f04111cfd7114979d6dfddce2669c9380dDevang Patel
92b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner    bool runOnModule(Module &M) {
933b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      InitializeAliasAnalysis(this);                 // set up super class
943b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      AnalyzeGlobals(M);                          // find non-addr taken globals
953b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      AnalyzeCallGraph(getAnalysis<CallGraph>(), M); // Propagate on CG
963b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      return false;
973b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    }
983b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
993b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1003b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      AliasAnalysis::getAnalysisUsage(AU);
1013b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      AU.addRequired<CallGraph>();
1023b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      AU.setPreservesAll();                         // Does not transform code
1033b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    }
1043b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
1053b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    //------------------------------------------------
1063b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    // Implement the AliasAnalysis API
1072b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman    //
1083b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    AliasResult alias(const Value *V1, unsigned V1Size,
1093b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner                      const Value *V2, unsigned V2Size);
1103b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
1114a7ebfa4111305ea22fc753d4f029eed88149662Reid Spencer    ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
1124a7ebfa4111305ea22fc753d4f029eed88149662Reid Spencer      return AliasAnalysis::getModRefInfo(CS1,CS2);
1134a7ebfa4111305ea22fc753d4f029eed88149662Reid Spencer    }
1143b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    bool hasNoModRefInfoForCalls() const { return false; }
1153b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
1160af024c5d00c2a2d0e35ba33f4aaf4ed3fadfae2Chris Lattner    /// getModRefBehavior - Return the behavior of the specified function if
1170af024c5d00c2a2d0e35ba33f4aaf4ed3fadfae2Chris Lattner    /// called from the specified call site.  The call site may be null in which
1180af024c5d00c2a2d0e35ba33f4aaf4ed3fadfae2Chris Lattner    /// case the most generic behavior of this function should be returned.
11941925f87d3e7fc9eb2b583267b99012dcb2774a0Chris Lattner    virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS,
12041925f87d3e7fc9eb2b583267b99012dcb2774a0Chris Lattner                                         std::vector<PointerAccessInfo> *Info) {
121ae9f3a3b7c915f725aef5a7250e88eaeddda03c6Anton Korobeynikov      if (FunctionRecord *FR = getFunctionInfo(F)) {
122fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        if (FR->FunctionEffect == 0)
1230af024c5d00c2a2d0e35ba33f4aaf4ed3fadfae2Chris Lattner          return DoesNotAccessMemory;
124dedf2bd5a34dac25e4245f58bb902ced6b64edd9Misha Brukman        else if ((FR->FunctionEffect & Mod) == 0)
125dedf2bd5a34dac25e4245f58bb902ced6b64edd9Misha Brukman          return OnlyReadsMemory;
126ae9f3a3b7c915f725aef5a7250e88eaeddda03c6Anton Korobeynikov      }
1272b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman      return AliasAnalysis::getModRefBehavior(F, CS, Info);
128fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    }
129fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner
1303b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    virtual void deleteValue(Value *V);
1313b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    virtual void copyValue(Value *From, Value *To);
1323b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
1333b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  private:
134fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    /// getFunctionInfo - Return the function info for the function, or null if
135fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    /// the function calls an external function (in which case we don't have
136fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    /// anything useful to say about it).
137fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    FunctionRecord *getFunctionInfo(Function *F) {
138fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner      std::map<Function*, FunctionRecord>::iterator I = FunctionInfo.find(F);
139fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner      if (I != FunctionInfo.end())
140fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        return &I->second;
141fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner      return 0;
142fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    }
143fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner
1443b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    void AnalyzeGlobals(Module &M);
1453b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    void AnalyzeCallGraph(CallGraph &CG, Module &M);
146fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    void AnalyzeSCC(std::vector<CallGraphNode *> &SCC);
147ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    bool AnalyzeUsesOfPointer(Value *V, std::vector<Function*> &Readers,
148ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner                              std::vector<Function*> &Writers,
149ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner                              GlobalValue *OkayStoreDest = 0);
150ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    bool AnalyzeIndirectGlobalMemory(GlobalValue *GV);
1513b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  };
1522b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman
1531997473cf72957d0e70322e2fe6fe2ab141c58a6Devang Patel  char GlobalsModRef::ID = 0;
1547f8897f22e88271cfa114998a4d6088e7c8e8e11Chris Lattner  RegisterPass<GlobalsModRef> X("globalsmodref-aa",
1557f8897f22e88271cfa114998a4d6088e7c8e8e11Chris Lattner                                "Simple mod/ref analysis for globals");
156a5370172b64bed5daf8e2869d7bf7cb52f80d6b7Chris Lattner  RegisterAnalysisGroup<AliasAnalysis> Y(X);
1573b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner}
1583b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
1593b04a8ac455568f9b98ab7e9a43076260cb737aeChris LattnerPass *llvm::createGlobalsModRefPass() { return new GlobalsModRef(); }
1603b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
161ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// getUnderlyingObject - This traverses the use chain to figure out what object
162ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// the specified value points to.  If the value points to, or is derived from,
163ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// a global object, return it.
164ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattnerstatic Value *getUnderlyingObject(Value *V) {
165ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  if (!isa<PointerType>(V->getType())) return V;
166ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
167ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // If we are at some type of object... return it.
168ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV;
169ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
170ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // Traverse through different addressing mechanisms.
171ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  if (Instruction *I = dyn_cast<Instruction>(V)) {
1723da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer    if (isa<BitCastInst>(I) || isa<GetElementPtrInst>(I))
173ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      return getUnderlyingObject(I->getOperand(0));
174ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1753da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer    if (CE->getOpcode() == Instruction::BitCast ||
176ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        CE->getOpcode() == Instruction::GetElementPtr)
177ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      return getUnderlyingObject(CE->getOperand(0));
178ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  }
179ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
180ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // Othewise, we don't know what this is, return it as the base pointer.
181ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  return V;
182ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner}
1833b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
184ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// AnalyzeGlobals - Scan through the users of all of the internal
1853b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// GlobalValue's in the program.  If none of them have their "Address taken"
1863b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// (really, their address passed to something nontrivial), record this fact,
1873b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// and record the functions that they are used directly in.
1883b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnervoid GlobalsModRef::AnalyzeGlobals(Module &M) {
1893b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  std::vector<Function*> Readers, Writers;
1903b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1913b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    if (I->hasInternalLinkage()) {
192ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (!AnalyzeUsesOfPointer(I, Readers, Writers)) {
193fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        // Remember that we are tracking this global.
194fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        NonAddressTakenGlobals.insert(I);
1953b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner        ++NumNonAddrTakenFunctions;
1963b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      }
1973b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      Readers.clear(); Writers.clear();
1983b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    }
1993b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
200f5eaf3c62c7b2f2da1f85ac411c4f13bcbf131f1Chris Lattner  for (Module::global_iterator I = M.global_begin(), E = M.global_end();
201f5eaf3c62c7b2f2da1f85ac411c4f13bcbf131f1Chris Lattner       I != E; ++I)
2023b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    if (I->hasInternalLinkage()) {
203ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (!AnalyzeUsesOfPointer(I, Readers, Writers)) {
2043b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner        // Remember that we are tracking this global, and the mod/ref fns
205fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        NonAddressTakenGlobals.insert(I);
206fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        for (unsigned i = 0, e = Readers.size(); i != e; ++i)
207fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner          FunctionInfo[Readers[i]].GlobalInfo[I] |= Ref;
208fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner
209fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        if (!I->isConstant())  // No need to keep track of writers to constants
210fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner          for (unsigned i = 0, e = Writers.size(); i != e; ++i)
211fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner            FunctionInfo[Writers[i]].GlobalInfo[I] |= Mod;
2123b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner        ++NumNonAddrTakenGlobalVars;
213ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
214ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        // If this global holds a pointer type, see if it is an indirect global.
215ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        if (isa<PointerType>(I->getType()->getElementType()) &&
216ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner            AnalyzeIndirectGlobalMemory(I))
217ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner          ++NumIndirectGlobalVars;
2183b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      }
2193b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      Readers.clear(); Writers.clear();
2203b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    }
2213b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner}
2223b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
223ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// AnalyzeUsesOfPointer - Look at all of the users of the specified pointer.
224ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// If this is used by anything complex (i.e., the address escapes), return
225ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// true.  Also, while we are at it, keep track of those functions that read and
226ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// write to the value.
227ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner///
228ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// If OkayStoreDest is non-null, stores into this global are allowed.
229ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattnerbool GlobalsModRef::AnalyzeUsesOfPointer(Value *V,
230ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner                                         std::vector<Function*> &Readers,
231ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner                                         std::vector<Function*> &Writers,
232ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner                                         GlobalValue *OkayStoreDest) {
233fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  if (!isa<PointerType>(V->getType())) return true;
2343b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
2353b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI)
2363b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
2373b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      Readers.push_back(LI->getParent()->getParent());
2383b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    } else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
239ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (V == SI->getOperand(1)) {
240ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        Writers.push_back(SI->getParent()->getParent());
241ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      } else if (SI->getOperand(1) != OkayStoreDest) {
242ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        return true;  // Storing the pointer
243ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      }
2443b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) {
245ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (AnalyzeUsesOfPointer(GEP, Readers, Writers)) return true;
2463b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    } else if (CallInst *CI = dyn_cast<CallInst>(*UI)) {
2473b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      // Make sure that this is just the function being called, not that it is
2483b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      // passing into the function.
2493b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i)
2503b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner        if (CI->getOperand(i) == V) return true;
2513b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    } else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) {
2523b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      // Make sure that this is just the function being called, not that it is
2533b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      // passing into the function.
2543b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      for (unsigned i = 3, e = II->getNumOperands(); i != e; ++i)
2553b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner        if (II->getOperand(i) == V) return true;
2563b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(*UI)) {
2573da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer      if (CE->getOpcode() == Instruction::GetElementPtr ||
2583da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer          CE->getOpcode() == Instruction::BitCast) {
259ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        if (AnalyzeUsesOfPointer(CE, Readers, Writers))
2603b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner          return true;
2613b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      } else {
2623b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner        return true;
2632b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman      }
264e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer    } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(*UI)) {
265e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer      if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
266ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        return true;  // Allow comparison against null.
267ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    } else if (FreeInst *F = dyn_cast<FreeInst>(*UI)) {
268ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      Writers.push_back(F->getParent()->getParent());
2693b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    } else {
2703b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      return true;
2713b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    }
2723b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  return false;
2733b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner}
2743b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
275ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// AnalyzeIndirectGlobalMemory - We found an non-address-taken global variable
276ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// which holds a pointer type.  See if the global always points to non-aliased
277ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// heap memory: that is, all initializers of the globals are allocations, and
278ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// those allocations have no use other than initialization of the global.
279ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// Further, all loads out of GV must directly use the memory, not store the
280ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// pointer somewhere.  If this is true, we consider the memory pointed to by
281ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// GV to be owned by GV and can disambiguate other pointers from it.
282ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattnerbool GlobalsModRef::AnalyzeIndirectGlobalMemory(GlobalValue *GV) {
283ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // Keep track of values related to the allocation of the memory, f.e. the
284ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // value produced by the malloc call and any casts.
285ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  std::vector<Value*> AllocRelatedValues;
286ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
287ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // Walk the user list of the global.  If we find anything other than a direct
288ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // load or store, bail out.
289ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  for (Value::use_iterator I = GV->use_begin(), E = GV->use_end(); I != E; ++I){
290ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    if (LoadInst *LI = dyn_cast<LoadInst>(*I)) {
291ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // The pointer loaded from the global can only be used in simple ways:
292ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // we allow addressing of it and loading storing to it.  We do *not* allow
293ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // storing the loaded pointer somewhere else or passing to a function.
294ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      std::vector<Function*> ReadersWriters;
295ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (AnalyzeUsesOfPointer(LI, ReadersWriters, ReadersWriters))
296ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        return false;  // Loaded pointer escapes.
297ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // TODO: Could try some IP mod/ref of the loaded pointer.
298ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    } else if (StoreInst *SI = dyn_cast<StoreInst>(*I)) {
299ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // Storing the global itself.
300ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (SI->getOperand(0) == GV) return false;
301ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
302ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // If storing the null pointer, ignore it.
303ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (isa<ConstantPointerNull>(SI->getOperand(0)))
304ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        continue;
305ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
306ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // Check the value being stored.
307ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      Value *Ptr = getUnderlyingObject(SI->getOperand(0));
308ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
309ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (isa<MallocInst>(Ptr)) {
310ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        // Okay, easy case.
311ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      } else if (CallInst *CI = dyn_cast<CallInst>(Ptr)) {
312ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        Function *F = CI->getCalledFunction();
3135cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer        if (!F || !F->isDeclaration()) return false;     // Too hard to analyze.
314ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        if (F->getName() != "calloc") return false;   // Not calloc.
315ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      } else {
316ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        return false;  // Too hard to analyze.
317ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      }
318ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
319ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // Analyze all uses of the allocation.  If any of them are used in a
320ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // non-simple way (e.g. stored to another global) bail out.
321ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      std::vector<Function*> ReadersWriters;
322ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (AnalyzeUsesOfPointer(Ptr, ReadersWriters, ReadersWriters, GV))
323ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        return false;  // Loaded pointer escapes.
324ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
325ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // Remember that this allocation is related to the indirect global.
326ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      AllocRelatedValues.push_back(Ptr);
327ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    } else {
328ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // Something complex, bail out.
329ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      return false;
330ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    }
331ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  }
332ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
333ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // Okay, this is an indirect global.  Remember all of the allocations for
334ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // this global in AllocsForIndirectGlobals.
335ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  while (!AllocRelatedValues.empty()) {
336ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV;
337ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    AllocRelatedValues.pop_back();
338ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  }
339ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  IndirectGlobals.insert(GV);
340ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  return true;
341ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner}
342ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
3433b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// AnalyzeCallGraph - At this point, we know the functions where globals are
3443b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// immediately stored to and read from.  Propagate this information up the call
345fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner/// graph to all callers and compute the mod/ref info for all memory for each
3462b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman/// function.
3473b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnervoid GlobalsModRef::AnalyzeCallGraph(CallGraph &CG, Module &M) {
3483b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  // We do a bottom-up SCC traversal of the call graph.  In other words, we
3493b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  // visit all callees before callers (leaf-first).
350fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  for (scc_iterator<CallGraph*> I = scc_begin(&CG), E = scc_end(&CG); I!=E; ++I)
351105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner    if ((*I).size() != 1) {
352fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner      AnalyzeSCC(*I);
353105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner    } else if (Function *F = (*I)[0]->getFunction()) {
3545cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer      if (!F->isDeclaration()) {
355105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner        // Nonexternal function.
356105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner        AnalyzeSCC(*I);
357105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner      } else {
358105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner        // Otherwise external function.  Handle intrinsics and other special
359105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner        // cases here.
360105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner        if (getAnalysis<AliasAnalysis>().doesNotAccessMemory(F))
361105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner          // If it does not access memory, process the function, causing us to
362105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner          // realize it doesn't do anything (the body is empty).
363105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner          AnalyzeSCC(*I);
364105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner        else {
365105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner          // Otherwise, don't process it.  This will cause us to conservatively
366105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner          // assume the worst.
367105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner        }
368105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner      }
369105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner    } else {
370105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner      // Do not process the external node, assume the worst.
371105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner    }
372fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner}
373fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner
374fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattnervoid GlobalsModRef::AnalyzeSCC(std::vector<CallGraphNode *> &SCC) {
375fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  assert(!SCC.empty() && "SCC with no functions?");
376fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  FunctionRecord &FR = FunctionInfo[SCC[0]->getFunction()];
377fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner
378fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  bool CallsExternal = false;
379fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  unsigned FunctionEffect = 0;
380fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner
381fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  // Collect the mod/ref properties due to called functions.  We only compute
382fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  // one mod-ref set
383fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  for (unsigned i = 0, e = SCC.size(); i != e && !CallsExternal; ++i)
384fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end();
385fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner         CI != E; ++CI)
386d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner      if (Function *Callee = CI->second->getFunction()) {
387fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        if (FunctionRecord *CalleeFR = getFunctionInfo(Callee)) {
388fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner          // Propagate function effect up.
389fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner          FunctionEffect |= CalleeFR->FunctionEffect;
390fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner
391fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner          // Incorporate callee's effects on globals into our info.
392fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner          for (std::map<GlobalValue*, unsigned>::iterator GI =
393fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner                 CalleeFR->GlobalInfo.begin(), E = CalleeFR->GlobalInfo.end();
394fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner               GI != E; ++GI)
395fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner            FR.GlobalInfo[GI->first] |= GI->second;
3963b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
3973b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner        } else {
398ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner          // Okay, if we can't say anything about it, maybe some other alias
399ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner          // analysis can.
400ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner          ModRefBehavior MRB =
401dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands            AliasAnalysis::getModRefBehavior(Callee);
402ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner          if (MRB != DoesNotAccessMemory) {
40362da315c859eb20e0ad38c034f71901ac6558d4dChris Lattner            // FIXME: could make this more aggressive for functions that just
40462da315c859eb20e0ad38c034f71901ac6558d4dChris Lattner            // read memory.  We should just say they read all globals.
40562da315c859eb20e0ad38c034f71901ac6558d4dChris Lattner            CallsExternal = true;
40662da315c859eb20e0ad38c034f71901ac6558d4dChris Lattner            break;
407ec6518ddc2baa110ffdadc9ba8625ae968f746beChris Lattner          }
4083b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner        }
409fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner      } else {
410fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        CallsExternal = true;
411fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        break;
4123b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      }
413fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner
414fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  // If this SCC calls an external function, we can't say anything about it, so
415fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  // remove all SCC functions from the FunctionInfo map.
416fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  if (CallsExternal) {
4173b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    for (unsigned i = 0, e = SCC.size(); i != e; ++i)
418fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner      FunctionInfo.erase(SCC[i]->getFunction());
419fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    return;
4203b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  }
4212b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman
422fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  // Otherwise, unless we already know that this function mod/refs memory, scan
423fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  // the function bodies to see if there are any explicit loads or stores.
424fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  if (FunctionEffect != ModRef) {
425fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    for (unsigned i = 0, e = SCC.size(); i != e && FunctionEffect != ModRef;++i)
426fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner      for (inst_iterator II = inst_begin(SCC[i]->getFunction()),
4272b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman             E = inst_end(SCC[i]->getFunction());
428fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner           II != E && FunctionEffect != ModRef; ++II)
429fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        if (isa<LoadInst>(*II))
430fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner          FunctionEffect |= Ref;
431fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        else if (isa<StoreInst>(*II))
432fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner          FunctionEffect |= Mod;
4333fe4d3cb5bd5ea7948faeed8451b61380d928808Chris Lattner        else if (isa<MallocInst>(*II) || isa<FreeInst>(*II))
4343fe4d3cb5bd5ea7948faeed8451b61380d928808Chris Lattner          FunctionEffect |= ModRef;
435fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  }
436fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner
437fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  if ((FunctionEffect & Mod) == 0)
438fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    ++NumReadMemFunctions;
439fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  if (FunctionEffect == 0)
440fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    ++NumNoMemFunctions;
441fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  FR.FunctionEffect = FunctionEffect;
442fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner
443fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  // Finally, now that we know the full effect on this SCC, clone the
444fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  // information to each function in the SCC.
445fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  for (unsigned i = 1, e = SCC.size(); i != e; ++i)
446fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    FunctionInfo[SCC[i]->getFunction()] = FR;
4473b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner}
4483b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
4493b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
4503b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
4513b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// alias - If one of the pointers is to a global that we are tracking, and the
4523b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// other is some random pointer, we know there cannot be an alias, because the
4533b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// address of the global isn't taken.
4543b04a8ac455568f9b98ab7e9a43076260cb737aeChris LattnerAliasAnalysis::AliasResult
4553b04a8ac455568f9b98ab7e9a43076260cb737aeChris LattnerGlobalsModRef::alias(const Value *V1, unsigned V1Size,
4563b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner                     const Value *V2, unsigned V2Size) {
457ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // Get the base object these pointers point to.
458ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  Value *UV1 = getUnderlyingObject(const_cast<Value*>(V1));
459ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  Value *UV2 = getUnderlyingObject(const_cast<Value*>(V2));
460ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
461ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // If either of the underlying values is a global, they may be non-addr-taken
462ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // globals, which we can answer queries about.
463ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  GlobalValue *GV1 = dyn_cast<GlobalValue>(UV1);
464ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  GlobalValue *GV2 = dyn_cast<GlobalValue>(UV2);
465ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  if (GV1 || GV2) {
466ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    // If the global's address is taken, pretend we don't know it's a pointer to
467ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    // the global.
468ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    if (GV1 && !NonAddressTakenGlobals.count(GV1)) GV1 = 0;
469ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    if (GV2 && !NonAddressTakenGlobals.count(GV2)) GV2 = 0;
470ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
471ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    // If the the two pointers are derived from two different non-addr-taken
472ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    // globals, or if one is and the other isn't, we know these can't alias.
473ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    if ((GV1 || GV2) && GV1 != GV2)
474ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      return NoAlias;
475ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
476ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    // Otherwise if they are both derived from the same addr-taken global, we
477ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    // can't know the two accesses don't overlap.
478ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  }
479ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
480ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // These pointers may be based on the memory owned by an indirect global.  If
481ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // so, we may be able to handle this.  First check to see if the base pointer
482ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // is a direct load from an indirect global.
483ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  GV1 = GV2 = 0;
484ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  if (LoadInst *LI = dyn_cast<LoadInst>(UV1))
485ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
486ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (IndirectGlobals.count(GV))
487ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        GV1 = GV;
488ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  if (LoadInst *LI = dyn_cast<LoadInst>(UV2))
489ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
490ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (IndirectGlobals.count(GV))
491ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        GV2 = GV;
492ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
493ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // These pointers may also be from an allocation for the indirect global.  If
494ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // so, also handle them.
495ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  if (AllocsForIndirectGlobals.count(UV1))
496ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    GV1 = AllocsForIndirectGlobals[UV1];
497ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  if (AllocsForIndirectGlobals.count(UV2))
498ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    GV2 = AllocsForIndirectGlobals[UV2];
499ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
500ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // Now that we know whether the two pointers are related to indirect globals,
501ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // use this to disambiguate the pointers.  If either pointer is based on an
502ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // indirect global and if they are not both based on the same indirect global,
503ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // they cannot alias.
5043b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  if ((GV1 || GV2) && GV1 != GV2)
5053b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    return NoAlias;
506ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
5073b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
5083b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner}
5093b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
5103b04a8ac455568f9b98ab7e9a43076260cb737aeChris LattnerAliasAnalysis::ModRefResult
5113b04a8ac455568f9b98ab7e9a43076260cb737aeChris LattnerGlobalsModRef::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
5123b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  unsigned Known = ModRef;
5133b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
5143b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  // If we are asking for mod/ref info of a direct call with a pointer to a
515fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  // global we are tracking, return information if we have it.
516ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  if (GlobalValue *GV = dyn_cast<GlobalValue>(getUnderlyingObject(P)))
5173b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    if (GV->hasInternalLinkage())
518fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner      if (Function *F = CS.getCalledFunction())
519fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        if (NonAddressTakenGlobals.count(GV))
520fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner          if (FunctionRecord *FR = getFunctionInfo(F))
521fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner            Known = FR->getInfoForGlobal(GV);
5223b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
5233b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  if (Known == NoModRef)
5243b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    return NoModRef; // No need to query other mod/ref analyses
5253b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  return ModRefResult(Known & AliasAnalysis::getModRefInfo(CS, P, Size));
5263b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner}
5273b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
5283b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
5293b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner//===----------------------------------------------------------------------===//
5303b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner// Methods to update the analysis as a result of the client transformation.
5313b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner//
5323b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnervoid GlobalsModRef::deleteValue(Value *V) {
533ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
534ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    if (NonAddressTakenGlobals.erase(GV)) {
535ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // This global might be an indirect global.  If so, remove it and remove
536ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // any AllocRelatedValues for it.
537ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (IndirectGlobals.erase(GV)) {
538ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        // Remove any entries in AllocsForIndirectGlobals for this global.
539ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        for (std::map<Value*, GlobalValue*>::iterator
540ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner             I = AllocsForIndirectGlobals.begin(),
541ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner             E = AllocsForIndirectGlobals.end(); I != E; ) {
542ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner          if (I->second == GV) {
543ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner            AllocsForIndirectGlobals.erase(I++);
544ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner          } else {
545ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner            ++I;
546ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner          }
547ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        }
548ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      }
549ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    }
550ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  }
551ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
552ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // Otherwise, if this is an allocation related to an indirect global, remove
553ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // it.
554ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  AllocsForIndirectGlobals.erase(V);
555cd883f203d23c7e44b12dff62df2e6d65d8f231cChris Lattner
556cd883f203d23c7e44b12dff62df2e6d65d8f231cChris Lattner  AliasAnalysis::deleteValue(V);
5573b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner}
5583b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
5593b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnervoid GlobalsModRef::copyValue(Value *From, Value *To) {
560cd883f203d23c7e44b12dff62df2e6d65d8f231cChris Lattner  AliasAnalysis::copyValue(From, To);
5613b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner}
562