[llvm] [DAG] SDPatternMatch - add matchers for reassociatable binops (PR #119985)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 6 06:54:14 PST 2025


================
@@ -1072,6 +1072,94 @@ inline BinaryOpc_match<ValTy, AllOnes_match, true> m_Not(const ValTy &V) {
   return m_Xor(V, m_AllOnes());
 }
 
+template <typename... PatternTs> struct ReassociatableOpc_match {
+  unsigned Opcode;
+  std::tuple<PatternTs...> Patterns;
+
+  ReassociatableOpc_match(unsigned Opcode, const PatternTs &...Patterns)
+      : Opcode(Opcode), Patterns(Patterns...) {}
+
+  template <typename MatchContext>
+  bool match(const MatchContext &Ctx, SDValue N) {
+    SmallVector<SDValue> Leaves;
+    collectLeaves(N, Leaves);
+    if (Leaves.size() != std::tuple_size_v<std::tuple<PatternTs...>>) {
+      return false;
+    }
+
+    // J in Matches[I] iff sd_context_match(Leaves[I], Ctx,
+    // std::get<J>(Patterns)) == true
+    SmallVector<SmallVector<size_t>> Matches(Leaves.size());
----------------
RKSimon wrote:

SmallVector can be rather heavy - can we use a std::array<SmallVector<size_t>, std::tuple_size_v<std::tuple<PatternTs...>>> here? Try to move as much to compile time as possible.

Could we use BitVector / APInt instead of SmallVector<size_t> as well?

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


More information about the llvm-commits mailing list