Skip to content

Commit

Permalink
[MIPatternMatch]: Add mi_match for MachineInstr
Browse files Browse the repository at this point in the history
This utility allows more efficient start of pattern match.
Often MachineInstr(MI) is available and instead of using
mi_match(MI.getOperand(0).getReg(), MRI, ...) followed by
MRI.getVRegDef(Reg) that gives back MI we now use
mi_match(MI, MRI, ...).

Differential Revision: https://reviews.llvm.org/D99735
  • Loading branch information
petar-avramovic committed Apr 27, 2021
1 parent ebe408a commit 39662ab
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
10 changes: 10 additions & 0 deletions llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ bool mi_match(Reg R, const MachineRegisterInfo &MRI, Pattern &&P) {
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;
Expand Down Expand Up @@ -182,6 +187,11 @@ template <> struct bind_helper<MachineInstr *> {
return true;
return false;
}
static bool bind(const MachineRegisterInfo &MRI, MachineInstr *&MI,
MachineInstr *Inst) {
MI = Inst;
return MI;
}
};

template <> struct bind_helper<LLT> {
Expand Down
31 changes: 31 additions & 0 deletions llvm/unittests/CodeGen/GlobalISel/PatternMatchTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,37 @@ TEST_F(AArch64GISelMITest, MatchIntConstantRegister) {
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)
Expand Down

0 comments on commit 39662ab

Please sign in to comment.