[PATCH] D124590: [InstCombine] add casts from splat-a-bit pattern if necessary

Chenbing.Zheng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 28 19:19:28 PDT 2022


Chenbing.Zheng updated this revision to Diff 425949.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D124590/new/

https://reviews.llvm.org/D124590

Files:
  llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
  llvm/test/Transforms/InstCombine/sext.ll


Index: llvm/test/Transforms/InstCombine/sext.ll
===================================================================
--- llvm/test/Transforms/InstCombine/sext.ll
+++ llvm/test/Transforms/InstCombine/sext.ll
@@ -381,17 +381,44 @@
   ret i32 %s
 }
 
-; TODO: this could be mask+compare+sext or shifts+trunc
-
 define i16 @smear_set_bit_different_dest_type(i32 %x) {
 ; CHECK-LABEL: @smear_set_bit_different_dest_type(
+; CHECK-NEXT:    [[TMP1:%.*]] = shl i32 [[X:%.*]], 24
+; CHECK-NEXT:    [[TMP2:%.*]] = ashr i32 [[TMP1]], 31
+; CHECK-NEXT:    [[S:%.*]] = trunc i32 [[TMP2]] to i16
+; CHECK-NEXT:    ret i16 [[S]]
+;
+  %t = trunc i32 %x to i8
+  %a = ashr i8 %t, 7
+  %s = sext i8 %a to i16
+  ret i16 %s
+}
+
+define i16 @smear_set_bit_different_dest_type_extra_use(i32 %x) {
+; CHECK-LABEL: @smear_set_bit_different_dest_type_extra_use(
 ; CHECK-NEXT:    [[T:%.*]] = trunc i32 [[X:%.*]] to i8
-; CHECK-NEXT:    [[A:%.*]] = ashr i8 [[T]], 7
-; CHECK-NEXT:    [[S:%.*]] = sext i8 [[A]] to i16
+; CHECK-NEXT:    call void @use(i8 [[T]])
+; CHECK-NEXT:    [[TMP1:%.*]] = shl i32 [[X]], 24
+; CHECK-NEXT:    [[TMP2:%.*]] = ashr i32 [[TMP1]], 31
+; CHECK-NEXT:    [[S:%.*]] = trunc i32 [[TMP2]] to i16
 ; CHECK-NEXT:    ret i16 [[S]]
 ;
   %t = trunc i32 %x to i8
+  call void @use(i8 %t)
   %a = ashr i8 %t, 7
   %s = sext i8 %a to i16
   ret i16 %s
 }
+
+define i64 @smear_set_bit_different_dest_type_wider_dst(i32 %x) {
+; CHECK-LABEL: @smear_set_bit_different_dest_type_wider_dst(
+; CHECK-NEXT:    [[TMP1:%.*]] = shl i32 [[X:%.*]], 24
+; CHECK-NEXT:    [[TMP2:%.*]] = ashr i32 [[TMP1]], 31
+; CHECK-NEXT:    [[S:%.*]] = sext i32 [[TMP2]] to i64
+; CHECK-NEXT:    ret i64 [[S]]
+;
+  %t = trunc i32 %x to i8
+  %a = ashr i8 %t, 7
+  %s = sext i8 %a to i64
+  ret i64 %s
+}
Index: llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -1592,14 +1592,20 @@
 
   // Splatting a bit of constant-index across a value:
   // sext (ashr (trunc iN X to iM), M-1) to iN --> ashr (shl X, N-M), N-1
-  // TODO: If the dest type is different, use a cast (adjust use check).
+  // If the dest type is different, use a cast (adjust use check).
   if (match(Src, m_OneUse(m_AShr(m_Trunc(m_Value(X)),
-                                 m_SpecificInt(SrcBitSize - 1)))) &&
-      X->getType() == DestTy) {
-    Constant *ShlAmtC = ConstantInt::get(DestTy, DestBitSize - SrcBitSize);
-    Constant *AshrAmtC = ConstantInt::get(DestTy, DestBitSize - 1);
+                                 m_SpecificInt(SrcBitSize - 1))))) {
+    Type *XTy = X->getType();
+    unsigned XBitSize = XTy->getScalarSizeInBits();
+    Constant *ShlAmtC = ConstantInt::get(XTy, XBitSize - SrcBitSize);
+    Constant *AshrAmtC = ConstantInt::get(XTy, XBitSize - 1);
     Value *Shl = Builder.CreateShl(X, ShlAmtC);
-    return BinaryOperator::CreateAShr(Shl, AshrAmtC);
+    if (XTy == DestTy) {
+      return BinaryOperator::CreateAShr(Shl, AshrAmtC);
+    } else {
+      Value *Ashr = Builder.CreateAShr(Shl, AshrAmtC);
+      return CastInst::CreateIntegerCast(Ashr, DestTy, /* isSigned */ true);
+    }
   }
 
   if (match(Src, m_VScale(DL))) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D124590.425949.patch
Type: text/x-patch
Size: 3290 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220429/88e4967c/attachment.bin>


More information about the llvm-commits mailing list