<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/117938>117938</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Failure to fuse adjacent loops
</td>
</tr>
<tr>
<th>Labels</th>
<td>
missed-optimization
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Kmeakin
</td>
</tr>
</table>
<pre>
`src` calculates the sum of a slice of integers, then the product. `tgt` does the same in a single loop. LLVM should fuse the two loops into one, since they are side effect free
https://godbolt.org/z/Kdd7z4c88
```c
#include <stddef.h>
#include <stdint.h>
typedef uint32_t u32;
struct tuple {
u32 sum;
u32 prod;
};
struct tuple src(u32* p, size_t len) {
u32 sum = 0;
u32 prod = 1;
for (size_t i = 0; i < len; i++) {
sum += p[i];
}
for (size_t i = 0; i < len; i++) {
prod *= p[i];
}
return (struct tuple){.sum = sum, .prod = prod};
}
struct tuple tgt(u32* p, size_t len) {
u32 sum = 0;
u32 prod = 1;
for (size_t i = 0; i < len; i++) {
sum += p[i];
prod *= p[i];
}
return (struct tuple){.sum = sum, .prod = prod};
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzElMtu6zYQhp-G2gwiSEPblBda-FJtTrrtNqDEkc2UEgVxmCJ--oLyJW0aFAGK4hAETHJm_p_-bI4OwZ5Golqs92J9zHTks5_rHwPp3-2Ytd6812JThLkTmwI67broNFMAPhOEOIDvQUNwtqO0tCPTieYg8JAyxiVtmr2JHecgNgWfOAkZf5fQA4Edk4YdT47AeT_l8Pz8268Qzj46A30MtOTyH34Jh2TjwY-UbIIduyX-DnomCNYQUN9Tx9DPRKLYiWJ3Zp6CkDuBjcDm5E3rHed-PglsLgKbH8aoy6qrqpS-Ka6zSxuUduxcNARCHgIbQ31-FvKXr2J25Ees2PH7RIZ6iHZkiS8MUaKQ-2sw8Bw7Bo6TIxAqnaZ4QnrNAYDlIMG7Vanj1-Xpx8EqqeMOpiuTC70wOBoFbj_Jg5BHKP5pspyXD4cU6v0MAqubmn1ULsvDIp82AvfLfDgtLrhP6ZNY761YHz_80tf4zw7XC-PuGxYzcZzHxeUv0ARuhdrndx4JOx4gf4BYsD-A3_X-hj39lb-B_Yb4M_n_Aftt_Cv9NH4KvPujykwtzVZudUZ1qSSqVVVJzM51oSol-76X5UaXVG6or7arXplCqx7bHjNbY4GrskSFslAl5qoita6o1bQpt6ZVYlXQoK3LnXsb0uPObAiR6rJUW1llTrfkwtLoEAcbApknP7Ed7EWz9aNATB1wrlP5UxtPQawKZwOHD0G27KhutHVxJmB_bU7avOqORr52pyzOrv7UcCyfY5t3fhDYJLHbx9M0-1fqWGCzXDUIbG63favxzwAAAP__2Hudtg">