https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109746
Bug ID: 109746
Summary: Fails removing redundant comparison in for loop over
multiple variables, unless members of struct
Product: gcc
Version: 13.1.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: magnus.hegdahl at gmail dot com
Target Milestone: ---
Compiled with g++ (GCC) 13.1.1 20230429 on x86-64 Linux, with -O2 or higher.
In the code below, the comparisons with i are redundant because j is increasing
much faster.
GCC manages to optimize this away when i and j are members of some struct, but
not when they are just integers.
struct S {
unsigned x;
};
unsigned f() {
unsigned N = 1e9, x = 0;
for (unsigned i = 3, j = 1; i < N && j < N; i += 2, j += 3)
x ^= i * j;
return x;
}
unsigned g() {
unsigned N = 1e9, x = 0;
for (S i {3}, j {1}; i.x < N && j.x < N; i.x += 2, j.x += 3)
x ^= i.x * j.x;
return x;
}
int main() {
return f();
}