Skip to content

Commit a1b9736

Browse files
committed
[PatternMatch] Add m_c_DisjointOr (NFC)
Add commutative variant of m_DisjointOr.
1 parent e8ce188 commit a1b9736

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

llvm/include/llvm/IR/PatternMatch.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ inline SpecificBinaryOp_match<LHS, RHS> m_BinOp(unsigned Opcode, const LHS &L,
12391239
return SpecificBinaryOp_match<LHS, RHS>(Opcode, L, R);
12401240
}
12411241

1242-
template <typename LHS, typename RHS>
1242+
template <typename LHS, typename RHS, bool Commutable = false>
12431243
struct DisjointOr_match {
12441244
LHS L;
12451245
RHS R;
@@ -1251,7 +1251,9 @@ struct DisjointOr_match {
12511251
assert(PDI->getOpcode() == Instruction::Or && "Only or can be disjoint");
12521252
if (!PDI->isDisjoint())
12531253
return false;
1254-
return L.match(PDI->getOperand(0)) && R.match(PDI->getOperand(1));
1254+
return (L.match(PDI->getOperand(0)) && R.match(PDI->getOperand(1))) ||
1255+
(Commutable && L.match(PDI->getOperand(1)) &&
1256+
R.match(PDI->getOperand(0)));
12551257
}
12561258
return false;
12571259
}
@@ -1262,6 +1264,12 @@ inline DisjointOr_match<LHS, RHS> m_DisjointOr(const LHS &L, const RHS &R) {
12621264
return DisjointOr_match<LHS, RHS>(L, R);
12631265
}
12641266

1267+
template <typename LHS, typename RHS>
1268+
inline DisjointOr_match<LHS, RHS, true> m_c_DisjointOr(const LHS &L,
1269+
const RHS &R) {
1270+
return DisjointOr_match<LHS, RHS, true>(L, R);
1271+
}
1272+
12651273
//===----------------------------------------------------------------------===//
12661274
// Class that matches a group of binary opcodes.
12671275
//

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3393,9 +3393,8 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
33933393

33943394
// If the operands have no common bits set:
33953395
// or (mul X, Y), X --> add (mul X, Y), X --> mul X, (Y + 1)
3396-
if (match(&I,
3397-
m_c_Or(m_OneUse(m_Mul(m_Value(X), m_Value(Y))), m_Deferred(X))) &&
3398-
cast<PossiblyDisjointInst>(I).isDisjoint()) {
3396+
if (match(&I, m_c_DisjointOr(m_OneUse(m_Mul(m_Value(X), m_Value(Y))),
3397+
m_Deferred(X)))) {
33993398
Value *IncrementY = Builder.CreateAdd(Y, ConstantInt::get(Ty, 1));
34003399
return BinaryOperator::CreateMul(X, IncrementY);
34013400
}

0 commit comments

Comments
 (0)