1 /* Tree switch conversion for GNU compiler.
2 Copyright (C) 2017-2019 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). */
50 cluster (tree case_label_expr
, basic_block case_bb
, profile_probability prob
,
51 profile_probability subtree_prob
);
58 virtual cluster_type
get_type () = 0;
60 /* Get low value covered by a cluster. */
61 virtual tree
get_low () = 0;
63 /* Get high value covered by a cluster. */
64 virtual tree
get_high () = 0;
66 /* Debug content of a cluster. */
67 virtual void debug () = 0;
69 /* Dump content of a cluster. */
70 virtual void dump (FILE *f
, bool details
= false) = 0;
72 /* Emit GIMPLE code to handle the cluster. */
73 virtual void emit (tree
, tree
, tree
, basic_block
) = 0;
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 ()
82 /* Return range of a cluster. If value would overflow in type of LOW,
84 static unsigned HOST_WIDE_INT
get_range (tree low
, tree high
)
86 tree r
= fold_build2 (MINUS_EXPR
, TREE_TYPE (low
), high
, low
);
87 if (!tree_fits_uhwi_p (r
))
90 return tree_to_uhwi (r
) + 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 struct simple_cluster
: public cluster
123 simple_cluster (tree low
, tree high
, tree case_label_expr
,
124 basic_block case_bb
, profile_probability prob
);
155 dump (FILE *f
, bool details ATTRIBUTE_UNUSED
= false)
157 PRINT_CASE (f
, get_low ());
158 if (get_low () != get_high ())
161 PRINT_CASE (f
, get_high ());
166 void emit (tree
, tree
, tree
, basic_block
)
171 bool is_single_value_p ()
173 return tree_int_cst_equal (get_low (), get_high ());
176 /* Low value of the case. */
179 /* High value of the case. */
182 /* True if case is a range. */
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
)
191 m_range_p
= m_high
!= NULL
;
196 /* Abstract subclass of jump table and bit test cluster,
197 handling a collection of simple_cluster instances. */
199 struct group_cluster
: public cluster
202 group_cluster (vec
<cluster
*> &clusters
, unsigned start
, unsigned end
);
210 return m_cases
[0]->get_low ();
216 return m_cases
[m_cases
.length () - 1]->get_high ();
225 void dump (FILE *f
, bool details
= false);
227 /* List of simple clusters handled by the group. */
228 vec
<simple_cluster
*> m_cases
;
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. */
236 struct jump_table_cluster
: public group_cluster
239 jump_table_cluster (vec
<cluster
*> &clusters
, unsigned start
, unsigned end
)
240 : group_cluster (clusters
, start
, end
)
249 void emit (tree index_expr
, tree index_type
,
250 tree default_label_expr
, basic_block default_bb
);
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
);
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
,
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
,
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);
270 /* Return whether jump table expansion is allowed. */
271 static bool is_enabled (void);
274 /* A GIMPLE switch statement can be expanded to a short sequence of bit-wise
275 comparisons. "switch(x)" is converted into "if ((1 << (x-MINVAL)) & CST)"
276 where CST and MINVAL are integer constants. This is better than a series
277 of compare-and-banch insns in some cases, e.g. we can implement:
279 if ((x==4) || (x==6) || (x==9) || (x==11))
281 as a single bit test:
283 if ((1<<x) & ((1<<4)|(1<<6)|(1<<9)|(1<<11)))
285 This transformation is only applied if the number of case targets is small,
286 if CST constains at least 3 bits, and "1 << x" is cheap. The bit tests are
287 performed in "word_mode".
289 The following example shows the code the transformation generates:
295 case '0': case '1': case '2': case '3': case '4':
296 case '5': case '6': case '7': case '8': case '9':
297 case 'A': case 'B': case 'C': case 'D': case 'E':
309 if (tmp1 > (70 - 48)) goto L2;
311 tmp3 = 0b11111100000001111111111;
312 if ((tmp2 & tmp3) != 0) goto L1 ; else goto L2;
319 TODO: There are still some improvements to this transformation that could
322 * A narrower mode than word_mode could be used if that is cheaper, e.g.
323 for x86_64 where a narrower-mode shift may result in smaller code.
325 * The compounded constant could be shifted rather than the one. The
326 test would be either on the sign bit or on the least significant bit,
327 depending on the direction of the shift. On some machines, the test
328 for the branch would be free if the bit to test is already set by the
331 This transformation was contributed by Roger Sayle, see this e-mail:
332 http://gcc.gnu.org/ml/gcc-patches/2003-01/msg01950.html
335 struct bit_test_cluster
: public group_cluster
338 bit_test_cluster (vec
<cluster
*> &clusters
, unsigned start
, unsigned end
,
339 bool handles_entire_switch
)
340 :group_cluster (clusters
, start
, end
),
341 m_handles_entire_switch (handles_entire_switch
)
350 /* Expand a switch statement by a short sequence of bit-wise
351 comparisons. "switch(x)" is effectively converted into
352 "if ((1 << (x-MINVAL)) & CST)" where CST and MINVAL are
355 INDEX_EXPR is the value being switched on.
357 MINVAL is the lowest case value of in the case nodes,
358 and RANGE is highest value minus MINVAL. MINVAL and RANGE
359 are not guaranteed to be of the same type as INDEX_EXPR
360 (the gimplifier doesn't change the type of case label values,
361 and MINVAL and RANGE are derived from those values).
362 MAXVAL is MINVAL + RANGE.
364 There *MUST* be max_case_bit_tests or less unique case
366 void emit (tree index_expr
, tree index_type
,
367 tree default_label_expr
, basic_block default_bb
);
369 /* Find bit tests of given CLUSTERS, where all members of the vector
370 are of type simple_cluster. New clusters are returned. */
371 static vec
<cluster
*> find_bit_tests (vec
<cluster
*> &clusters
);
373 /* Return true when RANGE of case values with UNIQ labels
374 can build a bit test. */
375 static bool can_be_handled (unsigned HOST_WIDE_INT range
, unsigned uniq
);
377 /* Return true when cluster starting at START and ending at END (inclusive)
378 can build a bit test. */
379 static bool can_be_handled (const vec
<cluster
*> &clusters
, unsigned start
,
382 /* Return true when COUNT of cases of UNIQ labels is beneficial for bit test
384 static bool is_beneficial (unsigned count
, unsigned uniq
);
386 /* Return true if cluster starting at START and ending at END (inclusive)
387 is profitable transformation. */
388 static bool is_beneficial (const vec
<cluster
*> &clusters
, unsigned start
,
391 /* Split the basic block at the statement pointed to by GSIP, and insert
392 a branch to the target basic block of E_TRUE conditional on tree
395 It is assumed that there is already an edge from the to-be-split
396 basic block to E_TRUE->dest block. This edge is removed, and the
397 profile information on the edge is re-used for the new conditional
400 The CFG is updated. The dominator tree will not be valid after
401 this transformation, but the immediate dominators are updated if
402 UPDATE_DOMINATORS is true.
404 Returns the newly created basic block. */
405 static basic_block
hoist_edge_and_branch_if_true (gimple_stmt_iterator
*gsip
,
408 profile_probability prob
);
410 /* True when the jump table handles an entire switch statement. */
411 bool m_handles_entire_switch
;
413 /* Maximum number of different basic blocks that can be handled by
415 static const int m_max_case_bit_tests
= 3;
418 /* Helper struct to find minimal clusters. */
420 struct min_cluster_item
423 min_cluster_item (unsigned count
, unsigned start
, unsigned non_jt_cases
):
424 m_count (count
), m_start (start
), m_non_jt_cases (non_jt_cases
)
427 /* Count of clusters. */
430 /* Index where is cluster boundary. */
433 /* Total number of cases that will not be in a jump table. */
434 unsigned m_non_jt_cases
;
437 /* Helper struct to represent switch decision tree. */
439 struct case_tree_node
441 /* Empty Constructor. */
444 /* Return true when it has a child. */
447 return m_left
!= NULL
|| m_right
!= NULL
;
450 /* Left son in binary tree. */
451 case_tree_node
*m_left
;
453 /* Right son in binary tree; also node chain. */
454 case_tree_node
*m_right
;
456 /* Parent of node in binary tree. */
457 case_tree_node
*m_parent
;
459 /* Cluster represented by this tree node. */
464 case_tree_node::case_tree_node ():
465 m_left (NULL
), m_right (NULL
), m_parent (NULL
), m_c (NULL
)
470 jump_table_cluster::case_values_threshold (void)
472 unsigned int threshold
= PARAM_VALUE (PARAM_CASE_VALUES_THRESHOLD
);
475 threshold
= targetm
.case_values_threshold ();
480 /* Return whether jump table expansion is allowed. */
481 bool jump_table_cluster::is_enabled (void)
483 /* If neither casesi or tablejump is available, or flag_jump_tables
484 over-ruled us, we really have no choice. */
485 if (!targetm
.have_casesi () && !targetm
.have_tablejump ())
487 if (!flag_jump_tables
)
489 #ifndef ASM_OUTPUT_ADDR_DIFF_ELT
497 /* A case_bit_test represents a set of case nodes that may be
498 selected from using a bit-wise comparison. HI and LO hold
499 the integer to be tested against, TARGET_EDGE contains the
500 edge to the basic block to jump to upon success and BITS
501 counts the number of case nodes handled by this test,
502 typically the number of bits set in HI:LO. The LABEL field
503 is used to quickly identify all cases in this set without
504 looking at label_to_block for every case label. */
509 basic_block target_bb
;
513 /* Comparison function for qsort to order bit tests by decreasing
514 probability of execution. */
515 static int cmp (const void *p1
, const void *p2
);
518 struct switch_decision_tree
521 switch_decision_tree (gswitch
*swtch
): m_switch (swtch
), m_phi_mapping (),
522 m_case_bbs (), m_case_node_pool ("struct case_node pool"),
527 /* Analyze switch statement and return true when the statement is expanded
529 bool analyze_switch_statement ();
531 /* Attempt to expand CLUSTERS as a decision tree. Return true when
533 bool try_switch_expansion (vec
<cluster
*> &clusters
);
534 /* Compute the number of case labels that correspond to each outgoing edge of
535 switch statement. Record this information in the aux field of the edge.
537 void compute_cases_per_edge ();
539 /* Before switch transformation, record all SSA_NAMEs defined in switch BB
540 and used in a label basic block. */
541 void record_phi_operand_mapping ();
543 /* Append new operands to PHI statements that were introduced due to
544 addition of new edges to case labels. */
545 void fix_phi_operands_for_edges ();
547 /* Generate a decision tree, switching on INDEX_EXPR and jumping to
548 one of the labels in CASE_LIST or to the DEFAULT_LABEL.
550 We generate a binary decision tree to select the appropriate target
552 void emit (basic_block bb
, tree index_expr
,
553 profile_probability default_prob
, tree index_type
);
555 /* Emit step-by-step code to select a case for the value of INDEX.
556 The thus generated decision tree follows the form of the
557 case-node binary tree NODE, whose nodes represent test conditions.
558 DEFAULT_PROB is probability of cases leading to default BB.
559 INDEX_TYPE is the type of the index of the switch. */
560 basic_block
emit_case_nodes (basic_block bb
, tree index
,
561 case_tree_node
*node
,
562 profile_probability default_prob
,
563 tree index_type
, location_t
);
565 /* Take an ordered list of case nodes
566 and transform them into a near optimal binary tree,
567 on the assumption that any target code selection value is as
570 The transformation is performed by splitting the ordered
571 list into two equal sections plus a pivot. The parts are
572 then attached to the pivot as left and right branches. Each
573 branch is then transformed recursively. */
574 static void balance_case_nodes (case_tree_node
**head
,
575 case_tree_node
*parent
);
577 /* Dump ROOT, a list or tree of case nodes, to file F. */
578 static void dump_case_nodes (FILE *f
, case_tree_node
*root
, int indent_step
,
581 /* Add an unconditional jump to CASE_BB that happens in basic block BB. */
582 static void emit_jump (basic_block bb
, basic_block case_bb
);
584 /* Generate code to compare OP0 with OP1 so that the condition codes are
585 set and to jump to LABEL_BB if the condition is true.
586 COMPARISON is the GIMPLE comparison (EQ, NE, GT, etc.).
587 PROB is the probability of jumping to LABEL_BB. */
588 static basic_block
emit_cmp_and_jump_insns (basic_block bb
, tree op0
,
589 tree op1
, tree_code comparison
,
590 basic_block label_bb
,
591 profile_probability prob
,
594 /* Generate code to jump to LABEL if OP0 and OP1 are equal in mode MODE.
595 PROB is the probability of jumping to LABEL_BB. */
596 static basic_block
do_jump_if_equal (basic_block bb
, tree op0
, tree op1
,
597 basic_block label_bb
,
598 profile_probability prob
,
601 /* Reset the aux field of all outgoing edges of switch basic block. */
602 static inline void reset_out_edges_aux (gswitch
*swtch
);
604 /* Switch statement. */
607 /* Map of PHI nodes that have to be fixed after expansion. */
608 hash_map
<tree
, tree
> m_phi_mapping
;
610 /* List of basic blocks that belong to labels of the switch. */
611 auto_vec
<basic_block
> m_case_bbs
;
613 /* Basic block with default label. */
614 basic_block m_default_bb
;
616 /* A pool for case nodes. */
617 object_allocator
<case_tree_node
> m_case_node_pool
;
619 /* Balanced tree of case nodes. */
620 case_tree_node
*m_case_list
;
624 Switch initialization conversion
626 The following pass changes simple initializations of scalars in a switch
627 statement into initializations from a static array. Obviously, the values
628 must be constant and known at compile time and a default branch must be
629 provided. For example, the following code:
653 a_5 = PHI <a_1, a_2, a_3, a_4>
654 b_5 = PHI <b_1, b_2, b_3, b_4>
659 static const int = CSWTCH01[] = {6, 6, 5, 1, 1, 1, 1, 1, 1, 1, 1, 4};
660 static const int = CSWTCH02[] = {8, 8, 9, 16, 16, 16, 16, 16, 16, 16,
663 if (((unsigned) argc) - 1 < 11)
665 a_6 = CSWTCH02[argc - 1];
666 b_6 = CSWTCH01[argc - 1];
676 There are further constraints. Specifically, the range of values across all
677 case labels must not be bigger than SWITCH_CONVERSION_BRANCH_RATIO (default
678 eight) times the number of the actual switch branches.
680 This transformation was contributed by Martin Jambor, see this e-mail:
681 http://gcc.gnu.org/ml/gcc-patches/2008-07/msg00011.html */
683 /* The main structure of the pass. */
684 struct switch_conversion
687 switch_conversion ();
690 ~switch_conversion ();
692 /* The following function is invoked on every switch statement (the current
693 one is given in SWTCH) and runs the individual phases of switch
694 conversion on it one after another until one fails or the conversion
695 is completed. On success, NULL is in m_reason, otherwise points
696 to a string with the reason why the conversion failed. */
697 void expand (gswitch
*swtch
);
699 /* Collection information about SWTCH statement. */
700 void collect (gswitch
*swtch
);
702 /* Checks whether the range given by individual case statements of the switch
703 switch statement isn't too big and whether the number of branches actually
704 satisfies the size of the new array. */
707 /* Checks whether all but the final BB basic blocks are empty. */
708 bool check_all_empty_except_final ();
710 /* This function checks whether all required values in phi nodes in final_bb
711 are constants. Required values are those that correspond to a basic block
712 which is a part of the examined switch statement. It returns true if the
713 phi nodes are OK, otherwise false. */
714 bool check_final_bb ();
716 /* The following function allocates default_values, target_{in,out}_names and
717 constructors arrays. The last one is also populated with pointers to
718 vectors that will become constructors of new arrays. */
719 void create_temp_arrays ();
721 /* Populate the array of default values in the order of phi nodes.
722 DEFAULT_CASE is the CASE_LABEL_EXPR for the default switch branch
723 if the range is non-contiguous or the default case has standard
724 structure, otherwise it is the first non-default case instead. */
725 void gather_default_values (tree default_case
);
727 /* The following function populates the vectors in the constructors array with
728 future contents of the static arrays. The vectors are populated in the
729 order of phi nodes. */
730 void build_constructors ();
732 /* If all values in the constructor vector are products of a linear function
733 a * x + b, then return true. When true, COEFF_A and COEFF_B and
734 coefficients of the linear function. Note that equal values are special
735 case of a linear function with a and b equal to zero. */
736 bool contains_linear_function_p (vec
<constructor_elt
, va_gc
> *vec
,
737 wide_int
*coeff_a
, wide_int
*coeff_b
);
739 /* Return type which should be used for array elements, either TYPE's
740 main variant or, for integral types, some smaller integral type
741 that can still hold all the constants. */
742 tree
array_value_type (tree type
, int num
);
744 /* Create an appropriate array type and declaration and assemble a static
745 array variable. Also create a load statement that initializes
746 the variable in question with a value from the static array. SWTCH is
747 the switch statement being converted, NUM is the index to
748 arrays of constructors, default values and target SSA names
749 for this particular array. ARR_INDEX_TYPE is the type of the index
750 of the new array, PHI is the phi node of the final BB that corresponds
751 to the value that will be loaded from the created array. TIDX
752 is an ssa name of a temporary variable holding the index for loads from the
754 void build_one_array (int num
, tree arr_index_type
,
755 gphi
*phi
, tree tidx
);
757 /* Builds and initializes static arrays initialized with values gathered from
758 the switch statement. Also creates statements that load values from
760 void build_arrays ();
762 /* Generates and appropriately inserts loads of default values at the position
763 given by GSI. Returns the last inserted statement. */
764 gassign
*gen_def_assigns (gimple_stmt_iterator
*gsi
);
766 /* Deletes the unused bbs and edges that now contain the switch statement and
767 its empty branch bbs. BBD is the now dead BB containing
768 the original switch statement, FINAL is the last BB of the converted
769 switch statement (in terms of succession). */
770 void prune_bbs (basic_block bbd
, basic_block final
, basic_block default_bb
);
772 /* Add values to phi nodes in final_bb for the two new edges. E1F is the edge
773 from the basic block loading values from an array and E2F from the basic
774 block loading default values. BBF is the last switch basic block (see the
775 bbf description in the comment below). */
776 void fix_phi_nodes (edge e1f
, edge e2f
, basic_block bbf
);
778 /* Creates a check whether the switch expression value actually falls into the
779 range given by all the cases. If it does not, the temporaries are loaded
780 with default values instead. */
781 void gen_inbound_check ();
783 /* Switch statement for which switch conversion takes place. */
786 /* The expression used to decide the switch branch. */
789 /* The following integer constants store the minimum and maximum value
790 covered by the case labels. */
794 /* The difference between the above two numbers. Stored here because it
795 is used in all the conversion heuristics, as well as for some of the
796 transformation, and it is expensive to re-compute it all the time. */
799 /* Basic block that contains the actual GIMPLE_SWITCH. */
800 basic_block m_switch_bb
;
802 /* Basic block that is the target of the default case. */
803 basic_block m_default_bb
;
805 /* The single successor block of all branches out of the GIMPLE_SWITCH,
806 if such a block exists. Otherwise NULL. */
807 basic_block m_final_bb
;
809 /* The probability of the default edge in the replaced switch. */
810 profile_probability m_default_prob
;
812 /* The count of the default edge in the replaced switch. */
813 profile_count m_default_count
;
815 /* Combined count of all other (non-default) edges in the replaced switch. */
816 profile_count m_other_count
;
818 /* Number of phi nodes in the final bb (that we'll be replacing). */
821 /* Constructors of new static arrays. */
822 vec
<constructor_elt
, va_gc
> **m_constructors
;
824 /* Array of default values, in the same order as phi nodes. */
825 tree
*m_default_values
;
827 /* Array of ssa names that are initialized with a value from a new static
829 tree
*m_target_inbound_names
;
831 /* Array of ssa names that are initialized with the default value if the
832 switch expression is out of range. */
833 tree
*m_target_outbound_names
;
838 /* The first load statement that loads a temporary from a new static array.
840 gimple
*m_arr_ref_first
;
842 /* The last load statement that loads a temporary from a new static array. */
843 gimple
*m_arr_ref_last
;
845 /* String reason why the case wasn't a good candidate that is written to the
846 dump file, if there is one. */
847 const char *m_reason
;
849 /* True if default case is not used for any value between range_min and
850 range_max inclusive. */
851 bool m_contiguous_range
;
853 /* True if default case does not have the required shape for other case
855 bool m_default_case_nonstandard
;
857 /* Number of uniq labels for non-default edges. */
860 /* Count is number of non-default edges. */
861 unsigned int m_count
;
863 /* True if CFG has been changed. */
868 switch_decision_tree::reset_out_edges_aux (gswitch
*swtch
)
870 basic_block bb
= gimple_bb (swtch
);
873 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
877 } // tree_switch_conversion namespace
879 #endif // TREE_SWITCH_CONVERSION_H