Skip to content

Commit 514f267

Browse files
committed
removed match and moved into selfmultiply condition, simplified condition to drop ternary and moved test to llvm/test/Transforms/InstCombine since wasn't folded with instsimplifiy
1 parent 72cd125 commit 514f267

File tree

3 files changed

+48
-51
lines changed

3 files changed

+48
-51
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,24 @@ static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
409409
}
410410

411411
bool SelfMultiply = Op0 == Op1;
412-
if (SelfMultiply)
412+
if (SelfMultiply) {
413413
SelfMultiply &=
414414
isGuaranteedNotToBeUndef(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1);
415-
Known = KnownBits::mul(Known, Known2, SelfMultiply);
415+
416+
Known = KnownBits::mul(Known, Known2, SelfMultiply);
417+
418+
unsigned SignBits = ComputeNumSignBits(Op0, DemandedElts, Q, Depth + 1);
419+
unsigned TyBits = Op0->getType()->getScalarSizeInBits();
420+
unsigned OutValidBits = 2 * (TyBits - SignBits + 1);
421+
422+
if (OutValidBits < TyBits) {
423+
APInt KnownZeroMask =
424+
APInt::getHighBitsSet(TyBits, TyBits - OutValidBits + 1);
425+
Known.Zero |= KnownZeroMask;
426+
}
427+
} else {
428+
Known = KnownBits::mul(Known, Known2, SelfMultiply);
429+
}
416430

417431
// Only make use of no-wrap flags if we failed to compute the sign bit
418432
// directly. This matters if the multiplication always overflows, in
@@ -423,22 +437,6 @@ static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
423437
Known.makeNonNegative();
424438
else if (isKnownNegative && !Known.isNonNegative())
425439
Known.makeNegative();
426-
427-
// Check if both operands are the same sign-extension of a single value.
428-
const Value *A = nullptr;
429-
if (match(Op0, m_SExt(m_Value(A))) && match(Op1, m_SExt(m_Specific(A)))) {
430-
unsigned SignBits = ComputeNumSignBits(Op0, DemandedElts, Q, Depth + 1);
431-
unsigned TyBits = Op0->getType()->getScalarSizeInBits();
432-
// The output of the Mul can be at most twice the valid bits
433-
unsigned OutValidBits = 2 * (TyBits - SignBits + 1);
434-
unsigned OutSignBits =
435-
OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
436-
437-
if (OutSignBits > 1) {
438-
APInt KnownZeroMask = APInt::getHighBitsSet(TyBits, OutSignBits);
439-
Known.Zero |= KnownZeroMask;
440-
}
441-
}
442440
}
443441

444442
void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges,

llvm/test/Analysis/ValueTracking/known-bits.ll

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -49,36 +49,3 @@ define i1 @vec_reverse_known_bits_demanded_fail(<4 x i8> %xx) {
4949
%r = icmp slt i8 %ele, 0
5050
ret i1 %r
5151
}
52-
53-
; Test known bits for (sext i8 x) * (sext i8 x)
54-
; RUN: opt -passes=instcombine < %s -S | FileCheck %s --check-prefix=SEXT_SQUARE
55-
56-
define i1 @sext_square_bit31(i8 %x) {
57-
; SEXT_SQUARE-LABEL: @sext_square_bit31(
58-
; SEXT_SQUARE-NEXT: ret i1 false
59-
%sx = sext i8 %x to i32
60-
%mul = mul nsw i32 %sx, %sx
61-
%and = and i32 %mul, 2147483648 ; 1 << 31
62-
%cmp = icmp ne i32 %and, 0
63-
ret i1 %cmp
64-
}
65-
66-
define i1 @sext_square_bit30(i8 %x) {
67-
; SEXT_SQUARE-LABEL: @sext_square_bit30(
68-
; SEXT_SQUARE-NEXT: ret i1 false
69-
%sx = sext i8 %x to i32
70-
%mul = mul nsw i32 %sx, %sx
71-
%and = and i32 %mul, 1073741824 ; 1 << 30
72-
%cmp = icmp ne i32 %and, 0
73-
ret i1 %cmp
74-
}
75-
76-
define i1 @sext_square_bit14(i8 %x) {
77-
; SEXT_SQUARE-LABEL: @sext_square_bit14(
78-
; SEXT_SQUARE-NOT: ret i1 false
79-
%sx = sext i8 %x to i32
80-
%mul = mul nsw i32 %sx, %sx
81-
%and = and i32 %mul, 16384 ; 1 << 14
82-
%cmp = icmp ne i32 %and, 0
83-
ret i1 %cmp
84-
}

llvm/test/Transforms/InstCombine/sext.ll

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,3 +423,35 @@ define i64 @smear_set_bit_different_dest_type_wider_dst(i32 %x) {
423423
%s = sext i8 %a to i64
424424
ret i64 %s
425425
}
426+
427+
; Test known bits for (sext i8 x) * (sext i8 x)
428+
429+
define i1 @sext_square_bit30(i8 %x) {
430+
; CHECK-LABEL: @sext_square_bit30(
431+
; CHECK: ret i1 false
432+
%sx = sext i8 %x to i32
433+
%mul = mul nsw i32 %sx, %sx
434+
%and = and i32 %mul, 1073741824 ; 1 << 30
435+
%cmp = icmp ne i32 %and, 0
436+
ret i1 %cmp
437+
}
438+
439+
define i1 @sext_square_bit15(i8 %x) {
440+
; CHECK-LABEL: @sext_square_bit15(
441+
; CHECK: ret i1 false
442+
%sx = sext i8 %x to i32
443+
%mul = mul nsw i32 %sx, %sx
444+
%and = and i32 %mul, 32768 ; 1 << 15
445+
%cmp = icmp ne i32 %and, 0
446+
ret i1 %cmp
447+
}
448+
449+
define i1 @sext_square_bit14(i8 %x) {
450+
; CHECK-LABEL: @sext_square_bit14(
451+
; CHECK-NOT: ret i1 false
452+
%sx = sext i8 %x to i32
453+
%mul = mul nsw i32 %sx, %sx
454+
%and = and i32 %mul, 16384 ; 1 << 14
455+
%cmp = icmp ne i32 %and, 0
456+
ret i1 %cmp
457+
}

0 commit comments

Comments
 (0)