public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/83171] __builtin_popcountl ((unsigned long long)unsigned_var) is not being optimized to __builtin_popcount (unsigned_var)
       [not found] <bug-83171-4@http.gcc.gnu.org/bugzilla/>
@ 2021-08-19 18:10 ` pinskia at gcc dot gnu.org
  2023-11-17 14:11 ` cvs-commit at gcc dot gnu.org
  1 sibling, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-19 18:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83171

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|2017-11-26 00:00:00         |2021-8-19

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
>Clang (-O2, using libstdc++) and MSVC(/O2, Visual 2017) seem to handle this correctly. 
clang is definitely able to handle it.
We should inline the popcount too if the target does not support popcount but
that is a different bug report I had saw before.

anyways here is a reduced testcase:
#include <stdint.h>
#include <stddef.h>

size_t foo1(uint32_t value)
{
  unsigned char t = value;
  return __builtin_popcount(t);
}
size_t foo2(uint32_t value)
{
  unsigned char t = value;
  unsigned int t1 = t;
  return __builtin_popcountl(t1);
}
----- CUT ----
Note for aarch64, it does not matter as we always zero_extend to 64bit, though
I suspect there might be a way to remove the addv if we are just doing one byte
popcount.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug tree-optimization/83171] __builtin_popcountl ((unsigned long long)unsigned_var) is not being optimized to __builtin_popcount (unsigned_var)
       [not found] <bug-83171-4@http.gcc.gnu.org/bugzilla/>
  2021-08-19 18:10 ` [Bug tree-optimization/83171] __builtin_popcountl ((unsigned long long)unsigned_var) is not being optimized to __builtin_popcount (unsigned_var) pinskia at gcc dot gnu.org
@ 2023-11-17 14:11 ` cvs-commit at gcc dot gnu.org
  1 sibling, 0 replies; 2+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-11-17 14:11 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83171

--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:6dd4c703be17fa5dd56136d067e7fdc4a61584b3

commit r14-5557-g6dd4c703be17fa5dd56136d067e7fdc4a61584b3
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri Nov 17 15:10:51 2023 +0100

    match.pd: Optimize ctz/popcount/parity/ffs on extended argument [PR112566]

    ctz(ext(X)) is the same as ctz(X) in the UB on zero case (or could be also
    in the 2 argument case on large BITINT_TYPE by preserving the argument, not
    implemented in this patch),
    popcount(zext(X)) is the same as popcount(X),
    parity(zext(X)) is the same as parity(X),
    parity(sext(X)) is the same as parity(X) provided the bit difference
between
    the extended and unextended types is even,
    ffs(ext(X)) is the same as ffs(X).

    The following patch optimizes those in match.pd if those are beneficial
    (always in the large BITINT_TYPE case, or if the narrower type has optab
    and the wider doesn't, or the wider is larger than word and narrower is
    one of the standard argument sizes (tested just int and long long, as
    long is on most targets same bitsize as one of those two).

    Joseph in the PR mentioned that ctz(narrow(X)) is the same as ctz(X)
    if UB on 0, but that can be handled incrementally (and would need different
    decisions when it is profitable).
    And clz(zext(X)) is clz(X) + bit_difference, but not sure we want to change
    that in match.pd at all, perhaps during insn selection?

    2023-11-17  Jakub Jelinek  <jakub@redhat.com>

            PR tree-optimization/112566
            PR tree-optimization/83171
            * match.pd (ctz(ext(X)) -> ctz(X), popcount(zext(X)) ->
popcount(X),
            parity(ext(X)) -> parity(X), ffs(ext(X)) -> ffs(X)): New
            simplifications.
            ( __builtin_ffs (X) == 0 -> X == 0): Use FFS rather than
            BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL BUILT_IN_FFSIMAX.

            * gcc.dg/pr112566-1.c: New test.
            * gcc.dg/pr112566-2.c: New test.
            * gcc.target/i386/pr78057.c (foo): Pass another long long argument
            and use it in __builtin_ia32_*zcnt_u64 instead of the int one.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-11-17 14:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-83171-4@http.gcc.gnu.org/bugzilla/>
2021-08-19 18:10 ` [Bug tree-optimization/83171] __builtin_popcountl ((unsigned long long)unsigned_var) is not being optimized to __builtin_popcount (unsigned_var) pinskia at gcc dot gnu.org
2023-11-17 14:11 ` cvs-commit at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).