Skip to content

Commit

Permalink
[ValueTracking] Add logic for (sub x, y) != 0 if we know `KnownX !=…
Browse files Browse the repository at this point in the history
… KnownY`

Alive2 Link:
    https://alive2.llvm.org/ce/z/TAFcjF

Differential Revision: https://reviews.llvm.org/D149202
  • Loading branch information
goldsteinn committed Apr 27, 2023
1 parent 73f5f1a commit 9b3c865
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
17 changes: 15 additions & 2 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2705,12 +2705,25 @@ bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth,
Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
return isKnownNonZero(I->getOperand(0), Depth, Q);
break;
case Instruction::Sub:
case Instruction::Sub: {
if (auto *C = dyn_cast<Constant>(I->getOperand(0)))
if (C->isNullValue() &&
isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q))
return true;
break;

KnownBits XKnown =
computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q);
if (!XKnown.isUnknown()) {
KnownBits YKnown =
computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q);
// If X != Y then X - Y is non zero.
std::optional<bool> ne = KnownBits::ne(XKnown, YKnown);
// If we are unable to compute if X != Y, we won't be able to do anything
// computing the knownbits of the sub expression so just return here.
return ne && *ne;
}
return false;
}
case Instruction::Or:
// X | Y != 0 if X != 0 or Y != 0.
return isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q) ||
Expand Down
7 changes: 1 addition & 6 deletions llvm/test/Analysis/ValueTracking/known-non-zero.ll
Original file line number Diff line number Diff line change
Expand Up @@ -442,12 +442,7 @@ define i1 @shl_nonzero_and_shift_out_zeros_fail(i32 %ccnt, i32 %y) {

define i1 @sub_nonzero_ops_ne(i8 %xx, i8 %yy, i8 %z) {
; CHECK-LABEL: @sub_nonzero_ops_ne(
; CHECK-NEXT: [[X:%.*]] = and i8 [[XX:%.*]], -65
; CHECK-NEXT: [[Y:%.*]] = or i8 [[YY:%.*]], 64
; CHECK-NEXT: [[S:%.*]] = sub i8 [[X]], [[Y]]
; CHECK-NEXT: [[EXP:%.*]] = or i8 [[Z:%.*]], [[S]]
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[EXP]], 0
; CHECK-NEXT: ret i1 [[R]]
; CHECK-NEXT: ret i1 false
;
%x = and i8 %xx, 191
%y = or i8 %yy, 64
Expand Down

0 comments on commit 9b3c865

Please sign in to comment.