1 1.1 mrg /* Tree switch conversion for GNU compiler. 2 1.1.1.3 mrg Copyright (C) 2017-2022 Free Software Foundation, Inc. 3 1.1 mrg 4 1.1 mrg This file is part of GCC. 5 1.1 mrg 6 1.1 mrg GCC is free software; you can redistribute it and/or modify it under 7 1.1 mrg the terms of the GNU General Public License as published by the Free 8 1.1 mrg Software Foundation; either version 3, or (at your option) any later 9 1.1 mrg version. 10 1.1 mrg 11 1.1 mrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY 12 1.1 mrg WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 1.1 mrg FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 1.1 mrg for more details. 15 1.1 mrg 16 1.1 mrg You should have received a copy of the GNU General Public License 17 1.1 mrg along with GCC; see the file COPYING3. If not see 18 1.1 mrg <http://www.gnu.org/licenses/>. */ 19 1.1 mrg 20 1.1 mrg #ifndef TREE_SWITCH_CONVERSION_H 21 1.1 mrg #define TREE_SWITCH_CONVERSION_H 22 1.1 mrg 23 1.1 mrg namespace tree_switch_conversion { 24 1.1 mrg 25 1.1 mrg /* Type of cluster. */ 26 1.1 mrg 27 1.1 mrg enum cluster_type 28 1.1 mrg { 29 1.1 mrg SIMPLE_CASE, 30 1.1 mrg JUMP_TABLE, 31 1.1 mrg BIT_TEST 32 1.1 mrg }; 33 1.1 mrg 34 1.1 mrg #define PRINT_CASE(f,c) print_generic_expr (f, c) 35 1.1 mrg 36 1.1 mrg /* Abstract base class for representing a cluster of cases. 37 1.1 mrg 38 1.1 mrg Here is the inheritance hierarachy, and the enum_cluster_type 39 1.1 mrg values for the concrete subclasses: 40 1.1 mrg 41 1.1 mrg cluster 42 1.1 mrg |-simple_cluster (SIMPLE_CASE) 43 1.1 mrg `-group_cluster 44 1.1 mrg |-jump_table_cluster (JUMP_TABLE) 45 1.1 mrg `-bit_test_cluster (BIT_TEST). */ 46 1.1 mrg 47 1.1.1.2 mrg class cluster 48 1.1 mrg { 49 1.1.1.2 mrg public: 50 1.1 mrg /* Constructor. */ 51 1.1.1.3 mrg inline cluster (tree case_label_expr, basic_block case_bb, 52 1.1.1.3 mrg profile_probability prob, profile_probability subtree_prob); 53 1.1 mrg 54 1.1 mrg /* Destructor. */ 55 1.1 mrg virtual ~cluster () 56 1.1 mrg {} 57 1.1 mrg 58 1.1 mrg /* Return type. */ 59 1.1 mrg virtual cluster_type get_type () = 0; 60 1.1 mrg 61 1.1 mrg /* Get low value covered by a cluster. */ 62 1.1 mrg virtual tree get_low () = 0; 63 1.1 mrg 64 1.1 mrg /* Get high value covered by a cluster. */ 65 1.1 mrg virtual tree get_high () = 0; 66 1.1 mrg 67 1.1 mrg /* Debug content of a cluster. */ 68 1.1 mrg virtual void debug () = 0; 69 1.1 mrg 70 1.1 mrg /* Dump content of a cluster. */ 71 1.1 mrg virtual void dump (FILE *f, bool details = false) = 0; 72 1.1 mrg 73 1.1 mrg /* Emit GIMPLE code to handle the cluster. */ 74 1.1.1.2 mrg virtual void emit (tree, tree, tree, basic_block, location_t) = 0; 75 1.1 mrg 76 1.1 mrg /* Return true if a cluster handles only a single case value and the 77 1.1 mrg value is not a range. */ 78 1.1 mrg virtual bool is_single_value_p () 79 1.1 mrg { 80 1.1 mrg return false; 81 1.1 mrg } 82 1.1 mrg 83 1.1 mrg /* Return range of a cluster. If value would overflow in type of LOW, 84 1.1 mrg then return 0. */ 85 1.1 mrg static unsigned HOST_WIDE_INT get_range (tree low, tree high) 86 1.1 mrg { 87 1.1.1.2 mrg wide_int w = wi::to_wide (high) - wi::to_wide (low); 88 1.1.1.2 mrg if (wi::neg_p (w, TYPE_SIGN (TREE_TYPE (low))) || !wi::fits_uhwi_p (w)) 89 1.1 mrg return 0; 90 1.1.1.2 mrg return w.to_uhwi () + 1; 91 1.1 mrg } 92 1.1 mrg 93 1.1 mrg /* Case label. */ 94 1.1 mrg tree m_case_label_expr; 95 1.1 mrg 96 1.1 mrg /* Basic block of the case. */ 97 1.1 mrg basic_block m_case_bb; 98 1.1 mrg 99 1.1 mrg /* Probability of taking this cluster. */ 100 1.1 mrg profile_probability m_prob; 101 1.1 mrg 102 1.1 mrg /* Probability of reaching subtree rooted at this node. */ 103 1.1 mrg profile_probability m_subtree_prob; 104 1.1 mrg 105 1.1 mrg protected: 106 1.1 mrg /* Default constructor. */ 107 1.1 mrg cluster () {} 108 1.1 mrg }; 109 1.1 mrg 110 1.1 mrg cluster::cluster (tree case_label_expr, basic_block case_bb, 111 1.1 mrg profile_probability prob, profile_probability subtree_prob): 112 1.1 mrg m_case_label_expr (case_label_expr), m_case_bb (case_bb), m_prob (prob), 113 1.1 mrg m_subtree_prob (subtree_prob) 114 1.1 mrg { 115 1.1 mrg } 116 1.1 mrg 117 1.1 mrg /* Subclass of cluster representing a simple contiguous range 118 1.1 mrg from [low..high]. */ 119 1.1 mrg 120 1.1.1.2 mrg class simple_cluster: public cluster 121 1.1 mrg { 122 1.1.1.2 mrg public: 123 1.1 mrg /* Constructor. */ 124 1.1.1.3 mrg inline simple_cluster (tree low, tree high, tree case_label_expr, 125 1.1.1.3 mrg basic_block case_bb, profile_probability prob, 126 1.1.1.3 mrg bool has_forward_bb = false); 127 1.1 mrg 128 1.1 mrg /* Destructor. */ 129 1.1 mrg ~simple_cluster () 130 1.1 mrg {} 131 1.1 mrg 132 1.1 mrg cluster_type 133 1.1 mrg get_type () 134 1.1 mrg { 135 1.1 mrg return SIMPLE_CASE; 136 1.1 mrg } 137 1.1 mrg 138 1.1 mrg tree 139 1.1 mrg get_low () 140 1.1 mrg { 141 1.1 mrg return m_low; 142 1.1 mrg } 143 1.1 mrg 144 1.1 mrg tree 145 1.1 mrg get_high () 146 1.1 mrg { 147 1.1 mrg return m_high; 148 1.1 mrg } 149 1.1 mrg 150 1.1.1.3 mrg void set_high (tree high) 151 1.1.1.3 mrg { 152 1.1.1.3 mrg m_high = high; 153 1.1.1.3 mrg } 154 1.1.1.3 mrg 155 1.1 mrg void 156 1.1 mrg debug () 157 1.1 mrg { 158 1.1 mrg dump (stderr); 159 1.1 mrg } 160 1.1 mrg 161 1.1 mrg void 162 1.1 mrg dump (FILE *f, bool details ATTRIBUTE_UNUSED = false) 163 1.1 mrg { 164 1.1 mrg PRINT_CASE (f, get_low ()); 165 1.1 mrg if (get_low () != get_high ()) 166 1.1 mrg { 167 1.1 mrg fprintf (f, "-"); 168 1.1 mrg PRINT_CASE (f, get_high ()); 169 1.1 mrg } 170 1.1 mrg fprintf (f, " "); 171 1.1 mrg } 172 1.1 mrg 173 1.1.1.2 mrg void emit (tree, tree, tree, basic_block, location_t) 174 1.1 mrg { 175 1.1 mrg gcc_unreachable (); 176 1.1 mrg } 177 1.1 mrg 178 1.1 mrg bool is_single_value_p () 179 1.1 mrg { 180 1.1 mrg return tree_int_cst_equal (get_low (), get_high ()); 181 1.1 mrg } 182 1.1 mrg 183 1.1.1.3 mrg /* Return number of comparisons needed for the case. */ 184 1.1.1.3 mrg unsigned 185 1.1.1.3 mrg get_comparison_count () 186 1.1.1.3 mrg { 187 1.1.1.3 mrg return m_range_p ? 2 : 1; 188 1.1.1.3 mrg } 189 1.1.1.3 mrg 190 1.1 mrg /* Low value of the case. */ 191 1.1 mrg tree m_low; 192 1.1 mrg 193 1.1 mrg /* High value of the case. */ 194 1.1 mrg tree m_high; 195 1.1 mrg 196 1.1 mrg /* True if case is a range. */ 197 1.1 mrg bool m_range_p; 198 1.1.1.3 mrg 199 1.1.1.3 mrg /* True if the case will use a forwarder BB. */ 200 1.1.1.3 mrg bool m_has_forward_bb; 201 1.1 mrg }; 202 1.1 mrg 203 1.1 mrg simple_cluster::simple_cluster (tree low, tree high, tree case_label_expr, 204 1.1.1.3 mrg basic_block case_bb, profile_probability prob, 205 1.1.1.3 mrg bool has_forward_bb): 206 1.1 mrg cluster (case_label_expr, case_bb, prob, prob), 207 1.1.1.3 mrg m_low (low), m_high (high), m_has_forward_bb (has_forward_bb) 208 1.1 mrg { 209 1.1 mrg m_range_p = m_high != NULL; 210 1.1 mrg if (m_high == NULL) 211 1.1 mrg m_high = m_low; 212 1.1 mrg } 213 1.1 mrg 214 1.1 mrg /* Abstract subclass of jump table and bit test cluster, 215 1.1 mrg handling a collection of simple_cluster instances. */ 216 1.1 mrg 217 1.1.1.2 mrg class group_cluster: public cluster 218 1.1 mrg { 219 1.1.1.2 mrg public: 220 1.1 mrg /* Constructor. */ 221 1.1 mrg group_cluster (vec<cluster *> &clusters, unsigned start, unsigned end); 222 1.1 mrg 223 1.1 mrg /* Destructor. */ 224 1.1 mrg ~group_cluster (); 225 1.1 mrg 226 1.1 mrg tree 227 1.1 mrg get_low () 228 1.1 mrg { 229 1.1 mrg return m_cases[0]->get_low (); 230 1.1 mrg } 231 1.1 mrg 232 1.1 mrg tree 233 1.1 mrg get_high () 234 1.1 mrg { 235 1.1 mrg return m_cases[m_cases.length () - 1]->get_high (); 236 1.1 mrg } 237 1.1 mrg 238 1.1 mrg void 239 1.1 mrg debug () 240 1.1 mrg { 241 1.1 mrg dump (stderr); 242 1.1 mrg } 243 1.1 mrg 244 1.1 mrg void dump (FILE *f, bool details = false); 245 1.1 mrg 246 1.1 mrg /* List of simple clusters handled by the group. */ 247 1.1 mrg vec<simple_cluster *> m_cases; 248 1.1 mrg }; 249 1.1 mrg 250 1.1 mrg /* Concrete subclass of group_cluster representing a collection 251 1.1 mrg of cases to be implemented as a jump table. 252 1.1 mrg The "emit" vfunc gernerates a nested switch statement which 253 1.1 mrg is later lowered to a jump table. */ 254 1.1 mrg 255 1.1.1.2 mrg class jump_table_cluster: public group_cluster 256 1.1 mrg { 257 1.1.1.2 mrg public: 258 1.1 mrg /* Constructor. */ 259 1.1 mrg jump_table_cluster (vec<cluster *> &clusters, unsigned start, unsigned end) 260 1.1 mrg : group_cluster (clusters, start, end) 261 1.1 mrg {} 262 1.1 mrg 263 1.1 mrg cluster_type 264 1.1 mrg get_type () 265 1.1 mrg { 266 1.1 mrg return JUMP_TABLE; 267 1.1 mrg } 268 1.1 mrg 269 1.1 mrg void emit (tree index_expr, tree index_type, 270 1.1.1.2 mrg tree default_label_expr, basic_block default_bb, location_t loc); 271 1.1 mrg 272 1.1 mrg /* Find jump tables of given CLUSTERS, where all members of the vector 273 1.1 mrg are of type simple_cluster. New clusters are returned. */ 274 1.1 mrg static vec<cluster *> find_jump_tables (vec<cluster *> &clusters); 275 1.1 mrg 276 1.1 mrg /* Return true when cluster starting at START and ending at END (inclusive) 277 1.1.1.3 mrg can build a jump-table. COMPARISON_COUNT is number of comparison 278 1.1.1.3 mrg operations needed if the clusters are expanded as decision tree. 279 1.1.1.3 mrg MAX_RATIO tells about the maximum code growth (in percent). */ 280 1.1 mrg static bool can_be_handled (const vec<cluster *> &clusters, unsigned start, 281 1.1.1.3 mrg unsigned end, unsigned HOST_WIDE_INT max_ratio, 282 1.1.1.3 mrg unsigned HOST_WIDE_INT comparison_count); 283 1.1 mrg 284 1.1 mrg /* Return true if cluster starting at START and ending at END (inclusive) 285 1.1 mrg is profitable transformation. */ 286 1.1 mrg static bool is_beneficial (const vec<cluster *> &clusters, unsigned start, 287 1.1 mrg unsigned end); 288 1.1 mrg 289 1.1 mrg /* Return the smallest number of different values for which it is best 290 1.1 mrg to use a jump-table instead of a tree of conditional branches. */ 291 1.1 mrg static inline unsigned int case_values_threshold (void); 292 1.1 mrg 293 1.1 mrg /* Return whether jump table expansion is allowed. */ 294 1.1.1.3 mrg static inline bool is_enabled (void); 295 1.1 mrg }; 296 1.1 mrg 297 1.1 mrg /* A GIMPLE switch statement can be expanded to a short sequence of bit-wise 298 1.1 mrg comparisons. "switch(x)" is converted into "if ((1 << (x-MINVAL)) & CST)" 299 1.1 mrg where CST and MINVAL are integer constants. This is better than a series 300 1.1 mrg of compare-and-banch insns in some cases, e.g. we can implement: 301 1.1 mrg 302 1.1 mrg if ((x==4) || (x==6) || (x==9) || (x==11)) 303 1.1 mrg 304 1.1 mrg as a single bit test: 305 1.1 mrg 306 1.1 mrg if ((1<<x) & ((1<<4)|(1<<6)|(1<<9)|(1<<11))) 307 1.1 mrg 308 1.1 mrg This transformation is only applied if the number of case targets is small, 309 1.1 mrg if CST constains at least 3 bits, and "1 << x" is cheap. The bit tests are 310 1.1 mrg performed in "word_mode". 311 1.1 mrg 312 1.1 mrg The following example shows the code the transformation generates: 313 1.1 mrg 314 1.1 mrg int bar(int x) 315 1.1 mrg { 316 1.1 mrg switch (x) 317 1.1 mrg { 318 1.1 mrg case '0': case '1': case '2': case '3': case '4': 319 1.1 mrg case '5': case '6': case '7': case '8': case '9': 320 1.1 mrg case 'A': case 'B': case 'C': case 'D': case 'E': 321 1.1 mrg case 'F': 322 1.1 mrg return 1; 323 1.1 mrg } 324 1.1 mrg return 0; 325 1.1 mrg } 326 1.1 mrg 327 1.1 mrg ==> 328 1.1 mrg 329 1.1 mrg bar (int x) 330 1.1 mrg { 331 1.1 mrg tmp1 = x - 48; 332 1.1 mrg if (tmp1 > (70 - 48)) goto L2; 333 1.1 mrg tmp2 = 1 << tmp1; 334 1.1 mrg tmp3 = 0b11111100000001111111111; 335 1.1 mrg if ((tmp2 & tmp3) != 0) goto L1 ; else goto L2; 336 1.1 mrg L1: 337 1.1 mrg return 1; 338 1.1 mrg L2: 339 1.1 mrg return 0; 340 1.1 mrg } 341 1.1 mrg 342 1.1 mrg TODO: There are still some improvements to this transformation that could 343 1.1 mrg be implemented: 344 1.1 mrg 345 1.1 mrg * A narrower mode than word_mode could be used if that is cheaper, e.g. 346 1.1 mrg for x86_64 where a narrower-mode shift may result in smaller code. 347 1.1 mrg 348 1.1 mrg * The compounded constant could be shifted rather than the one. The 349 1.1 mrg test would be either on the sign bit or on the least significant bit, 350 1.1 mrg depending on the direction of the shift. On some machines, the test 351 1.1 mrg for the branch would be free if the bit to test is already set by the 352 1.1 mrg shift operation. 353 1.1 mrg 354 1.1 mrg This transformation was contributed by Roger Sayle, see this e-mail: 355 1.1 mrg http://gcc.gnu.org/ml/gcc-patches/2003-01/msg01950.html 356 1.1 mrg */ 357 1.1 mrg 358 1.1.1.2 mrg class bit_test_cluster: public group_cluster 359 1.1 mrg { 360 1.1.1.2 mrg public: 361 1.1 mrg /* Constructor. */ 362 1.1 mrg bit_test_cluster (vec<cluster *> &clusters, unsigned start, unsigned end, 363 1.1 mrg bool handles_entire_switch) 364 1.1 mrg :group_cluster (clusters, start, end), 365 1.1 mrg m_handles_entire_switch (handles_entire_switch) 366 1.1 mrg {} 367 1.1 mrg 368 1.1 mrg cluster_type 369 1.1 mrg get_type () 370 1.1 mrg { 371 1.1 mrg return BIT_TEST; 372 1.1 mrg } 373 1.1 mrg 374 1.1 mrg /* Expand a switch statement by a short sequence of bit-wise 375 1.1 mrg comparisons. "switch(x)" is effectively converted into 376 1.1 mrg "if ((1 << (x-MINVAL)) & CST)" where CST and MINVAL are 377 1.1 mrg integer constants. 378 1.1 mrg 379 1.1 mrg INDEX_EXPR is the value being switched on. 380 1.1 mrg 381 1.1 mrg MINVAL is the lowest case value of in the case nodes, 382 1.1 mrg and RANGE is highest value minus MINVAL. MINVAL and RANGE 383 1.1 mrg are not guaranteed to be of the same type as INDEX_EXPR 384 1.1 mrg (the gimplifier doesn't change the type of case label values, 385 1.1 mrg and MINVAL and RANGE are derived from those values). 386 1.1 mrg MAXVAL is MINVAL + RANGE. 387 1.1 mrg 388 1.1 mrg There *MUST* be max_case_bit_tests or less unique case 389 1.1 mrg node targets. */ 390 1.1 mrg void emit (tree index_expr, tree index_type, 391 1.1.1.2 mrg tree default_label_expr, basic_block default_bb, location_t loc); 392 1.1 mrg 393 1.1 mrg /* Find bit tests of given CLUSTERS, where all members of the vector 394 1.1 mrg are of type simple_cluster. New clusters are returned. */ 395 1.1 mrg static vec<cluster *> find_bit_tests (vec<cluster *> &clusters); 396 1.1 mrg 397 1.1 mrg /* Return true when RANGE of case values with UNIQ labels 398 1.1 mrg can build a bit test. */ 399 1.1 mrg static bool can_be_handled (unsigned HOST_WIDE_INT range, unsigned uniq); 400 1.1 mrg 401 1.1 mrg /* Return true when cluster starting at START and ending at END (inclusive) 402 1.1 mrg can build a bit test. */ 403 1.1 mrg static bool can_be_handled (const vec<cluster *> &clusters, unsigned start, 404 1.1 mrg unsigned end); 405 1.1 mrg 406 1.1 mrg /* Return true when COUNT of cases of UNIQ labels is beneficial for bit test 407 1.1 mrg transformation. */ 408 1.1 mrg static bool is_beneficial (unsigned count, unsigned uniq); 409 1.1 mrg 410 1.1 mrg /* Return true if cluster starting at START and ending at END (inclusive) 411 1.1 mrg is profitable transformation. */ 412 1.1 mrg static bool is_beneficial (const vec<cluster *> &clusters, unsigned start, 413 1.1 mrg unsigned end); 414 1.1 mrg 415 1.1 mrg /* Split the basic block at the statement pointed to by GSIP, and insert 416 1.1 mrg a branch to the target basic block of E_TRUE conditional on tree 417 1.1 mrg expression COND. 418 1.1 mrg 419 1.1 mrg It is assumed that there is already an edge from the to-be-split 420 1.1 mrg basic block to E_TRUE->dest block. This edge is removed, and the 421 1.1 mrg profile information on the edge is re-used for the new conditional 422 1.1 mrg jump. 423 1.1 mrg 424 1.1 mrg The CFG is updated. The dominator tree will not be valid after 425 1.1 mrg this transformation, but the immediate dominators are updated if 426 1.1 mrg UPDATE_DOMINATORS is true. 427 1.1 mrg 428 1.1 mrg Returns the newly created basic block. */ 429 1.1 mrg static basic_block hoist_edge_and_branch_if_true (gimple_stmt_iterator *gsip, 430 1.1 mrg tree cond, 431 1.1 mrg basic_block case_bb, 432 1.1.1.2 mrg profile_probability prob, 433 1.1.1.2 mrg location_t); 434 1.1 mrg 435 1.1.1.3 mrg /* Return whether bit test expansion is allowed. */ 436 1.1.1.3 mrg static inline bool is_enabled (void) 437 1.1.1.3 mrg { 438 1.1.1.3 mrg return flag_bit_tests; 439 1.1.1.3 mrg } 440 1.1.1.3 mrg 441 1.1 mrg /* True when the jump table handles an entire switch statement. */ 442 1.1 mrg bool m_handles_entire_switch; 443 1.1 mrg 444 1.1 mrg /* Maximum number of different basic blocks that can be handled by 445 1.1 mrg a bit test. */ 446 1.1 mrg static const int m_max_case_bit_tests = 3; 447 1.1 mrg }; 448 1.1 mrg 449 1.1 mrg /* Helper struct to find minimal clusters. */ 450 1.1 mrg 451 1.1.1.2 mrg class min_cluster_item 452 1.1 mrg { 453 1.1.1.2 mrg public: 454 1.1 mrg /* Constructor. */ 455 1.1 mrg min_cluster_item (unsigned count, unsigned start, unsigned non_jt_cases): 456 1.1 mrg m_count (count), m_start (start), m_non_jt_cases (non_jt_cases) 457 1.1 mrg {} 458 1.1 mrg 459 1.1 mrg /* Count of clusters. */ 460 1.1 mrg unsigned m_count; 461 1.1 mrg 462 1.1 mrg /* Index where is cluster boundary. */ 463 1.1 mrg unsigned m_start; 464 1.1 mrg 465 1.1 mrg /* Total number of cases that will not be in a jump table. */ 466 1.1 mrg unsigned m_non_jt_cases; 467 1.1 mrg }; 468 1.1 mrg 469 1.1 mrg /* Helper struct to represent switch decision tree. */ 470 1.1 mrg 471 1.1.1.2 mrg class case_tree_node 472 1.1 mrg { 473 1.1.1.2 mrg public: 474 1.1 mrg /* Empty Constructor. */ 475 1.1 mrg case_tree_node (); 476 1.1 mrg 477 1.1 mrg /* Return true when it has a child. */ 478 1.1 mrg bool has_child () 479 1.1 mrg { 480 1.1 mrg return m_left != NULL || m_right != NULL; 481 1.1 mrg } 482 1.1 mrg 483 1.1 mrg /* Left son in binary tree. */ 484 1.1 mrg case_tree_node *m_left; 485 1.1 mrg 486 1.1 mrg /* Right son in binary tree; also node chain. */ 487 1.1 mrg case_tree_node *m_right; 488 1.1 mrg 489 1.1 mrg /* Parent of node in binary tree. */ 490 1.1 mrg case_tree_node *m_parent; 491 1.1 mrg 492 1.1 mrg /* Cluster represented by this tree node. */ 493 1.1 mrg cluster *m_c; 494 1.1 mrg }; 495 1.1 mrg 496 1.1 mrg inline 497 1.1 mrg case_tree_node::case_tree_node (): 498 1.1 mrg m_left (NULL), m_right (NULL), m_parent (NULL), m_c (NULL) 499 1.1 mrg { 500 1.1 mrg } 501 1.1 mrg 502 1.1 mrg unsigned int 503 1.1 mrg jump_table_cluster::case_values_threshold (void) 504 1.1 mrg { 505 1.1.1.2 mrg unsigned int threshold = param_case_values_threshold; 506 1.1 mrg 507 1.1 mrg if (threshold == 0) 508 1.1 mrg threshold = targetm.case_values_threshold (); 509 1.1 mrg 510 1.1 mrg return threshold; 511 1.1 mrg } 512 1.1 mrg 513 1.1 mrg /* Return whether jump table expansion is allowed. */ 514 1.1 mrg bool jump_table_cluster::is_enabled (void) 515 1.1 mrg { 516 1.1 mrg /* If neither casesi or tablejump is available, or flag_jump_tables 517 1.1 mrg over-ruled us, we really have no choice. */ 518 1.1 mrg if (!targetm.have_casesi () && !targetm.have_tablejump ()) 519 1.1 mrg return false; 520 1.1 mrg if (!flag_jump_tables) 521 1.1 mrg return false; 522 1.1 mrg #ifndef ASM_OUTPUT_ADDR_DIFF_ELT 523 1.1 mrg if (flag_pic) 524 1.1 mrg return false; 525 1.1 mrg #endif 526 1.1 mrg 527 1.1 mrg return true; 528 1.1 mrg } 529 1.1 mrg 530 1.1 mrg /* A case_bit_test represents a set of case nodes that may be 531 1.1 mrg selected from using a bit-wise comparison. HI and LO hold 532 1.1 mrg the integer to be tested against, TARGET_EDGE contains the 533 1.1 mrg edge to the basic block to jump to upon success and BITS 534 1.1 mrg counts the number of case nodes handled by this test, 535 1.1 mrg typically the number of bits set in HI:LO. The LABEL field 536 1.1 mrg is used to quickly identify all cases in this set without 537 1.1 mrg looking at label_to_block for every case label. */ 538 1.1 mrg 539 1.1.1.2 mrg class case_bit_test 540 1.1 mrg { 541 1.1.1.2 mrg public: 542 1.1 mrg wide_int mask; 543 1.1 mrg basic_block target_bb; 544 1.1 mrg tree label; 545 1.1 mrg int bits; 546 1.1 mrg 547 1.1 mrg /* Comparison function for qsort to order bit tests by decreasing 548 1.1 mrg probability of execution. */ 549 1.1 mrg static int cmp (const void *p1, const void *p2); 550 1.1 mrg }; 551 1.1 mrg 552 1.1.1.2 mrg class switch_decision_tree 553 1.1 mrg { 554 1.1.1.2 mrg public: 555 1.1 mrg /* Constructor. */ 556 1.1 mrg switch_decision_tree (gswitch *swtch): m_switch (swtch), m_phi_mapping (), 557 1.1 mrg m_case_bbs (), m_case_node_pool ("struct case_node pool"), 558 1.1 mrg m_case_list (NULL) 559 1.1 mrg { 560 1.1 mrg } 561 1.1 mrg 562 1.1 mrg /* Analyze switch statement and return true when the statement is expanded 563 1.1 mrg as decision tree. */ 564 1.1 mrg bool analyze_switch_statement (); 565 1.1 mrg 566 1.1 mrg /* Attempt to expand CLUSTERS as a decision tree. Return true when 567 1.1 mrg expanded. */ 568 1.1 mrg bool try_switch_expansion (vec<cluster *> &clusters); 569 1.1 mrg /* Compute the number of case labels that correspond to each outgoing edge of 570 1.1 mrg switch statement. Record this information in the aux field of the edge. 571 1.1 mrg */ 572 1.1 mrg void compute_cases_per_edge (); 573 1.1 mrg 574 1.1 mrg /* Before switch transformation, record all SSA_NAMEs defined in switch BB 575 1.1 mrg and used in a label basic block. */ 576 1.1 mrg void record_phi_operand_mapping (); 577 1.1 mrg 578 1.1 mrg /* Append new operands to PHI statements that were introduced due to 579 1.1 mrg addition of new edges to case labels. */ 580 1.1 mrg void fix_phi_operands_for_edges (); 581 1.1 mrg 582 1.1 mrg /* Generate a decision tree, switching on INDEX_EXPR and jumping to 583 1.1 mrg one of the labels in CASE_LIST or to the DEFAULT_LABEL. 584 1.1 mrg 585 1.1 mrg We generate a binary decision tree to select the appropriate target 586 1.1 mrg code. */ 587 1.1 mrg void emit (basic_block bb, tree index_expr, 588 1.1 mrg profile_probability default_prob, tree index_type); 589 1.1 mrg 590 1.1 mrg /* Emit step-by-step code to select a case for the value of INDEX. 591 1.1 mrg The thus generated decision tree follows the form of the 592 1.1 mrg case-node binary tree NODE, whose nodes represent test conditions. 593 1.1 mrg DEFAULT_PROB is probability of cases leading to default BB. 594 1.1 mrg INDEX_TYPE is the type of the index of the switch. */ 595 1.1 mrg basic_block emit_case_nodes (basic_block bb, tree index, 596 1.1 mrg case_tree_node *node, 597 1.1 mrg profile_probability default_prob, 598 1.1 mrg tree index_type, location_t); 599 1.1 mrg 600 1.1 mrg /* Take an ordered list of case nodes 601 1.1 mrg and transform them into a near optimal binary tree, 602 1.1 mrg on the assumption that any target code selection value is as 603 1.1 mrg likely as any other. 604 1.1 mrg 605 1.1 mrg The transformation is performed by splitting the ordered 606 1.1 mrg list into two equal sections plus a pivot. The parts are 607 1.1 mrg then attached to the pivot as left and right branches. Each 608 1.1 mrg branch is then transformed recursively. */ 609 1.1 mrg static void balance_case_nodes (case_tree_node **head, 610 1.1 mrg case_tree_node *parent); 611 1.1 mrg 612 1.1 mrg /* Dump ROOT, a list or tree of case nodes, to file F. */ 613 1.1 mrg static void dump_case_nodes (FILE *f, case_tree_node *root, int indent_step, 614 1.1 mrg int indent_level); 615 1.1 mrg 616 1.1 mrg /* Add an unconditional jump to CASE_BB that happens in basic block BB. */ 617 1.1 mrg static void emit_jump (basic_block bb, basic_block case_bb); 618 1.1 mrg 619 1.1 mrg /* Generate code to compare OP0 with OP1 so that the condition codes are 620 1.1 mrg set and to jump to LABEL_BB if the condition is true. 621 1.1 mrg COMPARISON is the GIMPLE comparison (EQ, NE, GT, etc.). 622 1.1 mrg PROB is the probability of jumping to LABEL_BB. */ 623 1.1 mrg static basic_block emit_cmp_and_jump_insns (basic_block bb, tree op0, 624 1.1 mrg tree op1, tree_code comparison, 625 1.1 mrg basic_block label_bb, 626 1.1 mrg profile_probability prob, 627 1.1 mrg location_t); 628 1.1 mrg 629 1.1 mrg /* Generate code to jump to LABEL if OP0 and OP1 are equal in mode MODE. 630 1.1 mrg PROB is the probability of jumping to LABEL_BB. */ 631 1.1 mrg static basic_block do_jump_if_equal (basic_block bb, tree op0, tree op1, 632 1.1 mrg basic_block label_bb, 633 1.1 mrg profile_probability prob, 634 1.1 mrg location_t); 635 1.1 mrg 636 1.1 mrg /* Reset the aux field of all outgoing edges of switch basic block. */ 637 1.1 mrg static inline void reset_out_edges_aux (gswitch *swtch); 638 1.1 mrg 639 1.1 mrg /* Switch statement. */ 640 1.1 mrg gswitch *m_switch; 641 1.1 mrg 642 1.1 mrg /* Map of PHI nodes that have to be fixed after expansion. */ 643 1.1 mrg hash_map<tree, tree> m_phi_mapping; 644 1.1 mrg 645 1.1 mrg /* List of basic blocks that belong to labels of the switch. */ 646 1.1 mrg auto_vec<basic_block> m_case_bbs; 647 1.1 mrg 648 1.1 mrg /* Basic block with default label. */ 649 1.1 mrg basic_block m_default_bb; 650 1.1 mrg 651 1.1 mrg /* A pool for case nodes. */ 652 1.1 mrg object_allocator<case_tree_node> m_case_node_pool; 653 1.1 mrg 654 1.1 mrg /* Balanced tree of case nodes. */ 655 1.1 mrg case_tree_node *m_case_list; 656 1.1 mrg }; 657 1.1 mrg 658 1.1 mrg /* 659 1.1 mrg Switch initialization conversion 660 1.1 mrg 661 1.1 mrg The following pass changes simple initializations of scalars in a switch 662 1.1 mrg statement into initializations from a static array. Obviously, the values 663 1.1 mrg must be constant and known at compile time and a default branch must be 664 1.1 mrg provided. For example, the following code: 665 1.1 mrg 666 1.1 mrg int a,b; 667 1.1 mrg 668 1.1 mrg switch (argc) 669 1.1 mrg { 670 1.1 mrg case 1: 671 1.1 mrg case 2: 672 1.1 mrg a_1 = 8; 673 1.1 mrg b_1 = 6; 674 1.1 mrg break; 675 1.1 mrg case 3: 676 1.1 mrg a_2 = 9; 677 1.1 mrg b_2 = 5; 678 1.1 mrg break; 679 1.1 mrg case 12: 680 1.1 mrg a_3 = 10; 681 1.1 mrg b_3 = 4; 682 1.1 mrg break; 683 1.1 mrg default: 684 1.1 mrg a_4 = 16; 685 1.1 mrg b_4 = 1; 686 1.1 mrg break; 687 1.1 mrg } 688 1.1 mrg a_5 = PHI <a_1, a_2, a_3, a_4> 689 1.1 mrg b_5 = PHI <b_1, b_2, b_3, b_4> 690 1.1 mrg 691 1.1 mrg 692 1.1 mrg is changed into: 693 1.1 mrg 694 1.1 mrg static const int = CSWTCH01[] = {6, 6, 5, 1, 1, 1, 1, 1, 1, 1, 1, 4}; 695 1.1 mrg static const int = CSWTCH02[] = {8, 8, 9, 16, 16, 16, 16, 16, 16, 16, 696 1.1 mrg 16, 16, 10}; 697 1.1 mrg 698 1.1 mrg if (((unsigned) argc) - 1 < 11) 699 1.1 mrg { 700 1.1 mrg a_6 = CSWTCH02[argc - 1]; 701 1.1 mrg b_6 = CSWTCH01[argc - 1]; 702 1.1 mrg } 703 1.1 mrg else 704 1.1 mrg { 705 1.1 mrg a_7 = 16; 706 1.1 mrg b_7 = 1; 707 1.1 mrg } 708 1.1 mrg a_5 = PHI <a_6, a_7> 709 1.1 mrg b_b = PHI <b_6, b_7> 710 1.1 mrg 711 1.1 mrg There are further constraints. Specifically, the range of values across all 712 1.1.1.2 mrg case labels must not be bigger than param_switch_conversion_branch_ratio 713 1.1.1.2 mrg (default eight) times the number of the actual switch branches. 714 1.1 mrg 715 1.1 mrg This transformation was contributed by Martin Jambor, see this e-mail: 716 1.1 mrg http://gcc.gnu.org/ml/gcc-patches/2008-07/msg00011.html */ 717 1.1 mrg 718 1.1 mrg /* The main structure of the pass. */ 719 1.1.1.2 mrg class switch_conversion 720 1.1 mrg { 721 1.1.1.2 mrg public: 722 1.1 mrg /* Constructor. */ 723 1.1 mrg switch_conversion (); 724 1.1 mrg 725 1.1 mrg /* Destructor. */ 726 1.1 mrg ~switch_conversion (); 727 1.1 mrg 728 1.1 mrg /* The following function is invoked on every switch statement (the current 729 1.1 mrg one is given in SWTCH) and runs the individual phases of switch 730 1.1 mrg conversion on it one after another until one fails or the conversion 731 1.1 mrg is completed. On success, NULL is in m_reason, otherwise points 732 1.1 mrg to a string with the reason why the conversion failed. */ 733 1.1 mrg void expand (gswitch *swtch); 734 1.1 mrg 735 1.1 mrg /* Collection information about SWTCH statement. */ 736 1.1 mrg void collect (gswitch *swtch); 737 1.1 mrg 738 1.1 mrg /* Checks whether the range given by individual case statements of the switch 739 1.1 mrg switch statement isn't too big and whether the number of branches actually 740 1.1 mrg satisfies the size of the new array. */ 741 1.1 mrg bool check_range (); 742 1.1 mrg 743 1.1 mrg /* Checks whether all but the final BB basic blocks are empty. */ 744 1.1 mrg bool check_all_empty_except_final (); 745 1.1 mrg 746 1.1 mrg /* This function checks whether all required values in phi nodes in final_bb 747 1.1 mrg are constants. Required values are those that correspond to a basic block 748 1.1 mrg which is a part of the examined switch statement. It returns true if the 749 1.1 mrg phi nodes are OK, otherwise false. */ 750 1.1 mrg bool check_final_bb (); 751 1.1 mrg 752 1.1 mrg /* The following function allocates default_values, target_{in,out}_names and 753 1.1 mrg constructors arrays. The last one is also populated with pointers to 754 1.1 mrg vectors that will become constructors of new arrays. */ 755 1.1 mrg void create_temp_arrays (); 756 1.1 mrg 757 1.1 mrg /* Populate the array of default values in the order of phi nodes. 758 1.1 mrg DEFAULT_CASE is the CASE_LABEL_EXPR for the default switch branch 759 1.1 mrg if the range is non-contiguous or the default case has standard 760 1.1 mrg structure, otherwise it is the first non-default case instead. */ 761 1.1 mrg void gather_default_values (tree default_case); 762 1.1 mrg 763 1.1 mrg /* The following function populates the vectors in the constructors array with 764 1.1 mrg future contents of the static arrays. The vectors are populated in the 765 1.1 mrg order of phi nodes. */ 766 1.1 mrg void build_constructors (); 767 1.1 mrg 768 1.1 mrg /* If all values in the constructor vector are products of a linear function 769 1.1 mrg a * x + b, then return true. When true, COEFF_A and COEFF_B and 770 1.1 mrg coefficients of the linear function. Note that equal values are special 771 1.1 mrg case of a linear function with a and b equal to zero. */ 772 1.1 mrg bool contains_linear_function_p (vec<constructor_elt, va_gc> *vec, 773 1.1 mrg wide_int *coeff_a, wide_int *coeff_b); 774 1.1 mrg 775 1.1 mrg /* Return type which should be used for array elements, either TYPE's 776 1.1 mrg main variant or, for integral types, some smaller integral type 777 1.1 mrg that can still hold all the constants. */ 778 1.1 mrg tree array_value_type (tree type, int num); 779 1.1 mrg 780 1.1 mrg /* Create an appropriate array type and declaration and assemble a static 781 1.1 mrg array variable. Also create a load statement that initializes 782 1.1 mrg the variable in question with a value from the static array. SWTCH is 783 1.1 mrg the switch statement being converted, NUM is the index to 784 1.1 mrg arrays of constructors, default values and target SSA names 785 1.1 mrg for this particular array. ARR_INDEX_TYPE is the type of the index 786 1.1 mrg of the new array, PHI is the phi node of the final BB that corresponds 787 1.1 mrg to the value that will be loaded from the created array. TIDX 788 1.1 mrg is an ssa name of a temporary variable holding the index for loads from the 789 1.1 mrg new array. */ 790 1.1 mrg void build_one_array (int num, tree arr_index_type, 791 1.1 mrg gphi *phi, tree tidx); 792 1.1 mrg 793 1.1 mrg /* Builds and initializes static arrays initialized with values gathered from 794 1.1 mrg the switch statement. Also creates statements that load values from 795 1.1 mrg them. */ 796 1.1 mrg void build_arrays (); 797 1.1 mrg 798 1.1 mrg /* Generates and appropriately inserts loads of default values at the position 799 1.1 mrg given by GSI. Returns the last inserted statement. */ 800 1.1 mrg gassign *gen_def_assigns (gimple_stmt_iterator *gsi); 801 1.1 mrg 802 1.1 mrg /* Deletes the unused bbs and edges that now contain the switch statement and 803 1.1 mrg its empty branch bbs. BBD is the now dead BB containing 804 1.1 mrg the original switch statement, FINAL is the last BB of the converted 805 1.1 mrg switch statement (in terms of succession). */ 806 1.1 mrg void prune_bbs (basic_block bbd, basic_block final, basic_block default_bb); 807 1.1 mrg 808 1.1 mrg /* Add values to phi nodes in final_bb for the two new edges. E1F is the edge 809 1.1 mrg from the basic block loading values from an array and E2F from the basic 810 1.1 mrg block loading default values. BBF is the last switch basic block (see the 811 1.1 mrg bbf description in the comment below). */ 812 1.1 mrg void fix_phi_nodes (edge e1f, edge e2f, basic_block bbf); 813 1.1 mrg 814 1.1 mrg /* Creates a check whether the switch expression value actually falls into the 815 1.1 mrg range given by all the cases. If it does not, the temporaries are loaded 816 1.1 mrg with default values instead. */ 817 1.1 mrg void gen_inbound_check (); 818 1.1 mrg 819 1.1 mrg /* Switch statement for which switch conversion takes place. */ 820 1.1 mrg gswitch *m_switch; 821 1.1 mrg 822 1.1 mrg /* The expression used to decide the switch branch. */ 823 1.1 mrg tree m_index_expr; 824 1.1 mrg 825 1.1 mrg /* The following integer constants store the minimum and maximum value 826 1.1 mrg covered by the case labels. */ 827 1.1 mrg tree m_range_min; 828 1.1 mrg tree m_range_max; 829 1.1 mrg 830 1.1 mrg /* The difference between the above two numbers. Stored here because it 831 1.1 mrg is used in all the conversion heuristics, as well as for some of the 832 1.1 mrg transformation, and it is expensive to re-compute it all the time. */ 833 1.1 mrg tree m_range_size; 834 1.1 mrg 835 1.1 mrg /* Basic block that contains the actual GIMPLE_SWITCH. */ 836 1.1 mrg basic_block m_switch_bb; 837 1.1 mrg 838 1.1 mrg /* Basic block that is the target of the default case. */ 839 1.1 mrg basic_block m_default_bb; 840 1.1 mrg 841 1.1 mrg /* The single successor block of all branches out of the GIMPLE_SWITCH, 842 1.1 mrg if such a block exists. Otherwise NULL. */ 843 1.1 mrg basic_block m_final_bb; 844 1.1 mrg 845 1.1 mrg /* The probability of the default edge in the replaced switch. */ 846 1.1 mrg profile_probability m_default_prob; 847 1.1 mrg 848 1.1 mrg /* Number of phi nodes in the final bb (that we'll be replacing). */ 849 1.1 mrg int m_phi_count; 850 1.1 mrg 851 1.1 mrg /* Constructors of new static arrays. */ 852 1.1 mrg vec<constructor_elt, va_gc> **m_constructors; 853 1.1 mrg 854 1.1 mrg /* Array of default values, in the same order as phi nodes. */ 855 1.1 mrg tree *m_default_values; 856 1.1 mrg 857 1.1 mrg /* Array of ssa names that are initialized with a value from a new static 858 1.1 mrg array. */ 859 1.1 mrg tree *m_target_inbound_names; 860 1.1 mrg 861 1.1 mrg /* Array of ssa names that are initialized with the default value if the 862 1.1 mrg switch expression is out of range. */ 863 1.1 mrg tree *m_target_outbound_names; 864 1.1 mrg 865 1.1 mrg /* VOP SSA_NAME. */ 866 1.1 mrg tree m_target_vop; 867 1.1 mrg 868 1.1 mrg /* The first load statement that loads a temporary from a new static array. 869 1.1 mrg */ 870 1.1 mrg gimple *m_arr_ref_first; 871 1.1 mrg 872 1.1 mrg /* The last load statement that loads a temporary from a new static array. */ 873 1.1 mrg gimple *m_arr_ref_last; 874 1.1 mrg 875 1.1 mrg /* String reason why the case wasn't a good candidate that is written to the 876 1.1 mrg dump file, if there is one. */ 877 1.1 mrg const char *m_reason; 878 1.1 mrg 879 1.1 mrg /* True if default case is not used for any value between range_min and 880 1.1 mrg range_max inclusive. */ 881 1.1 mrg bool m_contiguous_range; 882 1.1 mrg 883 1.1 mrg /* True if default case does not have the required shape for other case 884 1.1 mrg labels. */ 885 1.1 mrg bool m_default_case_nonstandard; 886 1.1 mrg 887 1.1 mrg /* Number of uniq labels for non-default edges. */ 888 1.1 mrg unsigned int m_uniq; 889 1.1 mrg 890 1.1 mrg /* Count is number of non-default edges. */ 891 1.1 mrg unsigned int m_count; 892 1.1 mrg 893 1.1 mrg /* True if CFG has been changed. */ 894 1.1 mrg bool m_cfg_altered; 895 1.1 mrg }; 896 1.1 mrg 897 1.1 mrg void 898 1.1 mrg switch_decision_tree::reset_out_edges_aux (gswitch *swtch) 899 1.1 mrg { 900 1.1 mrg basic_block bb = gimple_bb (swtch); 901 1.1 mrg edge e; 902 1.1 mrg edge_iterator ei; 903 1.1 mrg FOR_EACH_EDGE (e, ei, bb->succs) 904 1.1 mrg e->aux = (void *) 0; 905 1.1 mrg } 906 1.1 mrg 907 1.1.1.3 mrg /* Release CLUSTERS vector and destruct all dynamically allocated items. */ 908 1.1.1.3 mrg 909 1.1.1.3 mrg static inline void 910 1.1.1.3 mrg release_clusters (vec<cluster *> &clusters) 911 1.1.1.3 mrg { 912 1.1.1.3 mrg for (unsigned i = 0; i < clusters.length (); i++) 913 1.1.1.3 mrg delete clusters[i]; 914 1.1.1.3 mrg clusters.release (); 915 1.1.1.3 mrg } 916 1.1.1.3 mrg 917 1.1 mrg } // tree_switch_conversion namespace 918 1.1 mrg 919 1.1 mrg #endif // TREE_SWITCH_CONVERSION_H 920