]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/stmt.c
coretypes.h: Include hash-table.h and hash-set.h for host files.
[thirdparty/gcc.git] / gcc / stmt.c
1 /* Expands front end tree to back end RTL for GCC
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 /* This file handles the generation of rtl code from tree structure
21 above the level of expressions, using subroutines in exp*.c and emit-rtl.c.
22 The functions whose names start with `expand_' are called by the
23 expander to generate RTL instructions for various kinds of constructs. */
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29
30 #include "rtl.h"
31 #include "hard-reg-set.h"
32 #include "input.h"
33 #include "alias.h"
34 #include "symtab.h"
35 #include "tree.h"
36 #include "fold-const.h"
37 #include "varasm.h"
38 #include "stor-layout.h"
39 #include "tm_p.h"
40 #include "flags.h"
41 #include "except.h"
42 #include "function.h"
43 #include "insn-config.h"
44 #include "expmed.h"
45 #include "dojump.h"
46 #include "explow.h"
47 #include "calls.h"
48 #include "emit-rtl.h"
49 #include "stmt.h"
50 #include "expr.h"
51 #include "libfuncs.h"
52 #include "recog.h"
53 #include "diagnostic-core.h"
54 #include "output.h"
55 #include "langhooks.h"
56 #include "predict.h"
57 #include "insn-codes.h"
58 #include "optabs.h"
59 #include "target.h"
60 #include "cfganal.h"
61 #include "basic-block.h"
62 #include "tree-ssa-alias.h"
63 #include "internal-fn.h"
64 #include "gimple-expr.h"
65 #include "is-a.h"
66 #include "gimple.h"
67 #include "regs.h"
68 #include "alloc-pool.h"
69 #include "pretty-print.h"
70 #include "params.h"
71 #include "dumpfile.h"
72 #include "builtins.h"
73
74 \f
75 /* Functions and data structures for expanding case statements. */
76
77 /* Case label structure, used to hold info on labels within case
78 statements. We handle "range" labels; for a single-value label
79 as in C, the high and low limits are the same.
80
81 We start with a vector of case nodes sorted in ascending order, and
82 the default label as the last element in the vector. Before expanding
83 to RTL, we transform this vector into a list linked via the RIGHT
84 fields in the case_node struct. Nodes with higher case values are
85 later in the list.
86
87 Switch statements can be output in three forms. A branch table is
88 used if there are more than a few labels and the labels are dense
89 within the range between the smallest and largest case value. If a
90 branch table is used, no further manipulations are done with the case
91 node chain.
92
93 The alternative to the use of a branch table is to generate a series
94 of compare and jump insns. When that is done, we use the LEFT, RIGHT,
95 and PARENT fields to hold a binary tree. Initially the tree is
96 totally unbalanced, with everything on the right. We balance the tree
97 with nodes on the left having lower case values than the parent
98 and nodes on the right having higher values. We then output the tree
99 in order.
100
101 For very small, suitable switch statements, we can generate a series
102 of simple bit test and branches instead. */
103
104 struct case_node
105 {
106 struct case_node *left; /* Left son in binary tree */
107 struct case_node *right; /* Right son in binary tree; also node chain */
108 struct case_node *parent; /* Parent of node in binary tree */
109 tree low; /* Lowest index value for this label */
110 tree high; /* Highest index value for this label */
111 tree code_label; /* Label to jump to when node matches */
112 int prob; /* Probability of taking this case. */
113 /* Probability of reaching subtree rooted at this node */
114 int subtree_prob;
115 };
116
117 typedef struct case_node case_node;
118 typedef struct case_node *case_node_ptr;
119
120 extern basic_block label_to_block_fn (struct function *, tree);
121 \f
122 static bool check_unique_operand_names (tree, tree, tree);
123 static char *resolve_operand_name_1 (char *, tree, tree, tree);
124 static void balance_case_nodes (case_node_ptr *, case_node_ptr);
125 static int node_has_low_bound (case_node_ptr, tree);
126 static int node_has_high_bound (case_node_ptr, tree);
127 static int node_is_bounded (case_node_ptr, tree);
128 static void emit_case_nodes (rtx, case_node_ptr, rtx_code_label *, int, tree);
129 \f
130 /* Return the rtx-label that corresponds to a LABEL_DECL,
131 creating it if necessary. */
132
133 rtx_insn *
134 label_rtx (tree label)
135 {
136 gcc_assert (TREE_CODE (label) == LABEL_DECL);
137
138 if (!DECL_RTL_SET_P (label))
139 {
140 rtx_code_label *r = gen_label_rtx ();
141 SET_DECL_RTL (label, r);
142 if (FORCED_LABEL (label) || DECL_NONLOCAL (label))
143 LABEL_PRESERVE_P (r) = 1;
144 }
145
146 return as_a <rtx_insn *> (DECL_RTL (label));
147 }
148
149 /* As above, but also put it on the forced-reference list of the
150 function that contains it. */
151 rtx_insn *
152 force_label_rtx (tree label)
153 {
154 rtx_insn *ref = label_rtx (label);
155 tree function = decl_function_context (label);
156
157 gcc_assert (function);
158
159 forced_labels = gen_rtx_INSN_LIST (VOIDmode, ref, forced_labels);
160 return ref;
161 }
162
163 /* As label_rtx, but ensures (in check build), that returned value is
164 an existing label (i.e. rtx with code CODE_LABEL). */
165 rtx_code_label *
166 jump_target_rtx (tree label)
167 {
168 return as_a <rtx_code_label *> (label_rtx (label));
169 }
170
171 /* Add an unconditional jump to LABEL as the next sequential instruction. */
172
173 void
174 emit_jump (rtx label)
175 {
176 do_pending_stack_adjust ();
177 emit_jump_insn (gen_jump (label));
178 emit_barrier ();
179 }
180 \f
181 /* Handle goto statements and the labels that they can go to. */
182
183 /* Specify the location in the RTL code of a label LABEL,
184 which is a LABEL_DECL tree node.
185
186 This is used for the kind of label that the user can jump to with a
187 goto statement, and for alternatives of a switch or case statement.
188 RTL labels generated for loops and conditionals don't go through here;
189 they are generated directly at the RTL level, by other functions below.
190
191 Note that this has nothing to do with defining label *names*.
192 Languages vary in how they do that and what that even means. */
193
194 void
195 expand_label (tree label)
196 {
197 rtx_code_label *label_r = jump_target_rtx (label);
198
199 do_pending_stack_adjust ();
200 emit_label (label_r);
201 if (DECL_NAME (label))
202 LABEL_NAME (DECL_RTL (label)) = IDENTIFIER_POINTER (DECL_NAME (label));
203
204 if (DECL_NONLOCAL (label))
205 {
206 expand_builtin_setjmp_receiver (NULL);
207 nonlocal_goto_handler_labels
208 = gen_rtx_INSN_LIST (VOIDmode, label_r,
209 nonlocal_goto_handler_labels);
210 }
211
212 if (FORCED_LABEL (label))
213 forced_labels = gen_rtx_INSN_LIST (VOIDmode, label_r, forced_labels);
214
215 if (DECL_NONLOCAL (label) || FORCED_LABEL (label))
216 maybe_set_first_label_num (label_r);
217 }
218 \f
219 /* Parse the output constraint pointed to by *CONSTRAINT_P. It is the
220 OPERAND_NUMth output operand, indexed from zero. There are NINPUTS
221 inputs and NOUTPUTS outputs to this extended-asm. Upon return,
222 *ALLOWS_MEM will be TRUE iff the constraint allows the use of a
223 memory operand. Similarly, *ALLOWS_REG will be TRUE iff the
224 constraint allows the use of a register operand. And, *IS_INOUT
225 will be true if the operand is read-write, i.e., if it is used as
226 an input as well as an output. If *CONSTRAINT_P is not in
227 canonical form, it will be made canonical. (Note that `+' will be
228 replaced with `=' as part of this process.)
229
230 Returns TRUE if all went well; FALSE if an error occurred. */
231
232 bool
233 parse_output_constraint (const char **constraint_p, int operand_num,
234 int ninputs, int noutputs, bool *allows_mem,
235 bool *allows_reg, bool *is_inout)
236 {
237 const char *constraint = *constraint_p;
238 const char *p;
239
240 /* Assume the constraint doesn't allow the use of either a register
241 or memory. */
242 *allows_mem = false;
243 *allows_reg = false;
244
245 /* Allow the `=' or `+' to not be at the beginning of the string,
246 since it wasn't explicitly documented that way, and there is a
247 large body of code that puts it last. Swap the character to
248 the front, so as not to uglify any place else. */
249 p = strchr (constraint, '=');
250 if (!p)
251 p = strchr (constraint, '+');
252
253 /* If the string doesn't contain an `=', issue an error
254 message. */
255 if (!p)
256 {
257 error ("output operand constraint lacks %<=%>");
258 return false;
259 }
260
261 /* If the constraint begins with `+', then the operand is both read
262 from and written to. */
263 *is_inout = (*p == '+');
264
265 /* Canonicalize the output constraint so that it begins with `='. */
266 if (p != constraint || *is_inout)
267 {
268 char *buf;
269 size_t c_len = strlen (constraint);
270
271 if (p != constraint)
272 warning (0, "output constraint %qc for operand %d "
273 "is not at the beginning",
274 *p, operand_num);
275
276 /* Make a copy of the constraint. */
277 buf = XALLOCAVEC (char, c_len + 1);
278 strcpy (buf, constraint);
279 /* Swap the first character and the `=' or `+'. */
280 buf[p - constraint] = buf[0];
281 /* Make sure the first character is an `='. (Until we do this,
282 it might be a `+'.) */
283 buf[0] = '=';
284 /* Replace the constraint with the canonicalized string. */
285 *constraint_p = ggc_alloc_string (buf, c_len);
286 constraint = *constraint_p;
287 }
288
289 /* Loop through the constraint string. */
290 for (p = constraint + 1; *p; p += CONSTRAINT_LEN (*p, p))
291 switch (*p)
292 {
293 case '+':
294 case '=':
295 error ("operand constraint contains incorrectly positioned "
296 "%<+%> or %<=%>");
297 return false;
298
299 case '%':
300 if (operand_num + 1 == ninputs + noutputs)
301 {
302 error ("%<%%%> constraint used with last operand");
303 return false;
304 }
305 break;
306
307 case '?': case '!': case '*': case '&': case '#':
308 case '$': case '^':
309 case 'E': case 'F': case 'G': case 'H':
310 case 's': case 'i': case 'n':
311 case 'I': case 'J': case 'K': case 'L': case 'M':
312 case 'N': case 'O': case 'P': case ',':
313 break;
314
315 case '0': case '1': case '2': case '3': case '4':
316 case '5': case '6': case '7': case '8': case '9':
317 case '[':
318 error ("matching constraint not valid in output operand");
319 return false;
320
321 case '<': case '>':
322 /* ??? Before flow, auto inc/dec insns are not supposed to exist,
323 excepting those that expand_call created. So match memory
324 and hope. */
325 *allows_mem = true;
326 break;
327
328 case 'g': case 'X':
329 *allows_reg = true;
330 *allows_mem = true;
331 break;
332
333 default:
334 if (!ISALPHA (*p))
335 break;
336 enum constraint_num cn = lookup_constraint (p);
337 if (reg_class_for_constraint (cn) != NO_REGS
338 || insn_extra_address_constraint (cn))
339 *allows_reg = true;
340 else if (insn_extra_memory_constraint (cn))
341 *allows_mem = true;
342 else
343 insn_extra_constraint_allows_reg_mem (cn, allows_reg, allows_mem);
344 break;
345 }
346
347 return true;
348 }
349
350 /* Similar, but for input constraints. */
351
352 bool
353 parse_input_constraint (const char **constraint_p, int input_num,
354 int ninputs, int noutputs, int ninout,
355 const char * const * constraints,
356 bool *allows_mem, bool *allows_reg)
357 {
358 const char *constraint = *constraint_p;
359 const char *orig_constraint = constraint;
360 size_t c_len = strlen (constraint);
361 size_t j;
362 bool saw_match = false;
363
364 /* Assume the constraint doesn't allow the use of either
365 a register or memory. */
366 *allows_mem = false;
367 *allows_reg = false;
368
369 /* Make sure constraint has neither `=', `+', nor '&'. */
370
371 for (j = 0; j < c_len; j += CONSTRAINT_LEN (constraint[j], constraint+j))
372 switch (constraint[j])
373 {
374 case '+': case '=': case '&':
375 if (constraint == orig_constraint)
376 {
377 error ("input operand constraint contains %qc", constraint[j]);
378 return false;
379 }
380 break;
381
382 case '%':
383 if (constraint == orig_constraint
384 && input_num + 1 == ninputs - ninout)
385 {
386 error ("%<%%%> constraint used with last operand");
387 return false;
388 }
389 break;
390
391 case '<': case '>':
392 case '?': case '!': case '*': case '#':
393 case '$': case '^':
394 case 'E': case 'F': case 'G': case 'H':
395 case 's': case 'i': case 'n':
396 case 'I': case 'J': case 'K': case 'L': case 'M':
397 case 'N': case 'O': case 'P': case ',':
398 break;
399
400 /* Whether or not a numeric constraint allows a register is
401 decided by the matching constraint, and so there is no need
402 to do anything special with them. We must handle them in
403 the default case, so that we don't unnecessarily force
404 operands to memory. */
405 case '0': case '1': case '2': case '3': case '4':
406 case '5': case '6': case '7': case '8': case '9':
407 {
408 char *end;
409 unsigned long match;
410
411 saw_match = true;
412
413 match = strtoul (constraint + j, &end, 10);
414 if (match >= (unsigned long) noutputs)
415 {
416 error ("matching constraint references invalid operand number");
417 return false;
418 }
419
420 /* Try and find the real constraint for this dup. Only do this
421 if the matching constraint is the only alternative. */
422 if (*end == '\0'
423 && (j == 0 || (j == 1 && constraint[0] == '%')))
424 {
425 constraint = constraints[match];
426 *constraint_p = constraint;
427 c_len = strlen (constraint);
428 j = 0;
429 /* ??? At the end of the loop, we will skip the first part of
430 the matched constraint. This assumes not only that the
431 other constraint is an output constraint, but also that
432 the '=' or '+' come first. */
433 break;
434 }
435 else
436 j = end - constraint;
437 /* Anticipate increment at end of loop. */
438 j--;
439 }
440 /* Fall through. */
441
442 case 'g': case 'X':
443 *allows_reg = true;
444 *allows_mem = true;
445 break;
446
447 default:
448 if (! ISALPHA (constraint[j]))
449 {
450 error ("invalid punctuation %qc in constraint", constraint[j]);
451 return false;
452 }
453 enum constraint_num cn = lookup_constraint (constraint + j);
454 if (reg_class_for_constraint (cn) != NO_REGS
455 || insn_extra_address_constraint (cn))
456 *allows_reg = true;
457 else if (insn_extra_memory_constraint (cn))
458 *allows_mem = true;
459 else
460 insn_extra_constraint_allows_reg_mem (cn, allows_reg, allows_mem);
461 break;
462 }
463
464 if (saw_match && !*allows_reg)
465 warning (0, "matching constraint does not allow a register");
466
467 return true;
468 }
469
470 /* Return DECL iff there's an overlap between *REGS and DECL, where DECL
471 can be an asm-declared register. Called via walk_tree. */
472
473 static tree
474 decl_overlaps_hard_reg_set_p (tree *declp, int *walk_subtrees ATTRIBUTE_UNUSED,
475 void *data)
476 {
477 tree decl = *declp;
478 const HARD_REG_SET *const regs = (const HARD_REG_SET *) data;
479
480 if (TREE_CODE (decl) == VAR_DECL)
481 {
482 if (DECL_HARD_REGISTER (decl)
483 && REG_P (DECL_RTL (decl))
484 && REGNO (DECL_RTL (decl)) < FIRST_PSEUDO_REGISTER)
485 {
486 rtx reg = DECL_RTL (decl);
487
488 if (overlaps_hard_reg_set_p (*regs, GET_MODE (reg), REGNO (reg)))
489 return decl;
490 }
491 walk_subtrees = 0;
492 }
493 else if (TYPE_P (decl) || TREE_CODE (decl) == PARM_DECL)
494 walk_subtrees = 0;
495 return NULL_TREE;
496 }
497
498 /* If there is an overlap between *REGS and DECL, return the first overlap
499 found. */
500 tree
501 tree_overlaps_hard_reg_set (tree decl, HARD_REG_SET *regs)
502 {
503 return walk_tree (&decl, decl_overlaps_hard_reg_set_p, regs, NULL);
504 }
505
506
507 /* A subroutine of expand_asm_operands. Check that all operand names
508 are unique. Return true if so. We rely on the fact that these names
509 are identifiers, and so have been canonicalized by get_identifier,
510 so all we need are pointer comparisons. */
511
512 static bool
513 check_unique_operand_names (tree outputs, tree inputs, tree labels)
514 {
515 tree i, j, i_name = NULL_TREE;
516
517 for (i = outputs; i ; i = TREE_CHAIN (i))
518 {
519 i_name = TREE_PURPOSE (TREE_PURPOSE (i));
520 if (! i_name)
521 continue;
522
523 for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
524 if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
525 goto failure;
526 }
527
528 for (i = inputs; i ; i = TREE_CHAIN (i))
529 {
530 i_name = TREE_PURPOSE (TREE_PURPOSE (i));
531 if (! i_name)
532 continue;
533
534 for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
535 if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
536 goto failure;
537 for (j = outputs; j ; j = TREE_CHAIN (j))
538 if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
539 goto failure;
540 }
541
542 for (i = labels; i ; i = TREE_CHAIN (i))
543 {
544 i_name = TREE_PURPOSE (i);
545 if (! i_name)
546 continue;
547
548 for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
549 if (simple_cst_equal (i_name, TREE_PURPOSE (j)))
550 goto failure;
551 for (j = inputs; j ; j = TREE_CHAIN (j))
552 if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
553 goto failure;
554 }
555
556 return true;
557
558 failure:
559 error ("duplicate asm operand name %qs", TREE_STRING_POINTER (i_name));
560 return false;
561 }
562
563 /* Resolve the names of the operands in *POUTPUTS and *PINPUTS to numbers,
564 and replace the name expansions in STRING and in the constraints to
565 those numbers. This is generally done in the front end while creating
566 the ASM_EXPR generic tree that eventually becomes the GIMPLE_ASM. */
567
568 tree
569 resolve_asm_operand_names (tree string, tree outputs, tree inputs, tree labels)
570 {
571 char *buffer;
572 char *p;
573 const char *c;
574 tree t;
575
576 check_unique_operand_names (outputs, inputs, labels);
577
578 /* Substitute [<name>] in input constraint strings. There should be no
579 named operands in output constraints. */
580 for (t = inputs; t ; t = TREE_CHAIN (t))
581 {
582 c = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
583 if (strchr (c, '[') != NULL)
584 {
585 p = buffer = xstrdup (c);
586 while ((p = strchr (p, '[')) != NULL)
587 p = resolve_operand_name_1 (p, outputs, inputs, NULL);
588 TREE_VALUE (TREE_PURPOSE (t))
589 = build_string (strlen (buffer), buffer);
590 free (buffer);
591 }
592 }
593
594 /* Now check for any needed substitutions in the template. */
595 c = TREE_STRING_POINTER (string);
596 while ((c = strchr (c, '%')) != NULL)
597 {
598 if (c[1] == '[')
599 break;
600 else if (ISALPHA (c[1]) && c[2] == '[')
601 break;
602 else
603 {
604 c += 1 + (c[1] == '%');
605 continue;
606 }
607 }
608
609 if (c)
610 {
611 /* OK, we need to make a copy so we can perform the substitutions.
612 Assume that we will not need extra space--we get to remove '['
613 and ']', which means we cannot have a problem until we have more
614 than 999 operands. */
615 buffer = xstrdup (TREE_STRING_POINTER (string));
616 p = buffer + (c - TREE_STRING_POINTER (string));
617
618 while ((p = strchr (p, '%')) != NULL)
619 {
620 if (p[1] == '[')
621 p += 1;
622 else if (ISALPHA (p[1]) && p[2] == '[')
623 p += 2;
624 else
625 {
626 p += 1 + (p[1] == '%');
627 continue;
628 }
629
630 p = resolve_operand_name_1 (p, outputs, inputs, labels);
631 }
632
633 string = build_string (strlen (buffer), buffer);
634 free (buffer);
635 }
636
637 return string;
638 }
639
640 /* A subroutine of resolve_operand_names. P points to the '[' for a
641 potential named operand of the form [<name>]. In place, replace
642 the name and brackets with a number. Return a pointer to the
643 balance of the string after substitution. */
644
645 static char *
646 resolve_operand_name_1 (char *p, tree outputs, tree inputs, tree labels)
647 {
648 char *q;
649 int op;
650 tree t;
651
652 /* Collect the operand name. */
653 q = strchr (++p, ']');
654 if (!q)
655 {
656 error ("missing close brace for named operand");
657 return strchr (p, '\0');
658 }
659 *q = '\0';
660
661 /* Resolve the name to a number. */
662 for (op = 0, t = outputs; t ; t = TREE_CHAIN (t), op++)
663 {
664 tree name = TREE_PURPOSE (TREE_PURPOSE (t));
665 if (name && strcmp (TREE_STRING_POINTER (name), p) == 0)
666 goto found;
667 }
668 for (t = inputs; t ; t = TREE_CHAIN (t), op++)
669 {
670 tree name = TREE_PURPOSE (TREE_PURPOSE (t));
671 if (name && strcmp (TREE_STRING_POINTER (name), p) == 0)
672 goto found;
673 }
674 for (t = labels; t ; t = TREE_CHAIN (t), op++)
675 {
676 tree name = TREE_PURPOSE (t);
677 if (name && strcmp (TREE_STRING_POINTER (name), p) == 0)
678 goto found;
679 }
680
681 error ("undefined named operand %qs", identifier_to_locale (p));
682 op = 0;
683
684 found:
685 /* Replace the name with the number. Unfortunately, not all libraries
686 get the return value of sprintf correct, so search for the end of the
687 generated string by hand. */
688 sprintf (--p, "%d", op);
689 p = strchr (p, '\0');
690
691 /* Verify the no extra buffer space assumption. */
692 gcc_assert (p <= q);
693
694 /* Shift the rest of the buffer down to fill the gap. */
695 memmove (p, q + 1, strlen (q + 1) + 1);
696
697 return p;
698 }
699 \f
700
701 /* Generate RTL to return directly from the current function.
702 (That is, we bypass any return value.) */
703
704 void
705 expand_naked_return (void)
706 {
707 rtx_code_label *end_label;
708
709 clear_pending_stack_adjust ();
710 do_pending_stack_adjust ();
711
712 end_label = naked_return_label;
713 if (end_label == 0)
714 end_label = naked_return_label = gen_label_rtx ();
715
716 emit_jump (end_label);
717 }
718
719 /* Generate code to jump to LABEL if OP0 and OP1 are equal in mode MODE. PROB
720 is the probability of jumping to LABEL. */
721 static void
722 do_jump_if_equal (machine_mode mode, rtx op0, rtx op1, rtx_code_label *label,
723 int unsignedp, int prob)
724 {
725 gcc_assert (prob <= REG_BR_PROB_BASE);
726 do_compare_rtx_and_jump (op0, op1, EQ, unsignedp, mode,
727 NULL_RTX, NULL, label, prob);
728 }
729 \f
730 /* Do the insertion of a case label into case_list. The labels are
731 fed to us in descending order from the sorted vector of case labels used
732 in the tree part of the middle end. So the list we construct is
733 sorted in ascending order.
734
735 LABEL is the case label to be inserted. LOW and HIGH are the bounds
736 against which the index is compared to jump to LABEL and PROB is the
737 estimated probability LABEL is reached from the switch statement. */
738
739 static struct case_node *
740 add_case_node (struct case_node *head, tree low, tree high,
741 tree label, int prob, pool_allocator<case_node> &case_node_pool)
742 {
743 struct case_node *r;
744
745 gcc_checking_assert (low);
746 gcc_checking_assert (high && (TREE_TYPE (low) == TREE_TYPE (high)));
747
748 /* Add this label to the chain. */
749 r = case_node_pool.allocate ();
750 r->low = low;
751 r->high = high;
752 r->code_label = label;
753 r->parent = r->left = NULL;
754 r->prob = prob;
755 r->subtree_prob = prob;
756 r->right = head;
757 return r;
758 }
759 \f
760 /* Dump ROOT, a list or tree of case nodes, to file. */
761
762 static void
763 dump_case_nodes (FILE *f, struct case_node *root,
764 int indent_step, int indent_level)
765 {
766 if (root == 0)
767 return;
768 indent_level++;
769
770 dump_case_nodes (f, root->left, indent_step, indent_level);
771
772 fputs (";; ", f);
773 fprintf (f, "%*s", indent_step * indent_level, "");
774 print_dec (root->low, f, TYPE_SIGN (TREE_TYPE (root->low)));
775 if (!tree_int_cst_equal (root->low, root->high))
776 {
777 fprintf (f, " ... ");
778 print_dec (root->high, f, TYPE_SIGN (TREE_TYPE (root->high)));
779 }
780 fputs ("\n", f);
781
782 dump_case_nodes (f, root->right, indent_step, indent_level);
783 }
784 \f
785 #ifndef HAVE_casesi
786 #define HAVE_casesi 0
787 #endif
788
789 /* Return the smallest number of different values for which it is best to use a
790 jump-table instead of a tree of conditional branches. */
791
792 static unsigned int
793 case_values_threshold (void)
794 {
795 unsigned int threshold = PARAM_VALUE (PARAM_CASE_VALUES_THRESHOLD);
796
797 if (threshold == 0)
798 threshold = targetm.case_values_threshold ();
799
800 return threshold;
801 }
802
803 /* Return true if a switch should be expanded as a decision tree.
804 RANGE is the difference between highest and lowest case.
805 UNIQ is number of unique case node targets, not counting the default case.
806 COUNT is the number of comparisons needed, not counting the default case. */
807
808 static bool
809 expand_switch_as_decision_tree_p (tree range,
810 unsigned int uniq ATTRIBUTE_UNUSED,
811 unsigned int count)
812 {
813 int max_ratio;
814
815 /* If neither casesi or tablejump is available, or flag_jump_tables
816 over-ruled us, we really have no choice. */
817 if (!HAVE_casesi && !HAVE_tablejump)
818 return true;
819 if (!flag_jump_tables)
820 return true;
821 #ifndef ASM_OUTPUT_ADDR_DIFF_ELT
822 if (flag_pic)
823 return true;
824 #endif
825
826 /* If the switch is relatively small such that the cost of one
827 indirect jump on the target are higher than the cost of a
828 decision tree, go with the decision tree.
829
830 If range of values is much bigger than number of values,
831 or if it is too large to represent in a HOST_WIDE_INT,
832 make a sequence of conditional branches instead of a dispatch.
833
834 The definition of "much bigger" depends on whether we are
835 optimizing for size or for speed. If the former, the maximum
836 ratio range/count = 3, because this was found to be the optimal
837 ratio for size on i686-pc-linux-gnu, see PR11823. The ratio
838 10 is much older, and was probably selected after an extensive
839 benchmarking investigation on numerous platforms. Or maybe it
840 just made sense to someone at some point in the history of GCC,
841 who knows... */
842 max_ratio = optimize_insn_for_size_p () ? 3 : 10;
843 if (count < case_values_threshold ()
844 || ! tree_fits_uhwi_p (range)
845 || compare_tree_int (range, max_ratio * count) > 0)
846 return true;
847
848 return false;
849 }
850
851 /* Generate a decision tree, switching on INDEX_EXPR and jumping to
852 one of the labels in CASE_LIST or to the DEFAULT_LABEL.
853 DEFAULT_PROB is the estimated probability that it jumps to
854 DEFAULT_LABEL.
855
856 We generate a binary decision tree to select the appropriate target
857 code. This is done as follows:
858
859 If the index is a short or char that we do not have
860 an insn to handle comparisons directly, convert it to
861 a full integer now, rather than letting each comparison
862 generate the conversion.
863
864 Load the index into a register.
865
866 The list of cases is rearranged into a binary tree,
867 nearly optimal assuming equal probability for each case.
868
869 The tree is transformed into RTL, eliminating redundant
870 test conditions at the same time.
871
872 If program flow could reach the end of the decision tree
873 an unconditional jump to the default code is emitted.
874
875 The above process is unaware of the CFG. The caller has to fix up
876 the CFG itself. This is done in cfgexpand.c. */
877
878 static void
879 emit_case_decision_tree (tree index_expr, tree index_type,
880 case_node_ptr case_list, rtx_code_label *default_label,
881 int default_prob)
882 {
883 rtx index = expand_normal (index_expr);
884
885 if (GET_MODE_CLASS (GET_MODE (index)) == MODE_INT
886 && ! have_insn_for (COMPARE, GET_MODE (index)))
887 {
888 int unsignedp = TYPE_UNSIGNED (index_type);
889 machine_mode wider_mode;
890 for (wider_mode = GET_MODE (index); wider_mode != VOIDmode;
891 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
892 if (have_insn_for (COMPARE, wider_mode))
893 {
894 index = convert_to_mode (wider_mode, index, unsignedp);
895 break;
896 }
897 }
898
899 do_pending_stack_adjust ();
900
901 if (MEM_P (index))
902 {
903 index = copy_to_reg (index);
904 if (TREE_CODE (index_expr) == SSA_NAME)
905 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (index_expr), index);
906 }
907
908 balance_case_nodes (&case_list, NULL);
909
910 if (dump_file && (dump_flags & TDF_DETAILS))
911 {
912 int indent_step = ceil_log2 (TYPE_PRECISION (index_type)) + 2;
913 fprintf (dump_file, ";; Expanding GIMPLE switch as decision tree:\n");
914 dump_case_nodes (dump_file, case_list, indent_step, 0);
915 }
916
917 emit_case_nodes (index, case_list, default_label, default_prob, index_type);
918 if (default_label)
919 emit_jump (default_label);
920 }
921
922 /* Return the sum of probabilities of outgoing edges of basic block BB. */
923
924 static int
925 get_outgoing_edge_probs (basic_block bb)
926 {
927 edge e;
928 edge_iterator ei;
929 int prob_sum = 0;
930 if (!bb)
931 return 0;
932 FOR_EACH_EDGE (e, ei, bb->succs)
933 prob_sum += e->probability;
934 return prob_sum;
935 }
936
937 /* Computes the conditional probability of jumping to a target if the branch
938 instruction is executed.
939 TARGET_PROB is the estimated probability of jumping to a target relative
940 to some basic block BB.
941 BASE_PROB is the probability of reaching the branch instruction relative
942 to the same basic block BB. */
943
944 static inline int
945 conditional_probability (int target_prob, int base_prob)
946 {
947 if (base_prob > 0)
948 {
949 gcc_assert (target_prob >= 0);
950 gcc_assert (target_prob <= base_prob);
951 return GCOV_COMPUTE_SCALE (target_prob, base_prob);
952 }
953 return -1;
954 }
955
956 /* Generate a dispatch tabler, switching on INDEX_EXPR and jumping to
957 one of the labels in CASE_LIST or to the DEFAULT_LABEL.
958 MINVAL, MAXVAL, and RANGE are the extrema and range of the case
959 labels in CASE_LIST. STMT_BB is the basic block containing the statement.
960
961 First, a jump insn is emitted. First we try "casesi". If that
962 fails, try "tablejump". A target *must* have one of them (or both).
963
964 Then, a table with the target labels is emitted.
965
966 The process is unaware of the CFG. The caller has to fix up
967 the CFG itself. This is done in cfgexpand.c. */
968
969 static void
970 emit_case_dispatch_table (tree index_expr, tree index_type,
971 struct case_node *case_list, rtx default_label,
972 tree minval, tree maxval, tree range,
973 basic_block stmt_bb)
974 {
975 int i, ncases;
976 struct case_node *n;
977 rtx *labelvec;
978 rtx_insn *fallback_label = label_rtx (case_list->code_label);
979 rtx_code_label *table_label = gen_label_rtx ();
980 bool has_gaps = false;
981 edge default_edge = stmt_bb ? EDGE_SUCC (stmt_bb, 0) : NULL;
982 int default_prob = default_edge ? default_edge->probability : 0;
983 int base = get_outgoing_edge_probs (stmt_bb);
984 bool try_with_tablejump = false;
985
986 int new_default_prob = conditional_probability (default_prob,
987 base);
988
989 if (! try_casesi (index_type, index_expr, minval, range,
990 table_label, default_label, fallback_label,
991 new_default_prob))
992 {
993 /* Index jumptables from zero for suitable values of minval to avoid
994 a subtraction. For the rationale see:
995 "http://gcc.gnu.org/ml/gcc-patches/2001-10/msg01234.html". */
996 if (optimize_insn_for_speed_p ()
997 && compare_tree_int (minval, 0) > 0
998 && compare_tree_int (minval, 3) < 0)
999 {
1000 minval = build_int_cst (index_type, 0);
1001 range = maxval;
1002 has_gaps = true;
1003 }
1004 try_with_tablejump = true;
1005 }
1006
1007 /* Get table of labels to jump to, in order of case index. */
1008
1009 ncases = tree_to_shwi (range) + 1;
1010 labelvec = XALLOCAVEC (rtx, ncases);
1011 memset (labelvec, 0, ncases * sizeof (rtx));
1012
1013 for (n = case_list; n; n = n->right)
1014 {
1015 /* Compute the low and high bounds relative to the minimum
1016 value since that should fit in a HOST_WIDE_INT while the
1017 actual values may not. */
1018 HOST_WIDE_INT i_low
1019 = tree_to_uhwi (fold_build2 (MINUS_EXPR, index_type,
1020 n->low, minval));
1021 HOST_WIDE_INT i_high
1022 = tree_to_uhwi (fold_build2 (MINUS_EXPR, index_type,
1023 n->high, minval));
1024 HOST_WIDE_INT i;
1025
1026 for (i = i_low; i <= i_high; i ++)
1027 labelvec[i]
1028 = gen_rtx_LABEL_REF (Pmode, label_rtx (n->code_label));
1029 }
1030
1031 /* Fill in the gaps with the default. We may have gaps at
1032 the beginning if we tried to avoid the minval subtraction,
1033 so substitute some label even if the default label was
1034 deemed unreachable. */
1035 if (!default_label)
1036 default_label = fallback_label;
1037 for (i = 0; i < ncases; i++)
1038 if (labelvec[i] == 0)
1039 {
1040 has_gaps = true;
1041 labelvec[i] = gen_rtx_LABEL_REF (Pmode, default_label);
1042 }
1043
1044 if (has_gaps)
1045 {
1046 /* There is at least one entry in the jump table that jumps
1047 to default label. The default label can either be reached
1048 through the indirect jump or the direct conditional jump
1049 before that. Split the probability of reaching the
1050 default label among these two jumps. */
1051 new_default_prob = conditional_probability (default_prob/2,
1052 base);
1053 default_prob /= 2;
1054 base -= default_prob;
1055 }
1056 else
1057 {
1058 base -= default_prob;
1059 default_prob = 0;
1060 }
1061
1062 if (default_edge)
1063 default_edge->probability = default_prob;
1064
1065 /* We have altered the probability of the default edge. So the probabilities
1066 of all other edges need to be adjusted so that it sums up to
1067 REG_BR_PROB_BASE. */
1068 if (base)
1069 {
1070 edge e;
1071 edge_iterator ei;
1072 FOR_EACH_EDGE (e, ei, stmt_bb->succs)
1073 e->probability = GCOV_COMPUTE_SCALE (e->probability, base);
1074 }
1075
1076 if (try_with_tablejump)
1077 {
1078 bool ok = try_tablejump (index_type, index_expr, minval, range,
1079 table_label, default_label, new_default_prob);
1080 gcc_assert (ok);
1081 }
1082 /* Output the table. */
1083 emit_label (table_label);
1084
1085 if (CASE_VECTOR_PC_RELATIVE || flag_pic)
1086 emit_jump_table_data (gen_rtx_ADDR_DIFF_VEC (CASE_VECTOR_MODE,
1087 gen_rtx_LABEL_REF (Pmode,
1088 table_label),
1089 gen_rtvec_v (ncases, labelvec),
1090 const0_rtx, const0_rtx));
1091 else
1092 emit_jump_table_data (gen_rtx_ADDR_VEC (CASE_VECTOR_MODE,
1093 gen_rtvec_v (ncases, labelvec)));
1094
1095 /* Record no drop-through after the table. */
1096 emit_barrier ();
1097 }
1098
1099 /* Reset the aux field of all outgoing edges of basic block BB. */
1100
1101 static inline void
1102 reset_out_edges_aux (basic_block bb)
1103 {
1104 edge e;
1105 edge_iterator ei;
1106 FOR_EACH_EDGE (e, ei, bb->succs)
1107 e->aux = (void *)0;
1108 }
1109
1110 /* Compute the number of case labels that correspond to each outgoing edge of
1111 STMT. Record this information in the aux field of the edge. */
1112
1113 static inline void
1114 compute_cases_per_edge (gswitch *stmt)
1115 {
1116 basic_block bb = gimple_bb (stmt);
1117 reset_out_edges_aux (bb);
1118 int ncases = gimple_switch_num_labels (stmt);
1119 for (int i = ncases - 1; i >= 1; --i)
1120 {
1121 tree elt = gimple_switch_label (stmt, i);
1122 tree lab = CASE_LABEL (elt);
1123 basic_block case_bb = label_to_block_fn (cfun, lab);
1124 edge case_edge = find_edge (bb, case_bb);
1125 case_edge->aux = (void *)((intptr_t)(case_edge->aux) + 1);
1126 }
1127 }
1128
1129 /* Terminate a case (Pascal/Ada) or switch (C) statement
1130 in which ORIG_INDEX is the expression to be tested.
1131 If ORIG_TYPE is not NULL, it is the original ORIG_INDEX
1132 type as given in the source before any compiler conversions.
1133 Generate the code to test it and jump to the right place. */
1134
1135 void
1136 expand_case (gswitch *stmt)
1137 {
1138 tree minval = NULL_TREE, maxval = NULL_TREE, range = NULL_TREE;
1139 rtx_code_label *default_label = NULL;
1140 unsigned int count, uniq;
1141 int i;
1142 int ncases = gimple_switch_num_labels (stmt);
1143 tree index_expr = gimple_switch_index (stmt);
1144 tree index_type = TREE_TYPE (index_expr);
1145 tree elt;
1146 basic_block bb = gimple_bb (stmt);
1147
1148 /* A list of case labels; it is first built as a list and it may then
1149 be rearranged into a nearly balanced binary tree. */
1150 struct case_node *case_list = 0;
1151
1152 /* A pool for case nodes. */
1153 pool_allocator<case_node> case_node_pool ("struct case_node pool", 100);
1154
1155 /* An ERROR_MARK occurs for various reasons including invalid data type.
1156 ??? Can this still happen, with GIMPLE and all? */
1157 if (index_type == error_mark_node)
1158 return;
1159
1160 /* cleanup_tree_cfg removes all SWITCH_EXPR with their index
1161 expressions being INTEGER_CST. */
1162 gcc_assert (TREE_CODE (index_expr) != INTEGER_CST);
1163
1164
1165 do_pending_stack_adjust ();
1166
1167 /* Find the default case target label. */
1168 default_label = jump_target_rtx
1169 (CASE_LABEL (gimple_switch_default_label (stmt)));
1170 edge default_edge = EDGE_SUCC (bb, 0);
1171 int default_prob = default_edge->probability;
1172
1173 /* Get upper and lower bounds of case values. */
1174 elt = gimple_switch_label (stmt, 1);
1175 minval = fold_convert (index_type, CASE_LOW (elt));
1176 elt = gimple_switch_label (stmt, ncases - 1);
1177 if (CASE_HIGH (elt))
1178 maxval = fold_convert (index_type, CASE_HIGH (elt));
1179 else
1180 maxval = fold_convert (index_type, CASE_LOW (elt));
1181
1182 /* Compute span of values. */
1183 range = fold_build2 (MINUS_EXPR, index_type, maxval, minval);
1184
1185 /* Listify the labels queue and gather some numbers to decide
1186 how to expand this switch(). */
1187 uniq = 0;
1188 count = 0;
1189 hash_set<tree> seen_labels;
1190 compute_cases_per_edge (stmt);
1191
1192 for (i = ncases - 1; i >= 1; --i)
1193 {
1194 elt = gimple_switch_label (stmt, i);
1195 tree low = CASE_LOW (elt);
1196 gcc_assert (low);
1197 tree high = CASE_HIGH (elt);
1198 gcc_assert (! high || tree_int_cst_lt (low, high));
1199 tree lab = CASE_LABEL (elt);
1200
1201 /* Count the elements.
1202 A range counts double, since it requires two compares. */
1203 count++;
1204 if (high)
1205 count++;
1206
1207 /* If we have not seen this label yet, then increase the
1208 number of unique case node targets seen. */
1209 if (!seen_labels.add (lab))
1210 uniq++;
1211
1212 /* The bounds on the case range, LOW and HIGH, have to be converted
1213 to case's index type TYPE. Note that the original type of the
1214 case index in the source code is usually "lost" during
1215 gimplification due to type promotion, but the case labels retain the
1216 original type. Make sure to drop overflow flags. */
1217 low = fold_convert (index_type, low);
1218 if (TREE_OVERFLOW (low))
1219 low = wide_int_to_tree (index_type, low);
1220
1221 /* The canonical from of a case label in GIMPLE is that a simple case
1222 has an empty CASE_HIGH. For the casesi and tablejump expanders,
1223 the back ends want simple cases to have high == low. */
1224 if (! high)
1225 high = low;
1226 high = fold_convert (index_type, high);
1227 if (TREE_OVERFLOW (high))
1228 high = wide_int_to_tree (index_type, high);
1229
1230 basic_block case_bb = label_to_block_fn (cfun, lab);
1231 edge case_edge = find_edge (bb, case_bb);
1232 case_list = add_case_node (
1233 case_list, low, high, lab,
1234 case_edge->probability / (intptr_t)(case_edge->aux),
1235 case_node_pool);
1236 }
1237 reset_out_edges_aux (bb);
1238
1239 /* cleanup_tree_cfg removes all SWITCH_EXPR with a single
1240 destination, such as one with a default case only.
1241 It also removes cases that are out of range for the switch
1242 type, so we should never get a zero here. */
1243 gcc_assert (count > 0);
1244
1245 rtx_insn *before_case = get_last_insn ();
1246
1247 /* Decide how to expand this switch.
1248 The two options at this point are a dispatch table (casesi or
1249 tablejump) or a decision tree. */
1250
1251 if (expand_switch_as_decision_tree_p (range, uniq, count))
1252 emit_case_decision_tree (index_expr, index_type,
1253 case_list, default_label,
1254 default_prob);
1255 else
1256 emit_case_dispatch_table (index_expr, index_type,
1257 case_list, default_label,
1258 minval, maxval, range, bb);
1259
1260 reorder_insns (NEXT_INSN (before_case), get_last_insn (), before_case);
1261
1262 free_temp_slots ();
1263 }
1264
1265 /* Expand the dispatch to a short decrement chain if there are few cases
1266 to dispatch to. Likewise if neither casesi nor tablejump is available,
1267 or if flag_jump_tables is set. Otherwise, expand as a casesi or a
1268 tablejump. The index mode is always the mode of integer_type_node.
1269 Trap if no case matches the index.
1270
1271 DISPATCH_INDEX is the index expression to switch on. It should be a
1272 memory or register operand.
1273
1274 DISPATCH_TABLE is a set of case labels. The set should be sorted in
1275 ascending order, be contiguous, starting with value 0, and contain only
1276 single-valued case labels. */
1277
1278 void
1279 expand_sjlj_dispatch_table (rtx dispatch_index,
1280 vec<tree> dispatch_table)
1281 {
1282 tree index_type = integer_type_node;
1283 machine_mode index_mode = TYPE_MODE (index_type);
1284
1285 int ncases = dispatch_table.length ();
1286
1287 do_pending_stack_adjust ();
1288 rtx_insn *before_case = get_last_insn ();
1289
1290 /* Expand as a decrement-chain if there are 5 or fewer dispatch
1291 labels. This covers more than 98% of the cases in libjava,
1292 and seems to be a reasonable compromise between the "old way"
1293 of expanding as a decision tree or dispatch table vs. the "new
1294 way" with decrement chain or dispatch table. */
1295 if (dispatch_table.length () <= 5
1296 || (!HAVE_casesi && !HAVE_tablejump)
1297 || !flag_jump_tables)
1298 {
1299 /* Expand the dispatch as a decrement chain:
1300
1301 "switch(index) {case 0: do_0; case 1: do_1; ...; case N: do_N;}"
1302
1303 ==>
1304
1305 if (index == 0) do_0; else index--;
1306 if (index == 0) do_1; else index--;
1307 ...
1308 if (index == 0) do_N; else index--;
1309
1310 This is more efficient than a dispatch table on most machines.
1311 The last "index--" is redundant but the code is trivially dead
1312 and will be cleaned up by later passes. */
1313 rtx index = copy_to_mode_reg (index_mode, dispatch_index);
1314 rtx zero = CONST0_RTX (index_mode);
1315 for (int i = 0; i < ncases; i++)
1316 {
1317 tree elt = dispatch_table[i];
1318 rtx_code_label *lab = jump_target_rtx (CASE_LABEL (elt));
1319 do_jump_if_equal (index_mode, index, zero, lab, 0, -1);
1320 force_expand_binop (index_mode, sub_optab,
1321 index, CONST1_RTX (index_mode),
1322 index, 0, OPTAB_DIRECT);
1323 }
1324 }
1325 else
1326 {
1327 /* Similar to expand_case, but much simpler. */
1328 struct case_node *case_list = 0;
1329 pool_allocator<case_node> case_node_pool ("struct sjlj_case pool",
1330 ncases);
1331 tree index_expr = make_tree (index_type, dispatch_index);
1332 tree minval = build_int_cst (index_type, 0);
1333 tree maxval = CASE_LOW (dispatch_table.last ());
1334 tree range = maxval;
1335 rtx_code_label *default_label = gen_label_rtx ();
1336
1337 for (int i = ncases - 1; i >= 0; --i)
1338 {
1339 tree elt = dispatch_table[i];
1340 tree low = CASE_LOW (elt);
1341 tree lab = CASE_LABEL (elt);
1342 case_list = add_case_node (case_list, low, low, lab, 0, case_node_pool);
1343 }
1344
1345 emit_case_dispatch_table (index_expr, index_type,
1346 case_list, default_label,
1347 minval, maxval, range,
1348 BLOCK_FOR_INSN (before_case));
1349 emit_label (default_label);
1350 }
1351
1352 /* Dispatching something not handled? Trap! */
1353 expand_builtin_trap ();
1354
1355 reorder_insns (NEXT_INSN (before_case), get_last_insn (), before_case);
1356
1357 free_temp_slots ();
1358 }
1359
1360 \f
1361 /* Take an ordered list of case nodes
1362 and transform them into a near optimal binary tree,
1363 on the assumption that any target code selection value is as
1364 likely as any other.
1365
1366 The transformation is performed by splitting the ordered
1367 list into two equal sections plus a pivot. The parts are
1368 then attached to the pivot as left and right branches. Each
1369 branch is then transformed recursively. */
1370
1371 static void
1372 balance_case_nodes (case_node_ptr *head, case_node_ptr parent)
1373 {
1374 case_node_ptr np;
1375
1376 np = *head;
1377 if (np)
1378 {
1379 int i = 0;
1380 int ranges = 0;
1381 case_node_ptr *npp;
1382 case_node_ptr left;
1383
1384 /* Count the number of entries on branch. Also count the ranges. */
1385
1386 while (np)
1387 {
1388 if (!tree_int_cst_equal (np->low, np->high))
1389 ranges++;
1390
1391 i++;
1392 np = np->right;
1393 }
1394
1395 if (i > 2)
1396 {
1397 /* Split this list if it is long enough for that to help. */
1398 npp = head;
1399 left = *npp;
1400
1401 /* If there are just three nodes, split at the middle one. */
1402 if (i == 3)
1403 npp = &(*npp)->right;
1404 else
1405 {
1406 /* Find the place in the list that bisects the list's total cost,
1407 where ranges count as 2.
1408 Here I gets half the total cost. */
1409 i = (i + ranges + 1) / 2;
1410 while (1)
1411 {
1412 /* Skip nodes while their cost does not reach that amount. */
1413 if (!tree_int_cst_equal ((*npp)->low, (*npp)->high))
1414 i--;
1415 i--;
1416 if (i <= 0)
1417 break;
1418 npp = &(*npp)->right;
1419 }
1420 }
1421 *head = np = *npp;
1422 *npp = 0;
1423 np->parent = parent;
1424 np->left = left;
1425
1426 /* Optimize each of the two split parts. */
1427 balance_case_nodes (&np->left, np);
1428 balance_case_nodes (&np->right, np);
1429 np->subtree_prob = np->prob;
1430 np->subtree_prob += np->left->subtree_prob;
1431 np->subtree_prob += np->right->subtree_prob;
1432 }
1433 else
1434 {
1435 /* Else leave this branch as one level,
1436 but fill in `parent' fields. */
1437 np = *head;
1438 np->parent = parent;
1439 np->subtree_prob = np->prob;
1440 for (; np->right; np = np->right)
1441 {
1442 np->right->parent = np;
1443 (*head)->subtree_prob += np->right->subtree_prob;
1444 }
1445 }
1446 }
1447 }
1448 \f
1449 /* Search the parent sections of the case node tree
1450 to see if a test for the lower bound of NODE would be redundant.
1451 INDEX_TYPE is the type of the index expression.
1452
1453 The instructions to generate the case decision tree are
1454 output in the same order as nodes are processed so it is
1455 known that if a parent node checks the range of the current
1456 node minus one that the current node is bounded at its lower
1457 span. Thus the test would be redundant. */
1458
1459 static int
1460 node_has_low_bound (case_node_ptr node, tree index_type)
1461 {
1462 tree low_minus_one;
1463 case_node_ptr pnode;
1464
1465 /* If the lower bound of this node is the lowest value in the index type,
1466 we need not test it. */
1467
1468 if (tree_int_cst_equal (node->low, TYPE_MIN_VALUE (index_type)))
1469 return 1;
1470
1471 /* If this node has a left branch, the value at the left must be less
1472 than that at this node, so it cannot be bounded at the bottom and
1473 we need not bother testing any further. */
1474
1475 if (node->left)
1476 return 0;
1477
1478 low_minus_one = fold_build2 (MINUS_EXPR, TREE_TYPE (node->low),
1479 node->low,
1480 build_int_cst (TREE_TYPE (node->low), 1));
1481
1482 /* If the subtraction above overflowed, we can't verify anything.
1483 Otherwise, look for a parent that tests our value - 1. */
1484
1485 if (! tree_int_cst_lt (low_minus_one, node->low))
1486 return 0;
1487
1488 for (pnode = node->parent; pnode; pnode = pnode->parent)
1489 if (tree_int_cst_equal (low_minus_one, pnode->high))
1490 return 1;
1491
1492 return 0;
1493 }
1494
1495 /* Search the parent sections of the case node tree
1496 to see if a test for the upper bound of NODE would be redundant.
1497 INDEX_TYPE is the type of the index expression.
1498
1499 The instructions to generate the case decision tree are
1500 output in the same order as nodes are processed so it is
1501 known that if a parent node checks the range of the current
1502 node plus one that the current node is bounded at its upper
1503 span. Thus the test would be redundant. */
1504
1505 static int
1506 node_has_high_bound (case_node_ptr node, tree index_type)
1507 {
1508 tree high_plus_one;
1509 case_node_ptr pnode;
1510
1511 /* If there is no upper bound, obviously no test is needed. */
1512
1513 if (TYPE_MAX_VALUE (index_type) == NULL)
1514 return 1;
1515
1516 /* If the upper bound of this node is the highest value in the type
1517 of the index expression, we need not test against it. */
1518
1519 if (tree_int_cst_equal (node->high, TYPE_MAX_VALUE (index_type)))
1520 return 1;
1521
1522 /* If this node has a right branch, the value at the right must be greater
1523 than that at this node, so it cannot be bounded at the top and
1524 we need not bother testing any further. */
1525
1526 if (node->right)
1527 return 0;
1528
1529 high_plus_one = fold_build2 (PLUS_EXPR, TREE_TYPE (node->high),
1530 node->high,
1531 build_int_cst (TREE_TYPE (node->high), 1));
1532
1533 /* If the addition above overflowed, we can't verify anything.
1534 Otherwise, look for a parent that tests our value + 1. */
1535
1536 if (! tree_int_cst_lt (node->high, high_plus_one))
1537 return 0;
1538
1539 for (pnode = node->parent; pnode; pnode = pnode->parent)
1540 if (tree_int_cst_equal (high_plus_one, pnode->low))
1541 return 1;
1542
1543 return 0;
1544 }
1545
1546 /* Search the parent sections of the
1547 case node tree to see if both tests for the upper and lower
1548 bounds of NODE would be redundant. */
1549
1550 static int
1551 node_is_bounded (case_node_ptr node, tree index_type)
1552 {
1553 return (node_has_low_bound (node, index_type)
1554 && node_has_high_bound (node, index_type));
1555 }
1556 \f
1557
1558 /* Emit step-by-step code to select a case for the value of INDEX.
1559 The thus generated decision tree follows the form of the
1560 case-node binary tree NODE, whose nodes represent test conditions.
1561 INDEX_TYPE is the type of the index of the switch.
1562
1563 Care is taken to prune redundant tests from the decision tree
1564 by detecting any boundary conditions already checked by
1565 emitted rtx. (See node_has_high_bound, node_has_low_bound
1566 and node_is_bounded, above.)
1567
1568 Where the test conditions can be shown to be redundant we emit
1569 an unconditional jump to the target code. As a further
1570 optimization, the subordinates of a tree node are examined to
1571 check for bounded nodes. In this case conditional and/or
1572 unconditional jumps as a result of the boundary check for the
1573 current node are arranged to target the subordinates associated
1574 code for out of bound conditions on the current node.
1575
1576 We can assume that when control reaches the code generated here,
1577 the index value has already been compared with the parents
1578 of this node, and determined to be on the same side of each parent
1579 as this node is. Thus, if this node tests for the value 51,
1580 and a parent tested for 52, we don't need to consider
1581 the possibility of a value greater than 51. If another parent
1582 tests for the value 50, then this node need not test anything. */
1583
1584 static void
1585 emit_case_nodes (rtx index, case_node_ptr node, rtx_code_label *default_label,
1586 int default_prob, tree index_type)
1587 {
1588 /* If INDEX has an unsigned type, we must make unsigned branches. */
1589 int unsignedp = TYPE_UNSIGNED (index_type);
1590 int probability;
1591 int prob = node->prob, subtree_prob = node->subtree_prob;
1592 machine_mode mode = GET_MODE (index);
1593 machine_mode imode = TYPE_MODE (index_type);
1594
1595 /* Handle indices detected as constant during RTL expansion. */
1596 if (mode == VOIDmode)
1597 mode = imode;
1598
1599 /* See if our parents have already tested everything for us.
1600 If they have, emit an unconditional jump for this node. */
1601 if (node_is_bounded (node, index_type))
1602 emit_jump (label_rtx (node->code_label));
1603
1604 else if (tree_int_cst_equal (node->low, node->high))
1605 {
1606 probability = conditional_probability (prob, subtree_prob + default_prob);
1607 /* Node is single valued. First see if the index expression matches
1608 this node and then check our children, if any. */
1609 do_jump_if_equal (mode, index,
1610 convert_modes (mode, imode,
1611 expand_normal (node->low),
1612 unsignedp),
1613 jump_target_rtx (node->code_label),
1614 unsignedp, probability);
1615 /* Since this case is taken at this point, reduce its weight from
1616 subtree_weight. */
1617 subtree_prob -= prob;
1618 if (node->right != 0 && node->left != 0)
1619 {
1620 /* This node has children on both sides.
1621 Dispatch to one side or the other
1622 by comparing the index value with this node's value.
1623 If one subtree is bounded, check that one first,
1624 so we can avoid real branches in the tree. */
1625
1626 if (node_is_bounded (node->right, index_type))
1627 {
1628 probability = conditional_probability (
1629 node->right->prob,
1630 subtree_prob + default_prob);
1631 emit_cmp_and_jump_insns (index,
1632 convert_modes
1633 (mode, imode,
1634 expand_normal (node->high),
1635 unsignedp),
1636 GT, NULL_RTX, mode, unsignedp,
1637 label_rtx (node->right->code_label),
1638 probability);
1639 emit_case_nodes (index, node->left, default_label, default_prob,
1640 index_type);
1641 }
1642
1643 else if (node_is_bounded (node->left, index_type))
1644 {
1645 probability = conditional_probability (
1646 node->left->prob,
1647 subtree_prob + default_prob);
1648 emit_cmp_and_jump_insns (index,
1649 convert_modes
1650 (mode, imode,
1651 expand_normal (node->high),
1652 unsignedp),
1653 LT, NULL_RTX, mode, unsignedp,
1654 label_rtx (node->left->code_label),
1655 probability);
1656 emit_case_nodes (index, node->right, default_label, default_prob,
1657 index_type);
1658 }
1659
1660 /* If both children are single-valued cases with no
1661 children, finish up all the work. This way, we can save
1662 one ordered comparison. */
1663 else if (tree_int_cst_equal (node->right->low, node->right->high)
1664 && node->right->left == 0
1665 && node->right->right == 0
1666 && tree_int_cst_equal (node->left->low, node->left->high)
1667 && node->left->left == 0
1668 && node->left->right == 0)
1669 {
1670 /* Neither node is bounded. First distinguish the two sides;
1671 then emit the code for one side at a time. */
1672
1673 /* See if the value matches what the right hand side
1674 wants. */
1675 probability = conditional_probability (
1676 node->right->prob,
1677 subtree_prob + default_prob);
1678 do_jump_if_equal (mode, index,
1679 convert_modes (mode, imode,
1680 expand_normal (node->right->low),
1681 unsignedp),
1682 jump_target_rtx (node->right->code_label),
1683 unsignedp, probability);
1684
1685 /* See if the value matches what the left hand side
1686 wants. */
1687 probability = conditional_probability (
1688 node->left->prob,
1689 subtree_prob + default_prob);
1690 do_jump_if_equal (mode, index,
1691 convert_modes (mode, imode,
1692 expand_normal (node->left->low),
1693 unsignedp),
1694 jump_target_rtx (node->left->code_label),
1695 unsignedp, probability);
1696 }
1697
1698 else
1699 {
1700 /* Neither node is bounded. First distinguish the two sides;
1701 then emit the code for one side at a time. */
1702
1703 tree test_label
1704 = build_decl (curr_insn_location (),
1705 LABEL_DECL, NULL_TREE, void_type_node);
1706
1707 /* The default label could be reached either through the right
1708 subtree or the left subtree. Divide the probability
1709 equally. */
1710 probability = conditional_probability (
1711 node->right->subtree_prob + default_prob/2,
1712 subtree_prob + default_prob);
1713 /* See if the value is on the right. */
1714 emit_cmp_and_jump_insns (index,
1715 convert_modes
1716 (mode, imode,
1717 expand_normal (node->high),
1718 unsignedp),
1719 GT, NULL_RTX, mode, unsignedp,
1720 label_rtx (test_label),
1721 probability);
1722 default_prob /= 2;
1723
1724 /* Value must be on the left.
1725 Handle the left-hand subtree. */
1726 emit_case_nodes (index, node->left, default_label, default_prob, index_type);
1727 /* If left-hand subtree does nothing,
1728 go to default. */
1729 if (default_label)
1730 emit_jump (default_label);
1731
1732 /* Code branches here for the right-hand subtree. */
1733 expand_label (test_label);
1734 emit_case_nodes (index, node->right, default_label, default_prob, index_type);
1735 }
1736 }
1737
1738 else if (node->right != 0 && node->left == 0)
1739 {
1740 /* Here we have a right child but no left so we issue a conditional
1741 branch to default and process the right child.
1742
1743 Omit the conditional branch to default if the right child
1744 does not have any children and is single valued; it would
1745 cost too much space to save so little time. */
1746
1747 if (node->right->right || node->right->left
1748 || !tree_int_cst_equal (node->right->low, node->right->high))
1749 {
1750 if (!node_has_low_bound (node, index_type))
1751 {
1752 probability = conditional_probability (
1753 default_prob/2,
1754 subtree_prob + default_prob);
1755 emit_cmp_and_jump_insns (index,
1756 convert_modes
1757 (mode, imode,
1758 expand_normal (node->high),
1759 unsignedp),
1760 LT, NULL_RTX, mode, unsignedp,
1761 default_label,
1762 probability);
1763 default_prob /= 2;
1764 }
1765
1766 emit_case_nodes (index, node->right, default_label, default_prob, index_type);
1767 }
1768 else
1769 {
1770 probability = conditional_probability (
1771 node->right->subtree_prob,
1772 subtree_prob + default_prob);
1773 /* We cannot process node->right normally
1774 since we haven't ruled out the numbers less than
1775 this node's value. So handle node->right explicitly. */
1776 do_jump_if_equal (mode, index,
1777 convert_modes
1778 (mode, imode,
1779 expand_normal (node->right->low),
1780 unsignedp),
1781 jump_target_rtx (node->right->code_label),
1782 unsignedp, probability);
1783 }
1784 }
1785
1786 else if (node->right == 0 && node->left != 0)
1787 {
1788 /* Just one subtree, on the left. */
1789 if (node->left->left || node->left->right
1790 || !tree_int_cst_equal (node->left->low, node->left->high))
1791 {
1792 if (!node_has_high_bound (node, index_type))
1793 {
1794 probability = conditional_probability (
1795 default_prob/2,
1796 subtree_prob + default_prob);
1797 emit_cmp_and_jump_insns (index,
1798 convert_modes
1799 (mode, imode,
1800 expand_normal (node->high),
1801 unsignedp),
1802 GT, NULL_RTX, mode, unsignedp,
1803 default_label,
1804 probability);
1805 default_prob /= 2;
1806 }
1807
1808 emit_case_nodes (index, node->left, default_label,
1809 default_prob, index_type);
1810 }
1811 else
1812 {
1813 probability = conditional_probability (
1814 node->left->subtree_prob,
1815 subtree_prob + default_prob);
1816 /* We cannot process node->left normally
1817 since we haven't ruled out the numbers less than
1818 this node's value. So handle node->left explicitly. */
1819 do_jump_if_equal (mode, index,
1820 convert_modes
1821 (mode, imode,
1822 expand_normal (node->left->low),
1823 unsignedp),
1824 jump_target_rtx (node->left->code_label),
1825 unsignedp, probability);
1826 }
1827 }
1828 }
1829 else
1830 {
1831 /* Node is a range. These cases are very similar to those for a single
1832 value, except that we do not start by testing whether this node
1833 is the one to branch to. */
1834
1835 if (node->right != 0 && node->left != 0)
1836 {
1837 /* Node has subtrees on both sides.
1838 If the right-hand subtree is bounded,
1839 test for it first, since we can go straight there.
1840 Otherwise, we need to make a branch in the control structure,
1841 then handle the two subtrees. */
1842 tree test_label = 0;
1843
1844 if (node_is_bounded (node->right, index_type))
1845 {
1846 /* Right hand node is fully bounded so we can eliminate any
1847 testing and branch directly to the target code. */
1848 probability = conditional_probability (
1849 node->right->subtree_prob,
1850 subtree_prob + default_prob);
1851 emit_cmp_and_jump_insns (index,
1852 convert_modes
1853 (mode, imode,
1854 expand_normal (node->high),
1855 unsignedp),
1856 GT, NULL_RTX, mode, unsignedp,
1857 label_rtx (node->right->code_label),
1858 probability);
1859 }
1860 else
1861 {
1862 /* Right hand node requires testing.
1863 Branch to a label where we will handle it later. */
1864
1865 test_label = build_decl (curr_insn_location (),
1866 LABEL_DECL, NULL_TREE, void_type_node);
1867 probability = conditional_probability (
1868 node->right->subtree_prob + default_prob/2,
1869 subtree_prob + default_prob);
1870 emit_cmp_and_jump_insns (index,
1871 convert_modes
1872 (mode, imode,
1873 expand_normal (node->high),
1874 unsignedp),
1875 GT, NULL_RTX, mode, unsignedp,
1876 label_rtx (test_label),
1877 probability);
1878 default_prob /= 2;
1879 }
1880
1881 /* Value belongs to this node or to the left-hand subtree. */
1882
1883 probability = conditional_probability (
1884 prob,
1885 subtree_prob + default_prob);
1886 emit_cmp_and_jump_insns (index,
1887 convert_modes
1888 (mode, imode,
1889 expand_normal (node->low),
1890 unsignedp),
1891 GE, NULL_RTX, mode, unsignedp,
1892 label_rtx (node->code_label),
1893 probability);
1894
1895 /* Handle the left-hand subtree. */
1896 emit_case_nodes (index, node->left, default_label, default_prob, index_type);
1897
1898 /* If right node had to be handled later, do that now. */
1899
1900 if (test_label)
1901 {
1902 /* If the left-hand subtree fell through,
1903 don't let it fall into the right-hand subtree. */
1904 if (default_label)
1905 emit_jump (default_label);
1906
1907 expand_label (test_label);
1908 emit_case_nodes (index, node->right, default_label, default_prob, index_type);
1909 }
1910 }
1911
1912 else if (node->right != 0 && node->left == 0)
1913 {
1914 /* Deal with values to the left of this node,
1915 if they are possible. */
1916 if (!node_has_low_bound (node, index_type))
1917 {
1918 probability = conditional_probability (
1919 default_prob/2,
1920 subtree_prob + default_prob);
1921 emit_cmp_and_jump_insns (index,
1922 convert_modes
1923 (mode, imode,
1924 expand_normal (node->low),
1925 unsignedp),
1926 LT, NULL_RTX, mode, unsignedp,
1927 default_label,
1928 probability);
1929 default_prob /= 2;
1930 }
1931
1932 /* Value belongs to this node or to the right-hand subtree. */
1933
1934 probability = conditional_probability (
1935 prob,
1936 subtree_prob + default_prob);
1937 emit_cmp_and_jump_insns (index,
1938 convert_modes
1939 (mode, imode,
1940 expand_normal (node->high),
1941 unsignedp),
1942 LE, NULL_RTX, mode, unsignedp,
1943 label_rtx (node->code_label),
1944 probability);
1945
1946 emit_case_nodes (index, node->right, default_label, default_prob, index_type);
1947 }
1948
1949 else if (node->right == 0 && node->left != 0)
1950 {
1951 /* Deal with values to the right of this node,
1952 if they are possible. */
1953 if (!node_has_high_bound (node, index_type))
1954 {
1955 probability = conditional_probability (
1956 default_prob/2,
1957 subtree_prob + default_prob);
1958 emit_cmp_and_jump_insns (index,
1959 convert_modes
1960 (mode, imode,
1961 expand_normal (node->high),
1962 unsignedp),
1963 GT, NULL_RTX, mode, unsignedp,
1964 default_label,
1965 probability);
1966 default_prob /= 2;
1967 }
1968
1969 /* Value belongs to this node or to the left-hand subtree. */
1970
1971 probability = conditional_probability (
1972 prob,
1973 subtree_prob + default_prob);
1974 emit_cmp_and_jump_insns (index,
1975 convert_modes
1976 (mode, imode,
1977 expand_normal (node->low),
1978 unsignedp),
1979 GE, NULL_RTX, mode, unsignedp,
1980 label_rtx (node->code_label),
1981 probability);
1982
1983 emit_case_nodes (index, node->left, default_label, default_prob, index_type);
1984 }
1985
1986 else
1987 {
1988 /* Node has no children so we check low and high bounds to remove
1989 redundant tests. Only one of the bounds can exist,
1990 since otherwise this node is bounded--a case tested already. */
1991 int high_bound = node_has_high_bound (node, index_type);
1992 int low_bound = node_has_low_bound (node, index_type);
1993
1994 if (!high_bound && low_bound)
1995 {
1996 probability = conditional_probability (
1997 default_prob,
1998 subtree_prob + default_prob);
1999 emit_cmp_and_jump_insns (index,
2000 convert_modes
2001 (mode, imode,
2002 expand_normal (node->high),
2003 unsignedp),
2004 GT, NULL_RTX, mode, unsignedp,
2005 default_label,
2006 probability);
2007 }
2008
2009 else if (!low_bound && high_bound)
2010 {
2011 probability = conditional_probability (
2012 default_prob,
2013 subtree_prob + default_prob);
2014 emit_cmp_and_jump_insns (index,
2015 convert_modes
2016 (mode, imode,
2017 expand_normal (node->low),
2018 unsignedp),
2019 LT, NULL_RTX, mode, unsignedp,
2020 default_label,
2021 probability);
2022 }
2023 else if (!low_bound && !high_bound)
2024 {
2025 /* Widen LOW and HIGH to the same width as INDEX. */
2026 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
2027 tree low = build1 (CONVERT_EXPR, type, node->low);
2028 tree high = build1 (CONVERT_EXPR, type, node->high);
2029 rtx low_rtx, new_index, new_bound;
2030
2031 /* Instead of doing two branches, emit one unsigned branch for
2032 (index-low) > (high-low). */
2033 low_rtx = expand_expr (low, NULL_RTX, mode, EXPAND_NORMAL);
2034 new_index = expand_simple_binop (mode, MINUS, index, low_rtx,
2035 NULL_RTX, unsignedp,
2036 OPTAB_WIDEN);
2037 new_bound = expand_expr (fold_build2 (MINUS_EXPR, type,
2038 high, low),
2039 NULL_RTX, mode, EXPAND_NORMAL);
2040
2041 probability = conditional_probability (
2042 default_prob,
2043 subtree_prob + default_prob);
2044 emit_cmp_and_jump_insns (new_index, new_bound, GT, NULL_RTX,
2045 mode, 1, default_label, probability);
2046 }
2047
2048 emit_jump (jump_target_rtx (node->code_label));
2049 }
2050 }
2051 }