<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/78744>78744</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Eagerly computed values (and loads) are potentially discarded
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
thetheodor
</td>
</tr>
</table>
<pre>
https://godbolt.org/z/q5rWGroxT
Given the following code:
```C
int foo(int a, int b){
if (a > 0)
return 0;
return a * b;
}
```
we get:
```asm
foo: # @foo
imull %edi, %esi
xorl %eax, %eax
testl %edi, %edi
cmovlel %esi, %eax
retq
```
If `foo` is called with a positive integer then the result of `imull %edi, %esi` is never used. If `foo` is inlined into another function there's more potential for "wasted work", e.g.:
```C
int bar(int *a, int *b, int n){
int s = 0;
for (int i = 0; i < n; ++i)
s += foo(a[i], b[i]);
return s;
}
```
We get this loop (with `-Os`):
```asm
.LBB1_4: # =>This Inner Loop Header: Depth=1
movl (%rdi,%r8,4), %r9d
movl (%rsi,%r8,4), %r10d
imull %r9d, %r10d
testl %r9d, %r9d
cmovgl %edx, %r10d
addl %r10d, %eax
incq %r8
cmpq %r8, %rcx
jne .LBB1_4
retq
```
`movl (%rsi,%r8,4), %r10d` and ` movl (%rsi,%r8,4), %r10d` are unconditionally executed even if they are not needed.
If I inline `foo` manually:
```C
int bar2(int *a, int *b, int n){
int s = 0;
for (int i = 0; i < n; ++i){
int c;
if (a[i] > 0)
c = 0;
else
c = a[i] * b[i];
s += c;
}
return s;
}
```
then the loop becomes:
```asm
.LBB2_4: # =>This Inner Loop Header: Depth=1
movl (%rdi,%rdx,4), %r8d
movl $0, %r9d
testl %r8d, %r8d
jg .LBB2_6
imull (%rsi,%rdx,4), %r8d
movl %r8d, %r9d
.LBB2_6: # in Loop: Header=BB2_4 Depth=1
addl %r9d, %eax
incq %rdx
cmpq %rdx, %rcx
jne .LBB2_4
```
here the second load and multiplication only happen if their result is needed.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEVk1vGzcQ_TXUZRBhl_sh6aCDZUWpgQC9BMix4C5HWqYUqZBcW-qvL4b7IWnlNClQoIZhczkzb4bke0MK79XBIK5ZsWHFdiba0Fi3Dg2GBq20blZZeVk3IZw8y54Y3zG-O1hZWR3m1h0Y3_3F-O574b5-cvb8hSVbljx1fz-pVzQQGoS91dq-KXOA2koknBs3Vibd73P3rUyAvbWML2kkGH8GGlSMr9hi0_kAgNoD40sBLPsICdkGA4DD0DoDCcuu7v2cAMafoBotbLGdVHFb2hvCAQPLnmDiJPyxm6FCycwzYHlCX31CdWy1BsYLlIqWQCOvBuvZus4ozoNRnHtjQB-mkXKMrI_2VaMe8KbBtM7vw3iyJuhXAS97MlGxZQLKQy20RglvKjQg4GS9CuoVadfxgI5OsDtGh77VAWwM_8ECO0SDr-ig9SjnD9mU0cqgJHgLwtjQoIN9a-qgbMzjkPGFh6N1CCcb0AQlNOytA8b5m_CBarXuT8Y5Jcb5YT5SCqZs6rgjXE8nxp9GRjH-VA1jM2FXnPTAsu09jfoyIpYazXH4DIaGjG8Y36h7RgIQGN9QQMdtwYqNYsWWCqjG8eouV89Z_1O2fo08hdAoD9raE1UYT5OVyYffPfkR9FRwI4vnnzeb9I98ZHK2ZdnHL4T2Ygw6-EyYv6GQ6Mhni6fQsGybDqUSJykn44WLbKDBkvHnnPJ25HAr-Z67_5F7msgHLRHGg3nUy431mov0cuh5en4HW0ipx7kHNSlTf4euuhHvNMz0YPXo_c0gDFv5IMdpgymTX96GMgFhJB3mr28dxTiE1tTWSEXSElpfAM9YtyQgpN6s9qS3S_Q0NoBBlCjnty3wZQ8vvWRvZHwUpiW8d1rjTRevhOP_h-5u8QbM-g4NrhdIr73pPVK_UwP9oPZ4PwO97xUp3jC9pKfxYxu4L2hU9r_S_diZo-grrO0R_U90zv9rnUdZ3dJvORV6njzq8qrapXwM_HaArtZy2gPumf_z3Lf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