llvm.org GIT mirror llvm / df2b939
[BasicAA] Use PhiValuesAnalysis if available when handling phi alias By using PhiValuesAnalysis we can get all the values reachable from a phi, so we can be more precise instead of giving up when a phi has phi operands. We can't make BaseicAA directly use PhiValuesAnalysis though, as the user of BasicAA may modify the function in ways that PhiValuesAnalysis can't cope with. For this optional usage to work correctly BasicAAWrapperPass now needs to be not marked as CFG-only (i.e. it is now invalidated even when CFG is preserved) due to how the legacy pass manager handles dependent passes being invalidated, namely the depending pass still has a pointer to the now-dead dependent pass. Differential Revision: https://reviews.llvm.org/D44564 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@338242 91177308-0d34-0410-b5e6-96231b3b80d8 John Brawn 1 year, 1 month ago
9 changed file(s) with 185 addition(s) and 34 deletion(s). Raw diff Collapse all Expand all
4242 class PHINode;
4343 class SelectInst;
4444 class TargetLibraryInfo;
45 class PhiValues;
4546 class Value;
4647
4748 /// This is the AA result object for the basic, local, and stateless alias
5960 AssumptionCache ∾
6061 DominatorTree *DT;
6162 LoopInfo *LI;
63 PhiValues *PV;
6264
6365 public:
6466 BasicAAResult(const DataLayout &DL, const Function &F,
6567 const TargetLibraryInfo &TLI, AssumptionCache &AC,
66 DominatorTree *DT = nullptr, LoopInfo *LI = nullptr)
67 : AAResultBase(), DL(DL), F(F), TLI(TLI), AC(AC), DT(DT), LI(LI) {}
68 DominatorTree *DT = nullptr, LoopInfo *LI = nullptr,
69 PhiValues *PV = nullptr)
70 : AAResultBase(), DL(DL), F(F), TLI(TLI), AC(AC), DT(DT), LI(LI), PV(PV)
71 {}
6872
6973 BasicAAResult(const BasicAAResult &Arg)
7074 : AAResultBase(Arg), DL(Arg.DL), F(Arg.F), TLI(Arg.TLI), AC(Arg.AC),
71 DT(Arg.DT), LI(Arg.LI) {}
75 DT(Arg.DT), LI(Arg.LI), PV(Arg.PV) {}
7276 BasicAAResult(BasicAAResult &&Arg)
7377 : AAResultBase(std::move(Arg)), DL(Arg.DL), F(Arg.F), TLI(Arg.TLI),
74 AC(Arg.AC), DT(Arg.DT), LI(Arg.LI) {}
78 AC(Arg.AC), DT(Arg.DT), LI(Arg.LI), PV(Arg.PV) {}
7579
7680 /// Handle invalidation events in the new pass manager.
7781 bool invalidate(Function &Fn, const PreservedAnalyses &PA,
2727 #include "llvm/Analysis/MemoryLocation.h"
2828 #include "llvm/Analysis/TargetLibraryInfo.h"
2929 #include "llvm/Analysis/ValueTracking.h"
30 #include "llvm/Analysis/PhiValues.h"
3031 #include "llvm/IR/Argument.h"
3132 #include "llvm/IR/Attributes.h"
3233 #include "llvm/IR/CallSite.h"
9293 // depend on them.
9394 if (Inv.invalidate(Fn, PA) ||
9495 (DT && Inv.invalidate(Fn, PA)) ||
95 (LI && Inv.invalidate(Fn, PA)))
96 (LI && Inv.invalidate(Fn, PA)) ||
97 (PV && Inv.invalidate(Fn, PA)))
9698 return true;
9799
98100 // Otherwise this analysis result remains valid.
15261528 return Alias;
15271529 }
15281530
1529 SmallPtrSet UniqueSrc;
15301531 SmallVector V1Srcs;
15311532 bool isRecursive = false;
1532 for (Value *PV1 : PN->incoming_values()) {
1533 if (isa(PV1))
1534 // If any of the source itself is a PHI, return MayAlias conservatively
1535 // to avoid compile time explosion. The worst possible case is if both
1536 // sides are PHI nodes. In which case, this is O(m x n) time where 'm'
1537 // and 'n' are the number of PHI sources.
1533 if (PV) {
1534 // If we have PhiValues then use it to get the underlying phi values.
1535 const PhiValues::ValueSet &PhiValueSet = PV->getValuesForPhi(PN);
1536 // If we have more phi values than the search depth then return MayAlias
1537 // conservatively to avoid compile time explosion. The worst possible case
1538 // is if both sides are PHI nodes. In which case, this is O(m x n) time
1539 // where 'm' and 'n' are the number of PHI sources.
1540 if (PhiValueSet.size() > MaxLookupSearchDepth)
15381541 return MayAlias;
1539
1540 if (EnableRecPhiAnalysis)
1541 if (GEPOperator *PV1GEP = dyn_cast(PV1)) {
1542 // Check whether the incoming value is a GEP that advances the pointer
1543 // result of this PHI node (e.g. in a loop). If this is the case, we
1544 // would recurse and always get a MayAlias. Handle this case specially
1545 // below.
1546 if (PV1GEP->getPointerOperand() == PN && PV1GEP->getNumIndices() == 1 &&
1547 isa(PV1GEP->idx_begin())) {
1548 isRecursive = true;
1549 continue;
1542 // Add the values to V1Srcs
1543 for (Value *PV1 : PhiValueSet) {
1544 if (EnableRecPhiAnalysis) {
1545 if (GEPOperator *PV1GEP = dyn_cast(PV1)) {
1546 // Check whether the incoming value is a GEP that advances the pointer
1547 // result of this PHI node (e.g. in a loop). If this is the case, we
1548 // would recurse and always get a MayAlias. Handle this case specially
1549 // below.
1550 if (PV1GEP->getPointerOperand() == PN && PV1GEP->getNumIndices() == 1 &&
1551 isa(PV1GEP->idx_begin())) {
1552 isRecursive = true;
1553 continue;
1554 }
15501555 }
15511556 }
1552
1553 if (UniqueSrc.insert(PV1).second)
15541557 V1Srcs.push_back(PV1);
1555 }
1558 }
1559 } else {
1560 // If we don't have PhiInfo then just look at the operands of the phi itself
1561 // FIXME: Remove this once we can guarantee that we have PhiInfo always
1562 SmallPtrSet UniqueSrc;
1563 for (Value *PV1 : PN->incoming_values()) {
1564 if (isa(PV1))
1565 // If any of the source itself is a PHI, return MayAlias conservatively
1566 // to avoid compile time explosion. The worst possible case is if both
1567 // sides are PHI nodes. In which case, this is O(m x n) time where 'm'
1568 // and 'n' are the number of PHI sources.
1569 return MayAlias;
1570
1571 if (EnableRecPhiAnalysis)
1572 if (GEPOperator *PV1GEP = dyn_cast(PV1)) {
1573 // Check whether the incoming value is a GEP that advances the pointer
1574 // result of this PHI node (e.g. in a loop). If this is the case, we
1575 // would recurse and always get a MayAlias. Handle this case specially
1576 // below.
1577 if (PV1GEP->getPointerOperand() == PN && PV1GEP->getNumIndices() == 1 &&
1578 isa(PV1GEP->idx_begin())) {
1579 isRecursive = true;
1580 continue;
1581 }
1582 }
1583
1584 if (UniqueSrc.insert(PV1).second)
1585 V1Srcs.push_back(PV1);
1586 }
1587 }
1588
1589 // If V1Srcs is empty then that means that the phi has no underlying non-phi
1590 // value. This should only be possible in blocks unreachable from the entry
1591 // block, but return MayAlias just in case.
1592 if (V1Srcs.empty())
1593 return MayAlias;
15561594
15571595 // If this PHI node is recursive, set the size of the accessed memory to
15581596 // unknown to represent all the possible values the GEP could advance the
18781916 AM.getResult(F),
18791917 AM.getResult(F),
18801918 &AM.getResult(F),
1881 AM.getCachedResult(F));
1919 AM.getCachedResult(F),
1920 AM.getCachedResult(F));
18821921 }
18831922
18841923 BasicAAWrapperPass::BasicAAWrapperPass() : FunctionPass(ID) {
18901929 void BasicAAWrapperPass::anchor() {}
18911930
18921931 INITIALIZE_PASS_BEGIN(BasicAAWrapperPass, "basicaa",
1893 "Basic Alias Analysis (stateless AA impl)", true, true)
1932 "Basic Alias Analysis (stateless AA impl)", false, true)
18941933 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
18951934 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
18961935 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
18971936 INITIALIZE_PASS_END(BasicAAWrapperPass, "basicaa",
1898 "Basic Alias Analysis (stateless AA impl)", true, true)
1937 "Basic Alias Analysis (stateless AA impl)", false, true)
18991938
19001939 FunctionPass *llvm::createBasicAAWrapperPass() {
19011940 return new BasicAAWrapperPass();
19061945 auto &TLIWP = getAnalysis();
19071946 auto &DTWP = getAnalysis();
19081947 auto *LIWP = getAnalysisIfAvailable();
1948 auto *PVWP = getAnalysisIfAvailable();
19091949
19101950 Result.reset(new BasicAAResult(F.getParent()->getDataLayout(), F, TLIWP.getTLI(),
19111951 ACT.getAssumptionCache(F), &DTWP.getDomTree(),
1912 LIWP ? &LIWP->getLoopInfo() : nullptr));
1952 LIWP ? &LIWP->getLoopInfo() : nullptr,
1953 PVWP ? &PVWP->getResult() : nullptr));
19131954
19141955 return false;
19151956 }
19191960 AU.addRequired();
19201961 AU.addRequired();
19211962 AU.addRequired();
1963 AU.addUsedIfAvailable();
19221964 }
19231965
19241966 BasicAAResult llvm::createLegacyPMBasicAAResult(Pass &P, Function &F) {
2323 ; CHECK-LI-INVALIDATE: Invalidating analysis: BasicAA
2424 ; CHECK-LI-INVALIDATE: Running pass: AAEvaluator
2525 ; CHECK-LI-INVALIDATE: Running analysis: BasicAA
26 ;
27 ; Check PhiValues specifically.
28 ; RUN: opt -disable-output -disable-verify -debug-pass-manager %s 2>&1 \
29 ; RUN: -passes='require,require,invalidate,aa-eval' -aa-pipeline='basic-aa' \
30 ; RUN: | FileCheck %s --check-prefix=CHECK-PV-INVALIDATE
31 ; CHECK-PV-INVALIDATE: Running pass: RequireAnalysisPass
32 ; CHECK-PV-INVALIDATE: Running analysis: BasicAA
33 ; CHECK-PV-INVALIDATE: Running pass: InvalidateAnalysisPass
34 ; CHECK-PV-INVALIDATE: Invalidating analysis: PhiValuesAnalysis
35 ; CHECK-PV-INVALIDATE: Invalidating analysis: BasicAA
36 ; CHECK-PV-INVALIDATE: Running pass: AAEvaluator
37 ; CHECK-PV-INVALIDATE: Running analysis: BasicAA
2638
2739 ; Some code that will result in actual AA queries, including inside of a loop.
2840 ; FIXME: Sadly, none of these queries managed to use either the domtree or
None ; RUN: opt < %s -basicaa -aa-eval -print-all-alias-modref-info -disable-output 2>&1 | FileCheck %s
1 ; RUN: opt < %s -aa-pipeline=basic-aa -passes=aa-eval -print-all-alias-modref-info -disable-output 2>&1 | FileCheck %s
0 ; RUN: opt < %s -phi-values -basicaa -aa-eval -print-all-alias-modref-info -disable-output 2>&1 | FileCheck %s
1 ; RUN: opt < %s -aa-pipeline=basic-aa -passes='require,aa-eval' -print-all-alias-modref-info -disable-output 2>&1 | FileCheck %s
22 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
33 target triple = "x86_64-unknown-linux-gnu"
44
7777 declare void @inc(i32*)
7878
7979
80 ; When we have a chain of phis in nested loops we should recognise if there's
81 ; actually only one underlying value.
82 ; CHECK-LABEL: loop_phi_chain
83 ; CHECK: NoAlias: i32* %val1, i32* @Y
84 ; CHECK: NoAlias: i32* %val2, i32* @Y
85 ; CHECK: NoAlias: i32* %val3, i32* @Y
86 define void @loop_phi_chain(i32 %a, i32 %b, i32 %c) {
87 entry:
88 br label %loop1
89
90 loop1:
91 %n1 = phi i32 [ 0, %entry ], [ %add1, %loop2 ]
92 %val1 = phi i32* [ @X, %entry ], [ %val2, %loop2 ]
93 %add1 = add i32 %n1, 1
94 %cmp1 = icmp ne i32 %n1, 32
95 br i1 %cmp1, label %loop2, label %end
96
97 loop2:
98 %n2 = phi i32 [ 0, %loop1 ], [ %add2, %loop3 ]
99 %val2 = phi i32* [ %val1, %loop1 ], [ %val3, %loop3 ]
100 %add2 = add i32 %n2, 1
101 %cmp2 = icmp ne i32 %n2, 32
102 br i1 %cmp2, label %loop3, label %loop1
103
104 loop3:
105 %n3 = phi i32 [ 0, %loop2 ], [ %add3, %loop3 ]
106 %val3 = phi i32* [ %val2, %loop2 ], [ %val3, %loop3 ]
107 store i32 0, i32* %val3, align 4
108 store i32 0, i32* @Y, align 4
109 %add3 = add i32 %n3, 1
110 %cmp3 = icmp ne i32 %n3, 32
111 br i1 %cmp3, label %loop3, label %loop2
112
113 end:
114 ret void
115 }
0 ; RUN: opt -debug-pass=Executions -phi-values -memcpyopt -instcombine -disable-output < %s 2>&1 | FileCheck %s
1
2 ; Check that phi values is not run when it's not already available, and that
3 ; basicaa is freed after a pass that preserves CFG.
4
5 ; CHECK: Executing Pass 'Phi Values Analysis'
6 ; CHECK: Executing Pass 'Basic Alias Analysis (stateless AA impl)'
7 ; CHECK: Executing Pass 'Memory Dependence Analysis'
8 ; CHECK: Executing Pass 'MemCpy Optimization'
9 ; CHECK-DAG: Freeing Pass 'MemCpy Optimization'
10 ; CHECK-DAG: Freeing Pass 'Phi Values Analysis'
11 ; CHECK-DAG: Freeing Pass 'Memory Dependence Analysis'
12 ; CHECK-DAG: Freeing Pass 'Basic Alias Analysis (stateless AA impl)'
13 ; CHECK-NOT: Executing Pass 'Phi Values Analysis'
14 ; CHECK: Executing Pass 'Basic Alias Analysis (stateless AA impl)'
15 ; CHECK: Executing Pass 'Combine redundant instructions'
16
17 declare void @otherfn([4 x i8]*)
18 declare i32 @__gxx_personality_v0(...)
19
20 ; This function is one where if we didn't free basicaa after memcpyopt then the
21 ; usage of basicaa in instcombine would cause a segfault due to stale phi-values
22 ; results being used.
23 define void @fn(i8* %this, i64* %ptr) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
24 entry:
25 %arr = alloca [4 x i8], align 8
26 %gep1 = getelementptr inbounds [4 x i8], [4 x i8]* %arr, i64 0, i32 0
27 br i1 undef, label %then, label %if
28
29 if:
30 br label %then
31
32 then:
33 %phi = phi i64* [ %ptr, %if ], [ null, %entry ]
34 store i8 1, i8* %gep1, align 8
35 %load = load i64, i64* %phi, align 8
36 %gep2 = getelementptr inbounds i8, i8* undef, i64 %load
37 %gep3 = getelementptr inbounds i8, i8* %gep2, i64 40
38 invoke i32 undef(i8* undef)
39 to label %invoke unwind label %lpad
40
41 invoke:
42 unreachable
43
44 lpad:
45 landingpad { i8*, i32 }
46 catch i8* null
47 call void @otherfn([4 x i8]* nonnull %arr)
48 unreachable
49 }
124124 ; CHECK-NEXT: Delete dead loops
125125 ; CHECK-NEXT: Unroll loops
126126 ; CHECK-NEXT: MergedLoadStoreMotion
127 ; CHECK-NEXT: Basic Alias Analysis (stateless AA impl)
127128 ; CHECK-NEXT: Function Alias Analysis Results
128129 ; CHECK-NEXT: Memory Dependence Analysis
129130 ; CHECK-NEXT: Lazy Branch Probability Analysis
137138 ; CHECK-NEXT: Sparse Conditional Constant Propagation
138139 ; CHECK-NEXT: Demanded bits analysis
139140 ; CHECK-NEXT: Bit-Tracking Dead Code Elimination
141 ; CHECK-NEXT: Basic Alias Analysis (stateless AA impl)
140142 ; CHECK-NEXT: Function Alias Analysis Results
141143 ; CHECK-NEXT: Natural Loop Information
142144 ; CHECK-NEXT: Lazy Branch Probability Analysis
154156 ; CHECK-NEXT: Canonicalize natural loops
155157 ; CHECK-NEXT: LCSSA Verifier
156158 ; CHECK-NEXT: Loop-Closed SSA Form Pass
159 ; CHECK-NEXT: Basic Alias Analysis (stateless AA impl)
157160 ; CHECK-NEXT: Function Alias Analysis Results
158161 ; CHECK-NEXT: Scalar Evolution Analysis
159162 ; CHECK-NEXT: Loop Pass Manager
128128 ; CHECK-NEXT: Delete dead loops
129129 ; CHECK-NEXT: Unroll loops
130130 ; CHECK-NEXT: MergedLoadStoreMotion
131 ; CHECK-NEXT: Basic Alias Analysis (stateless AA impl)
131132 ; CHECK-NEXT: Function Alias Analysis Results
132133 ; CHECK-NEXT: Memory Dependence Analysis
133134 ; CHECK-NEXT: Lazy Branch Probability Analysis
141142 ; CHECK-NEXT: Sparse Conditional Constant Propagation
142143 ; CHECK-NEXT: Demanded bits analysis
143144 ; CHECK-NEXT: Bit-Tracking Dead Code Elimination
145 ; CHECK-NEXT: Basic Alias Analysis (stateless AA impl)
144146 ; CHECK-NEXT: Function Alias Analysis Results
145147 ; CHECK-NEXT: Natural Loop Information
146148 ; CHECK-NEXT: Lazy Branch Probability Analysis
158160 ; CHECK-NEXT: Canonicalize natural loops
159161 ; CHECK-NEXT: LCSSA Verifier
160162 ; CHECK-NEXT: Loop-Closed SSA Form Pass
163 ; CHECK-NEXT: Basic Alias Analysis (stateless AA impl)
161164 ; CHECK-NEXT: Function Alias Analysis Results
162165 ; CHECK-NEXT: Scalar Evolution Analysis
163166 ; CHECK-NEXT: Loop Pass Manager
111111 ; CHECK-NEXT: Delete dead loops
112112 ; CHECK-NEXT: Unroll loops
113113 ; CHECK-NEXT: MergedLoadStoreMotion
114 ; CHECK-NEXT: Basic Alias Analysis (stateless AA impl)
114115 ; CHECK-NEXT: Function Alias Analysis Results
115116 ; CHECK-NEXT: Memory Dependence Analysis
116117 ; CHECK-NEXT: Lazy Branch Probability Analysis
124125 ; CHECK-NEXT: Sparse Conditional Constant Propagation
125126 ; CHECK-NEXT: Demanded bits analysis
126127 ; CHECK-NEXT: Bit-Tracking Dead Code Elimination
128 ; CHECK-NEXT: Basic Alias Analysis (stateless AA impl)
127129 ; CHECK-NEXT: Function Alias Analysis Results
128130 ; CHECK-NEXT: Natural Loop Information
129131 ; CHECK-NEXT: Lazy Branch Probability Analysis
141143 ; CHECK-NEXT: Canonicalize natural loops
142144 ; CHECK-NEXT: LCSSA Verifier
143145 ; CHECK-NEXT: Loop-Closed SSA Form Pass
146 ; CHECK-NEXT: Basic Alias Analysis (stateless AA impl)
144147 ; CHECK-NEXT: Function Alias Analysis Results
145148 ; CHECK-NEXT: Scalar Evolution Analysis
146149 ; CHECK-NEXT: Loop Pass Manager
66 ; CHECK: Globals Alias Analysis
77 ; CHECK: Dominator Tree Construction
88 ; CHECK: Natural Loop Information
9 ; CHECK: Basic Alias Analysis (stateless AA impl)
109 ; CHECK: Sparse Conditional Constant Propagation
1110 ; CHECK-NOT: Dominator Tree Construction
1211 ; CHECK-NOT: Natural Loop Information
13 ; CHECK-NOT: Basic Alias Analysis (stateless AA impl)
1412 ; CHECK-NOT: Globals Alias Analysis
1513 ; CHECK: Loop Vectorization
1614