https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83496
Bug ID: 83496
Summary: MIPS BE: wrong code generates under "-Os
-mbranch-cost=1"
Product: gcc
Version: 7.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: [email protected]
Target Milestone: ---
Created attachment 42924
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=42924&action=edit
Test case
"mips-buildroot-linux-gnu-gcc test.c -o test2 -Os -mbranch-cost=1" generates
wrong code for me.
This was seen with GCC 7.2 for MIPS32 r2 BE in OpenWrt / LEDE and with the free
electrons toolchain. I also tested the GCC snapshot from 14. December 2017 and
saw the same problem.
This is the code:
int mytest(mp_int * a, mp_digit b)
{
/* compare based on sign */
if (a->sign == 1) {
return -1;
}
/* compare based on magnitude */
if (a->used > 1) {
return 1;
}
/* compare the only digit of a to b */
if (a->dp[0] > b) {
return 1;
} else if (a->dp[0] < b) {
return -1;
} else {
return 0;
}
}
This is the wrong ASM:
004006c0 <mytest>:
4006c0: 8c830008 lw v1,8(a0)
4006c4: 24020001 li v0,1
4006c8: 1062000c beq v1,v0,4006fc <mytest+0x3c>
4006cc: 2402ffff li v0,-1
4006d0: 8c830000 lw v1,0(a0)
4006d4: 28630002 slti v1,v1,2
4006d8: 10600008 beqz v1,4006fc <mytest+0x3c>
4006dc: 00000000 nop
4006e0: 8c82000c lw v0,12(a0)
4006e4: 8c420000 lw v0,0(v0)
4006e8: 00a2182b sltu v1,a1,v0
4006ec: 14600005 bnez v1,400704 <mytest+0x44>
4006f0: 00000000 nop
4006f4: 0045102b sltu v0,v0,a1
4006f8: 00021023 negu v0,v0
4006fc: 03e00008 jr ra
400700: 00000000 nop
400704: 03e00008 jr ra
400708: 24020001 li v0,1
40070c: 00000000 nop
In line 4006dc it should say "li v0,1" instead of nop. This code will return -1
if "if (a->used > 1) " is true, but it should return 1.
I have also attached the code and compiled it with:
~/mips32--glibc--bleeding-edge/bin/mips-buildroot-linux-gnu-gcc test.c -o test2
-Os -mbranch-cost=1
When I compile it without "-mbranch-cost=1" it generates correct code.
When I use this it also generates correct code:
~/mips32--glibc--bleeding-edge/bin/mips-buildroot-linux-gnu-gcc test.c -o test4
-Os -mbranch-cost=1 -funroll-loops