LLVM  9.0.0svn
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 
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/IR/InstrTypes.h"
24 #include "llvm/IR/PassManager.h"
25 #include "llvm/Pass.h"
26 #include <algorithm>
27 #include <cstdint>
28 #include <memory>
29 #include <utility>
30 
31 namespace llvm {
32 
33 struct AAMDNodes;
34 class APInt;
35 class AssumptionCache;
36 class BasicBlock;
37 class DataLayout;
38 class DominatorTree;
39 class Function;
40 class GEPOperator;
41 class LoopInfo;
42 class PHINode;
43 class SelectInst;
44 class TargetLibraryInfo;
45 class PhiValues;
46 class Value;
47 
48 /// This is the AA result object for the basic, local, and stateless alias
49 /// analysis. It implements the AA query interface in an entirely stateless
50 /// manner. As one consequence, it is never invalidated due to IR changes.
51 /// While it does retain some storage, that is used as an optimization and not
52 /// to preserve information from query to query. However it does retain handles
53 /// to various other analyses and must be recomputed when those analyses are.
54 class BasicAAResult : public AAResultBase<BasicAAResult> {
56 
57  const DataLayout &DL;
58  const Function &F;
59  const TargetLibraryInfo &TLI;
60  AssumptionCache &AC;
61  DominatorTree *DT;
62  LoopInfo *LI;
63  PhiValues *PV;
64 
65 public:
66  BasicAAResult(const DataLayout &DL, const Function &F,
67  const TargetLibraryInfo &TLI, AssumptionCache &AC,
68  DominatorTree *DT = nullptr, LoopInfo *LI = nullptr,
69  PhiValues *PV = nullptr)
70  : AAResultBase(), DL(DL), F(F), TLI(TLI), AC(AC), DT(DT), LI(LI), PV(PV)
71  {}
72 
74  : AAResultBase(Arg), DL(Arg.DL), F(Arg.F), TLI(Arg.TLI), AC(Arg.AC),
75  DT(Arg.DT), LI(Arg.LI), PV(Arg.PV) {}
77  : AAResultBase(std::move(Arg)), DL(Arg.DL), F(Arg.F), TLI(Arg.TLI),
78  AC(Arg.AC), DT(Arg.DT), LI(Arg.LI), PV(Arg.PV) {}
79 
80  /// Handle invalidation events in the new pass manager.
81  bool invalidate(Function &Fn, const PreservedAnalyses &PA,
83 
84  AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB,
85  AAQueryInfo &AAQI);
86 
87  ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc,
88  AAQueryInfo &AAQI);
89 
90  ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2,
91  AAQueryInfo &AAQI);
92 
93  /// Chases pointers until we find a (constant global) or not.
94  bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI,
95  bool OrLocal);
96 
97  /// Get the location associated with a pointer argument of a callsite.
98  ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx);
99 
100  /// Returns the behavior when calling the given call site.
102 
103  /// Returns the behavior when calling the given function. For use when the
104  /// call site is not known.
106 
107 private:
108  // A linear transformation of a Value; this class represents ZExt(SExt(V,
109  // SExtBits), ZExtBits) * Scale + Offset.
110  struct VariableGEPIndex {
111  // An opaque Value - we can't decompose this further.
112  const Value *V;
113 
114  // We need to track what extensions we've done as we consider the same Value
115  // with different extensions as different variables in a GEP's linear
116  // expression;
117  // e.g.: if V == -1, then sext(x) != zext(x).
118  unsigned ZExtBits;
119  unsigned SExtBits;
120 
121  APInt Scale;
122 
123  bool operator==(const VariableGEPIndex &Other) const {
124  return V == Other.V && ZExtBits == Other.ZExtBits &&
125  SExtBits == Other.SExtBits && Scale == Other.Scale;
126  }
127 
128  bool operator!=(const VariableGEPIndex &Other) const {
129  return !operator==(Other);
130  }
131  };
132 
133  // Represents the internal structure of a GEP, decomposed into a base pointer,
134  // constant offsets, and variable scaled indices.
135  struct DecomposedGEP {
136  // Base pointer of the GEP
137  const Value *Base;
138  // Total constant offset w.r.t the base from indexing into structs
139  APInt StructOffset;
140  // Total constant offset w.r.t the base from indexing through
141  // pointers/arrays/vectors
142  APInt OtherOffset;
143  // Scaled variable (non-constant) indices.
145  };
146 
147  /// Tracks phi nodes we have visited.
148  ///
149  /// When interpret "Value" pointer equality as value equality we need to make
150  /// sure that the "Value" is not part of a cycle. Otherwise, two uses could
151  /// come from different "iterations" of a cycle and see different values for
152  /// the same "Value" pointer.
153  ///
154  /// The following example shows the problem:
155  /// %p = phi(%alloca1, %addr2)
156  /// %l = load %ptr
157  /// %addr1 = gep, %alloca2, 0, %l
158  /// %addr2 = gep %alloca2, 0, (%l + 1)
159  /// alias(%p, %addr1) -> MayAlias !
160  /// store %l, ...
162 
163  /// Tracks instructions visited by pointsToConstantMemory.
165 
166  static const Value *
167  GetLinearExpression(const Value *V, APInt &Scale, APInt &Offset,
168  unsigned &ZExtBits, unsigned &SExtBits,
169  const DataLayout &DL, unsigned Depth, AssumptionCache *AC,
170  DominatorTree *DT, bool &NSW, bool &NUW);
171 
172  static bool DecomposeGEPExpression(const Value *V, DecomposedGEP &Decomposed,
173  const DataLayout &DL, AssumptionCache *AC, DominatorTree *DT);
174 
175  static bool isGEPBaseAtNegativeOffset(const GEPOperator *GEPOp,
176  const DecomposedGEP &DecompGEP, const DecomposedGEP &DecompObject,
177  LocationSize ObjectAccessSize);
178 
179  /// A Heuristic for aliasGEP that searches for a constant offset
180  /// between the variables.
181  ///
182  /// GetLinearExpression has some limitations, as generally zext(%x + 1)
183  /// != zext(%x) + zext(1) if the arithmetic overflows. GetLinearExpression
184  /// will therefore conservatively refuse to decompose these expressions.
185  /// However, we know that, for all %x, zext(%x) != zext(%x + 1), even if
186  /// the addition overflows.
187  bool
188  constantOffsetHeuristic(const SmallVectorImpl<VariableGEPIndex> &VarIndices,
189  LocationSize V1Size, LocationSize V2Size,
190  APInt BaseOffset, AssumptionCache *AC,
191  DominatorTree *DT);
192 
193  bool isValueEqualInPotentialCycles(const Value *V1, const Value *V2);
194 
195  void GetIndexDifference(SmallVectorImpl<VariableGEPIndex> &Dest,
197 
198  AliasResult aliasGEP(const GEPOperator *V1, LocationSize V1Size,
199  const AAMDNodes &V1AAInfo, const Value *V2,
200  LocationSize V2Size, const AAMDNodes &V2AAInfo,
201  const Value *UnderlyingV1, const Value *UnderlyingV2,
202  AAQueryInfo &AAQI);
203 
204  AliasResult aliasPHI(const PHINode *PN, LocationSize PNSize,
205  const AAMDNodes &PNAAInfo, const Value *V2,
206  LocationSize V2Size, const AAMDNodes &V2AAInfo,
207  const Value *UnderV2, AAQueryInfo &AAQI);
208 
209  AliasResult aliasSelect(const SelectInst *SI, LocationSize SISize,
210  const AAMDNodes &SIAAInfo, const Value *V2,
211  LocationSize V2Size, const AAMDNodes &V2AAInfo,
212  const Value *UnderV2, AAQueryInfo &AAQI);
213 
214  AliasResult aliasCheck(const Value *V1, LocationSize V1Size,
215  AAMDNodes V1AATag, const Value *V2,
216  LocationSize V2Size, AAMDNodes V2AATag,
217  AAQueryInfo &AAQI, const Value *O1 = nullptr,
218  const Value *O2 = nullptr);
219 };
220 
221 /// Analysis pass providing a never-invalidated alias analysis result.
222 class BasicAA : public AnalysisInfoMixin<BasicAA> {
224 
225  static AnalysisKey Key;
226 
227 public:
229 
231 };
232 
233 /// Legacy wrapper pass to provide the BasicAAResult object.
235  std::unique_ptr<BasicAAResult> Result;
236 
237  virtual void anchor();
238 
239 public:
240  static char ID;
241 
243 
244  BasicAAResult &getResult() { return *Result; }
245  const BasicAAResult &getResult() const { return *Result; }
246 
247  bool runOnFunction(Function &F) override;
248  void getAnalysisUsage(AnalysisUsage &AU) const override;
249 };
250 
252 
253 /// A helper for the legacy pass manager to create a \c BasicAAResult object
254 /// populated to the best of our ability for a particular function when inside
255 /// of a \c ModulePass or a \c CallGraphSCCPass.
257 
258 /// This class is a functor to be used in legacy module or SCC passes for
259 /// computing AA results for a function. We store the results in fields so that
260 /// they live long enough to be queried, but we re-use them each time.
262  Pass &P;
265 
266 public:
267  LegacyAARGetter(Pass &P) : P(P) {}
270  AAR.emplace(createLegacyPMAAResults(P, F, *BAR));
271  return *AAR;
272  }
273 };
274 
275 } // end namespace llvm
276 
277 #endif // LLVM_ANALYSIS_BASICALIASANALYSIS_H
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:80
bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI, bool OrLocal)
Chases pointers until we find a (constant global) or not.
const BasicAAResult & getResult() const
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Various leaf nodes.
Definition: ISDOpcodes.h:59
BasicAAResult createLegacyPMBasicAAResult(Pass &P, Function &F)
A helper for the legacy pass manager to create a BasicAAResult object populated to the best of our ab...
This class stores info we want to provide to or retain within an alias query.
void emplace(ArgTypes &&... Args)
Create a new object by constructing it in place with the given arguments.
Definition: Optional.h:237
A cache of @llvm.assume calls within a function.
This is the AA result object for the basic, local, and stateless alias analysis.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1014
ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx)
Get the location associated with a pointer argument of a callsite.
Definition: BitVector.h:937
This class represents the LLVM &#39;select&#39; instruction.
This class is a functor to be used in legacy module or SCC passes for computing AA results for a func...
AAResults createLegacyPMAAResults(Pass &P, Function &F, BasicAAResult &BAR)
A helper for the legacy pass manager to create a AAResults object populated to the best of our abilit...
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:849
A CRTP-driven "mixin" base class to help implement the function alias analysis results concept...
Key
PAL metadata keys.
#define F(x, y, z)
Definition: MD5.cpp:55
FunctionModRefBehavior
Summary of how a function affects memory in the program.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:144
AliasResult
The possible results of an alias query.
Definition: AliasAnalysis.h:78
BasicAAResult(const DataLayout &DL, const Function &F, const TargetLibraryInfo &TLI, AssumptionCache &AC, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, PhiValues *PV=nullptr)
static bool runOnFunction(Function &F, bool PostInlining)
#define P(N)
FunctionModRefBehavior getModRefBehavior(const CallBase *Call)
Returns the behavior when calling the given call site.
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
AAResults & operator()(Function &F)
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:382
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
bool invalidate(Function &Fn, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
Handle invalidation events in the new pass manager.
Representation for a specific memory location.
Provides information about what library functions are available for the current target.
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:643
Class for arbitrary precision integers.
Definition: APInt.h:69
FunctionPass * createBasicAAWrapperPass()
Analysis pass providing a never-invalidated alias analysis result.
bool operator!=(uint64_t V1, const APInt &V2)
Definition: APInt.h:1968
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, AAQueryInfo &AAQI)
Checks to see if the specified callsite can clobber the specified memory object.
BasicAAResult(BasicAAResult &&Arg)
BasicAAResult(const BasicAAResult &Arg)
This file provides utility analysis objects describing memory locations.
Class for calculating and caching the underlying values of phis in a function.
Definition: PhiValues.h:42
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:641
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI)
LLVM Value Representation.
Definition: Value.h:72
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
A container for analyses that lazily runs them and caches their results.
bool operator==(uint64_t V1, const APInt &V2)
Definition: APInt.h:1966
This header defines various interfaces for pass management in LLVM.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:70
Legacy wrapper pass to provide the BasicAAResult object.