// TODO: Make these support undef elements.
static Type *shrinkFPConstantVector(Value *V) {
auto *CV = dyn_cast<Constant>(V);
- auto *CVVTy = dyn_cast<VectorType>(V->getType());
+ auto *CVVTy = dyn_cast<FixedVectorType>(V->getType());
if (!CV || !CVVTy)
return nullptr;
Type *MinType = nullptr;
- unsigned NumElts = cast<FixedVectorType>(CVVTy)->getNumElements();
+ unsigned NumElts = CVVTy->getNumElements();
+
+ // For fixed-width vectors we find the minimal type by looking
+ // through the constant values of the vector.
for (unsigned i = 0; i != NumElts; ++i) {
auto *CFP = dyn_cast_or_null<ConstantFP>(CV->getAggregateElement(i));
if (!CFP)
if (Type *T = shrinkFPConstant(CFP))
return T;
- // Try to shrink a vector of FP constants.
+ // We can only correctly find a minimum type for a scalable vector when it is
+ // a splat. For splats of constant values the fpext is wrapped up as a
+ // ConstantExpr.
+ if (auto *FPCExt = dyn_cast<ConstantExpr>(V))
+ if (FPCExt->getOpcode() == Instruction::FPExt)
+ return FPCExt->getOperand(0)->getType();
+
+ // Try to shrink a vector of FP constants. This returns nullptr on scalable
+ // vectors
if (Type *T = shrinkFPConstantVector(V))
return T;
--- /dev/null
+; RUN: opt -instcombine -mtriple aarch64-linux-gnu -mattr=+sve -S -o - < %s 2>%t | FileCheck %s
+; RUN: FileCheck --check-prefix=WARN --allow-empty %s <%t
+
+; If this check fails please read test/CodeGen/AArch64/README for instructions on how to resolve it.
+; WARN-NOT: warning
+
+define <vscale x 2 x float> @shrink_splat_scalable_extend(<vscale x 2 x float> %a) {
+ ; CHECK-LABEL: @shrink_splat_scalable_extend
+ ; CHECK-NEXT: %[[FADD:.*]] = fadd <vscale x 2 x float> %a, shufflevector (<vscale x 2 x float> insertelement (<vscale x 2 x float> undef, float -1.000000e+00, i32 0), <vscale x 2 x float> undef, <vscale x 2 x i32> zeroinitializer)
+ ; CHECK-NEXT: ret <vscale x 2 x float> %[[FADD]]
+ %1 = shufflevector <vscale x 2 x float> insertelement (<vscale x 2 x float> undef, float -1.000000e+00, i32 0), <vscale x 2 x float> undef, <vscale x 2 x i32> zeroinitializer
+ %2 = fpext <vscale x 2 x float> %a to <vscale x 2 x double>
+ %3 = fpext <vscale x 2 x float> %1 to <vscale x 2 x double>
+ %4 = fadd <vscale x 2 x double> %2, %3
+ %5 = fptrunc <vscale x 2 x double> %4 to <vscale x 2 x float>
+ ret <vscale x 2 x float> %5
+}