1 /* Tree switch conversion for GNU compiler.
2 Copyright (C) 2017-2021 Free Software Foundation, Inc.
4 This file is part of GCC.
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
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
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/>. */
20 #ifndef TREE_SWITCH_CONVERSION_H
21 #define TREE_SWITCH_CONVERSION_H
23 namespace tree_switch_conversion
{
25 /* Type of cluster. */
34 #define PRINT_CASE(f,c) print_generic_expr (f, c)
36 /* Abstract base class for representing a cluster of cases.
38 Here is the inheritance hierarachy, and the enum_cluster_type
39 values for the concrete subclasses:
42 |-simple_cluster (SIMPLE_CASE)
44 |-jump_table_cluster (JUMP_TABLE)
45 `-bit_test_cluster (BIT_TEST). */
51 inline cluster (tree case_label_expr
, basic_block case_bb
,
52 profile_probability prob
, profile_probability subtree_prob
);
59 virtual cluster_type
get_type () = 0;
61 /* Get low value covered by a cluster. */
62 virtual tree
get_low () = 0;
64 /* Get high value covered by a cluster. */
65 virtual tree
get_high () = 0;
67 /* Debug content of a cluster. */
68 virtual void debug () = 0;
70 /* Dump content of a cluster. */
71 virtual void dump (FILE *f
, bool details
= false) = 0;
73 /* Emit GIMPLE code to handle the cluster. */
74 virtual void emit (tree
, tree
, tree
, basic_block
, location_t
) = 0;
76 /* Return true if a cluster handles only a single case value and the
77 value is not a range. */
78 virtual bool is_single_value_p ()
83 /* Return range of a cluster. If value would overflow in type of LOW,
85 static unsigned HOST_WIDE_INT
get_range (tree low
, tree high
)
87 wide_int w
= wi::to_wide (high
) - wi::to_wide (low
);
88 if (wi::neg_p (w
, TYPE_SIGN (TREE_TYPE (low
))) || !wi::fits_uhwi_p (w
))
90 return w
.to_uhwi () + 1;
94 tree m_case_label_expr
;
96 /* Basic block of the case. */
97 basic_block m_case_bb
;
99 /* Probability of taking this cluster. */
100 profile_probability m_prob
;
102 /* Probability of reaching subtree rooted at this node. */
103 profile_probability m_subtree_prob
;
106 /* Default constructor. */
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
)
117 /* Subclass of cluster representing a simple contiguous range
120 class simple_cluster
: public cluster
124 inline simple_cluster (tree low
, tree high
, tree case_label_expr
,
125 basic_block case_bb
, profile_probability prob
,
126 bool has_forward_bb
= false);
150 void set_high (tree high
)
162 dump (FILE *f
, bool details ATTRIBUTE_UNUSED
= false)
164 PRINT_CASE (f
, get_low ());
165 if (get_low () != get_high ())
168 PRINT_CASE (f
, get_high ());
173 void emit (tree
, tree
, tree
, basic_block
, location_t
)
178 bool is_single_value_p ()
180 return tree_int_cst_equal (get_low (), get_high ());
183 /* Low value of the case. */
186 /* High value of the case. */
189 /* True if case is a range. */
192 /* True if the case will use a forwarder BB. */
193 bool m_has_forward_bb
;
196 simple_cluster::simple_cluster (tree low
, tree high
, tree case_label_expr
,
197 basic_block case_bb
, profile_probability prob
,
198 bool has_forward_bb
):
199 cluster (case_label_expr
, case_bb
, prob
, prob
),
200 m_low (low
), m_high (high
), m_has_forward_bb (has_forward_bb
)
202 m_range_p
= m_high
!= NULL
;
207 /* Abstract subclass of jump table and bit test cluster,
208 handling a collection of simple_cluster instances. */
210 class group_cluster
: public cluster
214 group_cluster (vec
<cluster
*> &clusters
, unsigned start
, unsigned end
);
222 return m_cases
[0]->get_low ();
228 return m_cases
[m_cases
.length () - 1]->get_high ();
237 void dump (FILE *f
, bool details
= false);
239 /* List of simple clusters handled by the group. */
240 vec
<simple_cluster
*> m_cases
;
243 /* Concrete subclass of group_cluster representing a collection
244 of cases to be implemented as a jump table.
245 The "emit" vfunc gernerates a nested switch statement which
246 is later lowered to a jump table. */
248 class jump_table_cluster
: public group_cluster
252 jump_table_cluster (vec
<cluster
*> &clusters
, unsigned start
, unsigned end
)
253 : group_cluster (clusters
, start
, end
)
262 void emit (tree index_expr
, tree index_type
,
263 tree default_label_expr
, basic_block default_bb
, location_t loc
);
265 /* Find jump tables of given CLUSTERS, where all members of the vector
266 are of type simple_cluster. New clusters are returned. */
267 static vec
<cluster
*> find_jump_tables (vec
<cluster
*> &clusters
);
269 /* Return true when cluster starting at START and ending at END (inclusive)
270 can build a jump-table. */
271 static bool can_be_handled (const vec
<cluster
*> &clusters
, unsigned start
,
274 /* Return true if cluster starting at START and ending at END (inclusive)
275 is profitable transformation. */
276 static bool is_beneficial (const vec
<cluster
*> &clusters
, unsigned start
,
279 /* Return the smallest number of different values for which it is best
280 to use a jump-table instead of a tree of conditional branches. */
281 static inline unsigned int case_values_threshold (void);
283 /* Return whether jump table expansion is allowed. */
284 static inline bool is_enabled (void);
287 /* A GIMPLE switch statement can be expanded to a short sequence of bit-wise
288 comparisons. "switch(x)" is converted into "if ((1 << (x-MINVAL)) & CST)"
289 where CST and MINVAL are integer constants. This is better than a series
290 of compare-and-banch insns in some cases, e.g. we can implement:
292 if ((x==4) || (x==6) || (x==9) || (x==11))
294 as a single bit test:
296 if ((1<<x) & ((1<<4)|(1<<6)|(1<<9)|(1<<11)))
298 This transformation is only applied if the number of case targets is small,
299 if CST constains at least 3 bits, and "1 << x" is cheap. The bit tests are
300 performed in "word_mode".
302 The following example shows the code the transformation generates:
308 case '0': case '1': case '2': case '3': case '4':
309 case '5': case '6': case '7': case '8': case '9':
310 case 'A': case 'B': case 'C': case 'D': case 'E':
322 if (tmp1 > (70 - 48)) goto L2;
324 tmp3 = 0b11111100000001111111111;
325 if ((tmp2 & tmp3) != 0) goto L1 ; else goto L2;
332 TODO: There are still some improvements to this transformation that could
335 * A narrower mode than word_mode could be used if that is cheaper, e.g.
336 for x86_64 where a narrower-mode shift may result in smaller code.
338 * The compounded constant could be shifted rather than the one. The
339 test would be either on the sign bit or on the least significant bit,
340 depending on the direction of the shift. On some machines, the test
341 for the branch would be free if the bit to test is already set by the
344 This transformation was contributed by Roger Sayle, see this e-mail:
345 http://gcc.gnu.org/ml/gcc-patches/2003-01/msg01950.html
348 class bit_test_cluster
: public group_cluster
352 bit_test_cluster (vec
<cluster
*> &clusters
, unsigned start
, unsigned end
,
353 bool handles_entire_switch
)
354 :group_cluster (clusters
, start
, end
),
355 m_handles_entire_switch (handles_entire_switch
)
364 /* Expand a switch statement by a short sequence of bit-wise
365 comparisons. "switch(x)" is effectively converted into
366 "if ((1 << (x-MINVAL)) & CST)" where CST and MINVAL are
369 INDEX_EXPR is the value being switched on.
371 MINVAL is the lowest case value of in the case nodes,
372 and RANGE is highest value minus MINVAL. MINVAL and RANGE
373 are not guaranteed to be of the same type as INDEX_EXPR
374 (the gimplifier doesn't change the type of case label values,
375 and MINVAL and RANGE are derived from those values).
376 MAXVAL is MINVAL + RANGE.
378 There *MUST* be max_case_bit_tests or less unique case
380 void emit (tree index_expr
, tree index_type
,
381 tree default_label_expr
, basic_block default_bb
, location_t loc
);
383 /* Find bit tests of given CLUSTERS, where all members of the vector
384 are of type simple_cluster. New clusters are returned. */
385 static vec
<cluster
*> find_bit_tests (vec
<cluster
*> &clusters
);
387 /* Return true when RANGE of case values with UNIQ labels
388 can build a bit test. */
389 static bool can_be_handled (unsigned HOST_WIDE_INT range
, unsigned uniq
);
391 /* Return true when cluster starting at START and ending at END (inclusive)
392 can build a bit test. */
393 static bool can_be_handled (const vec
<cluster
*> &clusters
, unsigned start
,
396 /* Return true when COUNT of cases of UNIQ labels is beneficial for bit test
398 static bool is_beneficial (unsigned count
, unsigned uniq
);
400 /* Return true if cluster starting at START and ending at END (inclusive)
401 is profitable transformation. */
402 static bool is_beneficial (const vec
<cluster
*> &clusters
, unsigned start
,
405 /* Split the basic block at the statement pointed to by GSIP, and insert
406 a branch to the target basic block of E_TRUE conditional on tree
409 It is assumed that there is already an edge from the to-be-split
410 basic block to E_TRUE->dest block. This edge is removed, and the
411 profile information on the edge is re-used for the new conditional
414 The CFG is updated. The dominator tree will not be valid after
415 this transformation, but the immediate dominators are updated if
416 UPDATE_DOMINATORS is true.
418 Returns the newly created basic block. */
419 static basic_block
hoist_edge_and_branch_if_true (gimple_stmt_iterator
*gsip
,
422 profile_probability prob
);
424 /* Return whether bit test expansion is allowed. */
425 static inline bool is_enabled (void)
427 return flag_bit_tests
;
430 /* True when the jump table handles an entire switch statement. */
431 bool m_handles_entire_switch
;
433 /* Maximum number of different basic blocks that can be handled by
435 static const int m_max_case_bit_tests
= 3;
438 /* Helper struct to find minimal clusters. */
440 class min_cluster_item
444 min_cluster_item (unsigned count
, unsigned start
, unsigned non_jt_cases
):
445 m_count (count
), m_start (start
), m_non_jt_cases (non_jt_cases
)
448 /* Count of clusters. */
451 /* Index where is cluster boundary. */
454 /* Total number of cases that will not be in a jump table. */
455 unsigned m_non_jt_cases
;
458 /* Helper struct to represent switch decision tree. */
463 /* Empty Constructor. */
466 /* Return true when it has a child. */
469 return m_left
!= NULL
|| m_right
!= NULL
;
472 /* Left son in binary tree. */
473 case_tree_node
*m_left
;
475 /* Right son in binary tree; also node chain. */
476 case_tree_node
*m_right
;
478 /* Parent of node in binary tree. */
479 case_tree_node
*m_parent
;
481 /* Cluster represented by this tree node. */
486 case_tree_node::case_tree_node ():
487 m_left (NULL
), m_right (NULL
), m_parent (NULL
), m_c (NULL
)
492 jump_table_cluster::case_values_threshold (void)
494 unsigned int threshold
= param_case_values_threshold
;
497 threshold
= targetm
.case_values_threshold ();
502 /* Return whether jump table expansion is allowed. */
503 bool jump_table_cluster::is_enabled (void)
505 /* If neither casesi or tablejump is available, or flag_jump_tables
506 over-ruled us, we really have no choice. */
507 if (!targetm
.have_casesi () && !targetm
.have_tablejump ())
509 if (!flag_jump_tables
)
511 #ifndef ASM_OUTPUT_ADDR_DIFF_ELT
519 /* A case_bit_test represents a set of case nodes that may be
520 selected from using a bit-wise comparison. HI and LO hold
521 the integer to be tested against, TARGET_EDGE contains the
522 edge to the basic block to jump to upon success and BITS
523 counts the number of case nodes handled by this test,
524 typically the number of bits set in HI:LO. The LABEL field
525 is used to quickly identify all cases in this set without
526 looking at label_to_block for every case label. */
532 basic_block target_bb
;
536 /* Comparison function for qsort to order bit tests by decreasing
537 probability of execution. */
538 static int cmp (const void *p1
, const void *p2
);
541 class switch_decision_tree
545 switch_decision_tree (gswitch
*swtch
): m_switch (swtch
), m_phi_mapping (),
546 m_case_bbs (), m_case_node_pool ("struct case_node pool"),
551 /* Analyze switch statement and return true when the statement is expanded
553 bool analyze_switch_statement ();
555 /* Attempt to expand CLUSTERS as a decision tree. Return true when
557 bool try_switch_expansion (vec
<cluster
*> &clusters
);
558 /* Compute the number of case labels that correspond to each outgoing edge of
559 switch statement. Record this information in the aux field of the edge.
561 void compute_cases_per_edge ();
563 /* Before switch transformation, record all SSA_NAMEs defined in switch BB
564 and used in a label basic block. */
565 void record_phi_operand_mapping ();
567 /* Append new operands to PHI statements that were introduced due to
568 addition of new edges to case labels. */
569 void fix_phi_operands_for_edges ();
571 /* Generate a decision tree, switching on INDEX_EXPR and jumping to
572 one of the labels in CASE_LIST or to the DEFAULT_LABEL.
574 We generate a binary decision tree to select the appropriate target
576 void emit (basic_block bb
, tree index_expr
,
577 profile_probability default_prob
, tree index_type
);
579 /* Emit step-by-step code to select a case for the value of INDEX.
580 The thus generated decision tree follows the form of the
581 case-node binary tree NODE, whose nodes represent test conditions.
582 DEFAULT_PROB is probability of cases leading to default BB.
583 INDEX_TYPE is the type of the index of the switch. */
584 basic_block
emit_case_nodes (basic_block bb
, tree index
,
585 case_tree_node
*node
,
586 profile_probability default_prob
,
587 tree index_type
, location_t
);
589 /* Take an ordered list of case nodes
590 and transform them into a near optimal binary tree,
591 on the assumption that any target code selection value is as
594 The transformation is performed by splitting the ordered
595 list into two equal sections plus a pivot. The parts are
596 then attached to the pivot as left and right branches. Each
597 branch is then transformed recursively. */
598 static void balance_case_nodes (case_tree_node
**head
,
599 case_tree_node
*parent
);
601 /* Dump ROOT, a list or tree of case nodes, to file F. */
602 static void dump_case_nodes (FILE *f
, case_tree_node
*root
, int indent_step
,
605 /* Add an unconditional jump to CASE_BB that happens in basic block BB. */
606 static void emit_jump (basic_block bb
, basic_block case_bb
);
608 /* Generate code to compare OP0 with OP1 so that the condition codes are
609 set and to jump to LABEL_BB if the condition is true.
610 COMPARISON is the GIMPLE comparison (EQ, NE, GT, etc.).
611 PROB is the probability of jumping to LABEL_BB. */
612 static basic_block
emit_cmp_and_jump_insns (basic_block bb
, tree op0
,
613 tree op1
, tree_code comparison
,
614 basic_block label_bb
,
615 profile_probability prob
,
618 /* Generate code to jump to LABEL if OP0 and OP1 are equal in mode MODE.
619 PROB is the probability of jumping to LABEL_BB. */
620 static basic_block
do_jump_if_equal (basic_block bb
, tree op0
, tree op1
,
621 basic_block label_bb
,
622 profile_probability prob
,
625 /* Reset the aux field of all outgoing edges of switch basic block. */
626 static inline void reset_out_edges_aux (gswitch
*swtch
);
628 /* Switch statement. */
631 /* Map of PHI nodes that have to be fixed after expansion. */
632 hash_map
<tree
, tree
> m_phi_mapping
;
634 /* List of basic blocks that belong to labels of the switch. */
635 auto_vec
<basic_block
> m_case_bbs
;
637 /* Basic block with default label. */
638 basic_block m_default_bb
;
640 /* A pool for case nodes. */
641 object_allocator
<case_tree_node
> m_case_node_pool
;
643 /* Balanced tree of case nodes. */
644 case_tree_node
*m_case_list
;
648 Switch initialization conversion
650 The following pass changes simple initializations of scalars in a switch
651 statement into initializations from a static array. Obviously, the values
652 must be constant and known at compile time and a default branch must be
653 provided. For example, the following code:
677 a_5 = PHI <a_1, a_2, a_3, a_4>
678 b_5 = PHI <b_1, b_2, b_3, b_4>
683 static const int = CSWTCH01[] = {6, 6, 5, 1, 1, 1, 1, 1, 1, 1, 1, 4};
684 static const int = CSWTCH02[] = {8, 8, 9, 16, 16, 16, 16, 16, 16, 16,
687 if (((unsigned) argc) - 1 < 11)
689 a_6 = CSWTCH02[argc - 1];
690 b_6 = CSWTCH01[argc - 1];
700 There are further constraints. Specifically, the range of values across all
701 case labels must not be bigger than param_switch_conversion_branch_ratio
702 (default eight) times the number of the actual switch branches.
704 This transformation was contributed by Martin Jambor, see this e-mail:
705 http://gcc.gnu.org/ml/gcc-patches/2008-07/msg00011.html */
707 /* The main structure of the pass. */
708 class switch_conversion
712 switch_conversion ();
715 ~switch_conversion ();
717 /* The following function is invoked on every switch statement (the current
718 one is given in SWTCH) and runs the individual phases of switch
719 conversion on it one after another until one fails or the conversion
720 is completed. On success, NULL is in m_reason, otherwise points
721 to a string with the reason why the conversion failed. */
722 void expand (gswitch
*swtch
);
724 /* Collection information about SWTCH statement. */
725 void collect (gswitch
*swtch
);
727 /* Checks whether the range given by individual case statements of the switch
728 switch statement isn't too big and whether the number of branches actually
729 satisfies the size of the new array. */
732 /* Checks whether all but the final BB basic blocks are empty. */
733 bool check_all_empty_except_final ();
735 /* This function checks whether all required values in phi nodes in final_bb
736 are constants. Required values are those that correspond to a basic block
737 which is a part of the examined switch statement. It returns true if the
738 phi nodes are OK, otherwise false. */
739 bool check_final_bb ();
741 /* The following function allocates default_values, target_{in,out}_names and
742 constructors arrays. The last one is also populated with pointers to
743 vectors that will become constructors of new arrays. */
744 void create_temp_arrays ();
746 /* Populate the array of default values in the order of phi nodes.
747 DEFAULT_CASE is the CASE_LABEL_EXPR for the default switch branch
748 if the range is non-contiguous or the default case has standard
749 structure, otherwise it is the first non-default case instead. */
750 void gather_default_values (tree default_case
);
752 /* The following function populates the vectors in the constructors array with
753 future contents of the static arrays. The vectors are populated in the
754 order of phi nodes. */
755 void build_constructors ();
757 /* If all values in the constructor vector are products of a linear function
758 a * x + b, then return true. When true, COEFF_A and COEFF_B and
759 coefficients of the linear function. Note that equal values are special
760 case of a linear function with a and b equal to zero. */
761 bool contains_linear_function_p (vec
<constructor_elt
, va_gc
> *vec
,
762 wide_int
*coeff_a
, wide_int
*coeff_b
);
764 /* Return type which should be used for array elements, either TYPE's
765 main variant or, for integral types, some smaller integral type
766 that can still hold all the constants. */
767 tree
array_value_type (tree type
, int num
);
769 /* Create an appropriate array type and declaration and assemble a static
770 array variable. Also create a load statement that initializes
771 the variable in question with a value from the static array. SWTCH is
772 the switch statement being converted, NUM is the index to
773 arrays of constructors, default values and target SSA names
774 for this particular array. ARR_INDEX_TYPE is the type of the index
775 of the new array, PHI is the phi node of the final BB that corresponds
776 to the value that will be loaded from the created array. TIDX
777 is an ssa name of a temporary variable holding the index for loads from the
779 void build_one_array (int num
, tree arr_index_type
,
780 gphi
*phi
, tree tidx
);
782 /* Builds and initializes static arrays initialized with values gathered from
783 the switch statement. Also creates statements that load values from
785 void build_arrays ();
787 /* Generates and appropriately inserts loads of default values at the position
788 given by GSI. Returns the last inserted statement. */
789 gassign
*gen_def_assigns (gimple_stmt_iterator
*gsi
);
791 /* Deletes the unused bbs and edges that now contain the switch statement and
792 its empty branch bbs. BBD is the now dead BB containing
793 the original switch statement, FINAL is the last BB of the converted
794 switch statement (in terms of succession). */
795 void prune_bbs (basic_block bbd
, basic_block final
, basic_block default_bb
);
797 /* Add values to phi nodes in final_bb for the two new edges. E1F is the edge
798 from the basic block loading values from an array and E2F from the basic
799 block loading default values. BBF is the last switch basic block (see the
800 bbf description in the comment below). */
801 void fix_phi_nodes (edge e1f
, edge e2f
, basic_block bbf
);
803 /* Creates a check whether the switch expression value actually falls into the
804 range given by all the cases. If it does not, the temporaries are loaded
805 with default values instead. */
806 void gen_inbound_check ();
808 /* Switch statement for which switch conversion takes place. */
811 /* The expression used to decide the switch branch. */
814 /* The following integer constants store the minimum and maximum value
815 covered by the case labels. */
819 /* The difference between the above two numbers. Stored here because it
820 is used in all the conversion heuristics, as well as for some of the
821 transformation, and it is expensive to re-compute it all the time. */
824 /* Basic block that contains the actual GIMPLE_SWITCH. */
825 basic_block m_switch_bb
;
827 /* Basic block that is the target of the default case. */
828 basic_block m_default_bb
;
830 /* The single successor block of all branches out of the GIMPLE_SWITCH,
831 if such a block exists. Otherwise NULL. */
832 basic_block m_final_bb
;
834 /* The probability of the default edge in the replaced switch. */
835 profile_probability m_default_prob
;
837 /* Number of phi nodes in the final bb (that we'll be replacing). */
840 /* Constructors of new static arrays. */
841 vec
<constructor_elt
, va_gc
> **m_constructors
;
843 /* Array of default values, in the same order as phi nodes. */
844 tree
*m_default_values
;
846 /* Array of ssa names that are initialized with a value from a new static
848 tree
*m_target_inbound_names
;
850 /* Array of ssa names that are initialized with the default value if the
851 switch expression is out of range. */
852 tree
*m_target_outbound_names
;
857 /* The first load statement that loads a temporary from a new static array.
859 gimple
*m_arr_ref_first
;
861 /* The last load statement that loads a temporary from a new static array. */
862 gimple
*m_arr_ref_last
;
864 /* String reason why the case wasn't a good candidate that is written to the
865 dump file, if there is one. */
866 const char *m_reason
;
868 /* True if default case is not used for any value between range_min and
869 range_max inclusive. */
870 bool m_contiguous_range
;
872 /* True if default case does not have the required shape for other case
874 bool m_default_case_nonstandard
;
876 /* Number of uniq labels for non-default edges. */
879 /* Count is number of non-default edges. */
880 unsigned int m_count
;
882 /* True if CFG has been changed. */
887 switch_decision_tree::reset_out_edges_aux (gswitch
*swtch
)
889 basic_block bb
= gimple_bb (swtch
);
892 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
896 /* Release CLUSTERS vector and destruct all dynamically allocated items. */
899 release_clusters (vec
<cluster
*> &clusters
)
901 for (unsigned i
= 0; i
< clusters
.length (); i
++)
906 } // tree_switch_conversion namespace
908 #endif // TREE_SWITCH_CONVERSION_H