From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 15768 invoked by alias); 12 Dec 2011 10:54:02 -0000 Received: (qmail 15756 invoked by uid 22791); 12 Dec 2011 10:54:02 -0000 X-SWARE-Spam-Status: No, hits=-2.8 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00,TW_BJ X-Spam-Check-By: sourceware.org Received: from localhost (HELO gcc.gnu.org) (127.0.0.1) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 12 Dec 2011 10:53:48 +0000 From: "sgunderson at bigfoot dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/51513] New: [missed optimization] Only partially optimizes away unreachable switch default case Date: Mon, 12 Dec 2011 10:54:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Keywords: X-Bugzilla-Severity: enhancement X-Bugzilla-Who: sgunderson at bigfoot dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2011-12/txt/msg01231.txt.bz2 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 : 0: 83 ff 0a cmp $0xa,%edi 3: 76 03 jbe 8 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 1d: 0f 1f 00 nopl (%rax) 20: e9 00 00 00 00 jmpq 25 25: 0f 1f 00 nopl (%rax) 28: e9 00 00 00 00 jmpq 2d 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.