http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57186
Bug #: 57186 Summary: implement load sinking in loops Classification: Unclassified Product: gcc Version: 4.9.0 URL: http://gcc.gnu.org/ml/gcc-patches/2012-10/msg00742.htm l Status: UNCONFIRMED Keywords: missed-optimization Severity: enhancement Priority: P3 Component: tree-optimization AssignedTo: unassig...@gcc.gnu.org ReportedBy: ebotca...@gcc.gnu.org Created attachment 30040 --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=30040 Original implementation Even at -O3, the compiler doesn't figure out that the following loop is dumb: #define SIZE 64 int foo (int v[]) { int r; for (i = 0; i < SIZE; i++) r = v[i]; return r; } This isn't entirely unexpected, as it probably matters only for (slightly) pathological cases. The attached patch nevertheless implements a form of load sinking in loops so as to optimize these cases. It's combined with invariant motion to optimize: int foo (int v[], int a) { int r, i; for (i = 0; i < SIZE; i++) r = v[i] + a; return r; } and with store sinking to optimize: int foo (int v1[], int v2[]) { int r[SIZE]; int i, j; for (j = 0; j < SIZE; j++) for (i = 0; i < SIZE; i++) r[j] = v1[j] + v2[i]; return r[SIZE - 1]; } The optimization is enabled at -O2 in the patch for measurement purposes but, given how rarely it triggers (e.g. exactly 10 occurrences in a GCC bootstrap, compiler-only, all languages except Go), it's probably best suited to -O3. It also comes with 3 testcases.