[llvm] [DAG] Constant fold ISD::PARITY (PR #224387)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 17 12:26:09 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-x86
Author: Philip Reames (preames)
<details>
<summary>Changes</summary>
Context: Starting a claude assisted sweep for small gaps in SDAG's core routines,
starting with constant folding. This is the first of a small series of patches covering
small obvious seeming gaps. My main motivation is simply making sure we have
full coverage and as an exercise in getting myself re-started upstream as I've been
inactive for a few months.
ISD::PARITY was missing from getNode / FoldConstantArithmetic which meant it wasn't
being constant folded at construction. Also implement the DAG combine so that an
already created PARITY node can be folded if it's arguments become constant. This
is the path which actually triggers outside of type legalization.
Since PARITY doesn't have an IR form, we have to form tests in a way to let the DAG
combine from ctpop form them. This results in somewhat fragile tests which is
unfortunate.
Assisted-by: Claude Fable 5.1
(By which I mean, entirely written by Claude with guidance from me on taste)
---
Full diff: https://github.com/llvm/llvm-project/pull/224387.diff
4 Files Affected:
- (modified) llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (+13)
- (modified) llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp (+4)
- (modified) llvm/test/CodeGen/X86/parity.ll (+70)
- (modified) llvm/test/CodeGen/X86/vector-reduce-xor.ll (+1-5)
``````````diff
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index d4a7403a6b25f..f204a218bb57c 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -481,6 +481,7 @@ namespace {
SDValue visitCTTZ(SDNode *N);
SDValue visitCTTZ_ZERO_POISON(SDNode *N);
SDValue visitCTPOP(SDNode *N);
+ SDValue visitPARITY(SDNode *N);
SDValue visitSELECT(SDNode *N);
SDValue visitVSELECT(SDNode *N);
SDValue visitSELECT_CC(SDNode *N);
@@ -2036,6 +2037,7 @@ SDValue DAGCombiner::visit(SDNode *N) {
case ISD::CTTZ: return visitCTTZ(N);
case ISD::CTTZ_ZERO_POISON: return visitCTTZ_ZERO_POISON(N);
case ISD::CTPOP: return visitCTPOP(N);
+ case ISD::PARITY: return visitPARITY(N);
case ISD::SELECT: return visitSELECT(N);
case ISD::VSELECT: return visitVSELECT(N);
case ISD::SELECT_CC: return visitSELECT_CC(N);
@@ -12740,6 +12742,17 @@ SDValue DAGCombiner::visitCTTZ_ZERO_POISON(SDNode *N) {
return SDValue();
}
+SDValue DAGCombiner::visitPARITY(SDNode *N) {
+ SDValue N0 = N->getOperand(0);
+ EVT VT = N->getValueType(0);
+
+ // fold (parity c1) -> c2
+ if (SDValue C = DAG.FoldConstantArithmetic(ISD::PARITY, SDLoc(N), VT, {N0}))
+ return C;
+
+ return SDValue();
+}
+
SDValue DAGCombiner::visitCTPOP(SDNode *N) {
SDValue N0 = N->getOperand(0);
EVT VT = N->getValueType(0);
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 66a445ec0827e..0c8a621c11ac1 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -7124,6 +7124,7 @@ SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
case ISD::CTTZ:
case ISD::CTTZ_ZERO_POISON:
case ISD::CTPOP:
+ case ISD::PARITY:
case ISD::CTLS:
case ISD::VECREDUCE_ADD:
case ISD::VECREDUCE_SMAX:
@@ -7700,6 +7701,9 @@ SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL,
case ISD::CTPOP:
return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
C->isOpaque());
+ case ISD::PARITY:
+ return getConstant(Val.popcount() & 1, DL, VT, C->isTargetOpcode(),
+ C->isOpaque());
case ISD::CTLZ:
case ISD::CTLZ_ZERO_POISON:
return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
diff --git a/llvm/test/CodeGen/X86/parity.ll b/llvm/test/CodeGen/X86/parity.ll
index 31a7f1125150b..b75f3bd18dbf8 100644
--- a/llvm/test/CodeGen/X86/parity.ll
+++ b/llvm/test/CodeGen/X86/parity.ll
@@ -662,9 +662,79 @@ define i64 @parity_64_shift(i64 %0) {
ret i64 %4
}
+; The sub only folds to zero in DAGCombine, after the ctpop+and has already
+; been turned into a PARITY node.
+define i32 @parity_32_constant(i32 %x) {
+; X86-LABEL: parity_32_constant:
+; X86: # %bb.0:
+; X86-NEXT: xorl %eax, %eax
+; X86-NEXT: retl
+;
+; X64-LABEL: parity_32_constant:
+; X64: # %bb.0:
+; X64-NEXT: xorl %eax, %eax
+; X64-NEXT: retq
+ %1 = sub i32 %x, %x
+ %2 = tail call i32 @llvm.ctpop.i32(i32 %1)
+ %3 = and i32 %2, 1
+ ret i32 %3
+}
+
+define i32 @parity_32_constant_odd(i32 %x) {
+; X86-LABEL: parity_32_constant_odd:
+; X86: # %bb.0:
+; X86-NEXT: movl $1, %eax
+; X86-NEXT: retl
+;
+; X64-LABEL: parity_32_constant_odd:
+; X64: # %bb.0:
+; X64-NEXT: movl $1, %eax
+; X64-NEXT: retq
+ %1 = sub i32 %x, %x
+ %2 = or i32 %1, 7
+ %3 = tail call i32 @llvm.ctpop.i32(i32 %2)
+ %4 = and i32 %3, 1
+ ret i32 %4
+}
+
+; As above, but the i128 PARITY node is expanded by type legalization, which
+; recreates a narrower PARITY node with an already constant operand.
+define i128 @parity_128_constant(i128 %x) {
+; X86-LABEL: parity_128_constant:
+; X86: # %bb.0:
+; X86-NEXT: pushl %ebp
+; X86-NEXT: .cfi_def_cfa_offset 8
+; X86-NEXT: .cfi_offset %ebp, -8
+; X86-NEXT: movl %esp, %ebp
+; X86-NEXT: .cfi_def_cfa_register %ebp
+; X86-NEXT: andl $-16, %esp
+; X86-NEXT: subl $16, %esp
+; X86-NEXT: movl 8(%ebp), %eax
+; X86-NEXT: movl $0, 12(%eax)
+; X86-NEXT: movl $0, 8(%eax)
+; X86-NEXT: movl $0, 4(%eax)
+; X86-NEXT: movl $1, (%eax)
+; X86-NEXT: movl %ebp, %esp
+; X86-NEXT: popl %ebp
+; X86-NEXT: .cfi_def_cfa %esp, 4
+; X86-NEXT: retl $4
+;
+; X64-LABEL: parity_128_constant:
+; X64: # %bb.0:
+; X64-NEXT: movl $1, %eax
+; X64-NEXT: xorl %edx, %edx
+; X64-NEXT: retq
+ %1 = sub i128 %x, %x
+ %2 = or i128 %1, 7
+ %3 = tail call i128 @llvm.ctpop.i128(i128 %2)
+ %4 = and i128 %3, 1
+ ret i128 %4
+}
+
declare i4 @llvm.ctpop.i4(i4 %x)
declare i8 @llvm.ctpop.i8(i8 %x)
declare i16 @llvm.ctpop.i16(i16 %x)
declare i17 @llvm.ctpop.i17(i17 %x)
declare i32 @llvm.ctpop.i32(i32 %x)
declare i64 @llvm.ctpop.i64(i64 %x)
+declare i128 @llvm.ctpop.i128(i128 %x)
diff --git a/llvm/test/CodeGen/X86/vector-reduce-xor.ll b/llvm/test/CodeGen/X86/vector-reduce-xor.ll
index 8fc68928c3a15..a1b9430e60bbb 100644
--- a/llvm/test/CodeGen/X86/vector-reduce-xor.ll
+++ b/llvm/test/CodeGen/X86/vector-reduce-xor.ll
@@ -1560,11 +1560,7 @@ define i8 @test_v128i8(<128 x i8> %a0) nounwind {
define i32 @PR215069() {
; CHECK-LABEL: PR215069:
; CHECK: # %bb.0:
-; CHECK-NEXT: movb $7, %cl
-; CHECK-NEXT: xorl %eax, %eax
-; CHECK-NEXT: testb %cl, %cl
-; CHECK-NEXT: setnp %al
-; CHECK-NEXT: negl %eax
+; CHECK-NEXT: movl $-1, %eax
; CHECK-NEXT: ret{{[l|q]}}
%i0 = insertelement <4 x i32> zeroinitializer, i32 0, i64 0
%i1 = xor <4 x i32> %i0, <i32 -1, i32 -1, i32 -1, i32 0>
``````````
</details>
https://github.com/llvm/llvm-project/pull/224387
More information about the llvm-commits
mailing list