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