Re: Optimization of "switch" stmt in C

Vladimir Makarov <vmakarov@cygnus.com>
24 Sep 1999 22:56:54 -0400

          From comp.compilers

Related articles
Optimization of "switch" stmt in C mayur_naik@my-deja.com (1999-09-20)
Re: Optimization of "switch" stmt in C ast@halcyon.com (Andrew Tucker) (1999-09-20)
Re: Optimization of "switch" stmt in C jejones@microware.com (James Jones) (1999-09-20)
Re: Optimization of "switch" stmt in C jcownie@etnus.com (James Cownie) (1999-09-24)
Re: Optimization of "switch" stmt in C ger@informatik.uni-nospam-bremen.de (George Russell) (1999-09-24)
Re: Optimization of "switch" stmt in C vmakarov@cygnus.com (Vladimir Makarov) (1999-09-24)
Re: Optimization of "switch" stmt in C danielv@crt.umontreal.ca (Daniel Villeneuve) (1999-09-27)
Re: Optimization of "switch" stmt in C drh@microsoft.com (Dave Hanson) (1999-09-28)
Re: Optimization of "switch" stmt in C cmilner@virginia.edu (Christopher W. Milner) (1999-10-01)
| List of all articles for this month |

From: Vladimir Makarov <vmakarov@cygnus.com>
Newsgroups: comp.compilers
Date: 24 Sep 1999 22:56:54 -0400
Organization: Cygnus Solutions
References: 99-09-081
Keywords: C, code

mayur_naik@my-deja.com wrote:


> GNU CC generates a switch table, a binary search, or a linear
> search for a switch stmt. I want to understand the exact algorithm.
> Any help is greatly appreciated.
>


GNU CC has a simple heuristic which is mainly (there are many hints too)
the following


      if number of cases < 5 (or 4 if machine has special insn to jump
through dispatch table, including bounds checking)
                                or range of case values > 10 * number of cases, then
            the binary search is used which becomes a linear search if there
are few cases.


      otherwise
              the jump table is used.


When gcc uses binary search, it believes that all cases have equal
probability. The algorithm could be improved if we use profile
information.


Another drawback of Gcc switch generation is that gcc uses binary search
if we have several dense regions of case values but the range of case
values is large, e.g.


                      case 1:
                      case 2:
                      ....
                      case 100001:
                      case 100002:


Some combination binary search and jump table would be helpful to
improve the code.


Actually, there are some works in improving GNU CC switch generation.


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.