public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/49054] New: useless cmp+jmp generated for switch when "default:" is unreachable
@ 2011-05-18 21:58 adl at gnu dot org
  2011-05-18 22:47 ` [Bug middle-end/49054] " pinskia at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: adl at gnu dot org @ 2011-05-18 21:58 UTC (permalink / raw)
  To: gcc-bugs

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

           Summary: useless cmp+jmp generated for switch when "default:"
                    is unreachable
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: adl@gnu.org
              Host: x86-64
             Build: x86-64


gcc (Debian 4.6.0-7) 4.6.1 20110507 (prerelease)

When I compile the following switch, GCC generates code to 
check that id <= 4 and to conditionally jump to... the next instruction.


% cat foo.c
unsigned f(void);
unsigned g(void);
unsigned h(void);
unsigned i(void);
unsigned j(void);

unsigned int baz(unsigned int id)
{
  switch (id)
    {
    case 0:
      return f();
    case 1:
      return g();
    case 2:
      return h();
    case 3:
      return i();
    case 4:
      return j();
    default:
      __builtin_unreachable();
    }
}
% gcc -march=core2 -m64 -O3 foo.c -c -o foo.o
% objdump -DC foo.o | head -23

foo.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <baz>:
   0:   83 ff 05                cmp    $0x4,%edi
   3:   76 03                   jbe    8 <baz+0x8>
   5:   0f 1f 00                nopl   (%rax)
   8:   89 ff                   mov    %edi,%edi
   a:   ff 24 fd 00 00 00 00    jmpq   *0x0(,%rdi,8)
  11:   0f 1f 80 00 00 00 00    nopl   0x0(%rax)
  18:   e9 00 00 00 00          jmpq   1d <baz+0x1d>
  1d:   0f 1f 00                nopl   (%rax)
  20:   e9 00 00 00 00          jmpq   25 <baz+0x25>
  25:   0f 1f 00                nopl   (%rax)
  28:   e9 00 00 00 00          jmpq   2d <baz+0x2d>
  2d:   0f 1f 00                nopl   (%rax)
  30:   e9 00 00 00 00          jmpq   35 <baz+0x35>
  35:   0f 1f 00                nopl   (%rax)
  38:   e9 00 00 00 00          jmpq   3d <baz+0x3d>


What is the point of the first three instructions?  I would have expected baz
to start at adress 8.


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

* [Bug middle-end/49054] useless cmp+jmp generated for switch when "default:" is unreachable
  2011-05-18 21:58 [Bug c/49054] New: useless cmp+jmp generated for switch when "default:" is unreachable adl at gnu dot org
@ 2011-05-18 22:47 ` pinskia at gcc dot gnu.org
  2011-05-19  9:18 ` rguenth at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2011-05-18 22:47 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
          Component|c                           |middle-end

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> 2011-05-18 22:09:25 UTC ---
a mips64-linux-gnu gcc produces wrong code for this testcase, removing the
whole jump table.


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

* [Bug middle-end/49054] useless cmp+jmp generated for switch when "default:" is unreachable
  2011-05-18 21:58 [Bug c/49054] New: useless cmp+jmp generated for switch when "default:" is unreachable adl at gnu dot org
  2011-05-18 22:47 ` [Bug middle-end/49054] " pinskia at gcc dot gnu.org
@ 2011-05-19  9:18 ` rguenth at gcc dot gnu.org
  2011-07-24 20:46 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: rguenth at gcc dot gnu.org @ 2011-05-19  9:18 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Richard Guenther <rguenth at gcc dot gnu.org> 2011-05-19 08:56:11 UTC ---
I think this is just an artifact of how we handle __builtin_unreachable ().


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

* [Bug middle-end/49054] useless cmp+jmp generated for switch when "default:" is unreachable
  2011-05-18 21:58 [Bug c/49054] New: useless cmp+jmp generated for switch when "default:" is unreachable adl at gnu dot org
  2011-05-18 22:47 ` [Bug middle-end/49054] " pinskia at gcc dot gnu.org
  2011-05-19  9:18 ` rguenth at gcc dot gnu.org
@ 2011-07-24 20:46 ` pinskia at gcc dot gnu.org
  2011-11-22 13:01 ` [Bug rtl-optimization/49054] " ebotcazou at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2011-07-24 20:46 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2011.07.24 20:46:29
     Ever Confirmed|0                           |1

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> 2011-07-24 20:46:29 UTC ---
Confirmed.


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

* [Bug rtl-optimization/49054] useless cmp+jmp generated for switch when "default:" is unreachable
  2011-05-18 21:58 [Bug c/49054] New: useless cmp+jmp generated for switch when "default:" is unreachable adl at gnu dot org
                   ` (2 preceding siblings ...)
  2011-07-24 20:46 ` pinskia at gcc dot gnu.org
@ 2011-11-22 13:01 ` ebotcazou at gcc dot gnu.org
  2015-08-20  9:52 ` johannespfau at gmail dot com
  2023-05-13 17:57 ` chfast at gmail dot com
  5 siblings, 0 replies; 7+ messages in thread
From: ebotcazou at gcc dot gnu.org @ 2011-11-22 13:01 UTC (permalink / raw)
  To: gcc-bugs

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

Eric Botcazou <ebotcazou at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|wrong-code                  |missed-optimization
                 CC|                            |ebotcazou at gcc dot
                   |                            |gnu.org
          Component|middle-end                  |rtl-optimization
               Host|x86-64                      |
            Version|unknown                     |4.6.0
              Build|x86-64                      |
           Severity|normal                      |enhancement

--- Comment #4 from Eric Botcazou <ebotcazou at gcc dot gnu.org> 2011-11-22 11:37:38 UTC ---
The CFG cleanup code should be enhanced to deal with this.


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

* [Bug rtl-optimization/49054] useless cmp+jmp generated for switch when "default:" is unreachable
  2011-05-18 21:58 [Bug c/49054] New: useless cmp+jmp generated for switch when "default:" is unreachable adl at gnu dot org
                   ` (3 preceding siblings ...)
  2011-11-22 13:01 ` [Bug rtl-optimization/49054] " ebotcazou at gcc dot gnu.org
@ 2015-08-20  9:52 ` johannespfau at gmail dot com
  2023-05-13 17:57 ` chfast at gmail dot com
  5 siblings, 0 replies; 7+ messages in thread
From: johannespfau at gmail dot com @ 2015-08-20  9:52 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49054

Johannes Pfau <johannespfau at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |johannespfau at gmail dot com

--- Comment #5 from Johannes Pfau <johannespfau at gmail dot com> ---
This would also be useful for the GDC frontend. There's a `final switch`
statement which forces the developer to handle all possible cases. Would be
nice if we could get rid of the `cmp` check for these `final switch`
statements.


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

* [Bug rtl-optimization/49054] useless cmp+jmp generated for switch when "default:" is unreachable
  2011-05-18 21:58 [Bug c/49054] New: useless cmp+jmp generated for switch when "default:" is unreachable adl at gnu dot org
                   ` (4 preceding siblings ...)
  2015-08-20  9:52 ` johannespfau at gmail dot com
@ 2023-05-13 17:57 ` chfast at gmail dot com
  5 siblings, 0 replies; 7+ messages in thread
From: chfast at gmail dot com @ 2023-05-13 17:57 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49054

Paweł Bylica <chfast at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |chfast at gmail dot com

--- Comment #7 from Paweł Bylica <chfast at gmail dot com> ---
GCC 13 generates optimal decision tree for the mentioned modified case.

if id == 3:
    i()
elif id <= 3:
    if id == 0:
        f()
    else:  # 1
        g()
else:
    if id == 4:
        j()
    else:  # 23456
        h()

https://godbolt.org/z/9j6b88qKE

So I think this issue is fixed.

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

end of thread, other threads:[~2023-05-13 17:57 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-18 21:58 [Bug c/49054] New: useless cmp+jmp generated for switch when "default:" is unreachable adl at gnu dot org
2011-05-18 22:47 ` [Bug middle-end/49054] " pinskia at gcc dot gnu.org
2011-05-19  9:18 ` rguenth at gcc dot gnu.org
2011-07-24 20:46 ` pinskia at gcc dot gnu.org
2011-11-22 13:01 ` [Bug rtl-optimization/49054] " ebotcazou at gcc dot gnu.org
2015-08-20  9:52 ` johannespfau at gmail dot com
2023-05-13 17:57 ` chfast at gmail dot com

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).