public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/95643] New: Optimizer fails to realize that a variable tested twice in a row is the same both times
@ 2020-06-11 16:32 josephcsible at gmail dot com
  2020-06-11 18:27 ` [Bug tree-optimization/95643] " glisse at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: josephcsible at gmail dot com @ 2020-06-11 16:32 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 95643
           Summary: Optimizer fails to realize that a variable tested
                    twice in a row is the same both times
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: josephcsible at gmail dot com
  Target Milestone: ---

Consider this code, compiled at -O3:

extern int e;
void f(int x, int y) {
    if(y) {
        if(y && !x) __builtin_unreachable();
        if(x) ++e;
    }
}

GCC 10.1 on AMD64 produces the following assembly:

f:
        testl   %edi, %edi
        je      .L1
        testl   %esi, %esi
        jne     .L10
.L1:
        ret
.L10:
        addl    $1, e(%rip)
        ret

Godbolt link: https://godbolt.org/z/Z75QTM
The "y" in "if(y && !x)" is necessarily true, but GCC doesn't realize this,
since changing "if(y && !x)" to the equivalent "if(!x)" results in much better
assembly:

f:
        testl   %esi, %esi
        je      .L1
        addl    $1, e(%rip)
.L1:
        ret

We should be able to generate this assembly even with the redundant check of
"y".

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

* [Bug tree-optimization/95643] Optimizer fails to realize that a variable tested twice in a row is the same both times
  2020-06-11 16:32 [Bug tree-optimization/95643] New: Optimizer fails to realize that a variable tested twice in a row is the same both times josephcsible at gmail dot com
@ 2020-06-11 18:27 ` glisse at gcc dot gnu.org
  2020-06-12  7:33 ` rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: glisse at gcc dot gnu.org @ 2020-06-11 18:27 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Marc Glisse <glisse at gcc dot gnu.org> ---
After FRE1 we have

  _2 = x_9(D) == 0;
  if (_2 != 0)

so we assert things for _2 and not x_9, and we lose the __builtin_unreachable
information in CCP2.

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

* [Bug tree-optimization/95643] Optimizer fails to realize that a variable tested twice in a row is the same both times
  2020-06-11 16:32 [Bug tree-optimization/95643] New: Optimizer fails to realize that a variable tested twice in a row is the same both times josephcsible at gmail dot com
  2020-06-11 18:27 ` [Bug tree-optimization/95643] " glisse at gcc dot gnu.org
@ 2020-06-12  7:33 ` rguenth at gcc dot gnu.org
  2021-05-30 22:48 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-06-12  7:33 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rguenth at gcc dot gnu.org
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2020-06-12
             Status|UNCONFIRMED                 |NEW
           Keywords|                            |easyhack

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.  value-numbering doesn't use the alternate assertion discovery
code and thus for the partly simplified condition does not record the
appropriate
expressions.  Likewise EVRP lacks simple forward-propagation during this
assertion discovery (value-numbering would also need that) in
register_edge_assert_for (where it tests for defs like a & b and a | b
it needs to look for a simple boolean re-test/inversion as well).

Should be easy to amend for this case.

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

* [Bug tree-optimization/95643] Optimizer fails to realize that a variable tested twice in a row is the same both times
  2020-06-11 16:32 [Bug tree-optimization/95643] New: Optimizer fails to realize that a variable tested twice in a row is the same both times josephcsible at gmail dot com
  2020-06-11 18:27 ` [Bug tree-optimization/95643] " glisse at gcc dot gnu.org
  2020-06-12  7:33 ` rguenth at gcc dot gnu.org
@ 2021-05-30 22:48 ` pinskia at gcc dot gnu.org
  2023-08-09 22:15 ` pinskia at gcc dot gnu.org
  2023-08-09 22:16 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-05-30 22:48 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

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

* [Bug tree-optimization/95643] Optimizer fails to realize that a variable tested twice in a row is the same both times
  2020-06-11 16:32 [Bug tree-optimization/95643] New: Optimizer fails to realize that a variable tested twice in a row is the same both times josephcsible at gmail dot com
                   ` (2 preceding siblings ...)
  2021-05-30 22:48 ` pinskia at gcc dot gnu.org
@ 2023-08-09 22:15 ` pinskia at gcc dot gnu.org
  2023-08-09 22:16 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-09 22:15 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |11.0
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=95757

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

* [Bug tree-optimization/95643] Optimizer fails to realize that a variable tested twice in a row is the same both times
  2020-06-11 16:32 [Bug tree-optimization/95643] New: Optimizer fails to realize that a variable tested twice in a row is the same both times josephcsible at gmail dot com
                   ` (3 preceding siblings ...)
  2023-08-09 22:15 ` pinskia at gcc dot gnu.org
@ 2023-08-09 22:16 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-09 22:16 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Fixed for GCC 11 by r11-7448-gff92ede8d269375f800e1 .

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

end of thread, other threads:[~2023-08-09 22:16 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-11 16:32 [Bug tree-optimization/95643] New: Optimizer fails to realize that a variable tested twice in a row is the same both times josephcsible at gmail dot com
2020-06-11 18:27 ` [Bug tree-optimization/95643] " glisse at gcc dot gnu.org
2020-06-12  7:33 ` rguenth at gcc dot gnu.org
2021-05-30 22:48 ` pinskia at gcc dot gnu.org
2023-08-09 22:15 ` pinskia at gcc dot gnu.org
2023-08-09 22:16 ` 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).