[llvm] ffdb484 - [InstCombine/RISCV] Constant-fold bitcast(vmv.v.x) (#182630)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 9 09:56:22 PDT 2026
Author: Ramkumar Ramachandra
Date: 2026-03-09T16:56:17Z
New Revision: ffdb484752dca7a5c6679383b98c41c9da3ae6b0
URL: https://github.com/llvm/llvm-project/commit/ffdb484752dca7a5c6679383b98c41c9da3ae6b0
DIFF: https://github.com/llvm/llvm-project/commit/ffdb484752dca7a5c6679383b98c41c9da3ae6b0.diff
LOG: [InstCombine/RISCV] Constant-fold bitcast(vmv.v.x) (#182630)
Constant-fold bitcast(vmv.v.x) to avoid creating redundant masks.
llc run showing vsetvli eliminated: https://godbolt.org/z/d1Gx3KqaT
Added:
llvm/test/Transforms/InstCombine/RISCV/riscv-vmv-v-x.ll
Modified:
llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
llvm/lib/Target/RISCV/RISCVTargetTransformInfo.h
Removed:
################################################################################
diff --git a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
index 5394d5dc0be11..820daf96218fd 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
@@ -17,6 +17,7 @@
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicsRISCV.h"
#include "llvm/IR/PatternMatch.h"
+#include "llvm/Transforms/InstCombine/InstCombiner.h"
#include <cmath>
#include <optional>
using namespace llvm;
@@ -3603,3 +3604,54 @@ bool RISCVTTIImpl::shouldCopyAttributeWhenOutliningFrom(
return BaseT::shouldCopyAttributeWhenOutliningFrom(Caller, Attr);
}
+
+std::optional<Instruction *>
+RISCVTTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const {
+ // If all operands of a vmv.v.x are constant, fold a bitcast(vmv.v.x) to scale
+ // the vmv.v.x, enabling removal of the bitcast. The transform helps avoid
+ // creating redundant masks.
+ const DataLayout &DL = IC.getDataLayout();
+ if (II.user_empty())
+ return {};
+ auto *TargetVecTy = dyn_cast<ScalableVectorType>(II.user_back()->getType());
+ if (!TargetVecTy)
+ return {};
+ const APInt *Scalar;
+ uint64_t VL;
+ if (!match(&II, m_Intrinsic<Intrinsic::riscv_vmv_v_x>(
+ m_Poison(), m_APInt(Scalar), m_ConstantInt(VL))) ||
+ !all_of(II.users(), [TargetVecTy](User *U) {
+ return U->getType() == TargetVecTy && match(U, m_BitCast(m_Value()));
+ }))
+ return {};
+ auto *SourceVecTy = cast<ScalableVectorType>(II.getType());
+ unsigned TargetEltBW = DL.getTypeSizeInBits(TargetVecTy->getElementType());
+ unsigned SourceEltBW = DL.getTypeSizeInBits(SourceVecTy->getElementType());
+ if (TargetEltBW % SourceEltBW)
+ return {};
+ unsigned TargetScale = TargetEltBW / SourceEltBW;
+ if (VL % TargetScale)
+ return {};
+ Type *VLTy = II.getOperand(2)->getType();
+ ElementCount SourceEC = SourceVecTy->getElementCount();
+ unsigned NewEltBW = SourceEltBW * TargetScale;
+ if (!SourceEC.isKnownMultipleOf(TargetScale) ||
+ !DL.fitsInLegalInteger(NewEltBW))
+ return {};
+ auto *NewEltTy = IntegerType::get(II.getContext(), NewEltBW);
+ if (!TLI->isLegalElementTypeForRVV(TLI->getValueType(DL, NewEltTy)))
+ return {};
+ ElementCount NewEC = SourceEC.divideCoefficientBy(TargetScale);
+ Type *RetTy = VectorType::get(NewEltTy, NewEC);
+ assert(SourceVecTy->canLosslesslyBitCastTo(RetTy) &&
+ "Lossless bitcast between types expected");
+ APInt NewScalar = APInt::getSplat(NewEltBW, *Scalar);
+ return IC.replaceInstUsesWith(
+ II,
+ IC.Builder.CreateBitCast(
+ IC.Builder.CreateIntrinsic(
+ RetTy, Intrinsic::riscv_vmv_v_x,
+ {PoisonValue::get(RetTy), ConstantInt::get(NewEltTy, NewScalar),
+ ConstantInt::get(VLTy, VL / TargetScale)}),
+ SourceVecTy));
+}
diff --git a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.h b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.h
index 97a364d741314..ad0248138d5bf 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.h
+++ b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.h
@@ -506,6 +506,9 @@ class RISCVTTIImpl final : public BasicTTIImplBase<RISCVTTIImpl> {
bool
shouldCopyAttributeWhenOutliningFrom(const Function *Caller,
const Attribute &Attr) const override;
+
+ std::optional<Instruction *>
+ instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const override;
};
} // end namespace llvm
diff --git a/llvm/test/Transforms/InstCombine/RISCV/riscv-vmv-v-x.ll b/llvm/test/Transforms/InstCombine/RISCV/riscv-vmv-v-x.ll
new file mode 100644
index 0000000000000..2c07f5494fa2b
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/RISCV/riscv-vmv-v-x.ll
@@ -0,0 +1,186 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -p instcombine -mtriple=riscv32 -mattr=+v -S %s | FileCheck %s --check-prefixes=CHECK,RV32
+; RUN: opt -p instcombine -mtriple=riscv64 -mattr=+v -S %s | FileCheck %s --check-prefixes=CHECK,RV64
+; RUN: opt -p instcombine -mtriple=riscv32 -mattr=+zve32x -S %s | FileCheck %s --check-prefixes=CHECK,ZVE32X
+; RUN: opt -p instcombine -mtriple=riscv64 -mattr=+zve32x -S %s | FileCheck %s --check-prefixes=CHECK,ZVE32X
+
+define <vscale x 2 x i32> @target_vl_one() {
+; CHECK-LABEL: define <vscale x 2 x i32> @target_vl_one(
+; CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT: [[TMP1:%.*]] = call <vscale x 2 x i32> @llvm.riscv.vmv.v.x.nxv2i32.i64(<vscale x 2 x i32> poison, i32 1431655765, i64 1)
+; CHECK-NEXT: ret <vscale x 2 x i32> [[TMP1]]
+;
+ %a = call <vscale x 8 x i8> @llvm.riscv.vmv.v.x.nxv8i8(<vscale x 8 x i8> poison, i8 85, i64 4)
+ %b = bitcast <vscale x 8 x i8> %a to <vscale x 2 x i32>
+ ret <vscale x 2 x i32> %b
+}
+
+define <vscale x 4 x i16> @target_vl_two() {
+; CHECK-LABEL: define <vscale x 4 x i16> @target_vl_two(
+; CHECK-SAME: ) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP1:%.*]] = call <vscale x 4 x i16> @llvm.riscv.vmv.v.x.nxv4i16.i64(<vscale x 4 x i16> poison, i16 21845, i64 2)
+; CHECK-NEXT: ret <vscale x 4 x i16> [[TMP1]]
+;
+ %a = call <vscale x 8 x i8> @llvm.riscv.vmv.v.x.nxv8i8(<vscale x 8 x i8> poison, i8 85, i64 4)
+ %b = bitcast <vscale x 8 x i8> %a to <vscale x 4 x i16>
+ ret <vscale x 4 x i16> %b
+}
+
+define <vscale x 2 x i32> @target_vl_32() {
+; CHECK-LABEL: define <vscale x 2 x i32> @target_vl_32(
+; CHECK-SAME: ) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP1:%.*]] = call <vscale x 2 x i32> @llvm.riscv.vmv.v.x.nxv2i32.i64(<vscale x 2 x i32> poison, i32 1431655765, i64 32)
+; CHECK-NEXT: ret <vscale x 2 x i32> [[TMP1]]
+;
+ %a = call <vscale x 8 x i8> @llvm.riscv.vmv.v.x.nxv8i8(<vscale x 8 x i8> poison, i8 85, i64 128)
+ %b = bitcast <vscale x 8 x i8> %a to <vscale x 2 x i32>
+ ret <vscale x 2 x i32> %b
+}
+
+define <vscale x 2 x i32> @small_scalar() {
+; CHECK-LABEL: define <vscale x 2 x i32> @small_scalar(
+; CHECK-SAME: ) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP1:%.*]] = call <vscale x 2 x i32> @llvm.riscv.vmv.v.x.nxv2i32.i64(<vscale x 2 x i32> poison, i32 50529027, i64 1)
+; CHECK-NEXT: ret <vscale x 2 x i32> [[TMP1]]
+;
+ %a = call <vscale x 8 x i8> @llvm.riscv.vmv.v.x.nxv8i8(<vscale x 8 x i8> poison, i8 3, i64 4)
+ %b = bitcast <vscale x 8 x i8> %a to <vscale x 2 x i32>
+ ret <vscale x 2 x i32> %b
+}
+
+define <vscale x 2 x i32> @negative_scalar() {
+; CHECK-LABEL: define <vscale x 2 x i32> @negative_scalar(
+; CHECK-SAME: ) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP1:%.*]] = call <vscale x 2 x i32> @llvm.riscv.vmv.v.x.nxv2i32.i64(<vscale x 2 x i32> poison, i32 -50529028, i64 1)
+; CHECK-NEXT: ret <vscale x 2 x i32> [[TMP1]]
+;
+ %a = call <vscale x 8 x i8> @llvm.riscv.vmv.v.x.nxv8i8(<vscale x 8 x i8> poison, i8 -4, i64 4)
+ %b = bitcast <vscale x 8 x i8> %a to <vscale x 2 x i32>
+ ret <vscale x 2 x i32> %b
+}
+
+define <vscale x 8 x i8> @no_bitcast() {
+; CHECK-LABEL: define <vscale x 8 x i8> @no_bitcast(
+; CHECK-SAME: ) #[[ATTR0]] {
+; CHECK-NEXT: [[A:%.*]] = call <vscale x 8 x i8> @llvm.riscv.vmv.v.x.nxv8i8.i64(<vscale x 8 x i8> poison, i8 85, i64 4)
+; CHECK-NEXT: ret <vscale x 8 x i8> [[A]]
+;
+ %a = call <vscale x 8 x i8> @llvm.riscv.vmv.v.x.nxv8i8(<vscale x 8 x i8> poison, i8 85, i64 4)
+ ret <vscale x 8 x i8> %a
+}
+
+declare void @use.nxv2i32(<vscale x 2 x i32>)
+declare void @use.nxv1i64(<vscale x 1 x i64>)
+
+define void @bitcast_users_type_mismatch() {
+; CHECK-LABEL: define void @bitcast_users_type_mismatch(
+; CHECK-SAME: ) #[[ATTR0]] {
+; CHECK-NEXT: [[VMV:%.*]] = call <vscale x 8 x i8> @llvm.riscv.vmv.v.x.nxv8i8.i64(<vscale x 8 x i8> poison, i8 85, i64 4)
+; CHECK-NEXT: [[CAST_1:%.*]] = bitcast <vscale x 8 x i8> [[VMV]] to <vscale x 2 x i32>
+; CHECK-NEXT: [[CAST_2:%.*]] = bitcast <vscale x 8 x i8> [[VMV]] to <vscale x 1 x i64>
+; CHECK-NEXT: call void @use.nxv2i32(<vscale x 2 x i32> [[CAST_1]])
+; CHECK-NEXT: call void @use.nxv1i64(<vscale x 1 x i64> [[CAST_2]])
+; CHECK-NEXT: ret void
+;
+ %vmv = call <vscale x 8 x i8> @llvm.riscv.vmv.v.x.nxv8i8(<vscale x 8 x i8> poison, i8 85, i64 4)
+ %cast.1 = bitcast <vscale x 8 x i8> %vmv to <vscale x 2 x i32>
+ %cast.2 = bitcast <vscale x 8 x i8> %vmv to <vscale x 1 x i64>
+ call void @use.nxv2i32(<vscale x 2 x i32> %cast.1)
+ call void @use.nxv1i64(<vscale x 1 x i64> %cast.2)
+ ret void
+}
+
+define <vscale x 1 x i64> @bitcast_target_elt_type_too_large() {
+; CHECK-LABEL: define <vscale x 1 x i64> @bitcast_target_elt_type_too_large(
+; CHECK-SAME: ) #[[ATTR0]] {
+; CHECK-NEXT: [[A:%.*]] = call <vscale x 8 x i8> @llvm.riscv.vmv.v.x.nxv8i8.i64(<vscale x 8 x i8> poison, i8 85, i64 4)
+; CHECK-NEXT: [[B:%.*]] = bitcast <vscale x 8 x i8> [[A]] to <vscale x 1 x i64>
+; CHECK-NEXT: ret <vscale x 1 x i64> [[B]]
+;
+ %a = call <vscale x 8 x i8> @llvm.riscv.vmv.v.x.nxv8i8(<vscale x 8 x i8> poison, i8 85, i64 4)
+ %b = bitcast <vscale x 8 x i8> %a to <vscale x 1 x i64>
+ ret <vscale x 1 x i64> %b
+}
+
+define <vscale x 64 x i1> @bitcast_target_elt_type_too_small() {
+; CHECK-LABEL: define <vscale x 64 x i1> @bitcast_target_elt_type_too_small(
+; CHECK-SAME: ) #[[ATTR0]] {
+; CHECK-NEXT: [[A:%.*]] = call <vscale x 8 x i8> @llvm.riscv.vmv.v.x.nxv8i8.i64(<vscale x 8 x i8> poison, i8 85, i64 4)
+; CHECK-NEXT: [[B:%.*]] = bitcast <vscale x 8 x i8> [[A]] to <vscale x 64 x i1>
+; CHECK-NEXT: ret <vscale x 64 x i1> [[B]]
+;
+ %a = call <vscale x 8 x i8> @llvm.riscv.vmv.v.x.nxv8i8(<vscale x 8 x i8> poison, i8 85, i64 4)
+ %b = bitcast <vscale x 8 x i8> %a to <vscale x 64 x i1>
+ ret <vscale x 64 x i1> %b
+}
+
+define <vscale x 2 x i32> @passthru_non_poison(<vscale x 8 x i8> %x) {
+; CHECK-LABEL: define <vscale x 2 x i32> @passthru_non_poison(
+; CHECK-SAME: <vscale x 8 x i8> [[X:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[A:%.*]] = call <vscale x 8 x i8> @llvm.riscv.vmv.v.x.nxv8i8.i64(<vscale x 8 x i8> [[X]], i8 85, i64 4)
+; CHECK-NEXT: [[B:%.*]] = bitcast <vscale x 8 x i8> [[A]] to <vscale x 2 x i32>
+; CHECK-NEXT: ret <vscale x 2 x i32> [[B]]
+;
+ %a = call <vscale x 8 x i8> @llvm.riscv.vmv.v.x.nxv8i8(<vscale x 8 x i8> %x, i8 85, i64 4)
+ %b = bitcast <vscale x 8 x i8> %a to <vscale x 2 x i32>
+ ret <vscale x 2 x i32> %b
+}
+
+define <vscale x 2 x i32> @scalar_non_constant(i8 %scalar) {
+; CHECK-LABEL: define <vscale x 2 x i32> @scalar_non_constant(
+; CHECK-SAME: i8 [[SCALAR:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[A:%.*]] = call <vscale x 8 x i8> @llvm.riscv.vmv.v.x.nxv8i8.i64(<vscale x 8 x i8> poison, i8 [[SCALAR]], i64 4)
+; CHECK-NEXT: [[B:%.*]] = bitcast <vscale x 8 x i8> [[A]] to <vscale x 2 x i32>
+; CHECK-NEXT: ret <vscale x 2 x i32> [[B]]
+;
+ %a = call <vscale x 8 x i8> @llvm.riscv.vmv.v.x.nxv8i8(<vscale x 8 x i8> poison, i8 %scalar, i64 4)
+ %b = bitcast <vscale x 8 x i8> %a to <vscale x 2 x i32>
+ ret <vscale x 2 x i32> %b
+}
+
+define <vscale x 2 x i32> @vl_non_constant(i64 %vl) {
+; CHECK-LABEL: define <vscale x 2 x i32> @vl_non_constant(
+; CHECK-SAME: i64 [[VL:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[A:%.*]] = call <vscale x 8 x i8> @llvm.riscv.vmv.v.x.nxv8i8.i64(<vscale x 8 x i8> poison, i8 85, i64 [[VL]])
+; CHECK-NEXT: [[B:%.*]] = bitcast <vscale x 8 x i8> [[A]] to <vscale x 2 x i32>
+; CHECK-NEXT: ret <vscale x 2 x i32> [[B]]
+;
+ %a = call <vscale x 8 x i8> @llvm.riscv.vmv.v.x.nxv8i8(<vscale x 8 x i8> poison, i8 85, i64 %vl)
+ %b = bitcast <vscale x 8 x i8> %a to <vscale x 2 x i32>
+ ret <vscale x 2 x i32> %b
+}
+
+define <vscale x 1 x i32> @vl_not_divisible() {
+; CHECK-LABEL: define <vscale x 1 x i32> @vl_not_divisible(
+; CHECK-SAME: ) #[[ATTR0]] {
+; CHECK-NEXT: [[A:%.*]] = call <vscale x 4 x i8> @llvm.riscv.vmv.v.x.nxv4i8.i64(<vscale x 4 x i8> poison, i8 85, i64 7)
+; CHECK-NEXT: [[B:%.*]] = bitcast <vscale x 4 x i8> [[A]] to <vscale x 1 x i32>
+; CHECK-NEXT: ret <vscale x 1 x i32> [[B]]
+;
+ %a = call <vscale x 4 x i8> @llvm.riscv.vmv.v.x.nxv8i8(<vscale x 4 x i8> poison, i8 85, i64 7)
+ %b = bitcast <vscale x 4 x i8> %a to <vscale x 1 x i32>
+ ret <vscale x 1 x i32> %b
+}
+
+define <vscale x 1 x i64> @vector_elt_type_legality() {
+; RV32-LABEL: define <vscale x 1 x i64> @vector_elt_type_legality(
+; RV32-SAME: ) #[[ATTR0]] {
+; RV32-NEXT: [[A:%.*]] = call <vscale x 8 x i8> @llvm.riscv.vmv.v.x.nxv8i8.i64(<vscale x 8 x i8> poison, i8 85, i64 8)
+; RV32-NEXT: [[B:%.*]] = bitcast <vscale x 8 x i8> [[A]] to <vscale x 1 x i64>
+; RV32-NEXT: ret <vscale x 1 x i64> [[B]]
+;
+; RV64-LABEL: define <vscale x 1 x i64> @vector_elt_type_legality(
+; RV64-SAME: ) #[[ATTR0]] {
+; RV64-NEXT: [[TMP1:%.*]] = call <vscale x 1 x i64> @llvm.riscv.vmv.v.x.nxv1i64.i64(<vscale x 1 x i64> poison, i64 6148914691236517205, i64 1)
+; RV64-NEXT: ret <vscale x 1 x i64> [[TMP1]]
+;
+; ZVE32X-LABEL: define <vscale x 1 x i64> @vector_elt_type_legality(
+; ZVE32X-SAME: ) #[[ATTR0]] {
+; ZVE32X-NEXT: [[A:%.*]] = call <vscale x 8 x i8> @llvm.riscv.vmv.v.x.nxv8i8.i64(<vscale x 8 x i8> poison, i8 85, i64 8)
+; ZVE32X-NEXT: [[B:%.*]] = bitcast <vscale x 8 x i8> [[A]] to <vscale x 1 x i64>
+; ZVE32X-NEXT: ret <vscale x 1 x i64> [[B]]
+;
+ %a = call <vscale x 8 x i8> @llvm.riscv.vmv.v.x.nxv8i8(<vscale x 8 x i8> poison, i8 85, i64 8)
+ %b = bitcast <vscale x 8 x i8> %a to <vscale x 1 x i64>
+ ret <vscale x 1 x i64> %b
+}
More information about the llvm-commits
mailing list