[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());
+    for (size_t I = 0; I < Leaves.size(); I += 1) {
+      SmallVector<bool> MatchResults;
+      std::apply(
+          [&](auto &...P) {
+            (MatchResults.emplace_back(sd_context_match(Leaves[I], Ctx, P)),
+             ...);
+          },
+          Patterns);
+      for (size_t J = 0; J < MatchResults.size(); J += 1) {
+        if (MatchResults[J]) {
+          Matches[I].emplace_back(J);
+        }
+      }
+    }
+
+    SmallVector<bool> Used(std::tuple_size_v<std::tuple<PatternTs...>>, false);
----------------
RKSimon wrote:

BitVector / SmallBitVector?

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


More information about the llvm-commits mailing list