https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68340
Bug ID: 68340 Summary: Inner-nested constant-bounded loop not recognized as constant-bounded during loop unrolling Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: rtl-optimization Assignee: unassigned at gcc dot gnu.org Reporter: kelvin at gcc dot gnu.org Target Milestone: --- As currently implemented, only inner-most loops are unrolled by the loop unrolling code found in loop-unroll.c. If a constant-bounded loop is nested within another loop, processing of the outer loop results in induction variable optimization of the inner loop before the inner loop is seen by the loop unroller. After the original code is replaced with induction variables, the inner loop is no longer recognized by the loop unrolling code as constant bounded. Consider, for example, this simple program: void foo(double *d, unsigned long int n) { unsigned long int i, j; for (i = 0; i < n; i++) { for (j = 0; j < 10000002; j++) { d[j * 2] = 0.0; } } } Compile this code with: gcc -S -O3 -fno-tree-vectorize -funroll-loops --param max-unroll-times=4 -fdump-rtl-loop2_unroll file.c Following induction-variable replacements, the transformed inner loop is restructured to resemble the following: void *array_end = (void *) &d[10000002 * 2]; double *ivtmp_9 = d; while (ivtmp_9 < (double *) array_end) { *ivtmp_9 = 0.0; ivtmp_9 += 2; } This can be seen in the trace output reported by the loop2_unroll file. Following the first heading "starting region dump", the rtl instructions associated with "bb 4" (basic block 4) holds the contents of the loop body, which matches the C pseudo-code shown immediately above. Subsequent messages within this same file report: ;; Considering unrolling loop with constant number of iterations ;; Unable to prove that the loop iterates constant times If the same inner loop is compiled in an outer-most context, you will not see the error message about "unable to prove that the loop iterates constant times" and consequently, the result of loop unrolling will be very different.