]>
Commit | Line | Data |
---|---|---|
531b10fc SB |
1 | /* Lower GIMPLE_SWITCH expressions to something more efficient than |
2 | a jump table. | |
a5544970 | 3 | Copyright (C) 2006-2019 Free Software Foundation, Inc. |
b6e99746 MJ |
4 | |
5 | This file is part of GCC. | |
6 | ||
7 | GCC is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU General Public License as published by the | |
9 | Free Software Foundation; either version 3, or (at your option) any | |
10 | later version. | |
11 | ||
12 | GCC is distributed in the hope that it will be useful, but WITHOUT | |
13 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GCC; see the file COPYING3. If not, write to the Free | |
19 | Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA | |
20 | 02110-1301, USA. */ | |
21 | ||
531b10fc SB |
22 | /* This file handles the lowering of GIMPLE_SWITCH to an indexed |
23 | load, or a series of bit-test-and-branch expressions. */ | |
24 | ||
25 | #include "config.h" | |
26 | #include "system.h" | |
27 | #include "coretypes.h" | |
c7131fb2 | 28 | #include "backend.h" |
957060b5 AM |
29 | #include "insn-codes.h" |
30 | #include "rtl.h" | |
c7131fb2 AM |
31 | #include "tree.h" |
32 | #include "gimple.h" | |
957060b5 AM |
33 | #include "cfghooks.h" |
34 | #include "tree-pass.h" | |
c7131fb2 | 35 | #include "ssa.h" |
957060b5 AM |
36 | #include "optabs-tree.h" |
37 | #include "cgraph.h" | |
38 | #include "gimple-pretty-print.h" | |
531b10fc | 39 | #include "params.h" |
40e23961 | 40 | #include "fold-const.h" |
d8a2d370 DN |
41 | #include "varasm.h" |
42 | #include "stor-layout.h" | |
60393bbc | 43 | #include "cfganal.h" |
45b0be94 | 44 | #include "gimplify.h" |
5be5c238 | 45 | #include "gimple-iterator.h" |
18f429e2 | 46 | #include "gimplify-me.h" |
767d4551 | 47 | #include "gimple-fold.h" |
442b4905 | 48 | #include "tree-cfg.h" |
a9e0d843 | 49 | #include "cfgloop.h" |
9dc3d6a9 ML |
50 | #include "alloc-pool.h" |
51 | #include "target.h" | |
52 | #include "tree-into-ssa.h" | |
46dbeb40 | 53 | #include "omp-general.h" |
7ee2468b SB |
54 | |
55 | /* ??? For lang_hooks.types.type_for_mode, but is there a word_mode | |
56 | type in the GIMPLE type system that is language-independent? */ | |
531b10fc SB |
57 | #include "langhooks.h" |
58 | ||
789410e4 | 59 | #include "tree-switch-conversion.h" |
531b10fc | 60 | \f |
789410e4 | 61 | using namespace tree_switch_conversion; |
531b10fc | 62 | |
789410e4 | 63 | /* Constructor. */ |
531b10fc | 64 | |
789410e4 ML |
65 | switch_conversion::switch_conversion (): m_final_bb (NULL), m_other_count (), |
66 | m_constructors (NULL), m_default_values (NULL), | |
67 | m_arr_ref_first (NULL), m_arr_ref_last (NULL), | |
68 | m_reason (NULL), m_default_case_nonstandard (false), m_cfg_altered (false) | |
531b10fc | 69 | { |
531b10fc | 70 | } |
b6e99746 | 71 | |
789410e4 | 72 | /* Collection information about SWTCH statement. */ |
886cd84f | 73 | |
789410e4 ML |
74 | void |
75 | switch_conversion::collect (gswitch *swtch) | |
b6e99746 | 76 | { |
726a989a | 77 | unsigned int branch_num = gimple_switch_num_labels (swtch); |
886cd84f | 78 | tree min_case, max_case; |
789410e4 | 79 | unsigned int i; |
18bfe940 | 80 | edge e, e_default, e_first; |
886cd84f SB |
81 | edge_iterator ei; |
82 | ||
789410e4 | 83 | m_switch = swtch; |
b6e99746 MJ |
84 | |
85 | /* The gimplifier has already sorted the cases by CASE_LOW and ensured there | |
fd8d363e SB |
86 | is a default label which is the first in the vector. |
87 | Collect the bits we can deduce from the CFG. */ | |
789410e4 ML |
88 | m_index_expr = gimple_switch_index (swtch); |
89 | m_switch_bb = gimple_bb (swtch); | |
61ff5d6f ML |
90 | e_default = gimple_switch_default_edge (cfun, swtch); |
91 | m_default_bb = e_default->dest; | |
789410e4 ML |
92 | m_default_prob = e_default->probability; |
93 | m_default_count = e_default->count (); | |
94 | FOR_EACH_EDGE (e, ei, m_switch_bb->succs) | |
886cd84f | 95 | if (e != e_default) |
789410e4 | 96 | m_other_count += e->count (); |
b6e99746 | 97 | |
18bfe940 JJ |
98 | /* Get upper and lower bounds of case values, and the covered range. */ |
99 | min_case = gimple_switch_label (swtch, 1); | |
100 | max_case = gimple_switch_label (swtch, branch_num - 1); | |
101 | ||
789410e4 | 102 | m_range_min = CASE_LOW (min_case); |
18bfe940 | 103 | if (CASE_HIGH (max_case) != NULL_TREE) |
789410e4 | 104 | m_range_max = CASE_HIGH (max_case); |
18bfe940 | 105 | else |
789410e4 | 106 | m_range_max = CASE_LOW (max_case); |
18bfe940 | 107 | |
789410e4 ML |
108 | m_contiguous_range = true; |
109 | tree last = CASE_HIGH (min_case) ? CASE_HIGH (min_case) : m_range_min; | |
18bfe940 JJ |
110 | for (i = 2; i < branch_num; i++) |
111 | { | |
112 | tree elt = gimple_switch_label (swtch, i); | |
8e6cdc90 | 113 | if (wi::to_wide (last) + 1 != wi::to_wide (CASE_LOW (elt))) |
18bfe940 | 114 | { |
789410e4 | 115 | m_contiguous_range = false; |
18bfe940 JJ |
116 | break; |
117 | } | |
118 | last = CASE_HIGH (elt) ? CASE_HIGH (elt) : CASE_LOW (elt); | |
119 | } | |
120 | ||
789410e4 | 121 | if (m_contiguous_range) |
61ff5d6f | 122 | e_first = gimple_switch_edge (cfun, swtch, 1); |
18bfe940 | 123 | else |
61ff5d6f | 124 | e_first = e_default; |
18bfe940 | 125 | |
886cd84f | 126 | /* See if there is one common successor block for all branch |
866f20d6 | 127 | targets. If it exists, record it in FINAL_BB. |
18bfe940 JJ |
128 | Start with the destination of the first non-default case |
129 | if the range is contiguous and default case otherwise as | |
130 | guess or its destination in case it is a forwarder block. */ | |
131 | if (! single_pred_p (e_first->dest)) | |
789410e4 | 132 | m_final_bb = e_first->dest; |
18bfe940 JJ |
133 | else if (single_succ_p (e_first->dest) |
134 | && ! single_pred_p (single_succ (e_first->dest))) | |
789410e4 | 135 | m_final_bb = single_succ (e_first->dest); |
866f20d6 | 136 | /* Require that all switch destinations are either that common |
18bfe940 JJ |
137 | FINAL_BB or a forwarder to it, except for the default |
138 | case if contiguous range. */ | |
789410e4 ML |
139 | if (m_final_bb) |
140 | FOR_EACH_EDGE (e, ei, m_switch_bb->succs) | |
886cd84f | 141 | { |
789410e4 | 142 | if (e->dest == m_final_bb) |
886cd84f SB |
143 | continue; |
144 | ||
145 | if (single_pred_p (e->dest) | |
146 | && single_succ_p (e->dest) | |
789410e4 | 147 | && single_succ (e->dest) == m_final_bb) |
886cd84f SB |
148 | continue; |
149 | ||
789410e4 | 150 | if (e == e_default && m_contiguous_range) |
18bfe940 | 151 | { |
789410e4 | 152 | m_default_case_nonstandard = true; |
18bfe940 JJ |
153 | continue; |
154 | } | |
155 | ||
789410e4 | 156 | m_final_bb = NULL; |
886cd84f SB |
157 | break; |
158 | } | |
159 | ||
789410e4 ML |
160 | m_range_size |
161 | = int_const_binop (MINUS_EXPR, m_range_max, m_range_min); | |
b6e99746 | 162 | |
886cd84f SB |
163 | /* Get a count of the number of case labels. Single-valued case labels |
164 | simply count as one, but a case range counts double, since it may | |
165 | require two compares if it gets lowered as a branching tree. */ | |
789410e4 | 166 | m_count = 0; |
886cd84f SB |
167 | for (i = 1; i < branch_num; i++) |
168 | { | |
169 | tree elt = gimple_switch_label (swtch, i); | |
789410e4 | 170 | m_count++; |
886cd84f SB |
171 | if (CASE_HIGH (elt) |
172 | && ! tree_int_cst_equal (CASE_LOW (elt), CASE_HIGH (elt))) | |
789410e4 | 173 | m_count++; |
886cd84f | 174 | } |
dc223ad4 ML |
175 | |
176 | /* Get the number of unique non-default targets out of the GIMPLE_SWITCH | |
177 | block. Assume a CFG cleanup would have already removed degenerate | |
178 | switch statements, this allows us to just use EDGE_COUNT. */ | |
179 | m_uniq = EDGE_COUNT (gimple_bb (swtch)->succs) - 1; | |
886cd84f | 180 | } |
b6e99746 | 181 | |
789410e4 | 182 | /* Checks whether the range given by individual case statements of the switch |
886cd84f SB |
183 | switch statement isn't too big and whether the number of branches actually |
184 | satisfies the size of the new array. */ | |
b6e99746 | 185 | |
789410e4 ML |
186 | bool |
187 | switch_conversion::check_range () | |
886cd84f | 188 | { |
789410e4 ML |
189 | gcc_assert (m_range_size); |
190 | if (!tree_fits_uhwi_p (m_range_size)) | |
b6e99746 | 191 | { |
789410e4 | 192 | m_reason = "index range way too large or otherwise unusable"; |
b6e99746 MJ |
193 | return false; |
194 | } | |
195 | ||
789410e4 ML |
196 | if (tree_to_uhwi (m_range_size) |
197 | > ((unsigned) m_count * SWITCH_CONVERSION_BRANCH_RATIO)) | |
b6e99746 | 198 | { |
789410e4 | 199 | m_reason = "the maximum range-branch ratio exceeded"; |
b6e99746 MJ |
200 | return false; |
201 | } | |
202 | ||
203 | return true; | |
204 | } | |
205 | ||
789410e4 | 206 | /* Checks whether all but the final BB basic blocks are empty. */ |
b6e99746 | 207 | |
789410e4 ML |
208 | bool |
209 | switch_conversion::check_all_empty_except_final () | |
b6e99746 | 210 | { |
789410e4 | 211 | edge e, e_default = find_edge (m_switch_bb, m_default_bb); |
886cd84f | 212 | edge_iterator ei; |
b6e99746 | 213 | |
789410e4 | 214 | FOR_EACH_EDGE (e, ei, m_switch_bb->succs) |
b6e99746 | 215 | { |
789410e4 | 216 | if (e->dest == m_final_bb) |
886cd84f | 217 | continue; |
b6e99746 | 218 | |
886cd84f | 219 | if (!empty_block_p (e->dest)) |
b6e99746 | 220 | { |
789410e4 | 221 | if (m_contiguous_range && e == e_default) |
18bfe940 | 222 | { |
789410e4 | 223 | m_default_case_nonstandard = true; |
18bfe940 JJ |
224 | continue; |
225 | } | |
226 | ||
789410e4 | 227 | m_reason = "bad case - a non-final BB not empty"; |
b6e99746 MJ |
228 | return false; |
229 | } | |
b6e99746 MJ |
230 | } |
231 | ||
232 | return true; | |
233 | } | |
234 | ||
235 | /* This function checks whether all required values in phi nodes in final_bb | |
236 | are constants. Required values are those that correspond to a basic block | |
237 | which is a part of the examined switch statement. It returns true if the | |
238 | phi nodes are OK, otherwise false. */ | |
239 | ||
789410e4 ML |
240 | bool |
241 | switch_conversion::check_final_bb () | |
b6e99746 | 242 | { |
538dd0b7 | 243 | gphi_iterator gsi; |
b6e99746 | 244 | |
789410e4 ML |
245 | m_phi_count = 0; |
246 | for (gsi = gsi_start_phis (m_final_bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
b6e99746 | 247 | { |
538dd0b7 | 248 | gphi *phi = gsi.phi (); |
726a989a | 249 | unsigned int i; |
b6e99746 | 250 | |
18bfe940 JJ |
251 | if (virtual_operand_p (gimple_phi_result (phi))) |
252 | continue; | |
253 | ||
789410e4 | 254 | m_phi_count++; |
b6e99746 | 255 | |
726a989a | 256 | for (i = 0; i < gimple_phi_num_args (phi); i++) |
b6e99746 | 257 | { |
726a989a | 258 | basic_block bb = gimple_phi_arg_edge (phi, i)->src; |
b6e99746 | 259 | |
789410e4 | 260 | if (bb == m_switch_bb |
18bfe940 | 261 | || (single_pred_p (bb) |
789410e4 ML |
262 | && single_pred (bb) == m_switch_bb |
263 | && (!m_default_case_nonstandard | |
18bfe940 | 264 | || empty_block_p (bb)))) |
b6e99746 | 265 | { |
f6e6e990 | 266 | tree reloc, val; |
18bfe940 | 267 | const char *reason = NULL; |
f6e6e990 JJ |
268 | |
269 | val = gimple_phi_arg_def (phi, i); | |
270 | if (!is_gimple_ip_invariant (val)) | |
18bfe940 JJ |
271 | reason = "non-invariant value from a case"; |
272 | else | |
f6e6e990 | 273 | { |
18bfe940 JJ |
274 | reloc = initializer_constant_valid_p (val, TREE_TYPE (val)); |
275 | if ((flag_pic && reloc != null_pointer_node) | |
276 | || (!flag_pic && reloc == NULL_TREE)) | |
277 | { | |
278 | if (reloc) | |
279 | reason | |
280 | = "value from a case would need runtime relocations"; | |
281 | else | |
282 | reason | |
283 | = "value from a case is not a valid initializer"; | |
284 | } | |
f6e6e990 | 285 | } |
18bfe940 | 286 | if (reason) |
f6e6e990 | 287 | { |
18bfe940 JJ |
288 | /* For contiguous range, we can allow non-constant |
289 | or one that needs relocation, as long as it is | |
290 | only reachable from the default case. */ | |
789410e4 ML |
291 | if (bb == m_switch_bb) |
292 | bb = m_final_bb; | |
293 | if (!m_contiguous_range || bb != m_default_bb) | |
18bfe940 | 294 | { |
789410e4 | 295 | m_reason = reason; |
18bfe940 JJ |
296 | return false; |
297 | } | |
298 | ||
789410e4 | 299 | unsigned int branch_num = gimple_switch_num_labels (m_switch); |
18bfe940 JJ |
300 | for (unsigned int i = 1; i < branch_num; i++) |
301 | { | |
61ff5d6f | 302 | if (gimple_switch_label_bb (cfun, m_switch, i) == bb) |
18bfe940 | 303 | { |
789410e4 | 304 | m_reason = reason; |
18bfe940 JJ |
305 | return false; |
306 | } | |
307 | } | |
789410e4 | 308 | m_default_case_nonstandard = true; |
f6e6e990 | 309 | } |
b6e99746 MJ |
310 | } |
311 | } | |
312 | } | |
313 | ||
314 | return true; | |
315 | } | |
316 | ||
317 | /* The following function allocates default_values, target_{in,out}_names and | |
318 | constructors arrays. The last one is also populated with pointers to | |
319 | vectors that will become constructors of new arrays. */ | |
320 | ||
789410e4 ML |
321 | void |
322 | switch_conversion::create_temp_arrays () | |
b6e99746 MJ |
323 | { |
324 | int i; | |
325 | ||
789410e4 | 326 | m_default_values = XCNEWVEC (tree, m_phi_count * 3); |
9771b263 DN |
327 | /* ??? Macros do not support multi argument templates in their |
328 | argument list. We create a typedef to work around that problem. */ | |
329 | typedef vec<constructor_elt, va_gc> *vec_constructor_elt_gc; | |
789410e4 ML |
330 | m_constructors = XCNEWVEC (vec_constructor_elt_gc, m_phi_count); |
331 | m_target_inbound_names = m_default_values + m_phi_count; | |
332 | m_target_outbound_names = m_target_inbound_names + m_phi_count; | |
333 | for (i = 0; i < m_phi_count; i++) | |
334 | vec_alloc (m_constructors[i], tree_to_uhwi (m_range_size) + 1); | |
b6e99746 MJ |
335 | } |
336 | ||
337 | /* Populate the array of default values in the order of phi nodes. | |
18bfe940 JJ |
338 | DEFAULT_CASE is the CASE_LABEL_EXPR for the default switch branch |
339 | if the range is non-contiguous or the default case has standard | |
340 | structure, otherwise it is the first non-default case instead. */ | |
b6e99746 | 341 | |
789410e4 ML |
342 | void |
343 | switch_conversion::gather_default_values (tree default_case) | |
b6e99746 | 344 | { |
538dd0b7 | 345 | gphi_iterator gsi; |
61ff5d6f | 346 | basic_block bb = label_to_block (cfun, CASE_LABEL (default_case)); |
b6e99746 | 347 | edge e; |
726a989a | 348 | int i = 0; |
b6e99746 | 349 | |
18bfe940 | 350 | gcc_assert (CASE_LOW (default_case) == NULL_TREE |
789410e4 | 351 | || m_default_case_nonstandard); |
b6e99746 | 352 | |
789410e4 ML |
353 | if (bb == m_final_bb) |
354 | e = find_edge (m_switch_bb, bb); | |
b6e99746 MJ |
355 | else |
356 | e = single_succ_edge (bb); | |
357 | ||
789410e4 | 358 | for (gsi = gsi_start_phis (m_final_bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
b6e99746 | 359 | { |
538dd0b7 | 360 | gphi *phi = gsi.phi (); |
18bfe940 JJ |
361 | if (virtual_operand_p (gimple_phi_result (phi))) |
362 | continue; | |
b6e99746 MJ |
363 | tree val = PHI_ARG_DEF_FROM_EDGE (phi, e); |
364 | gcc_assert (val); | |
789410e4 | 365 | m_default_values[i++] = val; |
b6e99746 MJ |
366 | } |
367 | } | |
368 | ||
369 | /* The following function populates the vectors in the constructors array with | |
370 | future contents of the static arrays. The vectors are populated in the | |
789410e4 | 371 | order of phi nodes. */ |
b6e99746 | 372 | |
789410e4 ML |
373 | void |
374 | switch_conversion::build_constructors () | |
b6e99746 | 375 | { |
789410e4 ML |
376 | unsigned i, branch_num = gimple_switch_num_labels (m_switch); |
377 | tree pos = m_range_min; | |
18bfe940 | 378 | tree pos_one = build_int_cst (TREE_TYPE (pos), 1); |
b6e99746 | 379 | |
726a989a | 380 | for (i = 1; i < branch_num; i++) |
b6e99746 | 381 | { |
789410e4 | 382 | tree cs = gimple_switch_label (m_switch, i); |
61ff5d6f | 383 | basic_block bb = label_to_block (cfun, CASE_LABEL (cs)); |
b6e99746 | 384 | edge e; |
726a989a | 385 | tree high; |
538dd0b7 | 386 | gphi_iterator gsi; |
b6e99746 MJ |
387 | int j; |
388 | ||
789410e4 ML |
389 | if (bb == m_final_bb) |
390 | e = find_edge (m_switch_bb, bb); | |
b6e99746 MJ |
391 | else |
392 | e = single_succ_edge (bb); | |
393 | gcc_assert (e); | |
394 | ||
395 | while (tree_int_cst_lt (pos, CASE_LOW (cs))) | |
396 | { | |
397 | int k; | |
789410e4 | 398 | for (k = 0; k < m_phi_count; k++) |
b6e99746 | 399 | { |
f32682ca | 400 | constructor_elt elt; |
b6e99746 | 401 | |
789410e4 | 402 | elt.index = int_const_binop (MINUS_EXPR, pos, m_range_min); |
d1f98542 | 403 | elt.value |
789410e4 ML |
404 | = unshare_expr_without_location (m_default_values[k]); |
405 | m_constructors[k]->quick_push (elt); | |
b6e99746 MJ |
406 | } |
407 | ||
18bfe940 | 408 | pos = int_const_binop (PLUS_EXPR, pos, pos_one); |
b6e99746 | 409 | } |
b1ae1681 | 410 | gcc_assert (tree_int_cst_equal (pos, CASE_LOW (cs))); |
b6e99746 MJ |
411 | |
412 | j = 0; | |
413 | if (CASE_HIGH (cs)) | |
414 | high = CASE_HIGH (cs); | |
415 | else | |
b1ae1681 | 416 | high = CASE_LOW (cs); |
789410e4 | 417 | for (gsi = gsi_start_phis (m_final_bb); |
726a989a | 418 | !gsi_end_p (gsi); gsi_next (&gsi)) |
b6e99746 | 419 | { |
538dd0b7 | 420 | gphi *phi = gsi.phi (); |
18bfe940 JJ |
421 | if (virtual_operand_p (gimple_phi_result (phi))) |
422 | continue; | |
b6e99746 | 423 | tree val = PHI_ARG_DEF_FROM_EDGE (phi, e); |
7f2a9982 | 424 | tree low = CASE_LOW (cs); |
b6e99746 MJ |
425 | pos = CASE_LOW (cs); |
426 | ||
b8698a0f | 427 | do |
b6e99746 | 428 | { |
f32682ca | 429 | constructor_elt elt; |
b6e99746 | 430 | |
789410e4 | 431 | elt.index = int_const_binop (MINUS_EXPR, pos, m_range_min); |
d1f98542 | 432 | elt.value = unshare_expr_without_location (val); |
789410e4 | 433 | m_constructors[j]->quick_push (elt); |
b6e99746 | 434 | |
18bfe940 | 435 | pos = int_const_binop (PLUS_EXPR, pos, pos_one); |
7156c8ab MJ |
436 | } while (!tree_int_cst_lt (high, pos) |
437 | && tree_int_cst_lt (low, pos)); | |
b6e99746 MJ |
438 | j++; |
439 | } | |
440 | } | |
441 | } | |
442 | ||
767d4551 ML |
443 | /* If all values in the constructor vector are products of a linear function |
444 | a * x + b, then return true. When true, COEFF_A and COEFF_B and | |
445 | coefficients of the linear function. Note that equal values are special | |
446 | case of a linear function with a and b equal to zero. */ | |
7156c8ab | 447 | |
767d4551 ML |
448 | bool |
449 | switch_conversion::contains_linear_function_p (vec<constructor_elt, va_gc> *vec, | |
450 | wide_int *coeff_a, | |
451 | wide_int *coeff_b) | |
7156c8ab | 452 | { |
8e97bc2b | 453 | unsigned int i; |
8e97bc2b | 454 | constructor_elt *elt; |
7156c8ab | 455 | |
767d4551 ML |
456 | gcc_assert (vec->length () >= 2); |
457 | ||
458 | /* Let's try to find any linear function a * x + y that can apply to | |
459 | given values. 'a' can be calculated as follows: | |
460 | ||
461 | a = (y2 - y1) / (x2 - x1) where x2 - x1 = 1 (consecutive case indices) | |
462 | a = y2 - y1 | |
463 | ||
464 | and | |
465 | ||
466 | b = y2 - a * x2 | |
467 | ||
468 | */ | |
469 | ||
470 | tree elt0 = (*vec)[0].value; | |
471 | tree elt1 = (*vec)[1].value; | |
472 | ||
473 | if (TREE_CODE (elt0) != INTEGER_CST || TREE_CODE (elt1) != INTEGER_CST) | |
474 | return false; | |
475 | ||
5a5474ba ML |
476 | wide_int range_min |
477 | = wide_int::from (wi::to_wide (m_range_min), | |
478 | TYPE_PRECISION (TREE_TYPE (elt0)), | |
479 | TYPE_SIGN (TREE_TYPE (m_range_min))); | |
767d4551 ML |
480 | wide_int y1 = wi::to_wide (elt0); |
481 | wide_int y2 = wi::to_wide (elt1); | |
482 | wide_int a = y2 - y1; | |
483 | wide_int b = y2 - a * (range_min + 1); | |
484 | ||
485 | /* Verify that all values fulfill the linear function. */ | |
9771b263 | 486 | FOR_EACH_VEC_SAFE_ELT (vec, i, elt) |
7156c8ab | 487 | { |
767d4551 ML |
488 | if (TREE_CODE (elt->value) != INTEGER_CST) |
489 | return false; | |
490 | ||
491 | wide_int value = wi::to_wide (elt->value); | |
492 | if (a * range_min + b != value) | |
493 | return false; | |
494 | ||
495 | ++range_min; | |
7156c8ab | 496 | } |
767d4551 ML |
497 | |
498 | *coeff_a = a; | |
499 | *coeff_b = b; | |
500 | ||
501 | return true; | |
7156c8ab MJ |
502 | } |
503 | ||
f1b0632a OH |
504 | /* Return type which should be used for array elements, either TYPE's |
505 | main variant or, for integral types, some smaller integral type | |
506 | that can still hold all the constants. */ | |
8e97bc2b | 507 | |
789410e4 ML |
508 | tree |
509 | switch_conversion::array_value_type (tree type, int num) | |
8e97bc2b | 510 | { |
789410e4 | 511 | unsigned int i, len = vec_safe_length (m_constructors[num]); |
8e97bc2b | 512 | constructor_elt *elt; |
8e97bc2b JJ |
513 | int sign = 0; |
514 | tree smaller_type; | |
515 | ||
f1b0632a OH |
516 | /* Types with alignments greater than their size can reach here, e.g. out of |
517 | SRA. We couldn't use these as an array component type so get back to the | |
518 | main variant first, which, for our purposes, is fine for other types as | |
519 | well. */ | |
520 | ||
521 | type = TYPE_MAIN_VARIANT (type); | |
522 | ||
8e97bc2b JJ |
523 | if (!INTEGRAL_TYPE_P (type)) |
524 | return type; | |
525 | ||
7a504f33 | 526 | scalar_int_mode type_mode = SCALAR_INT_TYPE_MODE (type); |
095a2d76 | 527 | scalar_int_mode mode = get_narrowest_mode (type_mode); |
ec35d572 | 528 | if (GET_MODE_SIZE (type_mode) <= GET_MODE_SIZE (mode)) |
8e97bc2b JJ |
529 | return type; |
530 | ||
789410e4 | 531 | if (len < (optimize_bb_for_size_p (gimple_bb (m_switch)) ? 2 : 32)) |
8e97bc2b JJ |
532 | return type; |
533 | ||
789410e4 | 534 | FOR_EACH_VEC_SAFE_ELT (m_constructors[num], i, elt) |
8e97bc2b | 535 | { |
807e902e | 536 | wide_int cst; |
8e97bc2b JJ |
537 | |
538 | if (TREE_CODE (elt->value) != INTEGER_CST) | |
539 | return type; | |
540 | ||
8e6cdc90 | 541 | cst = wi::to_wide (elt->value); |
8e97bc2b JJ |
542 | while (1) |
543 | { | |
544 | unsigned int prec = GET_MODE_BITSIZE (mode); | |
545 | if (prec > HOST_BITS_PER_WIDE_INT) | |
546 | return type; | |
547 | ||
807e902e | 548 | if (sign >= 0 && cst == wi::zext (cst, prec)) |
8e97bc2b | 549 | { |
807e902e | 550 | if (sign == 0 && cst == wi::sext (cst, prec)) |
8e97bc2b JJ |
551 | break; |
552 | sign = 1; | |
553 | break; | |
554 | } | |
807e902e | 555 | if (sign <= 0 && cst == wi::sext (cst, prec)) |
8e97bc2b JJ |
556 | { |
557 | sign = -1; | |
558 | break; | |
559 | } | |
560 | ||
561 | if (sign == 1) | |
562 | sign = 0; | |
563 | ||
490d0f6c | 564 | if (!GET_MODE_WIDER_MODE (mode).exists (&mode) |
ec35d572 | 565 | || GET_MODE_SIZE (mode) >= GET_MODE_SIZE (type_mode)) |
8e97bc2b JJ |
566 | return type; |
567 | } | |
568 | } | |
569 | ||
570 | if (sign == 0) | |
571 | sign = TYPE_UNSIGNED (type) ? 1 : -1; | |
572 | smaller_type = lang_hooks.types.type_for_mode (mode, sign >= 0); | |
7a504f33 RS |
573 | if (GET_MODE_SIZE (type_mode) |
574 | <= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (smaller_type))) | |
8e97bc2b JJ |
575 | return type; |
576 | ||
577 | return smaller_type; | |
578 | } | |
579 | ||
789410e4 ML |
580 | /* Create an appropriate array type and declaration and assemble a static |
581 | array variable. Also create a load statement that initializes | |
582 | the variable in question with a value from the static array. SWTCH is | |
583 | the switch statement being converted, NUM is the index to | |
584 | arrays of constructors, default values and target SSA names | |
585 | for this particular array. ARR_INDEX_TYPE is the type of the index | |
586 | of the new array, PHI is the phi node of the final BB that corresponds | |
587 | to the value that will be loaded from the created array. TIDX | |
7156c8ab MJ |
588 | is an ssa name of a temporary variable holding the index for loads from the |
589 | new array. */ | |
b6e99746 | 590 | |
789410e4 ML |
591 | void |
592 | switch_conversion::build_one_array (int num, tree arr_index_type, | |
593 | gphi *phi, tree tidx) | |
b6e99746 | 594 | { |
767d4551 | 595 | tree name; |
355fe088 | 596 | gimple *load; |
789410e4 ML |
597 | gimple_stmt_iterator gsi = gsi_for_stmt (m_switch); |
598 | location_t loc = gimple_location (m_switch); | |
b6e99746 | 599 | |
789410e4 | 600 | gcc_assert (m_default_values[num]); |
b6e99746 | 601 | |
b731b390 | 602 | name = copy_ssa_name (PHI_RESULT (phi)); |
789410e4 | 603 | m_target_inbound_names[num] = name; |
b6e99746 | 604 | |
5a5474ba | 605 | vec<constructor_elt, va_gc> *constructor = m_constructors[num]; |
767d4551 | 606 | wide_int coeff_a, coeff_b; |
5a5474ba | 607 | bool linear_p = contains_linear_function_p (constructor, &coeff_a, &coeff_b); |
767d4551 ML |
608 | if (linear_p) |
609 | { | |
610 | if (dump_file && coeff_a.to_uhwi () > 0) | |
611 | fprintf (dump_file, "Linear transformation with A = %" PRId64 | |
612 | " and B = %" PRId64 "\n", coeff_a.to_shwi (), | |
613 | coeff_b.to_shwi ()); | |
614 | ||
5a5474ba ML |
615 | /* We must use type of constructor values. */ |
616 | tree t = unsigned_type_for (TREE_TYPE ((*constructor)[0].value)); | |
767d4551 ML |
617 | gimple_seq seq = NULL; |
618 | tree tmp = gimple_convert (&seq, t, m_index_expr); | |
619 | tree tmp2 = gimple_build (&seq, MULT_EXPR, t, | |
620 | wide_int_to_tree (t, coeff_a), tmp); | |
621 | tree tmp3 = gimple_build (&seq, PLUS_EXPR, t, tmp2, | |
622 | wide_int_to_tree (t, coeff_b)); | |
623 | tree tmp4 = gimple_convert (&seq, TREE_TYPE (name), tmp3); | |
624 | gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT); | |
625 | load = gimple_build_assign (name, tmp4); | |
626 | } | |
7156c8ab MJ |
627 | else |
628 | { | |
8e97bc2b | 629 | tree array_type, ctor, decl, value_type, fetch, default_type; |
7156c8ab | 630 | |
789410e4 ML |
631 | default_type = TREE_TYPE (m_default_values[num]); |
632 | value_type = array_value_type (default_type, num); | |
7156c8ab | 633 | array_type = build_array_type (value_type, arr_index_type); |
8e97bc2b JJ |
634 | if (default_type != value_type) |
635 | { | |
636 | unsigned int i; | |
637 | constructor_elt *elt; | |
638 | ||
5a5474ba | 639 | FOR_EACH_VEC_SAFE_ELT (constructor, i, elt) |
8e97bc2b JJ |
640 | elt->value = fold_convert (value_type, elt->value); |
641 | } | |
5a5474ba | 642 | ctor = build_constructor (array_type, constructor); |
7156c8ab | 643 | TREE_CONSTANT (ctor) = true; |
5f7ae6b6 | 644 | TREE_STATIC (ctor) = true; |
7156c8ab | 645 | |
c2255bc4 | 646 | decl = build_decl (loc, VAR_DECL, NULL_TREE, array_type); |
7156c8ab MJ |
647 | TREE_STATIC (decl) = 1; |
648 | DECL_INITIAL (decl) = ctor; | |
649 | ||
650 | DECL_NAME (decl) = create_tmp_var_name ("CSWTCH"); | |
651 | DECL_ARTIFICIAL (decl) = 1; | |
f8d851c6 | 652 | DECL_IGNORED_P (decl) = 1; |
7156c8ab | 653 | TREE_CONSTANT (decl) = 1; |
2e3b4885 | 654 | TREE_READONLY (decl) = 1; |
d7438551 | 655 | DECL_IGNORED_P (decl) = 1; |
46dbeb40 TV |
656 | if (offloading_function_p (cfun->decl)) |
657 | DECL_ATTRIBUTES (decl) | |
658 | = tree_cons (get_identifier ("omp declare target"), NULL_TREE, | |
659 | NULL_TREE); | |
9041d2e6 | 660 | varpool_node::finalize_decl (decl); |
7156c8ab MJ |
661 | |
662 | fetch = build4 (ARRAY_REF, value_type, decl, tidx, NULL_TREE, | |
663 | NULL_TREE); | |
8e97bc2b JJ |
664 | if (default_type != value_type) |
665 | { | |
666 | fetch = fold_convert (default_type, fetch); | |
667 | fetch = force_gimple_operand_gsi (&gsi, fetch, true, NULL_TREE, | |
668 | true, GSI_SAME_STMT); | |
669 | } | |
7156c8ab MJ |
670 | load = gimple_build_assign (name, fetch); |
671 | } | |
b6e99746 | 672 | |
726a989a | 673 | gsi_insert_before (&gsi, load, GSI_SAME_STMT); |
7156c8ab | 674 | update_stmt (load); |
789410e4 | 675 | m_arr_ref_last = load; |
b6e99746 MJ |
676 | } |
677 | ||
678 | /* Builds and initializes static arrays initialized with values gathered from | |
789410e4 | 679 | the switch statement. Also creates statements that load values from |
b6e99746 MJ |
680 | them. */ |
681 | ||
789410e4 ML |
682 | void |
683 | switch_conversion::build_arrays () | |
b6e99746 MJ |
684 | { |
685 | tree arr_index_type; | |
83d5977e | 686 | tree tidx, sub, utype; |
355fe088 | 687 | gimple *stmt; |
726a989a | 688 | gimple_stmt_iterator gsi; |
538dd0b7 | 689 | gphi_iterator gpi; |
b6e99746 | 690 | int i; |
789410e4 | 691 | location_t loc = gimple_location (m_switch); |
b6e99746 | 692 | |
789410e4 | 693 | gsi = gsi_for_stmt (m_switch); |
04e78aa9 | 694 | |
edb9b69e | 695 | /* Make sure we do not generate arithmetics in a subrange. */ |
789410e4 | 696 | utype = TREE_TYPE (m_index_expr); |
edb9b69e JJ |
697 | if (TREE_TYPE (utype)) |
698 | utype = lang_hooks.types.type_for_mode (TYPE_MODE (TREE_TYPE (utype)), 1); | |
699 | else | |
700 | utype = lang_hooks.types.type_for_mode (TYPE_MODE (utype), 1); | |
701 | ||
789410e4 | 702 | arr_index_type = build_index_type (m_range_size); |
b731b390 | 703 | tidx = make_ssa_name (utype); |
edb9b69e | 704 | sub = fold_build2_loc (loc, MINUS_EXPR, utype, |
789410e4 ML |
705 | fold_convert_loc (loc, utype, m_index_expr), |
706 | fold_convert_loc (loc, utype, m_range_min)); | |
fae1034e | 707 | sub = force_gimple_operand_gsi (&gsi, sub, |
726a989a RB |
708 | false, NULL, true, GSI_SAME_STMT); |
709 | stmt = gimple_build_assign (tidx, sub); | |
b6e99746 | 710 | |
726a989a | 711 | gsi_insert_before (&gsi, stmt, GSI_SAME_STMT); |
7156c8ab | 712 | update_stmt (stmt); |
789410e4 | 713 | m_arr_ref_first = stmt; |
b6e99746 | 714 | |
789410e4 | 715 | for (gpi = gsi_start_phis (m_final_bb), i = 0; |
18bfe940 JJ |
716 | !gsi_end_p (gpi); gsi_next (&gpi)) |
717 | { | |
718 | gphi *phi = gpi.phi (); | |
719 | if (!virtual_operand_p (gimple_phi_result (phi))) | |
789410e4 | 720 | build_one_array (i++, arr_index_type, phi, tidx); |
8dc6a926 JJ |
721 | else |
722 | { | |
723 | edge e; | |
724 | edge_iterator ei; | |
789410e4 | 725 | FOR_EACH_EDGE (e, ei, m_switch_bb->succs) |
8dc6a926 | 726 | { |
789410e4 | 727 | if (e->dest == m_final_bb) |
8dc6a926 | 728 | break; |
789410e4 ML |
729 | if (!m_default_case_nonstandard |
730 | || e->dest != m_default_bb) | |
8dc6a926 JJ |
731 | { |
732 | e = single_succ_edge (e->dest); | |
733 | break; | |
734 | } | |
735 | } | |
789410e4 ML |
736 | gcc_assert (e && e->dest == m_final_bb); |
737 | m_target_vop = PHI_ARG_DEF_FROM_EDGE (phi, e); | |
8dc6a926 | 738 | } |
18bfe940 | 739 | } |
b6e99746 MJ |
740 | } |
741 | ||
742 | /* Generates and appropriately inserts loads of default values at the position | |
789410e4 | 743 | given by GSI. Returns the last inserted statement. */ |
b6e99746 | 744 | |
789410e4 ML |
745 | gassign * |
746 | switch_conversion::gen_def_assigns (gimple_stmt_iterator *gsi) | |
b6e99746 MJ |
747 | { |
748 | int i; | |
538dd0b7 | 749 | gassign *assign = NULL; |
b6e99746 | 750 | |
789410e4 | 751 | for (i = 0; i < m_phi_count; i++) |
b6e99746 | 752 | { |
789410e4 ML |
753 | tree name = copy_ssa_name (m_target_inbound_names[i]); |
754 | m_target_outbound_names[i] = name; | |
755 | assign = gimple_build_assign (name, m_default_values[i]); | |
726a989a | 756 | gsi_insert_before (gsi, assign, GSI_SAME_STMT); |
7156c8ab | 757 | update_stmt (assign); |
b6e99746 MJ |
758 | } |
759 | return assign; | |
760 | } | |
761 | ||
762 | /* Deletes the unused bbs and edges that now contain the switch statement and | |
789410e4 ML |
763 | its empty branch bbs. BBD is the now dead BB containing |
764 | the original switch statement, FINAL is the last BB of the converted | |
765 | switch statement (in terms of succession). */ | |
b6e99746 | 766 | |
789410e4 ML |
767 | void |
768 | switch_conversion::prune_bbs (basic_block bbd, basic_block final, | |
769 | basic_block default_bb) | |
b6e99746 MJ |
770 | { |
771 | edge_iterator ei; | |
772 | edge e; | |
773 | ||
774 | for (ei = ei_start (bbd->succs); (e = ei_safe_edge (ei)); ) | |
775 | { | |
776 | basic_block bb; | |
777 | bb = e->dest; | |
778 | remove_edge (e); | |
18bfe940 | 779 | if (bb != final && bb != default_bb) |
b6e99746 MJ |
780 | delete_basic_block (bb); |
781 | } | |
782 | delete_basic_block (bbd); | |
783 | } | |
784 | ||
785 | /* Add values to phi nodes in final_bb for the two new edges. E1F is the edge | |
786 | from the basic block loading values from an array and E2F from the basic | |
787 | block loading default values. BBF is the last switch basic block (see the | |
788 | bbf description in the comment below). */ | |
789 | ||
789410e4 ML |
790 | void |
791 | switch_conversion::fix_phi_nodes (edge e1f, edge e2f, basic_block bbf) | |
b6e99746 | 792 | { |
538dd0b7 | 793 | gphi_iterator gsi; |
b6e99746 MJ |
794 | int i; |
795 | ||
726a989a | 796 | for (gsi = gsi_start_phis (bbf), i = 0; |
18bfe940 | 797 | !gsi_end_p (gsi); gsi_next (&gsi)) |
b6e99746 | 798 | { |
538dd0b7 | 799 | gphi *phi = gsi.phi (); |
18bfe940 JJ |
800 | tree inbound, outbound; |
801 | if (virtual_operand_p (gimple_phi_result (phi))) | |
789410e4 | 802 | inbound = outbound = m_target_vop; |
18bfe940 JJ |
803 | else |
804 | { | |
789410e4 ML |
805 | inbound = m_target_inbound_names[i]; |
806 | outbound = m_target_outbound_names[i++]; | |
18bfe940 JJ |
807 | } |
808 | add_phi_arg (phi, inbound, e1f, UNKNOWN_LOCATION); | |
789410e4 | 809 | if (!m_default_case_nonstandard) |
18bfe940 | 810 | add_phi_arg (phi, outbound, e2f, UNKNOWN_LOCATION); |
b6e99746 | 811 | } |
b6e99746 MJ |
812 | } |
813 | ||
814 | /* Creates a check whether the switch expression value actually falls into the | |
815 | range given by all the cases. If it does not, the temporaries are loaded | |
789410e4 | 816 | with default values instead. */ |
b6e99746 | 817 | |
789410e4 ML |
818 | void |
819 | switch_conversion::gen_inbound_check () | |
b6e99746 | 820 | { |
c2255bc4 AH |
821 | tree label_decl1 = create_artificial_label (UNKNOWN_LOCATION); |
822 | tree label_decl2 = create_artificial_label (UNKNOWN_LOCATION); | |
823 | tree label_decl3 = create_artificial_label (UNKNOWN_LOCATION); | |
538dd0b7 | 824 | glabel *label1, *label2, *label3; |
edb9b69e | 825 | tree utype, tidx; |
b6e99746 MJ |
826 | tree bound; |
827 | ||
538dd0b7 | 828 | gcond *cond_stmt; |
b6e99746 | 829 | |
18bfe940 | 830 | gassign *last_assign = NULL; |
726a989a | 831 | gimple_stmt_iterator gsi; |
b6e99746 | 832 | basic_block bb0, bb1, bb2, bbf, bbd; |
18bfe940 | 833 | edge e01 = NULL, e02, e21, e1d, e1f, e2f; |
789410e4 | 834 | location_t loc = gimple_location (m_switch); |
b6e99746 | 835 | |
789410e4 | 836 | gcc_assert (m_default_values); |
6ab1ab14 | 837 | |
789410e4 | 838 | bb0 = gimple_bb (m_switch); |
b6e99746 | 839 | |
789410e4 | 840 | tidx = gimple_assign_lhs (m_arr_ref_first); |
edb9b69e | 841 | utype = TREE_TYPE (tidx); |
145544ab | 842 | |
b6e99746 | 843 | /* (end of) block 0 */ |
789410e4 | 844 | gsi = gsi_for_stmt (m_arr_ref_first); |
edb9b69e | 845 | gsi_next (&gsi); |
b6e99746 | 846 | |
789410e4 | 847 | bound = fold_convert_loc (loc, utype, m_range_size); |
edb9b69e | 848 | cond_stmt = gimple_build_cond (LE_EXPR, tidx, bound, NULL_TREE, NULL_TREE); |
726a989a | 849 | gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT); |
7156c8ab | 850 | update_stmt (cond_stmt); |
b6e99746 MJ |
851 | |
852 | /* block 2 */ | |
789410e4 | 853 | if (!m_default_case_nonstandard) |
18bfe940 JJ |
854 | { |
855 | label2 = gimple_build_label (label_decl2); | |
856 | gsi_insert_before (&gsi, label2, GSI_SAME_STMT); | |
789410e4 | 857 | last_assign = gen_def_assigns (&gsi); |
18bfe940 | 858 | } |
b6e99746 MJ |
859 | |
860 | /* block 1 */ | |
726a989a RB |
861 | label1 = gimple_build_label (label_decl1); |
862 | gsi_insert_before (&gsi, label1, GSI_SAME_STMT); | |
b6e99746 MJ |
863 | |
864 | /* block F */ | |
789410e4 | 865 | gsi = gsi_start_bb (m_final_bb); |
726a989a RB |
866 | label3 = gimple_build_label (label_decl3); |
867 | gsi_insert_before (&gsi, label3, GSI_SAME_STMT); | |
b6e99746 MJ |
868 | |
869 | /* cfg fix */ | |
726a989a | 870 | e02 = split_block (bb0, cond_stmt); |
b6e99746 MJ |
871 | bb2 = e02->dest; |
872 | ||
789410e4 | 873 | if (m_default_case_nonstandard) |
18bfe940 JJ |
874 | { |
875 | bb1 = bb2; | |
789410e4 | 876 | bb2 = m_default_bb; |
18bfe940 JJ |
877 | e01 = e02; |
878 | e01->flags = EDGE_TRUE_VALUE; | |
879 | e02 = make_edge (bb0, bb2, EDGE_FALSE_VALUE); | |
880 | edge e_default = find_edge (bb1, bb2); | |
881 | for (gphi_iterator gsi = gsi_start_phis (bb2); | |
882 | !gsi_end_p (gsi); gsi_next (&gsi)) | |
883 | { | |
884 | gphi *phi = gsi.phi (); | |
885 | tree arg = PHI_ARG_DEF_FROM_EDGE (phi, e_default); | |
886 | add_phi_arg (phi, arg, e02, | |
887 | gimple_phi_arg_location_from_edge (phi, e_default)); | |
888 | } | |
889 | /* Partially fix the dominator tree, if it is available. */ | |
890 | if (dom_info_available_p (CDI_DOMINATORS)) | |
891 | redirect_immediate_dominators (CDI_DOMINATORS, bb1, bb0); | |
892 | } | |
893 | else | |
894 | { | |
895 | e21 = split_block (bb2, last_assign); | |
896 | bb1 = e21->dest; | |
897 | remove_edge (e21); | |
898 | } | |
b6e99746 | 899 | |
789410e4 | 900 | e1d = split_block (bb1, m_arr_ref_last); |
b6e99746 MJ |
901 | bbd = e1d->dest; |
902 | remove_edge (e1d); | |
903 | ||
789410e4 ML |
904 | /* Flags and profiles of the edge for in-range values. */ |
905 | if (!m_default_case_nonstandard) | |
18bfe940 | 906 | e01 = make_edge (bb0, bb1, EDGE_TRUE_VALUE); |
789410e4 | 907 | e01->probability = m_default_prob.invert (); |
b6e99746 | 908 | |
789410e4 | 909 | /* Flags and profiles of the edge taking care of out-of-range values. */ |
b6e99746 MJ |
910 | e02->flags &= ~EDGE_FALLTHRU; |
911 | e02->flags |= EDGE_FALSE_VALUE; | |
789410e4 | 912 | e02->probability = m_default_prob; |
b6e99746 | 913 | |
789410e4 | 914 | bbf = m_final_bb; |
b6e99746 MJ |
915 | |
916 | e1f = make_edge (bb1, bbf, EDGE_FALLTHRU); | |
357067f2 | 917 | e1f->probability = profile_probability::always (); |
b6e99746 | 918 | |
789410e4 | 919 | if (m_default_case_nonstandard) |
18bfe940 JJ |
920 | e2f = NULL; |
921 | else | |
922 | { | |
923 | e2f = make_edge (bb2, bbf, EDGE_FALLTHRU); | |
357067f2 | 924 | e2f->probability = profile_probability::always (); |
18bfe940 | 925 | } |
b6e99746 MJ |
926 | |
927 | /* frequencies of the new BBs */ | |
e7a74006 JH |
928 | bb1->count = e01->count (); |
929 | bb2->count = e02->count (); | |
789410e4 | 930 | if (!m_default_case_nonstandard) |
e7a74006 | 931 | bbf->count = e1f->count () + e2f->count (); |
b6e99746 | 932 | |
6ab1ab14 | 933 | /* Tidy blocks that have become unreachable. */ |
789410e4 ML |
934 | prune_bbs (bbd, m_final_bb, |
935 | m_default_case_nonstandard ? m_default_bb : NULL); | |
b6e99746 | 936 | |
6ab1ab14 | 937 | /* Fixup the PHI nodes in bbF. */ |
789410e4 | 938 | fix_phi_nodes (e1f, e2f, bbf); |
b6e99746 | 939 | |
6ab1ab14 SB |
940 | /* Fix the dominator tree, if it is available. */ |
941 | if (dom_info_available_p (CDI_DOMINATORS)) | |
942 | { | |
9771b263 | 943 | vec<basic_block> bbs_to_fix_dom; |
6ab1ab14 SB |
944 | |
945 | set_immediate_dominator (CDI_DOMINATORS, bb1, bb0); | |
789410e4 | 946 | if (!m_default_case_nonstandard) |
18bfe940 | 947 | set_immediate_dominator (CDI_DOMINATORS, bb2, bb0); |
531b10fc | 948 | if (! get_immediate_dominator (CDI_DOMINATORS, bbf)) |
6ab1ab14 SB |
949 | /* If bbD was the immediate dominator ... */ |
950 | set_immediate_dominator (CDI_DOMINATORS, bbf, bb0); | |
951 | ||
18bfe940 | 952 | bbs_to_fix_dom.create (3 + (bb2 != bbf)); |
9771b263 DN |
953 | bbs_to_fix_dom.quick_push (bb0); |
954 | bbs_to_fix_dom.quick_push (bb1); | |
18bfe940 JJ |
955 | if (bb2 != bbf) |
956 | bbs_to_fix_dom.quick_push (bb2); | |
9771b263 | 957 | bbs_to_fix_dom.quick_push (bbf); |
6ab1ab14 SB |
958 | |
959 | iterate_fix_dominators (CDI_DOMINATORS, bbs_to_fix_dom, true); | |
9771b263 | 960 | bbs_to_fix_dom.release (); |
6ab1ab14 | 961 | } |
b6e99746 MJ |
962 | } |
963 | ||
789410e4 ML |
964 | /* The following function is invoked on every switch statement (the current |
965 | one is given in SWTCH) and runs the individual phases of switch | |
966 | conversion on it one after another until one fails or the conversion | |
967 | is completed. On success, NULL is in m_reason, otherwise points | |
968 | to a string with the reason why the conversion failed. */ | |
b6e99746 | 969 | |
789410e4 ML |
970 | void |
971 | switch_conversion::expand (gswitch *swtch) | |
b6e99746 | 972 | { |
238065a7 SB |
973 | /* Group case labels so that we get the right results from the heuristics |
974 | that decide on the code generation approach for this switch. */ | |
789410e4 | 975 | m_cfg_altered |= group_case_labels_stmt (swtch); |
d78bcb13 ML |
976 | |
977 | /* If this switch is now a degenerate case with only a default label, | |
978 | there is nothing left for us to do. */ | |
979 | if (gimple_switch_num_labels (swtch) < 2) | |
980 | { | |
981 | m_reason = "switch is a degenerate case"; | |
982 | return; | |
983 | } | |
886cd84f | 984 | |
789410e4 | 985 | collect (swtch); |
886cd84f SB |
986 | |
987 | /* No error markers should reach here (they should be filtered out | |
988 | during gimplification). */ | |
789410e4 | 989 | gcc_checking_assert (TREE_TYPE (m_index_expr) != error_mark_node); |
886cd84f | 990 | |
531b10fc | 991 | /* A switch on a constant should have been optimized in tree-cfg-cleanup. */ |
789410e4 | 992 | gcc_checking_assert (!TREE_CONSTANT (m_index_expr)); |
886cd84f | 993 | |
dc223ad4 ML |
994 | /* Prefer bit test if possible. */ |
995 | if (tree_fits_uhwi_p (m_range_size) | |
996 | && bit_test_cluster::can_be_handled (tree_to_uhwi (m_range_size), m_uniq) | |
997 | && bit_test_cluster::is_beneficial (m_count, m_uniq)) | |
998 | { | |
999 | m_reason = "expanding as bit test is preferable"; | |
1000 | return; | |
1001 | } | |
1002 | ||
1003 | if (m_uniq <= 2) | |
1004 | { | |
1005 | /* This will be expanded as a decision tree . */ | |
1006 | m_reason = "expanding as jumps is preferable"; | |
1007 | return; | |
1008 | } | |
1009 | ||
789410e4 ML |
1010 | /* If there is no common successor, we cannot do the transformation. */ |
1011 | if (!m_final_bb) | |
886cd84f | 1012 | { |
789410e4 ML |
1013 | m_reason = "no common successor to all case label target blocks found"; |
1014 | return; | |
886cd84f | 1015 | } |
b6e99746 MJ |
1016 | |
1017 | /* Check the case label values are within reasonable range: */ | |
789410e4 | 1018 | if (!check_range ()) |
fade902a | 1019 | { |
789410e4 ML |
1020 | gcc_assert (m_reason); |
1021 | return; | |
fade902a | 1022 | } |
b6e99746 MJ |
1023 | |
1024 | /* For all the cases, see whether they are empty, the assignments they | |
1025 | represent constant and so on... */ | |
789410e4 | 1026 | if (!check_all_empty_except_final ()) |
8e97bc2b | 1027 | { |
789410e4 ML |
1028 | gcc_assert (m_reason); |
1029 | return; | |
8e97bc2b | 1030 | } |
789410e4 | 1031 | if (!check_final_bb ()) |
fade902a | 1032 | { |
789410e4 ML |
1033 | gcc_assert (m_reason); |
1034 | return; | |
fade902a | 1035 | } |
b6e99746 MJ |
1036 | |
1037 | /* At this point all checks have passed and we can proceed with the | |
1038 | transformation. */ | |
1039 | ||
789410e4 ML |
1040 | create_temp_arrays (); |
1041 | gather_default_values (m_default_case_nonstandard | |
18bfe940 | 1042 | ? gimple_switch_label (swtch, 1) |
789410e4 ML |
1043 | : gimple_switch_default_label (swtch)); |
1044 | build_constructors (); | |
b6e99746 | 1045 | |
789410e4 ML |
1046 | build_arrays (); /* Build the static arrays and assignments. */ |
1047 | gen_inbound_check (); /* Build the bounds check. */ | |
b6e99746 | 1048 | |
789410e4 ML |
1049 | m_cfg_altered = true; |
1050 | } | |
1051 | ||
1052 | /* Destructor. */ | |
1053 | ||
1054 | switch_conversion::~switch_conversion () | |
1055 | { | |
1056 | XDELETEVEC (m_constructors); | |
1057 | XDELETEVEC (m_default_values); | |
b6e99746 MJ |
1058 | } |
1059 | ||
dc223ad4 | 1060 | /* Constructor. */ |
be55bfe6 | 1061 | |
dc223ad4 ML |
1062 | group_cluster::group_cluster (vec<cluster *> &clusters, |
1063 | unsigned start, unsigned end) | |
be55bfe6 | 1064 | { |
dc223ad4 ML |
1065 | gcc_checking_assert (end - start + 1 >= 1); |
1066 | m_prob = profile_probability::never (); | |
1067 | m_cases.create (end - start + 1); | |
1068 | for (unsigned i = start; i <= end; i++) | |
1069 | { | |
1070 | m_cases.quick_push (static_cast<simple_cluster *> (clusters[i])); | |
1071 | m_prob += clusters[i]->m_prob; | |
1072 | } | |
1073 | m_subtree_prob = m_prob; | |
1074 | } | |
be55bfe6 | 1075 | |
dc223ad4 ML |
1076 | /* Destructor. */ |
1077 | ||
1078 | group_cluster::~group_cluster () | |
be55bfe6 | 1079 | { |
dc223ad4 ML |
1080 | for (unsigned i = 0; i < m_cases.length (); i++) |
1081 | delete m_cases[i]; | |
be55bfe6 | 1082 | |
dc223ad4 ML |
1083 | m_cases.release (); |
1084 | } | |
be55bfe6 | 1085 | |
dc223ad4 | 1086 | /* Dump content of a cluster. */ |
be55bfe6 | 1087 | |
dc223ad4 ML |
1088 | void |
1089 | group_cluster::dump (FILE *f, bool details) | |
b6e99746 | 1090 | { |
dc223ad4 ML |
1091 | unsigned total_values = 0; |
1092 | for (unsigned i = 0; i < m_cases.length (); i++) | |
1093 | total_values += m_cases[i]->get_range (m_cases[i]->get_low (), | |
1094 | m_cases[i]->get_high ()); | |
726a989a | 1095 | |
dc223ad4 ML |
1096 | unsigned comparison_count = 0; |
1097 | for (unsigned i = 0; i < m_cases.length (); i++) | |
1098 | { | |
1099 | simple_cluster *sc = static_cast<simple_cluster *> (m_cases[i]); | |
1100 | comparison_count += sc->m_range_p ? 2 : 1; | |
1101 | } | |
b6e99746 | 1102 | |
dc223ad4 ML |
1103 | unsigned HOST_WIDE_INT range = get_range (get_low (), get_high ()); |
1104 | fprintf (f, "%s", get_type () == JUMP_TABLE ? "JT" : "BT"); | |
531b10fc | 1105 | |
dc223ad4 ML |
1106 | if (details) |
1107 | fprintf (f, "(values:%d comparisons:%d range:" HOST_WIDE_INT_PRINT_DEC | |
1108 | " density: %.2f%%)", total_values, comparison_count, range, | |
1109 | 100.0f * comparison_count / range); | |
b6e99746 | 1110 | |
dc223ad4 ML |
1111 | fprintf (f, ":"); |
1112 | PRINT_CASE (f, get_low ()); | |
1113 | fprintf (f, "-"); | |
1114 | PRINT_CASE (f, get_high ()); | |
1115 | fprintf (f, " "); | |
b6e99746 MJ |
1116 | } |
1117 | ||
dc223ad4 | 1118 | /* Emit GIMPLE code to handle the cluster. */ |
27a4cd48 | 1119 | |
dc223ad4 ML |
1120 | void |
1121 | jump_table_cluster::emit (tree index_expr, tree, | |
1122 | tree default_label_expr, basic_block default_bb) | |
27a4cd48 | 1123 | { |
dbdfaaba ML |
1124 | unsigned HOST_WIDE_INT range = get_range (get_low (), get_high ()); |
1125 | unsigned HOST_WIDE_INT nondefault_range = 0; | |
1126 | ||
dc223ad4 ML |
1127 | /* For jump table we just emit a new gswitch statement that will |
1128 | be latter lowered to jump table. */ | |
1129 | auto_vec <tree> labels; | |
1130 | labels.create (m_cases.length ()); | |
1131 | ||
1132 | make_edge (m_case_bb, default_bb, 0); | |
1133 | for (unsigned i = 0; i < m_cases.length (); i++) | |
1134 | { | |
1135 | labels.quick_push (unshare_expr (m_cases[i]->m_case_label_expr)); | |
1136 | make_edge (m_case_bb, m_cases[i]->m_case_bb, 0); | |
1137 | } | |
1138 | ||
1139 | gswitch *s = gimple_build_switch (index_expr, | |
1140 | unshare_expr (default_label_expr), labels); | |
1141 | gimple_stmt_iterator gsi = gsi_start_bb (m_case_bb); | |
1142 | gsi_insert_after (&gsi, s, GSI_NEW_STMT); | |
dbdfaaba ML |
1143 | |
1144 | /* Set up even probabilities for all cases. */ | |
1145 | for (unsigned i = 0; i < m_cases.length (); i++) | |
1146 | { | |
1147 | simple_cluster *sc = static_cast<simple_cluster *> (m_cases[i]); | |
1148 | edge case_edge = find_edge (m_case_bb, sc->m_case_bb); | |
1149 | unsigned HOST_WIDE_INT case_range | |
1150 | = sc->get_range (sc->get_low (), sc->get_high ()); | |
1151 | nondefault_range += case_range; | |
1152 | ||
1153 | /* case_edge->aux is number of values in a jump-table that are covered | |
1154 | by the case_edge. */ | |
1155 | case_edge->aux = (void *) ((intptr_t) (case_edge->aux) + case_range); | |
1156 | } | |
1157 | ||
1158 | edge default_edge = gimple_switch_default_edge (cfun, s); | |
1159 | default_edge->probability = profile_probability::never (); | |
1160 | ||
1161 | for (unsigned i = 0; i < m_cases.length (); i++) | |
1162 | { | |
1163 | simple_cluster *sc = static_cast<simple_cluster *> (m_cases[i]); | |
1164 | edge case_edge = find_edge (m_case_bb, sc->m_case_bb); | |
1165 | case_edge->probability | |
1166 | = profile_probability::always ().apply_scale ((intptr_t)case_edge->aux, | |
1167 | range); | |
1168 | } | |
1169 | ||
1170 | /* Number of non-default values is probability of default edge. */ | |
1171 | default_edge->probability | |
1172 | += profile_probability::always ().apply_scale (nondefault_range, | |
1173 | range).invert (); | |
1174 | ||
1175 | switch_decision_tree::reset_out_edges_aux (s); | |
27a4cd48 | 1176 | } |
9dc3d6a9 | 1177 | |
2f928c1b ML |
1178 | /* Find jump tables of given CLUSTERS, where all members of the vector |
1179 | are of type simple_cluster. New clusters are returned. */ | |
1180 | ||
1181 | vec<cluster *> | |
1182 | jump_table_cluster::find_jump_tables (vec<cluster *> &clusters) | |
1183 | { | |
5885a1bd ML |
1184 | if (!is_enabled ()) |
1185 | return clusters.copy (); | |
1186 | ||
2f928c1b ML |
1187 | unsigned l = clusters.length (); |
1188 | auto_vec<min_cluster_item> min; | |
1189 | min.reserve (l + 1); | |
1190 | ||
1191 | min.quick_push (min_cluster_item (0, 0, 0)); | |
1192 | ||
1193 | for (unsigned i = 1; i <= l; i++) | |
1194 | { | |
1195 | /* Set minimal # of clusters with i-th item to infinite. */ | |
1196 | min.quick_push (min_cluster_item (INT_MAX, INT_MAX, INT_MAX)); | |
1197 | ||
1198 | for (unsigned j = 0; j < i; j++) | |
1199 | { | |
1200 | unsigned HOST_WIDE_INT s = min[j].m_non_jt_cases; | |
1201 | if (i - j < case_values_threshold ()) | |
1202 | s += i - j; | |
1203 | ||
1204 | /* Prefer clusters with smaller number of numbers covered. */ | |
1205 | if ((min[j].m_count + 1 < min[i].m_count | |
1206 | || (min[j].m_count + 1 == min[i].m_count | |
1207 | && s < min[i].m_non_jt_cases)) | |
1208 | && can_be_handled (clusters, j, i - 1)) | |
1209 | min[i] = min_cluster_item (min[j].m_count + 1, j, s); | |
1210 | } | |
df7c7974 ML |
1211 | |
1212 | gcc_checking_assert (min[i].m_count != INT_MAX); | |
2f928c1b ML |
1213 | } |
1214 | ||
1215 | /* No result. */ | |
1216 | if (min[l].m_count == INT_MAX) | |
1217 | return clusters.copy (); | |
1218 | ||
1219 | vec<cluster *> output; | |
1220 | output.create (4); | |
1221 | ||
1222 | /* Find and build the clusters. */ | |
1223 | for (int end = l;;) | |
1224 | { | |
1225 | int start = min[end].m_start; | |
1226 | ||
1227 | /* Do not allow clusters with small number of cases. */ | |
1228 | if (is_beneficial (clusters, start, end - 1)) | |
1229 | output.safe_push (new jump_table_cluster (clusters, start, end - 1)); | |
1230 | else | |
1231 | for (int i = end - 1; i >= start; i--) | |
1232 | output.safe_push (clusters[i]); | |
1233 | ||
1234 | end = start; | |
1235 | ||
1236 | if (start <= 0) | |
1237 | break; | |
1238 | } | |
1239 | ||
1240 | output.reverse (); | |
1241 | return output; | |
1242 | } | |
1243 | ||
dc223ad4 ML |
1244 | /* Return true when cluster starting at START and ending at END (inclusive) |
1245 | can build a jump-table. */ | |
1246 | ||
1247 | bool | |
1248 | jump_table_cluster::can_be_handled (const vec<cluster *> &clusters, | |
1249 | unsigned start, unsigned end) | |
9dc3d6a9 | 1250 | { |
dc223ad4 ML |
1251 | /* If the switch is relatively small such that the cost of one |
1252 | indirect jump on the target are higher than the cost of a | |
1253 | decision tree, go with the decision tree. | |
9dc3d6a9 | 1254 | |
dc223ad4 ML |
1255 | If range of values is much bigger than number of values, |
1256 | or if it is too large to represent in a HOST_WIDE_INT, | |
1257 | make a sequence of conditional branches instead of a dispatch. | |
9dc3d6a9 | 1258 | |
dc223ad4 | 1259 | The definition of "much bigger" depends on whether we are |
de840bde | 1260 | optimizing for size or for speed. */ |
dc223ad4 ML |
1261 | if (!flag_jump_tables) |
1262 | return false; | |
9dc3d6a9 | 1263 | |
df7c7974 ML |
1264 | /* For algorithm correctness, jump table for a single case must return |
1265 | true. We bail out in is_beneficial if it's called just for | |
1266 | a single case. */ | |
1267 | if (start == end) | |
1268 | return true; | |
9dc3d6a9 | 1269 | |
1aabb71d | 1270 | unsigned HOST_WIDE_INT max_ratio |
26f36b50 ML |
1271 | = (optimize_insn_for_size_p () |
1272 | ? PARAM_VALUE (PARAM_JUMP_TABLE_MAX_GROWTH_RATIO_FOR_SIZE) | |
1273 | : PARAM_VALUE (PARAM_JUMP_TABLE_MAX_GROWTH_RATIO_FOR_SPEED)); | |
dc223ad4 ML |
1274 | unsigned HOST_WIDE_INT range = get_range (clusters[start]->get_low (), |
1275 | clusters[end]->get_high ()); | |
1276 | /* Check overflow. */ | |
1277 | if (range == 0) | |
1278 | return false; | |
9dc3d6a9 | 1279 | |
dc223ad4 ML |
1280 | unsigned HOST_WIDE_INT comparison_count = 0; |
1281 | for (unsigned i = start; i <= end; i++) | |
1282 | { | |
1283 | simple_cluster *sc = static_cast<simple_cluster *> (clusters[i]); | |
1284 | comparison_count += sc->m_range_p ? 2 : 1; | |
1285 | } | |
9dc3d6a9 | 1286 | |
26f36b50 | 1287 | return 100 * range <= max_ratio * comparison_count; |
9dc3d6a9 ML |
1288 | } |
1289 | ||
dc223ad4 ML |
1290 | /* Return true if cluster starting at START and ending at END (inclusive) |
1291 | is profitable transformation. */ | |
9dc3d6a9 | 1292 | |
dc223ad4 ML |
1293 | bool |
1294 | jump_table_cluster::is_beneficial (const vec<cluster *> &, | |
1295 | unsigned start, unsigned end) | |
9dc3d6a9 | 1296 | { |
df7c7974 ML |
1297 | /* Single case bail out. */ |
1298 | if (start == end) | |
1299 | return false; | |
1300 | ||
dc223ad4 | 1301 | return end - start + 1 >= case_values_threshold (); |
9dc3d6a9 ML |
1302 | } |
1303 | ||
2f928c1b ML |
1304 | /* Find bit tests of given CLUSTERS, where all members of the vector |
1305 | are of type simple_cluster. New clusters are returned. */ | |
1306 | ||
1307 | vec<cluster *> | |
1308 | bit_test_cluster::find_bit_tests (vec<cluster *> &clusters) | |
1309 | { | |
1310 | vec<cluster *> output; | |
1311 | output.create (4); | |
1312 | ||
1313 | unsigned l = clusters.length (); | |
1314 | auto_vec<min_cluster_item> min; | |
1315 | min.reserve (l + 1); | |
1316 | ||
1317 | min.quick_push (min_cluster_item (0, 0, 0)); | |
1318 | ||
1319 | for (unsigned i = 1; i <= l; i++) | |
1320 | { | |
1321 | /* Set minimal # of clusters with i-th item to infinite. */ | |
1322 | min.quick_push (min_cluster_item (INT_MAX, INT_MAX, INT_MAX)); | |
1323 | ||
1324 | for (unsigned j = 0; j < i; j++) | |
1325 | { | |
1326 | if (min[j].m_count + 1 < min[i].m_count | |
1327 | && can_be_handled (clusters, j, i - 1)) | |
1328 | min[i] = min_cluster_item (min[j].m_count + 1, j, INT_MAX); | |
1329 | } | |
df7c7974 ML |
1330 | |
1331 | gcc_checking_assert (min[i].m_count != INT_MAX); | |
2f928c1b ML |
1332 | } |
1333 | ||
1334 | /* No result. */ | |
1335 | if (min[l].m_count == INT_MAX) | |
1336 | return clusters.copy (); | |
1337 | ||
1338 | /* Find and build the clusters. */ | |
377afcd5 | 1339 | for (unsigned end = l;;) |
2f928c1b ML |
1340 | { |
1341 | int start = min[end].m_start; | |
1342 | ||
1343 | if (is_beneficial (clusters, start, end - 1)) | |
377afcd5 ML |
1344 | { |
1345 | bool entire = start == 0 && end == clusters.length (); | |
1346 | output.safe_push (new bit_test_cluster (clusters, start, end - 1, | |
1347 | entire)); | |
1348 | } | |
2f928c1b ML |
1349 | else |
1350 | for (int i = end - 1; i >= start; i--) | |
1351 | output.safe_push (clusters[i]); | |
1352 | ||
1353 | end = start; | |
1354 | ||
1355 | if (start <= 0) | |
1356 | break; | |
1357 | } | |
1358 | ||
1359 | output.reverse (); | |
1360 | return output; | |
1361 | } | |
1362 | ||
dc223ad4 ML |
1363 | /* Return true when RANGE of case values with UNIQ labels |
1364 | can build a bit test. */ | |
9dc3d6a9 | 1365 | |
dc223ad4 ML |
1366 | bool |
1367 | bit_test_cluster::can_be_handled (unsigned HOST_WIDE_INT range, | |
1368 | unsigned int uniq) | |
9dc3d6a9 | 1369 | { |
dc223ad4 ML |
1370 | /* Check overflow. */ |
1371 | if (range == 0) | |
1372 | return 0; | |
1373 | ||
1374 | if (range >= GET_MODE_BITSIZE (word_mode)) | |
1375 | return false; | |
1376 | ||
1377 | return uniq <= 3; | |
1378 | } | |
1379 | ||
1380 | /* Return true when cluster starting at START and ending at END (inclusive) | |
1381 | can build a bit test. */ | |
1382 | ||
1383 | bool | |
1384 | bit_test_cluster::can_be_handled (const vec<cluster *> &clusters, | |
1385 | unsigned start, unsigned end) | |
1386 | { | |
df7c7974 ML |
1387 | /* For algorithm correctness, bit test for a single case must return |
1388 | true. We bail out in is_beneficial if it's called just for | |
1389 | a single case. */ | |
1390 | if (start == end) | |
1391 | return true; | |
1392 | ||
dc223ad4 ML |
1393 | unsigned HOST_WIDE_INT range = get_range (clusters[start]->get_low (), |
1394 | clusters[end]->get_high ()); | |
1395 | auto_bitmap dest_bbs; | |
1396 | ||
1397 | for (unsigned i = start; i <= end; i++) | |
9dc3d6a9 | 1398 | { |
dc223ad4 ML |
1399 | simple_cluster *sc = static_cast<simple_cluster *> (clusters[i]); |
1400 | bitmap_set_bit (dest_bbs, sc->m_case_bb->index); | |
9dc3d6a9 | 1401 | } |
9dc3d6a9 | 1402 | |
dc223ad4 ML |
1403 | return can_be_handled (range, bitmap_count_bits (dest_bbs)); |
1404 | } | |
9dc3d6a9 | 1405 | |
dc223ad4 ML |
1406 | /* Return true when COUNT of cases of UNIQ labels is beneficial for bit test |
1407 | transformation. */ | |
9dc3d6a9 | 1408 | |
dc223ad4 ML |
1409 | bool |
1410 | bit_test_cluster::is_beneficial (unsigned count, unsigned uniq) | |
9dc3d6a9 | 1411 | { |
dc223ad4 ML |
1412 | return (((uniq == 1 && count >= 3) |
1413 | || (uniq == 2 && count >= 5) | |
1414 | || (uniq == 3 && count >= 6))); | |
9dc3d6a9 ML |
1415 | } |
1416 | ||
dc223ad4 ML |
1417 | /* Return true if cluster starting at START and ending at END (inclusive) |
1418 | is profitable transformation. */ | |
9dc3d6a9 | 1419 | |
dc223ad4 ML |
1420 | bool |
1421 | bit_test_cluster::is_beneficial (const vec<cluster *> &clusters, | |
1422 | unsigned start, unsigned end) | |
9dc3d6a9 | 1423 | { |
df7c7974 ML |
1424 | /* Single case bail out. */ |
1425 | if (start == end) | |
1426 | return false; | |
1427 | ||
dc223ad4 | 1428 | auto_bitmap dest_bbs; |
9dc3d6a9 | 1429 | |
dc223ad4 | 1430 | for (unsigned i = start; i <= end; i++) |
9dc3d6a9 | 1431 | { |
dc223ad4 ML |
1432 | simple_cluster *sc = static_cast<simple_cluster *> (clusters[i]); |
1433 | bitmap_set_bit (dest_bbs, sc->m_case_bb->index); | |
9dc3d6a9 | 1434 | } |
9dc3d6a9 | 1435 | |
dc223ad4 ML |
1436 | unsigned uniq = bitmap_count_bits (dest_bbs); |
1437 | unsigned count = end - start + 1; | |
1438 | return is_beneficial (count, uniq); | |
9dc3d6a9 ML |
1439 | } |
1440 | ||
dc223ad4 ML |
1441 | /* Comparison function for qsort to order bit tests by decreasing |
1442 | probability of execution. */ | |
9dc3d6a9 | 1443 | |
dc223ad4 ML |
1444 | int |
1445 | case_bit_test::cmp (const void *p1, const void *p2) | |
1446 | { | |
1447 | const struct case_bit_test *const d1 = (const struct case_bit_test *) p1; | |
1448 | const struct case_bit_test *const d2 = (const struct case_bit_test *) p2; | |
1449 | ||
1450 | if (d2->bits != d1->bits) | |
1451 | return d2->bits - d1->bits; | |
1452 | ||
1453 | /* Stabilize the sort. */ | |
1454 | return (LABEL_DECL_UID (CASE_LABEL (d2->label)) | |
1455 | - LABEL_DECL_UID (CASE_LABEL (d1->label))); | |
1456 | } | |
1457 | ||
1458 | /* Expand a switch statement by a short sequence of bit-wise | |
1459 | comparisons. "switch(x)" is effectively converted into | |
1460 | "if ((1 << (x-MINVAL)) & CST)" where CST and MINVAL are | |
1461 | integer constants. | |
1462 | ||
1463 | INDEX_EXPR is the value being switched on. | |
1464 | ||
1465 | MINVAL is the lowest case value of in the case nodes, | |
1466 | and RANGE is highest value minus MINVAL. MINVAL and RANGE | |
1467 | are not guaranteed to be of the same type as INDEX_EXPR | |
1468 | (the gimplifier doesn't change the type of case label values, | |
1469 | and MINVAL and RANGE are derived from those values). | |
1470 | MAXVAL is MINVAL + RANGE. | |
9dc3d6a9 | 1471 | |
dc223ad4 ML |
1472 | There *MUST* be max_case_bit_tests or less unique case |
1473 | node targets. */ | |
1474 | ||
1475 | void | |
1476 | bit_test_cluster::emit (tree index_expr, tree index_type, | |
1477 | tree, basic_block default_bb) | |
9dc3d6a9 | 1478 | { |
dc223ad4 ML |
1479 | struct case_bit_test test[m_max_case_bit_tests] = { {} }; |
1480 | unsigned int i, j, k; | |
1481 | unsigned int count; | |
9dc3d6a9 | 1482 | |
dc223ad4 | 1483 | tree unsigned_index_type = unsigned_type_for (index_type); |
9dc3d6a9 | 1484 | |
dc223ad4 ML |
1485 | gimple_stmt_iterator gsi; |
1486 | gassign *shift_stmt; | |
9dc3d6a9 | 1487 | |
dc223ad4 ML |
1488 | tree idx, tmp, csui; |
1489 | tree word_type_node = lang_hooks.types.type_for_mode (word_mode, 1); | |
1490 | tree word_mode_zero = fold_convert (word_type_node, integer_zero_node); | |
1491 | tree word_mode_one = fold_convert (word_type_node, integer_one_node); | |
1492 | int prec = TYPE_PRECISION (word_type_node); | |
1493 | wide_int wone = wi::one (prec); | |
9dc3d6a9 | 1494 | |
dc223ad4 ML |
1495 | tree minval = get_low (); |
1496 | tree maxval = get_high (); | |
1497 | tree range = int_const_binop (MINUS_EXPR, maxval, minval); | |
377afcd5 | 1498 | unsigned HOST_WIDE_INT bt_range = get_range (minval, maxval); |
9dc3d6a9 | 1499 | |
dc223ad4 ML |
1500 | /* Go through all case labels, and collect the case labels, profile |
1501 | counts, and other information we need to build the branch tests. */ | |
1502 | count = 0; | |
1503 | for (i = 0; i < m_cases.length (); i++) | |
1504 | { | |
1505 | unsigned int lo, hi; | |
1506 | simple_cluster *n = static_cast<simple_cluster *> (m_cases[i]); | |
1507 | for (k = 0; k < count; k++) | |
1508 | if (n->m_case_bb == test[k].target_bb) | |
1509 | break; | |
1510 | ||
1511 | if (k == count) | |
9dc3d6a9 | 1512 | { |
dc223ad4 ML |
1513 | gcc_checking_assert (count < m_max_case_bit_tests); |
1514 | test[k].mask = wi::zero (prec); | |
1515 | test[k].target_bb = n->m_case_bb; | |
1516 | test[k].label = n->m_case_label_expr; | |
377afcd5 | 1517 | test[k].bits = 0; |
dc223ad4 ML |
1518 | count++; |
1519 | } | |
377afcd5 ML |
1520 | |
1521 | test[k].bits += n->get_range (n->get_low (), n->get_high ()); | |
9dc3d6a9 | 1522 | |
dc223ad4 ML |
1523 | lo = tree_to_uhwi (int_const_binop (MINUS_EXPR, n->get_low (), minval)); |
1524 | if (n->get_high () == NULL_TREE) | |
1525 | hi = lo; | |
1526 | else | |
1527 | hi = tree_to_uhwi (int_const_binop (MINUS_EXPR, n->get_high (), | |
1528 | minval)); | |
9dc3d6a9 | 1529 | |
dc223ad4 ML |
1530 | for (j = lo; j <= hi; j++) |
1531 | test[k].mask |= wi::lshift (wone, j); | |
1532 | } | |
1533 | ||
1534 | qsort (test, count, sizeof (*test), case_bit_test::cmp); | |
1535 | ||
1536 | /* If all values are in the 0 .. BITS_PER_WORD-1 range, we can get rid of | |
1537 | the minval subtractions, but it might make the mask constants more | |
1538 | expensive. So, compare the costs. */ | |
1539 | if (compare_tree_int (minval, 0) > 0 | |
1540 | && compare_tree_int (maxval, GET_MODE_BITSIZE (word_mode)) < 0) | |
1541 | { | |
1542 | int cost_diff; | |
1543 | HOST_WIDE_INT m = tree_to_uhwi (minval); | |
1544 | rtx reg = gen_raw_REG (word_mode, 10000); | |
1545 | bool speed_p = optimize_insn_for_speed_p (); | |
1546 | cost_diff = set_rtx_cost (gen_rtx_PLUS (word_mode, reg, | |
1547 | GEN_INT (-m)), speed_p); | |
1548 | for (i = 0; i < count; i++) | |
1549 | { | |
1550 | rtx r = immed_wide_int_const (test[i].mask, word_mode); | |
1551 | cost_diff += set_src_cost (gen_rtx_AND (word_mode, reg, r), | |
1552 | word_mode, speed_p); | |
1553 | r = immed_wide_int_const (wi::lshift (test[i].mask, m), word_mode); | |
1554 | cost_diff -= set_src_cost (gen_rtx_AND (word_mode, reg, r), | |
1555 | word_mode, speed_p); | |
9dc3d6a9 | 1556 | } |
dc223ad4 | 1557 | if (cost_diff > 0) |
9dc3d6a9 | 1558 | { |
dc223ad4 ML |
1559 | for (i = 0; i < count; i++) |
1560 | test[i].mask = wi::lshift (test[i].mask, m); | |
1561 | minval = build_zero_cst (TREE_TYPE (minval)); | |
1562 | range = maxval; | |
9dc3d6a9 ML |
1563 | } |
1564 | } | |
9dc3d6a9 | 1565 | |
dc223ad4 ML |
1566 | /* Now build the test-and-branch code. */ |
1567 | ||
1568 | gsi = gsi_last_bb (m_case_bb); | |
1569 | ||
1570 | /* idx = (unsigned)x - minval. */ | |
1571 | idx = fold_convert (unsigned_index_type, index_expr); | |
1572 | idx = fold_build2 (MINUS_EXPR, unsigned_index_type, idx, | |
1573 | fold_convert (unsigned_index_type, minval)); | |
1574 | idx = force_gimple_operand_gsi (&gsi, idx, | |
1575 | /*simple=*/true, NULL_TREE, | |
1576 | /*before=*/true, GSI_SAME_STMT); | |
1577 | ||
377afcd5 ML |
1578 | if (m_handles_entire_switch) |
1579 | { | |
1580 | /* if (idx > range) goto default */ | |
1581 | range | |
1582 | = force_gimple_operand_gsi (&gsi, | |
dc223ad4 ML |
1583 | fold_convert (unsigned_index_type, range), |
1584 | /*simple=*/true, NULL_TREE, | |
1585 | /*before=*/true, GSI_SAME_STMT); | |
377afcd5 ML |
1586 | tmp = fold_build2 (GT_EXPR, boolean_type_node, idx, range); |
1587 | basic_block new_bb | |
1588 | = hoist_edge_and_branch_if_true (&gsi, tmp, default_bb, | |
1589 | profile_probability::unlikely ()); | |
1590 | gsi = gsi_last_bb (new_bb); | |
1591 | } | |
dc223ad4 ML |
1592 | |
1593 | /* csui = (1 << (word_mode) idx) */ | |
1594 | csui = make_ssa_name (word_type_node); | |
1595 | tmp = fold_build2 (LSHIFT_EXPR, word_type_node, word_mode_one, | |
1596 | fold_convert (word_type_node, idx)); | |
1597 | tmp = force_gimple_operand_gsi (&gsi, tmp, | |
1598 | /*simple=*/false, NULL_TREE, | |
1599 | /*before=*/true, GSI_SAME_STMT); | |
1600 | shift_stmt = gimple_build_assign (csui, tmp); | |
1601 | gsi_insert_before (&gsi, shift_stmt, GSI_SAME_STMT); | |
1602 | update_stmt (shift_stmt); | |
1603 | ||
377afcd5 ML |
1604 | profile_probability prob = profile_probability::always (); |
1605 | ||
dc223ad4 ML |
1606 | /* for each unique set of cases: |
1607 | if (const & csui) goto target */ | |
1608 | for (k = 0; k < count; k++) | |
1609 | { | |
377afcd5 ML |
1610 | prob = profile_probability::always ().apply_scale (test[k].bits, |
1611 | bt_range); | |
1612 | bt_range -= test[k].bits; | |
dc223ad4 ML |
1613 | tmp = wide_int_to_tree (word_type_node, test[k].mask); |
1614 | tmp = fold_build2 (BIT_AND_EXPR, word_type_node, csui, tmp); | |
1615 | tmp = force_gimple_operand_gsi (&gsi, tmp, | |
1616 | /*simple=*/true, NULL_TREE, | |
1617 | /*before=*/true, GSI_SAME_STMT); | |
1618 | tmp = fold_build2 (NE_EXPR, boolean_type_node, tmp, word_mode_zero); | |
377afcd5 ML |
1619 | basic_block new_bb |
1620 | = hoist_edge_and_branch_if_true (&gsi, tmp, test[k].target_bb, prob); | |
dc223ad4 ML |
1621 | gsi = gsi_last_bb (new_bb); |
1622 | } | |
9dc3d6a9 | 1623 | |
dc223ad4 ML |
1624 | /* We should have removed all edges now. */ |
1625 | gcc_assert (EDGE_COUNT (gsi_bb (gsi)->succs) == 0); | |
9dc3d6a9 | 1626 | |
dc223ad4 | 1627 | /* If nothing matched, go to the default label. */ |
377afcd5 ML |
1628 | edge e = make_edge (gsi_bb (gsi), default_bb, EDGE_FALLTHRU); |
1629 | e->probability = profile_probability::always (); | |
dc223ad4 | 1630 | } |
9dc3d6a9 | 1631 | |
dc223ad4 ML |
1632 | /* Split the basic block at the statement pointed to by GSIP, and insert |
1633 | a branch to the target basic block of E_TRUE conditional on tree | |
1634 | expression COND. | |
9dc3d6a9 | 1635 | |
dc223ad4 ML |
1636 | It is assumed that there is already an edge from the to-be-split |
1637 | basic block to E_TRUE->dest block. This edge is removed, and the | |
1638 | profile information on the edge is re-used for the new conditional | |
1639 | jump. | |
9dc3d6a9 | 1640 | |
dc223ad4 ML |
1641 | The CFG is updated. The dominator tree will not be valid after |
1642 | this transformation, but the immediate dominators are updated if | |
1643 | UPDATE_DOMINATORS is true. | |
9dc3d6a9 | 1644 | |
dc223ad4 | 1645 | Returns the newly created basic block. */ |
9dc3d6a9 | 1646 | |
dc223ad4 ML |
1647 | basic_block |
1648 | bit_test_cluster::hoist_edge_and_branch_if_true (gimple_stmt_iterator *gsip, | |
377afcd5 ML |
1649 | tree cond, basic_block case_bb, |
1650 | profile_probability prob) | |
9dc3d6a9 | 1651 | { |
dc223ad4 ML |
1652 | tree tmp; |
1653 | gcond *cond_stmt; | |
1654 | edge e_false; | |
1655 | basic_block new_bb, split_bb = gsi_bb (*gsip); | |
9dc3d6a9 | 1656 | |
dc223ad4 | 1657 | edge e_true = make_edge (split_bb, case_bb, EDGE_TRUE_VALUE); |
377afcd5 | 1658 | e_true->probability = prob; |
dc223ad4 | 1659 | gcc_assert (e_true->src == split_bb); |
9dc3d6a9 | 1660 | |
dc223ad4 ML |
1661 | tmp = force_gimple_operand_gsi (gsip, cond, /*simple=*/true, NULL, |
1662 | /*before=*/true, GSI_SAME_STMT); | |
1663 | cond_stmt = gimple_build_cond_from_tree (tmp, NULL_TREE, NULL_TREE); | |
1664 | gsi_insert_before (gsip, cond_stmt, GSI_SAME_STMT); | |
9dc3d6a9 | 1665 | |
dc223ad4 ML |
1666 | e_false = split_block (split_bb, cond_stmt); |
1667 | new_bb = e_false->dest; | |
1668 | redirect_edge_pred (e_true, split_bb); | |
9dc3d6a9 | 1669 | |
dc223ad4 ML |
1670 | e_false->flags &= ~EDGE_FALLTHRU; |
1671 | e_false->flags |= EDGE_FALSE_VALUE; | |
1672 | e_false->probability = e_true->probability.invert (); | |
1673 | new_bb->count = e_false->count (); | |
1674 | ||
1675 | return new_bb; | |
9dc3d6a9 ML |
1676 | } |
1677 | ||
dc223ad4 ML |
1678 | /* Compute the number of case labels that correspond to each outgoing edge of |
1679 | switch statement. Record this information in the aux field of the edge. */ | |
9dc3d6a9 | 1680 | |
dc223ad4 ML |
1681 | void |
1682 | switch_decision_tree::compute_cases_per_edge () | |
1683 | { | |
dbdfaaba | 1684 | reset_out_edges_aux (m_switch); |
dc223ad4 ML |
1685 | int ncases = gimple_switch_num_labels (m_switch); |
1686 | for (int i = ncases - 1; i >= 1; --i) | |
1687 | { | |
61ff5d6f | 1688 | edge case_edge = gimple_switch_edge (cfun, m_switch, i); |
dc223ad4 ML |
1689 | case_edge->aux = (void *) ((intptr_t) (case_edge->aux) + 1); |
1690 | } | |
1691 | } | |
1692 | ||
1693 | /* Analyze switch statement and return true when the statement is expanded | |
1694 | as decision tree. */ | |
9dc3d6a9 | 1695 | |
dc223ad4 ML |
1696 | bool |
1697 | switch_decision_tree::analyze_switch_statement () | |
9dc3d6a9 | 1698 | { |
dc223ad4 ML |
1699 | unsigned l = gimple_switch_num_labels (m_switch); |
1700 | basic_block bb = gimple_bb (m_switch); | |
1701 | auto_vec<cluster *> clusters; | |
1702 | clusters.create (l - 1); | |
1703 | ||
61ff5d6f | 1704 | basic_block default_bb = gimple_switch_default_bb (cfun, m_switch); |
dc223ad4 ML |
1705 | m_case_bbs.reserve (l); |
1706 | m_case_bbs.quick_push (default_bb); | |
1707 | ||
1708 | compute_cases_per_edge (); | |
1709 | ||
1710 | for (unsigned i = 1; i < l; i++) | |
1711 | { | |
1712 | tree elt = gimple_switch_label (m_switch, i); | |
1713 | tree lab = CASE_LABEL (elt); | |
61ff5d6f | 1714 | basic_block case_bb = label_to_block (cfun, lab); |
dc223ad4 ML |
1715 | edge case_edge = find_edge (bb, case_bb); |
1716 | tree low = CASE_LOW (elt); | |
1717 | tree high = CASE_HIGH (elt); | |
1718 | ||
1719 | profile_probability p | |
1720 | = case_edge->probability.apply_scale (1, (intptr_t) (case_edge->aux)); | |
61ff5d6f ML |
1721 | clusters.quick_push (new simple_cluster (low, high, elt, case_edge->dest, |
1722 | p)); | |
1723 | m_case_bbs.quick_push (case_edge->dest); | |
dc223ad4 ML |
1724 | } |
1725 | ||
dbdfaaba | 1726 | reset_out_edges_aux (m_switch); |
dc223ad4 | 1727 | |
2f928c1b ML |
1728 | /* Find jump table clusters. */ |
1729 | vec<cluster *> output = jump_table_cluster::find_jump_tables (clusters); | |
1730 | ||
df7c7974 | 1731 | /* Find bit test clusters. */ |
2f928c1b ML |
1732 | vec<cluster *> output2; |
1733 | auto_vec<cluster *> tmp; | |
1734 | output2.create (1); | |
1735 | tmp.create (1); | |
1736 | ||
1737 | for (unsigned i = 0; i < output.length (); i++) | |
1738 | { | |
1739 | cluster *c = output[i]; | |
1740 | if (c->get_type () != SIMPLE_CASE) | |
1741 | { | |
1742 | if (!tmp.is_empty ()) | |
1743 | { | |
1744 | vec<cluster *> n = bit_test_cluster::find_bit_tests (tmp); | |
1745 | output2.safe_splice (n); | |
1746 | n.release (); | |
1747 | tmp.truncate (0); | |
1748 | } | |
1749 | output2.safe_push (c); | |
1750 | } | |
1751 | else | |
1752 | tmp.safe_push (c); | |
1753 | } | |
1754 | ||
1755 | /* We still can have a temporary vector to test. */ | |
1756 | if (!tmp.is_empty ()) | |
1757 | { | |
1758 | vec<cluster *> n = bit_test_cluster::find_bit_tests (tmp); | |
1759 | output2.safe_splice (n); | |
1760 | n.release (); | |
1761 | } | |
9dc3d6a9 ML |
1762 | |
1763 | if (dump_file) | |
9dc3d6a9 | 1764 | { |
dc223ad4 | 1765 | fprintf (dump_file, ";; GIMPLE switch case clusters: "); |
2f928c1b ML |
1766 | for (unsigned i = 0; i < output2.length (); i++) |
1767 | output2[i]->dump (dump_file, dump_flags & TDF_DETAILS); | |
dc223ad4 ML |
1768 | fprintf (dump_file, "\n"); |
1769 | } | |
1770 | ||
2f928c1b | 1771 | output.release (); |
dc223ad4 | 1772 | |
2f928c1b ML |
1773 | bool expanded = try_switch_expansion (output2); |
1774 | ||
1775 | for (unsigned i = 0; i < output2.length (); i++) | |
1776 | delete output2[i]; | |
1777 | ||
1778 | output2.release (); | |
dc223ad4 ML |
1779 | |
1780 | return expanded; | |
1781 | } | |
1782 | ||
1783 | /* Attempt to expand CLUSTERS as a decision tree. Return true when | |
1784 | expanded. */ | |
1785 | ||
1786 | bool | |
1787 | switch_decision_tree::try_switch_expansion (vec<cluster *> &clusters) | |
1788 | { | |
1789 | tree index_expr = gimple_switch_index (m_switch); | |
1790 | tree index_type = TREE_TYPE (index_expr); | |
1791 | basic_block bb = gimple_bb (m_switch); | |
1792 | ||
1793 | if (gimple_switch_num_labels (m_switch) == 1) | |
1794 | return false; | |
1795 | ||
1796 | /* Find the default case target label. */ | |
61ff5d6f ML |
1797 | edge default_edge = gimple_switch_default_edge (cfun, m_switch); |
1798 | m_default_bb = default_edge->dest; | |
dc223ad4 ML |
1799 | |
1800 | /* Do the insertion of a case label into m_case_list. The labels are | |
1801 | fed to us in descending order from the sorted vector of case labels used | |
1802 | in the tree part of the middle end. So the list we construct is | |
1803 | sorted in ascending order. */ | |
1804 | ||
1805 | for (int i = clusters.length () - 1; i >= 0; i--) | |
1806 | { | |
1807 | case_tree_node *r = m_case_list; | |
1808 | m_case_list = m_case_node_pool.allocate (); | |
1809 | m_case_list->m_right = r; | |
1810 | m_case_list->m_c = clusters[i]; | |
9dc3d6a9 ML |
1811 | } |
1812 | ||
dc223ad4 ML |
1813 | record_phi_operand_mapping (); |
1814 | ||
1815 | /* Split basic block that contains the gswitch statement. */ | |
9dc3d6a9 ML |
1816 | gimple_stmt_iterator gsi = gsi_last_bb (bb); |
1817 | edge e; | |
1818 | if (gsi_end_p (gsi)) | |
1819 | e = split_block_after_labels (bb); | |
1820 | else | |
1821 | { | |
1822 | gsi_prev (&gsi); | |
1823 | e = split_block (bb, gsi_stmt (gsi)); | |
1824 | } | |
1825 | bb = split_edge (e); | |
1826 | ||
dc223ad4 ML |
1827 | /* Create new basic blocks for non-case clusters where specific expansion |
1828 | needs to happen. */ | |
1829 | for (unsigned i = 0; i < clusters.length (); i++) | |
1830 | if (clusters[i]->get_type () != SIMPLE_CASE) | |
1831 | { | |
1832 | clusters[i]->m_case_bb = create_empty_bb (bb); | |
1833 | clusters[i]->m_case_bb->loop_father = bb->loop_father; | |
1834 | } | |
9dc3d6a9 | 1835 | |
dc223ad4 ML |
1836 | /* Do not do an extra work for a single cluster. */ |
1837 | if (clusters.length () == 1 | |
1838 | && clusters[0]->get_type () != SIMPLE_CASE) | |
3f10efd4 ML |
1839 | { |
1840 | cluster *c = clusters[0]; | |
1841 | c->emit (index_expr, index_type, | |
1842 | gimple_switch_default_label (m_switch), m_default_bb); | |
1843 | redirect_edge_succ (single_succ_edge (bb), c->m_case_bb); | |
1844 | } | |
dc223ad4 ML |
1845 | else |
1846 | { | |
1847 | emit (bb, index_expr, default_edge->probability, index_type); | |
1848 | ||
1849 | /* Emit cluster-specific switch handling. */ | |
1850 | for (unsigned i = 0; i < clusters.length (); i++) | |
1851 | if (clusters[i]->get_type () != SIMPLE_CASE) | |
1852 | clusters[i]->emit (index_expr, index_type, | |
1853 | gimple_switch_default_label (m_switch), | |
1854 | m_default_bb); | |
1855 | } | |
9dc3d6a9 | 1856 | |
dc223ad4 ML |
1857 | fix_phi_operands_for_edges (); |
1858 | ||
1859 | return true; | |
9dc3d6a9 ML |
1860 | } |
1861 | ||
dc223ad4 ML |
1862 | /* Before switch transformation, record all SSA_NAMEs defined in switch BB |
1863 | and used in a label basic block. */ | |
1864 | ||
1865 | void | |
1866 | switch_decision_tree::record_phi_operand_mapping () | |
9dc3d6a9 | 1867 | { |
dc223ad4 | 1868 | basic_block switch_bb = gimple_bb (m_switch); |
9dc3d6a9 | 1869 | /* Record all PHI nodes that have to be fixed after conversion. */ |
dc223ad4 | 1870 | for (unsigned i = 0; i < m_case_bbs.length (); i++) |
9dc3d6a9 | 1871 | { |
9dc3d6a9 | 1872 | gphi_iterator gsi; |
dc223ad4 | 1873 | basic_block bb = m_case_bbs[i]; |
9dc3d6a9 ML |
1874 | for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
1875 | { | |
1876 | gphi *phi = gsi.phi (); | |
1877 | ||
1878 | for (unsigned i = 0; i < gimple_phi_num_args (phi); i++) | |
1879 | { | |
1880 | basic_block phi_src_bb = gimple_phi_arg_edge (phi, i)->src; | |
1881 | if (phi_src_bb == switch_bb) | |
1882 | { | |
1883 | tree def = gimple_phi_arg_def (phi, i); | |
1884 | tree result = gimple_phi_result (phi); | |
dc223ad4 | 1885 | m_phi_mapping.put (result, def); |
9dc3d6a9 ML |
1886 | break; |
1887 | } | |
1888 | } | |
1889 | } | |
1890 | } | |
1891 | } | |
1892 | ||
dc223ad4 ML |
1893 | /* Append new operands to PHI statements that were introduced due to |
1894 | addition of new edges to case labels. */ | |
9dc3d6a9 | 1895 | |
dc223ad4 ML |
1896 | void |
1897 | switch_decision_tree::fix_phi_operands_for_edges () | |
9dc3d6a9 | 1898 | { |
dc223ad4 | 1899 | gphi_iterator gsi; |
9dc3d6a9 | 1900 | |
dc223ad4 ML |
1901 | for (unsigned i = 0; i < m_case_bbs.length (); i++) |
1902 | { | |
1903 | basic_block bb = m_case_bbs[i]; | |
1904 | for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
1905 | { | |
1906 | gphi *phi = gsi.phi (); | |
1907 | for (unsigned j = 0; j < gimple_phi_num_args (phi); j++) | |
1908 | { | |
1909 | tree def = gimple_phi_arg_def (phi, j); | |
1910 | if (def == NULL_TREE) | |
1911 | { | |
1912 | edge e = gimple_phi_arg_edge (phi, j); | |
1913 | tree *definition | |
1914 | = m_phi_mapping.get (gimple_phi_result (phi)); | |
1915 | gcc_assert (definition); | |
1916 | add_phi_arg (phi, *definition, e, UNKNOWN_LOCATION); | |
1917 | } | |
1918 | } | |
1919 | } | |
1920 | } | |
1921 | } | |
9dc3d6a9 | 1922 | |
dc223ad4 ML |
1923 | /* Generate a decision tree, switching on INDEX_EXPR and jumping to |
1924 | one of the labels in CASE_LIST or to the DEFAULT_LABEL. | |
9dc3d6a9 | 1925 | |
dc223ad4 ML |
1926 | We generate a binary decision tree to select the appropriate target |
1927 | code. */ | |
9dc3d6a9 | 1928 | |
dc223ad4 ML |
1929 | void |
1930 | switch_decision_tree::emit (basic_block bb, tree index_expr, | |
1931 | profile_probability default_prob, tree index_type) | |
1932 | { | |
1933 | balance_case_nodes (&m_case_list, NULL); | |
9dc3d6a9 | 1934 | |
dc223ad4 ML |
1935 | if (dump_file) |
1936 | dump_function_to_file (current_function_decl, dump_file, dump_flags); | |
1937 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1938 | { | |
1939 | int indent_step = ceil_log2 (TYPE_PRECISION (index_type)) + 2; | |
1940 | fprintf (dump_file, ";; Expanding GIMPLE switch as decision tree:\n"); | |
1941 | gcc_assert (m_case_list != NULL); | |
1942 | dump_case_nodes (dump_file, m_case_list, indent_step, 0); | |
1943 | } | |
9dc3d6a9 | 1944 | |
4359b631 EB |
1945 | bb = emit_case_nodes (bb, index_expr, m_case_list, default_prob, index_type, |
1946 | gimple_location (m_switch)); | |
9dc3d6a9 | 1947 | |
dc223ad4 ML |
1948 | if (bb) |
1949 | emit_jump (bb, m_default_bb); | |
9dc3d6a9 | 1950 | |
dc223ad4 ML |
1951 | /* Remove all edges and do just an edge that will reach default_bb. */ |
1952 | bb = gimple_bb (m_switch); | |
1953 | gimple_stmt_iterator gsi = gsi_last_bb (bb); | |
1954 | gsi_remove (&gsi, true); | |
9dc3d6a9 | 1955 | |
dc223ad4 ML |
1956 | delete_basic_block (bb); |
1957 | } | |
1958 | ||
1959 | /* Take an ordered list of case nodes | |
1960 | and transform them into a near optimal binary tree, | |
1961 | on the assumption that any target code selection value is as | |
1962 | likely as any other. | |
1963 | ||
1964 | The transformation is performed by splitting the ordered | |
1965 | list into two equal sections plus a pivot. The parts are | |
1966 | then attached to the pivot as left and right branches. Each | |
1967 | branch is then transformed recursively. */ | |
1968 | ||
1969 | void | |
1970 | switch_decision_tree::balance_case_nodes (case_tree_node **head, | |
1971 | case_tree_node *parent) | |
1972 | { | |
1973 | case_tree_node *np; | |
1974 | ||
1975 | np = *head; | |
1976 | if (np) | |
9dc3d6a9 | 1977 | { |
dc223ad4 ML |
1978 | int i = 0; |
1979 | int ranges = 0; | |
1980 | case_tree_node **npp; | |
1981 | case_tree_node *left; | |
add4cbca | 1982 | profile_probability prob = profile_probability::never (); |
9dc3d6a9 | 1983 | |
dc223ad4 | 1984 | /* Count the number of entries on branch. Also count the ranges. */ |
9dc3d6a9 | 1985 | |
dc223ad4 ML |
1986 | while (np) |
1987 | { | |
1988 | if (!tree_int_cst_equal (np->m_c->get_low (), np->m_c->get_high ())) | |
1989 | ranges++; | |
9dc3d6a9 | 1990 | |
dc223ad4 | 1991 | i++; |
add4cbca | 1992 | prob += np->m_c->m_prob; |
dc223ad4 ML |
1993 | np = np->m_right; |
1994 | } | |
9dc3d6a9 | 1995 | |
dc223ad4 ML |
1996 | if (i > 2) |
1997 | { | |
1998 | /* Split this list if it is long enough for that to help. */ | |
1999 | npp = head; | |
2000 | left = *npp; | |
add4cbca | 2001 | profile_probability pivot_prob = prob.apply_scale (1, 2); |
9dc3d6a9 | 2002 | |
add4cbca ML |
2003 | /* Find the place in the list that bisects the list's total cost, |
2004 | where ranges count as 2. */ | |
2005 | while (1) | |
dc223ad4 | 2006 | { |
add4cbca ML |
2007 | /* Skip nodes while their probability does not reach |
2008 | that amount. */ | |
2009 | prob -= (*npp)->m_c->m_prob; | |
a6b75a69 ML |
2010 | if ((prob.initialized_p () && prob < pivot_prob) |
2011 | || ! (*npp)->m_right) | |
add4cbca ML |
2012 | break; |
2013 | npp = &(*npp)->m_right; | |
dc223ad4 | 2014 | } |
add4cbca ML |
2015 | |
2016 | np = *npp; | |
2017 | *npp = 0; | |
2018 | *head = np; | |
dc223ad4 | 2019 | np->m_parent = parent; |
add4cbca | 2020 | np->m_left = left == np ? NULL : left; |
9dc3d6a9 | 2021 | |
dc223ad4 ML |
2022 | /* Optimize each of the two split parts. */ |
2023 | balance_case_nodes (&np->m_left, np); | |
2024 | balance_case_nodes (&np->m_right, np); | |
2025 | np->m_c->m_subtree_prob = np->m_c->m_prob; | |
add4cbca ML |
2026 | if (np->m_left) |
2027 | np->m_c->m_subtree_prob += np->m_left->m_c->m_subtree_prob; | |
2028 | if (np->m_right) | |
2029 | np->m_c->m_subtree_prob += np->m_right->m_c->m_subtree_prob; | |
dc223ad4 ML |
2030 | } |
2031 | else | |
2032 | { | |
2033 | /* Else leave this branch as one level, | |
2034 | but fill in `parent' fields. */ | |
2035 | np = *head; | |
2036 | np->m_parent = parent; | |
2037 | np->m_c->m_subtree_prob = np->m_c->m_prob; | |
2038 | for (; np->m_right; np = np->m_right) | |
2039 | { | |
2040 | np->m_right->m_parent = np; | |
2041 | (*head)->m_c->m_subtree_prob += np->m_right->m_c->m_subtree_prob; | |
2042 | } | |
2043 | } | |
9dc3d6a9 | 2044 | } |
dc223ad4 ML |
2045 | } |
2046 | ||
2047 | /* Dump ROOT, a list or tree of case nodes, to file. */ | |
9dc3d6a9 | 2048 | |
dc223ad4 ML |
2049 | void |
2050 | switch_decision_tree::dump_case_nodes (FILE *f, case_tree_node *root, | |
2051 | int indent_step, int indent_level) | |
2052 | { | |
2053 | if (root == 0) | |
2054 | return; | |
2055 | indent_level++; | |
2056 | ||
2057 | dump_case_nodes (f, root->m_left, indent_step, indent_level); | |
2058 | ||
2059 | fputs (";; ", f); | |
2060 | fprintf (f, "%*s", indent_step * indent_level, ""); | |
2061 | root->m_c->dump (f); | |
2062 | root->m_c->m_prob.dump (f); | |
bb79aba4 ML |
2063 | fputs (" subtree: ", f); |
2064 | root->m_c->m_subtree_prob.dump (f); | |
2065 | fputs (")\n", f); | |
dc223ad4 ML |
2066 | |
2067 | dump_case_nodes (f, root->m_right, indent_step, indent_level); | |
2068 | } | |
2069 | ||
2070 | ||
2071 | /* Add an unconditional jump to CASE_BB that happens in basic block BB. */ | |
2072 | ||
2073 | void | |
2074 | switch_decision_tree::emit_jump (basic_block bb, basic_block case_bb) | |
2075 | { | |
2076 | edge e = single_succ_edge (bb); | |
2077 | redirect_edge_succ (e, case_bb); | |
2078 | } | |
2079 | ||
2080 | /* Generate code to compare OP0 with OP1 so that the condition codes are | |
2081 | set and to jump to LABEL_BB if the condition is true. | |
2082 | COMPARISON is the GIMPLE comparison (EQ, NE, GT, etc.). | |
2083 | PROB is the probability of jumping to LABEL_BB. */ | |
2084 | ||
2085 | basic_block | |
2086 | switch_decision_tree::emit_cmp_and_jump_insns (basic_block bb, tree op0, | |
2087 | tree op1, tree_code comparison, | |
2088 | basic_block label_bb, | |
4359b631 EB |
2089 | profile_probability prob, |
2090 | location_t loc) | |
dc223ad4 ML |
2091 | { |
2092 | // TODO: it's once called with lhs != index. | |
2093 | op1 = fold_convert (TREE_TYPE (op0), op1); | |
2094 | ||
2095 | gcond *cond = gimple_build_cond (comparison, op0, op1, NULL_TREE, NULL_TREE); | |
4359b631 | 2096 | gimple_set_location (cond, loc); |
dc223ad4 ML |
2097 | gimple_stmt_iterator gsi = gsi_last_bb (bb); |
2098 | gsi_insert_after (&gsi, cond, GSI_NEW_STMT); | |
2099 | ||
2100 | gcc_assert (single_succ_p (bb)); | |
2101 | ||
2102 | /* Make a new basic block where false branch will take place. */ | |
2103 | edge false_edge = split_block (bb, cond); | |
2104 | false_edge->flags = EDGE_FALSE_VALUE; | |
2105 | false_edge->probability = prob.invert (); | |
2106 | ||
2107 | edge true_edge = make_edge (bb, label_bb, EDGE_TRUE_VALUE); | |
2108 | true_edge->probability = prob; | |
2109 | ||
2110 | return false_edge->dest; | |
2111 | } | |
2112 | ||
bb79aba4 ML |
2113 | /* Generate code to jump to LABEL if OP0 and OP1 are equal. |
2114 | PROB is the probability of jumping to LABEL_BB. | |
2115 | BB is a basic block where the new condition will be placed. */ | |
2116 | ||
2117 | basic_block | |
2118 | switch_decision_tree::do_jump_if_equal (basic_block bb, tree op0, tree op1, | |
2119 | basic_block label_bb, | |
4359b631 EB |
2120 | profile_probability prob, |
2121 | location_t loc) | |
bb79aba4 ML |
2122 | { |
2123 | op1 = fold_convert (TREE_TYPE (op0), op1); | |
2124 | ||
2125 | gcond *cond = gimple_build_cond (EQ_EXPR, op0, op1, NULL_TREE, NULL_TREE); | |
4359b631 | 2126 | gimple_set_location (cond, loc); |
bb79aba4 ML |
2127 | gimple_stmt_iterator gsi = gsi_last_bb (bb); |
2128 | gsi_insert_before (&gsi, cond, GSI_SAME_STMT); | |
2129 | ||
2130 | gcc_assert (single_succ_p (bb)); | |
2131 | ||
2132 | /* Make a new basic block where false branch will take place. */ | |
2133 | edge false_edge = split_block (bb, cond); | |
2134 | false_edge->flags = EDGE_FALSE_VALUE; | |
2135 | false_edge->probability = prob.invert (); | |
2136 | ||
2137 | edge true_edge = make_edge (bb, label_bb, EDGE_TRUE_VALUE); | |
2138 | true_edge->probability = prob; | |
2139 | ||
2140 | return false_edge->dest; | |
2141 | } | |
2142 | ||
dc223ad4 ML |
2143 | /* Emit step-by-step code to select a case for the value of INDEX. |
2144 | The thus generated decision tree follows the form of the | |
2145 | case-node binary tree NODE, whose nodes represent test conditions. | |
2146 | DEFAULT_PROB is probability of cases leading to default BB. | |
2147 | INDEX_TYPE is the type of the index of the switch. */ | |
2148 | ||
2149 | basic_block | |
2150 | switch_decision_tree::emit_case_nodes (basic_block bb, tree index, | |
2151 | case_tree_node *node, | |
2152 | profile_probability default_prob, | |
4359b631 | 2153 | tree index_type, location_t loc) |
dc223ad4 | 2154 | { |
bb79aba4 ML |
2155 | profile_probability p; |
2156 | ||
dc223ad4 ML |
2157 | /* If node is null, we are done. */ |
2158 | if (node == NULL) | |
2159 | return bb; | |
2160 | ||
bb79aba4 ML |
2161 | /* Single value case. */ |
2162 | if (node->m_c->is_single_value_p ()) | |
2163 | { | |
2164 | /* Node is single valued. First see if the index expression matches | |
2165 | this node and then check our children, if any. */ | |
2166 | p = node->m_c->m_prob / (node->m_c->m_subtree_prob + default_prob); | |
2167 | bb = do_jump_if_equal (bb, index, node->m_c->get_low (), | |
4359b631 | 2168 | node->m_c->m_case_bb, p, loc); |
bb79aba4 ML |
2169 | /* Since this case is taken at this point, reduce its weight from |
2170 | subtree_weight. */ | |
2171 | node->m_c->m_subtree_prob -= p; | |
2172 | ||
2173 | if (node->m_left != NULL && node->m_right != NULL) | |
2174 | { | |
2175 | /* 1) the node has both children | |
2176 | ||
2177 | If both children are single-valued cases with no | |
2178 | children, finish up all the work. This way, we can save | |
2179 | one ordered comparison. */ | |
2180 | ||
2181 | if (!node->m_left->has_child () | |
2182 | && node->m_left->m_c->is_single_value_p () | |
2183 | && !node->m_right->has_child () | |
2184 | && node->m_right->m_c->is_single_value_p ()) | |
2185 | { | |
2186 | p = (node->m_right->m_c->m_prob | |
2187 | / (node->m_c->m_subtree_prob + default_prob)); | |
2188 | bb = do_jump_if_equal (bb, index, node->m_right->m_c->get_low (), | |
4359b631 | 2189 | node->m_right->m_c->m_case_bb, p, loc); |
bb79aba4 ML |
2190 | |
2191 | p = (node->m_left->m_c->m_prob | |
2192 | / (node->m_c->m_subtree_prob + default_prob)); | |
2193 | bb = do_jump_if_equal (bb, index, node->m_left->m_c->get_low (), | |
4359b631 | 2194 | node->m_left->m_c->m_case_bb, p, loc); |
bb79aba4 ML |
2195 | } |
2196 | else | |
2197 | { | |
2198 | /* Branch to a label where we will handle it later. */ | |
2199 | basic_block test_bb = split_edge (single_succ_edge (bb)); | |
2200 | redirect_edge_succ (single_pred_edge (test_bb), | |
2201 | single_succ_edge (bb)->dest); | |
2202 | ||
2203 | p = ((node->m_right->m_c->m_subtree_prob | |
2204 | + default_prob.apply_scale (1, 2)) | |
2205 | / (node->m_c->m_subtree_prob + default_prob)); | |
2206 | bb = emit_cmp_and_jump_insns (bb, index, node->m_c->get_high (), | |
4359b631 | 2207 | GT_EXPR, test_bb, p, loc); |
bb79aba4 ML |
2208 | default_prob = default_prob.apply_scale (1, 2); |
2209 | ||
2210 | /* Handle the left-hand subtree. */ | |
2211 | bb = emit_case_nodes (bb, index, node->m_left, | |
4359b631 | 2212 | default_prob, index_type, loc); |
bb79aba4 ML |
2213 | |
2214 | /* If the left-hand subtree fell through, | |
2215 | don't let it fall into the right-hand subtree. */ | |
2216 | if (bb && m_default_bb) | |
2217 | emit_jump (bb, m_default_bb); | |
2218 | ||
2219 | bb = emit_case_nodes (test_bb, index, node->m_right, | |
4359b631 | 2220 | default_prob, index_type, loc); |
bb79aba4 ML |
2221 | } |
2222 | } | |
2223 | else if (node->m_left == NULL && node->m_right != NULL) | |
2224 | { | |
2225 | /* 2) the node has only right child. */ | |
dc223ad4 | 2226 | |
bb79aba4 ML |
2227 | /* Here we have a right child but no left so we issue a conditional |
2228 | branch to default and process the right child. | |
2229 | ||
2230 | Omit the conditional branch to default if the right child | |
2231 | does not have any children and is single valued; it would | |
2232 | cost too much space to save so little time. */ | |
2233 | ||
2234 | if (node->m_right->has_child () | |
2235 | || !node->m_right->m_c->is_single_value_p ()) | |
2236 | { | |
2237 | p = (default_prob.apply_scale (1, 2) | |
2238 | / (node->m_c->m_subtree_prob + default_prob)); | |
2239 | bb = emit_cmp_and_jump_insns (bb, index, node->m_c->get_low (), | |
4359b631 | 2240 | LT_EXPR, m_default_bb, p, loc); |
bb79aba4 ML |
2241 | default_prob = default_prob.apply_scale (1, 2); |
2242 | ||
2243 | bb = emit_case_nodes (bb, index, node->m_right, default_prob, | |
4359b631 | 2244 | index_type, loc); |
bb79aba4 ML |
2245 | } |
2246 | else | |
2247 | { | |
2248 | /* We cannot process node->right normally | |
2249 | since we haven't ruled out the numbers less than | |
2250 | this node's value. So handle node->right explicitly. */ | |
2251 | p = (node->m_right->m_c->m_subtree_prob | |
2252 | / (node->m_c->m_subtree_prob + default_prob)); | |
2253 | bb = do_jump_if_equal (bb, index, node->m_right->m_c->get_low (), | |
4359b631 | 2254 | node->m_right->m_c->m_case_bb, p, loc); |
bb79aba4 ML |
2255 | } |
2256 | } | |
2257 | else if (node->m_left != NULL && node->m_right == NULL) | |
2258 | { | |
2259 | /* 3) just one subtree, on the left. Similar case as previous. */ | |
2260 | ||
2261 | if (node->m_left->has_child () | |
2262 | || !node->m_left->m_c->is_single_value_p ()) | |
2263 | { | |
2264 | p = (default_prob.apply_scale (1, 2) | |
2265 | / (node->m_c->m_subtree_prob + default_prob)); | |
2266 | bb = emit_cmp_and_jump_insns (bb, index, node->m_c->get_high (), | |
4359b631 | 2267 | GT_EXPR, m_default_bb, p, loc); |
bb79aba4 ML |
2268 | default_prob = default_prob.apply_scale (1, 2); |
2269 | ||
2270 | bb = emit_case_nodes (bb, index, node->m_left, default_prob, | |
4359b631 | 2271 | index_type, loc); |
bb79aba4 ML |
2272 | } |
2273 | else | |
2274 | { | |
2275 | /* We cannot process node->left normally | |
2276 | since we haven't ruled out the numbers less than | |
2277 | this node's value. So handle node->left explicitly. */ | |
2278 | p = (node->m_left->m_c->m_subtree_prob | |
2279 | / (node->m_c->m_subtree_prob + default_prob)); | |
2280 | bb = do_jump_if_equal (bb, index, node->m_left->m_c->get_low (), | |
4359b631 | 2281 | node->m_left->m_c->m_case_bb, p, loc); |
bb79aba4 ML |
2282 | } |
2283 | } | |
2284 | } | |
2285 | else | |
2286 | { | |
2287 | /* Node is a range. These cases are very similar to those for a single | |
2288 | value, except that we do not start by testing whether this node | |
2289 | is the one to branch to. */ | |
2290 | if (node->has_child () || node->m_c->get_type () != SIMPLE_CASE) | |
2291 | { | |
2292 | /* Branch to a label where we will handle it later. */ | |
2293 | basic_block test_bb = split_edge (single_succ_edge (bb)); | |
2294 | redirect_edge_succ (single_pred_edge (test_bb), | |
2295 | single_succ_edge (bb)->dest); | |
2296 | ||
2297 | ||
2298 | profile_probability right_prob = profile_probability::never (); | |
2299 | if (node->m_right) | |
2300 | right_prob = node->m_right->m_c->m_subtree_prob; | |
2301 | p = ((right_prob + default_prob.apply_scale (1, 2)) | |
2302 | / (node->m_c->m_subtree_prob + default_prob)); | |
2303 | ||
2304 | bb = emit_cmp_and_jump_insns (bb, index, node->m_c->get_high (), | |
4359b631 | 2305 | GT_EXPR, test_bb, p, loc); |
bb79aba4 ML |
2306 | default_prob = default_prob.apply_scale (1, 2); |
2307 | ||
2308 | /* Value belongs to this node or to the left-hand subtree. */ | |
2309 | p = node->m_c->m_prob / (node->m_c->m_subtree_prob + default_prob); | |
2310 | bb = emit_cmp_and_jump_insns (bb, index, node->m_c->get_low (), | |
4359b631 | 2311 | GE_EXPR, node->m_c->m_case_bb, p, loc); |
bb79aba4 ML |
2312 | |
2313 | /* Handle the left-hand subtree. */ | |
2314 | bb = emit_case_nodes (bb, index, node->m_left, | |
4359b631 | 2315 | default_prob, index_type, loc); |
bb79aba4 ML |
2316 | |
2317 | /* If the left-hand subtree fell through, | |
2318 | don't let it fall into the right-hand subtree. */ | |
2319 | if (bb && m_default_bb) | |
2320 | emit_jump (bb, m_default_bb); | |
2321 | ||
2322 | bb = emit_case_nodes (test_bb, index, node->m_right, | |
4359b631 | 2323 | default_prob, index_type, loc); |
bb79aba4 ML |
2324 | } |
2325 | else | |
2326 | { | |
2327 | /* Node has no children so we check low and high bounds to remove | |
2328 | redundant tests. Only one of the bounds can exist, | |
2329 | since otherwise this node is bounded--a case tested already. */ | |
2330 | tree lhs, rhs; | |
2331 | generate_range_test (bb, index, node->m_c->get_low (), | |
2332 | node->m_c->get_high (), &lhs, &rhs); | |
2333 | p = default_prob / (node->m_c->m_subtree_prob + default_prob); | |
2334 | ||
2335 | bb = emit_cmp_and_jump_insns (bb, lhs, rhs, GT_EXPR, | |
4359b631 | 2336 | m_default_bb, p, loc); |
bb79aba4 ML |
2337 | |
2338 | emit_jump (bb, node->m_c->m_case_bb); | |
2339 | return NULL; | |
2340 | } | |
2341 | } | |
dc223ad4 ML |
2342 | |
2343 | return bb; | |
2344 | } | |
2345 | ||
2346 | /* The main function of the pass scans statements for switches and invokes | |
2347 | process_switch on them. */ | |
2348 | ||
2349 | namespace { | |
2350 | ||
2351 | const pass_data pass_data_convert_switch = | |
2352 | { | |
2353 | GIMPLE_PASS, /* type */ | |
2354 | "switchconv", /* name */ | |
2355 | OPTGROUP_NONE, /* optinfo_flags */ | |
2356 | TV_TREE_SWITCH_CONVERSION, /* tv_id */ | |
2357 | ( PROP_cfg | PROP_ssa ), /* properties_required */ | |
2358 | 0, /* properties_provided */ | |
2359 | 0, /* properties_destroyed */ | |
2360 | 0, /* todo_flags_start */ | |
2361 | TODO_update_ssa, /* todo_flags_finish */ | |
2362 | }; | |
2363 | ||
2364 | class pass_convert_switch : public gimple_opt_pass | |
2365 | { | |
2366 | public: | |
2367 | pass_convert_switch (gcc::context *ctxt) | |
2368 | : gimple_opt_pass (pass_data_convert_switch, ctxt) | |
2369 | {} | |
2370 | ||
2371 | /* opt_pass methods: */ | |
2372 | virtual bool gate (function *) { return flag_tree_switch_conversion != 0; } | |
2373 | virtual unsigned int execute (function *); | |
2374 | ||
2375 | }; // class pass_convert_switch | |
2376 | ||
2377 | unsigned int | |
2378 | pass_convert_switch::execute (function *fun) | |
2379 | { | |
2380 | basic_block bb; | |
2381 | bool cfg_altered = false; | |
2382 | ||
2383 | FOR_EACH_BB_FN (bb, fun) | |
2384 | { | |
2385 | gimple *stmt = last_stmt (bb); | |
2386 | if (stmt && gimple_code (stmt) == GIMPLE_SWITCH) | |
2387 | { | |
2388 | if (dump_file) | |
2389 | { | |
2390 | expanded_location loc = expand_location (gimple_location (stmt)); | |
2391 | ||
2392 | fprintf (dump_file, "beginning to process the following " | |
2393 | "SWITCH statement (%s:%d) : ------- \n", | |
2394 | loc.file, loc.line); | |
2395 | print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); | |
2396 | putc ('\n', dump_file); | |
2397 | } | |
2398 | ||
2399 | switch_conversion sconv; | |
2400 | sconv.expand (as_a <gswitch *> (stmt)); | |
2401 | cfg_altered |= sconv.m_cfg_altered; | |
2402 | if (!sconv.m_reason) | |
2403 | { | |
2404 | if (dump_file) | |
2405 | { | |
2406 | fputs ("Switch converted\n", dump_file); | |
2407 | fputs ("--------------------------------\n", dump_file); | |
2408 | } | |
2409 | ||
2410 | /* Make no effort to update the post-dominator tree. | |
2411 | It is actually not that hard for the transformations | |
2412 | we have performed, but it is not supported | |
2413 | by iterate_fix_dominators. */ | |
2414 | free_dominance_info (CDI_POST_DOMINATORS); | |
2415 | } | |
2416 | else | |
2417 | { | |
2418 | if (dump_file) | |
2419 | { | |
2420 | fputs ("Bailing out - ", dump_file); | |
2421 | fputs (sconv.m_reason, dump_file); | |
2422 | fputs ("\n--------------------------------\n", dump_file); | |
2423 | } | |
2424 | } | |
2425 | } | |
2426 | } | |
2427 | ||
2428 | return cfg_altered ? TODO_cleanup_cfg : 0;; | |
2429 | } | |
2430 | ||
2431 | } // anon namespace | |
2432 | ||
2433 | gimple_opt_pass * | |
2434 | make_pass_convert_switch (gcc::context *ctxt) | |
2435 | { | |
2436 | return new pass_convert_switch (ctxt); | |
9dc3d6a9 ML |
2437 | } |
2438 | ||
2439 | /* The main function of the pass scans statements for switches and invokes | |
2440 | process_switch on them. */ | |
2441 | ||
2442 | namespace { | |
2443 | ||
eb63c01f | 2444 | template <bool O0> class pass_lower_switch: public gimple_opt_pass |
9dc3d6a9 | 2445 | { |
eb63c01f ML |
2446 | public: |
2447 | pass_lower_switch (gcc::context *ctxt) : gimple_opt_pass (data, ctxt) {} | |
2448 | ||
2449 | static const pass_data data; | |
2450 | opt_pass * | |
2451 | clone () | |
2452 | { | |
2453 | return new pass_lower_switch<O0> (m_ctxt); | |
2454 | } | |
2455 | ||
2456 | virtual bool | |
2457 | gate (function *) | |
2458 | { | |
2459 | return !O0 || !optimize; | |
2460 | } | |
2461 | ||
2462 | virtual unsigned int execute (function *fun); | |
2463 | }; // class pass_lower_switch | |
2464 | ||
2465 | template <bool O0> | |
2466 | const pass_data pass_lower_switch<O0>::data = { | |
2467 | GIMPLE_PASS, /* type */ | |
2468 | O0 ? "switchlower_O0" : "switchlower", /* name */ | |
9dc3d6a9 ML |
2469 | OPTGROUP_NONE, /* optinfo_flags */ |
2470 | TV_TREE_SWITCH_LOWERING, /* tv_id */ | |
2471 | ( PROP_cfg | PROP_ssa ), /* properties_required */ | |
2472 | 0, /* properties_provided */ | |
2473 | 0, /* properties_destroyed */ | |
2474 | 0, /* todo_flags_start */ | |
2475 | TODO_update_ssa | TODO_cleanup_cfg, /* todo_flags_finish */ | |
2476 | }; | |
2477 | ||
eb63c01f | 2478 | template <bool O0> |
9dc3d6a9 | 2479 | unsigned int |
eb63c01f | 2480 | pass_lower_switch<O0>::execute (function *fun) |
9dc3d6a9 ML |
2481 | { |
2482 | basic_block bb; | |
2483 | bool expanded = false; | |
2484 | ||
dc223ad4 ML |
2485 | auto_vec<gimple *> switch_statements; |
2486 | switch_statements.create (1); | |
2487 | ||
9dc3d6a9 ML |
2488 | FOR_EACH_BB_FN (bb, fun) |
2489 | { | |
2490 | gimple *stmt = last_stmt (bb); | |
e6c5d9f0 ML |
2491 | gswitch *swtch; |
2492 | if (stmt && (swtch = dyn_cast<gswitch *> (stmt))) | |
2493 | { | |
2494 | if (!O0) | |
2495 | group_case_labels_stmt (swtch); | |
2496 | switch_statements.safe_push (swtch); | |
2497 | } | |
dc223ad4 ML |
2498 | } |
2499 | ||
2500 | for (unsigned i = 0; i < switch_statements.length (); i++) | |
2501 | { | |
2502 | gimple *stmt = switch_statements[i]; | |
2503 | if (dump_file) | |
9dc3d6a9 | 2504 | { |
dc223ad4 | 2505 | expanded_location loc = expand_location (gimple_location (stmt)); |
9dc3d6a9 | 2506 | |
dc223ad4 ML |
2507 | fprintf (dump_file, "beginning to process the following " |
2508 | "SWITCH statement (%s:%d) : ------- \n", | |
2509 | loc.file, loc.line); | |
2510 | print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); | |
2511 | putc ('\n', dump_file); | |
2512 | } | |
9dc3d6a9 | 2513 | |
dc223ad4 ML |
2514 | gswitch *swtch = dyn_cast<gswitch *> (stmt); |
2515 | if (swtch) | |
2516 | { | |
2517 | switch_decision_tree dt (swtch); | |
2518 | expanded |= dt.analyze_switch_statement (); | |
9dc3d6a9 ML |
2519 | } |
2520 | } | |
2521 | ||
2522 | if (expanded) | |
2523 | { | |
2524 | free_dominance_info (CDI_DOMINATORS); | |
2525 | free_dominance_info (CDI_POST_DOMINATORS); | |
2526 | mark_virtual_operands_for_renaming (cfun); | |
2527 | } | |
2528 | ||
2529 | return 0; | |
2530 | } | |
2531 | ||
2532 | } // anon namespace | |
2533 | ||
2534 | gimple_opt_pass * | |
eb63c01f | 2535 | make_pass_lower_switch_O0 (gcc::context *ctxt) |
9dc3d6a9 | 2536 | { |
eb63c01f | 2537 | return new pass_lower_switch<true> (ctxt); |
9dc3d6a9 | 2538 | } |
eb63c01f ML |
2539 | gimple_opt_pass * |
2540 | make_pass_lower_switch (gcc::context *ctxt) | |
9dc3d6a9 | 2541 | { |
eb63c01f | 2542 | return new pass_lower_switch<false> (ctxt); |
9dc3d6a9 ML |
2543 | } |
2544 | ||
9dc3d6a9 | 2545 |