Immediate offset in array index

Yufeng Zhang Yufeng.Zhang@arm.com
Mon Nov 11 16:40:00 GMT 2013


Hi,

I'm trying to understand the following code-gen difference in the 
following code:

#ifdef BEFORE
typedef int arr_2[4][5];
#else
typedef int arr_2[4][4];
#endif

void foo (arr_2 a2, int i, int j)
{
   a2[i + 2][j] = 2;
}

The code-gen using a mainline gcc -O2 -DBEFORE is:

         movslq  %esi, %rsi
         movslq  %edx, %rdx
         leaq    (%rsi,%rsi,4), %rax
         leaq    40(%rdi,%rax,4), %rax
         movl    $2, (%rax,%rdx,4)
         ret

and -O2 is:

         movslq  %esi, %rsi
         movslq  %edx, %rdx
         addq    $2, %rsi
         salq    $4, %rsi
         addq    %rdi, %rsi
         movl    $2, (%rsi,%rdx,4)
         ret

When the typedef is arr_2[4][5], the compiler distributes (i + 2) * 20 
to i * 20 + 40, while not doing the distribution when the typedef is 
arr_2[4][4].

The -fdump-tree-original dumps already diff, so I guess that it is the 
parser which does something interesting.

Can anyone enlighten me that which part of the parser I shall further 
look into to get a better understanding of the code-gen difference?

Regards,
Yufeng

p.s.

diff of the -fdump-tree-original

-  (*(a2 + ((sizetype) ((long unsigned int) i * 20) + 40)))[j] = 2;
+  (*(a2 + ((sizetype) i + 2) * 16))[j] = 2;



More information about the Gcc-help mailing list