1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/patches/gcc/4.2.0/260-flatten-switch-stmt-00.patch Sun May 08 10:52:21 2011 +0200
1.3 @@ -0,0 +1,153 @@
1.4 +Hi,
1.5 +
1.6 +The attached patch makes sure that we create smaller object code for
1.7 +simple switch statements. We just make sure to flatten the switch
1.8 +statement into an if-else chain, basically.
1.9 +
1.10 +This fixes a size-regression as compared to gcc-3.4, as can be seen
1.11 +below.
1.12 +
1.13 +2007-04-15 Bernhard Fischer <..>
1.14 +
1.15 + * stmt.c (expand_case): Do not create a complex binary tree when
1.16 + optimizing for size but rather use the simple ordered list.
1.17 + (emit_case_nodes): do not emit jumps to the default_label when
1.18 + optimizing for size.
1.19 +
1.20 +Not regtested so far.
1.21 +Comments?
1.22 +
1.23 +Attached is the test switch.c mentioned below.
1.24 +
1.25 +$ for i in 2.95 3.3 3.4 4.0 4.1 4.2.orig-HEAD 4.3.orig-HEAD 4.3-HEAD;do
1.26 +gcc-$i -DCHAIN -Os -o switch-CHAIN-$i.o -c switch.c ;done
1.27 +$ for i in 2.95 3.3 3.4 4.0 4.1 4.2.orig-HEAD 4.3.orig-HEAD 4.3-HEAD;do
1.28 +gcc-$i -UCHAIN -Os -o switch-$i.o -c switch.c ;done
1.29 +
1.30 +$ size switch-*.o
1.31 + text data bss dec hex filename
1.32 + 169 0 0 169 a9 switch-2.95.o
1.33 + 115 0 0 115 73 switch-3.3.o
1.34 + 103 0 0 103 67 switch-3.4.o
1.35 + 124 0 0 124 7c switch-4.0.o
1.36 + 124 0 0 124 7c switch-4.1.o
1.37 + 124 0 0 124 7c switch-4.2.orig-HEAD.o
1.38 + 95 0 0 95 5f switch-4.3-HEAD.o
1.39 + 124 0 0 124 7c switch-4.3.orig-HEAD.o
1.40 + 166 0 0 166 a6 switch-CHAIN-2.95.o
1.41 + 111 0 0 111 6f switch-CHAIN-3.3.o
1.42 + 95 0 0 95 5f switch-CHAIN-3.4.o
1.43 + 95 0 0 95 5f switch-CHAIN-4.0.o
1.44 + 95 0 0 95 5f switch-CHAIN-4.1.o
1.45 + 95 0 0 95 5f switch-CHAIN-4.2.orig-HEAD.o
1.46 + 95 0 0 95 5f switch-CHAIN-4.3-HEAD.o
1.47 + 95 0 0 95 5f switch-CHAIN-4.3.orig-HEAD.o
1.48 +
1.49 +
1.50 +Content-Type: text/x-diff; charset=us-ascii
1.51 +Content-Disposition: attachment; filename="gcc-4.3.gcc-flatten-switch-stmt.00.diff"
1.52 +
1.53 +Index: gcc-4.2.0/gcc/stmt.c
1.54 +===================================================================
1.55 +--- gcc-4.2.0.orig/gcc/stmt.c (revision 123843)
1.56 ++++ gcc-4.2.0/gcc/stmt.c (working copy)
1.57 +@@ -2517,7 +2517,11 @@ expand_case (tree exp)
1.58 + use_cost_table
1.59 + = (TREE_CODE (orig_type) != ENUMERAL_TYPE
1.60 + && estimate_case_costs (case_list));
1.61 +- balance_case_nodes (&case_list, NULL);
1.62 ++ /* When optimizing for size, we want a straight list to avoid
1.63 ++ jumps as much as possible. This basically creates an if-else
1.64 ++ chain. */
1.65 ++ if (!optimize_size)
1.66 ++ balance_case_nodes (&case_list, NULL);
1.67 + emit_case_nodes (index, case_list, default_label, index_type);
1.68 + emit_jump (default_label);
1.69 + }
1.70 +@@ -3075,6 +3079,7 @@ emit_case_nodes (rtx index, case_node_pt
1.71 + {
1.72 + if (!node_has_low_bound (node, index_type))
1.73 + {
1.74 ++ if (!optimize_size) /* don't jl to the .default_label. */
1.75 + emit_cmp_and_jump_insns (index,
1.76 + convert_modes
1.77 + (mode, imode,
1.78 +
1.79 +
1.80 +Content-Type: text/x-csrc; charset=us-ascii
1.81 +Content-Disposition: attachment; filename="switch.c"
1.82 +
1.83 +int
1.84 +commutative_tree_code (int code)
1.85 +{
1.86 +#define CASE(val, ret) case val:/* __asm__("# val="#val ",ret="#ret);*/ return ret;
1.87 +#ifndef CHAIN
1.88 + switch (code)
1.89 + {
1.90 +# if 1
1.91 + CASE(1,3)
1.92 + CASE(3,2)
1.93 + CASE(5,8)
1.94 + CASE(7,1)
1.95 + CASE(33,4)
1.96 + CASE(44,9)
1.97 + CASE(55,10)
1.98 + CASE(66,-1)
1.99 + CASE(77,99)
1.100 + CASE(666,0)
1.101 +# else
1.102 + case 1:
1.103 + return 3;
1.104 + case 3:
1.105 + return 2;
1.106 + case 5:
1.107 + return 8;
1.108 + case 7:
1.109 + return 1;
1.110 + case 33:
1.111 + return 4;
1.112 + case 44:
1.113 + return 9;
1.114 + case 55:
1.115 + return 10;
1.116 + case 66:
1.117 + return -1;
1.118 + case 77:
1.119 + return 99;
1.120 + case 666:
1.121 + return 0;
1.122 +# endif
1.123 + default:
1.124 + break;
1.125 + }
1.126 + return 4711;
1.127 +
1.128 +#else
1.129 + if (code == 1)
1.130 + return 3;
1.131 + else if (code == 3)
1.132 + return 2;
1.133 + else if (code == 5)
1.134 + return 8;
1.135 + else if (code == 7)
1.136 + return 1;
1.137 + else if (code == 33)
1.138 + return 4;
1.139 + else if (code == 44)
1.140 + return 9;
1.141 + else if (code == 55)
1.142 + return 10;
1.143 + else if (code == 66)
1.144 + return -1;
1.145 + else if (code == 77)
1.146 + return 99;
1.147 + else if (code == 666)
1.148 + return 0;
1.149 + else
1.150 + return 4711;
1.151 +#endif
1.152 +}
1.153 +
1.154 +
1.155 +--AhhlLboLdkugWU4S--
1.156 +