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"
19d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/SCCIterator.h"
20d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/Statistic.h"
213b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include "llvm/Analysis/AliasAnalysis.h"
223b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include "llvm/Analysis/CallGraph.h"
23f006b183e2d2bebcf6968d1dd7350397c95b0325Victor Hernandez#include "llvm/Analysis/MemoryBuiltins.h"
245034dd318a9dfa0dc45a3ac01e58e60f2aa2498dDan Gohman#include "llvm/Analysis/ValueTracking.h"
250b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Constants.h"
260b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DerivedTypes.h"
270b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Instructions.h"
280b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/IntrinsicInst.h"
290b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Module.h"
30d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Pass.h"
31551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/CommandLine.h"
32d7d83db5f22d05e5b14b6b1d838668222113c83aReid Spencer#include "llvm/Support/InstIterator.h"
333b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner#include <set>
343b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnerusing namespace llvm;
353b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
363b27d68c6af2582df0962557f1fe5c3f70f46e3fChris LattnerSTATISTIC(NumNonAddrTakenGlobalVars,
373b27d68c6af2582df0962557f1fe5c3f70f46e3fChris Lattner          "Number of global vars without address taken");
383b27d68c6af2582df0962557f1fe5c3f70f46e3fChris LattnerSTATISTIC(NumNonAddrTakenFunctions,"Number of functions without address taken");
393b27d68c6af2582df0962557f1fe5c3f70f46e3fChris LattnerSTATISTIC(NumNoMemFunctions, "Number of functions that do not access memory");
403b27d68c6af2582df0962557f1fe5c3f70f46e3fChris LattnerSTATISTIC(NumReadMemFunctions, "Number of functions that only read memory");
413b27d68c6af2582df0962557f1fe5c3f70f46e3fChris LattnerSTATISTIC(NumIndirectGlobalVars, "Number of indirect global objects");
423b27d68c6af2582df0962557f1fe5c3f70f46e3fChris Lattner
433b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnernamespace {
44fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  /// FunctionRecord - One instance of this structure is stored for every
45fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  /// function in the program.  Later, the entries for these functions are
46fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  /// removed if the function is found to call an external function (in which
47fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  /// case we know nothing about it.
486726b6d75a8b679068a58cb954ba97cf9d1690baNick Lewycky  struct FunctionRecord {
49fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    /// GlobalInfo - Maintain mod/ref info for all of the globals without
50fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    /// addresses taken that are read or written (transitively) by this
51fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    /// function.
5279fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman    std::map<const GlobalValue*, unsigned> GlobalInfo;
53fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner
542bb4a4d31590c1d3c7b620d7ab6959af05920cd5Duncan Sands    /// MayReadAnyGlobal - May read global variables, but it is not known which.
552bb4a4d31590c1d3c7b620d7ab6959af05920cd5Duncan Sands    bool MayReadAnyGlobal;
562bb4a4d31590c1d3c7b620d7ab6959af05920cd5Duncan Sands
5779fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman    unsigned getInfoForGlobal(const GlobalValue *GV) const {
582bb4a4d31590c1d3c7b620d7ab6959af05920cd5Duncan Sands      unsigned Effect = MayReadAnyGlobal ? AliasAnalysis::Ref : 0;
5979fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman      std::map<const GlobalValue*, unsigned>::const_iterator I =
6079fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman        GlobalInfo.find(GV);
61fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner      if (I != GlobalInfo.end())
622bb4a4d31590c1d3c7b620d7ab6959af05920cd5Duncan Sands        Effect |= I->second;
632bb4a4d31590c1d3c7b620d7ab6959af05920cd5Duncan Sands      return Effect;
64fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    }
652b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman
66fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    /// FunctionEffect - Capture whether or not this function reads or writes to
67fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    /// ANY memory.  If not, we can do a lot of aggressive analysis on it.
68fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    unsigned FunctionEffect;
69105d26acb4158daea4e54919cfd12545a3d901eaChris Lattner
702bb4a4d31590c1d3c7b620d7ab6959af05920cd5Duncan Sands    FunctionRecord() : MayReadAnyGlobal (false), FunctionEffect(0) {}
71fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  };
723b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
73fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  /// GlobalsModRef - The actual analysis pass.
746726b6d75a8b679068a58cb954ba97cf9d1690baNick Lewycky  class GlobalsModRef : public ModulePass, public AliasAnalysis {
75fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    /// NonAddressTakenGlobals - The globals that do not have their addresses
76fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    /// taken.
7779fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman    std::set<const GlobalValue*> NonAddressTakenGlobals;
783b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
79ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    /// IndirectGlobals - The memory pointed to by this global is known to be
80ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    /// 'owned' by the global.
8179fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman    std::set<const GlobalValue*> IndirectGlobals;
829a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
83ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    /// AllocsForIndirectGlobals - If an instruction allocates memory for an
84ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    /// indirect global, this map indicates which one.
8579fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman    std::map<const Value*, const GlobalValue*> AllocsForIndirectGlobals;
869a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
873b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    /// FunctionInfo - For each function, keep track of what globals are
883b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    /// modified or read.
8979fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman    std::map<const Function*, FunctionRecord> FunctionInfo;
903b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
913b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  public:
921997473cf72957d0e70322e2fe6fe2ab141c58a6Devang Patel    static char ID;
93081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson    GlobalsModRef() : ModulePass(ID) {
94081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson      initializeGlobalsModRefPass(*PassRegistry::getPassRegistry());
95081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson    }
961cee94f04111cfd7114979d6dfddce2669c9380dDevang Patel
97b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner    bool runOnModule(Module &M) {
983b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      InitializeAliasAnalysis(this);                 // set up super class
993b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      AnalyzeGlobals(M);                          // find non-addr taken globals
1003b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      AnalyzeCallGraph(getAnalysis<CallGraph>(), M); // Propagate on CG
1013b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      return false;
1023b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    }
1033b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
1043b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1053b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      AliasAnalysis::getAnalysisUsage(AU);
1063b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      AU.addRequired<CallGraph>();
1073b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      AU.setPreservesAll();                         // Does not transform code
1083b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    }
1093b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
1103b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    //------------------------------------------------
1113b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    // Implement the AliasAnalysis API
1122b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman    //
113b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    AliasResult alias(const Location &LocA, const Location &LocB);
11479fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman    ModRefResult getModRefInfo(ImmutableCallSite CS,
115b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman                               const Location &Loc);
11679fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman    ModRefResult getModRefInfo(ImmutableCallSite CS1,
11779fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman                               ImmutableCallSite CS2) {
11879fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman      return AliasAnalysis::getModRefInfo(CS1, CS2);
1194a7ebfa4111305ea22fc753d4f029eed88149662Reid Spencer    }
1203b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
1210af024c5d00c2a2d0e35ba33f4aaf4ed3fadfae2Chris Lattner    /// getModRefBehavior - Return the behavior of the specified function if
1220af024c5d00c2a2d0e35ba33f4aaf4ed3fadfae2Chris Lattner    /// called from the specified call site.  The call site may be null in which
1230af024c5d00c2a2d0e35ba33f4aaf4ed3fadfae2Chris Lattner    /// case the most generic behavior of this function should be returned.
124a41af344c2964573318a77e2b43eafd4d3804003Dan Gohman    ModRefBehavior getModRefBehavior(const Function *F) {
12542c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman      ModRefBehavior Min = UnknownModRefBehavior;
12642c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman
127ae9f3a3b7c915f725aef5a7250e88eaeddda03c6Anton Korobeynikov      if (FunctionRecord *FR = getFunctionInfo(F)) {
128fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        if (FR->FunctionEffect == 0)
12942c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman          Min = DoesNotAccessMemory;
130dedf2bd5a34dac25e4245f58bb902ced6b64edd9Misha Brukman        else if ((FR->FunctionEffect & Mod) == 0)
13142c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman          Min = OnlyReadsMemory;
132ae9f3a3b7c915f725aef5a7250e88eaeddda03c6Anton Korobeynikov      }
13342c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman
13442c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman      return ModRefBehavior(AliasAnalysis::getModRefBehavior(F) & Min);
135e79422096ea5319a365d160693d0957a2a4df75eOwen Anderson    }
136e79422096ea5319a365d160693d0957a2a4df75eOwen Anderson
137e79422096ea5319a365d160693d0957a2a4df75eOwen Anderson    /// getModRefBehavior - Return the behavior of the specified function if
138e79422096ea5319a365d160693d0957a2a4df75eOwen Anderson    /// called from the specified call site.  The call site may be null in which
139e79422096ea5319a365d160693d0957a2a4df75eOwen Anderson    /// case the most generic behavior of this function should be returned.
140a41af344c2964573318a77e2b43eafd4d3804003Dan Gohman    ModRefBehavior getModRefBehavior(ImmutableCallSite CS) {
14142c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman      ModRefBehavior Min = UnknownModRefBehavior;
14242c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman
14342c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman      if (const Function* F = CS.getCalledFunction())
14442c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman        if (FunctionRecord *FR = getFunctionInfo(F)) {
14542c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman          if (FR->FunctionEffect == 0)
14642c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman            Min = DoesNotAccessMemory;
14742c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman          else if ((FR->FunctionEffect & Mod) == 0)
14842c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman            Min = OnlyReadsMemory;
14942c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman        }
15042c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman
15142c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman      return ModRefBehavior(AliasAnalysis::getModRefBehavior(CS) & Min);
152fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    }
153fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner
1543b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    virtual void deleteValue(Value *V);
1553b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    virtual void copyValue(Value *From, Value *To);
156392249fcf368a22f48091a16c180160221c7e89aOwen Anderson    virtual void addEscapingUse(Use &U);
1573b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
158fc570e4bff4e08aaf8efa9f90a2cdb5b885f321cChris Lattner    /// getAdjustedAnalysisPointer - This method is used when a pass implements
159fc570e4bff4e08aaf8efa9f90a2cdb5b885f321cChris Lattner    /// an analysis interface through multiple inheritance.  If needed, it
160fc570e4bff4e08aaf8efa9f90a2cdb5b885f321cChris Lattner    /// should override this to adjust the this pointer as needed for the
161fc570e4bff4e08aaf8efa9f90a2cdb5b885f321cChris Lattner    /// specified pass info.
16290c579de5a383cee278acc3f7e7b9d0a656e6a35Owen Anderson    virtual void *getAdjustedAnalysisPointer(AnalysisID PI) {
16390c579de5a383cee278acc3f7e7b9d0a656e6a35Owen Anderson      if (PI == &AliasAnalysis::ID)
164fc570e4bff4e08aaf8efa9f90a2cdb5b885f321cChris Lattner        return (AliasAnalysis*)this;
165fc570e4bff4e08aaf8efa9f90a2cdb5b885f321cChris Lattner      return this;
166fc570e4bff4e08aaf8efa9f90a2cdb5b885f321cChris Lattner    }
167fc570e4bff4e08aaf8efa9f90a2cdb5b885f321cChris Lattner
1683b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  private:
169fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    /// getFunctionInfo - Return the function info for the function, or null if
1709a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    /// we don't have anything useful to say about it.
17179fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman    FunctionRecord *getFunctionInfo(const Function *F) {
17279fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman      std::map<const Function*, FunctionRecord>::iterator I =
17379fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman        FunctionInfo.find(F);
174fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner      if (I != FunctionInfo.end())
175fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        return &I->second;
176fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner      return 0;
177fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    }
178fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner
1793b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    void AnalyzeGlobals(Module &M);
1803b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    void AnalyzeCallGraph(CallGraph &CG, Module &M);
181ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    bool AnalyzeUsesOfPointer(Value *V, std::vector<Function*> &Readers,
182ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner                              std::vector<Function*> &Writers,
183ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner                              GlobalValue *OkayStoreDest = 0);
184ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    bool AnalyzeIndirectGlobalMemory(GlobalValue *GV);
1853b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  };
1863b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner}
1873b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
188844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanchar GlobalsModRef::ID = 0;
1892ab36d350293c77fc8941ce1023e4899df7e3a82Owen AndersonINITIALIZE_AG_PASS_BEGIN(GlobalsModRef, AliasAnalysis,
1902ab36d350293c77fc8941ce1023e4899df7e3a82Owen Anderson                "globalsmodref-aa", "Simple mod/ref analysis for globals",
1912ab36d350293c77fc8941ce1023e4899df7e3a82Owen Anderson                false, true, false)
1922ab36d350293c77fc8941ce1023e4899df7e3a82Owen AndersonINITIALIZE_AG_DEPENDENCY(CallGraph)
1932ab36d350293c77fc8941ce1023e4899df7e3a82Owen AndersonINITIALIZE_AG_PASS_END(GlobalsModRef, AliasAnalysis,
19402dd53e1c5b941ca5f60fca1b95ebcaf9ccd1dfcOwen Anderson                "globalsmodref-aa", "Simple mod/ref analysis for globals",
195ce665bd2e2b581ab0858d1afe359192bac96b868Owen Anderson                false, true, false)
196844731a7f1909f55935e3514c9e713a62d67662eDan Gohman
1973b04a8ac455568f9b98ab7e9a43076260cb737aeChris LattnerPass *llvm::createGlobalsModRefPass() { return new GlobalsModRef(); }
1983b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
199ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// AnalyzeGlobals - Scan through the users of all of the internal
2009a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands/// GlobalValue's in the program.  If none of them have their "address taken"
2013b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// (really, their address passed to something nontrivial), record this fact,
2023b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// and record the functions that they are used directly in.
2033b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnervoid GlobalsModRef::AnalyzeGlobals(Module &M) {
2043b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  std::vector<Function*> Readers, Writers;
2053b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
206bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola    if (I->hasLocalLinkage()) {
207ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (!AnalyzeUsesOfPointer(I, Readers, Writers)) {
208fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        // Remember that we are tracking this global.
209fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        NonAddressTakenGlobals.insert(I);
2103b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner        ++NumNonAddrTakenFunctions;
2113b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      }
2123b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      Readers.clear(); Writers.clear();
2133b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    }
2143b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
215f5eaf3c62c7b2f2da1f85ac411c4f13bcbf131f1Chris Lattner  for (Module::global_iterator I = M.global_begin(), E = M.global_end();
216f5eaf3c62c7b2f2da1f85ac411c4f13bcbf131f1Chris Lattner       I != E; ++I)
217bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola    if (I->hasLocalLinkage()) {
218ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (!AnalyzeUsesOfPointer(I, Readers, Writers)) {
2193b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner        // Remember that we are tracking this global, and the mod/ref fns
220fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        NonAddressTakenGlobals.insert(I);
2219a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
222fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        for (unsigned i = 0, e = Readers.size(); i != e; ++i)
223fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner          FunctionInfo[Readers[i]].GlobalInfo[I] |= Ref;
224fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner
225fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        if (!I->isConstant())  // No need to keep track of writers to constants
226fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner          for (unsigned i = 0, e = Writers.size(); i != e; ++i)
227fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner            FunctionInfo[Writers[i]].GlobalInfo[I] |= Mod;
2283b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner        ++NumNonAddrTakenGlobalVars;
2299a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
230ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        // If this global holds a pointer type, see if it is an indirect global.
2311df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands        if (I->getType()->getElementType()->isPointerTy() &&
232ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner            AnalyzeIndirectGlobalMemory(I))
233ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner          ++NumIndirectGlobalVars;
2343b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      }
2353b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      Readers.clear(); Writers.clear();
2363b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    }
2373b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner}
2383b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
239ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// AnalyzeUsesOfPointer - Look at all of the users of the specified pointer.
240ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// If this is used by anything complex (i.e., the address escapes), return
241ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// true.  Also, while we are at it, keep track of those functions that read and
242ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// write to the value.
243ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner///
244ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// If OkayStoreDest is non-null, stores into this global are allowed.
245ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattnerbool GlobalsModRef::AnalyzeUsesOfPointer(Value *V,
246ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner                                         std::vector<Function*> &Readers,
247ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner                                         std::vector<Function*> &Writers,
248ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner                                         GlobalValue *OkayStoreDest) {
2491df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands  if (!V->getType()->isPointerTy()) return true;
2503b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
251d7cc5215f131a4df933c750595d7c642df3ee5b8Gabor Greif  for (Value::use_iterator UI = V->use_begin(), E=V->use_end(); UI != E; ++UI) {
252d7cc5215f131a4df933c750595d7c642df3ee5b8Gabor Greif    User *U = *UI;
253d7cc5215f131a4df933c750595d7c642df3ee5b8Gabor Greif    if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
2543b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      Readers.push_back(LI->getParent()->getParent());
255d7cc5215f131a4df933c750595d7c642df3ee5b8Gabor Greif    } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
256ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (V == SI->getOperand(1)) {
257ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        Writers.push_back(SI->getParent()->getParent());
258ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      } else if (SI->getOperand(1) != OkayStoreDest) {
259ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        return true;  // Storing the pointer
260ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      }
261d7cc5215f131a4df933c750595d7c642df3ee5b8Gabor Greif    } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
262ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (AnalyzeUsesOfPointer(GEP, Readers, Writers)) return true;
263d7cc5215f131a4df933c750595d7c642df3ee5b8Gabor Greif    } else if (BitCastInst *BCI = dyn_cast<BitCastInst>(U)) {
26446e8312fb733338e9af4db3757a1a8beddeae15aVictor Hernandez      if (AnalyzeUsesOfPointer(BCI, Readers, Writers, OkayStoreDest))
26546e8312fb733338e9af4db3757a1a8beddeae15aVictor Hernandez        return true;
2668e0d1c03ca7fd86e6879b4e37d0d7f0e982feef6Benjamin Kramer    } else if (isFreeCall(U, TLI)) {
267d7cc5215f131a4df933c750595d7c642df3ee5b8Gabor Greif      Writers.push_back(cast<Instruction>(U)->getParent()->getParent());
268d7cc5215f131a4df933c750595d7c642df3ee5b8Gabor Greif    } else if (CallInst *CI = dyn_cast<CallInst>(U)) {
2693b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      // Make sure that this is just the function being called, not that it is
2703b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      // passing into the function.
27122a5b298201d62daea3e06718469d8ad017895e1Bill Wendling      for (unsigned i = 0, e = CI->getNumArgOperands(); i != e; ++i)
27222a5b298201d62daea3e06718469d8ad017895e1Bill Wendling        if (CI->getArgOperand(i) == V) return true;
273d7cc5215f131a4df933c750595d7c642df3ee5b8Gabor Greif    } else if (InvokeInst *II = dyn_cast<InvokeInst>(U)) {
2743b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      // Make sure that this is just the function being called, not that it is
2753b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      // passing into the function.
27622a5b298201d62daea3e06718469d8ad017895e1Bill Wendling      for (unsigned i = 0, e = II->getNumArgOperands(); i != e; ++i)
27722a5b298201d62daea3e06718469d8ad017895e1Bill Wendling        if (II->getArgOperand(i) == V) return true;
278d7cc5215f131a4df933c750595d7c642df3ee5b8Gabor Greif    } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
2799a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands      if (CE->getOpcode() == Instruction::GetElementPtr ||
2803da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer          CE->getOpcode() == Instruction::BitCast) {
281ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        if (AnalyzeUsesOfPointer(CE, Readers, Writers))
2823b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner          return true;
2833b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      } else {
2843b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner        return true;
2852b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman      }
286d7cc5215f131a4df933c750595d7c642df3ee5b8Gabor Greif    } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(U)) {
287e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer      if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
288ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        return true;  // Allow comparison against null.
2893b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    } else {
2903b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      return true;
2913b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    }
292d7cc5215f131a4df933c750595d7c642df3ee5b8Gabor Greif  }
293d7cc5215f131a4df933c750595d7c642df3ee5b8Gabor Greif
2943b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  return false;
2953b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner}
2963b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
297ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// AnalyzeIndirectGlobalMemory - We found an non-address-taken global variable
298ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// which holds a pointer type.  See if the global always points to non-aliased
299ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// heap memory: that is, all initializers of the globals are allocations, and
300ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// those allocations have no use other than initialization of the global.
301ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// Further, all loads out of GV must directly use the memory, not store the
302ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// pointer somewhere.  If this is true, we consider the memory pointed to by
303ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner/// GV to be owned by GV and can disambiguate other pointers from it.
304ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattnerbool GlobalsModRef::AnalyzeIndirectGlobalMemory(GlobalValue *GV) {
305ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // Keep track of values related to the allocation of the memory, f.e. the
306ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // value produced by the malloc call and any casts.
307ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  std::vector<Value*> AllocRelatedValues;
3089a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
309ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // Walk the user list of the global.  If we find anything other than a direct
310ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // load or store, bail out.
311ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  for (Value::use_iterator I = GV->use_begin(), E = GV->use_end(); I != E; ++I){
3128ba992ad527e50060083e83433b7125bbaa6b737Gabor Greif    User *U = *I;
3138ba992ad527e50060083e83433b7125bbaa6b737Gabor Greif    if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
314ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // The pointer loaded from the global can only be used in simple ways:
315ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // we allow addressing of it and loading storing to it.  We do *not* allow
316ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // storing the loaded pointer somewhere else or passing to a function.
317ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      std::vector<Function*> ReadersWriters;
318ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (AnalyzeUsesOfPointer(LI, ReadersWriters, ReadersWriters))
319ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        return false;  // Loaded pointer escapes.
320ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // TODO: Could try some IP mod/ref of the loaded pointer.
3218ba992ad527e50060083e83433b7125bbaa6b737Gabor Greif    } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
322ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // Storing the global itself.
323ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (SI->getOperand(0) == GV) return false;
3249a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
325ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // If storing the null pointer, ignore it.
326ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (isa<ConstantPointerNull>(SI->getOperand(0)))
327ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        continue;
3289a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
329ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // Check the value being stored.
3305034dd318a9dfa0dc45a3ac01e58e60f2aa2498dDan Gohman      Value *Ptr = GetUnderlyingObject(SI->getOperand(0));
331ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
3328e0d1c03ca7fd86e6879b4e37d0d7f0e982feef6Benjamin Kramer      if (!isAllocLikeFn(Ptr, TLI))
333ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        return false;  // Too hard to analyze.
3349a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
335ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // Analyze all uses of the allocation.  If any of them are used in a
336ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // non-simple way (e.g. stored to another global) bail out.
337ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      std::vector<Function*> ReadersWriters;
338ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (AnalyzeUsesOfPointer(Ptr, ReadersWriters, ReadersWriters, GV))
339ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        return false;  // Loaded pointer escapes.
340ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
341ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // Remember that this allocation is related to the indirect global.
342ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      AllocRelatedValues.push_back(Ptr);
343ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    } else {
344ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // Something complex, bail out.
345ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      return false;
346ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    }
347ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  }
3489a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
349ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // Okay, this is an indirect global.  Remember all of the allocations for
350ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // this global in AllocsForIndirectGlobals.
351ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  while (!AllocRelatedValues.empty()) {
352ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV;
353ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    AllocRelatedValues.pop_back();
354ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  }
355ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  IndirectGlobals.insert(GV);
356ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  return true;
357ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner}
358ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
3593b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// AnalyzeCallGraph - At this point, we know the functions where globals are
3603b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// immediately stored to and read from.  Propagate this information up the call
361fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner/// graph to all callers and compute the mod/ref info for all memory for each
3622b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman/// function.
3633b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnervoid GlobalsModRef::AnalyzeCallGraph(CallGraph &CG, Module &M) {
3643b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  // We do a bottom-up SCC traversal of the call graph.  In other words, we
3653b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  // visit all callees before callers (leaf-first).
3669a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands  for (scc_iterator<CallGraph*> I = scc_begin(&CG), E = scc_end(&CG); I != E;
3679a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands       ++I) {
3689a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    std::vector<CallGraphNode *> &SCC = *I;
369f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands    assert(!SCC.empty() && "SCC with no functions?");
3709a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
371f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands    if (!SCC[0]->getFunction()) {
372f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands      // Calls externally - can't say anything useful.  Remove any existing
373f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands      // function records (may have been created when scanning globals).
374f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands      for (unsigned i = 0, e = SCC.size(); i != e; ++i)
375f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands        FunctionInfo.erase(SCC[i]->getFunction());
37634f2a0ca17f00f9ce2081ee278685a007856fd90Duncan Sands      continue;
377f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands    }
378f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands
379f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands    FunctionRecord &FR = FunctionInfo[SCC[0]->getFunction()];
3803b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
3819a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    bool KnowNothing = false;
3829a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    unsigned FunctionEffect = 0;
3839a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
3849a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    // Collect the mod/ref properties due to called functions.  We only compute
3859a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    // one mod-ref set.
3869a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    for (unsigned i = 0, e = SCC.size(); i != e && !KnowNothing; ++i) {
3879a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands      Function *F = SCC[i]->getFunction();
3889a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands      if (!F) {
3899a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands        KnowNothing = true;
390fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        break;
3913b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner      }
392fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner
3939a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands      if (F->isDeclaration()) {
3949a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands        // Try to get mod/ref behaviour from function attributes.
395d0ac373660de64fe210e50458c7702432b3f9605Duncan Sands        if (F->doesNotAccessMemory()) {
396d0ac373660de64fe210e50458c7702432b3f9605Duncan Sands          // Can't do better than that!
397d0ac373660de64fe210e50458c7702432b3f9605Duncan Sands        } else if (F->onlyReadsMemory()) {
3989a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands          FunctionEffect |= Ref;
3992bb4a4d31590c1d3c7b620d7ab6959af05920cd5Duncan Sands          if (!F->isIntrinsic())
4001abe60b9be1b7b33e1fa422add5296d392831850Duncan Sands            // This function might call back into the module and read a global -
4012bb4a4d31590c1d3c7b620d7ab6959af05920cd5Duncan Sands            // consider every global as possibly being read by this function.
4022bb4a4d31590c1d3c7b620d7ab6959af05920cd5Duncan Sands            FR.MayReadAnyGlobal = true;
403d0ac373660de64fe210e50458c7702432b3f9605Duncan Sands        } else {
404892b84007c980e58181cd94fea7d70c5ed9642c8Duncan Sands          FunctionEffect |= ModRef;
405892b84007c980e58181cd94fea7d70c5ed9642c8Duncan Sands          // Can't say anything useful unless it's an intrinsic - they don't
406892b84007c980e58181cd94fea7d70c5ed9642c8Duncan Sands          // read or write global variables of the kind considered here.
407892b84007c980e58181cd94fea7d70c5ed9642c8Duncan Sands          KnowNothing = !F->isIntrinsic();
4089a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands        }
4099a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands        continue;
4109a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands      }
4119a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
4129a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands      for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end();
413f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands           CI != E && !KnowNothing; ++CI)
4149a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands        if (Function *Callee = CI->second->getFunction()) {
4159a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands          if (FunctionRecord *CalleeFR = getFunctionInfo(Callee)) {
4169a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands            // Propagate function effect up.
4179a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands            FunctionEffect |= CalleeFR->FunctionEffect;
4189a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
4199a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands            // Incorporate callee's effects on globals into our info.
42079fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman            for (std::map<const GlobalValue*, unsigned>::iterator GI =
4219a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands                   CalleeFR->GlobalInfo.begin(), E = CalleeFR->GlobalInfo.end();
4229a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands                 GI != E; ++GI)
423f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands              FR.GlobalInfo[GI->first] |= GI->second;
4242bb4a4d31590c1d3c7b620d7ab6959af05920cd5Duncan Sands            FR.MayReadAnyGlobal |= CalleeFR->MayReadAnyGlobal;
4259a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands          } else {
4269a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands            // Can't say anything about it.  However, if it is inside our SCC,
4279a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands            // then nothing needs to be done.
4289a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands            CallGraphNode *CalleeNode = CG[Callee];
4299a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands            if (std::find(SCC.begin(), SCC.end(), CalleeNode) == SCC.end())
4309a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands              KnowNothing = true;
4319a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands          }
4329a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands        } else {
4339a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands          KnowNothing = true;
4349a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands        }
4359a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    }
4369a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
4379a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    // If we can't say anything useful about this SCC, remove all SCC functions
4389a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    // from the FunctionInfo map.
4399a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    if (KnowNothing) {
4409a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands      for (unsigned i = 0, e = SCC.size(); i != e; ++i)
4419a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands        FunctionInfo.erase(SCC[i]->getFunction());
442b070beee77cf9a0befd06a4fdacb824b1da0b55aDuncan Sands      continue;
4439a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    }
4442b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman
4459a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    // Scan the function bodies for explicit loads or stores.
446fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner    for (unsigned i = 0, e = SCC.size(); i != e && FunctionEffect != ModRef;++i)
447fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner      for (inst_iterator II = inst_begin(SCC[i]->getFunction()),
4482b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman             E = inst_end(SCC[i]->getFunction());
449fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner           II != E && FunctionEffect != ModRef; ++II)
4509e72a79ef4a9fcda482ce0b0e1f0bd6a4f16cffdNuno Lopes        if (LoadInst *LI = dyn_cast<LoadInst>(&*II)) {
451fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner          FunctionEffect |= Ref;
4529e72a79ef4a9fcda482ce0b0e1f0bd6a4f16cffdNuno Lopes          if (LI->isVolatile())
453b8ca4ff6439c1543a0c6729cbcd6c70c3be7dc3cDuncan Sands            // Volatile loads may have side-effects, so mark them as writing
454b8ca4ff6439c1543a0c6729cbcd6c70c3be7dc3cDuncan Sands            // memory (for example, a flag inside the processor).
455b8ca4ff6439c1543a0c6729cbcd6c70c3be7dc3cDuncan Sands            FunctionEffect |= Mod;
4569e72a79ef4a9fcda482ce0b0e1f0bd6a4f16cffdNuno Lopes        } else if (StoreInst *SI = dyn_cast<StoreInst>(&*II)) {
457fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner          FunctionEffect |= Mod;
4589e72a79ef4a9fcda482ce0b0e1f0bd6a4f16cffdNuno Lopes          if (SI->isVolatile())
459b8ca4ff6439c1543a0c6729cbcd6c70c3be7dc3cDuncan Sands            // Treat volatile stores as reading memory somewhere.
460b8ca4ff6439c1543a0c6729cbcd6c70c3be7dc3cDuncan Sands            FunctionEffect |= Ref;
4618e0d1c03ca7fd86e6879b4e37d0d7f0e982feef6Benjamin Kramer        } else if (isAllocationFn(&*II, TLI) || isFreeCall(&*II, TLI)) {
4623fe4d3cb5bd5ea7948faeed8451b61380d928808Chris Lattner          FunctionEffect |= ModRef;
4638f3fabe0febb7335c4349d3d6081deca95419d48Rafael Espindola        } else if (IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(&*II)) {
4648f3fabe0febb7335c4349d3d6081deca95419d48Rafael Espindola          // The callgraph doesn't include intrinsic calls.
4658f3fabe0febb7335c4349d3d6081deca95419d48Rafael Espindola          Function *Callee = Intrinsic->getCalledFunction();
4668f3fabe0febb7335c4349d3d6081deca95419d48Rafael Espindola          ModRefBehavior Behaviour = AliasAnalysis::getModRefBehavior(Callee);
4678f3fabe0febb7335c4349d3d6081deca95419d48Rafael Espindola          FunctionEffect |= (Behaviour & ModRef);
468b8ca4ff6439c1543a0c6729cbcd6c70c3be7dc3cDuncan Sands        }
469fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner
4709a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    if ((FunctionEffect & Mod) == 0)
4719a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands      ++NumReadMemFunctions;
4729a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    if (FunctionEffect == 0)
4739a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands      ++NumNoMemFunctions;
474f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands    FR.FunctionEffect = FunctionEffect;
475fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner
4769a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    // Finally, now that we know the full effect on this SCC, clone the
4779a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    // information to each function in the SCC.
4789a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands    for (unsigned i = 1, e = SCC.size(); i != e; ++i)
479f423abc289bca6c0b0cbb3c28bfb2f844bc7398fDuncan Sands      FunctionInfo[SCC[i]->getFunction()] = FR;
4809a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands  }
4813b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner}
4823b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
4833b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
4843b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
4853b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// alias - If one of the pointers is to a global that we are tracking, and the
4863b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// other is some random pointer, we know there cannot be an alias, because the
4873b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner/// address of the global isn't taken.
4883b04a8ac455568f9b98ab7e9a43076260cb737aeChris LattnerAliasAnalysis::AliasResult
489b2143b6247901ae4eca2192ee134564c4f5f7853Dan GohmanGlobalsModRef::alias(const Location &LocA,
490b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman                     const Location &LocB) {
491ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // Get the base object these pointers point to.
4925034dd318a9dfa0dc45a3ac01e58e60f2aa2498dDan Gohman  const Value *UV1 = GetUnderlyingObject(LocA.Ptr);
4935034dd318a9dfa0dc45a3ac01e58e60f2aa2498dDan Gohman  const Value *UV2 = GetUnderlyingObject(LocB.Ptr);
4949a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
495ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // If either of the underlying values is a global, they may be non-addr-taken
496ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // globals, which we can answer queries about.
49779fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman  const GlobalValue *GV1 = dyn_cast<GlobalValue>(UV1);
49879fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman  const GlobalValue *GV2 = dyn_cast<GlobalValue>(UV2);
499ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  if (GV1 || GV2) {
500ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    // If the global's address is taken, pretend we don't know it's a pointer to
501ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    // the global.
502ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    if (GV1 && !NonAddressTakenGlobals.count(GV1)) GV1 = 0;
503ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    if (GV2 && !NonAddressTakenGlobals.count(GV2)) GV2 = 0;
504ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
505f451cb870efcf9e0302d25ed05f4cac6bb494e42Dan Gohman    // If the two pointers are derived from two different non-addr-taken
506ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    // globals, or if one is and the other isn't, we know these can't alias.
507ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    if ((GV1 || GV2) && GV1 != GV2)
508ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      return NoAlias;
509ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner
510ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    // Otherwise if they are both derived from the same addr-taken global, we
511ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    // can't know the two accesses don't overlap.
512ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  }
5139a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
514ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // These pointers may be based on the memory owned by an indirect global.  If
515ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // so, we may be able to handle this.  First check to see if the base pointer
516ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // is a direct load from an indirect global.
517ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  GV1 = GV2 = 0;
51879fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman  if (const LoadInst *LI = dyn_cast<LoadInst>(UV1))
519ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
520ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (IndirectGlobals.count(GV))
521ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        GV1 = GV;
52279fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman  if (const LoadInst *LI = dyn_cast<LoadInst>(UV2))
52379fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman    if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
524ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (IndirectGlobals.count(GV))
525ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        GV2 = GV;
5269a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
527ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // These pointers may also be from an allocation for the indirect global.  If
528ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // so, also handle them.
529ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  if (AllocsForIndirectGlobals.count(UV1))
530ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    GV1 = AllocsForIndirectGlobals[UV1];
531ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  if (AllocsForIndirectGlobals.count(UV2))
532ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    GV2 = AllocsForIndirectGlobals[UV2];
5339a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
534ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // Now that we know whether the two pointers are related to indirect globals,
535ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // use this to disambiguate the pointers.  If either pointer is based on an
536ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // indirect global and if they are not both based on the same indirect global,
537ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // they cannot alias.
5383b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  if ((GV1 || GV2) && GV1 != GV2)
5393b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    return NoAlias;
5409a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
541b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  return AliasAnalysis::alias(LocA, LocB);
5423b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner}
5433b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
5443b04a8ac455568f9b98ab7e9a43076260cb737aeChris LattnerAliasAnalysis::ModRefResult
54579fca6fea87be7221843031870bbf2c9ae1fc555Dan GohmanGlobalsModRef::getModRefInfo(ImmutableCallSite CS,
546b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman                             const Location &Loc) {
5473b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  unsigned Known = ModRef;
5483b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
5493b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  // If we are asking for mod/ref info of a direct call with a pointer to a
550fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner  // global we are tracking, return information if we have it.
551b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  if (const GlobalValue *GV =
5525034dd318a9dfa0dc45a3ac01e58e60f2aa2498dDan Gohman        dyn_cast<GlobalValue>(GetUnderlyingObject(Loc.Ptr)))
553bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola    if (GV->hasLocalLinkage())
55479fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman      if (const Function *F = CS.getCalledFunction())
555fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner        if (NonAddressTakenGlobals.count(GV))
55679fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman          if (const FunctionRecord *FR = getFunctionInfo(F))
557fe98f27e807b478d85240240cb19b6673aaaafb8Chris Lattner            Known = FR->getInfoForGlobal(GV);
5583b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
5593b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner  if (Known == NoModRef)
5603b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner    return NoModRef; // No need to query other mod/ref analyses
561b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  return ModRefResult(Known & AliasAnalysis::getModRefInfo(CS, Loc));
5623b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner}
5633b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
5643b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
5653b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner//===----------------------------------------------------------------------===//
5663b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner// Methods to update the analysis as a result of the client transformation.
5673b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner//
5683b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnervoid GlobalsModRef::deleteValue(Value *V) {
569ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
570ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    if (NonAddressTakenGlobals.erase(GV)) {
571ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // This global might be an indirect global.  If so, remove it and remove
572ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      // any AllocRelatedValues for it.
573ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      if (IndirectGlobals.erase(GV)) {
574ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        // Remove any entries in AllocsForIndirectGlobals for this global.
57579fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman        for (std::map<const Value*, const GlobalValue*>::iterator
576ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner             I = AllocsForIndirectGlobals.begin(),
577ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner             E = AllocsForIndirectGlobals.end(); I != E; ) {
578ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner          if (I->second == GV) {
579ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner            AllocsForIndirectGlobals.erase(I++);
580ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner          } else {
581ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner            ++I;
582ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner          }
583ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner        }
584ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner      }
585ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner    }
586ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  }
5879a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
588ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // Otherwise, if this is an allocation related to an indirect global, remove
589ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  // it.
590ab38358fa08bcb4c7c1fc1409bc01408b49de933Chris Lattner  AllocsForIndirectGlobals.erase(V);
5919a036b945c67aeb44093c6c515e4b85e21094335Duncan Sands
592cd883f203d23c7e44b12dff62df2e6d65d8f231cChris Lattner  AliasAnalysis::deleteValue(V);
5933b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner}
5943b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner
5953b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattnervoid GlobalsModRef::copyValue(Value *From, Value *To) {
596cd883f203d23c7e44b12dff62df2e6d65d8f231cChris Lattner  AliasAnalysis::copyValue(From, To);
5973b04a8ac455568f9b98ab7e9a43076260cb737aeChris Lattner}
598392249fcf368a22f48091a16c180160221c7e89aOwen Anderson
599392249fcf368a22f48091a16c180160221c7e89aOwen Andersonvoid GlobalsModRef::addEscapingUse(Use &U) {
600392249fcf368a22f48091a16c180160221c7e89aOwen Anderson  // For the purposes of this analysis, it is conservatively correct to treat
601392249fcf368a22f48091a16c180160221c7e89aOwen Anderson  // a newly escaping value equivalently to a deleted one.  We could perhaps
602392249fcf368a22f48091a16c180160221c7e89aOwen Anderson  // be more precise by processing the new use and attempting to update our
6037a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner  // saved analysis results to accommodate it.
604392249fcf368a22f48091a16c180160221c7e89aOwen Anderson  deleteValue(U);
605392249fcf368a22f48091a16c180160221c7e89aOwen Anderson
606392249fcf368a22f48091a16c180160221c7e89aOwen Anderson  AliasAnalysis::addEscapingUse(U);
607392249fcf368a22f48091a16c180160221c7e89aOwen Anderson}
608