Amusing bad code generation for ARM

Tobias Ringström tobias@ringis.se
Thu Apr 22 10:36:00 GMT 2010


On 2010-04-21 16:56, Ian Lance Taylor wrote:
> This is presumably happening because of the volatile declarations.

Further experimentation reveals that it isn't the case. The following code:

     struct A { int r0; int r1; };
     void ugh(struct A *restrict a, const int *restrict d)
     {
         for (int i = 0; i < 8; ++i) {
             if (*d++)
                 a->r0 = 1;
             else
                 a->r1 = 1;
         }
     }

compiled using

     arm-none-eabi-gcc -O3 -S -o- -std=c99 tmp.c

also produces very suboptimal code:

ugh:
         @ Function supports interworking.
         @ args = 0, pretend = 0, frame = 0
         @ frame_needed = 0, uses_anonymous_args = 0
         @ link register save eliminated.
         ldr     r3, [r1, #0]
         cmp     r3, #0
         moveq   r2, #1
         movne   r2, #1
         streq   r2, [r0, #4]
         strne   r2, [r0, #0]
         ldr     r2, [r1, #4]
         cmp     r2, #0
         add     r3, r1, #4
         moveq   r1, #1
         movne   r1, #1
         streq   r1, [r0, #4]
         strne   r1, [r0, #0]
         add     r2, r3, #4
         ldr     r3, [r3, #4]
         cmp     r3, #0
         moveq   r1, #1
         movne   r1, #1
         streq   r1, [r0, #4]
         strne   r1, [r0, #0]
         add     r3, r2, #4
         ldr     r2, [r2, #4]
         ... and keeps repeating six times more...

Note the silly reloading of r1. I'll file a bug report about this case 
instead.

/Tobias



More information about the Gcc-help mailing list