optimizing inlined switch statements and -fPIC

Jan Hubicka jh@suse.cz
Thu Dec 13 01:41:00 GMT 2001


> 
> I noticed something really weird, and I need to find out if there's a way 
> to correct this odd behavior.
> 
> I've noticed this with egcs, gcc-2.96 from red hat 7.2, gcc-3.0.1 and 
> gcc-3.0.2 (from source).
> 
> I have an inline function which performs a switch statement, each switch 
> returns a constant value.  When this function is called with a constant 
> value, the entire switch COULD be eliminated.  But it isn't in some cases.
> 
> 1) it works when -fPIC isn't specified
> 2) it works if there are 3 or less cases and -fPIC is specified
This is because up to upcoming 3.1 version, the inlining is done at lowlevel
RTL code, where eliminating of such switch statements is dificult, as PIC often
obfstructates thinks in a way gcc is no longer able to prove constantness of
the jump.

New gcc do inlining on tree level that should solve the problem you are seeing.
In case you want to get this with older gcc, I would recommend you to rewrite
switch to series of ifs in case the parameter is always constant.  You can
using __builtin_constant_p trick in case it is not.

In case the switch is small, the rewriting to ifs is done automatically by gcc
that explains why you are not seeing the problem for small amount of cases.

Honza



More information about the Gcc mailing list