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