public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/94787] New: Failure to detect single bit popcount pattern
@ 2020-04-27  8:35 gabravier at gmail dot com
  2020-04-27  8:56 ` [Bug tree-optimization/94787] " gabravier at gmail dot com
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: gabravier at gmail dot com @ 2020-04-27  8:35 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 94787
           Summary: Failure to detect single bit popcount pattern
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gabravier at gmail dot com
  Target Milestone: ---

bool f(unsigned v)
{
    return v && !(v & (v - 1));
}

Depending on the supported architecture, we may want to optimize this to
`__builtin_popcount(v) == 1;` (when a fast hardware instruction is available).
For example, on x86, this should make programs faster when `popcnt` is
available. LLVM already does this.

Comparison with x86 `-mpopcnt` here : https://godbolt.org/z/M6f28d

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

* [Bug tree-optimization/94787] Failure to detect single bit popcount pattern
  2020-04-27  8:35 [Bug tree-optimization/94787] New: Failure to detect single bit popcount pattern gabravier at gmail dot com
@ 2020-04-27  8:56 ` gabravier at gmail dot com
  2020-04-27 10:28 ` rguenth at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: gabravier at gmail dot com @ 2020-04-27  8:56 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Gabriel Ravier <gabravier at gmail dot com> ---
Inversely, I'd also suggest doing the opposite. That is, if there is no
hardware popcount instruction, `__builtin_popcount(v) == 1` should be optimized
to `v && !(v & (v - 1))`

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

* [Bug tree-optimization/94787] Failure to detect single bit popcount pattern
  2020-04-27  8:35 [Bug tree-optimization/94787] New: Failure to detect single bit popcount pattern gabravier at gmail dot com
  2020-04-27  8:56 ` [Bug tree-optimization/94787] " gabravier at gmail dot com
@ 2020-04-27 10:28 ` rguenth at gcc dot gnu.org
  2020-04-29 12:52 ` wilco at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-04-27 10:28 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2020-04-27
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.  match.pd plus RTL expansion helper.

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

* [Bug tree-optimization/94787] Failure to detect single bit popcount pattern
  2020-04-27  8:35 [Bug tree-optimization/94787] New: Failure to detect single bit popcount pattern gabravier at gmail dot com
  2020-04-27  8:56 ` [Bug tree-optimization/94787] " gabravier at gmail dot com
  2020-04-27 10:28 ` rguenth at gcc dot gnu.org
@ 2020-04-29 12:52 ` wilco at gcc dot gnu.org
  2021-12-12 13:24 ` [Bug middle-end/94787] " pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: wilco at gcc dot gnu.org @ 2020-04-29 12:52 UTC (permalink / raw)
  To: gcc-bugs

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

Wilco <wilco at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |wilco at gcc dot gnu.org

--- Comment #3 from Wilco <wilco at gcc dot gnu.org> ---
(In reply to Gabriel Ravier from comment #1)
> Inversely, I'd also suggest doing the opposite. That is, if there is no
> hardware popcount instruction, `__builtin_popcount(v) == 1` should be
> optimized to `v && !(v & (v - 1))`

I actually posted a patch for this and popcount(x) > 1 given the reverse
transformation is faster on all targets - even if they have popcount
instruction (since they are typically more expensive). This is true on x86 as
well, (x-1) <u (x & -x) is never slower than using popcount.

So I suggest not to have LLVM emit popcount for this is there a popcount
instruction since that is non-optimal for pretty much every target.

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

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

* [Bug middle-end/94787] Failure to detect single bit popcount pattern
  2020-04-27  8:35 [Bug tree-optimization/94787] New: Failure to detect single bit popcount pattern gabravier at gmail dot com
                   ` (2 preceding siblings ...)
  2020-04-29 12:52 ` wilco at gcc dot gnu.org
@ 2021-12-12 13:24 ` pinskia at gcc dot gnu.org
  2023-11-15 19:27 ` tavianator at gmail dot com
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-12-12 13:24 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
          Component|tree-optimization           |middle-end

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

* [Bug middle-end/94787] Failure to detect single bit popcount pattern
  2020-04-27  8:35 [Bug tree-optimization/94787] New: Failure to detect single bit popcount pattern gabravier at gmail dot com
                   ` (3 preceding siblings ...)
  2021-12-12 13:24 ` [Bug middle-end/94787] " pinskia at gcc dot gnu.org
@ 2023-11-15 19:27 ` tavianator at gmail dot com
  2024-03-04  7:45 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: tavianator at gmail dot com @ 2023-11-15 19:27 UTC (permalink / raw)
  To: gcc-bugs

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

Tavian Barnes <tavianator at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |tavianator at gmail dot com

--- Comment #4 from Tavian Barnes <tavianator at gmail dot com> ---
(In reply to Wilco from comment #3)
> I actually posted a patch for this and popcount(x) > 1 given the reverse
> transformation is faster on all targets - even if they have popcount
> instruction (since they are typically more expensive). This is true on x86
> as well, (x-1) <u (x & -x) is never slower than using popcount.

There's also these:

(x-1) <u (x ^ (x-1))

-x >u (x ^ -x)

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

* [Bug middle-end/94787] Failure to detect single bit popcount pattern
  2020-04-27  8:35 [Bug tree-optimization/94787] New: Failure to detect single bit popcount pattern gabravier at gmail dot com
                   ` (4 preceding siblings ...)
  2023-11-15 19:27 ` tavianator at gmail dot com
@ 2024-03-04  7:45 ` pinskia at gcc dot gnu.org
  2024-03-04 16:38 ` pinskia at gcc dot gnu.org
  2024-03-04 16:42 ` pinskia at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-04  7:45 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |pinskia at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note the expansion part is handled by r14-5612, r14-5613, and r14-6940 .

So now we just need the match part which I will handle for 15.

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

* [Bug middle-end/94787] Failure to detect single bit popcount pattern
  2020-04-27  8:35 [Bug tree-optimization/94787] New: Failure to detect single bit popcount pattern gabravier at gmail dot com
                   ` (5 preceding siblings ...)
  2024-03-04  7:45 ` pinskia at gcc dot gnu.org
@ 2024-03-04 16:38 ` pinskia at gcc dot gnu.org
  2024-03-04 16:42 ` pinskia at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-04 16:38 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #5)
> Note the expansion part is handled by r14-5612, r14-5613, and r14-6940 .
> 
> So now we just need the match part which I will handle for 15.

Actually the expansion part is not fully complete.
```
int f(int a)
{
        return __builtin_popcount(a) <= 1;
}

int f1(int a)
{
        return __builtin_popcount(a) == 1;
}
```

f1 is handled but f is not.
f should expand to `!(v & (v - 1))`.

The other match patterns needed:
```
int g(int a)
{
        if (a == 0) return 0;
        return __builtin_popcount(a) <= 1;
}

int g1(int a)
{
        if (a == 0) return 1;
        return __builtin_popcount(a) <= 1;
}
```

g should be transformed into just `__builtin_popcount(a) == 1`
and g1 should be transformed into just `__builtin_popcount(a) <= 1`.
Both during phi-opt.

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

* [Bug middle-end/94787] Failure to detect single bit popcount pattern
  2020-04-27  8:35 [Bug tree-optimization/94787] New: Failure to detect single bit popcount pattern gabravier at gmail dot com
                   ` (6 preceding siblings ...)
  2024-03-04 16:38 ` pinskia at gcc dot gnu.org
@ 2024-03-04 16:42 ` pinskia at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-04 16:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
And add:
```
int h(int a)
{
        if (a == 0) return 0;
        return __builtin_popcount(a) == 1;
}

int h1(int a)
{
        if (a == 0) return 1;
        return __builtin_popcount(a) == 1;
}
```

h should be just `__builtin_popcount(a) == 1`.
While h1 should be just `__builtin_popcount(a) <= 1`.

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

end of thread, other threads:[~2024-03-04 16:42 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-27  8:35 [Bug tree-optimization/94787] New: Failure to detect single bit popcount pattern gabravier at gmail dot com
2020-04-27  8:56 ` [Bug tree-optimization/94787] " gabravier at gmail dot com
2020-04-27 10:28 ` rguenth at gcc dot gnu.org
2020-04-29 12:52 ` wilco at gcc dot gnu.org
2021-12-12 13:24 ` [Bug middle-end/94787] " pinskia at gcc dot gnu.org
2023-11-15 19:27 ` tavianator at gmail dot com
2024-03-04  7:45 ` pinskia at gcc dot gnu.org
2024-03-04 16:38 ` pinskia at gcc dot gnu.org
2024-03-04 16:42 ` pinskia 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).