This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

random thought - optimizer


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

and discard the variable, since it is local to this function.

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.

-- 
zw   It may of course be possible that risks-awareness and extreme care are
     developed in the course of dancing with the fuckup fairy in the pale
     moonlight.
     	-- Anthony de Boer


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]