diff options
author | Noah Goldstein <goldstein.w.n@gmail.com> | 2023-04-25 13:47:49 -0500 |
---|---|---|
committer | Noah Goldstein <goldstein.w.n@gmail.com> | 2023-04-26 23:48:17 -0500 |
commit | 9b3c865d32d84f13a19fe3b45cc66fcef910a368 (patch) | |
tree | 7bee8c6ebafe57c3d5928cd327accbd1a7617558 /llvm/lib | |
parent | 73f5f1a8fa1325a2f29724e29dc331aa8e8b01d1 (diff) | |
download | llvm-9b3c865d32d84f13a19fe3b45cc66fcef910a368.zip llvm-9b3c865d32d84f13a19fe3b45cc66fcef910a368.tar.gz llvm-9b3c865d32d84f13a19fe3b45cc66fcef910a368.tar.bz2 |
[ValueTracking] Add logic for `(sub x, y) != 0` if we know `KnownX != KnownY`
Alive2 Link:
https://alive2.llvm.org/ce/z/TAFcjF
Differential Revision: https://reviews.llvm.org/D149202
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/Analysis/ValueTracking.cpp | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index 7ceca4a..76a9ef9 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -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) || |