[DAGCombine] Move AVG combine to SimplifyDemandBits
authorDavid Green <david.green@arm.com>
Tue, 15 Feb 2022 10:17:02 +0000 (10:17 +0000)
committerDavid Green <david.green@arm.com>
Tue, 15 Feb 2022 10:17:02 +0000 (10:17 +0000)
commit655d0d86f91bae449459d9e0eb4b0fed3b78a53f
tree4aca127a645370b3bee805061ea1c4f5c37e3e6a
parente7dcf09fc321010fb012afd270afdef20378dee7
[DAGCombine] Move AVG combine to SimplifyDemandBits

This moves the matching of AVGFloor and AVGCeil into a place where
demand bit are available, so that it can detect more cases for more
folds. It changes the transform to start from a shift, not from a
truncate. We match the pattern shr(add(ext(A), ext(B)), 1), transforming
to ext(hadd(A, B)).

For signed values, because only the bottom bits are demanded llvm will
transform the above to use a lshr too, as opposed to ashr. In order to
correctly detect the hadd we need to know the demanded bits to turn it
back. Depending on whether the shift is signed (ashr) or logical (lshr),
and the extensions are signed or unsigned we can create different nodes.
If the shift is signed:
  Needs >= 2 sign bits. https://alive2.llvm.org/ce/z/h4gQAW generating signed rhadd.
  Needs >= 2 zero bits. https://alive2.llvm.org/ce/z/B64DUA generating unsigned rhadd.
If the shift is unsigned:
  Needs >= 1 zero bits. https://alive2.llvm.org/ce/z/ByD8sj generating unsigned rhadd.
  Needs 1 demanded bit zero and >= 2 sign bits https://alive2.llvm.org/ce/z/hvPGxX and
    https://alive2.llvm.org/ce/z/32P5n1 generating signed rhadd.

Differential Revision: https://reviews.llvm.org/D119072
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
llvm/test/CodeGen/AArch64/arm64-vhadd.ll
llvm/test/CodeGen/AArch64/hadd-combine.ll
llvm/test/CodeGen/X86/avg.ll