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

DCE affected by applying De Morgan's law

Open ThomasMayerl opened this issue 3 years ago • 0 comments

Sometimes the compiler's ability to detect and eliminate dead code is affected by applying De Morgan's law.

In the following snippet, the compiler detects the dead code and thus removes it:

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

void DCEMarker0_();

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

Now, De Morgan's law is applied. However, LLVM (14) is not able anymore to detect and eliminate the dead code:

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

void DCEMarker0_();

void f(bool x, bool x5, bool c) {
    if ((((x && !x) || !(x || x5 || !c)) && !(c || 0) == !x5)) {
        DCEMarker0_();
    }
}

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

(Might be related to #57120)

ThomasMayerl avatar Aug 16 '22 12:08 ThomasMayerl