Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 3dd7de0

Browse files
bmrzyckidotdash
authored andcommitted
[JumpThreading] PR36133 enable/disable DominatorTree for LVI analysis
Summary: The LazyValueInfo pass caches a copy of the DominatorTree when available. Whenever there are pending DominatorTree updates within JumpThreading's DeferredDominance object we cannot use the cached DT for LVI analysis. This commit adds the new methods enableDT() and disableDT() to LVI. JumpThreading also sets the appropriate usage model before calling LVI analysis methods. Fixes https://bugs.llvm.org/show_bug.cgi?id=36133 Reviewers: sebpop, dberlin, kuhar Reviewed by: sebpop, kuhar Subscribers: uabelho, llvm-commits, aprantl, hiraditya, a.elovikov Differential Revision: https://reviews.llvm.org/D42717 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325356 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 90f447e commit 3dd7de0

File tree

6 files changed

+123
-1
lines changed

6 files changed

+123
-1
lines changed

include/llvm/Analysis/LazyValueInfo.h

+7
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ class LazyValueInfo {
113113
/// in LVI, so we need to pass it here as an argument.
114114
void printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS);
115115

116+
/// Disables use of the DominatorTree within LVI.
117+
void disableDT();
118+
119+
/// Enables use of the DominatorTree within LVI. Does nothing if the class
120+
/// instance was initialized without a DT pointer.
121+
void enableDT();
122+
116123
// For old PM pass. Delete once LazyValueInfoWrapperPass is gone.
117124
void releaseMemory();
118125

include/llvm/IR/Dominators.h

+3
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ class DeferredDominance {
342342
/// \brief Returns true if DelBB is awaiting deletion at a flush() event.
343343
bool pendingDeletedBB(BasicBlock *DelBB);
344344

345+
/// \brief Returns true if pending DT updates are queued for a flush() event.
346+
bool pending();
347+
345348
/// \brief Flushes all pending updates and block deletions. Returns a
346349
/// correct DominatorTree reference to be used by the caller for analysis.
347350
DominatorTree &flush();

lib/Analysis/LazyValueInfo.cpp

+29-1
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ namespace {
401401
AssumptionCache *AC; ///< A pointer to the cache of @llvm.assume calls.
402402
const DataLayout &DL; ///< A mandatory DataLayout
403403
DominatorTree *DT; ///< An optional DT pointer.
404+
DominatorTree *DisabledDT; ///< Stores DT if it's disabled.
404405

405406
ValueLatticeElement getBlockValue(Value *Val, BasicBlock *BB);
406407
bool getEdgeValue(Value *V, BasicBlock *F, BasicBlock *T,
@@ -463,13 +464,30 @@ namespace {
463464
TheCache.eraseBlock(BB);
464465
}
465466

467+
/// Disables use of the DominatorTree within LVI.
468+
void disableDT() {
469+
if (DT) {
470+
assert(!DisabledDT && "Both DT and DisabledDT are not nullptr!");
471+
std::swap(DT, DisabledDT);
472+
}
473+
}
474+
475+
/// Enables use of the DominatorTree within LVI. Does nothing if the class
476+
/// instance was initialized without a DT pointer.
477+
void enableDT() {
478+
if (DisabledDT) {
479+
assert(!DT && "Both DT and DisabledDT are not nullptr!");
480+
std::swap(DT, DisabledDT);
481+
}
482+
}
483+
466484
/// This is the update interface to inform the cache that an edge from
467485
/// PredBB to OldSucc has been threaded to be from PredBB to NewSucc.
468486
void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
469487

470488
LazyValueInfoImpl(AssumptionCache *AC, const DataLayout &DL,
471489
DominatorTree *DT = nullptr)
472-
: AC(AC), DL(DL), DT(DT) {}
490+
: AC(AC), DL(DL), DT(DT), DisabledDT(nullptr) {}
473491
};
474492
} // end anonymous namespace
475493

@@ -1791,6 +1809,16 @@ void LazyValueInfo::printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS)
17911809
}
17921810
}
17931811

1812+
void LazyValueInfo::disableDT() {
1813+
if (PImpl)
1814+
getImpl(PImpl, AC, DL, DT).disableDT();
1815+
}
1816+
1817+
void LazyValueInfo::enableDT() {
1818+
if (PImpl)
1819+
getImpl(PImpl, AC, DL, DT).enableDT();
1820+
}
1821+
17941822
// Print the LVI for the function arguments at the start of each basic block.
17951823
void LazyValueInfoAnnotatedWriter::emitBasicBlockStartAnnot(
17961824
const BasicBlock *BB, formatted_raw_ostream &OS) {

lib/IR/Dominators.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,9 @@ bool DeferredDominance::pendingDeletedBB(BasicBlock *DelBB) {
453453
return DeletedBBs.count(DelBB) != 0;
454454
}
455455

456+
/// \brief Returns true if pending DT updates are queued for a flush() event.
457+
bool DeferredDominance::pending() { return !PendUpdates.empty(); }
458+
456459
/// \brief Flushes all pending updates and block deletions. Returns a
457460
/// correct DominatorTree reference to be used by the caller for analysis.
458461
DominatorTree &DeferredDominance::flush() {

lib/Transforms/Scalar/JumpThreading.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ bool JumpThreadingPass::runImpl(Function &F, TargetLibraryInfo *TLI_,
425425

426426
LoopHeaders.clear();
427427
DDT->flush();
428+
LVI->enableDT();
428429
return EverChanged;
429430
}
430431

@@ -617,6 +618,10 @@ bool JumpThreadingPass::ComputeValueKnownInPredecessors(
617618
// "X < 4" and "X < 3" is known true but "X < 4" itself is not available.
618619
// Perhaps getConstantOnEdge should be smart enough to do this?
619620

621+
if (DDT->pending())
622+
LVI->disableDT();
623+
else
624+
LVI->enableDT();
620625
for (BasicBlock *P : predecessors(BB)) {
621626
// If the value is known by LazyValueInfo to be a constant in a
622627
// predecessor, use that information to try to thread this block.
@@ -630,6 +635,10 @@ bool JumpThreadingPass::ComputeValueKnownInPredecessors(
630635

631636
/// If I is a PHI node, then we know the incoming values for any constants.
632637
if (PHINode *PN = dyn_cast<PHINode>(I)) {
638+
if (DDT->pending())
639+
LVI->disableDT();
640+
else
641+
LVI->enableDT();
633642
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
634643
Value *InVal = PN->getIncomingValue(i);
635644
if (Constant *KC = getKnownConstant(InVal, Preference)) {
@@ -759,6 +768,10 @@ bool JumpThreadingPass::ComputeValueKnownInPredecessors(
759768
const DataLayout &DL = PN->getModule()->getDataLayout();
760769
// We can do this simplification if any comparisons fold to true or false.
761770
// See if any do.
771+
if (DDT->pending())
772+
LVI->disableDT();
773+
else
774+
LVI->enableDT();
762775
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
763776
BasicBlock *PredBB = PN->getIncomingBlock(i);
764777
Value *LHS = PN->getIncomingValue(i);
@@ -792,6 +805,10 @@ bool JumpThreadingPass::ComputeValueKnownInPredecessors(
792805

793806
if (!isa<Instruction>(CmpLHS) ||
794807
cast<Instruction>(CmpLHS)->getParent() != BB) {
808+
if (DDT->pending())
809+
LVI->disableDT();
810+
else
811+
LVI->enableDT();
795812
for (BasicBlock *P : predecessors(BB)) {
796813
// If the value is known by LazyValueInfo to be a constant in a
797814
// predecessor, use that information to try to thread this block.
@@ -820,6 +837,10 @@ bool JumpThreadingPass::ComputeValueKnownInPredecessors(
820837
match(CmpLHS, m_Add(m_Value(AddLHS), m_ConstantInt(AddConst)))) {
821838
if (!isa<Instruction>(AddLHS) ||
822839
cast<Instruction>(AddLHS)->getParent() != BB) {
840+
if (DDT->pending())
841+
LVI->disableDT();
842+
else
843+
LVI->enableDT();
823844
for (BasicBlock *P : predecessors(BB)) {
824845
// If the value is known by LazyValueInfo to be a ConstantRange in
825846
// a predecessor, use that information to try to thread this
@@ -901,6 +922,10 @@ bool JumpThreadingPass::ComputeValueKnownInPredecessors(
901922
}
902923

903924
// If all else fails, see if LVI can figure out a constant value for us.
925+
if (DDT->pending())
926+
LVI->disableDT();
927+
else
928+
LVI->enableDT();
904929
Constant *CI = LVI->getConstant(V, BB, CxtI);
905930
if (Constant *KC = getKnownConstant(CI, Preference)) {
906931
for (BasicBlock *Pred : predecessors(BB))
@@ -1102,6 +1127,10 @@ bool JumpThreadingPass::ProcessBlock(BasicBlock *BB) {
11021127
// threading is concerned.
11031128
assert(CondBr->isConditional() && "Threading on unconditional terminator");
11041129

1130+
if (DDT->pending())
1131+
LVI->disableDT();
1132+
else
1133+
LVI->enableDT();
11051134
LazyValueInfo::Tristate Ret =
11061135
LVI->getPredicateAt(CondCmp->getPredicate(), CondCmp->getOperand(0),
11071136
CondConst, CondBr);
@@ -1899,6 +1928,10 @@ bool JumpThreadingPass::ThreadEdge(BasicBlock *BB,
18991928
<< ", across block:\n "
19001929
<< *BB << "\n");
19011930

1931+
if (DDT->pending())
1932+
LVI->disableDT();
1933+
else
1934+
LVI->enableDT();
19021935
LVI->threadEdge(PredBB, BB, SuccBB);
19031936

19041937
// We are going to have to map operands from the original BB block to the new
@@ -2368,6 +2401,10 @@ bool JumpThreadingPass::TryToUnfoldSelect(CmpInst *CondCmp, BasicBlock *BB) {
23682401
// Now check if one of the select values would allow us to constant fold the
23692402
// terminator in BB. We don't do the transform if both sides fold, those
23702403
// cases will be threaded in any case.
2404+
if (DDT->pending())
2405+
LVI->disableDT();
2406+
else
2407+
LVI->enableDT();
23712408
LazyValueInfo::Tristate LHSFolds =
23722409
LVI->getPredicateOnEdge(CondCmp->getPredicate(), SI->getOperand(1),
23732410
CondRHS, Pred, BB, CondCmp);
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
; RUN: opt -jump-threading -S < %s | FileCheck %s
2+
@global = external global i8*, align 8
3+
4+
define i32 @foo(i32 %arg) {
5+
; CHECK-LABEL: @foo
6+
; CHECK-LABEL: bb:
7+
; CHECK: icmp eq
8+
; CHECK-NEXT: br i1 %tmp1, label %bb7, label %bb7
9+
bb:
10+
%tmp = load i8*, i8** @global, align 8
11+
%tmp1 = icmp eq i8* %tmp, null
12+
br i1 %tmp1, label %bb3, label %bb2
13+
14+
; CHECK-NOT: bb2:
15+
bb2:
16+
br label %bb3
17+
18+
; CHECK-NOT: bb3:
19+
bb3:
20+
%tmp4 = phi i8 [ 1, %bb2 ], [ 0, %bb ]
21+
%tmp5 = icmp eq i8 %tmp4, 0
22+
br i1 %tmp5, label %bb7, label %bb6
23+
24+
; CHECK-NOT: bb6:
25+
bb6:
26+
br label %bb7
27+
28+
; CHECK-LABEL: bb7:
29+
bb7:
30+
%tmp8 = icmp eq i32 %arg, -1
31+
br i1 %tmp8, label %bb9, label %bb10
32+
33+
; CHECK-LABEL: bb9:
34+
bb9:
35+
ret i32 0
36+
37+
; CHECK-LABEL: bb10:
38+
bb10:
39+
%tmp11 = icmp sgt i32 %arg, -1
40+
call void @llvm.assume(i1 %tmp11)
41+
ret i32 1
42+
}
43+
44+
declare void @llvm.assume(i1)

0 commit comments

Comments
 (0)