Skip to content

Commit 0c969e6

Browse files
committed
[ValueTracking] Handle recursive select/PHI in IsKnownNonZero
1 parent cc5208b commit 0c969e6

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
@@ -3033,18 +3033,36 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
30333033
return true;
30343034

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