[llvm] [LoongArch] Optimize extractelement containing variable index (PR #151475)

via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 3 18:42:55 PDT 2025


================
@@ -2614,21 +2614,71 @@ LoongArchTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
   SDValue Idx = Op->getOperand(1);
   unsigned NumElts = VecTy.getVectorNumElements();
   SDLoc DL(Op);
+  MVT GRLenVT = Subtarget.getGRLenVT();
 
   assert(VecTy.is256BitVector() && "Unexpected EXTRACT_VECTOR_ELT vector type");
 
   if (isa<ConstantSDNode>(Idx) && Idx->getAsZExtVal() < NumElts)
     return Op;
 
-  // TODO: Deal with other legal 256-bits vector types?
-  if (!isa<ConstantSDNode>(Idx) &&
-      (VecTy == MVT::v8i32 || VecTy == MVT::v8f32)) {
-    SDValue SplatIdx = DAG.getSplatBuildVector(MVT::v8i32, DL, Idx);
-    SDValue SplatValue =
-        DAG.getNode(LoongArchISD::XVPERM, DL, VecTy, Vec, SplatIdx);
-
-    return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, SplatValue,
-                       DAG.getConstant(0, DL, Subtarget.getGRLenVT()));
+  if (!isa<ConstantSDNode>(Idx)) {
----------------
zhaoqi5 wrote:

Good catch. Thanks.

It seems that `Idx->getAsZExtVal() < NumElts` can also be deleted. I have tried that if the constant idx is greater than or equal to `NumElts`, initial DAG will use undef as its idx.

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


More information about the llvm-commits mailing list