Index: lib/CodeGen/SelectionDAG/DAGCombiner.cpp =================================================================== --- lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -5380,11 +5380,11 @@ auto matchAndXor = [&X, &Y, &M](SDValue And, unsigned XorIdx, SDValue Other) { if (And.getOpcode() != ISD::AND || !And.hasOneUse()) return false; - if (And.getOperand(XorIdx).getOpcode() != ISD::XOR || - !And.getOperand(XorIdx).hasOneUse()) + SDValue Xor = And.getOperand(XorIdx); + if (Xor.getOpcode() != ISD::XOR || !Xor.hasOneUse()) return false; - SDValue Xor0 = And.getOperand(XorIdx).getOperand(0); - SDValue Xor1 = And.getOperand(XorIdx).getOperand(1); + SDValue Xor0 = Xor.getOperand(0); + SDValue Xor1 = Xor.getOperand(1); if (Other == Xor0) std::swap(Xor0, Xor1); if (Other != Xor1) @@ -5395,6 +5395,11 @@ return true; }; + // Don't do anything if the second operand of xor is a constant. + // It can only be a second operand because of canonicalization. + if (isa(N->getOperand(1).getNode())) + return SDValue(); + SDValue A = N->getOperand(0); SDValue B = N->getOperand(1); if (!matchAndXor(A, 0, B) && !matchAndXor(A, 1, B) && !matchAndXor(B, 0, A) && Index: test/CodeGen/AArch64/unfold-masked-merge-scalar-variablemask.ll =================================================================== --- test/CodeGen/AArch64/unfold-masked-merge-scalar-variablemask.ll +++ test/CodeGen/AArch64/unfold-masked-merge-scalar-variablemask.ll @@ -347,8 +347,8 @@ define i32 @in_constant_varx_mone(i32 %x, i32 %y, i32 %mask) { ; CHECK-LABEL: in_constant_varx_mone: ; CHECK: // %bb.0: -; CHECK-NEXT: and w8, w0, w2 -; CHECK-NEXT: orn w0, w8, w2 +; CHECK-NEXT: bic w8, w2, w0 +; CHECK-NEXT: mvn w0, w8 ; CHECK-NEXT: ret %n0 = xor i32 %x, -1 ; %x %n1 = and i32 %n0, %mask @@ -370,8 +370,9 @@ define i32 @in_constant_varx_mone_invmask(i32 %x, i32 %y, i32 %mask) { ; CHECK-LABEL: in_constant_varx_mone_invmask: ; CHECK: // %bb.0: -; CHECK-NEXT: bic w8, w0, w2 -; CHECK-NEXT: orr w0, w8, w2 +; CHECK-NEXT: mvn w8, w0 +; CHECK-NEXT: bic w8, w8, w2 +; CHECK-NEXT: mvn w0, w8 ; CHECK-NEXT: ret %notmask = xor i32 %mask, -1 %n0 = xor i32 %x, -1 ; %x @@ -397,9 +398,9 @@ ; CHECK-LABEL: in_constant_varx_42: ; CHECK: // %bb.0: ; CHECK-NEXT: mov w8, #42 -; CHECK-NEXT: bic w8, w8, w2 -; CHECK-NEXT: and w9, w0, w2 -; CHECK-NEXT: orr w0, w9, w8 +; CHECK-NEXT: eor w9, w0, w8 +; CHECK-NEXT: and w9, w9, w2 +; CHECK-NEXT: eor w0, w9, w8 ; CHECK-NEXT: ret %n0 = xor i32 %x, 42 ; %x %n1 = and i32 %n0, %mask @@ -424,9 +425,9 @@ ; CHECK-LABEL: in_constant_varx_42_invmask: ; CHECK: // %bb.0: ; CHECK-NEXT: mov w8, #42 -; CHECK-NEXT: and w8, w2, w8 -; CHECK-NEXT: bic w9, w0, w2 -; CHECK-NEXT: orr w0, w9, w8 +; CHECK-NEXT: eor w9, w0, w8 +; CHECK-NEXT: bic w9, w9, w2 +; CHECK-NEXT: eor w0, w9, w8 ; CHECK-NEXT: ret %notmask = xor i32 %mask, -1 %n0 = xor i32 %x, 42 ; %x Index: test/CodeGen/X86/unfold-masked-merge-scalar-variablemask.ll =================================================================== --- test/CodeGen/X86/unfold-masked-merge-scalar-variablemask.ll +++ test/CodeGen/X86/unfold-masked-merge-scalar-variablemask.ll @@ -570,10 +570,8 @@ ; ; CHECK-BMI-LABEL: in_constant_varx_mone: ; CHECK-BMI: # %bb.0: -; CHECK-BMI-NEXT: andl %edx, %edi -; CHECK-BMI-NEXT: notl %edx -; CHECK-BMI-NEXT: orl %edi, %edx -; CHECK-BMI-NEXT: movl %edx, %eax +; CHECK-BMI-NEXT: andnl %edx, %edi, %eax +; CHECK-BMI-NEXT: notl %eax ; CHECK-BMI-NEXT: retq %n0 = xor i32 %x, -1 ; %x %n1 = and i32 %n0, %mask @@ -612,8 +610,9 @@ ; ; CHECK-BMI-LABEL: in_constant_varx_mone_invmask: ; CHECK-BMI: # %bb.0: -; CHECK-BMI-NEXT: andnl %edi, %edx, %eax -; CHECK-BMI-NEXT: orl %edx, %eax +; CHECK-BMI-NEXT: notl %edx +; CHECK-BMI-NEXT: andnl %edx, %edi, %eax +; CHECK-BMI-NEXT: notl %eax ; CHECK-BMI-NEXT: retq %notmask = xor i32 %mask, -1 %n0 = xor i32 %x, -1 ; %x @@ -656,11 +655,10 @@ ; ; CHECK-BMI-LABEL: in_constant_varx_42: ; CHECK-BMI: # %bb.0: +; CHECK-BMI-NEXT: xorl $42, %edi ; CHECK-BMI-NEXT: andl %edx, %edi -; CHECK-BMI-NEXT: notl %edx -; CHECK-BMI-NEXT: andl $42, %edx -; CHECK-BMI-NEXT: orl %edi, %edx -; CHECK-BMI-NEXT: movl %edx, %eax +; CHECK-BMI-NEXT: xorl $42, %edi +; CHECK-BMI-NEXT: movl %edi, %eax ; CHECK-BMI-NEXT: retq %n0 = xor i32 %x, 42 ; %x %n1 = and i32 %n0, %mask @@ -702,9 +700,9 @@ ; ; CHECK-BMI-LABEL: in_constant_varx_42_invmask: ; CHECK-BMI: # %bb.0: +; CHECK-BMI-NEXT: xorl $42, %edi ; CHECK-BMI-NEXT: andnl %edi, %edx, %eax -; CHECK-BMI-NEXT: andl $42, %edx -; CHECK-BMI-NEXT: orl %edx, %eax +; CHECK-BMI-NEXT: xorl $42, %eax ; CHECK-BMI-NEXT: retq %notmask = xor i32 %mask, -1 %n0 = xor i32 %x, 42 ; %x