1 /* Tree switch conversion for GNU compiler.
2 Copyright (C) 2017 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 range of a cluster. If value would overflow in type of LOW,
77 static unsigned HOST_WIDE_INT
get_range (tree low
, tree high
)
79 tree r
= fold_build2 (MINUS_EXPR
, TREE_TYPE (low
), high
, low
);
80 if (!tree_fits_uhwi_p (r
))
83 return tree_to_uhwi (r
) + 1;
87 tree m_case_label_expr
;
89 /* Basic block of the case. */
90 basic_block m_case_bb
;
92 /* Probability of taking this cluster. */
93 profile_probability m_prob
;
95 /* Probability of reaching subtree rooted at this node. */
96 profile_probability m_subtree_prob
;
99 /* Default constructor. */
103 cluster::cluster (tree case_label_expr
, basic_block case_bb
,
104 profile_probability prob
, profile_probability subtree_prob
):
105 m_case_label_expr (case_label_expr
), m_case_bb (case_bb
), m_prob (prob
),
106 m_subtree_prob (subtree_prob
)
110 /* Subclass of cluster representing a simple contiguous range
113 struct simple_cluster
: public cluster
116 simple_cluster (tree low
, tree high
, tree case_label_expr
,
117 basic_block case_bb
, profile_probability prob
);
148 dump (FILE *f
, bool details ATTRIBUTE_UNUSED
= false)
150 PRINT_CASE (f
, get_low ());
151 if (get_low () != get_high ())
154 PRINT_CASE (f
, get_high ());
159 void emit (tree
, tree
, tree
, basic_block
)
164 /* Low value of the case. */
167 /* High value of the case. */
170 /* True if case is a range. */
174 simple_cluster::simple_cluster (tree low
, tree high
, tree case_label_expr
,
175 basic_block case_bb
, profile_probability prob
):
176 cluster (case_label_expr
, case_bb
, prob
, prob
),
177 m_low (low
), m_high (high
)
179 m_range_p
= m_high
!= NULL
;
184 /* Abstract subclass of jump table and bit test cluster,
185 handling a collection of simple_cluster instances. */
187 struct group_cluster
: public cluster
190 group_cluster (vec
<cluster
*> &clusters
, unsigned start
, unsigned end
);
198 return m_cases
[0]->get_low ();
204 return m_cases
[m_cases
.length () - 1]->get_high ();
213 void dump (FILE *f
, bool details
= false);
215 /* List of simple clusters handled by the group. */
216 vec
<simple_cluster
*> m_cases
;
219 /* Concrete subclass of group_cluster representing a collection
220 of cases to be implemented as a jump table.
221 The "emit" vfunc gernerates a nested switch statement which
222 is later lowered to a jump table. */
224 struct jump_table_cluster
: public group_cluster
227 jump_table_cluster (vec
<cluster
*> &clusters
, unsigned start
, unsigned end
)
228 : group_cluster (clusters
, start
, end
)
237 void emit (tree index_expr
, tree index_type
,
238 tree default_label_expr
, basic_block default_bb
);
240 /* Find jump tables of given CLUSTERS, where all members of the vector
241 are of type simple_cluster. New clusters are returned. */
242 static vec
<cluster
*> find_jump_tables (vec
<cluster
*> &clusters
);
244 /* Return true when cluster starting at START and ending at END (inclusive)
245 can build a jump-table. */
246 static bool can_be_handled (const vec
<cluster
*> &clusters
, unsigned start
,
249 /* Return true if cluster starting at START and ending at END (inclusive)
250 is profitable transformation. */
251 static bool is_beneficial (const vec
<cluster
*> &clusters
, unsigned start
,
254 /* Return the smallest number of different values for which it is best
255 to use a jump-table instead of a tree of conditional branches. */
256 static inline unsigned int case_values_threshold (void);
258 /* Return whether jump table expansion is allowed. */
259 static bool is_enabled (void);
261 /* Max growth ratio for code that is optimized for size. */
262 static const unsigned HOST_WIDE_INT max_ratio_for_size
= 3;
264 /* Max growth ratio for code that is optimized for speed. */
265 static const unsigned HOST_WIDE_INT max_ratio_for_speed
= 8;
268 /* A GIMPLE switch statement can be expanded to a short sequence of bit-wise
269 comparisons. "switch(x)" is converted into "if ((1 << (x-MINVAL)) & CST)"
270 where CST and MINVAL are integer constants. This is better than a series
271 of compare-and-banch insns in some cases, e.g. we can implement:
273 if ((x==4) || (x==6) || (x==9) || (x==11))
275 as a single bit test:
277 if ((1<<x) & ((1<<4)|(1<<6)|(1<<9)|(1<<11)))
279 This transformation is only applied if the number of case targets is small,
280 if CST constains at least 3 bits, and "1 << x" is cheap. The bit tests are
281 performed in "word_mode".
283 The following example shows the code the transformation generates:
289 case '0': case '1': case '2': case '3': case '4':
290 case '5': case '6': case '7': case '8': case '9':
291 case 'A': case 'B': case 'C': case 'D': case 'E':
303 if (tmp1 > (70 - 48)) goto L2;
305 tmp3 = 0b11111100000001111111111;
306 if ((tmp2 & tmp3) != 0) goto L1 ; else goto L2;
313 TODO: There are still some improvements to this transformation that could
316 * A narrower mode than word_mode could be used if that is cheaper, e.g.
317 for x86_64 where a narrower-mode shift may result in smaller code.
319 * The compounded constant could be shifted rather than the one. The
320 test would be either on the sign bit or on the least significant bit,
321 depending on the direction of the shift. On some machines, the test
322 for the branch would be free if the bit to test is already set by the
325 This transformation was contributed by Roger Sayle, see this e-mail:
326 http://gcc.gnu.org/ml/gcc-patches/2003-01/msg01950.html
329 struct bit_test_cluster
: public group_cluster
332 bit_test_cluster (vec
<cluster
*> &clusters
, unsigned start
, unsigned end
)
333 :group_cluster (clusters
, start
, end
)
342 /* Expand a switch statement by a short sequence of bit-wise
343 comparisons. "switch(x)" is effectively converted into
344 "if ((1 << (x-MINVAL)) & CST)" where CST and MINVAL are
347 INDEX_EXPR is the value being switched on.
349 MINVAL is the lowest case value of in the case nodes,
350 and RANGE is highest value minus MINVAL. MINVAL and RANGE
351 are not guaranteed to be of the same type as INDEX_EXPR
352 (the gimplifier doesn't change the type of case label values,
353 and MINVAL and RANGE are derived from those values).
354 MAXVAL is MINVAL + RANGE.
356 There *MUST* be max_case_bit_tests or less unique case
358 void emit (tree index_expr
, tree index_type
,
359 tree default_label_expr
, basic_block default_bb
);
361 /* Find bit tests of given CLUSTERS, where all members of the vector
362 are of type simple_cluster. New clusters are returned. */
363 static vec
<cluster
*> find_bit_tests (vec
<cluster
*> &clusters
);
365 /* Return true when RANGE of case values with UNIQ labels
366 can build a bit test. */
367 static bool can_be_handled (unsigned HOST_WIDE_INT range
, unsigned uniq
);
369 /* Return true when cluster starting at START and ending at END (inclusive)
370 can build a bit test. */
371 static bool can_be_handled (const vec
<cluster
*> &clusters
, unsigned start
,
374 /* Return true when COUNT of cases of UNIQ labels is beneficial for bit test
376 static bool is_beneficial (unsigned count
, unsigned uniq
);
378 /* Return true if cluster starting at START and ending at END (inclusive)
379 is profitable transformation. */
380 static bool is_beneficial (const vec
<cluster
*> &clusters
, unsigned start
,
383 /* Split the basic block at the statement pointed to by GSIP, and insert
384 a branch to the target basic block of E_TRUE conditional on tree
387 It is assumed that there is already an edge from the to-be-split
388 basic block to E_TRUE->dest block. This edge is removed, and the
389 profile information on the edge is re-used for the new conditional
392 The CFG is updated. The dominator tree will not be valid after
393 this transformation, but the immediate dominators are updated if
394 UPDATE_DOMINATORS is true.
396 Returns the newly created basic block. */
397 static basic_block
hoist_edge_and_branch_if_true (gimple_stmt_iterator
*gsip
,
399 basic_block case_bb
);
401 /* Maximum number of different basic blocks that can be handled by
403 static const int m_max_case_bit_tests
= 3;
406 /* Helper struct to find minimal clusters. */
408 struct min_cluster_item
411 min_cluster_item (unsigned count
, unsigned start
, unsigned non_jt_cases
):
412 m_count (count
), m_start (start
), m_non_jt_cases (non_jt_cases
)
415 /* Count of clusters. */
418 /* Index where is cluster boundary. */
421 /* Total number of cases that will not be in a jump table. */
422 unsigned m_non_jt_cases
;
425 /* Helper struct to represent switch decision tree. */
427 struct case_tree_node
429 /* Empty Constructor. */
432 /* Left son in binary tree. */
433 case_tree_node
*m_left
;
435 /* Right son in binary tree; also node chain. */
436 case_tree_node
*m_right
;
438 /* Parent of node in binary tree. */
439 case_tree_node
*m_parent
;
441 /* Cluster represented by this tree node. */
446 case_tree_node::case_tree_node ():
447 m_left (NULL
), m_right (NULL
), m_parent (NULL
), m_c (NULL
)
452 jump_table_cluster::case_values_threshold (void)
454 unsigned int threshold
= PARAM_VALUE (PARAM_CASE_VALUES_THRESHOLD
);
457 threshold
= targetm
.case_values_threshold ();
462 /* Return whether jump table expansion is allowed. */
463 bool jump_table_cluster::is_enabled (void)
465 /* If neither casesi or tablejump is available, or flag_jump_tables
466 over-ruled us, we really have no choice. */
467 if (!targetm
.have_casesi () && !targetm
.have_tablejump ())
469 if (!flag_jump_tables
)
471 #ifndef ASM_OUTPUT_ADDR_DIFF_ELT
479 /* A case_bit_test represents a set of case nodes that may be
480 selected from using a bit-wise comparison. HI and LO hold
481 the integer to be tested against, TARGET_EDGE contains the
482 edge to the basic block to jump to upon success and BITS
483 counts the number of case nodes handled by this test,
484 typically the number of bits set in HI:LO. The LABEL field
485 is used to quickly identify all cases in this set without
486 looking at label_to_block for every case label. */
491 basic_block target_bb
;
495 /* Comparison function for qsort to order bit tests by decreasing
496 probability of execution. */
497 static int cmp (const void *p1
, const void *p2
);
500 struct switch_decision_tree
503 switch_decision_tree (gswitch
*swtch
): m_switch (swtch
), m_phi_mapping (),
504 m_case_bbs (), m_case_node_pool ("struct case_node pool"),
509 /* Analyze switch statement and return true when the statement is expanded
511 bool analyze_switch_statement ();
513 /* Attempt to expand CLUSTERS as a decision tree. Return true when
515 bool try_switch_expansion (vec
<cluster
*> &clusters
);
517 /* Reset the aux field of all outgoing edges of switch basic block. */
518 inline void reset_out_edges_aux ();
520 /* Compute the number of case labels that correspond to each outgoing edge of
521 switch statement. Record this information in the aux field of the edge.
523 void compute_cases_per_edge ();
525 /* Before switch transformation, record all SSA_NAMEs defined in switch BB
526 and used in a label basic block. */
527 void record_phi_operand_mapping ();
529 /* Append new operands to PHI statements that were introduced due to
530 addition of new edges to case labels. */
531 void fix_phi_operands_for_edges ();
533 /* Generate a decision tree, switching on INDEX_EXPR and jumping to
534 one of the labels in CASE_LIST or to the DEFAULT_LABEL.
536 We generate a binary decision tree to select the appropriate target
538 void emit (basic_block bb
, tree index_expr
,
539 profile_probability default_prob
, tree index_type
);
541 /* Emit step-by-step code to select a case for the value of INDEX.
542 The thus generated decision tree follows the form of the
543 case-node binary tree NODE, whose nodes represent test conditions.
544 DEFAULT_PROB is probability of cases leading to default BB.
545 INDEX_TYPE is the type of the index of the switch. */
546 basic_block
emit_case_nodes (basic_block bb
, tree index
,
547 case_tree_node
*node
,
548 profile_probability default_prob
,
551 /* Take an ordered list of case nodes
552 and transform them into a near optimal binary tree,
553 on the assumption that any target code selection value is as
556 The transformation is performed by splitting the ordered
557 list into two equal sections plus a pivot. The parts are
558 then attached to the pivot as left and right branches. Each
559 branch is then transformed recursively. */
560 static void balance_case_nodes (case_tree_node
**head
,
561 case_tree_node
*parent
);
563 /* Dump ROOT, a list or tree of case nodes, to file F. */
564 static void dump_case_nodes (FILE *f
, case_tree_node
*root
, int indent_step
,
567 /* Add an unconditional jump to CASE_BB that happens in basic block BB. */
568 static void emit_jump (basic_block bb
, basic_block case_bb
);
570 /* Generate code to compare OP0 with OP1 so that the condition codes are
571 set and to jump to LABEL_BB if the condition is true.
572 COMPARISON is the GIMPLE comparison (EQ, NE, GT, etc.).
573 PROB is the probability of jumping to LABEL_BB. */
574 static basic_block
emit_cmp_and_jump_insns (basic_block bb
, tree op0
,
575 tree op1
, tree_code comparison
,
576 basic_block label_bb
,
577 profile_probability prob
);
579 /* Switch statement. */
582 /* Map of PHI nodes that have to be fixed after expansion. */
583 hash_map
<tree
, tree
> m_phi_mapping
;
585 /* List of basic blocks that belong to labels of the switch. */
586 auto_vec
<basic_block
> m_case_bbs
;
588 /* Basic block with default label. */
589 basic_block m_default_bb
;
591 /* A pool for case nodes. */
592 object_allocator
<case_tree_node
> m_case_node_pool
;
594 /* Balanced tree of case nodes. */
595 case_tree_node
*m_case_list
;
599 Switch initialization conversion
601 The following pass changes simple initializations of scalars in a switch
602 statement into initializations from a static array. Obviously, the values
603 must be constant and known at compile time and a default branch must be
604 provided. For example, the following code:
628 a_5 = PHI <a_1, a_2, a_3, a_4>
629 b_5 = PHI <b_1, b_2, b_3, b_4>
634 static const int = CSWTCH01[] = {6, 6, 5, 1, 1, 1, 1, 1, 1, 1, 1, 4};
635 static const int = CSWTCH02[] = {8, 8, 9, 16, 16, 16, 16, 16, 16, 16,
638 if (((unsigned) argc) - 1 < 11)
640 a_6 = CSWTCH02[argc - 1];
641 b_6 = CSWTCH01[argc - 1];
651 There are further constraints. Specifically, the range of values across all
652 case labels must not be bigger than SWITCH_CONVERSION_BRANCH_RATIO (default
653 eight) times the number of the actual switch branches.
655 This transformation was contributed by Martin Jambor, see this e-mail:
656 http://gcc.gnu.org/ml/gcc-patches/2008-07/msg00011.html */
658 /* The main structure of the pass. */
659 struct switch_conversion
662 switch_conversion ();
665 ~switch_conversion ();
667 /* The following function is invoked on every switch statement (the current
668 one is given in SWTCH) and runs the individual phases of switch
669 conversion on it one after another until one fails or the conversion
670 is completed. On success, NULL is in m_reason, otherwise points
671 to a string with the reason why the conversion failed. */
672 void expand (gswitch
*swtch
);
674 /* Collection information about SWTCH statement. */
675 void collect (gswitch
*swtch
);
677 /* Checks whether the range given by individual case statements of the switch
678 switch statement isn't too big and whether the number of branches actually
679 satisfies the size of the new array. */
682 /* Checks whether all but the final BB basic blocks are empty. */
683 bool check_all_empty_except_final ();
685 /* This function checks whether all required values in phi nodes in final_bb
686 are constants. Required values are those that correspond to a basic block
687 which is a part of the examined switch statement. It returns true if the
688 phi nodes are OK, otherwise false. */
689 bool check_final_bb ();
691 /* The following function allocates default_values, target_{in,out}_names and
692 constructors arrays. The last one is also populated with pointers to
693 vectors that will become constructors of new arrays. */
694 void create_temp_arrays ();
696 /* Populate the array of default values in the order of phi nodes.
697 DEFAULT_CASE is the CASE_LABEL_EXPR for the default switch branch
698 if the range is non-contiguous or the default case has standard
699 structure, otherwise it is the first non-default case instead. */
700 void gather_default_values (tree default_case
);
702 /* The following function populates the vectors in the constructors array with
703 future contents of the static arrays. The vectors are populated in the
704 order of phi nodes. */
705 void build_constructors ();
707 /* If all values in the constructor vector are the same, return the value.
708 Otherwise return NULL_TREE. Not supposed to be called for empty
710 tree
contains_same_values_p (vec
<constructor_elt
, va_gc
> *vec
);
712 /* Return type which should be used for array elements, either TYPE's
713 main variant or, for integral types, some smaller integral type
714 that can still hold all the constants. */
715 tree
array_value_type (tree type
, int num
);
717 /* Create an appropriate array type and declaration and assemble a static
718 array variable. Also create a load statement that initializes
719 the variable in question with a value from the static array. SWTCH is
720 the switch statement being converted, NUM is the index to
721 arrays of constructors, default values and target SSA names
722 for this particular array. ARR_INDEX_TYPE is the type of the index
723 of the new array, PHI is the phi node of the final BB that corresponds
724 to the value that will be loaded from the created array. TIDX
725 is an ssa name of a temporary variable holding the index for loads from the
727 void build_one_array (int num
, tree arr_index_type
,
728 gphi
*phi
, tree tidx
);
730 /* Builds and initializes static arrays initialized with values gathered from
731 the switch statement. Also creates statements that load values from
733 void build_arrays ();
735 /* Generates and appropriately inserts loads of default values at the position
736 given by GSI. Returns the last inserted statement. */
737 gassign
*gen_def_assigns (gimple_stmt_iterator
*gsi
);
739 /* Deletes the unused bbs and edges that now contain the switch statement and
740 its empty branch bbs. BBD is the now dead BB containing
741 the original switch statement, FINAL is the last BB of the converted
742 switch statement (in terms of succession). */
743 void prune_bbs (basic_block bbd
, basic_block final
, basic_block default_bb
);
745 /* Add values to phi nodes in final_bb for the two new edges. E1F is the edge
746 from the basic block loading values from an array and E2F from the basic
747 block loading default values. BBF is the last switch basic block (see the
748 bbf description in the comment below). */
749 void fix_phi_nodes (edge e1f
, edge e2f
, basic_block bbf
);
751 /* Creates a check whether the switch expression value actually falls into the
752 range given by all the cases. If it does not, the temporaries are loaded
753 with default values instead. */
754 void gen_inbound_check ();
756 /* Switch statement for which switch conversion takes place. */
759 /* The expression used to decide the switch branch. */
762 /* The following integer constants store the minimum and maximum value
763 covered by the case labels. */
767 /* The difference between the above two numbers. Stored here because it
768 is used in all the conversion heuristics, as well as for some of the
769 transformation, and it is expensive to re-compute it all the time. */
772 /* Basic block that contains the actual GIMPLE_SWITCH. */
773 basic_block m_switch_bb
;
775 /* Basic block that is the target of the default case. */
776 basic_block m_default_bb
;
778 /* The single successor block of all branches out of the GIMPLE_SWITCH,
779 if such a block exists. Otherwise NULL. */
780 basic_block m_final_bb
;
782 /* The probability of the default edge in the replaced switch. */
783 profile_probability m_default_prob
;
785 /* The count of the default edge in the replaced switch. */
786 profile_count m_default_count
;
788 /* Combined count of all other (non-default) edges in the replaced switch. */
789 profile_count m_other_count
;
791 /* Number of phi nodes in the final bb (that we'll be replacing). */
794 /* Constructors of new static arrays. */
795 vec
<constructor_elt
, va_gc
> **m_constructors
;
797 /* Array of default values, in the same order as phi nodes. */
798 tree
*m_default_values
;
800 /* Array of ssa names that are initialized with a value from a new static
802 tree
*m_target_inbound_names
;
804 /* Array of ssa names that are initialized with the default value if the
805 switch expression is out of range. */
806 tree
*m_target_outbound_names
;
811 /* The first load statement that loads a temporary from a new static array.
813 gimple
*m_arr_ref_first
;
815 /* The last load statement that loads a temporary from a new static array. */
816 gimple
*m_arr_ref_last
;
818 /* String reason why the case wasn't a good candidate that is written to the
819 dump file, if there is one. */
820 const char *m_reason
;
822 /* True if default case is not used for any value between range_min and
823 range_max inclusive. */
824 bool m_contiguous_range
;
826 /* True if default case does not have the required shape for other case
828 bool m_default_case_nonstandard
;
830 /* Number of uniq labels for non-default edges. */
833 /* Count is number of non-default edges. */
834 unsigned int m_count
;
836 /* True if CFG has been changed. */
841 switch_decision_tree::reset_out_edges_aux ()
843 basic_block bb
= gimple_bb (m_switch
);
846 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
850 } // tree_switch_conversion namespace
852 #endif // TREE_SWITCH_CONVERSION_H