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

            Bug ID: 106570
           Summary: DCE sometimes fails with depending if statements
           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: ---

Sometimes, DCE fails when multiple if statements are used. 

For example, GCC detects that the following if statements always 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 next snippet, the if statements are used to set a variable. This
variable is then used in the next if statement. However, GCC now fails to
detect and eliminate the dead code:

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

void DCEMarker0_();

void f(bool s, bool c) {
    int intermediate_result = 0;
    if (!c == !s) {
        if (s && !c) {
            intermediate_result = 1;
        }
    }
    if (((!c == !s) && (s && !c)) || intermediate_result) {
        DCEMarker0_();
    }
}

This is actually a regression: It works fine until GCC 11.3.

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

Reply via email to