<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/56291>56291</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
missed optimization in scalar multiplication of array that is never used again
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
mrvn
</td>
</tr>
</table>
<pre>
I have found a missed optimization in all versions of clang from 9.0 to 14.0.0 with -O2 and -O3. I have distilled the problem down to this test case:
```
constexpr int SIZE = 10000000;
static void mult(int* a, int b) {
for (int i = 0; i < SIZE; i++) a[i] *= b;
}
int main() {
int* a = new int[SIZE]{};
mult(a, 5);
delete[] a;
}
```
The array `a` is never used again after the call to `mult()` and `mult()` has no side effect apart from modifying `a`. The function call can be removed, which then allows the `new` and `delete` to be merged and removed.
Expected result: the equivalent of `int main() { }`
Actual result: `mult()` gets inlined and unrolled into a hugely complex SIMD operation.
PS: gcc manages to remove the function call.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx1VMFymzAQ_Rq47ITBwtj4wMGO05kcOulMeupNiAXUEYhKwo779V0JPHHihpEx0mrfvt19UqXrS_kMHT8hNHoaauDQS2uxBj062cu_3Ek9gByAKwUnNJamFnQDQvGhhcboHnZJCk7Dap2k9HWWroOHFwac4B5esgSWALW0TipF2K5DGI2uFPZQ6_PgvV0nLTi0DgS3GGX7KD1G6fW9SZcRpoI4OHwbDRFz8Pr86wmi7AirdH6i7HDrbB0lIeCkZQ39pFzECnKL2B54xB4DRBWxHUTbxQ3oabSBeSPIAO5Rw-djCBhmETuEsSOk_CCj_Eg-e7-7euewPd6S8YA9lwNh38W8sgrxBjyHhfwQwuVHv5WwshuHJZuQRk5wH4w1KnRI_p4W_4rPp8rO75_UH24MvwAZOP2AejMgtR8mrw3ecq-IxtGC76Xw4qAe0s6FkidDbl4Cd4sdJzANVtYI2DQoHPCRGzeLqde1bC6StLXETsDTaaZBBCmGWIIPUCEY7PUJa5_-uZOi82SCUvXZBmLkTnW8YbIUZRMESwg9mtYnRNYFLLktxNPbSPTQG63PIdsHWPwzyRNXSL2kk0Bo910FX-drVffCTVzdgNzVpEVnqd1KDgubaTA6nBWC1iSJbmpRXUDoflT4RhL8fqQjiiaczw-cf7z6CK0QxGjgLVqf6pxcIP-hkklcl1m9y3Y8dtIpLL84_ZY2cxMEJ0clxWyg5GeZuI67_2kknowqO-dG6w80-0ajpethqhJKhCZKna5_D3Qh_KZi05QoTGjpI9-w3SruSl4JZFw0WbVOq-22yIuGVQVbFet8tc3WLFa8QmXLWeyxLFnKWOqdi_WGpclGZHVRNA1bb6uGNatonSK1SyU-cKJNG5sycKim1pJR0U1l343cWtkOiFd8PrlOm7I3pyEOXMtA9B9ux52z">