simple optimisation question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I was recently investigating what these two (relatively similar) functions would compile to:

- - - - -
int read0(int (*x)[12], int i, int j)
{
    return x[i][j];
}

int read1(int *x, int i, int j)
{
    return x[i * 12 + j];
}
- - - - -

I found (through Godbolt) the following behavior (all on x86-64):

- On GCC: The two functions compile to different assembly code regardless of the flags tested.

- On Clang: They compile to the same assembly code under ‘-m32 -Oz’.

- On Clang: They compile to different assembly code under other tested flags.

The flags I tested were ‘-O3’ vs. ‘-Oz’ and ‘-m32’ vs. none. (Four combinations per compiler.)

In GCC, the assembly code, although different, under ‘-m32 -Oz’ was of the same size (in bytes, after assembled) for both functions. For ‘-Oz’ withough ‘-m32’, the first one was larger.

Is this a missed size optimisation for x86-64? Even in the case where the assembly code is larger, the time performance difference seems unobservable. (Though I’d have imagined the the larger one would have been slower in each case.)

These are URLs for the tests I made:

- ‘gcc -O3’: https://godbolt.org/z/8T77ehaEE
- ‘gcc -Oz’: https://godbolt.org/z/1E1jMxY78
- ‘gcc -m32 -O3’: https://godbolt.org/z/P8G614xzv
- ‘gcc -m32 -Oz’: https://godbolt.org/z/TTEGKqzn1
- ‘clang -O3’: https://godbolt.org/z/nEPbvfsb6
- ‘clang -Oz’: https://godbolt.org/z/hv1z3dEcf
- ‘clang -m32 -O3’: https://godbolt.org/z/6snbhj68n
- ‘clang -m32 -Oz’: https://godbolt.org/z/EEK87dn3d




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux