]>
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); |
1d9cd701 JJ |
608 | tree type; |
609 | if (linear_p | |
610 | && (type = range_check_type (TREE_TYPE ((*constructor)[0].value)))) | |
767d4551 ML |
611 | { |
612 | if (dump_file && coeff_a.to_uhwi () > 0) | |
613 | fprintf (dump_file, "Linear transformation with A = %" PRId64 | |
614 | " and B = %" PRId64 "\n", coeff_a.to_shwi (), | |
615 | coeff_b.to_shwi ()); | |
616 | ||
5a5474ba | 617 | /* We must use type of constructor values. */ |
767d4551 | 618 | gimple_seq seq = NULL; |
1d9cd701 JJ |
619 | tree tmp = gimple_convert (&seq, type, m_index_expr); |
620 | tree tmp2 = gimple_build (&seq, MULT_EXPR, type, | |
621 | wide_int_to_tree (type, coeff_a), tmp); | |
622 | tree tmp3 = gimple_build (&seq, PLUS_EXPR, type, tmp2, | |
623 | wide_int_to_tree (type, coeff_b)); | |
767d4551 ML |
624 | tree tmp4 = gimple_convert (&seq, TREE_TYPE (name), tmp3); |
625 | gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT); | |
626 | load = gimple_build_assign (name, tmp4); | |
627 | } | |
7156c8ab MJ |
628 | else |
629 | { | |
8e97bc2b | 630 | tree array_type, ctor, decl, value_type, fetch, default_type; |
7156c8ab | 631 | |
789410e4 ML |
632 | default_type = TREE_TYPE (m_default_values[num]); |
633 | value_type = array_value_type (default_type, num); | |
7156c8ab | 634 | array_type = build_array_type (value_type, arr_index_type); |
8e97bc2b JJ |
635 | if (default_type != value_type) |
636 | { | |
637 | unsigned int i; | |
638 | constructor_elt *elt; | |
639 | ||
5a5474ba | 640 | FOR_EACH_VEC_SAFE_ELT (constructor, i, elt) |
8e97bc2b JJ |
641 | elt->value = fold_convert (value_type, elt->value); |
642 | } | |
5a5474ba | 643 | ctor = build_constructor (array_type, constructor); |
7156c8ab | 644 | TREE_CONSTANT (ctor) = true; |
5f7ae6b6 | 645 | TREE_STATIC (ctor) = true; |
7156c8ab | 646 | |
c2255bc4 | 647 | decl = build_decl (loc, VAR_DECL, NULL_TREE, array_type); |
7156c8ab MJ |
648 | TREE_STATIC (decl) = 1; |
649 | DECL_INITIAL (decl) = ctor; | |
650 | ||
651 | DECL_NAME (decl) = create_tmp_var_name ("CSWTCH"); | |
652 | DECL_ARTIFICIAL (decl) = 1; | |
f8d851c6 | 653 | DECL_IGNORED_P (decl) = 1; |
7156c8ab | 654 | TREE_CONSTANT (decl) = 1; |
2e3b4885 | 655 | TREE_READONLY (decl) = 1; |
d7438551 | 656 | DECL_IGNORED_P (decl) = 1; |
46dbeb40 TV |
657 | if (offloading_function_p (cfun->decl)) |
658 | DECL_ATTRIBUTES (decl) | |
659 | = tree_cons (get_identifier ("omp declare target"), NULL_TREE, | |
660 | NULL_TREE); | |
9041d2e6 | 661 | varpool_node::finalize_decl (decl); |
7156c8ab MJ |
662 | |
663 | fetch = build4 (ARRAY_REF, value_type, decl, tidx, NULL_TREE, | |
664 | NULL_TREE); | |
8e97bc2b JJ |
665 | if (default_type != value_type) |
666 | { | |
667 | fetch = fold_convert (default_type, fetch); | |
668 | fetch = force_gimple_operand_gsi (&gsi, fetch, true, NULL_TREE, | |
669 | true, GSI_SAME_STMT); | |
670 | } | |
7156c8ab MJ |
671 | load = gimple_build_assign (name, fetch); |
672 | } | |
b6e99746 | 673 | |
726a989a | 674 | gsi_insert_before (&gsi, load, GSI_SAME_STMT); |
7156c8ab | 675 | update_stmt (load); |
789410e4 | 676 | m_arr_ref_last = load; |
b6e99746 MJ |
677 | } |
678 | ||
679 | /* Builds and initializes static arrays initialized with values gathered from | |
789410e4 | 680 | the switch statement. Also creates statements that load values from |
b6e99746 MJ |
681 | them. */ |
682 | ||
789410e4 ML |
683 | void |
684 | switch_conversion::build_arrays () | |
b6e99746 MJ |
685 | { |
686 | tree arr_index_type; | |
83d5977e | 687 | tree tidx, sub, utype; |
355fe088 | 688 | gimple *stmt; |
726a989a | 689 | gimple_stmt_iterator gsi; |
538dd0b7 | 690 | gphi_iterator gpi; |
b6e99746 | 691 | int i; |
789410e4 | 692 | location_t loc = gimple_location (m_switch); |
b6e99746 | 693 | |
789410e4 | 694 | gsi = gsi_for_stmt (m_switch); |
04e78aa9 | 695 | |
edb9b69e | 696 | /* Make sure we do not generate arithmetics in a subrange. */ |
789410e4 | 697 | utype = TREE_TYPE (m_index_expr); |
edb9b69e JJ |
698 | if (TREE_TYPE (utype)) |
699 | utype = lang_hooks.types.type_for_mode (TYPE_MODE (TREE_TYPE (utype)), 1); | |
700 | else | |
701 | utype = lang_hooks.types.type_for_mode (TYPE_MODE (utype), 1); | |
702 | ||
789410e4 | 703 | arr_index_type = build_index_type (m_range_size); |
b731b390 | 704 | tidx = make_ssa_name (utype); |
edb9b69e | 705 | sub = fold_build2_loc (loc, MINUS_EXPR, utype, |
789410e4 ML |
706 | fold_convert_loc (loc, utype, m_index_expr), |
707 | fold_convert_loc (loc, utype, m_range_min)); | |
fae1034e | 708 | sub = force_gimple_operand_gsi (&gsi, sub, |
726a989a RB |
709 | false, NULL, true, GSI_SAME_STMT); |
710 | stmt = gimple_build_assign (tidx, sub); | |
b6e99746 | 711 | |
726a989a | 712 | gsi_insert_before (&gsi, stmt, GSI_SAME_STMT); |
7156c8ab | 713 | update_stmt (stmt); |
789410e4 | 714 | m_arr_ref_first = stmt; |
b6e99746 | 715 | |
789410e4 | 716 | for (gpi = gsi_start_phis (m_final_bb), i = 0; |
18bfe940 JJ |
717 | !gsi_end_p (gpi); gsi_next (&gpi)) |
718 | { | |
719 | gphi *phi = gpi.phi (); | |
720 | if (!virtual_operand_p (gimple_phi_result (phi))) | |
789410e4 | 721 | build_one_array (i++, arr_index_type, phi, tidx); |
8dc6a926 JJ |
722 | else |
723 | { | |
724 | edge e; | |
725 | edge_iterator ei; | |
789410e4 | 726 | FOR_EACH_EDGE (e, ei, m_switch_bb->succs) |
8dc6a926 | 727 | { |
789410e4 | 728 | if (e->dest == m_final_bb) |
8dc6a926 | 729 | break; |
789410e4 ML |
730 | if (!m_default_case_nonstandard |
731 | || e->dest != m_default_bb) | |
8dc6a926 JJ |
732 | { |
733 | e = single_succ_edge (e->dest); | |
734 | break; | |
735 | } | |
736 | } | |
789410e4 ML |
737 | gcc_assert (e && e->dest == m_final_bb); |
738 | m_target_vop = PHI_ARG_DEF_FROM_EDGE (phi, e); | |
8dc6a926 | 739 | } |
18bfe940 | 740 | } |
b6e99746 MJ |
741 | } |
742 | ||
743 | /* Generates and appropriately inserts loads of default values at the position | |
789410e4 | 744 | given by GSI. Returns the last inserted statement. */ |
b6e99746 | 745 | |
789410e4 ML |
746 | gassign * |
747 | switch_conversion::gen_def_assigns (gimple_stmt_iterator *gsi) | |
b6e99746 MJ |
748 | { |
749 | int i; | |
538dd0b7 | 750 | gassign *assign = NULL; |
b6e99746 | 751 | |
789410e4 | 752 | for (i = 0; i < m_phi_count; i++) |
b6e99746 | 753 | { |
789410e4 ML |
754 | tree name = copy_ssa_name (m_target_inbound_names[i]); |
755 | m_target_outbound_names[i] = name; | |
756 | assign = gimple_build_assign (name, m_default_values[i]); | |
726a989a | 757 | gsi_insert_before (gsi, assign, GSI_SAME_STMT); |
7156c8ab | 758 | update_stmt (assign); |
b6e99746 MJ |
759 | } |
760 | return assign; | |
761 | } | |
762 | ||
763 | /* Deletes the unused bbs and edges that now contain the switch statement and | |
789410e4 ML |
764 | its empty branch bbs. BBD is the now dead BB containing |
765 | the original switch statement, FINAL is the last BB of the converted | |
766 | switch statement (in terms of succession). */ | |
b6e99746 | 767 | |
789410e4 ML |
768 | void |
769 | switch_conversion::prune_bbs (basic_block bbd, basic_block final, | |
770 | basic_block default_bb) | |
b6e99746 MJ |
771 | { |
772 | edge_iterator ei; | |
773 | edge e; | |
774 | ||
775 | for (ei = ei_start (bbd->succs); (e = ei_safe_edge (ei)); ) | |
776 | { | |
777 | basic_block bb; | |
778 | bb = e->dest; | |
779 | remove_edge (e); | |
18bfe940 | 780 | if (bb != final && bb != default_bb) |
b6e99746 MJ |
781 | delete_basic_block (bb); |
782 | } | |
783 | delete_basic_block (bbd); | |
784 | } | |
785 | ||
786 | /* Add values to phi nodes in final_bb for the two new edges. E1F is the edge | |
787 | from the basic block loading values from an array and E2F from the basic | |
788 | block loading default values. BBF is the last switch basic block (see the | |
789 | bbf description in the comment below). */ | |
790 | ||
789410e4 ML |
791 | void |
792 | switch_conversion::fix_phi_nodes (edge e1f, edge e2f, basic_block bbf) | |
b6e99746 | 793 | { |
538dd0b7 | 794 | gphi_iterator gsi; |
b6e99746 MJ |
795 | int i; |
796 | ||
726a989a | 797 | for (gsi = gsi_start_phis (bbf), i = 0; |
18bfe940 | 798 | !gsi_end_p (gsi); gsi_next (&gsi)) |
b6e99746 | 799 | { |
538dd0b7 | 800 | gphi *phi = gsi.phi (); |
18bfe940 JJ |
801 | tree inbound, outbound; |
802 | if (virtual_operand_p (gimple_phi_result (phi))) | |
789410e4 | 803 | inbound = outbound = m_target_vop; |
18bfe940 JJ |
804 | else |
805 | { | |
789410e4 ML |
806 | inbound = m_target_inbound_names[i]; |
807 | outbound = m_target_outbound_names[i++]; | |
18bfe940 JJ |
808 | } |
809 | add_phi_arg (phi, inbound, e1f, UNKNOWN_LOCATION); | |
789410e4 | 810 | if (!m_default_case_nonstandard) |
18bfe940 | 811 | add_phi_arg (phi, outbound, e2f, UNKNOWN_LOCATION); |
b6e99746 | 812 | } |
b6e99746 MJ |
813 | } |
814 | ||
815 | /* Creates a check whether the switch expression value actually falls into the | |
816 | range given by all the cases. If it does not, the temporaries are loaded | |
789410e4 | 817 | with default values instead. */ |
b6e99746 | 818 | |
789410e4 ML |
819 | void |
820 | switch_conversion::gen_inbound_check () | |
b6e99746 | 821 | { |
c2255bc4 AH |
822 | tree label_decl1 = create_artificial_label (UNKNOWN_LOCATION); |
823 | tree label_decl2 = create_artificial_label (UNKNOWN_LOCATION); | |
824 | tree label_decl3 = create_artificial_label (UNKNOWN_LOCATION); | |
538dd0b7 | 825 | glabel *label1, *label2, *label3; |
edb9b69e | 826 | tree utype, tidx; |
b6e99746 MJ |
827 | tree bound; |
828 | ||
538dd0b7 | 829 | gcond *cond_stmt; |
b6e99746 | 830 | |
18bfe940 | 831 | gassign *last_assign = NULL; |
726a989a | 832 | gimple_stmt_iterator gsi; |
b6e99746 | 833 | basic_block bb0, bb1, bb2, bbf, bbd; |
18bfe940 | 834 | edge e01 = NULL, e02, e21, e1d, e1f, e2f; |
789410e4 | 835 | location_t loc = gimple_location (m_switch); |
b6e99746 | 836 | |
789410e4 | 837 | gcc_assert (m_default_values); |
6ab1ab14 | 838 | |
789410e4 | 839 | bb0 = gimple_bb (m_switch); |
b6e99746 | 840 | |
789410e4 | 841 | tidx = gimple_assign_lhs (m_arr_ref_first); |
edb9b69e | 842 | utype = TREE_TYPE (tidx); |
145544ab | 843 | |
b6e99746 | 844 | /* (end of) block 0 */ |
789410e4 | 845 | gsi = gsi_for_stmt (m_arr_ref_first); |
edb9b69e | 846 | gsi_next (&gsi); |
b6e99746 | 847 | |
789410e4 | 848 | bound = fold_convert_loc (loc, utype, m_range_size); |
edb9b69e | 849 | cond_stmt = gimple_build_cond (LE_EXPR, tidx, bound, NULL_TREE, NULL_TREE); |
726a989a | 850 | gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT); |
7156c8ab | 851 | update_stmt (cond_stmt); |
b6e99746 MJ |
852 | |
853 | /* block 2 */ | |
789410e4 | 854 | if (!m_default_case_nonstandard) |
18bfe940 JJ |
855 | { |
856 | label2 = gimple_build_label (label_decl2); | |
857 | gsi_insert_before (&gsi, label2, GSI_SAME_STMT); | |
789410e4 | 858 | last_assign = gen_def_assigns (&gsi); |
18bfe940 | 859 | } |
b6e99746 MJ |
860 | |
861 | /* block 1 */ | |
726a989a RB |
862 | label1 = gimple_build_label (label_decl1); |
863 | gsi_insert_before (&gsi, label1, GSI_SAME_STMT); | |
b6e99746 MJ |
864 | |
865 | /* block F */ | |
789410e4 | 866 | gsi = gsi_start_bb (m_final_bb); |
726a989a RB |
867 | label3 = gimple_build_label (label_decl3); |
868 | gsi_insert_before (&gsi, label3, GSI_SAME_STMT); | |
b6e99746 MJ |
869 | |
870 | /* cfg fix */ | |
726a989a | 871 | e02 = split_block (bb0, cond_stmt); |
b6e99746 MJ |
872 | bb2 = e02->dest; |
873 | ||
789410e4 | 874 | if (m_default_case_nonstandard) |
18bfe940 JJ |
875 | { |
876 | bb1 = bb2; | |
789410e4 | 877 | bb2 = m_default_bb; |
18bfe940 JJ |
878 | e01 = e02; |
879 | e01->flags = EDGE_TRUE_VALUE; | |
880 | e02 = make_edge (bb0, bb2, EDGE_FALSE_VALUE); | |
881 | edge e_default = find_edge (bb1, bb2); | |
882 | for (gphi_iterator gsi = gsi_start_phis (bb2); | |
883 | !gsi_end_p (gsi); gsi_next (&gsi)) | |
884 | { | |
885 | gphi *phi = gsi.phi (); | |
886 | tree arg = PHI_ARG_DEF_FROM_EDGE (phi, e_default); | |
887 | add_phi_arg (phi, arg, e02, | |
888 | gimple_phi_arg_location_from_edge (phi, e_default)); | |
889 | } | |
890 | /* Partially fix the dominator tree, if it is available. */ | |
891 | if (dom_info_available_p (CDI_DOMINATORS)) | |
892 | redirect_immediate_dominators (CDI_DOMINATORS, bb1, bb0); | |
893 | } | |
894 | else | |
895 | { | |
896 | e21 = split_block (bb2, last_assign); | |
897 | bb1 = e21->dest; | |
898 | remove_edge (e21); | |
899 | } | |
b6e99746 | 900 | |
789410e4 | 901 | e1d = split_block (bb1, m_arr_ref_last); |
b6e99746 MJ |
902 | bbd = e1d->dest; |
903 | remove_edge (e1d); | |
904 | ||
789410e4 ML |
905 | /* Flags and profiles of the edge for in-range values. */ |
906 | if (!m_default_case_nonstandard) | |
18bfe940 | 907 | e01 = make_edge (bb0, bb1, EDGE_TRUE_VALUE); |
789410e4 | 908 | e01->probability = m_default_prob.invert (); |
b6e99746 | 909 | |
789410e4 | 910 | /* Flags and profiles of the edge taking care of out-of-range values. */ |
b6e99746 MJ |
911 | e02->flags &= ~EDGE_FALLTHRU; |
912 | e02->flags |= EDGE_FALSE_VALUE; | |
789410e4 | 913 | e02->probability = m_default_prob; |
b6e99746 | 914 | |
789410e4 | 915 | bbf = m_final_bb; |
b6e99746 MJ |
916 | |
917 | e1f = make_edge (bb1, bbf, EDGE_FALLTHRU); | |
357067f2 | 918 | e1f->probability = profile_probability::always (); |
b6e99746 | 919 | |
789410e4 | 920 | if (m_default_case_nonstandard) |
18bfe940 JJ |
921 | e2f = NULL; |
922 | else | |
923 | { | |
924 | e2f = make_edge (bb2, bbf, EDGE_FALLTHRU); | |
357067f2 | 925 | e2f->probability = profile_probability::always (); |
18bfe940 | 926 | } |
b6e99746 MJ |
927 | |
928 | /* frequencies of the new BBs */ | |
e7a74006 JH |
929 | bb1->count = e01->count (); |
930 | bb2->count = e02->count (); | |
789410e4 | 931 | if (!m_default_case_nonstandard) |
e7a74006 | 932 | bbf->count = e1f->count () + e2f->count (); |
b6e99746 | 933 | |
6ab1ab14 | 934 | /* Tidy blocks that have become unreachable. */ |
789410e4 ML |
935 | prune_bbs (bbd, m_final_bb, |
936 | m_default_case_nonstandard ? m_default_bb : NULL); | |
b6e99746 | 937 | |
6ab1ab14 | 938 | /* Fixup the PHI nodes in bbF. */ |
789410e4 | 939 | fix_phi_nodes (e1f, e2f, bbf); |
b6e99746 | 940 | |
6ab1ab14 SB |
941 | /* Fix the dominator tree, if it is available. */ |
942 | if (dom_info_available_p (CDI_DOMINATORS)) | |
943 | { | |
9771b263 | 944 | vec<basic_block> bbs_to_fix_dom; |
6ab1ab14 SB |
945 | |
946 | set_immediate_dominator (CDI_DOMINATORS, bb1, bb0); | |
789410e4 | 947 | if (!m_default_case_nonstandard) |
18bfe940 | 948 | set_immediate_dominator (CDI_DOMINATORS, bb2, bb0); |
531b10fc | 949 | if (! get_immediate_dominator (CDI_DOMINATORS, bbf)) |
6ab1ab14 SB |
950 | /* If bbD was the immediate dominator ... */ |
951 | set_immediate_dominator (CDI_DOMINATORS, bbf, bb0); | |
952 | ||
18bfe940 | 953 | bbs_to_fix_dom.create (3 + (bb2 != bbf)); |
9771b263 DN |
954 | bbs_to_fix_dom.quick_push (bb0); |
955 | bbs_to_fix_dom.quick_push (bb1); | |
18bfe940 JJ |
956 | if (bb2 != bbf) |
957 | bbs_to_fix_dom.quick_push (bb2); | |
9771b263 | 958 | bbs_to_fix_dom.quick_push (bbf); |
6ab1ab14 SB |
959 | |
960 | iterate_fix_dominators (CDI_DOMINATORS, bbs_to_fix_dom, true); | |
9771b263 | 961 | bbs_to_fix_dom.release (); |
6ab1ab14 | 962 | } |
b6e99746 MJ |
963 | } |
964 | ||
789410e4 ML |
965 | /* The following function is invoked on every switch statement (the current |
966 | one is given in SWTCH) and runs the individual phases of switch | |
967 | conversion on it one after another until one fails or the conversion | |
968 | is completed. On success, NULL is in m_reason, otherwise points | |
969 | to a string with the reason why the conversion failed. */ | |
b6e99746 | 970 | |
789410e4 ML |
971 | void |
972 | switch_conversion::expand (gswitch *swtch) | |
b6e99746 | 973 | { |
238065a7 SB |
974 | /* Group case labels so that we get the right results from the heuristics |
975 | that decide on the code generation approach for this switch. */ | |
789410e4 | 976 | m_cfg_altered |= group_case_labels_stmt (swtch); |
d78bcb13 ML |
977 | |
978 | /* If this switch is now a degenerate case with only a default label, | |
979 | there is nothing left for us to do. */ | |
980 | if (gimple_switch_num_labels (swtch) < 2) | |
981 | { | |
982 | m_reason = "switch is a degenerate case"; | |
983 | return; | |
984 | } | |
886cd84f | 985 | |
789410e4 | 986 | collect (swtch); |
886cd84f SB |
987 | |
988 | /* No error markers should reach here (they should be filtered out | |
989 | during gimplification). */ | |
789410e4 | 990 | gcc_checking_assert (TREE_TYPE (m_index_expr) != error_mark_node); |
886cd84f | 991 | |
531b10fc | 992 | /* A switch on a constant should have been optimized in tree-cfg-cleanup. */ |
789410e4 | 993 | gcc_checking_assert (!TREE_CONSTANT (m_index_expr)); |
886cd84f | 994 | |
dc223ad4 ML |
995 | /* Prefer bit test if possible. */ |
996 | if (tree_fits_uhwi_p (m_range_size) | |
997 | && bit_test_cluster::can_be_handled (tree_to_uhwi (m_range_size), m_uniq) | |
998 | && bit_test_cluster::is_beneficial (m_count, m_uniq)) | |
999 | { | |
1000 | m_reason = "expanding as bit test is preferable"; | |
1001 | return; | |
1002 | } | |
1003 | ||
1004 | if (m_uniq <= 2) | |
1005 | { | |
1006 | /* This will be expanded as a decision tree . */ | |
1007 | m_reason = "expanding as jumps is preferable"; | |
1008 | return; | |
1009 | } | |
1010 | ||
789410e4 ML |
1011 | /* If there is no common successor, we cannot do the transformation. */ |
1012 | if (!m_final_bb) | |
886cd84f | 1013 | { |
789410e4 ML |
1014 | m_reason = "no common successor to all case label target blocks found"; |
1015 | return; | |
886cd84f | 1016 | } |
b6e99746 MJ |
1017 | |
1018 | /* Check the case label values are within reasonable range: */ | |
789410e4 | 1019 | if (!check_range ()) |
fade902a | 1020 | { |
789410e4 ML |
1021 | gcc_assert (m_reason); |
1022 | return; | |
fade902a | 1023 | } |
b6e99746 MJ |
1024 | |
1025 | /* For all the cases, see whether they are empty, the assignments they | |
1026 | represent constant and so on... */ | |
789410e4 | 1027 | if (!check_all_empty_except_final ()) |
8e97bc2b | 1028 | { |
789410e4 ML |
1029 | gcc_assert (m_reason); |
1030 | return; | |
8e97bc2b | 1031 | } |
789410e4 | 1032 | if (!check_final_bb ()) |
fade902a | 1033 | { |
789410e4 ML |
1034 | gcc_assert (m_reason); |
1035 | return; | |
fade902a | 1036 | } |
b6e99746 MJ |
1037 | |
1038 | /* At this point all checks have passed and we can proceed with the | |
1039 | transformation. */ | |
1040 | ||
789410e4 ML |
1041 | create_temp_arrays (); |
1042 | gather_default_values (m_default_case_nonstandard | |
18bfe940 | 1043 | ? gimple_switch_label (swtch, 1) |
789410e4 ML |
1044 | : gimple_switch_default_label (swtch)); |
1045 | build_constructors (); | |
b6e99746 | 1046 | |
789410e4 ML |
1047 | build_arrays (); /* Build the static arrays and assignments. */ |
1048 | gen_inbound_check (); /* Build the bounds check. */ | |
b6e99746 | 1049 | |
789410e4 ML |
1050 | m_cfg_altered = true; |
1051 | } | |
1052 | ||
1053 | /* Destructor. */ | |
1054 | ||
1055 | switch_conversion::~switch_conversion () | |
1056 | { | |
1057 | XDELETEVEC (m_constructors); | |
1058 | XDELETEVEC (m_default_values); | |
b6e99746 MJ |
1059 | } |
1060 | ||
dc223ad4 | 1061 | /* Constructor. */ |
be55bfe6 | 1062 | |
dc223ad4 ML |
1063 | group_cluster::group_cluster (vec<cluster *> &clusters, |
1064 | unsigned start, unsigned end) | |
be55bfe6 | 1065 | { |
dc223ad4 ML |
1066 | gcc_checking_assert (end - start + 1 >= 1); |
1067 | m_prob = profile_probability::never (); | |
1068 | m_cases.create (end - start + 1); | |
1069 | for (unsigned i = start; i <= end; i++) | |
1070 | { | |
1071 | m_cases.quick_push (static_cast<simple_cluster *> (clusters[i])); | |
1072 | m_prob += clusters[i]->m_prob; | |
1073 | } | |
1074 | m_subtree_prob = m_prob; | |
1075 | } | |
be55bfe6 | 1076 | |
dc223ad4 ML |
1077 | /* Destructor. */ |
1078 | ||
1079 | group_cluster::~group_cluster () | |
be55bfe6 | 1080 | { |
dc223ad4 ML |
1081 | for (unsigned i = 0; i < m_cases.length (); i++) |
1082 | delete m_cases[i]; | |
be55bfe6 | 1083 | |
dc223ad4 ML |
1084 | m_cases.release (); |
1085 | } | |
be55bfe6 | 1086 | |
dc223ad4 | 1087 | /* Dump content of a cluster. */ |
be55bfe6 | 1088 | |
dc223ad4 ML |
1089 | void |
1090 | group_cluster::dump (FILE *f, bool details) | |
b6e99746 | 1091 | { |
dc223ad4 ML |
1092 | unsigned total_values = 0; |
1093 | for (unsigned i = 0; i < m_cases.length (); i++) | |
1094 | total_values += m_cases[i]->get_range (m_cases[i]->get_low (), | |
1095 | m_cases[i]->get_high ()); | |
726a989a | 1096 | |
dc223ad4 ML |
1097 | unsigned comparison_count = 0; |
1098 | for (unsigned i = 0; i < m_cases.length (); i++) | |
1099 | { | |
1100 | simple_cluster *sc = static_cast<simple_cluster *> (m_cases[i]); | |
1101 | comparison_count += sc->m_range_p ? 2 : 1; | |
1102 | } | |
b6e99746 | 1103 | |
dc223ad4 ML |
1104 | unsigned HOST_WIDE_INT range = get_range (get_low (), get_high ()); |
1105 | fprintf (f, "%s", get_type () == JUMP_TABLE ? "JT" : "BT"); | |
531b10fc | 1106 | |
dc223ad4 ML |
1107 | if (details) |
1108 | fprintf (f, "(values:%d comparisons:%d range:" HOST_WIDE_INT_PRINT_DEC | |
1109 | " density: %.2f%%)", total_values, comparison_count, range, | |
1110 | 100.0f * comparison_count / range); | |
b6e99746 | 1111 | |
dc223ad4 ML |
1112 | fprintf (f, ":"); |
1113 | PRINT_CASE (f, get_low ()); | |
1114 | fprintf (f, "-"); | |
1115 | PRINT_CASE (f, get_high ()); | |
1116 | fprintf (f, " "); | |
b6e99746 MJ |
1117 | } |
1118 | ||
dc223ad4 | 1119 | /* Emit GIMPLE code to handle the cluster. */ |
27a4cd48 | 1120 | |
dc223ad4 ML |
1121 | void |
1122 | jump_table_cluster::emit (tree index_expr, tree, | |
1123 | tree default_label_expr, basic_block default_bb) | |
27a4cd48 | 1124 | { |
dbdfaaba ML |
1125 | unsigned HOST_WIDE_INT range = get_range (get_low (), get_high ()); |
1126 | unsigned HOST_WIDE_INT nondefault_range = 0; | |
1127 | ||
dc223ad4 ML |
1128 | /* For jump table we just emit a new gswitch statement that will |
1129 | be latter lowered to jump table. */ | |
1130 | auto_vec <tree> labels; | |
1131 | labels.create (m_cases.length ()); | |
1132 | ||
1133 | make_edge (m_case_bb, default_bb, 0); | |
1134 | for (unsigned i = 0; i < m_cases.length (); i++) | |
1135 | { | |
1136 | labels.quick_push (unshare_expr (m_cases[i]->m_case_label_expr)); | |
1137 | make_edge (m_case_bb, m_cases[i]->m_case_bb, 0); | |
1138 | } | |
1139 | ||
1140 | gswitch *s = gimple_build_switch (index_expr, | |
1141 | unshare_expr (default_label_expr), labels); | |
1142 | gimple_stmt_iterator gsi = gsi_start_bb (m_case_bb); | |
1143 | gsi_insert_after (&gsi, s, GSI_NEW_STMT); | |
dbdfaaba ML |
1144 | |
1145 | /* Set up even probabilities for all cases. */ | |
1146 | for (unsigned i = 0; i < m_cases.length (); i++) | |
1147 | { | |
1148 | simple_cluster *sc = static_cast<simple_cluster *> (m_cases[i]); | |
1149 | edge case_edge = find_edge (m_case_bb, sc->m_case_bb); | |
1150 | unsigned HOST_WIDE_INT case_range | |
1151 | = sc->get_range (sc->get_low (), sc->get_high ()); | |
1152 | nondefault_range += case_range; | |
1153 | ||
1154 | /* case_edge->aux is number of values in a jump-table that are covered | |
1155 | by the case_edge. */ | |
1156 | case_edge->aux = (void *) ((intptr_t) (case_edge->aux) + case_range); | |
1157 | } | |
1158 | ||
1159 | edge default_edge = gimple_switch_default_edge (cfun, s); | |
1160 | default_edge->probability = profile_probability::never (); | |
1161 | ||
1162 | for (unsigned i = 0; i < m_cases.length (); i++) | |
1163 | { | |
1164 | simple_cluster *sc = static_cast<simple_cluster *> (m_cases[i]); | |
1165 | edge case_edge = find_edge (m_case_bb, sc->m_case_bb); | |
1166 | case_edge->probability | |
1167 | = profile_probability::always ().apply_scale ((intptr_t)case_edge->aux, | |
1168 | range); | |
1169 | } | |
1170 | ||
1171 | /* Number of non-default values is probability of default edge. */ | |
1172 | default_edge->probability | |
1173 | += profile_probability::always ().apply_scale (nondefault_range, | |
1174 | range).invert (); | |
1175 | ||
1176 | switch_decision_tree::reset_out_edges_aux (s); | |
27a4cd48 | 1177 | } |
9dc3d6a9 | 1178 | |
2f928c1b ML |
1179 | /* Find jump tables of given CLUSTERS, where all members of the vector |
1180 | are of type simple_cluster. New clusters are returned. */ | |
1181 | ||
1182 | vec<cluster *> | |
1183 | jump_table_cluster::find_jump_tables (vec<cluster *> &clusters) | |
1184 | { | |
5885a1bd ML |
1185 | if (!is_enabled ()) |
1186 | return clusters.copy (); | |
1187 | ||
2f928c1b ML |
1188 | unsigned l = clusters.length (); |
1189 | auto_vec<min_cluster_item> min; | |
1190 | min.reserve (l + 1); | |
1191 | ||
1192 | min.quick_push (min_cluster_item (0, 0, 0)); | |
1193 | ||
1194 | for (unsigned i = 1; i <= l; i++) | |
1195 | { | |
1196 | /* Set minimal # of clusters with i-th item to infinite. */ | |
1197 | min.quick_push (min_cluster_item (INT_MAX, INT_MAX, INT_MAX)); | |
1198 | ||
1199 | for (unsigned j = 0; j < i; j++) | |
1200 | { | |
1201 | unsigned HOST_WIDE_INT s = min[j].m_non_jt_cases; | |
1202 | if (i - j < case_values_threshold ()) | |
1203 | s += i - j; | |
1204 | ||
1205 | /* Prefer clusters with smaller number of numbers covered. */ | |
1206 | if ((min[j].m_count + 1 < min[i].m_count | |
1207 | || (min[j].m_count + 1 == min[i].m_count | |
1208 | && s < min[i].m_non_jt_cases)) | |
1209 | && can_be_handled (clusters, j, i - 1)) | |
1210 | min[i] = min_cluster_item (min[j].m_count + 1, j, s); | |
1211 | } | |
df7c7974 ML |
1212 | |
1213 | gcc_checking_assert (min[i].m_count != INT_MAX); | |
2f928c1b ML |
1214 | } |
1215 | ||
1216 | /* No result. */ | |
1217 | if (min[l].m_count == INT_MAX) | |
1218 | return clusters.copy (); | |
1219 | ||
1220 | vec<cluster *> output; | |
1221 | output.create (4); | |
1222 | ||
1223 | /* Find and build the clusters. */ | |
1224 | for (int end = l;;) | |
1225 | { | |
1226 | int start = min[end].m_start; | |
1227 | ||
1228 | /* Do not allow clusters with small number of cases. */ | |
1229 | if (is_beneficial (clusters, start, end - 1)) | |
1230 | output.safe_push (new jump_table_cluster (clusters, start, end - 1)); | |
1231 | else | |
1232 | for (int i = end - 1; i >= start; i--) | |
1233 | output.safe_push (clusters[i]); | |
1234 | ||
1235 | end = start; | |
1236 | ||
1237 | if (start <= 0) | |
1238 | break; | |
1239 | } | |
1240 | ||
1241 | output.reverse (); | |
1242 | return output; | |
1243 | } | |
1244 | ||
dc223ad4 ML |
1245 | /* Return true when cluster starting at START and ending at END (inclusive) |
1246 | can build a jump-table. */ | |
1247 | ||
1248 | bool | |
1249 | jump_table_cluster::can_be_handled (const vec<cluster *> &clusters, | |
1250 | unsigned start, unsigned end) | |
9dc3d6a9 | 1251 | { |
dc223ad4 ML |
1252 | /* If the switch is relatively small such that the cost of one |
1253 | indirect jump on the target are higher than the cost of a | |
1254 | decision tree, go with the decision tree. | |
9dc3d6a9 | 1255 | |
dc223ad4 ML |
1256 | If range of values is much bigger than number of values, |
1257 | or if it is too large to represent in a HOST_WIDE_INT, | |
1258 | make a sequence of conditional branches instead of a dispatch. | |
9dc3d6a9 | 1259 | |
dc223ad4 | 1260 | The definition of "much bigger" depends on whether we are |
de840bde | 1261 | optimizing for size or for speed. */ |
dc223ad4 ML |
1262 | if (!flag_jump_tables) |
1263 | return false; | |
9dc3d6a9 | 1264 | |
df7c7974 ML |
1265 | /* For algorithm correctness, jump table for a single case must return |
1266 | true. We bail out in is_beneficial if it's called just for | |
1267 | a single case. */ | |
1268 | if (start == end) | |
1269 | return true; | |
9dc3d6a9 | 1270 | |
1aabb71d | 1271 | unsigned HOST_WIDE_INT max_ratio |
26f36b50 ML |
1272 | = (optimize_insn_for_size_p () |
1273 | ? PARAM_VALUE (PARAM_JUMP_TABLE_MAX_GROWTH_RATIO_FOR_SIZE) | |
1274 | : PARAM_VALUE (PARAM_JUMP_TABLE_MAX_GROWTH_RATIO_FOR_SPEED)); | |
dc223ad4 ML |
1275 | unsigned HOST_WIDE_INT range = get_range (clusters[start]->get_low (), |
1276 | clusters[end]->get_high ()); | |
1277 | /* Check overflow. */ | |
1278 | if (range == 0) | |
1279 | return false; | |
9dc3d6a9 | 1280 | |
dc223ad4 ML |
1281 | unsigned HOST_WIDE_INT comparison_count = 0; |
1282 | for (unsigned i = start; i <= end; i++) | |
1283 | { | |
1284 | simple_cluster *sc = static_cast<simple_cluster *> (clusters[i]); | |
1285 | comparison_count += sc->m_range_p ? 2 : 1; | |
1286 | } | |
9dc3d6a9 | 1287 | |
86e3947e ML |
1288 | unsigned HOST_WIDE_INT lhs = 100 * range; |
1289 | if (lhs < range) | |
1290 | return false; | |
1291 | ||
1292 | return lhs <= max_ratio * comparison_count; | |
9dc3d6a9 ML |
1293 | } |
1294 | ||
dc223ad4 ML |
1295 | /* Return true if cluster starting at START and ending at END (inclusive) |
1296 | is profitable transformation. */ | |
9dc3d6a9 | 1297 | |
dc223ad4 ML |
1298 | bool |
1299 | jump_table_cluster::is_beneficial (const vec<cluster *> &, | |
1300 | unsigned start, unsigned end) | |
9dc3d6a9 | 1301 | { |
df7c7974 ML |
1302 | /* Single case bail out. */ |
1303 | if (start == end) | |
1304 | return false; | |
1305 | ||
dc223ad4 | 1306 | return end - start + 1 >= case_values_threshold (); |
9dc3d6a9 ML |
1307 | } |
1308 | ||
2f928c1b ML |
1309 | /* Find bit tests of given CLUSTERS, where all members of the vector |
1310 | are of type simple_cluster. New clusters are returned. */ | |
1311 | ||
1312 | vec<cluster *> | |
1313 | bit_test_cluster::find_bit_tests (vec<cluster *> &clusters) | |
1314 | { | |
1315 | vec<cluster *> output; | |
1316 | output.create (4); | |
1317 | ||
1318 | unsigned l = clusters.length (); | |
1319 | auto_vec<min_cluster_item> min; | |
1320 | min.reserve (l + 1); | |
1321 | ||
1322 | min.quick_push (min_cluster_item (0, 0, 0)); | |
1323 | ||
1324 | for (unsigned i = 1; i <= l; i++) | |
1325 | { | |
1326 | /* Set minimal # of clusters with i-th item to infinite. */ | |
1327 | min.quick_push (min_cluster_item (INT_MAX, INT_MAX, INT_MAX)); | |
1328 | ||
1329 | for (unsigned j = 0; j < i; j++) | |
1330 | { | |
1331 | if (min[j].m_count + 1 < min[i].m_count | |
1332 | && can_be_handled (clusters, j, i - 1)) | |
1333 | min[i] = min_cluster_item (min[j].m_count + 1, j, INT_MAX); | |
1334 | } | |
df7c7974 ML |
1335 | |
1336 | gcc_checking_assert (min[i].m_count != INT_MAX); | |
2f928c1b ML |
1337 | } |
1338 | ||
1339 | /* No result. */ | |
1340 | if (min[l].m_count == INT_MAX) | |
1341 | return clusters.copy (); | |
1342 | ||
1343 | /* Find and build the clusters. */ | |
377afcd5 | 1344 | for (unsigned end = l;;) |
2f928c1b ML |
1345 | { |
1346 | int start = min[end].m_start; | |
1347 | ||
1348 | if (is_beneficial (clusters, start, end - 1)) | |
377afcd5 ML |
1349 | { |
1350 | bool entire = start == 0 && end == clusters.length (); | |
1351 | output.safe_push (new bit_test_cluster (clusters, start, end - 1, | |
1352 | entire)); | |
1353 | } | |
2f928c1b | 1354 | else |
1d9cd701 | 1355 | for (int i = end - 1; i >= start; i--) |
2f928c1b ML |
1356 | output.safe_push (clusters[i]); |
1357 | ||
1358 | end = start; | |
1359 | ||
1360 | if (start <= 0) | |
1361 | break; | |
1362 | } | |
1363 | ||
1364 | output.reverse (); | |
1365 | return output; | |
1366 | } | |
1367 | ||
dc223ad4 ML |
1368 | /* Return true when RANGE of case values with UNIQ labels |
1369 | can build a bit test. */ | |
9dc3d6a9 | 1370 | |
dc223ad4 ML |
1371 | bool |
1372 | bit_test_cluster::can_be_handled (unsigned HOST_WIDE_INT range, | |
1373 | unsigned int uniq) | |
9dc3d6a9 | 1374 | { |
dc223ad4 ML |
1375 | /* Check overflow. */ |
1376 | if (range == 0) | |
1377 | return 0; | |
1378 | ||
1379 | if (range >= GET_MODE_BITSIZE (word_mode)) | |
1380 | return false; | |
1381 | ||
1382 | return uniq <= 3; | |
1383 | } | |
1384 | ||
1385 | /* Return true when cluster starting at START and ending at END (inclusive) | |
1386 | can build a bit test. */ | |
1387 | ||
1388 | bool | |
1389 | bit_test_cluster::can_be_handled (const vec<cluster *> &clusters, | |
1390 | unsigned start, unsigned end) | |
1391 | { | |
df7c7974 ML |
1392 | /* For algorithm correctness, bit test for a single case must return |
1393 | true. We bail out in is_beneficial if it's called just for | |
1394 | a single case. */ | |
1395 | if (start == end) | |
1396 | return true; | |
1397 | ||
dc223ad4 ML |
1398 | unsigned HOST_WIDE_INT range = get_range (clusters[start]->get_low (), |
1399 | clusters[end]->get_high ()); | |
1400 | auto_bitmap dest_bbs; | |
1401 | ||
1402 | for (unsigned i = start; i <= end; i++) | |
9dc3d6a9 | 1403 | { |
dc223ad4 ML |
1404 | simple_cluster *sc = static_cast<simple_cluster *> (clusters[i]); |
1405 | bitmap_set_bit (dest_bbs, sc->m_case_bb->index); | |
9dc3d6a9 | 1406 | } |
9dc3d6a9 | 1407 | |
dc223ad4 ML |
1408 | return can_be_handled (range, bitmap_count_bits (dest_bbs)); |
1409 | } | |
9dc3d6a9 | 1410 | |
dc223ad4 ML |
1411 | /* Return true when COUNT of cases of UNIQ labels is beneficial for bit test |
1412 | transformation. */ | |
9dc3d6a9 | 1413 | |
dc223ad4 ML |
1414 | bool |
1415 | bit_test_cluster::is_beneficial (unsigned count, unsigned uniq) | |
9dc3d6a9 | 1416 | { |
dc223ad4 ML |
1417 | return (((uniq == 1 && count >= 3) |
1418 | || (uniq == 2 && count >= 5) | |
1419 | || (uniq == 3 && count >= 6))); | |
9dc3d6a9 ML |
1420 | } |
1421 | ||
dc223ad4 ML |
1422 | /* Return true if cluster starting at START and ending at END (inclusive) |
1423 | is profitable transformation. */ | |
9dc3d6a9 | 1424 | |
dc223ad4 ML |
1425 | bool |
1426 | bit_test_cluster::is_beneficial (const vec<cluster *> &clusters, | |
1427 | unsigned start, unsigned end) | |
9dc3d6a9 | 1428 | { |
df7c7974 ML |
1429 | /* Single case bail out. */ |
1430 | if (start == end) | |
1431 | return false; | |
1432 | ||
dc223ad4 | 1433 | auto_bitmap dest_bbs; |
9dc3d6a9 | 1434 | |
dc223ad4 | 1435 | for (unsigned i = start; i <= end; i++) |
9dc3d6a9 | 1436 | { |
dc223ad4 ML |
1437 | simple_cluster *sc = static_cast<simple_cluster *> (clusters[i]); |
1438 | bitmap_set_bit (dest_bbs, sc->m_case_bb->index); | |
9dc3d6a9 | 1439 | } |
9dc3d6a9 | 1440 | |
dc223ad4 ML |
1441 | unsigned uniq = bitmap_count_bits (dest_bbs); |
1442 | unsigned count = end - start + 1; | |
1443 | return is_beneficial (count, uniq); | |
9dc3d6a9 ML |
1444 | } |
1445 | ||
dc223ad4 ML |
1446 | /* Comparison function for qsort to order bit tests by decreasing |
1447 | probability of execution. */ | |
9dc3d6a9 | 1448 | |
dc223ad4 ML |
1449 | int |
1450 | case_bit_test::cmp (const void *p1, const void *p2) | |
1451 | { | |
99b1c316 MS |
1452 | const case_bit_test *const d1 = (const case_bit_test *) p1; |
1453 | const case_bit_test *const d2 = (const case_bit_test *) p2; | |
dc223ad4 ML |
1454 | |
1455 | if (d2->bits != d1->bits) | |
1456 | return d2->bits - d1->bits; | |
1457 | ||
1458 | /* Stabilize the sort. */ | |
1459 | return (LABEL_DECL_UID (CASE_LABEL (d2->label)) | |
1460 | - LABEL_DECL_UID (CASE_LABEL (d1->label))); | |
1461 | } | |
1462 | ||
1463 | /* Expand a switch statement by a short sequence of bit-wise | |
1464 | comparisons. "switch(x)" is effectively converted into | |
1465 | "if ((1 << (x-MINVAL)) & CST)" where CST and MINVAL are | |
1466 | integer constants. | |
1467 | ||
1468 | INDEX_EXPR is the value being switched on. | |
1469 | ||
1470 | MINVAL is the lowest case value of in the case nodes, | |
1471 | and RANGE is highest value minus MINVAL. MINVAL and RANGE | |
1472 | are not guaranteed to be of the same type as INDEX_EXPR | |
1473 | (the gimplifier doesn't change the type of case label values, | |
1474 | and MINVAL and RANGE are derived from those values). | |
1475 | MAXVAL is MINVAL + RANGE. | |
9dc3d6a9 | 1476 | |
dc223ad4 ML |
1477 | There *MUST* be max_case_bit_tests or less unique case |
1478 | node targets. */ | |
1479 | ||
1480 | void | |
1481 | bit_test_cluster::emit (tree index_expr, tree index_type, | |
1482 | tree, basic_block default_bb) | |
9dc3d6a9 | 1483 | { |
99b1c316 | 1484 | case_bit_test test[m_max_case_bit_tests] = { {} }; |
dc223ad4 ML |
1485 | unsigned int i, j, k; |
1486 | unsigned int count; | |
9dc3d6a9 | 1487 | |
1d9cd701 | 1488 | tree unsigned_index_type = range_check_type (index_type); |
9dc3d6a9 | 1489 | |
dc223ad4 ML |
1490 | gimple_stmt_iterator gsi; |
1491 | gassign *shift_stmt; | |
9dc3d6a9 | 1492 | |
dc223ad4 ML |
1493 | tree idx, tmp, csui; |
1494 | tree word_type_node = lang_hooks.types.type_for_mode (word_mode, 1); | |
1495 | tree word_mode_zero = fold_convert (word_type_node, integer_zero_node); | |
1496 | tree word_mode_one = fold_convert (word_type_node, integer_one_node); | |
1497 | int prec = TYPE_PRECISION (word_type_node); | |
1498 | wide_int wone = wi::one (prec); | |
9dc3d6a9 | 1499 | |
dc223ad4 ML |
1500 | tree minval = get_low (); |
1501 | tree maxval = get_high (); | |
1502 | tree range = int_const_binop (MINUS_EXPR, maxval, minval); | |
377afcd5 | 1503 | unsigned HOST_WIDE_INT bt_range = get_range (minval, maxval); |
9dc3d6a9 | 1504 | |
dc223ad4 ML |
1505 | /* Go through all case labels, and collect the case labels, profile |
1506 | counts, and other information we need to build the branch tests. */ | |
1507 | count = 0; | |
1508 | for (i = 0; i < m_cases.length (); i++) | |
1509 | { | |
1510 | unsigned int lo, hi; | |
1511 | simple_cluster *n = static_cast<simple_cluster *> (m_cases[i]); | |
1512 | for (k = 0; k < count; k++) | |
1513 | if (n->m_case_bb == test[k].target_bb) | |
1514 | break; | |
1515 | ||
1516 | if (k == count) | |
9dc3d6a9 | 1517 | { |
dc223ad4 ML |
1518 | gcc_checking_assert (count < m_max_case_bit_tests); |
1519 | test[k].mask = wi::zero (prec); | |
1520 | test[k].target_bb = n->m_case_bb; | |
1521 | test[k].label = n->m_case_label_expr; | |
377afcd5 | 1522 | test[k].bits = 0; |
dc223ad4 ML |
1523 | count++; |
1524 | } | |
377afcd5 ML |
1525 | |
1526 | test[k].bits += n->get_range (n->get_low (), n->get_high ()); | |
9dc3d6a9 | 1527 | |
dc223ad4 ML |
1528 | lo = tree_to_uhwi (int_const_binop (MINUS_EXPR, n->get_low (), minval)); |
1529 | if (n->get_high () == NULL_TREE) | |
1530 | hi = lo; | |
1531 | else | |
1532 | hi = tree_to_uhwi (int_const_binop (MINUS_EXPR, n->get_high (), | |
1533 | minval)); | |
9dc3d6a9 | 1534 | |
dc223ad4 ML |
1535 | for (j = lo; j <= hi; j++) |
1536 | test[k].mask |= wi::lshift (wone, j); | |
1537 | } | |
1538 | ||
1539 | qsort (test, count, sizeof (*test), case_bit_test::cmp); | |
1540 | ||
1541 | /* If all values are in the 0 .. BITS_PER_WORD-1 range, we can get rid of | |
1542 | the minval subtractions, but it might make the mask constants more | |
1543 | expensive. So, compare the costs. */ | |
1544 | if (compare_tree_int (minval, 0) > 0 | |
1545 | && compare_tree_int (maxval, GET_MODE_BITSIZE (word_mode)) < 0) | |
1546 | { | |
1547 | int cost_diff; | |
1548 | HOST_WIDE_INT m = tree_to_uhwi (minval); | |
1549 | rtx reg = gen_raw_REG (word_mode, 10000); | |
1550 | bool speed_p = optimize_insn_for_speed_p (); | |
1551 | cost_diff = set_rtx_cost (gen_rtx_PLUS (word_mode, reg, | |
1552 | GEN_INT (-m)), speed_p); | |
1553 | for (i = 0; i < count; i++) | |
1554 | { | |
1555 | rtx r = immed_wide_int_const (test[i].mask, word_mode); | |
1556 | cost_diff += set_src_cost (gen_rtx_AND (word_mode, reg, r), | |
1557 | word_mode, speed_p); | |
1558 | r = immed_wide_int_const (wi::lshift (test[i].mask, m), word_mode); | |
1559 | cost_diff -= set_src_cost (gen_rtx_AND (word_mode, reg, r), | |
1560 | word_mode, speed_p); | |
9dc3d6a9 | 1561 | } |
dc223ad4 | 1562 | if (cost_diff > 0) |
9dc3d6a9 | 1563 | { |
dc223ad4 ML |
1564 | for (i = 0; i < count; i++) |
1565 | test[i].mask = wi::lshift (test[i].mask, m); | |
1566 | minval = build_zero_cst (TREE_TYPE (minval)); | |
1567 | range = maxval; | |
9dc3d6a9 ML |
1568 | } |
1569 | } | |
9dc3d6a9 | 1570 | |
dc223ad4 ML |
1571 | /* Now build the test-and-branch code. */ |
1572 | ||
1573 | gsi = gsi_last_bb (m_case_bb); | |
1574 | ||
1575 | /* idx = (unsigned)x - minval. */ | |
1576 | idx = fold_convert (unsigned_index_type, index_expr); | |
1577 | idx = fold_build2 (MINUS_EXPR, unsigned_index_type, idx, | |
1578 | fold_convert (unsigned_index_type, minval)); | |
1579 | idx = force_gimple_operand_gsi (&gsi, idx, | |
1580 | /*simple=*/true, NULL_TREE, | |
1581 | /*before=*/true, GSI_SAME_STMT); | |
1582 | ||
377afcd5 ML |
1583 | if (m_handles_entire_switch) |
1584 | { | |
1585 | /* if (idx > range) goto default */ | |
1586 | range | |
1587 | = force_gimple_operand_gsi (&gsi, | |
dc223ad4 ML |
1588 | fold_convert (unsigned_index_type, range), |
1589 | /*simple=*/true, NULL_TREE, | |
1590 | /*before=*/true, GSI_SAME_STMT); | |
377afcd5 ML |
1591 | tmp = fold_build2 (GT_EXPR, boolean_type_node, idx, range); |
1592 | basic_block new_bb | |
1593 | = hoist_edge_and_branch_if_true (&gsi, tmp, default_bb, | |
1594 | profile_probability::unlikely ()); | |
1595 | gsi = gsi_last_bb (new_bb); | |
1596 | } | |
dc223ad4 ML |
1597 | |
1598 | /* csui = (1 << (word_mode) idx) */ | |
1599 | csui = make_ssa_name (word_type_node); | |
1600 | tmp = fold_build2 (LSHIFT_EXPR, word_type_node, word_mode_one, | |
1601 | fold_convert (word_type_node, idx)); | |
1602 | tmp = force_gimple_operand_gsi (&gsi, tmp, | |
1603 | /*simple=*/false, NULL_TREE, | |
1604 | /*before=*/true, GSI_SAME_STMT); | |
1605 | shift_stmt = gimple_build_assign (csui, tmp); | |
1606 | gsi_insert_before (&gsi, shift_stmt, GSI_SAME_STMT); | |
1607 | update_stmt (shift_stmt); | |
1608 | ||
377afcd5 ML |
1609 | profile_probability prob = profile_probability::always (); |
1610 | ||
dc223ad4 ML |
1611 | /* for each unique set of cases: |
1612 | if (const & csui) goto target */ | |
1613 | for (k = 0; k < count; k++) | |
1614 | { | |
377afcd5 ML |
1615 | prob = profile_probability::always ().apply_scale (test[k].bits, |
1616 | bt_range); | |
1617 | bt_range -= test[k].bits; | |
dc223ad4 ML |
1618 | tmp = wide_int_to_tree (word_type_node, test[k].mask); |
1619 | tmp = fold_build2 (BIT_AND_EXPR, word_type_node, csui, tmp); | |
1620 | tmp = force_gimple_operand_gsi (&gsi, tmp, | |
1621 | /*simple=*/true, NULL_TREE, | |
1622 | /*before=*/true, GSI_SAME_STMT); | |
1623 | tmp = fold_build2 (NE_EXPR, boolean_type_node, tmp, word_mode_zero); | |
377afcd5 ML |
1624 | basic_block new_bb |
1625 | = hoist_edge_and_branch_if_true (&gsi, tmp, test[k].target_bb, prob); | |
dc223ad4 ML |
1626 | gsi = gsi_last_bb (new_bb); |
1627 | } | |
9dc3d6a9 | 1628 | |
dc223ad4 ML |
1629 | /* We should have removed all edges now. */ |
1630 | gcc_assert (EDGE_COUNT (gsi_bb (gsi)->succs) == 0); | |
9dc3d6a9 | 1631 | |
dc223ad4 | 1632 | /* If nothing matched, go to the default label. */ |
377afcd5 ML |
1633 | edge e = make_edge (gsi_bb (gsi), default_bb, EDGE_FALLTHRU); |
1634 | e->probability = profile_probability::always (); | |
dc223ad4 | 1635 | } |
9dc3d6a9 | 1636 | |
dc223ad4 ML |
1637 | /* Split the basic block at the statement pointed to by GSIP, and insert |
1638 | a branch to the target basic block of E_TRUE conditional on tree | |
1639 | expression COND. | |
9dc3d6a9 | 1640 | |
dc223ad4 ML |
1641 | It is assumed that there is already an edge from the to-be-split |
1642 | basic block to E_TRUE->dest block. This edge is removed, and the | |
1643 | profile information on the edge is re-used for the new conditional | |
1644 | jump. | |
9dc3d6a9 | 1645 | |
dc223ad4 ML |
1646 | The CFG is updated. The dominator tree will not be valid after |
1647 | this transformation, but the immediate dominators are updated if | |
1648 | UPDATE_DOMINATORS is true. | |
9dc3d6a9 | 1649 | |
dc223ad4 | 1650 | Returns the newly created basic block. */ |
9dc3d6a9 | 1651 | |
dc223ad4 ML |
1652 | basic_block |
1653 | bit_test_cluster::hoist_edge_and_branch_if_true (gimple_stmt_iterator *gsip, | |
377afcd5 ML |
1654 | tree cond, basic_block case_bb, |
1655 | profile_probability prob) | |
9dc3d6a9 | 1656 | { |
dc223ad4 ML |
1657 | tree tmp; |
1658 | gcond *cond_stmt; | |
1659 | edge e_false; | |
1660 | basic_block new_bb, split_bb = gsi_bb (*gsip); | |
9dc3d6a9 | 1661 | |
dc223ad4 | 1662 | edge e_true = make_edge (split_bb, case_bb, EDGE_TRUE_VALUE); |
377afcd5 | 1663 | e_true->probability = prob; |
dc223ad4 | 1664 | gcc_assert (e_true->src == split_bb); |
9dc3d6a9 | 1665 | |
dc223ad4 ML |
1666 | tmp = force_gimple_operand_gsi (gsip, cond, /*simple=*/true, NULL, |
1667 | /*before=*/true, GSI_SAME_STMT); | |
1668 | cond_stmt = gimple_build_cond_from_tree (tmp, NULL_TREE, NULL_TREE); | |
1669 | gsi_insert_before (gsip, cond_stmt, GSI_SAME_STMT); | |
9dc3d6a9 | 1670 | |
dc223ad4 ML |
1671 | e_false = split_block (split_bb, cond_stmt); |
1672 | new_bb = e_false->dest; | |
1673 | redirect_edge_pred (e_true, split_bb); | |
9dc3d6a9 | 1674 | |
dc223ad4 ML |
1675 | e_false->flags &= ~EDGE_FALLTHRU; |
1676 | e_false->flags |= EDGE_FALSE_VALUE; | |
1677 | e_false->probability = e_true->probability.invert (); | |
1678 | new_bb->count = e_false->count (); | |
1679 | ||
1680 | return new_bb; | |
9dc3d6a9 ML |
1681 | } |
1682 | ||
dc223ad4 ML |
1683 | /* Compute the number of case labels that correspond to each outgoing edge of |
1684 | switch statement. Record this information in the aux field of the edge. */ | |
9dc3d6a9 | 1685 | |
dc223ad4 ML |
1686 | void |
1687 | switch_decision_tree::compute_cases_per_edge () | |
1688 | { | |
dbdfaaba | 1689 | reset_out_edges_aux (m_switch); |
dc223ad4 ML |
1690 | int ncases = gimple_switch_num_labels (m_switch); |
1691 | for (int i = ncases - 1; i >= 1; --i) | |
1692 | { | |
61ff5d6f | 1693 | edge case_edge = gimple_switch_edge (cfun, m_switch, i); |
dc223ad4 ML |
1694 | case_edge->aux = (void *) ((intptr_t) (case_edge->aux) + 1); |
1695 | } | |
1696 | } | |
1697 | ||
1698 | /* Analyze switch statement and return true when the statement is expanded | |
1699 | as decision tree. */ | |
9dc3d6a9 | 1700 | |
dc223ad4 ML |
1701 | bool |
1702 | switch_decision_tree::analyze_switch_statement () | |
9dc3d6a9 | 1703 | { |
dc223ad4 ML |
1704 | unsigned l = gimple_switch_num_labels (m_switch); |
1705 | basic_block bb = gimple_bb (m_switch); | |
1706 | auto_vec<cluster *> clusters; | |
1707 | clusters.create (l - 1); | |
1708 | ||
61ff5d6f | 1709 | basic_block default_bb = gimple_switch_default_bb (cfun, m_switch); |
dc223ad4 ML |
1710 | m_case_bbs.reserve (l); |
1711 | m_case_bbs.quick_push (default_bb); | |
1712 | ||
1713 | compute_cases_per_edge (); | |
1714 | ||
1715 | for (unsigned i = 1; i < l; i++) | |
1716 | { | |
1717 | tree elt = gimple_switch_label (m_switch, i); | |
1718 | tree lab = CASE_LABEL (elt); | |
61ff5d6f | 1719 | basic_block case_bb = label_to_block (cfun, lab); |
dc223ad4 ML |
1720 | edge case_edge = find_edge (bb, case_bb); |
1721 | tree low = CASE_LOW (elt); | |
1722 | tree high = CASE_HIGH (elt); | |
1723 | ||
1724 | profile_probability p | |
1725 | = case_edge->probability.apply_scale (1, (intptr_t) (case_edge->aux)); | |
61ff5d6f ML |
1726 | clusters.quick_push (new simple_cluster (low, high, elt, case_edge->dest, |
1727 | p)); | |
1728 | m_case_bbs.quick_push (case_edge->dest); | |
dc223ad4 ML |
1729 | } |
1730 | ||
dbdfaaba | 1731 | reset_out_edges_aux (m_switch); |
dc223ad4 | 1732 | |
2f928c1b ML |
1733 | /* Find jump table clusters. */ |
1734 | vec<cluster *> output = jump_table_cluster::find_jump_tables (clusters); | |
1735 | ||
df7c7974 | 1736 | /* Find bit test clusters. */ |
2f928c1b ML |
1737 | vec<cluster *> output2; |
1738 | auto_vec<cluster *> tmp; | |
1739 | output2.create (1); | |
1740 | tmp.create (1); | |
1741 | ||
1742 | for (unsigned i = 0; i < output.length (); i++) | |
1743 | { | |
1744 | cluster *c = output[i]; | |
1745 | if (c->get_type () != SIMPLE_CASE) | |
1746 | { | |
1747 | if (!tmp.is_empty ()) | |
1748 | { | |
1749 | vec<cluster *> n = bit_test_cluster::find_bit_tests (tmp); | |
1750 | output2.safe_splice (n); | |
1751 | n.release (); | |
1752 | tmp.truncate (0); | |
1753 | } | |
1754 | output2.safe_push (c); | |
1755 | } | |
1756 | else | |
1757 | tmp.safe_push (c); | |
1758 | } | |
1759 | ||
1760 | /* We still can have a temporary vector to test. */ | |
1761 | if (!tmp.is_empty ()) | |
1762 | { | |
1763 | vec<cluster *> n = bit_test_cluster::find_bit_tests (tmp); | |
1764 | output2.safe_splice (n); | |
1765 | n.release (); | |
1766 | } | |
9dc3d6a9 ML |
1767 | |
1768 | if (dump_file) | |
9dc3d6a9 | 1769 | { |
dc223ad4 | 1770 | fprintf (dump_file, ";; GIMPLE switch case clusters: "); |
2f928c1b ML |
1771 | for (unsigned i = 0; i < output2.length (); i++) |
1772 | output2[i]->dump (dump_file, dump_flags & TDF_DETAILS); | |
dc223ad4 ML |
1773 | fprintf (dump_file, "\n"); |
1774 | } | |
1775 | ||
2f928c1b | 1776 | output.release (); |
dc223ad4 | 1777 | |
2f928c1b ML |
1778 | bool expanded = try_switch_expansion (output2); |
1779 | ||
1780 | for (unsigned i = 0; i < output2.length (); i++) | |
1781 | delete output2[i]; | |
1782 | ||
1783 | output2.release (); | |
dc223ad4 ML |
1784 | |
1785 | return expanded; | |
1786 | } | |
1787 | ||
1788 | /* Attempt to expand CLUSTERS as a decision tree. Return true when | |
1789 | expanded. */ | |
1790 | ||
1791 | bool | |
1792 | switch_decision_tree::try_switch_expansion (vec<cluster *> &clusters) | |
1793 | { | |
1794 | tree index_expr = gimple_switch_index (m_switch); | |
1795 | tree index_type = TREE_TYPE (index_expr); | |
1796 | basic_block bb = gimple_bb (m_switch); | |
1797 | ||
1d9cd701 JJ |
1798 | if (gimple_switch_num_labels (m_switch) == 1 |
1799 | || range_check_type (index_type) == NULL_TREE) | |
dc223ad4 ML |
1800 | return false; |
1801 | ||
1802 | /* Find the default case target label. */ | |
61ff5d6f ML |
1803 | edge default_edge = gimple_switch_default_edge (cfun, m_switch); |
1804 | m_default_bb = default_edge->dest; | |
dc223ad4 ML |
1805 | |
1806 | /* Do the insertion of a case label into m_case_list. The labels are | |
1807 | fed to us in descending order from the sorted vector of case labels used | |
1808 | in the tree part of the middle end. So the list we construct is | |
1809 | sorted in ascending order. */ | |
1810 | ||
1811 | for (int i = clusters.length () - 1; i >= 0; i--) | |
1812 | { | |
1813 | case_tree_node *r = m_case_list; | |
1814 | m_case_list = m_case_node_pool.allocate (); | |
1815 | m_case_list->m_right = r; | |
1816 | m_case_list->m_c = clusters[i]; | |
9dc3d6a9 ML |
1817 | } |
1818 | ||
dc223ad4 ML |
1819 | record_phi_operand_mapping (); |
1820 | ||
1821 | /* Split basic block that contains the gswitch statement. */ | |
9dc3d6a9 ML |
1822 | gimple_stmt_iterator gsi = gsi_last_bb (bb); |
1823 | edge e; | |
1824 | if (gsi_end_p (gsi)) | |
1825 | e = split_block_after_labels (bb); | |
1826 | else | |
1827 | { | |
1828 | gsi_prev (&gsi); | |
1829 | e = split_block (bb, gsi_stmt (gsi)); | |
1830 | } | |
1831 | bb = split_edge (e); | |
1832 | ||
dc223ad4 ML |
1833 | /* Create new basic blocks for non-case clusters where specific expansion |
1834 | needs to happen. */ | |
1835 | for (unsigned i = 0; i < clusters.length (); i++) | |
1836 | if (clusters[i]->get_type () != SIMPLE_CASE) | |
1837 | { | |
1838 | clusters[i]->m_case_bb = create_empty_bb (bb); | |
1839 | clusters[i]->m_case_bb->loop_father = bb->loop_father; | |
1840 | } | |
9dc3d6a9 | 1841 | |
dc223ad4 ML |
1842 | /* Do not do an extra work for a single cluster. */ |
1843 | if (clusters.length () == 1 | |
1844 | && clusters[0]->get_type () != SIMPLE_CASE) | |
3f10efd4 ML |
1845 | { |
1846 | cluster *c = clusters[0]; | |
1847 | c->emit (index_expr, index_type, | |
1848 | gimple_switch_default_label (m_switch), m_default_bb); | |
1849 | redirect_edge_succ (single_succ_edge (bb), c->m_case_bb); | |
1850 | } | |
dc223ad4 ML |
1851 | else |
1852 | { | |
1853 | emit (bb, index_expr, default_edge->probability, index_type); | |
1854 | ||
1855 | /* Emit cluster-specific switch handling. */ | |
1856 | for (unsigned i = 0; i < clusters.length (); i++) | |
1857 | if (clusters[i]->get_type () != SIMPLE_CASE) | |
1858 | clusters[i]->emit (index_expr, index_type, | |
1859 | gimple_switch_default_label (m_switch), | |
1860 | m_default_bb); | |
1861 | } | |
9dc3d6a9 | 1862 | |
dc223ad4 ML |
1863 | fix_phi_operands_for_edges (); |
1864 | ||
1865 | return true; | |
9dc3d6a9 ML |
1866 | } |
1867 | ||
dc223ad4 ML |
1868 | /* Before switch transformation, record all SSA_NAMEs defined in switch BB |
1869 | and used in a label basic block. */ | |
1870 | ||
1871 | void | |
1872 | switch_decision_tree::record_phi_operand_mapping () | |
9dc3d6a9 | 1873 | { |
dc223ad4 | 1874 | basic_block switch_bb = gimple_bb (m_switch); |
9dc3d6a9 | 1875 | /* Record all PHI nodes that have to be fixed after conversion. */ |
dc223ad4 | 1876 | for (unsigned i = 0; i < m_case_bbs.length (); i++) |
9dc3d6a9 | 1877 | { |
9dc3d6a9 | 1878 | gphi_iterator gsi; |
dc223ad4 | 1879 | basic_block bb = m_case_bbs[i]; |
9dc3d6a9 ML |
1880 | for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
1881 | { | |
1882 | gphi *phi = gsi.phi (); | |
1883 | ||
1884 | for (unsigned i = 0; i < gimple_phi_num_args (phi); i++) | |
1885 | { | |
1886 | basic_block phi_src_bb = gimple_phi_arg_edge (phi, i)->src; | |
1887 | if (phi_src_bb == switch_bb) | |
1888 | { | |
1889 | tree def = gimple_phi_arg_def (phi, i); | |
1890 | tree result = gimple_phi_result (phi); | |
dc223ad4 | 1891 | m_phi_mapping.put (result, def); |
9dc3d6a9 ML |
1892 | break; |
1893 | } | |
1894 | } | |
1895 | } | |
1896 | } | |
1897 | } | |
1898 | ||
dc223ad4 ML |
1899 | /* Append new operands to PHI statements that were introduced due to |
1900 | addition of new edges to case labels. */ | |
9dc3d6a9 | 1901 | |
dc223ad4 ML |
1902 | void |
1903 | switch_decision_tree::fix_phi_operands_for_edges () | |
9dc3d6a9 | 1904 | { |
dc223ad4 | 1905 | gphi_iterator gsi; |
9dc3d6a9 | 1906 | |
dc223ad4 ML |
1907 | for (unsigned i = 0; i < m_case_bbs.length (); i++) |
1908 | { | |
1909 | basic_block bb = m_case_bbs[i]; | |
1910 | for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
1911 | { | |
1912 | gphi *phi = gsi.phi (); | |
1913 | for (unsigned j = 0; j < gimple_phi_num_args (phi); j++) | |
1914 | { | |
1915 | tree def = gimple_phi_arg_def (phi, j); | |
1916 | if (def == NULL_TREE) | |
1917 | { | |
1918 | edge e = gimple_phi_arg_edge (phi, j); | |
1919 | tree *definition | |
1920 | = m_phi_mapping.get (gimple_phi_result (phi)); | |
1921 | gcc_assert (definition); | |
1922 | add_phi_arg (phi, *definition, e, UNKNOWN_LOCATION); | |
1923 | } | |
1924 | } | |
1925 | } | |
1926 | } | |
1927 | } | |
9dc3d6a9 | 1928 | |
dc223ad4 ML |
1929 | /* Generate a decision tree, switching on INDEX_EXPR and jumping to |
1930 | one of the labels in CASE_LIST or to the DEFAULT_LABEL. | |
9dc3d6a9 | 1931 | |
dc223ad4 ML |
1932 | We generate a binary decision tree to select the appropriate target |
1933 | code. */ | |
9dc3d6a9 | 1934 | |
dc223ad4 ML |
1935 | void |
1936 | switch_decision_tree::emit (basic_block bb, tree index_expr, | |
1937 | profile_probability default_prob, tree index_type) | |
1938 | { | |
1939 | balance_case_nodes (&m_case_list, NULL); | |
9dc3d6a9 | 1940 | |
dc223ad4 ML |
1941 | if (dump_file) |
1942 | dump_function_to_file (current_function_decl, dump_file, dump_flags); | |
1943 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1944 | { | |
1945 | int indent_step = ceil_log2 (TYPE_PRECISION (index_type)) + 2; | |
1946 | fprintf (dump_file, ";; Expanding GIMPLE switch as decision tree:\n"); | |
1947 | gcc_assert (m_case_list != NULL); | |
1948 | dump_case_nodes (dump_file, m_case_list, indent_step, 0); | |
1949 | } | |
9dc3d6a9 | 1950 | |
4359b631 EB |
1951 | bb = emit_case_nodes (bb, index_expr, m_case_list, default_prob, index_type, |
1952 | gimple_location (m_switch)); | |
9dc3d6a9 | 1953 | |
dc223ad4 ML |
1954 | if (bb) |
1955 | emit_jump (bb, m_default_bb); | |
9dc3d6a9 | 1956 | |
dc223ad4 ML |
1957 | /* Remove all edges and do just an edge that will reach default_bb. */ |
1958 | bb = gimple_bb (m_switch); | |
1959 | gimple_stmt_iterator gsi = gsi_last_bb (bb); | |
1960 | gsi_remove (&gsi, true); | |
9dc3d6a9 | 1961 | |
dc223ad4 ML |
1962 | delete_basic_block (bb); |
1963 | } | |
1964 | ||
1965 | /* Take an ordered list of case nodes | |
1966 | and transform them into a near optimal binary tree, | |
1967 | on the assumption that any target code selection value is as | |
1968 | likely as any other. | |
1969 | ||
1970 | The transformation is performed by splitting the ordered | |
1971 | list into two equal sections plus a pivot. The parts are | |
1972 | then attached to the pivot as left and right branches. Each | |
1973 | branch is then transformed recursively. */ | |
1974 | ||
1975 | void | |
1976 | switch_decision_tree::balance_case_nodes (case_tree_node **head, | |
1977 | case_tree_node *parent) | |
1978 | { | |
1979 | case_tree_node *np; | |
1980 | ||
1981 | np = *head; | |
1982 | if (np) | |
9dc3d6a9 | 1983 | { |
dc223ad4 ML |
1984 | int i = 0; |
1985 | int ranges = 0; | |
1986 | case_tree_node **npp; | |
1987 | case_tree_node *left; | |
add4cbca | 1988 | profile_probability prob = profile_probability::never (); |
9dc3d6a9 | 1989 | |
dc223ad4 | 1990 | /* Count the number of entries on branch. Also count the ranges. */ |
9dc3d6a9 | 1991 | |
dc223ad4 ML |
1992 | while (np) |
1993 | { | |
1994 | if (!tree_int_cst_equal (np->m_c->get_low (), np->m_c->get_high ())) | |
1995 | ranges++; | |
9dc3d6a9 | 1996 | |
dc223ad4 | 1997 | i++; |
add4cbca | 1998 | prob += np->m_c->m_prob; |
dc223ad4 ML |
1999 | np = np->m_right; |
2000 | } | |
9dc3d6a9 | 2001 | |
dc223ad4 ML |
2002 | if (i > 2) |
2003 | { | |
2004 | /* Split this list if it is long enough for that to help. */ | |
2005 | npp = head; | |
2006 | left = *npp; | |
add4cbca | 2007 | profile_probability pivot_prob = prob.apply_scale (1, 2); |
9dc3d6a9 | 2008 | |
add4cbca ML |
2009 | /* Find the place in the list that bisects the list's total cost, |
2010 | where ranges count as 2. */ | |
2011 | while (1) | |
dc223ad4 | 2012 | { |
add4cbca ML |
2013 | /* Skip nodes while their probability does not reach |
2014 | that amount. */ | |
2015 | prob -= (*npp)->m_c->m_prob; | |
a6b75a69 ML |
2016 | if ((prob.initialized_p () && prob < pivot_prob) |
2017 | || ! (*npp)->m_right) | |
add4cbca ML |
2018 | break; |
2019 | npp = &(*npp)->m_right; | |
dc223ad4 | 2020 | } |
add4cbca ML |
2021 | |
2022 | np = *npp; | |
2023 | *npp = 0; | |
2024 | *head = np; | |
dc223ad4 | 2025 | np->m_parent = parent; |
add4cbca | 2026 | np->m_left = left == np ? NULL : left; |
9dc3d6a9 | 2027 | |
dc223ad4 ML |
2028 | /* Optimize each of the two split parts. */ |
2029 | balance_case_nodes (&np->m_left, np); | |
2030 | balance_case_nodes (&np->m_right, np); | |
2031 | np->m_c->m_subtree_prob = np->m_c->m_prob; | |
add4cbca ML |
2032 | if (np->m_left) |
2033 | np->m_c->m_subtree_prob += np->m_left->m_c->m_subtree_prob; | |
2034 | if (np->m_right) | |
2035 | np->m_c->m_subtree_prob += np->m_right->m_c->m_subtree_prob; | |
dc223ad4 ML |
2036 | } |
2037 | else | |
2038 | { | |
2039 | /* Else leave this branch as one level, | |
2040 | but fill in `parent' fields. */ | |
2041 | np = *head; | |
2042 | np->m_parent = parent; | |
2043 | np->m_c->m_subtree_prob = np->m_c->m_prob; | |
2044 | for (; np->m_right; np = np->m_right) | |
2045 | { | |
2046 | np->m_right->m_parent = np; | |
2047 | (*head)->m_c->m_subtree_prob += np->m_right->m_c->m_subtree_prob; | |
2048 | } | |
2049 | } | |
9dc3d6a9 | 2050 | } |
dc223ad4 ML |
2051 | } |
2052 | ||
2053 | /* Dump ROOT, a list or tree of case nodes, to file. */ | |
9dc3d6a9 | 2054 | |
dc223ad4 ML |
2055 | void |
2056 | switch_decision_tree::dump_case_nodes (FILE *f, case_tree_node *root, | |
2057 | int indent_step, int indent_level) | |
2058 | { | |
2059 | if (root == 0) | |
2060 | return; | |
2061 | indent_level++; | |
2062 | ||
2063 | dump_case_nodes (f, root->m_left, indent_step, indent_level); | |
2064 | ||
2065 | fputs (";; ", f); | |
2066 | fprintf (f, "%*s", indent_step * indent_level, ""); | |
2067 | root->m_c->dump (f); | |
2068 | root->m_c->m_prob.dump (f); | |
bb79aba4 ML |
2069 | fputs (" subtree: ", f); |
2070 | root->m_c->m_subtree_prob.dump (f); | |
2071 | fputs (")\n", f); | |
dc223ad4 ML |
2072 | |
2073 | dump_case_nodes (f, root->m_right, indent_step, indent_level); | |
2074 | } | |
2075 | ||
2076 | ||
2077 | /* Add an unconditional jump to CASE_BB that happens in basic block BB. */ | |
2078 | ||
2079 | void | |
2080 | switch_decision_tree::emit_jump (basic_block bb, basic_block case_bb) | |
2081 | { | |
2082 | edge e = single_succ_edge (bb); | |
2083 | redirect_edge_succ (e, case_bb); | |
2084 | } | |
2085 | ||
2086 | /* Generate code to compare OP0 with OP1 so that the condition codes are | |
2087 | set and to jump to LABEL_BB if the condition is true. | |
2088 | COMPARISON is the GIMPLE comparison (EQ, NE, GT, etc.). | |
2089 | PROB is the probability of jumping to LABEL_BB. */ | |
2090 | ||
2091 | basic_block | |
2092 | switch_decision_tree::emit_cmp_and_jump_insns (basic_block bb, tree op0, | |
2093 | tree op1, tree_code comparison, | |
2094 | basic_block label_bb, | |
4359b631 EB |
2095 | profile_probability prob, |
2096 | location_t loc) | |
dc223ad4 ML |
2097 | { |
2098 | // TODO: it's once called with lhs != index. | |
2099 | op1 = fold_convert (TREE_TYPE (op0), op1); | |
2100 | ||
2101 | gcond *cond = gimple_build_cond (comparison, op0, op1, NULL_TREE, NULL_TREE); | |
4359b631 | 2102 | gimple_set_location (cond, loc); |
dc223ad4 ML |
2103 | gimple_stmt_iterator gsi = gsi_last_bb (bb); |
2104 | gsi_insert_after (&gsi, cond, GSI_NEW_STMT); | |
2105 | ||
2106 | gcc_assert (single_succ_p (bb)); | |
2107 | ||
2108 | /* Make a new basic block where false branch will take place. */ | |
2109 | edge false_edge = split_block (bb, cond); | |
2110 | false_edge->flags = EDGE_FALSE_VALUE; | |
2111 | false_edge->probability = prob.invert (); | |
2112 | ||
2113 | edge true_edge = make_edge (bb, label_bb, EDGE_TRUE_VALUE); | |
2114 | true_edge->probability = prob; | |
2115 | ||
2116 | return false_edge->dest; | |
2117 | } | |
2118 | ||
bb79aba4 ML |
2119 | /* Generate code to jump to LABEL if OP0 and OP1 are equal. |
2120 | PROB is the probability of jumping to LABEL_BB. | |
2121 | BB is a basic block where the new condition will be placed. */ | |
2122 | ||
2123 | basic_block | |
2124 | switch_decision_tree::do_jump_if_equal (basic_block bb, tree op0, tree op1, | |
2125 | basic_block label_bb, | |
4359b631 EB |
2126 | profile_probability prob, |
2127 | location_t loc) | |
bb79aba4 ML |
2128 | { |
2129 | op1 = fold_convert (TREE_TYPE (op0), op1); | |
2130 | ||
2131 | gcond *cond = gimple_build_cond (EQ_EXPR, op0, op1, NULL_TREE, NULL_TREE); | |
4359b631 | 2132 | gimple_set_location (cond, loc); |
bb79aba4 ML |
2133 | gimple_stmt_iterator gsi = gsi_last_bb (bb); |
2134 | gsi_insert_before (&gsi, cond, GSI_SAME_STMT); | |
2135 | ||
2136 | gcc_assert (single_succ_p (bb)); | |
2137 | ||
2138 | /* Make a new basic block where false branch will take place. */ | |
2139 | edge false_edge = split_block (bb, cond); | |
2140 | false_edge->flags = EDGE_FALSE_VALUE; | |
2141 | false_edge->probability = prob.invert (); | |
2142 | ||
2143 | edge true_edge = make_edge (bb, label_bb, EDGE_TRUE_VALUE); | |
2144 | true_edge->probability = prob; | |
2145 | ||
2146 | return false_edge->dest; | |
2147 | } | |
2148 | ||
dc223ad4 ML |
2149 | /* Emit step-by-step code to select a case for the value of INDEX. |
2150 | The thus generated decision tree follows the form of the | |
2151 | case-node binary tree NODE, whose nodes represent test conditions. | |
2152 | DEFAULT_PROB is probability of cases leading to default BB. | |
2153 | INDEX_TYPE is the type of the index of the switch. */ | |
2154 | ||
2155 | basic_block | |
2156 | switch_decision_tree::emit_case_nodes (basic_block bb, tree index, | |
2157 | case_tree_node *node, | |
2158 | profile_probability default_prob, | |
4359b631 | 2159 | tree index_type, location_t loc) |
dc223ad4 | 2160 | { |
bb79aba4 ML |
2161 | profile_probability p; |
2162 | ||
dc223ad4 ML |
2163 | /* If node is null, we are done. */ |
2164 | if (node == NULL) | |
2165 | return bb; | |
2166 | ||
bb79aba4 ML |
2167 | /* Single value case. */ |
2168 | if (node->m_c->is_single_value_p ()) | |
2169 | { | |
2170 | /* Node is single valued. First see if the index expression matches | |
2171 | this node and then check our children, if any. */ | |
2172 | p = node->m_c->m_prob / (node->m_c->m_subtree_prob + default_prob); | |
2173 | bb = do_jump_if_equal (bb, index, node->m_c->get_low (), | |
4359b631 | 2174 | node->m_c->m_case_bb, p, loc); |
bb79aba4 ML |
2175 | /* Since this case is taken at this point, reduce its weight from |
2176 | subtree_weight. */ | |
2177 | node->m_c->m_subtree_prob -= p; | |
2178 | ||
2179 | if (node->m_left != NULL && node->m_right != NULL) | |
2180 | { | |
2181 | /* 1) the node has both children | |
2182 | ||
2183 | If both children are single-valued cases with no | |
2184 | children, finish up all the work. This way, we can save | |
2185 | one ordered comparison. */ | |
2186 | ||
2187 | if (!node->m_left->has_child () | |
2188 | && node->m_left->m_c->is_single_value_p () | |
2189 | && !node->m_right->has_child () | |
2190 | && node->m_right->m_c->is_single_value_p ()) | |
2191 | { | |
2192 | p = (node->m_right->m_c->m_prob | |
2193 | / (node->m_c->m_subtree_prob + default_prob)); | |
2194 | bb = do_jump_if_equal (bb, index, node->m_right->m_c->get_low (), | |
4359b631 | 2195 | node->m_right->m_c->m_case_bb, p, loc); |
bb79aba4 ML |
2196 | |
2197 | p = (node->m_left->m_c->m_prob | |
2198 | / (node->m_c->m_subtree_prob + default_prob)); | |
2199 | bb = do_jump_if_equal (bb, index, node->m_left->m_c->get_low (), | |
4359b631 | 2200 | node->m_left->m_c->m_case_bb, p, loc); |
bb79aba4 ML |
2201 | } |
2202 | else | |
2203 | { | |
2204 | /* Branch to a label where we will handle it later. */ | |
2205 | basic_block test_bb = split_edge (single_succ_edge (bb)); | |
2206 | redirect_edge_succ (single_pred_edge (test_bb), | |
2207 | single_succ_edge (bb)->dest); | |
2208 | ||
2209 | p = ((node->m_right->m_c->m_subtree_prob | |
2210 | + default_prob.apply_scale (1, 2)) | |
2211 | / (node->m_c->m_subtree_prob + default_prob)); | |
2212 | bb = emit_cmp_and_jump_insns (bb, index, node->m_c->get_high (), | |
4359b631 | 2213 | GT_EXPR, test_bb, p, loc); |
bb79aba4 ML |
2214 | default_prob = default_prob.apply_scale (1, 2); |
2215 | ||
2216 | /* Handle the left-hand subtree. */ | |
2217 | bb = emit_case_nodes (bb, index, node->m_left, | |
4359b631 | 2218 | default_prob, index_type, loc); |
bb79aba4 ML |
2219 | |
2220 | /* If the left-hand subtree fell through, | |
2221 | don't let it fall into the right-hand subtree. */ | |
2222 | if (bb && m_default_bb) | |
2223 | emit_jump (bb, m_default_bb); | |
2224 | ||
2225 | bb = emit_case_nodes (test_bb, index, node->m_right, | |
4359b631 | 2226 | default_prob, index_type, loc); |
bb79aba4 ML |
2227 | } |
2228 | } | |
2229 | else if (node->m_left == NULL && node->m_right != NULL) | |
2230 | { | |
2231 | /* 2) the node has only right child. */ | |
dc223ad4 | 2232 | |
bb79aba4 ML |
2233 | /* Here we have a right child but no left so we issue a conditional |
2234 | branch to default and process the right child. | |
2235 | ||
2236 | Omit the conditional branch to default if the right child | |
2237 | does not have any children and is single valued; it would | |
2238 | cost too much space to save so little time. */ | |
2239 | ||
2240 | if (node->m_right->has_child () | |
2241 | || !node->m_right->m_c->is_single_value_p ()) | |
2242 | { | |
2243 | p = (default_prob.apply_scale (1, 2) | |
2244 | / (node->m_c->m_subtree_prob + default_prob)); | |
2245 | bb = emit_cmp_and_jump_insns (bb, index, node->m_c->get_low (), | |
4359b631 | 2246 | LT_EXPR, m_default_bb, p, loc); |
bb79aba4 ML |
2247 | default_prob = default_prob.apply_scale (1, 2); |
2248 | ||
2249 | bb = emit_case_nodes (bb, index, node->m_right, default_prob, | |
4359b631 | 2250 | index_type, loc); |
bb79aba4 ML |
2251 | } |
2252 | else | |
2253 | { | |
2254 | /* We cannot process node->right normally | |
2255 | since we haven't ruled out the numbers less than | |
2256 | this node's value. So handle node->right explicitly. */ | |
2257 | p = (node->m_right->m_c->m_subtree_prob | |
2258 | / (node->m_c->m_subtree_prob + default_prob)); | |
2259 | bb = do_jump_if_equal (bb, index, node->m_right->m_c->get_low (), | |
4359b631 | 2260 | node->m_right->m_c->m_case_bb, p, loc); |
bb79aba4 ML |
2261 | } |
2262 | } | |
2263 | else if (node->m_left != NULL && node->m_right == NULL) | |
2264 | { | |
2265 | /* 3) just one subtree, on the left. Similar case as previous. */ | |
2266 | ||
2267 | if (node->m_left->has_child () | |
2268 | || !node->m_left->m_c->is_single_value_p ()) | |
2269 | { | |
2270 | p = (default_prob.apply_scale (1, 2) | |
2271 | / (node->m_c->m_subtree_prob + default_prob)); | |
2272 | bb = emit_cmp_and_jump_insns (bb, index, node->m_c->get_high (), | |
4359b631 | 2273 | GT_EXPR, m_default_bb, p, loc); |
bb79aba4 ML |
2274 | default_prob = default_prob.apply_scale (1, 2); |
2275 | ||
2276 | bb = emit_case_nodes (bb, index, node->m_left, default_prob, | |
4359b631 | 2277 | index_type, loc); |
bb79aba4 ML |
2278 | } |
2279 | else | |
2280 | { | |
2281 | /* We cannot process node->left normally | |
2282 | since we haven't ruled out the numbers less than | |
2283 | this node's value. So handle node->left explicitly. */ | |
2284 | p = (node->m_left->m_c->m_subtree_prob | |
2285 | / (node->m_c->m_subtree_prob + default_prob)); | |
2286 | bb = do_jump_if_equal (bb, index, node->m_left->m_c->get_low (), | |
4359b631 | 2287 | node->m_left->m_c->m_case_bb, p, loc); |
bb79aba4 ML |
2288 | } |
2289 | } | |
2290 | } | |
2291 | else | |
2292 | { | |
2293 | /* Node is a range. These cases are very similar to those for a single | |
2294 | value, except that we do not start by testing whether this node | |
2295 | is the one to branch to. */ | |
2296 | if (node->has_child () || node->m_c->get_type () != SIMPLE_CASE) | |
2297 | { | |
2298 | /* Branch to a label where we will handle it later. */ | |
2299 | basic_block test_bb = split_edge (single_succ_edge (bb)); | |
2300 | redirect_edge_succ (single_pred_edge (test_bb), | |
2301 | single_succ_edge (bb)->dest); | |
2302 | ||
2303 | ||
2304 | profile_probability right_prob = profile_probability::never (); | |
2305 | if (node->m_right) | |
2306 | right_prob = node->m_right->m_c->m_subtree_prob; | |
2307 | p = ((right_prob + default_prob.apply_scale (1, 2)) | |
2308 | / (node->m_c->m_subtree_prob + default_prob)); | |
2309 | ||
2310 | bb = emit_cmp_and_jump_insns (bb, index, node->m_c->get_high (), | |
4359b631 | 2311 | GT_EXPR, test_bb, p, loc); |
bb79aba4 ML |
2312 | default_prob = default_prob.apply_scale (1, 2); |
2313 | ||
2314 | /* Value belongs to this node or to the left-hand subtree. */ | |
2315 | p = node->m_c->m_prob / (node->m_c->m_subtree_prob + default_prob); | |
2316 | bb = emit_cmp_and_jump_insns (bb, index, node->m_c->get_low (), | |
4359b631 | 2317 | GE_EXPR, node->m_c->m_case_bb, p, loc); |
bb79aba4 ML |
2318 | |
2319 | /* Handle the left-hand subtree. */ | |
2320 | bb = emit_case_nodes (bb, index, node->m_left, | |
4359b631 | 2321 | default_prob, index_type, loc); |
bb79aba4 ML |
2322 | |
2323 | /* If the left-hand subtree fell through, | |
2324 | don't let it fall into the right-hand subtree. */ | |
2325 | if (bb && m_default_bb) | |
2326 | emit_jump (bb, m_default_bb); | |
2327 | ||
2328 | bb = emit_case_nodes (test_bb, index, node->m_right, | |
4359b631 | 2329 | default_prob, index_type, loc); |
bb79aba4 ML |
2330 | } |
2331 | else | |
2332 | { | |
2333 | /* Node has no children so we check low and high bounds to remove | |
2334 | redundant tests. Only one of the bounds can exist, | |
2335 | since otherwise this node is bounded--a case tested already. */ | |
2336 | tree lhs, rhs; | |
2337 | generate_range_test (bb, index, node->m_c->get_low (), | |
2338 | node->m_c->get_high (), &lhs, &rhs); | |
2339 | p = default_prob / (node->m_c->m_subtree_prob + default_prob); | |
2340 | ||
2341 | bb = emit_cmp_and_jump_insns (bb, lhs, rhs, GT_EXPR, | |
4359b631 | 2342 | m_default_bb, p, loc); |
bb79aba4 ML |
2343 | |
2344 | emit_jump (bb, node->m_c->m_case_bb); | |
2345 | return NULL; | |
2346 | } | |
2347 | } | |
dc223ad4 ML |
2348 | |
2349 | return bb; | |
2350 | } | |
2351 | ||
2352 | /* The main function of the pass scans statements for switches and invokes | |
2353 | process_switch on them. */ | |
2354 | ||
2355 | namespace { | |
2356 | ||
2357 | const pass_data pass_data_convert_switch = | |
2358 | { | |
2359 | GIMPLE_PASS, /* type */ | |
2360 | "switchconv", /* name */ | |
2361 | OPTGROUP_NONE, /* optinfo_flags */ | |
2362 | TV_TREE_SWITCH_CONVERSION, /* tv_id */ | |
2363 | ( PROP_cfg | PROP_ssa ), /* properties_required */ | |
2364 | 0, /* properties_provided */ | |
2365 | 0, /* properties_destroyed */ | |
2366 | 0, /* todo_flags_start */ | |
2367 | TODO_update_ssa, /* todo_flags_finish */ | |
2368 | }; | |
2369 | ||
2370 | class pass_convert_switch : public gimple_opt_pass | |
2371 | { | |
2372 | public: | |
2373 | pass_convert_switch (gcc::context *ctxt) | |
2374 | : gimple_opt_pass (pass_data_convert_switch, ctxt) | |
2375 | {} | |
2376 | ||
2377 | /* opt_pass methods: */ | |
2378 | virtual bool gate (function *) { return flag_tree_switch_conversion != 0; } | |
2379 | virtual unsigned int execute (function *); | |
2380 | ||
2381 | }; // class pass_convert_switch | |
2382 | ||
2383 | unsigned int | |
2384 | pass_convert_switch::execute (function *fun) | |
2385 | { | |
2386 | basic_block bb; | |
2387 | bool cfg_altered = false; | |
2388 | ||
2389 | FOR_EACH_BB_FN (bb, fun) | |
2390 | { | |
2391 | gimple *stmt = last_stmt (bb); | |
2392 | if (stmt && gimple_code (stmt) == GIMPLE_SWITCH) | |
2393 | { | |
2394 | if (dump_file) | |
2395 | { | |
2396 | expanded_location loc = expand_location (gimple_location (stmt)); | |
2397 | ||
2398 | fprintf (dump_file, "beginning to process the following " | |
2399 | "SWITCH statement (%s:%d) : ------- \n", | |
2400 | loc.file, loc.line); | |
2401 | print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); | |
2402 | putc ('\n', dump_file); | |
2403 | } | |
2404 | ||
2405 | switch_conversion sconv; | |
2406 | sconv.expand (as_a <gswitch *> (stmt)); | |
2407 | cfg_altered |= sconv.m_cfg_altered; | |
2408 | if (!sconv.m_reason) | |
2409 | { | |
2410 | if (dump_file) | |
2411 | { | |
2412 | fputs ("Switch converted\n", dump_file); | |
2413 | fputs ("--------------------------------\n", dump_file); | |
2414 | } | |
2415 | ||
2416 | /* Make no effort to update the post-dominator tree. | |
2417 | It is actually not that hard for the transformations | |
2418 | we have performed, but it is not supported | |
2419 | by iterate_fix_dominators. */ | |
2420 | free_dominance_info (CDI_POST_DOMINATORS); | |
2421 | } | |
2422 | else | |
2423 | { | |
2424 | if (dump_file) | |
2425 | { | |
2426 | fputs ("Bailing out - ", dump_file); | |
2427 | fputs (sconv.m_reason, dump_file); | |
2428 | fputs ("\n--------------------------------\n", dump_file); | |
2429 | } | |
2430 | } | |
2431 | } | |
2432 | } | |
2433 | ||
2434 | return cfg_altered ? TODO_cleanup_cfg : 0;; | |
2435 | } | |
2436 | ||
2437 | } // anon namespace | |
2438 | ||
2439 | gimple_opt_pass * | |
2440 | make_pass_convert_switch (gcc::context *ctxt) | |
2441 | { | |
2442 | return new pass_convert_switch (ctxt); | |
9dc3d6a9 ML |
2443 | } |
2444 | ||
2445 | /* The main function of the pass scans statements for switches and invokes | |
2446 | process_switch on them. */ | |
2447 | ||
2448 | namespace { | |
2449 | ||
eb63c01f | 2450 | template <bool O0> class pass_lower_switch: public gimple_opt_pass |
9dc3d6a9 | 2451 | { |
eb63c01f ML |
2452 | public: |
2453 | pass_lower_switch (gcc::context *ctxt) : gimple_opt_pass (data, ctxt) {} | |
2454 | ||
2455 | static const pass_data data; | |
2456 | opt_pass * | |
2457 | clone () | |
2458 | { | |
2459 | return new pass_lower_switch<O0> (m_ctxt); | |
2460 | } | |
2461 | ||
2462 | virtual bool | |
2463 | gate (function *) | |
2464 | { | |
2465 | return !O0 || !optimize; | |
2466 | } | |
2467 | ||
2468 | virtual unsigned int execute (function *fun); | |
2469 | }; // class pass_lower_switch | |
2470 | ||
2471 | template <bool O0> | |
2472 | const pass_data pass_lower_switch<O0>::data = { | |
2473 | GIMPLE_PASS, /* type */ | |
2474 | O0 ? "switchlower_O0" : "switchlower", /* name */ | |
9dc3d6a9 ML |
2475 | OPTGROUP_NONE, /* optinfo_flags */ |
2476 | TV_TREE_SWITCH_LOWERING, /* tv_id */ | |
2477 | ( PROP_cfg | PROP_ssa ), /* properties_required */ | |
2478 | 0, /* properties_provided */ | |
2479 | 0, /* properties_destroyed */ | |
2480 | 0, /* todo_flags_start */ | |
2481 | TODO_update_ssa | TODO_cleanup_cfg, /* todo_flags_finish */ | |
2482 | }; | |
2483 | ||
eb63c01f | 2484 | template <bool O0> |
9dc3d6a9 | 2485 | unsigned int |
eb63c01f | 2486 | pass_lower_switch<O0>::execute (function *fun) |
9dc3d6a9 ML |
2487 | { |
2488 | basic_block bb; | |
2489 | bool expanded = false; | |
2490 | ||
dc223ad4 ML |
2491 | auto_vec<gimple *> switch_statements; |
2492 | switch_statements.create (1); | |
2493 | ||
9dc3d6a9 ML |
2494 | FOR_EACH_BB_FN (bb, fun) |
2495 | { | |
2496 | gimple *stmt = last_stmt (bb); | |
e6c5d9f0 ML |
2497 | gswitch *swtch; |
2498 | if (stmt && (swtch = dyn_cast<gswitch *> (stmt))) | |
2499 | { | |
2500 | if (!O0) | |
2501 | group_case_labels_stmt (swtch); | |
2502 | switch_statements.safe_push (swtch); | |
2503 | } | |
dc223ad4 ML |
2504 | } |
2505 | ||
2506 | for (unsigned i = 0; i < switch_statements.length (); i++) | |
2507 | { | |
2508 | gimple *stmt = switch_statements[i]; | |
2509 | if (dump_file) | |
9dc3d6a9 | 2510 | { |
dc223ad4 | 2511 | expanded_location loc = expand_location (gimple_location (stmt)); |
9dc3d6a9 | 2512 | |
dc223ad4 ML |
2513 | fprintf (dump_file, "beginning to process the following " |
2514 | "SWITCH statement (%s:%d) : ------- \n", | |
2515 | loc.file, loc.line); | |
2516 | print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); | |
2517 | putc ('\n', dump_file); | |
2518 | } | |
9dc3d6a9 | 2519 | |
dc223ad4 ML |
2520 | gswitch *swtch = dyn_cast<gswitch *> (stmt); |
2521 | if (swtch) | |
2522 | { | |
2523 | switch_decision_tree dt (swtch); | |
2524 | expanded |= dt.analyze_switch_statement (); | |
9dc3d6a9 ML |
2525 | } |
2526 | } | |
2527 | ||
2528 | if (expanded) | |
2529 | { | |
2530 | free_dominance_info (CDI_DOMINATORS); | |
2531 | free_dominance_info (CDI_POST_DOMINATORS); | |
2532 | mark_virtual_operands_for_renaming (cfun); | |
2533 | } | |
2534 | ||
2535 | return 0; | |
2536 | } | |
2537 | ||
2538 | } // anon namespace | |
2539 | ||
2540 | gimple_opt_pass * | |
eb63c01f | 2541 | make_pass_lower_switch_O0 (gcc::context *ctxt) |
9dc3d6a9 | 2542 | { |
eb63c01f | 2543 | return new pass_lower_switch<true> (ctxt); |
9dc3d6a9 | 2544 | } |
eb63c01f ML |
2545 | gimple_opt_pass * |
2546 | make_pass_lower_switch (gcc::context *ctxt) | |
9dc3d6a9 | 2547 | { |
eb63c01f | 2548 | return new pass_lower_switch<false> (ctxt); |
9dc3d6a9 ML |
2549 | } |
2550 | ||
9dc3d6a9 | 2551 |