[llvm] [InstCombine] Move extends across identity shuffles. (PR #146901)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 4 07:27:17 PDT 2025


================
@@ -2579,15 +2578,30 @@ static Instruction *foldCastShuffle(ShuffleVectorInst &Shuf,
     return nullptr;
   }
 
+  VectorType *CastSrcTy = cast<VectorType>(Cast0->getSrcTy());
   VectorType *ShufTy = Shuf.getType();
   VectorType *ShufOpTy = cast<VectorType>(Shuf.getOperand(0)->getType());
-  VectorType *CastSrcTy = cast<VectorType>(Cast0->getSrcTy());
 
   // TODO: Allow length-increasing shuffles?
   if (ShufTy->getElementCount().getKnownMinValue() >
       ShufOpTy->getElementCount().getKnownMinValue())
     return nullptr;
 
+  // shuffle (cast X), Y, identity-with-extract-mask -->
+  // cast (shuffle X, Y, identity-with-extract-mask).
+  if (Cast0->hasOneUse() && Shuf.isIdentityWithExtract()) {
----------------
dtcxzyw wrote:

The second operand should be a poison.
Miscompilation reproducer: https://alive2.llvm.org/ce/z/tXo9tJ
```
define <4 x i16> @src(<8 x i8> %x, <8 x i16> %y) {
entry:
  %e.1 = zext <8 x i8> %x to <8 x i16>
  %shuffle = shufflevector <8 x i16> %e.1, <8 x i16> %y, <4 x i32> <i32 8, i32 9, i32 10, i32 11>
  ret <4 x i16> %shuffle
}

define <4 x i16> @tgt(<8 x i8> %x, <8 x i16> %y) {
entry:
  ret <4 x i16> poison
}
```

https://github.com/llvm/llvm-project/pull/146901


More information about the llvm-commits mailing list