aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/CodeGen/GlobalISel/PatternMatchTest.cpp
diff options
context:
space:
mode:
authorJessica Paquette <jpaquette@apple.com>2022-10-07 12:19:33 -0700
committerJessica Paquette <jpaquette@apple.com>2022-10-07 20:06:13 -0700
commit45b9c6b01f263d087a456b098b36e9ee90c607b2 (patch)
tree1fb109f9916afd1a5fec590b2ee3819263858005 /llvm/unittests/CodeGen/GlobalISel/PatternMatchTest.cpp
parent9e80add2cfa9bb03bbe77cca9adec18137204538 (diff)
downloadllvm-45b9c6b01f263d087a456b098b36e9ee90c607b2.zip
llvm-45b9c6b01f263d087a456b098b36e9ee90c607b2.tar.gz
llvm-45b9c6b01f263d087a456b098b36e9ee90c607b2.tar.bz2
[GlobalISel] Add commutative matchers for compares.
This adds: * `m_c_GICmp` * `m_c_GFCmp` These work the same way as the standard matchers, but will also try to commute the LHS and RHS of a compare to get a match. E.g. ``` m_c_GICmp(m_Pred(...), m_GAdd(...), m_GSub(...)) ``` Can match either of ``` icmp cc (add x, y), (sub a, b) icmp swapped_cc (sub a, b), (add x, y) ``` Differential Revision: https://reviews.llvm.org/D135415
Diffstat (limited to 'llvm/unittests/CodeGen/GlobalISel/PatternMatchTest.cpp')
-rw-r--r--llvm/unittests/CodeGen/GlobalISel/PatternMatchTest.cpp58
1 files changed, 58 insertions, 0 deletions
diff --git a/llvm/unittests/CodeGen/GlobalISel/PatternMatchTest.cpp b/llvm/unittests/CodeGen/GlobalISel/PatternMatchTest.cpp
index 5821957..9ed72c9 100644
--- a/llvm/unittests/CodeGen/GlobalISel/PatternMatchTest.cpp
+++ b/llvm/unittests/CodeGen/GlobalISel/PatternMatchTest.cpp
@@ -323,6 +323,64 @@ TEST_F(AArch64GISelMITest, MatchFCmp) {
EXPECT_EQ(Copies[1], Reg1);
}
+TEST_F(AArch64GISelMITest, MatcCommutativeICmp) {
+ setUp();
+ if (!TM)
+ return;
+ const LLT s1 = LLT::scalar(1);
+ Register LHS = Copies[0];
+ Register RHS = Copies[1];
+ CmpInst::Predicate MatchedPred;
+ bool Match = false;
+ for (unsigned P = CmpInst::Predicate::FIRST_ICMP_PREDICATE;
+ P < CmpInst::Predicate::LAST_ICMP_PREDICATE; ++P) {
+ auto CurrPred = static_cast<CmpInst::Predicate>(P);
+ auto Cmp = B.buildICmp(CurrPred, s1, LHS, RHS);
+ // Basic matching.
+ Match = mi_match(
+ Cmp.getReg(0), *MRI,
+ m_c_GICmp(m_Pred(MatchedPred), m_SpecificReg(LHS), m_SpecificReg(RHS)));
+ EXPECT_TRUE(Match);
+ EXPECT_EQ(MatchedPred, CurrPred);
+ // Commuting operands should still match, but the predicate should be
+ // swapped.
+ Match = mi_match(
+ Cmp.getReg(0), *MRI,
+ m_c_GICmp(m_Pred(MatchedPred), m_SpecificReg(RHS), m_SpecificReg(LHS)));
+ EXPECT_TRUE(Match);
+ EXPECT_EQ(MatchedPred, CmpInst::getSwappedPredicate(CurrPred));
+ }
+}
+
+TEST_F(AArch64GISelMITest, MatcCommutativeFCmp) {
+ setUp();
+ if (!TM)
+ return;
+ const LLT s1 = LLT::scalar(1);
+ Register LHS = Copies[0];
+ Register RHS = Copies[1];
+ CmpInst::Predicate MatchedPred;
+ bool Match = false;
+ for (unsigned P = CmpInst::Predicate::FIRST_FCMP_PREDICATE;
+ P < CmpInst::Predicate::LAST_FCMP_PREDICATE; ++P) {
+ auto CurrPred = static_cast<CmpInst::Predicate>(P);
+ auto Cmp = B.buildFCmp(CurrPred, s1, LHS, RHS);
+ // Basic matching.
+ Match = mi_match(
+ Cmp.getReg(0), *MRI,
+ m_c_GFCmp(m_Pred(MatchedPred), m_SpecificReg(LHS), m_SpecificReg(RHS)));
+ EXPECT_TRUE(Match);
+ EXPECT_EQ(MatchedPred, CurrPred);
+ // Commuting operands should still match, but the predicate should be
+ // swapped.
+ Match = mi_match(
+ Cmp.getReg(0), *MRI,
+ m_c_GFCmp(m_Pred(MatchedPred), m_SpecificReg(RHS), m_SpecificReg(LHS)));
+ EXPECT_TRUE(Match);
+ EXPECT_EQ(MatchedPred, CmpInst::getSwappedPredicate(CurrPred));
+ }
+}
+
TEST_F(AArch64GISelMITest, MatchFPUnaryOp) {
setUp();
if (!TM)