optimization of switch statements on i386
Godmar Back
godmar@gmail.com
Sat Feb 9 03:02:00 GMT 2008
On 08 Feb 2008 18:04:23 -0800, Ian Lance Taylor <iant@google.com> wrote:
>
> Look at expand_case in gcc/stmt.c.
>
Thanks. So I did that, and that explains why my example with 4 cases
does not use a table.
gcc/expr.c says:
/* If the machine does not have a case insn that compares the bounds,
this means extra overhead for dispatch tables, which raises the
threshold for using them. */
#ifndef CASE_VALUES_THRESHOLD
#define CASE_VALUES_THRESHOLD (HAVE_casesi ? 4 : 5)
#endif /* CASE_VALUES_THRESHOLD */
and a binary search is used if #arms < CASE_VALUES_THRESHOLD.
This leads to my next question.
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.)
Thanks,
- Godmar
More information about the Gcc-help
mailing list