Update copyright years.
[platform/upstream/gcc.git] / gcc / tree-switch-conversion.c
1 /* Lower GIMPLE_SWITCH expressions to something more efficient than
2    a jump table.
3    Copyright (C) 2006-2015 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
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.
11
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.
16
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.  */
21
22 /* This file handles the lowering of GIMPLE_SWITCH to an indexed
23    load, or a series of bit-test-and-branch expressions.  */
24
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"
67
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"
71
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"
76 \f
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
81
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.
85
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.
90    
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.
94    
95    Returns the newly created basic block.  */
96
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)
101 {
102   tree tmp;
103   gcond *cond_stmt;
104   edge e_false;
105   basic_block new_bb, split_bb = gsi_bb (*gsip);
106   bool dominated_e_true = false;
107
108   gcc_assert (e_true->src == split_bb);
109
110   if (update_dominators
111       && get_immediate_dominator (CDI_DOMINATORS, e_true->dest) == split_bb)
112     dominated_e_true = true;
113
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);
118
119   e_false = split_block (split_bb, cond_stmt);
120   new_bb = e_false->dest;
121   redirect_edge_pred (e_true, split_bb);
122
123   e_true->flags &= ~EDGE_FALLTHRU;
124   e_true->flags |= EDGE_TRUE_VALUE;
125
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;
131
132   if (update_dominators)
133     {
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);
137     }
138
139   return new_bb;
140 }
141
142
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.  */
147
148 static bool
149 expand_switch_using_bit_tests_p (tree range,
150                                  unsigned int uniq,
151                                  unsigned int count, bool speed_p)
152 {
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);
159 }
160 \f
161 /* Implement switch statements with bit tests
162
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:
167
168         if ((x==4) || (x==6) || (x==9) || (x==11))
169
170 as a single bit test:
171
172         if ((1<<x) & ((1<<4)|(1<<6)|(1<<9)|(1<<11)))
173
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".
177
178 The following example shows the code the transformation generates:
179
180         int bar(int x)
181         {
182                 switch (x)
183                 {
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;
189                 }
190                 return 0;
191         }
192
193 ==>
194
195         bar (int x)
196         {
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;
202         L1:
203                 return 1;
204         L2:
205                 return 0;
206         }
207
208 TODO: There are still some improvements to this transformation that could
209 be implemented:
210
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.
213
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.
219
220 This transformation was contributed by Roger Sayle, see this e-mail:
221    http://gcc.gnu.org/ml/gcc-patches/2003-01/msg01950.html
222 */
223
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.  */
232
233 struct case_bit_test
234 {
235   wide_int mask;
236   edge target_edge;
237   tree label;
238   int bits;
239 };
240
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.
246
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...).  */
254
255 static int
256 case_bit_test_cmp (const void *p1, const void *p2)
257 {
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;
260
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;
265
266   /* Stabilize the sort.  */
267   return LABEL_DECL_UID (d2->label) - LABEL_DECL_UID (d1->label);
268 }
269
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.
274
275     INDEX_EXPR is the value being switched on.
276
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.
283
284     There *MUST* be MAX_CASE_BIT_TESTS or less unique case
285     node targets.  */
286
287 static void
288 emit_case_bit_tests (gswitch *swtch, tree index_expr,
289                      tree minval, tree range, tree maxval)
290 {
291   struct case_bit_test test[MAX_CASE_BIT_TESTS];
292   unsigned int i, j, k;
293   unsigned int count;
294
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);
299
300   vec<basic_block> bbs_to_fix_dom = vNULL;
301
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);
305
306   gimple_stmt_iterator gsi;
307   gassign *shift_stmt;
308
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);
315
316   memset (&test, 0, sizeof (test));
317
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);
322
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++)
327     {
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;
335
336       if (k == count)
337         {
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++;
344         }
345       else
346         test[k].bits++;
347
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));
355
356       for (j = lo; j <= hi; j++)
357         test[k].mask |= wi::lshift (wone, j);
358     }
359
360   qsort (test, count, sizeof (*test), case_bit_test_cmp);
361
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)
367     {
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++)
375         {
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);
380         }
381       if (cost_diff > 0)
382         {
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;
387         }
388     }
389
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);
394
395   if (update_dom)
396     {
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);
401     }
402
403   /* Now build the test-and-branch code.  */
404
405   gsi = gsi_last_bb (switch_bb);
406
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);
414
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);
426
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)
430     {
431       vec<basic_block> dom_bbs;
432       basic_block dom_son;
433
434       dom_bbs = get_dominated_by (CDI_DOMINATORS, new_bb);
435       FOR_EACH_VEC_ELT (dom_bbs, i, dom_son)
436         {
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);
442         }
443       dom_bbs.release ();
444     }
445
446   /* csui = (1 << (word_mode) idx) */
447   csui = make_ssa_name (word_type_node);
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);
456
457   /* for each unique set of cases:
458         if (const & csui) goto target  */
459   for (k = 0; k < count; k++)
460     {
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);
473     }
474
475   /* We should have removed all edges now.  */
476   gcc_assert (EDGE_COUNT (gsi_bb (gsi)->succs) == 0);
477
478   /* If nothing matched, go to the default label.  */
479   make_edge (gsi_bb (gsi), new_default_bb, EDGE_FALLTHRU);
480
481   /* The GIMPLE_SWITCH is now redundant.  */
482   gsi_remove (&gsi, true);
483
484   if (update_dom)
485     {
486       /* Fix up the dominator tree.  */
487       iterate_fix_dominators (CDI_DOMINATORS, bbs_to_fix_dom, true);
488       bbs_to_fix_dom.release ();
489     }
490 }
491 \f
492 /*
493      Switch initialization conversion
494
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:
499
500         int a,b;
501
502         switch (argc)
503         {
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;
521         }
522         a_5 = PHI <a_1, a_2, a_3, a_4>
523         b_5 = PHI <b_1, b_2, b_3, b_4>
524
525
526 is changed into:
527
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};
531
532         if (((unsigned) argc) - 1 < 11)
533           {
534             a_6 = CSWTCH02[argc - 1];
535             b_6 = CSWTCH01[argc - 1];
536           }
537         else
538           {
539             a_7 = 16;
540             b_7 = 1;
541           }
542         a_5 = PHI <a_6, a_7>
543         b_b = PHI <b_6, b_7>
544
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.
548
549 This transformation was contributed by Martin Jambor, see this e-mail:
550    http://gcc.gnu.org/ml/gcc-patches/2008-07/msg00011.html  */
551
552 /* The main structure of the pass.  */
553 struct switch_conv_info
554 {
555   /* The expression used to decide the switch branch.  */
556   tree index_expr;
557
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;
562
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;
567
568   /* Basic block that contains the actual GIMPLE_SWITCH.  */
569   basic_block switch_bb;
570
571   /* Basic block that is the target of the default case.  */
572   basic_block default_bb;
573
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;
577
578   /* The probability of the default edge in the replaced switch.  */
579   int default_prob;
580
581   /* The count of the default edge in the replaced switch.  */
582   gcov_type default_count;
583
584   /* Combined count of all other (non-default) edges in the replaced switch.  */
585   gcov_type other_count;
586
587   /* Number of phi nodes in the final bb (that we'll be replacing).  */
588   int phi_count;
589
590   /* Array of default values, in the same order as phi nodes.  */
591   tree *default_values;
592
593   /* Constructors of new static arrays.  */
594   vec<constructor_elt, va_gc> **constructors;
595
596   /* Array of ssa names that are initialized with a value from a new static
597      array.  */
598   tree *target_inbound_names;
599
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;
603
604   /* The first load statement that loads a temporary from a new static array.
605    */
606   gimple arr_ref_first;
607
608   /* The last load statement that loads a temporary from a new static array.  */
609   gimple arr_ref_last;
610
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;
614
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;
619 };
620
621 /* Collect information about GIMPLE_SWITCH statement SWTCH into INFO.  */
622
623 static void
624 collect_switch_conv_info (gswitch *swtch, struct switch_conv_info *info)
625 {
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;
631
632   memset (info, 0, sizeof (*info));
633
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;
647
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)
661       {
662         if (e->dest == info->final_bb)
663           continue;
664
665         if (single_pred_p (e->dest)
666             && single_succ_p (e->dest)
667             && single_succ (e->dest) == info->final_bb)
668           continue;
669
670         info->final_bb = NULL;
671         break;
672       }
673
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);
677
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);
683
684   info->range_size =
685     int_const_binop (MINUS_EXPR, info->range_max, info->range_min);
686
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++)
692     {
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++;
698     }
699   info->count = count;
700  
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;
705 }
706
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.  */
710
711 static bool
712 check_range (struct switch_conv_info *info)
713 {
714   gcc_assert (info->range_size);
715   if (!tree_fits_uhwi_p (info->range_size))
716     {
717       info->reason = "index range way too large or otherwise unusable";
718       return false;
719     }
720
721   if (tree_to_uhwi (info->range_size)
722       > ((unsigned) info->count * SWITCH_CONVERSION_BRANCH_RATIO))
723     {
724       info->reason = "the maximum range-branch ratio exceeded";
725       return false;
726     }
727
728   return true;
729 }
730
731 /* Checks whether all but the FINAL_BB basic blocks are empty.  */
732
733 static bool
734 check_all_empty_except_final (struct switch_conv_info *info)
735 {
736   edge e;
737   edge_iterator ei;
738
739   FOR_EACH_EDGE (e, ei, info->switch_bb->succs)
740     {
741       if (e->dest == info->final_bb)
742         continue;
743
744       if (!empty_block_p (e->dest))
745         {
746           info->reason = "bad case - a non-final BB not empty";
747           return false;
748         }
749     }
750
751   return true;
752 }
753
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.  */
758
759 static bool
760 check_final_bb (struct switch_conv_info *info)
761 {
762   gphi_iterator gsi;
763
764   info->phi_count = 0;
765   for (gsi = gsi_start_phis (info->final_bb); !gsi_end_p (gsi); gsi_next (&gsi))
766     {
767       gphi *phi = gsi.phi ();
768       unsigned int i;
769
770       info->phi_count++;
771
772       for (i = 0; i < gimple_phi_num_args (phi); i++)
773         {
774           basic_block bb = gimple_phi_arg_edge (phi, i)->src;
775
776           if (bb == info->switch_bb
777               || (single_pred_p (bb) && single_pred (bb) == info->switch_bb))
778             {
779               tree reloc, val;
780
781               val = gimple_phi_arg_def (phi, i);
782               if (!is_gimple_ip_invariant (val))
783                 {
784                   info->reason = "non-invariant value from a case";
785                   return false; /* Non-invariant argument.  */
786                 }
787               reloc = initializer_constant_valid_p (val, TREE_TYPE (val));
788               if ((flag_pic && reloc != null_pointer_node)
789                   || (!flag_pic && reloc == NULL_TREE))
790                 {
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;
798                 }
799             }
800         }
801     }
802
803   return true;
804 }
805
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.  */
809
810 static void
811 create_temp_arrays (struct switch_conv_info *info)
812 {
813   int i;
814
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);
824 }
825
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.  */
829
830 static void
831 free_temp_arrays (struct switch_conv_info *info)
832 {
833   XDELETEVEC (info->constructors);
834   XDELETEVEC (info->default_values);
835 }
836
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.  */
839
840 static void
841 gather_default_values (tree default_case, struct switch_conv_info *info)
842 {
843   gphi_iterator gsi;
844   basic_block bb = label_to_block (CASE_LABEL (default_case));
845   edge e;
846   int i = 0;
847
848   gcc_assert (CASE_LOW (default_case) == NULL_TREE);
849
850   if (bb == info->final_bb)
851     e = find_edge (info->switch_bb, bb);
852   else
853     e = single_succ_edge (bb);
854
855   for (gsi = gsi_start_phis (info->final_bb); !gsi_end_p (gsi); gsi_next (&gsi))
856     {
857       gphi *phi = gsi.phi ();
858       tree val = PHI_ARG_DEF_FROM_EDGE (phi, e);
859       gcc_assert (val);
860       info->default_values[i++] = val;
861     }
862 }
863
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.  */
867
868 static void
869 build_constructors (gswitch *swtch, struct switch_conv_info *info)
870 {
871   unsigned i, branch_num = gimple_switch_num_labels (swtch);
872   tree pos = info->range_min;
873
874   for (i = 1; i < branch_num; i++)
875     {
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       gphi_iterator gsi;
881       int j;
882
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);
888
889       while (tree_int_cst_lt (pos, CASE_LOW (cs)))
890         {
891           int k;
892           for (k = 0; k < info->phi_count; k++)
893             {
894               constructor_elt elt;
895
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);
900             }
901
902           pos = int_const_binop (PLUS_EXPR, pos,
903                                  build_int_cst (TREE_TYPE (pos), 1));
904         }
905       gcc_assert (tree_int_cst_equal (pos, CASE_LOW (cs)));
906
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))
914         {
915           gphi *phi = gsi.phi ();
916           tree val = PHI_ARG_DEF_FROM_EDGE (phi, e);
917           tree low = CASE_LOW (cs);
918           pos = CASE_LOW (cs);
919
920           do
921             {
922               constructor_elt elt;
923
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);
927
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++;
933         }
934     }
935 }
936
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.  */
940
941 static tree
942 constructor_contains_same_values_p (vec<constructor_elt, va_gc> *vec)
943 {
944   unsigned int i;
945   tree prev = NULL_TREE;
946   constructor_elt *elt;
947
948   FOR_EACH_VEC_SAFE_ELT (vec, i, elt)
949     {
950       if (!prev)
951         prev = elt->value;
952       else if (!operand_equal_p (elt->value, prev, OEP_ONLY_CONST))
953         return NULL_TREE;
954     }
955   return prev;
956 }
957
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.  */
961
962 static tree
963 array_value_type (gswitch *swtch, tree type, int num,
964                   struct switch_conv_info *info)
965 {
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;
971
972   if (!INTEGRAL_TYPE_P (type))
973     return type;
974
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;
978
979   if (len < (optimize_bb_for_size_p (gimple_bb (swtch)) ? 2 : 32))
980     return type;
981
982   FOR_EACH_VEC_SAFE_ELT (info->constructors[num], i, elt)
983     {
984       wide_int cst;
985
986       if (TREE_CODE (elt->value) != INTEGER_CST)
987         return type;
988
989       cst = elt->value;
990       while (1)
991         {
992           unsigned int prec = GET_MODE_BITSIZE (mode);
993           if (prec > HOST_BITS_PER_WIDE_INT)
994             return type;
995
996           if (sign >= 0 && cst == wi::zext (cst, prec))
997             {
998               if (sign == 0 && cst == wi::sext (cst, prec))
999                 break;
1000               sign = 1;
1001               break;
1002             }
1003           if (sign <= 0 && cst == wi::sext (cst, prec))
1004             {
1005               sign = -1;
1006               break;
1007             }
1008
1009           if (sign == 1)
1010             sign = 0;
1011
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;
1016         }
1017     }
1018
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;
1025
1026   return smaller_type;
1027 }
1028
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.  */
1038
1039 static void
1040 build_one_array (gswitch *swtch, int num, tree arr_index_type,
1041                  gphi *phi, tree tidx, struct switch_conv_info *info)
1042 {
1043   tree name, cst;
1044   gimple load;
1045   gimple_stmt_iterator gsi = gsi_for_stmt (swtch);
1046   location_t loc = gimple_location (swtch);
1047
1048   gcc_assert (info->default_values[num]);
1049
1050   name = copy_ssa_name (PHI_RESULT (phi));
1051   info->target_inbound_names[num] = name;
1052
1053   cst = constructor_contains_same_values_p (info->constructors[num]);
1054   if (cst)
1055     load = gimple_build_assign (name, cst);
1056   else
1057     {
1058       tree array_type, ctor, decl, value_type, fetch, default_type;
1059
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)
1064         {
1065           unsigned int i;
1066           constructor_elt *elt;
1067
1068           FOR_EACH_VEC_SAFE_ELT (info->constructors[num], i, elt)
1069             elt->value = fold_convert (value_type, elt->value);
1070         }
1071       ctor = build_constructor (array_type, info->constructors[num]);
1072       TREE_CONSTANT (ctor) = true;
1073       TREE_STATIC (ctor) = true;
1074
1075       decl = build_decl (loc, VAR_DECL, NULL_TREE, array_type);
1076       TREE_STATIC (decl) = 1;
1077       DECL_INITIAL (decl) = ctor;
1078
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);
1084
1085       fetch = build4 (ARRAY_REF, value_type, decl, tidx, NULL_TREE,
1086                       NULL_TREE);
1087       if (default_type != value_type)
1088         {
1089           fetch = fold_convert (default_type, fetch);
1090           fetch = force_gimple_operand_gsi (&gsi, fetch, true, NULL_TREE,
1091                                             true, GSI_SAME_STMT);
1092         }
1093       load = gimple_build_assign (name, fetch);
1094     }
1095
1096   gsi_insert_before (&gsi, load, GSI_SAME_STMT);
1097   update_stmt (load);
1098   info->arr_ref_last = load;
1099 }
1100
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.  */
1104
1105 static void
1106 build_arrays (gswitch *swtch, struct switch_conv_info *info)
1107 {
1108   tree arr_index_type;
1109   tree tidx, sub, utype;
1110   gimple stmt;
1111   gimple_stmt_iterator gsi;
1112   gphi_iterator gpi;
1113   int i;
1114   location_t loc = gimple_location (swtch);
1115
1116   gsi = gsi_for_stmt (swtch);
1117
1118   /* Make sure we do not generate arithmetics in a subrange.  */
1119   utype = TREE_TYPE (info->index_expr);
1120   if (TREE_TYPE (utype))
1121     utype = lang_hooks.types.type_for_mode (TYPE_MODE (TREE_TYPE (utype)), 1);
1122   else
1123     utype = lang_hooks.types.type_for_mode (TYPE_MODE (utype), 1);
1124
1125   arr_index_type = build_index_type (info->range_size);
1126   tidx = make_ssa_name (utype);
1127   sub = fold_build2_loc (loc, MINUS_EXPR, utype,
1128                          fold_convert_loc (loc, utype, info->index_expr),
1129                          fold_convert_loc (loc, utype, info->range_min));
1130   sub = force_gimple_operand_gsi (&gsi, sub,
1131                                   false, NULL, true, GSI_SAME_STMT);
1132   stmt = gimple_build_assign (tidx, sub);
1133
1134   gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
1135   update_stmt (stmt);
1136   info->arr_ref_first = stmt;
1137
1138   for (gpi = gsi_start_phis (info->final_bb), i = 0;
1139        !gsi_end_p (gpi); gsi_next (&gpi), i++)
1140     build_one_array (swtch, i, arr_index_type, gpi.phi (), tidx, info);
1141 }
1142
1143 /* Generates and appropriately inserts loads of default values at the position
1144    given by BSI.  Returns the last inserted statement.  */
1145
1146 static gassign *
1147 gen_def_assigns (gimple_stmt_iterator *gsi, struct switch_conv_info *info)
1148 {
1149   int i;
1150   gassign *assign = NULL;
1151
1152   for (i = 0; i < info->phi_count; i++)
1153     {
1154       tree name = copy_ssa_name (info->target_inbound_names[i]);
1155       info->target_outbound_names[i] = name;
1156       assign = gimple_build_assign (name, info->default_values[i]);
1157       gsi_insert_before (gsi, assign, GSI_SAME_STMT);
1158       update_stmt (assign);
1159     }
1160   return assign;
1161 }
1162
1163 /* Deletes the unused bbs and edges that now contain the switch statement and
1164    its empty branch bbs.  BBD is the now dead BB containing the original switch
1165    statement, FINAL is the last BB of the converted switch statement (in terms
1166    of succession).  */
1167
1168 static void
1169 prune_bbs (basic_block bbd, basic_block final)
1170 {
1171   edge_iterator ei;
1172   edge e;
1173
1174   for (ei = ei_start (bbd->succs); (e = ei_safe_edge (ei)); )
1175     {
1176       basic_block bb;
1177       bb = e->dest;
1178       remove_edge (e);
1179       if (bb != final)
1180         delete_basic_block (bb);
1181     }
1182   delete_basic_block (bbd);
1183 }
1184
1185 /* Add values to phi nodes in final_bb for the two new edges.  E1F is the edge
1186    from the basic block loading values from an array and E2F from the basic
1187    block loading default values.  BBF is the last switch basic block (see the
1188    bbf description in the comment below).  */
1189
1190 static void
1191 fix_phi_nodes (edge e1f, edge e2f, basic_block bbf,
1192                struct switch_conv_info *info)
1193 {
1194   gphi_iterator gsi;
1195   int i;
1196
1197   for (gsi = gsi_start_phis (bbf), i = 0;
1198        !gsi_end_p (gsi); gsi_next (&gsi), i++)
1199     {
1200       gphi *phi = gsi.phi ();
1201       add_phi_arg (phi, info->target_inbound_names[i], e1f, UNKNOWN_LOCATION);
1202       add_phi_arg (phi, info->target_outbound_names[i], e2f, UNKNOWN_LOCATION);
1203     }
1204 }
1205
1206 /* Creates a check whether the switch expression value actually falls into the
1207    range given by all the cases.  If it does not, the temporaries are loaded
1208    with default values instead.  SWTCH is the switch statement being converted.
1209
1210    bb0 is the bb with the switch statement, however, we'll end it with a
1211        condition instead.
1212
1213    bb1 is the bb to be used when the range check went ok.  It is derived from
1214        the switch BB
1215
1216    bb2 is the bb taken when the expression evaluated outside of the range
1217        covered by the created arrays.  It is populated by loads of default
1218        values.
1219
1220    bbF is a fall through for both bb1 and bb2 and contains exactly what
1221        originally followed the switch statement.
1222
1223    bbD contains the switch statement (in the end).  It is unreachable but we
1224        still need to strip off its edges.
1225 */
1226
1227 static void
1228 gen_inbound_check (gswitch *swtch, struct switch_conv_info *info)
1229 {
1230   tree label_decl1 = create_artificial_label (UNKNOWN_LOCATION);
1231   tree label_decl2 = create_artificial_label (UNKNOWN_LOCATION);
1232   tree label_decl3 = create_artificial_label (UNKNOWN_LOCATION);
1233   glabel *label1, *label2, *label3;
1234   tree utype, tidx;
1235   tree bound;
1236
1237   gcond *cond_stmt;
1238
1239   gassign *last_assign;
1240   gimple_stmt_iterator gsi;
1241   basic_block bb0, bb1, bb2, bbf, bbd;
1242   edge e01, e02, e21, e1d, e1f, e2f;
1243   location_t loc = gimple_location (swtch);
1244
1245   gcc_assert (info->default_values);
1246
1247   bb0 = gimple_bb (swtch);
1248
1249   tidx = gimple_assign_lhs (info->arr_ref_first);
1250   utype = TREE_TYPE (tidx);
1251
1252   /* (end of) block 0 */
1253   gsi = gsi_for_stmt (info->arr_ref_first);
1254   gsi_next (&gsi);
1255
1256   bound = fold_convert_loc (loc, utype, info->range_size);
1257   cond_stmt = gimple_build_cond (LE_EXPR, tidx, bound, NULL_TREE, NULL_TREE);
1258   gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1259   update_stmt (cond_stmt);
1260
1261   /* block 2 */
1262   label2 = gimple_build_label (label_decl2);
1263   gsi_insert_before (&gsi, label2, GSI_SAME_STMT);
1264   last_assign = gen_def_assigns (&gsi, info);
1265
1266   /* block 1 */
1267   label1 = gimple_build_label (label_decl1);
1268   gsi_insert_before (&gsi, label1, GSI_SAME_STMT);
1269
1270   /* block F */
1271   gsi = gsi_start_bb (info->final_bb);
1272   label3 = gimple_build_label (label_decl3);
1273   gsi_insert_before (&gsi, label3, GSI_SAME_STMT);
1274
1275   /* cfg fix */
1276   e02 = split_block (bb0, cond_stmt);
1277   bb2 = e02->dest;
1278
1279   e21 = split_block (bb2, last_assign);
1280   bb1 = e21->dest;
1281   remove_edge (e21);
1282
1283   e1d = split_block (bb1, info->arr_ref_last);
1284   bbd = e1d->dest;
1285   remove_edge (e1d);
1286
1287   /* flags and profiles of the edge for in-range values */
1288   e01 = make_edge (bb0, bb1, EDGE_TRUE_VALUE);
1289   e01->probability = REG_BR_PROB_BASE - info->default_prob;
1290   e01->count = info->other_count;
1291
1292   /* flags and profiles of the edge taking care of out-of-range values */
1293   e02->flags &= ~EDGE_FALLTHRU;
1294   e02->flags |= EDGE_FALSE_VALUE;
1295   e02->probability = info->default_prob;
1296   e02->count = info->default_count;
1297
1298   bbf = info->final_bb;
1299
1300   e1f = make_edge (bb1, bbf, EDGE_FALLTHRU);
1301   e1f->probability = REG_BR_PROB_BASE;
1302   e1f->count = info->other_count;
1303
1304   e2f = make_edge (bb2, bbf, EDGE_FALLTHRU);
1305   e2f->probability = REG_BR_PROB_BASE;
1306   e2f->count = info->default_count;
1307
1308   /* frequencies of the new BBs */
1309   bb1->frequency = EDGE_FREQUENCY (e01);
1310   bb2->frequency = EDGE_FREQUENCY (e02);
1311   bbf->frequency = EDGE_FREQUENCY (e1f) + EDGE_FREQUENCY (e2f);
1312
1313   /* Tidy blocks that have become unreachable.  */
1314   prune_bbs (bbd, info->final_bb);
1315
1316   /* Fixup the PHI nodes in bbF.  */
1317   fix_phi_nodes (e1f, e2f, bbf, info);
1318
1319   /* Fix the dominator tree, if it is available.  */
1320   if (dom_info_available_p (CDI_DOMINATORS))
1321     {
1322       vec<basic_block> bbs_to_fix_dom;
1323
1324       set_immediate_dominator (CDI_DOMINATORS, bb1, bb0);
1325       set_immediate_dominator (CDI_DOMINATORS, bb2, bb0);
1326       if (! get_immediate_dominator (CDI_DOMINATORS, bbf))
1327         /* If bbD was the immediate dominator ...  */
1328         set_immediate_dominator (CDI_DOMINATORS, bbf, bb0);
1329
1330       bbs_to_fix_dom.create (4);
1331       bbs_to_fix_dom.quick_push (bb0);
1332       bbs_to_fix_dom.quick_push (bb1);
1333       bbs_to_fix_dom.quick_push (bb2);
1334       bbs_to_fix_dom.quick_push (bbf);
1335
1336       iterate_fix_dominators (CDI_DOMINATORS, bbs_to_fix_dom, true);
1337       bbs_to_fix_dom.release ();
1338     }
1339 }
1340
1341 /* The following function is invoked on every switch statement (the current one
1342    is given in SWTCH) and runs the individual phases of switch conversion on it
1343    one after another until one fails or the conversion is completed.
1344    Returns NULL on success, or a pointer to a string with the reason why the
1345    conversion failed.  */
1346
1347 static const char *
1348 process_switch (gswitch *swtch)
1349 {
1350   struct switch_conv_info info;
1351
1352   /* Group case labels so that we get the right results from the heuristics
1353      that decide on the code generation approach for this switch.  */
1354   group_case_labels_stmt (swtch);
1355
1356   /* If this switch is now a degenerate case with only a default label,
1357      there is nothing left for us to do.   */
1358   if (gimple_switch_num_labels (swtch) < 2)
1359     return "switch is a degenerate case";
1360
1361   collect_switch_conv_info (swtch, &info);
1362
1363   /* No error markers should reach here (they should be filtered out
1364      during gimplification).  */
1365   gcc_checking_assert (TREE_TYPE (info.index_expr) != error_mark_node);
1366
1367   /* A switch on a constant should have been optimized in tree-cfg-cleanup.  */
1368   gcc_checking_assert (! TREE_CONSTANT (info.index_expr));
1369
1370   if (info.uniq <= MAX_CASE_BIT_TESTS)
1371     {
1372       if (expand_switch_using_bit_tests_p (info.range_size,
1373                                            info.uniq, info.count,
1374                                            optimize_bb_for_speed_p
1375                                              (gimple_bb (swtch))))
1376         {
1377           if (dump_file)
1378             fputs ("  expanding as bit test is preferable\n", dump_file);
1379           emit_case_bit_tests (swtch, info.index_expr, info.range_min,
1380                                info.range_size, info.range_max);
1381           loops_state_set (LOOPS_NEED_FIXUP);
1382           return NULL;
1383         }
1384
1385       if (info.uniq <= 2)
1386         /* This will be expanded as a decision tree in stmt.c:expand_case.  */
1387         return "  expanding as jumps is preferable";
1388     }
1389
1390   /* If there is no common successor, we cannot do the transformation.  */
1391   if (! info.final_bb)
1392     return "no common successor to all case label target blocks found";
1393
1394   /* Check the case label values are within reasonable range:  */
1395   if (!check_range (&info))
1396     {
1397       gcc_assert (info.reason);
1398       return info.reason;
1399     }
1400
1401   /* For all the cases, see whether they are empty, the assignments they
1402      represent constant and so on...  */
1403   if (! check_all_empty_except_final (&info))
1404     {
1405       gcc_assert (info.reason);
1406       return info.reason;
1407     }
1408   if (!check_final_bb (&info))
1409     {
1410       gcc_assert (info.reason);
1411       return info.reason;
1412     }
1413
1414   /* At this point all checks have passed and we can proceed with the
1415      transformation.  */
1416
1417   create_temp_arrays (&info);
1418   gather_default_values (gimple_switch_default_label (swtch), &info);
1419   build_constructors (swtch, &info);
1420
1421   build_arrays (swtch, &info); /* Build the static arrays and assignments.   */
1422   gen_inbound_check (swtch, &info);     /* Build the bounds check.  */
1423
1424   /* Cleanup:  */
1425   free_temp_arrays (&info);
1426   return NULL;
1427 }
1428
1429 /* The main function of the pass scans statements for switches and invokes
1430    process_switch on them.  */
1431
1432 namespace {
1433
1434 const pass_data pass_data_convert_switch =
1435 {
1436   GIMPLE_PASS, /* type */
1437   "switchconv", /* name */
1438   OPTGROUP_NONE, /* optinfo_flags */
1439   TV_TREE_SWITCH_CONVERSION, /* tv_id */
1440   ( PROP_cfg | PROP_ssa ), /* properties_required */
1441   0, /* properties_provided */
1442   0, /* properties_destroyed */
1443   0, /* todo_flags_start */
1444   TODO_update_ssa, /* todo_flags_finish */
1445 };
1446
1447 class pass_convert_switch : public gimple_opt_pass
1448 {
1449 public:
1450   pass_convert_switch (gcc::context *ctxt)
1451     : gimple_opt_pass (pass_data_convert_switch, ctxt)
1452   {}
1453
1454   /* opt_pass methods: */
1455   virtual bool gate (function *) { return flag_tree_switch_conversion != 0; }
1456   virtual unsigned int execute (function *);
1457
1458 }; // class pass_convert_switch
1459
1460 unsigned int
1461 pass_convert_switch::execute (function *fun)
1462 {
1463   basic_block bb;
1464
1465   FOR_EACH_BB_FN (bb, fun)
1466   {
1467     const char *failure_reason;
1468     gimple stmt = last_stmt (bb);
1469     if (stmt && gimple_code (stmt) == GIMPLE_SWITCH)
1470       {
1471         if (dump_file)
1472           {
1473             expanded_location loc = expand_location (gimple_location (stmt));
1474
1475             fprintf (dump_file, "beginning to process the following "
1476                      "SWITCH statement (%s:%d) : ------- \n",
1477                      loc.file, loc.line);
1478             print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1479             putc ('\n', dump_file);
1480           }
1481
1482         failure_reason = process_switch (as_a <gswitch *> (stmt));
1483         if (! failure_reason)
1484           {
1485             if (dump_file)
1486               {
1487                 fputs ("Switch converted\n", dump_file);
1488                 fputs ("--------------------------------\n", dump_file);
1489               }
1490
1491             /* Make no effort to update the post-dominator tree.  It is actually not
1492                that hard for the transformations we have performed, but it is not
1493                supported by iterate_fix_dominators.  */
1494             free_dominance_info (CDI_POST_DOMINATORS);
1495           }
1496         else
1497           {
1498             if (dump_file)
1499               {
1500                 fputs ("Bailing out - ", dump_file);
1501                 fputs (failure_reason, dump_file);
1502                 fputs ("\n--------------------------------\n", dump_file);
1503               }
1504           }
1505       }
1506   }
1507
1508   return 0;
1509 }
1510
1511 } // anon namespace
1512
1513 gimple_opt_pass *
1514 make_pass_convert_switch (gcc::context *ctxt)
1515 {
1516   return new pass_convert_switch (ctxt);
1517 }