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
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?)
59
60 /// Handle to clear this analysis on deletion of values.
61 struct LLVM_ABI 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(const AnalysisOptions &Opts, const DataLayout &DL);
78
80
81public:
82 LLVM_ABI GlobalsAAResult(GlobalsAAResult &&Arg);
84
86 ModuleAnalysisManager::Invalidator &);
87
88 LLVM_ABI static GlobalsAAResult analyzeModule(Module &M, CallGraph &CG);
89
90 //------------------------------------------------
91 // Implement the AliasAnalysis API
92 //
94 const MemoryLocation &LocB, AAQueryInfo &AAQI,
95 const Instruction *CtxI);
96
99 const MemoryLocation &Loc,
100 AAQueryInfo &AAQI);
101
103 /// getMemoryEffects - Return the behavior of the specified function if
104 /// called from the specified call site. The call site may be null in which
105 /// case the most generic behavior of this function should be returned.
107
108private:
109 FunctionInfo *getFunctionInfo(const Function *F);
110
111 void AnalyzeGlobals(Module &M);
112 void AnalyzeCallGraph(CallGraph &CG, Module &M);
113 bool AnalyzeUsesOfPointer(Value *V,
114 SmallPtrSetImpl<Function *> *Readers = nullptr,
115 SmallPtrSetImpl<Function *> *Writers = nullptr,
116 GlobalValue *OkayStoreDest = nullptr);
117 bool AnalyzeIndirectGlobalMemory(GlobalVariable *GV);
118 void CollectSCCMembership(CallGraph &CG);
119
120 bool isNonEscapingGlobalNoAlias(const GlobalValue *GV, const Value *V,
121 const Instruction *CtxI);
122 ModRefInfo getModRefInfoForArgument(const CallBase *Call,
123 const GlobalValue *GV, AAQueryInfo &AAQI);
124};
125
126/// Analysis pass providing a never-invalidated alias analysis result.
127class GlobalsAA : public AnalysisInfoMixin<GlobalsAA> {
129 LLVM_ABI static AnalysisKey Key;
130
131public:
133
135};
136
140
141/// Legacy wrapper pass to provide the GlobalsAAResult object.
143 std::unique_ptr<GlobalsAAResult> Result;
144
145public:
146 static char ID;
147
149
150 GlobalsAAResult &getResult() { return *Result; }
151 const GlobalsAAResult &getResult() const { return *Result; }
152
153 bool runOnModule(Module &M) override;
154 bool doFinalization(Module &M) override;
155 void getAnalysisUsage(AnalysisUsage &AU) const override;
156};
157
158//===--------------------------------------------------------------------===//
159//
160// createGlobalsAAWrapperPass - This pass provides alias and mod/ref info for
161// global values that do not have their addresses taken.
162//
164}
165
166#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:63
An alias analysis result set for globals.
static LLVM_ABI GlobalsAAResult analyzeModule(Module &M, CallGraph &CG)
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 &)
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.
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)