PR c++/86342 - -Wdeprecated-copy and system headers.
[official-gcc.git] / gcc / tree-switch-conversion.c
blobb79f2fdb6e659eb527d0415ce6ec58ca063dc7c1
1 /* Lower GIMPLE_SWITCH expressions to something more efficient than
2 a jump table.
3 Copyright (C) 2006-2018 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 "params.h"
40 #include "fold-const.h"
41 #include "varasm.h"
42 #include "stor-layout.h"
43 #include "cfganal.h"
44 #include "gimplify.h"
45 #include "gimple-iterator.h"
46 #include "gimplify-me.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"
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? */
56 #include "langhooks.h"
58 #include "tree-switch-conversion.h"
60 using namespace tree_switch_conversion;
62 /* Constructor. */
64 switch_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)
71 /* Collection information about SWTCH statement. */
73 void
74 switch_conversion::collect (gswitch *swtch)
76 unsigned int branch_num = gimple_switch_num_labels (swtch);
77 tree min_case, max_case;
78 unsigned int i;
79 edge e, e_default, e_first;
80 edge_iterator ei;
81 basic_block first;
83 m_switch = swtch;
85 /* The gimplifier has already sorted the cases by CASE_LOW and ensured there
86 is a default label which is the first in the vector.
87 Collect the bits we can deduce from the CFG. */
88 m_index_expr = gimple_switch_index (swtch);
89 m_switch_bb = gimple_bb (swtch);
90 m_default_bb
91 = label_to_block (CASE_LABEL (gimple_switch_default_label (swtch)));
92 e_default = find_edge (m_switch_bb, m_default_bb);
93 m_default_prob = e_default->probability;
94 m_default_count = e_default->count ();
95 FOR_EACH_EDGE (e, ei, m_switch_bb->succs)
96 if (e != e_default)
97 m_other_count += e->count ();
99 /* Get upper and lower bounds of case values, and the covered range. */
100 min_case = gimple_switch_label (swtch, 1);
101 max_case = gimple_switch_label (swtch, branch_num - 1);
103 m_range_min = CASE_LOW (min_case);
104 if (CASE_HIGH (max_case) != NULL_TREE)
105 m_range_max = CASE_HIGH (max_case);
106 else
107 m_range_max = CASE_LOW (max_case);
109 m_contiguous_range = true;
110 tree last = CASE_HIGH (min_case) ? CASE_HIGH (min_case) : m_range_min;
111 for (i = 2; i < branch_num; i++)
113 tree elt = gimple_switch_label (swtch, i);
114 if (wi::to_wide (last) + 1 != wi::to_wide (CASE_LOW (elt)))
116 m_contiguous_range = false;
117 break;
119 last = CASE_HIGH (elt) ? CASE_HIGH (elt) : CASE_LOW (elt);
122 if (m_contiguous_range)
124 first = label_to_block (CASE_LABEL (gimple_switch_label (swtch, 1)));
125 e_first = find_edge (m_switch_bb, first);
127 else
129 first = m_default_bb;
130 e_first = e_default;
133 /* See if there is one common successor block for all branch
134 targets. If it exists, record it in FINAL_BB.
135 Start with the destination of the first non-default case
136 if the range is contiguous and default case otherwise as
137 guess or its destination in case it is a forwarder block. */
138 if (! single_pred_p (e_first->dest))
139 m_final_bb = e_first->dest;
140 else if (single_succ_p (e_first->dest)
141 && ! single_pred_p (single_succ (e_first->dest)))
142 m_final_bb = single_succ (e_first->dest);
143 /* Require that all switch destinations are either that common
144 FINAL_BB or a forwarder to it, except for the default
145 case if contiguous range. */
146 if (m_final_bb)
147 FOR_EACH_EDGE (e, ei, m_switch_bb->succs)
149 if (e->dest == m_final_bb)
150 continue;
152 if (single_pred_p (e->dest)
153 && single_succ_p (e->dest)
154 && single_succ (e->dest) == m_final_bb)
155 continue;
157 if (e == e_default && m_contiguous_range)
159 m_default_case_nonstandard = true;
160 continue;
163 m_final_bb = NULL;
164 break;
167 m_range_size
168 = int_const_binop (MINUS_EXPR, m_range_max, m_range_min);
170 /* Get a count of the number of case labels. Single-valued case labels
171 simply count as one, but a case range counts double, since it may
172 require two compares if it gets lowered as a branching tree. */
173 m_count = 0;
174 for (i = 1; i < branch_num; i++)
176 tree elt = gimple_switch_label (swtch, i);
177 m_count++;
178 if (CASE_HIGH (elt)
179 && ! tree_int_cst_equal (CASE_LOW (elt), CASE_HIGH (elt)))
180 m_count++;
183 /* Get the number of unique non-default targets out of the GIMPLE_SWITCH
184 block. Assume a CFG cleanup would have already removed degenerate
185 switch statements, this allows us to just use EDGE_COUNT. */
186 m_uniq = EDGE_COUNT (gimple_bb (swtch)->succs) - 1;
189 /* Checks whether the range given by individual case statements of the switch
190 switch statement isn't too big and whether the number of branches actually
191 satisfies the size of the new array. */
193 bool
194 switch_conversion::check_range ()
196 gcc_assert (m_range_size);
197 if (!tree_fits_uhwi_p (m_range_size))
199 m_reason = "index range way too large or otherwise unusable";
200 return false;
203 if (tree_to_uhwi (m_range_size)
204 > ((unsigned) m_count * SWITCH_CONVERSION_BRANCH_RATIO))
206 m_reason = "the maximum range-branch ratio exceeded";
207 return false;
210 return true;
213 /* Checks whether all but the final BB basic blocks are empty. */
215 bool
216 switch_conversion::check_all_empty_except_final ()
218 edge e, e_default = find_edge (m_switch_bb, m_default_bb);
219 edge_iterator ei;
221 FOR_EACH_EDGE (e, ei, m_switch_bb->succs)
223 if (e->dest == m_final_bb)
224 continue;
226 if (!empty_block_p (e->dest))
228 if (m_contiguous_range && e == e_default)
230 m_default_case_nonstandard = true;
231 continue;
234 m_reason = "bad case - a non-final BB not empty";
235 return false;
239 return true;
242 /* This function checks whether all required values in phi nodes in final_bb
243 are constants. Required values are those that correspond to a basic block
244 which is a part of the examined switch statement. It returns true if the
245 phi nodes are OK, otherwise false. */
247 bool
248 switch_conversion::check_final_bb ()
250 gphi_iterator gsi;
252 m_phi_count = 0;
253 for (gsi = gsi_start_phis (m_final_bb); !gsi_end_p (gsi); gsi_next (&gsi))
255 gphi *phi = gsi.phi ();
256 unsigned int i;
258 if (virtual_operand_p (gimple_phi_result (phi)))
259 continue;
261 m_phi_count++;
263 for (i = 0; i < gimple_phi_num_args (phi); i++)
265 basic_block bb = gimple_phi_arg_edge (phi, i)->src;
267 if (bb == m_switch_bb
268 || (single_pred_p (bb)
269 && single_pred (bb) == m_switch_bb
270 && (!m_default_case_nonstandard
271 || empty_block_p (bb))))
273 tree reloc, val;
274 const char *reason = NULL;
276 val = gimple_phi_arg_def (phi, i);
277 if (!is_gimple_ip_invariant (val))
278 reason = "non-invariant value from a case";
279 else
281 reloc = initializer_constant_valid_p (val, TREE_TYPE (val));
282 if ((flag_pic && reloc != null_pointer_node)
283 || (!flag_pic && reloc == NULL_TREE))
285 if (reloc)
286 reason
287 = "value from a case would need runtime relocations";
288 else
289 reason
290 = "value from a case is not a valid initializer";
293 if (reason)
295 /* For contiguous range, we can allow non-constant
296 or one that needs relocation, as long as it is
297 only reachable from the default case. */
298 if (bb == m_switch_bb)
299 bb = m_final_bb;
300 if (!m_contiguous_range || bb != m_default_bb)
302 m_reason = reason;
303 return false;
306 unsigned int branch_num = gimple_switch_num_labels (m_switch);
307 for (unsigned int i = 1; i < branch_num; i++)
309 tree lab = CASE_LABEL (gimple_switch_label (m_switch, i));
310 if (label_to_block (lab) == bb)
312 m_reason = reason;
313 return false;
316 m_default_case_nonstandard = true;
322 return true;
325 /* The following function allocates default_values, target_{in,out}_names and
326 constructors arrays. The last one is also populated with pointers to
327 vectors that will become constructors of new arrays. */
329 void
330 switch_conversion::create_temp_arrays ()
332 int i;
334 m_default_values = XCNEWVEC (tree, m_phi_count * 3);
335 /* ??? Macros do not support multi argument templates in their
336 argument list. We create a typedef to work around that problem. */
337 typedef vec<constructor_elt, va_gc> *vec_constructor_elt_gc;
338 m_constructors = XCNEWVEC (vec_constructor_elt_gc, m_phi_count);
339 m_target_inbound_names = m_default_values + m_phi_count;
340 m_target_outbound_names = m_target_inbound_names + m_phi_count;
341 for (i = 0; i < m_phi_count; i++)
342 vec_alloc (m_constructors[i], tree_to_uhwi (m_range_size) + 1);
345 /* Populate the array of default values in the order of phi nodes.
346 DEFAULT_CASE is the CASE_LABEL_EXPR for the default switch branch
347 if the range is non-contiguous or the default case has standard
348 structure, otherwise it is the first non-default case instead. */
350 void
351 switch_conversion::gather_default_values (tree default_case)
353 gphi_iterator gsi;
354 basic_block bb = label_to_block (CASE_LABEL (default_case));
355 edge e;
356 int i = 0;
358 gcc_assert (CASE_LOW (default_case) == NULL_TREE
359 || m_default_case_nonstandard);
361 if (bb == m_final_bb)
362 e = find_edge (m_switch_bb, bb);
363 else
364 e = single_succ_edge (bb);
366 for (gsi = gsi_start_phis (m_final_bb); !gsi_end_p (gsi); gsi_next (&gsi))
368 gphi *phi = gsi.phi ();
369 if (virtual_operand_p (gimple_phi_result (phi)))
370 continue;
371 tree val = PHI_ARG_DEF_FROM_EDGE (phi, e);
372 gcc_assert (val);
373 m_default_values[i++] = val;
377 /* The following function populates the vectors in the constructors array with
378 future contents of the static arrays. The vectors are populated in the
379 order of phi nodes. */
381 void
382 switch_conversion::build_constructors ()
384 unsigned i, branch_num = gimple_switch_num_labels (m_switch);
385 tree pos = m_range_min;
386 tree pos_one = build_int_cst (TREE_TYPE (pos), 1);
388 for (i = 1; i < branch_num; i++)
390 tree cs = gimple_switch_label (m_switch, i);
391 basic_block bb = label_to_block (CASE_LABEL (cs));
392 edge e;
393 tree high;
394 gphi_iterator gsi;
395 int j;
397 if (bb == m_final_bb)
398 e = find_edge (m_switch_bb, bb);
399 else
400 e = single_succ_edge (bb);
401 gcc_assert (e);
403 while (tree_int_cst_lt (pos, CASE_LOW (cs)))
405 int k;
406 for (k = 0; k < m_phi_count; k++)
408 constructor_elt elt;
410 elt.index = int_const_binop (MINUS_EXPR, pos, m_range_min);
411 elt.value
412 = unshare_expr_without_location (m_default_values[k]);
413 m_constructors[k]->quick_push (elt);
416 pos = int_const_binop (PLUS_EXPR, pos, pos_one);
418 gcc_assert (tree_int_cst_equal (pos, CASE_LOW (cs)));
420 j = 0;
421 if (CASE_HIGH (cs))
422 high = CASE_HIGH (cs);
423 else
424 high = CASE_LOW (cs);
425 for (gsi = gsi_start_phis (m_final_bb);
426 !gsi_end_p (gsi); gsi_next (&gsi))
428 gphi *phi = gsi.phi ();
429 if (virtual_operand_p (gimple_phi_result (phi)))
430 continue;
431 tree val = PHI_ARG_DEF_FROM_EDGE (phi, e);
432 tree low = CASE_LOW (cs);
433 pos = CASE_LOW (cs);
437 constructor_elt elt;
439 elt.index = int_const_binop (MINUS_EXPR, pos, m_range_min);
440 elt.value = unshare_expr_without_location (val);
441 m_constructors[j]->quick_push (elt);
443 pos = int_const_binop (PLUS_EXPR, pos, pos_one);
444 } while (!tree_int_cst_lt (high, pos)
445 && tree_int_cst_lt (low, pos));
446 j++;
451 /* If all values in the constructor vector are the same, return the value.
452 Otherwise return NULL_TREE. Not supposed to be called for empty
453 vectors. */
455 tree
456 switch_conversion::contains_same_values_p (vec<constructor_elt, va_gc> *vec)
458 unsigned int i;
459 tree prev = NULL_TREE;
460 constructor_elt *elt;
462 FOR_EACH_VEC_SAFE_ELT (vec, i, elt)
464 if (!prev)
465 prev = elt->value;
466 else if (!operand_equal_p (elt->value, prev, OEP_ONLY_CONST))
467 return NULL_TREE;
469 return prev;
472 /* Return type which should be used for array elements, either TYPE's
473 main variant or, for integral types, some smaller integral type
474 that can still hold all the constants. */
476 tree
477 switch_conversion::array_value_type (tree type, int num)
479 unsigned int i, len = vec_safe_length (m_constructors[num]);
480 constructor_elt *elt;
481 int sign = 0;
482 tree smaller_type;
484 /* Types with alignments greater than their size can reach here, e.g. out of
485 SRA. We couldn't use these as an array component type so get back to the
486 main variant first, which, for our purposes, is fine for other types as
487 well. */
489 type = TYPE_MAIN_VARIANT (type);
491 if (!INTEGRAL_TYPE_P (type))
492 return type;
494 scalar_int_mode type_mode = SCALAR_INT_TYPE_MODE (type);
495 scalar_int_mode mode = get_narrowest_mode (type_mode);
496 if (GET_MODE_SIZE (type_mode) <= GET_MODE_SIZE (mode))
497 return type;
499 if (len < (optimize_bb_for_size_p (gimple_bb (m_switch)) ? 2 : 32))
500 return type;
502 FOR_EACH_VEC_SAFE_ELT (m_constructors[num], i, elt)
504 wide_int cst;
506 if (TREE_CODE (elt->value) != INTEGER_CST)
507 return type;
509 cst = wi::to_wide (elt->value);
510 while (1)
512 unsigned int prec = GET_MODE_BITSIZE (mode);
513 if (prec > HOST_BITS_PER_WIDE_INT)
514 return type;
516 if (sign >= 0 && cst == wi::zext (cst, prec))
518 if (sign == 0 && cst == wi::sext (cst, prec))
519 break;
520 sign = 1;
521 break;
523 if (sign <= 0 && cst == wi::sext (cst, prec))
525 sign = -1;
526 break;
529 if (sign == 1)
530 sign = 0;
532 if (!GET_MODE_WIDER_MODE (mode).exists (&mode)
533 || GET_MODE_SIZE (mode) >= GET_MODE_SIZE (type_mode))
534 return type;
538 if (sign == 0)
539 sign = TYPE_UNSIGNED (type) ? 1 : -1;
540 smaller_type = lang_hooks.types.type_for_mode (mode, sign >= 0);
541 if (GET_MODE_SIZE (type_mode)
542 <= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (smaller_type)))
543 return type;
545 return smaller_type;
548 /* Create an appropriate array type and declaration and assemble a static
549 array variable. Also create a load statement that initializes
550 the variable in question with a value from the static array. SWTCH is
551 the switch statement being converted, NUM is the index to
552 arrays of constructors, default values and target SSA names
553 for this particular array. ARR_INDEX_TYPE is the type of the index
554 of the new array, PHI is the phi node of the final BB that corresponds
555 to the value that will be loaded from the created array. TIDX
556 is an ssa name of a temporary variable holding the index for loads from the
557 new array. */
559 void
560 switch_conversion::build_one_array (int num, tree arr_index_type,
561 gphi *phi, tree tidx)
563 tree name, cst;
564 gimple *load;
565 gimple_stmt_iterator gsi = gsi_for_stmt (m_switch);
566 location_t loc = gimple_location (m_switch);
568 gcc_assert (m_default_values[num]);
570 name = copy_ssa_name (PHI_RESULT (phi));
571 m_target_inbound_names[num] = name;
573 cst = contains_same_values_p (m_constructors[num]);
574 if (cst)
575 load = gimple_build_assign (name, cst);
576 else
578 tree array_type, ctor, decl, value_type, fetch, default_type;
580 default_type = TREE_TYPE (m_default_values[num]);
581 value_type = array_value_type (default_type, num);
582 array_type = build_array_type (value_type, arr_index_type);
583 if (default_type != value_type)
585 unsigned int i;
586 constructor_elt *elt;
588 FOR_EACH_VEC_SAFE_ELT (m_constructors[num], i, elt)
589 elt->value = fold_convert (value_type, elt->value);
591 ctor = build_constructor (array_type, m_constructors[num]);
592 TREE_CONSTANT (ctor) = true;
593 TREE_STATIC (ctor) = true;
595 decl = build_decl (loc, VAR_DECL, NULL_TREE, array_type);
596 TREE_STATIC (decl) = 1;
597 DECL_INITIAL (decl) = ctor;
599 DECL_NAME (decl) = create_tmp_var_name ("CSWTCH");
600 DECL_ARTIFICIAL (decl) = 1;
601 DECL_IGNORED_P (decl) = 1;
602 TREE_CONSTANT (decl) = 1;
603 TREE_READONLY (decl) = 1;
604 DECL_IGNORED_P (decl) = 1;
605 if (offloading_function_p (cfun->decl))
606 DECL_ATTRIBUTES (decl)
607 = tree_cons (get_identifier ("omp declare target"), NULL_TREE,
608 NULL_TREE);
609 varpool_node::finalize_decl (decl);
611 fetch = build4 (ARRAY_REF, value_type, decl, tidx, NULL_TREE,
612 NULL_TREE);
613 if (default_type != value_type)
615 fetch = fold_convert (default_type, fetch);
616 fetch = force_gimple_operand_gsi (&gsi, fetch, true, NULL_TREE,
617 true, GSI_SAME_STMT);
619 load = gimple_build_assign (name, fetch);
622 gsi_insert_before (&gsi, load, GSI_SAME_STMT);
623 update_stmt (load);
624 m_arr_ref_last = load;
627 /* Builds and initializes static arrays initialized with values gathered from
628 the switch statement. Also creates statements that load values from
629 them. */
631 void
632 switch_conversion::build_arrays ()
634 tree arr_index_type;
635 tree tidx, sub, utype;
636 gimple *stmt;
637 gimple_stmt_iterator gsi;
638 gphi_iterator gpi;
639 int i;
640 location_t loc = gimple_location (m_switch);
642 gsi = gsi_for_stmt (m_switch);
644 /* Make sure we do not generate arithmetics in a subrange. */
645 utype = TREE_TYPE (m_index_expr);
646 if (TREE_TYPE (utype))
647 utype = lang_hooks.types.type_for_mode (TYPE_MODE (TREE_TYPE (utype)), 1);
648 else
649 utype = lang_hooks.types.type_for_mode (TYPE_MODE (utype), 1);
651 arr_index_type = build_index_type (m_range_size);
652 tidx = make_ssa_name (utype);
653 sub = fold_build2_loc (loc, MINUS_EXPR, utype,
654 fold_convert_loc (loc, utype, m_index_expr),
655 fold_convert_loc (loc, utype, m_range_min));
656 sub = force_gimple_operand_gsi (&gsi, sub,
657 false, NULL, true, GSI_SAME_STMT);
658 stmt = gimple_build_assign (tidx, sub);
660 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
661 update_stmt (stmt);
662 m_arr_ref_first = stmt;
664 for (gpi = gsi_start_phis (m_final_bb), i = 0;
665 !gsi_end_p (gpi); gsi_next (&gpi))
667 gphi *phi = gpi.phi ();
668 if (!virtual_operand_p (gimple_phi_result (phi)))
669 build_one_array (i++, arr_index_type, phi, tidx);
670 else
672 edge e;
673 edge_iterator ei;
674 FOR_EACH_EDGE (e, ei, m_switch_bb->succs)
676 if (e->dest == m_final_bb)
677 break;
678 if (!m_default_case_nonstandard
679 || e->dest != m_default_bb)
681 e = single_succ_edge (e->dest);
682 break;
685 gcc_assert (e && e->dest == m_final_bb);
686 m_target_vop = PHI_ARG_DEF_FROM_EDGE (phi, e);
691 /* Generates and appropriately inserts loads of default values at the position
692 given by GSI. Returns the last inserted statement. */
694 gassign *
695 switch_conversion::gen_def_assigns (gimple_stmt_iterator *gsi)
697 int i;
698 gassign *assign = NULL;
700 for (i = 0; i < m_phi_count; i++)
702 tree name = copy_ssa_name (m_target_inbound_names[i]);
703 m_target_outbound_names[i] = name;
704 assign = gimple_build_assign (name, m_default_values[i]);
705 gsi_insert_before (gsi, assign, GSI_SAME_STMT);
706 update_stmt (assign);
708 return assign;
711 /* Deletes the unused bbs and edges that now contain the switch statement and
712 its empty branch bbs. BBD is the now dead BB containing
713 the original switch statement, FINAL is the last BB of the converted
714 switch statement (in terms of succession). */
716 void
717 switch_conversion::prune_bbs (basic_block bbd, basic_block final,
718 basic_block default_bb)
720 edge_iterator ei;
721 edge e;
723 for (ei = ei_start (bbd->succs); (e = ei_safe_edge (ei)); )
725 basic_block bb;
726 bb = e->dest;
727 remove_edge (e);
728 if (bb != final && bb != default_bb)
729 delete_basic_block (bb);
731 delete_basic_block (bbd);
734 /* Add values to phi nodes in final_bb for the two new edges. E1F is the edge
735 from the basic block loading values from an array and E2F from the basic
736 block loading default values. BBF is the last switch basic block (see the
737 bbf description in the comment below). */
739 void
740 switch_conversion::fix_phi_nodes (edge e1f, edge e2f, basic_block bbf)
742 gphi_iterator gsi;
743 int i;
745 for (gsi = gsi_start_phis (bbf), i = 0;
746 !gsi_end_p (gsi); gsi_next (&gsi))
748 gphi *phi = gsi.phi ();
749 tree inbound, outbound;
750 if (virtual_operand_p (gimple_phi_result (phi)))
751 inbound = outbound = m_target_vop;
752 else
754 inbound = m_target_inbound_names[i];
755 outbound = m_target_outbound_names[i++];
757 add_phi_arg (phi, inbound, e1f, UNKNOWN_LOCATION);
758 if (!m_default_case_nonstandard)
759 add_phi_arg (phi, outbound, e2f, UNKNOWN_LOCATION);
763 /* Creates a check whether the switch expression value actually falls into the
764 range given by all the cases. If it does not, the temporaries are loaded
765 with default values instead. */
767 void
768 switch_conversion::gen_inbound_check ()
770 tree label_decl1 = create_artificial_label (UNKNOWN_LOCATION);
771 tree label_decl2 = create_artificial_label (UNKNOWN_LOCATION);
772 tree label_decl3 = create_artificial_label (UNKNOWN_LOCATION);
773 glabel *label1, *label2, *label3;
774 tree utype, tidx;
775 tree bound;
777 gcond *cond_stmt;
779 gassign *last_assign = NULL;
780 gimple_stmt_iterator gsi;
781 basic_block bb0, bb1, bb2, bbf, bbd;
782 edge e01 = NULL, e02, e21, e1d, e1f, e2f;
783 location_t loc = gimple_location (m_switch);
785 gcc_assert (m_default_values);
787 bb0 = gimple_bb (m_switch);
789 tidx = gimple_assign_lhs (m_arr_ref_first);
790 utype = TREE_TYPE (tidx);
792 /* (end of) block 0 */
793 gsi = gsi_for_stmt (m_arr_ref_first);
794 gsi_next (&gsi);
796 bound = fold_convert_loc (loc, utype, m_range_size);
797 cond_stmt = gimple_build_cond (LE_EXPR, tidx, bound, NULL_TREE, NULL_TREE);
798 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
799 update_stmt (cond_stmt);
801 /* block 2 */
802 if (!m_default_case_nonstandard)
804 label2 = gimple_build_label (label_decl2);
805 gsi_insert_before (&gsi, label2, GSI_SAME_STMT);
806 last_assign = gen_def_assigns (&gsi);
809 /* block 1 */
810 label1 = gimple_build_label (label_decl1);
811 gsi_insert_before (&gsi, label1, GSI_SAME_STMT);
813 /* block F */
814 gsi = gsi_start_bb (m_final_bb);
815 label3 = gimple_build_label (label_decl3);
816 gsi_insert_before (&gsi, label3, GSI_SAME_STMT);
818 /* cfg fix */
819 e02 = split_block (bb0, cond_stmt);
820 bb2 = e02->dest;
822 if (m_default_case_nonstandard)
824 bb1 = bb2;
825 bb2 = m_default_bb;
826 e01 = e02;
827 e01->flags = EDGE_TRUE_VALUE;
828 e02 = make_edge (bb0, bb2, EDGE_FALSE_VALUE);
829 edge e_default = find_edge (bb1, bb2);
830 for (gphi_iterator gsi = gsi_start_phis (bb2);
831 !gsi_end_p (gsi); gsi_next (&gsi))
833 gphi *phi = gsi.phi ();
834 tree arg = PHI_ARG_DEF_FROM_EDGE (phi, e_default);
835 add_phi_arg (phi, arg, e02,
836 gimple_phi_arg_location_from_edge (phi, e_default));
838 /* Partially fix the dominator tree, if it is available. */
839 if (dom_info_available_p (CDI_DOMINATORS))
840 redirect_immediate_dominators (CDI_DOMINATORS, bb1, bb0);
842 else
844 e21 = split_block (bb2, last_assign);
845 bb1 = e21->dest;
846 remove_edge (e21);
849 e1d = split_block (bb1, m_arr_ref_last);
850 bbd = e1d->dest;
851 remove_edge (e1d);
853 /* Flags and profiles of the edge for in-range values. */
854 if (!m_default_case_nonstandard)
855 e01 = make_edge (bb0, bb1, EDGE_TRUE_VALUE);
856 e01->probability = m_default_prob.invert ();
858 /* Flags and profiles of the edge taking care of out-of-range values. */
859 e02->flags &= ~EDGE_FALLTHRU;
860 e02->flags |= EDGE_FALSE_VALUE;
861 e02->probability = m_default_prob;
863 bbf = m_final_bb;
865 e1f = make_edge (bb1, bbf, EDGE_FALLTHRU);
866 e1f->probability = profile_probability::always ();
868 if (m_default_case_nonstandard)
869 e2f = NULL;
870 else
872 e2f = make_edge (bb2, bbf, EDGE_FALLTHRU);
873 e2f->probability = profile_probability::always ();
876 /* frequencies of the new BBs */
877 bb1->count = e01->count ();
878 bb2->count = e02->count ();
879 if (!m_default_case_nonstandard)
880 bbf->count = e1f->count () + e2f->count ();
882 /* Tidy blocks that have become unreachable. */
883 prune_bbs (bbd, m_final_bb,
884 m_default_case_nonstandard ? m_default_bb : NULL);
886 /* Fixup the PHI nodes in bbF. */
887 fix_phi_nodes (e1f, e2f, bbf);
889 /* Fix the dominator tree, if it is available. */
890 if (dom_info_available_p (CDI_DOMINATORS))
892 vec<basic_block> bbs_to_fix_dom;
894 set_immediate_dominator (CDI_DOMINATORS, bb1, bb0);
895 if (!m_default_case_nonstandard)
896 set_immediate_dominator (CDI_DOMINATORS, bb2, bb0);
897 if (! get_immediate_dominator (CDI_DOMINATORS, bbf))
898 /* If bbD was the immediate dominator ... */
899 set_immediate_dominator (CDI_DOMINATORS, bbf, bb0);
901 bbs_to_fix_dom.create (3 + (bb2 != bbf));
902 bbs_to_fix_dom.quick_push (bb0);
903 bbs_to_fix_dom.quick_push (bb1);
904 if (bb2 != bbf)
905 bbs_to_fix_dom.quick_push (bb2);
906 bbs_to_fix_dom.quick_push (bbf);
908 iterate_fix_dominators (CDI_DOMINATORS, bbs_to_fix_dom, true);
909 bbs_to_fix_dom.release ();
913 /* The following function is invoked on every switch statement (the current
914 one is given in SWTCH) and runs the individual phases of switch
915 conversion on it one after another until one fails or the conversion
916 is completed. On success, NULL is in m_reason, otherwise points
917 to a string with the reason why the conversion failed. */
919 void
920 switch_conversion::expand (gswitch *swtch)
922 /* Group case labels so that we get the right results from the heuristics
923 that decide on the code generation approach for this switch. */
924 m_cfg_altered |= group_case_labels_stmt (swtch);
926 /* If this switch is now a degenerate case with only a default label,
927 there is nothing left for us to do. */
928 if (gimple_switch_num_labels (swtch) < 2)
930 m_reason = "switch is a degenerate case";
931 return;
934 collect (swtch);
936 /* No error markers should reach here (they should be filtered out
937 during gimplification). */
938 gcc_checking_assert (TREE_TYPE (m_index_expr) != error_mark_node);
940 /* A switch on a constant should have been optimized in tree-cfg-cleanup. */
941 gcc_checking_assert (!TREE_CONSTANT (m_index_expr));
943 /* Prefer bit test if possible. */
944 if (tree_fits_uhwi_p (m_range_size)
945 && bit_test_cluster::can_be_handled (tree_to_uhwi (m_range_size), m_uniq)
946 && bit_test_cluster::is_beneficial (m_count, m_uniq))
948 m_reason = "expanding as bit test is preferable";
949 return;
952 if (m_uniq <= 2)
954 /* This will be expanded as a decision tree . */
955 m_reason = "expanding as jumps is preferable";
956 return;
959 /* If there is no common successor, we cannot do the transformation. */
960 if (!m_final_bb)
962 m_reason = "no common successor to all case label target blocks found";
963 return;
966 /* Check the case label values are within reasonable range: */
967 if (!check_range ())
969 gcc_assert (m_reason);
970 return;
973 /* For all the cases, see whether they are empty, the assignments they
974 represent constant and so on... */
975 if (!check_all_empty_except_final ())
977 gcc_assert (m_reason);
978 return;
980 if (!check_final_bb ())
982 gcc_assert (m_reason);
983 return;
986 /* At this point all checks have passed and we can proceed with the
987 transformation. */
989 create_temp_arrays ();
990 gather_default_values (m_default_case_nonstandard
991 ? gimple_switch_label (swtch, 1)
992 : gimple_switch_default_label (swtch));
993 build_constructors ();
995 build_arrays (); /* Build the static arrays and assignments. */
996 gen_inbound_check (); /* Build the bounds check. */
998 m_cfg_altered = true;
1001 /* Destructor. */
1003 switch_conversion::~switch_conversion ()
1005 XDELETEVEC (m_constructors);
1006 XDELETEVEC (m_default_values);
1009 /* Constructor. */
1011 group_cluster::group_cluster (vec<cluster *> &clusters,
1012 unsigned start, unsigned end)
1014 gcc_checking_assert (end - start + 1 >= 1);
1015 m_prob = profile_probability::never ();
1016 m_cases.create (end - start + 1);
1017 for (unsigned i = start; i <= end; i++)
1019 m_cases.quick_push (static_cast<simple_cluster *> (clusters[i]));
1020 m_prob += clusters[i]->m_prob;
1022 m_subtree_prob = m_prob;
1025 /* Destructor. */
1027 group_cluster::~group_cluster ()
1029 for (unsigned i = 0; i < m_cases.length (); i++)
1030 delete m_cases[i];
1032 m_cases.release ();
1035 /* Dump content of a cluster. */
1037 void
1038 group_cluster::dump (FILE *f, bool details)
1040 unsigned total_values = 0;
1041 for (unsigned i = 0; i < m_cases.length (); i++)
1042 total_values += m_cases[i]->get_range (m_cases[i]->get_low (),
1043 m_cases[i]->get_high ());
1045 unsigned comparison_count = 0;
1046 for (unsigned i = 0; i < m_cases.length (); i++)
1048 simple_cluster *sc = static_cast<simple_cluster *> (m_cases[i]);
1049 comparison_count += sc->m_range_p ? 2 : 1;
1052 unsigned HOST_WIDE_INT range = get_range (get_low (), get_high ());
1053 fprintf (f, "%s", get_type () == JUMP_TABLE ? "JT" : "BT");
1055 if (details)
1056 fprintf (f, "(values:%d comparisons:%d range:" HOST_WIDE_INT_PRINT_DEC
1057 " density: %.2f%%)", total_values, comparison_count, range,
1058 100.0f * comparison_count / range);
1060 fprintf (f, ":");
1061 PRINT_CASE (f, get_low ());
1062 fprintf (f, "-");
1063 PRINT_CASE (f, get_high ());
1064 fprintf (f, " ");
1067 /* Emit GIMPLE code to handle the cluster. */
1069 void
1070 jump_table_cluster::emit (tree index_expr, tree,
1071 tree default_label_expr, basic_block default_bb)
1073 /* For jump table we just emit a new gswitch statement that will
1074 be latter lowered to jump table. */
1075 auto_vec <tree> labels;
1076 labels.create (m_cases.length ());
1078 make_edge (m_case_bb, default_bb, 0);
1079 for (unsigned i = 0; i < m_cases.length (); i++)
1081 labels.quick_push (unshare_expr (m_cases[i]->m_case_label_expr));
1082 make_edge (m_case_bb, m_cases[i]->m_case_bb, 0);
1085 gswitch *s = gimple_build_switch (index_expr,
1086 unshare_expr (default_label_expr), labels);
1087 gimple_stmt_iterator gsi = gsi_start_bb (m_case_bb);
1088 gsi_insert_after (&gsi, s, GSI_NEW_STMT);
1091 /* Find jump tables of given CLUSTERS, where all members of the vector
1092 are of type simple_cluster. New clusters are returned. */
1094 vec<cluster *>
1095 jump_table_cluster::find_jump_tables (vec<cluster *> &clusters)
1097 if (!is_enabled ())
1098 return clusters.copy ();
1100 unsigned l = clusters.length ();
1101 auto_vec<min_cluster_item> min;
1102 min.reserve (l + 1);
1104 min.quick_push (min_cluster_item (0, 0, 0));
1106 for (unsigned i = 1; i <= l; i++)
1108 /* Set minimal # of clusters with i-th item to infinite. */
1109 min.quick_push (min_cluster_item (INT_MAX, INT_MAX, INT_MAX));
1111 for (unsigned j = 0; j < i; j++)
1113 unsigned HOST_WIDE_INT s = min[j].m_non_jt_cases;
1114 if (i - j < case_values_threshold ())
1115 s += i - j;
1117 /* Prefer clusters with smaller number of numbers covered. */
1118 if ((min[j].m_count + 1 < min[i].m_count
1119 || (min[j].m_count + 1 == min[i].m_count
1120 && s < min[i].m_non_jt_cases))
1121 && can_be_handled (clusters, j, i - 1))
1122 min[i] = min_cluster_item (min[j].m_count + 1, j, s);
1125 gcc_checking_assert (min[i].m_count != INT_MAX);
1128 /* No result. */
1129 if (min[l].m_count == INT_MAX)
1130 return clusters.copy ();
1132 vec<cluster *> output;
1133 output.create (4);
1135 /* Find and build the clusters. */
1136 for (int end = l;;)
1138 int start = min[end].m_start;
1140 /* Do not allow clusters with small number of cases. */
1141 if (is_beneficial (clusters, start, end - 1))
1142 output.safe_push (new jump_table_cluster (clusters, start, end - 1));
1143 else
1144 for (int i = end - 1; i >= start; i--)
1145 output.safe_push (clusters[i]);
1147 end = start;
1149 if (start <= 0)
1150 break;
1153 output.reverse ();
1154 return output;
1157 /* Return true when cluster starting at START and ending at END (inclusive)
1158 can build a jump-table. */
1160 bool
1161 jump_table_cluster::can_be_handled (const vec<cluster *> &clusters,
1162 unsigned start, unsigned end)
1164 /* If the switch is relatively small such that the cost of one
1165 indirect jump on the target are higher than the cost of a
1166 decision tree, go with the decision tree.
1168 If range of values is much bigger than number of values,
1169 or if it is too large to represent in a HOST_WIDE_INT,
1170 make a sequence of conditional branches instead of a dispatch.
1172 The definition of "much bigger" depends on whether we are
1173 optimizing for size or for speed. */
1174 if (!flag_jump_tables)
1175 return false;
1177 /* For algorithm correctness, jump table for a single case must return
1178 true. We bail out in is_beneficial if it's called just for
1179 a single case. */
1180 if (start == end)
1181 return true;
1183 unsigned HOST_WIDE_INT max_ratio
1184 = optimize_insn_for_size_p () ? max_ratio_for_size : max_ratio_for_speed;
1185 unsigned HOST_WIDE_INT range = get_range (clusters[start]->get_low (),
1186 clusters[end]->get_high ());
1187 /* Check overflow. */
1188 if (range == 0)
1189 return false;
1191 unsigned HOST_WIDE_INT comparison_count = 0;
1192 for (unsigned i = start; i <= end; i++)
1194 simple_cluster *sc = static_cast<simple_cluster *> (clusters[i]);
1195 comparison_count += sc->m_range_p ? 2 : 1;
1198 return range <= max_ratio * comparison_count;
1201 /* Return true if cluster starting at START and ending at END (inclusive)
1202 is profitable transformation. */
1204 bool
1205 jump_table_cluster::is_beneficial (const vec<cluster *> &,
1206 unsigned start, unsigned end)
1208 /* Single case bail out. */
1209 if (start == end)
1210 return false;
1212 return end - start + 1 >= case_values_threshold ();
1215 /* Find bit tests of given CLUSTERS, where all members of the vector
1216 are of type simple_cluster. New clusters are returned. */
1218 vec<cluster *>
1219 bit_test_cluster::find_bit_tests (vec<cluster *> &clusters)
1221 vec<cluster *> output;
1222 output.create (4);
1224 unsigned l = clusters.length ();
1225 auto_vec<min_cluster_item> min;
1226 min.reserve (l + 1);
1228 min.quick_push (min_cluster_item (0, 0, 0));
1230 for (unsigned i = 1; i <= l; i++)
1232 /* Set minimal # of clusters with i-th item to infinite. */
1233 min.quick_push (min_cluster_item (INT_MAX, INT_MAX, INT_MAX));
1235 for (unsigned j = 0; j < i; j++)
1237 if (min[j].m_count + 1 < min[i].m_count
1238 && can_be_handled (clusters, j, i - 1))
1239 min[i] = min_cluster_item (min[j].m_count + 1, j, INT_MAX);
1242 gcc_checking_assert (min[i].m_count != INT_MAX);
1245 /* No result. */
1246 if (min[l].m_count == INT_MAX)
1247 return clusters.copy ();
1249 /* Find and build the clusters. */
1250 for (int end = l;;)
1252 int start = min[end].m_start;
1254 if (is_beneficial (clusters, start, end - 1))
1255 output.safe_push (new bit_test_cluster (clusters, start, end - 1));
1256 else
1257 for (int i = end - 1; i >= start; i--)
1258 output.safe_push (clusters[i]);
1260 end = start;
1262 if (start <= 0)
1263 break;
1266 output.reverse ();
1267 return output;
1270 /* Return true when RANGE of case values with UNIQ labels
1271 can build a bit test. */
1273 bool
1274 bit_test_cluster::can_be_handled (unsigned HOST_WIDE_INT range,
1275 unsigned int uniq)
1277 /* Check overflow. */
1278 if (range == 0)
1279 return 0;
1281 if (range >= GET_MODE_BITSIZE (word_mode))
1282 return false;
1284 return uniq <= 3;
1287 /* Return true when cluster starting at START and ending at END (inclusive)
1288 can build a bit test. */
1290 bool
1291 bit_test_cluster::can_be_handled (const vec<cluster *> &clusters,
1292 unsigned start, unsigned end)
1294 /* For algorithm correctness, bit test for a single case must return
1295 true. We bail out in is_beneficial if it's called just for
1296 a single case. */
1297 if (start == end)
1298 return true;
1300 unsigned HOST_WIDE_INT range = get_range (clusters[start]->get_low (),
1301 clusters[end]->get_high ());
1302 auto_bitmap dest_bbs;
1304 for (unsigned i = start; i <= end; i++)
1306 simple_cluster *sc = static_cast<simple_cluster *> (clusters[i]);
1307 bitmap_set_bit (dest_bbs, sc->m_case_bb->index);
1310 return can_be_handled (range, bitmap_count_bits (dest_bbs));
1313 /* Return true when COUNT of cases of UNIQ labels is beneficial for bit test
1314 transformation. */
1316 bool
1317 bit_test_cluster::is_beneficial (unsigned count, unsigned uniq)
1319 return (((uniq == 1 && count >= 3)
1320 || (uniq == 2 && count >= 5)
1321 || (uniq == 3 && count >= 6)));
1324 /* Return true if cluster starting at START and ending at END (inclusive)
1325 is profitable transformation. */
1327 bool
1328 bit_test_cluster::is_beneficial (const vec<cluster *> &clusters,
1329 unsigned start, unsigned end)
1331 /* Single case bail out. */
1332 if (start == end)
1333 return false;
1335 auto_bitmap dest_bbs;
1337 for (unsigned i = start; i <= end; i++)
1339 simple_cluster *sc = static_cast<simple_cluster *> (clusters[i]);
1340 bitmap_set_bit (dest_bbs, sc->m_case_bb->index);
1343 unsigned uniq = bitmap_count_bits (dest_bbs);
1344 unsigned count = end - start + 1;
1345 return is_beneficial (count, uniq);
1348 /* Comparison function for qsort to order bit tests by decreasing
1349 probability of execution. */
1352 case_bit_test::cmp (const void *p1, const void *p2)
1354 const struct case_bit_test *const d1 = (const struct case_bit_test *) p1;
1355 const struct case_bit_test *const d2 = (const struct case_bit_test *) p2;
1357 if (d2->bits != d1->bits)
1358 return d2->bits - d1->bits;
1360 /* Stabilize the sort. */
1361 return (LABEL_DECL_UID (CASE_LABEL (d2->label))
1362 - LABEL_DECL_UID (CASE_LABEL (d1->label)));
1365 /* Expand a switch statement by a short sequence of bit-wise
1366 comparisons. "switch(x)" is effectively converted into
1367 "if ((1 << (x-MINVAL)) & CST)" where CST and MINVAL are
1368 integer constants.
1370 INDEX_EXPR is the value being switched on.
1372 MINVAL is the lowest case value of in the case nodes,
1373 and RANGE is highest value minus MINVAL. MINVAL and RANGE
1374 are not guaranteed to be of the same type as INDEX_EXPR
1375 (the gimplifier doesn't change the type of case label values,
1376 and MINVAL and RANGE are derived from those values).
1377 MAXVAL is MINVAL + RANGE.
1379 There *MUST* be max_case_bit_tests or less unique case
1380 node targets. */
1382 void
1383 bit_test_cluster::emit (tree index_expr, tree index_type,
1384 tree, basic_block default_bb)
1386 struct case_bit_test test[m_max_case_bit_tests] = { {} };
1387 unsigned int i, j, k;
1388 unsigned int count;
1390 tree unsigned_index_type = unsigned_type_for (index_type);
1392 gimple_stmt_iterator gsi;
1393 gassign *shift_stmt;
1395 tree idx, tmp, csui;
1396 tree word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
1397 tree word_mode_zero = fold_convert (word_type_node, integer_zero_node);
1398 tree word_mode_one = fold_convert (word_type_node, integer_one_node);
1399 int prec = TYPE_PRECISION (word_type_node);
1400 wide_int wone = wi::one (prec);
1402 tree minval = get_low ();
1403 tree maxval = get_high ();
1404 tree range = int_const_binop (MINUS_EXPR, maxval, minval);
1406 /* Go through all case labels, and collect the case labels, profile
1407 counts, and other information we need to build the branch tests. */
1408 count = 0;
1409 for (i = 0; i < m_cases.length (); i++)
1411 unsigned int lo, hi;
1412 simple_cluster *n = static_cast<simple_cluster *> (m_cases[i]);
1413 for (k = 0; k < count; k++)
1414 if (n->m_case_bb == test[k].target_bb)
1415 break;
1417 if (k == count)
1419 gcc_checking_assert (count < m_max_case_bit_tests);
1420 test[k].mask = wi::zero (prec);
1421 test[k].target_bb = n->m_case_bb;
1422 test[k].label = n->m_case_label_expr;
1423 test[k].bits = 1;
1424 count++;
1426 else
1427 test[k].bits++;
1429 lo = tree_to_uhwi (int_const_binop (MINUS_EXPR, n->get_low (), minval));
1430 if (n->get_high () == NULL_TREE)
1431 hi = lo;
1432 else
1433 hi = tree_to_uhwi (int_const_binop (MINUS_EXPR, n->get_high (),
1434 minval));
1436 for (j = lo; j <= hi; j++)
1437 test[k].mask |= wi::lshift (wone, j);
1440 qsort (test, count, sizeof (*test), case_bit_test::cmp);
1442 /* If all values are in the 0 .. BITS_PER_WORD-1 range, we can get rid of
1443 the minval subtractions, but it might make the mask constants more
1444 expensive. So, compare the costs. */
1445 if (compare_tree_int (minval, 0) > 0
1446 && compare_tree_int (maxval, GET_MODE_BITSIZE (word_mode)) < 0)
1448 int cost_diff;
1449 HOST_WIDE_INT m = tree_to_uhwi (minval);
1450 rtx reg = gen_raw_REG (word_mode, 10000);
1451 bool speed_p = optimize_insn_for_speed_p ();
1452 cost_diff = set_rtx_cost (gen_rtx_PLUS (word_mode, reg,
1453 GEN_INT (-m)), speed_p);
1454 for (i = 0; i < count; i++)
1456 rtx r = immed_wide_int_const (test[i].mask, word_mode);
1457 cost_diff += set_src_cost (gen_rtx_AND (word_mode, reg, r),
1458 word_mode, speed_p);
1459 r = immed_wide_int_const (wi::lshift (test[i].mask, m), word_mode);
1460 cost_diff -= set_src_cost (gen_rtx_AND (word_mode, reg, r),
1461 word_mode, speed_p);
1463 if (cost_diff > 0)
1465 for (i = 0; i < count; i++)
1466 test[i].mask = wi::lshift (test[i].mask, m);
1467 minval = build_zero_cst (TREE_TYPE (minval));
1468 range = maxval;
1472 /* Now build the test-and-branch code. */
1474 gsi = gsi_last_bb (m_case_bb);
1476 /* idx = (unsigned)x - minval. */
1477 idx = fold_convert (unsigned_index_type, index_expr);
1478 idx = fold_build2 (MINUS_EXPR, unsigned_index_type, idx,
1479 fold_convert (unsigned_index_type, minval));
1480 idx = force_gimple_operand_gsi (&gsi, idx,
1481 /*simple=*/true, NULL_TREE,
1482 /*before=*/true, GSI_SAME_STMT);
1484 /* if (idx > range) goto default */
1485 range = force_gimple_operand_gsi (&gsi,
1486 fold_convert (unsigned_index_type, range),
1487 /*simple=*/true, NULL_TREE,
1488 /*before=*/true, GSI_SAME_STMT);
1489 tmp = fold_build2 (GT_EXPR, boolean_type_node, idx, range);
1490 basic_block new_bb = hoist_edge_and_branch_if_true (&gsi, tmp, default_bb);
1491 gsi = gsi_last_bb (new_bb);
1493 /* csui = (1 << (word_mode) idx) */
1494 csui = make_ssa_name (word_type_node);
1495 tmp = fold_build2 (LSHIFT_EXPR, word_type_node, word_mode_one,
1496 fold_convert (word_type_node, idx));
1497 tmp = force_gimple_operand_gsi (&gsi, tmp,
1498 /*simple=*/false, NULL_TREE,
1499 /*before=*/true, GSI_SAME_STMT);
1500 shift_stmt = gimple_build_assign (csui, tmp);
1501 gsi_insert_before (&gsi, shift_stmt, GSI_SAME_STMT);
1502 update_stmt (shift_stmt);
1504 /* for each unique set of cases:
1505 if (const & csui) goto target */
1506 for (k = 0; k < count; k++)
1508 tmp = wide_int_to_tree (word_type_node, test[k].mask);
1509 tmp = fold_build2 (BIT_AND_EXPR, word_type_node, csui, tmp);
1510 tmp = force_gimple_operand_gsi (&gsi, tmp,
1511 /*simple=*/true, NULL_TREE,
1512 /*before=*/true, GSI_SAME_STMT);
1513 tmp = fold_build2 (NE_EXPR, boolean_type_node, tmp, word_mode_zero);
1514 new_bb = hoist_edge_and_branch_if_true (&gsi, tmp, test[k].target_bb);
1515 gsi = gsi_last_bb (new_bb);
1518 /* We should have removed all edges now. */
1519 gcc_assert (EDGE_COUNT (gsi_bb (gsi)->succs) == 0);
1521 /* If nothing matched, go to the default label. */
1522 make_edge (gsi_bb (gsi), default_bb, EDGE_FALLTHRU);
1525 /* Split the basic block at the statement pointed to by GSIP, and insert
1526 a branch to the target basic block of E_TRUE conditional on tree
1527 expression COND.
1529 It is assumed that there is already an edge from the to-be-split
1530 basic block to E_TRUE->dest block. This edge is removed, and the
1531 profile information on the edge is re-used for the new conditional
1532 jump.
1534 The CFG is updated. The dominator tree will not be valid after
1535 this transformation, but the immediate dominators are updated if
1536 UPDATE_DOMINATORS is true.
1538 Returns the newly created basic block. */
1540 basic_block
1541 bit_test_cluster::hoist_edge_and_branch_if_true (gimple_stmt_iterator *gsip,
1542 tree cond, basic_block case_bb)
1544 tree tmp;
1545 gcond *cond_stmt;
1546 edge e_false;
1547 basic_block new_bb, split_bb = gsi_bb (*gsip);
1549 edge e_true = make_edge (split_bb, case_bb, EDGE_TRUE_VALUE);
1550 gcc_assert (e_true->src == split_bb);
1552 tmp = force_gimple_operand_gsi (gsip, cond, /*simple=*/true, NULL,
1553 /*before=*/true, GSI_SAME_STMT);
1554 cond_stmt = gimple_build_cond_from_tree (tmp, NULL_TREE, NULL_TREE);
1555 gsi_insert_before (gsip, cond_stmt, GSI_SAME_STMT);
1557 e_false = split_block (split_bb, cond_stmt);
1558 new_bb = e_false->dest;
1559 redirect_edge_pred (e_true, split_bb);
1561 e_false->flags &= ~EDGE_FALLTHRU;
1562 e_false->flags |= EDGE_FALSE_VALUE;
1563 e_false->probability = e_true->probability.invert ();
1564 new_bb->count = e_false->count ();
1566 return new_bb;
1569 /* Compute the number of case labels that correspond to each outgoing edge of
1570 switch statement. Record this information in the aux field of the edge. */
1572 void
1573 switch_decision_tree::compute_cases_per_edge ()
1575 basic_block bb = gimple_bb (m_switch);
1576 reset_out_edges_aux ();
1577 int ncases = gimple_switch_num_labels (m_switch);
1578 for (int i = ncases - 1; i >= 1; --i)
1580 tree elt = gimple_switch_label (m_switch, i);
1581 tree lab = CASE_LABEL (elt);
1582 basic_block case_bb = label_to_block_fn (cfun, lab);
1583 edge case_edge = find_edge (bb, case_bb);
1584 case_edge->aux = (void *) ((intptr_t) (case_edge->aux) + 1);
1588 /* Analyze switch statement and return true when the statement is expanded
1589 as decision tree. */
1591 bool
1592 switch_decision_tree::analyze_switch_statement ()
1594 unsigned l = gimple_switch_num_labels (m_switch);
1595 basic_block bb = gimple_bb (m_switch);
1596 auto_vec<cluster *> clusters;
1597 clusters.create (l - 1);
1599 tree default_label = CASE_LABEL (gimple_switch_default_label (m_switch));
1600 basic_block default_bb = label_to_block_fn (cfun, default_label);
1601 m_case_bbs.reserve (l);
1602 m_case_bbs.quick_push (default_bb);
1604 compute_cases_per_edge ();
1606 for (unsigned i = 1; i < l; i++)
1608 tree elt = gimple_switch_label (m_switch, i);
1609 tree lab = CASE_LABEL (elt);
1610 basic_block case_bb = label_to_block_fn (cfun, lab);
1611 edge case_edge = find_edge (bb, case_bb);
1612 tree low = CASE_LOW (elt);
1613 tree high = CASE_HIGH (elt);
1615 profile_probability p
1616 = case_edge->probability.apply_scale (1, (intptr_t) (case_edge->aux));
1617 clusters.quick_push (new simple_cluster (low, high, elt, case_bb, p));
1618 m_case_bbs.quick_push (case_bb);
1621 reset_out_edges_aux ();
1623 /* Find jump table clusters. */
1624 vec<cluster *> output = jump_table_cluster::find_jump_tables (clusters);
1626 /* Find bit test clusters. */
1627 vec<cluster *> output2;
1628 auto_vec<cluster *> tmp;
1629 output2.create (1);
1630 tmp.create (1);
1632 for (unsigned i = 0; i < output.length (); i++)
1634 cluster *c = output[i];
1635 if (c->get_type () != SIMPLE_CASE)
1637 if (!tmp.is_empty ())
1639 vec<cluster *> n = bit_test_cluster::find_bit_tests (tmp);
1640 output2.safe_splice (n);
1641 n.release ();
1642 tmp.truncate (0);
1644 output2.safe_push (c);
1646 else
1647 tmp.safe_push (c);
1650 /* We still can have a temporary vector to test. */
1651 if (!tmp.is_empty ())
1653 vec<cluster *> n = bit_test_cluster::find_bit_tests (tmp);
1654 output2.safe_splice (n);
1655 n.release ();
1658 if (dump_file)
1660 fprintf (dump_file, ";; GIMPLE switch case clusters: ");
1661 for (unsigned i = 0; i < output2.length (); i++)
1662 output2[i]->dump (dump_file, dump_flags & TDF_DETAILS);
1663 fprintf (dump_file, "\n");
1666 output.release ();
1668 bool expanded = try_switch_expansion (output2);
1670 for (unsigned i = 0; i < output2.length (); i++)
1671 delete output2[i];
1673 output2.release ();
1675 return expanded;
1678 /* Attempt to expand CLUSTERS as a decision tree. Return true when
1679 expanded. */
1681 bool
1682 switch_decision_tree::try_switch_expansion (vec<cluster *> &clusters)
1684 tree index_expr = gimple_switch_index (m_switch);
1685 tree index_type = TREE_TYPE (index_expr);
1686 basic_block bb = gimple_bb (m_switch);
1688 if (gimple_switch_num_labels (m_switch) == 1)
1689 return false;
1691 /* Find the default case target label. */
1692 tree default_label_expr = CASE_LABEL (gimple_switch_default_label (m_switch));
1693 m_default_bb = label_to_block_fn (cfun, default_label_expr);
1694 edge default_edge = find_edge (bb, m_default_bb);
1696 /* Do the insertion of a case label into m_case_list. The labels are
1697 fed to us in descending order from the sorted vector of case labels used
1698 in the tree part of the middle end. So the list we construct is
1699 sorted in ascending order. */
1701 for (int i = clusters.length () - 1; i >= 0; i--)
1703 case_tree_node *r = m_case_list;
1704 m_case_list = m_case_node_pool.allocate ();
1705 m_case_list->m_right = r;
1706 m_case_list->m_c = clusters[i];
1709 record_phi_operand_mapping ();
1711 /* Split basic block that contains the gswitch statement. */
1712 gimple_stmt_iterator gsi = gsi_last_bb (bb);
1713 edge e;
1714 if (gsi_end_p (gsi))
1715 e = split_block_after_labels (bb);
1716 else
1718 gsi_prev (&gsi);
1719 e = split_block (bb, gsi_stmt (gsi));
1721 bb = split_edge (e);
1723 /* Create new basic blocks for non-case clusters where specific expansion
1724 needs to happen. */
1725 for (unsigned i = 0; i < clusters.length (); i++)
1726 if (clusters[i]->get_type () != SIMPLE_CASE)
1728 clusters[i]->m_case_bb = create_empty_bb (bb);
1729 clusters[i]->m_case_bb->loop_father = bb->loop_father;
1732 /* Do not do an extra work for a single cluster. */
1733 if (clusters.length () == 1
1734 && clusters[0]->get_type () != SIMPLE_CASE)
1735 clusters[0]->emit (index_expr, index_type,
1736 gimple_switch_default_label (m_switch), m_default_bb);
1737 else
1739 emit (bb, index_expr, default_edge->probability, index_type);
1741 /* Emit cluster-specific switch handling. */
1742 for (unsigned i = 0; i < clusters.length (); i++)
1743 if (clusters[i]->get_type () != SIMPLE_CASE)
1744 clusters[i]->emit (index_expr, index_type,
1745 gimple_switch_default_label (m_switch),
1746 m_default_bb);
1749 fix_phi_operands_for_edges ();
1751 return true;
1754 /* Before switch transformation, record all SSA_NAMEs defined in switch BB
1755 and used in a label basic block. */
1757 void
1758 switch_decision_tree::record_phi_operand_mapping ()
1760 basic_block switch_bb = gimple_bb (m_switch);
1761 /* Record all PHI nodes that have to be fixed after conversion. */
1762 for (unsigned i = 0; i < m_case_bbs.length (); i++)
1764 gphi_iterator gsi;
1765 basic_block bb = m_case_bbs[i];
1766 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1768 gphi *phi = gsi.phi ();
1770 for (unsigned i = 0; i < gimple_phi_num_args (phi); i++)
1772 basic_block phi_src_bb = gimple_phi_arg_edge (phi, i)->src;
1773 if (phi_src_bb == switch_bb)
1775 tree def = gimple_phi_arg_def (phi, i);
1776 tree result = gimple_phi_result (phi);
1777 m_phi_mapping.put (result, def);
1778 break;
1785 /* Append new operands to PHI statements that were introduced due to
1786 addition of new edges to case labels. */
1788 void
1789 switch_decision_tree::fix_phi_operands_for_edges ()
1791 gphi_iterator gsi;
1793 for (unsigned i = 0; i < m_case_bbs.length (); i++)
1795 basic_block bb = m_case_bbs[i];
1796 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1798 gphi *phi = gsi.phi ();
1799 for (unsigned j = 0; j < gimple_phi_num_args (phi); j++)
1801 tree def = gimple_phi_arg_def (phi, j);
1802 if (def == NULL_TREE)
1804 edge e = gimple_phi_arg_edge (phi, j);
1805 tree *definition
1806 = m_phi_mapping.get (gimple_phi_result (phi));
1807 gcc_assert (definition);
1808 add_phi_arg (phi, *definition, e, UNKNOWN_LOCATION);
1815 /* Generate a decision tree, switching on INDEX_EXPR and jumping to
1816 one of the labels in CASE_LIST or to the DEFAULT_LABEL.
1818 We generate a binary decision tree to select the appropriate target
1819 code. */
1821 void
1822 switch_decision_tree::emit (basic_block bb, tree index_expr,
1823 profile_probability default_prob, tree index_type)
1825 balance_case_nodes (&m_case_list, NULL);
1827 if (dump_file)
1828 dump_function_to_file (current_function_decl, dump_file, dump_flags);
1829 if (dump_file && (dump_flags & TDF_DETAILS))
1831 int indent_step = ceil_log2 (TYPE_PRECISION (index_type)) + 2;
1832 fprintf (dump_file, ";; Expanding GIMPLE switch as decision tree:\n");
1833 gcc_assert (m_case_list != NULL);
1834 dump_case_nodes (dump_file, m_case_list, indent_step, 0);
1837 bb = emit_case_nodes (bb, index_expr, m_case_list, default_prob, index_type);
1839 if (bb)
1840 emit_jump (bb, m_default_bb);
1842 /* Remove all edges and do just an edge that will reach default_bb. */
1843 bb = gimple_bb (m_switch);
1844 gimple_stmt_iterator gsi = gsi_last_bb (bb);
1845 gsi_remove (&gsi, true);
1847 delete_basic_block (bb);
1850 /* Take an ordered list of case nodes
1851 and transform them into a near optimal binary tree,
1852 on the assumption that any target code selection value is as
1853 likely as any other.
1855 The transformation is performed by splitting the ordered
1856 list into two equal sections plus a pivot. The parts are
1857 then attached to the pivot as left and right branches. Each
1858 branch is then transformed recursively. */
1860 void
1861 switch_decision_tree::balance_case_nodes (case_tree_node **head,
1862 case_tree_node *parent)
1864 case_tree_node *np;
1866 np = *head;
1867 if (np)
1869 int i = 0;
1870 int ranges = 0;
1871 case_tree_node **npp;
1872 case_tree_node *left;
1874 /* Count the number of entries on branch. Also count the ranges. */
1876 while (np)
1878 if (!tree_int_cst_equal (np->m_c->get_low (), np->m_c->get_high ()))
1879 ranges++;
1881 i++;
1882 np = np->m_right;
1885 if (i > 2)
1887 /* Split this list if it is long enough for that to help. */
1888 npp = head;
1889 left = *npp;
1891 /* If there are just three nodes, split at the middle one. */
1892 if (i == 3)
1893 npp = &(*npp)->m_right;
1894 else
1896 /* Find the place in the list that bisects the list's total cost,
1897 where ranges count as 2.
1898 Here I gets half the total cost. */
1899 i = (i + ranges + 1) / 2;
1900 while (1)
1902 /* Skip nodes while their cost does not reach that amount. */
1903 if (!tree_int_cst_equal ((*npp)->m_c->get_low (),
1904 (*npp)->m_c->get_high ()))
1905 i--;
1906 i--;
1907 if (i <= 0)
1908 break;
1909 npp = &(*npp)->m_right;
1912 *head = np = *npp;
1913 *npp = 0;
1914 np->m_parent = parent;
1915 np->m_left = left;
1917 /* Optimize each of the two split parts. */
1918 balance_case_nodes (&np->m_left, np);
1919 balance_case_nodes (&np->m_right, np);
1920 np->m_c->m_subtree_prob = np->m_c->m_prob;
1921 np->m_c->m_subtree_prob += np->m_left->m_c->m_subtree_prob;
1922 np->m_c->m_subtree_prob += np->m_right->m_c->m_subtree_prob;
1924 else
1926 /* Else leave this branch as one level,
1927 but fill in `parent' fields. */
1928 np = *head;
1929 np->m_parent = parent;
1930 np->m_c->m_subtree_prob = np->m_c->m_prob;
1931 for (; np->m_right; np = np->m_right)
1933 np->m_right->m_parent = np;
1934 (*head)->m_c->m_subtree_prob += np->m_right->m_c->m_subtree_prob;
1940 /* Dump ROOT, a list or tree of case nodes, to file. */
1942 void
1943 switch_decision_tree::dump_case_nodes (FILE *f, case_tree_node *root,
1944 int indent_step, int indent_level)
1946 if (root == 0)
1947 return;
1948 indent_level++;
1950 dump_case_nodes (f, root->m_left, indent_step, indent_level);
1952 fputs (";; ", f);
1953 fprintf (f, "%*s", indent_step * indent_level, "");
1954 root->m_c->dump (f);
1955 root->m_c->m_prob.dump (f);
1956 fputs ("\n", f);
1958 dump_case_nodes (f, root->m_right, indent_step, indent_level);
1962 /* Add an unconditional jump to CASE_BB that happens in basic block BB. */
1964 void
1965 switch_decision_tree::emit_jump (basic_block bb, basic_block case_bb)
1967 edge e = single_succ_edge (bb);
1968 redirect_edge_succ (e, case_bb);
1971 /* Generate code to compare OP0 with OP1 so that the condition codes are
1972 set and to jump to LABEL_BB if the condition is true.
1973 COMPARISON is the GIMPLE comparison (EQ, NE, GT, etc.).
1974 PROB is the probability of jumping to LABEL_BB. */
1976 basic_block
1977 switch_decision_tree::emit_cmp_and_jump_insns (basic_block bb, tree op0,
1978 tree op1, tree_code comparison,
1979 basic_block label_bb,
1980 profile_probability prob)
1982 // TODO: it's once called with lhs != index.
1983 op1 = fold_convert (TREE_TYPE (op0), op1);
1985 gcond *cond = gimple_build_cond (comparison, op0, op1, NULL_TREE, NULL_TREE);
1986 gimple_stmt_iterator gsi = gsi_last_bb (bb);
1987 gsi_insert_after (&gsi, cond, GSI_NEW_STMT);
1989 gcc_assert (single_succ_p (bb));
1991 /* Make a new basic block where false branch will take place. */
1992 edge false_edge = split_block (bb, cond);
1993 false_edge->flags = EDGE_FALSE_VALUE;
1994 false_edge->probability = prob.invert ();
1996 edge true_edge = make_edge (bb, label_bb, EDGE_TRUE_VALUE);
1997 true_edge->probability = prob;
1999 return false_edge->dest;
2002 /* Emit step-by-step code to select a case for the value of INDEX.
2003 The thus generated decision tree follows the form of the
2004 case-node binary tree NODE, whose nodes represent test conditions.
2005 DEFAULT_PROB is probability of cases leading to default BB.
2006 INDEX_TYPE is the type of the index of the switch. */
2008 basic_block
2009 switch_decision_tree::emit_case_nodes (basic_block bb, tree index,
2010 case_tree_node *node,
2011 profile_probability default_prob,
2012 tree index_type)
2014 /* If node is null, we are done. */
2015 if (node == NULL)
2016 return bb;
2018 /* Branch to a label where we will handle it later. */
2019 basic_block test_bb = split_edge (single_succ_edge (bb));
2020 redirect_edge_succ (single_pred_edge (test_bb),
2021 single_succ_edge (bb)->dest);
2023 profile_probability probability
2024 = (node->m_right
2025 ? node->m_right->m_c->m_subtree_prob : profile_probability::never ());
2026 probability = ((probability + default_prob.apply_scale (1, 2))
2027 / (node->m_c->m_subtree_prob + default_prob));
2028 bb = emit_cmp_and_jump_insns (bb, index, node->m_c->get_high (), GT_EXPR,
2029 test_bb, probability);
2030 default_prob = default_prob.apply_scale (1, 2);
2032 /* Value belongs to this node or to the left-hand subtree. */
2033 probability = node->m_c->m_prob /
2034 (node->m_c->m_subtree_prob + default_prob);
2035 bb = emit_cmp_and_jump_insns (bb, index, node->m_c->get_low (), GE_EXPR,
2036 node->m_c->m_case_bb, probability);
2038 /* Handle the left-hand subtree. */
2039 bb = emit_case_nodes (bb, index, node->m_left,
2040 default_prob, index_type);
2042 /* If the left-hand subtree fell through,
2043 don't let it fall into the right-hand subtree. */
2044 if (m_default_bb)
2045 emit_jump (bb, m_default_bb);
2047 bb = emit_case_nodes (test_bb, index, node->m_right,
2048 default_prob, index_type);
2050 return bb;
2053 /* The main function of the pass scans statements for switches and invokes
2054 process_switch on them. */
2056 namespace {
2058 const pass_data pass_data_convert_switch =
2060 GIMPLE_PASS, /* type */
2061 "switchconv", /* name */
2062 OPTGROUP_NONE, /* optinfo_flags */
2063 TV_TREE_SWITCH_CONVERSION, /* tv_id */
2064 ( PROP_cfg | PROP_ssa ), /* properties_required */
2065 0, /* properties_provided */
2066 0, /* properties_destroyed */
2067 0, /* todo_flags_start */
2068 TODO_update_ssa, /* todo_flags_finish */
2071 class pass_convert_switch : public gimple_opt_pass
2073 public:
2074 pass_convert_switch (gcc::context *ctxt)
2075 : gimple_opt_pass (pass_data_convert_switch, ctxt)
2078 /* opt_pass methods: */
2079 virtual bool gate (function *) { return flag_tree_switch_conversion != 0; }
2080 virtual unsigned int execute (function *);
2082 }; // class pass_convert_switch
2084 unsigned int
2085 pass_convert_switch::execute (function *fun)
2087 basic_block bb;
2088 bool cfg_altered = false;
2090 FOR_EACH_BB_FN (bb, fun)
2092 gimple *stmt = last_stmt (bb);
2093 if (stmt && gimple_code (stmt) == GIMPLE_SWITCH)
2095 if (dump_file)
2097 expanded_location loc = expand_location (gimple_location (stmt));
2099 fprintf (dump_file, "beginning to process the following "
2100 "SWITCH statement (%s:%d) : ------- \n",
2101 loc.file, loc.line);
2102 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2103 putc ('\n', dump_file);
2106 switch_conversion sconv;
2107 sconv.expand (as_a <gswitch *> (stmt));
2108 cfg_altered |= sconv.m_cfg_altered;
2109 if (!sconv.m_reason)
2111 if (dump_file)
2113 fputs ("Switch converted\n", dump_file);
2114 fputs ("--------------------------------\n", dump_file);
2117 /* Make no effort to update the post-dominator tree.
2118 It is actually not that hard for the transformations
2119 we have performed, but it is not supported
2120 by iterate_fix_dominators. */
2121 free_dominance_info (CDI_POST_DOMINATORS);
2123 else
2125 if (dump_file)
2127 fputs ("Bailing out - ", dump_file);
2128 fputs (sconv.m_reason, dump_file);
2129 fputs ("\n--------------------------------\n", dump_file);
2135 return cfg_altered ? TODO_cleanup_cfg : 0;;
2138 } // anon namespace
2140 gimple_opt_pass *
2141 make_pass_convert_switch (gcc::context *ctxt)
2143 return new pass_convert_switch (ctxt);
2146 /* The main function of the pass scans statements for switches and invokes
2147 process_switch on them. */
2149 namespace {
2151 template <bool O0> class pass_lower_switch: public gimple_opt_pass
2153 public:
2154 pass_lower_switch (gcc::context *ctxt) : gimple_opt_pass (data, ctxt) {}
2156 static const pass_data data;
2157 opt_pass *
2158 clone ()
2160 return new pass_lower_switch<O0> (m_ctxt);
2163 virtual bool
2164 gate (function *)
2166 return !O0 || !optimize;
2169 virtual unsigned int execute (function *fun);
2170 }; // class pass_lower_switch
2172 template <bool O0>
2173 const pass_data pass_lower_switch<O0>::data = {
2174 GIMPLE_PASS, /* type */
2175 O0 ? "switchlower_O0" : "switchlower", /* name */
2176 OPTGROUP_NONE, /* optinfo_flags */
2177 TV_TREE_SWITCH_LOWERING, /* tv_id */
2178 ( PROP_cfg | PROP_ssa ), /* properties_required */
2179 0, /* properties_provided */
2180 0, /* properties_destroyed */
2181 0, /* todo_flags_start */
2182 TODO_update_ssa | TODO_cleanup_cfg, /* todo_flags_finish */
2185 template <bool O0>
2186 unsigned int
2187 pass_lower_switch<O0>::execute (function *fun)
2189 basic_block bb;
2190 bool expanded = false;
2192 auto_vec<gimple *> switch_statements;
2193 switch_statements.create (1);
2195 FOR_EACH_BB_FN (bb, fun)
2197 gimple *stmt = last_stmt (bb);
2198 if (stmt && gimple_code (stmt) == GIMPLE_SWITCH)
2199 switch_statements.safe_push (stmt);
2202 for (unsigned i = 0; i < switch_statements.length (); i++)
2204 gimple *stmt = switch_statements[i];
2205 if (dump_file)
2207 expanded_location loc = expand_location (gimple_location (stmt));
2209 fprintf (dump_file, "beginning to process the following "
2210 "SWITCH statement (%s:%d) : ------- \n",
2211 loc.file, loc.line);
2212 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2213 putc ('\n', dump_file);
2216 gswitch *swtch = dyn_cast<gswitch *> (stmt);
2217 if (swtch)
2219 switch_decision_tree dt (swtch);
2220 expanded |= dt.analyze_switch_statement ();
2224 if (expanded)
2226 free_dominance_info (CDI_DOMINATORS);
2227 free_dominance_info (CDI_POST_DOMINATORS);
2228 mark_virtual_operands_for_renaming (cfun);
2231 return 0;
2234 } // anon namespace
2236 gimple_opt_pass *
2237 make_pass_lower_switch_O0 (gcc::context *ctxt)
2239 return new pass_lower_switch<true> (ctxt);
2241 gimple_opt_pass *
2242 make_pass_lower_switch (gcc::context *ctxt)
2244 return new pass_lower_switch<false> (ctxt);