Bug 14583 - a switch statement can be converted to an array access
Summary: a switch statement can be converted to an array access
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: 4.0.0
: P2 enhancement
Target Milestone: 4.4.0
Assignee: Not yet assigned to anyone
Keywords: missed-optimization
Depends on:
Blocks: 16996
  Show dependency treegraph
Reported: 2004-03-15 16:11 UTC by Kazu Hirata
Modified: 2008-09-08 19:51 UTC (History)
2 users (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed: 2006-03-05 03:12:40


Note You need to log in before you can comment on or make changes to this bug.
Description Kazu Hirata 2004-03-15 16:11:46 UTC
From reverse_condition() from jump.c

enum rtx_code
reverse_condition (enum rtx_code code)
  switch (code)
    case EQ:
      return NE;
    case NE:
      return EQ;
    case GT:
      return LE;


    case LTGT:
      return UNKNOWN;

      abort ();

All the cases except the last one returns a value, so the compiler
should be able to convert this switch statement to something like

  code -= LOWER_BOUND;
  if ((unsigned) code > UPPER_BOUND)
    abort ();
  tem = cases[code];
  if (tem == DEFAULT_CASE)
    abort ();
  return tem;

I think this is a win for both code size and speed on most, if not
all, platforms.

This kind of switch should be fairly common, and people may not like
to use an array themselves if the switch condition is an enum, just
like reverse_condition() in jump.c.

tree-ssa may be a good place to detect this.  I don't know if the
implementation should be done in tree-ssa or at expand time.  One
thing we can do is that just like CALL_EXPR_TAILCALL, we could flag
this kind of switch statement in tree-ssa and then emit an optimized
switch statement at the expand time.
Comment 1 Andrew Pinski 2004-03-15 16:57:51 UTC
Confirmed, one thing that needs to be done to do this optimization is combine all the return 
statements into one block for the switch statement, I think I can add that to tree-ssa-return.
Comment 2 Kazu Hirata 2004-03-25 17:22:27 UTC
Some hope of getting this fixed:

Comment 3 Steven Bosscher 2005-09-02 11:24:29 UTC
Kazu, is this still not optimized properly? 
Comment 4 Andrew Pinski 2008-09-08 19:51:56 UTC
This was fixed by:
2008-07-01  Martin Jambor  <mjambor@suse.cz>

        * Makefile.in (tree-switch-conversion.o): Add.
        (OBJS-common): Add tree-swtch-conversion.o.
        * passes.c (init_optimization_passes): Add pass_convert_switch.
        * tree-pass.h: (pass_convert_switch): Add.
        * tree-switch-conversion.c: New file.
        * gcc.dg/tree-ssa/cswtch.c: New testcase.
        * common.opt (ftree-cswtch): New option.
        * params.h (PARAM_SWITCH_CONVERSION_BRANCH_RATIO): New parameter.
        * params.def (PARAM_SWITCH_CONVERSION_BRANCH_RATIO): New parameter.
        * opts.c (decode_options): Set flag_tree_switch_conversion when
        optimization level is >= 2.
        * doc/invoke.texi (Optimize Options): Added description of
        -ftree-swtch-conversion and switch-conversion-max-branch-ratio.