[llvm] [RISCV] Improve performCONCAT_VECTORCombine stride matching (PR #68726)

Michael Maitland via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 10 17:44:08 PDT 2023


================
@@ -13821,6 +13822,56 @@ static SDValue performCONCAT_VECTORSCombine(SDNode *N, SelectionDAG &DAG,
     Align = std::min(Align, Ld->getAlign());
   }
 
+  // If the load ptrs can be decomposed into a common (Base + Index) with a
+  // common constant stride, then return the constant stride. This matcher
+  // enables some additional optimization since BaseIndexOffset is capable of
+  // decomposing the load ptrs to (add (add Base, Index), Stride) instead of
+  // (add LastPtr, Stride) or (add NextPtr, Stride) that matchForwardStrided and
+  // matchReverseStrided use respectively.
+  auto matchConstantStride = [&DAG, &N](ArrayRef<SDUse> Loads) {
+    // Initialize match constraints based on the first load. Initialize
+    // ConstStride by taking the difference between the offset of the first two
+    // loads.
+    if (Loads.size() < 2)
+      return SDValue();
+    BaseIndexOffset BaseLdBIO =
----------------
michaelmaitland wrote:

> I realized we could factor out a helper routine of the form optional<int64_t> getPtrDiff(LD1, LD2). Then we could loop over all of the indices, and check that the offset is known and the same.

This seems to be very close to what BaseIndexOffset match does since if the (Base+Index) matches then the ptr diff is Offset. We could definitely add this helper method to simplify our checks in matchConstantStride.

 > After that, it occurred to me that both of the previous two matchers can also be written as rules inside a getPtrDiff helper. (The return value has to become SDValue instead, but otherwise it works.)

What happens in the case where there is no constant stride in matchFoward and matchReverse (i.e. stride can be a common add SDValue that is not a SDConstant).

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


More information about the llvm-commits mailing list