[PATCH] flatten switch-stmt into if-else chain for -Os

Mark Mitchell mark@codesourcery.com
Sun Apr 15 21:07:00 GMT 2007


Bernhard Fischer wrote:

> 2007-04-15  Bernhard Fischer  <..>
> 
> 	* stmt.c (expand_case): Do not create a complex binary tree when
> 	optimizing for size but rather use the simple ordered list.
> 	(emit_case_nodes): do not emit jumps to the default_label when
> 	optimizing for size.
> 
> Not regtested so far.
> Comments?

I think this is a good idea.  In general, I'm sure that we could better
tune -Os, without adding much to the compiler, just by making these
kinds of adjustments.  So, I'm happy to see you doing that.

Obviously, you need to do full testing.

> +	  /* When optimizing for size, we want a straight list to avoid
> +	     jumps as much as possible. This basically creates an if-else
> +	     chain.  */
> +	  if (!optimize_size)
> +	    balance_case_nodes (&case_list, NULL);

Perhaps the comment could be better written as:

/* When optimizing for speed, we build a binary tree, so that the number
of tests executed to determine which case applies is minimized.  The
binary tree method introduces additional tests, corresponding to
interior nodes in the tree.  Therefore, when optimizing for space, we
just emit a linear series of tests; while the program is likely to
execute more tests, we avoid consuming generating code for the
interior-node tests.  */

Have you by any chance tried running CSiBE with this patch, with -Os
turned on, to see what kind of different it makes?  I think that it
would be good to get some benchmark data (CSiBE, GCC itself, some other
application of interest) to make sure that this change actually does
seem to consistently help across more than just a small test case.

Thanks,

-- 
Mark Mitchell
CodeSourcery
mark@codesourcery.com
(650) 331-3385 x713



More information about the Gcc-patches mailing list