[llvm] 5a654bf - Revert "[InstCombine] `sext(trunc(x)) --> sext(x)` iff trunc is NSW (PR49543)"
Roman Lebedev via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 20 15:11:39 PDT 2021
Author: Roman Lebedev
Date: 2021-04-21T01:11:15+03:00
New Revision: 5a654bfeab588715fa6d266a7789484850605a18
URL: https://github.com/llvm/llvm-project/commit/5a654bfeab588715fa6d266a7789484850605a18
DIFF: https://github.com/llvm/llvm-project/commit/5a654bfeab588715fa6d266a7789484850605a18.diff
LOG: Revert "[InstCombine] `sext(trunc(x)) --> sext(x)` iff trunc is NSW (PR49543)"
I forgot about the case where we sign-extend to width smaller than the original.
This reverts commit 1e6ca23ab8e350c7bab5d7f93e4d3dee18d180cc.
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
llvm/test/Transforms/InstCombine/sext-of-trunc-nsw.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index 68f6d0e01b80..21ca15c43a4b 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -1493,22 +1493,12 @@ Instruction *InstCombinerImpl::visitSExt(SExtInst &CI) {
// If the input is a trunc from the destination type, then turn sext(trunc(x))
// into shifts.
Value *X;
- if (match(Src, m_Trunc(m_Value(X)))) {
+ if (match(Src, m_OneUse(m_Trunc(m_Value(X)))) && X->getType() == DestTy) {
+ // sext(trunc(X)) --> ashr(shl(X, C), C)
unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
unsigned DestBitSize = DestTy->getScalarSizeInBits();
- unsigned XBitSize = X->getType()->getScalarSizeInBits();
-
- // Iff X had more sign bits than the number of bits that were chopped off
- // by the truncation, we can directly sign-extend the X.
- unsigned XNumSignBits = ComputeNumSignBits(X, 0, &CI);
- if (XNumSignBits > (XBitSize - SrcBitSize))
- return CastInst::Create(Instruction::SExt, X, DestTy);
-
- if (Src->hasOneUse() && X->getType() == DestTy) {
- // sext(trunc(X)) --> ashr(shl(X, C), C)
- Constant *ShAmt = ConstantInt::get(DestTy, DestBitSize - SrcBitSize);
- return BinaryOperator::CreateAShr(Builder.CreateShl(X, ShAmt), ShAmt);
- }
+ Constant *ShAmt = ConstantInt::get(DestTy, DestBitSize - SrcBitSize);
+ return BinaryOperator::CreateAShr(Builder.CreateShl(X, ShAmt), ShAmt);
}
if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src))
diff --git a/llvm/test/Transforms/InstCombine/sext-of-trunc-nsw.ll b/llvm/test/Transforms/InstCombine/sext-of-trunc-nsw.ll
index e5912ee7bc66..10098d946bf7 100644
--- a/llvm/test/Transforms/InstCombine/sext-of-trunc-nsw.ll
+++ b/llvm/test/Transforms/InstCombine/sext-of-trunc-nsw.ll
@@ -13,7 +13,8 @@ define i16 @t0(i8 %x) {
; CHECK-LABEL: @t0(
; CHECK-NEXT: [[A:%.*]] = ashr i8 [[X:%.*]], 5
; CHECK-NEXT: call void @use8(i8 [[A]])
-; CHECK-NEXT: [[C:%.*]] = sext i8 [[A]] to i16
+; CHECK-NEXT: [[B:%.*]] = trunc i8 [[A]] to i4
+; CHECK-NEXT: [[C:%.*]] = sext i4 [[B]] to i16
; CHECK-NEXT: ret i16 [[C]]
;
%a = ashr i8 %x, 5
@@ -27,7 +28,8 @@ define i16 @t1(i8 %x) {
; CHECK-LABEL: @t1(
; CHECK-NEXT: [[A:%.*]] = ashr i8 [[X:%.*]], 4
; CHECK-NEXT: call void @use8(i8 [[A]])
-; CHECK-NEXT: [[C:%.*]] = sext i8 [[A]] to i16
+; CHECK-NEXT: [[B:%.*]] = trunc i8 [[A]] to i4
+; CHECK-NEXT: [[C:%.*]] = sext i4 [[B]] to i16
; CHECK-NEXT: ret i16 [[C]]
;
%a = ashr i8 %x, 4
@@ -57,7 +59,8 @@ define <2 x i16> @t3_vec(<2 x i8> %x) {
; CHECK-LABEL: @t3_vec(
; CHECK-NEXT: [[A:%.*]] = ashr <2 x i8> [[X:%.*]], <i8 4, i8 4>
; CHECK-NEXT: call void @usevec(<2 x i8> [[A]])
-; CHECK-NEXT: [[C:%.*]] = sext <2 x i8> [[A]] to <2 x i16>
+; CHECK-NEXT: [[B:%.*]] = trunc <2 x i8> [[A]] to <2 x i4>
+; CHECK-NEXT: [[C:%.*]] = sext <2 x i4> [[B]] to <2 x i16>
; CHECK-NEXT: ret <2 x i16> [[C]]
;
%a = ashr <2 x i8> %x, <i8 4, i8 4>
@@ -88,7 +91,7 @@ define i16 @t5_extrause(i8 %x) {
; CHECK-NEXT: call void @use8(i8 [[A]])
; CHECK-NEXT: [[B:%.*]] = trunc i8 [[A]] to i4
; CHECK-NEXT: call void @use4(i4 [[B]])
-; CHECK-NEXT: [[C:%.*]] = sext i8 [[A]] to i16
+; CHECK-NEXT: [[C:%.*]] = sext i4 [[B]] to i16
; CHECK-NEXT: ret i16 [[C]]
;
%a = ashr i8 %x, 5
More information about the llvm-commits
mailing list