This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
random thought - optimizer
- To: gcc at gcc dot gnu dot org
- Subject: random thought - optimizer
- From: "Zack Weinberg" <zackw at stanford dot edu>
- Date: Sun, 1 Jul 2001 19:48:19 -0700
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