<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/57299>57299</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            DCE improved when using dependant if statements 
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          ThomasMayerl
      </td>
    </tr>
</table>

<pre>
    Sometimes, the compiler's ability to detect and eliminate dead code improves when using dependant if statements. For example, LLVM cannot detect the dead code in the following example:

```c
#include <stdbool.h>

void DCEMarker0_();

void f(bool scr1_scr1_scr2, bool scr1, bool cr1_scr, bool r1, bool c) {
    if (scr1) {
        if (((!scr1_scr1_scr2 == !r1) && (r1 && !(scr1_scr1_scr2)))) {
            DCEMarker0_();
        }
    }
}
```

Now instead of the function call, a variable is assigned. As the if is dead, the variable is always false. Now we also add new if statements having the same formulas as before but we add a disjunction with this variable:

```c
#include <stdbool.h>

void DCEMarker0_();

void f(bool scr1_scr1_scr2, bool scr1, bool cr1_scr, bool r1, bool c) {
    int intermediate_result = 0;
    if (scr1) {
        if (((!scr1_scr1_scr2 == !r1) && (r1 && !(scr1_scr1_scr2)))) {
            intermediate_result = 1;
        }
    }
    if (scr1 || intermediate_result) {
        if (((!scr1_scr1_scr2 == !r1) && (r1 && !(scr1_scr1_scr2))) || intermediate_result) {
            DCEMarker0_();
        }
    }
}
```

LLVM is now able to eliminate the dead code. If the marker function call is added before the assignment within the if, DCE also breaks and generates calls for both markers. However, no additional checks are generated - so, LLVM knows that the if expressions are dependant on each other. 

```c
#include <stdbool.h>

void DCEMarker0_();

void f(bool scr1_scr1_scr2, bool scr1, bool cr1_scr, bool r1, bool c) {
    int intermediate_result = 0;
    if (scr1) {
        if (((!scr1_scr1_scr2 == !r1) && (r1 && !(scr1_scr1_scr2)))) {
            DCEMarker0_();
            intermediate_result = 1;
        }
    }
    if (scr1 || intermediate_result) {
        if (((!scr1_scr1_scr2 == !r1) && (r1 && !(scr1_scr1_scr2))) || intermediate_result) {
            DCEMarker0_();
        }
    }
}

```
This issue is the inverse to #57028. 

This behaviour was observed locally using -O3 and commit f3acb54c1b7b7721cea5c07f64194151dddd3c4b

It was also observed using CompilerExplorer: https://godbolt.org/z/GGMEEMo36

(Might be related to #56711)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztV0uP2zYQ_jXyZbCCnpZ90CGxvWmAuD00yHVBiSOLWUo0SMpe99d3SEt-5IGmhyBBEIGWPSTnyflm6ErxU_m36tCKDk2QrMC2CLXq9kKiDpLCAKuEFPYEVgFHi7UF1nNAKTrRM4s0yThxcATR7bU6oIFjiz0MRvQ7Wt1jz1lvQTRgLDF02FsTwqPSgC-s20t0at-9-7CFmvW9spMaZ8mN8N5PNEpKdXSSJ-b0VRCtg2h6z6PzqEc6SUVfy4EkBOnKWF4pJcM2SDe3XAclOKxXmy3Tz6ijpyBZBMkySF9_tqmhJScCTK3jp-mVOBcu0xdiXL3Qt0skH4JiVAD0UHxI9pn_k6Xr8jTie_Xk25oG7YhH9mROw3Ho-ErEo4Jbu5eX8blO93w9KtOOoFhfJy7E9cd0Irex_FMd6USNdcermvPJDn1theopC6R0cWJwYFqwStLhUxoaI3Y98hBeGb-fAkLTLkGmtL3bLo_sZKBh0mAITt0RadIoYJxDj8f7fISWHVxSOTGGdS7PdDdI5vRChUQhVIP1QoifARfm42TwUdiWOEnrZMGvk5QOuL1F3SEXFKwnjWaQ1uUbRHeZ8JOm79eMj78xje9do_kVjS9J_RFe_19zvh-effkmAPQENA9B6hbXFnFXyUN4ewZ85824x72HLufIJ9C5jWfoO5x6qI2dQDQub8mdM6wrjezZ-N60wx416TVepHFYpvwmjJ41UvP5Qx3xgB4Dva8IwlnACAEt1k4KaZ6kcHgAoy496plcdAWI2akK4cueYm5IwJnx2vLIKWR1C6QbdQi_S8JPURL-GwG_C8d3KRxfKh_vXdsUx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