1 From 3dd7de023be43bca7d6d45140e4fde42e22bc336 Mon Sep 17 00:00:00 2001
2 From: "Brian M. Rzycki" <brzycki@gmail.com>
3 Date: Fri, 16 Feb 2018 16:35:17 +0000
4 Subject: [PATCH 2/2] [JumpThreading] PR36133 enable/disable DominatorTree for
8 The LazyValueInfo pass caches a copy of the DominatorTree when available.
9 Whenever there are pending DominatorTree updates within JumpThreading's
10 DeferredDominance object we cannot use the cached DT for LVI analysis.
11 This commit adds the new methods enableDT() and disableDT() to LVI.
12 JumpThreading also sets the appropriate usage model before calling LVI
15 Fixes https://bugs.llvm.org/show_bug.cgi?id=36133
17 Reviewers: sebpop, dberlin, kuhar
19 Reviewed by: sebpop, kuhar
21 Subscribers: uabelho, llvm-commits, aprantl, hiraditya, a.elovikov
23 Differential Revision: https://reviews.llvm.org/D42717
27 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325356 91177308-0d34-0410-b5e6-96231b3b80d8
29 include/llvm/Analysis/LazyValueInfo.h | 7 ++++
30 include/llvm/IR/Dominators.h | 3 ++
31 lib/Analysis/LazyValueInfo.cpp | 30 +++++++++++++++-
32 lib/IR/Dominators.cpp | 3 ++
33 lib/Transforms/Scalar/JumpThreading.cpp | 37 ++++++++++++++++++++
34 test/Transforms/JumpThreading/pr36133.ll | 44 ++++++++++++++++++++++++
35 6 files changed, 123 insertions(+), 1 deletion(-)
36 create mode 100644 test/Transforms/JumpThreading/pr36133.ll
38 diff --git a/llvm/include/llvm/Analysis/LazyValueInfo.h b/llvm/include/llvm/Analysis/LazyValueInfo.h
39 index 787c88cc6ec..cea5bf0df80 100644
40 --- a/llvm/include/llvm/Analysis/LazyValueInfo.h
41 +++ b/llvm/include/llvm/Analysis/LazyValueInfo.h
42 @@ -113,6 +113,13 @@ public:
43 /// in LVI, so we need to pass it here as an argument.
44 void printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS);
46 + /// Disables use of the DominatorTree within LVI.
49 + /// Enables use of the DominatorTree within LVI. Does nothing if the class
50 + /// instance was initialized without a DT pointer.
53 // For old PM pass. Delete once LazyValueInfoWrapperPass is gone.
56 diff --git a/llvm/include/llvm/IR/Dominators.h b/llvm/include/llvm/IR/Dominators.h
57 index c5373376ade..ccb18527abc 100644
58 --- a/llvm/include/llvm/IR/Dominators.h
59 +++ b/llvm/include/llvm/IR/Dominators.h
60 @@ -342,6 +342,9 @@ public:
61 /// \brief Returns true if DelBB is awaiting deletion at a flush() event.
62 bool pendingDeletedBB(BasicBlock *DelBB);
64 + /// \brief Returns true if pending DT updates are queued for a flush() event.
67 /// \brief Flushes all pending updates and block deletions. Returns a
68 /// correct DominatorTree reference to be used by the caller for analysis.
69 DominatorTree &flush();
70 diff --git a/llvm/lib/Analysis/LazyValueInfo.cpp b/llvm/lib/Analysis/LazyValueInfo.cpp
71 index d7da669f6e7..fcbfb08cf1d 100644
72 --- a/llvm/lib/Analysis/LazyValueInfo.cpp
73 +++ b/llvm/lib/Analysis/LazyValueInfo.cpp
74 @@ -401,6 +401,7 @@ namespace {
75 AssumptionCache *AC; ///< A pointer to the cache of @llvm.assume calls.
76 const DataLayout &DL; ///< A mandatory DataLayout
77 DominatorTree *DT; ///< An optional DT pointer.
78 + DominatorTree *DisabledDT; ///< Stores DT if it's disabled.
80 ValueLatticeElement getBlockValue(Value *Val, BasicBlock *BB);
81 bool getEdgeValue(Value *V, BasicBlock *F, BasicBlock *T,
82 @@ -463,13 +464,30 @@ namespace {
83 TheCache.eraseBlock(BB);
86 + /// Disables use of the DominatorTree within LVI.
89 + assert(!DisabledDT && "Both DT and DisabledDT are not nullptr!");
90 + std::swap(DT, DisabledDT);
94 + /// Enables use of the DominatorTree within LVI. Does nothing if the class
95 + /// instance was initialized without a DT pointer.
98 + assert(!DT && "Both DT and DisabledDT are not nullptr!");
99 + std::swap(DT, DisabledDT);
103 /// This is the update interface to inform the cache that an edge from
104 /// PredBB to OldSucc has been threaded to be from PredBB to NewSucc.
105 void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
107 LazyValueInfoImpl(AssumptionCache *AC, const DataLayout &DL,
108 DominatorTree *DT = nullptr)
109 - : AC(AC), DL(DL), DT(DT) {}
110 + : AC(AC), DL(DL), DT(DT), DisabledDT(nullptr) {}
112 } // end anonymous namespace
114 @@ -1791,6 +1809,16 @@ void LazyValueInfo::printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS)
118 +void LazyValueInfo::disableDT() {
120 + getImpl(PImpl, AC, DL, DT).disableDT();
123 +void LazyValueInfo::enableDT() {
125 + getImpl(PImpl, AC, DL, DT).enableDT();
128 // Print the LVI for the function arguments at the start of each basic block.
129 void LazyValueInfoAnnotatedWriter::emitBasicBlockStartAnnot(
130 const BasicBlock *BB, formatted_raw_ostream &OS) {
131 diff --git a/llvm/lib/IR/Dominators.cpp b/llvm/lib/IR/Dominators.cpp
132 index e44e845b324..5efd0df5db9 100644
133 --- a/llvm/lib/IR/Dominators.cpp
134 +++ b/llvm/lib/IR/Dominators.cpp
135 @@ -453,6 +453,9 @@ bool DeferredDominance::pendingDeletedBB(BasicBlock *DelBB) {
136 return DeletedBBs.count(DelBB) != 0;
139 +/// \brief Returns true if pending DT updates are queued for a flush() event.
140 +bool DeferredDominance::pending() { return !PendUpdates.empty(); }
142 /// \brief Flushes all pending updates and block deletions. Returns a
143 /// correct DominatorTree reference to be used by the caller for analysis.
144 DominatorTree &DeferredDominance::flush() {
145 diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
146 index 4d366e8e392..9ff91cf4924 100644
147 --- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp
148 +++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
149 @@ -425,6 +425,7 @@ bool JumpThreadingPass::runImpl(Function &F, TargetLibraryInfo *TLI_,
157 @@ -617,6 +618,10 @@ bool JumpThreadingPass::ComputeValueKnownInPredecessors(
158 // "X < 4" and "X < 3" is known true but "X < 4" itself is not available.
159 // Perhaps getConstantOnEdge should be smart enough to do this?
161 + if (DDT->pending())
165 for (BasicBlock *P : predecessors(BB)) {
166 // If the value is known by LazyValueInfo to be a constant in a
167 // predecessor, use that information to try to thread this block.
168 @@ -630,6 +635,10 @@ bool JumpThreadingPass::ComputeValueKnownInPredecessors(
170 /// If I is a PHI node, then we know the incoming values for any constants.
171 if (PHINode *PN = dyn_cast<PHINode>(I)) {
172 + if (DDT->pending())
176 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
177 Value *InVal = PN->getIncomingValue(i);
178 if (Constant *KC = getKnownConstant(InVal, Preference)) {
179 @@ -759,6 +768,10 @@ bool JumpThreadingPass::ComputeValueKnownInPredecessors(
180 const DataLayout &DL = PN->getModule()->getDataLayout();
181 // We can do this simplification if any comparisons fold to true or false.
183 + if (DDT->pending())
187 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
188 BasicBlock *PredBB = PN->getIncomingBlock(i);
189 Value *LHS = PN->getIncomingValue(i);
190 @@ -792,6 +805,10 @@ bool JumpThreadingPass::ComputeValueKnownInPredecessors(
192 if (!isa<Instruction>(CmpLHS) ||
193 cast<Instruction>(CmpLHS)->getParent() != BB) {
194 + if (DDT->pending())
198 for (BasicBlock *P : predecessors(BB)) {
199 // If the value is known by LazyValueInfo to be a constant in a
200 // predecessor, use that information to try to thread this block.
201 @@ -820,6 +837,10 @@ bool JumpThreadingPass::ComputeValueKnownInPredecessors(
202 match(CmpLHS, m_Add(m_Value(AddLHS), m_ConstantInt(AddConst)))) {
203 if (!isa<Instruction>(AddLHS) ||
204 cast<Instruction>(AddLHS)->getParent() != BB) {
205 + if (DDT->pending())
209 for (BasicBlock *P : predecessors(BB)) {
210 // If the value is known by LazyValueInfo to be a ConstantRange in
211 // a predecessor, use that information to try to thread this
212 @@ -901,6 +922,10 @@ bool JumpThreadingPass::ComputeValueKnownInPredecessors(
215 // If all else fails, see if LVI can figure out a constant value for us.
216 + if (DDT->pending())
220 Constant *CI = LVI->getConstant(V, BB, CxtI);
221 if (Constant *KC = getKnownConstant(CI, Preference)) {
222 for (BasicBlock *Pred : predecessors(BB))
223 @@ -1102,6 +1127,10 @@ bool JumpThreadingPass::ProcessBlock(BasicBlock *BB) {
224 // threading is concerned.
225 assert(CondBr->isConditional() && "Threading on unconditional terminator");
227 + if (DDT->pending())
231 LazyValueInfo::Tristate Ret =
232 LVI->getPredicateAt(CondCmp->getPredicate(), CondCmp->getOperand(0),
234 @@ -1899,6 +1928,10 @@ bool JumpThreadingPass::ThreadEdge(BasicBlock *BB,
235 << ", across block:\n "
238 + if (DDT->pending())
242 LVI->threadEdge(PredBB, BB, SuccBB);
244 // We are going to have to map operands from the original BB block to the new
245 @@ -2368,6 +2401,10 @@ bool JumpThreadingPass::TryToUnfoldSelect(CmpInst *CondCmp, BasicBlock *BB) {
246 // Now check if one of the select values would allow us to constant fold the
247 // terminator in BB. We don't do the transform if both sides fold, those
248 // cases will be threaded in any case.
249 + if (DDT->pending())
253 LazyValueInfo::Tristate LHSFolds =
254 LVI->getPredicateOnEdge(CondCmp->getPredicate(), SI->getOperand(1),
255 CondRHS, Pred, BB, CondCmp);
256 diff --git a/llvm/test/Transforms/JumpThreading/pr36133.ll b/llvm/test/Transforms/JumpThreading/pr36133.ll
258 index 00000000000..b8d8c5fac46
260 +++ b/llvm/test/Transforms/JumpThreading/pr36133.ll
262 +; RUN: opt -jump-threading -S < %s | FileCheck %s
263 +@global = external global i8*, align 8
265 +define i32 @foo(i32 %arg) {
269 +; CHECK-NEXT: br i1 %tmp1, label %bb7, label %bb7
271 + %tmp = load i8*, i8** @global, align 8
272 + %tmp1 = icmp eq i8* %tmp, null
273 + br i1 %tmp1, label %bb3, label %bb2
281 + %tmp4 = phi i8 [ 1, %bb2 ], [ 0, %bb ]
282 + %tmp5 = icmp eq i8 %tmp4, 0
283 + br i1 %tmp5, label %bb7, label %bb6
291 + %tmp8 = icmp eq i32 %arg, -1
292 + br i1 %tmp8, label %bb9, label %bb10
298 +; CHECK-LABEL: bb10:
300 + %tmp11 = icmp sgt i32 %arg, -1
301 + call void @llvm.assume(i1 %tmp11)
305 +declare void @llvm.assume(i1)