The following code:
typedef struct node { struct node **next; } node_t;
__attribute__((fastcall)) void *f(node_t *n, int i) {
return n->next[i];
}
compiled with -O2 generates the following assembly (generated esp/ebp dead code
removed for readability):
f:
movl (%ecx), %eax
movl (%eax,%edx,4), %eax
ret
which seems optimal. However, simply storing the return value in a variable
damages the generated code:
typedef struct node { struct node **next; } node_t;
__attribute__((fastcall)) void *f(node_t *n, int i) {
n = n->next[i];
return n;
}
generates:
f:
sall $2, %edx
addl (%ecx), %edx
movl (%edx), %eax
ret
which is less optimal than the previous version, for no reason.
--
Summary: -O2 doesn't use movl (A,B,4),C to its full extent to
access an array
Product: gcc
Version: 4.4.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: jerome dot abela at free dot fr
GCC build triplet: i386-pc-linux-gnu
GCC host triplet: i386-pc-linux-gnu
GCC target triplet: i386-pc-linux-gnu
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43035