[llvm] [LoongArch] Optimize insertelement containing variable index using compare+select (PR #151131)

Lu Weining via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 29 19:25:21 PDT 2025


================
@@ -2621,8 +2621,40 @@ LoongArchTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
 SDValue
 LoongArchTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
                                                 SelectionDAG &DAG) const {
-  if (isa<ConstantSDNode>(Op->getOperand(2)))
+  MVT VT = Op.getSimpleValueType();
+  MVT EltVT = VT.getVectorElementType();
+  unsigned NumElts = VT.getVectorNumElements();
+  unsigned EltSizeInBits = EltVT.getScalarSizeInBits();
+  SDLoc DL(Op);
+  SDValue Op0 = Op.getOperand(0);
+  SDValue Op1 = Op.getOperand(1);
+  SDValue Op2 = Op.getOperand(2);
+
+  if (isa<ConstantSDNode>(Op2)) {
     return Op;
+  } else {
+    MVT IdxTy = MVT::getIntegerVT(EltSizeInBits);
+    MVT IdxVTy = MVT::getVectorVT(IdxTy, NumElts);
+
+    if (!isTypeLegal(VT) || !isTypeLegal(IdxVTy))
+      return SDValue();
+
+    SDValue SplatElt = DAG.getSplatBuildVector(VT, DL, Op1);
+    SDValue SplatIdx = DAG.getSplatBuildVector(IdxVTy, DL, Op2);
+
+    SmallVector<SDValue, 32> RawIndices;
+    for (unsigned i = 0; i < NumElts; ++i)
+      RawIndices.push_back(DAG.getConstant(i, DL, Subtarget.getGRLenVT()));
----------------
SixWeining wrote:

How about loongarch32 if we use `Subtarget.getGRLenVT()` when the vector is v{2,4}i64 or v{2,4}f64?

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


More information about the llvm-commits mailing list