[llvm] b865eea - [InstCombine] eliminate sext and/or trunc if value has enough signbits

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 3 10:59:41 PDT 2021


Author: Sanjay Patel
Date: 2021-06-03T13:58:19-04:00
New Revision: b865eead76577b031c6fae5e2490e7be0073f201

URL: https://github.com/llvm/llvm-project/commit/b865eead76577b031c6fae5e2490e7be0073f201
DIFF: https://github.com/llvm/llvm-project/commit/b865eead76577b031c6fae5e2490e7be0073f201.diff

LOG: [InstCombine] eliminate sext and/or trunc if value has enough signbits

If we have enough signbits in a source value, we can skip an
intermediate cast for a trunc+sext pair:
https://alive2.llvm.org/ce/z/A_mQt-

This is the original problem shown in:
https://llvm.org/PR49543

There's a test that shows we transformed what used to be
a pair of shifts, so that suggests we could add another
ComputeNumSignBits fold starting from a shift.

There does not appear to be any change in compile-time
from the extra analysis:
https://llvm-compile-time-tracker.com/compare.php?from=3d2c9069dcafd0cbb641841aa3dd6e851fb7d760&to=b9513cdf2419704c7bb0c3a02a9ca06aae13d902&stat=instructions

Differential Revision: https://reviews.llvm.org/D103617

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 ca5f02d20025a..f658dc35deea7 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -1526,13 +1526,20 @@ Instruction *InstCombinerImpl::visitSExt(SExtInst &CI) {
                                       ShAmt);
   }
 
-  // If the input is a trunc from the destination type, then turn sext(trunc(x))
-  // into shifts.
   Value *X;
-  if (match(Src, m_OneUse(m_Trunc(m_Value(X)))) && 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);
+  if (match(Src, m_Trunc(m_Value(X)))) {
+    // If the input has more sign bits than bits truncated, then convert
+    // directly to final type.
+    unsigned XBitSize = X->getType()->getScalarSizeInBits();
+    if (ComputeNumSignBits(X, 0, &CI) > XBitSize - SrcBitSize)
+      return CastInst::CreateIntegerCast(X, DestTy, /* isSigned */ true);
+
+    // If input is a trunc from the destination type, then convert into shifts.
+    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);
+    }
   }
 
   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 8aa244971cf41..c2211deb6aa23 100644
--- a/llvm/test/Transforms/InstCombine/sext-of-trunc-nsw.ll
+++ b/llvm/test/Transforms/InstCombine/sext-of-trunc-nsw.ll
@@ -13,8 +13,7 @@ define i16 @t0(i8 %x) {
 ; CHECK-LABEL: @t0(
 ; CHECK-NEXT:    [[A:%.*]] = ashr i8 [[X:%.*]], 5
 ; CHECK-NEXT:    call void @use8(i8 [[A]])
-; CHECK-NEXT:    [[B:%.*]] = trunc i8 [[A]] to i4
-; CHECK-NEXT:    [[C:%.*]] = sext i4 [[B]] to i16
+; CHECK-NEXT:    [[C:%.*]] = sext i8 [[A]] to i16
 ; CHECK-NEXT:    ret i16 [[C]]
 ;
   %a = ashr i8 %x, 5
@@ -28,8 +27,7 @@ define i16 @t1(i8 %x) {
 ; CHECK-LABEL: @t1(
 ; CHECK-NEXT:    [[A:%.*]] = ashr i8 [[X:%.*]], 4
 ; CHECK-NEXT:    call void @use8(i8 [[A]])
-; CHECK-NEXT:    [[B:%.*]] = trunc i8 [[A]] to i4
-; CHECK-NEXT:    [[C:%.*]] = sext i4 [[B]] to i16
+; CHECK-NEXT:    [[C:%.*]] = sext i8 [[A]] to i16
 ; CHECK-NEXT:    ret i16 [[C]]
 ;
   %a = ashr i8 %x, 4
@@ -59,8 +57,7 @@ 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:    [[B:%.*]] = trunc <2 x i8> [[A]] to <2 x i4>
-; CHECK-NEXT:    [[C:%.*]] = sext <2 x i4> [[B]] to <2 x i16>
+; CHECK-NEXT:    [[C:%.*]] = sext <2 x i8> [[A]] to <2 x i16>
 ; CHECK-NEXT:    ret <2 x i16> [[C]]
 ;
   %a = ashr <2 x i8> %x, <i8 4, i8 4>
@@ -91,7 +88,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 i4 [[B]] to i16
+; CHECK-NEXT:    [[C:%.*]] = sext i8 [[A]] to i16
 ; CHECK-NEXT:    ret i16 [[C]]
 ;
   %a = ashr i8 %x, 5
@@ -106,8 +103,7 @@ define i64 @narrow_source_matching_signbits(i32 %x) {
 ; CHECK-LABEL: @narrow_source_matching_signbits(
 ; CHECK-NEXT:    [[M:%.*]] = and i32 [[X:%.*]], 7
 ; CHECK-NEXT:    [[A:%.*]] = shl nsw i32 -1, [[M]]
-; CHECK-NEXT:    [[B:%.*]] = trunc i32 [[A]] to i8
-; CHECK-NEXT:    [[C:%.*]] = sext i8 [[B]] to i64
+; CHECK-NEXT:    [[C:%.*]] = sext i32 [[A]] to i64
 ; CHECK-NEXT:    ret i64 [[C]]
 ;
   %m = and i32 %x, 7
@@ -117,6 +113,8 @@ define i64 @narrow_source_matching_signbits(i32 %x) {
   ret i64 %c
 }
 
+; negative test - not enough sign-bits
+
 define i64 @narrow_source_not_matching_signbits(i32 %x) {
 ; CHECK-LABEL: @narrow_source_not_matching_signbits(
 ; CHECK-NEXT:    [[M:%.*]] = and i32 [[X:%.*]], 8
@@ -136,8 +134,7 @@ define i24 @wide_source_matching_signbits(i32 %x) {
 ; CHECK-LABEL: @wide_source_matching_signbits(
 ; CHECK-NEXT:    [[M:%.*]] = and i32 [[X:%.*]], 7
 ; CHECK-NEXT:    [[A:%.*]] = shl nsw i32 -1, [[M]]
-; CHECK-NEXT:    [[B:%.*]] = trunc i32 [[A]] to i8
-; CHECK-NEXT:    [[C:%.*]] = sext i8 [[B]] to i24
+; CHECK-NEXT:    [[C:%.*]] = trunc i32 [[A]] to i24
 ; CHECK-NEXT:    ret i24 [[C]]
 ;
   %m = and i32 %x, 7
@@ -147,6 +144,8 @@ define i24 @wide_source_matching_signbits(i32 %x) {
   ret i24 %c
 }
 
+; negative test - not enough sign-bits
+
 define i24 @wide_source_not_matching_signbits(i32 %x) {
 ; CHECK-LABEL: @wide_source_not_matching_signbits(
 ; CHECK-NEXT:    [[M2:%.*]] = and i32 [[X:%.*]], 8
@@ -165,9 +164,8 @@ define i24 @wide_source_not_matching_signbits(i32 %x) {
 define i32 @same_source_matching_signbits(i32 %x) {
 ; CHECK-LABEL: @same_source_matching_signbits(
 ; CHECK-NEXT:    [[M:%.*]] = and i32 [[X:%.*]], 7
-; CHECK-NEXT:    [[TMP1:%.*]] = shl i32 -16777216, [[M]]
-; CHECK-NEXT:    [[C:%.*]] = ashr exact i32 [[TMP1]], 24
-; CHECK-NEXT:    ret i32 [[C]]
+; CHECK-NEXT:    [[A:%.*]] = shl nsw i32 -1, [[M]]
+; CHECK-NEXT:    ret i32 [[A]]
 ;
   %m = and i32 %x, 7
   %a = shl nsw i32 -1, %m
@@ -176,6 +174,8 @@ define i32 @same_source_matching_signbits(i32 %x) {
   ret i32 %c
 }
 
+; negative test - not enough sign-bits
+
 define i32 @same_source_not_matching_signbits(i32 %x) {
 ; CHECK-LABEL: @same_source_not_matching_signbits(
 ; CHECK-NEXT:    [[M2:%.*]] = and i32 [[X:%.*]], 8
@@ -196,8 +196,7 @@ define i32 @same_source_matching_signbits_extra_use(i32 %x) {
 ; CHECK-NEXT:    [[A:%.*]] = shl nsw i32 -1, [[M]]
 ; CHECK-NEXT:    [[B:%.*]] = trunc i32 [[A]] to i8
 ; CHECK-NEXT:    call void @use8(i8 [[B]])
-; CHECK-NEXT:    [[C:%.*]] = sext i8 [[B]] to i32
-; CHECK-NEXT:    ret i32 [[C]]
+; CHECK-NEXT:    ret i32 [[A]]
 ;
   %m = and i32 %x, 7
   %a = shl nsw i32 -1, %m


        


More information about the llvm-commits mailing list