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;
100 using AliasAnalysis::getModRefInfo;
101 ModRefInfo getModRefInfo(ImmutableCallSite CS,
102 const MemoryLocation &Loc) override;
104 /// getModRefBehavior - Return the behavior of the specified function if
105 /// called from the specified call site. The call site may be null in which
106 /// case the most generic behavior of this function should be returned.
107 FunctionModRefBehavior getModRefBehavior(const Function *F) override;
109 /// getModRefBehavior - Return the behavior of the specified function if
110 /// called from the specified call site. The call site may be null in which
111 /// case the most generic behavior of this function should be returned.
112 FunctionModRefBehavior getModRefBehavior(ImmutableCallSite CS) override;
115 FunctionInfo *getFunctionInfo(const Function *F);
117 void AnalyzeGlobals(Module &M);
118 void AnalyzeCallGraph(CallGraph &CG, Module &M);
119 bool AnalyzeUsesOfPointer(Value *V,
120 SmallPtrSetImpl<Function *> *Readers = nullptr,
121 SmallPtrSetImpl<Function *> *Writers = nullptr,
122 GlobalValue *OkayStoreDest = nullptr);
123 bool AnalyzeIndirectGlobalMemory(GlobalValue *GV);
125 bool isNonEscapingGlobalNoAlias(const GlobalValue *GV, const Value *V);
128 //===--------------------------------------------------------------------===//
130 // createGlobalsModRefPass - This pass provides alias and mod/ref info for
131 // global values that do not have their addresses taken.
133 Pass *createGlobalsModRefPass();