public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "semicontinuity at yandex dot ru" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/58405] New: Unoptimal code generated for computed goto
Date: Thu, 12 Sep 2013 11:15:00 -0000	[thread overview]
Message-ID: <bug-58405-4@http.gcc.gnu.org/bugzilla/> (raw)

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)


             reply	other threads:[~2013-09-12 11:15 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-12 11:15 semicontinuity at yandex dot ru [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-58405-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).