https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110519
Bug ID: 110519
Summary: Optimize loop that only assigns to a local variable
Product: gcc
Version: 12.0
Status: UNCONFIRMED
Keywords: missed-optimization
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: roland.illig at gmx dot de
Target Milestone: ---
~~~c
struct symbol {
struct symbol *next;
};
void f(const struct symbol *sym)
{
for (const struct symbol *s = sym; s != (void *)0; s = s->next)
do { } while (0);
}
~~~
The above code was extracted from
<https://github.com/NetBSD/src/blob/f74666e8d086952b27d76155223a5d497e69f5a8/usr.bin/xlint/lint1/decl.c#L1319>.
Several other compilers know that this loop has no side effects, even at low
optimization levels, see <https://godbolt.org/z/Y85EGcW3d>.