https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61837
Bug ID: 61837 Summary: missed loop invariant expression optimization Product: gcc Version: 4.10.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: carrot at google dot com Host: x86_64-unknown-linux-gnu Target: powerpc64le Compile following code with trunk compiler and options -O2 -m64 -mcpu=power8 void foo(int *p1, char *p2, int s) { int n, v, i; v = 0; for (n = 0; n <= 100; n++) { for (i = 0; i < s; i++) if (p2[i] == n) p1[i] = v; v += 88; } } I got foo: addi 9,5,-1 cmpwi 5,5,0 rldicl 9,9,0,32 li 6,0 li 7,0 add 5,4,9 .p2align 4,,15 .L2: ble 5,.L6 addi 8,4,-1 mr 10,3 subf 9,8,5 // A mtctr 9 b .L4 .p2align 4,,15 .L3: addi 10,10,4 bdz .L6 .L4: lbzu 9,1(8) cmpw 7,9,7 bne 7,.L3 stw 6,0(10) addi 10,10,4 bdnz .L4 .L6: addi 6,6,88 addi 7,7,1 cmpwi 7,6,8888 extsw 7,7 extsw 6,6 bne 7,.L2 blr Instruction A computes the inner loop counter, it is loop invariant for the outer loop, so it can be hoisted out of the outer loop.