Skip to content

Commit 444383e

Browse files
committed
[ValueTracking] Do more thorough non-zero check in isKnownToBePowerOfTwo when OrZero is no set.
We can cover more cases by directly checking if the result is known-nonzero for common patterns when they are missing `OrZero`. This patch add `isKnownNonZero` checks for `shl`, `lshr`, `and`, and `mul`. Differential Revision: https://reviews.llvm.org/D157309
1 parent dfda65c commit 444383e

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,20 +2061,19 @@ bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
20612061
return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
20622062
return false;
20632063
case Instruction::Mul:
2064-
return OrZero &&
2065-
isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Depth, Q) &&
2066-
isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2064+
return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Depth, Q) &&
2065+
isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q) &&
2066+
(OrZero || isKnownNonZero(I, Depth, Q));
20672067
case Instruction::And:
2068-
if (OrZero) {
2069-
// A power of two and'd with anything is a power of two or zero.
2070-
if (isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ true, Depth, Q) ||
2071-
isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ true, Depth, Q))
2072-
return true;
2073-
// X & (-X) is always a power of two or zero.
2074-
if (match(I->getOperand(0), m_Neg(m_Specific(I->getOperand(1)))) ||
2075-
match(I->getOperand(1), m_Neg(m_Specific(I->getOperand(0)))))
2076-
return true;
2077-
}
2068+
// A power of two and'd with anything is a power of two or zero.
2069+
if (OrZero &&
2070+
(isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ true, Depth, Q) ||
2071+
isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ true, Depth, Q)))
2072+
return true;
2073+
// X & (-X) is always a power of two or zero.
2074+
if (match(I->getOperand(0), m_Neg(m_Specific(I->getOperand(1)))) ||
2075+
match(I->getOperand(1), m_Neg(m_Specific(I->getOperand(0)))))
2076+
return OrZero || isKnownNonZero(I->getOperand(0), Depth, Q);
20782077
return false;
20792078
case Instruction::Add: {
20802079
// Adding a power-of-two or zero to the same power-of-two or zero yields

llvm/test/Analysis/ValueTracking/known-power-of-two.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,9 @@ define i1 @and_is_pow2(i16 %x, i16 %y) {
584584
; CHECK-SAME: (i16 [[X:%.*]], i16 [[Y:%.*]]) {
585585
; CHECK-NEXT: [[XNZ:%.*]] = or i16 [[X]], 4
586586
; CHECK-NEXT: [[X_NEG:%.*]] = sub nsw i16 0, [[XNZ]]
587-
; CHECK-NEXT: [[XX:%.*]] = and i16 [[XNZ]], [[X_NEG]]
588-
; CHECK-NEXT: [[AND:%.*]] = and i16 [[XX]], [[Y]]
589-
; CHECK-NEXT: [[R:%.*]] = icmp eq i16 [[AND]], [[XX]]
587+
; CHECK-NEXT: [[TMP1:%.*]] = and i16 [[X_NEG]], [[Y]]
588+
; CHECK-NEXT: [[AND:%.*]] = and i16 [[TMP1]], [[XNZ]]
589+
; CHECK-NEXT: [[R:%.*]] = icmp ne i16 [[AND]], 0
590590
; CHECK-NEXT: ret i1 [[R]]
591591
;
592592
%xnz = or i16 %x, 4

llvm/test/Transforms/InstSimplify/ctpop-pow2.ll

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ define i16 @ctpop_x_and_negx(i16 %x) {
4141

4242
define i8 @ctpop_x_nz_and_negx(i8 %x) {
4343
; CHECK-LABEL: @ctpop_x_nz_and_negx(
44-
; CHECK-NEXT: [[X1:%.*]] = or i8 [[X:%.*]], 1
45-
; CHECK-NEXT: [[V0:%.*]] = sub i8 0, [[X1]]
46-
; CHECK-NEXT: [[V1:%.*]] = and i8 [[X1]], [[V0]]
47-
; CHECK-NEXT: ret i8 [[V1]]
44+
; CHECK-NEXT: ret i8 1
4845
;
4946
%x1 = or i8 %x, 1
5047
%v0 = sub i8 0, %x1

0 commit comments

Comments
 (0)