https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115091
Bug ID: 115091
Summary: Support value speculation in frontend
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: andi-gcc at firstfloor dot org
Target Milestone: ---
This blog post describes an interesting optimization technique for memory
access. https://mazzo.li/posts/value-speculation.html
A linked list walk is often be limited by the latency of the L1 cache. When the
program can guess the next address (e.g. because the nodes are often allocated
sequentially in memory) it is possible to use construct like
if (node->next == node + 1)
node++;
else
node = node->next;
and rely on the CPU speculating the fast case.
However this often runs into problems with the compiler, e.g. for
next = node->next;
node++;
if (node != next)
node = next;
is often optimized away. While this can be worked around with some code
restructuring, this may not always work for more complex cases. I wonder if it
makes sense to formally support this technique with a "nocse" or similar
variable attribute that is honored by optimization passes.