warn-access: wrong -Wdangling-pointer with labels [PR106080]
[official-gcc.git] / gcc / tree-switch-conversion.cc
blobc08c22039c939d244bd9c5302cfb9b91d9b3b2e6
1 /* Lower GIMPLE_SWITCH expressions to something more efficient than
2 a jump table.
3 Copyright (C) 2006-2023 Free Software Foundation, Inc.
5 This file is part of GCC.
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.
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.
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. */
22 /* This file handles the lowering of GIMPLE_SWITCH to an indexed
23 load, or a series of bit-test-and-branch expressions. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "backend.h"
29 #include "insn-codes.h"
30 #include "rtl.h"
31 #include "tree.h"
32 #include "gimple.h"
33 #include "cfghooks.h"
34 #include "tree-pass.h"
35 #include "ssa.h"
36 #include "optabs-tree.h"
37 #include "cgraph.h"
38 #include "gimple-pretty-print.h"
39 #include "fold-const.h"
40 #include "varasm.h"
41 #include "stor-layout.h"
42 #include "cfganal.h"
43 #include "gimplify.h"
44 #include "gimple-iterator.h"
45 #include "gimplify-me.h"
46 #include "gimple-fold.h"
47 #include "tree-cfg.h"
48 #include "cfgloop.h"
49 #include "alloc-pool.h"
50 #include "target.h"
51 #include "tree-into-ssa.h"
52 #include "omp-general.h"
53 #include "gimple-range.h"
54 #include "tree-cfgcleanup.h"
56 /* ??? For lang_hooks.types.type_for_mode, but is there a word_mode
57 type in the GIMPLE type system that is language-independent? */
58 #include "langhooks.h"
60 #include "tree-switch-conversion.h"
62 using namespace tree_switch_conversion;
64 /* Constructor. */
66 switch_conversion::switch_conversion (): m_final_bb (NULL),
67 m_constructors (NULL), m_default_values (NULL),
68 m_arr_ref_first (NULL), m_arr_ref_last (NULL),
69 m_reason (NULL), m_default_case_nonstandard (false), m_cfg_altered (false)
73 /* Collection information about SWTCH statement. */
75 void
76 switch_conversion::collect (gswitch *swtch)
78 unsigned int branch_num = gimple_switch_num_labels (swtch);
79 tree min_case, max_case;
80 unsigned int i;
81 edge e, e_default, e_first;
82 edge_iterator ei;
84 m_switch = swtch;
86 /* The gimplifier has already sorted the cases by CASE_LOW and ensured there
87 is a default label which is the first in the vector.
88 Collect the bits we can deduce from the CFG. */
89 m_index_expr = gimple_switch_index (swtch);
90 m_switch_bb = gimple_bb (swtch);
91 e_default = gimple_switch_default_edge (cfun, swtch);
92 m_default_bb = e_default->dest;
93 m_default_prob = e_default->probability;
95 /* Get upper and lower bounds of case values, and the covered range. */
96 min_case = gimple_switch_label (swtch, 1);
97 max_case = gimple_switch_label (swtch, branch_num - 1);
99 m_range_min = CASE_LOW (min_case);
100 if (CASE_HIGH (max_case) != NULL_TREE)
101 m_range_max = CASE_HIGH (max_case);
102 else
103 m_range_max = CASE_LOW (max_case);
105 m_contiguous_range = true;
106 tree last = CASE_HIGH (min_case) ? CASE_HIGH (min_case) : m_range_min;
107 for (i = 2; i < branch_num; i++)
109 tree elt = gimple_switch_label (swtch, i);
110 if (wi::to_wide (last) + 1 != wi::to_wide (CASE_LOW (elt)))
112 m_contiguous_range = false;
113 break;
115 last = CASE_HIGH (elt) ? CASE_HIGH (elt) : CASE_LOW (elt);
118 if (m_contiguous_range)
119 e_first = gimple_switch_edge (cfun, swtch, 1);
120 else
121 e_first = e_default;
123 /* See if there is one common successor block for all branch
124 targets. If it exists, record it in FINAL_BB.
125 Start with the destination of the first non-default case
126 if the range is contiguous and default case otherwise as
127 guess or its destination in case it is a forwarder block. */
128 if (! single_pred_p (e_first->dest))
129 m_final_bb = e_first->dest;
130 else if (single_succ_p (e_first->dest)
131 && ! single_pred_p (single_succ (e_first->dest)))
132 m_final_bb = single_succ (e_first->dest);
133 /* Require that all switch destinations are either that common
134 FINAL_BB or a forwarder to it, except for the default
135 case if contiguous range. */
136 auto_vec<edge, 10> fw_edges;
137 m_uniq = 0;
138 if (m_final_bb)
139 FOR_EACH_EDGE (e, ei, m_switch_bb->succs)
141 edge phi_e = nullptr;
142 if (e->dest == m_final_bb)
143 phi_e = e;
144 else if (single_pred_p (e->dest)
145 && single_succ_p (e->dest)
146 && single_succ (e->dest) == m_final_bb)
147 phi_e = single_succ_edge (e->dest);
148 if (phi_e)
150 if (e == e_default)
152 else if (phi_e == e || empty_block_p (e->dest))
154 /* For empty blocks consider forwarders with equal
155 PHI arguments in m_final_bb as unique. */
156 unsigned i;
157 for (i = 0; i < fw_edges.length (); ++i)
158 if (phi_alternatives_equal (m_final_bb, fw_edges[i], phi_e))
159 break;
160 if (i == fw_edges.length ())
162 /* But limit the above possibly quadratic search. */
163 if (fw_edges.length () < 10)
164 fw_edges.quick_push (phi_e);
165 m_uniq++;
168 else
169 m_uniq++;
170 continue;
173 if (e == e_default && m_contiguous_range)
175 m_default_case_nonstandard = true;
176 continue;
179 m_final_bb = NULL;
180 break;
183 /* When there's not a single common successor block conservatively
184 approximate the number of unique non-default targets. */
185 if (!m_final_bb)
186 m_uniq = EDGE_COUNT (gimple_bb (swtch)->succs) - 1;
188 m_range_size
189 = int_const_binop (MINUS_EXPR, m_range_max, m_range_min);
191 /* Get a count of the number of case labels. Single-valued case labels
192 simply count as one, but a case range counts double, since it may
193 require two compares if it gets lowered as a branching tree. */
194 m_count = 0;
195 for (i = 1; i < branch_num; i++)
197 tree elt = gimple_switch_label (swtch, i);
198 m_count++;
199 if (CASE_HIGH (elt)
200 && ! tree_int_cst_equal (CASE_LOW (elt), CASE_HIGH (elt)))
201 m_count++;
205 /* Checks whether the range given by individual case statements of the switch
206 switch statement isn't too big and whether the number of branches actually
207 satisfies the size of the new array. */
209 bool
210 switch_conversion::check_range ()
212 gcc_assert (m_range_size);
213 if (!tree_fits_uhwi_p (m_range_size))
215 m_reason = "index range way too large or otherwise unusable";
216 return false;
219 if (tree_to_uhwi (m_range_size)
220 > ((unsigned) m_count * param_switch_conversion_branch_ratio))
222 m_reason = "the maximum range-branch ratio exceeded";
223 return false;
226 return true;
229 /* Checks whether all but the final BB basic blocks are empty. */
231 bool
232 switch_conversion::check_all_empty_except_final ()
234 edge e, e_default = find_edge (m_switch_bb, m_default_bb);
235 edge_iterator ei;
237 FOR_EACH_EDGE (e, ei, m_switch_bb->succs)
239 if (e->dest == m_final_bb)
240 continue;
242 if (!empty_block_p (e->dest))
244 if (m_contiguous_range && e == e_default)
246 m_default_case_nonstandard = true;
247 continue;
250 m_reason = "bad case - a non-final BB not empty";
251 return false;
255 return true;
258 /* This function checks whether all required values in phi nodes in final_bb
259 are constants. Required values are those that correspond to a basic block
260 which is a part of the examined switch statement. It returns true if the
261 phi nodes are OK, otherwise false. */
263 bool
264 switch_conversion::check_final_bb ()
266 gphi_iterator gsi;
268 m_phi_count = 0;
269 for (gsi = gsi_start_phis (m_final_bb); !gsi_end_p (gsi); gsi_next (&gsi))
271 gphi *phi = gsi.phi ();
272 unsigned int i;
274 if (virtual_operand_p (gimple_phi_result (phi)))
275 continue;
277 m_phi_count++;
279 for (i = 0; i < gimple_phi_num_args (phi); i++)
281 basic_block bb = gimple_phi_arg_edge (phi, i)->src;
283 if (bb == m_switch_bb
284 || (single_pred_p (bb)
285 && single_pred (bb) == m_switch_bb
286 && (!m_default_case_nonstandard
287 || empty_block_p (bb))))
289 tree reloc, val;
290 const char *reason = NULL;
292 val = gimple_phi_arg_def (phi, i);
293 if (!is_gimple_ip_invariant (val))
294 reason = "non-invariant value from a case";
295 else
297 reloc = initializer_constant_valid_p (val, TREE_TYPE (val));
298 if ((flag_pic && reloc != null_pointer_node)
299 || (!flag_pic && reloc == NULL_TREE))
301 if (reloc)
302 reason
303 = "value from a case would need runtime relocations";
304 else
305 reason
306 = "value from a case is not a valid initializer";
309 if (reason)
311 /* For contiguous range, we can allow non-constant
312 or one that needs relocation, as long as it is
313 only reachable from the default case. */
314 if (bb == m_switch_bb)
315 bb = m_final_bb;
316 if (!m_contiguous_range || bb != m_default_bb)
318 m_reason = reason;
319 return false;
322 unsigned int branch_num = gimple_switch_num_labels (m_switch);
323 for (unsigned int i = 1; i < branch_num; i++)
325 if (gimple_switch_label_bb (cfun, m_switch, i) == bb)
327 m_reason = reason;
328 return false;
331 m_default_case_nonstandard = true;
337 return true;
340 /* The following function allocates default_values, target_{in,out}_names and
341 constructors arrays. The last one is also populated with pointers to
342 vectors that will become constructors of new arrays. */
344 void
345 switch_conversion::create_temp_arrays ()
347 int i;
349 m_default_values = XCNEWVEC (tree, m_phi_count * 3);
350 /* ??? Macros do not support multi argument templates in their
351 argument list. We create a typedef to work around that problem. */
352 typedef vec<constructor_elt, va_gc> *vec_constructor_elt_gc;
353 m_constructors = XCNEWVEC (vec_constructor_elt_gc, m_phi_count);
354 m_target_inbound_names = m_default_values + m_phi_count;
355 m_target_outbound_names = m_target_inbound_names + m_phi_count;
356 for (i = 0; i < m_phi_count; i++)
357 vec_alloc (m_constructors[i], tree_to_uhwi (m_range_size) + 1);
360 /* Populate the array of default values in the order of phi nodes.
361 DEFAULT_CASE is the CASE_LABEL_EXPR for the default switch branch
362 if the range is non-contiguous or the default case has standard
363 structure, otherwise it is the first non-default case instead. */
365 void
366 switch_conversion::gather_default_values (tree default_case)
368 gphi_iterator gsi;
369 basic_block bb = label_to_block (cfun, CASE_LABEL (default_case));
370 edge e;
371 int i = 0;
373 gcc_assert (CASE_LOW (default_case) == NULL_TREE
374 || m_default_case_nonstandard);
376 if (bb == m_final_bb)
377 e = find_edge (m_switch_bb, bb);
378 else
379 e = single_succ_edge (bb);
381 for (gsi = gsi_start_phis (m_final_bb); !gsi_end_p (gsi); gsi_next (&gsi))
383 gphi *phi = gsi.phi ();
384 if (virtual_operand_p (gimple_phi_result (phi)))
385 continue;
386 tree val = PHI_ARG_DEF_FROM_EDGE (phi, e);
387 gcc_assert (val);
388 m_default_values[i++] = val;
392 /* The following function populates the vectors in the constructors array with
393 future contents of the static arrays. The vectors are populated in the
394 order of phi nodes. */
396 void
397 switch_conversion::build_constructors ()
399 unsigned i, branch_num = gimple_switch_num_labels (m_switch);
400 tree pos = m_range_min;
401 tree pos_one = build_int_cst (TREE_TYPE (pos), 1);
403 for (i = 1; i < branch_num; i++)
405 tree cs = gimple_switch_label (m_switch, i);
406 basic_block bb = label_to_block (cfun, CASE_LABEL (cs));
407 edge e;
408 tree high;
409 gphi_iterator gsi;
410 int j;
412 if (bb == m_final_bb)
413 e = find_edge (m_switch_bb, bb);
414 else
415 e = single_succ_edge (bb);
416 gcc_assert (e);
418 while (tree_int_cst_lt (pos, CASE_LOW (cs)))
420 int k;
421 for (k = 0; k < m_phi_count; k++)
423 constructor_elt elt;
425 elt.index = int_const_binop (MINUS_EXPR, pos, m_range_min);
426 elt.value
427 = unshare_expr_without_location (m_default_values[k]);
428 m_constructors[k]->quick_push (elt);
431 pos = int_const_binop (PLUS_EXPR, pos, pos_one);
433 gcc_assert (tree_int_cst_equal (pos, CASE_LOW (cs)));
435 j = 0;
436 if (CASE_HIGH (cs))
437 high = CASE_HIGH (cs);
438 else
439 high = CASE_LOW (cs);
440 for (gsi = gsi_start_phis (m_final_bb);
441 !gsi_end_p (gsi); gsi_next (&gsi))
443 gphi *phi = gsi.phi ();
444 if (virtual_operand_p (gimple_phi_result (phi)))
445 continue;
446 tree val = PHI_ARG_DEF_FROM_EDGE (phi, e);
447 tree low = CASE_LOW (cs);
448 pos = CASE_LOW (cs);
452 constructor_elt elt;
454 elt.index = int_const_binop (MINUS_EXPR, pos, m_range_min);
455 elt.value = unshare_expr_without_location (val);
456 m_constructors[j]->quick_push (elt);
458 pos = int_const_binop (PLUS_EXPR, pos, pos_one);
459 } while (!tree_int_cst_lt (high, pos)
460 && tree_int_cst_lt (low, pos));
461 j++;
466 /* If all values in the constructor vector are products of a linear function
467 a * x + b, then return true. When true, COEFF_A and COEFF_B and
468 coefficients of the linear function. Note that equal values are special
469 case of a linear function with a and b equal to zero. */
471 bool
472 switch_conversion::contains_linear_function_p (vec<constructor_elt, va_gc> *vec,
473 wide_int *coeff_a,
474 wide_int *coeff_b)
476 unsigned int i;
477 constructor_elt *elt;
479 gcc_assert (vec->length () >= 2);
481 /* Let's try to find any linear function a * x + y that can apply to
482 given values. 'a' can be calculated as follows:
484 a = (y2 - y1) / (x2 - x1) where x2 - x1 = 1 (consecutive case indices)
485 a = y2 - y1
489 b = y2 - a * x2
493 tree elt0 = (*vec)[0].value;
494 tree elt1 = (*vec)[1].value;
496 if (TREE_CODE (elt0) != INTEGER_CST || TREE_CODE (elt1) != INTEGER_CST)
497 return false;
499 wide_int range_min
500 = wide_int::from (wi::to_wide (m_range_min),
501 TYPE_PRECISION (TREE_TYPE (elt0)),
502 TYPE_SIGN (TREE_TYPE (m_range_min)));
503 wide_int y1 = wi::to_wide (elt0);
504 wide_int y2 = wi::to_wide (elt1);
505 wide_int a = y2 - y1;
506 wide_int b = y2 - a * (range_min + 1);
508 /* Verify that all values fulfill the linear function. */
509 FOR_EACH_VEC_SAFE_ELT (vec, i, elt)
511 if (TREE_CODE (elt->value) != INTEGER_CST)
512 return false;
514 wide_int value = wi::to_wide (elt->value);
515 if (a * range_min + b != value)
516 return false;
518 ++range_min;
521 *coeff_a = a;
522 *coeff_b = b;
524 return true;
527 /* Return type which should be used for array elements, either TYPE's
528 main variant or, for integral types, some smaller integral type
529 that can still hold all the constants. */
531 tree
532 switch_conversion::array_value_type (tree type, int num)
534 unsigned int i, len = vec_safe_length (m_constructors[num]);
535 constructor_elt *elt;
536 int sign = 0;
537 tree smaller_type;
539 /* Types with alignments greater than their size can reach here, e.g. out of
540 SRA. We couldn't use these as an array component type so get back to the
541 main variant first, which, for our purposes, is fine for other types as
542 well. */
544 type = TYPE_MAIN_VARIANT (type);
546 if (!INTEGRAL_TYPE_P (type))
547 return type;
549 scalar_int_mode type_mode = SCALAR_INT_TYPE_MODE (type);
550 scalar_int_mode mode = get_narrowest_mode (type_mode);
551 if (GET_MODE_SIZE (type_mode) <= GET_MODE_SIZE (mode))
552 return type;
554 if (len < (optimize_bb_for_size_p (gimple_bb (m_switch)) ? 2 : 32))
555 return type;
557 FOR_EACH_VEC_SAFE_ELT (m_constructors[num], i, elt)
559 wide_int cst;
561 if (TREE_CODE (elt->value) != INTEGER_CST)
562 return type;
564 cst = wi::to_wide (elt->value);
565 while (1)
567 unsigned int prec = GET_MODE_BITSIZE (mode);
568 if (prec > HOST_BITS_PER_WIDE_INT)
569 return type;
571 if (sign >= 0 && cst == wi::zext (cst, prec))
573 if (sign == 0 && cst == wi::sext (cst, prec))
574 break;
575 sign = 1;
576 break;
578 if (sign <= 0 && cst == wi::sext (cst, prec))
580 sign = -1;
581 break;
584 if (sign == 1)
585 sign = 0;
587 if (!GET_MODE_WIDER_MODE (mode).exists (&mode)
588 || GET_MODE_SIZE (mode) >= GET_MODE_SIZE (type_mode))
589 return type;
593 if (sign == 0)
594 sign = TYPE_UNSIGNED (type) ? 1 : -1;
595 smaller_type = lang_hooks.types.type_for_mode (mode, sign >= 0);
596 if (GET_MODE_SIZE (type_mode)
597 <= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (smaller_type)))
598 return type;
600 return smaller_type;
603 /* Create an appropriate array type and declaration and assemble a static
604 array variable. Also create a load statement that initializes
605 the variable in question with a value from the static array. SWTCH is
606 the switch statement being converted, NUM is the index to
607 arrays of constructors, default values and target SSA names
608 for this particular array. ARR_INDEX_TYPE is the type of the index
609 of the new array, PHI is the phi node of the final BB that corresponds
610 to the value that will be loaded from the created array. TIDX
611 is an ssa name of a temporary variable holding the index for loads from the
612 new array. */
614 void
615 switch_conversion::build_one_array (int num, tree arr_index_type,
616 gphi *phi, tree tidx)
618 tree name;
619 gimple *load;
620 gimple_stmt_iterator gsi = gsi_for_stmt (m_switch);
621 location_t loc = gimple_location (m_switch);
623 gcc_assert (m_default_values[num]);
625 name = copy_ssa_name (PHI_RESULT (phi));
626 m_target_inbound_names[num] = name;
628 vec<constructor_elt, va_gc> *constructor = m_constructors[num];
629 wide_int coeff_a, coeff_b;
630 bool linear_p = contains_linear_function_p (constructor, &coeff_a, &coeff_b);
631 tree type;
632 if (linear_p
633 && (type = range_check_type (TREE_TYPE ((*constructor)[0].value))))
635 if (dump_file && coeff_a.to_uhwi () > 0)
636 fprintf (dump_file, "Linear transformation with A = %" PRId64
637 " and B = %" PRId64 "\n", coeff_a.to_shwi (),
638 coeff_b.to_shwi ());
640 /* We must use type of constructor values. */
641 gimple_seq seq = NULL;
642 tree tmp = gimple_convert (&seq, type, m_index_expr);
643 tree tmp2 = gimple_build (&seq, MULT_EXPR, type,
644 wide_int_to_tree (type, coeff_a), tmp);
645 tree tmp3 = gimple_build (&seq, PLUS_EXPR, type, tmp2,
646 wide_int_to_tree (type, coeff_b));
647 tree tmp4 = gimple_convert (&seq, TREE_TYPE (name), tmp3);
648 gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
649 load = gimple_build_assign (name, tmp4);
651 else
653 tree array_type, ctor, decl, value_type, fetch, default_type;
655 default_type = TREE_TYPE (m_default_values[num]);
656 value_type = array_value_type (default_type, num);
657 array_type = build_array_type (value_type, arr_index_type);
658 if (default_type != value_type)
660 unsigned int i;
661 constructor_elt *elt;
663 FOR_EACH_VEC_SAFE_ELT (constructor, i, elt)
664 elt->value = fold_convert (value_type, elt->value);
666 ctor = build_constructor (array_type, constructor);
667 TREE_CONSTANT (ctor) = true;
668 TREE_STATIC (ctor) = true;
670 decl = build_decl (loc, VAR_DECL, NULL_TREE, array_type);
671 TREE_STATIC (decl) = 1;
672 DECL_INITIAL (decl) = ctor;
674 DECL_NAME (decl) = create_tmp_var_name ("CSWTCH");
675 DECL_ARTIFICIAL (decl) = 1;
676 DECL_IGNORED_P (decl) = 1;
677 TREE_CONSTANT (decl) = 1;
678 TREE_READONLY (decl) = 1;
679 DECL_IGNORED_P (decl) = 1;
680 if (offloading_function_p (cfun->decl))
681 DECL_ATTRIBUTES (decl)
682 = tree_cons (get_identifier ("omp declare target"), NULL_TREE,
683 NULL_TREE);
684 varpool_node::finalize_decl (decl);
686 fetch = build4 (ARRAY_REF, value_type, decl, tidx, NULL_TREE,
687 NULL_TREE);
688 if (default_type != value_type)
690 fetch = fold_convert (default_type, fetch);
691 fetch = force_gimple_operand_gsi (&gsi, fetch, true, NULL_TREE,
692 true, GSI_SAME_STMT);
694 load = gimple_build_assign (name, fetch);
697 gsi_insert_before (&gsi, load, GSI_SAME_STMT);
698 update_stmt (load);
699 m_arr_ref_last = load;
702 /* Builds and initializes static arrays initialized with values gathered from
703 the switch statement. Also creates statements that load values from
704 them. */
706 void
707 switch_conversion::build_arrays ()
709 tree arr_index_type;
710 tree tidx, sub, utype;
711 gimple *stmt;
712 gimple_stmt_iterator gsi;
713 gphi_iterator gpi;
714 int i;
715 location_t loc = gimple_location (m_switch);
717 gsi = gsi_for_stmt (m_switch);
719 /* Make sure we do not generate arithmetics in a subrange. */
720 utype = TREE_TYPE (m_index_expr);
721 if (TREE_TYPE (utype))
722 utype = lang_hooks.types.type_for_mode (TYPE_MODE (TREE_TYPE (utype)), 1);
723 else
724 utype = lang_hooks.types.type_for_mode (TYPE_MODE (utype), 1);
726 arr_index_type = build_index_type (m_range_size);
727 tidx = make_ssa_name (utype);
728 sub = fold_build2_loc (loc, MINUS_EXPR, utype,
729 fold_convert_loc (loc, utype, m_index_expr),
730 fold_convert_loc (loc, utype, m_range_min));
731 sub = force_gimple_operand_gsi (&gsi, sub,
732 false, NULL, true, GSI_SAME_STMT);
733 stmt = gimple_build_assign (tidx, sub);
735 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
736 update_stmt (stmt);
737 m_arr_ref_first = stmt;
739 for (gpi = gsi_start_phis (m_final_bb), i = 0;
740 !gsi_end_p (gpi); gsi_next (&gpi))
742 gphi *phi = gpi.phi ();
743 if (!virtual_operand_p (gimple_phi_result (phi)))
744 build_one_array (i++, arr_index_type, phi, tidx);
745 else
747 edge e;
748 edge_iterator ei;
749 FOR_EACH_EDGE (e, ei, m_switch_bb->succs)
751 if (e->dest == m_final_bb)
752 break;
753 if (!m_default_case_nonstandard
754 || e->dest != m_default_bb)
756 e = single_succ_edge (e->dest);
757 break;
760 gcc_assert (e && e->dest == m_final_bb);
761 m_target_vop = PHI_ARG_DEF_FROM_EDGE (phi, e);
766 /* Generates and appropriately inserts loads of default values at the position
767 given by GSI. Returns the last inserted statement. */
769 gassign *
770 switch_conversion::gen_def_assigns (gimple_stmt_iterator *gsi)
772 int i;
773 gassign *assign = NULL;
775 for (i = 0; i < m_phi_count; i++)
777 tree name = copy_ssa_name (m_target_inbound_names[i]);
778 m_target_outbound_names[i] = name;
779 assign = gimple_build_assign (name, m_default_values[i]);
780 gsi_insert_before (gsi, assign, GSI_SAME_STMT);
781 update_stmt (assign);
783 return assign;
786 /* Deletes the unused bbs and edges that now contain the switch statement and
787 its empty branch bbs. BBD is the now dead BB containing
788 the original switch statement, FINAL is the last BB of the converted
789 switch statement (in terms of succession). */
791 void
792 switch_conversion::prune_bbs (basic_block bbd, basic_block final,
793 basic_block default_bb)
795 edge_iterator ei;
796 edge e;
798 for (ei = ei_start (bbd->succs); (e = ei_safe_edge (ei)); )
800 basic_block bb;
801 bb = e->dest;
802 remove_edge (e);
803 if (bb != final && bb != default_bb)
804 delete_basic_block (bb);
806 delete_basic_block (bbd);
809 /* Add values to phi nodes in final_bb for the two new edges. E1F is the edge
810 from the basic block loading values from an array and E2F from the basic
811 block loading default values. BBF is the last switch basic block (see the
812 bbf description in the comment below). */
814 void
815 switch_conversion::fix_phi_nodes (edge e1f, edge e2f, basic_block bbf)
817 gphi_iterator gsi;
818 int i;
820 for (gsi = gsi_start_phis (bbf), i = 0;
821 !gsi_end_p (gsi); gsi_next (&gsi))
823 gphi *phi = gsi.phi ();
824 tree inbound, outbound;
825 if (virtual_operand_p (gimple_phi_result (phi)))
826 inbound = outbound = m_target_vop;
827 else
829 inbound = m_target_inbound_names[i];
830 outbound = m_target_outbound_names[i++];
832 add_phi_arg (phi, inbound, e1f, UNKNOWN_LOCATION);
833 if (!m_default_case_nonstandard)
834 add_phi_arg (phi, outbound, e2f, UNKNOWN_LOCATION);
838 /* Creates a check whether the switch expression value actually falls into the
839 range given by all the cases. If it does not, the temporaries are loaded
840 with default values instead. */
842 void
843 switch_conversion::gen_inbound_check ()
845 tree label_decl1 = create_artificial_label (UNKNOWN_LOCATION);
846 tree label_decl2 = create_artificial_label (UNKNOWN_LOCATION);
847 tree label_decl3 = create_artificial_label (UNKNOWN_LOCATION);
848 glabel *label1, *label2, *label3;
849 tree utype, tidx;
850 tree bound;
852 gcond *cond_stmt;
854 gassign *last_assign = NULL;
855 gimple_stmt_iterator gsi;
856 basic_block bb0, bb1, bb2, bbf, bbd;
857 edge e01 = NULL, e02, e21, e1d, e1f, e2f;
858 location_t loc = gimple_location (m_switch);
860 gcc_assert (m_default_values);
862 bb0 = gimple_bb (m_switch);
864 tidx = gimple_assign_lhs (m_arr_ref_first);
865 utype = TREE_TYPE (tidx);
867 /* (end of) block 0 */
868 gsi = gsi_for_stmt (m_arr_ref_first);
869 gsi_next (&gsi);
871 bound = fold_convert_loc (loc, utype, m_range_size);
872 cond_stmt = gimple_build_cond (LE_EXPR, tidx, bound, NULL_TREE, NULL_TREE);
873 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
874 update_stmt (cond_stmt);
876 /* block 2 */
877 if (!m_default_case_nonstandard)
879 label2 = gimple_build_label (label_decl2);
880 gsi_insert_before (&gsi, label2, GSI_SAME_STMT);
881 last_assign = gen_def_assigns (&gsi);
884 /* block 1 */
885 label1 = gimple_build_label (label_decl1);
886 gsi_insert_before (&gsi, label1, GSI_SAME_STMT);
888 /* block F */
889 gsi = gsi_start_bb (m_final_bb);
890 label3 = gimple_build_label (label_decl3);
891 gsi_insert_before (&gsi, label3, GSI_SAME_STMT);
893 /* cfg fix */
894 e02 = split_block (bb0, cond_stmt);
895 bb2 = e02->dest;
897 if (m_default_case_nonstandard)
899 bb1 = bb2;
900 bb2 = m_default_bb;
901 e01 = e02;
902 e01->flags = EDGE_TRUE_VALUE;
903 e02 = make_edge (bb0, bb2, EDGE_FALSE_VALUE);
904 edge e_default = find_edge (bb1, bb2);
905 for (gphi_iterator gsi = gsi_start_phis (bb2);
906 !gsi_end_p (gsi); gsi_next (&gsi))
908 gphi *phi = gsi.phi ();
909 tree arg = PHI_ARG_DEF_FROM_EDGE (phi, e_default);
910 add_phi_arg (phi, arg, e02,
911 gimple_phi_arg_location_from_edge (phi, e_default));
913 /* Partially fix the dominator tree, if it is available. */
914 if (dom_info_available_p (CDI_DOMINATORS))
915 redirect_immediate_dominators (CDI_DOMINATORS, bb1, bb0);
917 else
919 e21 = split_block (bb2, last_assign);
920 bb1 = e21->dest;
921 remove_edge (e21);
924 e1d = split_block (bb1, m_arr_ref_last);
925 bbd = e1d->dest;
926 remove_edge (e1d);
928 /* Flags and profiles of the edge for in-range values. */
929 if (!m_default_case_nonstandard)
930 e01 = make_edge (bb0, bb1, EDGE_TRUE_VALUE);
931 e01->probability = m_default_prob.invert ();
933 /* Flags and profiles of the edge taking care of out-of-range values. */
934 e02->flags &= ~EDGE_FALLTHRU;
935 e02->flags |= EDGE_FALSE_VALUE;
936 e02->probability = m_default_prob;
938 bbf = m_final_bb;
940 e1f = make_edge (bb1, bbf, EDGE_FALLTHRU);
941 e1f->probability = profile_probability::always ();
943 if (m_default_case_nonstandard)
944 e2f = NULL;
945 else
947 e2f = make_edge (bb2, bbf, EDGE_FALLTHRU);
948 e2f->probability = profile_probability::always ();
951 /* frequencies of the new BBs */
952 bb1->count = e01->count ();
953 bb2->count = e02->count ();
954 if (!m_default_case_nonstandard)
955 bbf->count = e1f->count () + e2f->count ();
957 /* Tidy blocks that have become unreachable. */
958 prune_bbs (bbd, m_final_bb,
959 m_default_case_nonstandard ? m_default_bb : NULL);
961 /* Fixup the PHI nodes in bbF. */
962 fix_phi_nodes (e1f, e2f, bbf);
964 /* Fix the dominator tree, if it is available. */
965 if (dom_info_available_p (CDI_DOMINATORS))
967 vec<basic_block> bbs_to_fix_dom;
969 set_immediate_dominator (CDI_DOMINATORS, bb1, bb0);
970 if (!m_default_case_nonstandard)
971 set_immediate_dominator (CDI_DOMINATORS, bb2, bb0);
972 if (! get_immediate_dominator (CDI_DOMINATORS, bbf))
973 /* If bbD was the immediate dominator ... */
974 set_immediate_dominator (CDI_DOMINATORS, bbf, bb0);
976 bbs_to_fix_dom.create (3 + (bb2 != bbf));
977 bbs_to_fix_dom.quick_push (bb0);
978 bbs_to_fix_dom.quick_push (bb1);
979 if (bb2 != bbf)
980 bbs_to_fix_dom.quick_push (bb2);
981 bbs_to_fix_dom.quick_push (bbf);
983 iterate_fix_dominators (CDI_DOMINATORS, bbs_to_fix_dom, true);
984 bbs_to_fix_dom.release ();
988 /* The following function is invoked on every switch statement (the current
989 one is given in SWTCH) and runs the individual phases of switch
990 conversion on it one after another until one fails or the conversion
991 is completed. On success, NULL is in m_reason, otherwise points
992 to a string with the reason why the conversion failed. */
994 void
995 switch_conversion::expand (gswitch *swtch)
997 /* Group case labels so that we get the right results from the heuristics
998 that decide on the code generation approach for this switch. */
999 m_cfg_altered |= group_case_labels_stmt (swtch);
1001 /* If this switch is now a degenerate case with only a default label,
1002 there is nothing left for us to do. */
1003 if (gimple_switch_num_labels (swtch) < 2)
1005 m_reason = "switch is a degenerate case";
1006 return;
1009 collect (swtch);
1011 /* No error markers should reach here (they should be filtered out
1012 during gimplification). */
1013 gcc_checking_assert (TREE_TYPE (m_index_expr) != error_mark_node);
1015 /* Prefer bit test if possible. */
1016 if (tree_fits_uhwi_p (m_range_size)
1017 && bit_test_cluster::can_be_handled (tree_to_uhwi (m_range_size), m_uniq)
1018 && bit_test_cluster::is_beneficial (m_count, m_uniq))
1020 m_reason = "expanding as bit test is preferable";
1021 return;
1024 if (m_uniq <= 2)
1026 /* This will be expanded as a decision tree . */
1027 m_reason = "expanding as jumps is preferable";
1028 return;
1031 /* If there is no common successor, we cannot do the transformation. */
1032 if (!m_final_bb)
1034 m_reason = "no common successor to all case label target blocks found";
1035 return;
1038 /* Check the case label values are within reasonable range: */
1039 if (!check_range ())
1041 gcc_assert (m_reason);
1042 return;
1045 /* For all the cases, see whether they are empty, the assignments they
1046 represent constant and so on... */
1047 if (!check_all_empty_except_final ())
1049 gcc_assert (m_reason);
1050 return;
1052 if (!check_final_bb ())
1054 gcc_assert (m_reason);
1055 return;
1058 /* At this point all checks have passed and we can proceed with the
1059 transformation. */
1061 create_temp_arrays ();
1062 gather_default_values (m_default_case_nonstandard
1063 ? gimple_switch_label (swtch, 1)
1064 : gimple_switch_default_label (swtch));
1065 build_constructors ();
1067 build_arrays (); /* Build the static arrays and assignments. */
1068 gen_inbound_check (); /* Build the bounds check. */
1070 m_cfg_altered = true;
1073 /* Destructor. */
1075 switch_conversion::~switch_conversion ()
1077 XDELETEVEC (m_constructors);
1078 XDELETEVEC (m_default_values);
1081 /* Constructor. */
1083 group_cluster::group_cluster (vec<cluster *> &clusters,
1084 unsigned start, unsigned end)
1086 gcc_checking_assert (end - start + 1 >= 1);
1087 m_prob = profile_probability::never ();
1088 m_cases.create (end - start + 1);
1089 for (unsigned i = start; i <= end; i++)
1091 m_cases.quick_push (static_cast<simple_cluster *> (clusters[i]));
1092 m_prob += clusters[i]->m_prob;
1094 m_subtree_prob = m_prob;
1097 /* Destructor. */
1099 group_cluster::~group_cluster ()
1101 for (unsigned i = 0; i < m_cases.length (); i++)
1102 delete m_cases[i];
1104 m_cases.release ();
1107 /* Dump content of a cluster. */
1109 void
1110 group_cluster::dump (FILE *f, bool details)
1112 unsigned total_values = 0;
1113 for (unsigned i = 0; i < m_cases.length (); i++)
1114 total_values += m_cases[i]->get_range (m_cases[i]->get_low (),
1115 m_cases[i]->get_high ());
1117 unsigned comparison_count = 0;
1118 for (unsigned i = 0; i < m_cases.length (); i++)
1120 simple_cluster *sc = static_cast<simple_cluster *> (m_cases[i]);
1121 comparison_count += sc->get_comparison_count ();
1124 unsigned HOST_WIDE_INT range = get_range (get_low (), get_high ());
1125 fprintf (f, "%s", get_type () == JUMP_TABLE ? "JT" : "BT");
1127 if (details)
1128 fprintf (f, "(values:%d comparisons:%d range:" HOST_WIDE_INT_PRINT_DEC
1129 " density: %.2f%%)", total_values, comparison_count, range,
1130 100.0f * comparison_count / range);
1132 fprintf (f, ":");
1133 PRINT_CASE (f, get_low ());
1134 fprintf (f, "-");
1135 PRINT_CASE (f, get_high ());
1136 fprintf (f, " ");
1139 /* Emit GIMPLE code to handle the cluster. */
1141 void
1142 jump_table_cluster::emit (tree index_expr, tree,
1143 tree default_label_expr, basic_block default_bb,
1144 location_t loc)
1146 unsigned HOST_WIDE_INT range = get_range (get_low (), get_high ());
1147 unsigned HOST_WIDE_INT nondefault_range = 0;
1149 /* For jump table we just emit a new gswitch statement that will
1150 be latter lowered to jump table. */
1151 auto_vec <tree> labels;
1152 labels.create (m_cases.length ());
1154 make_edge (m_case_bb, default_bb, 0);
1155 for (unsigned i = 0; i < m_cases.length (); i++)
1157 labels.quick_push (unshare_expr (m_cases[i]->m_case_label_expr));
1158 make_edge (m_case_bb, m_cases[i]->m_case_bb, 0);
1161 gswitch *s = gimple_build_switch (index_expr,
1162 unshare_expr (default_label_expr), labels);
1163 gimple_set_location (s, loc);
1164 gimple_stmt_iterator gsi = gsi_start_bb (m_case_bb);
1165 gsi_insert_after (&gsi, s, GSI_NEW_STMT);
1167 /* Set up even probabilities for all cases. */
1168 for (unsigned i = 0; i < m_cases.length (); i++)
1170 simple_cluster *sc = static_cast<simple_cluster *> (m_cases[i]);
1171 edge case_edge = find_edge (m_case_bb, sc->m_case_bb);
1172 unsigned HOST_WIDE_INT case_range
1173 = sc->get_range (sc->get_low (), sc->get_high ());
1174 nondefault_range += case_range;
1176 /* case_edge->aux is number of values in a jump-table that are covered
1177 by the case_edge. */
1178 case_edge->aux = (void *) ((intptr_t) (case_edge->aux) + case_range);
1181 edge default_edge = gimple_switch_default_edge (cfun, s);
1182 default_edge->probability = profile_probability::never ();
1184 for (unsigned i = 0; i < m_cases.length (); i++)
1186 simple_cluster *sc = static_cast<simple_cluster *> (m_cases[i]);
1187 edge case_edge = find_edge (m_case_bb, sc->m_case_bb);
1188 case_edge->probability
1189 = profile_probability::always ().apply_scale ((intptr_t)case_edge->aux,
1190 range);
1193 /* Number of non-default values is probability of default edge. */
1194 default_edge->probability
1195 += profile_probability::always ().apply_scale (nondefault_range,
1196 range).invert ();
1198 switch_decision_tree::reset_out_edges_aux (s);
1201 /* Find jump tables of given CLUSTERS, where all members of the vector
1202 are of type simple_cluster. New clusters are returned. */
1204 vec<cluster *>
1205 jump_table_cluster::find_jump_tables (vec<cluster *> &clusters)
1207 if (!is_enabled ())
1208 return clusters.copy ();
1210 unsigned l = clusters.length ();
1211 auto_vec<min_cluster_item> min;
1212 min.reserve (l + 1);
1214 min.quick_push (min_cluster_item (0, 0, 0));
1216 unsigned HOST_WIDE_INT max_ratio
1217 = (optimize_insn_for_size_p ()
1218 ? param_jump_table_max_growth_ratio_for_size
1219 : param_jump_table_max_growth_ratio_for_speed);
1221 for (unsigned i = 1; i <= l; i++)
1223 /* Set minimal # of clusters with i-th item to infinite. */
1224 min.quick_push (min_cluster_item (INT_MAX, INT_MAX, INT_MAX));
1226 /* Pre-calculate number of comparisons for the clusters. */
1227 HOST_WIDE_INT comparison_count = 0;
1228 for (unsigned k = 0; k <= i - 1; k++)
1230 simple_cluster *sc = static_cast<simple_cluster *> (clusters[k]);
1231 comparison_count += sc->get_comparison_count ();
1234 for (unsigned j = 0; j < i; j++)
1236 unsigned HOST_WIDE_INT s = min[j].m_non_jt_cases;
1237 if (i - j < case_values_threshold ())
1238 s += i - j;
1240 /* Prefer clusters with smaller number of numbers covered. */
1241 if ((min[j].m_count + 1 < min[i].m_count
1242 || (min[j].m_count + 1 == min[i].m_count
1243 && s < min[i].m_non_jt_cases))
1244 && can_be_handled (clusters, j, i - 1, max_ratio,
1245 comparison_count))
1246 min[i] = min_cluster_item (min[j].m_count + 1, j, s);
1248 simple_cluster *sc = static_cast<simple_cluster *> (clusters[j]);
1249 comparison_count -= sc->get_comparison_count ();
1252 gcc_checking_assert (comparison_count == 0);
1253 gcc_checking_assert (min[i].m_count != INT_MAX);
1256 /* No result. */
1257 if (min[l].m_count == l)
1258 return clusters.copy ();
1260 vec<cluster *> output;
1261 output.create (4);
1263 /* Find and build the clusters. */
1264 for (unsigned int end = l;;)
1266 int start = min[end].m_start;
1268 /* Do not allow clusters with small number of cases. */
1269 if (is_beneficial (clusters, start, end - 1))
1270 output.safe_push (new jump_table_cluster (clusters, start, end - 1));
1271 else
1272 for (int i = end - 1; i >= start; i--)
1273 output.safe_push (clusters[i]);
1275 end = start;
1277 if (start <= 0)
1278 break;
1281 output.reverse ();
1282 return output;
1285 /* Return true when cluster starting at START and ending at END (inclusive)
1286 can build a jump-table. */
1288 bool
1289 jump_table_cluster::can_be_handled (const vec<cluster *> &clusters,
1290 unsigned start, unsigned end,
1291 unsigned HOST_WIDE_INT max_ratio,
1292 unsigned HOST_WIDE_INT comparison_count)
1294 /* If the switch is relatively small such that the cost of one
1295 indirect jump on the target are higher than the cost of a
1296 decision tree, go with the decision tree.
1298 If range of values is much bigger than number of values,
1299 or if it is too large to represent in a HOST_WIDE_INT,
1300 make a sequence of conditional branches instead of a dispatch.
1302 The definition of "much bigger" depends on whether we are
1303 optimizing for size or for speed.
1305 For algorithm correctness, jump table for a single case must return
1306 true. We bail out in is_beneficial if it's called just for
1307 a single case. */
1308 if (start == end)
1309 return true;
1311 unsigned HOST_WIDE_INT range = get_range (clusters[start]->get_low (),
1312 clusters[end]->get_high ());
1313 /* Check overflow. */
1314 if (range == 0)
1315 return false;
1317 if (range > HOST_WIDE_INT_M1U / 100)
1318 return false;
1320 unsigned HOST_WIDE_INT lhs = 100 * range;
1321 if (lhs < range)
1322 return false;
1324 return lhs <= max_ratio * comparison_count;
1327 /* Return true if cluster starting at START and ending at END (inclusive)
1328 is profitable transformation. */
1330 bool
1331 jump_table_cluster::is_beneficial (const vec<cluster *> &,
1332 unsigned start, unsigned end)
1334 /* Single case bail out. */
1335 if (start == end)
1336 return false;
1338 return end - start + 1 >= case_values_threshold ();
1341 /* Find bit tests of given CLUSTERS, where all members of the vector
1342 are of type simple_cluster. New clusters are returned. */
1344 vec<cluster *>
1345 bit_test_cluster::find_bit_tests (vec<cluster *> &clusters)
1347 if (!is_enabled ())
1348 return clusters.copy ();
1350 unsigned l = clusters.length ();
1351 auto_vec<min_cluster_item> min;
1352 min.reserve (l + 1);
1354 min.quick_push (min_cluster_item (0, 0, 0));
1356 for (unsigned i = 1; i <= l; i++)
1358 /* Set minimal # of clusters with i-th item to infinite. */
1359 min.quick_push (min_cluster_item (INT_MAX, INT_MAX, INT_MAX));
1361 for (unsigned j = 0; j < i; j++)
1363 if (min[j].m_count + 1 < min[i].m_count
1364 && can_be_handled (clusters, j, i - 1))
1365 min[i] = min_cluster_item (min[j].m_count + 1, j, INT_MAX);
1368 gcc_checking_assert (min[i].m_count != INT_MAX);
1371 /* No result. */
1372 if (min[l].m_count == l)
1373 return clusters.copy ();
1375 vec<cluster *> output;
1376 output.create (4);
1378 /* Find and build the clusters. */
1379 for (unsigned end = l;;)
1381 int start = min[end].m_start;
1383 if (is_beneficial (clusters, start, end - 1))
1385 bool entire = start == 0 && end == clusters.length ();
1386 output.safe_push (new bit_test_cluster (clusters, start, end - 1,
1387 entire));
1389 else
1390 for (int i = end - 1; i >= start; i--)
1391 output.safe_push (clusters[i]);
1393 end = start;
1395 if (start <= 0)
1396 break;
1399 output.reverse ();
1400 return output;
1403 /* Return true when RANGE of case values with UNIQ labels
1404 can build a bit test. */
1406 bool
1407 bit_test_cluster::can_be_handled (unsigned HOST_WIDE_INT range,
1408 unsigned int uniq)
1410 /* Check overflow. */
1411 if (range == 0)
1412 return false;
1414 if (range >= GET_MODE_BITSIZE (word_mode))
1415 return false;
1417 return uniq <= m_max_case_bit_tests;
1420 /* Return true when cluster starting at START and ending at END (inclusive)
1421 can build a bit test. */
1423 bool
1424 bit_test_cluster::can_be_handled (const vec<cluster *> &clusters,
1425 unsigned start, unsigned end)
1427 auto_vec<int, m_max_case_bit_tests> dest_bbs;
1428 /* For algorithm correctness, bit test for a single case must return
1429 true. We bail out in is_beneficial if it's called just for
1430 a single case. */
1431 if (start == end)
1432 return true;
1434 unsigned HOST_WIDE_INT range = get_range (clusters[start]->get_low (),
1435 clusters[end]->get_high ());
1437 /* Make a guess first. */
1438 if (!can_be_handled (range, m_max_case_bit_tests))
1439 return false;
1441 for (unsigned i = start; i <= end; i++)
1443 simple_cluster *sc = static_cast<simple_cluster *> (clusters[i]);
1444 /* m_max_case_bit_tests is very small integer, thus the operation
1445 is constant. */
1446 if (!dest_bbs.contains (sc->m_case_bb->index))
1448 if (dest_bbs.length () >= m_max_case_bit_tests)
1449 return false;
1450 dest_bbs.quick_push (sc->m_case_bb->index);
1454 return true;
1457 /* Return true when COUNT of cases of UNIQ labels is beneficial for bit test
1458 transformation. */
1460 bool
1461 bit_test_cluster::is_beneficial (unsigned count, unsigned uniq)
1463 return (((uniq == 1 && count >= 3)
1464 || (uniq == 2 && count >= 5)
1465 || (uniq == 3 && count >= 6)));
1468 /* Return true if cluster starting at START and ending at END (inclusive)
1469 is profitable transformation. */
1471 bool
1472 bit_test_cluster::is_beneficial (const vec<cluster *> &clusters,
1473 unsigned start, unsigned end)
1475 /* Single case bail out. */
1476 if (start == end)
1477 return false;
1479 auto_bitmap dest_bbs;
1481 for (unsigned i = start; i <= end; i++)
1483 simple_cluster *sc = static_cast<simple_cluster *> (clusters[i]);
1484 bitmap_set_bit (dest_bbs, sc->m_case_bb->index);
1487 unsigned uniq = bitmap_count_bits (dest_bbs);
1488 unsigned count = end - start + 1;
1489 return is_beneficial (count, uniq);
1492 /* Comparison function for qsort to order bit tests by decreasing
1493 probability of execution. */
1496 case_bit_test::cmp (const void *p1, const void *p2)
1498 const case_bit_test *const d1 = (const case_bit_test *) p1;
1499 const case_bit_test *const d2 = (const case_bit_test *) p2;
1501 if (d2->bits != d1->bits)
1502 return d2->bits - d1->bits;
1504 /* Stabilize the sort. */
1505 return (LABEL_DECL_UID (CASE_LABEL (d2->label))
1506 - LABEL_DECL_UID (CASE_LABEL (d1->label)));
1509 /* Expand a switch statement by a short sequence of bit-wise
1510 comparisons. "switch(x)" is effectively converted into
1511 "if ((1 << (x-MINVAL)) & CST)" where CST and MINVAL are
1512 integer constants.
1514 INDEX_EXPR is the value being switched on.
1516 MINVAL is the lowest case value of in the case nodes,
1517 and RANGE is highest value minus MINVAL. MINVAL and RANGE
1518 are not guaranteed to be of the same type as INDEX_EXPR
1519 (the gimplifier doesn't change the type of case label values,
1520 and MINVAL and RANGE are derived from those values).
1521 MAXVAL is MINVAL + RANGE.
1523 There *MUST* be max_case_bit_tests or less unique case
1524 node targets. */
1526 void
1527 bit_test_cluster::emit (tree index_expr, tree index_type,
1528 tree, basic_block default_bb, location_t loc)
1530 case_bit_test test[m_max_case_bit_tests] = { {} };
1531 unsigned int i, j, k;
1532 unsigned int count;
1534 tree unsigned_index_type = range_check_type (index_type);
1536 gimple_stmt_iterator gsi;
1537 gassign *shift_stmt;
1539 tree idx, tmp, csui;
1540 tree word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
1541 tree word_mode_zero = fold_convert (word_type_node, integer_zero_node);
1542 tree word_mode_one = fold_convert (word_type_node, integer_one_node);
1543 int prec = TYPE_PRECISION (word_type_node);
1544 wide_int wone = wi::one (prec);
1546 tree minval = get_low ();
1547 tree maxval = get_high ();
1549 /* Go through all case labels, and collect the case labels, profile
1550 counts, and other information we need to build the branch tests. */
1551 count = 0;
1552 for (i = 0; i < m_cases.length (); i++)
1554 unsigned int lo, hi;
1555 simple_cluster *n = static_cast<simple_cluster *> (m_cases[i]);
1556 for (k = 0; k < count; k++)
1557 if (n->m_case_bb == test[k].target_bb)
1558 break;
1560 if (k == count)
1562 gcc_checking_assert (count < m_max_case_bit_tests);
1563 test[k].mask = wi::zero (prec);
1564 test[k].target_bb = n->m_case_bb;
1565 test[k].label = n->m_case_label_expr;
1566 test[k].bits = 0;
1567 test[k].prob = profile_probability::never ();
1568 count++;
1571 test[k].bits += n->get_range (n->get_low (), n->get_high ());
1572 test[k].prob += n->m_prob;
1574 lo = tree_to_uhwi (int_const_binop (MINUS_EXPR, n->get_low (), minval));
1575 if (n->get_high () == NULL_TREE)
1576 hi = lo;
1577 else
1578 hi = tree_to_uhwi (int_const_binop (MINUS_EXPR, n->get_high (),
1579 minval));
1581 for (j = lo; j <= hi; j++)
1582 test[k].mask |= wi::lshift (wone, j);
1585 qsort (test, count, sizeof (*test), case_bit_test::cmp);
1587 /* If every possible relative value of the index expression is a valid shift
1588 amount, then we can merge the entry test in the bit test. */
1589 bool entry_test_needed;
1590 value_range r;
1591 if (TREE_CODE (index_expr) == SSA_NAME
1592 && get_range_query (cfun)->range_of_expr (r, index_expr)
1593 && r.kind () == VR_RANGE
1594 && wi::leu_p (r.upper_bound () - r.lower_bound (), prec - 1))
1596 wide_int min = r.lower_bound ();
1597 wide_int max = r.upper_bound ();
1598 tree index_type = TREE_TYPE (index_expr);
1599 minval = fold_convert (index_type, minval);
1600 wide_int iminval = wi::to_wide (minval);
1601 if (wi::lt_p (min, iminval, TYPE_SIGN (index_type)))
1603 minval = wide_int_to_tree (index_type, min);
1604 for (i = 0; i < count; i++)
1605 test[i].mask = wi::lshift (test[i].mask, iminval - min);
1607 else if (wi::gt_p (min, iminval, TYPE_SIGN (index_type)))
1609 minval = wide_int_to_tree (index_type, min);
1610 for (i = 0; i < count; i++)
1611 test[i].mask = wi::lrshift (test[i].mask, min - iminval);
1613 maxval = wide_int_to_tree (index_type, max);
1614 entry_test_needed = false;
1616 else
1617 entry_test_needed = true;
1619 /* If all values are in the 0 .. BITS_PER_WORD-1 range, we can get rid of
1620 the minval subtractions, but it might make the mask constants more
1621 expensive. So, compare the costs. */
1622 if (compare_tree_int (minval, 0) > 0 && compare_tree_int (maxval, prec) < 0)
1624 int cost_diff;
1625 HOST_WIDE_INT m = tree_to_uhwi (minval);
1626 rtx reg = gen_raw_REG (word_mode, 10000);
1627 bool speed_p = optimize_insn_for_speed_p ();
1628 cost_diff = set_src_cost (gen_rtx_PLUS (word_mode, reg,
1629 GEN_INT (-m)),
1630 word_mode, speed_p);
1631 for (i = 0; i < count; i++)
1633 rtx r = immed_wide_int_const (test[i].mask, word_mode);
1634 cost_diff += set_src_cost (gen_rtx_AND (word_mode, reg, r),
1635 word_mode, speed_p);
1636 r = immed_wide_int_const (wi::lshift (test[i].mask, m), word_mode);
1637 cost_diff -= set_src_cost (gen_rtx_AND (word_mode, reg, r),
1638 word_mode, speed_p);
1640 if (cost_diff > 0)
1642 for (i = 0; i < count; i++)
1643 test[i].mask = wi::lshift (test[i].mask, m);
1644 minval = build_zero_cst (TREE_TYPE (minval));
1648 /* Now build the test-and-branch code. */
1650 gsi = gsi_last_bb (m_case_bb);
1652 /* idx = (unsigned)x - minval. */
1653 idx = fold_convert_loc (loc, unsigned_index_type, index_expr);
1654 idx = fold_build2_loc (loc, MINUS_EXPR, unsigned_index_type, idx,
1655 fold_convert_loc (loc, unsigned_index_type, minval));
1656 idx = force_gimple_operand_gsi (&gsi, idx,
1657 /*simple=*/true, NULL_TREE,
1658 /*before=*/true, GSI_SAME_STMT);
1660 profile_probability subtree_prob = m_subtree_prob;
1661 profile_probability default_prob = m_default_prob;
1662 if (!default_prob.initialized_p ())
1663 default_prob = m_subtree_prob.invert ();
1665 if (m_handles_entire_switch && entry_test_needed)
1667 tree range = int_const_binop (MINUS_EXPR, maxval, minval);
1668 /* if (idx > range) goto default */
1669 range
1670 = force_gimple_operand_gsi (&gsi,
1671 fold_convert (unsigned_index_type, range),
1672 /*simple=*/true, NULL_TREE,
1673 /*before=*/true, GSI_SAME_STMT);
1674 tmp = fold_build2 (GT_EXPR, boolean_type_node, idx, range);
1675 default_prob = default_prob / 2;
1676 basic_block new_bb
1677 = hoist_edge_and_branch_if_true (&gsi, tmp, default_bb,
1678 default_prob, loc);
1679 gsi = gsi_last_bb (new_bb);
1682 tmp = fold_build2_loc (loc, LSHIFT_EXPR, word_type_node, word_mode_one,
1683 fold_convert_loc (loc, word_type_node, idx));
1685 /* csui = (1 << (word_mode) idx) */
1686 if (count > 1)
1688 csui = make_ssa_name (word_type_node);
1689 tmp = force_gimple_operand_gsi (&gsi, tmp,
1690 /*simple=*/false, NULL_TREE,
1691 /*before=*/true, GSI_SAME_STMT);
1692 shift_stmt = gimple_build_assign (csui, tmp);
1693 gsi_insert_before (&gsi, shift_stmt, GSI_SAME_STMT);
1694 update_stmt (shift_stmt);
1696 else
1697 csui = tmp;
1699 /* for each unique set of cases:
1700 if (const & csui) goto target */
1701 for (k = 0; k < count; k++)
1703 profile_probability prob = test[k].prob / (subtree_prob + default_prob);
1704 subtree_prob -= test[k].prob;
1705 tmp = wide_int_to_tree (word_type_node, test[k].mask);
1706 tmp = fold_build2_loc (loc, BIT_AND_EXPR, word_type_node, csui, tmp);
1707 tmp = fold_build2_loc (loc, NE_EXPR, boolean_type_node,
1708 tmp, word_mode_zero);
1709 tmp = force_gimple_operand_gsi (&gsi, tmp,
1710 /*simple=*/true, NULL_TREE,
1711 /*before=*/true, GSI_SAME_STMT);
1712 basic_block new_bb
1713 = hoist_edge_and_branch_if_true (&gsi, tmp, test[k].target_bb,
1714 prob, loc);
1715 gsi = gsi_last_bb (new_bb);
1718 /* We should have removed all edges now. */
1719 gcc_assert (EDGE_COUNT (gsi_bb (gsi)->succs) == 0);
1721 /* If nothing matched, go to the default label. */
1722 edge e = make_edge (gsi_bb (gsi), default_bb, EDGE_FALLTHRU);
1723 e->probability = profile_probability::always ();
1726 /* Split the basic block at the statement pointed to by GSIP, and insert
1727 a branch to the target basic block of E_TRUE conditional on tree
1728 expression COND.
1730 It is assumed that there is already an edge from the to-be-split
1731 basic block to E_TRUE->dest block. This edge is removed, and the
1732 profile information on the edge is re-used for the new conditional
1733 jump.
1735 The CFG is updated. The dominator tree will not be valid after
1736 this transformation, but the immediate dominators are updated if
1737 UPDATE_DOMINATORS is true.
1739 Returns the newly created basic block. */
1741 basic_block
1742 bit_test_cluster::hoist_edge_and_branch_if_true (gimple_stmt_iterator *gsip,
1743 tree cond, basic_block case_bb,
1744 profile_probability prob,
1745 location_t loc)
1747 tree tmp;
1748 gcond *cond_stmt;
1749 edge e_false;
1750 basic_block new_bb, split_bb = gsi_bb (*gsip);
1752 edge e_true = make_edge (split_bb, case_bb, EDGE_TRUE_VALUE);
1753 e_true->probability = prob;
1754 gcc_assert (e_true->src == split_bb);
1756 tmp = force_gimple_operand_gsi (gsip, cond, /*simple=*/true, NULL,
1757 /*before=*/true, GSI_SAME_STMT);
1758 cond_stmt = gimple_build_cond_from_tree (tmp, NULL_TREE, NULL_TREE);
1759 gimple_set_location (cond_stmt, loc);
1760 gsi_insert_before (gsip, cond_stmt, GSI_SAME_STMT);
1762 e_false = split_block (split_bb, cond_stmt);
1763 new_bb = e_false->dest;
1764 redirect_edge_pred (e_true, split_bb);
1766 e_false->flags &= ~EDGE_FALLTHRU;
1767 e_false->flags |= EDGE_FALSE_VALUE;
1768 e_false->probability = e_true->probability.invert ();
1769 new_bb->count = e_false->count ();
1771 return new_bb;
1774 /* Compute the number of case labels that correspond to each outgoing edge of
1775 switch statement. Record this information in the aux field of the edge. */
1777 void
1778 switch_decision_tree::compute_cases_per_edge ()
1780 reset_out_edges_aux (m_switch);
1781 int ncases = gimple_switch_num_labels (m_switch);
1782 for (int i = ncases - 1; i >= 1; --i)
1784 edge case_edge = gimple_switch_edge (cfun, m_switch, i);
1785 case_edge->aux = (void *) ((intptr_t) (case_edge->aux) + 1);
1789 /* Analyze switch statement and return true when the statement is expanded
1790 as decision tree. */
1792 bool
1793 switch_decision_tree::analyze_switch_statement ()
1795 unsigned l = gimple_switch_num_labels (m_switch);
1796 basic_block bb = gimple_bb (m_switch);
1797 auto_vec<cluster *> clusters;
1798 clusters.create (l - 1);
1800 basic_block default_bb = gimple_switch_default_bb (cfun, m_switch);
1801 m_case_bbs.reserve (l);
1802 m_case_bbs.quick_push (default_bb);
1804 compute_cases_per_edge ();
1806 for (unsigned i = 1; i < l; i++)
1808 tree elt = gimple_switch_label (m_switch, i);
1809 tree lab = CASE_LABEL (elt);
1810 basic_block case_bb = label_to_block (cfun, lab);
1811 edge case_edge = find_edge (bb, case_bb);
1812 tree low = CASE_LOW (elt);
1813 tree high = CASE_HIGH (elt);
1815 profile_probability p
1816 = case_edge->probability / ((intptr_t) (case_edge->aux));
1817 clusters.quick_push (new simple_cluster (low, high, elt, case_edge->dest,
1818 p));
1819 m_case_bbs.quick_push (case_edge->dest);
1822 reset_out_edges_aux (m_switch);
1824 /* Find bit-test clusters. */
1825 vec<cluster *> output = bit_test_cluster::find_bit_tests (clusters);
1827 /* Find jump table clusters. */
1828 vec<cluster *> output2;
1829 auto_vec<cluster *> tmp;
1830 output2.create (1);
1831 tmp.create (1);
1833 for (unsigned i = 0; i < output.length (); i++)
1835 cluster *c = output[i];
1836 if (c->get_type () != SIMPLE_CASE)
1838 if (!tmp.is_empty ())
1840 vec<cluster *> n = jump_table_cluster::find_jump_tables (tmp);
1841 output2.safe_splice (n);
1842 n.release ();
1843 tmp.truncate (0);
1845 output2.safe_push (c);
1847 else
1848 tmp.safe_push (c);
1851 /* We still can have a temporary vector to test. */
1852 if (!tmp.is_empty ())
1854 vec<cluster *> n = jump_table_cluster::find_jump_tables (tmp);
1855 output2.safe_splice (n);
1856 n.release ();
1859 if (dump_file)
1861 fprintf (dump_file, ";; GIMPLE switch case clusters: ");
1862 for (unsigned i = 0; i < output2.length (); i++)
1863 output2[i]->dump (dump_file, dump_flags & TDF_DETAILS);
1864 fprintf (dump_file, "\n");
1867 output.release ();
1869 bool expanded = try_switch_expansion (output2);
1870 release_clusters (output2);
1871 return expanded;
1874 /* Attempt to expand CLUSTERS as a decision tree. Return true when
1875 expanded. */
1877 bool
1878 switch_decision_tree::try_switch_expansion (vec<cluster *> &clusters)
1880 tree index_expr = gimple_switch_index (m_switch);
1881 tree index_type = TREE_TYPE (index_expr);
1882 basic_block bb = gimple_bb (m_switch);
1884 if (gimple_switch_num_labels (m_switch) == 1
1885 || range_check_type (index_type) == NULL_TREE)
1886 return false;
1888 /* Find the default case target label. */
1889 edge default_edge = gimple_switch_default_edge (cfun, m_switch);
1890 m_default_bb = default_edge->dest;
1892 /* Do the insertion of a case label into m_case_list. The labels are
1893 fed to us in descending order from the sorted vector of case labels used
1894 in the tree part of the middle end. So the list we construct is
1895 sorted in ascending order. */
1897 for (int i = clusters.length () - 1; i >= 0; i--)
1899 case_tree_node *r = m_case_list;
1900 m_case_list = m_case_node_pool.allocate ();
1901 m_case_list->m_right = r;
1902 m_case_list->m_c = clusters[i];
1905 record_phi_operand_mapping ();
1907 /* Split basic block that contains the gswitch statement. */
1908 gimple_stmt_iterator gsi = gsi_last_bb (bb);
1909 edge e;
1910 if (gsi_end_p (gsi))
1911 e = split_block_after_labels (bb);
1912 else
1914 gsi_prev (&gsi);
1915 e = split_block (bb, gsi_stmt (gsi));
1917 bb = split_edge (e);
1919 /* Create new basic blocks for non-case clusters where specific expansion
1920 needs to happen. */
1921 for (unsigned i = 0; i < clusters.length (); i++)
1922 if (clusters[i]->get_type () != SIMPLE_CASE)
1924 clusters[i]->m_case_bb = create_empty_bb (bb);
1925 clusters[i]->m_case_bb->count = bb->count;
1926 clusters[i]->m_case_bb->loop_father = bb->loop_father;
1929 /* Do not do an extra work for a single cluster. */
1930 if (clusters.length () == 1
1931 && clusters[0]->get_type () != SIMPLE_CASE)
1933 cluster *c = clusters[0];
1934 c->emit (index_expr, index_type,
1935 gimple_switch_default_label (m_switch), m_default_bb,
1936 gimple_location (m_switch));
1937 redirect_edge_succ (single_succ_edge (bb), c->m_case_bb);
1939 else
1941 emit (bb, index_expr, default_edge->probability, index_type);
1943 /* Emit cluster-specific switch handling. */
1944 for (unsigned i = 0; i < clusters.length (); i++)
1945 if (clusters[i]->get_type () != SIMPLE_CASE)
1947 edge e = single_pred_edge (clusters[i]->m_case_bb);
1948 e->dest->count = e->src->count.apply_probability (e->probability);
1949 clusters[i]->emit (index_expr, index_type,
1950 gimple_switch_default_label (m_switch),
1951 m_default_bb, gimple_location (m_switch));
1955 fix_phi_operands_for_edges ();
1957 return true;
1960 /* Before switch transformation, record all SSA_NAMEs defined in switch BB
1961 and used in a label basic block. */
1963 void
1964 switch_decision_tree::record_phi_operand_mapping ()
1966 basic_block switch_bb = gimple_bb (m_switch);
1967 /* Record all PHI nodes that have to be fixed after conversion. */
1968 for (unsigned i = 0; i < m_case_bbs.length (); i++)
1970 gphi_iterator gsi;
1971 basic_block bb = m_case_bbs[i];
1972 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1974 gphi *phi = gsi.phi ();
1976 for (unsigned i = 0; i < gimple_phi_num_args (phi); i++)
1978 basic_block phi_src_bb = gimple_phi_arg_edge (phi, i)->src;
1979 if (phi_src_bb == switch_bb)
1981 tree def = gimple_phi_arg_def (phi, i);
1982 tree result = gimple_phi_result (phi);
1983 m_phi_mapping.put (result, def);
1984 break;
1991 /* Append new operands to PHI statements that were introduced due to
1992 addition of new edges to case labels. */
1994 void
1995 switch_decision_tree::fix_phi_operands_for_edges ()
1997 gphi_iterator gsi;
1999 for (unsigned i = 0; i < m_case_bbs.length (); i++)
2001 basic_block bb = m_case_bbs[i];
2002 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2004 gphi *phi = gsi.phi ();
2005 for (unsigned j = 0; j < gimple_phi_num_args (phi); j++)
2007 tree def = gimple_phi_arg_def (phi, j);
2008 if (def == NULL_TREE)
2010 edge e = gimple_phi_arg_edge (phi, j);
2011 tree *definition
2012 = m_phi_mapping.get (gimple_phi_result (phi));
2013 gcc_assert (definition);
2014 add_phi_arg (phi, *definition, e, UNKNOWN_LOCATION);
2021 /* Generate a decision tree, switching on INDEX_EXPR and jumping to
2022 one of the labels in CASE_LIST or to the DEFAULT_LABEL.
2024 We generate a binary decision tree to select the appropriate target
2025 code. */
2027 void
2028 switch_decision_tree::emit (basic_block bb, tree index_expr,
2029 profile_probability default_prob, tree index_type)
2031 balance_case_nodes (&m_case_list, NULL);
2033 if (dump_file)
2034 dump_function_to_file (current_function_decl, dump_file, dump_flags);
2035 if (dump_file && (dump_flags & TDF_DETAILS))
2037 int indent_step = ceil_log2 (TYPE_PRECISION (index_type)) + 2;
2038 fprintf (dump_file, ";; Expanding GIMPLE switch as decision tree:\n");
2039 gcc_assert (m_case_list != NULL);
2040 dump_case_nodes (dump_file, m_case_list, indent_step, 0);
2043 bb = emit_case_nodes (bb, index_expr, m_case_list, default_prob, index_type,
2044 gimple_location (m_switch));
2046 if (bb)
2047 emit_jump (bb, m_default_bb);
2049 /* Remove all edges and do just an edge that will reach default_bb. */
2050 bb = gimple_bb (m_switch);
2051 gimple_stmt_iterator gsi = gsi_last_bb (bb);
2052 gsi_remove (&gsi, true);
2054 delete_basic_block (bb);
2057 /* Take an ordered list of case nodes
2058 and transform them into a near optimal binary tree,
2059 on the assumption that any target code selection value is as
2060 likely as any other.
2062 The transformation is performed by splitting the ordered
2063 list into two equal sections plus a pivot. The parts are
2064 then attached to the pivot as left and right branches. Each
2065 branch is then transformed recursively. */
2067 void
2068 switch_decision_tree::balance_case_nodes (case_tree_node **head,
2069 case_tree_node *parent)
2071 case_tree_node *np;
2073 np = *head;
2074 if (np)
2076 int i = 0;
2077 case_tree_node **npp;
2078 case_tree_node *left;
2079 profile_probability prob = profile_probability::never ();
2081 /* Count the number of entries on branch. */
2083 while (np)
2085 i++;
2086 prob += np->m_c->m_prob;
2087 np = np->m_right;
2090 if (i > 2)
2092 /* Split this list if it is long enough for that to help. */
2093 npp = head;
2094 left = *npp;
2095 profile_probability pivot_prob = prob / 2;
2097 /* Find the place in the list that bisects the list's total cost
2098 by probability. */
2099 while (1)
2101 /* Skip nodes while their probability does not reach
2102 that amount. */
2103 prob -= (*npp)->m_c->m_prob;
2104 if ((prob.initialized_p () && prob < pivot_prob)
2105 || ! (*npp)->m_right)
2106 break;
2107 npp = &(*npp)->m_right;
2110 np = *npp;
2111 *npp = 0;
2112 *head = np;
2113 np->m_parent = parent;
2114 np->m_left = left == np ? NULL : left;
2116 /* Optimize each of the two split parts. */
2117 balance_case_nodes (&np->m_left, np);
2118 balance_case_nodes (&np->m_right, np);
2119 np->m_c->m_subtree_prob = np->m_c->m_prob;
2120 if (np->m_left)
2121 np->m_c->m_subtree_prob += np->m_left->m_c->m_subtree_prob;
2122 if (np->m_right)
2123 np->m_c->m_subtree_prob += np->m_right->m_c->m_subtree_prob;
2125 else
2127 /* Else leave this branch as one level,
2128 but fill in `parent' fields. */
2129 np = *head;
2130 np->m_parent = parent;
2131 np->m_c->m_subtree_prob = np->m_c->m_prob;
2132 for (; np->m_right; np = np->m_right)
2134 np->m_right->m_parent = np;
2135 (*head)->m_c->m_subtree_prob += np->m_right->m_c->m_subtree_prob;
2141 /* Dump ROOT, a list or tree of case nodes, to file. */
2143 void
2144 switch_decision_tree::dump_case_nodes (FILE *f, case_tree_node *root,
2145 int indent_step, int indent_level)
2147 if (root == 0)
2148 return;
2149 indent_level++;
2151 dump_case_nodes (f, root->m_left, indent_step, indent_level);
2153 fputs (";; ", f);
2154 fprintf (f, "%*s", indent_step * indent_level, "");
2155 root->m_c->dump (f);
2156 root->m_c->m_prob.dump (f);
2157 fputs (" subtree: ", f);
2158 root->m_c->m_subtree_prob.dump (f);
2159 fputs (")\n", f);
2161 dump_case_nodes (f, root->m_right, indent_step, indent_level);
2165 /* Add an unconditional jump to CASE_BB that happens in basic block BB. */
2167 void
2168 switch_decision_tree::emit_jump (basic_block bb, basic_block case_bb)
2170 edge e = single_succ_edge (bb);
2171 redirect_edge_succ (e, case_bb);
2174 /* Generate code to compare OP0 with OP1 so that the condition codes are
2175 set and to jump to LABEL_BB if the condition is true.
2176 COMPARISON is the GIMPLE comparison (EQ, NE, GT, etc.).
2177 PROB is the probability of jumping to LABEL_BB. */
2179 basic_block
2180 switch_decision_tree::emit_cmp_and_jump_insns (basic_block bb, tree op0,
2181 tree op1, tree_code comparison,
2182 basic_block label_bb,
2183 profile_probability prob,
2184 location_t loc)
2186 // TODO: it's once called with lhs != index.
2187 op1 = fold_convert (TREE_TYPE (op0), op1);
2189 gcond *cond = gimple_build_cond (comparison, op0, op1, NULL_TREE, NULL_TREE);
2190 gimple_set_location (cond, loc);
2191 gimple_stmt_iterator gsi = gsi_last_bb (bb);
2192 gsi_insert_after (&gsi, cond, GSI_NEW_STMT);
2194 gcc_assert (single_succ_p (bb));
2196 /* Make a new basic block where false branch will take place. */
2197 edge false_edge = split_block (bb, cond);
2198 false_edge->flags = EDGE_FALSE_VALUE;
2199 false_edge->probability = prob.invert ();
2200 false_edge->dest->count = bb->count.apply_probability (prob.invert ());
2202 edge true_edge = make_edge (bb, label_bb, EDGE_TRUE_VALUE);
2203 true_edge->probability = prob;
2205 return false_edge->dest;
2208 /* Generate code to jump to LABEL if OP0 and OP1 are equal.
2209 PROB is the probability of jumping to LABEL_BB.
2210 BB is a basic block where the new condition will be placed. */
2212 basic_block
2213 switch_decision_tree::do_jump_if_equal (basic_block bb, tree op0, tree op1,
2214 basic_block label_bb,
2215 profile_probability prob,
2216 location_t loc)
2218 op1 = fold_convert (TREE_TYPE (op0), op1);
2220 gcond *cond = gimple_build_cond (EQ_EXPR, op0, op1, NULL_TREE, NULL_TREE);
2221 gimple_set_location (cond, loc);
2222 gimple_stmt_iterator gsi = gsi_last_bb (bb);
2223 gsi_insert_before (&gsi, cond, GSI_SAME_STMT);
2225 gcc_assert (single_succ_p (bb));
2227 /* Make a new basic block where false branch will take place. */
2228 edge false_edge = split_block (bb, cond);
2229 false_edge->flags = EDGE_FALSE_VALUE;
2230 false_edge->probability = prob.invert ();
2231 false_edge->dest->count = bb->count.apply_probability (prob.invert ());
2233 edge true_edge = make_edge (bb, label_bb, EDGE_TRUE_VALUE);
2234 true_edge->probability = prob;
2236 return false_edge->dest;
2239 /* Emit step-by-step code to select a case for the value of INDEX.
2240 The thus generated decision tree follows the form of the
2241 case-node binary tree NODE, whose nodes represent test conditions.
2242 DEFAULT_PROB is probability of cases leading to default BB.
2243 INDEX_TYPE is the type of the index of the switch. */
2245 basic_block
2246 switch_decision_tree::emit_case_nodes (basic_block bb, tree index,
2247 case_tree_node *node,
2248 profile_probability default_prob,
2249 tree index_type, location_t loc)
2251 profile_probability p;
2253 /* If node is null, we are done. */
2254 if (node == NULL)
2255 return bb;
2257 /* Single value case. */
2258 if (node->m_c->is_single_value_p ())
2260 /* Node is single valued. First see if the index expression matches
2261 this node and then check our children, if any. */
2262 p = node->m_c->m_prob / (node->m_c->m_subtree_prob + default_prob);
2263 bb = do_jump_if_equal (bb, index, node->m_c->get_low (),
2264 node->m_c->m_case_bb, p, loc);
2265 /* Since this case is taken at this point, reduce its weight from
2266 subtree_weight. */
2267 node->m_c->m_subtree_prob -= node->m_c->m_prob;
2269 if (node->m_left != NULL && node->m_right != NULL)
2271 /* 1) the node has both children
2273 If both children are single-valued cases with no
2274 children, finish up all the work. This way, we can save
2275 one ordered comparison. */
2277 if (!node->m_left->has_child ()
2278 && node->m_left->m_c->is_single_value_p ()
2279 && !node->m_right->has_child ()
2280 && node->m_right->m_c->is_single_value_p ())
2282 p = (node->m_right->m_c->m_prob
2283 / (node->m_c->m_subtree_prob + default_prob));
2284 bb = do_jump_if_equal (bb, index, node->m_right->m_c->get_low (),
2285 node->m_right->m_c->m_case_bb, p, loc);
2286 node->m_c->m_subtree_prob -= node->m_right->m_c->m_prob;
2288 p = (node->m_left->m_c->m_prob
2289 / (node->m_c->m_subtree_prob + default_prob));
2290 bb = do_jump_if_equal (bb, index, node->m_left->m_c->get_low (),
2291 node->m_left->m_c->m_case_bb, p, loc);
2293 else
2295 /* Branch to a label where we will handle it later. */
2296 basic_block test_bb = split_edge (single_succ_edge (bb));
2297 redirect_edge_succ (single_pred_edge (test_bb),
2298 single_succ_edge (bb)->dest);
2300 p = ((node->m_right->m_c->m_subtree_prob + default_prob / 2)
2301 / (node->m_c->m_subtree_prob + default_prob));
2302 test_bb->count = bb->count.apply_probability (p);
2303 bb = emit_cmp_and_jump_insns (bb, index, node->m_c->get_high (),
2304 GT_EXPR, test_bb, p, loc);
2305 default_prob /= 2;
2307 /* Handle the left-hand subtree. */
2308 bb = emit_case_nodes (bb, index, node->m_left,
2309 default_prob, index_type, loc);
2311 /* If the left-hand subtree fell through,
2312 don't let it fall into the right-hand subtree. */
2313 if (bb && m_default_bb)
2314 emit_jump (bb, m_default_bb);
2316 bb = emit_case_nodes (test_bb, index, node->m_right,
2317 default_prob, index_type, loc);
2320 else if (node->m_left == NULL && node->m_right != NULL)
2322 /* 2) the node has only right child. */
2324 /* Here we have a right child but no left so we issue a conditional
2325 branch to default and process the right child.
2327 Omit the conditional branch to default if the right child
2328 does not have any children and is single valued; it would
2329 cost too much space to save so little time. */
2331 if (node->m_right->has_child ()
2332 || !node->m_right->m_c->is_single_value_p ())
2334 p = ((default_prob / 2)
2335 / (node->m_c->m_subtree_prob + default_prob));
2336 bb = emit_cmp_and_jump_insns (bb, index, node->m_c->get_low (),
2337 LT_EXPR, m_default_bb, p, loc);
2338 default_prob /= 2;
2340 bb = emit_case_nodes (bb, index, node->m_right, default_prob,
2341 index_type, loc);
2343 else
2345 /* We cannot process node->right normally
2346 since we haven't ruled out the numbers less than
2347 this node's value. So handle node->right explicitly. */
2348 p = (node->m_right->m_c->m_subtree_prob
2349 / (node->m_c->m_subtree_prob + default_prob));
2350 bb = do_jump_if_equal (bb, index, node->m_right->m_c->get_low (),
2351 node->m_right->m_c->m_case_bb, p, loc);
2354 else if (node->m_left != NULL && node->m_right == NULL)
2356 /* 3) just one subtree, on the left. Similar case as previous. */
2358 if (node->m_left->has_child ()
2359 || !node->m_left->m_c->is_single_value_p ())
2361 p = ((default_prob / 2)
2362 / (node->m_c->m_subtree_prob + default_prob));
2363 bb = emit_cmp_and_jump_insns (bb, index, node->m_c->get_high (),
2364 GT_EXPR, m_default_bb, p, loc);
2365 default_prob /= 2;
2367 bb = emit_case_nodes (bb, index, node->m_left, default_prob,
2368 index_type, loc);
2370 else
2372 /* We cannot process node->left normally
2373 since we haven't ruled out the numbers less than
2374 this node's value. So handle node->left explicitly. */
2375 p = (node->m_left->m_c->m_subtree_prob
2376 / (node->m_c->m_subtree_prob + default_prob));
2377 bb = do_jump_if_equal (bb, index, node->m_left->m_c->get_low (),
2378 node->m_left->m_c->m_case_bb, p, loc);
2382 else
2384 /* Node is a range. These cases are very similar to those for a single
2385 value, except that we do not start by testing whether this node
2386 is the one to branch to. */
2387 if (node->has_child () || node->m_c->get_type () != SIMPLE_CASE)
2389 bool is_bt = node->m_c->get_type () == BIT_TEST;
2390 int parts = is_bt ? 3 : 2;
2392 /* Branch to a label where we will handle it later. */
2393 basic_block test_bb = split_edge (single_succ_edge (bb));
2394 redirect_edge_succ (single_pred_edge (test_bb),
2395 single_succ_edge (bb)->dest);
2397 profile_probability right_prob = profile_probability::never ();
2398 if (node->m_right)
2399 right_prob = node->m_right->m_c->m_subtree_prob;
2400 p = ((right_prob + default_prob / parts)
2401 / (node->m_c->m_subtree_prob + default_prob));
2402 test_bb->count = bb->count.apply_probability (p);
2404 bb = emit_cmp_and_jump_insns (bb, index, node->m_c->get_high (),
2405 GT_EXPR, test_bb, p, loc);
2407 default_prob /= parts;
2408 node->m_c->m_subtree_prob -= right_prob;
2409 if (is_bt)
2410 node->m_c->m_default_prob = default_prob;
2412 /* Value belongs to this node or to the left-hand subtree. */
2413 p = node->m_c->m_prob / (node->m_c->m_subtree_prob + default_prob);
2414 bb = emit_cmp_and_jump_insns (bb, index, node->m_c->get_low (),
2415 GE_EXPR, node->m_c->m_case_bb, p, loc);
2417 /* Handle the left-hand subtree. */
2418 bb = emit_case_nodes (bb, index, node->m_left, default_prob,
2419 index_type, loc);
2421 /* If the left-hand subtree fell through,
2422 don't let it fall into the right-hand subtree. */
2423 if (bb && m_default_bb)
2424 emit_jump (bb, m_default_bb);
2426 bb = emit_case_nodes (test_bb, index, node->m_right, default_prob,
2427 index_type, loc);
2429 else
2431 /* Node has no children so we check low and high bounds to remove
2432 redundant tests. Only one of the bounds can exist,
2433 since otherwise this node is bounded--a case tested already. */
2434 tree lhs, rhs;
2435 generate_range_test (bb, index, node->m_c->get_low (),
2436 node->m_c->get_high (), &lhs, &rhs);
2437 p = default_prob / (node->m_c->m_subtree_prob + default_prob);
2439 bb = emit_cmp_and_jump_insns (bb, lhs, rhs, GT_EXPR,
2440 m_default_bb, p, loc);
2442 emit_jump (bb, node->m_c->m_case_bb);
2443 return NULL;
2447 return bb;
2450 /* The main function of the pass scans statements for switches and invokes
2451 process_switch on them. */
2453 namespace {
2455 const pass_data pass_data_convert_switch =
2457 GIMPLE_PASS, /* type */
2458 "switchconv", /* name */
2459 OPTGROUP_NONE, /* optinfo_flags */
2460 TV_TREE_SWITCH_CONVERSION, /* tv_id */
2461 ( PROP_cfg | PROP_ssa ), /* properties_required */
2462 0, /* properties_provided */
2463 0, /* properties_destroyed */
2464 0, /* todo_flags_start */
2465 TODO_update_ssa, /* todo_flags_finish */
2468 class pass_convert_switch : public gimple_opt_pass
2470 public:
2471 pass_convert_switch (gcc::context *ctxt)
2472 : gimple_opt_pass (pass_data_convert_switch, ctxt)
2475 /* opt_pass methods: */
2476 bool gate (function *) final override
2478 return flag_tree_switch_conversion != 0;
2480 unsigned int execute (function *) final override;
2482 }; // class pass_convert_switch
2484 unsigned int
2485 pass_convert_switch::execute (function *fun)
2487 basic_block bb;
2488 bool cfg_altered = false;
2490 FOR_EACH_BB_FN (bb, fun)
2492 gimple *stmt = last_stmt (bb);
2493 if (stmt && gimple_code (stmt) == GIMPLE_SWITCH)
2495 if (dump_file)
2497 expanded_location loc = expand_location (gimple_location (stmt));
2499 fprintf (dump_file, "beginning to process the following "
2500 "SWITCH statement (%s:%d) : ------- \n",
2501 loc.file, loc.line);
2502 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2503 putc ('\n', dump_file);
2506 switch_conversion sconv;
2507 sconv.expand (as_a <gswitch *> (stmt));
2508 cfg_altered |= sconv.m_cfg_altered;
2509 if (!sconv.m_reason)
2511 if (dump_file)
2513 fputs ("Switch converted\n", dump_file);
2514 fputs ("--------------------------------\n", dump_file);
2517 /* Make no effort to update the post-dominator tree.
2518 It is actually not that hard for the transformations
2519 we have performed, but it is not supported
2520 by iterate_fix_dominators. */
2521 free_dominance_info (CDI_POST_DOMINATORS);
2523 else
2525 if (dump_file)
2527 fputs ("Bailing out - ", dump_file);
2528 fputs (sconv.m_reason, dump_file);
2529 fputs ("\n--------------------------------\n", dump_file);
2535 return cfg_altered ? TODO_cleanup_cfg : 0;;
2538 } // anon namespace
2540 gimple_opt_pass *
2541 make_pass_convert_switch (gcc::context *ctxt)
2543 return new pass_convert_switch (ctxt);
2546 /* The main function of the pass scans statements for switches and invokes
2547 process_switch on them. */
2549 namespace {
2551 template <bool O0> class pass_lower_switch: public gimple_opt_pass
2553 public:
2554 pass_lower_switch (gcc::context *ctxt) : gimple_opt_pass (data, ctxt) {}
2556 static const pass_data data;
2557 opt_pass *
2558 clone () final override
2560 return new pass_lower_switch<O0> (m_ctxt);
2563 bool
2564 gate (function *) final override
2566 return !O0 || !optimize;
2569 unsigned int execute (function *fun) final override;
2570 }; // class pass_lower_switch
2572 template <bool O0>
2573 const pass_data pass_lower_switch<O0>::data = {
2574 GIMPLE_PASS, /* type */
2575 O0 ? "switchlower_O0" : "switchlower", /* name */
2576 OPTGROUP_NONE, /* optinfo_flags */
2577 TV_TREE_SWITCH_LOWERING, /* tv_id */
2578 ( PROP_cfg | PROP_ssa ), /* properties_required */
2579 0, /* properties_provided */
2580 0, /* properties_destroyed */
2581 0, /* todo_flags_start */
2582 TODO_update_ssa | TODO_cleanup_cfg, /* todo_flags_finish */
2585 template <bool O0>
2586 unsigned int
2587 pass_lower_switch<O0>::execute (function *fun)
2589 basic_block bb;
2590 bool expanded = false;
2592 auto_vec<gimple *> switch_statements;
2593 switch_statements.create (1);
2595 FOR_EACH_BB_FN (bb, fun)
2597 gimple *stmt = last_stmt (bb);
2598 gswitch *swtch;
2599 if (stmt && (swtch = dyn_cast<gswitch *> (stmt)))
2601 if (!O0)
2602 group_case_labels_stmt (swtch);
2603 switch_statements.safe_push (swtch);
2607 for (unsigned i = 0; i < switch_statements.length (); i++)
2609 gimple *stmt = switch_statements[i];
2610 if (dump_file)
2612 expanded_location loc = expand_location (gimple_location (stmt));
2614 fprintf (dump_file, "beginning to process the following "
2615 "SWITCH statement (%s:%d) : ------- \n",
2616 loc.file, loc.line);
2617 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2618 putc ('\n', dump_file);
2621 gswitch *swtch = dyn_cast<gswitch *> (stmt);
2622 if (swtch)
2624 switch_decision_tree dt (swtch);
2625 expanded |= dt.analyze_switch_statement ();
2629 if (expanded)
2631 free_dominance_info (CDI_DOMINATORS);
2632 free_dominance_info (CDI_POST_DOMINATORS);
2633 mark_virtual_operands_for_renaming (cfun);
2636 return 0;
2639 } // anon namespace
2641 gimple_opt_pass *
2642 make_pass_lower_switch_O0 (gcc::context *ctxt)
2644 return new pass_lower_switch<true> (ctxt);
2646 gimple_opt_pass *
2647 make_pass_lower_switch (gcc::context *ctxt)
2649 return new pass_lower_switch<false> (ctxt);