Index: llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp =================================================================== --- llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp +++ llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp @@ -4539,11 +4539,28 @@ return false; } +static bool matchNoCommonBitsPattern(SDValue A, SDValue B) { + if (isBitwiseNot(A, true)) { + SDValue NotOperand = A->getOperand(0); + assert(B->getOpcode() == ISD::AND && "Expected AND opcode!"); + if (NotOperand == B->getOperand(0) || NotOperand == B->getOperand(1)) + return true; + } + return false; +} + // FIXME: unify with llvm::haveNoCommonBitsSet. -// FIXME: could also handle masked merge pattern (X & ~M) op (Y & M) bool SelectionDAG::haveNoCommonBitsSet(SDValue A, SDValue B) const { assert(A.getValueType() == B.getValueType() && "Values must have the same type"); + // Match masked merge pattern (X & ~M) op (Y & M) + if (A->getOpcode() == ISD::AND && B->getOpcode() == ISD::AND) { + if (matchNoCommonBitsPattern(A->getOperand(0), B) || + matchNoCommonBitsPattern(A->getOperand(1), B) || + matchNoCommonBitsPattern(B->getOperand(0), A) || + matchNoCommonBitsPattern(B->getOperand(1), A)) + return true; + } return KnownBits::haveNoCommonBitsSet(computeKnownBits(A), computeKnownBits(B)); }