annotate gcc/tree-switch-conversion.c @ 127:4c56639505ff

fix function.c and add CbC-example Makefile
author mir3636
date Wed, 11 Apr 2018 18:46:58 +0900
parents 04ced10e8804
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents: 67
diff changeset
1 /* Lower GIMPLE_SWITCH expressions to something more efficient than
kono
parents: 67
diff changeset
2 a jump table.
kono
parents: 67
diff changeset
3 Copyright (C) 2006-2017 Free Software Foundation, Inc.
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 This file is part of GCC.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 GCC is free software; you can redistribute it and/or modify it
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 Free Software Foundation; either version 3, or (at your option) any
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 later version.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 GCC is distributed in the hope that it will be useful, but WITHOUT
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 for more details.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 along with GCC; see the file COPYING3. If not, write to the Free
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 02110-1301, USA. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21
111
kono
parents: 67
diff changeset
22 /* This file handles the lowering of GIMPLE_SWITCH to an indexed
kono
parents: 67
diff changeset
23 load, or a series of bit-test-and-branch expressions. */
kono
parents: 67
diff changeset
24
kono
parents: 67
diff changeset
25 #include "config.h"
kono
parents: 67
diff changeset
26 #include "system.h"
kono
parents: 67
diff changeset
27 #include "coretypes.h"
kono
parents: 67
diff changeset
28 #include "backend.h"
kono
parents: 67
diff changeset
29 #include "insn-codes.h"
kono
parents: 67
diff changeset
30 #include "rtl.h"
kono
parents: 67
diff changeset
31 #include "tree.h"
kono
parents: 67
diff changeset
32 #include "gimple.h"
kono
parents: 67
diff changeset
33 #include "cfghooks.h"
kono
parents: 67
diff changeset
34 #include "tree-pass.h"
kono
parents: 67
diff changeset
35 #include "ssa.h"
kono
parents: 67
diff changeset
36 #include "optabs-tree.h"
kono
parents: 67
diff changeset
37 #include "cgraph.h"
kono
parents: 67
diff changeset
38 #include "gimple-pretty-print.h"
kono
parents: 67
diff changeset
39 #include "params.h"
kono
parents: 67
diff changeset
40 #include "fold-const.h"
kono
parents: 67
diff changeset
41 #include "varasm.h"
kono
parents: 67
diff changeset
42 #include "stor-layout.h"
kono
parents: 67
diff changeset
43 #include "cfganal.h"
kono
parents: 67
diff changeset
44 #include "gimplify.h"
kono
parents: 67
diff changeset
45 #include "gimple-iterator.h"
kono
parents: 67
diff changeset
46 #include "gimplify-me.h"
kono
parents: 67
diff changeset
47 #include "tree-cfg.h"
kono
parents: 67
diff changeset
48 #include "cfgloop.h"
kono
parents: 67
diff changeset
49 #include "alloc-pool.h"
kono
parents: 67
diff changeset
50 #include "target.h"
kono
parents: 67
diff changeset
51 #include "tree-into-ssa.h"
kono
parents: 67
diff changeset
52
kono
parents: 67
diff changeset
53 /* ??? For lang_hooks.types.type_for_mode, but is there a word_mode
kono
parents: 67
diff changeset
54 type in the GIMPLE type system that is language-independent? */
kono
parents: 67
diff changeset
55 #include "langhooks.h"
kono
parents: 67
diff changeset
56
kono
parents: 67
diff changeset
57
kono
parents: 67
diff changeset
58 /* Maximum number of case bit tests.
kono
parents: 67
diff changeset
59 FIXME: This should be derived from PARAM_CASE_VALUES_THRESHOLD and
kono
parents: 67
diff changeset
60 targetm.case_values_threshold(), or be its own param. */
kono
parents: 67
diff changeset
61 #define MAX_CASE_BIT_TESTS 3
kono
parents: 67
diff changeset
62
kono
parents: 67
diff changeset
63 /* Split the basic block at the statement pointed to by GSIP, and insert
kono
parents: 67
diff changeset
64 a branch to the target basic block of E_TRUE conditional on tree
kono
parents: 67
diff changeset
65 expression COND.
kono
parents: 67
diff changeset
66
kono
parents: 67
diff changeset
67 It is assumed that there is already an edge from the to-be-split
kono
parents: 67
diff changeset
68 basic block to E_TRUE->dest block. This edge is removed, and the
kono
parents: 67
diff changeset
69 profile information on the edge is re-used for the new conditional
kono
parents: 67
diff changeset
70 jump.
kono
parents: 67
diff changeset
71
kono
parents: 67
diff changeset
72 The CFG is updated. The dominator tree will not be valid after
kono
parents: 67
diff changeset
73 this transformation, but the immediate dominators are updated if
kono
parents: 67
diff changeset
74 UPDATE_DOMINATORS is true.
kono
parents: 67
diff changeset
75
kono
parents: 67
diff changeset
76 Returns the newly created basic block. */
kono
parents: 67
diff changeset
77
kono
parents: 67
diff changeset
78 static basic_block
kono
parents: 67
diff changeset
79 hoist_edge_and_branch_if_true (gimple_stmt_iterator *gsip,
kono
parents: 67
diff changeset
80 tree cond, edge e_true,
kono
parents: 67
diff changeset
81 bool update_dominators)
kono
parents: 67
diff changeset
82 {
kono
parents: 67
diff changeset
83 tree tmp;
kono
parents: 67
diff changeset
84 gcond *cond_stmt;
kono
parents: 67
diff changeset
85 edge e_false;
kono
parents: 67
diff changeset
86 basic_block new_bb, split_bb = gsi_bb (*gsip);
kono
parents: 67
diff changeset
87 bool dominated_e_true = false;
kono
parents: 67
diff changeset
88
kono
parents: 67
diff changeset
89 gcc_assert (e_true->src == split_bb);
kono
parents: 67
diff changeset
90
kono
parents: 67
diff changeset
91 if (update_dominators
kono
parents: 67
diff changeset
92 && get_immediate_dominator (CDI_DOMINATORS, e_true->dest) == split_bb)
kono
parents: 67
diff changeset
93 dominated_e_true = true;
kono
parents: 67
diff changeset
94
kono
parents: 67
diff changeset
95 tmp = force_gimple_operand_gsi (gsip, cond, /*simple=*/true, NULL,
kono
parents: 67
diff changeset
96 /*before=*/true, GSI_SAME_STMT);
kono
parents: 67
diff changeset
97 cond_stmt = gimple_build_cond_from_tree (tmp, NULL_TREE, NULL_TREE);
kono
parents: 67
diff changeset
98 gsi_insert_before (gsip, cond_stmt, GSI_SAME_STMT);
kono
parents: 67
diff changeset
99
kono
parents: 67
diff changeset
100 e_false = split_block (split_bb, cond_stmt);
kono
parents: 67
diff changeset
101 new_bb = e_false->dest;
kono
parents: 67
diff changeset
102 redirect_edge_pred (e_true, split_bb);
kono
parents: 67
diff changeset
103
kono
parents: 67
diff changeset
104 e_true->flags &= ~EDGE_FALLTHRU;
kono
parents: 67
diff changeset
105 e_true->flags |= EDGE_TRUE_VALUE;
kono
parents: 67
diff changeset
106
kono
parents: 67
diff changeset
107 e_false->flags &= ~EDGE_FALLTHRU;
kono
parents: 67
diff changeset
108 e_false->flags |= EDGE_FALSE_VALUE;
kono
parents: 67
diff changeset
109 e_false->probability = e_true->probability.invert ();
kono
parents: 67
diff changeset
110 new_bb->count = e_false->count ();
kono
parents: 67
diff changeset
111
kono
parents: 67
diff changeset
112 if (update_dominators)
kono
parents: 67
diff changeset
113 {
kono
parents: 67
diff changeset
114 if (dominated_e_true)
kono
parents: 67
diff changeset
115 set_immediate_dominator (CDI_DOMINATORS, e_true->dest, split_bb);
kono
parents: 67
diff changeset
116 set_immediate_dominator (CDI_DOMINATORS, e_false->dest, split_bb);
kono
parents: 67
diff changeset
117 }
kono
parents: 67
diff changeset
118
kono
parents: 67
diff changeset
119 return new_bb;
kono
parents: 67
diff changeset
120 }
kono
parents: 67
diff changeset
121
kono
parents: 67
diff changeset
122
kono
parents: 67
diff changeset
123 /* Return true if a switch should be expanded as a bit test.
kono
parents: 67
diff changeset
124 RANGE is the difference between highest and lowest case.
kono
parents: 67
diff changeset
125 UNIQ is number of unique case node targets, not counting the default case.
kono
parents: 67
diff changeset
126 COUNT is the number of comparisons needed, not counting the default case. */
kono
parents: 67
diff changeset
127
kono
parents: 67
diff changeset
128 static bool
kono
parents: 67
diff changeset
129 expand_switch_using_bit_tests_p (tree range,
kono
parents: 67
diff changeset
130 unsigned int uniq,
kono
parents: 67
diff changeset
131 unsigned int count, bool speed_p)
kono
parents: 67
diff changeset
132 {
kono
parents: 67
diff changeset
133 return (((uniq == 1 && count >= 3)
kono
parents: 67
diff changeset
134 || (uniq == 2 && count >= 5)
kono
parents: 67
diff changeset
135 || (uniq == 3 && count >= 6))
kono
parents: 67
diff changeset
136 && lshift_cheap_p (speed_p)
kono
parents: 67
diff changeset
137 && compare_tree_int (range, GET_MODE_BITSIZE (word_mode)) < 0
kono
parents: 67
diff changeset
138 && compare_tree_int (range, 0) > 0);
kono
parents: 67
diff changeset
139 }
kono
parents: 67
diff changeset
140
kono
parents: 67
diff changeset
141 /* Implement switch statements with bit tests
kono
parents: 67
diff changeset
142
kono
parents: 67
diff changeset
143 A GIMPLE switch statement can be expanded to a short sequence of bit-wise
kono
parents: 67
diff changeset
144 comparisons. "switch(x)" is converted into "if ((1 << (x-MINVAL)) & CST)"
kono
parents: 67
diff changeset
145 where CST and MINVAL are integer constants. This is better than a series
kono
parents: 67
diff changeset
146 of compare-and-banch insns in some cases, e.g. we can implement:
kono
parents: 67
diff changeset
147
kono
parents: 67
diff changeset
148 if ((x==4) || (x==6) || (x==9) || (x==11))
kono
parents: 67
diff changeset
149
kono
parents: 67
diff changeset
150 as a single bit test:
kono
parents: 67
diff changeset
151
kono
parents: 67
diff changeset
152 if ((1<<x) & ((1<<4)|(1<<6)|(1<<9)|(1<<11)))
kono
parents: 67
diff changeset
153
kono
parents: 67
diff changeset
154 This transformation is only applied if the number of case targets is small,
kono
parents: 67
diff changeset
155 if CST constains at least 3 bits, and "1 << x" is cheap. The bit tests are
kono
parents: 67
diff changeset
156 performed in "word_mode".
kono
parents: 67
diff changeset
157
kono
parents: 67
diff changeset
158 The following example shows the code the transformation generates:
kono
parents: 67
diff changeset
159
kono
parents: 67
diff changeset
160 int bar(int x)
kono
parents: 67
diff changeset
161 {
kono
parents: 67
diff changeset
162 switch (x)
kono
parents: 67
diff changeset
163 {
kono
parents: 67
diff changeset
164 case '0': case '1': case '2': case '3': case '4':
kono
parents: 67
diff changeset
165 case '5': case '6': case '7': case '8': case '9':
kono
parents: 67
diff changeset
166 case 'A': case 'B': case 'C': case 'D': case 'E':
kono
parents: 67
diff changeset
167 case 'F':
kono
parents: 67
diff changeset
168 return 1;
kono
parents: 67
diff changeset
169 }
kono
parents: 67
diff changeset
170 return 0;
kono
parents: 67
diff changeset
171 }
kono
parents: 67
diff changeset
172
kono
parents: 67
diff changeset
173 ==>
kono
parents: 67
diff changeset
174
kono
parents: 67
diff changeset
175 bar (int x)
kono
parents: 67
diff changeset
176 {
kono
parents: 67
diff changeset
177 tmp1 = x - 48;
kono
parents: 67
diff changeset
178 if (tmp1 > (70 - 48)) goto L2;
kono
parents: 67
diff changeset
179 tmp2 = 1 << tmp1;
kono
parents: 67
diff changeset
180 tmp3 = 0b11111100000001111111111;
kono
parents: 67
diff changeset
181 if ((tmp2 & tmp3) != 0) goto L1 ; else goto L2;
kono
parents: 67
diff changeset
182 L1:
kono
parents: 67
diff changeset
183 return 1;
kono
parents: 67
diff changeset
184 L2:
kono
parents: 67
diff changeset
185 return 0;
kono
parents: 67
diff changeset
186 }
kono
parents: 67
diff changeset
187
kono
parents: 67
diff changeset
188 TODO: There are still some improvements to this transformation that could
kono
parents: 67
diff changeset
189 be implemented:
kono
parents: 67
diff changeset
190
kono
parents: 67
diff changeset
191 * A narrower mode than word_mode could be used if that is cheaper, e.g.
kono
parents: 67
diff changeset
192 for x86_64 where a narrower-mode shift may result in smaller code.
kono
parents: 67
diff changeset
193
kono
parents: 67
diff changeset
194 * The compounded constant could be shifted rather than the one. The
kono
parents: 67
diff changeset
195 test would be either on the sign bit or on the least significant bit,
kono
parents: 67
diff changeset
196 depending on the direction of the shift. On some machines, the test
kono
parents: 67
diff changeset
197 for the branch would be free if the bit to test is already set by the
kono
parents: 67
diff changeset
198 shift operation.
kono
parents: 67
diff changeset
199
kono
parents: 67
diff changeset
200 This transformation was contributed by Roger Sayle, see this e-mail:
kono
parents: 67
diff changeset
201 http://gcc.gnu.org/ml/gcc-patches/2003-01/msg01950.html
kono
parents: 67
diff changeset
202 */
kono
parents: 67
diff changeset
203
kono
parents: 67
diff changeset
204 /* A case_bit_test represents a set of case nodes that may be
kono
parents: 67
diff changeset
205 selected from using a bit-wise comparison. HI and LO hold
kono
parents: 67
diff changeset
206 the integer to be tested against, TARGET_EDGE contains the
kono
parents: 67
diff changeset
207 edge to the basic block to jump to upon success and BITS
kono
parents: 67
diff changeset
208 counts the number of case nodes handled by this test,
kono
parents: 67
diff changeset
209 typically the number of bits set in HI:LO. The LABEL field
kono
parents: 67
diff changeset
210 is used to quickly identify all cases in this set without
kono
parents: 67
diff changeset
211 looking at label_to_block for every case label. */
kono
parents: 67
diff changeset
212
kono
parents: 67
diff changeset
213 struct case_bit_test
kono
parents: 67
diff changeset
214 {
kono
parents: 67
diff changeset
215 wide_int mask;
kono
parents: 67
diff changeset
216 edge target_edge;
kono
parents: 67
diff changeset
217 tree label;
kono
parents: 67
diff changeset
218 int bits;
kono
parents: 67
diff changeset
219 };
kono
parents: 67
diff changeset
220
kono
parents: 67
diff changeset
221 /* Comparison function for qsort to order bit tests by decreasing
kono
parents: 67
diff changeset
222 probability of execution. Our best guess comes from a measured
kono
parents: 67
diff changeset
223 profile. If the profile counts are equal, break even on the
kono
parents: 67
diff changeset
224 number of case nodes, i.e. the node with the most cases gets
kono
parents: 67
diff changeset
225 tested first.
kono
parents: 67
diff changeset
226
kono
parents: 67
diff changeset
227 TODO: Actually this currently runs before a profile is available.
kono
parents: 67
diff changeset
228 Therefore the case-as-bit-tests transformation should be done
kono
parents: 67
diff changeset
229 later in the pass pipeline, or something along the lines of
kono
parents: 67
diff changeset
230 "Efficient and effective branch reordering using profile data"
kono
parents: 67
diff changeset
231 (Yang et. al., 2002) should be implemented (although, how good
kono
parents: 67
diff changeset
232 is a paper is called "Efficient and effective ..." when the
kono
parents: 67
diff changeset
233 latter is implied by the former, but oh well...). */
kono
parents: 67
diff changeset
234
kono
parents: 67
diff changeset
235 static int
kono
parents: 67
diff changeset
236 case_bit_test_cmp (const void *p1, const void *p2)
kono
parents: 67
diff changeset
237 {
kono
parents: 67
diff changeset
238 const struct case_bit_test *const d1 = (const struct case_bit_test *) p1;
kono
parents: 67
diff changeset
239 const struct case_bit_test *const d2 = (const struct case_bit_test *) p2;
kono
parents: 67
diff changeset
240
kono
parents: 67
diff changeset
241 if (d2->target_edge->count () < d1->target_edge->count ())
kono
parents: 67
diff changeset
242 return -1;
kono
parents: 67
diff changeset
243 if (d2->target_edge->count () > d1->target_edge->count ())
kono
parents: 67
diff changeset
244 return 1;
kono
parents: 67
diff changeset
245 if (d2->bits != d1->bits)
kono
parents: 67
diff changeset
246 return d2->bits - d1->bits;
kono
parents: 67
diff changeset
247
kono
parents: 67
diff changeset
248 /* Stabilize the sort. */
kono
parents: 67
diff changeset
249 return LABEL_DECL_UID (d2->label) - LABEL_DECL_UID (d1->label);
kono
parents: 67
diff changeset
250 }
kono
parents: 67
diff changeset
251
kono
parents: 67
diff changeset
252 /* Expand a switch statement by a short sequence of bit-wise
kono
parents: 67
diff changeset
253 comparisons. "switch(x)" is effectively converted into
kono
parents: 67
diff changeset
254 "if ((1 << (x-MINVAL)) & CST)" where CST and MINVAL are
kono
parents: 67
diff changeset
255 integer constants.
kono
parents: 67
diff changeset
256
kono
parents: 67
diff changeset
257 INDEX_EXPR is the value being switched on.
kono
parents: 67
diff changeset
258
kono
parents: 67
diff changeset
259 MINVAL is the lowest case value of in the case nodes,
kono
parents: 67
diff changeset
260 and RANGE is highest value minus MINVAL. MINVAL and RANGE
kono
parents: 67
diff changeset
261 are not guaranteed to be of the same type as INDEX_EXPR
kono
parents: 67
diff changeset
262 (the gimplifier doesn't change the type of case label values,
kono
parents: 67
diff changeset
263 and MINVAL and RANGE are derived from those values).
kono
parents: 67
diff changeset
264 MAXVAL is MINVAL + RANGE.
kono
parents: 67
diff changeset
265
kono
parents: 67
diff changeset
266 There *MUST* be MAX_CASE_BIT_TESTS or less unique case
kono
parents: 67
diff changeset
267 node targets. */
kono
parents: 67
diff changeset
268
kono
parents: 67
diff changeset
269 static void
kono
parents: 67
diff changeset
270 emit_case_bit_tests (gswitch *swtch, tree index_expr,
kono
parents: 67
diff changeset
271 tree minval, tree range, tree maxval)
kono
parents: 67
diff changeset
272 {
kono
parents: 67
diff changeset
273 struct case_bit_test test[MAX_CASE_BIT_TESTS] = { {} };
kono
parents: 67
diff changeset
274 unsigned int i, j, k;
kono
parents: 67
diff changeset
275 unsigned int count;
kono
parents: 67
diff changeset
276
kono
parents: 67
diff changeset
277 basic_block switch_bb = gimple_bb (swtch);
kono
parents: 67
diff changeset
278 basic_block default_bb, new_default_bb, new_bb;
kono
parents: 67
diff changeset
279 edge default_edge;
kono
parents: 67
diff changeset
280 bool update_dom = dom_info_available_p (CDI_DOMINATORS);
kono
parents: 67
diff changeset
281
kono
parents: 67
diff changeset
282 vec<basic_block> bbs_to_fix_dom = vNULL;
kono
parents: 67
diff changeset
283
kono
parents: 67
diff changeset
284 tree index_type = TREE_TYPE (index_expr);
kono
parents: 67
diff changeset
285 tree unsigned_index_type = unsigned_type_for (index_type);
kono
parents: 67
diff changeset
286 unsigned int branch_num = gimple_switch_num_labels (swtch);
kono
parents: 67
diff changeset
287
kono
parents: 67
diff changeset
288 gimple_stmt_iterator gsi;
kono
parents: 67
diff changeset
289 gassign *shift_stmt;
kono
parents: 67
diff changeset
290
kono
parents: 67
diff changeset
291 tree idx, tmp, csui;
kono
parents: 67
diff changeset
292 tree word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
kono
parents: 67
diff changeset
293 tree word_mode_zero = fold_convert (word_type_node, integer_zero_node);
kono
parents: 67
diff changeset
294 tree word_mode_one = fold_convert (word_type_node, integer_one_node);
kono
parents: 67
diff changeset
295 int prec = TYPE_PRECISION (word_type_node);
kono
parents: 67
diff changeset
296 wide_int wone = wi::one (prec);
kono
parents: 67
diff changeset
297
kono
parents: 67
diff changeset
298 /* Get the edge for the default case. */
kono
parents: 67
diff changeset
299 tmp = gimple_switch_default_label (swtch);
kono
parents: 67
diff changeset
300 default_bb = label_to_block (CASE_LABEL (tmp));
kono
parents: 67
diff changeset
301 default_edge = find_edge (switch_bb, default_bb);
kono
parents: 67
diff changeset
302
kono
parents: 67
diff changeset
303 /* Go through all case labels, and collect the case labels, profile
kono
parents: 67
diff changeset
304 counts, and other information we need to build the branch tests. */
kono
parents: 67
diff changeset
305 count = 0;
kono
parents: 67
diff changeset
306 for (i = 1; i < branch_num; i++)
kono
parents: 67
diff changeset
307 {
kono
parents: 67
diff changeset
308 unsigned int lo, hi;
kono
parents: 67
diff changeset
309 tree cs = gimple_switch_label (swtch, i);
kono
parents: 67
diff changeset
310 tree label = CASE_LABEL (cs);
kono
parents: 67
diff changeset
311 edge e = find_edge (switch_bb, label_to_block (label));
kono
parents: 67
diff changeset
312 for (k = 0; k < count; k++)
kono
parents: 67
diff changeset
313 if (e == test[k].target_edge)
kono
parents: 67
diff changeset
314 break;
kono
parents: 67
diff changeset
315
kono
parents: 67
diff changeset
316 if (k == count)
kono
parents: 67
diff changeset
317 {
kono
parents: 67
diff changeset
318 gcc_checking_assert (count < MAX_CASE_BIT_TESTS);
kono
parents: 67
diff changeset
319 test[k].mask = wi::zero (prec);
kono
parents: 67
diff changeset
320 test[k].target_edge = e;
kono
parents: 67
diff changeset
321 test[k].label = label;
kono
parents: 67
diff changeset
322 test[k].bits = 1;
kono
parents: 67
diff changeset
323 count++;
kono
parents: 67
diff changeset
324 }
kono
parents: 67
diff changeset
325 else
kono
parents: 67
diff changeset
326 test[k].bits++;
kono
parents: 67
diff changeset
327
kono
parents: 67
diff changeset
328 lo = tree_to_uhwi (int_const_binop (MINUS_EXPR,
kono
parents: 67
diff changeset
329 CASE_LOW (cs), minval));
kono
parents: 67
diff changeset
330 if (CASE_HIGH (cs) == NULL_TREE)
kono
parents: 67
diff changeset
331 hi = lo;
kono
parents: 67
diff changeset
332 else
kono
parents: 67
diff changeset
333 hi = tree_to_uhwi (int_const_binop (MINUS_EXPR,
kono
parents: 67
diff changeset
334 CASE_HIGH (cs), minval));
kono
parents: 67
diff changeset
335
kono
parents: 67
diff changeset
336 for (j = lo; j <= hi; j++)
kono
parents: 67
diff changeset
337 test[k].mask |= wi::lshift (wone, j);
kono
parents: 67
diff changeset
338 }
kono
parents: 67
diff changeset
339
kono
parents: 67
diff changeset
340 qsort (test, count, sizeof (*test), case_bit_test_cmp);
kono
parents: 67
diff changeset
341
kono
parents: 67
diff changeset
342 /* If all values are in the 0 .. BITS_PER_WORD-1 range, we can get rid of
kono
parents: 67
diff changeset
343 the minval subtractions, but it might make the mask constants more
kono
parents: 67
diff changeset
344 expensive. So, compare the costs. */
kono
parents: 67
diff changeset
345 if (compare_tree_int (minval, 0) > 0
kono
parents: 67
diff changeset
346 && compare_tree_int (maxval, GET_MODE_BITSIZE (word_mode)) < 0)
kono
parents: 67
diff changeset
347 {
kono
parents: 67
diff changeset
348 int cost_diff;
kono
parents: 67
diff changeset
349 HOST_WIDE_INT m = tree_to_uhwi (minval);
kono
parents: 67
diff changeset
350 rtx reg = gen_raw_REG (word_mode, 10000);
kono
parents: 67
diff changeset
351 bool speed_p = optimize_bb_for_speed_p (gimple_bb (swtch));
kono
parents: 67
diff changeset
352 cost_diff = set_rtx_cost (gen_rtx_PLUS (word_mode, reg,
kono
parents: 67
diff changeset
353 GEN_INT (-m)), speed_p);
kono
parents: 67
diff changeset
354 for (i = 0; i < count; i++)
kono
parents: 67
diff changeset
355 {
kono
parents: 67
diff changeset
356 rtx r = immed_wide_int_const (test[i].mask, word_mode);
kono
parents: 67
diff changeset
357 cost_diff += set_src_cost (gen_rtx_AND (word_mode, reg, r),
kono
parents: 67
diff changeset
358 word_mode, speed_p);
kono
parents: 67
diff changeset
359 r = immed_wide_int_const (wi::lshift (test[i].mask, m), word_mode);
kono
parents: 67
diff changeset
360 cost_diff -= set_src_cost (gen_rtx_AND (word_mode, reg, r),
kono
parents: 67
diff changeset
361 word_mode, speed_p);
kono
parents: 67
diff changeset
362 }
kono
parents: 67
diff changeset
363 if (cost_diff > 0)
kono
parents: 67
diff changeset
364 {
kono
parents: 67
diff changeset
365 for (i = 0; i < count; i++)
kono
parents: 67
diff changeset
366 test[i].mask = wi::lshift (test[i].mask, m);
kono
parents: 67
diff changeset
367 minval = build_zero_cst (TREE_TYPE (minval));
kono
parents: 67
diff changeset
368 range = maxval;
kono
parents: 67
diff changeset
369 }
kono
parents: 67
diff changeset
370 }
kono
parents: 67
diff changeset
371
kono
parents: 67
diff changeset
372 /* We generate two jumps to the default case label.
kono
parents: 67
diff changeset
373 Split the default edge, so that we don't have to do any PHI node
kono
parents: 67
diff changeset
374 updating. */
kono
parents: 67
diff changeset
375 new_default_bb = split_edge (default_edge);
kono
parents: 67
diff changeset
376
kono
parents: 67
diff changeset
377 if (update_dom)
kono
parents: 67
diff changeset
378 {
kono
parents: 67
diff changeset
379 bbs_to_fix_dom.create (10);
kono
parents: 67
diff changeset
380 bbs_to_fix_dom.quick_push (switch_bb);
kono
parents: 67
diff changeset
381 bbs_to_fix_dom.quick_push (default_bb);
kono
parents: 67
diff changeset
382 bbs_to_fix_dom.quick_push (new_default_bb);
kono
parents: 67
diff changeset
383 }
kono
parents: 67
diff changeset
384
kono
parents: 67
diff changeset
385 /* Now build the test-and-branch code. */
kono
parents: 67
diff changeset
386
kono
parents: 67
diff changeset
387 gsi = gsi_last_bb (switch_bb);
kono
parents: 67
diff changeset
388
kono
parents: 67
diff changeset
389 /* idx = (unsigned)x - minval. */
kono
parents: 67
diff changeset
390 idx = fold_convert (unsigned_index_type, index_expr);
kono
parents: 67
diff changeset
391 idx = fold_build2 (MINUS_EXPR, unsigned_index_type, idx,
kono
parents: 67
diff changeset
392 fold_convert (unsigned_index_type, minval));
kono
parents: 67
diff changeset
393 idx = force_gimple_operand_gsi (&gsi, idx,
kono
parents: 67
diff changeset
394 /*simple=*/true, NULL_TREE,
kono
parents: 67
diff changeset
395 /*before=*/true, GSI_SAME_STMT);
kono
parents: 67
diff changeset
396
kono
parents: 67
diff changeset
397 /* if (idx > range) goto default */
kono
parents: 67
diff changeset
398 range = force_gimple_operand_gsi (&gsi,
kono
parents: 67
diff changeset
399 fold_convert (unsigned_index_type, range),
kono
parents: 67
diff changeset
400 /*simple=*/true, NULL_TREE,
kono
parents: 67
diff changeset
401 /*before=*/true, GSI_SAME_STMT);
kono
parents: 67
diff changeset
402 tmp = fold_build2 (GT_EXPR, boolean_type_node, idx, range);
kono
parents: 67
diff changeset
403 new_bb = hoist_edge_and_branch_if_true (&gsi, tmp, default_edge, update_dom);
kono
parents: 67
diff changeset
404 if (update_dom)
kono
parents: 67
diff changeset
405 bbs_to_fix_dom.quick_push (new_bb);
kono
parents: 67
diff changeset
406 gcc_assert (gimple_bb (swtch) == new_bb);
kono
parents: 67
diff changeset
407 gsi = gsi_last_bb (new_bb);
kono
parents: 67
diff changeset
408
kono
parents: 67
diff changeset
409 /* Any blocks dominated by the GIMPLE_SWITCH, but that are not successors
kono
parents: 67
diff changeset
410 of NEW_BB, are still immediately dominated by SWITCH_BB. Make it so. */
kono
parents: 67
diff changeset
411 if (update_dom)
kono
parents: 67
diff changeset
412 {
kono
parents: 67
diff changeset
413 vec<basic_block> dom_bbs;
kono
parents: 67
diff changeset
414 basic_block dom_son;
kono
parents: 67
diff changeset
415
kono
parents: 67
diff changeset
416 dom_bbs = get_dominated_by (CDI_DOMINATORS, new_bb);
kono
parents: 67
diff changeset
417 FOR_EACH_VEC_ELT (dom_bbs, i, dom_son)
kono
parents: 67
diff changeset
418 {
kono
parents: 67
diff changeset
419 edge e = find_edge (new_bb, dom_son);
kono
parents: 67
diff changeset
420 if (e && single_pred_p (e->dest))
kono
parents: 67
diff changeset
421 continue;
kono
parents: 67
diff changeset
422 set_immediate_dominator (CDI_DOMINATORS, dom_son, switch_bb);
kono
parents: 67
diff changeset
423 bbs_to_fix_dom.safe_push (dom_son);
kono
parents: 67
diff changeset
424 }
kono
parents: 67
diff changeset
425 dom_bbs.release ();
kono
parents: 67
diff changeset
426 }
kono
parents: 67
diff changeset
427
kono
parents: 67
diff changeset
428 /* csui = (1 << (word_mode) idx) */
kono
parents: 67
diff changeset
429 csui = make_ssa_name (word_type_node);
kono
parents: 67
diff changeset
430 tmp = fold_build2 (LSHIFT_EXPR, word_type_node, word_mode_one,
kono
parents: 67
diff changeset
431 fold_convert (word_type_node, idx));
kono
parents: 67
diff changeset
432 tmp = force_gimple_operand_gsi (&gsi, tmp,
kono
parents: 67
diff changeset
433 /*simple=*/false, NULL_TREE,
kono
parents: 67
diff changeset
434 /*before=*/true, GSI_SAME_STMT);
kono
parents: 67
diff changeset
435 shift_stmt = gimple_build_assign (csui, tmp);
kono
parents: 67
diff changeset
436 gsi_insert_before (&gsi, shift_stmt, GSI_SAME_STMT);
kono
parents: 67
diff changeset
437 update_stmt (shift_stmt);
kono
parents: 67
diff changeset
438
kono
parents: 67
diff changeset
439 /* for each unique set of cases:
kono
parents: 67
diff changeset
440 if (const & csui) goto target */
kono
parents: 67
diff changeset
441 for (k = 0; k < count; k++)
kono
parents: 67
diff changeset
442 {
kono
parents: 67
diff changeset
443 tmp = wide_int_to_tree (word_type_node, test[k].mask);
kono
parents: 67
diff changeset
444 tmp = fold_build2 (BIT_AND_EXPR, word_type_node, csui, tmp);
kono
parents: 67
diff changeset
445 tmp = force_gimple_operand_gsi (&gsi, tmp,
kono
parents: 67
diff changeset
446 /*simple=*/true, NULL_TREE,
kono
parents: 67
diff changeset
447 /*before=*/true, GSI_SAME_STMT);
kono
parents: 67
diff changeset
448 tmp = fold_build2 (NE_EXPR, boolean_type_node, tmp, word_mode_zero);
kono
parents: 67
diff changeset
449 new_bb = hoist_edge_and_branch_if_true (&gsi, tmp, test[k].target_edge,
kono
parents: 67
diff changeset
450 update_dom);
kono
parents: 67
diff changeset
451 if (update_dom)
kono
parents: 67
diff changeset
452 bbs_to_fix_dom.safe_push (new_bb);
kono
parents: 67
diff changeset
453 gcc_assert (gimple_bb (swtch) == new_bb);
kono
parents: 67
diff changeset
454 gsi = gsi_last_bb (new_bb);
kono
parents: 67
diff changeset
455 }
kono
parents: 67
diff changeset
456
kono
parents: 67
diff changeset
457 /* We should have removed all edges now. */
kono
parents: 67
diff changeset
458 gcc_assert (EDGE_COUNT (gsi_bb (gsi)->succs) == 0);
kono
parents: 67
diff changeset
459
kono
parents: 67
diff changeset
460 /* If nothing matched, go to the default label. */
kono
parents: 67
diff changeset
461 make_edge (gsi_bb (gsi), new_default_bb, EDGE_FALLTHRU);
kono
parents: 67
diff changeset
462
kono
parents: 67
diff changeset
463 /* The GIMPLE_SWITCH is now redundant. */
kono
parents: 67
diff changeset
464 gsi_remove (&gsi, true);
kono
parents: 67
diff changeset
465
kono
parents: 67
diff changeset
466 if (update_dom)
kono
parents: 67
diff changeset
467 {
kono
parents: 67
diff changeset
468 /* Fix up the dominator tree. */
kono
parents: 67
diff changeset
469 iterate_fix_dominators (CDI_DOMINATORS, bbs_to_fix_dom, true);
kono
parents: 67
diff changeset
470 bbs_to_fix_dom.release ();
kono
parents: 67
diff changeset
471 }
kono
parents: 67
diff changeset
472 }
kono
parents: 67
diff changeset
473
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
474 /*
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
475 Switch initialization conversion
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
476
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
477 The following pass changes simple initializations of scalars in a switch
111
kono
parents: 67
diff changeset
478 statement into initializations from a static array. Obviously, the values
kono
parents: 67
diff changeset
479 must be constant and known at compile time and a default branch must be
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
480 provided. For example, the following code:
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
481
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
482 int a,b;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
483
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
484 switch (argc)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
485 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
486 case 1:
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
487 case 2:
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
488 a_1 = 8;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
489 b_1 = 6;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
490 break;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
491 case 3:
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
492 a_2 = 9;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
493 b_2 = 5;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
494 break;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
495 case 12:
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
496 a_3 = 10;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
497 b_3 = 4;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
498 break;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
499 default:
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
500 a_4 = 16;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
501 b_4 = 1;
111
kono
parents: 67
diff changeset
502 break;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
503 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
504 a_5 = PHI <a_1, a_2, a_3, a_4>
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
505 b_5 = PHI <b_1, b_2, b_3, b_4>
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
506
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
507
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
508 is changed into:
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
509
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
510 static const int = CSWTCH01[] = {6, 6, 5, 1, 1, 1, 1, 1, 1, 1, 1, 4};
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
511 static const int = CSWTCH02[] = {8, 8, 9, 16, 16, 16, 16, 16, 16, 16,
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
512 16, 16, 10};
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
513
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
514 if (((unsigned) argc) - 1 < 11)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
515 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
516 a_6 = CSWTCH02[argc - 1];
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
517 b_6 = CSWTCH01[argc - 1];
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
518 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
519 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
520 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
521 a_7 = 16;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
522 b_7 = 1;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
523 }
111
kono
parents: 67
diff changeset
524 a_5 = PHI <a_6, a_7>
kono
parents: 67
diff changeset
525 b_b = PHI <b_6, b_7>
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
526
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
527 There are further constraints. Specifically, the range of values across all
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
528 case labels must not be bigger than SWITCH_CONVERSION_BRANCH_RATIO (default
111
kono
parents: 67
diff changeset
529 eight) times the number of the actual switch branches.
kono
parents: 67
diff changeset
530
kono
parents: 67
diff changeset
531 This transformation was contributed by Martin Jambor, see this e-mail:
kono
parents: 67
diff changeset
532 http://gcc.gnu.org/ml/gcc-patches/2008-07/msg00011.html */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
533
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
534 /* The main structure of the pass. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
535 struct switch_conv_info
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
536 {
111
kono
parents: 67
diff changeset
537 /* The expression used to decide the switch branch. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
538 tree index_expr;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
539
111
kono
parents: 67
diff changeset
540 /* The following integer constants store the minimum and maximum value
kono
parents: 67
diff changeset
541 covered by the case labels. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
542 tree range_min;
111
kono
parents: 67
diff changeset
543 tree range_max;
kono
parents: 67
diff changeset
544
kono
parents: 67
diff changeset
545 /* The difference between the above two numbers. Stored here because it
kono
parents: 67
diff changeset
546 is used in all the conversion heuristics, as well as for some of the
kono
parents: 67
diff changeset
547 transformation, and it is expensive to re-compute it all the time. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
548 tree range_size;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
549
111
kono
parents: 67
diff changeset
550 /* Basic block that contains the actual GIMPLE_SWITCH. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
551 basic_block switch_bb;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
552
111
kono
parents: 67
diff changeset
553 /* Basic block that is the target of the default case. */
kono
parents: 67
diff changeset
554 basic_block default_bb;
kono
parents: 67
diff changeset
555
kono
parents: 67
diff changeset
556 /* The single successor block of all branches out of the GIMPLE_SWITCH,
kono
parents: 67
diff changeset
557 if such a block exists. Otherwise NULL. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
558 basic_block final_bb;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
559
111
kono
parents: 67
diff changeset
560 /* The probability of the default edge in the replaced switch. */
kono
parents: 67
diff changeset
561 profile_probability default_prob;
kono
parents: 67
diff changeset
562
kono
parents: 67
diff changeset
563 /* The count of the default edge in the replaced switch. */
kono
parents: 67
diff changeset
564 profile_count default_count;
kono
parents: 67
diff changeset
565
kono
parents: 67
diff changeset
566 /* Combined count of all other (non-default) edges in the replaced switch. */
kono
parents: 67
diff changeset
567 profile_count other_count;
kono
parents: 67
diff changeset
568
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
569 /* Number of phi nodes in the final bb (that we'll be replacing). */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
570 int phi_count;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
571
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
572 /* Array of default values, in the same order as phi nodes. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
573 tree *default_values;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
574
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
575 /* Constructors of new static arrays. */
111
kono
parents: 67
diff changeset
576 vec<constructor_elt, va_gc> **constructors;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
577
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
578 /* Array of ssa names that are initialized with a value from a new static
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
579 array. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
580 tree *target_inbound_names;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
581
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
582 /* Array of ssa names that are initialized with the default value if the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
583 switch expression is out of range. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
584 tree *target_outbound_names;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
585
111
kono
parents: 67
diff changeset
586 /* VOP SSA_NAME. */
kono
parents: 67
diff changeset
587 tree target_vop;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
588
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
589 /* The first load statement that loads a temporary from a new static array.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
590 */
111
kono
parents: 67
diff changeset
591 gimple *arr_ref_first;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
592
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
593 /* The last load statement that loads a temporary from a new static array. */
111
kono
parents: 67
diff changeset
594 gimple *arr_ref_last;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
595
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
596 /* String reason why the case wasn't a good candidate that is written to the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
597 dump file, if there is one. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
598 const char *reason;
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
599
111
kono
parents: 67
diff changeset
600 /* True if default case is not used for any value between range_min and
kono
parents: 67
diff changeset
601 range_max inclusive. */
kono
parents: 67
diff changeset
602 bool contiguous_range;
kono
parents: 67
diff changeset
603
kono
parents: 67
diff changeset
604 /* True if default case does not have the required shape for other case
kono
parents: 67
diff changeset
605 labels. */
kono
parents: 67
diff changeset
606 bool default_case_nonstandard;
kono
parents: 67
diff changeset
607
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
608 /* Parameters for expand_switch_using_bit_tests. Should be computed
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
609 the same way as in expand_case. */
111
kono
parents: 67
diff changeset
610 unsigned int uniq;
kono
parents: 67
diff changeset
611 unsigned int count;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
612 };
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
613
111
kono
parents: 67
diff changeset
614 /* Collect information about GIMPLE_SWITCH statement SWTCH into INFO. */
kono
parents: 67
diff changeset
615
kono
parents: 67
diff changeset
616 static void
kono
parents: 67
diff changeset
617 collect_switch_conv_info (gswitch *swtch, struct switch_conv_info *info)
kono
parents: 67
diff changeset
618 {
kono
parents: 67
diff changeset
619 unsigned int branch_num = gimple_switch_num_labels (swtch);
kono
parents: 67
diff changeset
620 tree min_case, max_case;
kono
parents: 67
diff changeset
621 unsigned int count, i;
kono
parents: 67
diff changeset
622 edge e, e_default, e_first;
kono
parents: 67
diff changeset
623 edge_iterator ei;
kono
parents: 67
diff changeset
624 basic_block first;
kono
parents: 67
diff changeset
625
kono
parents: 67
diff changeset
626 memset (info, 0, sizeof (*info));
kono
parents: 67
diff changeset
627
kono
parents: 67
diff changeset
628 /* The gimplifier has already sorted the cases by CASE_LOW and ensured there
kono
parents: 67
diff changeset
629 is a default label which is the first in the vector.
kono
parents: 67
diff changeset
630 Collect the bits we can deduce from the CFG. */
kono
parents: 67
diff changeset
631 info->index_expr = gimple_switch_index (swtch);
kono
parents: 67
diff changeset
632 info->switch_bb = gimple_bb (swtch);
kono
parents: 67
diff changeset
633 info->default_bb
kono
parents: 67
diff changeset
634 = label_to_block (CASE_LABEL (gimple_switch_default_label (swtch)));
kono
parents: 67
diff changeset
635 e_default = find_edge (info->switch_bb, info->default_bb);
kono
parents: 67
diff changeset
636 info->default_prob = e_default->probability;
kono
parents: 67
diff changeset
637 info->default_count = e_default->count ();
kono
parents: 67
diff changeset
638 FOR_EACH_EDGE (e, ei, info->switch_bb->succs)
kono
parents: 67
diff changeset
639 if (e != e_default)
kono
parents: 67
diff changeset
640 info->other_count += e->count ();
kono
parents: 67
diff changeset
641
kono
parents: 67
diff changeset
642 /* Get upper and lower bounds of case values, and the covered range. */
kono
parents: 67
diff changeset
643 min_case = gimple_switch_label (swtch, 1);
kono
parents: 67
diff changeset
644 max_case = gimple_switch_label (swtch, branch_num - 1);
kono
parents: 67
diff changeset
645
kono
parents: 67
diff changeset
646 info->range_min = CASE_LOW (min_case);
kono
parents: 67
diff changeset
647 if (CASE_HIGH (max_case) != NULL_TREE)
kono
parents: 67
diff changeset
648 info->range_max = CASE_HIGH (max_case);
kono
parents: 67
diff changeset
649 else
kono
parents: 67
diff changeset
650 info->range_max = CASE_LOW (max_case);
kono
parents: 67
diff changeset
651
kono
parents: 67
diff changeset
652 info->contiguous_range = true;
kono
parents: 67
diff changeset
653 tree last = CASE_HIGH (min_case) ? CASE_HIGH (min_case) : info->range_min;
kono
parents: 67
diff changeset
654 for (i = 2; i < branch_num; i++)
kono
parents: 67
diff changeset
655 {
kono
parents: 67
diff changeset
656 tree elt = gimple_switch_label (swtch, i);
kono
parents: 67
diff changeset
657 if (wi::to_wide (last) + 1 != wi::to_wide (CASE_LOW (elt)))
kono
parents: 67
diff changeset
658 {
kono
parents: 67
diff changeset
659 info->contiguous_range = false;
kono
parents: 67
diff changeset
660 break;
kono
parents: 67
diff changeset
661 }
kono
parents: 67
diff changeset
662 last = CASE_HIGH (elt) ? CASE_HIGH (elt) : CASE_LOW (elt);
kono
parents: 67
diff changeset
663 }
kono
parents: 67
diff changeset
664
kono
parents: 67
diff changeset
665 if (info->contiguous_range)
kono
parents: 67
diff changeset
666 {
kono
parents: 67
diff changeset
667 first = label_to_block (CASE_LABEL (gimple_switch_label (swtch, 1)));
kono
parents: 67
diff changeset
668 e_first = find_edge (info->switch_bb, first);
kono
parents: 67
diff changeset
669 }
kono
parents: 67
diff changeset
670 else
kono
parents: 67
diff changeset
671 {
kono
parents: 67
diff changeset
672 first = info->default_bb;
kono
parents: 67
diff changeset
673 e_first = e_default;
kono
parents: 67
diff changeset
674 }
kono
parents: 67
diff changeset
675
kono
parents: 67
diff changeset
676 /* See if there is one common successor block for all branch
kono
parents: 67
diff changeset
677 targets. If it exists, record it in FINAL_BB.
kono
parents: 67
diff changeset
678 Start with the destination of the first non-default case
kono
parents: 67
diff changeset
679 if the range is contiguous and default case otherwise as
kono
parents: 67
diff changeset
680 guess or its destination in case it is a forwarder block. */
kono
parents: 67
diff changeset
681 if (! single_pred_p (e_first->dest))
kono
parents: 67
diff changeset
682 info->final_bb = e_first->dest;
kono
parents: 67
diff changeset
683 else if (single_succ_p (e_first->dest)
kono
parents: 67
diff changeset
684 && ! single_pred_p (single_succ (e_first->dest)))
kono
parents: 67
diff changeset
685 info->final_bb = single_succ (e_first->dest);
kono
parents: 67
diff changeset
686 /* Require that all switch destinations are either that common
kono
parents: 67
diff changeset
687 FINAL_BB or a forwarder to it, except for the default
kono
parents: 67
diff changeset
688 case if contiguous range. */
kono
parents: 67
diff changeset
689 if (info->final_bb)
kono
parents: 67
diff changeset
690 FOR_EACH_EDGE (e, ei, info->switch_bb->succs)
kono
parents: 67
diff changeset
691 {
kono
parents: 67
diff changeset
692 if (e->dest == info->final_bb)
kono
parents: 67
diff changeset
693 continue;
kono
parents: 67
diff changeset
694
kono
parents: 67
diff changeset
695 if (single_pred_p (e->dest)
kono
parents: 67
diff changeset
696 && single_succ_p (e->dest)
kono
parents: 67
diff changeset
697 && single_succ (e->dest) == info->final_bb)
kono
parents: 67
diff changeset
698 continue;
kono
parents: 67
diff changeset
699
kono
parents: 67
diff changeset
700 if (e == e_default && info->contiguous_range)
kono
parents: 67
diff changeset
701 {
kono
parents: 67
diff changeset
702 info->default_case_nonstandard = true;
kono
parents: 67
diff changeset
703 continue;
kono
parents: 67
diff changeset
704 }
kono
parents: 67
diff changeset
705
kono
parents: 67
diff changeset
706 info->final_bb = NULL;
kono
parents: 67
diff changeset
707 break;
kono
parents: 67
diff changeset
708 }
kono
parents: 67
diff changeset
709
kono
parents: 67
diff changeset
710 info->range_size
kono
parents: 67
diff changeset
711 = int_const_binop (MINUS_EXPR, info->range_max, info->range_min);
kono
parents: 67
diff changeset
712
kono
parents: 67
diff changeset
713 /* Get a count of the number of case labels. Single-valued case labels
kono
parents: 67
diff changeset
714 simply count as one, but a case range counts double, since it may
kono
parents: 67
diff changeset
715 require two compares if it gets lowered as a branching tree. */
kono
parents: 67
diff changeset
716 count = 0;
kono
parents: 67
diff changeset
717 for (i = 1; i < branch_num; i++)
kono
parents: 67
diff changeset
718 {
kono
parents: 67
diff changeset
719 tree elt = gimple_switch_label (swtch, i);
kono
parents: 67
diff changeset
720 count++;
kono
parents: 67
diff changeset
721 if (CASE_HIGH (elt)
kono
parents: 67
diff changeset
722 && ! tree_int_cst_equal (CASE_LOW (elt), CASE_HIGH (elt)))
kono
parents: 67
diff changeset
723 count++;
kono
parents: 67
diff changeset
724 }
kono
parents: 67
diff changeset
725 info->count = count;
kono
parents: 67
diff changeset
726
kono
parents: 67
diff changeset
727 /* Get the number of unique non-default targets out of the GIMPLE_SWITCH
kono
parents: 67
diff changeset
728 block. Assume a CFG cleanup would have already removed degenerate
kono
parents: 67
diff changeset
729 switch statements, this allows us to just use EDGE_COUNT. */
kono
parents: 67
diff changeset
730 info->uniq = EDGE_COUNT (gimple_bb (swtch)->succs) - 1;
kono
parents: 67
diff changeset
731 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
732
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
733 /* Checks whether the range given by individual case statements of the SWTCH
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
734 switch statement isn't too big and whether the number of branches actually
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
735 satisfies the size of the new array. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
736
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
737 static bool
111
kono
parents: 67
diff changeset
738 check_range (struct switch_conv_info *info)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
739 {
111
kono
parents: 67
diff changeset
740 gcc_assert (info->range_size);
kono
parents: 67
diff changeset
741 if (!tree_fits_uhwi_p (info->range_size))
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
742 {
111
kono
parents: 67
diff changeset
743 info->reason = "index range way too large or otherwise unusable";
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
744 return false;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
745 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
746
111
kono
parents: 67
diff changeset
747 if (tree_to_uhwi (info->range_size)
kono
parents: 67
diff changeset
748 > ((unsigned) info->count * SWITCH_CONVERSION_BRANCH_RATIO))
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
749 {
111
kono
parents: 67
diff changeset
750 info->reason = "the maximum range-branch ratio exceeded";
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
751 return false;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
752 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
753
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
754 return true;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
755 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
756
111
kono
parents: 67
diff changeset
757 /* Checks whether all but the FINAL_BB basic blocks are empty. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
758
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
759 static bool
111
kono
parents: 67
diff changeset
760 check_all_empty_except_final (struct switch_conv_info *info)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
761 {
111
kono
parents: 67
diff changeset
762 edge e, e_default = find_edge (info->switch_bb, info->default_bb);
kono
parents: 67
diff changeset
763 edge_iterator ei;
kono
parents: 67
diff changeset
764
kono
parents: 67
diff changeset
765 FOR_EACH_EDGE (e, ei, info->switch_bb->succs)
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
766 {
111
kono
parents: 67
diff changeset
767 if (e->dest == info->final_bb)
kono
parents: 67
diff changeset
768 continue;
kono
parents: 67
diff changeset
769
kono
parents: 67
diff changeset
770 if (!empty_block_p (e->dest))
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
771 {
111
kono
parents: 67
diff changeset
772 if (info->contiguous_range && e == e_default)
kono
parents: 67
diff changeset
773 {
kono
parents: 67
diff changeset
774 info->default_case_nonstandard = true;
kono
parents: 67
diff changeset
775 continue;
kono
parents: 67
diff changeset
776 }
kono
parents: 67
diff changeset
777
kono
parents: 67
diff changeset
778 info->reason = "bad case - a non-final BB not empty";
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
779 return false;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
780 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
781 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
782
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
783 return true;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
784 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
785
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
786 /* This function checks whether all required values in phi nodes in final_bb
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
787 are constants. Required values are those that correspond to a basic block
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
788 which is a part of the examined switch statement. It returns true if the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
789 phi nodes are OK, otherwise false. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
790
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
791 static bool
111
kono
parents: 67
diff changeset
792 check_final_bb (gswitch *swtch, struct switch_conv_info *info)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
793 {
111
kono
parents: 67
diff changeset
794 gphi_iterator gsi;
kono
parents: 67
diff changeset
795
kono
parents: 67
diff changeset
796 info->phi_count = 0;
kono
parents: 67
diff changeset
797 for (gsi = gsi_start_phis (info->final_bb); !gsi_end_p (gsi); gsi_next (&gsi))
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
798 {
111
kono
parents: 67
diff changeset
799 gphi *phi = gsi.phi ();
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
800 unsigned int i;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
801
111
kono
parents: 67
diff changeset
802 if (virtual_operand_p (gimple_phi_result (phi)))
kono
parents: 67
diff changeset
803 continue;
kono
parents: 67
diff changeset
804
kono
parents: 67
diff changeset
805 info->phi_count++;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
806
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
807 for (i = 0; i < gimple_phi_num_args (phi); i++)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
808 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
809 basic_block bb = gimple_phi_arg_edge (phi, i)->src;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
810
111
kono
parents: 67
diff changeset
811 if (bb == info->switch_bb
kono
parents: 67
diff changeset
812 || (single_pred_p (bb)
kono
parents: 67
diff changeset
813 && single_pred (bb) == info->switch_bb
kono
parents: 67
diff changeset
814 && (!info->default_case_nonstandard
kono
parents: 67
diff changeset
815 || empty_block_p (bb))))
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
816 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
817 tree reloc, val;
111
kono
parents: 67
diff changeset
818 const char *reason = NULL;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
819
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
820 val = gimple_phi_arg_def (phi, i);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
821 if (!is_gimple_ip_invariant (val))
111
kono
parents: 67
diff changeset
822 reason = "non-invariant value from a case";
kono
parents: 67
diff changeset
823 else
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
824 {
111
kono
parents: 67
diff changeset
825 reloc = initializer_constant_valid_p (val, TREE_TYPE (val));
kono
parents: 67
diff changeset
826 if ((flag_pic && reloc != null_pointer_node)
kono
parents: 67
diff changeset
827 || (!flag_pic && reloc == NULL_TREE))
kono
parents: 67
diff changeset
828 {
kono
parents: 67
diff changeset
829 if (reloc)
kono
parents: 67
diff changeset
830 reason
kono
parents: 67
diff changeset
831 = "value from a case would need runtime relocations";
kono
parents: 67
diff changeset
832 else
kono
parents: 67
diff changeset
833 reason
kono
parents: 67
diff changeset
834 = "value from a case is not a valid initializer";
kono
parents: 67
diff changeset
835 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
836 }
111
kono
parents: 67
diff changeset
837 if (reason)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
838 {
111
kono
parents: 67
diff changeset
839 /* For contiguous range, we can allow non-constant
kono
parents: 67
diff changeset
840 or one that needs relocation, as long as it is
kono
parents: 67
diff changeset
841 only reachable from the default case. */
kono
parents: 67
diff changeset
842 if (bb == info->switch_bb)
kono
parents: 67
diff changeset
843 bb = info->final_bb;
kono
parents: 67
diff changeset
844 if (!info->contiguous_range || bb != info->default_bb)
kono
parents: 67
diff changeset
845 {
kono
parents: 67
diff changeset
846 info->reason = reason;
kono
parents: 67
diff changeset
847 return false;
kono
parents: 67
diff changeset
848 }
kono
parents: 67
diff changeset
849
kono
parents: 67
diff changeset
850 unsigned int branch_num = gimple_switch_num_labels (swtch);
kono
parents: 67
diff changeset
851 for (unsigned int i = 1; i < branch_num; i++)
kono
parents: 67
diff changeset
852 {
kono
parents: 67
diff changeset
853 tree lab = CASE_LABEL (gimple_switch_label (swtch, i));
kono
parents: 67
diff changeset
854 if (label_to_block (lab) == bb)
kono
parents: 67
diff changeset
855 {
kono
parents: 67
diff changeset
856 info->reason = reason;
kono
parents: 67
diff changeset
857 return false;
kono
parents: 67
diff changeset
858 }
kono
parents: 67
diff changeset
859 }
kono
parents: 67
diff changeset
860 info->default_case_nonstandard = true;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
861 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
862 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
863 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
864 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
865
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
866 return true;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
867 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
868
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
869 /* The following function allocates default_values, target_{in,out}_names and
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
870 constructors arrays. The last one is also populated with pointers to
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
871 vectors that will become constructors of new arrays. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
872
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
873 static void
111
kono
parents: 67
diff changeset
874 create_temp_arrays (struct switch_conv_info *info)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
875 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
876 int i;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
877
111
kono
parents: 67
diff changeset
878 info->default_values = XCNEWVEC (tree, info->phi_count * 3);
kono
parents: 67
diff changeset
879 /* ??? Macros do not support multi argument templates in their
kono
parents: 67
diff changeset
880 argument list. We create a typedef to work around that problem. */
kono
parents: 67
diff changeset
881 typedef vec<constructor_elt, va_gc> *vec_constructor_elt_gc;
kono
parents: 67
diff changeset
882 info->constructors = XCNEWVEC (vec_constructor_elt_gc, info->phi_count);
kono
parents: 67
diff changeset
883 info->target_inbound_names = info->default_values + info->phi_count;
kono
parents: 67
diff changeset
884 info->target_outbound_names = info->target_inbound_names + info->phi_count;
kono
parents: 67
diff changeset
885 for (i = 0; i < info->phi_count; i++)
kono
parents: 67
diff changeset
886 vec_alloc (info->constructors[i], tree_to_uhwi (info->range_size) + 1);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
887 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
888
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
889 /* Free the arrays created by create_temp_arrays(). The vectors that are
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
890 created by that function are not freed here, however, because they have
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
891 already become constructors and must be preserved. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
892
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
893 static void
111
kono
parents: 67
diff changeset
894 free_temp_arrays (struct switch_conv_info *info)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
895 {
111
kono
parents: 67
diff changeset
896 XDELETEVEC (info->constructors);
kono
parents: 67
diff changeset
897 XDELETEVEC (info->default_values);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
898 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
899
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
900 /* Populate the array of default values in the order of phi nodes.
111
kono
parents: 67
diff changeset
901 DEFAULT_CASE is the CASE_LABEL_EXPR for the default switch branch
kono
parents: 67
diff changeset
902 if the range is non-contiguous or the default case has standard
kono
parents: 67
diff changeset
903 structure, otherwise it is the first non-default case instead. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
904
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
905 static void
111
kono
parents: 67
diff changeset
906 gather_default_values (tree default_case, struct switch_conv_info *info)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
907 {
111
kono
parents: 67
diff changeset
908 gphi_iterator gsi;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
909 basic_block bb = label_to_block (CASE_LABEL (default_case));
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
910 edge e;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
911 int i = 0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
912
111
kono
parents: 67
diff changeset
913 gcc_assert (CASE_LOW (default_case) == NULL_TREE
kono
parents: 67
diff changeset
914 || info->default_case_nonstandard);
kono
parents: 67
diff changeset
915
kono
parents: 67
diff changeset
916 if (bb == info->final_bb)
kono
parents: 67
diff changeset
917 e = find_edge (info->switch_bb, bb);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
918 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
919 e = single_succ_edge (bb);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
920
111
kono
parents: 67
diff changeset
921 for (gsi = gsi_start_phis (info->final_bb); !gsi_end_p (gsi); gsi_next (&gsi))
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
922 {
111
kono
parents: 67
diff changeset
923 gphi *phi = gsi.phi ();
kono
parents: 67
diff changeset
924 if (virtual_operand_p (gimple_phi_result (phi)))
kono
parents: 67
diff changeset
925 continue;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
926 tree val = PHI_ARG_DEF_FROM_EDGE (phi, e);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
927 gcc_assert (val);
111
kono
parents: 67
diff changeset
928 info->default_values[i++] = val;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
929 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
930 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
931
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
932 /* The following function populates the vectors in the constructors array with
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
933 future contents of the static arrays. The vectors are populated in the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
934 order of phi nodes. SWTCH is the switch statement being converted. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
935
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
936 static void
111
kono
parents: 67
diff changeset
937 build_constructors (gswitch *swtch, struct switch_conv_info *info)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
938 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
939 unsigned i, branch_num = gimple_switch_num_labels (swtch);
111
kono
parents: 67
diff changeset
940 tree pos = info->range_min;
kono
parents: 67
diff changeset
941 tree pos_one = build_int_cst (TREE_TYPE (pos), 1);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
942
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
943 for (i = 1; i < branch_num; i++)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
944 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
945 tree cs = gimple_switch_label (swtch, i);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
946 basic_block bb = label_to_block (CASE_LABEL (cs));
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
947 edge e;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
948 tree high;
111
kono
parents: 67
diff changeset
949 gphi_iterator gsi;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
950 int j;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
951
111
kono
parents: 67
diff changeset
952 if (bb == info->final_bb)
kono
parents: 67
diff changeset
953 e = find_edge (info->switch_bb, bb);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
954 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
955 e = single_succ_edge (bb);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
956 gcc_assert (e);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
957
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
958 while (tree_int_cst_lt (pos, CASE_LOW (cs)))
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
959 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
960 int k;
111
kono
parents: 67
diff changeset
961 gcc_assert (!info->contiguous_range);
kono
parents: 67
diff changeset
962 for (k = 0; k < info->phi_count; k++)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
963 {
111
kono
parents: 67
diff changeset
964 constructor_elt elt;
kono
parents: 67
diff changeset
965
kono
parents: 67
diff changeset
966 elt.index = int_const_binop (MINUS_EXPR, pos, info->range_min);
kono
parents: 67
diff changeset
967 elt.value
kono
parents: 67
diff changeset
968 = unshare_expr_without_location (info->default_values[k]);
kono
parents: 67
diff changeset
969 info->constructors[k]->quick_push (elt);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
970 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
971
111
kono
parents: 67
diff changeset
972 pos = int_const_binop (PLUS_EXPR, pos, pos_one);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
973 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
974 gcc_assert (tree_int_cst_equal (pos, CASE_LOW (cs)));
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
975
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
976 j = 0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
977 if (CASE_HIGH (cs))
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
978 high = CASE_HIGH (cs);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
979 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
980 high = CASE_LOW (cs);
111
kono
parents: 67
diff changeset
981 for (gsi = gsi_start_phis (info->final_bb);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
982 !gsi_end_p (gsi); gsi_next (&gsi))
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
983 {
111
kono
parents: 67
diff changeset
984 gphi *phi = gsi.phi ();
kono
parents: 67
diff changeset
985 if (virtual_operand_p (gimple_phi_result (phi)))
kono
parents: 67
diff changeset
986 continue;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
987 tree val = PHI_ARG_DEF_FROM_EDGE (phi, e);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
988 tree low = CASE_LOW (cs);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
989 pos = CASE_LOW (cs);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
990
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
991 do
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
992 {
111
kono
parents: 67
diff changeset
993 constructor_elt elt;
kono
parents: 67
diff changeset
994
kono
parents: 67
diff changeset
995 elt.index = int_const_binop (MINUS_EXPR, pos, info->range_min);
kono
parents: 67
diff changeset
996 elt.value = unshare_expr_without_location (val);
kono
parents: 67
diff changeset
997 info->constructors[j]->quick_push (elt);
kono
parents: 67
diff changeset
998
kono
parents: 67
diff changeset
999 pos = int_const_binop (PLUS_EXPR, pos, pos_one);
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1000 } while (!tree_int_cst_lt (high, pos)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1001 && tree_int_cst_lt (low, pos));
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1002 j++;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1003 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1004 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1005 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1006
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1007 /* If all values in the constructor vector are the same, return the value.
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1008 Otherwise return NULL_TREE. Not supposed to be called for empty
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1009 vectors. */
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1010
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1011 static tree
111
kono
parents: 67
diff changeset
1012 constructor_contains_same_values_p (vec<constructor_elt, va_gc> *vec)
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1013 {
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1014 unsigned int i;
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1015 tree prev = NULL_TREE;
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1016 constructor_elt *elt;
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1017
111
kono
parents: 67
diff changeset
1018 FOR_EACH_VEC_SAFE_ELT (vec, i, elt)
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1019 {
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1020 if (!prev)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1021 prev = elt->value;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1022 else if (!operand_equal_p (elt->value, prev, OEP_ONLY_CONST))
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1023 return NULL_TREE;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1024 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1025 return prev;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1026 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1027
111
kono
parents: 67
diff changeset
1028 /* Return type which should be used for array elements, either TYPE's
kono
parents: 67
diff changeset
1029 main variant or, for integral types, some smaller integral type
kono
parents: 67
diff changeset
1030 that can still hold all the constants. */
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1031
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1032 static tree
111
kono
parents: 67
diff changeset
1033 array_value_type (gswitch *swtch, tree type, int num,
kono
parents: 67
diff changeset
1034 struct switch_conv_info *info)
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1035 {
111
kono
parents: 67
diff changeset
1036 unsigned int i, len = vec_safe_length (info->constructors[num]);
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1037 constructor_elt *elt;
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1038 int sign = 0;
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1039 tree smaller_type;
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1040
111
kono
parents: 67
diff changeset
1041 /* Types with alignments greater than their size can reach here, e.g. out of
kono
parents: 67
diff changeset
1042 SRA. We couldn't use these as an array component type so get back to the
kono
parents: 67
diff changeset
1043 main variant first, which, for our purposes, is fine for other types as
kono
parents: 67
diff changeset
1044 well. */
kono
parents: 67
diff changeset
1045
kono
parents: 67
diff changeset
1046 type = TYPE_MAIN_VARIANT (type);
kono
parents: 67
diff changeset
1047
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1048 if (!INTEGRAL_TYPE_P (type))
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1049 return type;
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1050
111
kono
parents: 67
diff changeset
1051 scalar_int_mode type_mode = SCALAR_INT_TYPE_MODE (type);
kono
parents: 67
diff changeset
1052 scalar_int_mode mode = get_narrowest_mode (type_mode);
kono
parents: 67
diff changeset
1053 if (GET_MODE_SIZE (type_mode) <= GET_MODE_SIZE (mode))
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1054 return type;
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1055
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1056 if (len < (optimize_bb_for_size_p (gimple_bb (swtch)) ? 2 : 32))
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1057 return type;
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1058
111
kono
parents: 67
diff changeset
1059 FOR_EACH_VEC_SAFE_ELT (info->constructors[num], i, elt)
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1060 {
111
kono
parents: 67
diff changeset
1061 wide_int cst;
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1062
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1063 if (TREE_CODE (elt->value) != INTEGER_CST)
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1064 return type;
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1065
111
kono
parents: 67
diff changeset
1066 cst = wi::to_wide (elt->value);
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1067 while (1)
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1068 {
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1069 unsigned int prec = GET_MODE_BITSIZE (mode);
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1070 if (prec > HOST_BITS_PER_WIDE_INT)
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1071 return type;
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1072
111
kono
parents: 67
diff changeset
1073 if (sign >= 0 && cst == wi::zext (cst, prec))
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1074 {
111
kono
parents: 67
diff changeset
1075 if (sign == 0 && cst == wi::sext (cst, prec))
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1076 break;
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1077 sign = 1;
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1078 break;
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1079 }
111
kono
parents: 67
diff changeset
1080 if (sign <= 0 && cst == wi::sext (cst, prec))
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1081 {
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1082 sign = -1;
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1083 break;
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1084 }
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1085
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1086 if (sign == 1)
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1087 sign = 0;
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1088
111
kono
parents: 67
diff changeset
1089 if (!GET_MODE_WIDER_MODE (mode).exists (&mode)
kono
parents: 67
diff changeset
1090 || GET_MODE_SIZE (mode) >= GET_MODE_SIZE (type_mode))
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1091 return type;
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1092 }
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1093 }
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1094
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1095 if (sign == 0)
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1096 sign = TYPE_UNSIGNED (type) ? 1 : -1;
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1097 smaller_type = lang_hooks.types.type_for_mode (mode, sign >= 0);
111
kono
parents: 67
diff changeset
1098 if (GET_MODE_SIZE (type_mode)
kono
parents: 67
diff changeset
1099 <= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (smaller_type)))
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1100 return type;
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1101
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1102 return smaller_type;
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1103 }
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1104
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1105 /* Create an appropriate array type and declaration and assemble a static array
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1106 variable. Also create a load statement that initializes the variable in
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1107 question with a value from the static array. SWTCH is the switch statement
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1108 being converted, NUM is the index to arrays of constructors, default values
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1109 and target SSA names for this particular array. ARR_INDEX_TYPE is the type
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1110 of the index of the new array, PHI is the phi node of the final BB that
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1111 corresponds to the value that will be loaded from the created array. TIDX
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1112 is an ssa name of a temporary variable holding the index for loads from the
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1113 new array. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1114
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1115 static void
111
kono
parents: 67
diff changeset
1116 build_one_array (gswitch *swtch, int num, tree arr_index_type,
kono
parents: 67
diff changeset
1117 gphi *phi, tree tidx, struct switch_conv_info *info)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1118 {
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1119 tree name, cst;
111
kono
parents: 67
diff changeset
1120 gimple *load;
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1121 gimple_stmt_iterator gsi = gsi_for_stmt (swtch);
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1122 location_t loc = gimple_location (swtch);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1123
111
kono
parents: 67
diff changeset
1124 gcc_assert (info->default_values[num]);
kono
parents: 67
diff changeset
1125
kono
parents: 67
diff changeset
1126 name = copy_ssa_name (PHI_RESULT (phi));
kono
parents: 67
diff changeset
1127 info->target_inbound_names[num] = name;
kono
parents: 67
diff changeset
1128
kono
parents: 67
diff changeset
1129 cst = constructor_contains_same_values_p (info->constructors[num]);
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1130 if (cst)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1131 load = gimple_build_assign (name, cst);
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1132 else
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1133 {
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1134 tree array_type, ctor, decl, value_type, fetch, default_type;
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1135
111
kono
parents: 67
diff changeset
1136 default_type = TREE_TYPE (info->default_values[num]);
kono
parents: 67
diff changeset
1137 value_type = array_value_type (swtch, default_type, num, info);
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1138 array_type = build_array_type (value_type, arr_index_type);
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1139 if (default_type != value_type)
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1140 {
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1141 unsigned int i;
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1142 constructor_elt *elt;
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1143
111
kono
parents: 67
diff changeset
1144 FOR_EACH_VEC_SAFE_ELT (info->constructors[num], i, elt)
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1145 elt->value = fold_convert (value_type, elt->value);
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1146 }
111
kono
parents: 67
diff changeset
1147 ctor = build_constructor (array_type, info->constructors[num]);
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1148 TREE_CONSTANT (ctor) = true;
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1149 TREE_STATIC (ctor) = true;
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1150
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1151 decl = build_decl (loc, VAR_DECL, NULL_TREE, array_type);
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1152 TREE_STATIC (decl) = 1;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1153 DECL_INITIAL (decl) = ctor;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1154
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1155 DECL_NAME (decl) = create_tmp_var_name ("CSWTCH");
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1156 DECL_ARTIFICIAL (decl) = 1;
111
kono
parents: 67
diff changeset
1157 DECL_IGNORED_P (decl) = 1;
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1158 TREE_CONSTANT (decl) = 1;
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1159 TREE_READONLY (decl) = 1;
111
kono
parents: 67
diff changeset
1160 DECL_IGNORED_P (decl) = 1;
kono
parents: 67
diff changeset
1161 varpool_node::finalize_decl (decl);
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1162
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1163 fetch = build4 (ARRAY_REF, value_type, decl, tidx, NULL_TREE,
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1164 NULL_TREE);
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1165 if (default_type != value_type)
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1166 {
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1167 fetch = fold_convert (default_type, fetch);
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1168 fetch = force_gimple_operand_gsi (&gsi, fetch, true, NULL_TREE,
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1169 true, GSI_SAME_STMT);
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1170 }
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1171 load = gimple_build_assign (name, fetch);
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1172 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1173
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1174 gsi_insert_before (&gsi, load, GSI_SAME_STMT);
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1175 update_stmt (load);
111
kono
parents: 67
diff changeset
1176 info->arr_ref_last = load;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1177 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1178
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1179 /* Builds and initializes static arrays initialized with values gathered from
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1180 the SWTCH switch statement. Also creates statements that load values from
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1181 them. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1182
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1183 static void
111
kono
parents: 67
diff changeset
1184 build_arrays (gswitch *swtch, struct switch_conv_info *info)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1185 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1186 tree arr_index_type;
111
kono
parents: 67
diff changeset
1187 tree tidx, sub, utype;
kono
parents: 67
diff changeset
1188 gimple *stmt;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1189 gimple_stmt_iterator gsi;
111
kono
parents: 67
diff changeset
1190 gphi_iterator gpi;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1191 int i;
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1192 location_t loc = gimple_location (swtch);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1193
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1194 gsi = gsi_for_stmt (swtch);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1195
111
kono
parents: 67
diff changeset
1196 /* Make sure we do not generate arithmetics in a subrange. */
kono
parents: 67
diff changeset
1197 utype = TREE_TYPE (info->index_expr);
kono
parents: 67
diff changeset
1198 if (TREE_TYPE (utype))
kono
parents: 67
diff changeset
1199 utype = lang_hooks.types.type_for_mode (TYPE_MODE (TREE_TYPE (utype)), 1);
kono
parents: 67
diff changeset
1200 else
kono
parents: 67
diff changeset
1201 utype = lang_hooks.types.type_for_mode (TYPE_MODE (utype), 1);
kono
parents: 67
diff changeset
1202
kono
parents: 67
diff changeset
1203 arr_index_type = build_index_type (info->range_size);
kono
parents: 67
diff changeset
1204 tidx = make_ssa_name (utype);
kono
parents: 67
diff changeset
1205 sub = fold_build2_loc (loc, MINUS_EXPR, utype,
kono
parents: 67
diff changeset
1206 fold_convert_loc (loc, utype, info->index_expr),
kono
parents: 67
diff changeset
1207 fold_convert_loc (loc, utype, info->range_min));
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1208 sub = force_gimple_operand_gsi (&gsi, sub,
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1209 false, NULL, true, GSI_SAME_STMT);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1210 stmt = gimple_build_assign (tidx, sub);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1211
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1212 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1213 update_stmt (stmt);
111
kono
parents: 67
diff changeset
1214 info->arr_ref_first = stmt;
kono
parents: 67
diff changeset
1215
kono
parents: 67
diff changeset
1216 for (gpi = gsi_start_phis (info->final_bb), i = 0;
kono
parents: 67
diff changeset
1217 !gsi_end_p (gpi); gsi_next (&gpi))
kono
parents: 67
diff changeset
1218 {
kono
parents: 67
diff changeset
1219 gphi *phi = gpi.phi ();
kono
parents: 67
diff changeset
1220 if (!virtual_operand_p (gimple_phi_result (phi)))
kono
parents: 67
diff changeset
1221 build_one_array (swtch, i++, arr_index_type, phi, tidx, info);
kono
parents: 67
diff changeset
1222 else
kono
parents: 67
diff changeset
1223 {
kono
parents: 67
diff changeset
1224 edge e;
kono
parents: 67
diff changeset
1225 edge_iterator ei;
kono
parents: 67
diff changeset
1226 FOR_EACH_EDGE (e, ei, info->switch_bb->succs)
kono
parents: 67
diff changeset
1227 {
kono
parents: 67
diff changeset
1228 if (e->dest == info->final_bb)
kono
parents: 67
diff changeset
1229 break;
kono
parents: 67
diff changeset
1230 if (!info->default_case_nonstandard
kono
parents: 67
diff changeset
1231 || e->dest != info->default_bb)
kono
parents: 67
diff changeset
1232 {
kono
parents: 67
diff changeset
1233 e = single_succ_edge (e->dest);
kono
parents: 67
diff changeset
1234 break;
kono
parents: 67
diff changeset
1235 }
kono
parents: 67
diff changeset
1236 }
kono
parents: 67
diff changeset
1237 gcc_assert (e && e->dest == info->final_bb);
kono
parents: 67
diff changeset
1238 info->target_vop = PHI_ARG_DEF_FROM_EDGE (phi, e);
kono
parents: 67
diff changeset
1239 }
kono
parents: 67
diff changeset
1240 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1241 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1242
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1243 /* Generates and appropriately inserts loads of default values at the position
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1244 given by BSI. Returns the last inserted statement. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1245
111
kono
parents: 67
diff changeset
1246 static gassign *
kono
parents: 67
diff changeset
1247 gen_def_assigns (gimple_stmt_iterator *gsi, struct switch_conv_info *info)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1248 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1249 int i;
111
kono
parents: 67
diff changeset
1250 gassign *assign = NULL;
kono
parents: 67
diff changeset
1251
kono
parents: 67
diff changeset
1252 for (i = 0; i < info->phi_count; i++)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1253 {
111
kono
parents: 67
diff changeset
1254 tree name = copy_ssa_name (info->target_inbound_names[i]);
kono
parents: 67
diff changeset
1255 info->target_outbound_names[i] = name;
kono
parents: 67
diff changeset
1256 assign = gimple_build_assign (name, info->default_values[i]);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1257 gsi_insert_before (gsi, assign, GSI_SAME_STMT);
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1258 update_stmt (assign);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1259 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1260 return assign;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1261 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1262
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1263 /* Deletes the unused bbs and edges that now contain the switch statement and
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1264 its empty branch bbs. BBD is the now dead BB containing the original switch
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1265 statement, FINAL is the last BB of the converted switch statement (in terms
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1266 of succession). */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1267
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1268 static void
111
kono
parents: 67
diff changeset
1269 prune_bbs (basic_block bbd, basic_block final, basic_block default_bb)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1270 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1271 edge_iterator ei;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1272 edge e;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1273
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1274 for (ei = ei_start (bbd->succs); (e = ei_safe_edge (ei)); )
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1275 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1276 basic_block bb;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1277 bb = e->dest;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1278 remove_edge (e);
111
kono
parents: 67
diff changeset
1279 if (bb != final && bb != default_bb)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1280 delete_basic_block (bb);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1281 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1282 delete_basic_block (bbd);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1283 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1284
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1285 /* Add values to phi nodes in final_bb for the two new edges. E1F is the edge
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1286 from the basic block loading values from an array and E2F from the basic
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1287 block loading default values. BBF is the last switch basic block (see the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1288 bbf description in the comment below). */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1289
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1290 static void
111
kono
parents: 67
diff changeset
1291 fix_phi_nodes (edge e1f, edge e2f, basic_block bbf,
kono
parents: 67
diff changeset
1292 struct switch_conv_info *info)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1293 {
111
kono
parents: 67
diff changeset
1294 gphi_iterator gsi;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1295 int i;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1296
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1297 for (gsi = gsi_start_phis (bbf), i = 0;
111
kono
parents: 67
diff changeset
1298 !gsi_end_p (gsi); gsi_next (&gsi))
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1299 {
111
kono
parents: 67
diff changeset
1300 gphi *phi = gsi.phi ();
kono
parents: 67
diff changeset
1301 tree inbound, outbound;
kono
parents: 67
diff changeset
1302 if (virtual_operand_p (gimple_phi_result (phi)))
kono
parents: 67
diff changeset
1303 inbound = outbound = info->target_vop;
kono
parents: 67
diff changeset
1304 else
kono
parents: 67
diff changeset
1305 {
kono
parents: 67
diff changeset
1306 inbound = info->target_inbound_names[i];
kono
parents: 67
diff changeset
1307 outbound = info->target_outbound_names[i++];
kono
parents: 67
diff changeset
1308 }
kono
parents: 67
diff changeset
1309 add_phi_arg (phi, inbound, e1f, UNKNOWN_LOCATION);
kono
parents: 67
diff changeset
1310 if (!info->default_case_nonstandard)
kono
parents: 67
diff changeset
1311 add_phi_arg (phi, outbound, e2f, UNKNOWN_LOCATION);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1312 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1313 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1314
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1315 /* Creates a check whether the switch expression value actually falls into the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1316 range given by all the cases. If it does not, the temporaries are loaded
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1317 with default values instead. SWTCH is the switch statement being converted.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1318
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1319 bb0 is the bb with the switch statement, however, we'll end it with a
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1320 condition instead.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1321
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1322 bb1 is the bb to be used when the range check went ok. It is derived from
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1323 the switch BB
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1324
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1325 bb2 is the bb taken when the expression evaluated outside of the range
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1326 covered by the created arrays. It is populated by loads of default
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1327 values.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1328
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1329 bbF is a fall through for both bb1 and bb2 and contains exactly what
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1330 originally followed the switch statement.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1331
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1332 bbD contains the switch statement (in the end). It is unreachable but we
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1333 still need to strip off its edges.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1334 */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1335
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1336 static void
111
kono
parents: 67
diff changeset
1337 gen_inbound_check (gswitch *swtch, struct switch_conv_info *info)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1338 {
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1339 tree label_decl1 = create_artificial_label (UNKNOWN_LOCATION);
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1340 tree label_decl2 = create_artificial_label (UNKNOWN_LOCATION);
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1341 tree label_decl3 = create_artificial_label (UNKNOWN_LOCATION);
111
kono
parents: 67
diff changeset
1342 glabel *label1, *label2, *label3;
kono
parents: 67
diff changeset
1343 tree utype, tidx;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1344 tree bound;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1345
111
kono
parents: 67
diff changeset
1346 gcond *cond_stmt;
kono
parents: 67
diff changeset
1347
kono
parents: 67
diff changeset
1348 gassign *last_assign = NULL;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1349 gimple_stmt_iterator gsi;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1350 basic_block bb0, bb1, bb2, bbf, bbd;
111
kono
parents: 67
diff changeset
1351 edge e01 = NULL, e02, e21, e1d, e1f, e2f;
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1352 location_t loc = gimple_location (swtch);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1353
111
kono
parents: 67
diff changeset
1354 gcc_assert (info->default_values);
kono
parents: 67
diff changeset
1355
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1356 bb0 = gimple_bb (swtch);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1357
111
kono
parents: 67
diff changeset
1358 tidx = gimple_assign_lhs (info->arr_ref_first);
kono
parents: 67
diff changeset
1359 utype = TREE_TYPE (tidx);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1360
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1361 /* (end of) block 0 */
111
kono
parents: 67
diff changeset
1362 gsi = gsi_for_stmt (info->arr_ref_first);
kono
parents: 67
diff changeset
1363 gsi_next (&gsi);
kono
parents: 67
diff changeset
1364
kono
parents: 67
diff changeset
1365 bound = fold_convert_loc (loc, utype, info->range_size);
kono
parents: 67
diff changeset
1366 cond_stmt = gimple_build_cond (LE_EXPR, tidx, bound, NULL_TREE, NULL_TREE);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1367 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1368 update_stmt (cond_stmt);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1369
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1370 /* block 2 */
111
kono
parents: 67
diff changeset
1371 if (!info->default_case_nonstandard)
kono
parents: 67
diff changeset
1372 {
kono
parents: 67
diff changeset
1373 label2 = gimple_build_label (label_decl2);
kono
parents: 67
diff changeset
1374 gsi_insert_before (&gsi, label2, GSI_SAME_STMT);
kono
parents: 67
diff changeset
1375 last_assign = gen_def_assigns (&gsi, info);
kono
parents: 67
diff changeset
1376 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1377
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1378 /* block 1 */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1379 label1 = gimple_build_label (label_decl1);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1380 gsi_insert_before (&gsi, label1, GSI_SAME_STMT);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1381
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1382 /* block F */
111
kono
parents: 67
diff changeset
1383 gsi = gsi_start_bb (info->final_bb);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1384 label3 = gimple_build_label (label_decl3);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1385 gsi_insert_before (&gsi, label3, GSI_SAME_STMT);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1386
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1387 /* cfg fix */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1388 e02 = split_block (bb0, cond_stmt);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1389 bb2 = e02->dest;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1390
111
kono
parents: 67
diff changeset
1391 if (info->default_case_nonstandard)
kono
parents: 67
diff changeset
1392 {
kono
parents: 67
diff changeset
1393 bb1 = bb2;
kono
parents: 67
diff changeset
1394 bb2 = info->default_bb;
kono
parents: 67
diff changeset
1395 e01 = e02;
kono
parents: 67
diff changeset
1396 e01->flags = EDGE_TRUE_VALUE;
kono
parents: 67
diff changeset
1397 e02 = make_edge (bb0, bb2, EDGE_FALSE_VALUE);
kono
parents: 67
diff changeset
1398 edge e_default = find_edge (bb1, bb2);
kono
parents: 67
diff changeset
1399 for (gphi_iterator gsi = gsi_start_phis (bb2);
kono
parents: 67
diff changeset
1400 !gsi_end_p (gsi); gsi_next (&gsi))
kono
parents: 67
diff changeset
1401 {
kono
parents: 67
diff changeset
1402 gphi *phi = gsi.phi ();
kono
parents: 67
diff changeset
1403 tree arg = PHI_ARG_DEF_FROM_EDGE (phi, e_default);
kono
parents: 67
diff changeset
1404 add_phi_arg (phi, arg, e02,
kono
parents: 67
diff changeset
1405 gimple_phi_arg_location_from_edge (phi, e_default));
kono
parents: 67
diff changeset
1406 }
kono
parents: 67
diff changeset
1407 /* Partially fix the dominator tree, if it is available. */
kono
parents: 67
diff changeset
1408 if (dom_info_available_p (CDI_DOMINATORS))
kono
parents: 67
diff changeset
1409 redirect_immediate_dominators (CDI_DOMINATORS, bb1, bb0);
kono
parents: 67
diff changeset
1410 }
kono
parents: 67
diff changeset
1411 else
kono
parents: 67
diff changeset
1412 {
kono
parents: 67
diff changeset
1413 e21 = split_block (bb2, last_assign);
kono
parents: 67
diff changeset
1414 bb1 = e21->dest;
kono
parents: 67
diff changeset
1415 remove_edge (e21);
kono
parents: 67
diff changeset
1416 }
kono
parents: 67
diff changeset
1417
kono
parents: 67
diff changeset
1418 e1d = split_block (bb1, info->arr_ref_last);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1419 bbd = e1d->dest;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1420 remove_edge (e1d);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1421
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1422 /* flags and profiles of the edge for in-range values */
111
kono
parents: 67
diff changeset
1423 if (!info->default_case_nonstandard)
kono
parents: 67
diff changeset
1424 e01 = make_edge (bb0, bb1, EDGE_TRUE_VALUE);
kono
parents: 67
diff changeset
1425 e01->probability = info->default_prob.invert ();
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1426
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1427 /* flags and profiles of the edge taking care of out-of-range values */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1428 e02->flags &= ~EDGE_FALLTHRU;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1429 e02->flags |= EDGE_FALSE_VALUE;
111
kono
parents: 67
diff changeset
1430 e02->probability = info->default_prob;
kono
parents: 67
diff changeset
1431
kono
parents: 67
diff changeset
1432 bbf = info->final_bb;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1433
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1434 e1f = make_edge (bb1, bbf, EDGE_FALLTHRU);
111
kono
parents: 67
diff changeset
1435 e1f->probability = profile_probability::always ();
kono
parents: 67
diff changeset
1436
kono
parents: 67
diff changeset
1437 if (info->default_case_nonstandard)
kono
parents: 67
diff changeset
1438 e2f = NULL;
kono
parents: 67
diff changeset
1439 else
kono
parents: 67
diff changeset
1440 {
kono
parents: 67
diff changeset
1441 e2f = make_edge (bb2, bbf, EDGE_FALLTHRU);
kono
parents: 67
diff changeset
1442 e2f->probability = profile_probability::always ();
kono
parents: 67
diff changeset
1443 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1444
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1445 /* frequencies of the new BBs */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1446 bb1->frequency = EDGE_FREQUENCY (e01);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1447 bb2->frequency = EDGE_FREQUENCY (e02);
111
kono
parents: 67
diff changeset
1448 if (!info->default_case_nonstandard)
kono
parents: 67
diff changeset
1449 bbf->frequency = EDGE_FREQUENCY (e1f) + EDGE_FREQUENCY (e2f);
kono
parents: 67
diff changeset
1450
kono
parents: 67
diff changeset
1451 /* Tidy blocks that have become unreachable. */
kono
parents: 67
diff changeset
1452 prune_bbs (bbd, info->final_bb,
kono
parents: 67
diff changeset
1453 info->default_case_nonstandard ? info->default_bb : NULL);
kono
parents: 67
diff changeset
1454
kono
parents: 67
diff changeset
1455 /* Fixup the PHI nodes in bbF. */
kono
parents: 67
diff changeset
1456 fix_phi_nodes (e1f, e2f, bbf, info);
kono
parents: 67
diff changeset
1457
kono
parents: 67
diff changeset
1458 /* Fix the dominator tree, if it is available. */
kono
parents: 67
diff changeset
1459 if (dom_info_available_p (CDI_DOMINATORS))
kono
parents: 67
diff changeset
1460 {
kono
parents: 67
diff changeset
1461 vec<basic_block> bbs_to_fix_dom;
kono
parents: 67
diff changeset
1462
kono
parents: 67
diff changeset
1463 set_immediate_dominator (CDI_DOMINATORS, bb1, bb0);
kono
parents: 67
diff changeset
1464 if (!info->default_case_nonstandard)
kono
parents: 67
diff changeset
1465 set_immediate_dominator (CDI_DOMINATORS, bb2, bb0);
kono
parents: 67
diff changeset
1466 if (! get_immediate_dominator (CDI_DOMINATORS, bbf))
kono
parents: 67
diff changeset
1467 /* If bbD was the immediate dominator ... */
kono
parents: 67
diff changeset
1468 set_immediate_dominator (CDI_DOMINATORS, bbf, bb0);
kono
parents: 67
diff changeset
1469
kono
parents: 67
diff changeset
1470 bbs_to_fix_dom.create (3 + (bb2 != bbf));
kono
parents: 67
diff changeset
1471 bbs_to_fix_dom.quick_push (bb0);
kono
parents: 67
diff changeset
1472 bbs_to_fix_dom.quick_push (bb1);
kono
parents: 67
diff changeset
1473 if (bb2 != bbf)
kono
parents: 67
diff changeset
1474 bbs_to_fix_dom.quick_push (bb2);
kono
parents: 67
diff changeset
1475 bbs_to_fix_dom.quick_push (bbf);
kono
parents: 67
diff changeset
1476
kono
parents: 67
diff changeset
1477 iterate_fix_dominators (CDI_DOMINATORS, bbs_to_fix_dom, true);
kono
parents: 67
diff changeset
1478 bbs_to_fix_dom.release ();
kono
parents: 67
diff changeset
1479 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1480 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1481
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1482 /* The following function is invoked on every switch statement (the current one
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1483 is given in SWTCH) and runs the individual phases of switch conversion on it
111
kono
parents: 67
diff changeset
1484 one after another until one fails or the conversion is completed.
kono
parents: 67
diff changeset
1485 Returns NULL on success, or a pointer to a string with the reason why the
kono
parents: 67
diff changeset
1486 conversion failed. */
kono
parents: 67
diff changeset
1487
kono
parents: 67
diff changeset
1488 static const char *
kono
parents: 67
diff changeset
1489 process_switch (gswitch *swtch)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1490 {
111
kono
parents: 67
diff changeset
1491 struct switch_conv_info info;
kono
parents: 67
diff changeset
1492
kono
parents: 67
diff changeset
1493 /* Group case labels so that we get the right results from the heuristics
kono
parents: 67
diff changeset
1494 that decide on the code generation approach for this switch. */
kono
parents: 67
diff changeset
1495 group_case_labels_stmt (swtch);
kono
parents: 67
diff changeset
1496
kono
parents: 67
diff changeset
1497 /* If this switch is now a degenerate case with only a default label,
kono
parents: 67
diff changeset
1498 there is nothing left for us to do. */
kono
parents: 67
diff changeset
1499 if (gimple_switch_num_labels (swtch) < 2)
kono
parents: 67
diff changeset
1500 return "switch is a degenerate case";
kono
parents: 67
diff changeset
1501
kono
parents: 67
diff changeset
1502 collect_switch_conv_info (swtch, &info);
kono
parents: 67
diff changeset
1503
kono
parents: 67
diff changeset
1504 /* No error markers should reach here (they should be filtered out
kono
parents: 67
diff changeset
1505 during gimplification). */
kono
parents: 67
diff changeset
1506 gcc_checking_assert (TREE_TYPE (info.index_expr) != error_mark_node);
kono
parents: 67
diff changeset
1507
kono
parents: 67
diff changeset
1508 /* A switch on a constant should have been optimized in tree-cfg-cleanup. */
kono
parents: 67
diff changeset
1509 gcc_checking_assert (! TREE_CONSTANT (info.index_expr));
kono
parents: 67
diff changeset
1510
kono
parents: 67
diff changeset
1511 if (info.uniq <= MAX_CASE_BIT_TESTS)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1512 {
111
kono
parents: 67
diff changeset
1513 if (expand_switch_using_bit_tests_p (info.range_size,
kono
parents: 67
diff changeset
1514 info.uniq, info.count,
kono
parents: 67
diff changeset
1515 optimize_bb_for_speed_p
kono
parents: 67
diff changeset
1516 (gimple_bb (swtch))))
kono
parents: 67
diff changeset
1517 {
kono
parents: 67
diff changeset
1518 if (dump_file)
kono
parents: 67
diff changeset
1519 fputs (" expanding as bit test is preferable\n", dump_file);
kono
parents: 67
diff changeset
1520 emit_case_bit_tests (swtch, info.index_expr, info.range_min,
kono
parents: 67
diff changeset
1521 info.range_size, info.range_max);
kono
parents: 67
diff changeset
1522 loops_state_set (LOOPS_NEED_FIXUP);
kono
parents: 67
diff changeset
1523 return NULL;
kono
parents: 67
diff changeset
1524 }
kono
parents: 67
diff changeset
1525
kono
parents: 67
diff changeset
1526 if (info.uniq <= 2)
kono
parents: 67
diff changeset
1527 /* This will be expanded as a decision tree in stmt.c:expand_case. */
kono
parents: 67
diff changeset
1528 return " expanding as jumps is preferable";
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1529 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1530
111
kono
parents: 67
diff changeset
1531 /* If there is no common successor, we cannot do the transformation. */
kono
parents: 67
diff changeset
1532 if (! info.final_bb)
kono
parents: 67
diff changeset
1533 return "no common successor to all case label target blocks found";
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1534
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1535 /* Check the case label values are within reasonable range: */
111
kono
parents: 67
diff changeset
1536 if (!check_range (&info))
kono
parents: 67
diff changeset
1537 {
kono
parents: 67
diff changeset
1538 gcc_assert (info.reason);
kono
parents: 67
diff changeset
1539 return info.reason;
kono
parents: 67
diff changeset
1540 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1541
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1542 /* For all the cases, see whether they are empty, the assignments they
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1543 represent constant and so on... */
111
kono
parents: 67
diff changeset
1544 if (! check_all_empty_except_final (&info))
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1545 {
111
kono
parents: 67
diff changeset
1546 gcc_assert (info.reason);
kono
parents: 67
diff changeset
1547 return info.reason;
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1548 }
111
kono
parents: 67
diff changeset
1549 if (!check_final_bb (swtch, &info))
kono
parents: 67
diff changeset
1550 {
kono
parents: 67
diff changeset
1551 gcc_assert (info.reason);
kono
parents: 67
diff changeset
1552 return info.reason;
kono
parents: 67
diff changeset
1553 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1554
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1555 /* At this point all checks have passed and we can proceed with the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1556 transformation. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1557
111
kono
parents: 67
diff changeset
1558 create_temp_arrays (&info);
kono
parents: 67
diff changeset
1559 gather_default_values (info.default_case_nonstandard
kono
parents: 67
diff changeset
1560 ? gimple_switch_label (swtch, 1)
kono
parents: 67
diff changeset
1561 : gimple_switch_default_label (swtch), &info);
kono
parents: 67
diff changeset
1562 build_constructors (swtch, &info);
kono
parents: 67
diff changeset
1563
kono
parents: 67
diff changeset
1564 build_arrays (swtch, &info); /* Build the static arrays and assignments. */
kono
parents: 67
diff changeset
1565 gen_inbound_check (swtch, &info); /* Build the bounds check. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1566
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1567 /* Cleanup: */
111
kono
parents: 67
diff changeset
1568 free_temp_arrays (&info);
kono
parents: 67
diff changeset
1569 return NULL;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1570 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1571
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1572 /* The main function of the pass scans statements for switches and invokes
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1573 process_switch on them. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1574
111
kono
parents: 67
diff changeset
1575 namespace {
kono
parents: 67
diff changeset
1576
kono
parents: 67
diff changeset
1577 const pass_data pass_data_convert_switch =
kono
parents: 67
diff changeset
1578 {
kono
parents: 67
diff changeset
1579 GIMPLE_PASS, /* type */
kono
parents: 67
diff changeset
1580 "switchconv", /* name */
kono
parents: 67
diff changeset
1581 OPTGROUP_NONE, /* optinfo_flags */
kono
parents: 67
diff changeset
1582 TV_TREE_SWITCH_CONVERSION, /* tv_id */
kono
parents: 67
diff changeset
1583 ( PROP_cfg | PROP_ssa ), /* properties_required */
kono
parents: 67
diff changeset
1584 0, /* properties_provided */
kono
parents: 67
diff changeset
1585 0, /* properties_destroyed */
kono
parents: 67
diff changeset
1586 0, /* todo_flags_start */
kono
parents: 67
diff changeset
1587 TODO_update_ssa, /* todo_flags_finish */
kono
parents: 67
diff changeset
1588 };
kono
parents: 67
diff changeset
1589
kono
parents: 67
diff changeset
1590 class pass_convert_switch : public gimple_opt_pass
kono
parents: 67
diff changeset
1591 {
kono
parents: 67
diff changeset
1592 public:
kono
parents: 67
diff changeset
1593 pass_convert_switch (gcc::context *ctxt)
kono
parents: 67
diff changeset
1594 : gimple_opt_pass (pass_data_convert_switch, ctxt)
kono
parents: 67
diff changeset
1595 {}
kono
parents: 67
diff changeset
1596
kono
parents: 67
diff changeset
1597 /* opt_pass methods: */
kono
parents: 67
diff changeset
1598 virtual bool gate (function *) { return flag_tree_switch_conversion != 0; }
kono
parents: 67
diff changeset
1599 virtual unsigned int execute (function *);
kono
parents: 67
diff changeset
1600
kono
parents: 67
diff changeset
1601 }; // class pass_convert_switch
kono
parents: 67
diff changeset
1602
kono
parents: 67
diff changeset
1603 unsigned int
kono
parents: 67
diff changeset
1604 pass_convert_switch::execute (function *fun)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1605 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1606 basic_block bb;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1607
111
kono
parents: 67
diff changeset
1608 FOR_EACH_BB_FN (bb, fun)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1609 {
111
kono
parents: 67
diff changeset
1610 const char *failure_reason;
kono
parents: 67
diff changeset
1611 gimple *stmt = last_stmt (bb);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1612 if (stmt && gimple_code (stmt) == GIMPLE_SWITCH)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1613 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1614 if (dump_file)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1615 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1616 expanded_location loc = expand_location (gimple_location (stmt));
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1617
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1618 fprintf (dump_file, "beginning to process the following "
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1619 "SWITCH statement (%s:%d) : ------- \n",
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1620 loc.file, loc.line);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1621 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1622 putc ('\n', dump_file);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1623 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1624
111
kono
parents: 67
diff changeset
1625 failure_reason = process_switch (as_a <gswitch *> (stmt));
kono
parents: 67
diff changeset
1626 if (! failure_reason)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1627 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1628 if (dump_file)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1629 {
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1630 fputs ("Switch converted\n", dump_file);
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1631 fputs ("--------------------------------\n", dump_file);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1632 }
111
kono
parents: 67
diff changeset
1633
kono
parents: 67
diff changeset
1634 /* Make no effort to update the post-dominator tree. It is actually not
kono
parents: 67
diff changeset
1635 that hard for the transformations we have performed, but it is not
kono
parents: 67
diff changeset
1636 supported by iterate_fix_dominators. */
kono
parents: 67
diff changeset
1637 free_dominance_info (CDI_POST_DOMINATORS);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1638 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1639 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1640 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1641 if (dump_file)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1642 {
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1643 fputs ("Bailing out - ", dump_file);
111
kono
parents: 67
diff changeset
1644 fputs (failure_reason, dump_file);
kono
parents: 67
diff changeset
1645 fputs ("\n--------------------------------\n", dump_file);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1646 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1647 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1648 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1649 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1650
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1651 return 0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1652 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1653
111
kono
parents: 67
diff changeset
1654 } // anon namespace
kono
parents: 67
diff changeset
1655
kono
parents: 67
diff changeset
1656 gimple_opt_pass *
kono
parents: 67
diff changeset
1657 make_pass_convert_switch (gcc::context *ctxt)
kono
parents: 67
diff changeset
1658 {
kono
parents: 67
diff changeset
1659 return new pass_convert_switch (ctxt);
kono
parents: 67
diff changeset
1660 }
kono
parents: 67
diff changeset
1661
kono
parents: 67
diff changeset
1662 struct case_node
kono
parents: 67
diff changeset
1663 {
kono
parents: 67
diff changeset
1664 case_node *left; /* Left son in binary tree. */
kono
parents: 67
diff changeset
1665 case_node *right; /* Right son in binary tree;
kono
parents: 67
diff changeset
1666 also node chain. */
kono
parents: 67
diff changeset
1667 case_node *parent; /* Parent of node in binary tree. */
kono
parents: 67
diff changeset
1668 tree low; /* Lowest index value for this label. */
kono
parents: 67
diff changeset
1669 tree high; /* Highest index value for this label. */
kono
parents: 67
diff changeset
1670 basic_block case_bb; /* Label to jump to when node matches. */
kono
parents: 67
diff changeset
1671 tree case_label; /* Label to jump to when node matches. */
kono
parents: 67
diff changeset
1672 profile_probability prob; /* Probability of taking this case. */
kono
parents: 67
diff changeset
1673 profile_probability subtree_prob; /* Probability of reaching subtree
kono
parents: 67
diff changeset
1674 rooted at this node. */
kono
parents: 67
diff changeset
1675 };
kono
parents: 67
diff changeset
1676
kono
parents: 67
diff changeset
1677 typedef case_node *case_node_ptr;
kono
parents: 67
diff changeset
1678
kono
parents: 67
diff changeset
1679 static basic_block emit_case_nodes (basic_block, tree, case_node_ptr,
kono
parents: 67
diff changeset
1680 basic_block, tree, profile_probability,
kono
parents: 67
diff changeset
1681 tree, hash_map<tree, tree> *);
kono
parents: 67
diff changeset
1682 static bool node_has_low_bound (case_node_ptr, tree);
kono
parents: 67
diff changeset
1683 static bool node_has_high_bound (case_node_ptr, tree);
kono
parents: 67
diff changeset
1684 static bool node_is_bounded (case_node_ptr, tree);
kono
parents: 67
diff changeset
1685
kono
parents: 67
diff changeset
1686 /* Return the smallest number of different values for which it is best to use a
kono
parents: 67
diff changeset
1687 jump-table instead of a tree of conditional branches. */
kono
parents: 67
diff changeset
1688
kono
parents: 67
diff changeset
1689 static unsigned int
kono
parents: 67
diff changeset
1690 case_values_threshold (void)
kono
parents: 67
diff changeset
1691 {
kono
parents: 67
diff changeset
1692 unsigned int threshold = PARAM_VALUE (PARAM_CASE_VALUES_THRESHOLD);
kono
parents: 67
diff changeset
1693
kono
parents: 67
diff changeset
1694 if (threshold == 0)
kono
parents: 67
diff changeset
1695 threshold = targetm.case_values_threshold ();
kono
parents: 67
diff changeset
1696
kono
parents: 67
diff changeset
1697 return threshold;
kono
parents: 67
diff changeset
1698 }
kono
parents: 67
diff changeset
1699
kono
parents: 67
diff changeset
1700 /* Reset the aux field of all outgoing edges of basic block BB. */
kono
parents: 67
diff changeset
1701
kono
parents: 67
diff changeset
1702 static inline void
kono
parents: 67
diff changeset
1703 reset_out_edges_aux (basic_block bb)
kono
parents: 67
diff changeset
1704 {
kono
parents: 67
diff changeset
1705 edge e;
kono
parents: 67
diff changeset
1706 edge_iterator ei;
kono
parents: 67
diff changeset
1707 FOR_EACH_EDGE (e, ei, bb->succs)
kono
parents: 67
diff changeset
1708 e->aux = (void *) 0;
kono
parents: 67
diff changeset
1709 }
kono
parents: 67
diff changeset
1710
kono
parents: 67
diff changeset
1711 /* Compute the number of case labels that correspond to each outgoing edge of
kono
parents: 67
diff changeset
1712 STMT. Record this information in the aux field of the edge. */
kono
parents: 67
diff changeset
1713
kono
parents: 67
diff changeset
1714 static inline void
kono
parents: 67
diff changeset
1715 compute_cases_per_edge (gswitch *stmt)
kono
parents: 67
diff changeset
1716 {
kono
parents: 67
diff changeset
1717 basic_block bb = gimple_bb (stmt);
kono
parents: 67
diff changeset
1718 reset_out_edges_aux (bb);
kono
parents: 67
diff changeset
1719 int ncases = gimple_switch_num_labels (stmt);
kono
parents: 67
diff changeset
1720 for (int i = ncases - 1; i >= 1; --i)
kono
parents: 67
diff changeset
1721 {
kono
parents: 67
diff changeset
1722 tree elt = gimple_switch_label (stmt, i);
kono
parents: 67
diff changeset
1723 tree lab = CASE_LABEL (elt);
kono
parents: 67
diff changeset
1724 basic_block case_bb = label_to_block_fn (cfun, lab);
kono
parents: 67
diff changeset
1725 edge case_edge = find_edge (bb, case_bb);
kono
parents: 67
diff changeset
1726 case_edge->aux = (void *) ((intptr_t) (case_edge->aux) + 1);
kono
parents: 67
diff changeset
1727 }
kono
parents: 67
diff changeset
1728 }
kono
parents: 67
diff changeset
1729
kono
parents: 67
diff changeset
1730 /* Do the insertion of a case label into case_list. The labels are
kono
parents: 67
diff changeset
1731 fed to us in descending order from the sorted vector of case labels used
kono
parents: 67
diff changeset
1732 in the tree part of the middle end. So the list we construct is
kono
parents: 67
diff changeset
1733 sorted in ascending order.
kono
parents: 67
diff changeset
1734
kono
parents: 67
diff changeset
1735 LABEL is the case label to be inserted. LOW and HIGH are the bounds
kono
parents: 67
diff changeset
1736 against which the index is compared to jump to LABEL and PROB is the
kono
parents: 67
diff changeset
1737 estimated probability LABEL is reached from the switch statement. */
kono
parents: 67
diff changeset
1738
kono
parents: 67
diff changeset
1739 static case_node *
kono
parents: 67
diff changeset
1740 add_case_node (case_node *head, tree low, tree high, basic_block case_bb,
kono
parents: 67
diff changeset
1741 tree case_label, profile_probability prob,
kono
parents: 67
diff changeset
1742 object_allocator<case_node> &case_node_pool)
kono
parents: 67
diff changeset
1743 {
kono
parents: 67
diff changeset
1744 case_node *r;
kono
parents: 67
diff changeset
1745
kono
parents: 67
diff changeset
1746 gcc_checking_assert (low);
kono
parents: 67
diff changeset
1747 gcc_checking_assert (high && (TREE_TYPE (low) == TREE_TYPE (high)));
kono
parents: 67
diff changeset
1748
kono
parents: 67
diff changeset
1749 /* Add this label to the chain. */
kono
parents: 67
diff changeset
1750 r = case_node_pool.allocate ();
kono
parents: 67
diff changeset
1751 r->low = low;
kono
parents: 67
diff changeset
1752 r->high = high;
kono
parents: 67
diff changeset
1753 r->case_bb = case_bb;
kono
parents: 67
diff changeset
1754 r->case_label = case_label;
kono
parents: 67
diff changeset
1755 r->parent = r->left = NULL;
kono
parents: 67
diff changeset
1756 r->prob = prob;
kono
parents: 67
diff changeset
1757 r->subtree_prob = prob;
kono
parents: 67
diff changeset
1758 r->right = head;
kono
parents: 67
diff changeset
1759 return r;
kono
parents: 67
diff changeset
1760 }
kono
parents: 67
diff changeset
1761
kono
parents: 67
diff changeset
1762 /* Dump ROOT, a list or tree of case nodes, to file. */
kono
parents: 67
diff changeset
1763
kono
parents: 67
diff changeset
1764 static void
kono
parents: 67
diff changeset
1765 dump_case_nodes (FILE *f, case_node *root, int indent_step, int indent_level)
kono
parents: 67
diff changeset
1766 {
kono
parents: 67
diff changeset
1767 if (root == 0)
kono
parents: 67
diff changeset
1768 return;
kono
parents: 67
diff changeset
1769 indent_level++;
kono
parents: 67
diff changeset
1770
kono
parents: 67
diff changeset
1771 dump_case_nodes (f, root->left, indent_step, indent_level);
kono
parents: 67
diff changeset
1772
kono
parents: 67
diff changeset
1773 fputs (";; ", f);
kono
parents: 67
diff changeset
1774 fprintf (f, "%*s", indent_step * indent_level, "");
kono
parents: 67
diff changeset
1775 print_dec (wi::to_wide (root->low), f, TYPE_SIGN (TREE_TYPE (root->low)));
kono
parents: 67
diff changeset
1776 if (!tree_int_cst_equal (root->low, root->high))
kono
parents: 67
diff changeset
1777 {
kono
parents: 67
diff changeset
1778 fprintf (f, " ... ");
kono
parents: 67
diff changeset
1779 print_dec (wi::to_wide (root->high), f,
kono
parents: 67
diff changeset
1780 TYPE_SIGN (TREE_TYPE (root->high)));
kono
parents: 67
diff changeset
1781 }
kono
parents: 67
diff changeset
1782 fputs ("\n", f);
kono
parents: 67
diff changeset
1783
kono
parents: 67
diff changeset
1784 dump_case_nodes (f, root->right, indent_step, indent_level);
kono
parents: 67
diff changeset
1785 }
kono
parents: 67
diff changeset
1786
kono
parents: 67
diff changeset
1787 /* Take an ordered list of case nodes
kono
parents: 67
diff changeset
1788 and transform them into a near optimal binary tree,
kono
parents: 67
diff changeset
1789 on the assumption that any target code selection value is as
kono
parents: 67
diff changeset
1790 likely as any other.
kono
parents: 67
diff changeset
1791
kono
parents: 67
diff changeset
1792 The transformation is performed by splitting the ordered
kono
parents: 67
diff changeset
1793 list into two equal sections plus a pivot. The parts are
kono
parents: 67
diff changeset
1794 then attached to the pivot as left and right branches. Each
kono
parents: 67
diff changeset
1795 branch is then transformed recursively. */
kono
parents: 67
diff changeset
1796
kono
parents: 67
diff changeset
1797 static void
kono
parents: 67
diff changeset
1798 balance_case_nodes (case_node_ptr *head, case_node_ptr parent)
kono
parents: 67
diff changeset
1799 {
kono
parents: 67
diff changeset
1800 case_node_ptr np;
kono
parents: 67
diff changeset
1801
kono
parents: 67
diff changeset
1802 np = *head;
kono
parents: 67
diff changeset
1803 if (np)
kono
parents: 67
diff changeset
1804 {
kono
parents: 67
diff changeset
1805 int i = 0;
kono
parents: 67
diff changeset
1806 int ranges = 0;
kono
parents: 67
diff changeset
1807 case_node_ptr *npp;
kono
parents: 67
diff changeset
1808 case_node_ptr left;
kono
parents: 67
diff changeset
1809
kono
parents: 67
diff changeset
1810 /* Count the number of entries on branch. Also count the ranges. */
kono
parents: 67
diff changeset
1811
kono
parents: 67
diff changeset
1812 while (np)
kono
parents: 67
diff changeset
1813 {
kono
parents: 67
diff changeset
1814 if (!tree_int_cst_equal (np->low, np->high))
kono
parents: 67
diff changeset
1815 ranges++;
kono
parents: 67
diff changeset
1816
kono
parents: 67
diff changeset
1817 i++;
kono
parents: 67
diff changeset
1818 np = np->right;
kono
parents: 67
diff changeset
1819 }
kono
parents: 67
diff changeset
1820
kono
parents: 67
diff changeset
1821 if (i > 2)
kono
parents: 67
diff changeset
1822 {
kono
parents: 67
diff changeset
1823 /* Split this list if it is long enough for that to help. */
kono
parents: 67
diff changeset
1824 npp = head;
kono
parents: 67
diff changeset
1825 left = *npp;
kono
parents: 67
diff changeset
1826
kono
parents: 67
diff changeset
1827 /* If there are just three nodes, split at the middle one. */
kono
parents: 67
diff changeset
1828 if (i == 3)
kono
parents: 67
diff changeset
1829 npp = &(*npp)->right;
kono
parents: 67
diff changeset
1830 else
kono
parents: 67
diff changeset
1831 {
kono
parents: 67
diff changeset
1832 /* Find the place in the list that bisects the list's total cost,
kono
parents: 67
diff changeset
1833 where ranges count as 2.
kono
parents: 67
diff changeset
1834 Here I gets half the total cost. */
kono
parents: 67
diff changeset
1835 i = (i + ranges + 1) / 2;
kono
parents: 67
diff changeset
1836 while (1)
kono
parents: 67
diff changeset
1837 {
kono
parents: 67
diff changeset
1838 /* Skip nodes while their cost does not reach that amount. */
kono
parents: 67
diff changeset
1839 if (!tree_int_cst_equal ((*npp)->low, (*npp)->high))
kono
parents: 67
diff changeset
1840 i--;
kono
parents: 67
diff changeset
1841 i--;
kono
parents: 67
diff changeset
1842 if (i <= 0)
kono
parents: 67
diff changeset
1843 break;
kono
parents: 67
diff changeset
1844 npp = &(*npp)->right;
kono
parents: 67
diff changeset
1845 }
kono
parents: 67
diff changeset
1846 }
kono
parents: 67
diff changeset
1847 *head = np = *npp;
kono
parents: 67
diff changeset
1848 *npp = 0;
kono
parents: 67
diff changeset
1849 np->parent = parent;
kono
parents: 67
diff changeset
1850 np->left = left;
kono
parents: 67
diff changeset
1851
kono
parents: 67
diff changeset
1852 /* Optimize each of the two split parts. */
kono
parents: 67
diff changeset
1853 balance_case_nodes (&np->left, np);
kono
parents: 67
diff changeset
1854 balance_case_nodes (&np->right, np);
kono
parents: 67
diff changeset
1855 np->subtree_prob = np->prob;
kono
parents: 67
diff changeset
1856 np->subtree_prob += np->left->subtree_prob;
kono
parents: 67
diff changeset
1857 np->subtree_prob += np->right->subtree_prob;
kono
parents: 67
diff changeset
1858 }
kono
parents: 67
diff changeset
1859 else
kono
parents: 67
diff changeset
1860 {
kono
parents: 67
diff changeset
1861 /* Else leave this branch as one level,
kono
parents: 67
diff changeset
1862 but fill in `parent' fields. */
kono
parents: 67
diff changeset
1863 np = *head;
kono
parents: 67
diff changeset
1864 np->parent = parent;
kono
parents: 67
diff changeset
1865 np->subtree_prob = np->prob;
kono
parents: 67
diff changeset
1866 for (; np->right; np = np->right)
kono
parents: 67
diff changeset
1867 {
kono
parents: 67
diff changeset
1868 np->right->parent = np;
kono
parents: 67
diff changeset
1869 (*head)->subtree_prob += np->right->subtree_prob;
kono
parents: 67
diff changeset
1870 }
kono
parents: 67
diff changeset
1871 }
kono
parents: 67
diff changeset
1872 }
kono
parents: 67
diff changeset
1873 }
kono
parents: 67
diff changeset
1874
kono
parents: 67
diff changeset
1875 /* Return true if a switch should be expanded as a decision tree.
kono
parents: 67
diff changeset
1876 RANGE is the difference between highest and lowest case.
kono
parents: 67
diff changeset
1877 UNIQ is number of unique case node targets, not counting the default case.
kono
parents: 67
diff changeset
1878 COUNT is the number of comparisons needed, not counting the default case. */
kono
parents: 67
diff changeset
1879
kono
parents: 67
diff changeset
1880 static bool
kono
parents: 67
diff changeset
1881 expand_switch_as_decision_tree_p (tree range,
kono
parents: 67
diff changeset
1882 unsigned int uniq ATTRIBUTE_UNUSED,
kono
parents: 67
diff changeset
1883 unsigned int count)
kono
parents: 67
diff changeset
1884 {
kono
parents: 67
diff changeset
1885 int max_ratio;
kono
parents: 67
diff changeset
1886
kono
parents: 67
diff changeset
1887 /* If neither casesi or tablejump is available, or flag_jump_tables
kono
parents: 67
diff changeset
1888 over-ruled us, we really have no choice. */
kono
parents: 67
diff changeset
1889 if (!targetm.have_casesi () && !targetm.have_tablejump ())
kono
parents: 67
diff changeset
1890 return true;
kono
parents: 67
diff changeset
1891 if (!flag_jump_tables)
kono
parents: 67
diff changeset
1892 return true;
kono
parents: 67
diff changeset
1893 #ifndef ASM_OUTPUT_ADDR_DIFF_ELT
kono
parents: 67
diff changeset
1894 if (flag_pic)
kono
parents: 67
diff changeset
1895 return true;
kono
parents: 67
diff changeset
1896 #endif
kono
parents: 67
diff changeset
1897
kono
parents: 67
diff changeset
1898 /* If the switch is relatively small such that the cost of one
kono
parents: 67
diff changeset
1899 indirect jump on the target are higher than the cost of a
kono
parents: 67
diff changeset
1900 decision tree, go with the decision tree.
kono
parents: 67
diff changeset
1901
kono
parents: 67
diff changeset
1902 If range of values is much bigger than number of values,
kono
parents: 67
diff changeset
1903 or if it is too large to represent in a HOST_WIDE_INT,
kono
parents: 67
diff changeset
1904 make a sequence of conditional branches instead of a dispatch.
kono
parents: 67
diff changeset
1905
kono
parents: 67
diff changeset
1906 The definition of "much bigger" depends on whether we are
kono
parents: 67
diff changeset
1907 optimizing for size or for speed. If the former, the maximum
kono
parents: 67
diff changeset
1908 ratio range/count = 3, because this was found to be the optimal
kono
parents: 67
diff changeset
1909 ratio for size on i686-pc-linux-gnu, see PR11823. The ratio
kono
parents: 67
diff changeset
1910 10 is much older, and was probably selected after an extensive
kono
parents: 67
diff changeset
1911 benchmarking investigation on numerous platforms. Or maybe it
kono
parents: 67
diff changeset
1912 just made sense to someone at some point in the history of GCC,
kono
parents: 67
diff changeset
1913 who knows... */
kono
parents: 67
diff changeset
1914 max_ratio = optimize_insn_for_size_p () ? 3 : 10;
kono
parents: 67
diff changeset
1915 if (count < case_values_threshold () || !tree_fits_uhwi_p (range)
kono
parents: 67
diff changeset
1916 || compare_tree_int (range, max_ratio * count) > 0)
kono
parents: 67
diff changeset
1917 return true;
kono
parents: 67
diff changeset
1918
kono
parents: 67
diff changeset
1919 return false;
kono
parents: 67
diff changeset
1920 }
kono
parents: 67
diff changeset
1921
kono
parents: 67
diff changeset
1922 static void
kono
parents: 67
diff changeset
1923 fix_phi_operands_for_edge (edge e, hash_map<tree, tree> *phi_mapping)
kono
parents: 67
diff changeset
1924 {
kono
parents: 67
diff changeset
1925 basic_block bb = e->dest;
kono
parents: 67
diff changeset
1926 gphi_iterator gsi;
kono
parents: 67
diff changeset
1927 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
kono
parents: 67
diff changeset
1928 {
kono
parents: 67
diff changeset
1929 gphi *phi = gsi.phi ();
kono
parents: 67
diff changeset
1930
kono
parents: 67
diff changeset
1931 tree *definition = phi_mapping->get (gimple_phi_result (phi));
kono
parents: 67
diff changeset
1932 if (definition)
kono
parents: 67
diff changeset
1933 add_phi_arg (phi, *definition, e, UNKNOWN_LOCATION);
kono
parents: 67
diff changeset
1934 }
kono
parents: 67
diff changeset
1935 }
kono
parents: 67
diff changeset
1936
kono
parents: 67
diff changeset
1937
kono
parents: 67
diff changeset
1938 /* Add an unconditional jump to CASE_BB that happens in basic block BB. */
kono
parents: 67
diff changeset
1939
kono
parents: 67
diff changeset
1940 static void
kono
parents: 67
diff changeset
1941 emit_jump (basic_block bb, basic_block case_bb,
kono
parents: 67
diff changeset
1942 hash_map<tree, tree> *phi_mapping)
kono
parents: 67
diff changeset
1943 {
kono
parents: 67
diff changeset
1944 edge e = single_succ_edge (bb);
kono
parents: 67
diff changeset
1945 redirect_edge_succ (e, case_bb);
kono
parents: 67
diff changeset
1946 fix_phi_operands_for_edge (e, phi_mapping);
kono
parents: 67
diff changeset
1947 }
kono
parents: 67
diff changeset
1948
kono
parents: 67
diff changeset
1949 /* Generate a decision tree, switching on INDEX_EXPR and jumping to
kono
parents: 67
diff changeset
1950 one of the labels in CASE_LIST or to the DEFAULT_LABEL.
kono
parents: 67
diff changeset
1951 DEFAULT_PROB is the estimated probability that it jumps to
kono
parents: 67
diff changeset
1952 DEFAULT_LABEL.
kono
parents: 67
diff changeset
1953
kono
parents: 67
diff changeset
1954 We generate a binary decision tree to select the appropriate target
kono
parents: 67
diff changeset
1955 code. */
kono
parents: 67
diff changeset
1956
kono
parents: 67
diff changeset
1957 static void
kono
parents: 67
diff changeset
1958 emit_case_decision_tree (gswitch *s, tree index_expr, tree index_type,
kono
parents: 67
diff changeset
1959 case_node_ptr case_list, basic_block default_bb,
kono
parents: 67
diff changeset
1960 tree default_label, profile_probability default_prob,
kono
parents: 67
diff changeset
1961 hash_map<tree, tree> *phi_mapping)
kono
parents: 67
diff changeset
1962 {
kono
parents: 67
diff changeset
1963 balance_case_nodes (&case_list, NULL);
kono
parents: 67
diff changeset
1964
kono
parents: 67
diff changeset
1965 if (dump_file)
kono
parents: 67
diff changeset
1966 dump_function_to_file (current_function_decl, dump_file, dump_flags);
kono
parents: 67
diff changeset
1967 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents: 67
diff changeset
1968 {
kono
parents: 67
diff changeset
1969 int indent_step = ceil_log2 (TYPE_PRECISION (index_type)) + 2;
kono
parents: 67
diff changeset
1970 fprintf (dump_file, ";; Expanding GIMPLE switch as decision tree:\n");
kono
parents: 67
diff changeset
1971 dump_case_nodes (dump_file, case_list, indent_step, 0);
kono
parents: 67
diff changeset
1972 }
kono
parents: 67
diff changeset
1973
kono
parents: 67
diff changeset
1974 basic_block bb = gimple_bb (s);
kono
parents: 67
diff changeset
1975 gimple_stmt_iterator gsi = gsi_last_bb (bb);
kono
parents: 67
diff changeset
1976 edge e;
kono
parents: 67
diff changeset
1977 if (gsi_end_p (gsi))
kono
parents: 67
diff changeset
1978 e = split_block_after_labels (bb);
kono
parents: 67
diff changeset
1979 else
kono
parents: 67
diff changeset
1980 {
kono
parents: 67
diff changeset
1981 gsi_prev (&gsi);
kono
parents: 67
diff changeset
1982 e = split_block (bb, gsi_stmt (gsi));
kono
parents: 67
diff changeset
1983 }
kono
parents: 67
diff changeset
1984 bb = split_edge (e);
kono
parents: 67
diff changeset
1985
kono
parents: 67
diff changeset
1986 bb = emit_case_nodes (bb, index_expr, case_list, default_bb, default_label,
kono
parents: 67
diff changeset
1987 default_prob, index_type, phi_mapping);
kono
parents: 67
diff changeset
1988
kono
parents: 67
diff changeset
1989 if (bb)
kono
parents: 67
diff changeset
1990 emit_jump (bb, default_bb, phi_mapping);
kono
parents: 67
diff changeset
1991
kono
parents: 67
diff changeset
1992 /* Remove all edges and do just an edge that will reach default_bb. */
kono
parents: 67
diff changeset
1993 gsi = gsi_last_bb (gimple_bb (s));
kono
parents: 67
diff changeset
1994 gsi_remove (&gsi, true);
kono
parents: 67
diff changeset
1995 }
kono
parents: 67
diff changeset
1996
kono
parents: 67
diff changeset
1997 static void
kono
parents: 67
diff changeset
1998 record_phi_operand_mapping (const vec<basic_block> bbs, basic_block switch_bb,
kono
parents: 67
diff changeset
1999 hash_map <tree, tree> *map)
kono
parents: 67
diff changeset
2000 {
kono
parents: 67
diff changeset
2001 /* Record all PHI nodes that have to be fixed after conversion. */
kono
parents: 67
diff changeset
2002 for (unsigned i = 0; i < bbs.length (); i++)
kono
parents: 67
diff changeset
2003 {
kono
parents: 67
diff changeset
2004 basic_block bb = bbs[i];
kono
parents: 67
diff changeset
2005
kono
parents: 67
diff changeset
2006 gphi_iterator gsi;
kono
parents: 67
diff changeset
2007 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
kono
parents: 67
diff changeset
2008 {
kono
parents: 67
diff changeset
2009 gphi *phi = gsi.phi ();
kono
parents: 67
diff changeset
2010
kono
parents: 67
diff changeset
2011 for (unsigned i = 0; i < gimple_phi_num_args (phi); i++)
kono
parents: 67
diff changeset
2012 {
kono
parents: 67
diff changeset
2013 basic_block phi_src_bb = gimple_phi_arg_edge (phi, i)->src;
kono
parents: 67
diff changeset
2014 if (phi_src_bb == switch_bb)
kono
parents: 67
diff changeset
2015 {
kono
parents: 67
diff changeset
2016 tree def = gimple_phi_arg_def (phi, i);
kono
parents: 67
diff changeset
2017 tree result = gimple_phi_result (phi);
kono
parents: 67
diff changeset
2018 map->put (result, def);
kono
parents: 67
diff changeset
2019 break;
kono
parents: 67
diff changeset
2020 }
kono
parents: 67
diff changeset
2021 }
kono
parents: 67
diff changeset
2022 }
kono
parents: 67
diff changeset
2023 }
kono
parents: 67
diff changeset
2024 }
kono
parents: 67
diff changeset
2025
kono
parents: 67
diff changeset
2026 /* Attempt to expand gimple switch STMT to a decision tree. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2027
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2028 static bool
111
kono
parents: 67
diff changeset
2029 try_switch_expansion (gswitch *stmt)
kono
parents: 67
diff changeset
2030 {
kono
parents: 67
diff changeset
2031 tree minval = NULL_TREE, maxval = NULL_TREE, range = NULL_TREE;
kono
parents: 67
diff changeset
2032 basic_block default_bb;
kono
parents: 67
diff changeset
2033 unsigned int count, uniq;
kono
parents: 67
diff changeset
2034 int i;
kono
parents: 67
diff changeset
2035 int ncases = gimple_switch_num_labels (stmt);
kono
parents: 67
diff changeset
2036 tree index_expr = gimple_switch_index (stmt);
kono
parents: 67
diff changeset
2037 tree index_type = TREE_TYPE (index_expr);
kono
parents: 67
diff changeset
2038 tree elt;
kono
parents: 67
diff changeset
2039 basic_block bb = gimple_bb (stmt);
kono
parents: 67
diff changeset
2040
kono
parents: 67
diff changeset
2041 hash_map<tree, tree> phi_mapping;
kono
parents: 67
diff changeset
2042 auto_vec<basic_block> case_bbs;
kono
parents: 67
diff changeset
2043
kono
parents: 67
diff changeset
2044 /* A list of case labels; it is first built as a list and it may then
kono
parents: 67
diff changeset
2045 be rearranged into a nearly balanced binary tree. */
kono
parents: 67
diff changeset
2046 case_node *case_list = 0;
kono
parents: 67
diff changeset
2047
kono
parents: 67
diff changeset
2048 /* A pool for case nodes. */
kono
parents: 67
diff changeset
2049 object_allocator<case_node> case_node_pool ("struct case_node pool");
kono
parents: 67
diff changeset
2050
kono
parents: 67
diff changeset
2051 /* cleanup_tree_cfg removes all SWITCH_EXPR with their index
kono
parents: 67
diff changeset
2052 expressions being INTEGER_CST. */
kono
parents: 67
diff changeset
2053 gcc_assert (TREE_CODE (index_expr) != INTEGER_CST);
kono
parents: 67
diff changeset
2054
kono
parents: 67
diff changeset
2055 if (ncases == 1)
kono
parents: 67
diff changeset
2056 return false;
kono
parents: 67
diff changeset
2057
kono
parents: 67
diff changeset
2058 /* Find the default case target label. */
kono
parents: 67
diff changeset
2059 tree default_label = CASE_LABEL (gimple_switch_default_label (stmt));
kono
parents: 67
diff changeset
2060 default_bb = label_to_block_fn (cfun, default_label);
kono
parents: 67
diff changeset
2061 edge default_edge = find_edge (bb, default_bb);
kono
parents: 67
diff changeset
2062 profile_probability default_prob = default_edge->probability;
kono
parents: 67
diff changeset
2063 case_bbs.safe_push (default_bb);
kono
parents: 67
diff changeset
2064
kono
parents: 67
diff changeset
2065 /* Get upper and lower bounds of case values. */
kono
parents: 67
diff changeset
2066 elt = gimple_switch_label (stmt, 1);
kono
parents: 67
diff changeset
2067 minval = fold_convert (index_type, CASE_LOW (elt));
kono
parents: 67
diff changeset
2068 elt = gimple_switch_label (stmt, ncases - 1);
kono
parents: 67
diff changeset
2069 if (CASE_HIGH (elt))
kono
parents: 67
diff changeset
2070 maxval = fold_convert (index_type, CASE_HIGH (elt));
kono
parents: 67
diff changeset
2071 else
kono
parents: 67
diff changeset
2072 maxval = fold_convert (index_type, CASE_LOW (elt));
kono
parents: 67
diff changeset
2073
kono
parents: 67
diff changeset
2074 /* Compute span of values. */
kono
parents: 67
diff changeset
2075 range = fold_build2 (MINUS_EXPR, index_type, maxval, minval);
kono
parents: 67
diff changeset
2076
kono
parents: 67
diff changeset
2077 /* Listify the labels queue and gather some numbers to decide
kono
parents: 67
diff changeset
2078 how to expand this switch. */
kono
parents: 67
diff changeset
2079 uniq = 0;
kono
parents: 67
diff changeset
2080 count = 0;
kono
parents: 67
diff changeset
2081 hash_set<tree> seen_labels;
kono
parents: 67
diff changeset
2082 compute_cases_per_edge (stmt);
kono
parents: 67
diff changeset
2083
kono
parents: 67
diff changeset
2084 for (i = ncases - 1; i >= 1; --i)
kono
parents: 67
diff changeset
2085 {
kono
parents: 67
diff changeset
2086 elt = gimple_switch_label (stmt, i);
kono
parents: 67
diff changeset
2087 tree low = CASE_LOW (elt);
kono
parents: 67
diff changeset
2088 gcc_assert (low);
kono
parents: 67
diff changeset
2089 tree high = CASE_HIGH (elt);
kono
parents: 67
diff changeset
2090 gcc_assert (!high || tree_int_cst_lt (low, high));
kono
parents: 67
diff changeset
2091 tree lab = CASE_LABEL (elt);
kono
parents: 67
diff changeset
2092
kono
parents: 67
diff changeset
2093 /* Count the elements.
kono
parents: 67
diff changeset
2094 A range counts double, since it requires two compares. */
kono
parents: 67
diff changeset
2095 count++;
kono
parents: 67
diff changeset
2096 if (high)
kono
parents: 67
diff changeset
2097 count++;
kono
parents: 67
diff changeset
2098
kono
parents: 67
diff changeset
2099 /* If we have not seen this label yet, then increase the
kono
parents: 67
diff changeset
2100 number of unique case node targets seen. */
kono
parents: 67
diff changeset
2101 if (!seen_labels.add (lab))
kono
parents: 67
diff changeset
2102 uniq++;
kono
parents: 67
diff changeset
2103
kono
parents: 67
diff changeset
2104 /* The bounds on the case range, LOW and HIGH, have to be converted
kono
parents: 67
diff changeset
2105 to case's index type TYPE. Note that the original type of the
kono
parents: 67
diff changeset
2106 case index in the source code is usually "lost" during
kono
parents: 67
diff changeset
2107 gimplification due to type promotion, but the case labels retain the
kono
parents: 67
diff changeset
2108 original type. Make sure to drop overflow flags. */
kono
parents: 67
diff changeset
2109 low = fold_convert (index_type, low);
kono
parents: 67
diff changeset
2110 if (TREE_OVERFLOW (low))
kono
parents: 67
diff changeset
2111 low = wide_int_to_tree (index_type, wi::to_wide (low));
kono
parents: 67
diff changeset
2112
kono
parents: 67
diff changeset
2113 /* The canonical from of a case label in GIMPLE is that a simple case
kono
parents: 67
diff changeset
2114 has an empty CASE_HIGH. For the casesi and tablejump expanders,
kono
parents: 67
diff changeset
2115 the back ends want simple cases to have high == low. */
kono
parents: 67
diff changeset
2116 if (!high)
kono
parents: 67
diff changeset
2117 high = low;
kono
parents: 67
diff changeset
2118 high = fold_convert (index_type, high);
kono
parents: 67
diff changeset
2119 if (TREE_OVERFLOW (high))
kono
parents: 67
diff changeset
2120 high = wide_int_to_tree (index_type, wi::to_wide (high));
kono
parents: 67
diff changeset
2121
kono
parents: 67
diff changeset
2122 basic_block case_bb = label_to_block_fn (cfun, lab);
kono
parents: 67
diff changeset
2123 edge case_edge = find_edge (bb, case_bb);
kono
parents: 67
diff changeset
2124 case_list = add_case_node (
kono
parents: 67
diff changeset
2125 case_list, low, high, case_bb, lab,
kono
parents: 67
diff changeset
2126 case_edge->probability.apply_scale (1, (intptr_t) (case_edge->aux)),
kono
parents: 67
diff changeset
2127 case_node_pool);
kono
parents: 67
diff changeset
2128
kono
parents: 67
diff changeset
2129 case_bbs.safe_push (case_bb);
kono
parents: 67
diff changeset
2130 }
kono
parents: 67
diff changeset
2131 reset_out_edges_aux (bb);
kono
parents: 67
diff changeset
2132 record_phi_operand_mapping (case_bbs, bb, &phi_mapping);
kono
parents: 67
diff changeset
2133
kono
parents: 67
diff changeset
2134 /* cleanup_tree_cfg removes all SWITCH_EXPR with a single
kono
parents: 67
diff changeset
2135 destination, such as one with a default case only.
kono
parents: 67
diff changeset
2136 It also removes cases that are out of range for the switch
kono
parents: 67
diff changeset
2137 type, so we should never get a zero here. */
kono
parents: 67
diff changeset
2138 gcc_assert (count > 0);
kono
parents: 67
diff changeset
2139
kono
parents: 67
diff changeset
2140 /* Decide how to expand this switch.
kono
parents: 67
diff changeset
2141 The two options at this point are a dispatch table (casesi or
kono
parents: 67
diff changeset
2142 tablejump) or a decision tree. */
kono
parents: 67
diff changeset
2143
kono
parents: 67
diff changeset
2144 if (expand_switch_as_decision_tree_p (range, uniq, count))
kono
parents: 67
diff changeset
2145 {
kono
parents: 67
diff changeset
2146 emit_case_decision_tree (stmt, index_expr, index_type, case_list,
kono
parents: 67
diff changeset
2147 default_bb, default_label, default_prob,
kono
parents: 67
diff changeset
2148 &phi_mapping);
kono
parents: 67
diff changeset
2149 return true;
kono
parents: 67
diff changeset
2150 }
kono
parents: 67
diff changeset
2151
kono
parents: 67
diff changeset
2152 return false;
kono
parents: 67
diff changeset
2153 }
kono
parents: 67
diff changeset
2154
kono
parents: 67
diff changeset
2155 /* The main function of the pass scans statements for switches and invokes
kono
parents: 67
diff changeset
2156 process_switch on them. */
kono
parents: 67
diff changeset
2157
kono
parents: 67
diff changeset
2158 namespace {
kono
parents: 67
diff changeset
2159
kono
parents: 67
diff changeset
2160 const pass_data pass_data_lower_switch =
kono
parents: 67
diff changeset
2161 {
kono
parents: 67
diff changeset
2162 GIMPLE_PASS, /* type */
kono
parents: 67
diff changeset
2163 "switchlower", /* name */
kono
parents: 67
diff changeset
2164 OPTGROUP_NONE, /* optinfo_flags */
kono
parents: 67
diff changeset
2165 TV_TREE_SWITCH_LOWERING, /* tv_id */
kono
parents: 67
diff changeset
2166 ( PROP_cfg | PROP_ssa ), /* properties_required */
kono
parents: 67
diff changeset
2167 0, /* properties_provided */
kono
parents: 67
diff changeset
2168 0, /* properties_destroyed */
kono
parents: 67
diff changeset
2169 0, /* todo_flags_start */
kono
parents: 67
diff changeset
2170 TODO_update_ssa | TODO_cleanup_cfg, /* todo_flags_finish */
kono
parents: 67
diff changeset
2171 };
kono
parents: 67
diff changeset
2172
kono
parents: 67
diff changeset
2173 class pass_lower_switch : public gimple_opt_pass
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2174 {
111
kono
parents: 67
diff changeset
2175 public:
kono
parents: 67
diff changeset
2176 pass_lower_switch (gcc::context *ctxt)
kono
parents: 67
diff changeset
2177 : gimple_opt_pass (pass_data_lower_switch, ctxt)
kono
parents: 67
diff changeset
2178 {}
kono
parents: 67
diff changeset
2179
kono
parents: 67
diff changeset
2180 /* opt_pass methods: */
kono
parents: 67
diff changeset
2181 virtual bool gate (function *) { return true; }
kono
parents: 67
diff changeset
2182 virtual unsigned int execute (function *);
kono
parents: 67
diff changeset
2183
kono
parents: 67
diff changeset
2184 }; // class pass_lower_switch
kono
parents: 67
diff changeset
2185
kono
parents: 67
diff changeset
2186 unsigned int
kono
parents: 67
diff changeset
2187 pass_lower_switch::execute (function *fun)
kono
parents: 67
diff changeset
2188 {
kono
parents: 67
diff changeset
2189 basic_block bb;
kono
parents: 67
diff changeset
2190 bool expanded = false;
kono
parents: 67
diff changeset
2191
kono
parents: 67
diff changeset
2192 FOR_EACH_BB_FN (bb, fun)
kono
parents: 67
diff changeset
2193 {
kono
parents: 67
diff changeset
2194 gimple *stmt = last_stmt (bb);
kono
parents: 67
diff changeset
2195 if (stmt && gimple_code (stmt) == GIMPLE_SWITCH)
kono
parents: 67
diff changeset
2196 {
kono
parents: 67
diff changeset
2197 if (dump_file)
kono
parents: 67
diff changeset
2198 {
kono
parents: 67
diff changeset
2199 expanded_location loc = expand_location (gimple_location (stmt));
kono
parents: 67
diff changeset
2200
kono
parents: 67
diff changeset
2201 fprintf (dump_file, "beginning to process the following "
kono
parents: 67
diff changeset
2202 "SWITCH statement (%s:%d) : ------- \n",
kono
parents: 67
diff changeset
2203 loc.file, loc.line);
kono
parents: 67
diff changeset
2204 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
kono
parents: 67
diff changeset
2205 putc ('\n', dump_file);
kono
parents: 67
diff changeset
2206 }
kono
parents: 67
diff changeset
2207
kono
parents: 67
diff changeset
2208 expanded |= try_switch_expansion (as_a<gswitch *> (stmt));
kono
parents: 67
diff changeset
2209 }
kono
parents: 67
diff changeset
2210 }
kono
parents: 67
diff changeset
2211
kono
parents: 67
diff changeset
2212 if (expanded)
kono
parents: 67
diff changeset
2213 {
kono
parents: 67
diff changeset
2214 free_dominance_info (CDI_DOMINATORS);
kono
parents: 67
diff changeset
2215 free_dominance_info (CDI_POST_DOMINATORS);
kono
parents: 67
diff changeset
2216 mark_virtual_operands_for_renaming (cfun);
kono
parents: 67
diff changeset
2217 }
kono
parents: 67
diff changeset
2218
kono
parents: 67
diff changeset
2219 return 0;
kono
parents: 67
diff changeset
2220 }
kono
parents: 67
diff changeset
2221
kono
parents: 67
diff changeset
2222 } // anon namespace
kono
parents: 67
diff changeset
2223
kono
parents: 67
diff changeset
2224 gimple_opt_pass *
kono
parents: 67
diff changeset
2225 make_pass_lower_switch (gcc::context *ctxt)
kono
parents: 67
diff changeset
2226 {
kono
parents: 67
diff changeset
2227 return new pass_lower_switch (ctxt);
kono
parents: 67
diff changeset
2228 }
kono
parents: 67
diff changeset
2229
kono
parents: 67
diff changeset
2230 /* Generate code to jump to LABEL if OP0 and OP1 are equal in mode MODE.
kono
parents: 67
diff changeset
2231 PROB is the probability of jumping to LABEL. */
kono
parents: 67
diff changeset
2232 static basic_block
kono
parents: 67
diff changeset
2233 do_jump_if_equal (basic_block bb, tree op0, tree op1, basic_block label_bb,
kono
parents: 67
diff changeset
2234 profile_probability prob, hash_map<tree, tree> *phi_mapping)
kono
parents: 67
diff changeset
2235 {
kono
parents: 67
diff changeset
2236 gcond *cond = gimple_build_cond (EQ_EXPR, op0, op1, NULL_TREE, NULL_TREE);
kono
parents: 67
diff changeset
2237 gimple_stmt_iterator gsi = gsi_last_bb (bb);
kono
parents: 67
diff changeset
2238 gsi_insert_before (&gsi, cond, GSI_SAME_STMT);
kono
parents: 67
diff changeset
2239
kono
parents: 67
diff changeset
2240 gcc_assert (single_succ_p (bb));
kono
parents: 67
diff changeset
2241
kono
parents: 67
diff changeset
2242 /* Make a new basic block where false branch will take place. */
kono
parents: 67
diff changeset
2243 edge false_edge = split_block (bb, cond);
kono
parents: 67
diff changeset
2244 false_edge->flags = EDGE_FALSE_VALUE;
kono
parents: 67
diff changeset
2245 false_edge->probability = prob.invert ();
kono
parents: 67
diff changeset
2246
kono
parents: 67
diff changeset
2247 edge true_edge = make_edge (bb, label_bb, EDGE_TRUE_VALUE);
kono
parents: 67
diff changeset
2248 fix_phi_operands_for_edge (true_edge, phi_mapping);
kono
parents: 67
diff changeset
2249 true_edge->probability = prob;
kono
parents: 67
diff changeset
2250
kono
parents: 67
diff changeset
2251 return false_edge->dest;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2252 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2253
111
kono
parents: 67
diff changeset
2254 /* Generate code to compare X with Y so that the condition codes are
kono
parents: 67
diff changeset
2255 set and to jump to LABEL if the condition is true. If X is a
kono
parents: 67
diff changeset
2256 constant and Y is not a constant, then the comparison is swapped to
kono
parents: 67
diff changeset
2257 ensure that the comparison RTL has the canonical form.
kono
parents: 67
diff changeset
2258
kono
parents: 67
diff changeset
2259 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
kono
parents: 67
diff changeset
2260 need to be widened. UNSIGNEDP is also used to select the proper
kono
parents: 67
diff changeset
2261 branch condition code.
kono
parents: 67
diff changeset
2262
kono
parents: 67
diff changeset
2263 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
kono
parents: 67
diff changeset
2264
kono
parents: 67
diff changeset
2265 MODE is the mode of the inputs (in case they are const_int).
kono
parents: 67
diff changeset
2266
kono
parents: 67
diff changeset
2267 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
kono
parents: 67
diff changeset
2268 It will be potentially converted into an unsigned variant based on
kono
parents: 67
diff changeset
2269 UNSIGNEDP to select a proper jump instruction.
kono
parents: 67
diff changeset
2270
kono
parents: 67
diff changeset
2271 PROB is the probability of jumping to LABEL. */
kono
parents: 67
diff changeset
2272
kono
parents: 67
diff changeset
2273 static basic_block
kono
parents: 67
diff changeset
2274 emit_cmp_and_jump_insns (basic_block bb, tree op0, tree op1,
kono
parents: 67
diff changeset
2275 tree_code comparison, basic_block label_bb,
kono
parents: 67
diff changeset
2276 profile_probability prob,
kono
parents: 67
diff changeset
2277 hash_map<tree, tree> *phi_mapping)
kono
parents: 67
diff changeset
2278 {
kono
parents: 67
diff changeset
2279 gcond *cond = gimple_build_cond (comparison, op0, op1, NULL_TREE, NULL_TREE);
kono
parents: 67
diff changeset
2280 gimple_stmt_iterator gsi = gsi_last_bb (bb);
kono
parents: 67
diff changeset
2281 gsi_insert_after (&gsi, cond, GSI_NEW_STMT);
kono
parents: 67
diff changeset
2282
kono
parents: 67
diff changeset
2283 gcc_assert (single_succ_p (bb));
kono
parents: 67
diff changeset
2284
kono
parents: 67
diff changeset
2285 /* Make a new basic block where false branch will take place. */
kono
parents: 67
diff changeset
2286 edge false_edge = split_block (bb, cond);
kono
parents: 67
diff changeset
2287 false_edge->flags = EDGE_FALSE_VALUE;
kono
parents: 67
diff changeset
2288 false_edge->probability = prob.invert ();
kono
parents: 67
diff changeset
2289
kono
parents: 67
diff changeset
2290 edge true_edge = make_edge (bb, label_bb, EDGE_TRUE_VALUE);
kono
parents: 67
diff changeset
2291 fix_phi_operands_for_edge (true_edge, phi_mapping);
kono
parents: 67
diff changeset
2292 true_edge->probability = prob;
kono
parents: 67
diff changeset
2293
kono
parents: 67
diff changeset
2294 return false_edge->dest;
kono
parents: 67
diff changeset
2295 }
kono
parents: 67
diff changeset
2296
kono
parents: 67
diff changeset
2297 /* Computes the conditional probability of jumping to a target if the branch
kono
parents: 67
diff changeset
2298 instruction is executed.
kono
parents: 67
diff changeset
2299 TARGET_PROB is the estimated probability of jumping to a target relative
kono
parents: 67
diff changeset
2300 to some basic block BB.
kono
parents: 67
diff changeset
2301 BASE_PROB is the probability of reaching the branch instruction relative
kono
parents: 67
diff changeset
2302 to the same basic block BB. */
kono
parents: 67
diff changeset
2303
kono
parents: 67
diff changeset
2304 static inline profile_probability
kono
parents: 67
diff changeset
2305 conditional_probability (profile_probability target_prob,
kono
parents: 67
diff changeset
2306 profile_probability base_prob)
kono
parents: 67
diff changeset
2307 {
kono
parents: 67
diff changeset
2308 return target_prob / base_prob;
kono
parents: 67
diff changeset
2309 }
kono
parents: 67
diff changeset
2310
kono
parents: 67
diff changeset
2311 /* Emit step-by-step code to select a case for the value of INDEX.
kono
parents: 67
diff changeset
2312 The thus generated decision tree follows the form of the
kono
parents: 67
diff changeset
2313 case-node binary tree NODE, whose nodes represent test conditions.
kono
parents: 67
diff changeset
2314 INDEX_TYPE is the type of the index of the switch.
kono
parents: 67
diff changeset
2315
kono
parents: 67
diff changeset
2316 Care is taken to prune redundant tests from the decision tree
kono
parents: 67
diff changeset
2317 by detecting any boundary conditions already checked by
kono
parents: 67
diff changeset
2318 emitted rtx. (See node_has_high_bound, node_has_low_bound
kono
parents: 67
diff changeset
2319 and node_is_bounded, above.)
kono
parents: 67
diff changeset
2320
kono
parents: 67
diff changeset
2321 Where the test conditions can be shown to be redundant we emit
kono
parents: 67
diff changeset
2322 an unconditional jump to the target code. As a further
kono
parents: 67
diff changeset
2323 optimization, the subordinates of a tree node are examined to
kono
parents: 67
diff changeset
2324 check for bounded nodes. In this case conditional and/or
kono
parents: 67
diff changeset
2325 unconditional jumps as a result of the boundary check for the
kono
parents: 67
diff changeset
2326 current node are arranged to target the subordinates associated
kono
parents: 67
diff changeset
2327 code for out of bound conditions on the current node.
kono
parents: 67
diff changeset
2328
kono
parents: 67
diff changeset
2329 We can assume that when control reaches the code generated here,
kono
parents: 67
diff changeset
2330 the index value has already been compared with the parents
kono
parents: 67
diff changeset
2331 of this node, and determined to be on the same side of each parent
kono
parents: 67
diff changeset
2332 as this node is. Thus, if this node tests for the value 51,
kono
parents: 67
diff changeset
2333 and a parent tested for 52, we don't need to consider
kono
parents: 67
diff changeset
2334 the possibility of a value greater than 51. If another parent
kono
parents: 67
diff changeset
2335 tests for the value 50, then this node need not test anything. */
kono
parents: 67
diff changeset
2336
kono
parents: 67
diff changeset
2337 static basic_block
kono
parents: 67
diff changeset
2338 emit_case_nodes (basic_block bb, tree index, case_node_ptr node,
kono
parents: 67
diff changeset
2339 basic_block default_bb, tree default_label,
kono
parents: 67
diff changeset
2340 profile_probability default_prob, tree index_type,
kono
parents: 67
diff changeset
2341 hash_map<tree, tree> *phi_mapping)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2342 {
111
kono
parents: 67
diff changeset
2343 /* If INDEX has an unsigned type, we must make unsigned branches. */
kono
parents: 67
diff changeset
2344 profile_probability probability;
kono
parents: 67
diff changeset
2345 profile_probability prob = node->prob, subtree_prob = node->subtree_prob;
kono
parents: 67
diff changeset
2346
kono
parents: 67
diff changeset
2347 /* See if our parents have already tested everything for us.
kono
parents: 67
diff changeset
2348 If they have, emit an unconditional jump for this node. */
kono
parents: 67
diff changeset
2349 if (node_is_bounded (node, index_type))
kono
parents: 67
diff changeset
2350 {
kono
parents: 67
diff changeset
2351 emit_jump (bb, node->case_bb, phi_mapping);
kono
parents: 67
diff changeset
2352 return NULL;
kono
parents: 67
diff changeset
2353 }
kono
parents: 67
diff changeset
2354
kono
parents: 67
diff changeset
2355 else if (tree_int_cst_equal (node->low, node->high))
kono
parents: 67
diff changeset
2356 {
kono
parents: 67
diff changeset
2357 probability = conditional_probability (prob, subtree_prob + default_prob);
kono
parents: 67
diff changeset
2358 /* Node is single valued. First see if the index expression matches
kono
parents: 67
diff changeset
2359 this node and then check our children, if any. */
kono
parents: 67
diff changeset
2360 bb = do_jump_if_equal (bb, index, node->low, node->case_bb, probability,
kono
parents: 67
diff changeset
2361 phi_mapping);
kono
parents: 67
diff changeset
2362 /* Since this case is taken at this point, reduce its weight from
kono
parents: 67
diff changeset
2363 subtree_weight. */
kono
parents: 67
diff changeset
2364 subtree_prob -= prob;
kono
parents: 67
diff changeset
2365 if (node->right != 0 && node->left != 0)
kono
parents: 67
diff changeset
2366 {
kono
parents: 67
diff changeset
2367 /* This node has children on both sides.
kono
parents: 67
diff changeset
2368 Dispatch to one side or the other
kono
parents: 67
diff changeset
2369 by comparing the index value with this node's value.
kono
parents: 67
diff changeset
2370 If one subtree is bounded, check that one first,
kono
parents: 67
diff changeset
2371 so we can avoid real branches in the tree. */
kono
parents: 67
diff changeset
2372
kono
parents: 67
diff changeset
2373 if (node_is_bounded (node->right, index_type))
kono
parents: 67
diff changeset
2374 {
kono
parents: 67
diff changeset
2375 probability
kono
parents: 67
diff changeset
2376 = conditional_probability (node->right->prob,
kono
parents: 67
diff changeset
2377 subtree_prob + default_prob);
kono
parents: 67
diff changeset
2378 bb = emit_cmp_and_jump_insns (bb, index, node->high, GT_EXPR,
kono
parents: 67
diff changeset
2379 node->right->case_bb, probability,
kono
parents: 67
diff changeset
2380 phi_mapping);
kono
parents: 67
diff changeset
2381 bb = emit_case_nodes (bb, index, node->left, default_bb,
kono
parents: 67
diff changeset
2382 default_label, default_prob, index_type,
kono
parents: 67
diff changeset
2383 phi_mapping);
kono
parents: 67
diff changeset
2384 }
kono
parents: 67
diff changeset
2385
kono
parents: 67
diff changeset
2386 else if (node_is_bounded (node->left, index_type))
kono
parents: 67
diff changeset
2387 {
kono
parents: 67
diff changeset
2388 probability
kono
parents: 67
diff changeset
2389 = conditional_probability (node->left->prob,
kono
parents: 67
diff changeset
2390 subtree_prob + default_prob);
kono
parents: 67
diff changeset
2391 bb = emit_cmp_and_jump_insns (bb, index, node->high, LT_EXPR,
kono
parents: 67
diff changeset
2392 node->left->case_bb, probability,
kono
parents: 67
diff changeset
2393 phi_mapping);
kono
parents: 67
diff changeset
2394 bb = emit_case_nodes (bb, index, node->right, default_bb,
kono
parents: 67
diff changeset
2395 default_label, default_prob, index_type,
kono
parents: 67
diff changeset
2396 phi_mapping);
kono
parents: 67
diff changeset
2397 }
kono
parents: 67
diff changeset
2398
kono
parents: 67
diff changeset
2399 /* If both children are single-valued cases with no
kono
parents: 67
diff changeset
2400 children, finish up all the work. This way, we can save
kono
parents: 67
diff changeset
2401 one ordered comparison. */
kono
parents: 67
diff changeset
2402 else if (tree_int_cst_equal (node->right->low, node->right->high)
kono
parents: 67
diff changeset
2403 && node->right->left == 0 && node->right->right == 0
kono
parents: 67
diff changeset
2404 && tree_int_cst_equal (node->left->low, node->left->high)
kono
parents: 67
diff changeset
2405 && node->left->left == 0 && node->left->right == 0)
kono
parents: 67
diff changeset
2406 {
kono
parents: 67
diff changeset
2407 /* Neither node is bounded. First distinguish the two sides;
kono
parents: 67
diff changeset
2408 then emit the code for one side at a time. */
kono
parents: 67
diff changeset
2409
kono
parents: 67
diff changeset
2410 /* See if the value matches what the right hand side
kono
parents: 67
diff changeset
2411 wants. */
kono
parents: 67
diff changeset
2412 probability
kono
parents: 67
diff changeset
2413 = conditional_probability (node->right->prob,
kono
parents: 67
diff changeset
2414 subtree_prob + default_prob);
kono
parents: 67
diff changeset
2415 bb = do_jump_if_equal (bb, index, node->right->low,
kono
parents: 67
diff changeset
2416 node->right->case_bb, probability,
kono
parents: 67
diff changeset
2417 phi_mapping);
kono
parents: 67
diff changeset
2418
kono
parents: 67
diff changeset
2419 /* See if the value matches what the left hand side
kono
parents: 67
diff changeset
2420 wants. */
kono
parents: 67
diff changeset
2421 probability
kono
parents: 67
diff changeset
2422 = conditional_probability (node->left->prob,
kono
parents: 67
diff changeset
2423 subtree_prob + default_prob);
kono
parents: 67
diff changeset
2424 bb = do_jump_if_equal (bb, index, node->left->low,
kono
parents: 67
diff changeset
2425 node->left->case_bb, probability,
kono
parents: 67
diff changeset
2426 phi_mapping);
kono
parents: 67
diff changeset
2427 }
kono
parents: 67
diff changeset
2428
kono
parents: 67
diff changeset
2429 else
kono
parents: 67
diff changeset
2430 {
kono
parents: 67
diff changeset
2431 /* Neither node is bounded. First distinguish the two sides;
kono
parents: 67
diff changeset
2432 then emit the code for one side at a time. */
kono
parents: 67
diff changeset
2433
kono
parents: 67
diff changeset
2434 basic_block test_bb = split_edge (single_succ_edge (bb));
kono
parents: 67
diff changeset
2435 redirect_edge_succ (single_pred_edge (test_bb),
kono
parents: 67
diff changeset
2436 single_succ_edge (bb)->dest);
kono
parents: 67
diff changeset
2437
kono
parents: 67
diff changeset
2438 /* The default label could be reached either through the right
kono
parents: 67
diff changeset
2439 subtree or the left subtree. Divide the probability
kono
parents: 67
diff changeset
2440 equally. */
kono
parents: 67
diff changeset
2441 probability
kono
parents: 67
diff changeset
2442 = conditional_probability (node->right->subtree_prob
kono
parents: 67
diff changeset
2443 + default_prob.apply_scale (1, 2),
kono
parents: 67
diff changeset
2444 subtree_prob + default_prob);
kono
parents: 67
diff changeset
2445 /* See if the value is on the right. */
kono
parents: 67
diff changeset
2446 bb = emit_cmp_and_jump_insns (bb, index, node->high, GT_EXPR,
kono
parents: 67
diff changeset
2447 test_bb, probability, phi_mapping);
kono
parents: 67
diff changeset
2448 default_prob = default_prob.apply_scale (1, 2);
kono
parents: 67
diff changeset
2449
kono
parents: 67
diff changeset
2450 /* Value must be on the left.
kono
parents: 67
diff changeset
2451 Handle the left-hand subtree. */
kono
parents: 67
diff changeset
2452 bb = emit_case_nodes (bb, index, node->left, default_bb,
kono
parents: 67
diff changeset
2453 default_label, default_prob, index_type,
kono
parents: 67
diff changeset
2454 phi_mapping);
kono
parents: 67
diff changeset
2455 /* If left-hand subtree does nothing,
kono
parents: 67
diff changeset
2456 go to default. */
kono
parents: 67
diff changeset
2457
kono
parents: 67
diff changeset
2458 if (bb && default_bb)
kono
parents: 67
diff changeset
2459 emit_jump (bb, default_bb, phi_mapping);
kono
parents: 67
diff changeset
2460
kono
parents: 67
diff changeset
2461 /* Code branches here for the right-hand subtree. */
kono
parents: 67
diff changeset
2462 bb = emit_case_nodes (test_bb, index, node->right, default_bb,
kono
parents: 67
diff changeset
2463 default_label, default_prob, index_type,
kono
parents: 67
diff changeset
2464 phi_mapping);
kono
parents: 67
diff changeset
2465 }
kono
parents: 67
diff changeset
2466 }
kono
parents: 67
diff changeset
2467 else if (node->right != 0 && node->left == 0)
kono
parents: 67
diff changeset
2468 {
kono
parents: 67
diff changeset
2469 /* Here we have a right child but no left so we issue a conditional
kono
parents: 67
diff changeset
2470 branch to default and process the right child.
kono
parents: 67
diff changeset
2471
kono
parents: 67
diff changeset
2472 Omit the conditional branch to default if the right child
kono
parents: 67
diff changeset
2473 does not have any children and is single valued; it would
kono
parents: 67
diff changeset
2474 cost too much space to save so little time. */
kono
parents: 67
diff changeset
2475
kono
parents: 67
diff changeset
2476 if (node->right->right || node->right->left
kono
parents: 67
diff changeset
2477 || !tree_int_cst_equal (node->right->low, node->right->high))
kono
parents: 67
diff changeset
2478 {
kono
parents: 67
diff changeset
2479 if (!node_has_low_bound (node, index_type))
kono
parents: 67
diff changeset
2480 {
kono
parents: 67
diff changeset
2481 probability
kono
parents: 67
diff changeset
2482 = conditional_probability (default_prob.apply_scale (1, 2),
kono
parents: 67
diff changeset
2483 subtree_prob + default_prob);
kono
parents: 67
diff changeset
2484 bb = emit_cmp_and_jump_insns (bb, index, node->high, LT_EXPR,
kono
parents: 67
diff changeset
2485 default_bb, probability,
kono
parents: 67
diff changeset
2486 phi_mapping);
kono
parents: 67
diff changeset
2487 default_prob = default_prob.apply_scale (1, 2);
kono
parents: 67
diff changeset
2488 }
kono
parents: 67
diff changeset
2489
kono
parents: 67
diff changeset
2490 bb = emit_case_nodes (bb, index, node->right, default_bb,
kono
parents: 67
diff changeset
2491 default_label, default_prob, index_type,
kono
parents: 67
diff changeset
2492 phi_mapping);
kono
parents: 67
diff changeset
2493 }
kono
parents: 67
diff changeset
2494 else
kono
parents: 67
diff changeset
2495 {
kono
parents: 67
diff changeset
2496 probability
kono
parents: 67
diff changeset
2497 = conditional_probability (node->right->subtree_prob,
kono
parents: 67
diff changeset
2498 subtree_prob + default_prob);
kono
parents: 67
diff changeset
2499 /* We cannot process node->right normally
kono
parents: 67
diff changeset
2500 since we haven't ruled out the numbers less than
kono
parents: 67
diff changeset
2501 this node's value. So handle node->right explicitly. */
kono
parents: 67
diff changeset
2502 bb = do_jump_if_equal (bb, index, node->right->low,
kono
parents: 67
diff changeset
2503 node->right->case_bb, probability,
kono
parents: 67
diff changeset
2504 phi_mapping);
kono
parents: 67
diff changeset
2505 }
kono
parents: 67
diff changeset
2506 }
kono
parents: 67
diff changeset
2507
kono
parents: 67
diff changeset
2508 else if (node->right == 0 && node->left != 0)
kono
parents: 67
diff changeset
2509 {
kono
parents: 67
diff changeset
2510 /* Just one subtree, on the left. */
kono
parents: 67
diff changeset
2511 if (node->left->left || node->left->right
kono
parents: 67
diff changeset
2512 || !tree_int_cst_equal (node->left->low, node->left->high))
kono
parents: 67
diff changeset
2513 {
kono
parents: 67
diff changeset
2514 if (!node_has_high_bound (node, index_type))
kono
parents: 67
diff changeset
2515 {
kono
parents: 67
diff changeset
2516 probability
kono
parents: 67
diff changeset
2517 = conditional_probability (default_prob.apply_scale (1, 2),
kono
parents: 67
diff changeset
2518 subtree_prob + default_prob);
kono
parents: 67
diff changeset
2519 bb = emit_cmp_and_jump_insns (bb, index, node->high, GT_EXPR,
kono
parents: 67
diff changeset
2520 default_bb, probability,
kono
parents: 67
diff changeset
2521 phi_mapping);
kono
parents: 67
diff changeset
2522 default_prob = default_prob.apply_scale (1, 2);
kono
parents: 67
diff changeset
2523 }
kono
parents: 67
diff changeset
2524
kono
parents: 67
diff changeset
2525 bb = emit_case_nodes (bb, index, node->left, default_bb,
kono
parents: 67
diff changeset
2526 default_label, default_prob, index_type,
kono
parents: 67
diff changeset
2527 phi_mapping);
kono
parents: 67
diff changeset
2528 }
kono
parents: 67
diff changeset
2529 else
kono
parents: 67
diff changeset
2530 {
kono
parents: 67
diff changeset
2531 probability
kono
parents: 67
diff changeset
2532 = conditional_probability (node->left->subtree_prob,
kono
parents: 67
diff changeset
2533 subtree_prob + default_prob);
kono
parents: 67
diff changeset
2534 /* We cannot process node->left normally
kono
parents: 67
diff changeset
2535 since we haven't ruled out the numbers less than
kono
parents: 67
diff changeset
2536 this node's value. So handle node->left explicitly. */
kono
parents: 67
diff changeset
2537 do_jump_if_equal (bb, index, node->left->low, node->left->case_bb,
kono
parents: 67
diff changeset
2538 probability, phi_mapping);
kono
parents: 67
diff changeset
2539 }
kono
parents: 67
diff changeset
2540 }
kono
parents: 67
diff changeset
2541 }
kono
parents: 67
diff changeset
2542 else
kono
parents: 67
diff changeset
2543 {
kono
parents: 67
diff changeset
2544 /* Node is a range. These cases are very similar to those for a single
kono
parents: 67
diff changeset
2545 value, except that we do not start by testing whether this node
kono
parents: 67
diff changeset
2546 is the one to branch to. */
kono
parents: 67
diff changeset
2547
kono
parents: 67
diff changeset
2548 if (node->right != 0 && node->left != 0)
kono
parents: 67
diff changeset
2549 {
kono
parents: 67
diff changeset
2550 /* Node has subtrees on both sides.
kono
parents: 67
diff changeset
2551 If the right-hand subtree is bounded,
kono
parents: 67
diff changeset
2552 test for it first, since we can go straight there.
kono
parents: 67
diff changeset
2553 Otherwise, we need to make a branch in the control structure,
kono
parents: 67
diff changeset
2554 then handle the two subtrees. */
kono
parents: 67
diff changeset
2555 basic_block test_bb = NULL;
kono
parents: 67
diff changeset
2556
kono
parents: 67
diff changeset
2557 if (node_is_bounded (node->right, index_type))
kono
parents: 67
diff changeset
2558 {
kono
parents: 67
diff changeset
2559 /* Right hand node is fully bounded so we can eliminate any
kono
parents: 67
diff changeset
2560 testing and branch directly to the target code. */
kono
parents: 67
diff changeset
2561 probability
kono
parents: 67
diff changeset
2562 = conditional_probability (node->right->subtree_prob,
kono
parents: 67
diff changeset
2563 subtree_prob + default_prob);
kono
parents: 67
diff changeset
2564 bb = emit_cmp_and_jump_insns (bb, index, node->high, GT_EXPR,
kono
parents: 67
diff changeset
2565 node->right->case_bb, probability,
kono
parents: 67
diff changeset
2566 phi_mapping);
kono
parents: 67
diff changeset
2567 }
kono
parents: 67
diff changeset
2568 else
kono
parents: 67
diff changeset
2569 {
kono
parents: 67
diff changeset
2570 /* Right hand node requires testing.
kono
parents: 67
diff changeset
2571 Branch to a label where we will handle it later. */
kono
parents: 67
diff changeset
2572
kono
parents: 67
diff changeset
2573 test_bb = split_edge (single_succ_edge (bb));
kono
parents: 67
diff changeset
2574 redirect_edge_succ (single_pred_edge (test_bb),
kono
parents: 67
diff changeset
2575 single_succ_edge (bb)->dest);
kono
parents: 67
diff changeset
2576
kono
parents: 67
diff changeset
2577 probability
kono
parents: 67
diff changeset
2578 = conditional_probability (node->right->subtree_prob
kono
parents: 67
diff changeset
2579 + default_prob.apply_scale (1, 2),
kono
parents: 67
diff changeset
2580 subtree_prob + default_prob);
kono
parents: 67
diff changeset
2581 bb = emit_cmp_and_jump_insns (bb, index, node->high, GT_EXPR,
kono
parents: 67
diff changeset
2582 test_bb, probability, phi_mapping);
kono
parents: 67
diff changeset
2583 default_prob = default_prob.apply_scale (1, 2);
kono
parents: 67
diff changeset
2584 }
kono
parents: 67
diff changeset
2585
kono
parents: 67
diff changeset
2586 /* Value belongs to this node or to the left-hand subtree. */
kono
parents: 67
diff changeset
2587
kono
parents: 67
diff changeset
2588 probability
kono
parents: 67
diff changeset
2589 = conditional_probability (prob, subtree_prob + default_prob);
kono
parents: 67
diff changeset
2590 bb = emit_cmp_and_jump_insns (bb, index, node->low, GE_EXPR,
kono
parents: 67
diff changeset
2591 node->case_bb, probability,
kono
parents: 67
diff changeset
2592 phi_mapping);
kono
parents: 67
diff changeset
2593
kono
parents: 67
diff changeset
2594 /* Handle the left-hand subtree. */
kono
parents: 67
diff changeset
2595 bb = emit_case_nodes (bb, index, node->left, default_bb,
kono
parents: 67
diff changeset
2596 default_label, default_prob, index_type,
kono
parents: 67
diff changeset
2597 phi_mapping);
kono
parents: 67
diff changeset
2598
kono
parents: 67
diff changeset
2599 /* If right node had to be handled later, do that now. */
kono
parents: 67
diff changeset
2600 if (test_bb)
kono
parents: 67
diff changeset
2601 {
kono
parents: 67
diff changeset
2602 /* If the left-hand subtree fell through,
kono
parents: 67
diff changeset
2603 don't let it fall into the right-hand subtree. */
kono
parents: 67
diff changeset
2604 if (bb && default_bb)
kono
parents: 67
diff changeset
2605 emit_jump (bb, default_bb, phi_mapping);
kono
parents: 67
diff changeset
2606
kono
parents: 67
diff changeset
2607 bb = emit_case_nodes (test_bb, index, node->right, default_bb,
kono
parents: 67
diff changeset
2608 default_label, default_prob, index_type,
kono
parents: 67
diff changeset
2609 phi_mapping);
kono
parents: 67
diff changeset
2610 }
kono
parents: 67
diff changeset
2611 }
kono
parents: 67
diff changeset
2612
kono
parents: 67
diff changeset
2613 else if (node->right != 0 && node->left == 0)
kono
parents: 67
diff changeset
2614 {
kono
parents: 67
diff changeset
2615 /* Deal with values to the left of this node,
kono
parents: 67
diff changeset
2616 if they are possible. */
kono
parents: 67
diff changeset
2617 if (!node_has_low_bound (node, index_type))
kono
parents: 67
diff changeset
2618 {
kono
parents: 67
diff changeset
2619 probability
kono
parents: 67
diff changeset
2620 = conditional_probability (default_prob.apply_scale (1, 2),
kono
parents: 67
diff changeset
2621 subtree_prob + default_prob);
kono
parents: 67
diff changeset
2622 bb = emit_cmp_and_jump_insns (bb, index, node->low, LT_EXPR,
kono
parents: 67
diff changeset
2623 default_bb, probability,
kono
parents: 67
diff changeset
2624 phi_mapping);
kono
parents: 67
diff changeset
2625 default_prob = default_prob.apply_scale (1, 2);
kono
parents: 67
diff changeset
2626 }
kono
parents: 67
diff changeset
2627
kono
parents: 67
diff changeset
2628 /* Value belongs to this node or to the right-hand subtree. */
kono
parents: 67
diff changeset
2629
kono
parents: 67
diff changeset
2630 probability
kono
parents: 67
diff changeset
2631 = conditional_probability (prob, subtree_prob + default_prob);
kono
parents: 67
diff changeset
2632 bb = emit_cmp_and_jump_insns (bb, index, node->high, LE_EXPR,
kono
parents: 67
diff changeset
2633 node->case_bb, probability,
kono
parents: 67
diff changeset
2634 phi_mapping);
kono
parents: 67
diff changeset
2635
kono
parents: 67
diff changeset
2636 bb = emit_case_nodes (bb, index, node->right, default_bb,
kono
parents: 67
diff changeset
2637 default_label, default_prob, index_type,
kono
parents: 67
diff changeset
2638 phi_mapping);
kono
parents: 67
diff changeset
2639 }
kono
parents: 67
diff changeset
2640
kono
parents: 67
diff changeset
2641 else if (node->right == 0 && node->left != 0)
kono
parents: 67
diff changeset
2642 {
kono
parents: 67
diff changeset
2643 /* Deal with values to the right of this node,
kono
parents: 67
diff changeset
2644 if they are possible. */
kono
parents: 67
diff changeset
2645 if (!node_has_high_bound (node, index_type))
kono
parents: 67
diff changeset
2646 {
kono
parents: 67
diff changeset
2647 probability
kono
parents: 67
diff changeset
2648 = conditional_probability (default_prob.apply_scale (1, 2),
kono
parents: 67
diff changeset
2649 subtree_prob + default_prob);
kono
parents: 67
diff changeset
2650 bb = emit_cmp_and_jump_insns (bb, index, node->high, GT_EXPR,
kono
parents: 67
diff changeset
2651 default_bb, probability,
kono
parents: 67
diff changeset
2652 phi_mapping);
kono
parents: 67
diff changeset
2653 default_prob = default_prob.apply_scale (1, 2);
kono
parents: 67
diff changeset
2654 }
kono
parents: 67
diff changeset
2655
kono
parents: 67
diff changeset
2656 /* Value belongs to this node or to the left-hand subtree. */
kono
parents: 67
diff changeset
2657
kono
parents: 67
diff changeset
2658 probability
kono
parents: 67
diff changeset
2659 = conditional_probability (prob, subtree_prob + default_prob);
kono
parents: 67
diff changeset
2660 bb = emit_cmp_and_jump_insns (bb, index, node->low, GE_EXPR,
kono
parents: 67
diff changeset
2661 node->case_bb, probability,
kono
parents: 67
diff changeset
2662 phi_mapping);
kono
parents: 67
diff changeset
2663
kono
parents: 67
diff changeset
2664 bb = emit_case_nodes (bb, index, node->left, default_bb,
kono
parents: 67
diff changeset
2665 default_label, default_prob, index_type,
kono
parents: 67
diff changeset
2666 phi_mapping);
kono
parents: 67
diff changeset
2667 }
kono
parents: 67
diff changeset
2668
kono
parents: 67
diff changeset
2669 else
kono
parents: 67
diff changeset
2670 {
kono
parents: 67
diff changeset
2671 /* Node has no children so we check low and high bounds to remove
kono
parents: 67
diff changeset
2672 redundant tests. Only one of the bounds can exist,
kono
parents: 67
diff changeset
2673 since otherwise this node is bounded--a case tested already. */
kono
parents: 67
diff changeset
2674 bool high_bound = node_has_high_bound (node, index_type);
kono
parents: 67
diff changeset
2675 bool low_bound = node_has_low_bound (node, index_type);
kono
parents: 67
diff changeset
2676
kono
parents: 67
diff changeset
2677 if (!high_bound && low_bound)
kono
parents: 67
diff changeset
2678 {
kono
parents: 67
diff changeset
2679 probability
kono
parents: 67
diff changeset
2680 = conditional_probability (default_prob,
kono
parents: 67
diff changeset
2681 subtree_prob + default_prob);
kono
parents: 67
diff changeset
2682 bb = emit_cmp_and_jump_insns (bb, index, node->high, GT_EXPR,
kono
parents: 67
diff changeset
2683 default_bb, probability,
kono
parents: 67
diff changeset
2684 phi_mapping);
kono
parents: 67
diff changeset
2685 }
kono
parents: 67
diff changeset
2686
kono
parents: 67
diff changeset
2687 else if (!low_bound && high_bound)
kono
parents: 67
diff changeset
2688 {
kono
parents: 67
diff changeset
2689 probability
kono
parents: 67
diff changeset
2690 = conditional_probability (default_prob,
kono
parents: 67
diff changeset
2691 subtree_prob + default_prob);
kono
parents: 67
diff changeset
2692 bb = emit_cmp_and_jump_insns (bb, index, node->low, LT_EXPR,
kono
parents: 67
diff changeset
2693 default_bb, probability,
kono
parents: 67
diff changeset
2694 phi_mapping);
kono
parents: 67
diff changeset
2695 }
kono
parents: 67
diff changeset
2696 else if (!low_bound && !high_bound)
kono
parents: 67
diff changeset
2697 {
kono
parents: 67
diff changeset
2698 tree lhs, rhs;
kono
parents: 67
diff changeset
2699 generate_range_test (bb, index, node->low, node->high,
kono
parents: 67
diff changeset
2700 &lhs, &rhs);
kono
parents: 67
diff changeset
2701 probability
kono
parents: 67
diff changeset
2702 = conditional_probability (default_prob,
kono
parents: 67
diff changeset
2703 subtree_prob + default_prob);
kono
parents: 67
diff changeset
2704 bb = emit_cmp_and_jump_insns (bb, lhs, rhs, GT_EXPR,
kono
parents: 67
diff changeset
2705 default_bb, probability,
kono
parents: 67
diff changeset
2706 phi_mapping);
kono
parents: 67
diff changeset
2707 }
kono
parents: 67
diff changeset
2708
kono
parents: 67
diff changeset
2709 emit_jump (bb, node->case_bb, phi_mapping);
kono
parents: 67
diff changeset
2710 return NULL;
kono
parents: 67
diff changeset
2711 }
kono
parents: 67
diff changeset
2712 }
kono
parents: 67
diff changeset
2713
kono
parents: 67
diff changeset
2714 return bb;
kono
parents: 67
diff changeset
2715 }
kono
parents: 67
diff changeset
2716
kono
parents: 67
diff changeset
2717 /* Search the parent sections of the case node tree
kono
parents: 67
diff changeset
2718 to see if a test for the lower bound of NODE would be redundant.
kono
parents: 67
diff changeset
2719 INDEX_TYPE is the type of the index expression.
kono
parents: 67
diff changeset
2720
kono
parents: 67
diff changeset
2721 The instructions to generate the case decision tree are
kono
parents: 67
diff changeset
2722 output in the same order as nodes are processed so it is
kono
parents: 67
diff changeset
2723 known that if a parent node checks the range of the current
kono
parents: 67
diff changeset
2724 node minus one that the current node is bounded at its lower
kono
parents: 67
diff changeset
2725 span. Thus the test would be redundant. */
kono
parents: 67
diff changeset
2726
kono
parents: 67
diff changeset
2727 static bool
kono
parents: 67
diff changeset
2728 node_has_low_bound (case_node_ptr node, tree index_type)
kono
parents: 67
diff changeset
2729 {
kono
parents: 67
diff changeset
2730 tree low_minus_one;
kono
parents: 67
diff changeset
2731 case_node_ptr pnode;
kono
parents: 67
diff changeset
2732
kono
parents: 67
diff changeset
2733 /* If the lower bound of this node is the lowest value in the index type,
kono
parents: 67
diff changeset
2734 we need not test it. */
kono
parents: 67
diff changeset
2735
kono
parents: 67
diff changeset
2736 if (tree_int_cst_equal (node->low, TYPE_MIN_VALUE (index_type)))
kono
parents: 67
diff changeset
2737 return true;
kono
parents: 67
diff changeset
2738
kono
parents: 67
diff changeset
2739 /* If this node has a left branch, the value at the left must be less
kono
parents: 67
diff changeset
2740 than that at this node, so it cannot be bounded at the bottom and
kono
parents: 67
diff changeset
2741 we need not bother testing any further. */
kono
parents: 67
diff changeset
2742
kono
parents: 67
diff changeset
2743 if (node->left)
kono
parents: 67
diff changeset
2744 return false;
kono
parents: 67
diff changeset
2745
kono
parents: 67
diff changeset
2746 low_minus_one = fold_build2 (MINUS_EXPR, TREE_TYPE (node->low), node->low,
kono
parents: 67
diff changeset
2747 build_int_cst (TREE_TYPE (node->low), 1));
kono
parents: 67
diff changeset
2748
kono
parents: 67
diff changeset
2749 /* If the subtraction above overflowed, we can't verify anything.
kono
parents: 67
diff changeset
2750 Otherwise, look for a parent that tests our value - 1. */
kono
parents: 67
diff changeset
2751
kono
parents: 67
diff changeset
2752 if (!tree_int_cst_lt (low_minus_one, node->low))
kono
parents: 67
diff changeset
2753 return false;
kono
parents: 67
diff changeset
2754
kono
parents: 67
diff changeset
2755 for (pnode = node->parent; pnode; pnode = pnode->parent)
kono
parents: 67
diff changeset
2756 if (tree_int_cst_equal (low_minus_one, pnode->high))
kono
parents: 67
diff changeset
2757 return true;
kono
parents: 67
diff changeset
2758
kono
parents: 67
diff changeset
2759 return false;
kono
parents: 67
diff changeset
2760 }
kono
parents: 67
diff changeset
2761
kono
parents: 67
diff changeset
2762 /* Search the parent sections of the case node tree
kono
parents: 67
diff changeset
2763 to see if a test for the upper bound of NODE would be redundant.
kono
parents: 67
diff changeset
2764 INDEX_TYPE is the type of the index expression.
kono
parents: 67
diff changeset
2765
kono
parents: 67
diff changeset
2766 The instructions to generate the case decision tree are
kono
parents: 67
diff changeset
2767 output in the same order as nodes are processed so it is
kono
parents: 67
diff changeset
2768 known that if a parent node checks the range of the current
kono
parents: 67
diff changeset
2769 node plus one that the current node is bounded at its upper
kono
parents: 67
diff changeset
2770 span. Thus the test would be redundant. */
kono
parents: 67
diff changeset
2771
kono
parents: 67
diff changeset
2772 static bool
kono
parents: 67
diff changeset
2773 node_has_high_bound (case_node_ptr node, tree index_type)
kono
parents: 67
diff changeset
2774 {
kono
parents: 67
diff changeset
2775 tree high_plus_one;
kono
parents: 67
diff changeset
2776 case_node_ptr pnode;
kono
parents: 67
diff changeset
2777
kono
parents: 67
diff changeset
2778 /* If there is no upper bound, obviously no test is needed. */
kono
parents: 67
diff changeset
2779
kono
parents: 67
diff changeset
2780 if (TYPE_MAX_VALUE (index_type) == NULL)
kono
parents: 67
diff changeset
2781 return true;
kono
parents: 67
diff changeset
2782
kono
parents: 67
diff changeset
2783 /* If the upper bound of this node is the highest value in the type
kono
parents: 67
diff changeset
2784 of the index expression, we need not test against it. */
kono
parents: 67
diff changeset
2785
kono
parents: 67
diff changeset
2786 if (tree_int_cst_equal (node->high, TYPE_MAX_VALUE (index_type)))
kono
parents: 67
diff changeset
2787 return true;
kono
parents: 67
diff changeset
2788
kono
parents: 67
diff changeset
2789 /* If this node has a right branch, the value at the right must be greater
kono
parents: 67
diff changeset
2790 than that at this node, so it cannot be bounded at the top and
kono
parents: 67
diff changeset
2791 we need not bother testing any further. */
kono
parents: 67
diff changeset
2792
kono
parents: 67
diff changeset
2793 if (node->right)
kono
parents: 67
diff changeset
2794 return false;
kono
parents: 67
diff changeset
2795
kono
parents: 67
diff changeset
2796 high_plus_one = fold_build2 (PLUS_EXPR, TREE_TYPE (node->high), node->high,
kono
parents: 67
diff changeset
2797 build_int_cst (TREE_TYPE (node->high), 1));
kono
parents: 67
diff changeset
2798
kono
parents: 67
diff changeset
2799 /* If the addition above overflowed, we can't verify anything.
kono
parents: 67
diff changeset
2800 Otherwise, look for a parent that tests our value + 1. */
kono
parents: 67
diff changeset
2801
kono
parents: 67
diff changeset
2802 if (!tree_int_cst_lt (node->high, high_plus_one))
kono
parents: 67
diff changeset
2803 return false;
kono
parents: 67
diff changeset
2804
kono
parents: 67
diff changeset
2805 for (pnode = node->parent; pnode; pnode = pnode->parent)
kono
parents: 67
diff changeset
2806 if (tree_int_cst_equal (high_plus_one, pnode->low))
kono
parents: 67
diff changeset
2807 return true;
kono
parents: 67
diff changeset
2808
kono
parents: 67
diff changeset
2809 return false;
kono
parents: 67
diff changeset
2810 }
kono
parents: 67
diff changeset
2811
kono
parents: 67
diff changeset
2812 /* Search the parent sections of the
kono
parents: 67
diff changeset
2813 case node tree to see if both tests for the upper and lower
kono
parents: 67
diff changeset
2814 bounds of NODE would be redundant. */
kono
parents: 67
diff changeset
2815
kono
parents: 67
diff changeset
2816 static bool
kono
parents: 67
diff changeset
2817 node_is_bounded (case_node_ptr node, tree index_type)
kono
parents: 67
diff changeset
2818 {
kono
parents: 67
diff changeset
2819 return (node_has_low_bound (node, index_type)
kono
parents: 67
diff changeset
2820 && node_has_high_bound (node, index_type));
kono
parents: 67
diff changeset
2821 }