This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Suboptimal __restrict optimization?
- From: Ulf Magnusson <ulfalizer at gmail dot com>
- To: gcc at gcc dot gnu dot org
- Date: Sun, 2 Oct 2011 02:58:43 +0200
- Subject: Suboptimal __restrict optimization?
Hi,
Given the code
class C { void f(int *p); int q; };
void C::f(int * __restrict p) __restrict {
q += 10;
*p = 7;
q += 10;
}
g++ 4.5.2 with -O3 generates the following for C::f() (prologue and
epilogue omitted):
mov 0x8(%ebp),%eax // eax = this (= &q)
mov 0xc(%ebp),%ecx // ecx = p
mov (%eax),%edx // edx = q
movl $0x7,(%ecx) // *p = 7
add $0x14,%edx // q += 20
mov %edx,(%eax) // save q
If C::f() is rearranged as
void C::f(int * __restrict p) __restrict {
*p = 7;
q += 10;
q += 10;
}
the following is generated instead:
mov 0x8(%ebp),%eax // eax = this (= &q)
mov 0xc(%ebp),%edx // edx = p
movl $0x7,(%edx) // *p = 7
addl $0x14,(%eax) // q += 20
Is there some reason why GCC couldn't generate this code for the first
version of C::f()? Is this a failure of optimization, or am I missing
something in how __restricted works?
/Ulf