xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Analysis/BasicAliasAnalysis.h (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
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/Optional.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/Analysis/AliasAnalysis.h"
19 #include "llvm/IR/PassManager.h"
20 #include "llvm/Pass.h"
21 #include <algorithm>
22 #include <cstdint>
23 #include <memory>
24 #include <utility>
25 
26 namespace llvm {
27 
28 class AssumptionCache;
29 class BasicBlock;
30 class DataLayout;
31 class DominatorTree;
32 class Function;
33 class GEPOperator;
34 class PHINode;
35 class SelectInst;
36 class TargetLibraryInfo;
37 class PhiValues;
38 class Value;
39 
40 /// This is the AA result object for the basic, local, and stateless alias
41 /// analysis. It implements the AA query interface in an entirely stateless
42 /// manner. As one consequence, it is never invalidated due to IR changes.
43 /// While it does retain some storage, that is used as an optimization and not
44 /// to preserve information from query to query. However it does retain handles
45 /// to various other analyses and must be recomputed when those analyses are.
46 class BasicAAResult : public AAResultBase<BasicAAResult> {
47   friend AAResultBase<BasicAAResult>;
48 
49   const DataLayout &DL;
50   const Function &F;
51   const TargetLibraryInfo &TLI;
52   AssumptionCache &AC;
53   DominatorTree *DT;
54   PhiValues *PV;
55 
56 public:
57   BasicAAResult(const DataLayout &DL, const Function &F,
58                 const TargetLibraryInfo &TLI, AssumptionCache &AC,
59                 DominatorTree *DT = nullptr, PhiValues *PV = nullptr)
60       : DL(DL), F(F), TLI(TLI), AC(AC), DT(DT), PV(PV) {}
61 
62   BasicAAResult(const BasicAAResult &Arg)
63       : AAResultBase(Arg), DL(Arg.DL), F(Arg.F), TLI(Arg.TLI), AC(Arg.AC),
64         DT(Arg.DT), PV(Arg.PV) {}
65   BasicAAResult(BasicAAResult &&Arg)
66       : AAResultBase(std::move(Arg)), DL(Arg.DL), F(Arg.F), TLI(Arg.TLI),
67         AC(Arg.AC), DT(Arg.DT), PV(Arg.PV) {}
68 
69   /// Handle invalidation events in the new pass manager.
70   bool invalidate(Function &Fn, const PreservedAnalyses &PA,
71                   FunctionAnalysisManager::Invalidator &Inv);
72 
73   AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB,
74                     AAQueryInfo &AAQI);
75 
76   ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc,
77                            AAQueryInfo &AAQI);
78 
79   ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2,
80                            AAQueryInfo &AAQI);
81 
82   /// Chases pointers until we find a (constant global) or not.
83   bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI,
84                               bool OrLocal);
85 
86   /// Get the location associated with a pointer argument of a callsite.
87   ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx);
88 
89   /// Returns the behavior when calling the given call site.
90   FunctionModRefBehavior getModRefBehavior(const CallBase *Call);
91 
92   /// Returns the behavior when calling the given function. For use when the
93   /// call site is not known.
94   FunctionModRefBehavior getModRefBehavior(const Function *Fn);
95 
96 private:
97   struct DecomposedGEP;
98 
99   /// Tracks phi nodes we have visited.
100   ///
101   /// When interpret "Value" pointer equality as value equality we need to make
102   /// sure that the "Value" is not part of a cycle. Otherwise, two uses could
103   /// come from different "iterations" of a cycle and see different values for
104   /// the same "Value" pointer.
105   ///
106   /// The following example shows the problem:
107   ///   %p = phi(%alloca1, %addr2)
108   ///   %l = load %ptr
109   ///   %addr1 = gep, %alloca2, 0, %l
110   ///   %addr2 = gep  %alloca2, 0, (%l + 1)
111   ///      alias(%p, %addr1) -> MayAlias !
112   ///   store %l, ...
113   SmallPtrSet<const BasicBlock *, 8> VisitedPhiBBs;
114 
115   /// Tracks instructions visited by pointsToConstantMemory.
116   SmallPtrSet<const Value *, 16> Visited;
117 
118   static DecomposedGEP
119   DecomposeGEPExpression(const Value *V, const DataLayout &DL,
120                          AssumptionCache *AC, DominatorTree *DT);
121 
122   /// A Heuristic for aliasGEP that searches for a constant offset
123   /// between the variables.
124   ///
125   /// GetLinearExpression has some limitations, as generally zext(%x + 1)
126   /// != zext(%x) + zext(1) if the arithmetic overflows. GetLinearExpression
127   /// will therefore conservatively refuse to decompose these expressions.
128   /// However, we know that, for all %x, zext(%x) != zext(%x + 1), even if
129   /// the addition overflows.
130   bool
131   constantOffsetHeuristic(const DecomposedGEP &GEP, LocationSize V1Size,
132                           LocationSize V2Size, AssumptionCache *AC,
133                           DominatorTree *DT);
134 
135   bool isValueEqualInPotentialCycles(const Value *V1, const Value *V2);
136 
137   void subtractDecomposedGEPs(DecomposedGEP &DestGEP,
138                               const DecomposedGEP &SrcGEP);
139 
140   AliasResult aliasGEP(const GEPOperator *V1, LocationSize V1Size,
141                        const Value *V2, LocationSize V2Size,
142                        const Value *UnderlyingV1, const Value *UnderlyingV2,
143                        AAQueryInfo &AAQI);
144 
145   AliasResult aliasPHI(const PHINode *PN, LocationSize PNSize,
146                        const Value *V2, LocationSize V2Size, AAQueryInfo &AAQI);
147 
148   AliasResult aliasSelect(const SelectInst *SI, LocationSize SISize,
149                           const Value *V2, LocationSize V2Size,
150                           AAQueryInfo &AAQI);
151 
152   AliasResult aliasCheck(const Value *V1, LocationSize V1Size,
153                          const Value *V2, LocationSize V2Size,
154                          AAQueryInfo &AAQI);
155 
156   AliasResult aliasCheckRecursive(const Value *V1, LocationSize V1Size,
157                                   const Value *V2, LocationSize V2Size,
158                                   AAQueryInfo &AAQI, const Value *O1,
159                                   const Value *O2);
160 };
161 
162 /// Analysis pass providing a never-invalidated alias analysis result.
163 class BasicAA : public AnalysisInfoMixin<BasicAA> {
164   friend AnalysisInfoMixin<BasicAA>;
165 
166   static AnalysisKey Key;
167 
168 public:
169   using Result = BasicAAResult;
170 
171   BasicAAResult run(Function &F, FunctionAnalysisManager &AM);
172 };
173 
174 /// Legacy wrapper pass to provide the BasicAAResult object.
175 class BasicAAWrapperPass : public FunctionPass {
176   std::unique_ptr<BasicAAResult> Result;
177 
178   virtual void anchor();
179 
180 public:
181   static char ID;
182 
183   BasicAAWrapperPass();
184 
185   BasicAAResult &getResult() { return *Result; }
186   const BasicAAResult &getResult() const { return *Result; }
187 
188   bool runOnFunction(Function &F) override;
189   void getAnalysisUsage(AnalysisUsage &AU) const override;
190 };
191 
192 FunctionPass *createBasicAAWrapperPass();
193 
194 /// A helper for the legacy pass manager to create a \c BasicAAResult object
195 /// populated to the best of our ability for a particular function when inside
196 /// of a \c ModulePass or a \c CallGraphSCCPass.
197 BasicAAResult createLegacyPMBasicAAResult(Pass &P, Function &F);
198 
199 /// This class is a functor to be used in legacy module or SCC passes for
200 /// computing AA results for a function. We store the results in fields so that
201 /// they live long enough to be queried, but we re-use them each time.
202 class LegacyAARGetter {
203   Pass &P;
204   Optional<BasicAAResult> BAR;
205   Optional<AAResults> AAR;
206 
207 public:
208   LegacyAARGetter(Pass &P) : P(P) {}
209   AAResults &operator()(Function &F) {
210     BAR.emplace(createLegacyPMBasicAAResult(P, F));
211     AAR.emplace(createLegacyPMAAResults(P, F, *BAR));
212     return *AAR;
213   }
214 };
215 
216 } // end namespace llvm
217 
218 #endif // LLVM_ANALYSIS_BASICALIASANALYSIS_H
219