This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: bad dead code detection ?
- From: Fergus Henderson <fjh at cs dot mu dot OZ dot AU>
- To: David Jobet <david dot jobet at free dot fr>
- Cc: gcc at gcc dot gnu dot org
- Date: Sat, 5 Jul 2003 08:26:16 +1000
- Subject: Re: bad dead code detection ?
- References: <1056549175.3ef9a93736288@imp.free.fr>
On 25-Jun-2003, David Jobet <david.jobet@free.fr> wrote:
> I try to write an exception handling library, and I'm forced to play with gcc
> inline assembly.
I don't see why. For an example of an exception handling
library which does not use any inline assembly, see
<http://www.cs.mu.oz.au/~fjh/CXCPT/>.
> The trick is to register address of local labels in a global static table. Later
> when an exception is launched, I manage to jump to this label to do clean up.
> The problem is gcc detects the label cannot be reached and dead code elimination
> completely remove the exception handling code from the executable when compiled
> with -O3.
...
> I'm wondering if it's a bug, or if there is a way to prevent this.
It's not a bug... but there are ways to prevent it.
To prevent dead code elimination from eliminating a label,
you need to do two things:
1. Make sure that the address of each label is taken *and appears to
GCC to be used*, e.g. by storing it in a volatile global variable,
extern void *volatile volatile_global;
...
volatile_global = &&label;
or passing it to a volatile asm statement.
__asm__ __volatile__("" : : "g"(&&label))
The latter approach is better since it doesn't generate any code.
Of course this use of &&label needs to occur in *reachable* code,
e.g. at the start of the function.
2. You also need to make sure that the function contains at least one
reachable jump to an unknown-to-GCC location, e.g. by putting
the following at the start of the function, immediately after the
above-mentioned code to take and apparently use the addresses of labels.
For example, add
goto *identity_function(&&start);
start:
where identity_function() is a function whose body is not visible to GCC
that just returns its argument.
I suppose that
volatile_global = &&start;
goto *volatile_global;
ought to have a similar effect too, although I haven't tested that.
> When compiled without -03, the code is not removed, but the label is
> moved from the *good* location to the return label.
See the thread "equality and label addresses" starting at
<http://gcc.gnu.org/ml/gcc/2003-01/msg01519.html>, which is
at least relevant, though I'm not sure it completely addresses
your concern here.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.