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