Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DCE sometimes fails with depending if statements #57028

Closed
ThomasMayerl opened this issue Aug 9, 2022 · 2 comments
Closed

DCE sometimes fails with depending if statements #57028

ThomasMayerl opened this issue Aug 9, 2022 · 2 comments

Comments

@ThomasMayerl
Copy link

Sometimes, DCE fails when multiple if statements are used.

For example, LLVM detects that the following if statement always evaluates to false and thus removes the dead code:

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

void DCEMarker0_();

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

In the next snippet, the if statement is used to set a variable. This variable is then used in another if statement. However, LLVM now fails to detect and eliminate the dead code:

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

void DCEMarker0_();

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

This is actually a regression:
In LLVM 12 both snippets can be optimised.
In LLVM 13 neither snippet can be optimised.
In LLVM 14 only the top snippet can be optimised.

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

@rotateright
Copy link
Contributor

This example reduces to the same pattern as #56653, and it would be fixed with the same patch ( https://reviews.llvm.org/D131356 ).

@rotateright
Copy link
Contributor

Fixed with:
15e3d86

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants