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