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