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"
31 #include "hard-reg-set.h"
35 #include "fold-const.h"
37 #include "stor-layout.h"
42 #include "insn-config.h"
52 #include "diagnostic-core.h"
54 #include "langhooks.h"
56 #include "insn-codes.h"
60 #include "basic-block.h"
61 #include "tree-ssa-alias.h"
62 #include "internal-fn.h"
63 #include "gimple-expr.h"
66 #include "alloc-pool.h"
67 #include "pretty-print.h"
73 /* Functions and data structures for expanding case statements. */
75 /* Case label structure, used to hold info on labels within case
76 statements. We handle "range" labels; for a single-value label
77 as in C, the high and low limits are the same.
79 We start with a vector of case nodes sorted in ascending order, and
80 the default label as the last element in the vector. Before expanding
81 to RTL, we transform this vector into a list linked via the RIGHT
82 fields in the case_node struct. Nodes with higher case values are
85 Switch statements can be output in three forms. A branch table is
86 used if there are more than a few labels and the labels are dense
87 within the range between the smallest and largest case value. If a
88 branch table is used, no further manipulations are done with the case
91 The alternative to the use of a branch table is to generate a series
92 of compare and jump insns. When that is done, we use the LEFT, RIGHT,
93 and PARENT fields to hold a binary tree. Initially the tree is
94 totally unbalanced, with everything on the right. We balance the tree
95 with nodes on the left having lower case values than the parent
96 and nodes on the right having higher values. We then output the tree
99 For very small, suitable switch statements, we can generate a series
100 of simple bit test and branches instead. */
104 struct case_node
*left
; /* Left son in binary tree */
105 struct case_node
*right
; /* Right son in binary tree; also node chain */
106 struct case_node
*parent
; /* Parent of node in binary tree */
107 tree low
; /* Lowest index value for this label */
108 tree high
; /* Highest index value for this label */
109 tree code_label
; /* Label to jump to when node matches */
110 int prob
; /* Probability of taking this case. */
111 /* Probability of reaching subtree rooted at this node */
115 typedef struct case_node case_node
;
116 typedef struct case_node
*case_node_ptr
;
118 extern basic_block
label_to_block_fn (struct function
*, tree
);
120 static bool check_unique_operand_names (tree
, tree
, tree
);
121 static char *resolve_operand_name_1 (char *, tree
, tree
, tree
);
122 static void balance_case_nodes (case_node_ptr
*, case_node_ptr
);
123 static int node_has_low_bound (case_node_ptr
, tree
);
124 static int node_has_high_bound (case_node_ptr
, tree
);
125 static int node_is_bounded (case_node_ptr
, tree
);
126 static void emit_case_nodes (rtx
, case_node_ptr
, rtx_code_label
*, int, tree
);
128 /* Return the rtx-label that corresponds to a LABEL_DECL,
129 creating it if necessary. */
132 label_rtx (tree label
)
134 gcc_assert (TREE_CODE (label
) == LABEL_DECL
);
136 if (!DECL_RTL_SET_P (label
))
138 rtx_code_label
*r
= gen_label_rtx ();
139 SET_DECL_RTL (label
, r
);
140 if (FORCED_LABEL (label
) || DECL_NONLOCAL (label
))
141 LABEL_PRESERVE_P (r
) = 1;
144 return as_a
<rtx_insn
*> (DECL_RTL (label
));
147 /* As above, but also put it on the forced-reference list of the
148 function that contains it. */
150 force_label_rtx (tree label
)
152 rtx_insn
*ref
= label_rtx (label
);
153 tree function
= decl_function_context (label
);
155 gcc_assert (function
);
157 forced_labels
= gen_rtx_INSN_LIST (VOIDmode
, ref
, forced_labels
);
161 /* As label_rtx, but ensures (in check build), that returned value is
162 an existing label (i.e. rtx with code CODE_LABEL). */
164 jump_target_rtx (tree label
)
166 return as_a
<rtx_code_label
*> (label_rtx (label
));
169 /* Add an unconditional jump to LABEL as the next sequential instruction. */
172 emit_jump (rtx label
)
174 do_pending_stack_adjust ();
175 emit_jump_insn (targetm
.gen_jump (label
));
179 /* Handle goto statements and the labels that they can go to. */
181 /* Specify the location in the RTL code of a label LABEL,
182 which is a LABEL_DECL tree node.
184 This is used for the kind of label that the user can jump to with a
185 goto statement, and for alternatives of a switch or case statement.
186 RTL labels generated for loops and conditionals don't go through here;
187 they are generated directly at the RTL level, by other functions below.
189 Note that this has nothing to do with defining label *names*.
190 Languages vary in how they do that and what that even means. */
193 expand_label (tree label
)
195 rtx_code_label
*label_r
= jump_target_rtx (label
);
197 do_pending_stack_adjust ();
198 emit_label (label_r
);
199 if (DECL_NAME (label
))
200 LABEL_NAME (DECL_RTL (label
)) = IDENTIFIER_POINTER (DECL_NAME (label
));
202 if (DECL_NONLOCAL (label
))
204 expand_builtin_setjmp_receiver (NULL
);
205 nonlocal_goto_handler_labels
206 = gen_rtx_INSN_LIST (VOIDmode
, label_r
,
207 nonlocal_goto_handler_labels
);
210 if (FORCED_LABEL (label
))
211 forced_labels
= gen_rtx_INSN_LIST (VOIDmode
, label_r
, forced_labels
);
213 if (DECL_NONLOCAL (label
) || FORCED_LABEL (label
))
214 maybe_set_first_label_num (label_r
);
217 /* Parse the output constraint pointed to by *CONSTRAINT_P. It is the
218 OPERAND_NUMth output operand, indexed from zero. There are NINPUTS
219 inputs and NOUTPUTS outputs to this extended-asm. Upon return,
220 *ALLOWS_MEM will be TRUE iff the constraint allows the use of a
221 memory operand. Similarly, *ALLOWS_REG will be TRUE iff the
222 constraint allows the use of a register operand. And, *IS_INOUT
223 will be true if the operand is read-write, i.e., if it is used as
224 an input as well as an output. If *CONSTRAINT_P is not in
225 canonical form, it will be made canonical. (Note that `+' will be
226 replaced with `=' as part of this process.)
228 Returns TRUE if all went well; FALSE if an error occurred. */
231 parse_output_constraint (const char **constraint_p
, int operand_num
,
232 int ninputs
, int noutputs
, bool *allows_mem
,
233 bool *allows_reg
, bool *is_inout
)
235 const char *constraint
= *constraint_p
;
238 /* Assume the constraint doesn't allow the use of either a register
243 /* Allow the `=' or `+' to not be at the beginning of the string,
244 since it wasn't explicitly documented that way, and there is a
245 large body of code that puts it last. Swap the character to
246 the front, so as not to uglify any place else. */
247 p
= strchr (constraint
, '=');
249 p
= strchr (constraint
, '+');
251 /* If the string doesn't contain an `=', issue an error
255 error ("output operand constraint lacks %<=%>");
259 /* If the constraint begins with `+', then the operand is both read
260 from and written to. */
261 *is_inout
= (*p
== '+');
263 /* Canonicalize the output constraint so that it begins with `='. */
264 if (p
!= constraint
|| *is_inout
)
267 size_t c_len
= strlen (constraint
);
270 warning (0, "output constraint %qc for operand %d "
271 "is not at the beginning",
274 /* Make a copy of the constraint. */
275 buf
= XALLOCAVEC (char, c_len
+ 1);
276 strcpy (buf
, constraint
);
277 /* Swap the first character and the `=' or `+'. */
278 buf
[p
- constraint
] = buf
[0];
279 /* Make sure the first character is an `='. (Until we do this,
280 it might be a `+'.) */
282 /* Replace the constraint with the canonicalized string. */
283 *constraint_p
= ggc_alloc_string (buf
, c_len
);
284 constraint
= *constraint_p
;
287 /* Loop through the constraint string. */
288 for (p
= constraint
+ 1; *p
; p
+= CONSTRAINT_LEN (*p
, p
))
293 error ("operand constraint contains incorrectly positioned "
298 if (operand_num
+ 1 == ninputs
+ noutputs
)
300 error ("%<%%%> constraint used with last operand");
305 case '?': case '!': case '*': case '&': case '#':
307 case 'E': case 'F': case 'G': case 'H':
308 case 's': case 'i': case 'n':
309 case 'I': case 'J': case 'K': case 'L': case 'M':
310 case 'N': case 'O': case 'P': case ',':
313 case '0': case '1': case '2': case '3': case '4':
314 case '5': case '6': case '7': case '8': case '9':
316 error ("matching constraint not valid in output operand");
320 /* ??? Before flow, auto inc/dec insns are not supposed to exist,
321 excepting those that expand_call created. So match memory
334 enum constraint_num cn
= lookup_constraint (p
);
335 if (reg_class_for_constraint (cn
) != NO_REGS
336 || insn_extra_address_constraint (cn
))
338 else if (insn_extra_memory_constraint (cn
))
341 insn_extra_constraint_allows_reg_mem (cn
, allows_reg
, allows_mem
);
348 /* Similar, but for input constraints. */
351 parse_input_constraint (const char **constraint_p
, int input_num
,
352 int ninputs
, int noutputs
, int ninout
,
353 const char * const * constraints
,
354 bool *allows_mem
, bool *allows_reg
)
356 const char *constraint
= *constraint_p
;
357 const char *orig_constraint
= constraint
;
358 size_t c_len
= strlen (constraint
);
360 bool saw_match
= false;
362 /* Assume the constraint doesn't allow the use of either
363 a register or memory. */
367 /* Make sure constraint has neither `=', `+', nor '&'. */
369 for (j
= 0; j
< c_len
; j
+= CONSTRAINT_LEN (constraint
[j
], constraint
+j
))
370 switch (constraint
[j
])
372 case '+': case '=': case '&':
373 if (constraint
== orig_constraint
)
375 error ("input operand constraint contains %qc", constraint
[j
]);
381 if (constraint
== orig_constraint
382 && input_num
+ 1 == ninputs
- ninout
)
384 error ("%<%%%> constraint used with last operand");
390 case '?': case '!': case '*': case '#':
392 case 'E': case 'F': case 'G': case 'H':
393 case 's': case 'i': case 'n':
394 case 'I': case 'J': case 'K': case 'L': case 'M':
395 case 'N': case 'O': case 'P': case ',':
398 /* Whether or not a numeric constraint allows a register is
399 decided by the matching constraint, and so there is no need
400 to do anything special with them. We must handle them in
401 the default case, so that we don't unnecessarily force
402 operands to memory. */
403 case '0': case '1': case '2': case '3': case '4':
404 case '5': case '6': case '7': case '8': case '9':
411 match
= strtoul (constraint
+ j
, &end
, 10);
412 if (match
>= (unsigned long) noutputs
)
414 error ("matching constraint references invalid operand number");
418 /* Try and find the real constraint for this dup. Only do this
419 if the matching constraint is the only alternative. */
421 && (j
== 0 || (j
== 1 && constraint
[0] == '%')))
423 constraint
= constraints
[match
];
424 *constraint_p
= constraint
;
425 c_len
= strlen (constraint
);
427 /* ??? At the end of the loop, we will skip the first part of
428 the matched constraint. This assumes not only that the
429 other constraint is an output constraint, but also that
430 the '=' or '+' come first. */
434 j
= end
- constraint
;
435 /* Anticipate increment at end of loop. */
446 if (! ISALPHA (constraint
[j
]))
448 error ("invalid punctuation %qc in constraint", constraint
[j
]);
451 enum constraint_num cn
= lookup_constraint (constraint
+ j
);
452 if (reg_class_for_constraint (cn
) != NO_REGS
453 || insn_extra_address_constraint (cn
))
455 else if (insn_extra_memory_constraint (cn
))
458 insn_extra_constraint_allows_reg_mem (cn
, allows_reg
, allows_mem
);
462 if (saw_match
&& !*allows_reg
)
463 warning (0, "matching constraint does not allow a register");
468 /* Return DECL iff there's an overlap between *REGS and DECL, where DECL
469 can be an asm-declared register. Called via walk_tree. */
472 decl_overlaps_hard_reg_set_p (tree
*declp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
476 const HARD_REG_SET
*const regs
= (const HARD_REG_SET
*) data
;
478 if (TREE_CODE (decl
) == VAR_DECL
)
480 if (DECL_HARD_REGISTER (decl
)
481 && REG_P (DECL_RTL (decl
))
482 && REGNO (DECL_RTL (decl
)) < FIRST_PSEUDO_REGISTER
)
484 rtx reg
= DECL_RTL (decl
);
486 if (overlaps_hard_reg_set_p (*regs
, GET_MODE (reg
), REGNO (reg
)))
491 else if (TYPE_P (decl
) || TREE_CODE (decl
) == PARM_DECL
)
496 /* If there is an overlap between *REGS and DECL, return the first overlap
499 tree_overlaps_hard_reg_set (tree decl
, HARD_REG_SET
*regs
)
501 return walk_tree (&decl
, decl_overlaps_hard_reg_set_p
, regs
, NULL
);
505 /* A subroutine of expand_asm_operands. Check that all operand names
506 are unique. Return true if so. We rely on the fact that these names
507 are identifiers, and so have been canonicalized by get_identifier,
508 so all we need are pointer comparisons. */
511 check_unique_operand_names (tree outputs
, tree inputs
, tree labels
)
513 tree i
, j
, i_name
= NULL_TREE
;
515 for (i
= outputs
; i
; i
= TREE_CHAIN (i
))
517 i_name
= TREE_PURPOSE (TREE_PURPOSE (i
));
521 for (j
= TREE_CHAIN (i
); j
; j
= TREE_CHAIN (j
))
522 if (simple_cst_equal (i_name
, TREE_PURPOSE (TREE_PURPOSE (j
))))
526 for (i
= inputs
; i
; i
= TREE_CHAIN (i
))
528 i_name
= TREE_PURPOSE (TREE_PURPOSE (i
));
532 for (j
= TREE_CHAIN (i
); j
; j
= TREE_CHAIN (j
))
533 if (simple_cst_equal (i_name
, TREE_PURPOSE (TREE_PURPOSE (j
))))
535 for (j
= outputs
; j
; j
= TREE_CHAIN (j
))
536 if (simple_cst_equal (i_name
, TREE_PURPOSE (TREE_PURPOSE (j
))))
540 for (i
= labels
; i
; i
= TREE_CHAIN (i
))
542 i_name
= TREE_PURPOSE (i
);
546 for (j
= TREE_CHAIN (i
); j
; j
= TREE_CHAIN (j
))
547 if (simple_cst_equal (i_name
, TREE_PURPOSE (j
)))
549 for (j
= inputs
; j
; j
= TREE_CHAIN (j
))
550 if (simple_cst_equal (i_name
, TREE_PURPOSE (TREE_PURPOSE (j
))))
557 error ("duplicate asm operand name %qs", TREE_STRING_POINTER (i_name
));
561 /* Resolve the names of the operands in *POUTPUTS and *PINPUTS to numbers,
562 and replace the name expansions in STRING and in the constraints to
563 those numbers. This is generally done in the front end while creating
564 the ASM_EXPR generic tree that eventually becomes the GIMPLE_ASM. */
567 resolve_asm_operand_names (tree string
, tree outputs
, tree inputs
, tree labels
)
574 check_unique_operand_names (outputs
, inputs
, labels
);
576 /* Substitute [<name>] in input constraint strings. There should be no
577 named operands in output constraints. */
578 for (t
= inputs
; t
; t
= TREE_CHAIN (t
))
580 c
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t
)));
581 if (strchr (c
, '[') != NULL
)
583 p
= buffer
= xstrdup (c
);
584 while ((p
= strchr (p
, '[')) != NULL
)
585 p
= resolve_operand_name_1 (p
, outputs
, inputs
, NULL
);
586 TREE_VALUE (TREE_PURPOSE (t
))
587 = build_string (strlen (buffer
), buffer
);
592 /* Now check for any needed substitutions in the template. */
593 c
= TREE_STRING_POINTER (string
);
594 while ((c
= strchr (c
, '%')) != NULL
)
598 else if (ISALPHA (c
[1]) && c
[2] == '[')
602 c
+= 1 + (c
[1] == '%');
609 /* OK, we need to make a copy so we can perform the substitutions.
610 Assume that we will not need extra space--we get to remove '['
611 and ']', which means we cannot have a problem until we have more
612 than 999 operands. */
613 buffer
= xstrdup (TREE_STRING_POINTER (string
));
614 p
= buffer
+ (c
- TREE_STRING_POINTER (string
));
616 while ((p
= strchr (p
, '%')) != NULL
)
620 else if (ISALPHA (p
[1]) && p
[2] == '[')
624 p
+= 1 + (p
[1] == '%');
628 p
= resolve_operand_name_1 (p
, outputs
, inputs
, labels
);
631 string
= build_string (strlen (buffer
), buffer
);
638 /* A subroutine of resolve_operand_names. P points to the '[' for a
639 potential named operand of the form [<name>]. In place, replace
640 the name and brackets with a number. Return a pointer to the
641 balance of the string after substitution. */
644 resolve_operand_name_1 (char *p
, tree outputs
, tree inputs
, tree labels
)
650 /* Collect the operand name. */
651 q
= strchr (++p
, ']');
654 error ("missing close brace for named operand");
655 return strchr (p
, '\0');
659 /* Resolve the name to a number. */
660 for (op
= 0, t
= outputs
; 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
= inputs
; t
; t
= TREE_CHAIN (t
), op
++)
668 tree name
= TREE_PURPOSE (TREE_PURPOSE (t
));
669 if (name
&& strcmp (TREE_STRING_POINTER (name
), p
) == 0)
672 for (t
= labels
; t
; t
= TREE_CHAIN (t
), op
++)
674 tree name
= TREE_PURPOSE (t
);
675 if (name
&& strcmp (TREE_STRING_POINTER (name
), p
) == 0)
679 error ("undefined named operand %qs", identifier_to_locale (p
));
683 /* Replace the name with the number. Unfortunately, not all libraries
684 get the return value of sprintf correct, so search for the end of the
685 generated string by hand. */
686 sprintf (--p
, "%d", op
);
687 p
= strchr (p
, '\0');
689 /* Verify the no extra buffer space assumption. */
692 /* Shift the rest of the buffer down to fill the gap. */
693 memmove (p
, q
+ 1, strlen (q
+ 1) + 1);
699 /* Generate RTL to return directly from the current function.
700 (That is, we bypass any return value.) */
703 expand_naked_return (void)
705 rtx_code_label
*end_label
;
707 clear_pending_stack_adjust ();
708 do_pending_stack_adjust ();
710 end_label
= naked_return_label
;
712 end_label
= naked_return_label
= gen_label_rtx ();
714 emit_jump (end_label
);
717 /* Generate code to jump to LABEL if OP0 and OP1 are equal in mode MODE. PROB
718 is the probability of jumping to LABEL. */
720 do_jump_if_equal (machine_mode mode
, rtx op0
, rtx op1
, rtx_code_label
*label
,
721 int unsignedp
, int prob
)
723 gcc_assert (prob
<= REG_BR_PROB_BASE
);
724 do_compare_rtx_and_jump (op0
, op1
, EQ
, unsignedp
, mode
,
725 NULL_RTX
, NULL
, label
, prob
);
728 /* Do the insertion of a case label into case_list. The labels are
729 fed to us in descending order from the sorted vector of case labels used
730 in the tree part of the middle end. So the list we construct is
731 sorted in ascending order.
733 LABEL is the case label to be inserted. LOW and HIGH are the bounds
734 against which the index is compared to jump to LABEL and PROB is the
735 estimated probability LABEL is reached from the switch statement. */
737 static struct case_node
*
738 add_case_node (struct case_node
*head
, tree low
, tree high
,
739 tree label
, int prob
, pool_allocator
<case_node
> &case_node_pool
)
743 gcc_checking_assert (low
);
744 gcc_checking_assert (high
&& (TREE_TYPE (low
) == TREE_TYPE (high
)));
746 /* Add this label to the chain. */
747 r
= case_node_pool
.allocate ();
750 r
->code_label
= label
;
751 r
->parent
= r
->left
= NULL
;
753 r
->subtree_prob
= prob
;
758 /* Dump ROOT, a list or tree of case nodes, to file. */
761 dump_case_nodes (FILE *f
, struct case_node
*root
,
762 int indent_step
, int indent_level
)
768 dump_case_nodes (f
, root
->left
, indent_step
, indent_level
);
771 fprintf (f
, "%*s", indent_step
* indent_level
, "");
772 print_dec (root
->low
, f
, TYPE_SIGN (TREE_TYPE (root
->low
)));
773 if (!tree_int_cst_equal (root
->low
, root
->high
))
775 fprintf (f
, " ... ");
776 print_dec (root
->high
, f
, TYPE_SIGN (TREE_TYPE (root
->high
)));
780 dump_case_nodes (f
, root
->right
, indent_step
, indent_level
);
784 #define HAVE_casesi 0
787 /* Return the smallest number of different values for which it is best to use a
788 jump-table instead of a tree of conditional branches. */
791 case_values_threshold (void)
793 unsigned int threshold
= PARAM_VALUE (PARAM_CASE_VALUES_THRESHOLD
);
796 threshold
= targetm
.case_values_threshold ();
801 /* Return true if a switch should be expanded as a decision tree.
802 RANGE is the difference between highest and lowest case.
803 UNIQ is number of unique case node targets, not counting the default case.
804 COUNT is the number of comparisons needed, not counting the default case. */
807 expand_switch_as_decision_tree_p (tree range
,
808 unsigned int uniq ATTRIBUTE_UNUSED
,
813 /* If neither casesi or tablejump is available, or flag_jump_tables
814 over-ruled us, we really have no choice. */
815 if (!HAVE_casesi
&& !HAVE_tablejump
)
817 if (!flag_jump_tables
)
819 #ifndef ASM_OUTPUT_ADDR_DIFF_ELT
824 /* If the switch is relatively small such that the cost of one
825 indirect jump on the target are higher than the cost of a
826 decision tree, go with the decision tree.
828 If range of values is much bigger than number of values,
829 or if it is too large to represent in a HOST_WIDE_INT,
830 make a sequence of conditional branches instead of a dispatch.
832 The definition of "much bigger" depends on whether we are
833 optimizing for size or for speed. If the former, the maximum
834 ratio range/count = 3, because this was found to be the optimal
835 ratio for size on i686-pc-linux-gnu, see PR11823. The ratio
836 10 is much older, and was probably selected after an extensive
837 benchmarking investigation on numerous platforms. Or maybe it
838 just made sense to someone at some point in the history of GCC,
840 max_ratio
= optimize_insn_for_size_p () ? 3 : 10;
841 if (count
< case_values_threshold ()
842 || ! tree_fits_uhwi_p (range
)
843 || compare_tree_int (range
, max_ratio
* count
) > 0)
849 /* Generate a decision tree, switching on INDEX_EXPR and jumping to
850 one of the labels in CASE_LIST or to the DEFAULT_LABEL.
851 DEFAULT_PROB is the estimated probability that it jumps to
854 We generate a binary decision tree to select the appropriate target
855 code. This is done as follows:
857 If the index is a short or char that we do not have
858 an insn to handle comparisons directly, convert it to
859 a full integer now, rather than letting each comparison
860 generate the conversion.
862 Load the index into a register.
864 The list of cases is rearranged into a binary tree,
865 nearly optimal assuming equal probability for each case.
867 The tree is transformed into RTL, eliminating redundant
868 test conditions at the same time.
870 If program flow could reach the end of the decision tree
871 an unconditional jump to the default code is emitted.
873 The above process is unaware of the CFG. The caller has to fix up
874 the CFG itself. This is done in cfgexpand.c. */
877 emit_case_decision_tree (tree index_expr
, tree index_type
,
878 case_node_ptr case_list
, rtx_code_label
*default_label
,
881 rtx index
= expand_normal (index_expr
);
883 if (GET_MODE_CLASS (GET_MODE (index
)) == MODE_INT
884 && ! have_insn_for (COMPARE
, GET_MODE (index
)))
886 int unsignedp
= TYPE_UNSIGNED (index_type
);
887 machine_mode wider_mode
;
888 for (wider_mode
= GET_MODE (index
); wider_mode
!= VOIDmode
;
889 wider_mode
= GET_MODE_WIDER_MODE (wider_mode
))
890 if (have_insn_for (COMPARE
, wider_mode
))
892 index
= convert_to_mode (wider_mode
, index
, unsignedp
);
897 do_pending_stack_adjust ();
901 index
= copy_to_reg (index
);
902 if (TREE_CODE (index_expr
) == SSA_NAME
)
903 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (index_expr
), index
);
906 balance_case_nodes (&case_list
, NULL
);
908 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
910 int indent_step
= ceil_log2 (TYPE_PRECISION (index_type
)) + 2;
911 fprintf (dump_file
, ";; Expanding GIMPLE switch as decision tree:\n");
912 dump_case_nodes (dump_file
, case_list
, indent_step
, 0);
915 emit_case_nodes (index
, case_list
, default_label
, default_prob
, index_type
);
917 emit_jump (default_label
);
920 /* Return the sum of probabilities of outgoing edges of basic block BB. */
923 get_outgoing_edge_probs (basic_block bb
)
930 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
931 prob_sum
+= e
->probability
;
935 /* Computes the conditional probability of jumping to a target if the branch
936 instruction is executed.
937 TARGET_PROB is the estimated probability of jumping to a target relative
938 to some basic block BB.
939 BASE_PROB is the probability of reaching the branch instruction relative
940 to the same basic block BB. */
943 conditional_probability (int target_prob
, int base_prob
)
947 gcc_assert (target_prob
>= 0);
948 gcc_assert (target_prob
<= base_prob
);
949 return GCOV_COMPUTE_SCALE (target_prob
, base_prob
);
954 /* Generate a dispatch tabler, switching on INDEX_EXPR and jumping to
955 one of the labels in CASE_LIST or to the DEFAULT_LABEL.
956 MINVAL, MAXVAL, and RANGE are the extrema and range of the case
957 labels in CASE_LIST. STMT_BB is the basic block containing the statement.
959 First, a jump insn is emitted. First we try "casesi". If that
960 fails, try "tablejump". A target *must* have one of them (or both).
962 Then, a table with the target labels is emitted.
964 The process is unaware of the CFG. The caller has to fix up
965 the CFG itself. This is done in cfgexpand.c. */
968 emit_case_dispatch_table (tree index_expr
, tree index_type
,
969 struct case_node
*case_list
, rtx default_label
,
970 tree minval
, tree maxval
, tree range
,
976 rtx_insn
*fallback_label
= label_rtx (case_list
->code_label
);
977 rtx_code_label
*table_label
= gen_label_rtx ();
978 bool has_gaps
= false;
979 edge default_edge
= stmt_bb
? EDGE_SUCC (stmt_bb
, 0) : NULL
;
980 int default_prob
= default_edge
? default_edge
->probability
: 0;
981 int base
= get_outgoing_edge_probs (stmt_bb
);
982 bool try_with_tablejump
= false;
984 int new_default_prob
= conditional_probability (default_prob
,
987 if (! try_casesi (index_type
, index_expr
, minval
, range
,
988 table_label
, default_label
, fallback_label
,
991 /* Index jumptables from zero for suitable values of minval to avoid
992 a subtraction. For the rationale see:
993 "http://gcc.gnu.org/ml/gcc-patches/2001-10/msg01234.html". */
994 if (optimize_insn_for_speed_p ()
995 && compare_tree_int (minval
, 0) > 0
996 && compare_tree_int (minval
, 3) < 0)
998 minval
= build_int_cst (index_type
, 0);
1002 try_with_tablejump
= true;
1005 /* Get table of labels to jump to, in order of case index. */
1007 ncases
= tree_to_shwi (range
) + 1;
1008 labelvec
= XALLOCAVEC (rtx
, ncases
);
1009 memset (labelvec
, 0, ncases
* sizeof (rtx
));
1011 for (n
= case_list
; n
; n
= n
->right
)
1013 /* Compute the low and high bounds relative to the minimum
1014 value since that should fit in a HOST_WIDE_INT while the
1015 actual values may not. */
1017 = tree_to_uhwi (fold_build2 (MINUS_EXPR
, index_type
,
1019 HOST_WIDE_INT i_high
1020 = tree_to_uhwi (fold_build2 (MINUS_EXPR
, index_type
,
1024 for (i
= i_low
; i
<= i_high
; i
++)
1026 = gen_rtx_LABEL_REF (Pmode
, label_rtx (n
->code_label
));
1029 /* Fill in the gaps with the default. We may have gaps at
1030 the beginning if we tried to avoid the minval subtraction,
1031 so substitute some label even if the default label was
1032 deemed unreachable. */
1034 default_label
= fallback_label
;
1035 for (i
= 0; i
< ncases
; i
++)
1036 if (labelvec
[i
] == 0)
1039 labelvec
[i
] = gen_rtx_LABEL_REF (Pmode
, default_label
);
1044 /* There is at least one entry in the jump table that jumps
1045 to default label. The default label can either be reached
1046 through the indirect jump or the direct conditional jump
1047 before that. Split the probability of reaching the
1048 default label among these two jumps. */
1049 new_default_prob
= conditional_probability (default_prob
/2,
1052 base
-= default_prob
;
1056 base
-= default_prob
;
1061 default_edge
->probability
= default_prob
;
1063 /* We have altered the probability of the default edge. So the probabilities
1064 of all other edges need to be adjusted so that it sums up to
1065 REG_BR_PROB_BASE. */
1070 FOR_EACH_EDGE (e
, ei
, stmt_bb
->succs
)
1071 e
->probability
= GCOV_COMPUTE_SCALE (e
->probability
, base
);
1074 if (try_with_tablejump
)
1076 bool ok
= try_tablejump (index_type
, index_expr
, minval
, range
,
1077 table_label
, default_label
, new_default_prob
);
1080 /* Output the table. */
1081 emit_label (table_label
);
1083 if (CASE_VECTOR_PC_RELATIVE
|| flag_pic
)
1084 emit_jump_table_data (gen_rtx_ADDR_DIFF_VEC (CASE_VECTOR_MODE
,
1085 gen_rtx_LABEL_REF (Pmode
,
1087 gen_rtvec_v (ncases
, labelvec
),
1088 const0_rtx
, const0_rtx
));
1090 emit_jump_table_data (gen_rtx_ADDR_VEC (CASE_VECTOR_MODE
,
1091 gen_rtvec_v (ncases
, labelvec
)));
1093 /* Record no drop-through after the table. */
1097 /* Reset the aux field of all outgoing edges of basic block BB. */
1100 reset_out_edges_aux (basic_block bb
)
1104 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1108 /* Compute the number of case labels that correspond to each outgoing edge of
1109 STMT. Record this information in the aux field of the edge. */
1112 compute_cases_per_edge (gswitch
*stmt
)
1114 basic_block bb
= gimple_bb (stmt
);
1115 reset_out_edges_aux (bb
);
1116 int ncases
= gimple_switch_num_labels (stmt
);
1117 for (int i
= ncases
- 1; i
>= 1; --i
)
1119 tree elt
= gimple_switch_label (stmt
, i
);
1120 tree lab
= CASE_LABEL (elt
);
1121 basic_block case_bb
= label_to_block_fn (cfun
, lab
);
1122 edge case_edge
= find_edge (bb
, case_bb
);
1123 case_edge
->aux
= (void *)((intptr_t)(case_edge
->aux
) + 1);
1127 /* Terminate a case (Pascal/Ada) or switch (C) statement
1128 in which ORIG_INDEX is the expression to be tested.
1129 If ORIG_TYPE is not NULL, it is the original ORIG_INDEX
1130 type as given in the source before any compiler conversions.
1131 Generate the code to test it and jump to the right place. */
1134 expand_case (gswitch
*stmt
)
1136 tree minval
= NULL_TREE
, maxval
= NULL_TREE
, range
= NULL_TREE
;
1137 rtx_code_label
*default_label
= NULL
;
1138 unsigned int count
, uniq
;
1140 int ncases
= gimple_switch_num_labels (stmt
);
1141 tree index_expr
= gimple_switch_index (stmt
);
1142 tree index_type
= TREE_TYPE (index_expr
);
1144 basic_block bb
= gimple_bb (stmt
);
1146 /* A list of case labels; it is first built as a list and it may then
1147 be rearranged into a nearly balanced binary tree. */
1148 struct case_node
*case_list
= 0;
1150 /* A pool for case nodes. */
1151 pool_allocator
<case_node
> case_node_pool ("struct case_node pool", 100);
1153 /* An ERROR_MARK occurs for various reasons including invalid data type.
1154 ??? Can this still happen, with GIMPLE and all? */
1155 if (index_type
== error_mark_node
)
1158 /* cleanup_tree_cfg removes all SWITCH_EXPR with their index
1159 expressions being INTEGER_CST. */
1160 gcc_assert (TREE_CODE (index_expr
) != INTEGER_CST
);
1163 do_pending_stack_adjust ();
1165 /* Find the default case target label. */
1166 default_label
= jump_target_rtx
1167 (CASE_LABEL (gimple_switch_default_label (stmt
)));
1168 edge default_edge
= EDGE_SUCC (bb
, 0);
1169 int default_prob
= default_edge
->probability
;
1171 /* Get upper and lower bounds of case values. */
1172 elt
= gimple_switch_label (stmt
, 1);
1173 minval
= fold_convert (index_type
, CASE_LOW (elt
));
1174 elt
= gimple_switch_label (stmt
, ncases
- 1);
1175 if (CASE_HIGH (elt
))
1176 maxval
= fold_convert (index_type
, CASE_HIGH (elt
));
1178 maxval
= fold_convert (index_type
, CASE_LOW (elt
));
1180 /* Compute span of values. */
1181 range
= fold_build2 (MINUS_EXPR
, index_type
, maxval
, minval
);
1183 /* Listify the labels queue and gather some numbers to decide
1184 how to expand this switch(). */
1187 hash_set
<tree
> seen_labels
;
1188 compute_cases_per_edge (stmt
);
1190 for (i
= ncases
- 1; i
>= 1; --i
)
1192 elt
= gimple_switch_label (stmt
, i
);
1193 tree low
= CASE_LOW (elt
);
1195 tree high
= CASE_HIGH (elt
);
1196 gcc_assert (! high
|| tree_int_cst_lt (low
, high
));
1197 tree lab
= CASE_LABEL (elt
);
1199 /* Count the elements.
1200 A range counts double, since it requires two compares. */
1205 /* If we have not seen this label yet, then increase the
1206 number of unique case node targets seen. */
1207 if (!seen_labels
.add (lab
))
1210 /* The bounds on the case range, LOW and HIGH, have to be converted
1211 to case's index type TYPE. Note that the original type of the
1212 case index in the source code is usually "lost" during
1213 gimplification due to type promotion, but the case labels retain the
1214 original type. Make sure to drop overflow flags. */
1215 low
= fold_convert (index_type
, low
);
1216 if (TREE_OVERFLOW (low
))
1217 low
= wide_int_to_tree (index_type
, low
);
1219 /* The canonical from of a case label in GIMPLE is that a simple case
1220 has an empty CASE_HIGH. For the casesi and tablejump expanders,
1221 the back ends want simple cases to have high == low. */
1224 high
= fold_convert (index_type
, high
);
1225 if (TREE_OVERFLOW (high
))
1226 high
= wide_int_to_tree (index_type
, high
);
1228 basic_block case_bb
= label_to_block_fn (cfun
, lab
);
1229 edge case_edge
= find_edge (bb
, case_bb
);
1230 case_list
= add_case_node (
1231 case_list
, low
, high
, lab
,
1232 case_edge
->probability
/ (intptr_t)(case_edge
->aux
),
1235 reset_out_edges_aux (bb
);
1237 /* cleanup_tree_cfg removes all SWITCH_EXPR with a single
1238 destination, such as one with a default case only.
1239 It also removes cases that are out of range for the switch
1240 type, so we should never get a zero here. */
1241 gcc_assert (count
> 0);
1243 rtx_insn
*before_case
= get_last_insn ();
1245 /* Decide how to expand this switch.
1246 The two options at this point are a dispatch table (casesi or
1247 tablejump) or a decision tree. */
1249 if (expand_switch_as_decision_tree_p (range
, uniq
, count
))
1250 emit_case_decision_tree (index_expr
, index_type
,
1251 case_list
, default_label
,
1254 emit_case_dispatch_table (index_expr
, index_type
,
1255 case_list
, default_label
,
1256 minval
, maxval
, range
, bb
);
1258 reorder_insns (NEXT_INSN (before_case
), get_last_insn (), before_case
);
1263 /* Expand the dispatch to a short decrement chain if there are few cases
1264 to dispatch to. Likewise if neither casesi nor tablejump is available,
1265 or if flag_jump_tables is set. Otherwise, expand as a casesi or a
1266 tablejump. The index mode is always the mode of integer_type_node.
1267 Trap if no case matches the index.
1269 DISPATCH_INDEX is the index expression to switch on. It should be a
1270 memory or register operand.
1272 DISPATCH_TABLE is a set of case labels. The set should be sorted in
1273 ascending order, be contiguous, starting with value 0, and contain only
1274 single-valued case labels. */
1277 expand_sjlj_dispatch_table (rtx dispatch_index
,
1278 vec
<tree
> dispatch_table
)
1280 tree index_type
= integer_type_node
;
1281 machine_mode index_mode
= TYPE_MODE (index_type
);
1283 int ncases
= dispatch_table
.length ();
1285 do_pending_stack_adjust ();
1286 rtx_insn
*before_case
= get_last_insn ();
1288 /* Expand as a decrement-chain if there are 5 or fewer dispatch
1289 labels. This covers more than 98% of the cases in libjava,
1290 and seems to be a reasonable compromise between the "old way"
1291 of expanding as a decision tree or dispatch table vs. the "new
1292 way" with decrement chain or dispatch table. */
1293 if (dispatch_table
.length () <= 5
1294 || (!HAVE_casesi
&& !HAVE_tablejump
)
1295 || !flag_jump_tables
)
1297 /* Expand the dispatch as a decrement chain:
1299 "switch(index) {case 0: do_0; case 1: do_1; ...; case N: do_N;}"
1303 if (index == 0) do_0; else index--;
1304 if (index == 0) do_1; else index--;
1306 if (index == 0) do_N; else index--;
1308 This is more efficient than a dispatch table on most machines.
1309 The last "index--" is redundant but the code is trivially dead
1310 and will be cleaned up by later passes. */
1311 rtx index
= copy_to_mode_reg (index_mode
, dispatch_index
);
1312 rtx zero
= CONST0_RTX (index_mode
);
1313 for (int i
= 0; i
< ncases
; i
++)
1315 tree elt
= dispatch_table
[i
];
1316 rtx_code_label
*lab
= jump_target_rtx (CASE_LABEL (elt
));
1317 do_jump_if_equal (index_mode
, index
, zero
, lab
, 0, -1);
1318 force_expand_binop (index_mode
, sub_optab
,
1319 index
, CONST1_RTX (index_mode
),
1320 index
, 0, OPTAB_DIRECT
);
1325 /* Similar to expand_case, but much simpler. */
1326 struct case_node
*case_list
= 0;
1327 pool_allocator
<case_node
> case_node_pool ("struct sjlj_case pool",
1329 tree index_expr
= make_tree (index_type
, dispatch_index
);
1330 tree minval
= build_int_cst (index_type
, 0);
1331 tree maxval
= CASE_LOW (dispatch_table
.last ());
1332 tree range
= maxval
;
1333 rtx_code_label
*default_label
= gen_label_rtx ();
1335 for (int i
= ncases
- 1; i
>= 0; --i
)
1337 tree elt
= dispatch_table
[i
];
1338 tree low
= CASE_LOW (elt
);
1339 tree lab
= CASE_LABEL (elt
);
1340 case_list
= add_case_node (case_list
, low
, low
, lab
, 0, case_node_pool
);
1343 emit_case_dispatch_table (index_expr
, index_type
,
1344 case_list
, default_label
,
1345 minval
, maxval
, range
,
1346 BLOCK_FOR_INSN (before_case
));
1347 emit_label (default_label
);
1350 /* Dispatching something not handled? Trap! */
1351 expand_builtin_trap ();
1353 reorder_insns (NEXT_INSN (before_case
), get_last_insn (), before_case
);
1359 /* Take an ordered list of case nodes
1360 and transform them into a near optimal binary tree,
1361 on the assumption that any target code selection value is as
1362 likely as any other.
1364 The transformation is performed by splitting the ordered
1365 list into two equal sections plus a pivot. The parts are
1366 then attached to the pivot as left and right branches. Each
1367 branch is then transformed recursively. */
1370 balance_case_nodes (case_node_ptr
*head
, case_node_ptr parent
)
1382 /* Count the number of entries on branch. Also count the ranges. */
1386 if (!tree_int_cst_equal (np
->low
, np
->high
))
1395 /* Split this list if it is long enough for that to help. */
1399 /* If there are just three nodes, split at the middle one. */
1401 npp
= &(*npp
)->right
;
1404 /* Find the place in the list that bisects the list's total cost,
1405 where ranges count as 2.
1406 Here I gets half the total cost. */
1407 i
= (i
+ ranges
+ 1) / 2;
1410 /* Skip nodes while their cost does not reach that amount. */
1411 if (!tree_int_cst_equal ((*npp
)->low
, (*npp
)->high
))
1416 npp
= &(*npp
)->right
;
1421 np
->parent
= parent
;
1424 /* Optimize each of the two split parts. */
1425 balance_case_nodes (&np
->left
, np
);
1426 balance_case_nodes (&np
->right
, np
);
1427 np
->subtree_prob
= np
->prob
;
1428 np
->subtree_prob
+= np
->left
->subtree_prob
;
1429 np
->subtree_prob
+= np
->right
->subtree_prob
;
1433 /* Else leave this branch as one level,
1434 but fill in `parent' fields. */
1436 np
->parent
= parent
;
1437 np
->subtree_prob
= np
->prob
;
1438 for (; np
->right
; np
= np
->right
)
1440 np
->right
->parent
= np
;
1441 (*head
)->subtree_prob
+= np
->right
->subtree_prob
;
1447 /* Search the parent sections of the case node tree
1448 to see if a test for the lower bound of NODE would be redundant.
1449 INDEX_TYPE is the type of the index expression.
1451 The instructions to generate the case decision tree are
1452 output in the same order as nodes are processed so it is
1453 known that if a parent node checks the range of the current
1454 node minus one that the current node is bounded at its lower
1455 span. Thus the test would be redundant. */
1458 node_has_low_bound (case_node_ptr node
, tree index_type
)
1461 case_node_ptr pnode
;
1463 /* If the lower bound of this node is the lowest value in the index type,
1464 we need not test it. */
1466 if (tree_int_cst_equal (node
->low
, TYPE_MIN_VALUE (index_type
)))
1469 /* If this node has a left branch, the value at the left must be less
1470 than that at this node, so it cannot be bounded at the bottom and
1471 we need not bother testing any further. */
1476 low_minus_one
= fold_build2 (MINUS_EXPR
, TREE_TYPE (node
->low
),
1478 build_int_cst (TREE_TYPE (node
->low
), 1));
1480 /* If the subtraction above overflowed, we can't verify anything.
1481 Otherwise, look for a parent that tests our value - 1. */
1483 if (! tree_int_cst_lt (low_minus_one
, node
->low
))
1486 for (pnode
= node
->parent
; pnode
; pnode
= pnode
->parent
)
1487 if (tree_int_cst_equal (low_minus_one
, pnode
->high
))
1493 /* Search the parent sections of the case node tree
1494 to see if a test for the upper bound of NODE would be redundant.
1495 INDEX_TYPE is the type of the index expression.
1497 The instructions to generate the case decision tree are
1498 output in the same order as nodes are processed so it is
1499 known that if a parent node checks the range of the current
1500 node plus one that the current node is bounded at its upper
1501 span. Thus the test would be redundant. */
1504 node_has_high_bound (case_node_ptr node
, tree index_type
)
1507 case_node_ptr pnode
;
1509 /* If there is no upper bound, obviously no test is needed. */
1511 if (TYPE_MAX_VALUE (index_type
) == NULL
)
1514 /* If the upper bound of this node is the highest value in the type
1515 of the index expression, we need not test against it. */
1517 if (tree_int_cst_equal (node
->high
, TYPE_MAX_VALUE (index_type
)))
1520 /* If this node has a right branch, the value at the right must be greater
1521 than that at this node, so it cannot be bounded at the top and
1522 we need not bother testing any further. */
1527 high_plus_one
= fold_build2 (PLUS_EXPR
, TREE_TYPE (node
->high
),
1529 build_int_cst (TREE_TYPE (node
->high
), 1));
1531 /* If the addition above overflowed, we can't verify anything.
1532 Otherwise, look for a parent that tests our value + 1. */
1534 if (! tree_int_cst_lt (node
->high
, high_plus_one
))
1537 for (pnode
= node
->parent
; pnode
; pnode
= pnode
->parent
)
1538 if (tree_int_cst_equal (high_plus_one
, pnode
->low
))
1544 /* Search the parent sections of the
1545 case node tree to see if both tests for the upper and lower
1546 bounds of NODE would be redundant. */
1549 node_is_bounded (case_node_ptr node
, tree index_type
)
1551 return (node_has_low_bound (node
, index_type
)
1552 && node_has_high_bound (node
, index_type
));
1556 /* Emit step-by-step code to select a case for the value of INDEX.
1557 The thus generated decision tree follows the form of the
1558 case-node binary tree NODE, whose nodes represent test conditions.
1559 INDEX_TYPE is the type of the index of the switch.
1561 Care is taken to prune redundant tests from the decision tree
1562 by detecting any boundary conditions already checked by
1563 emitted rtx. (See node_has_high_bound, node_has_low_bound
1564 and node_is_bounded, above.)
1566 Where the test conditions can be shown to be redundant we emit
1567 an unconditional jump to the target code. As a further
1568 optimization, the subordinates of a tree node are examined to
1569 check for bounded nodes. In this case conditional and/or
1570 unconditional jumps as a result of the boundary check for the
1571 current node are arranged to target the subordinates associated
1572 code for out of bound conditions on the current node.
1574 We can assume that when control reaches the code generated here,
1575 the index value has already been compared with the parents
1576 of this node, and determined to be on the same side of each parent
1577 as this node is. Thus, if this node tests for the value 51,
1578 and a parent tested for 52, we don't need to consider
1579 the possibility of a value greater than 51. If another parent
1580 tests for the value 50, then this node need not test anything. */
1583 emit_case_nodes (rtx index
, case_node_ptr node
, rtx_code_label
*default_label
,
1584 int default_prob
, tree index_type
)
1586 /* If INDEX has an unsigned type, we must make unsigned branches. */
1587 int unsignedp
= TYPE_UNSIGNED (index_type
);
1589 int prob
= node
->prob
, subtree_prob
= node
->subtree_prob
;
1590 machine_mode mode
= GET_MODE (index
);
1591 machine_mode imode
= TYPE_MODE (index_type
);
1593 /* Handle indices detected as constant during RTL expansion. */
1594 if (mode
== VOIDmode
)
1597 /* See if our parents have already tested everything for us.
1598 If they have, emit an unconditional jump for this node. */
1599 if (node_is_bounded (node
, index_type
))
1600 emit_jump (label_rtx (node
->code_label
));
1602 else if (tree_int_cst_equal (node
->low
, node
->high
))
1604 probability
= conditional_probability (prob
, subtree_prob
+ default_prob
);
1605 /* Node is single valued. First see if the index expression matches
1606 this node and then check our children, if any. */
1607 do_jump_if_equal (mode
, index
,
1608 convert_modes (mode
, imode
,
1609 expand_normal (node
->low
),
1611 jump_target_rtx (node
->code_label
),
1612 unsignedp
, probability
);
1613 /* Since this case is taken at this point, reduce its weight from
1615 subtree_prob
-= prob
;
1616 if (node
->right
!= 0 && node
->left
!= 0)
1618 /* This node has children on both sides.
1619 Dispatch to one side or the other
1620 by comparing the index value with this node's value.
1621 If one subtree is bounded, check that one first,
1622 so we can avoid real branches in the tree. */
1624 if (node_is_bounded (node
->right
, index_type
))
1626 probability
= conditional_probability (
1628 subtree_prob
+ default_prob
);
1629 emit_cmp_and_jump_insns (index
,
1632 expand_normal (node
->high
),
1634 GT
, NULL_RTX
, mode
, unsignedp
,
1635 label_rtx (node
->right
->code_label
),
1637 emit_case_nodes (index
, node
->left
, default_label
, default_prob
,
1641 else if (node_is_bounded (node
->left
, index_type
))
1643 probability
= conditional_probability (
1645 subtree_prob
+ default_prob
);
1646 emit_cmp_and_jump_insns (index
,
1649 expand_normal (node
->high
),
1651 LT
, NULL_RTX
, mode
, unsignedp
,
1652 label_rtx (node
->left
->code_label
),
1654 emit_case_nodes (index
, node
->right
, default_label
, default_prob
,
1658 /* If both children are single-valued cases with no
1659 children, finish up all the work. This way, we can save
1660 one ordered comparison. */
1661 else if (tree_int_cst_equal (node
->right
->low
, node
->right
->high
)
1662 && node
->right
->left
== 0
1663 && node
->right
->right
== 0
1664 && tree_int_cst_equal (node
->left
->low
, node
->left
->high
)
1665 && node
->left
->left
== 0
1666 && node
->left
->right
== 0)
1668 /* Neither node is bounded. First distinguish the two sides;
1669 then emit the code for one side at a time. */
1671 /* See if the value matches what the right hand side
1673 probability
= conditional_probability (
1675 subtree_prob
+ default_prob
);
1676 do_jump_if_equal (mode
, index
,
1677 convert_modes (mode
, imode
,
1678 expand_normal (node
->right
->low
),
1680 jump_target_rtx (node
->right
->code_label
),
1681 unsignedp
, probability
);
1683 /* See if the value matches what the left hand side
1685 probability
= conditional_probability (
1687 subtree_prob
+ default_prob
);
1688 do_jump_if_equal (mode
, index
,
1689 convert_modes (mode
, imode
,
1690 expand_normal (node
->left
->low
),
1692 jump_target_rtx (node
->left
->code_label
),
1693 unsignedp
, probability
);
1698 /* Neither node is bounded. First distinguish the two sides;
1699 then emit the code for one side at a time. */
1702 = build_decl (curr_insn_location (),
1703 LABEL_DECL
, NULL_TREE
, void_type_node
);
1705 /* The default label could be reached either through the right
1706 subtree or the left subtree. Divide the probability
1708 probability
= conditional_probability (
1709 node
->right
->subtree_prob
+ default_prob
/2,
1710 subtree_prob
+ default_prob
);
1711 /* See if the value is on the right. */
1712 emit_cmp_and_jump_insns (index
,
1715 expand_normal (node
->high
),
1717 GT
, NULL_RTX
, mode
, unsignedp
,
1718 label_rtx (test_label
),
1722 /* Value must be on the left.
1723 Handle the left-hand subtree. */
1724 emit_case_nodes (index
, node
->left
, default_label
, default_prob
, index_type
);
1725 /* If left-hand subtree does nothing,
1728 emit_jump (default_label
);
1730 /* Code branches here for the right-hand subtree. */
1731 expand_label (test_label
);
1732 emit_case_nodes (index
, node
->right
, default_label
, default_prob
, index_type
);
1736 else if (node
->right
!= 0 && node
->left
== 0)
1738 /* Here we have a right child but no left so we issue a conditional
1739 branch to default and process the right child.
1741 Omit the conditional branch to default if the right child
1742 does not have any children and is single valued; it would
1743 cost too much space to save so little time. */
1745 if (node
->right
->right
|| node
->right
->left
1746 || !tree_int_cst_equal (node
->right
->low
, node
->right
->high
))
1748 if (!node_has_low_bound (node
, index_type
))
1750 probability
= conditional_probability (
1752 subtree_prob
+ default_prob
);
1753 emit_cmp_and_jump_insns (index
,
1756 expand_normal (node
->high
),
1758 LT
, NULL_RTX
, mode
, unsignedp
,
1764 emit_case_nodes (index
, node
->right
, default_label
, default_prob
, index_type
);
1768 probability
= conditional_probability (
1769 node
->right
->subtree_prob
,
1770 subtree_prob
+ default_prob
);
1771 /* We cannot process node->right normally
1772 since we haven't ruled out the numbers less than
1773 this node's value. So handle node->right explicitly. */
1774 do_jump_if_equal (mode
, index
,
1777 expand_normal (node
->right
->low
),
1779 jump_target_rtx (node
->right
->code_label
),
1780 unsignedp
, probability
);
1784 else if (node
->right
== 0 && node
->left
!= 0)
1786 /* Just one subtree, on the left. */
1787 if (node
->left
->left
|| node
->left
->right
1788 || !tree_int_cst_equal (node
->left
->low
, node
->left
->high
))
1790 if (!node_has_high_bound (node
, index_type
))
1792 probability
= conditional_probability (
1794 subtree_prob
+ default_prob
);
1795 emit_cmp_and_jump_insns (index
,
1798 expand_normal (node
->high
),
1800 GT
, NULL_RTX
, mode
, unsignedp
,
1806 emit_case_nodes (index
, node
->left
, default_label
,
1807 default_prob
, index_type
);
1811 probability
= conditional_probability (
1812 node
->left
->subtree_prob
,
1813 subtree_prob
+ default_prob
);
1814 /* We cannot process node->left normally
1815 since we haven't ruled out the numbers less than
1816 this node's value. So handle node->left explicitly. */
1817 do_jump_if_equal (mode
, index
,
1820 expand_normal (node
->left
->low
),
1822 jump_target_rtx (node
->left
->code_label
),
1823 unsignedp
, probability
);
1829 /* Node is a range. These cases are very similar to those for a single
1830 value, except that we do not start by testing whether this node
1831 is the one to branch to. */
1833 if (node
->right
!= 0 && node
->left
!= 0)
1835 /* Node has subtrees on both sides.
1836 If the right-hand subtree is bounded,
1837 test for it first, since we can go straight there.
1838 Otherwise, we need to make a branch in the control structure,
1839 then handle the two subtrees. */
1840 tree test_label
= 0;
1842 if (node_is_bounded (node
->right
, index_type
))
1844 /* Right hand node is fully bounded so we can eliminate any
1845 testing and branch directly to the target code. */
1846 probability
= conditional_probability (
1847 node
->right
->subtree_prob
,
1848 subtree_prob
+ default_prob
);
1849 emit_cmp_and_jump_insns (index
,
1852 expand_normal (node
->high
),
1854 GT
, NULL_RTX
, mode
, unsignedp
,
1855 label_rtx (node
->right
->code_label
),
1860 /* Right hand node requires testing.
1861 Branch to a label where we will handle it later. */
1863 test_label
= build_decl (curr_insn_location (),
1864 LABEL_DECL
, NULL_TREE
, void_type_node
);
1865 probability
= conditional_probability (
1866 node
->right
->subtree_prob
+ default_prob
/2,
1867 subtree_prob
+ default_prob
);
1868 emit_cmp_and_jump_insns (index
,
1871 expand_normal (node
->high
),
1873 GT
, NULL_RTX
, mode
, unsignedp
,
1874 label_rtx (test_label
),
1879 /* Value belongs to this node or to the left-hand subtree. */
1881 probability
= conditional_probability (
1883 subtree_prob
+ default_prob
);
1884 emit_cmp_and_jump_insns (index
,
1887 expand_normal (node
->low
),
1889 GE
, NULL_RTX
, mode
, unsignedp
,
1890 label_rtx (node
->code_label
),
1893 /* Handle the left-hand subtree. */
1894 emit_case_nodes (index
, node
->left
, default_label
, default_prob
, index_type
);
1896 /* If right node had to be handled later, do that now. */
1900 /* If the left-hand subtree fell through,
1901 don't let it fall into the right-hand subtree. */
1903 emit_jump (default_label
);
1905 expand_label (test_label
);
1906 emit_case_nodes (index
, node
->right
, default_label
, default_prob
, index_type
);
1910 else if (node
->right
!= 0 && node
->left
== 0)
1912 /* Deal with values to the left of this node,
1913 if they are possible. */
1914 if (!node_has_low_bound (node
, index_type
))
1916 probability
= conditional_probability (
1918 subtree_prob
+ default_prob
);
1919 emit_cmp_and_jump_insns (index
,
1922 expand_normal (node
->low
),
1924 LT
, NULL_RTX
, mode
, unsignedp
,
1930 /* Value belongs to this node or to the right-hand subtree. */
1932 probability
= conditional_probability (
1934 subtree_prob
+ default_prob
);
1935 emit_cmp_and_jump_insns (index
,
1938 expand_normal (node
->high
),
1940 LE
, NULL_RTX
, mode
, unsignedp
,
1941 label_rtx (node
->code_label
),
1944 emit_case_nodes (index
, node
->right
, default_label
, default_prob
, index_type
);
1947 else if (node
->right
== 0 && node
->left
!= 0)
1949 /* Deal with values to the right of this node,
1950 if they are possible. */
1951 if (!node_has_high_bound (node
, index_type
))
1953 probability
= conditional_probability (
1955 subtree_prob
+ default_prob
);
1956 emit_cmp_and_jump_insns (index
,
1959 expand_normal (node
->high
),
1961 GT
, NULL_RTX
, mode
, unsignedp
,
1967 /* Value belongs to this node or to the left-hand subtree. */
1969 probability
= conditional_probability (
1971 subtree_prob
+ default_prob
);
1972 emit_cmp_and_jump_insns (index
,
1975 expand_normal (node
->low
),
1977 GE
, NULL_RTX
, mode
, unsignedp
,
1978 label_rtx (node
->code_label
),
1981 emit_case_nodes (index
, node
->left
, default_label
, default_prob
, index_type
);
1986 /* Node has no children so we check low and high bounds to remove
1987 redundant tests. Only one of the bounds can exist,
1988 since otherwise this node is bounded--a case tested already. */
1989 int high_bound
= node_has_high_bound (node
, index_type
);
1990 int low_bound
= node_has_low_bound (node
, index_type
);
1992 if (!high_bound
&& low_bound
)
1994 probability
= conditional_probability (
1996 subtree_prob
+ default_prob
);
1997 emit_cmp_and_jump_insns (index
,
2000 expand_normal (node
->high
),
2002 GT
, NULL_RTX
, mode
, unsignedp
,
2007 else if (!low_bound
&& high_bound
)
2009 probability
= conditional_probability (
2011 subtree_prob
+ default_prob
);
2012 emit_cmp_and_jump_insns (index
,
2015 expand_normal (node
->low
),
2017 LT
, NULL_RTX
, mode
, unsignedp
,
2021 else if (!low_bound
&& !high_bound
)
2023 /* Widen LOW and HIGH to the same width as INDEX. */
2024 tree type
= lang_hooks
.types
.type_for_mode (mode
, unsignedp
);
2025 tree low
= build1 (CONVERT_EXPR
, type
, node
->low
);
2026 tree high
= build1 (CONVERT_EXPR
, type
, node
->high
);
2027 rtx low_rtx
, new_index
, new_bound
;
2029 /* Instead of doing two branches, emit one unsigned branch for
2030 (index-low) > (high-low). */
2031 low_rtx
= expand_expr (low
, NULL_RTX
, mode
, EXPAND_NORMAL
);
2032 new_index
= expand_simple_binop (mode
, MINUS
, index
, low_rtx
,
2033 NULL_RTX
, unsignedp
,
2035 new_bound
= expand_expr (fold_build2 (MINUS_EXPR
, type
,
2037 NULL_RTX
, mode
, EXPAND_NORMAL
);
2039 probability
= conditional_probability (
2041 subtree_prob
+ default_prob
);
2042 emit_cmp_and_jump_insns (new_index
, new_bound
, GT
, NULL_RTX
,
2043 mode
, 1, default_label
, probability
);
2046 emit_jump (jump_target_rtx (node
->code_label
));