C++ PATCH: PR 25895, 25856, 25858

Mark Mitchell mark@codesourcery.com
Sun Jan 22 02:40:00 GMT 2006


Andrew Pinski wrote:

> Actually the optimization is invalid as 
> 
> struct a
> {
>   int t;
> };
> 
> bool f(void)
> {
>   a *b = 0;
>   return &b->t == 0; 
> }
> 
> that should return 1 as it is a null pointer.

This is not the same issue; there is no base class cast here.

> Take:

> int f(int t)
> {
>   b t1;
>   a *t2 = &t1;
>   t1.t = t;
>   return t2.t == t;
> }
> 
> On the tree level we should get "return 1" but after this
> patch we get:
>   t1.t = t;
>   return ((a*)(&t1))->t == t;
> 
> Which means we lost a simple optimization on the tree level
> because we lost information from the front-end that &t1.BASE
> is the same as (a*)(&t1).

If the optimizers can't work that out, that's a bug in the optimizers.
It's certainly possible for an optimizer to figure out that
"((a*)(&t1))->t" is the same as "(&t1)->t" when the cast to "a*" is via
a NOP_EXPR.  We used to do things like "&t1 + 8"; we still use the field
approach for that case.

Eliminating the conditional check for NULL is a very useful optimization
for single-inheritance hierarchies, which are the common case.

-- 
Mark Mitchell
CodeSourcery
mark@codesourcery.com
(650) 331-3385 x713



More information about the Gcc-patches mailing list