public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/58405] New: Unoptimal code generated for computed goto
@ 2013-09-12 11:15 semicontinuity at yandex dot ru
2013-09-14 18:30 ` [Bug target/58405] " gjl at gcc dot gnu.org
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: semicontinuity at yandex dot ru @ 2013-09-12 11:15 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58405
Bug ID: 58405
Summary: Unoptimal code generated for computed goto
Product: gcc
Version: 4.7.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: semicontinuity at yandex dot ru
The following code
//
-----------------------------------------------------------------------------
static void test(void) {
char flag = 1;
static void* address = &&L0;
goto *address;
for(;;) {
L0:
asm volatile("nop\t\n");
flag = 0;
L1: (void)&&L1;
if (flag==0) { address = &&L1; return; }
asm volatile("nop\t\n");
flag = 0;
L2: (void)&&L2;
if (flag==0) { address = &&L2; return; }
}
}
int main(void) {
for(;;) {
test();
}
return 0;
}
//
-----------------------------------------------------------------------------
Compiles to:
//
-----------------------------------------------------------------------------
00000052 <test>:
static void test(void) {
52: cf 93 push r28
54: df 93 push r29
56: 00 d0 rcall .+0 ; 0x58 <test+0x6>
58: cd b7 in r28, 0x3d ; 61
5a: de b7 in r29, 0x3e ; 62
5c: 80 91 60 00 lds r24, 0x0060
60: 90 91 61 00 lds r25, 0x0061
64: 8f 93 push r24
66: 9f 93 push r25
68: 08 95 ret
goto *address;
for(;;) {
L0:
asm volatile("nop\t\n");
6a: 00 00 nop
flag = 0;
L1: (void)&&L1;
if (flag==0) { address = &&L1; return; }
6c: 81 e4 ldi r24, 0x41 ; 65
6e: 90 e0 ldi r25, 0x00 ; 0
70: 90 93 61 00 sts 0x0061, r25
74: 80 93 60 00 sts 0x0060, r24
flag = 0;
L2: (void)&&L2;
if (flag==0) { address = &&L2; return; }
}
}
78: 0f 90 pop r0
7a: 0f 90 pop r0
7c: df 91 pop r29
7e: cf 91 pop r28
80: 08 95 ret
...
//
-----------------------------------------------------------------------------
This code works but is non optimal:
- It saves stack pointer to r28:r29, but later r28:r29 is never used
- Because it uses r28:r29, it saves r28:r29 to stack.
- It makes rcall .+0 and later removes 2 bytes from stack (pop r0) - this is
not necessary; rcall .+0 and pop r0's can be removed.
- Computed goto is implemented with 5 instructions, 7 words, with stack usage:
5c: 80 91 60 00 lds r24, 0x0060
60: 90 91 61 00 lds r25, 0x0061
64: 8f 93 push r24
66: 9f 93 push r25
68: 08 95 ret
Instead, it could be implemented with
lds r30, 0x0060
lds r31, 0x0061
ijmp
with 3 instructions, 5 words, no additional memory access, but with clobbering
of r30:r31 instead of r24:r25 which is OK.
// ------------------------------------
Compiling C: main.c
avr-gcc -c -mmcu=atmega8535 -I. -gdwarf-2 -I../../bsp -I../../../.. -O3
-save-temps -v -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
-Wall -Wstrict-prototypes -Wundef -Wa,-adhlns=.obj/main.lst -std=gnu99 -Wundef
-MD -MP -MF .dep/main.o.d main.c -o .obj/main.o
Using built-in specs.
COLLECT_GCC=avr-gcc
Target: avr
Configured with: ../../gcc.gnu.org/gcc-4_7-branch/configure --target=avr
--prefix=/local/gnu/install/gcc-4.7-mingw32 --host=i386-mingw32
--build=i686-linux-gnu --enable-languages=c,c++ --disable-nls --disable-shared
--with-dwarf2 --with-avrlibc=yes
Thread model: single
gcc version 4.7.2 (GCC)
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug target/58405] Unoptimal code generated for computed goto
2013-09-12 11:15 [Bug c/58405] New: Unoptimal code generated for computed goto semicontinuity at yandex dot ru
@ 2013-09-14 18:30 ` gjl at gcc dot gnu.org
2013-09-14 18:30 ` gjl at gcc dot gnu.org
2013-09-16 16:14 ` gjl at gcc dot gnu.org
2 siblings, 0 replies; 4+ messages in thread
From: gjl at gcc dot gnu.org @ 2013-09-14 18:30 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58405
Georg-Johann Lay <gjl at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |gjl at gcc dot gnu.org
--- Comment #1 from Georg-Johann Lay <gjl at gcc dot gnu.org> ---
Created attachment 30822
--> http://gcc.gnu.org/bugzilla/attachment.cgi?id=30822&action=edit
Generated Assembly
I cannot reproduce this, see the attached assembly code generated with the same
4.7.2 build, same applies to 4.8.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug target/58405] Unoptimal code generated for computed goto
2013-09-12 11:15 [Bug c/58405] New: Unoptimal code generated for computed goto semicontinuity at yandex dot ru
2013-09-14 18:30 ` [Bug target/58405] " gjl at gcc dot gnu.org
@ 2013-09-14 18:30 ` gjl at gcc dot gnu.org
2013-09-16 16:14 ` gjl at gcc dot gnu.org
2 siblings, 0 replies; 4+ messages in thread
From: gjl at gcc dot gnu.org @ 2013-09-14 18:30 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58405
Georg-Johann Lay <gjl at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |WAITING
Last reconfirmed| |2013-09-14
Ever confirmed|0 |1
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug target/58405] Unoptimal code generated for computed goto
2013-09-12 11:15 [Bug c/58405] New: Unoptimal code generated for computed goto semicontinuity at yandex dot ru
2013-09-14 18:30 ` [Bug target/58405] " gjl at gcc dot gnu.org
2013-09-14 18:30 ` gjl at gcc dot gnu.org
@ 2013-09-16 16:14 ` gjl at gcc dot gnu.org
2 siblings, 0 replies; 4+ messages in thread
From: gjl at gcc dot gnu.org @ 2013-09-16 16:14 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58405
Georg-Johann Lay <gjl at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|WAITING |RESOLVED
Resolution|--- |WORKSFORME
--- Comment #3 from Georg-Johann Lay <gjl at gcc dot gnu.org> ---
(In reply to semicontinuity from comment #2)
> Now I cannot reproduce that as well..
> It seems that I've compiled it with -ffixed-r30 -ffixed-r31 (different from
> original intention) - in this case it produces this kind of assembly .
I can reproduce it with -ffixed-r30 -ffixed-r31. But as I already wrote above,
without Z (R30/R31) you cannot no more write sensible programs -- except
completely trivial ones. I don't think it makes sense to put effort into
optimizing useless programs or for programs that very likely will break by
occupying Z.
> Still, the code could be more optimal, without rjmp, push/pop - just with
> ijmp replaced by 2 pushes + ret.
Without -ffixed*, the code /does/ use IJMP. If you take away Z, it's not
possible to use IJMP, of course.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2013-09-16 16:14 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-09-12 11:15 [Bug c/58405] New: Unoptimal code generated for computed goto semicontinuity at yandex dot ru
2013-09-14 18:30 ` [Bug target/58405] " gjl at gcc dot gnu.org
2013-09-14 18:30 ` gjl at gcc dot gnu.org
2013-09-16 16:14 ` gjl at gcc dot gnu.org
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).