xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Analysis/BasicAliasAnalysis.h (revision 682f135f5de39cfc24cfd529ea8a161e94c76c8e)
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/SmallPtrSet.h"
17 #include "llvm/Analysis/AliasAnalysis.h"
18 #include "llvm/IR/PassManager.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Support/Compiler.h"
21 #include <memory>
22 #include <utility>
23 
24 namespace llvm {
25 
26 class AssumptionCache;
27 class DataLayout;
28 class DominatorTree;
29 class Function;
30 class GEPOperator;
31 class PHINode;
32 class SelectInst;
33 class TargetLibraryInfo;
34 class Value;
35 
36 /// This is the AA result object for the basic, local, and stateless alias
37 /// analysis. It implements the AA query interface in an entirely stateless
38 /// manner. As one consequence, it is never invalidated due to IR changes.
39 /// While it does retain some storage, that is used as an optimization and not
40 /// to preserve information from query to query. However it does retain handles
41 /// to various other analyses and must be recomputed when those analyses are.
42 class BasicAAResult : public AAResultBase {
43   const DataLayout &DL;
44   const Function &F;
45   const TargetLibraryInfo &TLI;
46   AssumptionCache &AC;
47   /// Use getDT() instead of accessing this member directly, in order to
48   /// respect the AAQI.UseDominatorTree option.
49   DominatorTree *DT_;
50 
51   DominatorTree *getDT(const AAQueryInfo &AAQI) const {
52     return AAQI.UseDominatorTree ? DT_ : nullptr;
53   }
54 
55 public:
56   BasicAAResult(const DataLayout &DL, const Function &F,
57                 const TargetLibraryInfo &TLI, AssumptionCache &AC,
58                 DominatorTree *DT = nullptr)
59       : DL(DL), F(F), TLI(TLI), AC(AC), DT_(DT) {}
60 
61   BasicAAResult(const BasicAAResult &Arg)
62       : AAResultBase(Arg), DL(Arg.DL), F(Arg.F), TLI(Arg.TLI), AC(Arg.AC),
63         DT_(Arg.DT_) {}
64   BasicAAResult(BasicAAResult &&Arg)
65       : AAResultBase(std::move(Arg)), DL(Arg.DL), F(Arg.F), TLI(Arg.TLI),
66         AC(Arg.AC), DT_(Arg.DT_) {}
67 
68   /// Handle invalidation events in the new pass manager.
69   LLVM_ABI bool invalidate(Function &Fn, const PreservedAnalyses &PA,
70                            FunctionAnalysisManager::Invalidator &Inv);
71 
72   LLVM_ABI AliasResult alias(const MemoryLocation &LocA,
73                              const MemoryLocation &LocB, AAQueryInfo &AAQI,
74                              const Instruction *CtxI);
75 
76   LLVM_ABI ModRefInfo getModRefInfo(const CallBase *Call,
77                                     const MemoryLocation &Loc,
78                                     AAQueryInfo &AAQI);
79 
80   LLVM_ABI ModRefInfo getModRefInfo(const CallBase *Call1,
81                                     const CallBase *Call2, AAQueryInfo &AAQI);
82 
83   /// Returns a bitmask that should be unconditionally applied to the ModRef
84   /// info of a memory location. This allows us to eliminate Mod and/or Ref
85   /// from the ModRef info based on the knowledge that the memory location
86   /// points to constant and/or locally-invariant memory.
87   ///
88   /// If IgnoreLocals is true, then this method returns NoModRef for memory
89   /// that points to a local alloca.
90   LLVM_ABI ModRefInfo getModRefInfoMask(const MemoryLocation &Loc,
91                                         AAQueryInfo &AAQI,
92                                         bool IgnoreLocals = false);
93 
94   /// Get the location associated with a pointer argument of a callsite.
95   LLVM_ABI ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx);
96 
97   /// Returns the behavior when calling the given call site.
98   LLVM_ABI MemoryEffects getMemoryEffects(const CallBase *Call,
99                                           AAQueryInfo &AAQI);
100 
101   /// Returns the behavior when calling the given function. For use when the
102   /// call site is not known.
103   LLVM_ABI MemoryEffects getMemoryEffects(const Function *Fn);
104 
105 private:
106   struct DecomposedGEP;
107 
108   /// Tracks instructions visited by pointsToConstantMemory.
109   SmallPtrSet<const Value *, 16> Visited;
110 
111   static DecomposedGEP
112   DecomposeGEPExpression(const Value *V, const DataLayout &DL,
113                          AssumptionCache *AC, DominatorTree *DT);
114 
115   /// A Heuristic for aliasGEP that searches for a constant offset
116   /// between the variables.
117   ///
118   /// GetLinearExpression has some limitations, as generally zext(%x + 1)
119   /// != zext(%x) + zext(1) if the arithmetic overflows. GetLinearExpression
120   /// will therefore conservatively refuse to decompose these expressions.
121   /// However, we know that, for all %x, zext(%x) != zext(%x + 1), even if
122   /// the addition overflows.
123   bool constantOffsetHeuristic(const DecomposedGEP &GEP, LocationSize V1Size,
124                                LocationSize V2Size, AssumptionCache *AC,
125                                DominatorTree *DT, const AAQueryInfo &AAQI);
126 
127   bool isValueEqualInPotentialCycles(const Value *V1, const Value *V2,
128                                      const AAQueryInfo &AAQI);
129 
130   void subtractDecomposedGEPs(DecomposedGEP &DestGEP,
131                               const DecomposedGEP &SrcGEP,
132                               const AAQueryInfo &AAQI);
133 
134   AliasResult aliasGEP(const GEPOperator *V1, LocationSize V1Size,
135                        const Value *V2, LocationSize V2Size,
136                        const Value *UnderlyingV1, const Value *UnderlyingV2,
137                        AAQueryInfo &AAQI);
138 
139   AliasResult aliasPHI(const PHINode *PN, LocationSize PNSize,
140                        const Value *V2, LocationSize V2Size, AAQueryInfo &AAQI);
141 
142   AliasResult aliasSelect(const SelectInst *SI, LocationSize SISize,
143                           const Value *V2, LocationSize V2Size,
144                           AAQueryInfo &AAQI);
145 
146   AliasResult aliasCheck(const Value *V1, LocationSize V1Size, const Value *V2,
147                          LocationSize V2Size, AAQueryInfo &AAQI,
148                          const Instruction *CtxI);
149 
150   AliasResult aliasCheckRecursive(const Value *V1, LocationSize V1Size,
151                                   const Value *V2, LocationSize V2Size,
152                                   AAQueryInfo &AAQI, const Value *O1,
153                                   const Value *O2);
154 };
155 
156 /// Analysis pass providing a never-invalidated alias analysis result.
157 class BasicAA : public AnalysisInfoMixin<BasicAA> {
158   friend AnalysisInfoMixin<BasicAA>;
159 
160   LLVM_ABI static AnalysisKey Key;
161 
162 public:
163   using Result = BasicAAResult;
164 
165   LLVM_ABI BasicAAResult run(Function &F, FunctionAnalysisManager &AM);
166 };
167 
168 /// Legacy wrapper pass to provide the BasicAAResult object.
169 class LLVM_ABI BasicAAWrapperPass : public FunctionPass {
170   std::unique_ptr<BasicAAResult> Result;
171 
172   virtual void anchor();
173 
174 public:
175   static char ID;
176 
177   BasicAAWrapperPass();
178 
179   BasicAAResult &getResult() { return *Result; }
180   const BasicAAResult &getResult() const { return *Result; }
181 
182   bool runOnFunction(Function &F) override;
183   void getAnalysisUsage(AnalysisUsage &AU) const override;
184 };
185 
186 LLVM_ABI FunctionPass *createBasicAAWrapperPass();
187 
188 } // end namespace llvm
189 
190 #endif // LLVM_ANALYSIS_BASICALIASANALYSIS_H
191