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"
20 #include "llvm/Analysis/AliasAnalysis.h"
21 #include "llvm/IR/PassManager.h"
22 #include "llvm/Pass.h"
23 #include <algorithm>
24 #include <cstdint>
25 #include <memory>
26 #include <utility>
27 
28 namespace llvm {
29 
30 struct AAMDNodes;
31 class APInt;
32 class AssumptionCache;
33 class BasicBlock;
34 class DataLayout;
35 class DominatorTree;
36 class Function;
37 class GEPOperator;
38 class LoopInfo;
39 class PHINode;
40 class SelectInst;
41 class TargetLibraryInfo;
42 class PhiValues;
43 class Value;
44 
45 /// This is the AA result object for the basic, local, and stateless alias
46 /// analysis. It implements the AA query interface in an entirely stateless
47 /// manner. As one consequence, it is never invalidated due to IR changes.
48 /// While it does retain some storage, that is used as an optimization and not
49 /// to preserve information from query to query. However it does retain handles
50 /// to various other analyses and must be recomputed when those analyses are.
51 class BasicAAResult : public AAResultBase<BasicAAResult> {
52   friend AAResultBase<BasicAAResult>;
53 
54   const DataLayout &DL;
55   const Function &F;
56   const TargetLibraryInfo &TLI;
57   AssumptionCache &AC;
58   DominatorTree *DT;
59   LoopInfo *LI;
60   PhiValues *PV;
61 
62 public:
63   BasicAAResult(const DataLayout &DL, const Function &F,
64                 const TargetLibraryInfo &TLI, AssumptionCache &AC,
65                 DominatorTree *DT = nullptr, LoopInfo *LI = nullptr,
66                 PhiValues *PV = nullptr)
67       : AAResultBase(), DL(DL), F(F), TLI(TLI), AC(AC), DT(DT), LI(LI), PV(PV)
68         {}
69 
70   BasicAAResult(const BasicAAResult &Arg)
71       : AAResultBase(Arg), DL(Arg.DL), F(Arg.F), TLI(Arg.TLI), AC(Arg.AC),
72         DT(Arg.DT),  LI(Arg.LI), PV(Arg.PV) {}
73   BasicAAResult(BasicAAResult &&Arg)
74       : AAResultBase(std::move(Arg)), DL(Arg.DL), F(Arg.F), TLI(Arg.TLI),
75         AC(Arg.AC), DT(Arg.DT), LI(Arg.LI), PV(Arg.PV) {}
76 
77   /// Handle invalidation events in the new pass manager.
78   bool invalidate(Function &Fn, const PreservedAnalyses &PA,
79                   FunctionAnalysisManager::Invalidator &Inv);
80 
81   AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB,
82                     AAQueryInfo &AAQI);
83 
84   ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc,
85                            AAQueryInfo &AAQI);
86 
87   ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2,
88                            AAQueryInfo &AAQI);
89 
90   /// Chases pointers until we find a (constant global) or not.
91   bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI,
92                               bool OrLocal);
93 
94   /// Get the location associated with a pointer argument of a callsite.
95   ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx);
96 
97   /// Returns the behavior when calling the given call site.
98   FunctionModRefBehavior getModRefBehavior(const CallBase *Call);
99 
100   /// Returns the behavior when calling the given function. For use when the
101   /// call site is not known.
102   FunctionModRefBehavior getModRefBehavior(const Function *Fn);
103 
104 private:
105   // A linear transformation of a Value; this class represents ZExt(SExt(V,
106   // SExtBits), ZExtBits) * Scale + Offset.
107   struct VariableGEPIndex {
108     // An opaque Value - we can't decompose this further.
109     const Value *V;
110 
111     // We need to track what extensions we've done as we consider the same Value
112     // with different extensions as different variables in a GEP's linear
113     // expression;
114     // e.g.: if V == -1, then sext(x) != zext(x).
115     unsigned ZExtBits;
116     unsigned SExtBits;
117 
118     APInt Scale;
119 
120     // Context instruction to use when querying information about this index.
121     const Instruction *CxtI;
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     void dump() const {
133       print(dbgs());
134       dbgs() << "\n";
135     }
136     void print(raw_ostream &OS) const {
137       OS << "(V=" << V->getName()
138 	 << ", zextbits=" << ZExtBits
139 	 << ", sextbits=" << SExtBits
140 	 << ", scale=" << Scale << ")";
141     }
142   };
143 
144   // Represents the internal structure of a GEP, decomposed into a base pointer,
145   // constant offsets, and variable scaled indices.
146   struct DecomposedGEP {
147     // Base pointer of the GEP
148     const Value *Base;
149     // Total constant offset from base.
150     APInt Offset;
151     // Scaled variable (non-constant) indices.
152     SmallVector<VariableGEPIndex, 4> VarIndices;
153     // Is GEP index scale compile-time constant.
154     bool HasCompileTimeConstantScale;
155 
156     void dump() const {
157       print(dbgs());
158       dbgs() << "\n";
159     }
160     void print(raw_ostream &OS) const {
161       OS << "(DecomposedGEP Base=" << Base->getName()
162 	 << ", Offset=" << Offset
163 	 << ", VarIndices=[";
164       for (size_t i = 0; i < VarIndices.size(); i++) {
165        if (i != 0)
166          OS << ", ";
167        VarIndices[i].print(OS);
168       }
169       OS << "], HasCompileTimeConstantScale=" << HasCompileTimeConstantScale
170 	 << ")";
171     }
172   };
173 
174   /// Tracks phi nodes we have visited.
175   ///
176   /// When interpret "Value" pointer equality as value equality we need to make
177   /// sure that the "Value" is not part of a cycle. Otherwise, two uses could
178   /// come from different "iterations" of a cycle and see different values for
179   /// the same "Value" pointer.
180   ///
181   /// The following example shows the problem:
182   ///   %p = phi(%alloca1, %addr2)
183   ///   %l = load %ptr
184   ///   %addr1 = gep, %alloca2, 0, %l
185   ///   %addr2 = gep  %alloca2, 0, (%l + 1)
186   ///      alias(%p, %addr1) -> MayAlias !
187   ///   store %l, ...
188   SmallPtrSet<const BasicBlock *, 8> VisitedPhiBBs;
189 
190   /// Tracks instructions visited by pointsToConstantMemory.
191   SmallPtrSet<const Value *, 16> Visited;
192 
193   static const Value *
194   GetLinearExpression(const Value *V, APInt &Scale, APInt &Offset,
195                       unsigned &ZExtBits, unsigned &SExtBits,
196                       const DataLayout &DL, unsigned Depth, AssumptionCache *AC,
197                       DominatorTree *DT, bool &NSW, bool &NUW);
198 
199   static DecomposedGEP
200   DecomposeGEPExpression(const Value *V, const DataLayout &DL,
201                          AssumptionCache *AC, DominatorTree *DT);
202 
203   static bool isGEPBaseAtNegativeOffset(const GEPOperator *GEPOp,
204       const DecomposedGEP &DecompGEP, const DecomposedGEP &DecompObject,
205       LocationSize ObjectAccessSize);
206 
207   /// A Heuristic for aliasGEP that searches for a constant offset
208   /// between the variables.
209   ///
210   /// GetLinearExpression has some limitations, as generally zext(%x + 1)
211   /// != zext(%x) + zext(1) if the arithmetic overflows. GetLinearExpression
212   /// will therefore conservatively refuse to decompose these expressions.
213   /// However, we know that, for all %x, zext(%x) != zext(%x + 1), even if
214   /// the addition overflows.
215   bool
216   constantOffsetHeuristic(const SmallVectorImpl<VariableGEPIndex> &VarIndices,
217                           LocationSize V1Size, LocationSize V2Size,
218                           const APInt &BaseOffset, AssumptionCache *AC,
219                           DominatorTree *DT);
220 
221   bool isValueEqualInPotentialCycles(const Value *V1, const Value *V2);
222 
223   void GetIndexDifference(SmallVectorImpl<VariableGEPIndex> &Dest,
224                           const SmallVectorImpl<VariableGEPIndex> &Src);
225 
226   AliasResult aliasGEP(const GEPOperator *V1, LocationSize V1Size,
227                        const AAMDNodes &V1AAInfo, const Value *V2,
228                        LocationSize V2Size, const AAMDNodes &V2AAInfo,
229                        const Value *UnderlyingV1, const Value *UnderlyingV2,
230                        AAQueryInfo &AAQI);
231 
232   AliasResult aliasPHI(const PHINode *PN, LocationSize PNSize,
233                        const AAMDNodes &PNAAInfo, const Value *V2,
234                        LocationSize V2Size, const AAMDNodes &V2AAInfo,
235                        AAQueryInfo &AAQI);
236 
237   AliasResult aliasSelect(const SelectInst *SI, LocationSize SISize,
238                           const AAMDNodes &SIAAInfo, const Value *V2,
239                           LocationSize V2Size, const AAMDNodes &V2AAInfo,
240                           AAQueryInfo &AAQI);
241 
242   AliasResult aliasCheck(const Value *V1, LocationSize V1Size,
243                          const AAMDNodes &V1AATag, const Value *V2,
244                          LocationSize V2Size, const AAMDNodes &V2AATag,
245                          AAQueryInfo &AAQI);
246 
247   AliasResult aliasCheckRecursive(const Value *V1, LocationSize V1Size,
248                                   const AAMDNodes &V1AATag, const Value *V2,
249                                   LocationSize V2Size, const AAMDNodes &V2AATag,
250                                   AAQueryInfo &AAQI, const Value *O1,
251                                   const Value *O2);
252 };
253 
254 /// Analysis pass providing a never-invalidated alias analysis result.
255 class BasicAA : public AnalysisInfoMixin<BasicAA> {
256   friend AnalysisInfoMixin<BasicAA>;
257 
258   static AnalysisKey Key;
259 
260 public:
261   using Result = BasicAAResult;
262 
263   BasicAAResult run(Function &F, FunctionAnalysisManager &AM);
264 };
265 
266 /// Legacy wrapper pass to provide the BasicAAResult object.
267 class BasicAAWrapperPass : public FunctionPass {
268   std::unique_ptr<BasicAAResult> Result;
269 
270   virtual void anchor();
271 
272 public:
273   static char ID;
274 
275   BasicAAWrapperPass();
276 
277   BasicAAResult &getResult() { return *Result; }
278   const BasicAAResult &getResult() const { return *Result; }
279 
280   bool runOnFunction(Function &F) override;
281   void getAnalysisUsage(AnalysisUsage &AU) const override;
282 };
283 
284 FunctionPass *createBasicAAWrapperPass();
285 
286 /// A helper for the legacy pass manager to create a \c BasicAAResult object
287 /// populated to the best of our ability for a particular function when inside
288 /// of a \c ModulePass or a \c CallGraphSCCPass.
289 BasicAAResult createLegacyPMBasicAAResult(Pass &P, Function &F);
290 
291 /// This class is a functor to be used in legacy module or SCC passes for
292 /// computing AA results for a function. We store the results in fields so that
293 /// they live long enough to be queried, but we re-use them each time.
294 class LegacyAARGetter {
295   Pass &P;
296   Optional<BasicAAResult> BAR;
297   Optional<AAResults> AAR;
298 
299 public:
300   LegacyAARGetter(Pass &P) : P(P) {}
301   AAResults &operator()(Function &F) {
302     BAR.emplace(createLegacyPMBasicAAResult(P, F));
303     AAR.emplace(createLegacyPMAAResults(P, F, *BAR));
304     return *AAR;
305   }
306 };
307 
308 } // end namespace llvm
309 
310 #endif // LLVM_ANALYSIS_BASICALIASANALYSIS_H
311