LLVM 24.0.0git
BasicAliasAnalysis.h
Go to the documentation of this file.
1//===- BasicAliasAnalysis.h - Stateless, local Alias Analysis ---*- 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 LLVM's primary stateless and local alias analysis.
10///
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_ANALYSIS_BASICALIASANALYSIS_H
14#define LLVM_ANALYSIS_BASICALIASANALYSIS_H
15
18#include "llvm/IR/PassManager.h"
19#include "llvm/Pass.h"
21#include <memory>
22#include <utility>
23
24namespace llvm {
25
26struct AnalysisOptions;
27class AssumptionCache;
28class DataLayout;
29class DominatorTree;
30class Function;
31class GEPOperator;
32struct KnownBits;
33class PHINode;
34class SelectInst;
36class Value;
37
38/// This is the AA result object for the basic, local, and stateless alias
39/// analysis. It implements the AA query interface in an entirely stateless
40/// manner. As one consequence, it is never invalidated due to IR changes.
41/// While it does retain some storage, that is used as an optimization and not
42/// to preserve information from query to query. However it does retain handles
43/// to various other analyses and must be recomputed when those analyses are.
45 const AnalysisOptions &Opts;
46 const DataLayout &DL;
47 const Function &F;
48 const TargetLibraryInfo &TLI;
50 /// Use getDT() instead of accessing this member directly, in order to
51 /// respect the AAQI.UseDominatorTree option.
52 DominatorTree *DT_;
53
54 DominatorTree *getDT(const AAQueryInfo &AAQI) const {
55 return AAQI.UseDominatorTree ? DT_ : nullptr;
56 }
57
58public:
59 LLVM_ABI BasicAAResult(const DataLayout &DL, const Function &F,
60 const TargetLibraryInfo &TLI, AssumptionCache &AC,
61 DominatorTree *DT = nullptr);
62
64 : AAResultBase(Arg), Opts(Arg.Opts), DL(Arg.DL), F(Arg.F), TLI(Arg.TLI),
65 AC(Arg.AC), DT_(Arg.DT_) {}
67 : AAResultBase(std::move(Arg)), Opts(Arg.Opts), DL(Arg.DL), F(Arg.F),
68 TLI(Arg.TLI), AC(Arg.AC), DT_(Arg.DT_) {}
69
70 /// Handle invalidation events in the new pass manager.
72 FunctionAnalysisManager::Invalidator &Inv);
73
75 const MemoryLocation &LocB, AAQueryInfo &AAQI,
76 const Instruction *CtxI);
77
79 const Instruction *CtxI);
80
83 const MemoryLocation &Loc,
84 AAQueryInfo &AAQI);
85
87 const CallBase *Call2, AAQueryInfo &AAQI);
88
89 /// Returns a bitmask that should be unconditionally applied to the ModRef
90 /// info of a memory location. This allows us to eliminate Mod and/or Ref
91 /// from the ModRef info based on the knowledge that the memory location
92 /// points to constant and/or locally-invariant memory.
93 ///
94 /// If IgnoreLocals is true, then this method returns NoModRef for memory
95 /// that points to a local alloca.
97 AAQueryInfo &AAQI,
98 bool IgnoreLocals = false);
99
100 /// Get the location associated with a pointer argument of a callsite.
101 LLVM_ABI ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx);
102
103 /// Returns the behavior when calling the given call site.
105 AAQueryInfo &AAQI);
106
107 /// Returns the behavior when calling the given function. For use when the
108 /// call site is not known.
110
111private:
112 struct DecomposedGEP;
113 struct VariableGEPOffsetInfo;
114
115 /// Tracks instructions visited by pointsToConstantMemory.
117
118 static DecomposedGEP
119 DecomposeGEPExpression(const Value *V, const DataLayout &DL,
121
122 /// Analyze the variable indices of a decomposed GEP, computing the GCD
123 /// that each Scale*V term is a multiple of, and an approximate range of
124 /// possible total offsets.
125 VariableGEPOffsetInfo analyzeVariableOffsets(const DecomposedGEP &GEP,
126 DominatorTree *DT);
127
128 /// Try to determine the range of values for VarIndex such that
129 /// VarIndex <= -MinAbsVarIndex || MinAbsVarIndex <= VarIndex, thus
130 /// establishing a minimum absolute value of the variable offset.
131 std::optional<APInt> computeMinAbsVarOffset(const DecomposedGEP &GEP,
132 ArrayRef<KnownBits> VIKnownBits,
133 DominatorTree *DT,
134 const AAQueryInfo &AAQI);
135
136 /// A Heuristic for aliasGEP that searches for a constant offset
137 /// between the variables.
138 ///
139 /// GetLinearExpression has some limitations, as generally zext(%x + 1)
140 /// != zext(%x) + zext(1) if the arithmetic overflows. GetLinearExpression
141 /// will therefore conservatively refuse to decompose these expressions.
142 /// However, we know that, for all %x, zext(%x) != zext(%x + 1), even if
143 /// the addition overflows.
144 bool computeConstantOffsetHeuristic(const DecomposedGEP &GEP,
145 LocationSize V1Size, LocationSize V2Size,
147 const AAQueryInfo &AAQI);
148
149 bool isValueEqualInPotentialCycles(const Value *V1, const Value *V2,
150 const AAQueryInfo &AAQI);
151
152 void subtractDecomposedGEPs(DecomposedGEP &DestGEP,
153 const DecomposedGEP &SrcGEP,
154 const AAQueryInfo &AAQI);
155
156 AliasResult aliasGEP(const GEPOperator *V1, LocationSize V1Size,
157 const Value *V2, LocationSize V2Size,
158 const Value *UnderlyingV1, const Value *UnderlyingV2,
159 AAQueryInfo &AAQI);
160
161 AliasResult aliasPHI(const PHINode *PN, LocationSize PNSize,
162 const Value *V2, LocationSize V2Size, AAQueryInfo &AAQI);
163
164 AliasResult aliasSelect(const SelectInst *SI, LocationSize SISize,
165 const Value *V2, LocationSize V2Size,
166 AAQueryInfo &AAQI);
167
168 AliasResult aliasCheck(const Value *V1, LocationSize V1Size, const Value *V2,
169 LocationSize V2Size, AAQueryInfo &AAQI,
170 const Instruction *CtxI);
171
172 AliasResult aliasCheckRecursive(const Value *V1, LocationSize V1Size,
173 const Value *V2, LocationSize V2Size,
174 AAQueryInfo &AAQI, const Value *O1,
175 const Value *O2);
176};
177
178/// Analysis pass providing a never-invalidated alias analysis result.
179class BasicAA : public AnalysisInfoMixin<BasicAA> {
181
182 LLVM_ABI static AnalysisKey Key;
183
184public:
186
188};
189
190/// Legacy wrapper pass to provide the BasicAAResult object.
192 std::unique_ptr<BasicAAResult> Result;
193
194 virtual void anchor();
195
196public:
197 static char ID;
198
200
201 BasicAAResult &getResult() { return *Result; }
202 const BasicAAResult &getResult() const { return *Result; }
203
204 bool runOnFunction(Function &F) override;
205 void getAnalysisUsage(AnalysisUsage &AU) const override;
206};
207
208LLVM_ABI FunctionPass *createBasicAAWrapperPass();
209
210} // end namespace llvm
211
212#endif // LLVM_ANALYSIS_BASICALIASANALYSIS_H
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define LLVM_ABI
Definition Compiler.h:211
static bool runOnFunction(Function &F, bool PostInlining)
Hexagon Common GEP
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
This file defines the SmallPtrSet class.
This class stores info we want to provide to or retain within an alias query.
bool UseDominatorTree
Whether alias analysis is allowed to use the dominator tree, for use by passes that lazily update the...
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.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
A cache of @llvm.assume calls within a function.
This is the AA result object for the basic, local, and stateless alias analysis.
LLVM_ABI ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, AAQueryInfo &AAQI)
Checks to see if the specified callsite can clobber the specified memory object.
LLVM_ABI ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx)
Get the location associated with a pointer argument of a callsite.
BasicAAResult(BasicAAResult &&Arg)
LLVM_ABI BasicAAResult(const DataLayout &DL, const Function &F, const TargetLibraryInfo &TLI, AssumptionCache &AC, DominatorTree *DT=nullptr)
LLVM_ABI MemoryEffects getMemoryEffects(const CallBase *Call, AAQueryInfo &AAQI)
Returns the behavior when calling the given call site.
LLVM_ABI AliasResult aliasErrno(const MemoryLocation &Loc, const Instruction *CtxI)
BasicAAResult(const BasicAAResult &Arg)
LLVM_ABI ModRefInfo getModRefInfoMask(const MemoryLocation &Loc, AAQueryInfo &AAQI, bool IgnoreLocals=false)
Returns a bitmask that should be unconditionally applied to the ModRef info of a memory location.
LLVM_ABI bool invalidate(Function &Fn, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
Handle invalidation events in the new pass manager.
LLVM_ABI AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI, const Instruction *CtxI)
const BasicAAResult & getResult() const
Analysis pass providing a never-invalidated alias analysis result.
BasicAAResult Result
LLVM_ABI BasicAAResult run(Function &F, FunctionAnalysisManager &AM)
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
FunctionPass(char &pid)
Definition Pass.h:314
Representation for a specific memory location.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
This class represents the LLVM 'select' instruction.
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.
@ O1
Optimize quickly without destroying debuggability.
@ O2
Optimize for fast execution as much as possible without triggering significant incremental compile ti...
MemoryEffectsBase< IRMemLocation > MemoryEffects
Summary of how a function affects memory in the program.
Definition ModRef.h:356
LLVM_ABI FunctionPass * createBasicAAWrapperPass()
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
Definition ModRef.h:28
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1933
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
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