public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/106727] New: Missed fold / canonicalization for checking if a number is a power of 2
@ 2022-08-24  2:51 llvm at rifkin dot dev
  2022-08-24  3:06 ` [Bug middle-end/106727] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: llvm at rifkin dot dev @ 2022-08-24  2:51 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106727
           Summary: Missed fold / canonicalization for checking if a
                    number is a power of 2
           Product: gcc
           Version: 12.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: llvm at rifkin dot dev
  Target Milestone: ---

These two are equivalent but generate different code:

bool foo(unsigned n) {
    return std::popcount(n) <= 1;
}
bool bar(unsigned n) {
    return (n & (n - 1)) == 0;
}

https://godbolt.org/z/crrxnfWo7

For systems that don't have popcount instructions it would be very beneficial
to transform foo -> bar. For systems that do have popcount instructions it
still would be beneficial to canonicalize the two.

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

* [Bug middle-end/106727] Missed fold / canonicalization for checking if a number is a power of 2
  2022-08-24  2:51 [Bug tree-optimization/106727] New: Missed fold / canonicalization for checking if a number is a power of 2 llvm at rifkin dot dev
@ 2022-08-24  3:06 ` pinskia at gcc dot gnu.org
  2022-08-24  7:21 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-08-24  3:06 UTC (permalink / raw)
  To: gcc-bugs

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

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] 5+ messages in thread

* [Bug middle-end/106727] Missed fold / canonicalization for checking if a number is a power of 2
  2022-08-24  2:51 [Bug tree-optimization/106727] New: Missed fold / canonicalization for checking if a number is a power of 2 llvm at rifkin dot dev
  2022-08-24  3:06 ` [Bug middle-end/106727] " pinskia at gcc dot gnu.org
@ 2022-08-24  7:21 ` rguenth at gcc dot gnu.org
  2024-03-04 22:57 ` pinskia at gcc dot gnu.org
  2024-03-04 22:58 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-08-24  7:21 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2022-08-24

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.  Expanding __builtin_popcount (n) <= 1 as (n & (n - 1)) == 0 might
be already done.  The canonicalization could be applied if .POPCOUNT is
available.

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

* [Bug middle-end/106727] Missed fold / canonicalization for checking if a number is a power of 2
  2022-08-24  2:51 [Bug tree-optimization/106727] New: Missed fold / canonicalization for checking if a number is a power of 2 llvm at rifkin dot dev
  2022-08-24  3:06 ` [Bug middle-end/106727] " pinskia at gcc dot gnu.org
  2022-08-24  7:21 ` rguenth at gcc dot gnu.org
@ 2024-03-04 22:57 ` pinskia at gcc dot gnu.org
  2024-03-04 22:58 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-04 22:57 UTC (permalink / raw)
  To: gcc-bugs

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

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 #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Mine.

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

* [Bug middle-end/106727] Missed fold / canonicalization for checking if a number is a power of 2
  2022-08-24  2:51 [Bug tree-optimization/106727] New: Missed fold / canonicalization for checking if a number is a power of 2 llvm at rifkin dot dev
                   ` (2 preceding siblings ...)
  2024-03-04 22:57 ` pinskia at gcc dot gnu.org
@ 2024-03-04 22:58 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-04 22:58 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Richard Biener from comment #1)
> Confirmed.  Expanding __builtin_popcount (n) <= 1 as (n & (n - 1)) == 0
> might be already done.  The canonicalization could be applied if .POPCOUNT
> is available.

No, it is not already done, expanding `__builtin_popcount (n) == 1` is done
(and including if n is known not to include 0 which is exapnded as `n & (n - 1)
== 0`)

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

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

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-24  2:51 [Bug tree-optimization/106727] New: Missed fold / canonicalization for checking if a number is a power of 2 llvm at rifkin dot dev
2022-08-24  3:06 ` [Bug middle-end/106727] " pinskia at gcc dot gnu.org
2022-08-24  7:21 ` rguenth at gcc dot gnu.org
2024-03-04 22:57 ` pinskia at gcc dot gnu.org
2024-03-04 22:58 ` 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).