Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
37 changes: 33 additions & 4 deletions llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4721,9 +4721,6 @@ bool X86DAGToDAGISel::tryVPTERNLOG(SDNode *N) {
if (!(Subtarget->hasVLX() || NVT.is512BitVector()))
return false;

SDValue N0 = N->getOperand(0);
SDValue N1 = N->getOperand(1);

auto getFoldableLogicOp = [](SDValue Op) {
// Peek through single use bitcast.
if (Op.getOpcode() == ISD::BITCAST && Op.hasOneUse())
Expand All @@ -4740,6 +4737,35 @@ bool X86DAGToDAGISel::tryVPTERNLOG(SDNode *N) {
return SDValue();
};

// Identify and (optionally) peel an outer NOT that wraps a pure logic tree
auto tryPeelOuterNotWrappingLogic = [&](SDNode *Op) {
if (Op->getOpcode() == ISD::XOR && Op->hasOneUse() &&
ISD::isBuildVectorAllOnes(Op->getOperand(1).getNode())) {
SDNode *InnerN = Op->getOperand(0).getNode();

unsigned InnerOpc = InnerN->getOpcode();
if (InnerOpc != ISD::AND && InnerOpc != ISD::OR && InnerOpc != ISD::XOR &&
InnerOpc != X86ISD::ANDNP) {
return Op;
}

SDValue InnerN0 = InnerN->getOperand(0);
SDValue InnerN1 = InnerN->getOperand(1);
if (getFoldableLogicOp(InnerN1) || getFoldableLogicOp(InnerN0))
return InnerN;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicated with 4766 line and later. Better to see if we can reuse them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll think about how to better handle this

}
return Op;
};

SDNode *OriN = N;
bool PeeledOuterNot = false;
N = tryPeelOuterNotWrappingLogic(N);
if (N != OriN)
PeeledOuterNot = true;

SDValue N0 = N->getOperand(0);
SDValue N1 = N->getOperand(1);

SDValue A, FoldableOp;
if ((FoldableOp = getFoldableLogicOp(N1))) {
A = N0;
Expand Down Expand Up @@ -4798,7 +4824,10 @@ bool X86DAGToDAGISel::tryVPTERNLOG(SDNode *N) {
case ISD::XOR: Imm ^= TernlogMagicA; break;
}

return matchVPTERNLOG(N, ParentA, ParentB, ParentC, A, B, C, Imm);
if (PeeledOuterNot)
Imm = ~Imm;

return matchVPTERNLOG(OriN, ParentA, ParentB, ParentC, A, B, C, Imm);
}

/// If the high bits of an 'and' operand are known zero, try setting the
Expand Down
13 changes: 13 additions & 0 deletions llvm/test/CodeGen/X86/issue163738.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
; RUN: llc < %s -mtriple=x86_64-- -mattr=+avx512f,+avx512vl | FileCheck %s --check-prefixes=CHECK

define <8 x i64> @foo(<8 x i64> %a, <8 x i64> %b, <8 x i64> %c) {
; CHECK-LABEL: foo:
; CHECK: # %bb.0:
; CHECK-NEXT: vpternlogq {{.*#+}} zmm0 = ~(zmm0 | zmm2 | zmm1)
; CHECK-NEXT: retq
%and.demorgan = or <8 x i64> %b, %a
%and3.demorgan = or <8 x i64> %and.demorgan, %c
%and3 = xor <8 x i64> %and3.demorgan, splat (i64 -1)
ret <8 x i64> %and3
}