* tree-loop-distribution.c (params.h): Include header file.
[official-gcc.git] / gcc / stmt.c
blob10d394eee695d9bc7d204cc73d0c7ca281f67310
1 /* Expands front end tree to back end RTL for GCC
2 Copyright (C) 1987-2017 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 "cfghooks.h"
34 #include "predict.h"
35 #include "alloc-pool.h"
36 #include "memmodel.h"
37 #include "tm_p.h"
38 #include "optabs.h"
39 #include "regs.h"
40 #include "emit-rtl.h"
41 #include "pretty-print.h"
42 #include "diagnostic-core.h"
44 #include "fold-const.h"
45 #include "varasm.h"
46 #include "stor-layout.h"
47 #include "dojump.h"
48 #include "explow.h"
49 #include "stmt.h"
50 #include "expr.h"
51 #include "langhooks.h"
52 #include "cfganal.h"
53 #include "tree-cfg.h"
54 #include "params.h"
55 #include "dumpfile.h"
56 #include "builtins.h"
59 /* Functions and data structures for expanding case statements. */
61 /* Case label structure, used to hold info on labels within case
62 statements. We handle "range" labels; for a single-value label
63 as in C, the high and low limits are the same.
65 We start with a vector of case nodes sorted in ascending order, and
66 the default label as the last element in the vector. Before expanding
67 to RTL, we transform this vector into a list linked via the RIGHT
68 fields in the case_node struct. Nodes with higher case values are
69 later in the list.
71 Switch statements can be output in three forms. A branch table is
72 used if there are more than a few labels and the labels are dense
73 within the range between the smallest and largest case value. If a
74 branch table is used, no further manipulations are done with the case
75 node chain.
77 The alternative to the use of a branch table is to generate a series
78 of compare and jump insns. When that is done, we use the LEFT, RIGHT,
79 and PARENT fields to hold a binary tree. Initially the tree is
80 totally unbalanced, with everything on the right. We balance the tree
81 with nodes on the left having lower case values than the parent
82 and nodes on the right having higher values. We then output the tree
83 in order.
85 For very small, suitable switch statements, we can generate a series
86 of simple bit test and branches instead. */
88 struct case_node
90 struct case_node *left; /* Left son in binary tree */
91 struct case_node *right; /* Right son in binary tree; also node chain */
92 struct case_node *parent; /* Parent of node in binary tree */
93 tree low; /* Lowest index value for this label */
94 tree high; /* Highest index value for this label */
95 tree code_label; /* Label to jump to when node matches */
96 profile_probability prob; /* Probability of taking this case. */
97 /* Probability of reaching subtree rooted at this node */
98 profile_probability subtree_prob;
101 typedef struct case_node *case_node_ptr;
103 extern basic_block label_to_block_fn (struct function *, tree);
105 static bool check_unique_operand_names (tree, tree, tree);
106 static char *resolve_operand_name_1 (char *, tree, tree, tree);
107 static void balance_case_nodes (case_node_ptr *, case_node_ptr);
108 static int node_has_low_bound (case_node_ptr, tree);
109 static int node_has_high_bound (case_node_ptr, tree);
110 static int node_is_bounded (case_node_ptr, tree);
111 static void emit_case_nodes (rtx, case_node_ptr, rtx_code_label *,
112 profile_probability, tree);
114 /* Return the rtx-label that corresponds to a LABEL_DECL,
115 creating it if necessary. */
117 rtx_insn *
118 label_rtx (tree label)
120 gcc_assert (TREE_CODE (label) == LABEL_DECL);
122 if (!DECL_RTL_SET_P (label))
124 rtx_code_label *r = gen_label_rtx ();
125 SET_DECL_RTL (label, r);
126 if (FORCED_LABEL (label) || DECL_NONLOCAL (label))
127 LABEL_PRESERVE_P (r) = 1;
130 return as_a <rtx_insn *> (DECL_RTL (label));
133 /* As above, but also put it on the forced-reference list of the
134 function that contains it. */
135 rtx_insn *
136 force_label_rtx (tree label)
138 rtx_insn *ref = label_rtx (label);
139 tree function = decl_function_context (label);
141 gcc_assert (function);
143 vec_safe_push (forced_labels, ref);
144 return ref;
147 /* As label_rtx, but ensures (in check build), that returned value is
148 an existing label (i.e. rtx with code CODE_LABEL). */
149 rtx_code_label *
150 jump_target_rtx (tree label)
152 return as_a <rtx_code_label *> (label_rtx (label));
155 /* Add an unconditional jump to LABEL as the next sequential instruction. */
157 void
158 emit_jump (rtx label)
160 do_pending_stack_adjust ();
161 emit_jump_insn (targetm.gen_jump (label));
162 emit_barrier ();
165 /* Handle goto statements and the labels that they can go to. */
167 /* Specify the location in the RTL code of a label LABEL,
168 which is a LABEL_DECL tree node.
170 This is used for the kind of label that the user can jump to with a
171 goto statement, and for alternatives of a switch or case statement.
172 RTL labels generated for loops and conditionals don't go through here;
173 they are generated directly at the RTL level, by other functions below.
175 Note that this has nothing to do with defining label *names*.
176 Languages vary in how they do that and what that even means. */
178 void
179 expand_label (tree label)
181 rtx_code_label *label_r = jump_target_rtx (label);
183 do_pending_stack_adjust ();
184 emit_label (label_r);
185 if (DECL_NAME (label))
186 LABEL_NAME (DECL_RTL (label)) = IDENTIFIER_POINTER (DECL_NAME (label));
188 if (DECL_NONLOCAL (label))
190 expand_builtin_setjmp_receiver (NULL);
191 nonlocal_goto_handler_labels
192 = gen_rtx_INSN_LIST (VOIDmode, label_r,
193 nonlocal_goto_handler_labels);
196 if (FORCED_LABEL (label))
197 vec_safe_push<rtx_insn *> (forced_labels, label_r);
199 if (DECL_NONLOCAL (label) || FORCED_LABEL (label))
200 maybe_set_first_label_num (label_r);
203 /* Parse the output constraint pointed to by *CONSTRAINT_P. It is the
204 OPERAND_NUMth output operand, indexed from zero. There are NINPUTS
205 inputs and NOUTPUTS outputs to this extended-asm. Upon return,
206 *ALLOWS_MEM will be TRUE iff the constraint allows the use of a
207 memory operand. Similarly, *ALLOWS_REG will be TRUE iff the
208 constraint allows the use of a register operand. And, *IS_INOUT
209 will be true if the operand is read-write, i.e., if it is used as
210 an input as well as an output. If *CONSTRAINT_P is not in
211 canonical form, it will be made canonical. (Note that `+' will be
212 replaced with `=' as part of this process.)
214 Returns TRUE if all went well; FALSE if an error occurred. */
216 bool
217 parse_output_constraint (const char **constraint_p, int operand_num,
218 int ninputs, int noutputs, bool *allows_mem,
219 bool *allows_reg, bool *is_inout)
221 const char *constraint = *constraint_p;
222 const char *p;
224 /* Assume the constraint doesn't allow the use of either a register
225 or memory. */
226 *allows_mem = false;
227 *allows_reg = false;
229 /* Allow the `=' or `+' to not be at the beginning of the string,
230 since it wasn't explicitly documented that way, and there is a
231 large body of code that puts it last. Swap the character to
232 the front, so as not to uglify any place else. */
233 p = strchr (constraint, '=');
234 if (!p)
235 p = strchr (constraint, '+');
237 /* If the string doesn't contain an `=', issue an error
238 message. */
239 if (!p)
241 error ("output operand constraint lacks %<=%>");
242 return false;
245 /* If the constraint begins with `+', then the operand is both read
246 from and written to. */
247 *is_inout = (*p == '+');
249 /* Canonicalize the output constraint so that it begins with `='. */
250 if (p != constraint || *is_inout)
252 char *buf;
253 size_t c_len = strlen (constraint);
255 if (p != constraint)
256 warning (0, "output constraint %qc for operand %d "
257 "is not at the beginning",
258 *p, operand_num);
260 /* Make a copy of the constraint. */
261 buf = XALLOCAVEC (char, c_len + 1);
262 strcpy (buf, constraint);
263 /* Swap the first character and the `=' or `+'. */
264 buf[p - constraint] = buf[0];
265 /* Make sure the first character is an `='. (Until we do this,
266 it might be a `+'.) */
267 buf[0] = '=';
268 /* Replace the constraint with the canonicalized string. */
269 *constraint_p = ggc_alloc_string (buf, c_len);
270 constraint = *constraint_p;
273 /* Loop through the constraint string. */
274 for (p = constraint + 1; *p; p += CONSTRAINT_LEN (*p, p))
275 switch (*p)
277 case '+':
278 case '=':
279 error ("operand constraint contains incorrectly positioned "
280 "%<+%> or %<=%>");
281 return false;
283 case '%':
284 if (operand_num + 1 == ninputs + noutputs)
286 error ("%<%%%> constraint used with last operand");
287 return false;
289 break;
291 case '?': case '!': case '*': case '&': case '#':
292 case '$': case '^':
293 case 'E': case 'F': case 'G': case 'H':
294 case 's': case 'i': case 'n':
295 case 'I': case 'J': case 'K': case 'L': case 'M':
296 case 'N': case 'O': case 'P': case ',':
297 break;
299 case '0': case '1': case '2': case '3': case '4':
300 case '5': case '6': case '7': case '8': case '9':
301 case '[':
302 error ("matching constraint not valid in output operand");
303 return false;
305 case '<': case '>':
306 /* ??? Before flow, auto inc/dec insns are not supposed to exist,
307 excepting those that expand_call created. So match memory
308 and hope. */
309 *allows_mem = true;
310 break;
312 case 'g': case 'X':
313 *allows_reg = true;
314 *allows_mem = true;
315 break;
317 default:
318 if (!ISALPHA (*p))
319 break;
320 enum constraint_num cn = lookup_constraint (p);
321 if (reg_class_for_constraint (cn) != NO_REGS
322 || insn_extra_address_constraint (cn))
323 *allows_reg = true;
324 else if (insn_extra_memory_constraint (cn))
325 *allows_mem = true;
326 else
327 insn_extra_constraint_allows_reg_mem (cn, allows_reg, allows_mem);
328 break;
331 return true;
334 /* Similar, but for input constraints. */
336 bool
337 parse_input_constraint (const char **constraint_p, int input_num,
338 int ninputs, int noutputs, int ninout,
339 const char * const * constraints,
340 bool *allows_mem, bool *allows_reg)
342 const char *constraint = *constraint_p;
343 const char *orig_constraint = constraint;
344 size_t c_len = strlen (constraint);
345 size_t j;
346 bool saw_match = false;
348 /* Assume the constraint doesn't allow the use of either
349 a register or memory. */
350 *allows_mem = false;
351 *allows_reg = false;
353 /* Make sure constraint has neither `=', `+', nor '&'. */
355 for (j = 0; j < c_len; j += CONSTRAINT_LEN (constraint[j], constraint+j))
356 switch (constraint[j])
358 case '+': case '=': case '&':
359 if (constraint == orig_constraint)
361 error ("input operand constraint contains %qc", constraint[j]);
362 return false;
364 break;
366 case '%':
367 if (constraint == orig_constraint
368 && input_num + 1 == ninputs - ninout)
370 error ("%<%%%> constraint used with last operand");
371 return false;
373 break;
375 case '<': case '>':
376 case '?': case '!': case '*': case '#':
377 case '$': case '^':
378 case 'E': case 'F': case 'G': case 'H':
379 case 's': case 'i': case 'n':
380 case 'I': case 'J': case 'K': case 'L': case 'M':
381 case 'N': case 'O': case 'P': case ',':
382 break;
384 /* Whether or not a numeric constraint allows a register is
385 decided by the matching constraint, and so there is no need
386 to do anything special with them. We must handle them in
387 the default case, so that we don't unnecessarily force
388 operands to memory. */
389 case '0': case '1': case '2': case '3': case '4':
390 case '5': case '6': case '7': case '8': case '9':
392 char *end;
393 unsigned long match;
395 saw_match = true;
397 match = strtoul (constraint + j, &end, 10);
398 if (match >= (unsigned long) noutputs)
400 error ("matching constraint references invalid operand number");
401 return false;
404 /* Try and find the real constraint for this dup. Only do this
405 if the matching constraint is the only alternative. */
406 if (*end == '\0'
407 && (j == 0 || (j == 1 && constraint[0] == '%')))
409 constraint = constraints[match];
410 *constraint_p = constraint;
411 c_len = strlen (constraint);
412 j = 0;
413 /* ??? At the end of the loop, we will skip the first part of
414 the matched constraint. This assumes not only that the
415 other constraint is an output constraint, but also that
416 the '=' or '+' come first. */
417 break;
419 else
420 j = end - constraint;
421 /* Anticipate increment at end of loop. */
422 j--;
424 /* Fall through. */
426 case 'g': case 'X':
427 *allows_reg = true;
428 *allows_mem = true;
429 break;
431 default:
432 if (! ISALPHA (constraint[j]))
434 error ("invalid punctuation %qc in constraint", constraint[j]);
435 return false;
437 enum constraint_num cn = lookup_constraint (constraint + j);
438 if (reg_class_for_constraint (cn) != NO_REGS
439 || insn_extra_address_constraint (cn))
440 *allows_reg = true;
441 else if (insn_extra_memory_constraint (cn)
442 || insn_extra_special_memory_constraint (cn))
443 *allows_mem = true;
444 else
445 insn_extra_constraint_allows_reg_mem (cn, allows_reg, allows_mem);
446 break;
449 if (saw_match && !*allows_reg)
450 warning (0, "matching constraint does not allow a register");
452 return true;
455 /* Return DECL iff there's an overlap between *REGS and DECL, where DECL
456 can be an asm-declared register. Called via walk_tree. */
458 static tree
459 decl_overlaps_hard_reg_set_p (tree *declp, int *walk_subtrees ATTRIBUTE_UNUSED,
460 void *data)
462 tree decl = *declp;
463 const HARD_REG_SET *const regs = (const HARD_REG_SET *) data;
465 if (VAR_P (decl))
467 if (DECL_HARD_REGISTER (decl)
468 && REG_P (DECL_RTL (decl))
469 && REGNO (DECL_RTL (decl)) < FIRST_PSEUDO_REGISTER)
471 rtx reg = DECL_RTL (decl);
473 if (overlaps_hard_reg_set_p (*regs, GET_MODE (reg), REGNO (reg)))
474 return decl;
476 walk_subtrees = 0;
478 else if (TYPE_P (decl) || TREE_CODE (decl) == PARM_DECL)
479 walk_subtrees = 0;
480 return NULL_TREE;
483 /* If there is an overlap between *REGS and DECL, return the first overlap
484 found. */
485 tree
486 tree_overlaps_hard_reg_set (tree decl, HARD_REG_SET *regs)
488 return walk_tree (&decl, decl_overlaps_hard_reg_set_p, regs, NULL);
492 /* A subroutine of expand_asm_operands. Check that all operand names
493 are unique. Return true if so. We rely on the fact that these names
494 are identifiers, and so have been canonicalized by get_identifier,
495 so all we need are pointer comparisons. */
497 static bool
498 check_unique_operand_names (tree outputs, tree inputs, tree labels)
500 tree i, j, i_name = NULL_TREE;
502 for (i = outputs; i ; i = TREE_CHAIN (i))
504 i_name = TREE_PURPOSE (TREE_PURPOSE (i));
505 if (! i_name)
506 continue;
508 for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
509 if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
510 goto failure;
513 for (i = inputs; i ; i = TREE_CHAIN (i))
515 i_name = TREE_PURPOSE (TREE_PURPOSE (i));
516 if (! i_name)
517 continue;
519 for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
520 if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
521 goto failure;
522 for (j = outputs; j ; j = TREE_CHAIN (j))
523 if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
524 goto failure;
527 for (i = labels; i ; i = TREE_CHAIN (i))
529 i_name = TREE_PURPOSE (i);
530 if (! i_name)
531 continue;
533 for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
534 if (simple_cst_equal (i_name, TREE_PURPOSE (j)))
535 goto failure;
536 for (j = inputs; j ; j = TREE_CHAIN (j))
537 if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
538 goto failure;
541 return true;
543 failure:
544 error ("duplicate asm operand name %qs", TREE_STRING_POINTER (i_name));
545 return false;
548 /* Resolve the names of the operands in *POUTPUTS and *PINPUTS to numbers,
549 and replace the name expansions in STRING and in the constraints to
550 those numbers. This is generally done in the front end while creating
551 the ASM_EXPR generic tree that eventually becomes the GIMPLE_ASM. */
553 tree
554 resolve_asm_operand_names (tree string, tree outputs, tree inputs, tree labels)
556 char *buffer;
557 char *p;
558 const char *c;
559 tree t;
561 check_unique_operand_names (outputs, inputs, labels);
563 /* Substitute [<name>] in input constraint strings. There should be no
564 named operands in output constraints. */
565 for (t = inputs; t ; t = TREE_CHAIN (t))
567 c = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
568 if (strchr (c, '[') != NULL)
570 p = buffer = xstrdup (c);
571 while ((p = strchr (p, '[')) != NULL)
572 p = resolve_operand_name_1 (p, outputs, inputs, NULL);
573 TREE_VALUE (TREE_PURPOSE (t))
574 = build_string (strlen (buffer), buffer);
575 free (buffer);
579 /* Now check for any needed substitutions in the template. */
580 c = TREE_STRING_POINTER (string);
581 while ((c = strchr (c, '%')) != NULL)
583 if (c[1] == '[')
584 break;
585 else if (ISALPHA (c[1]) && c[2] == '[')
586 break;
587 else
589 c += 1 + (c[1] == '%');
590 continue;
594 if (c)
596 /* OK, we need to make a copy so we can perform the substitutions.
597 Assume that we will not need extra space--we get to remove '['
598 and ']', which means we cannot have a problem until we have more
599 than 999 operands. */
600 buffer = xstrdup (TREE_STRING_POINTER (string));
601 p = buffer + (c - TREE_STRING_POINTER (string));
603 while ((p = strchr (p, '%')) != NULL)
605 if (p[1] == '[')
606 p += 1;
607 else if (ISALPHA (p[1]) && p[2] == '[')
608 p += 2;
609 else
611 p += 1 + (p[1] == '%');
612 continue;
615 p = resolve_operand_name_1 (p, outputs, inputs, labels);
618 string = build_string (strlen (buffer), buffer);
619 free (buffer);
622 return string;
625 /* A subroutine of resolve_operand_names. P points to the '[' for a
626 potential named operand of the form [<name>]. In place, replace
627 the name and brackets with a number. Return a pointer to the
628 balance of the string after substitution. */
630 static char *
631 resolve_operand_name_1 (char *p, tree outputs, tree inputs, tree labels)
633 char *q;
634 int op;
635 tree t;
637 /* Collect the operand name. */
638 q = strchr (++p, ']');
639 if (!q)
641 error ("missing close brace for named operand");
642 return strchr (p, '\0');
644 *q = '\0';
646 /* Resolve the name to a number. */
647 for (op = 0, t = outputs; t ; t = TREE_CHAIN (t), op++)
649 tree name = TREE_PURPOSE (TREE_PURPOSE (t));
650 if (name && strcmp (TREE_STRING_POINTER (name), p) == 0)
651 goto found;
653 for (t = inputs; t ; t = TREE_CHAIN (t), op++)
655 tree name = TREE_PURPOSE (TREE_PURPOSE (t));
656 if (name && strcmp (TREE_STRING_POINTER (name), p) == 0)
657 goto found;
659 for (t = labels; t ; t = TREE_CHAIN (t), op++)
661 tree name = TREE_PURPOSE (t);
662 if (name && strcmp (TREE_STRING_POINTER (name), p) == 0)
663 goto found;
666 error ("undefined named operand %qs", identifier_to_locale (p));
667 op = 0;
669 found:
670 /* Replace the name with the number. Unfortunately, not all libraries
671 get the return value of sprintf correct, so search for the end of the
672 generated string by hand. */
673 sprintf (--p, "%d", op);
674 p = strchr (p, '\0');
676 /* Verify the no extra buffer space assumption. */
677 gcc_assert (p <= q);
679 /* Shift the rest of the buffer down to fill the gap. */
680 memmove (p, q + 1, strlen (q + 1) + 1);
682 return p;
686 /* Generate RTL to return directly from the current function.
687 (That is, we bypass any return value.) */
689 void
690 expand_naked_return (void)
692 rtx_code_label *end_label;
694 clear_pending_stack_adjust ();
695 do_pending_stack_adjust ();
697 end_label = naked_return_label;
698 if (end_label == 0)
699 end_label = naked_return_label = gen_label_rtx ();
701 emit_jump (end_label);
704 /* Generate code to jump to LABEL if OP0 and OP1 are equal in mode MODE. PROB
705 is the probability of jumping to LABEL. */
706 static void
707 do_jump_if_equal (machine_mode mode, rtx op0, rtx op1, rtx_code_label *label,
708 int unsignedp, profile_probability prob)
710 do_compare_rtx_and_jump (op0, op1, EQ, unsignedp, mode,
711 NULL_RTX, NULL, label, prob);
714 /* Do the insertion of a case label into case_list. The labels are
715 fed to us in descending order from the sorted vector of case labels used
716 in the tree part of the middle end. So the list we construct is
717 sorted in ascending order.
719 LABEL is the case label to be inserted. LOW and HIGH are the bounds
720 against which the index is compared to jump to LABEL and PROB is the
721 estimated probability LABEL is reached from the switch statement. */
723 static struct case_node *
724 add_case_node (struct case_node *head, tree low, tree high,
725 tree label, profile_probability prob,
726 object_allocator<case_node> &case_node_pool)
728 struct case_node *r;
730 gcc_checking_assert (low);
731 gcc_checking_assert (high && (TREE_TYPE (low) == TREE_TYPE (high)));
733 /* Add this label to the chain. */
734 r = case_node_pool.allocate ();
735 r->low = low;
736 r->high = high;
737 r->code_label = label;
738 r->parent = r->left = NULL;
739 r->prob = prob;
740 r->subtree_prob = prob;
741 r->right = head;
742 return r;
745 /* Dump ROOT, a list or tree of case nodes, to file. */
747 static void
748 dump_case_nodes (FILE *f, struct case_node *root,
749 int indent_step, int indent_level)
751 if (root == 0)
752 return;
753 indent_level++;
755 dump_case_nodes (f, root->left, indent_step, indent_level);
757 fputs (";; ", f);
758 fprintf (f, "%*s", indent_step * indent_level, "");
759 print_dec (root->low, f, TYPE_SIGN (TREE_TYPE (root->low)));
760 if (!tree_int_cst_equal (root->low, root->high))
762 fprintf (f, " ... ");
763 print_dec (root->high, f, TYPE_SIGN (TREE_TYPE (root->high)));
765 fputs ("\n", f);
767 dump_case_nodes (f, root->right, indent_step, indent_level);
770 /* Return the smallest number of different values for which it is best to use a
771 jump-table instead of a tree of conditional branches. */
773 static unsigned int
774 case_values_threshold (void)
776 unsigned int threshold = PARAM_VALUE (PARAM_CASE_VALUES_THRESHOLD);
778 if (threshold == 0)
779 threshold = targetm.case_values_threshold ();
781 return threshold;
784 /* Return true if a switch should be expanded as a decision tree.
785 RANGE is the difference between highest and lowest case.
786 UNIQ is number of unique case node targets, not counting the default case.
787 COUNT is the number of comparisons needed, not counting the default case. */
789 static bool
790 expand_switch_as_decision_tree_p (tree range,
791 unsigned int uniq ATTRIBUTE_UNUSED,
792 unsigned int count)
794 int max_ratio;
796 /* If neither casesi or tablejump is available, or flag_jump_tables
797 over-ruled us, we really have no choice. */
798 if (!targetm.have_casesi () && !targetm.have_tablejump ())
799 return true;
800 if (!flag_jump_tables)
801 return true;
802 #ifndef ASM_OUTPUT_ADDR_DIFF_ELT
803 if (flag_pic)
804 return true;
805 #endif
807 /* If the switch is relatively small such that the cost of one
808 indirect jump on the target are higher than the cost of a
809 decision tree, go with the decision tree.
811 If range of values is much bigger than number of values,
812 or if it is too large to represent in a HOST_WIDE_INT,
813 make a sequence of conditional branches instead of a dispatch.
815 The definition of "much bigger" depends on whether we are
816 optimizing for size or for speed. If the former, the maximum
817 ratio range/count = 3, because this was found to be the optimal
818 ratio for size on i686-pc-linux-gnu, see PR11823. The ratio
819 10 is much older, and was probably selected after an extensive
820 benchmarking investigation on numerous platforms. Or maybe it
821 just made sense to someone at some point in the history of GCC,
822 who knows... */
823 max_ratio = optimize_insn_for_size_p () ? 3 : 10;
824 if (count < case_values_threshold ()
825 || ! tree_fits_uhwi_p (range)
826 || compare_tree_int (range, max_ratio * count) > 0)
827 return true;
829 return false;
832 /* Generate a decision tree, switching on INDEX_EXPR and jumping to
833 one of the labels in CASE_LIST or to the DEFAULT_LABEL.
834 DEFAULT_PROB is the estimated probability that it jumps to
835 DEFAULT_LABEL.
837 We generate a binary decision tree to select the appropriate target
838 code. This is done as follows:
840 If the index is a short or char that we do not have
841 an insn to handle comparisons directly, convert it to
842 a full integer now, rather than letting each comparison
843 generate the conversion.
845 Load the index into a register.
847 The list of cases is rearranged into a binary tree,
848 nearly optimal assuming equal probability for each case.
850 The tree is transformed into RTL, eliminating redundant
851 test conditions at the same time.
853 If program flow could reach the end of the decision tree
854 an unconditional jump to the default code is emitted.
856 The above process is unaware of the CFG. The caller has to fix up
857 the CFG itself. This is done in cfgexpand.c. */
859 static void
860 emit_case_decision_tree (tree index_expr, tree index_type,
861 case_node_ptr case_list, rtx_code_label *default_label,
862 profile_probability default_prob)
864 rtx index = expand_normal (index_expr);
866 if (GET_MODE_CLASS (GET_MODE (index)) == MODE_INT
867 && ! have_insn_for (COMPARE, GET_MODE (index)))
869 int unsignedp = TYPE_UNSIGNED (index_type);
870 machine_mode wider_mode;
871 for (wider_mode = GET_MODE (index); wider_mode != VOIDmode;
872 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
873 if (have_insn_for (COMPARE, wider_mode))
875 index = convert_to_mode (wider_mode, index, unsignedp);
876 break;
880 do_pending_stack_adjust ();
882 if (MEM_P (index))
884 index = copy_to_reg (index);
885 if (TREE_CODE (index_expr) == SSA_NAME)
886 set_reg_attrs_for_decl_rtl (index_expr, index);
889 balance_case_nodes (&case_list, NULL);
891 if (dump_file && (dump_flags & TDF_DETAILS))
893 int indent_step = ceil_log2 (TYPE_PRECISION (index_type)) + 2;
894 fprintf (dump_file, ";; Expanding GIMPLE switch as decision tree:\n");
895 dump_case_nodes (dump_file, case_list, indent_step, 0);
898 emit_case_nodes (index, case_list, default_label, default_prob, index_type);
899 if (default_label)
900 emit_jump (default_label);
903 /* Return the sum of probabilities of outgoing edges of basic block BB. */
905 static profile_probability
906 get_outgoing_edge_probs (basic_block bb)
908 edge e;
909 edge_iterator ei;
910 profile_probability prob_sum = profile_probability::never ();
911 if (!bb)
912 return profile_probability::never ();
913 FOR_EACH_EDGE (e, ei, bb->succs)
914 prob_sum += e->probability;
915 return prob_sum;
918 /* Computes the conditional probability of jumping to a target if the branch
919 instruction is executed.
920 TARGET_PROB is the estimated probability of jumping to a target relative
921 to some basic block BB.
922 BASE_PROB is the probability of reaching the branch instruction relative
923 to the same basic block BB. */
925 static inline profile_probability
926 conditional_probability (profile_probability target_prob,
927 profile_probability base_prob)
929 return target_prob / base_prob;
932 /* Generate a dispatch tabler, switching on INDEX_EXPR and jumping to
933 one of the labels in CASE_LIST or to the DEFAULT_LABEL.
934 MINVAL, MAXVAL, and RANGE are the extrema and range of the case
935 labels in CASE_LIST. STMT_BB is the basic block containing the statement.
937 First, a jump insn is emitted. First we try "casesi". If that
938 fails, try "tablejump". A target *must* have one of them (or both).
940 Then, a table with the target labels is emitted.
942 The process is unaware of the CFG. The caller has to fix up
943 the CFG itself. This is done in cfgexpand.c. */
945 static void
946 emit_case_dispatch_table (tree index_expr, tree index_type,
947 struct case_node *case_list, rtx default_label,
948 tree minval, tree maxval, tree range,
949 basic_block stmt_bb)
951 int i, ncases;
952 struct case_node *n;
953 rtx *labelvec;
954 rtx_insn *fallback_label = label_rtx (case_list->code_label);
955 rtx_code_label *table_label = gen_label_rtx ();
956 bool has_gaps = false;
957 edge default_edge = stmt_bb ? EDGE_SUCC (stmt_bb, 0) : NULL;
958 profile_probability default_prob = default_edge ? default_edge->probability
959 : profile_probability::never ();
960 profile_probability base = get_outgoing_edge_probs (stmt_bb);
961 bool try_with_tablejump = false;
963 profile_probability new_default_prob = conditional_probability (default_prob,
964 base);
966 if (! try_casesi (index_type, index_expr, minval, range,
967 table_label, default_label, fallback_label,
968 new_default_prob))
970 /* Index jumptables from zero for suitable values of minval to avoid
971 a subtraction. For the rationale see:
972 "http://gcc.gnu.org/ml/gcc-patches/2001-10/msg01234.html". */
973 if (optimize_insn_for_speed_p ()
974 && compare_tree_int (minval, 0) > 0
975 && compare_tree_int (minval, 3) < 0)
977 minval = build_int_cst (index_type, 0);
978 range = maxval;
979 has_gaps = true;
981 try_with_tablejump = true;
984 /* Get table of labels to jump to, in order of case index. */
986 ncases = tree_to_shwi (range) + 1;
987 labelvec = XALLOCAVEC (rtx, ncases);
988 memset (labelvec, 0, ncases * sizeof (rtx));
990 for (n = case_list; n; n = n->right)
992 /* Compute the low and high bounds relative to the minimum
993 value since that should fit in a HOST_WIDE_INT while the
994 actual values may not. */
995 HOST_WIDE_INT i_low
996 = tree_to_uhwi (fold_build2 (MINUS_EXPR, index_type,
997 n->low, minval));
998 HOST_WIDE_INT i_high
999 = tree_to_uhwi (fold_build2 (MINUS_EXPR, index_type,
1000 n->high, minval));
1001 HOST_WIDE_INT i;
1003 for (i = i_low; i <= i_high; i ++)
1004 labelvec[i]
1005 = gen_rtx_LABEL_REF (Pmode, label_rtx (n->code_label));
1008 /* The dispatch table may contain gaps, including at the beginning of
1009 the table if we tried to avoid the minval subtraction. We fill the
1010 dispatch table slots associated with the gaps with the default case label.
1011 However, in the event the default case is unreachable, we then use
1012 any label from one of the case statements. */
1013 rtx gap_label = (default_label) ? default_label : fallback_label;
1015 for (i = 0; i < ncases; i++)
1016 if (labelvec[i] == 0)
1018 has_gaps = true;
1019 labelvec[i] = gen_rtx_LABEL_REF (Pmode, gap_label);
1022 if (has_gaps && default_label)
1024 /* There is at least one entry in the jump table that jumps
1025 to default label. The default label can either be reached
1026 through the indirect jump or the direct conditional jump
1027 before that. Split the probability of reaching the
1028 default label among these two jumps. */
1029 new_default_prob = conditional_probability (default_prob.apply_scale
1030 (1, 2),
1031 base);
1032 default_prob = default_prob.apply_scale (1, 2);
1033 base -= default_prob;
1035 else
1037 base -= default_prob;
1038 default_prob = profile_probability::never ();
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 > profile_probability::never ())
1049 edge e;
1050 edge_iterator ei;
1051 FOR_EACH_EDGE (e, ei, stmt_bb->succs)
1052 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;
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);
1143 /* Optimization of switch statements with only one label has already
1144 occurred, so we should never see them at this point. */
1145 gcc_assert (ncases > 1);
1147 do_pending_stack_adjust ();
1149 /* Find the default case target label. */
1150 default_label = jump_target_rtx
1151 (CASE_LABEL (gimple_switch_default_label (stmt)));
1152 edge default_edge = EDGE_SUCC (bb, 0);
1153 profile_probability default_prob = default_edge->probability;
1155 /* Get upper and lower bounds of case values. */
1156 elt = gimple_switch_label (stmt, 1);
1157 minval = fold_convert (index_type, CASE_LOW (elt));
1158 elt = gimple_switch_label (stmt, ncases - 1);
1159 if (CASE_HIGH (elt))
1160 maxval = fold_convert (index_type, CASE_HIGH (elt));
1161 else
1162 maxval = fold_convert (index_type, CASE_LOW (elt));
1164 /* Compute span of values. */
1165 range = fold_build2 (MINUS_EXPR, index_type, maxval, minval);
1167 /* Listify the labels queue and gather some numbers to decide
1168 how to expand this switch(). */
1169 uniq = 0;
1170 count = 0;
1171 hash_set<tree> seen_labels;
1172 compute_cases_per_edge (stmt);
1174 for (i = ncases - 1; i >= 1; --i)
1176 elt = gimple_switch_label (stmt, i);
1177 tree low = CASE_LOW (elt);
1178 gcc_assert (low);
1179 tree high = CASE_HIGH (elt);
1180 gcc_assert (! high || tree_int_cst_lt (low, high));
1181 tree lab = CASE_LABEL (elt);
1183 /* Count the elements.
1184 A range counts double, since it requires two compares. */
1185 count++;
1186 if (high)
1187 count++;
1189 /* If we have not seen this label yet, then increase the
1190 number of unique case node targets seen. */
1191 if (!seen_labels.add (lab))
1192 uniq++;
1194 /* The bounds on the case range, LOW and HIGH, have to be converted
1195 to case's index type TYPE. Note that the original type of the
1196 case index in the source code is usually "lost" during
1197 gimplification due to type promotion, but the case labels retain the
1198 original type. Make sure to drop overflow flags. */
1199 low = fold_convert (index_type, low);
1200 if (TREE_OVERFLOW (low))
1201 low = wide_int_to_tree (index_type, low);
1203 /* The canonical from of a case label in GIMPLE is that a simple case
1204 has an empty CASE_HIGH. For the casesi and tablejump expanders,
1205 the back ends want simple cases to have high == low. */
1206 if (! high)
1207 high = low;
1208 high = fold_convert (index_type, high);
1209 if (TREE_OVERFLOW (high))
1210 high = wide_int_to_tree (index_type, high);
1212 basic_block case_bb = label_to_block_fn (cfun, lab);
1213 edge case_edge = find_edge (bb, case_bb);
1214 case_list = add_case_node (
1215 case_list, low, high, lab,
1216 case_edge->probability.apply_scale (1, (intptr_t)(case_edge->aux)),
1217 case_node_pool);
1219 reset_out_edges_aux (bb);
1221 /* cleanup_tree_cfg removes all SWITCH_EXPR with a single
1222 destination, such as one with a default case only.
1223 It also removes cases that are out of range for the switch
1224 type, so we should never get a zero here. */
1225 gcc_assert (count > 0);
1227 rtx_insn *before_case = get_last_insn ();
1229 /* Decide how to expand this switch.
1230 The two options at this point are a dispatch table (casesi or
1231 tablejump) or a decision tree. */
1233 if (expand_switch_as_decision_tree_p (range, uniq, count))
1234 emit_case_decision_tree (index_expr, index_type,
1235 case_list, default_label,
1236 default_prob);
1237 else
1239 /* If the default case is unreachable, then set default_label to NULL
1240 so that we omit the range check when generating the dispatch table.
1241 We also remove the edge to the unreachable default case. The block
1242 itself will be automatically removed later. */
1243 if (EDGE_COUNT (default_edge->dest->succs) == 0
1244 && gimple_seq_unreachable_p (bb_seq (default_edge->dest)))
1246 default_label = NULL;
1247 remove_edge (default_edge);
1249 emit_case_dispatch_table (index_expr, index_type,
1250 case_list, default_label,
1251 minval, maxval, range, bb);
1254 reorder_insns (NEXT_INSN (before_case), get_last_insn (), before_case);
1256 free_temp_slots ();
1259 /* Expand the dispatch to a short decrement chain if there are few cases
1260 to dispatch to. Likewise if neither casesi nor tablejump is available,
1261 or if flag_jump_tables is set. Otherwise, expand as a casesi or a
1262 tablejump. The index mode is always the mode of integer_type_node.
1263 Trap if no case matches the index.
1265 DISPATCH_INDEX is the index expression to switch on. It should be a
1266 memory or register operand.
1268 DISPATCH_TABLE is a set of case labels. The set should be sorted in
1269 ascending order, be contiguous, starting with value 0, and contain only
1270 single-valued case labels. */
1272 void
1273 expand_sjlj_dispatch_table (rtx dispatch_index,
1274 vec<tree> dispatch_table)
1276 tree index_type = integer_type_node;
1277 machine_mode index_mode = TYPE_MODE (index_type);
1279 int ncases = dispatch_table.length ();
1281 do_pending_stack_adjust ();
1282 rtx_insn *before_case = get_last_insn ();
1284 /* Expand as a decrement-chain if there are 5 or fewer dispatch
1285 labels. This covers more than 98% of the cases in libjava,
1286 and seems to be a reasonable compromise between the "old way"
1287 of expanding as a decision tree or dispatch table vs. the "new
1288 way" with decrement chain or dispatch table. */
1289 if (dispatch_table.length () <= 5
1290 || (!targetm.have_casesi () && !targetm.have_tablejump ())
1291 || !flag_jump_tables)
1293 /* Expand the dispatch as a decrement chain:
1295 "switch(index) {case 0: do_0; case 1: do_1; ...; case N: do_N;}"
1299 if (index == 0) do_0; else index--;
1300 if (index == 0) do_1; else index--;
1302 if (index == 0) do_N; else index--;
1304 This is more efficient than a dispatch table on most machines.
1305 The last "index--" is redundant but the code is trivially dead
1306 and will be cleaned up by later passes. */
1307 rtx index = copy_to_mode_reg (index_mode, dispatch_index);
1308 rtx zero = CONST0_RTX (index_mode);
1309 for (int i = 0; i < ncases; i++)
1311 tree elt = dispatch_table[i];
1312 rtx_code_label *lab = jump_target_rtx (CASE_LABEL (elt));
1313 do_jump_if_equal (index_mode, index, zero, lab, 0,
1314 profile_probability::uninitialized ());
1315 force_expand_binop (index_mode, sub_optab,
1316 index, CONST1_RTX (index_mode),
1317 index, 0, OPTAB_DIRECT);
1320 else
1322 /* Similar to expand_case, but much simpler. */
1323 struct case_node *case_list = 0;
1324 object_allocator<case_node> case_node_pool ("struct sjlj_case pool");
1325 tree index_expr = make_tree (index_type, dispatch_index);
1326 tree minval = build_int_cst (index_type, 0);
1327 tree maxval = CASE_LOW (dispatch_table.last ());
1328 tree range = maxval;
1329 rtx_code_label *default_label = gen_label_rtx ();
1331 for (int i = ncases - 1; i >= 0; --i)
1333 tree elt = dispatch_table[i];
1334 tree low = CASE_LOW (elt);
1335 tree lab = CASE_LABEL (elt);
1336 case_list = add_case_node (case_list, low, low, lab,
1337 profile_probability::guessed_always ()
1338 .apply_scale (1, ncases),
1339 case_node_pool);
1342 emit_case_dispatch_table (index_expr, index_type,
1343 case_list, default_label,
1344 minval, maxval, range,
1345 BLOCK_FOR_INSN (before_case));
1346 emit_label (default_label);
1349 /* Dispatching something not handled? Trap! */
1350 expand_builtin_trap ();
1352 reorder_insns (NEXT_INSN (before_case), get_last_insn (), before_case);
1354 free_temp_slots ();
1358 /* Take an ordered list of case nodes
1359 and transform them into a near optimal binary tree,
1360 on the assumption that any target code selection value is as
1361 likely as any other.
1363 The transformation is performed by splitting the ordered
1364 list into two equal sections plus a pivot. The parts are
1365 then attached to the pivot as left and right branches. Each
1366 branch is then transformed recursively. */
1368 static void
1369 balance_case_nodes (case_node_ptr *head, case_node_ptr parent)
1371 case_node_ptr np;
1373 np = *head;
1374 if (np)
1376 int i = 0;
1377 int ranges = 0;
1378 case_node_ptr *npp;
1379 case_node_ptr left;
1381 /* Count the number of entries on branch. Also count the ranges. */
1383 while (np)
1385 if (!tree_int_cst_equal (np->low, np->high))
1386 ranges++;
1388 i++;
1389 np = np->right;
1392 if (i > 2)
1394 /* Split this list if it is long enough for that to help. */
1395 npp = head;
1396 left = *npp;
1398 /* If there are just three nodes, split at the middle one. */
1399 if (i == 3)
1400 npp = &(*npp)->right;
1401 else
1403 /* Find the place in the list that bisects the list's total cost,
1404 where ranges count as 2.
1405 Here I gets half the total cost. */
1406 i = (i + ranges + 1) / 2;
1407 while (1)
1409 /* Skip nodes while their cost does not reach that amount. */
1410 if (!tree_int_cst_equal ((*npp)->low, (*npp)->high))
1411 i--;
1412 i--;
1413 if (i <= 0)
1414 break;
1415 npp = &(*npp)->right;
1418 *head = np = *npp;
1419 *npp = 0;
1420 np->parent = parent;
1421 np->left = left;
1423 /* Optimize each of the two split parts. */
1424 balance_case_nodes (&np->left, np);
1425 balance_case_nodes (&np->right, np);
1426 np->subtree_prob = np->prob;
1427 np->subtree_prob += np->left->subtree_prob;
1428 np->subtree_prob += np->right->subtree_prob;
1430 else
1432 /* Else leave this branch as one level,
1433 but fill in `parent' fields. */
1434 np = *head;
1435 np->parent = parent;
1436 np->subtree_prob = np->prob;
1437 for (; np->right; np = np->right)
1439 np->right->parent = np;
1440 (*head)->subtree_prob += np->right->subtree_prob;
1446 /* Search the parent sections of the case node tree
1447 to see if a test for the lower bound of NODE would be redundant.
1448 INDEX_TYPE is the type of the index expression.
1450 The instructions to generate the case decision tree are
1451 output in the same order as nodes are processed so it is
1452 known that if a parent node checks the range of the current
1453 node minus one that the current node is bounded at its lower
1454 span. Thus the test would be redundant. */
1456 static int
1457 node_has_low_bound (case_node_ptr node, tree index_type)
1459 tree low_minus_one;
1460 case_node_ptr pnode;
1462 /* If the lower bound of this node is the lowest value in the index type,
1463 we need not test it. */
1465 if (tree_int_cst_equal (node->low, TYPE_MIN_VALUE (index_type)))
1466 return 1;
1468 /* If this node has a left branch, the value at the left must be less
1469 than that at this node, so it cannot be bounded at the bottom and
1470 we need not bother testing any further. */
1472 if (node->left)
1473 return 0;
1475 low_minus_one = fold_build2 (MINUS_EXPR, TREE_TYPE (node->low),
1476 node->low,
1477 build_int_cst (TREE_TYPE (node->low), 1));
1479 /* If the subtraction above overflowed, we can't verify anything.
1480 Otherwise, look for a parent that tests our value - 1. */
1482 if (! tree_int_cst_lt (low_minus_one, node->low))
1483 return 0;
1485 for (pnode = node->parent; pnode; pnode = pnode->parent)
1486 if (tree_int_cst_equal (low_minus_one, pnode->high))
1487 return 1;
1489 return 0;
1492 /* Search the parent sections of the case node tree
1493 to see if a test for the upper bound of NODE would be redundant.
1494 INDEX_TYPE is the type of the index expression.
1496 The instructions to generate the case decision tree are
1497 output in the same order as nodes are processed so it is
1498 known that if a parent node checks the range of the current
1499 node plus one that the current node is bounded at its upper
1500 span. Thus the test would be redundant. */
1502 static int
1503 node_has_high_bound (case_node_ptr node, tree index_type)
1505 tree high_plus_one;
1506 case_node_ptr pnode;
1508 /* If there is no upper bound, obviously no test is needed. */
1510 if (TYPE_MAX_VALUE (index_type) == NULL)
1511 return 1;
1513 /* If the upper bound of this node is the highest value in the type
1514 of the index expression, we need not test against it. */
1516 if (tree_int_cst_equal (node->high, TYPE_MAX_VALUE (index_type)))
1517 return 1;
1519 /* If this node has a right branch, the value at the right must be greater
1520 than that at this node, so it cannot be bounded at the top and
1521 we need not bother testing any further. */
1523 if (node->right)
1524 return 0;
1526 high_plus_one = fold_build2 (PLUS_EXPR, TREE_TYPE (node->high),
1527 node->high,
1528 build_int_cst (TREE_TYPE (node->high), 1));
1530 /* If the addition above overflowed, we can't verify anything.
1531 Otherwise, look for a parent that tests our value + 1. */
1533 if (! tree_int_cst_lt (node->high, high_plus_one))
1534 return 0;
1536 for (pnode = node->parent; pnode; pnode = pnode->parent)
1537 if (tree_int_cst_equal (high_plus_one, pnode->low))
1538 return 1;
1540 return 0;
1543 /* Search the parent sections of the
1544 case node tree to see if both tests for the upper and lower
1545 bounds of NODE would be redundant. */
1547 static int
1548 node_is_bounded (case_node_ptr node, tree index_type)
1550 return (node_has_low_bound (node, index_type)
1551 && node_has_high_bound (node, index_type));
1555 /* Emit step-by-step code to select a case for the value of INDEX.
1556 The thus generated decision tree follows the form of the
1557 case-node binary tree NODE, whose nodes represent test conditions.
1558 INDEX_TYPE is the type of the index of the switch.
1560 Care is taken to prune redundant tests from the decision tree
1561 by detecting any boundary conditions already checked by
1562 emitted rtx. (See node_has_high_bound, node_has_low_bound
1563 and node_is_bounded, above.)
1565 Where the test conditions can be shown to be redundant we emit
1566 an unconditional jump to the target code. As a further
1567 optimization, the subordinates of a tree node are examined to
1568 check for bounded nodes. In this case conditional and/or
1569 unconditional jumps as a result of the boundary check for the
1570 current node are arranged to target the subordinates associated
1571 code for out of bound conditions on the current node.
1573 We can assume that when control reaches the code generated here,
1574 the index value has already been compared with the parents
1575 of this node, and determined to be on the same side of each parent
1576 as this node is. Thus, if this node tests for the value 51,
1577 and a parent tested for 52, we don't need to consider
1578 the possibility of a value greater than 51. If another parent
1579 tests for the value 50, then this node need not test anything. */
1581 static void
1582 emit_case_nodes (rtx index, case_node_ptr node, rtx_code_label *default_label,
1583 profile_probability default_prob, tree index_type)
1585 /* If INDEX has an unsigned type, we must make unsigned branches. */
1586 int unsignedp = TYPE_UNSIGNED (index_type);
1587 profile_probability probability;
1588 profile_probability prob = node->prob, subtree_prob = node->subtree_prob;
1589 machine_mode mode = GET_MODE (index);
1590 machine_mode imode = TYPE_MODE (index_type);
1592 /* Handle indices detected as constant during RTL expansion. */
1593 if (mode == VOIDmode)
1594 mode = imode;
1596 /* See if our parents have already tested everything for us.
1597 If they have, emit an unconditional jump for this node. */
1598 if (node_is_bounded (node, index_type))
1599 emit_jump (label_rtx (node->code_label));
1601 else if (tree_int_cst_equal (node->low, node->high))
1603 probability = conditional_probability (prob, subtree_prob + default_prob);
1604 /* Node is single valued. First see if the index expression matches
1605 this node and then check our children, if any. */
1606 do_jump_if_equal (mode, index,
1607 convert_modes (mode, imode,
1608 expand_normal (node->low),
1609 unsignedp),
1610 jump_target_rtx (node->code_label),
1611 unsignedp, probability);
1612 /* Since this case is taken at this point, reduce its weight from
1613 subtree_weight. */
1614 subtree_prob -= prob;
1615 if (node->right != 0 && node->left != 0)
1617 /* This node has children on both sides.
1618 Dispatch to one side or the other
1619 by comparing the index value with this node's value.
1620 If one subtree is bounded, check that one first,
1621 so we can avoid real branches in the tree. */
1623 if (node_is_bounded (node->right, index_type))
1625 probability = conditional_probability (
1626 node->right->prob,
1627 subtree_prob + default_prob);
1628 emit_cmp_and_jump_insns (index,
1629 convert_modes
1630 (mode, imode,
1631 expand_normal (node->high),
1632 unsignedp),
1633 GT, NULL_RTX, mode, unsignedp,
1634 label_rtx (node->right->code_label),
1635 probability);
1636 emit_case_nodes (index, node->left, default_label, default_prob,
1637 index_type);
1640 else if (node_is_bounded (node->left, index_type))
1642 probability = conditional_probability (
1643 node->left->prob,
1644 subtree_prob + default_prob);
1645 emit_cmp_and_jump_insns (index,
1646 convert_modes
1647 (mode, imode,
1648 expand_normal (node->high),
1649 unsignedp),
1650 LT, NULL_RTX, mode, unsignedp,
1651 label_rtx (node->left->code_label),
1652 probability);
1653 emit_case_nodes (index, node->right, default_label, default_prob,
1654 index_type);
1657 /* If both children are single-valued cases with no
1658 children, finish up all the work. This way, we can save
1659 one ordered comparison. */
1660 else if (tree_int_cst_equal (node->right->low, node->right->high)
1661 && node->right->left == 0
1662 && node->right->right == 0
1663 && tree_int_cst_equal (node->left->low, node->left->high)
1664 && node->left->left == 0
1665 && node->left->right == 0)
1667 /* Neither node is bounded. First distinguish the two sides;
1668 then emit the code for one side at a time. */
1670 /* See if the value matches what the right hand side
1671 wants. */
1672 probability = conditional_probability (
1673 node->right->prob,
1674 subtree_prob + default_prob);
1675 do_jump_if_equal (mode, index,
1676 convert_modes (mode, imode,
1677 expand_normal (node->right->low),
1678 unsignedp),
1679 jump_target_rtx (node->right->code_label),
1680 unsignedp, probability);
1682 /* See if the value matches what the left hand side
1683 wants. */
1684 probability = conditional_probability (
1685 node->left->prob,
1686 subtree_prob + default_prob);
1687 do_jump_if_equal (mode, index,
1688 convert_modes (mode, imode,
1689 expand_normal (node->left->low),
1690 unsignedp),
1691 jump_target_rtx (node->left->code_label),
1692 unsignedp, probability);
1695 else
1697 /* Neither node is bounded. First distinguish the two sides;
1698 then emit the code for one side at a time. */
1700 tree test_label
1701 = build_decl (curr_insn_location (),
1702 LABEL_DECL, NULL_TREE, void_type_node);
1704 /* The default label could be reached either through the right
1705 subtree or the left subtree. Divide the probability
1706 equally. */
1707 probability = conditional_probability (
1708 node->right->subtree_prob + default_prob.apply_scale (1, 2),
1709 subtree_prob + default_prob);
1710 /* See if the value is on the right. */
1711 emit_cmp_and_jump_insns (index,
1712 convert_modes
1713 (mode, imode,
1714 expand_normal (node->high),
1715 unsignedp),
1716 GT, NULL_RTX, mode, unsignedp,
1717 label_rtx (test_label),
1718 probability);
1719 default_prob = default_prob.apply_scale (1, 2);
1721 /* Value must be on the left.
1722 Handle the left-hand subtree. */
1723 emit_case_nodes (index, node->left, default_label, default_prob, index_type);
1724 /* If left-hand subtree does nothing,
1725 go to default. */
1726 if (default_label)
1727 emit_jump (default_label);
1729 /* Code branches here for the right-hand subtree. */
1730 expand_label (test_label);
1731 emit_case_nodes (index, node->right, default_label, default_prob, index_type);
1735 else if (node->right != 0 && node->left == 0)
1737 /* Here we have a right child but no left so we issue a conditional
1738 branch to default and process the right child.
1740 Omit the conditional branch to default if the right child
1741 does not have any children and is single valued; it would
1742 cost too much space to save so little time. */
1744 if (node->right->right || node->right->left
1745 || !tree_int_cst_equal (node->right->low, node->right->high))
1747 if (!node_has_low_bound (node, index_type))
1749 probability = conditional_probability (
1750 default_prob.apply_scale (1, 2),
1751 subtree_prob + default_prob);
1752 emit_cmp_and_jump_insns (index,
1753 convert_modes
1754 (mode, imode,
1755 expand_normal (node->high),
1756 unsignedp),
1757 LT, NULL_RTX, mode, unsignedp,
1758 default_label,
1759 probability);
1760 default_prob = default_prob.apply_scale (1, 2);
1763 emit_case_nodes (index, node->right, default_label, default_prob, index_type);
1765 else
1767 probability = conditional_probability (
1768 node->right->subtree_prob,
1769 subtree_prob + default_prob);
1770 /* We cannot process node->right normally
1771 since we haven't ruled out the numbers less than
1772 this node's value. So handle node->right explicitly. */
1773 do_jump_if_equal (mode, index,
1774 convert_modes
1775 (mode, imode,
1776 expand_normal (node->right->low),
1777 unsignedp),
1778 jump_target_rtx (node->right->code_label),
1779 unsignedp, probability);
1783 else if (node->right == 0 && node->left != 0)
1785 /* Just one subtree, on the left. */
1786 if (node->left->left || node->left->right
1787 || !tree_int_cst_equal (node->left->low, node->left->high))
1789 if (!node_has_high_bound (node, index_type))
1791 probability = conditional_probability (
1792 default_prob.apply_scale (1, 2),
1793 subtree_prob + default_prob);
1794 emit_cmp_and_jump_insns (index,
1795 convert_modes
1796 (mode, imode,
1797 expand_normal (node->high),
1798 unsignedp),
1799 GT, NULL_RTX, mode, unsignedp,
1800 default_label,
1801 probability);
1802 default_prob = default_prob.apply_scale (1, 2);
1805 emit_case_nodes (index, node->left, default_label,
1806 default_prob, index_type);
1808 else
1810 probability = conditional_probability (
1811 node->left->subtree_prob,
1812 subtree_prob + default_prob);
1813 /* We cannot process node->left normally
1814 since we haven't ruled out the numbers less than
1815 this node's value. So handle node->left explicitly. */
1816 do_jump_if_equal (mode, index,
1817 convert_modes
1818 (mode, imode,
1819 expand_normal (node->left->low),
1820 unsignedp),
1821 jump_target_rtx (node->left->code_label),
1822 unsignedp, probability);
1826 else
1828 /* Node is a range. These cases are very similar to those for a single
1829 value, except that we do not start by testing whether this node
1830 is the one to branch to. */
1832 if (node->right != 0 && node->left != 0)
1834 /* Node has subtrees on both sides.
1835 If the right-hand subtree is bounded,
1836 test for it first, since we can go straight there.
1837 Otherwise, we need to make a branch in the control structure,
1838 then handle the two subtrees. */
1839 tree test_label = 0;
1841 if (node_is_bounded (node->right, index_type))
1843 /* Right hand node is fully bounded so we can eliminate any
1844 testing and branch directly to the target code. */
1845 probability = conditional_probability (
1846 node->right->subtree_prob,
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 (node->right->code_label),
1855 probability);
1857 else
1859 /* Right hand node requires testing.
1860 Branch to a label where we will handle it later. */
1862 test_label = build_decl (curr_insn_location (),
1863 LABEL_DECL, NULL_TREE, void_type_node);
1864 probability = conditional_probability (
1865 node->right->subtree_prob + default_prob.apply_scale (1, 2),
1866 subtree_prob + default_prob);
1867 emit_cmp_and_jump_insns (index,
1868 convert_modes
1869 (mode, imode,
1870 expand_normal (node->high),
1871 unsignedp),
1872 GT, NULL_RTX, mode, unsignedp,
1873 label_rtx (test_label),
1874 probability);
1875 default_prob = default_prob.apply_scale (1, 2);
1878 /* Value belongs to this node or to the left-hand subtree. */
1880 probability = conditional_probability (
1881 prob,
1882 subtree_prob + default_prob);
1883 emit_cmp_and_jump_insns (index,
1884 convert_modes
1885 (mode, imode,
1886 expand_normal (node->low),
1887 unsignedp),
1888 GE, NULL_RTX, mode, unsignedp,
1889 label_rtx (node->code_label),
1890 probability);
1892 /* Handle the left-hand subtree. */
1893 emit_case_nodes (index, node->left, default_label, default_prob, index_type);
1895 /* If right node had to be handled later, do that now. */
1897 if (test_label)
1899 /* If the left-hand subtree fell through,
1900 don't let it fall into the right-hand subtree. */
1901 if (default_label)
1902 emit_jump (default_label);
1904 expand_label (test_label);
1905 emit_case_nodes (index, node->right, default_label, default_prob, index_type);
1909 else if (node->right != 0 && node->left == 0)
1911 /* Deal with values to the left of this node,
1912 if they are possible. */
1913 if (!node_has_low_bound (node, index_type))
1915 probability = conditional_probability (
1916 default_prob.apply_scale (1, 2),
1917 subtree_prob + default_prob);
1918 emit_cmp_and_jump_insns (index,
1919 convert_modes
1920 (mode, imode,
1921 expand_normal (node->low),
1922 unsignedp),
1923 LT, NULL_RTX, mode, unsignedp,
1924 default_label,
1925 probability);
1926 default_prob = default_prob.apply_scale (1, 2);
1929 /* Value belongs to this node or to the right-hand subtree. */
1931 probability = conditional_probability (
1932 prob,
1933 subtree_prob + default_prob);
1934 emit_cmp_and_jump_insns (index,
1935 convert_modes
1936 (mode, imode,
1937 expand_normal (node->high),
1938 unsignedp),
1939 LE, NULL_RTX, mode, unsignedp,
1940 label_rtx (node->code_label),
1941 probability);
1943 emit_case_nodes (index, node->right, default_label, default_prob, index_type);
1946 else if (node->right == 0 && node->left != 0)
1948 /* Deal with values to the right of this node,
1949 if they are possible. */
1950 if (!node_has_high_bound (node, index_type))
1952 probability = conditional_probability (
1953 default_prob.apply_scale (1, 2),
1954 subtree_prob + default_prob);
1955 emit_cmp_and_jump_insns (index,
1956 convert_modes
1957 (mode, imode,
1958 expand_normal (node->high),
1959 unsignedp),
1960 GT, NULL_RTX, mode, unsignedp,
1961 default_label,
1962 probability);
1963 default_prob = default_prob.apply_scale (1, 2);
1966 /* Value belongs to this node or to the left-hand subtree. */
1968 probability = conditional_probability (
1969 prob,
1970 subtree_prob + default_prob);
1971 emit_cmp_and_jump_insns (index,
1972 convert_modes
1973 (mode, imode,
1974 expand_normal (node->low),
1975 unsignedp),
1976 GE, NULL_RTX, mode, unsignedp,
1977 label_rtx (node->code_label),
1978 probability);
1980 emit_case_nodes (index, node->left, default_label, default_prob, index_type);
1983 else
1985 /* Node has no children so we check low and high bounds to remove
1986 redundant tests. Only one of the bounds can exist,
1987 since otherwise this node is bounded--a case tested already. */
1988 int high_bound = node_has_high_bound (node, index_type);
1989 int low_bound = node_has_low_bound (node, index_type);
1991 if (!high_bound && low_bound)
1993 probability = conditional_probability (
1994 default_prob,
1995 subtree_prob + default_prob);
1996 emit_cmp_and_jump_insns (index,
1997 convert_modes
1998 (mode, imode,
1999 expand_normal (node->high),
2000 unsignedp),
2001 GT, NULL_RTX, mode, unsignedp,
2002 default_label,
2003 probability);
2006 else if (!low_bound && high_bound)
2008 probability = conditional_probability (
2009 default_prob,
2010 subtree_prob + default_prob);
2011 emit_cmp_and_jump_insns (index,
2012 convert_modes
2013 (mode, imode,
2014 expand_normal (node->low),
2015 unsignedp),
2016 LT, NULL_RTX, mode, unsignedp,
2017 default_label,
2018 probability);
2020 else if (!low_bound && !high_bound)
2022 /* Widen LOW and HIGH to the same width as INDEX. */
2023 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
2024 tree low = build1 (CONVERT_EXPR, type, node->low);
2025 tree high = build1 (CONVERT_EXPR, type, node->high);
2026 rtx low_rtx, new_index, new_bound;
2028 /* Instead of doing two branches, emit one unsigned branch for
2029 (index-low) > (high-low). */
2030 low_rtx = expand_expr (low, NULL_RTX, mode, EXPAND_NORMAL);
2031 new_index = expand_simple_binop (mode, MINUS, index, low_rtx,
2032 NULL_RTX, unsignedp,
2033 OPTAB_WIDEN);
2034 new_bound = expand_expr (fold_build2 (MINUS_EXPR, type,
2035 high, low),
2036 NULL_RTX, mode, EXPAND_NORMAL);
2038 probability = conditional_probability (
2039 default_prob,
2040 subtree_prob + default_prob);
2041 emit_cmp_and_jump_insns (new_index, new_bound, GT, NULL_RTX,
2042 mode, 1, default_label, probability);
2045 emit_jump (jump_target_rtx (node->code_label));