Skip to content

Commit 3375046

Browse files
committed
[ValueTracking] Use isKnownNonEqual() in isNonZeroSub()
(x - y) != 0 is true iff x != y, so use the isKnownNonEqual() helper, which knows some additional tricks.
1 parent 7c1d8c7 commit 3375046

File tree

2 files changed

+3
-13
lines changed

2 files changed

+3
-13
lines changed

llvm/lib/Analysis/ValueTracking.cpp

+2-9
Original file line numberDiff line numberDiff line change
@@ -2368,19 +2368,12 @@ static bool isNonZeroAdd(const APInt &DemandedElts, unsigned Depth,
23682368
static bool isNonZeroSub(const APInt &DemandedElts, unsigned Depth,
23692369
const SimplifyQuery &Q, unsigned BitWidth, Value *X,
23702370
Value *Y) {
2371+
// TODO: Move this case into isKnownNonEqual().
23712372
if (auto *C = dyn_cast<Constant>(X))
23722373
if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Depth, Q))
23732374
return true;
23742375

2375-
KnownBits XKnown = computeKnownBits(X, DemandedElts, Depth, Q);
2376-
if (XKnown.isUnknown())
2377-
return false;
2378-
KnownBits YKnown = computeKnownBits(Y, DemandedElts, Depth, Q);
2379-
// If X != Y then X - Y is non zero.
2380-
std::optional<bool> ne = KnownBits::ne(XKnown, YKnown);
2381-
// If we are unable to compute if X != Y, we won't be able to do anything
2382-
// computing the knownbits of the sub expression so just return here.
2383-
return ne && *ne;
2376+
return ::isKnownNonEqual(X, Y, Depth, Q);
23842377
}
23852378

23862379
static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,

llvm/test/Analysis/ValueTracking/known-non-zero.ll

+1-4
Original file line numberDiff line numberDiff line change
@@ -1222,10 +1222,7 @@ define i1 @sub_via_non_eq(i8 %x, i8 %y) {
12221222
; CHECK-LABEL: @sub_via_non_eq(
12231223
; CHECK-NEXT: [[NE:%.*]] = icmp ne i8 [[X:%.*]], 0
12241224
; CHECK-NEXT: call void @llvm.assume(i1 [[NE]])
1225-
; CHECK-NEXT: [[SHL:%.*]] = shl nuw i8 [[X]], 3
1226-
; CHECK-NEXT: [[SUB:%.*]] = sub i8 [[X]], [[SHL]]
1227-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[SUB]], 0
1228-
; CHECK-NEXT: ret i1 [[CMP]]
1225+
; CHECK-NEXT: ret i1 false
12291226
;
12301227
%ne = icmp ne i8 %x, 0
12311228
call void @llvm.assume(i1 %ne)

0 commit comments

Comments
 (0)