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