[PATCH] D99735: [MIPatternMatch]: Add mi_match for MachineInstr

Petar Avramovic via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 27 02:28:57 PDT 2021


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG39662abf720f: [MIPatternMatch]: Add mi_match for MachineInstr (authored by Petar.Avramovic).

Changed prior to commit:
  https://reviews.llvm.org/D99735?vs=336073&id=340764#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D99735/new/

https://reviews.llvm.org/D99735

Files:
  llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h
  llvm/unittests/CodeGen/GlobalISel/PatternMatchTest.cpp


Index: llvm/unittests/CodeGen/GlobalISel/PatternMatchTest.cpp
===================================================================
--- llvm/unittests/CodeGen/GlobalISel/PatternMatchTest.cpp
+++ llvm/unittests/CodeGen/GlobalISel/PatternMatchTest.cpp
@@ -51,6 +51,37 @@
   EXPECT_EQ(Src0, MIBCst.getReg(0));
 }
 
+TEST_F(AArch64GISelMITest, MachineInstrPtrBind) {
+  setUp();
+  if (!TM)
+    return;
+  auto MIBAdd = B.buildAdd(LLT::scalar(64), Copies[0], Copies[1]);
+  // Test 'MachineInstr *' bind.
+  // Default mi_match.
+  MachineInstr *MIPtr = MIBAdd.getInstr();
+  bool match = mi_match(MIPtr, *MRI, m_GAdd(m_Reg(), m_Reg()));
+  EXPECT_TRUE(match);
+  // Specialized mi_match for MachineInstr &.
+  MachineInstr &MI = *MIBAdd.getInstr();
+  match = mi_match(MI, *MRI, m_GAdd(m_Reg(), m_Reg()));
+  EXPECT_TRUE(match);
+  // MachineInstrBuilder has automatic conversion to MachineInstr *.
+  match = mi_match(MIBAdd, *MRI, m_GAdd(m_Reg(), m_Reg()));
+  EXPECT_TRUE(match);
+  // Match instruction without def.
+  auto MIBBrcond = B.buildBrCond(Copies[0], B.getMBB());
+  MachineInstr *MatchedMI;
+  match = mi_match(MIBBrcond, *MRI, m_MInstr(MatchedMI));
+  EXPECT_TRUE(match);
+  EXPECT_TRUE(MIBBrcond.getInstr() == MatchedMI);
+  // Match instruction with two defs.
+  auto MIBUAddO =
+      B.buildUAddo(LLT::scalar(64), LLT::scalar(1), Copies[0], Copies[1]);
+  match = mi_match(MIBUAddO, *MRI, m_MInstr(MatchedMI));
+  EXPECT_TRUE(match);
+  EXPECT_TRUE(MIBUAddO.getInstr() == MatchedMI);
+}
+
 TEST_F(AArch64GISelMITest, MatchBinaryOp) {
   setUp();
   if (!TM)
Index: llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h
===================================================================
--- llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h
+++ llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h
@@ -25,6 +25,11 @@
   return P.match(MRI, R);
 }
 
+template <typename Pattern>
+bool mi_match(MachineInstr &MI, const MachineRegisterInfo &MRI, Pattern &&P) {
+  return P.match(MRI, &MI);
+}
+
 // TODO: Extend for N use.
 template <typename SubPatternT> struct OneUse_match {
   SubPatternT SubPat;
@@ -182,6 +187,11 @@
       return true;
     return false;
   }
+  static bool bind(const MachineRegisterInfo &MRI, MachineInstr *&MI,
+                   MachineInstr *Inst) {
+    MI = Inst;
+    return MI;
+  }
 };
 
 template <> struct bind_helper<LLT> {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99735.340764.patch
Type: text/x-patch
Size: 2379 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210427/f08631bf/attachment.bin>


More information about the llvm-commits mailing list