Skip to content

Commit f6a7038

Browse files
committed
[DAG] Fold (and X, (bswap/bitreverse (not Y))) -> (and X, (not (bswap/bitreverse Y))) on ANDNOT capable targets
Fixes #112425
1 parent 7da0a69 commit f6a7038

File tree

2 files changed

+303
-151
lines changed

2 files changed

+303
-151
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7350,6 +7350,21 @@ SDValue DAGCombiner::visitAND(SDNode *N) {
73507350
if (SDValue R = foldLogicOfShifts(N, N1, N0, DAG))
73517351
return R;
73527352

7353+
// If the target supports ANDNOT, attempt to reconstruct an ANDNOT pattern
7354+
// that might have become separated by a bitwise-agnostic instruction.
7355+
if (TLI.hasAndNot(SDValue(N, 0))) {
7356+
SDValue X, Y;
7357+
7358+
// Fold (and X, (bswap (not Y))) -> (and X, (not (bswap Y)))
7359+
// Fold (and X, (bitreverse (not Y))) -> (and X, (not (bitreverse Y)))
7360+
for (unsigned Opc : {ISD::BSWAP, ISD::BITREVERSE})
7361+
if (sd_match(N, m_And(m_Value(X),
7362+
m_OneUse(m_UnaryOp(Opc, m_Not(m_Value(Y)))))) &&
7363+
!sd_match(X, m_Not(m_Value())))
7364+
return DAG.getNode(ISD::AND, DL, VT, X,
7365+
DAG.getNOT(DL, DAG.getNode(Opc, DL, VT, Y), VT));
7366+
}
7367+
73537368
// Masking the negated extension of a boolean is just the zero-extended
73547369
// boolean:
73557370
// and (sub 0, zext(bool X)), 1 --> zext(bool X)

0 commit comments

Comments
 (0)