[llvm] 05527b6 - [InstCombine] fold more shuffles with FP<->Int cast operands

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Tue May 24 12:24:20 PDT 2022


Author: Sanjay Patel
Date: 2022-05-24T15:11:38-04:00
New Revision: 05527b68a0be19fe49b884f3b22195701dc4b46f

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

LOG: [InstCombine] fold more shuffles with FP<->Int cast operands

shuffle (cast X), (cast Y), Mask --> cast (shuffle X, Y, Mask)

This extends the transform added with 0353c2c996c5.

If the shuffle reduces vector length, the transform
reduces the width of the cast, so that should be a
win for most codegen (if not, it can be inverted).

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
    llvm/test/Transforms/InstCombine/vec_shuffle.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp b/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
index c5bdfa43f7491..7d616278e104a 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
@@ -2302,8 +2302,9 @@ static Instruction *foldCastShuffle(ShuffleVectorInst &Shuf,
   VectorType *ShufOpTy = cast<VectorType>(Shuf.getOperand(0)->getType());
   VectorType *CastSrcTy = cast<VectorType>(Cast0->getSrcTy());
 
-  // TODO: Allow length-changing shuffles?
-  if (ShufTy != ShufOpTy)
+  // TODO: Allow length-increasing shuffles?
+  if (ShufTy->getElementCount().getKnownMinValue() >
+      ShufOpTy->getElementCount().getKnownMinValue())
     return nullptr;
 
   // TODO: Allow element-size-decreasing casts (ex: fptosi float to i8)?

diff  --git a/llvm/test/Transforms/InstCombine/vec_shuffle.ll b/llvm/test/Transforms/InstCombine/vec_shuffle.ll
index 20dab18266d0e..0084e77787a91 100644
--- a/llvm/test/Transforms/InstCombine/vec_shuffle.ll
+++ b/llvm/test/Transforms/InstCombine/vec_shuffle.ll
@@ -2075,8 +2075,6 @@ define <3 x i16> @fptoui_shuf_
diff erent_source_types(<3 x float> %x, <3 x half>
   ret <3 x i16> %r
 }
 
-; negative test - must have same size elements
-
 define <4 x i32> @fptoui_shuf_widen_elts(<4 x half> %x, <4 x half> %y) {
 ; CHECK-LABEL: @fptoui_shuf_widen_elts(
 ; CHECK-NEXT:    [[TMP1:%.*]] = shufflevector <4 x half> [[X:%.*]], <4 x half> [[Y:%.*]], <4 x i32> <i32 0, i32 1, i32 4, i32 5>
@@ -2089,7 +2087,7 @@ define <4 x i32> @fptoui_shuf_widen_elts(<4 x half> %x, <4 x half> %y) {
   ret <4 x i32> %r
 }
 
-; negative test - must have same size elements
+; negative test - must have same or smaller size source elements
 
 define <4 x float> @sitofp_shuf_narrow_elts(<4 x i64> %x, <4 x i64> %y) {
 ; CHECK-LABEL: @sitofp_shuf_narrow_elts(
@@ -2172,7 +2170,7 @@ define <4 x i32> @fptoi_shuf(<4 x float> %x, <4 x float> %y) {
   ret <4 x i32> %r
 }
 
-; negative test - length-changing shuffle
+; negative test - length-increasing shuffle
 
 define <4 x float> @sitofp_shuf_widen(<2 x i32> %x, <2 x i32> %y) {
 ; CHECK-LABEL: @sitofp_shuf_widen(
@@ -2187,13 +2185,10 @@ define <4 x float> @sitofp_shuf_widen(<2 x i32> %x, <2 x i32> %y) {
   ret <4 x float> %r
 }
 
-; negative test - length-changing shuffle
-
 define <2 x float> @uitofp_shuf_narrow(<4 x i32> %x, <4 x i32> %y) {
 ; CHECK-LABEL: @uitofp_shuf_narrow(
-; CHECK-NEXT:    [[NX:%.*]] = uitofp <4 x i32> [[X:%.*]] to <4 x float>
-; CHECK-NEXT:    [[NY:%.*]] = uitofp <4 x i32> [[Y:%.*]] to <4 x float>
-; CHECK-NEXT:    [[R:%.*]] = shufflevector <4 x float> [[NX]], <4 x float> [[NY]], <2 x i32> <i32 3, i32 5>
+; CHECK-NEXT:    [[TMP1:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> [[Y:%.*]], <2 x i32> <i32 3, i32 5>
+; CHECK-NEXT:    [[R:%.*]] = uitofp <2 x i32> [[TMP1]] to <2 x float>
 ; CHECK-NEXT:    ret <2 x float> [[R]]
 ;
   %nx = uitofp <4 x i32> %x to <4 x float>


        


More information about the llvm-commits mailing list