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 /// An alias analysis result set for globals.
30 /// This focuses on handling aliasing properties of globals and interprocedural
31 /// function call mod/ref information.
32 class GlobalsAAResult : public AAResultBase<GlobalsAAResult> {
33 friend AAResultBase<GlobalsAAResult>;
39 /// The globals that do not have their addresses taken.
40 SmallPtrSet<const GlobalValue *, 8> NonAddressTakenGlobals;
42 /// IndirectGlobals - The memory pointed to by this global is known to be
43 /// 'owned' by the global.
44 SmallPtrSet<const GlobalValue *, 8> IndirectGlobals;
46 /// AllocsForIndirectGlobals - If an instruction allocates memory for an
47 /// indirect global, this map indicates which one.
48 DenseMap<const Value *, const GlobalValue *> AllocsForIndirectGlobals;
50 /// For each function, keep track of what globals are modified or read.
51 DenseMap<const Function *, FunctionInfo> FunctionInfos;
53 /// Handle to clear this analysis on deletion of values.
54 struct DeletionCallbackHandle final : CallbackVH {
56 std::list<DeletionCallbackHandle>::iterator I;
58 DeletionCallbackHandle(GlobalsAAResult &GAR, Value *V)
59 : CallbackVH(V), GAR(&GAR) {}
61 void deleted() override;
64 /// List of callbacks for globals being tracked by this analysis. Note that
65 /// these objects are quite large, but we only anticipate having one per
66 /// global tracked by this analysis. There are numerous optimizations we
67 /// could perform to the memory utilization here if this becomes a problem.
68 std::list<DeletionCallbackHandle> Handles;
70 explicit GlobalsAAResult(const DataLayout &DL, const TargetLibraryInfo &TLI);
73 GlobalsAAResult(GlobalsAAResult &&Arg);
75 static GlobalsAAResult analyzeModule(Module &M, const TargetLibraryInfo &TLI,
78 //------------------------------------------------
79 // Implement the AliasAnalysis API
81 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB);
83 using AAResultBase::getModRefInfo;
84 ModRefInfo getModRefInfo(ImmutableCallSite CS, const MemoryLocation &Loc);
86 /// getModRefBehavior - Return the behavior of the specified function if
87 /// called from the specified call site. The call site may be null in which
88 /// case the most generic behavior of this function should be returned.
89 FunctionModRefBehavior getModRefBehavior(const Function *F);
91 /// getModRefBehavior - Return the behavior of the specified function if
92 /// called from the specified call site. The call site may be null in which
93 /// case the most generic behavior of this function should be returned.
94 FunctionModRefBehavior getModRefBehavior(ImmutableCallSite CS);
97 FunctionInfo *getFunctionInfo(const Function *F);
99 void AnalyzeGlobals(Module &M);
100 void AnalyzeCallGraph(CallGraph &CG, Module &M);
101 bool AnalyzeUsesOfPointer(Value *V,
102 SmallPtrSetImpl<Function *> *Readers = nullptr,
103 SmallPtrSetImpl<Function *> *Writers = nullptr,
104 GlobalValue *OkayStoreDest = nullptr);
105 bool AnalyzeIndirectGlobalMemory(GlobalValue *GV);
107 bool isNonEscapingGlobalNoAlias(const GlobalValue *GV, const Value *V);
110 /// Analysis pass providing a never-invalidated alias analysis result.
113 typedef GlobalsAAResult Result;
115 /// \brief Opaque, unique identifier for this analysis pass.
116 static void *ID() { return (void *)&PassID; }
118 GlobalsAAResult run(Module &M, AnalysisManager<Module> *AM);
120 /// \brief Provide access to a name for this pass for debugging purposes.
121 static StringRef name() { return "GlobalsAA"; }
127 /// Legacy wrapper pass to provide the GlobalsAAResult object.
128 class GlobalsAAWrapperPass : public ModulePass {
129 std::unique_ptr<GlobalsAAResult> Result;
134 GlobalsAAWrapperPass();
136 GlobalsAAResult &getResult() { return *Result; }
137 const GlobalsAAResult &getResult() const { return *Result; }
139 bool runOnModule(Module &M) override;
140 bool doFinalization(Module &M) override;
141 void getAnalysisUsage(AnalysisUsage &AU) const override;
144 //===--------------------------------------------------------------------===//
146 // createGlobalsAAWrapperPass - This pass provides alias and mod/ref info for
147 // global values that do not have their addresses taken.
149 ModulePass *createGlobalsAAWrapperPass();