Given a structure and 3 field access

typedef struct network
{
   char inputfile[400];
   int* nodes;
   int* arcs;
   int* stop_arcs;
} network_t;

   int *arc;
   int *node = net->nodes;                                           <--- A
   void *stop = (void *)net->stop_arcs;                              <--- B
   for( arc = net->arcs; arc != (int *)stop; arc++ )                 <--- C

GCC generates following instruction sequence in thumb mode with options -O2
-Os, it needs 9 insts to load 3 fields 

       mov     r2, #200         <---  A1
       lsl     r1, r2, #1       <---  A2
       .loc 1 14 0
       mov     r4, #204         <----  B1
       lsl     r3, r4, #1       <---   B2
       .loc 1 13 0
       ldr     r2, [r0, r1]      <----  A3
       .loc 1 15 0
       mov     r1, #202          <---  C1
       .loc 1 14 0
       ldr     r4, [r0, r3]      <--- B3
       .loc 1 15 0
       lsl     r3, r1, #1        <---  C2
       ldr     r3, [r0, r3]       <---  C3

A better method is adjusting the base address first, which is nearer to all 3
fields we will access. Then we can use ldr dest, [base, offset] to load each
fields with only 1 instruction.

Although this opportunity is found in target ARM, it should also be applicable
to other architectures with addressing mode of (base + offset) and offset has a
limited value range.


-- 
           Summary: inefficient address calculation of fields of large
                    struct
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: carrot at google dot com
 GCC build triplet: i686-linux
  GCC host triplet: i686-linux
GCC target triplet: arm-eabi


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40314

Reply via email to