Skip to content

Commit 8edb12f

Browse files
committed
[IR] Add m_c_BitwiseLogic in pattern match; NFC
Just a missing matcher that came up in #73362 Closes #86632
1 parent 1c9d5c2 commit 8edb12f

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

llvm/include/llvm/IR/PatternMatch.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,7 +1357,8 @@ m_NUWAddLike(const LHS &L, const RHS &R) {
13571357
//===----------------------------------------------------------------------===//
13581358
// Class that matches a group of binary opcodes.
13591359
//
1360-
template <typename LHS_t, typename RHS_t, typename Predicate>
1360+
template <typename LHS_t, typename RHS_t, typename Predicate,
1361+
bool Commutable = false>
13611362
struct BinOpPred_match : Predicate {
13621363
LHS_t L;
13631364
RHS_t R;
@@ -1366,8 +1367,10 @@ struct BinOpPred_match : Predicate {
13661367

13671368
template <typename OpTy> bool match(OpTy *V) {
13681369
if (auto *I = dyn_cast<Instruction>(V))
1369-
return this->isOpType(I->getOpcode()) && L.match(I->getOperand(0)) &&
1370-
R.match(I->getOperand(1));
1370+
return this->isOpType(I->getOpcode()) &&
1371+
((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1372+
(Commutable && L.match(I->getOperand(1)) &&
1373+
R.match(I->getOperand(0))));
13711374
return false;
13721375
}
13731376
};
@@ -1434,6 +1437,13 @@ m_BitwiseLogic(const LHS &L, const RHS &R) {
14341437
return BinOpPred_match<LHS, RHS, is_bitwiselogic_op>(L, R);
14351438
}
14361439

1440+
/// Matches bitwise logic operations in either order.
1441+
template <typename LHS, typename RHS>
1442+
inline BinOpPred_match<LHS, RHS, is_bitwiselogic_op, true>
1443+
m_c_BitwiseLogic(const LHS &L, const RHS &R) {
1444+
return BinOpPred_match<LHS, RHS, is_bitwiselogic_op, true>(L, R);
1445+
}
1446+
14371447
/// Matches integer division operations.
14381448
template <typename LHS, typename RHS>
14391449
inline BinOpPred_match<LHS, RHS, is_idiv_op> m_IDiv(const LHS &L,

llvm/unittests/IR/PatternMatch.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,45 @@ TEST_F(PatternMatchTest, Unless) {
494494
EXPECT_FALSE(m_Unless(m_c_Add(m_Zero(), m_One())).match(X));
495495
}
496496

497+
TEST_F(PatternMatchTest, BitWise) {
498+
Value *Or = IRB.CreateOr(IRB.getInt32(1), IRB.getInt32(0));
499+
Value *Xor = IRB.CreateXor(IRB.getInt32(1), IRB.getInt32(0));
500+
Value *And = IRB.CreateXor(IRB.getInt32(1), IRB.getInt32(0));
501+
Constant *T = IRB.getInt1(true);
502+
Constant *F = IRB.getInt1(false);
503+
Value *Alloca = IRB.CreateAlloca(IRB.getInt1Ty());
504+
Value *X = IRB.CreateLoad(IRB.getInt1Ty(), Alloca);
505+
Value *Y = IRB.CreateLoad(IRB.getInt1Ty(), Alloca);
506+
Value *LAnd = IRB.CreateSelect(X, Y, F);
507+
Value *LOr = IRB.CreateSelect(X, T, Y);
508+
Value *Add = IRB.CreateAdd(IRB.getInt32(1), IRB.getInt32(0));
509+
510+
EXPECT_TRUE(m_BitwiseLogic(m_One(), m_Zero()).match(Or));
511+
EXPECT_TRUE(m_BitwiseLogic(m_One(), m_Zero()).match(Xor));
512+
EXPECT_TRUE(m_BitwiseLogic(m_One(), m_Zero()).match(And));
513+
EXPECT_FALSE(m_BitwiseLogic(m_Value(), m_Value()).match(LAnd));
514+
EXPECT_FALSE(m_BitwiseLogic(m_Value(), m_Value()).match(LOr));
515+
EXPECT_FALSE(m_BitwiseLogic(m_Value(), m_Value()).match(Add));
516+
517+
EXPECT_FALSE(m_BitwiseLogic(m_Zero(), m_One()).match(Or));
518+
EXPECT_FALSE(m_BitwiseLogic(m_Zero(), m_One()).match(Xor));
519+
EXPECT_FALSE(m_BitwiseLogic(m_Zero(), m_One()).match(And));
520+
521+
EXPECT_TRUE(m_c_BitwiseLogic(m_One(), m_Zero()).match(Or));
522+
EXPECT_TRUE(m_c_BitwiseLogic(m_One(), m_Zero()).match(Xor));
523+
EXPECT_TRUE(m_c_BitwiseLogic(m_One(), m_Zero()).match(And));
524+
EXPECT_FALSE(m_c_BitwiseLogic(m_Value(), m_Value()).match(LAnd));
525+
EXPECT_FALSE(m_c_BitwiseLogic(m_Value(), m_Value()).match(LOr));
526+
EXPECT_FALSE(m_c_BitwiseLogic(m_Value(), m_Value()).match(Add));
527+
528+
EXPECT_TRUE(m_c_BitwiseLogic(m_Zero(), m_One()).match(Or));
529+
EXPECT_TRUE(m_c_BitwiseLogic(m_Zero(), m_One()).match(Xor));
530+
EXPECT_TRUE(m_c_BitwiseLogic(m_Zero(), m_One()).match(And));
531+
532+
EXPECT_FALSE(m_c_BitwiseLogic(m_One(), m_One()).match(Or));
533+
EXPECT_FALSE(m_c_BitwiseLogic(m_Zero(), m_Zero()).match(Xor));
534+
}
535+
497536
TEST_F(PatternMatchTest, ZExtSExtSelf) {
498537
LLVMContext &Ctx = IRB.getContext();
499538

0 commit comments

Comments
 (0)