public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/98956] New: Failure to optimize out boolean left shift
@ 2021-02-03 16:03 gabravier at gmail dot com
  2021-02-04  7:45 ` [Bug tree-optimization/98956] " rguenth at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: gabravier at gmail dot com @ 2021-02-03 16:03 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 98956
           Summary: Failure to optimize out boolean left shift
           Product: gcc
           Version: 11.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(bool c)
{
    return c << 1;
}

This can be optimized to `return c;`. This transformation is done by LLVM, but
not by GCC.

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

* [Bug tree-optimization/98956] Failure to optimize out boolean left shift
  2021-02-03 16:03 [Bug tree-optimization/98956] New: Failure to optimize out boolean left shift gabravier at gmail dot com
@ 2021-02-04  7:45 ` rguenth at gcc dot gnu.org
  2021-07-16 23:06 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-02-04  7:45 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2021-02-04
             Status|UNCONFIRMED                 |NEW

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Can be generalized to more arithmetic ops.

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

* [Bug tree-optimization/98956] Failure to optimize out boolean left shift
  2021-02-03 16:03 [Bug tree-optimization/98956] New: Failure to optimize out boolean left shift gabravier at gmail dot com
  2021-02-04  7:45 ` [Bug tree-optimization/98956] " rguenth at gcc dot gnu.org
@ 2021-07-16 23:06 ` pinskia at gcc dot gnu.org
  2021-11-17  4:59 ` navidrahimi at microsoft dot com
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-07-16 23:06 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(for cmp (eq ne)
 (simplify
  (cmp (lshift truth_value@0 INTEGER_CST@1) zero_p@2)
  (cmp @0 @2)))

Something like the above, might apply to rotate too.

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

* [Bug tree-optimization/98956] Failure to optimize out boolean left shift
  2021-02-03 16:03 [Bug tree-optimization/98956] New: Failure to optimize out boolean left shift gabravier at gmail dot com
  2021-02-04  7:45 ` [Bug tree-optimization/98956] " rguenth at gcc dot gnu.org
  2021-07-16 23:06 ` pinskia at gcc dot gnu.org
@ 2021-11-17  4:59 ` navidrahimi at microsoft dot com
  2021-11-30 21:55 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: navidrahimi at microsoft dot com @ 2021-11-17  4:59 UTC (permalink / raw)
  To: gcc-bugs

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

Navid Rahimi <navidrahimi at microsoft dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |navidrahimi at microsoft dot com

--- Comment #3 from Navid Rahimi <navidrahimi at microsoft dot com> ---
I am sending a patch for this:

/* cmp : ==, != */
/* ((B0 << CST) cmp 0) -> B0 cmp 0 */
(for cmp (eq ne)
 (simplify
  (cmp (lshift (convert @0) INTEGER_CST@1) integer_zerop@2)
  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE) (cmp @0 @2))))


This does not apply to other arithmetic operations (at least it is not
verifiable). and for cases that it does apply to other arithmetic operators,
GCC already produces optimized code. You can play with the codegen I link
below:

Codegen:
https://compiler-explorer.com/z/nj4PTrecW

Proof:
https://alive2.llvm.org/ce/z/jyJAoS

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

* [Bug tree-optimization/98956] Failure to optimize out boolean left shift
  2021-02-03 16:03 [Bug tree-optimization/98956] New: Failure to optimize out boolean left shift gabravier at gmail dot com
                   ` (2 preceding siblings ...)
  2021-11-17  4:59 ` navidrahimi at microsoft dot com
@ 2021-11-30 21:55 ` pinskia at gcc dot gnu.org
  2022-08-15 16:41 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-11-30 21:55 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I just realized this is the same as PR 64992 which I almost have a patch. You
don't even need bools.

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64992#c8


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64992
[Bug 64992] More optimize opportunity

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

* [Bug tree-optimization/98956] Failure to optimize out boolean left shift
  2021-02-03 16:03 [Bug tree-optimization/98956] New: Failure to optimize out boolean left shift gabravier at gmail dot com
                   ` (3 preceding siblings ...)
  2021-11-30 21:55 ` pinskia at gcc dot gnu.org
@ 2022-08-15 16:41 ` cvs-commit at gcc dot gnu.org
  2022-09-10 21:10 ` roger at nextmovesoftware dot com
  2022-09-10 21:11 ` roger at nextmovesoftware dot com
  6 siblings, 0 replies; 8+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-08-15 16:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Roger Sayle <sayle@gcc.gnu.org>:

https://gcc.gnu.org/g:418b71c0d535bf91df78bad2e198c57934682eaa

commit r13-2048-g418b71c0d535bf91df78bad2e198c57934682eaa
Author: Roger Sayle <roger@nextmovesoftware.com>
Date:   Mon Aug 15 17:39:47 2022 +0100

    PR tree-optimization/64992: (B << 2) != 0 is B when B is Boolean.

    This patch resolves both PR tree-optimization/64992 and PR
    tree-optimization/98956 which are missed optimization enhancement
    request, for which Andrew Pinski already has a proposed solution
    (related to a fix for PR tree-optimization/98954).  Yesterday,
    I proposed an alternate improved patch for PR98954, which although
    superior in most respects, alas didn't address this case [which
    doesn't include a BIT_AND_EXPR], hence this follow-up fix.

    For many functions, F(B), of a (zero-one) Boolean value B, the
    expression F(B) != 0 can often be simplified to just B.  Hence
    "(B * 5) != 0" is B, "-B != 0" is B, "bswap(B) != 0" is B,
    "(B >>r 3) != 0" is B.  These are all currently optimized by GCC,
    with the strange exception of left shifts by a constant (possibly
    due to the undefined/implementation defined behaviour when the
    shift constant is larger than the first operand's precision).
    This patch adds support for this particular case, when the shift
    constant is valid.

    2022-08-15  Roger Sayle  <roger@nextmovesoftware.com>

    gcc/ChangeLog
            PR tree-optimization/64992
            PR tree-optimization/98956
            * match.pd (ne (lshift @0 @1) 0): Simplify (X << C) != 0 to X
            when X is zero_one_valued_p and the shift constant C is valid.
            (eq (lshift @0 @1) 0): Likewise, simplify (X << C) == 0 to !X
            when X is zero_one_valued_p and the shift constant C is valid.

    gcc/testsuite/ChangeLog
            PR tree-optimization/64992
            * gcc.dg/pr64992.c: New test case.

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

* [Bug tree-optimization/98956] Failure to optimize out boolean left shift
  2021-02-03 16:03 [Bug tree-optimization/98956] New: Failure to optimize out boolean left shift gabravier at gmail dot com
                   ` (4 preceding siblings ...)
  2022-08-15 16:41 ` cvs-commit at gcc dot gnu.org
@ 2022-09-10 21:10 ` roger at nextmovesoftware dot com
  2022-09-10 21:11 ` roger at nextmovesoftware dot com
  6 siblings, 0 replies; 8+ messages in thread
From: roger at nextmovesoftware dot com @ 2022-09-10 21:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98956
Bug 98956 depends on bug 64992, which changed state.

Bug 64992 Summary: More optimize opportunity
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64992

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

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

* [Bug tree-optimization/98956] Failure to optimize out boolean left shift
  2021-02-03 16:03 [Bug tree-optimization/98956] New: Failure to optimize out boolean left shift gabravier at gmail dot com
                   ` (5 preceding siblings ...)
  2022-09-10 21:10 ` roger at nextmovesoftware dot com
@ 2022-09-10 21:11 ` roger at nextmovesoftware dot com
  6 siblings, 0 replies; 8+ messages in thread
From: roger at nextmovesoftware dot com @ 2022-09-10 21:11 UTC (permalink / raw)
  To: gcc-bugs

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

Roger Sayle <roger at nextmovesoftware dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
                 CC|                            |roger at nextmovesoftware dot com
   Target Milestone|---                         |13.0
         Resolution|---                         |FIXED

--- Comment #6 from Roger Sayle <roger at nextmovesoftware dot com> ---
This should now be fixed on mainline.

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

end of thread, other threads:[~2022-09-10 21:11 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-03 16:03 [Bug tree-optimization/98956] New: Failure to optimize out boolean left shift gabravier at gmail dot com
2021-02-04  7:45 ` [Bug tree-optimization/98956] " rguenth at gcc dot gnu.org
2021-07-16 23:06 ` pinskia at gcc dot gnu.org
2021-11-17  4:59 ` navidrahimi at microsoft dot com
2021-11-30 21:55 ` pinskia at gcc dot gnu.org
2022-08-15 16:41 ` cvs-commit at gcc dot gnu.org
2022-09-10 21:10 ` roger at nextmovesoftware dot com
2022-09-10 21:11 ` roger at nextmovesoftware dot com

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).