LLVM 24.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"
21#include <list>
22
23namespace llvm {
24struct AnalysisOptions;
25class CallGraph;
26class Function;
27
28/// An alias analysis result set for globals.
29///
30/// This focuses on handling aliasing properties of globals and interprocedural
31/// function call mod/ref information.
32class GlobalsAAResult : public AAResultBase {
33 class FunctionInfo;
34
35 const AnalysisOptions &Opts;
36 const DataLayout &DL;
37 std::function<const TargetLibraryInfo &(Function &F)> GetTLI;
38
39 /// The globals that do not have their addresses taken.
40 SmallPtrSet<const GlobalValue *, 8> NonAddressTakenGlobals;
41
42 /// Are there functions with local linkage that may modify globals.
43 bool UnknownFunctionsWithLocalLinkage = false;
44
45 /// IndirectGlobals - The memory pointed to by this global is known to be
46 /// 'owned' by the global.
48
49 /// AllocsForIndirectGlobals - If an instruction allocates memory for an
50 /// indirect global, this map indicates which one.
51 DenseMap<const Value *, const GlobalValue *> AllocsForIndirectGlobals;
52
53 /// For each function, keep track of what globals are modified or read.
55
56 /// A map of functions to SCC. The SCCs are described by a simple integer
57 /// ID that is only useful for comparing for equality (are two functions
58 /// in the same SCC or not?)
60
61 /// Handle to clear this analysis on deletion of values.
62 struct LLVM_ABI DeletionCallbackHandle final : CallbackVH {
63 GlobalsAAResult *GAR;
64 std::list<DeletionCallbackHandle>::iterator I;
65
66 DeletionCallbackHandle(GlobalsAAResult &GAR, Value *V)
67 : CallbackVH(V), GAR(&GAR) {}
68
69 void deleted() override;
70 };
71
72 /// List of callbacks for globals being tracked by this analysis. Note that
73 /// these objects are quite large, but we only anticipate having one per
74 /// global tracked by this analysis. There are numerous optimizations we
75 /// could perform to the memory utilization here if this becomes a problem.
76 std::list<DeletionCallbackHandle> Handles;
77
78 explicit GlobalsAAResult(
79 const AnalysisOptions &Opts, const DataLayout &DL,
80 std::function<const TargetLibraryInfo &(Function &F)> GetTLI);
81
83
84public:
85 LLVM_ABI GlobalsAAResult(GlobalsAAResult &&Arg);
87
89 ModuleAnalysisManager::Invalidator &);
90
91 LLVM_ABI static GlobalsAAResult
93 std::function<const TargetLibraryInfo &(Function &F)> GetTLI,
94 CallGraph &CG);
95
96 //------------------------------------------------
97 // Implement the AliasAnalysis API
98 //
100 const MemoryLocation &LocB, AAQueryInfo &AAQI,
101 const Instruction *CtxI);
102
105 const MemoryLocation &Loc,
106 AAQueryInfo &AAQI);
107
109 /// getMemoryEffects - 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.
113
114private:
115 FunctionInfo *getFunctionInfo(const Function *F);
116
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(GlobalVariable *GV);
124 void CollectSCCMembership(CallGraph &CG);
125
126 bool isNonEscapingGlobalNoAlias(const GlobalValue *GV, const Value *V,
127 const Instruction *CtxI);
128 ModRefInfo getModRefInfoForArgument(const CallBase *Call,
129 const GlobalValue *GV, AAQueryInfo &AAQI);
130};
131
132/// Analysis pass providing a never-invalidated alias analysis result.
133class GlobalsAA : public AnalysisInfoMixin<GlobalsAA> {
135 LLVM_ABI static AnalysisKey Key;
136
137public:
139
141};
142
146
147/// Legacy wrapper pass to provide the GlobalsAAResult object.
149 std::unique_ptr<GlobalsAAResult> Result;
150
151public:
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//
170}
171
172#endif
#define LLVM_ABI
Definition Compiler.h:211
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
The mod/ref information collected for a particular function.
This class stores info we want to provide to or retain within an alias query.
MemoryEffects getMemoryEffects(const CallBase *Call, AAQueryInfo &AAQI)
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, AAQueryInfo &AAQI)
AAResultBase()=default
The possible results of an alias query.
Represent the analysis usage information of a pass.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
The basic data container for the call graph of a Module of IR.
Definition CallGraph.h:72
Value handle with callbacks on RAUW and destruction.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
An alias analysis result set for globals.
LLVM_ABI ~GlobalsAAResult()
LLVM_ABI ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, AAQueryInfo &AAQI)
LLVM_ABI bool invalidate(Module &M, const PreservedAnalyses &PA, ModuleAnalysisManager::Invalidator &)
static LLVM_ABI GlobalsAAResult analyzeModule(Module &M, std::function< const TargetLibraryInfo &(Function &F)> GetTLI, CallGraph &CG)
LLVM_ABI MemoryEffects getMemoryEffects(const Function *F)
getMemoryEffects - Return the behavior of the specified function if called from the specified call si...
LLVM_ABI AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI, const Instruction *CtxI)
alias - If one of the pointers is to a global that we are tracking, and the other is some random poin...
friend struct RecomputeGlobalsAAPass
const GlobalsAAResult & getResult() const
GlobalsAAResult & getResult()
Analysis pass providing a never-invalidated alias analysis result.
LLVM_ABI GlobalsAAResult run(Module &M, ModuleAnalysisManager &AM)
GlobalsAAResult Result
Representation for a specific memory location.
ModulePass(char &pid)
Definition Pass.h:255
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Provides information about what library functions are available for the current target.
LLVM Value Representation.
Definition Value.h:75
CallInst * Call
This is an optimization pass for GlobalISel generic memory operations.
MemoryEffectsBase< IRMemLocation > MemoryEffects
Summary of how a function affects memory in the program.
Definition ModRef.h:356
LLVM_ABI ModulePass * createGlobalsAAWrapperPass()
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
Definition ModRef.h:28
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
A CRTP mix-in that provides informational APIs needed for analysis passes.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
A CRTP mix-in for passes that can be skipped.
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)