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