]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-switch-conversion.c
fix typo in previous commit
[thirdparty/gcc.git] / gcc / tree-switch-conversion.c
CommitLineData
b6e99746
MJ
1/* Switch Conversion converts variable initializations based on switch
2 statements to initializations from a static array.
edb9b69e 3 Copyright (C) 2006, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
b6e99746
MJ
4 Contributed by Martin Jambor <jamborm@suse.cz>
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by the
10Free Software Foundation; either version 3, or (at your option) any
11later version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING3. If not, write to the Free
20Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
2102110-1301, USA. */
22
23/*
24 Switch initialization conversion
25
26The following pass changes simple initializations of scalars in a switch
fade902a
SB
27statement into initializations from a static array. Obviously, the values
28must be constant and known at compile time and a default branch must be
b6e99746
MJ
29provided. For example, the following code:
30
31 int a,b;
32
33 switch (argc)
34 {
35 case 1:
36 case 2:
37 a_1 = 8;
38 b_1 = 6;
39 break;
40 case 3:
41 a_2 = 9;
42 b_2 = 5;
43 break;
44 case 12:
45 a_3 = 10;
46 b_3 = 4;
47 break;
48 default:
49 a_4 = 16;
50 b_4 = 1;
886cd84f 51 break;
b6e99746
MJ
52 }
53 a_5 = PHI <a_1, a_2, a_3, a_4>
54 b_5 = PHI <b_1, b_2, b_3, b_4>
55
56
57is changed into:
58
59 static const int = CSWTCH01[] = {6, 6, 5, 1, 1, 1, 1, 1, 1, 1, 1, 4};
60 static const int = CSWTCH02[] = {8, 8, 9, 16, 16, 16, 16, 16, 16, 16,
61 16, 16, 10};
62
63 if (((unsigned) argc) - 1 < 11)
64 {
65 a_6 = CSWTCH02[argc - 1];
66 b_6 = CSWTCH01[argc - 1];
67 }
68 else
69 {
70 a_7 = 16;
71 b_7 = 1;
72 }
886cd84f
SB
73 a_5 = PHI <a_6, a_7>
74 b_b = PHI <b_6, b_7>
b6e99746
MJ
75
76There are further constraints. Specifically, the range of values across all
77case labels must not be bigger than SWITCH_CONVERSION_BRANCH_RATIO (default
78eight) times the number of the actual switch branches. */
79
80#include "config.h"
81#include "system.h"
82#include "coretypes.h"
83#include "tm.h"
b6e99746
MJ
84#include "line-map.h"
85#include "params.h"
86#include "flags.h"
87#include "tree.h"
88#include "basic-block.h"
89#include "tree-flow.h"
90#include "tree-flow-inline.h"
91#include "tree-ssa-operands.h"
b6e99746
MJ
92#include "input.h"
93#include "tree-pass.h"
cf835838 94#include "gimple-pretty-print.h"
b6e99746 95#include "tree-dump.h"
3fe1efe4 96#include "timevar.h"
f096c02a 97#include "langhooks.h"
b6e99746
MJ
98
99/* The main structure of the pass. */
100struct switch_conv_info
101{
886cd84f 102 /* The expression used to decide the switch branch. */
b6e99746
MJ
103 tree index_expr;
104
886cd84f
SB
105 /* The following integer constants store the minimum and maximum value
106 covered by the case labels. */
b6e99746 107 tree range_min;
886cd84f 108 tree range_max;
b6e99746 109
886cd84f
SB
110 /* The difference between the above two numbers. Stored here because it
111 is used in all the conversion heuristics, as well as for some of the
112 transformation, and it is expensive to re-compute it all the time. */
b6e99746
MJ
113 tree range_size;
114
886cd84f 115 /* Basic block that contains the actual GIMPLE_SWITCH. */
b6e99746
MJ
116 basic_block switch_bb;
117
886cd84f
SB
118 /* Basic block that is the target of the default case. */
119 basic_block default_bb;
120
121 /* The single successor block of all branches out of the GIMPLE_SWITCH,
122 if such a block exists. Otherwise NULL. */
b6e99746
MJ
123 basic_block final_bb;
124
886cd84f
SB
125 /* The probability of the default edge in the replaced switch. */
126 int default_prob;
127
128 /* The count of the default edge in the replaced switch. */
129 gcov_type default_count;
130
131 /* Combined count of all other (non-default) edges in the replaced switch. */
132 gcov_type other_count;
133
b6e99746
MJ
134 /* Number of phi nodes in the final bb (that we'll be replacing). */
135 int phi_count;
136
b1ae1681 137 /* Array of default values, in the same order as phi nodes. */
b6e99746
MJ
138 tree *default_values;
139
140 /* Constructors of new static arrays. */
141 VEC (constructor_elt, gc) **constructors;
142
143 /* Array of ssa names that are initialized with a value from a new static
144 array. */
145 tree *target_inbound_names;
146
147 /* Array of ssa names that are initialized with the default value if the
148 switch expression is out of range. */
149 tree *target_outbound_names;
150
b1ae1681
MJ
151 /* The first load statement that loads a temporary from a new static array.
152 */
726a989a 153 gimple arr_ref_first;
b6e99746
MJ
154
155 /* The last load statement that loads a temporary from a new static array. */
726a989a 156 gimple arr_ref_last;
b6e99746
MJ
157
158 /* String reason why the case wasn't a good candidate that is written to the
159 dump file, if there is one. */
160 const char *reason;
8e97bc2b
JJ
161
162 /* Parameters for expand_switch_using_bit_tests. Should be computed
163 the same way as in expand_case. */
886cd84f
SB
164 unsigned int uniq;
165 unsigned int count;
b6e99746
MJ
166};
167
886cd84f 168/* Collect information about GIMPLE_SWITCH statement SWTCH into INFO. */
b6e99746 169
886cd84f
SB
170static void
171collect_switch_conv_info (gimple swtch, struct switch_conv_info *info)
b6e99746 172{
726a989a 173 unsigned int branch_num = gimple_switch_num_labels (swtch);
886cd84f
SB
174 tree min_case, max_case;
175 unsigned int count, i;
176 edge e, e_default;
177 edge_iterator ei;
178
179 memset (info, 0, sizeof (*info));
b6e99746
MJ
180
181 /* The gimplifier has already sorted the cases by CASE_LOW and ensured there
8e97bc2b 182 is a default label which is the first in the vector. */
886cd84f 183 gcc_assert (CASE_LOW (gimple_switch_label (swtch, 0)) == NULL_TREE);
b6e99746 184
886cd84f
SB
185 /* Collect the bits we can deduce from the CFG. */
186 info->index_expr = gimple_switch_index (swtch);
187 info->switch_bb = gimple_bb (swtch);
188 info->default_bb =
189 label_to_block (CASE_LABEL (gimple_switch_label (swtch, 0)));
190 e_default = find_edge (info->switch_bb, info->default_bb);
191 info->default_prob = e_default->probability;
192 info->default_count = e_default->count;
193 FOR_EACH_EDGE (e, ei, info->switch_bb->succs)
194 if (e != e_default)
195 info->other_count += e->count;
b6e99746 196
886cd84f
SB
197 /* See if there is one common successor block for all branch
198 targets. If it exists, record it in FINAL_BB. */
199 FOR_EACH_EDGE (e, ei, info->switch_bb->succs)
200 {
201 if (! single_pred_p (e->dest))
202 {
203 info->final_bb = e->dest;
204 break;
205 }
206 }
207 if (info->final_bb)
208 FOR_EACH_EDGE (e, ei, info->switch_bb->succs)
209 {
210 if (e->dest == info->final_bb)
211 continue;
212
213 if (single_pred_p (e->dest)
214 && single_succ_p (e->dest)
215 && single_succ (e->dest) == info->final_bb)
216 continue;
217
218 info->final_bb = NULL;
219 break;
220 }
221
222 /* Get upper and lower bounds of case values, and the covered range. */
223 min_case = gimple_switch_label (swtch, 1);
726a989a 224 max_case = gimple_switch_label (swtch, branch_num - 1);
886cd84f
SB
225
226 info->range_min = CASE_LOW (min_case);
b6e99746 227 if (CASE_HIGH (max_case) != NULL_TREE)
886cd84f 228 info->range_max = CASE_HIGH (max_case);
b6e99746 229 else
886cd84f
SB
230 info->range_max = CASE_LOW (max_case);
231
232 info->range_size =
233 int_const_binop (MINUS_EXPR, info->range_max, info->range_min);
b6e99746 234
886cd84f
SB
235 /* Get a count of the number of case labels. Single-valued case labels
236 simply count as one, but a case range counts double, since it may
237 require two compares if it gets lowered as a branching tree. */
238 count = 0;
239 for (i = 1; i < branch_num; i++)
240 {
241 tree elt = gimple_switch_label (swtch, i);
242 count++;
243 if (CASE_HIGH (elt)
244 && ! tree_int_cst_equal (CASE_LOW (elt), CASE_HIGH (elt)))
245 count++;
246 }
247 info->count = count;
248
249 /* Get the number of unique non-default targets out of the GIMPLE_SWITCH
250 block. Assume a CFG cleanup would have already removed degenerate
251 switch statements, this allows us to just use EDGE_COUNT. */
252 info->uniq = EDGE_COUNT (gimple_bb (swtch)->succs) - 1;
253}
b6e99746 254
886cd84f
SB
255/* Checks whether the range given by individual case statements of the SWTCH
256 switch statement isn't too big and whether the number of branches actually
257 satisfies the size of the new array. */
b6e99746 258
886cd84f
SB
259static bool
260check_range (struct switch_conv_info *info)
261{
fade902a
SB
262 gcc_assert (info->range_size);
263 if (!host_integerp (info->range_size, 1))
b6e99746 264 {
fade902a 265 info->reason = "index range way too large or otherwise unusable";
b6e99746
MJ
266 return false;
267 }
268
fade902a 269 if ((unsigned HOST_WIDE_INT) tree_low_cst (info->range_size, 1)
886cd84f 270 > ((unsigned) info->count * SWITCH_CONVERSION_BRANCH_RATIO))
b6e99746 271 {
fade902a 272 info->reason = "the maximum range-branch ratio exceeded";
b6e99746
MJ
273 return false;
274 }
275
276 return true;
277}
278
886cd84f 279/* Checks whether all but the FINAL_BB basic blocks are empty. */
b6e99746
MJ
280
281static bool
886cd84f 282check_all_empty_except_final (struct switch_conv_info *info)
b6e99746 283{
b6e99746 284 edge e;
886cd84f 285 edge_iterator ei;
b6e99746 286
886cd84f 287 FOR_EACH_EDGE (e, ei, info->switch_bb->succs)
b6e99746 288 {
886cd84f
SB
289 if (e->dest == info->final_bb)
290 continue;
b6e99746 291
886cd84f 292 if (!empty_block_p (e->dest))
b6e99746 293 {
fade902a 294 info->reason = "bad case - a non-final BB not empty";
b6e99746
MJ
295 return false;
296 }
b6e99746
MJ
297 }
298
299 return true;
300}
301
302/* This function checks whether all required values in phi nodes in final_bb
303 are constants. Required values are those that correspond to a basic block
304 which is a part of the examined switch statement. It returns true if the
305 phi nodes are OK, otherwise false. */
306
307static bool
fade902a 308check_final_bb (struct switch_conv_info *info)
b6e99746 309{
726a989a 310 gimple_stmt_iterator gsi;
b6e99746 311
fade902a
SB
312 info->phi_count = 0;
313 for (gsi = gsi_start_phis (info->final_bb); !gsi_end_p (gsi); gsi_next (&gsi))
b6e99746 314 {
726a989a
RB
315 gimple phi = gsi_stmt (gsi);
316 unsigned int i;
b6e99746 317
fade902a 318 info->phi_count++;
b6e99746 319
726a989a 320 for (i = 0; i < gimple_phi_num_args (phi); i++)
b6e99746 321 {
726a989a 322 basic_block bb = gimple_phi_arg_edge (phi, i)->src;
b6e99746 323
fade902a
SB
324 if (bb == info->switch_bb
325 || (single_pred_p (bb) && single_pred (bb) == info->switch_bb))
b6e99746 326 {
f6e6e990
JJ
327 tree reloc, val;
328
329 val = gimple_phi_arg_def (phi, i);
330 if (!is_gimple_ip_invariant (val))
331 {
fade902a 332 info->reason = "non-invariant value from a case";
f6e6e990
JJ
333 return false; /* Non-invariant argument. */
334 }
335 reloc = initializer_constant_valid_p (val, TREE_TYPE (val));
336 if ((flag_pic && reloc != null_pointer_node)
337 || (!flag_pic && reloc == NULL_TREE))
338 {
339 if (reloc)
fade902a
SB
340 info->reason
341 = "value from a case would need runtime relocations";
f6e6e990 342 else
fade902a
SB
343 info->reason
344 = "value from a case is not a valid initializer";
f6e6e990
JJ
345 return false;
346 }
b6e99746
MJ
347 }
348 }
349 }
350
351 return true;
352}
353
354/* The following function allocates default_values, target_{in,out}_names and
355 constructors arrays. The last one is also populated with pointers to
356 vectors that will become constructors of new arrays. */
357
358static void
fade902a 359create_temp_arrays (struct switch_conv_info *info)
b6e99746
MJ
360{
361 int i;
362
fade902a
SB
363 info->default_values = XCNEWVEC (tree, info->phi_count * 3);
364 info->constructors = XCNEWVEC (VEC (constructor_elt, gc) *, info->phi_count);
365 info->target_inbound_names = info->default_values + info->phi_count;
366 info->target_outbound_names = info->target_inbound_names + info->phi_count;
367 for (i = 0; i < info->phi_count; i++)
368 info->constructors[i]
369 = VEC_alloc (constructor_elt, gc, tree_low_cst (info->range_size, 1) + 1);
b6e99746
MJ
370}
371
372/* Free the arrays created by create_temp_arrays(). The vectors that are
373 created by that function are not freed here, however, because they have
374 already become constructors and must be preserved. */
375
376static void
fade902a 377free_temp_arrays (struct switch_conv_info *info)
b6e99746 378{
fade902a
SB
379 XDELETEVEC (info->constructors);
380 XDELETEVEC (info->default_values);
b6e99746
MJ
381}
382
383/* Populate the array of default values in the order of phi nodes.
384 DEFAULT_CASE is the CASE_LABEL_EXPR for the default switch branch. */
385
386static void
fade902a 387gather_default_values (tree default_case, struct switch_conv_info *info)
b6e99746 388{
726a989a 389 gimple_stmt_iterator gsi;
b6e99746
MJ
390 basic_block bb = label_to_block (CASE_LABEL (default_case));
391 edge e;
726a989a 392 int i = 0;
b6e99746
MJ
393
394 gcc_assert (CASE_LOW (default_case) == NULL_TREE);
395
fade902a
SB
396 if (bb == info->final_bb)
397 e = find_edge (info->switch_bb, bb);
b6e99746
MJ
398 else
399 e = single_succ_edge (bb);
400
fade902a 401 for (gsi = gsi_start_phis (info->final_bb); !gsi_end_p (gsi); gsi_next (&gsi))
b6e99746 402 {
726a989a 403 gimple phi = gsi_stmt (gsi);
b6e99746
MJ
404 tree val = PHI_ARG_DEF_FROM_EDGE (phi, e);
405 gcc_assert (val);
fade902a 406 info->default_values[i++] = val;
b6e99746
MJ
407 }
408}
409
410/* The following function populates the vectors in the constructors array with
411 future contents of the static arrays. The vectors are populated in the
412 order of phi nodes. SWTCH is the switch statement being converted. */
413
414static void
fade902a 415build_constructors (gimple swtch, struct switch_conv_info *info)
b6e99746 416{
726a989a 417 unsigned i, branch_num = gimple_switch_num_labels (swtch);
fade902a 418 tree pos = info->range_min;
b6e99746 419
726a989a 420 for (i = 1; i < branch_num; i++)
b6e99746 421 {
726a989a 422 tree cs = gimple_switch_label (swtch, i);
b6e99746
MJ
423 basic_block bb = label_to_block (CASE_LABEL (cs));
424 edge e;
726a989a
RB
425 tree high;
426 gimple_stmt_iterator gsi;
b6e99746
MJ
427 int j;
428
fade902a
SB
429 if (bb == info->final_bb)
430 e = find_edge (info->switch_bb, bb);
b6e99746
MJ
431 else
432 e = single_succ_edge (bb);
433 gcc_assert (e);
434
435 while (tree_int_cst_lt (pos, CASE_LOW (cs)))
436 {
437 int k;
fade902a 438 for (k = 0; k < info->phi_count; k++)
b6e99746
MJ
439 {
440 constructor_elt *elt;
441
442 elt = VEC_quick_push (constructor_elt,
fade902a 443 info->constructors[k], NULL);
726a989a 444 elt->index = int_const_binop (MINUS_EXPR, pos,
fade902a
SB
445 info->range_min);
446 elt->value = info->default_values[k];
b6e99746
MJ
447 }
448
d35936ab 449 pos = int_const_binop (PLUS_EXPR, pos, integer_one_node);
b6e99746 450 }
b1ae1681 451 gcc_assert (tree_int_cst_equal (pos, CASE_LOW (cs)));
b6e99746
MJ
452
453 j = 0;
454 if (CASE_HIGH (cs))
455 high = CASE_HIGH (cs);
456 else
b1ae1681 457 high = CASE_LOW (cs);
fade902a 458 for (gsi = gsi_start_phis (info->final_bb);
726a989a 459 !gsi_end_p (gsi); gsi_next (&gsi))
b6e99746 460 {
726a989a 461 gimple phi = gsi_stmt (gsi);
b6e99746 462 tree val = PHI_ARG_DEF_FROM_EDGE (phi, e);
7f2a9982 463 tree low = CASE_LOW (cs);
b6e99746
MJ
464 pos = CASE_LOW (cs);
465
b8698a0f 466 do
b6e99746
MJ
467 {
468 constructor_elt *elt;
469
470 elt = VEC_quick_push (constructor_elt,
fade902a
SB
471 info->constructors[j], NULL);
472 elt->index = int_const_binop (MINUS_EXPR, pos, info->range_min);
b6e99746
MJ
473 elt->value = val;
474
d35936ab 475 pos = int_const_binop (PLUS_EXPR, pos, integer_one_node);
7156c8ab
MJ
476 } while (!tree_int_cst_lt (high, pos)
477 && tree_int_cst_lt (low, pos));
b6e99746
MJ
478 j++;
479 }
480 }
481}
482
7156c8ab
MJ
483/* If all values in the constructor vector are the same, return the value.
484 Otherwise return NULL_TREE. Not supposed to be called for empty
485 vectors. */
486
487static tree
488constructor_contains_same_values_p (VEC (constructor_elt, gc) *vec)
489{
8e97bc2b 490 unsigned int i;
7156c8ab 491 tree prev = NULL_TREE;
8e97bc2b 492 constructor_elt *elt;
7156c8ab 493
8e97bc2b 494 FOR_EACH_VEC_ELT (constructor_elt, vec, i, elt)
7156c8ab 495 {
7156c8ab
MJ
496 if (!prev)
497 prev = elt->value;
498 else if (!operand_equal_p (elt->value, prev, OEP_ONLY_CONST))
499 return NULL_TREE;
500 }
501 return prev;
502}
503
8e97bc2b
JJ
504/* Return type which should be used for array elements, either TYPE,
505 or for integral type some smaller integral type that can still hold
506 all the constants. */
507
508static tree
fade902a
SB
509array_value_type (gimple swtch, tree type, int num,
510 struct switch_conv_info *info)
8e97bc2b 511{
fade902a 512 unsigned int i, len = VEC_length (constructor_elt, info->constructors[num]);
8e97bc2b
JJ
513 constructor_elt *elt;
514 enum machine_mode mode;
515 int sign = 0;
516 tree smaller_type;
517
518 if (!INTEGRAL_TYPE_P (type))
519 return type;
520
521 mode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (TYPE_MODE (type)));
522 if (GET_MODE_SIZE (TYPE_MODE (type)) <= GET_MODE_SIZE (mode))
523 return type;
524
525 if (len < (optimize_bb_for_size_p (gimple_bb (swtch)) ? 2 : 32))
526 return type;
527
fade902a 528 FOR_EACH_VEC_ELT (constructor_elt, info->constructors[num], i, elt)
8e97bc2b
JJ
529 {
530 double_int cst;
531
532 if (TREE_CODE (elt->value) != INTEGER_CST)
533 return type;
534
535 cst = TREE_INT_CST (elt->value);
536 while (1)
537 {
538 unsigned int prec = GET_MODE_BITSIZE (mode);
539 if (prec > HOST_BITS_PER_WIDE_INT)
540 return type;
541
542 if (sign >= 0
543 && double_int_equal_p (cst, double_int_zext (cst, prec)))
544 {
545 if (sign == 0
546 && double_int_equal_p (cst, double_int_sext (cst, prec)))
547 break;
548 sign = 1;
549 break;
550 }
551 if (sign <= 0
552 && double_int_equal_p (cst, double_int_sext (cst, prec)))
553 {
554 sign = -1;
555 break;
556 }
557
558 if (sign == 1)
559 sign = 0;
560
561 mode = GET_MODE_WIDER_MODE (mode);
562 if (mode == VOIDmode
563 || GET_MODE_SIZE (mode) >= GET_MODE_SIZE (TYPE_MODE (type)))
564 return type;
565 }
566 }
567
568 if (sign == 0)
569 sign = TYPE_UNSIGNED (type) ? 1 : -1;
570 smaller_type = lang_hooks.types.type_for_mode (mode, sign >= 0);
571 if (GET_MODE_SIZE (TYPE_MODE (type))
572 <= GET_MODE_SIZE (TYPE_MODE (smaller_type)))
573 return type;
574
575 return smaller_type;
576}
577
b6e99746
MJ
578/* Create an appropriate array type and declaration and assemble a static array
579 variable. Also create a load statement that initializes the variable in
580 question with a value from the static array. SWTCH is the switch statement
581 being converted, NUM is the index to arrays of constructors, default values
582 and target SSA names for this particular array. ARR_INDEX_TYPE is the type
583 of the index of the new array, PHI is the phi node of the final BB that
584 corresponds to the value that will be loaded from the created array. TIDX
7156c8ab
MJ
585 is an ssa name of a temporary variable holding the index for loads from the
586 new array. */
b6e99746
MJ
587
588static void
726a989a 589build_one_array (gimple swtch, int num, tree arr_index_type, gimple phi,
fade902a 590 tree tidx, struct switch_conv_info *info)
b6e99746 591{
7156c8ab 592 tree name, cst;
726a989a 593 gimple load;
7156c8ab 594 gimple_stmt_iterator gsi = gsi_for_stmt (swtch);
c2255bc4 595 location_t loc = gimple_location (swtch);
b6e99746 596
fade902a 597 gcc_assert (info->default_values[num]);
b6e99746 598
726a989a 599 name = make_ssa_name (SSA_NAME_VAR (PHI_RESULT (phi)), NULL);
fade902a 600 info->target_inbound_names[num] = name;
b6e99746 601
fade902a 602 cst = constructor_contains_same_values_p (info->constructors[num]);
7156c8ab
MJ
603 if (cst)
604 load = gimple_build_assign (name, cst);
605 else
606 {
8e97bc2b 607 tree array_type, ctor, decl, value_type, fetch, default_type;
7156c8ab 608
fade902a
SB
609 default_type = TREE_TYPE (info->default_values[num]);
610 value_type = array_value_type (swtch, default_type, num, info);
7156c8ab 611 array_type = build_array_type (value_type, arr_index_type);
8e97bc2b
JJ
612 if (default_type != value_type)
613 {
614 unsigned int i;
615 constructor_elt *elt;
616
fade902a 617 FOR_EACH_VEC_ELT (constructor_elt, info->constructors[num], i, elt)
8e97bc2b
JJ
618 elt->value = fold_convert (value_type, elt->value);
619 }
fade902a 620 ctor = build_constructor (array_type, info->constructors[num]);
7156c8ab 621 TREE_CONSTANT (ctor) = true;
5f7ae6b6 622 TREE_STATIC (ctor) = true;
7156c8ab 623
c2255bc4 624 decl = build_decl (loc, VAR_DECL, NULL_TREE, array_type);
7156c8ab
MJ
625 TREE_STATIC (decl) = 1;
626 DECL_INITIAL (decl) = ctor;
627
628 DECL_NAME (decl) = create_tmp_var_name ("CSWTCH");
629 DECL_ARTIFICIAL (decl) = 1;
630 TREE_CONSTANT (decl) = 1;
2e3b4885 631 TREE_READONLY (decl) = 1;
7156c8ab
MJ
632 varpool_finalize_decl (decl);
633
634 fetch = build4 (ARRAY_REF, value_type, decl, tidx, NULL_TREE,
635 NULL_TREE);
8e97bc2b
JJ
636 if (default_type != value_type)
637 {
638 fetch = fold_convert (default_type, fetch);
639 fetch = force_gimple_operand_gsi (&gsi, fetch, true, NULL_TREE,
640 true, GSI_SAME_STMT);
641 }
7156c8ab
MJ
642 load = gimple_build_assign (name, fetch);
643 }
b6e99746 644
7156c8ab 645 SSA_NAME_DEF_STMT (name) = load;
726a989a 646 gsi_insert_before (&gsi, load, GSI_SAME_STMT);
7156c8ab 647 update_stmt (load);
fade902a 648 info->arr_ref_last = load;
b6e99746
MJ
649}
650
651/* Builds and initializes static arrays initialized with values gathered from
652 the SWTCH switch statement. Also creates statements that load values from
653 them. */
654
655static void
fade902a 656build_arrays (gimple swtch, struct switch_conv_info *info)
b6e99746
MJ
657{
658 tree arr_index_type;
edb9b69e 659 tree tidx, sub, tmp, utype;
726a989a
RB
660 gimple stmt;
661 gimple_stmt_iterator gsi;
b6e99746 662 int i;
db3927fb 663 location_t loc = gimple_location (swtch);
b6e99746 664
726a989a 665 gsi = gsi_for_stmt (swtch);
04e78aa9 666
edb9b69e 667 /* Make sure we do not generate arithmetics in a subrange. */
fade902a 668 utype = TREE_TYPE (info->index_expr);
edb9b69e
JJ
669 if (TREE_TYPE (utype))
670 utype = lang_hooks.types.type_for_mode (TYPE_MODE (TREE_TYPE (utype)), 1);
671 else
672 utype = lang_hooks.types.type_for_mode (TYPE_MODE (utype), 1);
673
fade902a 674 arr_index_type = build_index_type (info->range_size);
edb9b69e 675 tmp = create_tmp_var (utype, "csui");
9925bce0
RG
676 add_referenced_var (tmp);
677 tidx = make_ssa_name (tmp, NULL);
edb9b69e 678 sub = fold_build2_loc (loc, MINUS_EXPR, utype,
fade902a
SB
679 fold_convert_loc (loc, utype, info->index_expr),
680 fold_convert_loc (loc, utype, info->range_min));
fae1034e 681 sub = force_gimple_operand_gsi (&gsi, sub,
726a989a
RB
682 false, NULL, true, GSI_SAME_STMT);
683 stmt = gimple_build_assign (tidx, sub);
7156c8ab 684 SSA_NAME_DEF_STMT (tidx) = stmt;
b6e99746 685
726a989a 686 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
7156c8ab 687 update_stmt (stmt);
fade902a 688 info->arr_ref_first = stmt;
b6e99746 689
fade902a 690 for (gsi = gsi_start_phis (info->final_bb), i = 0;
726a989a 691 !gsi_end_p (gsi); gsi_next (&gsi), i++)
fade902a 692 build_one_array (swtch, i, arr_index_type, gsi_stmt (gsi), tidx, info);
b6e99746
MJ
693}
694
695/* Generates and appropriately inserts loads of default values at the position
696 given by BSI. Returns the last inserted statement. */
697
726a989a 698static gimple
fade902a 699gen_def_assigns (gimple_stmt_iterator *gsi, struct switch_conv_info *info)
b6e99746
MJ
700{
701 int i;
726a989a 702 gimple assign = NULL;
b6e99746 703
fade902a 704 for (i = 0; i < info->phi_count; i++)
b6e99746 705 {
726a989a 706 tree name
fade902a 707 = make_ssa_name (SSA_NAME_VAR (info->target_inbound_names[i]), NULL);
b6e99746 708
fade902a
SB
709 info->target_outbound_names[i] = name;
710 assign = gimple_build_assign (name, info->default_values[i]);
b6e99746 711 SSA_NAME_DEF_STMT (name) = assign;
726a989a 712 gsi_insert_before (gsi, assign, GSI_SAME_STMT);
7156c8ab 713 update_stmt (assign);
b6e99746
MJ
714 }
715 return assign;
716}
717
718/* Deletes the unused bbs and edges that now contain the switch statement and
719 its empty branch bbs. BBD is the now dead BB containing the original switch
720 statement, FINAL is the last BB of the converted switch statement (in terms
721 of succession). */
722
723static void
724prune_bbs (basic_block bbd, basic_block final)
725{
726 edge_iterator ei;
727 edge e;
728
729 for (ei = ei_start (bbd->succs); (e = ei_safe_edge (ei)); )
730 {
731 basic_block bb;
732 bb = e->dest;
733 remove_edge (e);
734 if (bb != final)
735 delete_basic_block (bb);
736 }
737 delete_basic_block (bbd);
738}
739
740/* Add values to phi nodes in final_bb for the two new edges. E1F is the edge
741 from the basic block loading values from an array and E2F from the basic
742 block loading default values. BBF is the last switch basic block (see the
743 bbf description in the comment below). */
744
745static void
fade902a
SB
746fix_phi_nodes (edge e1f, edge e2f, basic_block bbf,
747 struct switch_conv_info *info)
b6e99746 748{
726a989a 749 gimple_stmt_iterator gsi;
b6e99746
MJ
750 int i;
751
726a989a
RB
752 for (gsi = gsi_start_phis (bbf), i = 0;
753 !gsi_end_p (gsi); gsi_next (&gsi), i++)
b6e99746 754 {
726a989a 755 gimple phi = gsi_stmt (gsi);
fade902a
SB
756 add_phi_arg (phi, info->target_inbound_names[i], e1f, UNKNOWN_LOCATION);
757 add_phi_arg (phi, info->target_outbound_names[i], e2f, UNKNOWN_LOCATION);
b6e99746 758 }
b6e99746
MJ
759}
760
761/* Creates a check whether the switch expression value actually falls into the
762 range given by all the cases. If it does not, the temporaries are loaded
763 with default values instead. SWTCH is the switch statement being converted.
764
765 bb0 is the bb with the switch statement, however, we'll end it with a
766 condition instead.
767
768 bb1 is the bb to be used when the range check went ok. It is derived from
769 the switch BB
770
771 bb2 is the bb taken when the expression evaluated outside of the range
772 covered by the created arrays. It is populated by loads of default
773 values.
774
775 bbF is a fall through for both bb1 and bb2 and contains exactly what
776 originally followed the switch statement.
777
778 bbD contains the switch statement (in the end). It is unreachable but we
779 still need to strip off its edges.
780*/
781
782static void
fade902a 783gen_inbound_check (gimple swtch, struct switch_conv_info *info)
b6e99746 784{
c2255bc4
AH
785 tree label_decl1 = create_artificial_label (UNKNOWN_LOCATION);
786 tree label_decl2 = create_artificial_label (UNKNOWN_LOCATION);
787 tree label_decl3 = create_artificial_label (UNKNOWN_LOCATION);
726a989a 788 gimple label1, label2, label3;
edb9b69e 789 tree utype, tidx;
b6e99746
MJ
790 tree bound;
791
726a989a 792 gimple cond_stmt;
b6e99746 793
726a989a
RB
794 gimple last_assign;
795 gimple_stmt_iterator gsi;
b6e99746
MJ
796 basic_block bb0, bb1, bb2, bbf, bbd;
797 edge e01, e02, e21, e1d, e1f, e2f;
db3927fb 798 location_t loc = gimple_location (swtch);
b6e99746 799
fade902a 800 gcc_assert (info->default_values);
6ab1ab14
SB
801
802 /* Make no effort to update the post-dominator tree. It is actually not
803 that hard for the transformations we have performed, but it is not
804 supported by iterate_fix_dominators.
805 Freeing post-dominance info is dome early to avoid pointless work in
806 create_basic_block, which is called when we split SWITCH_BB. */
807 free_dominance_info (CDI_POST_DOMINATORS);
808
726a989a 809 bb0 = gimple_bb (swtch);
b6e99746 810
fade902a 811 tidx = gimple_assign_lhs (info->arr_ref_first);
edb9b69e 812 utype = TREE_TYPE (tidx);
145544ab 813
b6e99746 814 /* (end of) block 0 */
fade902a 815 gsi = gsi_for_stmt (info->arr_ref_first);
edb9b69e 816 gsi_next (&gsi);
b6e99746 817
fade902a 818 bound = fold_convert_loc (loc, utype, info->range_size);
edb9b69e 819 cond_stmt = gimple_build_cond (LE_EXPR, tidx, bound, NULL_TREE, NULL_TREE);
726a989a 820 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
7156c8ab 821 update_stmt (cond_stmt);
b6e99746
MJ
822
823 /* block 2 */
726a989a
RB
824 label2 = gimple_build_label (label_decl2);
825 gsi_insert_before (&gsi, label2, GSI_SAME_STMT);
fade902a 826 last_assign = gen_def_assigns (&gsi, info);
b6e99746
MJ
827
828 /* block 1 */
726a989a
RB
829 label1 = gimple_build_label (label_decl1);
830 gsi_insert_before (&gsi, label1, GSI_SAME_STMT);
b6e99746
MJ
831
832 /* block F */
fade902a 833 gsi = gsi_start_bb (info->final_bb);
726a989a
RB
834 label3 = gimple_build_label (label_decl3);
835 gsi_insert_before (&gsi, label3, GSI_SAME_STMT);
b6e99746
MJ
836
837 /* cfg fix */
726a989a 838 e02 = split_block (bb0, cond_stmt);
b6e99746
MJ
839 bb2 = e02->dest;
840
841 e21 = split_block (bb2, last_assign);
842 bb1 = e21->dest;
843 remove_edge (e21);
844
fade902a 845 e1d = split_block (bb1, info->arr_ref_last);
b6e99746
MJ
846 bbd = e1d->dest;
847 remove_edge (e1d);
848
849 /* flags and profiles of the edge for in-range values */
850 e01 = make_edge (bb0, bb1, EDGE_TRUE_VALUE);
fade902a
SB
851 e01->probability = REG_BR_PROB_BASE - info->default_prob;
852 e01->count = info->other_count;
b6e99746
MJ
853
854 /* flags and profiles of the edge taking care of out-of-range values */
855 e02->flags &= ~EDGE_FALLTHRU;
856 e02->flags |= EDGE_FALSE_VALUE;
fade902a
SB
857 e02->probability = info->default_prob;
858 e02->count = info->default_count;
b6e99746 859
fade902a 860 bbf = info->final_bb;
b6e99746
MJ
861
862 e1f = make_edge (bb1, bbf, EDGE_FALLTHRU);
863 e1f->probability = REG_BR_PROB_BASE;
fade902a 864 e1f->count = info->other_count;
b6e99746
MJ
865
866 e2f = make_edge (bb2, bbf, EDGE_FALLTHRU);
867 e2f->probability = REG_BR_PROB_BASE;
fade902a 868 e2f->count = info->default_count;
b6e99746
MJ
869
870 /* frequencies of the new BBs */
871 bb1->frequency = EDGE_FREQUENCY (e01);
872 bb2->frequency = EDGE_FREQUENCY (e02);
873 bbf->frequency = EDGE_FREQUENCY (e1f) + EDGE_FREQUENCY (e2f);
874
6ab1ab14
SB
875 /* Tidy blocks that have become unreachable. */
876 prune_bbs (bbd, info->final_bb);
b6e99746 877
6ab1ab14 878 /* Fixup the PHI nodes in bbF. */
fade902a 879 fix_phi_nodes (e1f, e2f, bbf, info);
b6e99746 880
6ab1ab14
SB
881 /* Fix the dominator tree, if it is available. */
882 if (dom_info_available_p (CDI_DOMINATORS))
883 {
884 VEC (basic_block, heap) *bbs_to_fix_dom;
885
886 set_immediate_dominator (CDI_DOMINATORS, bb1, bb0);
887 set_immediate_dominator (CDI_DOMINATORS, bb2, bb0);
888 if (! get_immediate_dominator(CDI_DOMINATORS, bbf))
889 /* If bbD was the immediate dominator ... */
890 set_immediate_dominator (CDI_DOMINATORS, bbf, bb0);
891
892 bbs_to_fix_dom = VEC_alloc (basic_block, heap, 4);
893 VEC_quick_push (basic_block, bbs_to_fix_dom, bb0);
894 VEC_quick_push (basic_block, bbs_to_fix_dom, bb1);
895 VEC_quick_push (basic_block, bbs_to_fix_dom, bb2);
896 VEC_quick_push (basic_block, bbs_to_fix_dom, bbf);
897
898 iterate_fix_dominators (CDI_DOMINATORS, bbs_to_fix_dom, true);
899 VEC_free (basic_block, heap, bbs_to_fix_dom);
900 }
b6e99746
MJ
901}
902
903/* The following function is invoked on every switch statement (the current one
904 is given in SWTCH) and runs the individual phases of switch conversion on it
fade902a
SB
905 one after another until one fails or the conversion is completed.
906 Returns NULL on success, or a pointer to a string with the reason why the
907 conversion failed. */
b6e99746 908
fade902a 909static const char *
726a989a 910process_switch (gimple swtch)
b6e99746 911{
fade902a 912 struct switch_conv_info info;
b6e99746 913
886cd84f
SB
914 /* Degenerate case with only a default label should never happen. */
915 gcc_checking_assert (gimple_switch_num_labels (swtch) > 1);
916
917 collect_switch_conv_info (swtch, &info);
918
919 /* No error markers should reach here (they should be filtered out
920 during gimplification). */
921 gcc_checking_assert (TREE_TYPE (info.index_expr) != error_mark_node);
922
923 /* If there is no common successor, we cannot do the transformation. */
924 if (! info.final_bb)
925 return "no common successor to all case label target blocks found";
926
927 if (info.uniq <= 2)
928 {
929 if (expand_switch_using_bit_tests_p (info.index_expr, info.range_size,
930 info.uniq, info.count))
931 return "expanding as bit test is preferable";
932 }
b6e99746
MJ
933
934 /* Check the case label values are within reasonable range: */
886cd84f 935 if (!check_range (&info))
fade902a
SB
936 {
937 gcc_assert (info.reason);
938 return info.reason;
939 }
b6e99746
MJ
940
941 /* For all the cases, see whether they are empty, the assignments they
942 represent constant and so on... */
886cd84f 943 if (! check_all_empty_except_final (&info))
8e97bc2b 944 {
886cd84f
SB
945 gcc_assert (info.reason);
946 return info.reason;
8e97bc2b 947 }
fade902a
SB
948 if (!check_final_bb (&info))
949 {
950 gcc_assert (info.reason);
951 return info.reason;
952 }
b6e99746
MJ
953
954 /* At this point all checks have passed and we can proceed with the
955 transformation. */
956
fade902a
SB
957 create_temp_arrays (&info);
958 gather_default_values (gimple_switch_label (swtch, 0), &info);
959 build_constructors (swtch, &info);
b6e99746 960
fade902a
SB
961 build_arrays (swtch, &info); /* Build the static arrays and assignments. */
962 gen_inbound_check (swtch, &info); /* Build the bounds check. */
b6e99746
MJ
963
964 /* Cleanup: */
fade902a
SB
965 free_temp_arrays (&info);
966 return NULL;
b6e99746
MJ
967}
968
969/* The main function of the pass scans statements for switches and invokes
970 process_switch on them. */
971
972static unsigned int
973do_switchconv (void)
974{
975 basic_block bb;
976
977 FOR_EACH_BB (bb)
978 {
fade902a 979 const char *failure_reason;
726a989a
RB
980 gimple stmt = last_stmt (bb);
981 if (stmt && gimple_code (stmt) == GIMPLE_SWITCH)
b6e99746 982 {
b6e99746
MJ
983 if (dump_file)
984 {
726a989a
RB
985 expanded_location loc = expand_location (gimple_location (stmt));
986
b6e99746
MJ
987 fprintf (dump_file, "beginning to process the following "
988 "SWITCH statement (%s:%d) : ------- \n",
989 loc.file, loc.line);
726a989a 990 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
edb30094 991 putc ('\n', dump_file);
b6e99746
MJ
992 }
993
fade902a
SB
994 failure_reason = process_switch (stmt);
995 if (! failure_reason)
b6e99746
MJ
996 {
997 if (dump_file)
998 {
edb30094
UB
999 fputs ("Switch converted\n", dump_file);
1000 fputs ("--------------------------------\n", dump_file);
b6e99746
MJ
1001 }
1002 }
1003 else
1004 {
1005 if (dump_file)
1006 {
edb30094 1007 fputs ("Bailing out - ", dump_file);
fade902a
SB
1008 fputs (failure_reason, dump_file);
1009 fputs ("\n--------------------------------\n", dump_file);
b6e99746
MJ
1010 }
1011 }
1012 }
1013 }
1014
1015 return 0;
1016}
1017
1018/* The pass gate. */
1019
1020static bool
1021switchconv_gate (void)
1022{
1023 return flag_tree_switch_conversion != 0;
1024}
1025
1026struct gimple_opt_pass pass_convert_switch =
1027{
1028 {
1029 GIMPLE_PASS,
1030 "switchconv", /* name */
b1ae1681 1031 switchconv_gate, /* gate */
b6e99746
MJ
1032 do_switchconv, /* execute */
1033 NULL, /* sub */
1034 NULL, /* next */
1035 0, /* static_pass_number */
a167a676 1036 TV_TREE_SWITCH_CONVERSION, /* tv_id */
b6e99746
MJ
1037 PROP_cfg | PROP_ssa, /* properties_required */
1038 0, /* properties_provided */
1039 0, /* properties_destroyed */
1040 0, /* todo_flags_start */
22c5fa5f 1041 TODO_update_ssa
b6e99746
MJ
1042 | TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */
1043 }
1044};