linux-gcc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Why -fPIC stops some optimization?
@ 2004-07-09 15:02 Denis Zaitsev
  2004-07-09 20:45 ` Michel Lespinasse
  0 siblings, 1 reply; 4+ messages in thread
From: Denis Zaitsev @ 2004-07-09 15:02 UTC (permalink / raw)
  To: gcc, linux-gcc

I have met such a behaviour while compiling GLIBC for x86.  A
construct which suffers looks like:


#define __xyz(x,y,z) ({ \
    ...                 \
    size_t __n= (z);    \
    ...                 \
    switch (__n) {      \
        case ...        \
        ...             \
    }                   \
    ...                 \
})


This macro is intended for use with some constant z
(__builtin_constant_p(z) is true).  And when this macro is used such a
way, GCC should unroll (or what is the word for this?) the switch into
the only branch which is needed.  And GCC does this optimization, but
only not for the -fPIC case.  There GCC emits the whole body for the
switch operator and then does just a jump for the correct branch.
Why?  Or it is some expected misbehaviour for the -fPIC case?

GCC-3.3.2.

Thanks in advance.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Why -fPIC stops some optimization?
  2004-07-09 15:02 Why -fPIC stops some optimization? Denis Zaitsev
@ 2004-07-09 20:45 ` Michel Lespinasse
  2004-07-09 21:58   ` Denis Zaitsev
  0 siblings, 1 reply; 4+ messages in thread
From: Michel Lespinasse @ 2004-07-09 20:45 UTC (permalink / raw)
  To: Denis Zaitsev; +Cc: gcc, linux-gcc

On Fri, Jul 09, 2004 at 09:02:30PM +0600, Denis Zaitsev wrote:
> I have met such a behaviour while compiling GLIBC for x86.  A
> construct which suffers looks like:
> 
> 
> #define __xyz(x,y,z) ({ \
>     ...                 \
>     size_t __n= (z);    \
>     ...                 \
>     switch (__n) {      \
>         case ...        \
>         ...             \
>     }                   \
>     ...                 \
> })

I can not comment about your specific case, but in the past I've had a
fairly similar issue with an inline function that had branches and was
supposed to be optimized out to straight-line code at the call site.

Try making __n a const and see if it helps. Yes, this is something
that gcc should really figure it out by itself.

-- 
Michel "Walken" Lespinasse
"In this time of war against Osama bin Laden and the oppressive
Taliban regime, we are thankful that OUR leader isn't the spoiled son
of a powerful politician from a wealthy oil family who is supported by
religious fundamentalists, operates through clandestine organizations,
has no respect for the democratic electoral process, bombs innocents,
and uses war to deny people their civil liberties." --The Boondocks

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Why -fPIC stops some optimization?
  2004-07-09 20:45 ` Michel Lespinasse
@ 2004-07-09 21:58   ` Denis Zaitsev
  2004-07-11 22:28     ` Michel Lespinasse
  0 siblings, 1 reply; 4+ messages in thread
From: Denis Zaitsev @ 2004-07-09 21:58 UTC (permalink / raw)
  To: Michel Lespinasse; +Cc: gcc, linux-gcc

On Fri, Jul 09, 2004 at 01:45:50PM -0700, Michel Lespinasse wrote:
> On Fri, Jul 09, 2004 at 09:02:30PM +0600, Denis Zaitsev wrote:
> > I have met such a behaviour while compiling GLIBC for x86.  A
> > construct which suffers looks like:
> > 
> > 
> > #define __xyz(x,y,z) ({ \
> >     ...                 \
> >     size_t __n= (z);    \
> >     ...                 \
> >     switch (__n) {      \
> >         case ...        \
> >         ...             \
> >     }                   \
> >     ...                 \
> > })
> 
> I can not comment about your specific case, but in the past I've had a
> fairly similar issue with an inline function that had branches and was
> supposed to be optimized out to straight-line code at the call site.
> 
> Try making __n a const and see if it helps. Yes, this is something
> that gcc should really figure it out by itself.

This is the same way I'm curing the problem for now.  Making the subst
variable const or using (z) directly in a statement-expression really
helps.  But, nevertheless, is this limitation is switch operator
specific?  Or is it a limit for optimization GCC can do, and it's
reached faster when the PIC-code is generated?  Or what's wrong?

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Why -fPIC stops some optimization?
  2004-07-09 21:58   ` Denis Zaitsev
@ 2004-07-11 22:28     ` Michel Lespinasse
  0 siblings, 0 replies; 4+ messages in thread
From: Michel Lespinasse @ 2004-07-11 22:28 UTC (permalink / raw)
  To: Denis Zaitsev; +Cc: gcc, linux-gcc

On Sat, Jul 10, 2004 at 03:58:01AM +0600, Denis Zaitsev wrote:
> > Try making __n a const and see if it helps. Yes, this is something
> > that gcc should really figure it out by itself.
> 
> This is the same way I'm curing the problem for now.  Making the subst
> variable const or using (z) directly in a statement-expression really
> helps.  But, nevertheless, is this limitation is switch operator
> specific?  Or is it a limit for optimization GCC can do, and it's
> reached faster when the PIC-code is generated?  Or what's wrong?

I can't really help you on this one - I'm just another gcc user.

All I know is, sometimes (often) gcc fails to see that some variable
is only assigned a constant once and never touched anywhere else, and
fails to optimize out that variable as a constant expression. When I
notice this, it's usualy possible to work around the issue by making
that variable a const. I have no idea why gcc does not notice the
obvious by itself though.

Cheers,

-- 
Michel "Walken" Lespinasse
"In this time of war against Osama bin Laden and the oppressive
Taliban regime, we are thankful that OUR leader isn't the spoiled son
of a powerful politician from a wealthy oil family who is supported by
religious fundamentalists, operates through clandestine organizations,
has no respect for the democratic electoral process, bombs innocents,
and uses war to deny people their civil liberties." --The Boondocks

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-07-11 22:28 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-07-09 15:02 Why -fPIC stops some optimization? Denis Zaitsev
2004-07-09 20:45 ` Michel Lespinasse
2004-07-09 21:58   ` Denis Zaitsev
2004-07-11 22:28     ` Michel Lespinasse

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).