<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - Missed optimization opportunity for loads with a dynamic index with a known small value range (e.g 0,1)."
href="https://bugs.llvm.org/show_bug.cgi?id=52392">52392</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Missed optimization opportunity for loads with a dynamic index with a known small value range (e.g 0,1).
</td>
</tr>
<tr>
<th>Product</th>
<td>new-bugs
</td>
</tr>
<tr>
<th>Version</th>
<td>unspecified
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>All
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>enhancement
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>new bugs
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>tra@google.com
</td>
</tr>
<tr>
<th>CC</th>
<td>alina.sbirlea@gmail.com, htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org
</td>
</tr></table>
<p>
<div>
<pre>Loads from an read-only array of known values with a dynamic index which is
known to have only few values (e.g 0 and 1), results in an unnecessary read
from memory.
E.g: <a href="https://godbolt.org/z/4r675d6dM">https://godbolt.org/z/4r675d6dM</a>
// This keeps 'a' on stack and reads return value from there.
int slow(int num, int v1, int v2) {
int a[2] = {v1, v2};
return a[num&1];
}
//slow (int, int, int):
// mov dword ptr [rsp - 8], esi
// mov dword ptr [rsp - 4], edx
// and edi, 1
// mov eax, dword ptr [rsp + 4*rdi - 8]
// ret
// If we add more code to manually fetch values from 'a', we end up
// with returning one v1/v1 directly and no longer need 'a'.
int fast(int num, int v1, int v2) {
int a[2] = {v1, v2};
int i0 = a[0];
int i1 = a[1];
return (num&1) ? i1:i0;
}
// fast (int, int, int):
// mov eax, esi
// test dil, 1
// cmovne eax, edx
// ret
Being able to get rid of local variables is rather important for performance
on GPUs (NVPTX, AMDGPU) and picking one of the few items in an array is a
fairly common pattern.</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>