-------- Original Message --------
Subject: Re: Gotos
Date: Fri, 09 Jan 2004 07:54:27 -0600
From: K.R. Foley <kr@cybsft.com>
To: Momchil Velikov <velco@fadata.bg>
References: <20040109050711.GA26024@littlegreenmen.armory.com> <87wu814i9h.fsf@freon.briz.fadata.bg> <87k74146t0.fsf@freon.briz.fadata.bg>
Momchil Velikov wrote:
"Momchil" == Momchil Velikov <velco@fadata.bg> writes:
Phil> Actually, if you try compiling that file with the patch and Phil> without it, they do not turn out the same. I tried without -O, Phil> with -O2, and with -O3.
One more example:
x1.c: ----- int foo (), bar ();
int
baz ()
{
int ret;
if (__builtin_expect (foo (), 0))
{
ret = 0;
goto out;
}
bar ();
ret = 1;
out: return ret; }
x2.c: ----- int foo (), bar ();
int
baz ()
{
int ret;
if (__builtin_expect (foo (), 0))
{
ret = 0;
return ret;
}
bar ();
ret = 1;
out: return ret; }
In my opinion, compile size aside, it is always better to have one point of exit. Even if you have to use loops to avoid gotos, as someone else already pointed out. In the case above the goto is more readable, not spagetti code and lends itself much better to self documentation. ;)
kr
gcc -S -O3 -fomit-frame-pointer x1.c x2.c
x1.s: ----- .file "x1.c" .text .p2align 4,,15 .globl baz .type baz, @function baz: subl $12, %esp call foo xorl %edx, %edx testl %eax, %eax jne .L3 call bar movl $1, %edx .L3: movl %edx, %eax addl $12, %esp ret .size baz, .-baz .ident "GCC: (GNU) 3.3.2"
x2.s: ----- .file "x2.c" .text .p2align 4,,15 .globl baz .type baz, @function baz: subl $12, %esp call foo xorl %edx, %edx testl %eax, %eax jne .L1 .L3: call bar movl $1, %edx .L1: movl %edx, %eax addl $12, %esp ret .size baz, .-baz .ident "GCC: (GNU) 3.3.2"
Right, they are identical :)
~velco
-- Kernelnewbies: Help each other learn about the Linux kernel. Archive: http://mail.nl.linux.org/kernelnewbies/ FAQ: http://kernelnewbies.org/faq/
-- K.R. Foley kr@cybsft.com www.cybsft.com
-- Kernelnewbies: Help each other learn about the Linux kernel. Archive: http://mail.nl.linux.org/kernelnewbies/ FAQ: http://kernelnewbies.org/faq/