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