I found gcc 2.95.3 generates bad code when optimizing redundant switch
statement.
I'm using RPM package (gcc-2.95.3-19.mipsel.rpm or
gcc-2.95.3-22.mipsel.rpm) by Maciej W. Rozycki (Thank you).
Here is a sample source code. Compiling this with -O2 generates bad
result (-O1 is OK).
--- foo.c ---
int
foo(int a, int b, int c)
{
int t;
switch(c) {
case 0:
case 1:
case 3:
t = 4; break;
case 2:
t = 1; break;
case 4:
t = 3; break;
default:
return 0;
}
return a * b;
}
--- foo.c ---
This is output of "gcc -o foo.s -O2 -S foo.s". Obviously this codes
is wrong. Neither "mult" nor "j $31" exist in this function!!
--- foo.s (-O2) ---
.file 1 "foo.c"
.abicalls
.version "01.01"
gcc2_compiled.:
.text
.align 2
.globl foo
.ent foo
foo:
.frame $sp,0,$31 # vars= 0, regs= 0/0, args= 0, extra= 0
.mask 0x00000000,0
.fmask 0x00000000,0
.set noreorder
.cpload $25
.set reorder
sltu $6,$6,5
.end foo
--- foo.s (-O2) ---
This is output of "gcc -o foo.s -O1 -S foo.s". This looks fine.
--- foo.s (-O1) ---
.file 1 "foo.c"
.abicalls
.version "01.01"
gcc2_compiled.:
.text
.align 2
.globl foo
.ent foo
foo:
.frame $sp,0,$31 # vars= 0, regs= 0/0, args= 0, extra= 0
.mask 0x00000000,0
.fmask 0x00000000,0
.set noreorder
.cpload $25
.set reorder
sltu $2,$6,5
.set noreorder
.set nomacro
beq $2,$0,$L9
sll $2,$6,2
.set macro
.set reorder
lw $2,$L10($2)
.set noreorder
.set nomacro
j $L12
mult $4,$5
.set macro
.set reorder
.rdata
.align 3
$L10:
.gpword $L3
.gpword $L3
.gpword $L3
.gpword $L3
.gpword $L3
.text
$L9:
.set noreorder
.set nomacro
j $31
move $2,$0
.set macro
.set reorder
$L3:
mult $4,$5
$L12:
mflo $2
#nop
j $31
.end foo
--- foo.s (-O1) ---
I tested with a cross compiler (mipsel-linux-gcc-2.95.3-22.i386.rpm)
and got same results.
Also gcc-2.96-99 (packaged by H.J.Lu) seems not have this problem.
---
Atsushi Nemoto
|