Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 28 additions & 55 deletions llvm/lib/Target/X86/X86ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3485,29 +3485,23 @@ bool X86TargetLowering::isMaskAndCmp0FoldingBeneficial(
}

bool X86TargetLowering::hasAndNotCompare(SDValue Y) const {
EVT VT = Y.getValueType();

if (VT.isVector())
return false;

if (!Subtarget.hasBMI())
return false;

// There are only 32-bit and 64-bit forms for 'andn'.
if (VT != MVT::i32 && VT != MVT::i64)
return false;

return !isa<ConstantSDNode>(Y) || cast<ConstantSDNode>(Y)->isOpaque();
return Y.getValueType().isScalarInteger();
}

bool X86TargetLowering::hasAndNot(SDValue Y) const {
EVT VT = Y.getValueType();

if (!VT.isVector())
return hasAndNotCompare(Y);
if (!VT.isVector()) {
if (!Subtarget.hasBMI())
return false;

// Vector.
// There are only 32-bit and 64-bit forms for 'andn'.
if (VT != MVT::i32 && VT != MVT::i64)
return false;
return !isa<ConstantSDNode>(Y) || cast<ConstantSDNode>(Y)->isOpaque();
}

// Vector.
if (!Subtarget.hasSSE1() || VT.getSizeInBits() < 128)
return false;

Expand Down Expand Up @@ -38455,9 +38449,9 @@ X86TargetLowering::targetShrinkDemandedConstant(SDValue Op,
return false;
}

// Only optimize Ands to prevent shrinking a constant that could be
// matched by movzx.
if (Opcode != ISD::AND)
// Only optimize certain opcodes to prevent shrinking a constant that could be
// matched by specific instructions.
if (Opcode != ISD::AND && Opcode != ISD::XOR)
return false;

// Make sure the RHS really is a constant.
Expand All @@ -38467,6 +38461,21 @@ X86TargetLowering::targetShrinkDemandedConstant(SDValue Op,

const APInt &Mask = C->getAPIntValue();

if (Opcode == ISD::XOR) {
// If all demanded bits are 1s in the mask, we can replace the mask with
// all 1s, which allows this to be turned into a NOT.
if (DemandedBits.isSubsetOf(Mask)) {
if (Mask.isAllOnes())
return false;
// Replace the constant with all ones.
SDLoc DL(Op);
SDValue NewC = TLO.DAG.getConstant(APInt::getAllOnes(EltSize), DL, VT);
SDValue NewOp = TLO.DAG.getNode(ISD::XOR, DL, VT, Op.getOperand(0), NewC);
return TLO.CombineTo(Op, NewOp);
}
return false;
}

// Clear all non-demanded bits initially.
APInt ShrunkMask = Mask & DemandedBits;

Expand Down Expand Up @@ -56363,42 +56372,6 @@ static SDValue combineSetCC(SDNode *N, SelectionDAG &DAG,

if (CC == ISD::SETNE || CC == ISD::SETEQ) {
if (OpVT.isScalarInteger()) {
// cmpeq(or(X,Y),X) --> cmpeq(and(~X,Y),0)
// cmpne(or(X,Y),X) --> cmpne(and(~X,Y),0)
auto MatchOrCmpEq = [&](SDValue N0, SDValue N1) {
if (N0.getOpcode() == ISD::OR && N0->hasOneUse()) {
if (N0.getOperand(0) == N1)
return DAG.getNode(ISD::AND, DL, OpVT, DAG.getNOT(DL, N1, OpVT),
N0.getOperand(1));
if (N0.getOperand(1) == N1)
return DAG.getNode(ISD::AND, DL, OpVT, DAG.getNOT(DL, N1, OpVT),
N0.getOperand(0));
}
return SDValue();
};
if (SDValue AndN = MatchOrCmpEq(LHS, RHS))
return DAG.getSetCC(DL, VT, AndN, DAG.getConstant(0, DL, OpVT), CC);
if (SDValue AndN = MatchOrCmpEq(RHS, LHS))
return DAG.getSetCC(DL, VT, AndN, DAG.getConstant(0, DL, OpVT), CC);

// cmpeq(and(X,Y),Y) --> cmpeq(and(~X,Y),0)
// cmpne(and(X,Y),Y) --> cmpne(and(~X,Y),0)
auto MatchAndCmpEq = [&](SDValue N0, SDValue N1) {
if (N0.getOpcode() == ISD::AND && N0->hasOneUse()) {
if (N0.getOperand(0) == N1)
return DAG.getNode(ISD::AND, DL, OpVT, N1,
DAG.getNOT(DL, N0.getOperand(1), OpVT));
if (N0.getOperand(1) == N1)
return DAG.getNode(ISD::AND, DL, OpVT, N1,
DAG.getNOT(DL, N0.getOperand(0), OpVT));
}
return SDValue();
};
if (SDValue AndN = MatchAndCmpEq(LHS, RHS))
return DAG.getSetCC(DL, VT, AndN, DAG.getConstant(0, DL, OpVT), CC);
if (SDValue AndN = MatchAndCmpEq(RHS, LHS))
return DAG.getSetCC(DL, VT, AndN, DAG.getConstant(0, DL, OpVT), CC);

// cmpeq(trunc(x),C) --> cmpeq(x,C)
// cmpne(trunc(x),C) --> cmpne(x,C)
// iff x upper bits are zero.
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/CodeGen/X86/avx512-cmp.ll
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ B:
define i32 @test10(i64 %b, i64 %c, i1 %d) {
; ALL-LABEL: test10:
; ALL: ## %bb.0:
; ALL-NEXT: xorb $1, %dl
; ALL-NEXT: cmpq %rsi, %rdi
; ALL-NEXT: sete %al
; ALL-NEXT: notb %dl
; ALL-NEXT: testb %al, %dl
; ALL-NEXT: testb %dl, %al
; ALL-NEXT: je LBB8_1
; ALL-NEXT: ## %bb.2: ## %if.end.i
; ALL-NEXT: movl $6, %eax
Expand Down
Loading
Loading