[llvm] 26ebe16 - [SLP]Fix PR88834: check if unsigned arg can be trunced, being used in smax/smin intrinsics.
Alexey Bataev via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 16 06:46:20 PDT 2024
Author: Alexey Bataev
Date: 2024-04-16T06:42:15-07:00
New Revision: 26ebe16d78b22329d602db0398ce163ad610b0dc
URL: https://github.com/llvm/llvm-project/commit/26ebe16d78b22329d602db0398ce163ad610b0dc
DIFF: https://github.com/llvm/llvm-project/commit/26ebe16d78b22329d602db0398ce163ad610b0dc.diff
LOG: [SLP]Fix PR88834: check if unsigned arg can be trunced, being used in smax/smin intrinsics.
Need to check that unsigned argument can be safely used in smax/smin
intrinsics by checking if at least single sign bit is cleared, otherwise
its value may be treated as negative instead of positive.
Added:
Modified:
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
llvm/test/Transforms/SLPVectorizer/RISCV/smax-unsigned-operand.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index d0bcdceae392bd..0cd3ca32933ca2 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -14639,10 +14639,16 @@ bool BoUpSLP::collectValuesToDemote(
assert((ID == Intrinsic::smin || ID == Intrinsic::smax) &&
"Expected min/max intrinsics only.");
unsigned SignBits = OrigBitWidth - BitWidth;
+ APInt Mask = APInt::getBitsSetFrom(OrigBitWidth, BitWidth - 1);
return SignBits <= ComputeNumSignBits(I->getOperand(0), *DL, 0, AC,
nullptr, DT) &&
+ (!isKnownNonNegative(I->getOperand(0), SimplifyQuery(*DL)) ||
+ MaskedValueIsZero(I->getOperand(0), Mask,
+ SimplifyQuery(*DL))) &&
SignBits <= ComputeNumSignBits(I->getOperand(1), *DL, 0, AC,
- nullptr, DT);
+ nullptr, DT) &&
+ (!isKnownNonNegative(I->getOperand(1), SimplifyQuery(*DL)) ||
+ MaskedValueIsZero(I->getOperand(1), Mask, SimplifyQuery(*DL)));
});
};
if (ID != Intrinsic::abs) {
diff --git a/llvm/test/Transforms/SLPVectorizer/RISCV/smax-unsigned-operand.ll b/llvm/test/Transforms/SLPVectorizer/RISCV/smax-unsigned-operand.ll
index 577d995f6a7654..5db148ac1b4855 100644
--- a/llvm/test/Transforms/SLPVectorizer/RISCV/smax-unsigned-operand.ll
+++ b/llvm/test/Transforms/SLPVectorizer/RISCV/smax-unsigned-operand.ll
@@ -9,11 +9,15 @@ define void @main(ptr noalias %p) {
; CHECK-LABEL: define void @main(
; CHECK-SAME: ptr noalias [[P:%.*]]) #[[ATTR0:[0-9]+]] {
; CHECK-NEXT: bb:
+; CHECK-NEXT: [[CONV_4:%.*]] = zext i32 0 to i64
+; CHECK-NEXT: [[COND_4:%.*]] = tail call i64 @llvm.smax.i64(i64 [[CONV_4]], i64 0)
+; CHECK-NEXT: [[CONV5_4:%.*]] = trunc i64 [[COND_4]] to i8
+; CHECK-NEXT: store i8 [[CONV5_4]], ptr getelementptr inbounds ([11 x i8], ptr @e, i64 0, i64 4), align 1
; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[P]], align 4
-; CHECK-NEXT: [[TMP1:%.*]] = insertelement <2 x i32> <i32 0, i32 poison>, i32 [[TMP0]], i32 1
-; CHECK-NEXT: [[TMP2:%.*]] = call <2 x i32> @llvm.smax.v2i32(<2 x i32> [[TMP1]], <2 x i32> <i32 0, i32 1>)
-; CHECK-NEXT: [[TMP3:%.*]] = trunc <2 x i32> [[TMP2]] to <2 x i8>
-; CHECK-NEXT: store <2 x i8> [[TMP3]], ptr getelementptr inbounds ([11 x i8], ptr @e, i64 0, i64 4), align 1
+; CHECK-NEXT: [[CONV_5:%.*]] = zext i32 [[TMP0]] to i64
+; CHECK-NEXT: [[COND_5:%.*]] = tail call i64 @llvm.smax.i64(i64 [[CONV_5]], i64 1)
+; CHECK-NEXT: [[CONV5_5:%.*]] = trunc i64 [[COND_5]] to i8
+; CHECK-NEXT: store i8 [[CONV5_5]], ptr getelementptr inbounds ([11 x i8], ptr @e, i64 0, i64 5), align 1
; CHECK-NEXT: ret void
;
bb:
More information about the llvm-commits
mailing list