Skip to content

Commit 3dfe4f0

Browse files
committed
[ValueTracking] Handle recursive select/PHI in IsKnownNonZero
1 parent fb0ed3b commit 3dfe4f0

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3032,18 +3032,36 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
30323032
return true;
30333033

30343034
// Check if all incoming values are non-zero using recursion.
3035-
SimplifyQuery RecQ = Q.getWithoutCondContext();
3036-
unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
30373035
return llvm::all_of(PN->operands(), [&](const Use &U) {
3038-
if (U.get() == PN)
3036+
Value *IncValue = U.get();
3037+
if (IncValue == PN)
30393038
return true;
3040-
RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
3039+
3040+
Instruction *CxtI = PN->getIncomingBlock(U)->getTerminator();
3041+
unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
3042+
if (auto *SI = dyn_cast<SelectInst>(IncValue)) {
3043+
if (SI->getTrueValue() == PN || SI->getFalseValue() == PN) {
3044+
IncValue = SI->getTrueValue() == PN ? SI->getFalseValue()
3045+
: SI->getTrueValue();
3046+
NewDepth = Depth;
3047+
}
3048+
} else if (auto *IncPhi = dyn_cast<PHINode>(IncValue);
3049+
IncPhi && IncPhi->getNumIncomingValues() == 2) {
3050+
for (int Idx = 0; Idx < 2; ++Idx) {
3051+
if (IncPhi->getIncomingValue(Idx) == PN) {
3052+
IncValue = IncPhi->getIncomingValue(1 - Idx);
3053+
CxtI = IncPhi->getIncomingBlock(1 - Idx)->getTerminator();
3054+
break;
3055+
}
3056+
}
3057+
}
3058+
SimplifyQuery RecQ = Q.getWithoutCondContext().getWithInstruction(CxtI);
30413059
// Check if the branch on the phi excludes zero.
30423060
ICmpInst::Predicate Pred;
30433061
Value *X;
30443062
BasicBlock *TrueSucc, *FalseSucc;
30453063
if (match(RecQ.CxtI,
3046-
m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
3064+
m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_Value(X)),
30473065
m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
30483066
// Check for cases of duplicate successors.
30493067
if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
@@ -3055,7 +3073,7 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
30553073
}
30563074
}
30573075
// Finally recurse on the edge and check it directly.
3058-
return isKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);
3076+
return isKnownNonZero(IncValue, DemandedElts, RecQ, NewDepth);
30593077
});
30603078
}
30613079
case Instruction::InsertElement: {

llvm/test/Transforms/InstCombine/known-phi-recurse.ll

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,19 +300,16 @@ define i1 @known_non_zero_phi_phi_test() {
300300
; CHECK-NEXT: entry:
301301
; CHECK-NEXT: br label [[LOOP:%.*]]
302302
; CHECK: loop:
303-
; CHECK-NEXT: [[INDVAR:%.*]] = phi i8 [ 2, [[ENTRY:%.*]] ], [ [[CONTAIN:%.*]], [[LOOP_BB1:%.*]] ]
304303
; CHECK-NEXT: [[COND0:%.*]] = call i1 @cond()
305-
; CHECK-NEXT: br i1 [[COND0]], label [[LOOP_BB0:%.*]], label [[LOOP_BB1]]
304+
; CHECK-NEXT: br i1 [[COND0]], label [[LOOP_BB0:%.*]], label [[LOOP_BB1:%.*]]
306305
; CHECK: loop.bb0:
307306
; CHECK-NEXT: call void @side.effect()
308307
; CHECK-NEXT: br label [[LOOP_BB1]]
309308
; CHECK: loop.bb1:
310-
; CHECK-NEXT: [[CONTAIN]] = phi i8 [ 1, [[LOOP_BB0]] ], [ [[INDVAR]], [[LOOP]] ]
311309
; CHECK-NEXT: [[COND1:%.*]] = call i1 @cond()
312310
; CHECK-NEXT: br i1 [[COND1]], label [[EXIT:%.*]], label [[LOOP]]
313311
; CHECK: exit:
314-
; CHECK-NEXT: [[BOOL:%.*]] = icmp eq i8 [[CONTAIN]], 0
315-
; CHECK-NEXT: ret i1 [[BOOL]]
312+
; CHECK-NEXT: ret i1 false
316313
;
317314
entry:
318315
br label %loop
@@ -345,8 +342,7 @@ define i1 @known_non_zero_phi_select_test() {
345342
; CHECK-NEXT: [[COND1:%.*]] = call i1 @cond()
346343
; CHECK-NEXT: br i1 [[COND1]], label [[EXIT:%.*]], label [[LOOP]]
347344
; CHECK: exit:
348-
; CHECK-NEXT: [[BOOL:%.*]] = icmp eq i8 [[CONTAIN]], 0
349-
; CHECK-NEXT: ret i1 [[BOOL]]
345+
; CHECK-NEXT: ret i1 false
350346
;
351347
entry:
352348
br label %loop

0 commit comments

Comments
 (0)