This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Boolean optimization
- To: gcc at gcc dot gnu dot org
- Subject: Boolean optimization
- From: <robert123 at operamail dot com>
- Date: Wed, 07 Mar 2001 11:34:46 -0000
I have noticed that then same logic produces very different assembler code for the SH, am I missing something obvious ?.
The following code.
inline bool foo(void)
{
extern unsigned short a;
return a & 0x10;
}
int test_foo (void)
{
if (foo())
return 123;
else
return 321;
}
inline int bar(void)
{
extern unsigned short a;
return a & 0x10;
}
int test_bar (void)
{
if (bar())
return 123;
else
return 321;
}
Produces.
test.o: file format coff-sh
Disassembly of section .text:
00000000 <_test_foo__Fv>:
int test_foo (void)
{
if (foo())
0: 2f e6 mov.l r14,@-r15
2: d1 09 mov.l 28 <.eb+0xa>,r1 ! 0x0
4: 60 11 mov.w @r1,r0
6: 6e f3 mov r15,r14
8: 40 21 shar r0 //**** Lots of shars when an AND #16 would do
a: 40 21 shar r0
c: 40 21 shar r0
e: 40 21 shar r0
10: c9 01 and #1,r0
12: 20 08 tst r0,r0
14: 8b 02 bf 1c <_test_foo__Fv+0x1c>
return 123;
else
return 321;
16: 90 05 mov.w 24 <.eb+0x6>,r0 ! 0x141
18: a0 01 bra 1e <.eb>
1a: 00 09 nop
1c: e0 7b mov #123,r0
0000001e <.eb>:
}
1e: 6f e3 mov r14,r15
20: 00 0b rts
22: 6e f6 mov.l @r15+,r14
24: 01 41 .word 0x0141
26: 00 09 nop
28: 00 00 .word 0x0000
...
0000002c <_test_bar__Fv>:
inline int bar(void)
{
extern unsigned short a;
return a & 0x10;
}
int test_bar (void)
{
2c: 2f e6 mov.l r14,@-r15
2e: d1 07 mov.l 4c <.eb+0xa>,r1 ! 0x0
30: 60 11 mov.w @r1,r0
32: 6e f3 mov r15,r14
34: c9 10 and #16,r0
if (bar())
36: 20 08 tst r0,r0
38: 8b 02 bf 40 <_test_bar__Fv+0x14>
return 123;
else
return 321;
3a: 90 05 mov.w 48 <.eb+0x6>,r0 ! 0x141
3c: a0 01 bra 42 <.eb>
3e: 00 09 nop
40: e0 7b mov #123,r0
00000042 <.eb>:
}
42: 6f e3 mov r14,r15
44: 00 0b rts
46: 6e f6 mov.l @r15+,r14
48: 01 41 .word 0x0141
4a: 00 09 nop
4c: 00 00 .word 0x0000
...
As you can see if the return type is a bool there is lots of messing in the test "if(foo())" this is not the case with the "if(bar())" test where the return type is an int.
Am I doing anything wrong?
The command line / tools are the following:-
D:\605plus\code>sh-coff-gcc -v
Reading specs from /sh-tools/sh-coff-2.95.2/lib/gcc-lib/sh-coff/2.95.2/specs
gcc version 2.95.2 19991024 (release)
D:\605plus\code>sh-coff-gcc -c -O3 -g test.cpp
D:\605plus\code>sh-coff-objdump -S test.o > test.lst
Thanks in advance.
Robert.