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

            Bug ID: 106474
           Summary: DCE depends on how if expressions are nested
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          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 how if
expressions are nested.

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>

static void __attribute__ ((noinline)) DCEMarker0_() {printf("DCE2.0");}

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

In the following snippet the !c term is moved into the outer if expression.
However, GCC cannot eliminate the dead code anymore:

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

static void __attribute__ ((noinline)) DCEMarker0_() {printf("DCE2.0");}

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

In the following snippet, only the outer if statement is kept and all terms are
moved there. GCC is now able to eliminate the code again:

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

static void __attribute__ ((noinline)) DCEMarker0_() {printf("DCE2.0");}

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

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

Reply via email to