Skip to content

Commit 297ec61

Browse files
author
Serguei Katkov
committed
[IsKnownNonZero] Handle the case with non-constant phi nodes
Handle the case when all inputs of phi are proven to be non zero. Constants are checked in beginning of this method before check for depth of recursion, so it is a partial case of non-constant phi. Recursion depth is already handled by the function. Reviewers: aqjune, nikic, efriedma Reviewed By: nikic Subscribers: dantrushin, hiraditya, jdoerfert, llvm-commits Differential Revision: https://reviews.llvm.org/D88276
1 parent b76df59 commit 297ec61

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2561,11 +2561,16 @@ bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth,
25612561
}
25622562
}
25632563
}
2564-
// Check if all incoming values are non-zero constant.
2565-
bool AllNonZeroConstants = llvm::all_of(PN->operands(), [](Value *V) {
2566-
return isa<ConstantInt>(V) && !cast<ConstantInt>(V)->isZero();
2564+
// Check if all incoming values are non-zero using recursion.
2565+
Query RecQ = Q;
2566+
unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
2567+
bool AllNonZero = llvm::all_of(PN->operands(), [&](const Use &U) {
2568+
if (U.get() == PN)
2569+
return true;
2570+
RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2571+
return isKnownNonZero(U.get(), DemandedElts, NewDepth, RecQ);
25672572
});
2568-
if (AllNonZeroConstants)
2573+
if (AllNonZero)
25692574
return true;
25702575
}
25712576
// ExtractElement

llvm/test/Transforms/InstCombine/phi.ll

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,35 @@ if.end: ; preds = %if.else, %if.then
10561056
ret i1 %cmp1
10571057
}
10581058

1059+
define i1 @phi_allnonzerononconstant(i1 %c, i32 %a, i32* nonnull %b1, i32* nonnull %b2) {
1060+
; CHECK-LABEL: @phi_allnonzerononconstant(
1061+
; CHECK-NEXT: entry:
1062+
; CHECK-NEXT: br i1 [[C:%.*]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
1063+
; CHECK: if.then:
1064+
; CHECK-NEXT: br label [[IF_END:%.*]]
1065+
; CHECK: if.else:
1066+
; CHECK-NEXT: call void @dummy()
1067+
; CHECK-NEXT: br label [[IF_END]]
1068+
; CHECK: if.end:
1069+
; CHECK-NEXT: ret i1 false
1070+
;
1071+
entry:
1072+
br i1 %c, label %if.then, label %if.else
1073+
1074+
if.then: ; preds = %entry
1075+
br label %if.end
1076+
1077+
if.else: ; preds = %entry
1078+
call void @dummy()
1079+
1080+
br label %if.end
1081+
1082+
if.end: ; preds = %if.else, %if.then
1083+
%x.0 = phi i32* [ %b1, %if.then ], [ %b2, %if.else ]
1084+
%cmp1 = icmp eq i32* %x.0, null
1085+
ret i1 %cmp1
1086+
}
1087+
10591088
declare void @dummy()
10601089

10611090
define i1 @phi_knownnonzero_eq(i32 %n, i32 %s, i32* nocapture readonly %P) {

0 commit comments

Comments
 (0)