On 11 Feb 2008 09:54:19 -0800, Ian Lance Taylor <iant@xxxxxxxxxx> wrote: > "Godmar Back" <godmar@xxxxxxxxx> 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