From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 3518 invoked by alias); 11 Feb 2008 18:13:44 -0000 Received: (qmail 3499 invoked by uid 22791); 11 Feb 2008 18:13:43 -0000 X-Spam-Check-By: sourceware.org Received: from mu-out-0910.google.com (HELO mu-out-0910.google.com) (209.85.134.190) by sourceware.org (qpsmtpd/0.31) with ESMTP; Mon, 11 Feb 2008 18:13:16 +0000 Received: by mu-out-0910.google.com with SMTP id w9so4115202mue.6 for ; Mon, 11 Feb 2008 10:13:12 -0800 (PST) Received: by 10.82.150.20 with SMTP id x20mr487858bud.37.1202753591973; Mon, 11 Feb 2008 10:13:11 -0800 (PST) Received: by 10.82.177.10 with HTTP; Mon, 11 Feb 2008 10:13:11 -0800 (PST) Message-ID: <719dced30802111013j661b2e25u45e5c775f6a8a2ec@mail.gmail.com> Date: Mon, 11 Feb 2008 18:13:00 -0000 From: "Godmar Back" To: "Ian Lance Taylor" Subject: Re: optimization of switch statements on i386 Cc: gcc-help@gcc.gnu.org In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <719dced30802081629g19f67f6fi76dfaa0ede35b7aa@mail.gmail.com> <719dced30802081630q6fca95a4u747533463c9177f7@mail.gmail.com> <719dced30802081901j4db41c18wbb930be9d9791e4c@mail.gmail.com> Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2008-02/txt/msg00079.txt.bz2 On 11 Feb 2008 09:54:19 -0800, Ian Lance Taylor wrote: > "Godmar Back" writes: > > > I increased the number of arms to 5, and retained the "default: > > gcc_unreachable()". > > Now gcc generates a bounds check, followed by a table jump. Good. Now > > how do I get rid of the bounds checks? > > > > Is there a way to inform gcc that the default branch is never taken? > > (Something along the lines of MSVC's __assert(0)? I had thought > > gcc_unreachable() would do that - but I may be misinformed here.) > > gcc_unreachable means nothing special to gcc. If you look at the > resulting code, you will see that it just compiles into a function > call. If you use -Wall you will see that the function was not > declared before being called. > > gcc_unreachable is used in the gcc source code itself, but that is > different from being recognized in gcc. It is defined as a macro in > gcc/system.h for use when building gcc itself: > #define gcc_unreachable() (fancy_abort (__FILE__, __LINE__, __FUNCTION__)) > Thanks. I did see that gcc_unreachable() showed up as a symbol in the assembly (consistent with it not meaning anything special); I suppose I was misled by this comment in the gcc coding conventions at http://gcc.gnu.org/codingconventions.html "Use gcc_unreachable() to mark places that should never be reachable (such as an unreachable default case of a switch). Do not use gcc_assert(0) for such purposes, as gcc_unreachable gives the compiler more information." Apparently, this discussion refers to the (currently executing) compiler, not the compiler used to compile the gcc code. > There is no way to tell gcc that the default branch is never taken, > although it will omit the default branch if it can prove that the > value is never out of range based on earlier conditional checks. > Good to know that I'm not looking for something that doesn't exist. I assume a corollary of that statement is that there is no way to trick the compiler into omitting the default branch without incurring runtime checks (or is there a clever way I'm not realizing)? - Godmar