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

    <tr>
        <th>Summary</th>
        <td>
            Expressions are better optimised by using nested if statements
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    In some cases, the compiler's ability to eliminate dead code depends on whether the if statement is nested or not.

LLVM detects that the if expressions in the following code snippet evaluate to false and thus removes the dead code:

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

static void __attribute__ ((noinline)) DCEMarker0_() {printf("DCE2.0");}

void f(unsigned s, unsigned c) {
    if (!c) {
        if (!s && s) {
            DCEMarker0_();
        }
    }
}
```
In the following snippet the nested if expressions are replaced by one if statement. However, LLVM cannot eliminate the dead code anymore:

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

static void __attribute__ ((noinline)) DCEMarker0_() {printf("DCE2.0");}

void f(unsigned s, unsigned c) {
    if (!c && (!s && s)) {
        DCEMarker0_();
    }
}
```

Note: This difference can only be seen if the datatype is set to unsigned instead of bool (Might be related to: #56653) and the order of the subexpressions in the if is not changed (Might be related to: #56654).

This was tested using LLVM 14.0.6. The described behaviour can also be seen via the following Compiler Explorer link: https://godbolt.org/z/jGo3Wxq5j
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztlc1y2zgMgJ9GunCq0a8tHXRonKSbmWZPnd2jhxJhiylNqgRlx_v0C1KxY7vZ7QvUQ0uCSAIg8AHqjDi2T5qh2QHrOQJG-Yq5gQSzG6UCG-VLZLyTSrojc4aBkjupuQMmgAtaJvzTCFogM5odBqDdNqiQG4aOVu5AOyaRaUAHghnLtHFJlN5H6ef5-vXrX8-kxUHvkLZyd9oPr6MFRGk0MqnD241Ryhyk3s62UctxBMdgz9Xk3SIfN1whMK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