llvm.org GIT mirror llvm / 734c778
[PM/AA] Hoist the interface for BasicAA into a header file. This is the first mechanical step in preparation for making this and all the other alias analysis passes available to the new pass manager. I'm factoring out all the totally boring changes I can so I'm moving code around here with no other changes. I've even minimized the formatting churn. I'll reformat and freshen comments on the interface now that its located in the right place so that the substantive changes don't triger this. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@244197 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 4 years ago
12 changed file(s) with 242 addition(s) and 199 deletion(s). Raw diff Collapse all Expand all
0 #include "llvm/Analysis/BasicAliasAnalysis.h"
1 #include "llvm/Analysis/Passes.h"
12 #include "llvm/ExecutionEngine/ExecutionEngine.h"
23 #include "llvm/ExecutionEngine/MCJIT.h"
0 #include "llvm/ADT/STLExtras.h"
1 #include "llvm/Analysis/BasicAliasAnalysis.h"
12 #include "llvm/Analysis/Passes.h"
23 #include "llvm/ExecutionEngine/ExecutionEngine.h"
34 #include "llvm/ExecutionEngine/MCJIT.h"
0 #include "llvm/ADT/STLExtras.h"
1 #include "llvm/Analysis/BasicAliasAnalysis.h"
12 #include "llvm/Analysis/Passes.h"
23 #include "llvm/ExecutionEngine/ExecutionEngine.h"
34 #include "llvm/ExecutionEngine/MCJIT.h"
0 #include "llvm/ADT/STLExtras.h"
1 #include "llvm/Analysis/BasicAliasAnalysis.h"
12 #include "llvm/Analysis/Passes.h"
23 #include "llvm/ExecutionEngine/ExecutionEngine.h"
34 #include "llvm/ExecutionEngine/MCJIT.h"
0 //===- BasicAliasAnalysis.h - Stateless, local Alias Analysis ---*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
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/Analysis/AssumptionCache.h"
19 #include "llvm/Analysis/TargetLibraryInfo.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/GetElementPtrTypeIterator.h"
22 #include "llvm/IR/Instruction.h"
23 #include "llvm/IR/LLVMContext.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/Pass.h"
26 #include "llvm/Support/ErrorHandling.h"
27
28 namespace llvm {
29
30 /// BasicAliasAnalysis - This is the primary alias analysis implementation.
31 struct BasicAliasAnalysis : public ImmutablePass, public AliasAnalysis {
32 static char ID; // Class identification, replacement for typeinfo
33
34 #ifndef NDEBUG
35 static const Function *getParent(const Value *V) {
36 if (const Instruction *inst = dyn_cast(V))
37 return inst->getParent()->getParent();
38
39 if (const Argument *arg = dyn_cast(V))
40 return arg->getParent();
41
42 return nullptr;
43 }
44
45 static bool notDifferentParent(const Value *O1, const Value *O2) {
46
47 const Function *F1 = getParent(O1);
48 const Function *F2 = getParent(O2);
49
50 return !F1 || !F2 || F1 == F2;
51 }
52 #endif
53
54 BasicAliasAnalysis() : ImmutablePass(ID) {
55 initializeBasicAliasAnalysisPass(*PassRegistry::getPassRegistry());
56 }
57
58 bool doInitialization(Module &M) override;
59
60 void getAnalysisUsage(AnalysisUsage &AU) const override {
61 AU.addRequired();
62 AU.addRequired();
63 AU.addRequired();
64 }
65
66 AliasResult alias(const MemoryLocation &LocA,
67 const MemoryLocation &LocB) override {
68 assert(AliasCache.empty() && "AliasCache must be cleared after use!");
69 assert(notDifferentParent(LocA.Ptr, LocB.Ptr) &&
70 "BasicAliasAnalysis doesn't support interprocedural queries.");
71 AliasResult Alias = aliasCheck(LocA.Ptr, LocA.Size, LocA.AATags,
72 LocB.Ptr, LocB.Size, LocB.AATags);
73 // AliasCache rarely has more than 1 or 2 elements, always use
74 // shrink_and_clear so it quickly returns to the inline capacity of the
75 // SmallDenseMap if it ever grows larger.
76 // FIXME: This should really be shrink_to_inline_capacity_and_clear().
77 AliasCache.shrink_and_clear();
78 VisitedPhiBBs.clear();
79 return Alias;
80 }
81
82 ModRefInfo getModRefInfo(ImmutableCallSite CS,
83 const MemoryLocation &Loc) override;
84
85 ModRefInfo getModRefInfo(ImmutableCallSite CS1,
86 ImmutableCallSite CS2) override;
87
88 /// pointsToConstantMemory - Chase pointers until we find a (constant
89 /// global) or not.
90 bool pointsToConstantMemory(const MemoryLocation &Loc,
91 bool OrLocal) override;
92
93 /// Get the location associated with a pointer argument of a callsite.
94 ModRefInfo getArgModRefInfo(ImmutableCallSite CS, unsigned ArgIdx) override;
95
96 /// getModRefBehavior - Return the behavior when calling the given
97 /// call site.
98 FunctionModRefBehavior getModRefBehavior(ImmutableCallSite CS) override;
99
100 /// getModRefBehavior - Return the behavior when calling the given function.
101 /// For use when the call site is not known.
102 FunctionModRefBehavior getModRefBehavior(const Function *F) override;
103
104 /// getAdjustedAnalysisPointer - This method is used when a pass implements
105 /// an analysis interface through multiple inheritance. If needed, it
106 /// should override this to adjust the this pointer as needed for the
107 /// specified pass info.
108 void *getAdjustedAnalysisPointer(const void *ID) override {
109 if (ID == &AliasAnalysis::ID)
110 return (AliasAnalysis*)this;
111 return this;
112 }
113
114 private:
115 enum ExtensionKind {
116 EK_NotExtended,
117 EK_SignExt,
118 EK_ZeroExt
119 };
120
121 struct VariableGEPIndex {
122 const Value *V;
123 ExtensionKind Extension;
124 int64_t Scale;
125
126 bool operator==(const VariableGEPIndex &Other) const {
127 return V == Other.V && Extension == Other.Extension &&
128 Scale == Other.Scale;
129 }
130
131 bool operator!=(const VariableGEPIndex &Other) const {
132 return !operator==(Other);
133 }
134 };
135
136 // AliasCache - Track alias queries to guard against recursion.
137 typedef std::pair LocPair;
138 typedef SmallDenseMap AliasCacheTy;
139 AliasCacheTy AliasCache;
140
141 /// \brief Track phi nodes we have visited. When interpret "Value" pointer
142 /// equality as value equality we need to make sure that the "Value" is not
143 /// part of a cycle. Otherwise, two uses could come from different
144 /// "iterations" of a cycle and see different values for the same "Value"
145 /// pointer.
146 /// The following example shows the problem:
147 /// %p = phi(%alloca1, %addr2)
148 /// %l = load %ptr
149 /// %addr1 = gep, %alloca2, 0, %l
150 /// %addr2 = gep %alloca2, 0, (%l + 1)
151 /// alias(%p, %addr1) -> MayAlias !
152 /// store %l, ...
153 SmallPtrSet VisitedPhiBBs;
154
155 // Visited - Track instructions visited by pointsToConstantMemory.
156 SmallPtrSet Visited;
157
158 static Value *GetLinearExpression(Value *V, APInt &Scale, APInt &Offset,
159 ExtensionKind &Extension,
160 const DataLayout &DL, unsigned Depth,
161 AssumptionCache *AC, DominatorTree *DT);
162
163 static const Value *
164 DecomposeGEPExpression(const Value *V, int64_t &BaseOffs,
165 SmallVectorImpl &VarIndices,
166 bool &MaxLookupReached, const DataLayout &DL,
167 AssumptionCache *AC, DominatorTree *DT);
168
169 /// \brief Check whether two Values can be considered equivalent.
170 ///
171 /// In addition to pointer equivalence of \p V1 and \p V2 this checks
172 /// whether they can not be part of a cycle in the value graph by looking at
173 /// all visited phi nodes an making sure that the phis cannot reach the
174 /// value. We have to do this because we are looking through phi nodes (That
175 /// is we say noalias(V, phi(VA, VB)) if noalias(V, VA) and noalias(V, VB).
176 bool isValueEqualInPotentialCycles(const Value *V1, const Value *V2);
177
178 /// \brief Dest and Src are the variable indices from two decomposed
179 /// GetElementPtr instructions GEP1 and GEP2 which have common base
180 /// pointers. Subtract the GEP2 indices from GEP1 to find the symbolic
181 /// difference between the two pointers.
182 void GetIndexDifference(SmallVectorImpl &Dest,
183 const SmallVectorImpl &Src);
184
185 // aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP
186 // instruction against another.
187 AliasResult aliasGEP(const GEPOperator *V1, uint64_t V1Size,
188 const AAMDNodes &V1AAInfo,
189 const Value *V2, uint64_t V2Size,
190 const AAMDNodes &V2AAInfo,
191 const Value *UnderlyingV1, const Value *UnderlyingV2);
192
193 // aliasPHI - Provide a bunch of ad-hoc rules to disambiguate a PHI
194 // instruction against another.
195 AliasResult aliasPHI(const PHINode *PN, uint64_t PNSize,
196 const AAMDNodes &PNAAInfo,
197 const Value *V2, uint64_t V2Size,
198 const AAMDNodes &V2AAInfo);
199
200 /// aliasSelect - Disambiguate a Select instruction against another value.
201 AliasResult aliasSelect(const SelectInst *SI, uint64_t SISize,
202 const AAMDNodes &SIAAInfo,
203 const Value *V2, uint64_t V2Size,
204 const AAMDNodes &V2AAInfo);
205
206 AliasResult aliasCheck(const Value *V1, uint64_t V1Size,
207 AAMDNodes V1AATag,
208 const Value *V2, uint64_t V2Size,
209 AAMDNodes V2AATag);
210 };
211
212
213 //===--------------------------------------------------------------------===//
214 //
215 // createBasicAliasAnalysisPass - This pass implements the stateless alias
216 // analysis.
217 //
218 ImmutablePass *createBasicAliasAnalysisPass();
219
220 }
221
222 #endif
5555 // createNoAAPass - This pass implements a "I don't know" alias analysis.
5656 //
5757 ImmutablePass *createNoAAPass();
58
59 //===--------------------------------------------------------------------===//
60 //
61 // createBasicAliasAnalysisPass - This pass implements the stateless alias
62 // analysis.
63 //
64 ImmutablePass *createBasicAliasAnalysisPass();
6558
6659 //===--------------------------------------------------------------------===//
6760 //
1616
1717 #include "llvm/ADT/Statistic.h"
1818 #include "llvm/Analysis/AliasSetTracker.h"
19 #include "llvm/Analysis/BasicAliasAnalysis.h"
1920 #include "llvm/Analysis/CallPrinter.h"
2021 #include "llvm/Analysis/DomPrinter.h"
2122 #include "llvm/Analysis/IntervalPartition.h"
1212 //
1313 //===----------------------------------------------------------------------===//
1414
15 #include "llvm/Analysis/Passes.h"
16 #include "llvm/ADT/SmallPtrSet.h"
15 #include "llvm/Analysis/BasicAliasAnalysis.h"
1716 #include "llvm/ADT/SmallVector.h"
1817 #include "llvm/ADT/Statistic.h"
1918 #include "llvm/Analysis/AliasAnalysis.h"
20 #include "llvm/Analysis/AssumptionCache.h"
2119 #include "llvm/Analysis/CFG.h"
2220 #include "llvm/Analysis/CaptureTracking.h"
2321 #include "llvm/Analysis/InstructionSimplify.h"
2422 #include "llvm/Analysis/LoopInfo.h"
2523 #include "llvm/Analysis/MemoryBuiltins.h"
26 #include "llvm/Analysis/TargetLibraryInfo.h"
2724 #include "llvm/Analysis/ValueTracking.h"
2825 #include "llvm/IR/Constants.h"
2926 #include "llvm/IR/DataLayout.h"
3027 #include "llvm/IR/DerivedTypes.h"
3128 #include "llvm/IR/Dominators.h"
32 #include "llvm/IR/Function.h"
33 #include "llvm/IR/GetElementPtrTypeIterator.h"
3429 #include "llvm/IR/GlobalAlias.h"
3530 #include "llvm/IR/GlobalVariable.h"
3631 #include "llvm/IR/Instructions.h"
173168 // GetElementPtr Instruction Decomposition and Analysis
174169 //===----------------------------------------------------------------------===//
175170
176 namespace {
177 enum ExtensionKind {
178 EK_NotExtended,
179 EK_SignExt,
180 EK_ZeroExt
181 };
182
183 struct VariableGEPIndex {
184 const Value *V;
185 ExtensionKind Extension;
186 int64_t Scale;
187
188 bool operator==(const VariableGEPIndex &Other) const {
189 return V == Other.V && Extension == Other.Extension &&
190 Scale == Other.Scale;
191 }
192
193 bool operator!=(const VariableGEPIndex &Other) const {
194 return !operator==(Other);
195 }
196 };
197 }
198
199
200171 /// GetLinearExpression - Analyze the specified value as a linear expression:
201172 /// "A*V + B", where A and B are constant integers. Return the scale and offset
202173 /// values as APInts and return V as a Value*, and return whether we looked
205176 ///
206177 /// Note that this looks through extends, so the high bits may not be
207178 /// represented in the result.
208 static Value *GetLinearExpression(Value *V, APInt &Scale, APInt &Offset,
209 ExtensionKind &Extension,
210 const DataLayout &DL, unsigned Depth,
211 AssumptionCache *AC, DominatorTree *DT) {
179 /*static*/ Value *BasicAliasAnalysis::GetLinearExpression(
180 Value *V, APInt &Scale, APInt &Offset, ExtensionKind &Extension,
181 const DataLayout &DL, unsigned Depth, AssumptionCache *AC,
182 DominatorTree *DT) {
212183 assert(V->getType()->isIntegerTy() && "Not an integer value");
213184
214185 // Limit our recursion depth.
301272 /// depth (MaxLookupSearchDepth).
302273 /// When DataLayout not is around, it just looks through pointer casts.
303274 ///
304 static const Value *
305 DecomposeGEPExpression(const Value *V, int64_t &BaseOffs,
306 SmallVectorImpl &VarIndices,
307 bool &MaxLookupReached, const DataLayout &DL,
308 AssumptionCache *AC, DominatorTree *DT) {
275 /*static*/ const Value *BasicAliasAnalysis::DecomposeGEPExpression(
276 const Value *V, int64_t &BaseOffs,
277 SmallVectorImpl &VarIndices, bool &MaxLookupReached,
278 const DataLayout &DL, AssumptionCache *AC, DominatorTree *DT) {
309279 // Limit recursion depth to limit compile time in crazy cases.
310280 unsigned MaxLookup = MaxLookupSearchDepth;
311281 MaxLookupReached = false;
437407 // BasicAliasAnalysis Pass
438408 //===----------------------------------------------------------------------===//
439409
440 #ifndef NDEBUG
441 static const Function *getParent(const Value *V) {
442 if (const Instruction *inst = dyn_cast(V))
443 return inst->getParent()->getParent();
444
445 if (const Argument *arg = dyn_cast(V))
446 return arg->getParent();
447
448 return nullptr;
449 }
450
451 static bool notDifferentParent(const Value *O1, const Value *O2) {
452
453 const Function *F1 = getParent(O1);
454 const Function *F2 = getParent(O2);
455
456 return !F1 || !F2 || F1 == F2;
457 }
458 #endif
459
460 namespace {
461 /// BasicAliasAnalysis - This is the primary alias analysis implementation.
462 struct BasicAliasAnalysis : public ImmutablePass, public AliasAnalysis {
463 static char ID; // Class identification, replacement for typeinfo
464 BasicAliasAnalysis() : ImmutablePass(ID) {
465 initializeBasicAliasAnalysisPass(*PassRegistry::getPassRegistry());
466 }
467
468 bool doInitialization(Module &M) override;
469
470 void getAnalysisUsage(AnalysisUsage &AU) const override {
471 AU.addRequired();
472 AU.addRequired();
473 AU.addRequired();
474 }
475
476 AliasResult alias(const MemoryLocation &LocA,
477 const MemoryLocation &LocB) override {
478 assert(AliasCache.empty() && "AliasCache must be cleared after use!");
479 assert(notDifferentParent(LocA.Ptr, LocB.Ptr) &&
480 "BasicAliasAnalysis doesn't support interprocedural queries.");
481 AliasResult Alias = aliasCheck(LocA.Ptr, LocA.Size, LocA.AATags,
482 LocB.Ptr, LocB.Size, LocB.AATags);
483 // AliasCache rarely has more than 1 or 2 elements, always use
484 // shrink_and_clear so it quickly returns to the inline capacity of the
485 // SmallDenseMap if it ever grows larger.
486 // FIXME: This should really be shrink_to_inline_capacity_and_clear().
487 AliasCache.shrink_and_clear();
488 VisitedPhiBBs.clear();
489 return Alias;
490 }
491
492 ModRefInfo getModRefInfo(ImmutableCallSite CS,
493 const MemoryLocation &Loc) override;
494
495 ModRefInfo getModRefInfo(ImmutableCallSite CS1,
496 ImmutableCallSite CS2) override;
497
498 /// pointsToConstantMemory - Chase pointers until we find a (constant
499 /// global) or not.
500 bool pointsToConstantMemory(const MemoryLocation &Loc,
501 bool OrLocal) override;
502
503 /// Get the location associated with a pointer argument of a callsite.
504 ModRefInfo getArgModRefInfo(ImmutableCallSite CS, unsigned ArgIdx) override;
505
506 /// getModRefBehavior - Return the behavior when calling the given
507 /// call site.
508 FunctionModRefBehavior getModRefBehavior(ImmutableCallSite CS) override;
509
510 /// getModRefBehavior - Return the behavior when calling the given function.
511 /// For use when the call site is not known.
512 FunctionModRefBehavior getModRefBehavior(const Function *F) override;
513
514 /// getAdjustedAnalysisPointer - This method is used when a pass implements
515 /// an analysis interface through multiple inheritance. If needed, it
516 /// should override this to adjust the this pointer as needed for the
517 /// specified pass info.
518 void *getAdjustedAnalysisPointer(const void *ID) override {
519 if (ID == &AliasAnalysis::ID)
520 return (AliasAnalysis*)this;
521 return this;
522 }
523
524 private:
525 // AliasCache - Track alias queries to guard against recursion.
526 typedef std::pair LocPair;
527 typedef SmallDenseMap AliasCacheTy;
528 AliasCacheTy AliasCache;
529
530 /// \brief Track phi nodes we have visited. When interpret "Value" pointer
531 /// equality as value equality we need to make sure that the "Value" is not
532 /// part of a cycle. Otherwise, two uses could come from different
533 /// "iterations" of a cycle and see different values for the same "Value"
534 /// pointer.
535 /// The following example shows the problem:
536 /// %p = phi(%alloca1, %addr2)
537 /// %l = load %ptr
538 /// %addr1 = gep, %alloca2, 0, %l
539 /// %addr2 = gep %alloca2, 0, (%l + 1)
540 /// alias(%p, %addr1) -> MayAlias !
541 /// store %l, ...
542 SmallPtrSet VisitedPhiBBs;
543
544 // Visited - Track instructions visited by pointsToConstantMemory.
545 SmallPtrSet Visited;
546
547 /// \brief Check whether two Values can be considered equivalent.
548 ///
549 /// In addition to pointer equivalence of \p V1 and \p V2 this checks
550 /// whether they can not be part of a cycle in the value graph by looking at
551 /// all visited phi nodes an making sure that the phis cannot reach the
552 /// value. We have to do this because we are looking through phi nodes (That
553 /// is we say noalias(V, phi(VA, VB)) if noalias(V, VA) and noalias(V, VB).
554 bool isValueEqualInPotentialCycles(const Value *V1, const Value *V2);
555
556 /// \brief Dest and Src are the variable indices from two decomposed
557 /// GetElementPtr instructions GEP1 and GEP2 which have common base
558 /// pointers. Subtract the GEP2 indices from GEP1 to find the symbolic
559 /// difference between the two pointers.
560 void GetIndexDifference(SmallVectorImpl &Dest,
561 const SmallVectorImpl &Src);
562
563 // aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP
564 // instruction against another.
565 AliasResult aliasGEP(const GEPOperator *V1, uint64_t V1Size,
566 const AAMDNodes &V1AAInfo,
567 const Value *V2, uint64_t V2Size,
568 const AAMDNodes &V2AAInfo,
569 const Value *UnderlyingV1, const Value *UnderlyingV2);
570
571 // aliasPHI - Provide a bunch of ad-hoc rules to disambiguate a PHI
572 // instruction against another.
573 AliasResult aliasPHI(const PHINode *PN, uint64_t PNSize,
574 const AAMDNodes &PNAAInfo,
575 const Value *V2, uint64_t V2Size,
576 const AAMDNodes &V2AAInfo);
577
578 /// aliasSelect - Disambiguate a Select instruction against another value.
579 AliasResult aliasSelect(const SelectInst *SI, uint64_t SISize,
580 const AAMDNodes &SIAAInfo,
581 const Value *V2, uint64_t V2Size,
582 const AAMDNodes &V2AAInfo);
583
584 AliasResult aliasCheck(const Value *V1, uint64_t V1Size,
585 AAMDNodes V1AATag,
586 const Value *V2, uint64_t V2Size,
587 AAMDNodes V2AATag);
588 };
589 } // End of anonymous namespace
590
591 // Register this pass...
410 // Register the pass...
592411 char BasicAliasAnalysis::ID = 0;
593412 INITIALIZE_AG_PASS_BEGIN(BasicAliasAnalysis, AliasAnalysis, "basicaa",
594413 "Basic Alias Analysis (stateless AA impl)",
598417 INITIALIZE_AG_PASS_END(BasicAliasAnalysis, AliasAnalysis, "basicaa",
599418 "Basic Alias Analysis (stateless AA impl)",
600419 false, true, false)
601
602420
603421 ImmutablePass *llvm::createBasicAliasAnalysisPass() {
604422 return new BasicAliasAnalysis();
1212 //===---------------------------------------------------------------------===//
1313
1414 #include "llvm/CodeGen/Passes.h"
15 #include "llvm/Analysis/BasicAliasAnalysis.h"
1516 #include "llvm/Analysis/Passes.h"
1617 #include "llvm/CodeGen/MachineFunctionPass.h"
1718 #include "llvm/CodeGen/RegAllocRegistry.h"
2121 #include "llvm/IR/LegacyPassManager.h"
2222 #include "llvm/Support/CommandLine.h"
2323 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Analysis/BasicAliasAnalysis.h"
2425 #include "llvm/Analysis/TargetLibraryInfo.h"
2526 #include "llvm/Target/TargetMachine.h"
2627 #include "llvm/Transforms/IPO.h"
1515 #include "llvm/Transforms/Scalar.h"
1616 #include "llvm-c/Initialization.h"
1717 #include "llvm-c/Transforms/Scalar.h"
18 #include "llvm/Analysis/BasicAliasAnalysis.h"
1819 #include "llvm/Analysis/Passes.h"
1920 #include "llvm/IR/DataLayout.h"
2021 #include "llvm/IR/Verifier.h"
77 //===----------------------------------------------------------------------===//
88
99 #include "llvm/Analysis/AliasAnalysis.h"
10 #include "llvm/Analysis/BasicAliasAnalysis.h"
1011 #include "llvm/Analysis/Passes.h"
1112 #include "llvm/IR/Constants.h"
1213 #include "llvm/IR/Instructions.h"