llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

DCE affected by inserting double negations

Open ThomasMayerl opened this issue 3 years ago • 1 comments

Sometimes the compiler's ability to detect and eliminate dead code decreases when double negations are inserted. LLVM detects that in the following snippet the if expression is always false and thus it can remove the dead code:

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

void DCEMarker0_();

void f(bool x, bool s, bool r) {
    if (((!x == !r) && (!1 == !(x || x)) && (!(x) || (s && !r)))) {
        DCEMarker0_();
    }
}

Now two double negations are inserted:

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

void DCEMarker0_();

void f(bool x, bool s, bool r) {
    if (((!x == !r) && (!1 == !(x || !(!(!(!(x)))))) && (!(x) || (s && !r)))) {
        DCEMarker0_();
    }
}

LLVM (14) is now unable to detect and remove the dead code.

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

ThomasMayerl avatar Aug 15 '22 12:08 ThomasMayerl

Using -O1 DCE is still happening with GCC but not with Clang. This might be by design though.

firewave avatar Aug 15 '22 13:08 firewave