LLVM  15.0.0git
GlobalsModRef.h
Go to the documentation of this file.
1 //===- GlobalsModRef.h - Simple Mod/Ref AA for Globals ----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 /// This is the interface for a simple mod/ref and alias analysis over globals.
10 ///
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_ANALYSIS_GLOBALSMODREF_H
14 #define LLVM_ANALYSIS_GLOBALSMODREF_H
15 
17 #include "llvm/IR/PassManager.h"
18 #include "llvm/IR/ValueHandle.h"
19 #include "llvm/Pass.h"
20 #include <list>
21 
22 namespace llvm {
23 class CallGraph;
24 class Function;
25 
26 /// An alias analysis result set for globals.
27 ///
28 /// This focuses on handling aliasing properties of globals and interprocedural
29 /// function call mod/ref information.
30 class GlobalsAAResult : public AAResultBase<GlobalsAAResult> {
32 
33  class FunctionInfo;
34 
35  const DataLayout &DL;
36  std::function<const TargetLibraryInfo &(Function &F)> GetTLI;
37 
38  /// The globals that do not have their addresses taken.
39  SmallPtrSet<const GlobalValue *, 8> NonAddressTakenGlobals;
40 
41  /// Are there functions with local linkage that may modify globals.
42  bool UnknownFunctionsWithLocalLinkage = false;
43 
44  /// IndirectGlobals - The memory pointed to by this global is known to be
45  /// 'owned' by the global.
47 
48  /// AllocsForIndirectGlobals - If an instruction allocates memory for an
49  /// indirect global, this map indicates which one.
50  DenseMap<const Value *, const GlobalValue *> AllocsForIndirectGlobals;
51 
52  /// For each function, keep track of what globals are modified or read.
54 
55  /// A map of functions to SCC. The SCCs are described by a simple integer
56  /// ID that is only useful for comparing for equality (are two functions
57  /// in the same SCC or not?)
58  DenseMap<const Function *, unsigned> FunctionToSCCMap;
59 
60  /// Handle to clear this analysis on deletion of values.
61  struct DeletionCallbackHandle final : CallbackVH {
62  GlobalsAAResult *GAR;
63  std::list<DeletionCallbackHandle>::iterator I;
64 
65  DeletionCallbackHandle(GlobalsAAResult &GAR, Value *V)
66  : CallbackVH(V), GAR(&GAR) {}
67 
68  void deleted() override;
69  };
70 
71  /// List of callbacks for globals being tracked by this analysis. Note that
72  /// these objects are quite large, but we only anticipate having one per
73  /// global tracked by this analysis. There are numerous optimizations we
74  /// could perform to the memory utilization here if this becomes a problem.
75  std::list<DeletionCallbackHandle> Handles;
76 
77  explicit GlobalsAAResult(
78  const DataLayout &DL,
79  std::function<const TargetLibraryInfo &(Function &F)> GetTLI);
80 
81  friend struct RecomputeGlobalsAAPass;
82 
83 public:
86 
87  bool invalidate(Module &M, const PreservedAnalyses &PA,
89 
90  static GlobalsAAResult
92  std::function<const TargetLibraryInfo &(Function &F)> GetTLI,
93  CallGraph &CG);
94 
95  //------------------------------------------------
96  // Implement the AliasAnalysis API
97  //
98  AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB,
99  AAQueryInfo &AAQI);
100 
103  AAQueryInfo &AAQI);
104 
105  /// getModRefBehavior - Return the behavior of the specified function if
106  /// called from the specified call site. The call site may be null in which
107  /// case the most generic behavior of this function should be returned.
109 
110  /// getModRefBehavior - Return the behavior of the specified function if
111  /// called from the specified call site. The call site may be null in which
112  /// case the most generic behavior of this function should be returned.
114 
115 private:
116  FunctionInfo *getFunctionInfo(const Function *F);
117 
118  void AnalyzeGlobals(Module &M);
119  void AnalyzeCallGraph(CallGraph &CG, Module &M);
120  bool AnalyzeUsesOfPointer(Value *V,
121  SmallPtrSetImpl<Function *> *Readers = nullptr,
122  SmallPtrSetImpl<Function *> *Writers = nullptr,
123  GlobalValue *OkayStoreDest = nullptr);
124  bool AnalyzeIndirectGlobalMemory(GlobalVariable *GV);
125  void CollectSCCMembership(CallGraph &CG);
126 
127  bool isNonEscapingGlobalNoAlias(const GlobalValue *GV, const Value *V);
128  ModRefInfo getModRefInfoForArgument(const CallBase *Call,
129  const GlobalValue *GV, AAQueryInfo &AAQI);
130 };
131 
132 /// Analysis pass providing a never-invalidated alias analysis result.
133 class GlobalsAA : public AnalysisInfoMixin<GlobalsAA> {
135  static AnalysisKey Key;
136 
137 public:
139 
141 };
142 
143 struct RecomputeGlobalsAAPass : PassInfoMixin<RecomputeGlobalsAAPass> {
145 };
146 
147 /// Legacy wrapper pass to provide the GlobalsAAResult object.
149  std::unique_ptr<GlobalsAAResult> Result;
150 
151 public:
152  static char ID;
153 
155 
156  GlobalsAAResult &getResult() { return *Result; }
157  const GlobalsAAResult &getResult() const { return *Result; }
158 
159  bool runOnModule(Module &M) override;
160  bool doFinalization(Module &M) override;
161  void getAnalysisUsage(AnalysisUsage &AU) const override;
162 };
163 
164 //===--------------------------------------------------------------------===//
165 //
166 // createGlobalsAAWrapperPass - This pass provides alias and mod/ref info for
167 // global values that do not have their addresses taken.
168 //
169 ModulePass *createGlobalsAAWrapperPass();
170 }
171 
172 #endif
llvm::GlobalsAA
Analysis pass providing a never-invalidated alias analysis result.
Definition: GlobalsModRef.h:133
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
llvm::RecomputeGlobalsAAPass
Definition: GlobalsModRef.h:143
llvm::GlobalsAAResult
An alias analysis result set for globals.
Definition: GlobalsModRef.h:30
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:113
llvm::GlobalsAAResult::analyzeModule
static GlobalsAAResult analyzeModule(Module &M, std::function< const TargetLibraryInfo &(Function &F)> GetTLI, CallGraph &CG)
Definition: GlobalsModRef.cpp:996
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:248
llvm::PassInfoMixin
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:371
llvm::Function
Definition: Function.h:60
Pass.h
llvm::GlobalVariable
Definition: GlobalVariable.h:39
llvm::CallGraph
The basic data container for the call graph of a Module of IR.
Definition: CallGraph.h:72
llvm::AliasResult
The possible results of an alias query.
Definition: AliasAnalysis.h:81
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:450
F
#define F(x, y, z)
Definition: MD5.cpp:55
AliasAnalysis.h
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:186
llvm::AAQueryInfo
This class stores info we want to provide to or retain within an alias query.
Definition: AliasAnalysis.h:462
llvm::GlobalsAAResult::getModRefBehavior
FunctionModRefBehavior getModRefBehavior(const Function *F)
getModRefBehavior - Return the behavior of the specified function if called from the specified call s...
Definition: GlobalsModRef.cpp:246
llvm::GlobalsAAResult::invalidate
bool invalidate(Module &M, const PreservedAnalyses &PA, ModuleAnalysisManager::Invalidator &)
Definition: GlobalsModRef.cpp:827
llvm::GlobalsAAWrapperPass::ID
static char ID
Definition: GlobalsModRef.h:152
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::RecomputeGlobalsAAPass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: GlobalsModRef.cpp:1025
llvm::AnalysisManager::Invalidator
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:667
llvm::MCID::Call
@ Call
Definition: MCInstrDesc.h:155
llvm::createGlobalsAAWrapperPass
ModulePass * createGlobalsAAWrapperPass()
Definition: GlobalsModRef.cpp:1051
llvm::GlobalValue
Definition: GlobalValue.h:44
llvm::GlobalsAAResult::FunctionInfo
The mod/ref information collected for a particular function.
Definition: GlobalsModRef.cpp:64
llvm::DenseMap
Definition: DenseMap.h:716
llvm::AnalysisKey
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:69
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::GlobalsAAWrapperPass::getResult
const GlobalsAAResult & getResult() const
Definition: GlobalsModRef.h:157
llvm::ModRefInfo
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
Definition: AliasAnalysis.h:148
function
print Print MemDeps of function
Definition: MemDepPrinter.cpp:82
llvm::GlobalsAAWrapperPass::doFinalization
bool doFinalization(Module &M) override
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
Definition: GlobalsModRef.cpp:1068
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::AnalysisInfoMixin
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:394
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::GlobalsAAWrapperPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: GlobalsModRef.cpp:1073
llvm::FunctionModRefBehavior
FunctionModRefBehavior
Summary of how a function affects memory in the program.
Definition: AliasAnalysis.h:262
ValueHandle.h
llvm::GlobalsAAWrapperPass::runOnModule
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
Definition: GlobalsModRef.cpp:1059
llvm::AAResultBase::getModRefInfo
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, AAQueryInfo &AAQI)
Definition: AliasAnalysis.h:1244
llvm::GlobalsAAResult::alias
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI)
alias - If one of the pointers is to a global that we are tracking, and the other is some random poin...
Definition: GlobalsModRef.cpp:838
llvm::GlobalsAA::run
GlobalsAAResult run(Module &M, ModuleAnalysisManager &AM)
Definition: GlobalsModRef.cpp:1015
PassManager.h
llvm::TargetLibraryInfo
Provides information about what library functions are available for the current target.
Definition: TargetLibraryInfo.h:222
llvm::GlobalsAAWrapperPass::GlobalsAAWrapperPass
GlobalsAAWrapperPass()
Definition: GlobalsModRef.cpp:1055
llvm::CallbackVH
Value handle with callbacks on RAUW and destruction.
Definition: ValueHandle.h:383
llvm::GlobalsAAResult::~GlobalsAAResult
~GlobalsAAResult()
llvm::GlobalsAA::Result
GlobalsAAResult Result
Definition: GlobalsModRef.h:138
llvm::GlobalsAAWrapperPass
Legacy wrapper pass to provide the GlobalsAAResult object.
Definition: GlobalsModRef.h:148
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1174
llvm::SmallPtrSetImpl
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:344
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
llvm::GlobalsAAResult::getModRefInfo
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, AAQueryInfo &AAQI)
Definition: GlobalsModRef.cpp:952
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::AAResultBase
A CRTP-driven "mixin" base class to help implement the function alias analysis results concept.
Definition: AliasAnalysis.h:1134
llvm::MemoryLocation
Representation for a specific memory location.
Definition: MemoryLocation.h:210
llvm::codeview::PublicSymFlags::Function
@ Function
llvm::GlobalsAAWrapperPass::getResult
GlobalsAAResult & getResult()
Definition: GlobalsModRef.h:156