Index: lib/CodeGen/SelectionDAG/DAGCombiner.cpp =================================================================== --- lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -5384,11 +5384,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) @@ -5399,8 +5399,14 @@ 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) && !matchAndXor(B, 1, A)) return SDValue(); 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 @@ -329,9 +329,59 @@ %r = xor i32 %y, %n1 ret i32 %r } +; %x is a constant +define i32 @out_constant_x_mone(i32 %y, i32 %mask) { +; CHECK-LABEL: out_constant_x_mone: +; CHECK: // %bb.0: +; CHECK-NEXT: bic w8, w0, w1 +; CHECK-NEXT: orr w0, w1, w8 +; CHECK-NEXT: ret + %mx = and i32 %mask, -1 ; %x + %notmask = xor i32 %mask, -1 + %my = and i32 %y, %notmask + %r = or i32 %mx, %my + ret i32 %r +} +define i32 @in_constant_x_mone(i32 %x, i32 %y, i32 %mask) { +; CHECK-LABEL: in_constant_x_mone: +; CHECK: // %bb.0: +; CHECK-NEXT: bic w8, w1, w2 +; CHECK-NEXT: orr w0, w2, w8 +; CHECK-NEXT: ret + %n0 = xor i32 %y, -1 ; %x + %n1 = and i32 %n0, %mask + %r = xor i32 %n1, %y + ret i32 %r +} +define i32 @out_constant_x_one(i32 %y, i32 %mask) { +; CHECK-LABEL: out_constant_x_one: +; CHECK: // %bb.0: +; CHECK-NEXT: and w8, w1, #0x1 +; CHECK-NEXT: bic w9, w0, w1 +; CHECK-NEXT: orr w0, w8, w9 +; CHECK-NEXT: ret + %mx = and i32 %mask, 1 ; %x + %notmask = xor i32 %mask, -1 + %my = and i32 %y, %notmask + %r = or i32 %mx, %my + ret i32 %r +} +define i32 @in_constant_x_one(i32 %x, i32 %y, i32 %mask) { +; CHECK-LABEL: in_constant_x_one: +; CHECK: // %bb.0: +; CHECK-NEXT: bic w8, w1, w2 +; CHECK-NEXT: and w9, w2, #0x1 +; CHECK-NEXT: orr w0, w9, w8 +; CHECK-NEXT: ret + %n0 = xor i32 %y, 1 ; %x + %n1 = and i32 %n0, %mask + %r = xor i32 %n1, %y + ret i32 %r +} +; ============================================================================ ; +; Negative tests. Should not be folded. ; ============================================================================ ; ; Both xor's have the same constant operand -; ============================================================================ ; define i32 @out_constant_y_mone(i32 %x, i32 %mask) { ; CHECK-LABEL: out_constant_y_mone: ; CHECK: // %bb.0: @@ -344,12 +394,11 @@ %r = or i32 %mx, %my ret i32 %r } -; FIXME: should be bic+mvn define i32 @in_constant_y_mone(i32 %x, i32 %mask) { ; CHECK-LABEL: in_constant_y_mone: ; CHECK: // %bb.0: -; CHECK-NEXT: and w8, w0, w1 -; CHECK-NEXT: orn w0, w8, w1 +; CHECK-NEXT: bic w8, w1, w0 +; CHECK-NEXT: mvn w0, w8 ; CHECK-NEXT: ret %n0 = xor i32 %x, -1 ; %y %n1 = and i32 %n0, %mask @@ -370,23 +419,18 @@ %r = or i32 %mx, %my ret i32 %r } -; FIXME: should be eor+and+eor define i32 @in_constant_y_one(i32 %x, i32 %mask) { ; CHECK-LABEL: in_constant_y_one: ; CHECK: // %bb.0: -; CHECK-NEXT: mvn w9, w1 -; CHECK-NEXT: and w8, w0, w1 -; CHECK-NEXT: and w9, w9, #0x1 -; CHECK-NEXT: orr w0, w8, w9 +; CHECK-NEXT: eor w8, w0, #0x1 +; CHECK-NEXT: and w8, w8, w1 +; CHECK-NEXT: eor w0, w8, #0x1 ; CHECK-NEXT: ret %n0 = xor i32 %x, 1 ; %y %n1 = and i32 %n0, %mask %r = xor i32 %n1, 1 ; %y ret i32 %r } -; ============================================================================ ; -; Negative tests. Should not be folded. -; ============================================================================ ; ; Multi-use tests. declare void @use32(i32) nounwind define i32 @in_multiuse_A(i32 %x, i32 %y, i32 %z, i32 %mask) nounwind { 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 @@ -534,9 +534,93 @@ %r = xor i32 %y, %n1 ret i32 %r } +; %x is a constant +define i32 @out_constant_x_mone(i32 %y, i32 %mask) { +; CHECK-NOBMI-LABEL: out_constant_x_mone: +; CHECK-NOBMI: # %bb.0: +; CHECK-NOBMI-NEXT: movl %esi, %eax +; CHECK-NOBMI-NEXT: notl %eax +; CHECK-NOBMI-NEXT: andl %edi, %eax +; CHECK-NOBMI-NEXT: orl %esi, %eax +; CHECK-NOBMI-NEXT: retq +; +; CHECK-BMI-LABEL: out_constant_x_mone: +; CHECK-BMI: # %bb.0: +; CHECK-BMI-NEXT: andnl %edi, %esi, %eax +; CHECK-BMI-NEXT: orl %esi, %eax +; CHECK-BMI-NEXT: retq + %mx = and i32 %mask, -1 ; %x + %notmask = xor i32 %mask, -1 + %my = and i32 %y, %notmask + %r = or i32 %mx, %my + ret i32 %r +} +define i32 @in_constant_x_mone(i32 %x, i32 %y, i32 %mask) { +; CHECK-NOBMI-LABEL: in_constant_x_mone: +; CHECK-NOBMI: # %bb.0: +; CHECK-NOBMI-NEXT: movl %esi, %eax +; CHECK-NOBMI-NEXT: notl %eax +; CHECK-NOBMI-NEXT: andl %edx, %eax +; CHECK-NOBMI-NEXT: xorl %esi, %eax +; CHECK-NOBMI-NEXT: retq +; +; CHECK-BMI-LABEL: in_constant_x_mone: +; CHECK-BMI: # %bb.0: +; CHECK-BMI-NEXT: andnl %esi, %edx, %eax +; CHECK-BMI-NEXT: orl %edx, %eax +; CHECK-BMI-NEXT: retq + %n0 = xor i32 %y, -1 ; %x + %n1 = and i32 %n0, %mask + %r = xor i32 %n1, %y + ret i32 %r +} +define i32 @out_constant_x_one(i32 %y, i32 %mask) { +; CHECK-NOBMI-LABEL: out_constant_x_one: +; CHECK-NOBMI: # %bb.0: +; CHECK-NOBMI-NEXT: movl %esi, %eax +; CHECK-NOBMI-NEXT: andl $1, %eax +; CHECK-NOBMI-NEXT: notl %esi +; CHECK-NOBMI-NEXT: andl %edi, %esi +; CHECK-NOBMI-NEXT: orl %eax, %esi +; CHECK-NOBMI-NEXT: movl %esi, %eax +; CHECK-NOBMI-NEXT: retq +; +; CHECK-BMI-LABEL: out_constant_x_one: +; CHECK-BMI: # %bb.0: +; CHECK-BMI-NEXT: andnl %edi, %esi, %eax +; CHECK-BMI-NEXT: andl $1, %esi +; CHECK-BMI-NEXT: orl %esi, %eax +; CHECK-BMI-NEXT: retq + %mx = and i32 %mask, 1 ; %x + %notmask = xor i32 %mask, -1 + %my = and i32 %y, %notmask + %r = or i32 %mx, %my + ret i32 %r +} +define i32 @in_constant_x_one(i32 %x, i32 %y, i32 %mask) { +; CHECK-NOBMI-LABEL: in_constant_x_one: +; CHECK-NOBMI: # %bb.0: +; CHECK-NOBMI-NEXT: movl %esi, %eax +; CHECK-NOBMI-NEXT: xorl $1, %eax +; CHECK-NOBMI-NEXT: andl %edx, %eax +; CHECK-NOBMI-NEXT: xorl %esi, %eax +; CHECK-NOBMI-NEXT: retq +; +; CHECK-BMI-LABEL: in_constant_x_one: +; CHECK-BMI: # %bb.0: +; CHECK-BMI-NEXT: andnl %esi, %edx, %eax +; CHECK-BMI-NEXT: andl $1, %edx +; CHECK-BMI-NEXT: orl %edx, %eax +; CHECK-BMI-NEXT: retq + %n0 = xor i32 %y, 1 ; %x + %n1 = and i32 %n0, %mask + %r = xor i32 %n1, %y + ret i32 %r +} +; ============================================================================ ; +; Negative tests. Should not be folded. ; ============================================================================ ; ; Both xor's have the same constant operand -; ============================================================================ ; define i32 @out_constant_y_mone(i32 %x, i32 %mask) { ; CHECK-NOBMI-LABEL: out_constant_y_mone: ; CHECK-NOBMI: # %bb.0: @@ -559,7 +643,6 @@ %r = or i32 %mx, %my ret i32 %r } -; FIXME: should be andnl+notl if BMI define i32 @in_constant_y_mone(i32 %x, i32 %mask) { ; CHECK-NOBMI-LABEL: in_constant_y_mone: ; CHECK-NOBMI: # %bb.0: @@ -571,10 +654,8 @@ ; ; CHECK-BMI-LABEL: in_constant_y_mone: ; CHECK-BMI: # %bb.0: -; CHECK-BMI-NEXT: andl %esi, %edi -; CHECK-BMI-NEXT: notl %esi -; CHECK-BMI-NEXT: orl %edi, %esi -; CHECK-BMI-NEXT: movl %esi, %eax +; CHECK-BMI-NEXT: andnl %esi, %edi, %eax +; CHECK-BMI-NEXT: notl %eax ; CHECK-BMI-NEXT: retq %n0 = xor i32 %x, -1 ; %y %n1 = and i32 %n0, %mask @@ -605,7 +686,6 @@ %r = or i32 %mx, %my ret i32 %r } -; FIXME: NOBMI and BMI should match, or BMI should be better. define i32 @in_constant_y_one(i32 %x, i32 %mask) { ; CHECK-NOBMI-LABEL: in_constant_y_one: ; CHECK-NOBMI: # %bb.0: @@ -617,20 +697,16 @@ ; ; CHECK-BMI-LABEL: in_constant_y_one: ; CHECK-BMI: # %bb.0: +; CHECK-BMI-NEXT: xorl $1, %edi ; CHECK-BMI-NEXT: andl %esi, %edi -; CHECK-BMI-NEXT: notl %esi -; CHECK-BMI-NEXT: andl $1, %esi -; CHECK-BMI-NEXT: orl %edi, %esi -; CHECK-BMI-NEXT: movl %esi, %eax +; CHECK-BMI-NEXT: xorl $1, %edi +; CHECK-BMI-NEXT: movl %edi, %eax ; CHECK-BMI-NEXT: retq %n0 = xor i32 %x, 1 ; %y %n1 = and i32 %n0, %mask %r = xor i32 %n1, 1 ; %y ret i32 %r } -; ============================================================================ ; -; Negative tests. Should not be folded. -; ============================================================================ ; ; Multi-use tests. declare void @use32(i32) nounwind define i32 @in_multiuse_A(i32 %x, i32 %y, i32 %z, i32 %mask) nounwind {