1 //===- BasicAliasAnalysis.h - Stateless, local Alias Analysis ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This is the interface for LLVM's primary stateless and local alias analysis.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_BASICALIASANALYSIS_H
15 #define LLVM_ANALYSIS_BASICALIASANALYSIS_H
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/Analysis/AliasAnalysis.h"
19 #include "llvm/Analysis/AssumptionCache.h"
20 #include "llvm/Analysis/TargetLibraryInfo.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/GetElementPtrTypeIterator.h"
23 #include "llvm/IR/Instruction.h"
24 #include "llvm/IR/LLVMContext.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/Pass.h"
27 #include "llvm/Support/ErrorHandling.h"
31 /// BasicAliasAnalysis - This is the primary alias analysis implementation.
32 struct BasicAliasAnalysis : public ImmutablePass, public AliasAnalysis {
33 static char ID; // Class identification, replacement for typeinfo
36 static const Function *getParent(const Value *V) {
37 if (const Instruction *inst = dyn_cast<Instruction>(V))
38 return inst->getParent()->getParent();
40 if (const Argument *arg = dyn_cast<Argument>(V))
41 return arg->getParent();
46 static bool notDifferentParent(const Value *O1, const Value *O2) {
48 const Function *F1 = getParent(O1);
49 const Function *F2 = getParent(O2);
51 return !F1 || !F2 || F1 == F2;
55 BasicAliasAnalysis() : ImmutablePass(ID) {
56 initializeBasicAliasAnalysisPass(*PassRegistry::getPassRegistry());
59 bool doInitialization(Module &M) override;
61 void getAnalysisUsage(AnalysisUsage &AU) const override {
62 AU.addRequired<AliasAnalysis>();
63 AU.addRequired<AssumptionCacheTracker>();
64 AU.addRequired<TargetLibraryInfoWrapperPass>();
67 AliasResult alias(const MemoryLocation &LocA,
68 const MemoryLocation &LocB) override {
69 assert(AliasCache.empty() && "AliasCache must be cleared after use!");
70 assert(notDifferentParent(LocA.Ptr, LocB.Ptr) &&
71 "BasicAliasAnalysis doesn't support interprocedural queries.");
72 AliasResult Alias = aliasCheck(LocA.Ptr, LocA.Size, LocA.AATags,
73 LocB.Ptr, LocB.Size, LocB.AATags);
74 // AliasCache rarely has more than 1 or 2 elements, always use
75 // shrink_and_clear so it quickly returns to the inline capacity of the
76 // SmallDenseMap if it ever grows larger.
77 // FIXME: This should really be shrink_to_inline_capacity_and_clear().
78 AliasCache.shrink_and_clear();
79 VisitedPhiBBs.clear();
83 ModRefInfo getModRefInfo(ImmutableCallSite CS,
84 const MemoryLocation &Loc) override;
86 ModRefInfo getModRefInfo(ImmutableCallSite CS1,
87 ImmutableCallSite CS2) override;
89 /// pointsToConstantMemory - Chase pointers until we find a (constant
91 bool pointsToConstantMemory(const MemoryLocation &Loc,
92 bool OrLocal) override;
94 /// Get the location associated with a pointer argument of a callsite.
95 ModRefInfo getArgModRefInfo(ImmutableCallSite CS, unsigned ArgIdx) override;
97 /// getModRefBehavior - Return the behavior when calling the given
99 FunctionModRefBehavior getModRefBehavior(ImmutableCallSite CS) override;
101 /// getModRefBehavior - Return the behavior when calling the given function.
102 /// For use when the call site is not known.
103 FunctionModRefBehavior getModRefBehavior(const Function *F) override;
105 /// getAdjustedAnalysisPointer - This method is used when a pass implements
106 /// an analysis interface through multiple inheritance. If needed, it
107 /// should override this to adjust the this pointer as needed for the
108 /// specified pass info.
109 void *getAdjustedAnalysisPointer(const void *ID) override {
110 if (ID == &AliasAnalysis::ID)
111 return (AliasAnalysis*)this;
122 struct VariableGEPIndex {
124 ExtensionKind Extension;
127 bool operator==(const VariableGEPIndex &Other) const {
128 return V == Other.V && Extension == Other.Extension &&
129 Scale == Other.Scale;
132 bool operator!=(const VariableGEPIndex &Other) const {
133 return !operator==(Other);
137 // AliasCache - Track alias queries to guard against recursion.
138 typedef std::pair<MemoryLocation, MemoryLocation> LocPair;
139 typedef SmallDenseMap<LocPair, AliasResult, 8> AliasCacheTy;
140 AliasCacheTy AliasCache;
142 /// \brief Track phi nodes we have visited. When interpret "Value" pointer
143 /// equality as value equality we need to make sure that the "Value" is not
144 /// part of a cycle. Otherwise, two uses could come from different
145 /// "iterations" of a cycle and see different values for the same "Value"
147 /// The following example shows the problem:
148 /// %p = phi(%alloca1, %addr2)
150 /// %addr1 = gep, %alloca2, 0, %l
151 /// %addr2 = gep %alloca2, 0, (%l + 1)
152 /// alias(%p, %addr1) -> MayAlias !
154 SmallPtrSet<const BasicBlock*, 8> VisitedPhiBBs;
156 // Visited - Track instructions visited by pointsToConstantMemory.
157 SmallPtrSet<const Value*, 16> Visited;
159 static Value *GetLinearExpression(Value *V, APInt &Scale, APInt &Offset,
160 ExtensionKind &Extension,
161 const DataLayout &DL, unsigned Depth,
162 AssumptionCache *AC, DominatorTree *DT);
165 DecomposeGEPExpression(const Value *V, int64_t &BaseOffs,
166 SmallVectorImpl<VariableGEPIndex> &VarIndices,
167 bool &MaxLookupReached, const DataLayout &DL,
168 AssumptionCache *AC, DominatorTree *DT);
170 /// \brief Check whether two Values can be considered equivalent.
172 /// In addition to pointer equivalence of \p V1 and \p V2 this checks
173 /// whether they can not be part of a cycle in the value graph by looking at
174 /// all visited phi nodes an making sure that the phis cannot reach the
175 /// value. We have to do this because we are looking through phi nodes (That
176 /// is we say noalias(V, phi(VA, VB)) if noalias(V, VA) and noalias(V, VB).
177 bool isValueEqualInPotentialCycles(const Value *V1, const Value *V2);
179 /// \brief Dest and Src are the variable indices from two decomposed
180 /// GetElementPtr instructions GEP1 and GEP2 which have common base
181 /// pointers. Subtract the GEP2 indices from GEP1 to find the symbolic
182 /// difference between the two pointers.
183 void GetIndexDifference(SmallVectorImpl<VariableGEPIndex> &Dest,
184 const SmallVectorImpl<VariableGEPIndex> &Src);
186 // aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP
187 // instruction against another.
188 AliasResult aliasGEP(const GEPOperator *V1, uint64_t V1Size,
189 const AAMDNodes &V1AAInfo,
190 const Value *V2, uint64_t V2Size,
191 const AAMDNodes &V2AAInfo,
192 const Value *UnderlyingV1, const Value *UnderlyingV2);
194 // aliasPHI - Provide a bunch of ad-hoc rules to disambiguate a PHI
195 // instruction against another.
196 AliasResult aliasPHI(const PHINode *PN, uint64_t PNSize,
197 const AAMDNodes &PNAAInfo,
198 const Value *V2, uint64_t V2Size,
199 const AAMDNodes &V2AAInfo);
201 /// aliasSelect - Disambiguate a Select instruction against another value.
202 AliasResult aliasSelect(const SelectInst *SI, uint64_t SISize,
203 const AAMDNodes &SIAAInfo,
204 const Value *V2, uint64_t V2Size,
205 const AAMDNodes &V2AAInfo);
207 AliasResult aliasCheck(const Value *V1, uint64_t V1Size,
209 const Value *V2, uint64_t V2Size,
214 //===--------------------------------------------------------------------===//
216 // createBasicAliasAnalysisPass - This pass implements the stateless alias
219 ImmutablePass *createBasicAliasAnalysisPass();