1 /* Expands front end tree to back end RTL for GCC
2 Copyright (C) 1987-2015 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
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
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. */
27 #include "coretypes.h"
35 #include "fold-const.h"
37 #include "stor-layout.h"
41 #include "insn-config.h"
51 #include "diagnostic-core.h"
53 #include "langhooks.h"
54 #include "insn-codes.h"
58 #include "internal-fn.h"
60 #include "alloc-pool.h"
61 #include "pretty-print.h"
67 /* Functions and data structures for expanding case statements. */
69 /* Case label structure, used to hold info on labels within case
70 statements. We handle "range" labels; for a single-value label
71 as in C, the high and low limits are the same.
73 We start with a vector of case nodes sorted in ascending order, and
74 the default label as the last element in the vector. Before expanding
75 to RTL, we transform this vector into a list linked via the RIGHT
76 fields in the case_node struct. Nodes with higher case values are
79 Switch statements can be output in three forms. A branch table is
80 used if there are more than a few labels and the labels are dense
81 within the range between the smallest and largest case value. If a
82 branch table is used, no further manipulations are done with the case
85 The alternative to the use of a branch table is to generate a series
86 of compare and jump insns. When that is done, we use the LEFT, RIGHT,
87 and PARENT fields to hold a binary tree. Initially the tree is
88 totally unbalanced, with everything on the right. We balance the tree
89 with nodes on the left having lower case values than the parent
90 and nodes on the right having higher values. We then output the tree
93 For very small, suitable switch statements, we can generate a series
94 of simple bit test and branches instead. */
98 struct case_node
*left
; /* Left son in binary tree */
99 struct case_node
*right
; /* Right son in binary tree; also node chain */
100 struct case_node
*parent
; /* Parent of node in binary tree */
101 tree low
; /* Lowest index value for this label */
102 tree high
; /* Highest index value for this label */
103 tree code_label
; /* Label to jump to when node matches */
104 int prob
; /* Probability of taking this case. */
105 /* Probability of reaching subtree rooted at this node */
109 typedef struct case_node case_node
;
110 typedef struct case_node
*case_node_ptr
;
112 extern basic_block
label_to_block_fn (struct function
*, tree
);
114 static bool check_unique_operand_names (tree
, tree
, tree
);
115 static char *resolve_operand_name_1 (char *, tree
, tree
, tree
);
116 static void balance_case_nodes (case_node_ptr
*, case_node_ptr
);
117 static int node_has_low_bound (case_node_ptr
, tree
);
118 static int node_has_high_bound (case_node_ptr
, tree
);
119 static int node_is_bounded (case_node_ptr
, tree
);
120 static void emit_case_nodes (rtx
, case_node_ptr
, rtx_code_label
*, int, tree
);
122 /* Return the rtx-label that corresponds to a LABEL_DECL,
123 creating it if necessary. */
126 label_rtx (tree label
)
128 gcc_assert (TREE_CODE (label
) == LABEL_DECL
);
130 if (!DECL_RTL_SET_P (label
))
132 rtx_code_label
*r
= gen_label_rtx ();
133 SET_DECL_RTL (label
, r
);
134 if (FORCED_LABEL (label
) || DECL_NONLOCAL (label
))
135 LABEL_PRESERVE_P (r
) = 1;
138 return as_a
<rtx_insn
*> (DECL_RTL (label
));
141 /* As above, but also put it on the forced-reference list of the
142 function that contains it. */
144 force_label_rtx (tree label
)
146 rtx_insn
*ref
= label_rtx (label
);
147 tree function
= decl_function_context (label
);
149 gcc_assert (function
);
151 forced_labels
= gen_rtx_INSN_LIST (VOIDmode
, ref
, forced_labels
);
155 /* As label_rtx, but ensures (in check build), that returned value is
156 an existing label (i.e. rtx with code CODE_LABEL). */
158 jump_target_rtx (tree label
)
160 return as_a
<rtx_code_label
*> (label_rtx (label
));
163 /* Add an unconditional jump to LABEL as the next sequential instruction. */
166 emit_jump (rtx label
)
168 do_pending_stack_adjust ();
169 emit_jump_insn (targetm
.gen_jump (label
));
173 /* Handle goto statements and the labels that they can go to. */
175 /* Specify the location in the RTL code of a label LABEL,
176 which is a LABEL_DECL tree node.
178 This is used for the kind of label that the user can jump to with a
179 goto statement, and for alternatives of a switch or case statement.
180 RTL labels generated for loops and conditionals don't go through here;
181 they are generated directly at the RTL level, by other functions below.
183 Note that this has nothing to do with defining label *names*.
184 Languages vary in how they do that and what that even means. */
187 expand_label (tree label
)
189 rtx_code_label
*label_r
= jump_target_rtx (label
);
191 do_pending_stack_adjust ();
192 emit_label (label_r
);
193 if (DECL_NAME (label
))
194 LABEL_NAME (DECL_RTL (label
)) = IDENTIFIER_POINTER (DECL_NAME (label
));
196 if (DECL_NONLOCAL (label
))
198 expand_builtin_setjmp_receiver (NULL
);
199 nonlocal_goto_handler_labels
200 = gen_rtx_INSN_LIST (VOIDmode
, label_r
,
201 nonlocal_goto_handler_labels
);
204 if (FORCED_LABEL (label
))
205 forced_labels
= gen_rtx_INSN_LIST (VOIDmode
, label_r
, forced_labels
);
207 if (DECL_NONLOCAL (label
) || FORCED_LABEL (label
))
208 maybe_set_first_label_num (label_r
);
211 /* Parse the output constraint pointed to by *CONSTRAINT_P. It is the
212 OPERAND_NUMth output operand, indexed from zero. There are NINPUTS
213 inputs and NOUTPUTS outputs to this extended-asm. Upon return,
214 *ALLOWS_MEM will be TRUE iff the constraint allows the use of a
215 memory operand. Similarly, *ALLOWS_REG will be TRUE iff the
216 constraint allows the use of a register operand. And, *IS_INOUT
217 will be true if the operand is read-write, i.e., if it is used as
218 an input as well as an output. If *CONSTRAINT_P is not in
219 canonical form, it will be made canonical. (Note that `+' will be
220 replaced with `=' as part of this process.)
222 Returns TRUE if all went well; FALSE if an error occurred. */
225 parse_output_constraint (const char **constraint_p
, int operand_num
,
226 int ninputs
, int noutputs
, bool *allows_mem
,
227 bool *allows_reg
, bool *is_inout
)
229 const char *constraint
= *constraint_p
;
232 /* Assume the constraint doesn't allow the use of either a register
237 /* Allow the `=' or `+' to not be at the beginning of the string,
238 since it wasn't explicitly documented that way, and there is a
239 large body of code that puts it last. Swap the character to
240 the front, so as not to uglify any place else. */
241 p
= strchr (constraint
, '=');
243 p
= strchr (constraint
, '+');
245 /* If the string doesn't contain an `=', issue an error
249 error ("output operand constraint lacks %<=%>");
253 /* If the constraint begins with `+', then the operand is both read
254 from and written to. */
255 *is_inout
= (*p
== '+');
257 /* Canonicalize the output constraint so that it begins with `='. */
258 if (p
!= constraint
|| *is_inout
)
261 size_t c_len
= strlen (constraint
);
264 warning (0, "output constraint %qc for operand %d "
265 "is not at the beginning",
268 /* Make a copy of the constraint. */
269 buf
= XALLOCAVEC (char, c_len
+ 1);
270 strcpy (buf
, constraint
);
271 /* Swap the first character and the `=' or `+'. */
272 buf
[p
- constraint
] = buf
[0];
273 /* Make sure the first character is an `='. (Until we do this,
274 it might be a `+'.) */
276 /* Replace the constraint with the canonicalized string. */
277 *constraint_p
= ggc_alloc_string (buf
, c_len
);
278 constraint
= *constraint_p
;
281 /* Loop through the constraint string. */
282 for (p
= constraint
+ 1; *p
; p
+= CONSTRAINT_LEN (*p
, p
))
287 error ("operand constraint contains incorrectly positioned "
292 if (operand_num
+ 1 == ninputs
+ noutputs
)
294 error ("%<%%%> constraint used with last operand");
299 case '?': case '!': case '*': case '&': case '#':
301 case 'E': case 'F': case 'G': case 'H':
302 case 's': case 'i': case 'n':
303 case 'I': case 'J': case 'K': case 'L': case 'M':
304 case 'N': case 'O': case 'P': case ',':
307 case '0': case '1': case '2': case '3': case '4':
308 case '5': case '6': case '7': case '8': case '9':
310 error ("matching constraint not valid in output operand");
314 /* ??? Before flow, auto inc/dec insns are not supposed to exist,
315 excepting those that expand_call created. So match memory
328 enum constraint_num cn
= lookup_constraint (p
);
329 if (reg_class_for_constraint (cn
) != NO_REGS
330 || insn_extra_address_constraint (cn
))
332 else if (insn_extra_memory_constraint (cn
))
335 insn_extra_constraint_allows_reg_mem (cn
, allows_reg
, allows_mem
);
342 /* Similar, but for input constraints. */
345 parse_input_constraint (const char **constraint_p
, int input_num
,
346 int ninputs
, int noutputs
, int ninout
,
347 const char * const * constraints
,
348 bool *allows_mem
, bool *allows_reg
)
350 const char *constraint
= *constraint_p
;
351 const char *orig_constraint
= constraint
;
352 size_t c_len
= strlen (constraint
);
354 bool saw_match
= false;
356 /* Assume the constraint doesn't allow the use of either
357 a register or memory. */
361 /* Make sure constraint has neither `=', `+', nor '&'. */
363 for (j
= 0; j
< c_len
; j
+= CONSTRAINT_LEN (constraint
[j
], constraint
+j
))
364 switch (constraint
[j
])
366 case '+': case '=': case '&':
367 if (constraint
== orig_constraint
)
369 error ("input operand constraint contains %qc", constraint
[j
]);
375 if (constraint
== orig_constraint
376 && input_num
+ 1 == ninputs
- ninout
)
378 error ("%<%%%> constraint used with last operand");
384 case '?': case '!': case '*': case '#':
386 case 'E': case 'F': case 'G': case 'H':
387 case 's': case 'i': case 'n':
388 case 'I': case 'J': case 'K': case 'L': case 'M':
389 case 'N': case 'O': case 'P': case ',':
392 /* Whether or not a numeric constraint allows a register is
393 decided by the matching constraint, and so there is no need
394 to do anything special with them. We must handle them in
395 the default case, so that we don't unnecessarily force
396 operands to memory. */
397 case '0': case '1': case '2': case '3': case '4':
398 case '5': case '6': case '7': case '8': case '9':
405 match
= strtoul (constraint
+ j
, &end
, 10);
406 if (match
>= (unsigned long) noutputs
)
408 error ("matching constraint references invalid operand number");
412 /* Try and find the real constraint for this dup. Only do this
413 if the matching constraint is the only alternative. */
415 && (j
== 0 || (j
== 1 && constraint
[0] == '%')))
417 constraint
= constraints
[match
];
418 *constraint_p
= constraint
;
419 c_len
= strlen (constraint
);
421 /* ??? At the end of the loop, we will skip the first part of
422 the matched constraint. This assumes not only that the
423 other constraint is an output constraint, but also that
424 the '=' or '+' come first. */
428 j
= end
- constraint
;
429 /* Anticipate increment at end of loop. */
440 if (! ISALPHA (constraint
[j
]))
442 error ("invalid punctuation %qc in constraint", constraint
[j
]);
445 enum constraint_num cn
= lookup_constraint (constraint
+ j
);
446 if (reg_class_for_constraint (cn
) != NO_REGS
447 || insn_extra_address_constraint (cn
))
449 else if (insn_extra_memory_constraint (cn
))
452 insn_extra_constraint_allows_reg_mem (cn
, allows_reg
, allows_mem
);
456 if (saw_match
&& !*allows_reg
)
457 warning (0, "matching constraint does not allow a register");
462 /* Return DECL iff there's an overlap between *REGS and DECL, where DECL
463 can be an asm-declared register. Called via walk_tree. */
466 decl_overlaps_hard_reg_set_p (tree
*declp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
470 const HARD_REG_SET
*const regs
= (const HARD_REG_SET
*) data
;
472 if (TREE_CODE (decl
) == VAR_DECL
)
474 if (DECL_HARD_REGISTER (decl
)
475 && REG_P (DECL_RTL (decl
))
476 && REGNO (DECL_RTL (decl
)) < FIRST_PSEUDO_REGISTER
)
478 rtx reg
= DECL_RTL (decl
);
480 if (overlaps_hard_reg_set_p (*regs
, GET_MODE (reg
), REGNO (reg
)))
485 else if (TYPE_P (decl
) || TREE_CODE (decl
) == PARM_DECL
)
490 /* If there is an overlap between *REGS and DECL, return the first overlap
493 tree_overlaps_hard_reg_set (tree decl
, HARD_REG_SET
*regs
)
495 return walk_tree (&decl
, decl_overlaps_hard_reg_set_p
, regs
, NULL
);
499 /* A subroutine of expand_asm_operands. Check that all operand names
500 are unique. Return true if so. We rely on the fact that these names
501 are identifiers, and so have been canonicalized by get_identifier,
502 so all we need are pointer comparisons. */
505 check_unique_operand_names (tree outputs
, tree inputs
, tree labels
)
507 tree i
, j
, i_name
= NULL_TREE
;
509 for (i
= outputs
; i
; i
= TREE_CHAIN (i
))
511 i_name
= TREE_PURPOSE (TREE_PURPOSE (i
));
515 for (j
= TREE_CHAIN (i
); j
; j
= TREE_CHAIN (j
))
516 if (simple_cst_equal (i_name
, TREE_PURPOSE (TREE_PURPOSE (j
))))
520 for (i
= inputs
; i
; i
= TREE_CHAIN (i
))
522 i_name
= TREE_PURPOSE (TREE_PURPOSE (i
));
526 for (j
= TREE_CHAIN (i
); j
; j
= TREE_CHAIN (j
))
527 if (simple_cst_equal (i_name
, TREE_PURPOSE (TREE_PURPOSE (j
))))
529 for (j
= outputs
; j
; j
= TREE_CHAIN (j
))
530 if (simple_cst_equal (i_name
, TREE_PURPOSE (TREE_PURPOSE (j
))))
534 for (i
= labels
; i
; i
= TREE_CHAIN (i
))
536 i_name
= TREE_PURPOSE (i
);
540 for (j
= TREE_CHAIN (i
); j
; j
= TREE_CHAIN (j
))
541 if (simple_cst_equal (i_name
, TREE_PURPOSE (j
)))
543 for (j
= inputs
; j
; j
= TREE_CHAIN (j
))
544 if (simple_cst_equal (i_name
, TREE_PURPOSE (TREE_PURPOSE (j
))))
551 error ("duplicate asm operand name %qs", TREE_STRING_POINTER (i_name
));
555 /* Resolve the names of the operands in *POUTPUTS and *PINPUTS to numbers,
556 and replace the name expansions in STRING and in the constraints to
557 those numbers. This is generally done in the front end while creating
558 the ASM_EXPR generic tree that eventually becomes the GIMPLE_ASM. */
561 resolve_asm_operand_names (tree string
, tree outputs
, tree inputs
, tree labels
)
568 check_unique_operand_names (outputs
, inputs
, labels
);
570 /* Substitute [<name>] in input constraint strings. There should be no
571 named operands in output constraints. */
572 for (t
= inputs
; t
; t
= TREE_CHAIN (t
))
574 c
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t
)));
575 if (strchr (c
, '[') != NULL
)
577 p
= buffer
= xstrdup (c
);
578 while ((p
= strchr (p
, '[')) != NULL
)
579 p
= resolve_operand_name_1 (p
, outputs
, inputs
, NULL
);
580 TREE_VALUE (TREE_PURPOSE (t
))
581 = build_string (strlen (buffer
), buffer
);
586 /* Now check for any needed substitutions in the template. */
587 c
= TREE_STRING_POINTER (string
);
588 while ((c
= strchr (c
, '%')) != NULL
)
592 else if (ISALPHA (c
[1]) && c
[2] == '[')
596 c
+= 1 + (c
[1] == '%');
603 /* OK, we need to make a copy so we can perform the substitutions.
604 Assume that we will not need extra space--we get to remove '['
605 and ']', which means we cannot have a problem until we have more
606 than 999 operands. */
607 buffer
= xstrdup (TREE_STRING_POINTER (string
));
608 p
= buffer
+ (c
- TREE_STRING_POINTER (string
));
610 while ((p
= strchr (p
, '%')) != NULL
)
614 else if (ISALPHA (p
[1]) && p
[2] == '[')
618 p
+= 1 + (p
[1] == '%');
622 p
= resolve_operand_name_1 (p
, outputs
, inputs
, labels
);
625 string
= build_string (strlen (buffer
), buffer
);
632 /* A subroutine of resolve_operand_names. P points to the '[' for a
633 potential named operand of the form [<name>]. In place, replace
634 the name and brackets with a number. Return a pointer to the
635 balance of the string after substitution. */
638 resolve_operand_name_1 (char *p
, tree outputs
, tree inputs
, tree labels
)
644 /* Collect the operand name. */
645 q
= strchr (++p
, ']');
648 error ("missing close brace for named operand");
649 return strchr (p
, '\0');
653 /* Resolve the name to a number. */
654 for (op
= 0, t
= outputs
; t
; t
= TREE_CHAIN (t
), op
++)
656 tree name
= TREE_PURPOSE (TREE_PURPOSE (t
));
657 if (name
&& strcmp (TREE_STRING_POINTER (name
), p
) == 0)
660 for (t
= inputs
; t
; t
= TREE_CHAIN (t
), op
++)
662 tree name
= TREE_PURPOSE (TREE_PURPOSE (t
));
663 if (name
&& strcmp (TREE_STRING_POINTER (name
), p
) == 0)
666 for (t
= labels
; t
; t
= TREE_CHAIN (t
), op
++)
668 tree name
= TREE_PURPOSE (t
);
669 if (name
&& strcmp (TREE_STRING_POINTER (name
), p
) == 0)
673 error ("undefined named operand %qs", identifier_to_locale (p
));
677 /* Replace the name with the number. Unfortunately, not all libraries
678 get the return value of sprintf correct, so search for the end of the
679 generated string by hand. */
680 sprintf (--p
, "%d", op
);
681 p
= strchr (p
, '\0');
683 /* Verify the no extra buffer space assumption. */
686 /* Shift the rest of the buffer down to fill the gap. */
687 memmove (p
, q
+ 1, strlen (q
+ 1) + 1);
693 /* Generate RTL to return directly from the current function.
694 (That is, we bypass any return value.) */
697 expand_naked_return (void)
699 rtx_code_label
*end_label
;
701 clear_pending_stack_adjust ();
702 do_pending_stack_adjust ();
704 end_label
= naked_return_label
;
706 end_label
= naked_return_label
= gen_label_rtx ();
708 emit_jump (end_label
);
711 /* Generate code to jump to LABEL if OP0 and OP1 are equal in mode MODE. PROB
712 is the probability of jumping to LABEL. */
714 do_jump_if_equal (machine_mode mode
, rtx op0
, rtx op1
, rtx_code_label
*label
,
715 int unsignedp
, int prob
)
717 gcc_assert (prob
<= REG_BR_PROB_BASE
);
718 do_compare_rtx_and_jump (op0
, op1
, EQ
, unsignedp
, mode
,
719 NULL_RTX
, NULL
, label
, prob
);
722 /* Do the insertion of a case label into case_list. The labels are
723 fed to us in descending order from the sorted vector of case labels used
724 in the tree part of the middle end. So the list we construct is
725 sorted in ascending order.
727 LABEL is the case label to be inserted. LOW and HIGH are the bounds
728 against which the index is compared to jump to LABEL and PROB is the
729 estimated probability LABEL is reached from the switch statement. */
731 static struct case_node
*
732 add_case_node (struct case_node
*head
, tree low
, tree high
,
733 tree label
, int prob
,
734 object_allocator
<case_node
> &case_node_pool
)
738 gcc_checking_assert (low
);
739 gcc_checking_assert (high
&& (TREE_TYPE (low
) == TREE_TYPE (high
)));
741 /* Add this label to the chain. */
742 r
= case_node_pool
.allocate ();
745 r
->code_label
= label
;
746 r
->parent
= r
->left
= NULL
;
748 r
->subtree_prob
= prob
;
753 /* Dump ROOT, a list or tree of case nodes, to file. */
756 dump_case_nodes (FILE *f
, struct case_node
*root
,
757 int indent_step
, int indent_level
)
763 dump_case_nodes (f
, root
->left
, indent_step
, indent_level
);
766 fprintf (f
, "%*s", indent_step
* indent_level
, "");
767 print_dec (root
->low
, f
, TYPE_SIGN (TREE_TYPE (root
->low
)));
768 if (!tree_int_cst_equal (root
->low
, root
->high
))
770 fprintf (f
, " ... ");
771 print_dec (root
->high
, f
, TYPE_SIGN (TREE_TYPE (root
->high
)));
775 dump_case_nodes (f
, root
->right
, indent_step
, indent_level
);
778 /* Return the smallest number of different values for which it is best to use a
779 jump-table instead of a tree of conditional branches. */
782 case_values_threshold (void)
784 unsigned int threshold
= PARAM_VALUE (PARAM_CASE_VALUES_THRESHOLD
);
787 threshold
= targetm
.case_values_threshold ();
792 /* Return true if a switch should be expanded as a decision tree.
793 RANGE is the difference between highest and lowest case.
794 UNIQ is number of unique case node targets, not counting the default case.
795 COUNT is the number of comparisons needed, not counting the default case. */
798 expand_switch_as_decision_tree_p (tree range
,
799 unsigned int uniq ATTRIBUTE_UNUSED
,
804 /* If neither casesi or tablejump is available, or flag_jump_tables
805 over-ruled us, we really have no choice. */
806 if (!targetm
.have_casesi () && !targetm
.have_tablejump ())
808 if (!flag_jump_tables
)
810 #ifndef ASM_OUTPUT_ADDR_DIFF_ELT
815 /* If the switch is relatively small such that the cost of one
816 indirect jump on the target are higher than the cost of a
817 decision tree, go with the decision tree.
819 If range of values is much bigger than number of values,
820 or if it is too large to represent in a HOST_WIDE_INT,
821 make a sequence of conditional branches instead of a dispatch.
823 The definition of "much bigger" depends on whether we are
824 optimizing for size or for speed. If the former, the maximum
825 ratio range/count = 3, because this was found to be the optimal
826 ratio for size on i686-pc-linux-gnu, see PR11823. The ratio
827 10 is much older, and was probably selected after an extensive
828 benchmarking investigation on numerous platforms. Or maybe it
829 just made sense to someone at some point in the history of GCC,
831 max_ratio
= optimize_insn_for_size_p () ? 3 : 10;
832 if (count
< case_values_threshold ()
833 || ! tree_fits_uhwi_p (range
)
834 || compare_tree_int (range
, max_ratio
* count
) > 0)
840 /* Generate a decision tree, switching on INDEX_EXPR and jumping to
841 one of the labels in CASE_LIST or to the DEFAULT_LABEL.
842 DEFAULT_PROB is the estimated probability that it jumps to
845 We generate a binary decision tree to select the appropriate target
846 code. This is done as follows:
848 If the index is a short or char that we do not have
849 an insn to handle comparisons directly, convert it to
850 a full integer now, rather than letting each comparison
851 generate the conversion.
853 Load the index into a register.
855 The list of cases is rearranged into a binary tree,
856 nearly optimal assuming equal probability for each case.
858 The tree is transformed into RTL, eliminating redundant
859 test conditions at the same time.
861 If program flow could reach the end of the decision tree
862 an unconditional jump to the default code is emitted.
864 The above process is unaware of the CFG. The caller has to fix up
865 the CFG itself. This is done in cfgexpand.c. */
868 emit_case_decision_tree (tree index_expr
, tree index_type
,
869 case_node_ptr case_list
, rtx_code_label
*default_label
,
872 rtx index
= expand_normal (index_expr
);
874 if (GET_MODE_CLASS (GET_MODE (index
)) == MODE_INT
875 && ! have_insn_for (COMPARE
, GET_MODE (index
)))
877 int unsignedp
= TYPE_UNSIGNED (index_type
);
878 machine_mode wider_mode
;
879 for (wider_mode
= GET_MODE (index
); wider_mode
!= VOIDmode
;
880 wider_mode
= GET_MODE_WIDER_MODE (wider_mode
))
881 if (have_insn_for (COMPARE
, wider_mode
))
883 index
= convert_to_mode (wider_mode
, index
, unsignedp
);
888 do_pending_stack_adjust ();
892 index
= copy_to_reg (index
);
893 if (TREE_CODE (index_expr
) == SSA_NAME
)
894 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (index_expr
), index
);
897 balance_case_nodes (&case_list
, NULL
);
899 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
901 int indent_step
= ceil_log2 (TYPE_PRECISION (index_type
)) + 2;
902 fprintf (dump_file
, ";; Expanding GIMPLE switch as decision tree:\n");
903 dump_case_nodes (dump_file
, case_list
, indent_step
, 0);
906 emit_case_nodes (index
, case_list
, default_label
, default_prob
, index_type
);
908 emit_jump (default_label
);
911 /* Return the sum of probabilities of outgoing edges of basic block BB. */
914 get_outgoing_edge_probs (basic_block bb
)
921 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
922 prob_sum
+= e
->probability
;
926 /* Computes the conditional probability of jumping to a target if the branch
927 instruction is executed.
928 TARGET_PROB is the estimated probability of jumping to a target relative
929 to some basic block BB.
930 BASE_PROB is the probability of reaching the branch instruction relative
931 to the same basic block BB. */
934 conditional_probability (int target_prob
, int base_prob
)
938 gcc_assert (target_prob
>= 0);
939 gcc_assert (target_prob
<= base_prob
);
940 return GCOV_COMPUTE_SCALE (target_prob
, base_prob
);
945 /* Generate a dispatch tabler, switching on INDEX_EXPR and jumping to
946 one of the labels in CASE_LIST or to the DEFAULT_LABEL.
947 MINVAL, MAXVAL, and RANGE are the extrema and range of the case
948 labels in CASE_LIST. STMT_BB is the basic block containing the statement.
950 First, a jump insn is emitted. First we try "casesi". If that
951 fails, try "tablejump". A target *must* have one of them (or both).
953 Then, a table with the target labels is emitted.
955 The process is unaware of the CFG. The caller has to fix up
956 the CFG itself. This is done in cfgexpand.c. */
959 emit_case_dispatch_table (tree index_expr
, tree index_type
,
960 struct case_node
*case_list
, rtx default_label
,
961 tree minval
, tree maxval
, tree range
,
967 rtx_insn
*fallback_label
= label_rtx (case_list
->code_label
);
968 rtx_code_label
*table_label
= gen_label_rtx ();
969 bool has_gaps
= false;
970 edge default_edge
= stmt_bb
? EDGE_SUCC (stmt_bb
, 0) : NULL
;
971 int default_prob
= default_edge
? default_edge
->probability
: 0;
972 int base
= get_outgoing_edge_probs (stmt_bb
);
973 bool try_with_tablejump
= false;
975 int new_default_prob
= conditional_probability (default_prob
,
978 if (! try_casesi (index_type
, index_expr
, minval
, range
,
979 table_label
, default_label
, fallback_label
,
982 /* Index jumptables from zero for suitable values of minval to avoid
983 a subtraction. For the rationale see:
984 "http://gcc.gnu.org/ml/gcc-patches/2001-10/msg01234.html". */
985 if (optimize_insn_for_speed_p ()
986 && compare_tree_int (minval
, 0) > 0
987 && compare_tree_int (minval
, 3) < 0)
989 minval
= build_int_cst (index_type
, 0);
993 try_with_tablejump
= true;
996 /* Get table of labels to jump to, in order of case index. */
998 ncases
= tree_to_shwi (range
) + 1;
999 labelvec
= XALLOCAVEC (rtx
, ncases
);
1000 memset (labelvec
, 0, ncases
* sizeof (rtx
));
1002 for (n
= case_list
; n
; n
= n
->right
)
1004 /* Compute the low and high bounds relative to the minimum
1005 value since that should fit in a HOST_WIDE_INT while the
1006 actual values may not. */
1008 = tree_to_uhwi (fold_build2 (MINUS_EXPR
, index_type
,
1010 HOST_WIDE_INT i_high
1011 = tree_to_uhwi (fold_build2 (MINUS_EXPR
, index_type
,
1015 for (i
= i_low
; i
<= i_high
; i
++)
1017 = gen_rtx_LABEL_REF (Pmode
, label_rtx (n
->code_label
));
1020 /* Fill in the gaps with the default. We may have gaps at
1021 the beginning if we tried to avoid the minval subtraction,
1022 so substitute some label even if the default label was
1023 deemed unreachable. */
1025 default_label
= fallback_label
;
1026 for (i
= 0; i
< ncases
; i
++)
1027 if (labelvec
[i
] == 0)
1030 labelvec
[i
] = gen_rtx_LABEL_REF (Pmode
, default_label
);
1035 /* There is at least one entry in the jump table that jumps
1036 to default label. The default label can either be reached
1037 through the indirect jump or the direct conditional jump
1038 before that. Split the probability of reaching the
1039 default label among these two jumps. */
1040 new_default_prob
= conditional_probability (default_prob
/2,
1043 base
-= default_prob
;
1047 base
-= default_prob
;
1052 default_edge
->probability
= default_prob
;
1054 /* We have altered the probability of the default edge. So the probabilities
1055 of all other edges need to be adjusted so that it sums up to
1056 REG_BR_PROB_BASE. */
1061 FOR_EACH_EDGE (e
, ei
, stmt_bb
->succs
)
1062 e
->probability
= GCOV_COMPUTE_SCALE (e
->probability
, base
);
1065 if (try_with_tablejump
)
1067 bool ok
= try_tablejump (index_type
, index_expr
, minval
, range
,
1068 table_label
, default_label
, new_default_prob
);
1071 /* Output the table. */
1072 emit_label (table_label
);
1074 if (CASE_VECTOR_PC_RELATIVE
|| flag_pic
)
1075 emit_jump_table_data (gen_rtx_ADDR_DIFF_VEC (CASE_VECTOR_MODE
,
1076 gen_rtx_LABEL_REF (Pmode
,
1078 gen_rtvec_v (ncases
, labelvec
),
1079 const0_rtx
, const0_rtx
));
1081 emit_jump_table_data (gen_rtx_ADDR_VEC (CASE_VECTOR_MODE
,
1082 gen_rtvec_v (ncases
, labelvec
)));
1084 /* Record no drop-through after the table. */
1088 /* Reset the aux field of all outgoing edges of basic block BB. */
1091 reset_out_edges_aux (basic_block bb
)
1095 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1099 /* Compute the number of case labels that correspond to each outgoing edge of
1100 STMT. Record this information in the aux field of the edge. */
1103 compute_cases_per_edge (gswitch
*stmt
)
1105 basic_block bb
= gimple_bb (stmt
);
1106 reset_out_edges_aux (bb
);
1107 int ncases
= gimple_switch_num_labels (stmt
);
1108 for (int i
= ncases
- 1; i
>= 1; --i
)
1110 tree elt
= gimple_switch_label (stmt
, i
);
1111 tree lab
= CASE_LABEL (elt
);
1112 basic_block case_bb
= label_to_block_fn (cfun
, lab
);
1113 edge case_edge
= find_edge (bb
, case_bb
);
1114 case_edge
->aux
= (void *)((intptr_t)(case_edge
->aux
) + 1);
1118 /* Terminate a case (Pascal/Ada) or switch (C) statement
1119 in which ORIG_INDEX is the expression to be tested.
1120 If ORIG_TYPE is not NULL, it is the original ORIG_INDEX
1121 type as given in the source before any compiler conversions.
1122 Generate the code to test it and jump to the right place. */
1125 expand_case (gswitch
*stmt
)
1127 tree minval
= NULL_TREE
, maxval
= NULL_TREE
, range
= NULL_TREE
;
1128 rtx_code_label
*default_label
= NULL
;
1129 unsigned int count
, uniq
;
1131 int ncases
= gimple_switch_num_labels (stmt
);
1132 tree index_expr
= gimple_switch_index (stmt
);
1133 tree index_type
= TREE_TYPE (index_expr
);
1135 basic_block bb
= gimple_bb (stmt
);
1137 /* A list of case labels; it is first built as a list and it may then
1138 be rearranged into a nearly balanced binary tree. */
1139 struct case_node
*case_list
= 0;
1141 /* A pool for case nodes. */
1142 object_allocator
<case_node
> case_node_pool ("struct case_node pool", 100);
1144 /* An ERROR_MARK occurs for various reasons including invalid data type.
1145 ??? Can this still happen, with GIMPLE and all? */
1146 if (index_type
== error_mark_node
)
1149 /* cleanup_tree_cfg removes all SWITCH_EXPR with their index
1150 expressions being INTEGER_CST. */
1151 gcc_assert (TREE_CODE (index_expr
) != INTEGER_CST
);
1154 do_pending_stack_adjust ();
1156 /* Find the default case target label. */
1157 default_label
= jump_target_rtx
1158 (CASE_LABEL (gimple_switch_default_label (stmt
)));
1159 edge default_edge
= EDGE_SUCC (bb
, 0);
1160 int default_prob
= default_edge
->probability
;
1162 /* Get upper and lower bounds of case values. */
1163 elt
= gimple_switch_label (stmt
, 1);
1164 minval
= fold_convert (index_type
, CASE_LOW (elt
));
1165 elt
= gimple_switch_label (stmt
, ncases
- 1);
1166 if (CASE_HIGH (elt
))
1167 maxval
= fold_convert (index_type
, CASE_HIGH (elt
));
1169 maxval
= fold_convert (index_type
, CASE_LOW (elt
));
1171 /* Compute span of values. */
1172 range
= fold_build2 (MINUS_EXPR
, index_type
, maxval
, minval
);
1174 /* Listify the labels queue and gather some numbers to decide
1175 how to expand this switch(). */
1178 hash_set
<tree
> seen_labels
;
1179 compute_cases_per_edge (stmt
);
1181 for (i
= ncases
- 1; i
>= 1; --i
)
1183 elt
= gimple_switch_label (stmt
, i
);
1184 tree low
= CASE_LOW (elt
);
1186 tree high
= CASE_HIGH (elt
);
1187 gcc_assert (! high
|| tree_int_cst_lt (low
, high
));
1188 tree lab
= CASE_LABEL (elt
);
1190 /* Count the elements.
1191 A range counts double, since it requires two compares. */
1196 /* If we have not seen this label yet, then increase the
1197 number of unique case node targets seen. */
1198 if (!seen_labels
.add (lab
))
1201 /* The bounds on the case range, LOW and HIGH, have to be converted
1202 to case's index type TYPE. Note that the original type of the
1203 case index in the source code is usually "lost" during
1204 gimplification due to type promotion, but the case labels retain the
1205 original type. Make sure to drop overflow flags. */
1206 low
= fold_convert (index_type
, low
);
1207 if (TREE_OVERFLOW (low
))
1208 low
= wide_int_to_tree (index_type
, low
);
1210 /* The canonical from of a case label in GIMPLE is that a simple case
1211 has an empty CASE_HIGH. For the casesi and tablejump expanders,
1212 the back ends want simple cases to have high == low. */
1215 high
= fold_convert (index_type
, high
);
1216 if (TREE_OVERFLOW (high
))
1217 high
= wide_int_to_tree (index_type
, high
);
1219 basic_block case_bb
= label_to_block_fn (cfun
, lab
);
1220 edge case_edge
= find_edge (bb
, case_bb
);
1221 case_list
= add_case_node (
1222 case_list
, low
, high
, lab
,
1223 case_edge
->probability
/ (intptr_t)(case_edge
->aux
),
1226 reset_out_edges_aux (bb
);
1228 /* cleanup_tree_cfg removes all SWITCH_EXPR with a single
1229 destination, such as one with a default case only.
1230 It also removes cases that are out of range for the switch
1231 type, so we should never get a zero here. */
1232 gcc_assert (count
> 0);
1234 rtx_insn
*before_case
= get_last_insn ();
1236 /* Decide how to expand this switch.
1237 The two options at this point are a dispatch table (casesi or
1238 tablejump) or a decision tree. */
1240 if (expand_switch_as_decision_tree_p (range
, uniq
, count
))
1241 emit_case_decision_tree (index_expr
, index_type
,
1242 case_list
, default_label
,
1245 emit_case_dispatch_table (index_expr
, index_type
,
1246 case_list
, default_label
,
1247 minval
, maxval
, range
, bb
);
1249 reorder_insns (NEXT_INSN (before_case
), get_last_insn (), before_case
);
1254 /* Expand the dispatch to a short decrement chain if there are few cases
1255 to dispatch to. Likewise if neither casesi nor tablejump is available,
1256 or if flag_jump_tables is set. Otherwise, expand as a casesi or a
1257 tablejump. The index mode is always the mode of integer_type_node.
1258 Trap if no case matches the index.
1260 DISPATCH_INDEX is the index expression to switch on. It should be a
1261 memory or register operand.
1263 DISPATCH_TABLE is a set of case labels. The set should be sorted in
1264 ascending order, be contiguous, starting with value 0, and contain only
1265 single-valued case labels. */
1268 expand_sjlj_dispatch_table (rtx dispatch_index
,
1269 vec
<tree
> dispatch_table
)
1271 tree index_type
= integer_type_node
;
1272 machine_mode index_mode
= TYPE_MODE (index_type
);
1274 int ncases
= dispatch_table
.length ();
1276 do_pending_stack_adjust ();
1277 rtx_insn
*before_case
= get_last_insn ();
1279 /* Expand as a decrement-chain if there are 5 or fewer dispatch
1280 labels. This covers more than 98% of the cases in libjava,
1281 and seems to be a reasonable compromise between the "old way"
1282 of expanding as a decision tree or dispatch table vs. the "new
1283 way" with decrement chain or dispatch table. */
1284 if (dispatch_table
.length () <= 5
1285 || (!targetm
.have_casesi () && !targetm
.have_tablejump ())
1286 || !flag_jump_tables
)
1288 /* Expand the dispatch as a decrement chain:
1290 "switch(index) {case 0: do_0; case 1: do_1; ...; case N: do_N;}"
1294 if (index == 0) do_0; else index--;
1295 if (index == 0) do_1; else index--;
1297 if (index == 0) do_N; else index--;
1299 This is more efficient than a dispatch table on most machines.
1300 The last "index--" is redundant but the code is trivially dead
1301 and will be cleaned up by later passes. */
1302 rtx index
= copy_to_mode_reg (index_mode
, dispatch_index
);
1303 rtx zero
= CONST0_RTX (index_mode
);
1304 for (int i
= 0; i
< ncases
; i
++)
1306 tree elt
= dispatch_table
[i
];
1307 rtx_code_label
*lab
= jump_target_rtx (CASE_LABEL (elt
));
1308 do_jump_if_equal (index_mode
, index
, zero
, lab
, 0, -1);
1309 force_expand_binop (index_mode
, sub_optab
,
1310 index
, CONST1_RTX (index_mode
),
1311 index
, 0, OPTAB_DIRECT
);
1316 /* Similar to expand_case, but much simpler. */
1317 struct case_node
*case_list
= 0;
1318 object_allocator
<case_node
> case_node_pool ("struct sjlj_case pool",
1320 tree index_expr
= make_tree (index_type
, dispatch_index
);
1321 tree minval
= build_int_cst (index_type
, 0);
1322 tree maxval
= CASE_LOW (dispatch_table
.last ());
1323 tree range
= maxval
;
1324 rtx_code_label
*default_label
= gen_label_rtx ();
1326 for (int i
= ncases
- 1; i
>= 0; --i
)
1328 tree elt
= dispatch_table
[i
];
1329 tree low
= CASE_LOW (elt
);
1330 tree lab
= CASE_LABEL (elt
);
1331 case_list
= add_case_node (case_list
, low
, low
, lab
, 0, case_node_pool
);
1334 emit_case_dispatch_table (index_expr
, index_type
,
1335 case_list
, default_label
,
1336 minval
, maxval
, range
,
1337 BLOCK_FOR_INSN (before_case
));
1338 emit_label (default_label
);
1341 /* Dispatching something not handled? Trap! */
1342 expand_builtin_trap ();
1344 reorder_insns (NEXT_INSN (before_case
), get_last_insn (), before_case
);
1350 /* Take an ordered list of case nodes
1351 and transform them into a near optimal binary tree,
1352 on the assumption that any target code selection value is as
1353 likely as any other.
1355 The transformation is performed by splitting the ordered
1356 list into two equal sections plus a pivot. The parts are
1357 then attached to the pivot as left and right branches. Each
1358 branch is then transformed recursively. */
1361 balance_case_nodes (case_node_ptr
*head
, case_node_ptr parent
)
1373 /* Count the number of entries on branch. Also count the ranges. */
1377 if (!tree_int_cst_equal (np
->low
, np
->high
))
1386 /* Split this list if it is long enough for that to help. */
1390 /* If there are just three nodes, split at the middle one. */
1392 npp
= &(*npp
)->right
;
1395 /* Find the place in the list that bisects the list's total cost,
1396 where ranges count as 2.
1397 Here I gets half the total cost. */
1398 i
= (i
+ ranges
+ 1) / 2;
1401 /* Skip nodes while their cost does not reach that amount. */
1402 if (!tree_int_cst_equal ((*npp
)->low
, (*npp
)->high
))
1407 npp
= &(*npp
)->right
;
1412 np
->parent
= parent
;
1415 /* Optimize each of the two split parts. */
1416 balance_case_nodes (&np
->left
, np
);
1417 balance_case_nodes (&np
->right
, np
);
1418 np
->subtree_prob
= np
->prob
;
1419 np
->subtree_prob
+= np
->left
->subtree_prob
;
1420 np
->subtree_prob
+= np
->right
->subtree_prob
;
1424 /* Else leave this branch as one level,
1425 but fill in `parent' fields. */
1427 np
->parent
= parent
;
1428 np
->subtree_prob
= np
->prob
;
1429 for (; np
->right
; np
= np
->right
)
1431 np
->right
->parent
= np
;
1432 (*head
)->subtree_prob
+= np
->right
->subtree_prob
;
1438 /* Search the parent sections of the case node tree
1439 to see if a test for the lower bound of NODE would be redundant.
1440 INDEX_TYPE is the type of the index expression.
1442 The instructions to generate the case decision tree are
1443 output in the same order as nodes are processed so it is
1444 known that if a parent node checks the range of the current
1445 node minus one that the current node is bounded at its lower
1446 span. Thus the test would be redundant. */
1449 node_has_low_bound (case_node_ptr node
, tree index_type
)
1452 case_node_ptr pnode
;
1454 /* If the lower bound of this node is the lowest value in the index type,
1455 we need not test it. */
1457 if (tree_int_cst_equal (node
->low
, TYPE_MIN_VALUE (index_type
)))
1460 /* If this node has a left branch, the value at the left must be less
1461 than that at this node, so it cannot be bounded at the bottom and
1462 we need not bother testing any further. */
1467 low_minus_one
= fold_build2 (MINUS_EXPR
, TREE_TYPE (node
->low
),
1469 build_int_cst (TREE_TYPE (node
->low
), 1));
1471 /* If the subtraction above overflowed, we can't verify anything.
1472 Otherwise, look for a parent that tests our value - 1. */
1474 if (! tree_int_cst_lt (low_minus_one
, node
->low
))
1477 for (pnode
= node
->parent
; pnode
; pnode
= pnode
->parent
)
1478 if (tree_int_cst_equal (low_minus_one
, pnode
->high
))
1484 /* Search the parent sections of the case node tree
1485 to see if a test for the upper bound of NODE would be redundant.
1486 INDEX_TYPE is the type of the index expression.
1488 The instructions to generate the case decision tree are
1489 output in the same order as nodes are processed so it is
1490 known that if a parent node checks the range of the current
1491 node plus one that the current node is bounded at its upper
1492 span. Thus the test would be redundant. */
1495 node_has_high_bound (case_node_ptr node
, tree index_type
)
1498 case_node_ptr pnode
;
1500 /* If there is no upper bound, obviously no test is needed. */
1502 if (TYPE_MAX_VALUE (index_type
) == NULL
)
1505 /* If the upper bound of this node is the highest value in the type
1506 of the index expression, we need not test against it. */
1508 if (tree_int_cst_equal (node
->high
, TYPE_MAX_VALUE (index_type
)))
1511 /* If this node has a right branch, the value at the right must be greater
1512 than that at this node, so it cannot be bounded at the top and
1513 we need not bother testing any further. */
1518 high_plus_one
= fold_build2 (PLUS_EXPR
, TREE_TYPE (node
->high
),
1520 build_int_cst (TREE_TYPE (node
->high
), 1));
1522 /* If the addition above overflowed, we can't verify anything.
1523 Otherwise, look for a parent that tests our value + 1. */
1525 if (! tree_int_cst_lt (node
->high
, high_plus_one
))
1528 for (pnode
= node
->parent
; pnode
; pnode
= pnode
->parent
)
1529 if (tree_int_cst_equal (high_plus_one
, pnode
->low
))
1535 /* Search the parent sections of the
1536 case node tree to see if both tests for the upper and lower
1537 bounds of NODE would be redundant. */
1540 node_is_bounded (case_node_ptr node
, tree index_type
)
1542 return (node_has_low_bound (node
, index_type
)
1543 && node_has_high_bound (node
, index_type
));
1547 /* Emit step-by-step code to select a case for the value of INDEX.
1548 The thus generated decision tree follows the form of the
1549 case-node binary tree NODE, whose nodes represent test conditions.
1550 INDEX_TYPE is the type of the index of the switch.
1552 Care is taken to prune redundant tests from the decision tree
1553 by detecting any boundary conditions already checked by
1554 emitted rtx. (See node_has_high_bound, node_has_low_bound
1555 and node_is_bounded, above.)
1557 Where the test conditions can be shown to be redundant we emit
1558 an unconditional jump to the target code. As a further
1559 optimization, the subordinates of a tree node are examined to
1560 check for bounded nodes. In this case conditional and/or
1561 unconditional jumps as a result of the boundary check for the
1562 current node are arranged to target the subordinates associated
1563 code for out of bound conditions on the current node.
1565 We can assume that when control reaches the code generated here,
1566 the index value has already been compared with the parents
1567 of this node, and determined to be on the same side of each parent
1568 as this node is. Thus, if this node tests for the value 51,
1569 and a parent tested for 52, we don't need to consider
1570 the possibility of a value greater than 51. If another parent
1571 tests for the value 50, then this node need not test anything. */
1574 emit_case_nodes (rtx index
, case_node_ptr node
, rtx_code_label
*default_label
,
1575 int default_prob
, tree index_type
)
1577 /* If INDEX has an unsigned type, we must make unsigned branches. */
1578 int unsignedp
= TYPE_UNSIGNED (index_type
);
1580 int prob
= node
->prob
, subtree_prob
= node
->subtree_prob
;
1581 machine_mode mode
= GET_MODE (index
);
1582 machine_mode imode
= TYPE_MODE (index_type
);
1584 /* Handle indices detected as constant during RTL expansion. */
1585 if (mode
== VOIDmode
)
1588 /* See if our parents have already tested everything for us.
1589 If they have, emit an unconditional jump for this node. */
1590 if (node_is_bounded (node
, index_type
))
1591 emit_jump (label_rtx (node
->code_label
));
1593 else if (tree_int_cst_equal (node
->low
, node
->high
))
1595 probability
= conditional_probability (prob
, subtree_prob
+ default_prob
);
1596 /* Node is single valued. First see if the index expression matches
1597 this node and then check our children, if any. */
1598 do_jump_if_equal (mode
, index
,
1599 convert_modes (mode
, imode
,
1600 expand_normal (node
->low
),
1602 jump_target_rtx (node
->code_label
),
1603 unsignedp
, probability
);
1604 /* Since this case is taken at this point, reduce its weight from
1606 subtree_prob
-= prob
;
1607 if (node
->right
!= 0 && node
->left
!= 0)
1609 /* This node has children on both sides.
1610 Dispatch to one side or the other
1611 by comparing the index value with this node's value.
1612 If one subtree is bounded, check that one first,
1613 so we can avoid real branches in the tree. */
1615 if (node_is_bounded (node
->right
, index_type
))
1617 probability
= conditional_probability (
1619 subtree_prob
+ default_prob
);
1620 emit_cmp_and_jump_insns (index
,
1623 expand_normal (node
->high
),
1625 GT
, NULL_RTX
, mode
, unsignedp
,
1626 label_rtx (node
->right
->code_label
),
1628 emit_case_nodes (index
, node
->left
, default_label
, default_prob
,
1632 else if (node_is_bounded (node
->left
, index_type
))
1634 probability
= conditional_probability (
1636 subtree_prob
+ default_prob
);
1637 emit_cmp_and_jump_insns (index
,
1640 expand_normal (node
->high
),
1642 LT
, NULL_RTX
, mode
, unsignedp
,
1643 label_rtx (node
->left
->code_label
),
1645 emit_case_nodes (index
, node
->right
, default_label
, default_prob
,
1649 /* If both children are single-valued cases with no
1650 children, finish up all the work. This way, we can save
1651 one ordered comparison. */
1652 else if (tree_int_cst_equal (node
->right
->low
, node
->right
->high
)
1653 && node
->right
->left
== 0
1654 && node
->right
->right
== 0
1655 && tree_int_cst_equal (node
->left
->low
, node
->left
->high
)
1656 && node
->left
->left
== 0
1657 && node
->left
->right
== 0)
1659 /* Neither node is bounded. First distinguish the two sides;
1660 then emit the code for one side at a time. */
1662 /* See if the value matches what the right hand side
1664 probability
= conditional_probability (
1666 subtree_prob
+ default_prob
);
1667 do_jump_if_equal (mode
, index
,
1668 convert_modes (mode
, imode
,
1669 expand_normal (node
->right
->low
),
1671 jump_target_rtx (node
->right
->code_label
),
1672 unsignedp
, probability
);
1674 /* See if the value matches what the left hand side
1676 probability
= conditional_probability (
1678 subtree_prob
+ default_prob
);
1679 do_jump_if_equal (mode
, index
,
1680 convert_modes (mode
, imode
,
1681 expand_normal (node
->left
->low
),
1683 jump_target_rtx (node
->left
->code_label
),
1684 unsignedp
, probability
);
1689 /* Neither node is bounded. First distinguish the two sides;
1690 then emit the code for one side at a time. */
1693 = build_decl (curr_insn_location (),
1694 LABEL_DECL
, NULL_TREE
, void_type_node
);
1696 /* The default label could be reached either through the right
1697 subtree or the left subtree. Divide the probability
1699 probability
= conditional_probability (
1700 node
->right
->subtree_prob
+ default_prob
/2,
1701 subtree_prob
+ default_prob
);
1702 /* See if the value is on the right. */
1703 emit_cmp_and_jump_insns (index
,
1706 expand_normal (node
->high
),
1708 GT
, NULL_RTX
, mode
, unsignedp
,
1709 label_rtx (test_label
),
1713 /* Value must be on the left.
1714 Handle the left-hand subtree. */
1715 emit_case_nodes (index
, node
->left
, default_label
, default_prob
, index_type
);
1716 /* If left-hand subtree does nothing,
1719 emit_jump (default_label
);
1721 /* Code branches here for the right-hand subtree. */
1722 expand_label (test_label
);
1723 emit_case_nodes (index
, node
->right
, default_label
, default_prob
, index_type
);
1727 else if (node
->right
!= 0 && node
->left
== 0)
1729 /* Here we have a right child but no left so we issue a conditional
1730 branch to default and process the right child.
1732 Omit the conditional branch to default if the right child
1733 does not have any children and is single valued; it would
1734 cost too much space to save so little time. */
1736 if (node
->right
->right
|| node
->right
->left
1737 || !tree_int_cst_equal (node
->right
->low
, node
->right
->high
))
1739 if (!node_has_low_bound (node
, index_type
))
1741 probability
= conditional_probability (
1743 subtree_prob
+ default_prob
);
1744 emit_cmp_and_jump_insns (index
,
1747 expand_normal (node
->high
),
1749 LT
, NULL_RTX
, mode
, unsignedp
,
1755 emit_case_nodes (index
, node
->right
, default_label
, default_prob
, index_type
);
1759 probability
= conditional_probability (
1760 node
->right
->subtree_prob
,
1761 subtree_prob
+ default_prob
);
1762 /* We cannot process node->right normally
1763 since we haven't ruled out the numbers less than
1764 this node's value. So handle node->right explicitly. */
1765 do_jump_if_equal (mode
, index
,
1768 expand_normal (node
->right
->low
),
1770 jump_target_rtx (node
->right
->code_label
),
1771 unsignedp
, probability
);
1775 else if (node
->right
== 0 && node
->left
!= 0)
1777 /* Just one subtree, on the left. */
1778 if (node
->left
->left
|| node
->left
->right
1779 || !tree_int_cst_equal (node
->left
->low
, node
->left
->high
))
1781 if (!node_has_high_bound (node
, index_type
))
1783 probability
= conditional_probability (
1785 subtree_prob
+ default_prob
);
1786 emit_cmp_and_jump_insns (index
,
1789 expand_normal (node
->high
),
1791 GT
, NULL_RTX
, mode
, unsignedp
,
1797 emit_case_nodes (index
, node
->left
, default_label
,
1798 default_prob
, index_type
);
1802 probability
= conditional_probability (
1803 node
->left
->subtree_prob
,
1804 subtree_prob
+ default_prob
);
1805 /* We cannot process node->left normally
1806 since we haven't ruled out the numbers less than
1807 this node's value. So handle node->left explicitly. */
1808 do_jump_if_equal (mode
, index
,
1811 expand_normal (node
->left
->low
),
1813 jump_target_rtx (node
->left
->code_label
),
1814 unsignedp
, probability
);
1820 /* Node is a range. These cases are very similar to those for a single
1821 value, except that we do not start by testing whether this node
1822 is the one to branch to. */
1824 if (node
->right
!= 0 && node
->left
!= 0)
1826 /* Node has subtrees on both sides.
1827 If the right-hand subtree is bounded,
1828 test for it first, since we can go straight there.
1829 Otherwise, we need to make a branch in the control structure,
1830 then handle the two subtrees. */
1831 tree test_label
= 0;
1833 if (node_is_bounded (node
->right
, index_type
))
1835 /* Right hand node is fully bounded so we can eliminate any
1836 testing and branch directly to the target code. */
1837 probability
= conditional_probability (
1838 node
->right
->subtree_prob
,
1839 subtree_prob
+ default_prob
);
1840 emit_cmp_and_jump_insns (index
,
1843 expand_normal (node
->high
),
1845 GT
, NULL_RTX
, mode
, unsignedp
,
1846 label_rtx (node
->right
->code_label
),
1851 /* Right hand node requires testing.
1852 Branch to a label where we will handle it later. */
1854 test_label
= build_decl (curr_insn_location (),
1855 LABEL_DECL
, NULL_TREE
, void_type_node
);
1856 probability
= conditional_probability (
1857 node
->right
->subtree_prob
+ default_prob
/2,
1858 subtree_prob
+ default_prob
);
1859 emit_cmp_and_jump_insns (index
,
1862 expand_normal (node
->high
),
1864 GT
, NULL_RTX
, mode
, unsignedp
,
1865 label_rtx (test_label
),
1870 /* Value belongs to this node or to the left-hand subtree. */
1872 probability
= conditional_probability (
1874 subtree_prob
+ default_prob
);
1875 emit_cmp_and_jump_insns (index
,
1878 expand_normal (node
->low
),
1880 GE
, NULL_RTX
, mode
, unsignedp
,
1881 label_rtx (node
->code_label
),
1884 /* Handle the left-hand subtree. */
1885 emit_case_nodes (index
, node
->left
, default_label
, default_prob
, index_type
);
1887 /* If right node had to be handled later, do that now. */
1891 /* If the left-hand subtree fell through,
1892 don't let it fall into the right-hand subtree. */
1894 emit_jump (default_label
);
1896 expand_label (test_label
);
1897 emit_case_nodes (index
, node
->right
, default_label
, default_prob
, index_type
);
1901 else if (node
->right
!= 0 && node
->left
== 0)
1903 /* Deal with values to the left of this node,
1904 if they are possible. */
1905 if (!node_has_low_bound (node
, index_type
))
1907 probability
= conditional_probability (
1909 subtree_prob
+ default_prob
);
1910 emit_cmp_and_jump_insns (index
,
1913 expand_normal (node
->low
),
1915 LT
, NULL_RTX
, mode
, unsignedp
,
1921 /* Value belongs to this node or to the right-hand subtree. */
1923 probability
= conditional_probability (
1925 subtree_prob
+ default_prob
);
1926 emit_cmp_and_jump_insns (index
,
1929 expand_normal (node
->high
),
1931 LE
, NULL_RTX
, mode
, unsignedp
,
1932 label_rtx (node
->code_label
),
1935 emit_case_nodes (index
, node
->right
, default_label
, default_prob
, index_type
);
1938 else if (node
->right
== 0 && node
->left
!= 0)
1940 /* Deal with values to the right of this node,
1941 if they are possible. */
1942 if (!node_has_high_bound (node
, index_type
))
1944 probability
= conditional_probability (
1946 subtree_prob
+ default_prob
);
1947 emit_cmp_and_jump_insns (index
,
1950 expand_normal (node
->high
),
1952 GT
, NULL_RTX
, mode
, unsignedp
,
1958 /* Value belongs to this node or to the left-hand subtree. */
1960 probability
= conditional_probability (
1962 subtree_prob
+ default_prob
);
1963 emit_cmp_and_jump_insns (index
,
1966 expand_normal (node
->low
),
1968 GE
, NULL_RTX
, mode
, unsignedp
,
1969 label_rtx (node
->code_label
),
1972 emit_case_nodes (index
, node
->left
, default_label
, default_prob
, index_type
);
1977 /* Node has no children so we check low and high bounds to remove
1978 redundant tests. Only one of the bounds can exist,
1979 since otherwise this node is bounded--a case tested already. */
1980 int high_bound
= node_has_high_bound (node
, index_type
);
1981 int low_bound
= node_has_low_bound (node
, index_type
);
1983 if (!high_bound
&& low_bound
)
1985 probability
= conditional_probability (
1987 subtree_prob
+ default_prob
);
1988 emit_cmp_and_jump_insns (index
,
1991 expand_normal (node
->high
),
1993 GT
, NULL_RTX
, mode
, unsignedp
,
1998 else if (!low_bound
&& high_bound
)
2000 probability
= conditional_probability (
2002 subtree_prob
+ default_prob
);
2003 emit_cmp_and_jump_insns (index
,
2006 expand_normal (node
->low
),
2008 LT
, NULL_RTX
, mode
, unsignedp
,
2012 else if (!low_bound
&& !high_bound
)
2014 /* Widen LOW and HIGH to the same width as INDEX. */
2015 tree type
= lang_hooks
.types
.type_for_mode (mode
, unsignedp
);
2016 tree low
= build1 (CONVERT_EXPR
, type
, node
->low
);
2017 tree high
= build1 (CONVERT_EXPR
, type
, node
->high
);
2018 rtx low_rtx
, new_index
, new_bound
;
2020 /* Instead of doing two branches, emit one unsigned branch for
2021 (index-low) > (high-low). */
2022 low_rtx
= expand_expr (low
, NULL_RTX
, mode
, EXPAND_NORMAL
);
2023 new_index
= expand_simple_binop (mode
, MINUS
, index
, low_rtx
,
2024 NULL_RTX
, unsignedp
,
2026 new_bound
= expand_expr (fold_build2 (MINUS_EXPR
, type
,
2028 NULL_RTX
, mode
, EXPAND_NORMAL
);
2030 probability
= conditional_probability (
2032 subtree_prob
+ default_prob
);
2033 emit_cmp_and_jump_insns (new_index
, new_bound
, GT
, NULL_RTX
,
2034 mode
, 1, default_label
, probability
);
2037 emit_jump (jump_target_rtx (node
->code_label
));