This is an archive of the discontinued LLVM Phabricator instance.

[SelectionDAG][X86][ARM][AArch64] Add ISD opcode for __builtin_parity. Expand it to shifts and xors.
ClosedPublic

Authored by craig.topper on Sep 6 2020, 12:26 PM.

Details

Summary

Clang emits (and (ctpop X), 1) for __builtin_parity. If ctpop
isn't natively supported by the target, this leads to poor codegen
due to the expansion of ctpop being more complex than what is needed
for parity.

This adds a DAG combine to convert the pattern to ISD::PARITY of
CTPOP is not supported for this type. Type legalization is updated
to handled Expanding and Promoting this operation. If after type
legalization, CTPOP is supported for this type, LegalizeDAG will
turn it back into CTPOP+AND. Otherwise LegalizeDAG will emit a
series of shifts and xors followed by an AND with 1.

I've avoided vectors in this patch to avoid more legalization
complexity for this patch.

X86 previously had a custom DAG combiner for this. This is now
moved to Custom lowering for the new opcode. There is a minor
regression in vector-reduce-xor-bool.ll, but a follow up patch
can easily fix that.

Fixes PR47433

Diff Detail

Event Timeline

craig.topper created this revision.Sep 6 2020, 12:26 PM
craig.topper requested review of this revision.Sep 6 2020, 12:26 PM
RKSimon added inline comments.Sep 7 2020, 6:07 AM
llvm/test/CodeGen/AArch64/parity.ll
5

Test non-pow2 type?

-Add a test for i17 so we have non-power of 2
-Change DAGCombiner to always do the conversion before LegalizeOperations instead of checking if CTPOP is legal. This gives targets more flexibility to use Custom to avoid CTPOP even if it is Legal.

efriedma added inline comments.Sep 11 2020, 2:22 PM
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
5591

clang-format doesn't like this

llvm/lib/Target/X86/X86ISelLowering.cpp
28894

Why ">= 64"?

-Run clang-format
-Remove Size variable from X86 code and just use VT.

This revision is now accepted and ready to land.Sep 11 2020, 3:46 PM
This revision was landed with ongoing or failed builds.Sep 12 2020, 11:43 AM
This revision was automatically updated to reflect the committed changes.