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

    <tr>
        <th>Summary</th>
        <td>
            UBSan hangs at -O0 and misses a bug at -O1 and above
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          shao-hua-li
      </td>
    </tr>
</table>

<pre>
    For the following code, Clang trunk's UBsan hangs at -O0 and misses the out-of-bound at -O1 and above. GCC can detect it at all optimization levels.

I checked the IR at -O1 and found the reason is that `SCCP pass` removes the out-of-bound check.

Compiler explorer https://godbolt.org/z/4ebWzv5fe

```shell
% cat a.c
struct a {
  unsigned b
} c;
void main() {
  long d = 0;
  struct a e[10];
  for (;d<11; d++)
    e[d] = c;
}
%
% clang-17 -O0 -fsanitize=undefined a.c
Killed - processing time exceeded
% clang-17 -O1 -fsanitize=undefined a.c && ./a.out
%
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx8U12vnDYQ_TXDywhk7AtmH3gIIKqoD60aRX02eAA3xl5hs0nvr69gbzbbJIpkgTQf55yxz6gQzOyIaigaKLpE7XHxWx0W5dNlV6k1yeD1v3XvN4wL4eSt9Z-Nm3H0moC32FrlZozb7j4BlwE_NkE5XJSbA6qI6R8MldO4mhAonBh-j6mf0sHvTt9L8rNEDf5GGf7Wtjgqh5oijRFNPGqUteiv0azmVUXjHVq6kQ0ZsA7Yu_v3PY4LjZ9Inyzv_3rGnk6yI76RCt6hObSoiFCyD237J15VCFAy3Gj1t58JPbH_x9f69WosbUhfrtZvtOES4zWAeAe8B97PXg_exsxvM_D-FXj_QsPfr7diomcYKNn9hIWsfYvxAsdj7Gy8B0Lc9jGiQpDNPYK4u_PtNA5vTbLDEcRb_uaNxlUZB7wCfnlutN7NqBFEh-xRj_jgICianEHRPSUnv-GBJBoNos1zEA1q4M15Ll-r8OzVUHQn-jc1ILvHZE8jHt5Jc3m6JJ2CciaaVwLR7U7TZI7hHlfwu7GWNKZ43fxIIRwmjGYlpC8jkSb9U9z8F7gIvAReYga8V5nf4_cSv75MomuhL-KiEqrzUla5lKyokqUuprKo2IULWU2yHESuhWSyGi80ikmIIjE1Z1wwwSSrcimqrKqkFroU06GZDxJeGK3K2Mza23pYJTEh7FSXORcviVUD2XAuJ-eOPuOZBM6PXd3qoycd9jnAC7MmxPANJZpoqf7YfPjFMioc9vmHDUz2zdbfOdnEZR-y0a_A-4Pi7ZdeN_8PjRF4fwoLwPtT-H8BAAD__3ZdTNI">