Re: question on common error-handling idiom

From: Chris Friesen
Date: Wed Nov 03 2004 - 11:55:16 EST


Jesper Juhl wrote:

Has anyone taken a look at what recent gcc's actually do with different variations of the constructs mentioned in this thread?

I did, out of curiosity:

I used the following (admittedly simplistic) code, compiled with -O2.

int bbbb(int a)
{
int err = -5;
if (a == 1)
goto out;
err=0;
out:
return err;
}

int cccc(int a)
{
int err=0;
if (a == 1) {
err = -5;
goto out;
}
out:
return err;
}


With gcc 3.2.2 for x86, both constructs generated the same code:

pushl %ebp
movl %esp, %ebp
xorl %eax, %eax
cmpl $1, 8(%ebp)
setne %al
leal -5(%eax,%eax,4), %eax
leave
ret

With gcc 2.96 (Mandrake) however, the standard construct generated this:


pushl %ebp
movl %esp, %ebp
subl $4, %esp
movl $-5, -4(%ebp)
cmpl $1, 8(%ebp)
jne .L3
jmp .L4
.p2align 4,,7
.L3:
movl $0, -4(%ebp)
.L4:
movl -4(%ebp), %eax
movl %eax, %eax
movl %ebp, %esp
popl %ebp
ret



While moving the err setting into the conditional generates the following:

pushl %ebp
movl %esp, %ebp
subl $4, %esp
movl $0, -4(%ebp)
cmpl $1, 8(%ebp)
jne .L6
movl $-5, -4(%ebp)
jmp .L7
.p2align 4,,7
.L6:
nop
.L7:
movl -4(%ebp), %eax
movl %eax, %eax
movl %ebp, %esp
popl %ebp
ret


For PPC, gcc 3.3.3, the standard construct gave:

xori 3,3,1
addic 3,3,-1
subfe 3,3,3
rlwinm 3,3,0,30,28
blr

While moving the err setting into the conditional generates the following:

xori 3,3,1
srawi 0,3,31
xor 3,0,3
subf 3,3,0
srawi 3,3,31
andi. 3,3,5
addi 3,3,-5
blr


So, it looks like the standard construct can actually generate better code in some cases, its almost never worse, and it's certainly nicer to read.

Chris
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/