GlobalsModRef.cpp revision 1abe60b9be1b7b33e1fa422add5296d392831850
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.
679a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands  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
739a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    /// ReadGlobals - The globals without addresses taken that are read by
749a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    /// some function.
759a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    std::set<GlobalValue*> ReadGlobals;
769a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
77ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    /// IndirectGlobals - The memory pointed to by this global is known to be
78ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    /// 'owned' by the global.
79ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    std::set<GlobalValue*> IndirectGlobals;
809a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
81ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    /// AllocsForIndirectGlobals - If an instruction allocates memory for an
82ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    /// indirect global, this map indicates which one.
83ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    std::map<Value*, GlobalValue*> AllocsForIndirectGlobals;
849a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
853b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    /// FunctionInfo - For each function, keep track of what globals are
863b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    /// modified or read.
87fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    std::map<Function*, FunctionRecord> FunctionInfo;
883b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
893b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  public:
901997473cf72957d0e70322e2fe6fe2ab141c58a6Devang Patel    static char ID;
91ae73dc1448d25b02cabc7c64c86c64371453dda8Dan Gohman    GlobalsModRef() : ModulePass(&ID) {}
921cee94f04111cfd7114979d6dfddce2669c9380dDevang Patel
93b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner    bool runOnModule(Module &M) {
943b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      InitializeAliasAnalysis(this);                 // set up super class
953b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      AnalyzeGlobals(M);                          // find non-addr taken globals
963b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      AnalyzeCallGraph(getAnalysis<CallGraph>(), M); // Propagate on CG
973b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      return false;
983b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    }
993b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
1003b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1013b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      AliasAnalysis::getAnalysisUsage(AU);
1023b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      AU.addRequired<CallGraph>();
1033b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      AU.setPreservesAll();                         // Does not transform code
1043b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    }
1053b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
1063b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    //------------------------------------------------
1073b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    // Implement the AliasAnalysis API
1082b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman    //
1093b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    AliasResult alias(const Value *V1, unsigned V1Size,
1103b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner                      const Value *V2, unsigned V2Size);
1113b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
1124a7ebfa4111305ea22fc753d4f029eed88149662Reid Spencer    ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
1134a7ebfa4111305ea22fc753d4f029eed88149662Reid Spencer      return AliasAnalysis::getModRefInfo(CS1,CS2);
1144a7ebfa4111305ea22fc753d4f029eed88149662Reid Spencer    }
1153b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    bool hasNoModRefInfoForCalls() const { return false; }
1163b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
1170af024c5d00c2a2d0e35ba33f4aaf4ed3fadfae2Chris Lattner    /// getModRefBehavior - Return the behavior of the specified function if
1180af024c5d00c2a2d0e35ba33f4aaf4ed3fadfae2Chris Lattner    /// called from the specified call site.  The call site may be null in which
1190af024c5d00c2a2d0e35ba33f4aaf4ed3fadfae2Chris Lattner    /// case the most generic behavior of this function should be returned.
12041925f87d3e7fc9eb2b583267b99012dcb2774a0Chris Lattner    virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS,
12141925f87d3e7fc9eb2b583267b99012dcb2774a0Chris Lattner                                         std::vector<PointerAccessInfo> *Info) {
122ae9f3a3b7c915f725aef5a7250e88eaeddda03c6Anton Korobeynikov      if (FunctionRecord *FR = getFunctionInfo(F)) {
123fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        if (FR->FunctionEffect == 0)
1240af024c5d00c2a2d0e35ba33f4aaf4ed3fadfae2Chris Lattner          return DoesNotAccessMemory;
125dedf2bd5a34dac25e4245f58bb902ced6b64edd9Misha Brukman        else if ((FR->FunctionEffect & Mod) == 0)
126dedf2bd5a34dac25e4245f58bb902ced6b64edd9Misha Brukman          return OnlyReadsMemory;
127ae9f3a3b7c915f725aef5a7250e88eaeddda03c6Anton Korobeynikov      }
1282b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman      return AliasAnalysis::getModRefBehavior(F, CS, Info);
129fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    }
130fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner
1313b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    virtual void deleteValue(Value *V);
1323b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    virtual void copyValue(Value *From, Value *To);
1333b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
1343b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  private:
135fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    /// getFunctionInfo - Return the function info for the function, or null if
1369a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    /// we don't have 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);
146ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    bool AnalyzeUsesOfPointer(Value *V, std::vector<Function*> &Readers,
147ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner                              std::vector<Function*> &Writers,
148ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner                              GlobalValue *OkayStoreDest = 0);
149ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    bool AnalyzeIndirectGlobalMemory(GlobalValue *GV);
1503b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  };
1513b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner}
1523b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
153844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanchar GlobalsModRef::ID = 0;
154844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanstatic RegisterPass<GlobalsModRef>
155844731a7f1909f55935e3514c9e713a62d67662eDan GohmanX("globalsmodref-aa", "Simple mod/ref analysis for globals", false, true);
156844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanstatic RegisterAnalysisGroup<AliasAnalysis> Y(X);
157844731a7f1909f55935e3514c9e713a62d67662eDan Gohman
1583b04a8ac455568f9b98ab7e9a43076260cb737aeChris LattnerPass *llvm::createGlobalsModRefPass() { return new GlobalsModRef(); }
1593b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
160ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// getUnderlyingObject - This traverses the use chain to figure out what object
161ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// the specified value points to.  If the value points to, or is derived from,
162ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// a global object, return it.
163ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattnerstatic Value *getUnderlyingObject(Value *V) {
164ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  if (!isa<PointerType>(V->getType())) return V;
1659a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
166ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // If we are at some type of object... return it.
167ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV;
1689a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
169ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // Traverse through different addressing mechanisms.
170ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  if (Instruction *I = dyn_cast<Instruction>(V)) {
1713da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer    if (isa<BitCastInst>(I) || isa<GetElementPtrInst>(I))
172ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      return getUnderlyingObject(I->getOperand(0));
173ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1749a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    if (CE->getOpcode() == Instruction::BitCast ||
175ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        CE->getOpcode() == Instruction::GetElementPtr)
176ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      return getUnderlyingObject(CE->getOperand(0));
177ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  }
1789a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
1799a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands  // Otherwise, we don't know what this is, return it as the base pointer.
180ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  return V;
181ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner}
1823b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
183ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// AnalyzeGlobals - Scan through the users of all of the internal
1849a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands/// GlobalValue's in the program.  If none of them have their "address taken"
1853b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// (really, their address passed to something nontrivial), record this fact,
1863b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// and record the functions that they are used directly in.
1873b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnervoid GlobalsModRef::AnalyzeGlobals(Module &M) {
1883b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  std::vector<Function*> Readers, Writers;
1893b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1903b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    if (I->hasInternalLinkage()) {
191ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (!AnalyzeUsesOfPointer(I, Readers, Writers)) {
192fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        // Remember that we are tracking this global.
193fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        NonAddressTakenGlobals.insert(I);
1943b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner        ++NumNonAddrTakenFunctions;
1953b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      }
1963b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      Readers.clear(); Writers.clear();
1973b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    }
1983b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
199f5eaf3c62c7b2f2da1f85ac411c4f13bcbf131f1Chris Lattner  for (Module::global_iterator I = M.global_begin(), E = M.global_end();
200f5eaf3c62c7b2f2da1f85ac411c4f13bcbf131f1Chris Lattner       I != E; ++I)
2013b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    if (I->hasInternalLinkage()) {
202ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (!AnalyzeUsesOfPointer(I, Readers, Writers)) {
2033b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner        // Remember that we are tracking this global, and the mod/ref fns
204fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        NonAddressTakenGlobals.insert(I);
2059a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
2069a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands        if (!Readers.empty())
2079a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands          // Some function read this global - remember that.
2089a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands          ReadGlobals.insert(I);
2099a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
210fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        for (unsigned i = 0, e = Readers.size(); i != e; ++i)
211fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner          FunctionInfo[Readers[i]].GlobalInfo[I] |= Ref;
212fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner
213fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        if (!I->isConstant())  // No need to keep track of writers to constants
214fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner          for (unsigned i = 0, e = Writers.size(); i != e; ++i)
215fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner            FunctionInfo[Writers[i]].GlobalInfo[I] |= Mod;
2163b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner        ++NumNonAddrTakenGlobalVars;
2179a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
218ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        // If this global holds a pointer type, see if it is an indirect global.
219ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        if (isa<PointerType>(I->getType()->getElementType()) &&
220ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner            AnalyzeIndirectGlobalMemory(I))
221ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner          ++NumIndirectGlobalVars;
2223b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      }
2233b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      Readers.clear(); Writers.clear();
2243b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    }
2253b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner}
2263b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
227ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// AnalyzeUsesOfPointer - Look at all of the users of the specified pointer.
228ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// If this is used by anything complex (i.e., the address escapes), return
229ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// true.  Also, while we are at it, keep track of those functions that read and
230ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// write to the value.
231ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner///
232ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// If OkayStoreDest is non-null, stores into this global are allowed.
233ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattnerbool GlobalsModRef::AnalyzeUsesOfPointer(Value *V,
234ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner                                         std::vector<Function*> &Readers,
235ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner                                         std::vector<Function*> &Writers,
236ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner                                         GlobalValue *OkayStoreDest) {
237fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  if (!isa<PointerType>(V->getType())) return true;
2383b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
2393b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI)
2403b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
2413b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      Readers.push_back(LI->getParent()->getParent());
2423b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    } else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
243ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (V == SI->getOperand(1)) {
244ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        Writers.push_back(SI->getParent()->getParent());
245ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      } else if (SI->getOperand(1) != OkayStoreDest) {
246ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        return true;  // Storing the pointer
247ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      }
2483b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) {
249ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (AnalyzeUsesOfPointer(GEP, Readers, Writers)) return true;
2503b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    } else if (CallInst *CI = dyn_cast<CallInst>(*UI)) {
2513b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      // Make sure that this is just the function being called, not that it is
2523b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      // passing into the function.
2533b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i)
2543b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner        if (CI->getOperand(i) == V) return true;
2553b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    } else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) {
2563b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      // Make sure that this is just the function being called, not that it is
2573b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      // passing into the function.
2583b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      for (unsigned i = 3, e = II->getNumOperands(); i != e; ++i)
2593b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner        if (II->getOperand(i) == V) return true;
2603b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(*UI)) {
2619a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands      if (CE->getOpcode() == Instruction::GetElementPtr ||
2623da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer          CE->getOpcode() == Instruction::BitCast) {
263ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        if (AnalyzeUsesOfPointer(CE, Readers, Writers))
2643b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner          return true;
2653b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      } else {
2663b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner        return true;
2672b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman      }
268e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer    } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(*UI)) {
269e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer      if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
270ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        return true;  // Allow comparison against null.
271ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    } else if (FreeInst *F = dyn_cast<FreeInst>(*UI)) {
272ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      Writers.push_back(F->getParent()->getParent());
2733b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    } else {
2743b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      return true;
2753b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    }
2763b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  return false;
2773b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner}
2783b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
279ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// AnalyzeIndirectGlobalMemory - We found an non-address-taken global variable
280ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// which holds a pointer type.  See if the global always points to non-aliased
281ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// heap memory: that is, all initializers of the globals are allocations, and
282ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// those allocations have no use other than initialization of the global.
283ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// Further, all loads out of GV must directly use the memory, not store the
284ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// pointer somewhere.  If this is true, we consider the memory pointed to by
285ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// GV to be owned by GV and can disambiguate other pointers from it.
286ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattnerbool GlobalsModRef::AnalyzeIndirectGlobalMemory(GlobalValue *GV) {
287ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // Keep track of values related to the allocation of the memory, f.e. the
288ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // value produced by the malloc call and any casts.
289ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  std::vector<Value*> AllocRelatedValues;
2909a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
291ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // Walk the user list of the global.  If we find anything other than a direct
292ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // load or store, bail out.
293ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  for (Value::use_iterator I = GV->use_begin(), E = GV->use_end(); I != E; ++I){
294ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    if (LoadInst *LI = dyn_cast<LoadInst>(*I)) {
295ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // The pointer loaded from the global can only be used in simple ways:
296ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // we allow addressing of it and loading storing to it.  We do *not* allow
297ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // storing the loaded pointer somewhere else or passing to a function.
298ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      std::vector<Function*> ReadersWriters;
299ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (AnalyzeUsesOfPointer(LI, ReadersWriters, ReadersWriters))
300ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        return false;  // Loaded pointer escapes.
301ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // TODO: Could try some IP mod/ref of the loaded pointer.
302ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    } else if (StoreInst *SI = dyn_cast<StoreInst>(*I)) {
303ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // Storing the global itself.
304ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (SI->getOperand(0) == GV) return false;
3059a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
306ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // If storing the null pointer, ignore it.
307ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (isa<ConstantPointerNull>(SI->getOperand(0)))
308ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        continue;
3099a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
310ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // Check the value being stored.
311ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      Value *Ptr = getUnderlyingObject(SI->getOperand(0));
312ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
313ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (isa<MallocInst>(Ptr)) {
314ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        // Okay, easy case.
315ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      } else if (CallInst *CI = dyn_cast<CallInst>(Ptr)) {
316ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        Function *F = CI->getCalledFunction();
3175cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer        if (!F || !F->isDeclaration()) return false;     // Too hard to analyze.
318ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        if (F->getName() != "calloc") return false;   // Not calloc.
319ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      } else {
320ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        return false;  // Too hard to analyze.
321ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      }
3229a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
323ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // Analyze all uses of the allocation.  If any of them are used in a
324ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // non-simple way (e.g. stored to another global) bail out.
325ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      std::vector<Function*> ReadersWriters;
326ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (AnalyzeUsesOfPointer(Ptr, ReadersWriters, ReadersWriters, GV))
327ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        return false;  // Loaded pointer escapes.
328ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
329ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // Remember that this allocation is related to the indirect global.
330ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      AllocRelatedValues.push_back(Ptr);
331ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    } else {
332ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // Something complex, bail out.
333ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      return false;
334ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    }
335ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  }
3369a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
337ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // Okay, this is an indirect global.  Remember all of the allocations for
338ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // this global in AllocsForIndirectGlobals.
339ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  while (!AllocRelatedValues.empty()) {
340ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV;
341ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    AllocRelatedValues.pop_back();
342ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  }
343ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  IndirectGlobals.insert(GV);
344ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  return true;
345ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner}
346ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
3473b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// AnalyzeCallGraph - At this point, we know the functions where globals are
3483b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// immediately stored to and read from.  Propagate this information up the call
349fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner/// graph to all callers and compute the mod/ref info for all memory for each
3502b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman/// function.
3513b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnervoid GlobalsModRef::AnalyzeCallGraph(CallGraph &CG, Module &M) {
3523b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  // We do a bottom-up SCC traversal of the call graph.  In other words, we
3533b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  // visit all callees before callers (leaf-first).
3549a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands  for (scc_iterator<CallGraph*> I = scc_begin(&CG), E = scc_end(&CG); I != E;
3559a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands       ++I) {
3569a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    std::vector<CallGraphNode *> &SCC = *I;
357f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands    assert(!SCC.empty() && "SCC with no functions?");
3589a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
359f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands    if (!SCC[0]->getFunction()) {
360f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands      // Calls externally - can't say anything useful.  Remove any existing
361f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands      // function records (may have been created when scanning globals).
362f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands      for (unsigned i = 0, e = SCC.size(); i != e; ++i)
363f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands        FunctionInfo.erase(SCC[i]->getFunction());
36434f2a0ca17f00f9ce2081ee278685a007856fd90Duncan Sands      continue;
365f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands    }
366f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands
367f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands    FunctionRecord &FR = FunctionInfo[SCC[0]->getFunction()];
3683b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
3699a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    bool KnowNothing = false;
3709a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    unsigned FunctionEffect = 0;
3719a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
3729a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    // Collect the mod/ref properties due to called functions.  We only compute
3739a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    // one mod-ref set.
3749a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    for (unsigned i = 0, e = SCC.size(); i != e && !KnowNothing; ++i) {
3759a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands      Function *F = SCC[i]->getFunction();
3769a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands      if (!F) {
3779a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands        KnowNothing = true;
378fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        break;
3793b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      }
380fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner
3819a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands      if (F->isDeclaration()) {
3829a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands        // Try to get mod/ref behaviour from function attributes.
383d0ac373660de64fe210e50458c7702432b3f9605Duncan Sands        if (F->doesNotAccessMemory()) {
384d0ac373660de64fe210e50458c7702432b3f9605Duncan Sands          // Can't do better than that!
385d0ac373660de64fe210e50458c7702432b3f9605Duncan Sands        } else if (F->onlyReadsMemory()) {
3869a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands          FunctionEffect |= Ref;
3871abe60b9be1b7b33e1fa422add5296d392831850Duncan Sands          if (!F->isIntrinsic()) {
3881abe60b9be1b7b33e1fa422add5296d392831850Duncan Sands            // This function might call back into the module and read a global -
3891abe60b9be1b7b33e1fa422add5296d392831850Duncan Sands            // mark all globals read somewhere as being read by this function.
3901abe60b9be1b7b33e1fa422add5296d392831850Duncan Sands            for (std::set<GlobalValue*>::iterator GI = ReadGlobals.begin(),
3911abe60b9be1b7b33e1fa422add5296d392831850Duncan Sands                 E = ReadGlobals.end(); GI != E; ++GI)
3921abe60b9be1b7b33e1fa422add5296d392831850Duncan Sands              FR.GlobalInfo[*GI] |= Ref;
3931abe60b9be1b7b33e1fa422add5296d392831850Duncan Sands          }
394d0ac373660de64fe210e50458c7702432b3f9605Duncan Sands        } else {
3959a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands          // Can't say anything useful.
3969a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands          KnowNothing = true;
3979a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands        }
3989a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands        continue;
3999a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands      }
4009a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
4019a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands      for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end();
402f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands           CI != E && !KnowNothing; ++CI)
4039a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands        if (Function *Callee = CI->second->getFunction()) {
4049a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands          if (FunctionRecord *CalleeFR = getFunctionInfo(Callee)) {
4059a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands            // Propagate function effect up.
4069a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands            FunctionEffect |= CalleeFR->FunctionEffect;
4079a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
4089a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands            // Incorporate callee's effects on globals into our info.
4099a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands            for (std::map<GlobalValue*, unsigned>::iterator GI =
4109a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands                   CalleeFR->GlobalInfo.begin(), E = CalleeFR->GlobalInfo.end();
4119a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands                 GI != E; ++GI)
412f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands              FR.GlobalInfo[GI->first] |= GI->second;
4139a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands          } else {
4149a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands            // Can't say anything about it.  However, if it is inside our SCC,
4159a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands            // then nothing needs to be done.
4169a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands            CallGraphNode *CalleeNode = CG[Callee];
4179a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands            if (std::find(SCC.begin(), SCC.end(), CalleeNode) == SCC.end())
4189a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands              KnowNothing = true;
4199a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands          }
4209a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands        } else {
4219a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands          KnowNothing = true;
4229a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands        }
4239a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    }
4249a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
4259a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    // If we can't say anything useful about this SCC, remove all SCC functions
4269a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    // from the FunctionInfo map.
4279a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    if (KnowNothing) {
4289a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands      for (unsigned i = 0, e = SCC.size(); i != e; ++i)
4299a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands        FunctionInfo.erase(SCC[i]->getFunction());
430b070beee77cf9a0befd06a4fdacb824b1da0b55aDuncan Sands      continue;
4319a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    }
4322b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman
4339a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    // Scan the function bodies for explicit loads or stores.
434fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    for (unsigned i = 0, e = SCC.size(); i != e && FunctionEffect != ModRef;++i)
435fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner      for (inst_iterator II = inst_begin(SCC[i]->getFunction()),
4362b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman             E = inst_end(SCC[i]->getFunction());
437fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner           II != E && FunctionEffect != ModRef; ++II)
438fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        if (isa<LoadInst>(*II))
439fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner          FunctionEffect |= Ref;
440fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        else if (isa<StoreInst>(*II))
441fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner          FunctionEffect |= Mod;
4423fe4d3cb5bd5ea7948faeed8451b61380d928808Chris Lattner        else if (isa<MallocInst>(*II) || isa<FreeInst>(*II))
4433fe4d3cb5bd5ea7948faeed8451b61380d928808Chris Lattner          FunctionEffect |= ModRef;
444fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner
4459a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    if ((FunctionEffect & Mod) == 0)
4469a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands      ++NumReadMemFunctions;
4479a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    if (FunctionEffect == 0)
4489a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands      ++NumNoMemFunctions;
449f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands    FR.FunctionEffect = FunctionEffect;
450fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner
4519a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    // Finally, now that we know the full effect on this SCC, clone the
4529a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    // information to each function in the SCC.
4539a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    for (unsigned i = 1, e = SCC.size(); i != e; ++i)
454f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands      FunctionInfo[SCC[i]->getFunction()] = FR;
4559a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands  }
4563b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner}
4573b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
4583b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
4593b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
4603b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// alias - If one of the pointers is to a global that we are tracking, and the
4613b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// other is some random pointer, we know there cannot be an alias, because the
4623b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// address of the global isn't taken.
4633b04a8ac455568f9b98ab7e9a43076260cb737aeChris LattnerAliasAnalysis::AliasResult
4643b04a8ac455568f9b98ab7e9a43076260cb737aeChris LattnerGlobalsModRef::alias(const Value *V1, unsigned V1Size,
4653b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner                     const Value *V2, unsigned V2Size) {
466ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // Get the base object these pointers point to.
467ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  Value *UV1 = getUnderlyingObject(const_cast<Value*>(V1));
468ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  Value *UV2 = getUnderlyingObject(const_cast<Value*>(V2));
4699a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
470ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // If either of the underlying values is a global, they may be non-addr-taken
471ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // globals, which we can answer queries about.
472ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  GlobalValue *GV1 = dyn_cast<GlobalValue>(UV1);
473ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  GlobalValue *GV2 = dyn_cast<GlobalValue>(UV2);
474ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  if (GV1 || GV2) {
475ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    // If the global's address is taken, pretend we don't know it's a pointer to
476ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    // the global.
477ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    if (GV1 && !NonAddressTakenGlobals.count(GV1)) GV1 = 0;
478ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    if (GV2 && !NonAddressTakenGlobals.count(GV2)) GV2 = 0;
479ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
480ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    // If the the two pointers are derived from two different non-addr-taken
481ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    // globals, or if one is and the other isn't, we know these can't alias.
482ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    if ((GV1 || GV2) && GV1 != GV2)
483ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      return NoAlias;
484ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
485ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    // Otherwise if they are both derived from the same addr-taken global, we
486ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    // can't know the two accesses don't overlap.
487ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  }
4889a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
489ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // These pointers may be based on the memory owned by an indirect global.  If
490ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // so, we may be able to handle this.  First check to see if the base pointer
491ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // is a direct load from an indirect global.
492ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  GV1 = GV2 = 0;
493ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  if (LoadInst *LI = dyn_cast<LoadInst>(UV1))
494ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
495ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (IndirectGlobals.count(GV))
496ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        GV1 = GV;
497ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  if (LoadInst *LI = dyn_cast<LoadInst>(UV2))
498ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
499ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (IndirectGlobals.count(GV))
500ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        GV2 = GV;
5019a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
502ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // These pointers may also be from an allocation for the indirect global.  If
503ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // so, also handle them.
504ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  if (AllocsForIndirectGlobals.count(UV1))
505ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    GV1 = AllocsForIndirectGlobals[UV1];
506ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  if (AllocsForIndirectGlobals.count(UV2))
507ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    GV2 = AllocsForIndirectGlobals[UV2];
5089a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
509ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // Now that we know whether the two pointers are related to indirect globals,
510ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // use this to disambiguate the pointers.  If either pointer is based on an
511ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // indirect global and if they are not both based on the same indirect global,
512ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // they cannot alias.
5133b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  if ((GV1 || GV2) && GV1 != GV2)
5143b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    return NoAlias;
5159a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
5163b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
5173b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner}
5183b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
5193b04a8ac455568f9b98ab7e9a43076260cb737aeChris LattnerAliasAnalysis::ModRefResult
5203b04a8ac455568f9b98ab7e9a43076260cb737aeChris LattnerGlobalsModRef::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
5213b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  unsigned Known = ModRef;
5223b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
5233b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  // If we are asking for mod/ref info of a direct call with a pointer to a
524fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  // global we are tracking, return information if we have it.
525ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  if (GlobalValue *GV = dyn_cast<GlobalValue>(getUnderlyingObject(P)))
5263b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    if (GV->hasInternalLinkage())
527fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner      if (Function *F = CS.getCalledFunction())
528fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        if (NonAddressTakenGlobals.count(GV))
529fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner          if (FunctionRecord *FR = getFunctionInfo(F))
530fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner            Known = FR->getInfoForGlobal(GV);
5313b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
5323b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  if (Known == NoModRef)
5333b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    return NoModRef; // No need to query other mod/ref analyses
5343b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  return ModRefResult(Known & AliasAnalysis::getModRefInfo(CS, P, Size));
5353b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner}
5363b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
5373b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
5383b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner//===----------------------------------------------------------------------===//
5393b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner// Methods to update the analysis as a result of the client transformation.
5403b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner//
5413b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnervoid GlobalsModRef::deleteValue(Value *V) {
542ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
543ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    if (NonAddressTakenGlobals.erase(GV)) {
544ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // This global might be an indirect global.  If so, remove it and remove
545ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // any AllocRelatedValues for it.
546ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (IndirectGlobals.erase(GV)) {
547ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        // Remove any entries in AllocsForIndirectGlobals for this global.
548ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        for (std::map<Value*, GlobalValue*>::iterator
549ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner             I = AllocsForIndirectGlobals.begin(),
550ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner             E = AllocsForIndirectGlobals.end(); I != E; ) {
551ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner          if (I->second == GV) {
552ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner            AllocsForIndirectGlobals.erase(I++);
553ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner          } else {
554ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner            ++I;
555ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner          }
556ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        }
557ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      }
558ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    }
559ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  }
5609a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
561ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // Otherwise, if this is an allocation related to an indirect global, remove
562ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // it.
563ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  AllocsForIndirectGlobals.erase(V);
5649a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
565cd883f203d23c7e44b12dff62df2e6d65d8f231cChris Lattner  AliasAnalysis::deleteValue(V);
5663b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner}
5673b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
5683b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnervoid GlobalsModRef::copyValue(Value *From, Value *To) {
569cd883f203d23c7e44b12dff62df2e6d65d8f231cChris Lattner  AliasAnalysis::copyValue(From, To);
5703b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner}
571