public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/51513] New: [missed optimization] Only partially optimizes away unreachable switch default case
@ 2011-12-12 10:54 sgunderson at bigfoot dot com
  2011-12-12 11:04 ` [Bug tree-optimization/51513] " sgunderson at bigfoot dot com
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: sgunderson at bigfoot dot com @ 2011-12-12 10:54 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51513

             Bug #: 51513
           Summary: [missed optimization] Only partially optimizes away
                    unreachable switch default case
    Classification: Unclassified
           Product: gcc
           Version: 4.6.2
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: sgunderson@bigfoot.com


Hi,

I have code that looks like this:

pannekake:~> cat test.c
void foo();
void bar();
void baz();

void func(int i)
{
    switch (i) {
        case 0: foo(); break;
        case 1: bar(); break;
        case 2: baz(); break;
        case 3: baz(); break;
        case 4: bar(); break;
        case 5: foo(); break;
        case 6: foo(); break;
        case 7: bar(); break;
        case 8: baz(); break;
        case 9: baz(); break;
        case 10: bar(); break;
        default: __builtin_unreachable(); break;
    }
}

Compiling this yields:

pannekake:~> gcc-4.6 -O2 -c test.c && objdump --disassemble test.o

test.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <func>:
   0:    83 ff 0a                 cmp    $0xa,%edi
   3:    76 03                    jbe    8 <func+0x8>
   5:    0f 1f 00                 nopl   (%rax)
   8:    89 ff                    mov    %edi,%edi
   a:    31 c0                    xor    %eax,%eax
   c:    ff 24 fd 00 00 00 00     jmpq   *0x0(,%rdi,8)
  13:    0f 1f 44 00 00           nopl   0x0(%rax,%rax,1)
  18:    e9 00 00 00 00           jmpq   1d <func+0x1d>
  1d:    0f 1f 00                 nopl   (%rax)
  20:    e9 00 00 00 00           jmpq   25 <func+0x25>
  25:    0f 1f 00                 nopl   (%rax)
  28:    e9 00 00 00 00           jmpq   2d <func+0x2d>

The first compare is, as you can see, unneeded; the code for the default case
itself (a repz ret) has been optimized away due to the __builtin_unreachable(),
but the compare and branch remains.

I've also seen it sometimes be able to remove the jump instruction itself, but
not the compare.


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2020-04-17 18:31 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-12-12 10:54 [Bug tree-optimization/51513] New: [missed optimization] Only partially optimizes away unreachable switch default case sgunderson at bigfoot dot com
2011-12-12 11:04 ` [Bug tree-optimization/51513] " sgunderson at bigfoot dot com
2011-12-15  1:28 ` [Bug tree-optimization/51513] Only partially optimizes away __builtin_unreachable " pinskia at gcc dot gnu.org
2012-12-11 21:54 ` empann at hotmail dot com
2012-12-11 22:42 ` steven at gcc dot gnu.org
2014-02-28  1:37 ` pinskia at gcc dot gnu.org
2015-04-29  1:46 ` pinskia at gcc dot gnu.org
2015-04-29  2:25 ` bergner at gcc dot gnu.org
2015-04-29 14:58 ` bergner at gcc dot gnu.org
2020-04-07 23:09 ` mirq-gccboogs at rere dot qmqm.pl
2020-04-17 18:31 ` bergner 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).