* c-c++-common/ubsan/float-cast-overflow-6.c: Add i?86-*-* target.
[official-gcc.git] / gcc / tree-switch-conversion.c
blob13a54106c494aaf1d802a577ed0050e6340adfc4
1 /* Lower GIMPLE_SWITCH expressions to something more efficient than
2 a jump table.
3 Copyright (C) 2006-2014 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 "tm.h"
29 #include "line-map.h"
30 #include "params.h"
31 #include "flags.h"
32 #include "tree.h"
33 #include "varasm.h"
34 #include "stor-layout.h"
35 #include "predict.h"
36 #include "vec.h"
37 #include "hashtab.h"
38 #include "hash-set.h"
39 #include "machmode.h"
40 #include "hard-reg-set.h"
41 #include "input.h"
42 #include "function.h"
43 #include "dominance.h"
44 #include "cfg.h"
45 #include "cfganal.h"
46 #include "basic-block.h"
47 #include "tree-ssa-alias.h"
48 #include "internal-fn.h"
49 #include "gimple-expr.h"
50 #include "is-a.h"
51 #include "gimple.h"
52 #include "gimplify.h"
53 #include "gimple-iterator.h"
54 #include "gimplify-me.h"
55 #include "gimple-ssa.h"
56 #include "hash-map.h"
57 #include "plugin-api.h"
58 #include "ipa-ref.h"
59 #include "cgraph.h"
60 #include "tree-cfg.h"
61 #include "tree-phinodes.h"
62 #include "stringpool.h"
63 #include "tree-ssanames.h"
64 #include "tree-pass.h"
65 #include "gimple-pretty-print.h"
66 #include "cfgloop.h"
68 /* ??? For lang_hooks.types.type_for_mode, but is there a word_mode
69 type in the GIMPLE type system that is language-independent? */
70 #include "langhooks.h"
72 /* Need to include expr.h and optabs.h for lshift_cheap_p. */
73 #include "expr.h"
74 #include "insn-codes.h"
75 #include "optabs.h"
77 /* Maximum number of case bit tests.
78 FIXME: This should be derived from PARAM_CASE_VALUES_THRESHOLD and
79 targetm.case_values_threshold(), or be its own param. */
80 #define MAX_CASE_BIT_TESTS 3
82 /* Split the basic block at the statement pointed to by GSIP, and insert
83 a branch to the target basic block of E_TRUE conditional on tree
84 expression COND.
86 It is assumed that there is already an edge from the to-be-split
87 basic block to E_TRUE->dest block. This edge is removed, and the
88 profile information on the edge is re-used for the new conditional
89 jump.
91 The CFG is updated. The dominator tree will not be valid after
92 this transformation, but the immediate dominators are updated if
93 UPDATE_DOMINATORS is true.
95 Returns the newly created basic block. */
97 static basic_block
98 hoist_edge_and_branch_if_true (gimple_stmt_iterator *gsip,
99 tree cond, edge e_true,
100 bool update_dominators)
102 tree tmp;
103 gimple cond_stmt;
104 edge e_false;
105 basic_block new_bb, split_bb = gsi_bb (*gsip);
106 bool dominated_e_true = false;
108 gcc_assert (e_true->src == split_bb);
110 if (update_dominators
111 && get_immediate_dominator (CDI_DOMINATORS, e_true->dest) == split_bb)
112 dominated_e_true = true;
114 tmp = force_gimple_operand_gsi (gsip, cond, /*simple=*/true, NULL,
115 /*before=*/true, GSI_SAME_STMT);
116 cond_stmt = gimple_build_cond_from_tree (tmp, NULL_TREE, NULL_TREE);
117 gsi_insert_before (gsip, cond_stmt, GSI_SAME_STMT);
119 e_false = split_block (split_bb, cond_stmt);
120 new_bb = e_false->dest;
121 redirect_edge_pred (e_true, split_bb);
123 e_true->flags &= ~EDGE_FALLTHRU;
124 e_true->flags |= EDGE_TRUE_VALUE;
126 e_false->flags &= ~EDGE_FALLTHRU;
127 e_false->flags |= EDGE_FALSE_VALUE;
128 e_false->probability = REG_BR_PROB_BASE - e_true->probability;
129 e_false->count = split_bb->count - e_true->count;
130 new_bb->count = e_false->count;
132 if (update_dominators)
134 if (dominated_e_true)
135 set_immediate_dominator (CDI_DOMINATORS, e_true->dest, split_bb);
136 set_immediate_dominator (CDI_DOMINATORS, e_false->dest, split_bb);
139 return new_bb;
143 /* Return true if a switch should be expanded as a bit test.
144 RANGE is the difference between highest and lowest case.
145 UNIQ is number of unique case node targets, not counting the default case.
146 COUNT is the number of comparisons needed, not counting the default case. */
148 static bool
149 expand_switch_using_bit_tests_p (tree range,
150 unsigned int uniq,
151 unsigned int count, bool speed_p)
153 return (((uniq == 1 && count >= 3)
154 || (uniq == 2 && count >= 5)
155 || (uniq == 3 && count >= 6))
156 && lshift_cheap_p (speed_p)
157 && compare_tree_int (range, GET_MODE_BITSIZE (word_mode)) < 0
158 && compare_tree_int (range, 0) > 0);
161 /* Implement switch statements with bit tests
163 A GIMPLE switch statement can be expanded to a short sequence of bit-wise
164 comparisons. "switch(x)" is converted into "if ((1 << (x-MINVAL)) & CST)"
165 where CST and MINVAL are integer constants. This is better than a series
166 of compare-and-banch insns in some cases, e.g. we can implement:
168 if ((x==4) || (x==6) || (x==9) || (x==11))
170 as a single bit test:
172 if ((1<<x) & ((1<<4)|(1<<6)|(1<<9)|(1<<11)))
174 This transformation is only applied if the number of case targets is small,
175 if CST constains at least 3 bits, and "1 << x" is cheap. The bit tests are
176 performed in "word_mode".
178 The following example shows the code the transformation generates:
180 int bar(int x)
182 switch (x)
184 case '0': case '1': case '2': case '3': case '4':
185 case '5': case '6': case '7': case '8': case '9':
186 case 'A': case 'B': case 'C': case 'D': case 'E':
187 case 'F':
188 return 1;
190 return 0;
195 bar (int x)
197 tmp1 = x - 48;
198 if (tmp1 > (70 - 48)) goto L2;
199 tmp2 = 1 << tmp1;
200 tmp3 = 0b11111100000001111111111;
201 if ((tmp2 & tmp3) != 0) goto L1 ; else goto L2;
203 return 1;
205 return 0;
208 TODO: There are still some improvements to this transformation that could
209 be implemented:
211 * A narrower mode than word_mode could be used if that is cheaper, e.g.
212 for x86_64 where a narrower-mode shift may result in smaller code.
214 * The compounded constant could be shifted rather than the one. The
215 test would be either on the sign bit or on the least significant bit,
216 depending on the direction of the shift. On some machines, the test
217 for the branch would be free if the bit to test is already set by the
218 shift operation.
220 This transformation was contributed by Roger Sayle, see this e-mail:
221 http://gcc.gnu.org/ml/gcc-patches/2003-01/msg01950.html
224 /* A case_bit_test represents a set of case nodes that may be
225 selected from using a bit-wise comparison. HI and LO hold
226 the integer to be tested against, TARGET_EDGE contains the
227 edge to the basic block to jump to upon success and BITS
228 counts the number of case nodes handled by this test,
229 typically the number of bits set in HI:LO. The LABEL field
230 is used to quickly identify all cases in this set without
231 looking at label_to_block for every case label. */
233 struct case_bit_test
235 wide_int mask;
236 edge target_edge;
237 tree label;
238 int bits;
241 /* Comparison function for qsort to order bit tests by decreasing
242 probability of execution. Our best guess comes from a measured
243 profile. If the profile counts are equal, break even on the
244 number of case nodes, i.e. the node with the most cases gets
245 tested first.
247 TODO: Actually this currently runs before a profile is available.
248 Therefore the case-as-bit-tests transformation should be done
249 later in the pass pipeline, or something along the lines of
250 "Efficient and effective branch reordering using profile data"
251 (Yang et. al., 2002) should be implemented (although, how good
252 is a paper is called "Efficient and effective ..." when the
253 latter is implied by the former, but oh well...). */
255 static int
256 case_bit_test_cmp (const void *p1, const void *p2)
258 const struct case_bit_test *const d1 = (const struct case_bit_test *) p1;
259 const struct case_bit_test *const d2 = (const struct case_bit_test *) p2;
261 if (d2->target_edge->count != d1->target_edge->count)
262 return d2->target_edge->count - d1->target_edge->count;
263 if (d2->bits != d1->bits)
264 return d2->bits - d1->bits;
266 /* Stabilize the sort. */
267 return LABEL_DECL_UID (d2->label) - LABEL_DECL_UID (d1->label);
270 /* Expand a switch statement by a short sequence of bit-wise
271 comparisons. "switch(x)" is effectively converted into
272 "if ((1 << (x-MINVAL)) & CST)" where CST and MINVAL are
273 integer constants.
275 INDEX_EXPR is the value being switched on.
277 MINVAL is the lowest case value of in the case nodes,
278 and RANGE is highest value minus MINVAL. MINVAL and RANGE
279 are not guaranteed to be of the same type as INDEX_EXPR
280 (the gimplifier doesn't change the type of case label values,
281 and MINVAL and RANGE are derived from those values).
282 MAXVAL is MINVAL + RANGE.
284 There *MUST* be MAX_CASE_BIT_TESTS or less unique case
285 node targets. */
287 static void
288 emit_case_bit_tests (gimple swtch, tree index_expr,
289 tree minval, tree range, tree maxval)
291 struct case_bit_test test[MAX_CASE_BIT_TESTS];
292 unsigned int i, j, k;
293 unsigned int count;
295 basic_block switch_bb = gimple_bb (swtch);
296 basic_block default_bb, new_default_bb, new_bb;
297 edge default_edge;
298 bool update_dom = dom_info_available_p (CDI_DOMINATORS);
300 vec<basic_block> bbs_to_fix_dom = vNULL;
302 tree index_type = TREE_TYPE (index_expr);
303 tree unsigned_index_type = unsigned_type_for (index_type);
304 unsigned int branch_num = gimple_switch_num_labels (swtch);
306 gimple_stmt_iterator gsi;
307 gimple shift_stmt;
309 tree idx, tmp, csui;
310 tree word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
311 tree word_mode_zero = fold_convert (word_type_node, integer_zero_node);
312 tree word_mode_one = fold_convert (word_type_node, integer_one_node);
313 int prec = TYPE_PRECISION (word_type_node);
314 wide_int wone = wi::one (prec);
316 memset (&test, 0, sizeof (test));
318 /* Get the edge for the default case. */
319 tmp = gimple_switch_default_label (swtch);
320 default_bb = label_to_block (CASE_LABEL (tmp));
321 default_edge = find_edge (switch_bb, default_bb);
323 /* Go through all case labels, and collect the case labels, profile
324 counts, and other information we need to build the branch tests. */
325 count = 0;
326 for (i = 1; i < branch_num; i++)
328 unsigned int lo, hi;
329 tree cs = gimple_switch_label (swtch, i);
330 tree label = CASE_LABEL (cs);
331 edge e = find_edge (switch_bb, label_to_block (label));
332 for (k = 0; k < count; k++)
333 if (e == test[k].target_edge)
334 break;
336 if (k == count)
338 gcc_checking_assert (count < MAX_CASE_BIT_TESTS);
339 test[k].mask = wi::zero (prec);
340 test[k].target_edge = e;
341 test[k].label = label;
342 test[k].bits = 1;
343 count++;
345 else
346 test[k].bits++;
348 lo = tree_to_uhwi (int_const_binop (MINUS_EXPR,
349 CASE_LOW (cs), minval));
350 if (CASE_HIGH (cs) == NULL_TREE)
351 hi = lo;
352 else
353 hi = tree_to_uhwi (int_const_binop (MINUS_EXPR,
354 CASE_HIGH (cs), minval));
356 for (j = lo; j <= hi; j++)
357 test[k].mask |= wi::lshift (wone, j);
360 qsort (test, count, sizeof (*test), case_bit_test_cmp);
362 /* If all values are in the 0 .. BITS_PER_WORD-1 range, we can get rid of
363 the minval subtractions, but it might make the mask constants more
364 expensive. So, compare the costs. */
365 if (compare_tree_int (minval, 0) > 0
366 && compare_tree_int (maxval, GET_MODE_BITSIZE (word_mode)) < 0)
368 int cost_diff;
369 HOST_WIDE_INT m = tree_to_uhwi (minval);
370 rtx reg = gen_raw_REG (word_mode, 10000);
371 bool speed_p = optimize_bb_for_speed_p (gimple_bb (swtch));
372 cost_diff = set_rtx_cost (gen_rtx_PLUS (word_mode, reg,
373 GEN_INT (-m)), speed_p);
374 for (i = 0; i < count; i++)
376 rtx r = immed_wide_int_const (test[i].mask, word_mode);
377 cost_diff += set_src_cost (gen_rtx_AND (word_mode, reg, r), speed_p);
378 r = immed_wide_int_const (wi::lshift (test[i].mask, m), word_mode);
379 cost_diff -= set_src_cost (gen_rtx_AND (word_mode, reg, r), speed_p);
381 if (cost_diff > 0)
383 for (i = 0; i < count; i++)
384 test[i].mask = wi::lshift (test[i].mask, m);
385 minval = build_zero_cst (TREE_TYPE (minval));
386 range = maxval;
390 /* We generate two jumps to the default case label.
391 Split the default edge, so that we don't have to do any PHI node
392 updating. */
393 new_default_bb = split_edge (default_edge);
395 if (update_dom)
397 bbs_to_fix_dom.create (10);
398 bbs_to_fix_dom.quick_push (switch_bb);
399 bbs_to_fix_dom.quick_push (default_bb);
400 bbs_to_fix_dom.quick_push (new_default_bb);
403 /* Now build the test-and-branch code. */
405 gsi = gsi_last_bb (switch_bb);
407 /* idx = (unsigned)x - minval. */
408 idx = fold_convert (unsigned_index_type, index_expr);
409 idx = fold_build2 (MINUS_EXPR, unsigned_index_type, idx,
410 fold_convert (unsigned_index_type, minval));
411 idx = force_gimple_operand_gsi (&gsi, idx,
412 /*simple=*/true, NULL_TREE,
413 /*before=*/true, GSI_SAME_STMT);
415 /* if (idx > range) goto default */
416 range = force_gimple_operand_gsi (&gsi,
417 fold_convert (unsigned_index_type, range),
418 /*simple=*/true, NULL_TREE,
419 /*before=*/true, GSI_SAME_STMT);
420 tmp = fold_build2 (GT_EXPR, boolean_type_node, idx, range);
421 new_bb = hoist_edge_and_branch_if_true (&gsi, tmp, default_edge, update_dom);
422 if (update_dom)
423 bbs_to_fix_dom.quick_push (new_bb);
424 gcc_assert (gimple_bb (swtch) == new_bb);
425 gsi = gsi_last_bb (new_bb);
427 /* Any blocks dominated by the GIMPLE_SWITCH, but that are not successors
428 of NEW_BB, are still immediately dominated by SWITCH_BB. Make it so. */
429 if (update_dom)
431 vec<basic_block> dom_bbs;
432 basic_block dom_son;
434 dom_bbs = get_dominated_by (CDI_DOMINATORS, new_bb);
435 FOR_EACH_VEC_ELT (dom_bbs, i, dom_son)
437 edge e = find_edge (new_bb, dom_son);
438 if (e && single_pred_p (e->dest))
439 continue;
440 set_immediate_dominator (CDI_DOMINATORS, dom_son, switch_bb);
441 bbs_to_fix_dom.safe_push (dom_son);
443 dom_bbs.release ();
446 /* csui = (1 << (word_mode) idx) */
447 csui = make_ssa_name (word_type_node, NULL);
448 tmp = fold_build2 (LSHIFT_EXPR, word_type_node, word_mode_one,
449 fold_convert (word_type_node, idx));
450 tmp = force_gimple_operand_gsi (&gsi, tmp,
451 /*simple=*/false, NULL_TREE,
452 /*before=*/true, GSI_SAME_STMT);
453 shift_stmt = gimple_build_assign (csui, tmp);
454 gsi_insert_before (&gsi, shift_stmt, GSI_SAME_STMT);
455 update_stmt (shift_stmt);
457 /* for each unique set of cases:
458 if (const & csui) goto target */
459 for (k = 0; k < count; k++)
461 tmp = wide_int_to_tree (word_type_node, test[k].mask);
462 tmp = fold_build2 (BIT_AND_EXPR, word_type_node, csui, tmp);
463 tmp = force_gimple_operand_gsi (&gsi, tmp,
464 /*simple=*/true, NULL_TREE,
465 /*before=*/true, GSI_SAME_STMT);
466 tmp = fold_build2 (NE_EXPR, boolean_type_node, tmp, word_mode_zero);
467 new_bb = hoist_edge_and_branch_if_true (&gsi, tmp, test[k].target_edge,
468 update_dom);
469 if (update_dom)
470 bbs_to_fix_dom.safe_push (new_bb);
471 gcc_assert (gimple_bb (swtch) == new_bb);
472 gsi = gsi_last_bb (new_bb);
475 /* We should have removed all edges now. */
476 gcc_assert (EDGE_COUNT (gsi_bb (gsi)->succs) == 0);
478 /* If nothing matched, go to the default label. */
479 make_edge (gsi_bb (gsi), new_default_bb, EDGE_FALLTHRU);
481 /* The GIMPLE_SWITCH is now redundant. */
482 gsi_remove (&gsi, true);
484 if (update_dom)
486 /* Fix up the dominator tree. */
487 iterate_fix_dominators (CDI_DOMINATORS, bbs_to_fix_dom, true);
488 bbs_to_fix_dom.release ();
493 Switch initialization conversion
495 The following pass changes simple initializations of scalars in a switch
496 statement into initializations from a static array. Obviously, the values
497 must be constant and known at compile time and a default branch must be
498 provided. For example, the following code:
500 int a,b;
502 switch (argc)
504 case 1:
505 case 2:
506 a_1 = 8;
507 b_1 = 6;
508 break;
509 case 3:
510 a_2 = 9;
511 b_2 = 5;
512 break;
513 case 12:
514 a_3 = 10;
515 b_3 = 4;
516 break;
517 default:
518 a_4 = 16;
519 b_4 = 1;
520 break;
522 a_5 = PHI <a_1, a_2, a_3, a_4>
523 b_5 = PHI <b_1, b_2, b_3, b_4>
526 is changed into:
528 static const int = CSWTCH01[] = {6, 6, 5, 1, 1, 1, 1, 1, 1, 1, 1, 4};
529 static const int = CSWTCH02[] = {8, 8, 9, 16, 16, 16, 16, 16, 16, 16,
530 16, 16, 10};
532 if (((unsigned) argc) - 1 < 11)
534 a_6 = CSWTCH02[argc - 1];
535 b_6 = CSWTCH01[argc - 1];
537 else
539 a_7 = 16;
540 b_7 = 1;
542 a_5 = PHI <a_6, a_7>
543 b_b = PHI <b_6, b_7>
545 There are further constraints. Specifically, the range of values across all
546 case labels must not be bigger than SWITCH_CONVERSION_BRANCH_RATIO (default
547 eight) times the number of the actual switch branches.
549 This transformation was contributed by Martin Jambor, see this e-mail:
550 http://gcc.gnu.org/ml/gcc-patches/2008-07/msg00011.html */
552 /* The main structure of the pass. */
553 struct switch_conv_info
555 /* The expression used to decide the switch branch. */
556 tree index_expr;
558 /* The following integer constants store the minimum and maximum value
559 covered by the case labels. */
560 tree range_min;
561 tree range_max;
563 /* The difference between the above two numbers. Stored here because it
564 is used in all the conversion heuristics, as well as for some of the
565 transformation, and it is expensive to re-compute it all the time. */
566 tree range_size;
568 /* Basic block that contains the actual GIMPLE_SWITCH. */
569 basic_block switch_bb;
571 /* Basic block that is the target of the default case. */
572 basic_block default_bb;
574 /* The single successor block of all branches out of the GIMPLE_SWITCH,
575 if such a block exists. Otherwise NULL. */
576 basic_block final_bb;
578 /* The probability of the default edge in the replaced switch. */
579 int default_prob;
581 /* The count of the default edge in the replaced switch. */
582 gcov_type default_count;
584 /* Combined count of all other (non-default) edges in the replaced switch. */
585 gcov_type other_count;
587 /* Number of phi nodes in the final bb (that we'll be replacing). */
588 int phi_count;
590 /* Array of default values, in the same order as phi nodes. */
591 tree *default_values;
593 /* Constructors of new static arrays. */
594 vec<constructor_elt, va_gc> **constructors;
596 /* Array of ssa names that are initialized with a value from a new static
597 array. */
598 tree *target_inbound_names;
600 /* Array of ssa names that are initialized with the default value if the
601 switch expression is out of range. */
602 tree *target_outbound_names;
604 /* The first load statement that loads a temporary from a new static array.
606 gimple arr_ref_first;
608 /* The last load statement that loads a temporary from a new static array. */
609 gimple arr_ref_last;
611 /* String reason why the case wasn't a good candidate that is written to the
612 dump file, if there is one. */
613 const char *reason;
615 /* Parameters for expand_switch_using_bit_tests. Should be computed
616 the same way as in expand_case. */
617 unsigned int uniq;
618 unsigned int count;
621 /* Collect information about GIMPLE_SWITCH statement SWTCH into INFO. */
623 static void
624 collect_switch_conv_info (gimple swtch, struct switch_conv_info *info)
626 unsigned int branch_num = gimple_switch_num_labels (swtch);
627 tree min_case, max_case;
628 unsigned int count, i;
629 edge e, e_default;
630 edge_iterator ei;
632 memset (info, 0, sizeof (*info));
634 /* The gimplifier has already sorted the cases by CASE_LOW and ensured there
635 is a default label which is the first in the vector.
636 Collect the bits we can deduce from the CFG. */
637 info->index_expr = gimple_switch_index (swtch);
638 info->switch_bb = gimple_bb (swtch);
639 info->default_bb =
640 label_to_block (CASE_LABEL (gimple_switch_default_label (swtch)));
641 e_default = find_edge (info->switch_bb, info->default_bb);
642 info->default_prob = e_default->probability;
643 info->default_count = e_default->count;
644 FOR_EACH_EDGE (e, ei, info->switch_bb->succs)
645 if (e != e_default)
646 info->other_count += e->count;
648 /* See if there is one common successor block for all branch
649 targets. If it exists, record it in FINAL_BB.
650 Start with the destination of the default case as guess
651 or its destination in case it is a forwarder block. */
652 if (! single_pred_p (e_default->dest))
653 info->final_bb = e_default->dest;
654 else if (single_succ_p (e_default->dest)
655 && ! single_pred_p (single_succ (e_default->dest)))
656 info->final_bb = single_succ (e_default->dest);
657 /* Require that all switch destinations are either that common
658 FINAL_BB or a forwarder to it. */
659 if (info->final_bb)
660 FOR_EACH_EDGE (e, ei, info->switch_bb->succs)
662 if (e->dest == info->final_bb)
663 continue;
665 if (single_pred_p (e->dest)
666 && single_succ_p (e->dest)
667 && single_succ (e->dest) == info->final_bb)
668 continue;
670 info->final_bb = NULL;
671 break;
674 /* Get upper and lower bounds of case values, and the covered range. */
675 min_case = gimple_switch_label (swtch, 1);
676 max_case = gimple_switch_label (swtch, branch_num - 1);
678 info->range_min = CASE_LOW (min_case);
679 if (CASE_HIGH (max_case) != NULL_TREE)
680 info->range_max = CASE_HIGH (max_case);
681 else
682 info->range_max = CASE_LOW (max_case);
684 info->range_size =
685 int_const_binop (MINUS_EXPR, info->range_max, info->range_min);
687 /* Get a count of the number of case labels. Single-valued case labels
688 simply count as one, but a case range counts double, since it may
689 require two compares if it gets lowered as a branching tree. */
690 count = 0;
691 for (i = 1; i < branch_num; i++)
693 tree elt = gimple_switch_label (swtch, i);
694 count++;
695 if (CASE_HIGH (elt)
696 && ! tree_int_cst_equal (CASE_LOW (elt), CASE_HIGH (elt)))
697 count++;
699 info->count = count;
701 /* Get the number of unique non-default targets out of the GIMPLE_SWITCH
702 block. Assume a CFG cleanup would have already removed degenerate
703 switch statements, this allows us to just use EDGE_COUNT. */
704 info->uniq = EDGE_COUNT (gimple_bb (swtch)->succs) - 1;
707 /* Checks whether the range given by individual case statements of the SWTCH
708 switch statement isn't too big and whether the number of branches actually
709 satisfies the size of the new array. */
711 static bool
712 check_range (struct switch_conv_info *info)
714 gcc_assert (info->range_size);
715 if (!tree_fits_uhwi_p (info->range_size))
717 info->reason = "index range way too large or otherwise unusable";
718 return false;
721 if (tree_to_uhwi (info->range_size)
722 > ((unsigned) info->count * SWITCH_CONVERSION_BRANCH_RATIO))
724 info->reason = "the maximum range-branch ratio exceeded";
725 return false;
728 return true;
731 /* Checks whether all but the FINAL_BB basic blocks are empty. */
733 static bool
734 check_all_empty_except_final (struct switch_conv_info *info)
736 edge e;
737 edge_iterator ei;
739 FOR_EACH_EDGE (e, ei, info->switch_bb->succs)
741 if (e->dest == info->final_bb)
742 continue;
744 if (!empty_block_p (e->dest))
746 info->reason = "bad case - a non-final BB not empty";
747 return false;
751 return true;
754 /* This function checks whether all required values in phi nodes in final_bb
755 are constants. Required values are those that correspond to a basic block
756 which is a part of the examined switch statement. It returns true if the
757 phi nodes are OK, otherwise false. */
759 static bool
760 check_final_bb (struct switch_conv_info *info)
762 gimple_stmt_iterator gsi;
764 info->phi_count = 0;
765 for (gsi = gsi_start_phis (info->final_bb); !gsi_end_p (gsi); gsi_next (&gsi))
767 gimple phi = gsi_stmt (gsi);
768 unsigned int i;
770 info->phi_count++;
772 for (i = 0; i < gimple_phi_num_args (phi); i++)
774 basic_block bb = gimple_phi_arg_edge (phi, i)->src;
776 if (bb == info->switch_bb
777 || (single_pred_p (bb) && single_pred (bb) == info->switch_bb))
779 tree reloc, val;
781 val = gimple_phi_arg_def (phi, i);
782 if (!is_gimple_ip_invariant (val))
784 info->reason = "non-invariant value from a case";
785 return false; /* Non-invariant argument. */
787 reloc = initializer_constant_valid_p (val, TREE_TYPE (val));
788 if ((flag_pic && reloc != null_pointer_node)
789 || (!flag_pic && reloc == NULL_TREE))
791 if (reloc)
792 info->reason
793 = "value from a case would need runtime relocations";
794 else
795 info->reason
796 = "value from a case is not a valid initializer";
797 return false;
803 return true;
806 /* The following function allocates default_values, target_{in,out}_names and
807 constructors arrays. The last one is also populated with pointers to
808 vectors that will become constructors of new arrays. */
810 static void
811 create_temp_arrays (struct switch_conv_info *info)
813 int i;
815 info->default_values = XCNEWVEC (tree, info->phi_count * 3);
816 /* ??? Macros do not support multi argument templates in their
817 argument list. We create a typedef to work around that problem. */
818 typedef vec<constructor_elt, va_gc> *vec_constructor_elt_gc;
819 info->constructors = XCNEWVEC (vec_constructor_elt_gc, info->phi_count);
820 info->target_inbound_names = info->default_values + info->phi_count;
821 info->target_outbound_names = info->target_inbound_names + info->phi_count;
822 for (i = 0; i < info->phi_count; i++)
823 vec_alloc (info->constructors[i], tree_to_uhwi (info->range_size) + 1);
826 /* Free the arrays created by create_temp_arrays(). The vectors that are
827 created by that function are not freed here, however, because they have
828 already become constructors and must be preserved. */
830 static void
831 free_temp_arrays (struct switch_conv_info *info)
833 XDELETEVEC (info->constructors);
834 XDELETEVEC (info->default_values);
837 /* Populate the array of default values in the order of phi nodes.
838 DEFAULT_CASE is the CASE_LABEL_EXPR for the default switch branch. */
840 static void
841 gather_default_values (tree default_case, struct switch_conv_info *info)
843 gimple_stmt_iterator gsi;
844 basic_block bb = label_to_block (CASE_LABEL (default_case));
845 edge e;
846 int i = 0;
848 gcc_assert (CASE_LOW (default_case) == NULL_TREE);
850 if (bb == info->final_bb)
851 e = find_edge (info->switch_bb, bb);
852 else
853 e = single_succ_edge (bb);
855 for (gsi = gsi_start_phis (info->final_bb); !gsi_end_p (gsi); gsi_next (&gsi))
857 gimple phi = gsi_stmt (gsi);
858 tree val = PHI_ARG_DEF_FROM_EDGE (phi, e);
859 gcc_assert (val);
860 info->default_values[i++] = val;
864 /* The following function populates the vectors in the constructors array with
865 future contents of the static arrays. The vectors are populated in the
866 order of phi nodes. SWTCH is the switch statement being converted. */
868 static void
869 build_constructors (gimple swtch, struct switch_conv_info *info)
871 unsigned i, branch_num = gimple_switch_num_labels (swtch);
872 tree pos = info->range_min;
874 for (i = 1; i < branch_num; i++)
876 tree cs = gimple_switch_label (swtch, i);
877 basic_block bb = label_to_block (CASE_LABEL (cs));
878 edge e;
879 tree high;
880 gimple_stmt_iterator gsi;
881 int j;
883 if (bb == info->final_bb)
884 e = find_edge (info->switch_bb, bb);
885 else
886 e = single_succ_edge (bb);
887 gcc_assert (e);
889 while (tree_int_cst_lt (pos, CASE_LOW (cs)))
891 int k;
892 for (k = 0; k < info->phi_count; k++)
894 constructor_elt elt;
896 elt.index = int_const_binop (MINUS_EXPR, pos, info->range_min);
897 elt.value
898 = unshare_expr_without_location (info->default_values[k]);
899 info->constructors[k]->quick_push (elt);
902 pos = int_const_binop (PLUS_EXPR, pos,
903 build_int_cst (TREE_TYPE (pos), 1));
905 gcc_assert (tree_int_cst_equal (pos, CASE_LOW (cs)));
907 j = 0;
908 if (CASE_HIGH (cs))
909 high = CASE_HIGH (cs);
910 else
911 high = CASE_LOW (cs);
912 for (gsi = gsi_start_phis (info->final_bb);
913 !gsi_end_p (gsi); gsi_next (&gsi))
915 gimple phi = gsi_stmt (gsi);
916 tree val = PHI_ARG_DEF_FROM_EDGE (phi, e);
917 tree low = CASE_LOW (cs);
918 pos = CASE_LOW (cs);
922 constructor_elt elt;
924 elt.index = int_const_binop (MINUS_EXPR, pos, info->range_min);
925 elt.value = unshare_expr_without_location (val);
926 info->constructors[j]->quick_push (elt);
928 pos = int_const_binop (PLUS_EXPR, pos,
929 build_int_cst (TREE_TYPE (pos), 1));
930 } while (!tree_int_cst_lt (high, pos)
931 && tree_int_cst_lt (low, pos));
932 j++;
937 /* If all values in the constructor vector are the same, return the value.
938 Otherwise return NULL_TREE. Not supposed to be called for empty
939 vectors. */
941 static tree
942 constructor_contains_same_values_p (vec<constructor_elt, va_gc> *vec)
944 unsigned int i;
945 tree prev = NULL_TREE;
946 constructor_elt *elt;
948 FOR_EACH_VEC_SAFE_ELT (vec, i, elt)
950 if (!prev)
951 prev = elt->value;
952 else if (!operand_equal_p (elt->value, prev, OEP_ONLY_CONST))
953 return NULL_TREE;
955 return prev;
958 /* Return type which should be used for array elements, either TYPE,
959 or for integral type some smaller integral type that can still hold
960 all the constants. */
962 static tree
963 array_value_type (gimple swtch, tree type, int num,
964 struct switch_conv_info *info)
966 unsigned int i, len = vec_safe_length (info->constructors[num]);
967 constructor_elt *elt;
968 machine_mode mode;
969 int sign = 0;
970 tree smaller_type;
972 if (!INTEGRAL_TYPE_P (type))
973 return type;
975 mode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (TYPE_MODE (type)));
976 if (GET_MODE_SIZE (TYPE_MODE (type)) <= GET_MODE_SIZE (mode))
977 return type;
979 if (len < (optimize_bb_for_size_p (gimple_bb (swtch)) ? 2 : 32))
980 return type;
982 FOR_EACH_VEC_SAFE_ELT (info->constructors[num], i, elt)
984 wide_int cst;
986 if (TREE_CODE (elt->value) != INTEGER_CST)
987 return type;
989 cst = elt->value;
990 while (1)
992 unsigned int prec = GET_MODE_BITSIZE (mode);
993 if (prec > HOST_BITS_PER_WIDE_INT)
994 return type;
996 if (sign >= 0 && cst == wi::zext (cst, prec))
998 if (sign == 0 && cst == wi::sext (cst, prec))
999 break;
1000 sign = 1;
1001 break;
1003 if (sign <= 0 && cst == wi::sext (cst, prec))
1005 sign = -1;
1006 break;
1009 if (sign == 1)
1010 sign = 0;
1012 mode = GET_MODE_WIDER_MODE (mode);
1013 if (mode == VOIDmode
1014 || GET_MODE_SIZE (mode) >= GET_MODE_SIZE (TYPE_MODE (type)))
1015 return type;
1019 if (sign == 0)
1020 sign = TYPE_UNSIGNED (type) ? 1 : -1;
1021 smaller_type = lang_hooks.types.type_for_mode (mode, sign >= 0);
1022 if (GET_MODE_SIZE (TYPE_MODE (type))
1023 <= GET_MODE_SIZE (TYPE_MODE (smaller_type)))
1024 return type;
1026 return smaller_type;
1029 /* Create an appropriate array type and declaration and assemble a static array
1030 variable. Also create a load statement that initializes the variable in
1031 question with a value from the static array. SWTCH is the switch statement
1032 being converted, NUM is the index to arrays of constructors, default values
1033 and target SSA names for this particular array. ARR_INDEX_TYPE is the type
1034 of the index of the new array, PHI is the phi node of the final BB that
1035 corresponds to the value that will be loaded from the created array. TIDX
1036 is an ssa name of a temporary variable holding the index for loads from the
1037 new array. */
1039 static void
1040 build_one_array (gimple swtch, int num, tree arr_index_type, gimple phi,
1041 tree tidx, struct switch_conv_info *info)
1043 tree name, cst;
1044 gimple load;
1045 gimple_stmt_iterator gsi = gsi_for_stmt (swtch);
1046 location_t loc = gimple_location (swtch);
1048 gcc_assert (info->default_values[num]);
1050 name = copy_ssa_name (PHI_RESULT (phi), NULL);
1051 info->target_inbound_names[num] = name;
1053 cst = constructor_contains_same_values_p (info->constructors[num]);
1054 if (cst)
1055 load = gimple_build_assign (name, cst);
1056 else
1058 tree array_type, ctor, decl, value_type, fetch, default_type;
1060 default_type = TREE_TYPE (info->default_values[num]);
1061 value_type = array_value_type (swtch, default_type, num, info);
1062 array_type = build_array_type (value_type, arr_index_type);
1063 if (default_type != value_type)
1065 unsigned int i;
1066 constructor_elt *elt;
1068 FOR_EACH_VEC_SAFE_ELT (info->constructors[num], i, elt)
1069 elt->value = fold_convert (value_type, elt->value);
1071 ctor = build_constructor (array_type, info->constructors[num]);
1072 TREE_CONSTANT (ctor) = true;
1073 TREE_STATIC (ctor) = true;
1075 decl = build_decl (loc, VAR_DECL, NULL_TREE, array_type);
1076 TREE_STATIC (decl) = 1;
1077 DECL_INITIAL (decl) = ctor;
1079 DECL_NAME (decl) = create_tmp_var_name ("CSWTCH");
1080 DECL_ARTIFICIAL (decl) = 1;
1081 TREE_CONSTANT (decl) = 1;
1082 TREE_READONLY (decl) = 1;
1083 varpool_node::finalize_decl (decl);
1085 fetch = build4 (ARRAY_REF, value_type, decl, tidx, NULL_TREE,
1086 NULL_TREE);
1087 if (default_type != value_type)
1089 fetch = fold_convert (default_type, fetch);
1090 fetch = force_gimple_operand_gsi (&gsi, fetch, true, NULL_TREE,
1091 true, GSI_SAME_STMT);
1093 load = gimple_build_assign (name, fetch);
1096 gsi_insert_before (&gsi, load, GSI_SAME_STMT);
1097 update_stmt (load);
1098 info->arr_ref_last = load;
1101 /* Builds and initializes static arrays initialized with values gathered from
1102 the SWTCH switch statement. Also creates statements that load values from
1103 them. */
1105 static void
1106 build_arrays (gimple swtch, struct switch_conv_info *info)
1108 tree arr_index_type;
1109 tree tidx, sub, utype;
1110 gimple stmt;
1111 gimple_stmt_iterator gsi;
1112 int i;
1113 location_t loc = gimple_location (swtch);
1115 gsi = gsi_for_stmt (swtch);
1117 /* Make sure we do not generate arithmetics in a subrange. */
1118 utype = TREE_TYPE (info->index_expr);
1119 if (TREE_TYPE (utype))
1120 utype = lang_hooks.types.type_for_mode (TYPE_MODE (TREE_TYPE (utype)), 1);
1121 else
1122 utype = lang_hooks.types.type_for_mode (TYPE_MODE (utype), 1);
1124 arr_index_type = build_index_type (info->range_size);
1125 tidx = make_ssa_name (utype, NULL);
1126 sub = fold_build2_loc (loc, MINUS_EXPR, utype,
1127 fold_convert_loc (loc, utype, info->index_expr),
1128 fold_convert_loc (loc, utype, info->range_min));
1129 sub = force_gimple_operand_gsi (&gsi, sub,
1130 false, NULL, true, GSI_SAME_STMT);
1131 stmt = gimple_build_assign (tidx, sub);
1133 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
1134 update_stmt (stmt);
1135 info->arr_ref_first = stmt;
1137 for (gsi = gsi_start_phis (info->final_bb), i = 0;
1138 !gsi_end_p (gsi); gsi_next (&gsi), i++)
1139 build_one_array (swtch, i, arr_index_type, gsi_stmt (gsi), tidx, info);
1142 /* Generates and appropriately inserts loads of default values at the position
1143 given by BSI. Returns the last inserted statement. */
1145 static gimple
1146 gen_def_assigns (gimple_stmt_iterator *gsi, struct switch_conv_info *info)
1148 int i;
1149 gimple assign = NULL;
1151 for (i = 0; i < info->phi_count; i++)
1153 tree name = copy_ssa_name (info->target_inbound_names[i], NULL);
1154 info->target_outbound_names[i] = name;
1155 assign = gimple_build_assign (name, info->default_values[i]);
1156 gsi_insert_before (gsi, assign, GSI_SAME_STMT);
1157 update_stmt (assign);
1159 return assign;
1162 /* Deletes the unused bbs and edges that now contain the switch statement and
1163 its empty branch bbs. BBD is the now dead BB containing the original switch
1164 statement, FINAL is the last BB of the converted switch statement (in terms
1165 of succession). */
1167 static void
1168 prune_bbs (basic_block bbd, basic_block final)
1170 edge_iterator ei;
1171 edge e;
1173 for (ei = ei_start (bbd->succs); (e = ei_safe_edge (ei)); )
1175 basic_block bb;
1176 bb = e->dest;
1177 remove_edge (e);
1178 if (bb != final)
1179 delete_basic_block (bb);
1181 delete_basic_block (bbd);
1184 /* Add values to phi nodes in final_bb for the two new edges. E1F is the edge
1185 from the basic block loading values from an array and E2F from the basic
1186 block loading default values. BBF is the last switch basic block (see the
1187 bbf description in the comment below). */
1189 static void
1190 fix_phi_nodes (edge e1f, edge e2f, basic_block bbf,
1191 struct switch_conv_info *info)
1193 gimple_stmt_iterator gsi;
1194 int i;
1196 for (gsi = gsi_start_phis (bbf), i = 0;
1197 !gsi_end_p (gsi); gsi_next (&gsi), i++)
1199 gimple phi = gsi_stmt (gsi);
1200 add_phi_arg (phi, info->target_inbound_names[i], e1f, UNKNOWN_LOCATION);
1201 add_phi_arg (phi, info->target_outbound_names[i], e2f, UNKNOWN_LOCATION);
1205 /* Creates a check whether the switch expression value actually falls into the
1206 range given by all the cases. If it does not, the temporaries are loaded
1207 with default values instead. SWTCH is the switch statement being converted.
1209 bb0 is the bb with the switch statement, however, we'll end it with a
1210 condition instead.
1212 bb1 is the bb to be used when the range check went ok. It is derived from
1213 the switch BB
1215 bb2 is the bb taken when the expression evaluated outside of the range
1216 covered by the created arrays. It is populated by loads of default
1217 values.
1219 bbF is a fall through for both bb1 and bb2 and contains exactly what
1220 originally followed the switch statement.
1222 bbD contains the switch statement (in the end). It is unreachable but we
1223 still need to strip off its edges.
1226 static void
1227 gen_inbound_check (gimple swtch, struct switch_conv_info *info)
1229 tree label_decl1 = create_artificial_label (UNKNOWN_LOCATION);
1230 tree label_decl2 = create_artificial_label (UNKNOWN_LOCATION);
1231 tree label_decl3 = create_artificial_label (UNKNOWN_LOCATION);
1232 gimple label1, label2, label3;
1233 tree utype, tidx;
1234 tree bound;
1236 gimple cond_stmt;
1238 gimple last_assign;
1239 gimple_stmt_iterator gsi;
1240 basic_block bb0, bb1, bb2, bbf, bbd;
1241 edge e01, e02, e21, e1d, e1f, e2f;
1242 location_t loc = gimple_location (swtch);
1244 gcc_assert (info->default_values);
1246 bb0 = gimple_bb (swtch);
1248 tidx = gimple_assign_lhs (info->arr_ref_first);
1249 utype = TREE_TYPE (tidx);
1251 /* (end of) block 0 */
1252 gsi = gsi_for_stmt (info->arr_ref_first);
1253 gsi_next (&gsi);
1255 bound = fold_convert_loc (loc, utype, info->range_size);
1256 cond_stmt = gimple_build_cond (LE_EXPR, tidx, bound, NULL_TREE, NULL_TREE);
1257 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1258 update_stmt (cond_stmt);
1260 /* block 2 */
1261 label2 = gimple_build_label (label_decl2);
1262 gsi_insert_before (&gsi, label2, GSI_SAME_STMT);
1263 last_assign = gen_def_assigns (&gsi, info);
1265 /* block 1 */
1266 label1 = gimple_build_label (label_decl1);
1267 gsi_insert_before (&gsi, label1, GSI_SAME_STMT);
1269 /* block F */
1270 gsi = gsi_start_bb (info->final_bb);
1271 label3 = gimple_build_label (label_decl3);
1272 gsi_insert_before (&gsi, label3, GSI_SAME_STMT);
1274 /* cfg fix */
1275 e02 = split_block (bb0, cond_stmt);
1276 bb2 = e02->dest;
1278 e21 = split_block (bb2, last_assign);
1279 bb1 = e21->dest;
1280 remove_edge (e21);
1282 e1d = split_block (bb1, info->arr_ref_last);
1283 bbd = e1d->dest;
1284 remove_edge (e1d);
1286 /* flags and profiles of the edge for in-range values */
1287 e01 = make_edge (bb0, bb1, EDGE_TRUE_VALUE);
1288 e01->probability = REG_BR_PROB_BASE - info->default_prob;
1289 e01->count = info->other_count;
1291 /* flags and profiles of the edge taking care of out-of-range values */
1292 e02->flags &= ~EDGE_FALLTHRU;
1293 e02->flags |= EDGE_FALSE_VALUE;
1294 e02->probability = info->default_prob;
1295 e02->count = info->default_count;
1297 bbf = info->final_bb;
1299 e1f = make_edge (bb1, bbf, EDGE_FALLTHRU);
1300 e1f->probability = REG_BR_PROB_BASE;
1301 e1f->count = info->other_count;
1303 e2f = make_edge (bb2, bbf, EDGE_FALLTHRU);
1304 e2f->probability = REG_BR_PROB_BASE;
1305 e2f->count = info->default_count;
1307 /* frequencies of the new BBs */
1308 bb1->frequency = EDGE_FREQUENCY (e01);
1309 bb2->frequency = EDGE_FREQUENCY (e02);
1310 bbf->frequency = EDGE_FREQUENCY (e1f) + EDGE_FREQUENCY (e2f);
1312 /* Tidy blocks that have become unreachable. */
1313 prune_bbs (bbd, info->final_bb);
1315 /* Fixup the PHI nodes in bbF. */
1316 fix_phi_nodes (e1f, e2f, bbf, info);
1318 /* Fix the dominator tree, if it is available. */
1319 if (dom_info_available_p (CDI_DOMINATORS))
1321 vec<basic_block> bbs_to_fix_dom;
1323 set_immediate_dominator (CDI_DOMINATORS, bb1, bb0);
1324 set_immediate_dominator (CDI_DOMINATORS, bb2, bb0);
1325 if (! get_immediate_dominator (CDI_DOMINATORS, bbf))
1326 /* If bbD was the immediate dominator ... */
1327 set_immediate_dominator (CDI_DOMINATORS, bbf, bb0);
1329 bbs_to_fix_dom.create (4);
1330 bbs_to_fix_dom.quick_push (bb0);
1331 bbs_to_fix_dom.quick_push (bb1);
1332 bbs_to_fix_dom.quick_push (bb2);
1333 bbs_to_fix_dom.quick_push (bbf);
1335 iterate_fix_dominators (CDI_DOMINATORS, bbs_to_fix_dom, true);
1336 bbs_to_fix_dom.release ();
1340 /* The following function is invoked on every switch statement (the current one
1341 is given in SWTCH) and runs the individual phases of switch conversion on it
1342 one after another until one fails or the conversion is completed.
1343 Returns NULL on success, or a pointer to a string with the reason why the
1344 conversion failed. */
1346 static const char *
1347 process_switch (gimple swtch)
1349 struct switch_conv_info info;
1351 /* Group case labels so that we get the right results from the heuristics
1352 that decide on the code generation approach for this switch. */
1353 group_case_labels_stmt (swtch);
1355 /* If this switch is now a degenerate case with only a default label,
1356 there is nothing left for us to do. */
1357 if (gimple_switch_num_labels (swtch) < 2)
1358 return "switch is a degenerate case";
1360 collect_switch_conv_info (swtch, &info);
1362 /* No error markers should reach here (they should be filtered out
1363 during gimplification). */
1364 gcc_checking_assert (TREE_TYPE (info.index_expr) != error_mark_node);
1366 /* A switch on a constant should have been optimized in tree-cfg-cleanup. */
1367 gcc_checking_assert (! TREE_CONSTANT (info.index_expr));
1369 if (info.uniq <= MAX_CASE_BIT_TESTS)
1371 if (expand_switch_using_bit_tests_p (info.range_size,
1372 info.uniq, info.count,
1373 optimize_bb_for_speed_p
1374 (gimple_bb (swtch))))
1376 if (dump_file)
1377 fputs (" expanding as bit test is preferable\n", dump_file);
1378 emit_case_bit_tests (swtch, info.index_expr, info.range_min,
1379 info.range_size, info.range_max);
1380 loops_state_set (LOOPS_NEED_FIXUP);
1381 return NULL;
1384 if (info.uniq <= 2)
1385 /* This will be expanded as a decision tree in stmt.c:expand_case. */
1386 return " expanding as jumps is preferable";
1389 /* If there is no common successor, we cannot do the transformation. */
1390 if (! info.final_bb)
1391 return "no common successor to all case label target blocks found";
1393 /* Check the case label values are within reasonable range: */
1394 if (!check_range (&info))
1396 gcc_assert (info.reason);
1397 return info.reason;
1400 /* For all the cases, see whether they are empty, the assignments they
1401 represent constant and so on... */
1402 if (! check_all_empty_except_final (&info))
1404 gcc_assert (info.reason);
1405 return info.reason;
1407 if (!check_final_bb (&info))
1409 gcc_assert (info.reason);
1410 return info.reason;
1413 /* At this point all checks have passed and we can proceed with the
1414 transformation. */
1416 create_temp_arrays (&info);
1417 gather_default_values (gimple_switch_default_label (swtch), &info);
1418 build_constructors (swtch, &info);
1420 build_arrays (swtch, &info); /* Build the static arrays and assignments. */
1421 gen_inbound_check (swtch, &info); /* Build the bounds check. */
1423 /* Cleanup: */
1424 free_temp_arrays (&info);
1425 return NULL;
1428 /* The main function of the pass scans statements for switches and invokes
1429 process_switch on them. */
1431 namespace {
1433 const pass_data pass_data_convert_switch =
1435 GIMPLE_PASS, /* type */
1436 "switchconv", /* name */
1437 OPTGROUP_NONE, /* optinfo_flags */
1438 TV_TREE_SWITCH_CONVERSION, /* tv_id */
1439 ( PROP_cfg | PROP_ssa ), /* properties_required */
1440 0, /* properties_provided */
1441 0, /* properties_destroyed */
1442 0, /* todo_flags_start */
1443 TODO_update_ssa, /* todo_flags_finish */
1446 class pass_convert_switch : public gimple_opt_pass
1448 public:
1449 pass_convert_switch (gcc::context *ctxt)
1450 : gimple_opt_pass (pass_data_convert_switch, ctxt)
1453 /* opt_pass methods: */
1454 virtual bool gate (function *) { return flag_tree_switch_conversion != 0; }
1455 virtual unsigned int execute (function *);
1457 }; // class pass_convert_switch
1459 unsigned int
1460 pass_convert_switch::execute (function *fun)
1462 basic_block bb;
1464 FOR_EACH_BB_FN (bb, fun)
1466 const char *failure_reason;
1467 gimple stmt = last_stmt (bb);
1468 if (stmt && gimple_code (stmt) == GIMPLE_SWITCH)
1470 if (dump_file)
1472 expanded_location loc = expand_location (gimple_location (stmt));
1474 fprintf (dump_file, "beginning to process the following "
1475 "SWITCH statement (%s:%d) : ------- \n",
1476 loc.file, loc.line);
1477 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1478 putc ('\n', dump_file);
1481 failure_reason = process_switch (stmt);
1482 if (! failure_reason)
1484 if (dump_file)
1486 fputs ("Switch converted\n", dump_file);
1487 fputs ("--------------------------------\n", dump_file);
1490 /* Make no effort to update the post-dominator tree. It is actually not
1491 that hard for the transformations we have performed, but it is not
1492 supported by iterate_fix_dominators. */
1493 free_dominance_info (CDI_POST_DOMINATORS);
1495 else
1497 if (dump_file)
1499 fputs ("Bailing out - ", dump_file);
1500 fputs (failure_reason, dump_file);
1501 fputs ("\n--------------------------------\n", dump_file);
1507 return 0;
1510 } // anon namespace
1512 gimple_opt_pass *
1513 make_pass_convert_switch (gcc::context *ctxt)
1515 return new pass_convert_switch (ctxt);