This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
optimizing inlined switch statements and -fPIC
- From: David Mansfield <gcc at dm dot cobite dot com>
- To: <gcc at gcc dot gnu dot org>
- Date: Wed, 12 Dec 2001 13:34:18 -0500 (EST)
- Subject: optimizing inlined switch statements and -fPIC
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
I have a sample .c file, inline.c.
I've been compiling it like:
gcc -O2 -S inline.c
and
gcc -fPIC -O2 -S inline.c
I'd really like to be able to have the compliler optimize away the switch
in all cases.
here's the file:
------- cut inline.c -------
#include <stdio.h>
#define VAL_1 1111
#define VAL_2 2222
#define VAL_3 3333
#define VAL_4 4444
#define KEY_1 1
#define KEY_2 2
#define KEY_3 3
#define KEY_4 4
/*
*
* if you comment out USE_4_CASES the compiler will
* properly optimize away the switch statement when
* compiled with -fPIC, otherwise it won't
*
* when compiled without -fPIC it works in either case
*
* complile as: gcc -S -O2 [-fPIC] inline.c and look
* at the assembly output
*/
#define USE_4_CASES
static __inline__ int get_val(int key)
{
switch(key)
{
case KEY_1: return VAL_1;
case KEY_2: return VAL_2;
case KEY_3: return VAL_3;
#ifdef USE_4_CASES
case KEY_4: return VAL_4;
#endif
}
}
int func()
{
printf("%d\n", get_val(KEY_1));
return 0;
}
---------- end inline.c ---------
Can anyone can tell me anything (is there an __attribute__ I can use?)?
David
--
/==============================\
| David Mansfield |
| david@cobite.com |
\==============================/