GlobalsModRef.cpp revision 5cbf985dcbc89fba3208e7baf8b6f488b06d3ec9
121e463b2bf864671a87ebe386cb100ef9349a540Nate Begeman//===- GlobalsModRef.cpp - Simple Mod/Ref Analysis for Globals ------------===//
2b5f662fa0314f7e7e690aae8ebff7136cc3a5ab0Misha Brukman//
3f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman//                     The LLVM Compiler Infrastructure
4f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file was developed by the LLVM research group and is distributed under
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// the University of Illinois Open Source License. See LICENSE.TXT for details.
7b5f662fa0314f7e7e690aae8ebff7136cc3a5ab0Misha Brukman//
8f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman//===----------------------------------------------------------------------===//
9f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman//
10f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman// This simple pass provides alias and mod/ref information for global values
11f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman// that do not have their address taken, and keeps track of whether functions
12f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman// read or write memory (are "pure").  For this simple (but very common) case,
13f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman// we can provide pretty accurate and useful information.
1416e71f2f70811c69c56052dd146324fe20e31db5Chris Lattner//
15df4ed6350b2a51f71c0980e86c9078f4046ea706Chris Lattner//===----------------------------------------------------------------------===//
164c7b43b43fdf943c7298718e15ab5d6dfe345be7Chris Lattner
17b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner#define DEBUG_TYPE "globalsmodref-aa"
18718cb665ca6ce2bc4d8e8479f46a45db91b49f86Owen Anderson#include "llvm/Analysis/Passes.h"
19f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman#include "llvm/Module.h"
20f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman#include "llvm/Pass.h"
21f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman#include "llvm/Instructions.h"
22b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner#include "llvm/Constants.h"
23718cb665ca6ce2bc4d8e8479f46a45db91b49f86Owen Anderson#include "llvm/DerivedTypes.h"
247ce45783531cfa81bfd7be561ea7e4738e8c6ca8Evan Cheng#include "llvm/Analysis/AliasAnalysis.h"
25b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner#include "llvm/Analysis/CallGraph.h"
26b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner#include "llvm/Support/InstIterator.h"
27b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner#include "llvm/Support/CommandLine.h"
28b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner#include "llvm/ADT/Statistic.h"
29b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner#include "llvm/ADT/SCCIterator.h"
30b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner#include <set>
31b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattnerusing namespace llvm;
32b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner
33b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris LattnerSTATISTIC(NumNonAddrTakenGlobalVars,
34b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner          "Number of global vars without address taken");
35f2ccb77ee9d8ab35866dae111fa36929689c7511Misha BrukmanSTATISTIC(NumNonAddrTakenFunctions,"Number of functions without address taken");
3621e463b2bf864671a87ebe386cb100ef9349a540Nate BegemanSTATISTIC(NumNoMemFunctions, "Number of functions that do not access memory");
3721e463b2bf864671a87ebe386cb100ef9349a540Nate BegemanSTATISTIC(NumReadMemFunctions, "Number of functions that only read memory");
3821e463b2bf864671a87ebe386cb100ef9349a540Nate BegemanSTATISTIC(NumIndirectGlobalVars, "Number of indirect global objects");
39f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman
40b410dc99774d52b4491750dab10b91cca1d661d8Chris Lattnernamespace {
4114c09b81ead8fe8b754fca2d0a8237cb810b37d6Chris Lattner  /// FunctionRecord - One instance of this structure is stored for every
421e341729dd003ca33ecea4abf13134f20062c5f8Evan Cheng  /// function in the program.  Later, the entries for these functions are
43f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman  /// removed if the function is found to call an external function (in which
44f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman  /// case we know nothing about it.
45f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman  struct FunctionRecord {
46f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    /// GlobalInfo - Maintain mod/ref info for all of the globals without
47f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    /// addresses taken that are read or written (transitively) by this
48f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    /// function.
49f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    std::map<GlobalValue*, unsigned> GlobalInfo;
50f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman
51f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    unsigned getInfoForGlobal(GlobalValue *GV) const {
52f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman      std::map<GlobalValue*, unsigned>::const_iterator I = GlobalInfo.find(GV);
531e341729dd003ca33ecea4abf13134f20062c5f8Evan Cheng      if (I != GlobalInfo.end())
54f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman        return I->second;
55f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman      return 0;
56f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    }
579a1ceaedc282f0cae31f2723f4d6c00c7b88fe90Chris Lattner
58f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    /// FunctionEffect - Capture whether or not this function reads or writes to
59f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    /// ANY memory.  If not, we can do a lot of aggressive analysis on it.
60f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    unsigned FunctionEffect;
61f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman
62cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman    FunctionRecord() : FunctionEffect(0) {}
631e341729dd003ca33ecea4abf13134f20062c5f8Evan Cheng  };
64cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman
65cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman  /// GlobalsModRef - The actual analysis pass.
66cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman  class GlobalsModRef : public ModulePass, public AliasAnalysis {
67cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman    /// NonAddressTakenGlobals - The globals that do not have their addresses
689a1ceaedc282f0cae31f2723f4d6c00c7b88fe90Chris Lattner    /// taken.
69cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman    std::set<GlobalValue*> NonAddressTakenGlobals;
70cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman
71cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman    /// IndirectGlobals - The memory pointed to by this global is known to be
72cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman    /// 'owned' by the global.
73eb5d47d99db0d9e4fc11f136fbacbd507c71a4c2Chris Lattner    std::set<GlobalValue*> IndirectGlobals;
74eb5d47d99db0d9e4fc11f136fbacbd507c71a4c2Chris Lattner
751e341729dd003ca33ecea4abf13134f20062c5f8Evan Cheng    /// AllocsForIndirectGlobals - If an instruction allocates memory for an
76f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    /// indirect global, this map indicates which one.
77f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    std::map<Value*, GlobalValue*> AllocsForIndirectGlobals;
78f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman
79f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    /// FunctionInfo - For each function, keep track of what globals are
80f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    /// modified or read.
81f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    std::map<Function*, FunctionRecord> FunctionInfo;
827af0248af47fbd86ec65d308adda22ec367accc4Nate Begeman
831e341729dd003ca33ecea4abf13134f20062c5f8Evan Cheng  public:
847af0248af47fbd86ec65d308adda22ec367accc4Nate Begeman    bool runOnModule(Module &M) {
857af0248af47fbd86ec65d308adda22ec367accc4Nate Begeman      InitializeAliasAnalysis(this);                 // set up super class
867af0248af47fbd86ec65d308adda22ec367accc4Nate Begeman      AnalyzeGlobals(M);                          // find non-addr taken globals
877af0248af47fbd86ec65d308adda22ec367accc4Nate Begeman      AnalyzeCallGraph(getAnalysis<CallGraph>(), M); // Propagate on CG
887af0248af47fbd86ec65d308adda22ec367accc4Nate Begeman      return false;
897af0248af47fbd86ec65d308adda22ec367accc4Nate Begeman    }
90f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman
91f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
92f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman      AliasAnalysis::getAnalysisUsage(AU);
93043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner      AU.addRequired<CallGraph>();
94408396014742a05cad1c91949d2226169e3f9d80Chris Lattner      AU.setPreservesAll();                         // Does not transform code
959c09c9ec9dab61450800b42cbf746164aa076b88Chris Lattner    }
96408396014742a05cad1c91949d2226169e3f9d80Chris Lattner
97408396014742a05cad1c91949d2226169e3f9d80Chris Lattner    //------------------------------------------------
98408396014742a05cad1c91949d2226169e3f9d80Chris Lattner    // Implement the AliasAnalysis API
99408396014742a05cad1c91949d2226169e3f9d80Chris Lattner    //
100408396014742a05cad1c91949d2226169e3f9d80Chris Lattner    AliasResult alias(const Value *V1, unsigned V1Size,
101408396014742a05cad1c91949d2226169e3f9d80Chris Lattner                      const Value *V2, unsigned V2Size);
1028aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner    ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
1038aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner    ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
1048aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner      return AliasAnalysis::getModRefInfo(CS1,CS2);
105408396014742a05cad1c91949d2226169e3f9d80Chris Lattner    }
106408396014742a05cad1c91949d2226169e3f9d80Chris Lattner    bool hasNoModRefInfoForCalls() const { return false; }
107408396014742a05cad1c91949d2226169e3f9d80Chris Lattner
108408396014742a05cad1c91949d2226169e3f9d80Chris Lattner    /// getModRefBehavior - Return the behavior of the specified function if
109408396014742a05cad1c91949d2226169e3f9d80Chris Lattner    /// called from the specified call site.  The call site may be null in which
1106524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner    /// case the most generic behavior of this function should be returned.
111408396014742a05cad1c91949d2226169e3f9d80Chris Lattner    virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS,
1126524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner                                         std::vector<PointerAccessInfo> *Info) {
1136524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner      if (FunctionRecord *FR = getFunctionInfo(F))
1146524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner        if (FR->FunctionEffect == 0)
1156524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner          return DoesNotAccessMemory;
1163b478b31e297208ef2c9f74750a8a603eb3726fbNate Begeman        else if ((FR->FunctionEffect & Mod) == 0)
1176524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner          return OnlyReadsMemory;
1186524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner      return AliasAnalysis::getModRefBehavior(F, CS, Info);
1196524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner    }
1208aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner
1218aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner    virtual void deleteValue(Value *V);
1228aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner    virtual void copyValue(Value *From, Value *To);
1236524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner
1246524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner  private:
1256524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner    /// getFunctionInfo - Return the function info for the function, or null if
1266524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner    /// the function calls an external function (in which case we don't have
1276524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner    /// anything useful to say about it).
1286524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner    FunctionRecord *getFunctionInfo(Function *F) {
129408396014742a05cad1c91949d2226169e3f9d80Chris Lattner      std::map<Function*, FunctionRecord>::iterator I = FunctionInfo.find(F);
130043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner      if (I != FunctionInfo.end())
131043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner        return &I->second;
13221e463b2bf864671a87ebe386cb100ef9349a540Nate Begeman      return 0;
133043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner    }
134043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner
135043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner    void AnalyzeGlobals(Module &M);
136043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner    void AnalyzeCallGraph(CallGraph &CG, Module &M);
137043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner    void AnalyzeSCC(std::vector<CallGraphNode *> &SCC);
1389a1ceaedc282f0cae31f2723f4d6c00c7b88fe90Chris Lattner    bool AnalyzeUsesOfPointer(Value *V, std::vector<Function*> &Readers,
139043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner                              std::vector<Function*> &Writers,
140043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner                              GlobalValue *OkayStoreDest = 0);
141043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner    bool AnalyzeIndirectGlobalMemory(GlobalValue *GV);
142043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  };
143043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner
144043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  RegisterPass<GlobalsModRef> X("globalsmodref-aa",
145043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner                                "Simple mod/ref analysis for globals");
146043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  RegisterAnalysisGroup<AliasAnalysis> Y(X);
147043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner}
148043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner
149043870dd85ea41e8972c304b122070a417c8a4bcChris LattnerPass *llvm::createGlobalsModRefPass() { return new GlobalsModRef(); }
150043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner
1516ce7dc2a97260eea5fba414332796464912b9359Evan Cheng/// getUnderlyingObject - This traverses the use chain to figure out what object
1526ce7dc2a97260eea5fba414332796464912b9359Evan Cheng/// the specified value points to.  If the value points to, or is derived from,
153e53f4a055f74bded20d6129b4724ddd17fd199f6Chris Lattner/// a global object, return it.
154e53f4a055f74bded20d6129b4724ddd17fd199f6Chris Lattnerstatic Value *getUnderlyingObject(Value *V) {
155f73823000e2d5d6e1cf65bdf5a107297e18d35fbChris Lattner  if (!isa<PointerType>(V->getType())) return V;
156f73823000e2d5d6e1cf65bdf5a107297e18d35fbChris Lattner
157043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  // If we are at some type of object... return it.
158043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV;
1599a1ceaedc282f0cae31f2723f4d6c00c7b88fe90Chris Lattner
1609a1ceaedc282f0cae31f2723f4d6c00c7b88fe90Chris Lattner  // Traverse through different addressing mechanisms.
1619a1ceaedc282f0cae31f2723f4d6c00c7b88fe90Chris Lattner  if (Instruction *I = dyn_cast<Instruction>(V)) {
1629a1ceaedc282f0cae31f2723f4d6c00c7b88fe90Chris Lattner    if (isa<BitCastInst>(I) || isa<GetElementPtrInst>(I))
163043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner      return getUnderlyingObject(I->getOperand(0));
164043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
165bbf1c72d51a77bf54c9c684b90a78e59f0b70b2fChris Lattner    if (CE->getOpcode() == Instruction::BitCast ||
166bbf1c72d51a77bf54c9c684b90a78e59f0b70b2fChris Lattner        CE->getOpcode() == Instruction::GetElementPtr)
167bbf1c72d51a77bf54c9c684b90a78e59f0b70b2fChris Lattner      return getUnderlyingObject(CE->getOperand(0));
168c0f64ffab93d11fb27a3b8a0707b77400918a20eEvan Cheng  }
169bbf1c72d51a77bf54c9c684b90a78e59f0b70b2fChris Lattner
170c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  // Othewise, we don't know what this is, return it as the base pointer.
171c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  return V;
172c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner}
173c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
174c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner/// AnalyzeGlobals - Scan through the users of all of the internal
175c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner/// GlobalValue's in the program.  If none of them have their "Address taken"
176c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner/// (really, their address passed to something nontrivial), record this fact,
177c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner/// and record the functions that they are used directly in.
178bfd2ec4a8ef51ebe982363a7e8d7156fdb3827d8Evan Chengvoid GlobalsModRef::AnalyzeGlobals(Module &M) {
179c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  std::vector<Function*> Readers, Writers;
180c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
181c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    if (I->hasInternalLinkage()) {
182c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner      if (!AnalyzeUsesOfPointer(I, Readers, Writers)) {
183c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner        // Remember that we are tracking this global.
184c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner        NonAddressTakenGlobals.insert(I);
185bfd2ec4a8ef51ebe982363a7e8d7156fdb3827d8Evan Cheng        ++NumNonAddrTakenFunctions;
186c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner      }
1878aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner      Readers.clear(); Writers.clear();
188c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    }
189289c2d5f4566d8d7722e3934f4763d3df92886f3Chris Lattner
190c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  for (Module::global_iterator I = M.global_begin(), E = M.global_end();
1918aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner       I != E; ++I)
192c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    if (I->hasInternalLinkage()) {
193c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner      if (!AnalyzeUsesOfPointer(I, Readers, Writers)) {
1947c4fe259f8bfeae542cfef25c1f1e9b1ff25a39bChris Lattner        // Remember that we are tracking this global, and the mod/ref fns
195c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner        NonAddressTakenGlobals.insert(I);
196c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner        for (unsigned i = 0, e = Readers.size(); i != e; ++i)
197c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner          FunctionInfo[Readers[i]].GlobalInfo[I] |= Ref;
198c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
199c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner        if (!I->isConstant())  // No need to keep track of writers to constants
200c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner          for (unsigned i = 0, e = Writers.size(); i != e; ++i)
201c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner            FunctionInfo[Writers[i]].GlobalInfo[I] |= Mod;
202c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner        ++NumNonAddrTakenGlobalVars;
203c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
204c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner        // If this global holds a pointer type, see if it is an indirect global.
205bfd2ec4a8ef51ebe982363a7e8d7156fdb3827d8Evan Cheng        if (isa<PointerType>(I->getType()->getElementType()) &&
206c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner            AnalyzeIndirectGlobalMemory(I))
207c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner          ++NumIndirectGlobalVars;
208289c2d5f4566d8d7722e3934f4763d3df92886f3Chris Lattner      }
209289c2d5f4566d8d7722e3934f4763d3df92886f3Chris Lattner      Readers.clear(); Writers.clear();
210c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    }
2118aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner}
212c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
213c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner/// AnalyzeUsesOfPointer - Look at all of the users of the specified pointer.
2148aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner/// If this is used by anything complex (i.e., the address escapes), return
215c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner/// true.  Also, while we are at it, keep track of those functions that read and
216c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner/// write to the value.
217c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner///
21813e8b51e3ec014c5d7ae83afdf3b8fd29c3a461dDale Johannesen/// If OkayStoreDest is non-null, stores into this global are allowed.
21913e8b51e3ec014c5d7ae83afdf3b8fd29c3a461dDale Johannesenbool GlobalsModRef::AnalyzeUsesOfPointer(Value *V,
22013e8b51e3ec014c5d7ae83afdf3b8fd29c3a461dDale Johannesen                                         std::vector<Function*> &Readers,
22113e8b51e3ec014c5d7ae83afdf3b8fd29c3a461dDale Johannesen                                         std::vector<Function*> &Writers,
2228aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner                                         GlobalValue *OkayStoreDest) {
22313e8b51e3ec014c5d7ae83afdf3b8fd29c3a461dDale Johannesen  if (!isa<PointerType>(V->getType())) return true;
22413e8b51e3ec014c5d7ae83afdf3b8fd29c3a461dDale Johannesen
22513e8b51e3ec014c5d7ae83afdf3b8fd29c3a461dDale Johannesen  for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI)
22613e8b51e3ec014c5d7ae83afdf3b8fd29c3a461dDale Johannesen    if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
22713e8b51e3ec014c5d7ae83afdf3b8fd29c3a461dDale Johannesen      Readers.push_back(LI->getParent()->getParent());
228c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    } else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
229c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner      if (V == SI->getOperand(1)) {
230c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner        Writers.push_back(SI->getParent()->getParent());
231c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner      } else if (SI->getOperand(1) != OkayStoreDest) {
232b5cdaa257e167a08a8a54ea9249d847ccc415ce0Evan Cheng        return true;  // Storing the pointer
233c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner      }
234b5cdaa257e167a08a8a54ea9249d847ccc415ce0Evan Cheng    } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) {
235c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner      if (AnalyzeUsesOfPointer(GEP, Readers, Writers)) return true;
236289c2d5f4566d8d7722e3934f4763d3df92886f3Chris Lattner    } else if (CallInst *CI = dyn_cast<CallInst>(*UI)) {
237b5cdaa257e167a08a8a54ea9249d847ccc415ce0Evan Cheng      // Make sure that this is just the function being called, not that it is
238c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner      // passing into the function.
239c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner      for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i)
240c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner        if (CI->getOperand(i) == V) return true;
241c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    } else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) {
242c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner      // Make sure that this is just the function being called, not that it is
243c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner      // passing into the function.
244b5cdaa257e167a08a8a54ea9249d847ccc415ce0Evan Cheng      for (unsigned i = 3, e = II->getNumOperands(); i != e; ++i)
245c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner        if (II->getOperand(i) == V) return true;
246289c2d5f4566d8d7722e3934f4763d3df92886f3Chris Lattner    } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(*UI)) {
247b5cdaa257e167a08a8a54ea9249d847ccc415ce0Evan Cheng      if (CE->getOpcode() == Instruction::GetElementPtr ||
248c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner          CE->getOpcode() == Instruction::BitCast) {
249c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner        if (AnalyzeUsesOfPointer(CE, Readers, Writers))
250c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner          return true;
251b5cdaa257e167a08a8a54ea9249d847ccc415ce0Evan Cheng      } else {
252c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner        return true;
253c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner      }
254b5cdaa257e167a08a8a54ea9249d847ccc415ce0Evan Cheng    } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(*UI)) {
255b5cdaa257e167a08a8a54ea9249d847ccc415ce0Evan Cheng      if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
256b5cdaa257e167a08a8a54ea9249d847ccc415ce0Evan Cheng        return true;  // Allow comparison against null.
257b5cdaa257e167a08a8a54ea9249d847ccc415ce0Evan Cheng    } else if (FreeInst *F = dyn_cast<FreeInst>(*UI)) {
2582dc7723474c54efcbcac6265dad0a7271902f1a5Chris Lattner      Writers.push_back(F->getParent()->getParent());
2592dc7723474c54efcbcac6265dad0a7271902f1a5Chris Lattner    } else {
26054108068b71a7dbc48f4ebf1b2d7d87ca541070aChris Lattner      return true;
26154108068b71a7dbc48f4ebf1b2d7d87ca541070aChris Lattner    }
2622dc7723474c54efcbcac6265dad0a7271902f1a5Chris Lattner  return false;
26354108068b71a7dbc48f4ebf1b2d7d87ca541070aChris Lattner}
2642dc7723474c54efcbcac6265dad0a7271902f1a5Chris Lattner
26554108068b71a7dbc48f4ebf1b2d7d87ca541070aChris Lattner/// AnalyzeIndirectGlobalMemory - We found an non-address-taken global variable
266c0f64ffab93d11fb27a3b8a0707b77400918a20eEvan Cheng/// which holds a pointer type.  See if the global always points to non-aliased
26754108068b71a7dbc48f4ebf1b2d7d87ca541070aChris Lattner/// heap memory: that is, all initializers of the globals are allocations, and
268c0f64ffab93d11fb27a3b8a0707b77400918a20eEvan Cheng/// those allocations have no use other than initialization of the global.
26918258c640466274c26e89016e361ec411ff78520Chris Lattner/// Further, all loads out of GV must directly use the memory, not store the
270b5cdaa257e167a08a8a54ea9249d847ccc415ce0Evan Cheng/// pointer somewhere.  If this is true, we consider the memory pointed to by
2712dc7723474c54efcbcac6265dad0a7271902f1a5Chris Lattner/// GV to be owned by GV and can disambiguate other pointers from it.
272c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattnerbool GlobalsModRef::AnalyzeIndirectGlobalMemory(GlobalValue *GV) {
273879d09cf130f3760a08865913c04d9ff328fad5fChris Lattner  // Keep track of values related to the allocation of the memory, f.e. the
274c0f64ffab93d11fb27a3b8a0707b77400918a20eEvan Cheng  // value produced by the malloc call and any casts.
27518258c640466274c26e89016e361ec411ff78520Chris Lattner  std::vector<Value*> AllocRelatedValues;
276c0f64ffab93d11fb27a3b8a0707b77400918a20eEvan Cheng
277b5cdaa257e167a08a8a54ea9249d847ccc415ce0Evan Cheng  // Walk the user list of the global.  If we find anything other than a direct
278c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  // load or store, bail out.
279c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  for (Value::use_iterator I = GV->use_begin(), E = GV->use_end(); I != E; ++I){
280d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson    if (LoadInst *LI = dyn_cast<LoadInst>(*I)) {
281d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson      // The pointer loaded from the global can only be used in simple ways:
282d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson      // we allow addressing of it and loading storing to it.  We do *not* allow
283d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson      // storing the loaded pointer somewhere else or passing to a function.
284d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson      std::vector<Function*> ReadersWriters;
285d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson      if (AnalyzeUsesOfPointer(LI, ReadersWriters, ReadersWriters))
286d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson        return false;  // Loaded pointer escapes.
287d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson      // TODO: Could try some IP mod/ref of the loaded pointer.
288d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson    } else if (StoreInst *SI = dyn_cast<StoreInst>(*I)) {
289d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson      // Storing the global itself.
290d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson      if (SI->getOperand(0) == GV) return false;
291d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson
292d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson      // If storing the null pointer, ignore it.
293d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson      if (isa<ConstantPointerNull>(SI->getOperand(0)))
294d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson        continue;
295d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson
296d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson      // Check the value being stored.
297d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson      Value *Ptr = getUnderlyingObject(SI->getOperand(0));
298d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson
299d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson      if (isa<MallocInst>(Ptr)) {
300d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson        // Okay, easy case.
301d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson      } else if (CallInst *CI = dyn_cast<CallInst>(Ptr)) {
302d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson        Function *F = CI->getCalledFunction();
303d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson        if (!F || !F->isDeclaration()) return false;     // Too hard to analyze.
304d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson        if (F->getName() != "calloc") return false;   // Not calloc.
305d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson      } else {
306d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson        return false;  // Too hard to analyze.
307d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson      }
308ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner
309ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner      // Analyze all uses of the allocation.  If any of them are used in a
310ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner      // non-simple way (e.g. stored to another global) bail out.
311ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner      std::vector<Function*> ReadersWriters;
312126f17a17625876adb63f06d043fc1b1e4f0361cEvan Cheng      if (AnalyzeUsesOfPointer(Ptr, ReadersWriters, ReadersWriters, GV))
313ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner        return false;  // Loaded pointer escapes.
314ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner
315ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner      // Remember that this allocation is related to the indirect global.
316ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner      AllocRelatedValues.push_back(Ptr);
317ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner    } else {
318ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner      // Something complex, bail out.
319ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner      return false;
320c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    }
321c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  }
3227c4fe259f8bfeae542cfef25c1f1e9b1ff25a39bChris Lattner
3237c4fe259f8bfeae542cfef25c1f1e9b1ff25a39bChris Lattner  // Okay, this is an indirect global.  Remember all of the allocations for
32418258c640466274c26e89016e361ec411ff78520Chris Lattner  // this global in AllocsForIndirectGlobals.
3257c4fe259f8bfeae542cfef25c1f1e9b1ff25a39bChris Lattner  while (!AllocRelatedValues.empty()) {
326c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV;
327    AllocRelatedValues.pop_back();
328  }
329  IndirectGlobals.insert(GV);
330  return true;
331}
332
333/// AnalyzeCallGraph - At this point, we know the functions where globals are
334/// immediately stored to and read from.  Propagate this information up the call
335/// graph to all callers and compute the mod/ref info for all memory for each
336/// function.
337void GlobalsModRef::AnalyzeCallGraph(CallGraph &CG, Module &M) {
338  // We do a bottom-up SCC traversal of the call graph.  In other words, we
339  // visit all callees before callers (leaf-first).
340  for (scc_iterator<CallGraph*> I = scc_begin(&CG), E = scc_end(&CG); I!=E; ++I)
341    if ((*I).size() != 1) {
342      AnalyzeSCC(*I);
343    } else if (Function *F = (*I)[0]->getFunction()) {
344      if (!F->isDeclaration()) {
345        // Nonexternal function.
346        AnalyzeSCC(*I);
347      } else {
348        // Otherwise external function.  Handle intrinsics and other special
349        // cases here.
350        if (getAnalysis<AliasAnalysis>().doesNotAccessMemory(F))
351          // If it does not access memory, process the function, causing us to
352          // realize it doesn't do anything (the body is empty).
353          AnalyzeSCC(*I);
354        else {
355          // Otherwise, don't process it.  This will cause us to conservatively
356          // assume the worst.
357        }
358      }
359    } else {
360      // Do not process the external node, assume the worst.
361    }
362}
363
364void GlobalsModRef::AnalyzeSCC(std::vector<CallGraphNode *> &SCC) {
365  assert(!SCC.empty() && "SCC with no functions?");
366  FunctionRecord &FR = FunctionInfo[SCC[0]->getFunction()];
367
368  bool CallsExternal = false;
369  unsigned FunctionEffect = 0;
370
371  // Collect the mod/ref properties due to called functions.  We only compute
372  // one mod-ref set
373  for (unsigned i = 0, e = SCC.size(); i != e && !CallsExternal; ++i)
374    for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end();
375         CI != E; ++CI)
376      if (Function *Callee = CI->second->getFunction()) {
377        if (FunctionRecord *CalleeFR = getFunctionInfo(Callee)) {
378          // Propagate function effect up.
379          FunctionEffect |= CalleeFR->FunctionEffect;
380
381          // Incorporate callee's effects on globals into our info.
382          for (std::map<GlobalValue*, unsigned>::iterator GI =
383                 CalleeFR->GlobalInfo.begin(), E = CalleeFR->GlobalInfo.end();
384               GI != E; ++GI)
385            FR.GlobalInfo[GI->first] |= GI->second;
386
387        } else {
388          // Okay, if we can't say anything about it, maybe some other alias
389          // analysis can.
390          ModRefBehavior MRB =
391            AliasAnalysis::getModRefBehavior(Callee, CallSite());
392          if (MRB != DoesNotAccessMemory) {
393            // FIXME: could make this more aggressive for functions that just
394            // read memory.  We should just say they read all globals.
395            CallsExternal = true;
396            break;
397          }
398        }
399      } else {
400        CallsExternal = true;
401        break;
402      }
403
404  // If this SCC calls an external function, we can't say anything about it, so
405  // remove all SCC functions from the FunctionInfo map.
406  if (CallsExternal) {
407    for (unsigned i = 0, e = SCC.size(); i != e; ++i)
408      FunctionInfo.erase(SCC[i]->getFunction());
409    return;
410  }
411
412  // Otherwise, unless we already know that this function mod/refs memory, scan
413  // the function bodies to see if there are any explicit loads or stores.
414  if (FunctionEffect != ModRef) {
415    for (unsigned i = 0, e = SCC.size(); i != e && FunctionEffect != ModRef;++i)
416      for (inst_iterator II = inst_begin(SCC[i]->getFunction()),
417             E = inst_end(SCC[i]->getFunction());
418           II != E && FunctionEffect != ModRef; ++II)
419        if (isa<LoadInst>(*II))
420          FunctionEffect |= Ref;
421        else if (isa<StoreInst>(*II))
422          FunctionEffect |= Mod;
423        else if (isa<MallocInst>(*II) || isa<FreeInst>(*II))
424          FunctionEffect |= ModRef;
425  }
426
427  if ((FunctionEffect & Mod) == 0)
428    ++NumReadMemFunctions;
429  if (FunctionEffect == 0)
430    ++NumNoMemFunctions;
431  FR.FunctionEffect = FunctionEffect;
432
433  // Finally, now that we know the full effect on this SCC, clone the
434  // information to each function in the SCC.
435  for (unsigned i = 1, e = SCC.size(); i != e; ++i)
436    FunctionInfo[SCC[i]->getFunction()] = FR;
437}
438
439
440
441/// alias - If one of the pointers is to a global that we are tracking, and the
442/// other is some random pointer, we know there cannot be an alias, because the
443/// address of the global isn't taken.
444AliasAnalysis::AliasResult
445GlobalsModRef::alias(const Value *V1, unsigned V1Size,
446                     const Value *V2, unsigned V2Size) {
447  // Get the base object these pointers point to.
448  Value *UV1 = getUnderlyingObject(const_cast<Value*>(V1));
449  Value *UV2 = getUnderlyingObject(const_cast<Value*>(V2));
450
451  // If either of the underlying values is a global, they may be non-addr-taken
452  // globals, which we can answer queries about.
453  GlobalValue *GV1 = dyn_cast<GlobalValue>(UV1);
454  GlobalValue *GV2 = dyn_cast<GlobalValue>(UV2);
455  if (GV1 || GV2) {
456    // If the global's address is taken, pretend we don't know it's a pointer to
457    // the global.
458    if (GV1 && !NonAddressTakenGlobals.count(GV1)) GV1 = 0;
459    if (GV2 && !NonAddressTakenGlobals.count(GV2)) GV2 = 0;
460
461    // If the the two pointers are derived from two different non-addr-taken
462    // globals, or if one is and the other isn't, we know these can't alias.
463    if ((GV1 || GV2) && GV1 != GV2)
464      return NoAlias;
465
466    // Otherwise if they are both derived from the same addr-taken global, we
467    // can't know the two accesses don't overlap.
468  }
469
470  // These pointers may be based on the memory owned by an indirect global.  If
471  // so, we may be able to handle this.  First check to see if the base pointer
472  // is a direct load from an indirect global.
473  GV1 = GV2 = 0;
474  if (LoadInst *LI = dyn_cast<LoadInst>(UV1))
475    if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
476      if (IndirectGlobals.count(GV))
477        GV1 = GV;
478  if (LoadInst *LI = dyn_cast<LoadInst>(UV2))
479    if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
480      if (IndirectGlobals.count(GV))
481        GV2 = GV;
482
483  // These pointers may also be from an allocation for the indirect global.  If
484  // so, also handle them.
485  if (AllocsForIndirectGlobals.count(UV1))
486    GV1 = AllocsForIndirectGlobals[UV1];
487  if (AllocsForIndirectGlobals.count(UV2))
488    GV2 = AllocsForIndirectGlobals[UV2];
489
490  // Now that we know whether the two pointers are related to indirect globals,
491  // use this to disambiguate the pointers.  If either pointer is based on an
492  // indirect global and if they are not both based on the same indirect global,
493  // they cannot alias.
494  if ((GV1 || GV2) && GV1 != GV2)
495    return NoAlias;
496
497  return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
498}
499
500AliasAnalysis::ModRefResult
501GlobalsModRef::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
502  unsigned Known = ModRef;
503
504  // If we are asking for mod/ref info of a direct call with a pointer to a
505  // global we are tracking, return information if we have it.
506  if (GlobalValue *GV = dyn_cast<GlobalValue>(getUnderlyingObject(P)))
507    if (GV->hasInternalLinkage())
508      if (Function *F = CS.getCalledFunction())
509        if (NonAddressTakenGlobals.count(GV))
510          if (FunctionRecord *FR = getFunctionInfo(F))
511            Known = FR->getInfoForGlobal(GV);
512
513  if (Known == NoModRef)
514    return NoModRef; // No need to query other mod/ref analyses
515  return ModRefResult(Known & AliasAnalysis::getModRefInfo(CS, P, Size));
516}
517
518
519//===----------------------------------------------------------------------===//
520// Methods to update the analysis as a result of the client transformation.
521//
522void GlobalsModRef::deleteValue(Value *V) {
523  if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
524    if (NonAddressTakenGlobals.erase(GV)) {
525      // This global might be an indirect global.  If so, remove it and remove
526      // any AllocRelatedValues for it.
527      if (IndirectGlobals.erase(GV)) {
528        // Remove any entries in AllocsForIndirectGlobals for this global.
529        for (std::map<Value*, GlobalValue*>::iterator
530             I = AllocsForIndirectGlobals.begin(),
531             E = AllocsForIndirectGlobals.end(); I != E; ) {
532          if (I->second == GV) {
533            AllocsForIndirectGlobals.erase(I++);
534          } else {
535            ++I;
536          }
537        }
538      }
539    }
540  }
541
542  // Otherwise, if this is an allocation related to an indirect global, remove
543  // it.
544  AllocsForIndirectGlobals.erase(V);
545}
546
547void GlobalsModRef::copyValue(Value *From, Value *To) {
548}
549