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