Skip to content

Commit 5c63b24

Browse files
author
Jessica Paquette
committed
[GlobalISel] Add a m_SpecificReg matcher
Similar to the specific matchers for constants. The intention here is to make it easier to write combines which check if a specific register is used more than once. e.g. matching patterns like: ``` (X + Y) == Y ``` Differential Revision: https://reviews.llvm.org/D135378
1 parent a5f5d72 commit 5c63b24

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,20 @@ inline SpecificConstantMatch m_AllOnesInt() {
237237
}
238238
///}
239239

240+
/// Matcher for a specific register.
241+
struct SpecificRegisterMatch {
242+
Register RequestedReg;
243+
SpecificRegisterMatch(Register RequestedReg) : RequestedReg(RequestedReg) {}
244+
bool match(const MachineRegisterInfo &MRI, Register Reg) {
245+
return Reg == RequestedReg;
246+
}
247+
};
248+
249+
/// Matches a register only if it is equal to \p RequestedReg.
250+
inline SpecificRegisterMatch m_SpecificReg(Register RequestedReg) {
251+
return SpecificRegisterMatch(RequestedReg);
252+
}
253+
240254
// TODO: Rework this for different kinds of MachineOperand.
241255
// Currently assumes the Src for a match is a register.
242256
// We might want to support taking in some MachineOperands and call getReg on

llvm/unittests/CodeGen/GlobalISel/PatternMatchTest.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,23 @@ TEST_F(AArch64GISelMITest, MatchNot) {
769769
EXPECT_TRUE(mi_match(AddInst.getReg(2), *MRI, m_Not(m_Reg(NotReg))));
770770
EXPECT_EQ(NotReg, Copies[0]);
771771
}
772+
773+
TEST_F(AArch64GISelMITest, MatchSpecificReg) {
774+
setUp();
775+
if (!TM)
776+
return;
777+
auto Cst1 = B.buildConstant(LLT::scalar(64), 42);
778+
auto Cst2 = B.buildConstant(LLT::scalar(64), 314);
779+
Register Reg = Cst1.getReg(0);
780+
// Basic case: Same register twice.
781+
EXPECT_TRUE(mi_match(Reg, *MRI, m_SpecificReg(Reg)));
782+
// Basic case: Two explicitly different registers.
783+
EXPECT_FALSE(mi_match(Reg, *MRI, m_SpecificReg(Cst2.getReg(0))));
784+
// Check that we can tell that an instruction uses a specific register.
785+
auto Add = B.buildAdd(LLT::scalar(64), Cst1, Cst2);
786+
EXPECT_TRUE(mi_match(Add.getReg(0), *MRI, m_GAdd(m_SpecificReg(Reg), m_Reg())));
787+
}
788+
772789
} // namespace
773790

774791
int main(int argc, char **argv) {

0 commit comments

Comments
 (0)