<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/63473>63473</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[ADCE] Dead-code elimination fails when increasing loop count beyond threshold
</td>
</tr>
<tr>
<th>Labels</th>
<td>
backend:X86,
loopoptim,
missed-optimization
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
antoniofrighetto
</td>
</tr>
</table>
<pre>
There is a missed opportunity to remove the following dead code on latest Clang when restraining optimizations to `O2`, that GCC seems to catch via CDDCE (Godbolt [here](https://godbolt.org/z/97aqvfaqY)).
```c
#define SIZE 100
void square(float x[SIZE], const float A[SIZE][SIZE], const int b[SIZE]) {
float y[SIZE];
for (int i = 0; i < SIZE; ++i)
for (int j = 0; j < SIZE; ++j)
y[j] += A[j][i] * b[i];
}
```
Dumping the LLVM IR at the different stages seems to reveal that the cause may stem from the current tradeoff on the threshold of loop unrolling. With `O2` and `--unroll-threshold=13000`, the optimization is accomplished by BDCE. Indeed when `SIZE` is relatively small (< 40, different between `O2` and `O3`) the optimization succeeds.
Yet, while adjusting `--unroll-threshold-aggressive` option might provide a more balanced tradeoff in `O3` scenario, I believe this would be an orthogonal solution, as the core issue seems to lie within ADCE (which does some control dependence analysis).
Very minor, but I'm tagging x86-64-backend as well, since, for some reason, only on x86_64 the above seems to be lowered into a dead loop that increments [`rax` till reaching zero](https://godbolt.org/z/xKjeE51Es) on small sizes (though this would likely be solved if it were DCE'd).
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVU2P4jgT_jXmUgIFh_Bx4NCQZtR659VIu6vZnb2snLiSmHFcjO1A079-VU4LmI_DtlqA7SrXU089VVYhmNYhbkWxE0U5UUPsyG-Vi-QMNd60HcZIk4r0dftHhx7BBFDQmxBQA51O5OPgTLxCJPDY0xkhdggNWUsX41rQqDTUpBHIgVURQ4S9Va6FS4cOPIbolXFsSq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