/cp
[official-gcc.git] / gcc / tree-switch-conversion.c
blob32019120b904de22b532c86ddea46eb29c17614f
1 /* Lower GIMPLE_SWITCH expressions to something more efficient than
2 a jump table.
3 Copyright (C) 2006-2015 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 /* This file handles the lowering of GIMPLE_SWITCH to an indexed
23 load, or a series of bit-test-and-branch expressions. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "backend.h"
29 #include "cfghooks.h"
30 #include "tree.h"
31 #include "gimple.h"
32 #include "rtl.h"
33 #include "ssa.h"
34 #include "params.h"
35 #include "flags.h"
36 #include "alias.h"
37 #include "fold-const.h"
38 #include "varasm.h"
39 #include "stor-layout.h"
40 #include "cfganal.h"
41 #include "internal-fn.h"
42 #include "gimplify.h"
43 #include "gimple-iterator.h"
44 #include "gimplify-me.h"
45 #include "cgraph.h"
46 #include "tree-cfg.h"
47 #include "tree-pass.h"
48 #include "gimple-pretty-print.h"
49 #include "cfgloop.h"
51 /* ??? For lang_hooks.types.type_for_mode, but is there a word_mode
52 type in the GIMPLE type system that is language-independent? */
53 #include "langhooks.h"
55 /* Need to include expr.h and optabs.h for lshift_cheap_p. */
56 #include "insn-config.h"
57 #include "expmed.h"
58 #include "dojump.h"
59 #include "explow.h"
60 #include "calls.h"
61 #include "emit-rtl.h"
62 #include "stmt.h"
63 #include "expr.h"
64 #include "insn-codes.h"
65 #include "optabs.h"
67 /* Maximum number of case bit tests.
68 FIXME: This should be derived from PARAM_CASE_VALUES_THRESHOLD and
69 targetm.case_values_threshold(), or be its own param. */
70 #define MAX_CASE_BIT_TESTS 3
72 /* Split the basic block at the statement pointed to by GSIP, and insert
73 a branch to the target basic block of E_TRUE conditional on tree
74 expression COND.
76 It is assumed that there is already an edge from the to-be-split
77 basic block to E_TRUE->dest block. This edge is removed, and the
78 profile information on the edge is re-used for the new conditional
79 jump.
81 The CFG is updated. The dominator tree will not be valid after
82 this transformation, but the immediate dominators are updated if
83 UPDATE_DOMINATORS is true.
85 Returns the newly created basic block. */
87 static basic_block
88 hoist_edge_and_branch_if_true (gimple_stmt_iterator *gsip,
89 tree cond, edge e_true,
90 bool update_dominators)
92 tree tmp;
93 gcond *cond_stmt;
94 edge e_false;
95 basic_block new_bb, split_bb = gsi_bb (*gsip);
96 bool dominated_e_true = false;
98 gcc_assert (e_true->src == split_bb);
100 if (update_dominators
101 && get_immediate_dominator (CDI_DOMINATORS, e_true->dest) == split_bb)
102 dominated_e_true = true;
104 tmp = force_gimple_operand_gsi (gsip, cond, /*simple=*/true, NULL,
105 /*before=*/true, GSI_SAME_STMT);
106 cond_stmt = gimple_build_cond_from_tree (tmp, NULL_TREE, NULL_TREE);
107 gsi_insert_before (gsip, cond_stmt, GSI_SAME_STMT);
109 e_false = split_block (split_bb, cond_stmt);
110 new_bb = e_false->dest;
111 redirect_edge_pred (e_true, split_bb);
113 e_true->flags &= ~EDGE_FALLTHRU;
114 e_true->flags |= EDGE_TRUE_VALUE;
116 e_false->flags &= ~EDGE_FALLTHRU;
117 e_false->flags |= EDGE_FALSE_VALUE;
118 e_false->probability = REG_BR_PROB_BASE - e_true->probability;
119 e_false->count = split_bb->count - e_true->count;
120 new_bb->count = e_false->count;
122 if (update_dominators)
124 if (dominated_e_true)
125 set_immediate_dominator (CDI_DOMINATORS, e_true->dest, split_bb);
126 set_immediate_dominator (CDI_DOMINATORS, e_false->dest, split_bb);
129 return new_bb;
133 /* Return true if a switch should be expanded as a bit test.
134 RANGE is the difference between highest and lowest case.
135 UNIQ is number of unique case node targets, not counting the default case.
136 COUNT is the number of comparisons needed, not counting the default case. */
138 static bool
139 expand_switch_using_bit_tests_p (tree range,
140 unsigned int uniq,
141 unsigned int count, bool speed_p)
143 return (((uniq == 1 && count >= 3)
144 || (uniq == 2 && count >= 5)
145 || (uniq == 3 && count >= 6))
146 && lshift_cheap_p (speed_p)
147 && compare_tree_int (range, GET_MODE_BITSIZE (word_mode)) < 0
148 && compare_tree_int (range, 0) > 0);
151 /* Implement switch statements with bit tests
153 A GIMPLE switch statement can be expanded to a short sequence of bit-wise
154 comparisons. "switch(x)" is converted into "if ((1 << (x-MINVAL)) & CST)"
155 where CST and MINVAL are integer constants. This is better than a series
156 of compare-and-banch insns in some cases, e.g. we can implement:
158 if ((x==4) || (x==6) || (x==9) || (x==11))
160 as a single bit test:
162 if ((1<<x) & ((1<<4)|(1<<6)|(1<<9)|(1<<11)))
164 This transformation is only applied if the number of case targets is small,
165 if CST constains at least 3 bits, and "1 << x" is cheap. The bit tests are
166 performed in "word_mode".
168 The following example shows the code the transformation generates:
170 int bar(int x)
172 switch (x)
174 case '0': case '1': case '2': case '3': case '4':
175 case '5': case '6': case '7': case '8': case '9':
176 case 'A': case 'B': case 'C': case 'D': case 'E':
177 case 'F':
178 return 1;
180 return 0;
185 bar (int x)
187 tmp1 = x - 48;
188 if (tmp1 > (70 - 48)) goto L2;
189 tmp2 = 1 << tmp1;
190 tmp3 = 0b11111100000001111111111;
191 if ((tmp2 & tmp3) != 0) goto L1 ; else goto L2;
193 return 1;
195 return 0;
198 TODO: There are still some improvements to this transformation that could
199 be implemented:
201 * A narrower mode than word_mode could be used if that is cheaper, e.g.
202 for x86_64 where a narrower-mode shift may result in smaller code.
204 * The compounded constant could be shifted rather than the one. The
205 test would be either on the sign bit or on the least significant bit,
206 depending on the direction of the shift. On some machines, the test
207 for the branch would be free if the bit to test is already set by the
208 shift operation.
210 This transformation was contributed by Roger Sayle, see this e-mail:
211 http://gcc.gnu.org/ml/gcc-patches/2003-01/msg01950.html
214 /* A case_bit_test represents a set of case nodes that may be
215 selected from using a bit-wise comparison. HI and LO hold
216 the integer to be tested against, TARGET_EDGE contains the
217 edge to the basic block to jump to upon success and BITS
218 counts the number of case nodes handled by this test,
219 typically the number of bits set in HI:LO. The LABEL field
220 is used to quickly identify all cases in this set without
221 looking at label_to_block for every case label. */
223 struct case_bit_test
225 wide_int mask;
226 edge target_edge;
227 tree label;
228 int bits;
231 /* Comparison function for qsort to order bit tests by decreasing
232 probability of execution. Our best guess comes from a measured
233 profile. If the profile counts are equal, break even on the
234 number of case nodes, i.e. the node with the most cases gets
235 tested first.
237 TODO: Actually this currently runs before a profile is available.
238 Therefore the case-as-bit-tests transformation should be done
239 later in the pass pipeline, or something along the lines of
240 "Efficient and effective branch reordering using profile data"
241 (Yang et. al., 2002) should be implemented (although, how good
242 is a paper is called "Efficient and effective ..." when the
243 latter is implied by the former, but oh well...). */
245 static int
246 case_bit_test_cmp (const void *p1, const void *p2)
248 const struct case_bit_test *const d1 = (const struct case_bit_test *) p1;
249 const struct case_bit_test *const d2 = (const struct case_bit_test *) p2;
251 if (d2->target_edge->count != d1->target_edge->count)
252 return d2->target_edge->count - d1->target_edge->count;
253 if (d2->bits != d1->bits)
254 return d2->bits - d1->bits;
256 /* Stabilize the sort. */
257 return LABEL_DECL_UID (d2->label) - LABEL_DECL_UID (d1->label);
260 /* Expand a switch statement by a short sequence of bit-wise
261 comparisons. "switch(x)" is effectively converted into
262 "if ((1 << (x-MINVAL)) & CST)" where CST and MINVAL are
263 integer constants.
265 INDEX_EXPR is the value being switched on.
267 MINVAL is the lowest case value of in the case nodes,
268 and RANGE is highest value minus MINVAL. MINVAL and RANGE
269 are not guaranteed to be of the same type as INDEX_EXPR
270 (the gimplifier doesn't change the type of case label values,
271 and MINVAL and RANGE are derived from those values).
272 MAXVAL is MINVAL + RANGE.
274 There *MUST* be MAX_CASE_BIT_TESTS or less unique case
275 node targets. */
277 static void
278 emit_case_bit_tests (gswitch *swtch, tree index_expr,
279 tree minval, tree range, tree maxval)
281 struct case_bit_test test[MAX_CASE_BIT_TESTS];
282 unsigned int i, j, k;
283 unsigned int count;
285 basic_block switch_bb = gimple_bb (swtch);
286 basic_block default_bb, new_default_bb, new_bb;
287 edge default_edge;
288 bool update_dom = dom_info_available_p (CDI_DOMINATORS);
290 vec<basic_block> bbs_to_fix_dom = vNULL;
292 tree index_type = TREE_TYPE (index_expr);
293 tree unsigned_index_type = unsigned_type_for (index_type);
294 unsigned int branch_num = gimple_switch_num_labels (swtch);
296 gimple_stmt_iterator gsi;
297 gassign *shift_stmt;
299 tree idx, tmp, csui;
300 tree word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
301 tree word_mode_zero = fold_convert (word_type_node, integer_zero_node);
302 tree word_mode_one = fold_convert (word_type_node, integer_one_node);
303 int prec = TYPE_PRECISION (word_type_node);
304 wide_int wone = wi::one (prec);
306 memset (&test, 0, sizeof (test));
308 /* Get the edge for the default case. */
309 tmp = gimple_switch_default_label (swtch);
310 default_bb = label_to_block (CASE_LABEL (tmp));
311 default_edge = find_edge (switch_bb, default_bb);
313 /* Go through all case labels, and collect the case labels, profile
314 counts, and other information we need to build the branch tests. */
315 count = 0;
316 for (i = 1; i < branch_num; i++)
318 unsigned int lo, hi;
319 tree cs = gimple_switch_label (swtch, i);
320 tree label = CASE_LABEL (cs);
321 edge e = find_edge (switch_bb, label_to_block (label));
322 for (k = 0; k < count; k++)
323 if (e == test[k].target_edge)
324 break;
326 if (k == count)
328 gcc_checking_assert (count < MAX_CASE_BIT_TESTS);
329 test[k].mask = wi::zero (prec);
330 test[k].target_edge = e;
331 test[k].label = label;
332 test[k].bits = 1;
333 count++;
335 else
336 test[k].bits++;
338 lo = tree_to_uhwi (int_const_binop (MINUS_EXPR,
339 CASE_LOW (cs), minval));
340 if (CASE_HIGH (cs) == NULL_TREE)
341 hi = lo;
342 else
343 hi = tree_to_uhwi (int_const_binop (MINUS_EXPR,
344 CASE_HIGH (cs), minval));
346 for (j = lo; j <= hi; j++)
347 test[k].mask |= wi::lshift (wone, j);
350 qsort (test, count, sizeof (*test), case_bit_test_cmp);
352 /* If all values are in the 0 .. BITS_PER_WORD-1 range, we can get rid of
353 the minval subtractions, but it might make the mask constants more
354 expensive. So, compare the costs. */
355 if (compare_tree_int (minval, 0) > 0
356 && compare_tree_int (maxval, GET_MODE_BITSIZE (word_mode)) < 0)
358 int cost_diff;
359 HOST_WIDE_INT m = tree_to_uhwi (minval);
360 rtx reg = gen_raw_REG (word_mode, 10000);
361 bool speed_p = optimize_bb_for_speed_p (gimple_bb (swtch));
362 cost_diff = set_rtx_cost (gen_rtx_PLUS (word_mode, reg,
363 GEN_INT (-m)), speed_p);
364 for (i = 0; i < count; i++)
366 rtx r = immed_wide_int_const (test[i].mask, word_mode);
367 cost_diff += set_src_cost (gen_rtx_AND (word_mode, reg, r),
368 word_mode, speed_p);
369 r = immed_wide_int_const (wi::lshift (test[i].mask, m), word_mode);
370 cost_diff -= set_src_cost (gen_rtx_AND (word_mode, reg, r),
371 word_mode, speed_p);
373 if (cost_diff > 0)
375 for (i = 0; i < count; i++)
376 test[i].mask = wi::lshift (test[i].mask, m);
377 minval = build_zero_cst (TREE_TYPE (minval));
378 range = maxval;
382 /* We generate two jumps to the default case label.
383 Split the default edge, so that we don't have to do any PHI node
384 updating. */
385 new_default_bb = split_edge (default_edge);
387 if (update_dom)
389 bbs_to_fix_dom.create (10);
390 bbs_to_fix_dom.quick_push (switch_bb);
391 bbs_to_fix_dom.quick_push (default_bb);
392 bbs_to_fix_dom.quick_push (new_default_bb);
395 /* Now build the test-and-branch code. */
397 gsi = gsi_last_bb (switch_bb);
399 /* idx = (unsigned)x - minval. */
400 idx = fold_convert (unsigned_index_type, index_expr);
401 idx = fold_build2 (MINUS_EXPR, unsigned_index_type, idx,
402 fold_convert (unsigned_index_type, minval));
403 idx = force_gimple_operand_gsi (&gsi, idx,
404 /*simple=*/true, NULL_TREE,
405 /*before=*/true, GSI_SAME_STMT);
407 /* if (idx > range) goto default */
408 range = force_gimple_operand_gsi (&gsi,
409 fold_convert (unsigned_index_type, range),
410 /*simple=*/true, NULL_TREE,
411 /*before=*/true, GSI_SAME_STMT);
412 tmp = fold_build2 (GT_EXPR, boolean_type_node, idx, range);
413 new_bb = hoist_edge_and_branch_if_true (&gsi, tmp, default_edge, update_dom);
414 if (update_dom)
415 bbs_to_fix_dom.quick_push (new_bb);
416 gcc_assert (gimple_bb (swtch) == new_bb);
417 gsi = gsi_last_bb (new_bb);
419 /* Any blocks dominated by the GIMPLE_SWITCH, but that are not successors
420 of NEW_BB, are still immediately dominated by SWITCH_BB. Make it so. */
421 if (update_dom)
423 vec<basic_block> dom_bbs;
424 basic_block dom_son;
426 dom_bbs = get_dominated_by (CDI_DOMINATORS, new_bb);
427 FOR_EACH_VEC_ELT (dom_bbs, i, dom_son)
429 edge e = find_edge (new_bb, dom_son);
430 if (e && single_pred_p (e->dest))
431 continue;
432 set_immediate_dominator (CDI_DOMINATORS, dom_son, switch_bb);
433 bbs_to_fix_dom.safe_push (dom_son);
435 dom_bbs.release ();
438 /* csui = (1 << (word_mode) idx) */
439 csui = make_ssa_name (word_type_node);
440 tmp = fold_build2 (LSHIFT_EXPR, word_type_node, word_mode_one,
441 fold_convert (word_type_node, idx));
442 tmp = force_gimple_operand_gsi (&gsi, tmp,
443 /*simple=*/false, NULL_TREE,
444 /*before=*/true, GSI_SAME_STMT);
445 shift_stmt = gimple_build_assign (csui, tmp);
446 gsi_insert_before (&gsi, shift_stmt, GSI_SAME_STMT);
447 update_stmt (shift_stmt);
449 /* for each unique set of cases:
450 if (const & csui) goto target */
451 for (k = 0; k < count; k++)
453 tmp = wide_int_to_tree (word_type_node, test[k].mask);
454 tmp = fold_build2 (BIT_AND_EXPR, word_type_node, csui, tmp);
455 tmp = force_gimple_operand_gsi (&gsi, tmp,
456 /*simple=*/true, NULL_TREE,
457 /*before=*/true, GSI_SAME_STMT);
458 tmp = fold_build2 (NE_EXPR, boolean_type_node, tmp, word_mode_zero);
459 new_bb = hoist_edge_and_branch_if_true (&gsi, tmp, test[k].target_edge,
460 update_dom);
461 if (update_dom)
462 bbs_to_fix_dom.safe_push (new_bb);
463 gcc_assert (gimple_bb (swtch) == new_bb);
464 gsi = gsi_last_bb (new_bb);
467 /* We should have removed all edges now. */
468 gcc_assert (EDGE_COUNT (gsi_bb (gsi)->succs) == 0);
470 /* If nothing matched, go to the default label. */
471 make_edge (gsi_bb (gsi), new_default_bb, EDGE_FALLTHRU);
473 /* The GIMPLE_SWITCH is now redundant. */
474 gsi_remove (&gsi, true);
476 if (update_dom)
478 /* Fix up the dominator tree. */
479 iterate_fix_dominators (CDI_DOMINATORS, bbs_to_fix_dom, true);
480 bbs_to_fix_dom.release ();
485 Switch initialization conversion
487 The following pass changes simple initializations of scalars in a switch
488 statement into initializations from a static array. Obviously, the values
489 must be constant and known at compile time and a default branch must be
490 provided. For example, the following code:
492 int a,b;
494 switch (argc)
496 case 1:
497 case 2:
498 a_1 = 8;
499 b_1 = 6;
500 break;
501 case 3:
502 a_2 = 9;
503 b_2 = 5;
504 break;
505 case 12:
506 a_3 = 10;
507 b_3 = 4;
508 break;
509 default:
510 a_4 = 16;
511 b_4 = 1;
512 break;
514 a_5 = PHI <a_1, a_2, a_3, a_4>
515 b_5 = PHI <b_1, b_2, b_3, b_4>
518 is changed into:
520 static const int = CSWTCH01[] = {6, 6, 5, 1, 1, 1, 1, 1, 1, 1, 1, 4};
521 static const int = CSWTCH02[] = {8, 8, 9, 16, 16, 16, 16, 16, 16, 16,
522 16, 16, 10};
524 if (((unsigned) argc) - 1 < 11)
526 a_6 = CSWTCH02[argc - 1];
527 b_6 = CSWTCH01[argc - 1];
529 else
531 a_7 = 16;
532 b_7 = 1;
534 a_5 = PHI <a_6, a_7>
535 b_b = PHI <b_6, b_7>
537 There are further constraints. Specifically, the range of values across all
538 case labels must not be bigger than SWITCH_CONVERSION_BRANCH_RATIO (default
539 eight) times the number of the actual switch branches.
541 This transformation was contributed by Martin Jambor, see this e-mail:
542 http://gcc.gnu.org/ml/gcc-patches/2008-07/msg00011.html */
544 /* The main structure of the pass. */
545 struct switch_conv_info
547 /* The expression used to decide the switch branch. */
548 tree index_expr;
550 /* The following integer constants store the minimum and maximum value
551 covered by the case labels. */
552 tree range_min;
553 tree range_max;
555 /* The difference between the above two numbers. Stored here because it
556 is used in all the conversion heuristics, as well as for some of the
557 transformation, and it is expensive to re-compute it all the time. */
558 tree range_size;
560 /* Basic block that contains the actual GIMPLE_SWITCH. */
561 basic_block switch_bb;
563 /* Basic block that is the target of the default case. */
564 basic_block default_bb;
566 /* The single successor block of all branches out of the GIMPLE_SWITCH,
567 if such a block exists. Otherwise NULL. */
568 basic_block final_bb;
570 /* The probability of the default edge in the replaced switch. */
571 int default_prob;
573 /* The count of the default edge in the replaced switch. */
574 gcov_type default_count;
576 /* Combined count of all other (non-default) edges in the replaced switch. */
577 gcov_type other_count;
579 /* Number of phi nodes in the final bb (that we'll be replacing). */
580 int phi_count;
582 /* Array of default values, in the same order as phi nodes. */
583 tree *default_values;
585 /* Constructors of new static arrays. */
586 vec<constructor_elt, va_gc> **constructors;
588 /* Array of ssa names that are initialized with a value from a new static
589 array. */
590 tree *target_inbound_names;
592 /* Array of ssa names that are initialized with the default value if the
593 switch expression is out of range. */
594 tree *target_outbound_names;
596 /* The first load statement that loads a temporary from a new static array.
598 gimple arr_ref_first;
600 /* The last load statement that loads a temporary from a new static array. */
601 gimple arr_ref_last;
603 /* String reason why the case wasn't a good candidate that is written to the
604 dump file, if there is one. */
605 const char *reason;
607 /* Parameters for expand_switch_using_bit_tests. Should be computed
608 the same way as in expand_case. */
609 unsigned int uniq;
610 unsigned int count;
613 /* Collect information about GIMPLE_SWITCH statement SWTCH into INFO. */
615 static void
616 collect_switch_conv_info (gswitch *swtch, struct switch_conv_info *info)
618 unsigned int branch_num = gimple_switch_num_labels (swtch);
619 tree min_case, max_case;
620 unsigned int count, i;
621 edge e, e_default;
622 edge_iterator ei;
624 memset (info, 0, sizeof (*info));
626 /* The gimplifier has already sorted the cases by CASE_LOW and ensured there
627 is a default label which is the first in the vector.
628 Collect the bits we can deduce from the CFG. */
629 info->index_expr = gimple_switch_index (swtch);
630 info->switch_bb = gimple_bb (swtch);
631 info->default_bb =
632 label_to_block (CASE_LABEL (gimple_switch_default_label (swtch)));
633 e_default = find_edge (info->switch_bb, info->default_bb);
634 info->default_prob = e_default->probability;
635 info->default_count = e_default->count;
636 FOR_EACH_EDGE (e, ei, info->switch_bb->succs)
637 if (e != e_default)
638 info->other_count += e->count;
640 /* See if there is one common successor block for all branch
641 targets. If it exists, record it in FINAL_BB.
642 Start with the destination of the default case as guess
643 or its destination in case it is a forwarder block. */
644 if (! single_pred_p (e_default->dest))
645 info->final_bb = e_default->dest;
646 else if (single_succ_p (e_default->dest)
647 && ! single_pred_p (single_succ (e_default->dest)))
648 info->final_bb = single_succ (e_default->dest);
649 /* Require that all switch destinations are either that common
650 FINAL_BB or a forwarder to it. */
651 if (info->final_bb)
652 FOR_EACH_EDGE (e, ei, info->switch_bb->succs)
654 if (e->dest == info->final_bb)
655 continue;
657 if (single_pred_p (e->dest)
658 && single_succ_p (e->dest)
659 && single_succ (e->dest) == info->final_bb)
660 continue;
662 info->final_bb = NULL;
663 break;
666 /* Get upper and lower bounds of case values, and the covered range. */
667 min_case = gimple_switch_label (swtch, 1);
668 max_case = gimple_switch_label (swtch, branch_num - 1);
670 info->range_min = CASE_LOW (min_case);
671 if (CASE_HIGH (max_case) != NULL_TREE)
672 info->range_max = CASE_HIGH (max_case);
673 else
674 info->range_max = CASE_LOW (max_case);
676 info->range_size =
677 int_const_binop (MINUS_EXPR, info->range_max, info->range_min);
679 /* Get a count of the number of case labels. Single-valued case labels
680 simply count as one, but a case range counts double, since it may
681 require two compares if it gets lowered as a branching tree. */
682 count = 0;
683 for (i = 1; i < branch_num; i++)
685 tree elt = gimple_switch_label (swtch, i);
686 count++;
687 if (CASE_HIGH (elt)
688 && ! tree_int_cst_equal (CASE_LOW (elt), CASE_HIGH (elt)))
689 count++;
691 info->count = count;
693 /* Get the number of unique non-default targets out of the GIMPLE_SWITCH
694 block. Assume a CFG cleanup would have already removed degenerate
695 switch statements, this allows us to just use EDGE_COUNT. */
696 info->uniq = EDGE_COUNT (gimple_bb (swtch)->succs) - 1;
699 /* Checks whether the range given by individual case statements of the SWTCH
700 switch statement isn't too big and whether the number of branches actually
701 satisfies the size of the new array. */
703 static bool
704 check_range (struct switch_conv_info *info)
706 gcc_assert (info->range_size);
707 if (!tree_fits_uhwi_p (info->range_size))
709 info->reason = "index range way too large or otherwise unusable";
710 return false;
713 if (tree_to_uhwi (info->range_size)
714 > ((unsigned) info->count * SWITCH_CONVERSION_BRANCH_RATIO))
716 info->reason = "the maximum range-branch ratio exceeded";
717 return false;
720 return true;
723 /* Checks whether all but the FINAL_BB basic blocks are empty. */
725 static bool
726 check_all_empty_except_final (struct switch_conv_info *info)
728 edge e;
729 edge_iterator ei;
731 FOR_EACH_EDGE (e, ei, info->switch_bb->succs)
733 if (e->dest == info->final_bb)
734 continue;
736 if (!empty_block_p (e->dest))
738 info->reason = "bad case - a non-final BB not empty";
739 return false;
743 return true;
746 /* This function checks whether all required values in phi nodes in final_bb
747 are constants. Required values are those that correspond to a basic block
748 which is a part of the examined switch statement. It returns true if the
749 phi nodes are OK, otherwise false. */
751 static bool
752 check_final_bb (struct switch_conv_info *info)
754 gphi_iterator gsi;
756 info->phi_count = 0;
757 for (gsi = gsi_start_phis (info->final_bb); !gsi_end_p (gsi); gsi_next (&gsi))
759 gphi *phi = gsi.phi ();
760 unsigned int i;
762 info->phi_count++;
764 for (i = 0; i < gimple_phi_num_args (phi); i++)
766 basic_block bb = gimple_phi_arg_edge (phi, i)->src;
768 if (bb == info->switch_bb
769 || (single_pred_p (bb) && single_pred (bb) == info->switch_bb))
771 tree reloc, val;
773 val = gimple_phi_arg_def (phi, i);
774 if (!is_gimple_ip_invariant (val))
776 info->reason = "non-invariant value from a case";
777 return false; /* Non-invariant argument. */
779 reloc = initializer_constant_valid_p (val, TREE_TYPE (val));
780 if ((flag_pic && reloc != null_pointer_node)
781 || (!flag_pic && reloc == NULL_TREE))
783 if (reloc)
784 info->reason
785 = "value from a case would need runtime relocations";
786 else
787 info->reason
788 = "value from a case is not a valid initializer";
789 return false;
795 return true;
798 /* The following function allocates default_values, target_{in,out}_names and
799 constructors arrays. The last one is also populated with pointers to
800 vectors that will become constructors of new arrays. */
802 static void
803 create_temp_arrays (struct switch_conv_info *info)
805 int i;
807 info->default_values = XCNEWVEC (tree, info->phi_count * 3);
808 /* ??? Macros do not support multi argument templates in their
809 argument list. We create a typedef to work around that problem. */
810 typedef vec<constructor_elt, va_gc> *vec_constructor_elt_gc;
811 info->constructors = XCNEWVEC (vec_constructor_elt_gc, info->phi_count);
812 info->target_inbound_names = info->default_values + info->phi_count;
813 info->target_outbound_names = info->target_inbound_names + info->phi_count;
814 for (i = 0; i < info->phi_count; i++)
815 vec_alloc (info->constructors[i], tree_to_uhwi (info->range_size) + 1);
818 /* Free the arrays created by create_temp_arrays(). The vectors that are
819 created by that function are not freed here, however, because they have
820 already become constructors and must be preserved. */
822 static void
823 free_temp_arrays (struct switch_conv_info *info)
825 XDELETEVEC (info->constructors);
826 XDELETEVEC (info->default_values);
829 /* Populate the array of default values in the order of phi nodes.
830 DEFAULT_CASE is the CASE_LABEL_EXPR for the default switch branch. */
832 static void
833 gather_default_values (tree default_case, struct switch_conv_info *info)
835 gphi_iterator gsi;
836 basic_block bb = label_to_block (CASE_LABEL (default_case));
837 edge e;
838 int i = 0;
840 gcc_assert (CASE_LOW (default_case) == NULL_TREE);
842 if (bb == info->final_bb)
843 e = find_edge (info->switch_bb, bb);
844 else
845 e = single_succ_edge (bb);
847 for (gsi = gsi_start_phis (info->final_bb); !gsi_end_p (gsi); gsi_next (&gsi))
849 gphi *phi = gsi.phi ();
850 tree val = PHI_ARG_DEF_FROM_EDGE (phi, e);
851 gcc_assert (val);
852 info->default_values[i++] = val;
856 /* The following function populates the vectors in the constructors array with
857 future contents of the static arrays. The vectors are populated in the
858 order of phi nodes. SWTCH is the switch statement being converted. */
860 static void
861 build_constructors (gswitch *swtch, struct switch_conv_info *info)
863 unsigned i, branch_num = gimple_switch_num_labels (swtch);
864 tree pos = info->range_min;
866 for (i = 1; i < branch_num; i++)
868 tree cs = gimple_switch_label (swtch, i);
869 basic_block bb = label_to_block (CASE_LABEL (cs));
870 edge e;
871 tree high;
872 gphi_iterator gsi;
873 int j;
875 if (bb == info->final_bb)
876 e = find_edge (info->switch_bb, bb);
877 else
878 e = single_succ_edge (bb);
879 gcc_assert (e);
881 while (tree_int_cst_lt (pos, CASE_LOW (cs)))
883 int k;
884 for (k = 0; k < info->phi_count; k++)
886 constructor_elt elt;
888 elt.index = int_const_binop (MINUS_EXPR, pos, info->range_min);
889 elt.value
890 = unshare_expr_without_location (info->default_values[k]);
891 info->constructors[k]->quick_push (elt);
894 pos = int_const_binop (PLUS_EXPR, pos,
895 build_int_cst (TREE_TYPE (pos), 1));
897 gcc_assert (tree_int_cst_equal (pos, CASE_LOW (cs)));
899 j = 0;
900 if (CASE_HIGH (cs))
901 high = CASE_HIGH (cs);
902 else
903 high = CASE_LOW (cs);
904 for (gsi = gsi_start_phis (info->final_bb);
905 !gsi_end_p (gsi); gsi_next (&gsi))
907 gphi *phi = gsi.phi ();
908 tree val = PHI_ARG_DEF_FROM_EDGE (phi, e);
909 tree low = CASE_LOW (cs);
910 pos = CASE_LOW (cs);
914 constructor_elt elt;
916 elt.index = int_const_binop (MINUS_EXPR, pos, info->range_min);
917 elt.value = unshare_expr_without_location (val);
918 info->constructors[j]->quick_push (elt);
920 pos = int_const_binop (PLUS_EXPR, pos,
921 build_int_cst (TREE_TYPE (pos), 1));
922 } while (!tree_int_cst_lt (high, pos)
923 && tree_int_cst_lt (low, pos));
924 j++;
929 /* If all values in the constructor vector are the same, return the value.
930 Otherwise return NULL_TREE. Not supposed to be called for empty
931 vectors. */
933 static tree
934 constructor_contains_same_values_p (vec<constructor_elt, va_gc> *vec)
936 unsigned int i;
937 tree prev = NULL_TREE;
938 constructor_elt *elt;
940 FOR_EACH_VEC_SAFE_ELT (vec, i, elt)
942 if (!prev)
943 prev = elt->value;
944 else if (!operand_equal_p (elt->value, prev, OEP_ONLY_CONST))
945 return NULL_TREE;
947 return prev;
950 /* Return type which should be used for array elements, either TYPE,
951 or for integral type some smaller integral type that can still hold
952 all the constants. */
954 static tree
955 array_value_type (gswitch *swtch, tree type, int num,
956 struct switch_conv_info *info)
958 unsigned int i, len = vec_safe_length (info->constructors[num]);
959 constructor_elt *elt;
960 machine_mode mode;
961 int sign = 0;
962 tree smaller_type;
964 if (!INTEGRAL_TYPE_P (type))
965 return type;
967 mode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (TYPE_MODE (type)));
968 if (GET_MODE_SIZE (TYPE_MODE (type)) <= GET_MODE_SIZE (mode))
969 return type;
971 if (len < (optimize_bb_for_size_p (gimple_bb (swtch)) ? 2 : 32))
972 return type;
974 FOR_EACH_VEC_SAFE_ELT (info->constructors[num], i, elt)
976 wide_int cst;
978 if (TREE_CODE (elt->value) != INTEGER_CST)
979 return type;
981 cst = elt->value;
982 while (1)
984 unsigned int prec = GET_MODE_BITSIZE (mode);
985 if (prec > HOST_BITS_PER_WIDE_INT)
986 return type;
988 if (sign >= 0 && cst == wi::zext (cst, prec))
990 if (sign == 0 && cst == wi::sext (cst, prec))
991 break;
992 sign = 1;
993 break;
995 if (sign <= 0 && cst == wi::sext (cst, prec))
997 sign = -1;
998 break;
1001 if (sign == 1)
1002 sign = 0;
1004 mode = GET_MODE_WIDER_MODE (mode);
1005 if (mode == VOIDmode
1006 || GET_MODE_SIZE (mode) >= GET_MODE_SIZE (TYPE_MODE (type)))
1007 return type;
1011 if (sign == 0)
1012 sign = TYPE_UNSIGNED (type) ? 1 : -1;
1013 smaller_type = lang_hooks.types.type_for_mode (mode, sign >= 0);
1014 if (GET_MODE_SIZE (TYPE_MODE (type))
1015 <= GET_MODE_SIZE (TYPE_MODE (smaller_type)))
1016 return type;
1018 return smaller_type;
1021 /* Create an appropriate array type and declaration and assemble a static array
1022 variable. Also create a load statement that initializes the variable in
1023 question with a value from the static array. SWTCH is the switch statement
1024 being converted, NUM is the index to arrays of constructors, default values
1025 and target SSA names for this particular array. ARR_INDEX_TYPE is the type
1026 of the index of the new array, PHI is the phi node of the final BB that
1027 corresponds to the value that will be loaded from the created array. TIDX
1028 is an ssa name of a temporary variable holding the index for loads from the
1029 new array. */
1031 static void
1032 build_one_array (gswitch *swtch, int num, tree arr_index_type,
1033 gphi *phi, tree tidx, struct switch_conv_info *info)
1035 tree name, cst;
1036 gimple load;
1037 gimple_stmt_iterator gsi = gsi_for_stmt (swtch);
1038 location_t loc = gimple_location (swtch);
1040 gcc_assert (info->default_values[num]);
1042 name = copy_ssa_name (PHI_RESULT (phi));
1043 info->target_inbound_names[num] = name;
1045 cst = constructor_contains_same_values_p (info->constructors[num]);
1046 if (cst)
1047 load = gimple_build_assign (name, cst);
1048 else
1050 tree array_type, ctor, decl, value_type, fetch, default_type;
1052 default_type = TREE_TYPE (info->default_values[num]);
1053 value_type = array_value_type (swtch, default_type, num, info);
1054 array_type = build_array_type (value_type, arr_index_type);
1055 if (default_type != value_type)
1057 unsigned int i;
1058 constructor_elt *elt;
1060 FOR_EACH_VEC_SAFE_ELT (info->constructors[num], i, elt)
1061 elt->value = fold_convert (value_type, elt->value);
1063 ctor = build_constructor (array_type, info->constructors[num]);
1064 TREE_CONSTANT (ctor) = true;
1065 TREE_STATIC (ctor) = true;
1067 decl = build_decl (loc, VAR_DECL, NULL_TREE, array_type);
1068 TREE_STATIC (decl) = 1;
1069 DECL_INITIAL (decl) = ctor;
1071 DECL_NAME (decl) = create_tmp_var_name ("CSWTCH");
1072 DECL_ARTIFICIAL (decl) = 1;
1073 DECL_IGNORED_P (decl) = 1;
1074 TREE_CONSTANT (decl) = 1;
1075 TREE_READONLY (decl) = 1;
1076 DECL_IGNORED_P (decl) = 1;
1077 varpool_node::finalize_decl (decl);
1079 fetch = build4 (ARRAY_REF, value_type, decl, tidx, NULL_TREE,
1080 NULL_TREE);
1081 if (default_type != value_type)
1083 fetch = fold_convert (default_type, fetch);
1084 fetch = force_gimple_operand_gsi (&gsi, fetch, true, NULL_TREE,
1085 true, GSI_SAME_STMT);
1087 load = gimple_build_assign (name, fetch);
1090 gsi_insert_before (&gsi, load, GSI_SAME_STMT);
1091 update_stmt (load);
1092 info->arr_ref_last = load;
1095 /* Builds and initializes static arrays initialized with values gathered from
1096 the SWTCH switch statement. Also creates statements that load values from
1097 them. */
1099 static void
1100 build_arrays (gswitch *swtch, struct switch_conv_info *info)
1102 tree arr_index_type;
1103 tree tidx, sub, utype;
1104 gimple stmt;
1105 gimple_stmt_iterator gsi;
1106 gphi_iterator gpi;
1107 int i;
1108 location_t loc = gimple_location (swtch);
1110 gsi = gsi_for_stmt (swtch);
1112 /* Make sure we do not generate arithmetics in a subrange. */
1113 utype = TREE_TYPE (info->index_expr);
1114 if (TREE_TYPE (utype))
1115 utype = lang_hooks.types.type_for_mode (TYPE_MODE (TREE_TYPE (utype)), 1);
1116 else
1117 utype = lang_hooks.types.type_for_mode (TYPE_MODE (utype), 1);
1119 arr_index_type = build_index_type (info->range_size);
1120 tidx = make_ssa_name (utype);
1121 sub = fold_build2_loc (loc, MINUS_EXPR, utype,
1122 fold_convert_loc (loc, utype, info->index_expr),
1123 fold_convert_loc (loc, utype, info->range_min));
1124 sub = force_gimple_operand_gsi (&gsi, sub,
1125 false, NULL, true, GSI_SAME_STMT);
1126 stmt = gimple_build_assign (tidx, sub);
1128 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
1129 update_stmt (stmt);
1130 info->arr_ref_first = stmt;
1132 for (gpi = gsi_start_phis (info->final_bb), i = 0;
1133 !gsi_end_p (gpi); gsi_next (&gpi), i++)
1134 build_one_array (swtch, i, arr_index_type, gpi.phi (), tidx, info);
1137 /* Generates and appropriately inserts loads of default values at the position
1138 given by BSI. Returns the last inserted statement. */
1140 static gassign *
1141 gen_def_assigns (gimple_stmt_iterator *gsi, struct switch_conv_info *info)
1143 int i;
1144 gassign *assign = NULL;
1146 for (i = 0; i < info->phi_count; i++)
1148 tree name = copy_ssa_name (info->target_inbound_names[i]);
1149 info->target_outbound_names[i] = name;
1150 assign = gimple_build_assign (name, info->default_values[i]);
1151 gsi_insert_before (gsi, assign, GSI_SAME_STMT);
1152 update_stmt (assign);
1154 return assign;
1157 /* Deletes the unused bbs and edges that now contain the switch statement and
1158 its empty branch bbs. BBD is the now dead BB containing the original switch
1159 statement, FINAL is the last BB of the converted switch statement (in terms
1160 of succession). */
1162 static void
1163 prune_bbs (basic_block bbd, basic_block final)
1165 edge_iterator ei;
1166 edge e;
1168 for (ei = ei_start (bbd->succs); (e = ei_safe_edge (ei)); )
1170 basic_block bb;
1171 bb = e->dest;
1172 remove_edge (e);
1173 if (bb != final)
1174 delete_basic_block (bb);
1176 delete_basic_block (bbd);
1179 /* Add values to phi nodes in final_bb for the two new edges. E1F is the edge
1180 from the basic block loading values from an array and E2F from the basic
1181 block loading default values. BBF is the last switch basic block (see the
1182 bbf description in the comment below). */
1184 static void
1185 fix_phi_nodes (edge e1f, edge e2f, basic_block bbf,
1186 struct switch_conv_info *info)
1188 gphi_iterator gsi;
1189 int i;
1191 for (gsi = gsi_start_phis (bbf), i = 0;
1192 !gsi_end_p (gsi); gsi_next (&gsi), i++)
1194 gphi *phi = gsi.phi ();
1195 add_phi_arg (phi, info->target_inbound_names[i], e1f, UNKNOWN_LOCATION);
1196 add_phi_arg (phi, info->target_outbound_names[i], e2f, UNKNOWN_LOCATION);
1200 /* Creates a check whether the switch expression value actually falls into the
1201 range given by all the cases. If it does not, the temporaries are loaded
1202 with default values instead. SWTCH is the switch statement being converted.
1204 bb0 is the bb with the switch statement, however, we'll end it with a
1205 condition instead.
1207 bb1 is the bb to be used when the range check went ok. It is derived from
1208 the switch BB
1210 bb2 is the bb taken when the expression evaluated outside of the range
1211 covered by the created arrays. It is populated by loads of default
1212 values.
1214 bbF is a fall through for both bb1 and bb2 and contains exactly what
1215 originally followed the switch statement.
1217 bbD contains the switch statement (in the end). It is unreachable but we
1218 still need to strip off its edges.
1221 static void
1222 gen_inbound_check (gswitch *swtch, struct switch_conv_info *info)
1224 tree label_decl1 = create_artificial_label (UNKNOWN_LOCATION);
1225 tree label_decl2 = create_artificial_label (UNKNOWN_LOCATION);
1226 tree label_decl3 = create_artificial_label (UNKNOWN_LOCATION);
1227 glabel *label1, *label2, *label3;
1228 tree utype, tidx;
1229 tree bound;
1231 gcond *cond_stmt;
1233 gassign *last_assign;
1234 gimple_stmt_iterator gsi;
1235 basic_block bb0, bb1, bb2, bbf, bbd;
1236 edge e01, e02, e21, e1d, e1f, e2f;
1237 location_t loc = gimple_location (swtch);
1239 gcc_assert (info->default_values);
1241 bb0 = gimple_bb (swtch);
1243 tidx = gimple_assign_lhs (info->arr_ref_first);
1244 utype = TREE_TYPE (tidx);
1246 /* (end of) block 0 */
1247 gsi = gsi_for_stmt (info->arr_ref_first);
1248 gsi_next (&gsi);
1250 bound = fold_convert_loc (loc, utype, info->range_size);
1251 cond_stmt = gimple_build_cond (LE_EXPR, tidx, bound, NULL_TREE, NULL_TREE);
1252 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1253 update_stmt (cond_stmt);
1255 /* block 2 */
1256 label2 = gimple_build_label (label_decl2);
1257 gsi_insert_before (&gsi, label2, GSI_SAME_STMT);
1258 last_assign = gen_def_assigns (&gsi, info);
1260 /* block 1 */
1261 label1 = gimple_build_label (label_decl1);
1262 gsi_insert_before (&gsi, label1, GSI_SAME_STMT);
1264 /* block F */
1265 gsi = gsi_start_bb (info->final_bb);
1266 label3 = gimple_build_label (label_decl3);
1267 gsi_insert_before (&gsi, label3, GSI_SAME_STMT);
1269 /* cfg fix */
1270 e02 = split_block (bb0, cond_stmt);
1271 bb2 = e02->dest;
1273 e21 = split_block (bb2, last_assign);
1274 bb1 = e21->dest;
1275 remove_edge (e21);
1277 e1d = split_block (bb1, info->arr_ref_last);
1278 bbd = e1d->dest;
1279 remove_edge (e1d);
1281 /* flags and profiles of the edge for in-range values */
1282 e01 = make_edge (bb0, bb1, EDGE_TRUE_VALUE);
1283 e01->probability = REG_BR_PROB_BASE - info->default_prob;
1284 e01->count = info->other_count;
1286 /* flags and profiles of the edge taking care of out-of-range values */
1287 e02->flags &= ~EDGE_FALLTHRU;
1288 e02->flags |= EDGE_FALSE_VALUE;
1289 e02->probability = info->default_prob;
1290 e02->count = info->default_count;
1292 bbf = info->final_bb;
1294 e1f = make_edge (bb1, bbf, EDGE_FALLTHRU);
1295 e1f->probability = REG_BR_PROB_BASE;
1296 e1f->count = info->other_count;
1298 e2f = make_edge (bb2, bbf, EDGE_FALLTHRU);
1299 e2f->probability = REG_BR_PROB_BASE;
1300 e2f->count = info->default_count;
1302 /* frequencies of the new BBs */
1303 bb1->frequency = EDGE_FREQUENCY (e01);
1304 bb2->frequency = EDGE_FREQUENCY (e02);
1305 bbf->frequency = EDGE_FREQUENCY (e1f) + EDGE_FREQUENCY (e2f);
1307 /* Tidy blocks that have become unreachable. */
1308 prune_bbs (bbd, info->final_bb);
1310 /* Fixup the PHI nodes in bbF. */
1311 fix_phi_nodes (e1f, e2f, bbf, info);
1313 /* Fix the dominator tree, if it is available. */
1314 if (dom_info_available_p (CDI_DOMINATORS))
1316 vec<basic_block> bbs_to_fix_dom;
1318 set_immediate_dominator (CDI_DOMINATORS, bb1, bb0);
1319 set_immediate_dominator (CDI_DOMINATORS, bb2, bb0);
1320 if (! get_immediate_dominator (CDI_DOMINATORS, bbf))
1321 /* If bbD was the immediate dominator ... */
1322 set_immediate_dominator (CDI_DOMINATORS, bbf, bb0);
1324 bbs_to_fix_dom.create (4);
1325 bbs_to_fix_dom.quick_push (bb0);
1326 bbs_to_fix_dom.quick_push (bb1);
1327 bbs_to_fix_dom.quick_push (bb2);
1328 bbs_to_fix_dom.quick_push (bbf);
1330 iterate_fix_dominators (CDI_DOMINATORS, bbs_to_fix_dom, true);
1331 bbs_to_fix_dom.release ();
1335 /* The following function is invoked on every switch statement (the current one
1336 is given in SWTCH) and runs the individual phases of switch conversion on it
1337 one after another until one fails or the conversion is completed.
1338 Returns NULL on success, or a pointer to a string with the reason why the
1339 conversion failed. */
1341 static const char *
1342 process_switch (gswitch *swtch)
1344 struct switch_conv_info info;
1346 /* Group case labels so that we get the right results from the heuristics
1347 that decide on the code generation approach for this switch. */
1348 group_case_labels_stmt (swtch);
1350 /* If this switch is now a degenerate case with only a default label,
1351 there is nothing left for us to do. */
1352 if (gimple_switch_num_labels (swtch) < 2)
1353 return "switch is a degenerate case";
1355 collect_switch_conv_info (swtch, &info);
1357 /* No error markers should reach here (they should be filtered out
1358 during gimplification). */
1359 gcc_checking_assert (TREE_TYPE (info.index_expr) != error_mark_node);
1361 /* A switch on a constant should have been optimized in tree-cfg-cleanup. */
1362 gcc_checking_assert (! TREE_CONSTANT (info.index_expr));
1364 if (info.uniq <= MAX_CASE_BIT_TESTS)
1366 if (expand_switch_using_bit_tests_p (info.range_size,
1367 info.uniq, info.count,
1368 optimize_bb_for_speed_p
1369 (gimple_bb (swtch))))
1371 if (dump_file)
1372 fputs (" expanding as bit test is preferable\n", dump_file);
1373 emit_case_bit_tests (swtch, info.index_expr, info.range_min,
1374 info.range_size, info.range_max);
1375 loops_state_set (LOOPS_NEED_FIXUP);
1376 return NULL;
1379 if (info.uniq <= 2)
1380 /* This will be expanded as a decision tree in stmt.c:expand_case. */
1381 return " expanding as jumps is preferable";
1384 /* If there is no common successor, we cannot do the transformation. */
1385 if (! info.final_bb)
1386 return "no common successor to all case label target blocks found";
1388 /* Check the case label values are within reasonable range: */
1389 if (!check_range (&info))
1391 gcc_assert (info.reason);
1392 return info.reason;
1395 /* For all the cases, see whether they are empty, the assignments they
1396 represent constant and so on... */
1397 if (! check_all_empty_except_final (&info))
1399 gcc_assert (info.reason);
1400 return info.reason;
1402 if (!check_final_bb (&info))
1404 gcc_assert (info.reason);
1405 return info.reason;
1408 /* At this point all checks have passed and we can proceed with the
1409 transformation. */
1411 create_temp_arrays (&info);
1412 gather_default_values (gimple_switch_default_label (swtch), &info);
1413 build_constructors (swtch, &info);
1415 build_arrays (swtch, &info); /* Build the static arrays and assignments. */
1416 gen_inbound_check (swtch, &info); /* Build the bounds check. */
1418 /* Cleanup: */
1419 free_temp_arrays (&info);
1420 return NULL;
1423 /* The main function of the pass scans statements for switches and invokes
1424 process_switch on them. */
1426 namespace {
1428 const pass_data pass_data_convert_switch =
1430 GIMPLE_PASS, /* type */
1431 "switchconv", /* name */
1432 OPTGROUP_NONE, /* optinfo_flags */
1433 TV_TREE_SWITCH_CONVERSION, /* tv_id */
1434 ( PROP_cfg | PROP_ssa ), /* properties_required */
1435 0, /* properties_provided */
1436 0, /* properties_destroyed */
1437 0, /* todo_flags_start */
1438 TODO_update_ssa, /* todo_flags_finish */
1441 class pass_convert_switch : public gimple_opt_pass
1443 public:
1444 pass_convert_switch (gcc::context *ctxt)
1445 : gimple_opt_pass (pass_data_convert_switch, ctxt)
1448 /* opt_pass methods: */
1449 virtual bool gate (function *) { return flag_tree_switch_conversion != 0; }
1450 virtual unsigned int execute (function *);
1452 }; // class pass_convert_switch
1454 unsigned int
1455 pass_convert_switch::execute (function *fun)
1457 basic_block bb;
1459 FOR_EACH_BB_FN (bb, fun)
1461 const char *failure_reason;
1462 gimple stmt = last_stmt (bb);
1463 if (stmt && gimple_code (stmt) == GIMPLE_SWITCH)
1465 if (dump_file)
1467 expanded_location loc = expand_location (gimple_location (stmt));
1469 fprintf (dump_file, "beginning to process the following "
1470 "SWITCH statement (%s:%d) : ------- \n",
1471 loc.file, loc.line);
1472 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1473 putc ('\n', dump_file);
1476 failure_reason = process_switch (as_a <gswitch *> (stmt));
1477 if (! failure_reason)
1479 if (dump_file)
1481 fputs ("Switch converted\n", dump_file);
1482 fputs ("--------------------------------\n", dump_file);
1485 /* Make no effort to update the post-dominator tree. It is actually not
1486 that hard for the transformations we have performed, but it is not
1487 supported by iterate_fix_dominators. */
1488 free_dominance_info (CDI_POST_DOMINATORS);
1490 else
1492 if (dump_file)
1494 fputs ("Bailing out - ", dump_file);
1495 fputs (failure_reason, dump_file);
1496 fputs ("\n--------------------------------\n", dump_file);
1502 return 0;
1505 } // anon namespace
1507 gimple_opt_pass *
1508 make_pass_convert_switch (gcc::context *ctxt)
1510 return new pass_convert_switch (ctxt);