Skip to content
Merged
12 changes: 12 additions & 0 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,18 @@ static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
isGuaranteedNotToBeUndef(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1);
Known = KnownBits::mul(Known, Known2, SelfMultiply);

if (SelfMultiply) {
unsigned SignBits = ComputeNumSignBits(Op0, DemandedElts, Q, Depth + 1);
unsigned TyBits = Op0->getType()->getScalarSizeInBits();
unsigned OutValidBits = 2 * (TyBits - SignBits + 1);

if (OutValidBits < TyBits) {
APInt KnownZeroMask =
APInt::getHighBitsSet(TyBits, TyBits - OutValidBits + 1);
Known.Zero |= KnownZeroMask;
}
}

// Only make use of no-wrap flags if we failed to compute the sign bit
// directly. This matters if the multiplication always overflows, in
// which case we prefer to follow the result of the direct computation,
Expand Down
38 changes: 38 additions & 0 deletions llvm/test/Transforms/InstCombine/sext.ll
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,41 @@ define i64 @smear_set_bit_different_dest_type_wider_dst(i32 %x) {
%s = sext i8 %a to i64
ret i64 %s
}

; Test known bits for (sext i8 x) * (sext i8 x)

define i1 @sext_square_bit30(i8 noundef %x) {
; CHECK-LABEL: @sext_square_bit30(
; CHECK-NEXT: ret i1 false
;
%sx = sext i8 %x to i32
%mul = mul nsw i32 %sx, %sx
%and = and i32 %mul, 1073741824 ; 1 << 30
%cmp = icmp ne i32 %and, 0
ret i1 %cmp
}

define i1 @sext_square_bit15(i8 noundef %x) {
; CHECK-LABEL: @sext_square_bit15(
; CHECK-NEXT: ret i1 false
;
%sx = sext i8 %x to i32
%mul = mul nsw i32 %sx, %sx
%and = and i32 %mul, 32768 ; 1 << 15
%cmp = icmp ne i32 %and, 0
ret i1 %cmp
}

define i1 @sext_square_bit14(i8 noundef %x) {
; CHECK-LABEL: @sext_square_bit14(
; CHECK-NEXT: [[SX:%.*]] = sext i8 [[X:%.*]] to i32
; CHECK-NEXT: [[MUL:%.*]] = mul nsw i32 [[SX]], [[SX]]
; CHECK-NEXT: [[CMP:%.*]] = icmp samesign ugt i32 [[MUL]], 16383
; CHECK-NEXT: ret i1 [[CMP]]
;
%sx = sext i8 %x to i32
%mul = mul nsw i32 %sx, %sx
%and = and i32 %mul, 16384 ; 1 << 14
%cmp = icmp ne i32 %and, 0
ret i1 %cmp
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this one is incorrect: https://alive2.llvm.org/ce/z/y8D-Yg

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though I don't really get why this case is folding. We should have TyBits = 32, SignBits = 9, OutValidBits = 16, OutSignBits = 17. So I would not expect bit 1<<14 to be set to zero.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait, I didn't notice that this one is actually using CHECK-NOT, not CHECK. So everything is correct.