http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52396
Bug #: 52396 Summary: Gcc failed to hoist loop invariant expression out of loop Classification: Unclassified Product: gcc Version: 4.7.0 Status: UNCONFIRMED Severity: enhancement Priority: P3 Component: tree-optimization AssignedTo: unassig...@gcc.gnu.org ReportedBy: car...@google.com Target: arm-linux-gnueabi Compile the following source code with options -march=armv7-a -mthumb -Os -fpic extern int array[]; int t0l(int len) { int t = 0; int i; for (i=0; i<len; i++) t = t + array[i]; return t; } ARM gcc 4.7 generates: t0l: ldr r1, .L4 movs r3, #0 push {r4, lr} .LPIC8: add r1, pc mov r2, r3 b .L2 .L3: ldr r4, .L4+4 // A ldr r4, [r1, r4] // B ldr r4, [r4, r3, lsl #2] adds r3, r3, #1 adds r2, r2, r4 .L2: cmp r3, r0 blt .L3 mov r0, r2 pop {r4, pc} .L5: .align 2 .L4: .word _GLOBAL_OFFSET_TABLE_-(.LPIC8+4) .word array(GOT) Instructions AB are loop invariant, could be taken out of the loop without increase code size. If I change -Os to -O2, ivopts can successfully do this optimization.