1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-19 02:52:53 +02:00

[ValueTracking] Add ComputeNumSignBits support for llvm.abs intrinsic

If absolute value needs turn a negative number into a positive number it reduces the number of sign bits by at most 1.

Differential Revision: https://reviews.llvm.org/D84971
This commit is contained in:
Craig Topper 2020-07-31 10:43:00 -07:00
parent cd7ddd2dc4
commit 2c4eee97f8
2 changed files with 28 additions and 0 deletions

View File

@ -3001,6 +3001,19 @@ static unsigned ComputeNumSignBitsImpl(const Value *V,
"Failed to determine minimum sign bits");
return Tmp;
}
case Instruction::Call: {
if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
switch (II->getIntrinsicID()) {
default: break;
case Intrinsic::abs:
Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
if (Tmp == 1) break;
// Absolute value reduces number of sign bits by at most 1.
return Tmp - 1;
}
}
}
}
}

View File

@ -51,3 +51,18 @@ define i32 @abs_trailing_zeros_negative(i32 %x) {
%and2 = and i32 %abs, -4
ret i32 %and2
}
; Make sure we infer this add doesn't overflow. The input to the abs has 3
; sign bits, the abs reduces this to 2 sign bits.
define i32 @abs_signbits(i30 %x) {
; CHECK-LABEL: @abs_signbits(
; CHECK-NEXT: [[AND:%.*]] = sext i30 [[X:%.*]] to i32
; CHECK-NEXT: [[ABS:%.*]] = call i32 @llvm.abs.i32(i32 [[AND]], i1 false)
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[ABS]], 1
; CHECK-NEXT: ret i32 [[ADD]]
;
%ext = sext i30 %x to i32
%abs = call i32 @llvm.abs.i32(i32 %ext, i1 false)
%add = add i32 %abs, 1
ret i32 %add
}