Skip to content

Commit f72dd4e

Browse files
authored
[ConstantFolding] Fold scalable shufflevector of poison/undef. (#143475)
1 parent be3c6a0 commit f72dd4e

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

llvm/lib/IR/ConstantFold.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -463,10 +463,9 @@ Constant *llvm::ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2,
463463
Constant *Elt =
464464
ConstantExpr::getExtractElement(V1, ConstantInt::get(Ty, 0));
465465

466-
if (Elt->isNullValue()) {
467-
auto *VTy = VectorType::get(EltTy, MaskEltCount);
468-
return ConstantAggregateZero::get(VTy);
469-
} else if (!MaskEltCount.isScalable())
466+
// For scalable vectors, make sure this doesn't fold back into a
467+
// shufflevector.
468+
if (!MaskEltCount.isScalable() || Elt->isNullValue() || isa<UndefValue>(Elt))
470469
return ConstantVector::getSplat(MaskEltCount, Elt);
471470
}
472471

llvm/test/Transforms/InstSimplify/shufflevector.ll

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,19 @@ define <4 x i32> @not_fold_identity2(<4 x i32> %x) {
337337
%revshuf = shufflevector <4 x i32> %shuf, <4 x i32> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
338338
ret <4 x i32> %revshuf
339339
}
340+
341+
define <vscale x 2 x i32> @scalable_splat_undef() {
342+
; CHECK-LABEL: @scalable_splat_undef(
343+
; CHECK-NEXT: ret <vscale x 2 x i32> undef
344+
;
345+
%shuf = shufflevector <vscale x 2 x i32> undef, <vscale x 2 x i32> undef, <vscale x 2 x i32> zeroinitializer
346+
ret <vscale x 2 x i32> %shuf
347+
}
348+
349+
define <vscale x 2 x i32> @scalable_splat_poison() {
350+
; CHECK-LABEL: @scalable_splat_poison(
351+
; CHECK-NEXT: ret <vscale x 2 x i32> poison
352+
;
353+
%shuf = shufflevector <vscale x 2 x i32> poison, <vscale x 2 x i32> poison, <vscale x 2 x i32> zeroinitializer
354+
ret <vscale x 2 x i32> %shuf
355+
}

0 commit comments

Comments
 (0)