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

    <tr>
        <th>Summary</th>
        <td>
            [clang] Constant "(a && 0)" not folded at -O0
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          pbo-linaro
      </td>
    </tr>
</table>

<pre>
    While working on QEMU, we recently exposed a missed constant folding with ``clang -O0``.

Given this code:
```
$ cat main.c
extern int f(void);
int main(int argc) {
  if (argc && 0)
    f();
}
$ clang -O0 main.c
/usr/bin/ld: /tmp/main-19bcf0.o: in function `main':
main.c:(.text+0x21): undefined reference to `f'
```

Since ``argc && 0`` is always false, it could be expected (though not required by the standard) that branch is eliminated and thus, `f` should not be referenced. Some code on QEMU expect dead code elimination to happen, to not cross reference some symbols.
When changing condition to ``(0 && argc)``, the branch is removed as expected.

Original discussion on QEMU side: https://lore.kernel.org/qemu-devel/95b00393-bdd2-4db3-ac39-02a09f83b4d7@linaro.org/
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJxcVEGP4zYP_TXKhUig0LFjH3zIzHz-TsWiWBR7liXaVleWspKcmfz7gk680ykQIDIpPj4-UlQp2dETtaJ8EeXbTi15CrG99mHvrFcx7Ppg7u2PyTqC9xB_Wj9C8PDn__74S-ArvBNE0uSzuwN9XEMiAwpmm_igg09Z-QxDcIYD322eQFRSVFI75UfYf5OPz4OQFyEv_7c38pAnm0AHQ6Jg4-MG_-RF4Am0yjAr6w9ayAt9ZIoeLGcRWN-CNQIbUbwIeWEjXxRY81HFUQtsQJzZCWAHEFizFQRWAiuQHMouWMF-A4nz25Z7o_3JQGC3pCiw6zlT54woLiCwy_NVYMfX9sem14M8BPZYD8PidbbBsxQPfudHpU_M4iKwPmT6yAJf5AceVyIXWLyhwXoyEGmgSF4T5MA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