This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: random thought - optimizer
- To: Zack Weinberg <zackw at stanford dot edu>
- Subject: Re: random thought - optimizer
- From: Diego Novillo <dnovillo at redhat dot com>
- Date: Sun, 1 Jul 2001 23:25:34 -0400
- Cc: gcc at gcc dot gnu dot org
- Organization: Red Hat Canada
- References: <20010701194819.I18369@stanford.edu>
On Sun, 01 Jul 2001, Zack Weinberg wrote:
> Suppose you have a routine which looks like this after the
> preprocessor gets through with it:
>
> void init(void)
> {
> static int initialized = 0;
> if(!initialized)
> {
> initialized = 1;
> }
> }
>
> (Under some conditions there is more code inside the if block. In
> this case, preprocessor conditionals have eliminated all of it.)
>
> We currently generate code that looks like this:
>
> initialized.0:
> .long 0
> init:
> movl initialized.0, %eax
> testl %eax, %eax
> je .L3
> ret
> .p2align 4,,15
> .L3:
> movl $1, %eax
> movl %eax, initialized.0
> ret
>
> Now, it seems to me that the as-if rule says we could generate instead
>
> init:
> ret
>
That would be the wrong code to generate. Variable 'initialized'
is only set to 0 the first time init() is executed. Subsequent
calls to init() should see variable 'initialized' set to 1.
> My question is, what sort of analysis would it take to recognize this
> condition? I'm aware that optimizing this function is somewhat silly,
> but I'd think that noticing when a static variable can have no effect
> on the output of a program, might be worthwhile in more places than this.
>
Now, if 'initialized' wasn't static, then it would be possible to
get what you suggest with SCCP (Sparse Conditional Constant
Propagation). It removes code affected by constant propagation.
It can also be done using standard cprop and dce. We currently
do this if initialized is not static.
Diego.