public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/106505] New: DCE depends on whether if or else is used
@ 2022-08-02 11:38 tmayerl at student dot ethz.ch
  2022-08-07 20:04 ` [Bug tree-optimization/106505] " pinskia at gcc dot gnu.org
  2023-09-17  5:42 ` pinskia at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: tmayerl at student dot ethz.ch @ 2022-08-02 11:38 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106505
           Summary: DCE depends on whether if or else is used
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tmayerl at student dot ethz.ch
  Target Milestone: ---

In some cases, the compiler's ability to eliminate dead code depends on whether
the if expression is left as it is or negated and the body moved to the else
block. 

GCC detects that the if expressions in the following code snippet evaluate to
false and thus removes the dead code:

#include <stdio.h>
#include <stdbool.h>

void DCEMarker0_();

void f(bool s, bool c) {
    if (!c == !s) {
        if (s && !c) {
            DCEMarker0_();
        }
    }
}

In the following snippet, the inner if has been negated and the body has been
moved to the else block. However, GCC cannot eliminate the dead code anymore:

#include <stdio.h>
#include <stdbool.h>

void DCEMarker0_();

void f(bool s, bool c) {
    if (!c == !s) {
        if (!(s && !c)) {}
        else {
            DCEMarker0_();
        }
    }
}

This can also be seen via the following Compiler Explorer link:
https://godbolt.org/z/P4a61nsdq

If s and c in the inner if expression are swapped, it is the other way round.
Without the else, the compiler cannot eliminate the dead code:

#include <stdio.h>
#include <stdbool.h>

void DCEMarker0_();

void f(bool s, bool c) {
    if (!c == !s) {
        if (c && !s) {
            DCEMarker0_();
        }
    }
}

However, it suddenly can optimise the code when the if expression is negated
and the body is moved to the else:

#include <stdio.h>
#include <stdbool.h>

void DCEMarker0_();

void f(bool s, bool c) {
    if (!c == !s) {
        if (!(c && !s)) {}
        else {
            DCEMarker0_();
        }
    }
}

This can also be seen via the following Compiler Explorer link:
https://godbolt.org/z/95jo3Gv45

Due to the order issue this might be related to the following bug
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106379

However, this example only works with C as a source language. With C++, it is
fine. Thus, this might be related to the following bug
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106381

Another example is given below. In this case, only one variable is needed.
However, this only works with the datatype unsigned instead of bool. Therefore,
it might be related to the following bug
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106380

Version with dead code eliminated:

#include <stdio.h>
#include <stdbool.h>

void DCEMarker0_();

void f(unsigned s) {
    if (!s == !!s) {
        DCEMarker0_();
    }
}

Version with dead code not eliminated:

#include <stdio.h>
#include <stdbool.h>

void DCEMarker0_();

void f(unsigned s) {
    if (!(!s == !!s)) {}
    else {
        DCEMarker0_();
    }
}

This can also be seen via the following Compiler Explorer link:
https://godbolt.org/z/zYfoTs3PE

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

* [Bug tree-optimization/106505] DCE depends on whether if or else is used
  2022-08-02 11:38 [Bug tree-optimization/106505] New: DCE depends on whether if or else is used tmayerl at student dot ethz.ch
@ 2022-08-07 20:04 ` pinskia at gcc dot gnu.org
  2023-09-17  5:42 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-08-07 20:04 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

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

* [Bug tree-optimization/106505] DCE depends on whether if or else is used
  2022-08-02 11:38 [Bug tree-optimization/106505] New: DCE depends on whether if or else is used tmayerl at student dot ethz.ch
  2022-08-07 20:04 ` [Bug tree-optimization/106505] " pinskia at gcc dot gnu.org
@ 2023-09-17  5:42 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-17  5:42 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2023-09-17
             Status|UNCONFIRMED                 |ASSIGNED
     Ever confirmed|0                           |1
           Assignee|unassigned at gcc dot gnu.org      |pinskia at gcc dot gnu.org

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

end of thread, other threads:[~2023-09-17  5:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-02 11:38 [Bug tree-optimization/106505] New: DCE depends on whether if or else is used tmayerl at student dot ethz.ch
2022-08-07 20:04 ` [Bug tree-optimization/106505] " pinskia at gcc dot gnu.org
2023-09-17  5: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).