[llvm] [RISCV] Cost @llvm.vector.{extract, insert} as free at index 0 (PR #81818)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 16 09:53:01 PST 2024


================
@@ -809,6 +809,34 @@ RISCVTTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
     }
     break;
   }
+  case Intrinsic::vector_extract: {
+    // A vector extract at index 0 is a (free) subregister extract.
+    if (auto *CIdx = dyn_cast<ConstantInt>(ICA.getArgs()[1]);
+        CIdx && CIdx->isZero())
+      return TTI::TCC_Free;
+    break;
+  }
+  case Intrinsic::vector_insert: {
+    auto FitsSubreg = [this](Type *Ty) {
+      if (!isa<ScalableVectorType>(Ty))
+        return false;
+      // Any scalable vector LMUL >= 1 will fit exactly into a register group.
+      auto [_Cost, LT] = getTypeLegalizationCost(Ty);
+      auto [_Coeff, Fractional] =
+          RISCVVType::decodeVLMUL(RISCVTargetLowering::getLMUL(LT));
+      return !Fractional;
+    };
+
+    // A vector insert at index 0 is a (free) subregister insert if:
+    //
+    // - The subvec fits exactly into a register group or
+    // - The vector is undef
+    if (auto *CIdx = dyn_cast<ConstantInt>(ICA.getArgs()[2]);
+        CIdx && CIdx->isZero() &&
+        (FitsSubreg(ICA.getArgTypes()[1]) || isa<UndefValue>(ICA.getArgs()[0])))
----------------
lukel97 wrote:

I believe we check if it's the vector that's undef, not the subvector, because if the vector is undef and the index is 0 we can just "replace" the entire vector with the subvector:

https://github.com/llvm/llvm-project/blob/f01ed3bc8884223bf3edbaad8d3685622444cbf5/llvm/lib/Target/RISCV/RISCVISelLowering.cpp#L9655-L9668

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


More information about the llvm-commits mailing list