llvm-project
llvm-project copied to clipboard
DCE affected by inserting double negations
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
Using -O1 DCE is still happening with GCC but not with Clang. This might be by design though.