N
Nomen Nescio
Harald said:How about this, then?
void f(void) {
int x;
x = 2;
}
int main(void) {
for (unsigned int i = 0; ++i; )
f();
}
Is the compiler allowed to remove this loop? Modifying an object is a side
effect, but I expect any decent compiler to treat this as an unnecessary
side effect.
Well, let's have a look at how gcc treats it with -O3.
gcc 2.95.x
----------
f:
pushl %ebp
movl %esp,%ebp
leave
ret
Leaves in the prolog and epilog code for f but optimizes
out the variable x.
main:
pushl %ebp
movl %esp,%ebp
movl $1,%eax
L7:
incl %eax
jnz .L7
xorl %eax,%eax
leave
ret
Does the full loop but optimizes out the call to f.
latest gcc
----------
Same code produced for f.
main:
pushl %ebp
movl %esp, %ebp
xorl %eax, %eax
leave
ret
But main is smarter. So it would appear that gcc believes it's
allowed to remove that loop, and I doubt anyone would complain.
Yours,
Han from China