[llvm] 7c69a03 - [ConstantFold][SVE] Fix constant folding for shufflevector.
Eli Friedman via llvm-commits
llvm-commits at lists.llvm.org
Mon Dec 9 15:32:07 PST 2019
Author: Eli Friedman
Date: 2019-12-09T15:31:50-08:00
New Revision: 7c69a03c56601a55f47f29ea59e33c37e62db556
URL: https://github.com/llvm/llvm-project/commit/7c69a03c56601a55f47f29ea59e33c37e62db556
DIFF: https://github.com/llvm/llvm-project/commit/7c69a03c56601a55f47f29ea59e33c37e62db556.diff
LOG: [ConstantFold][SVE] Fix constant folding for shufflevector.
Don't try to fold away shuffles which can't be folded. Fix creation of
shufflevector constant expressions.
Differential Revision: https://reviews.llvm.org/D71147
Added:
llvm/test/Analysis/ConstantFolding/shufflevector.ll
Modified:
llvm/lib/IR/ConstantFold.cpp
llvm/lib/IR/Constants.cpp
llvm/lib/IR/ConstantsContext.h
Removed:
################################################################################
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index bf01f9f2ae67..6e24f03c4cfd 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -873,6 +873,12 @@ Constant *llvm::ConstantFoldShuffleVectorInstruction(Constant *V1,
// Don't break the bitcode reader hack.
if (isa<ConstantExpr>(Mask)) return nullptr;
+ // Do not iterate on scalable vector. The num of elements is unknown at
+ // compile-time.
+ VectorType *ValTy = cast<VectorType>(V1->getType());
+ if (ValTy->isScalable())
+ return nullptr;
+
unsigned SrcNumElts = V1->getType()->getVectorNumElements();
// Loop over the shuffle mask, evaluating each element.
diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp
index 7ea5cb8b167b..fc215d6bf958 100644
--- a/llvm/lib/IR/Constants.cpp
+++ b/llvm/lib/IR/Constants.cpp
@@ -2211,7 +2211,7 @@ Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
return FC; // Fold a few common cases.
- unsigned NElts = Mask->getType()->getVectorNumElements();
+ ElementCount NElts = Mask->getType()->getVectorElementCount();
Type *EltTy = V1->getType()->getVectorElementType();
Type *ShufTy = VectorType::get(EltTy, NElts);
diff --git a/llvm/lib/IR/ConstantsContext.h b/llvm/lib/IR/ConstantsContext.h
index 1ec9087551f8..f5e2481f3903 100644
--- a/llvm/lib/IR/ConstantsContext.h
+++ b/llvm/lib/IR/ConstantsContext.h
@@ -149,7 +149,7 @@ class ShuffleVectorConstantExpr : public ConstantExpr {
ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
: ConstantExpr(VectorType::get(
cast<VectorType>(C1->getType())->getElementType(),
- cast<VectorType>(C3->getType())->getNumElements()),
+ cast<VectorType>(C3->getType())->getElementCount()),
Instruction::ShuffleVector,
&Op<0>(), 3) {
Op<0>() = C1;
diff --git a/llvm/test/Analysis/ConstantFolding/shufflevector.ll b/llvm/test/Analysis/ConstantFolding/shufflevector.ll
new file mode 100644
index 000000000000..d69c2caecc01
--- /dev/null
+++ b/llvm/test/Analysis/ConstantFolding/shufflevector.ll
@@ -0,0 +1,11 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -constprop -S | FileCheck %s
+
+define <vscale x 4 x i32> @shufflevector_scalable_constant() {
+; CHECK-LABEL: @shufflevector_scalable_constant(
+; CHECK-NEXT: ret <vscale x 4 x i32> shufflevector (<vscale x 4 x i32> insertelement (<vscale x 4 x i32> undef, i32 1, i32 0), <vscale x 4 x i32> undef, <vscale x 4 x i32> zeroinitializer)
+;
+ %i = insertelement <vscale x 4 x i32> undef, i32 1, i32 0
+ %i2 = shufflevector <vscale x 4 x i32> %i, <vscale x 4 x i32> undef, <vscale x 4 x i32> zeroinitializer
+ ret <vscale x 4 x i32> %i2
+}
More information about the llvm-commits
mailing list