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