1 //===- GlobalsModRef.h - Simple Mod/Ref AA for Globals ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This is the interface for a simple mod/ref and alias analysis over globlas.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_GLOBALSMODREF_H
15 #define LLVM_ANALYSIS_GLOBALSMODREF_H
17 #include "llvm/Analysis/AliasAnalysis.h"
18 #include "llvm/Analysis/CallGraph.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/IR/ValueHandle.h"
23 #include "llvm/Pass.h"
28 /// GlobalsModRef - The actual analysis pass.
29 class GlobalsModRef : public ModulePass, public AliasAnalysis {
32 /// The globals that do not have their addresses taken.
33 SmallPtrSet<const GlobalValue *, 8> NonAddressTakenGlobals;
35 /// IndirectGlobals - The memory pointed to by this global is known to be
36 /// 'owned' by the global.
37 SmallPtrSet<const GlobalValue *, 8> IndirectGlobals;
39 /// AllocsForIndirectGlobals - If an instruction allocates memory for an
40 /// indirect global, this map indicates which one.
41 DenseMap<const Value *, const GlobalValue *> AllocsForIndirectGlobals;
43 /// For each function, keep track of what globals are modified or read.
44 DenseMap<const Function *, FunctionInfo> FunctionInfos;
46 /// Handle to clear this analysis on deletion of values.
47 struct DeletionCallbackHandle final : CallbackVH {
49 std::list<DeletionCallbackHandle>::iterator I;
51 DeletionCallbackHandle(GlobalsModRef &GMR, Value *V)
52 : CallbackVH(V), GMR(GMR) {}
54 void deleted() override;
57 /// List of callbacks for globals being tracked by this analysis. Note that
58 /// these objects are quite large, but we only anticipate having one per
59 /// global tracked by this analysis. There are numerous optimizations we
60 /// could perform to the memory utilization here if this becomes a problem.
61 std::list<DeletionCallbackHandle> Handles;
67 bool runOnModule(Module &M) override {
68 InitializeAliasAnalysis(this, &M.getDataLayout());
70 // Find non-addr taken globals.
74 AnalyzeCallGraph(getAnalysis<CallGraphWrapperPass>().getCallGraph(), M);
78 void getAnalysisUsage(AnalysisUsage &AU) const override {
79 AliasAnalysis::getAnalysisUsage(AU);
80 AU.addRequired<CallGraphWrapperPass>();
81 AU.setPreservesAll(); // Does not transform code
84 /// getAdjustedAnalysisPointer - This method is used when a pass implements
85 /// an analysis interface through multiple inheritance. If needed, it
86 /// should override this to adjust the this pointer as needed for the
87 /// specified pass info.
88 void *getAdjustedAnalysisPointer(AnalysisID PI) override {
89 if (PI == &AliasAnalysis::ID)
90 return (AliasAnalysis *)this;
94 //------------------------------------------------
95 // Implement the AliasAnalysis API
97 AliasResult alias(const MemoryLocation &LocA,
98 const MemoryLocation &LocB) override;
99 ModRefInfo getModRefInfo(ImmutableCallSite CS,
100 const MemoryLocation &Loc) override;
101 ModRefInfo getModRefInfo(ImmutableCallSite CS1,
102 ImmutableCallSite CS2) override {
103 return AliasAnalysis::getModRefInfo(CS1, CS2);
106 /// getModRefBehavior - Return the behavior of the specified function if
107 /// called from the specified call site. The call site may be null in which
108 /// case the most generic behavior of this function should be returned.
109 FunctionModRefBehavior getModRefBehavior(const Function *F) override;
111 /// getModRefBehavior - Return the behavior of the specified function if
112 /// called from the specified call site. The call site may be null in which
113 /// case the most generic behavior of this function should be returned.
114 FunctionModRefBehavior getModRefBehavior(ImmutableCallSite CS) override;
117 FunctionInfo *getFunctionInfo(const Function *F);
119 void AnalyzeGlobals(Module &M);
120 void AnalyzeCallGraph(CallGraph &CG, Module &M);
121 bool AnalyzeUsesOfPointer(Value *V,
122 SmallPtrSetImpl<Function *> *Readers = nullptr,
123 SmallPtrSetImpl<Function *> *Writers = nullptr,
124 GlobalValue *OkayStoreDest = nullptr);
125 bool AnalyzeIndirectGlobalMemory(GlobalValue *GV);
127 bool isNonEscapingGlobalNoAlias(const GlobalValue *GV, const Value *V);
130 //===--------------------------------------------------------------------===//
132 // createGlobalsModRefPass - This pass provides alias and mod/ref info for
133 // global values that do not have their addresses taken.
135 Pass *createGlobalsModRefPass();