http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53044
Bug #: 53044 Summary: completely peel loops that do not run a constant time Classification: Unclassified Product: gcc Version: 4.8.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: enhancement Priority: P3 Component: tree-optimization AssignedTo: unassig...@gcc.gnu.org ReportedBy: rgue...@gcc.gnu.org There exist loops where we do not know how many times they run. Consider int objects[2]; void foo (int n) { int i; for (i = 0; i < n; ++i) objects[i] = i; } we do know the loop will run at most twice (due to undefined behavior analysis, the info is accessible via max_loop_iterations (loop)). It could be beneficial to completely peel such loops (retaining the exit checks, of course). At least for loops that do not run many times (like at most once or twice, as in the above example). int objects[1]; void foo (int n) { int i; for (i = 0; i < n; ++i) objects[i] = i; } jump threading should handle the above case from VRP, but currently VRP transforms the above to <bb 2>: if (n_2(D) > 0) goto <bb 4>; else goto <bb 3>; <bb 3>: return; <bb 4>: <bb 5>: ivtmp.5_1 = 0; i_9 = 0; MEM[(int[1] *)&objects] = 0; ivtmp.5_8 = 1; i_7 = 1; if (n_2(D) > 1) goto <bb 7>; else goto <bb 6>; <bb 6>: goto <bb 3>; <bb 7>: goto <bb 5>; thus, a conditionally endless loop (ugh, a side-effect that we even preserve).