1 /* Expands front end tree to back end RTL for GCC
2 Copyright (C) 1987-2013 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"
37 #include "insn-config.h"
42 #include "diagnostic-core.h"
45 #include "langhooks.h"
51 #include "alloc-pool.h"
52 #include "pretty-print.h"
53 #include "pointer-set.h"
58 /* Functions and data structures for expanding case statements. */
60 /* Case label structure, used to hold info on labels within case
61 statements. We handle "range" labels; for a single-value label
62 as in C, the high and low limits are the same.
64 We start with a vector of case nodes sorted in ascending order, and
65 the default label as the last element in the vector. Before expanding
66 to RTL, we transform this vector into a list linked via the RIGHT
67 fields in the case_node struct. Nodes with higher case values are
70 Switch statements can be output in three forms. A branch table is
71 used if there are more than a few labels and the labels are dense
72 within the range between the smallest and largest case value. If a
73 branch table is used, no further manipulations are done with the case
76 The alternative to the use of a branch table is to generate a series
77 of compare and jump insns. When that is done, we use the LEFT, RIGHT,
78 and PARENT fields to hold a binary tree. Initially the tree is
79 totally unbalanced, with everything on the right. We balance the tree
80 with nodes on the left having lower case values than the parent
81 and nodes on the right having higher values. We then output the tree
84 For very small, suitable switch statements, we can generate a series
85 of simple bit test and branches instead. */
89 struct case_node
*left
; /* Left son in binary tree */
90 struct case_node
*right
; /* Right son in binary tree; also node chain */
91 struct case_node
*parent
; /* Parent of node in binary tree */
92 tree low
; /* Lowest index value for this label */
93 tree high
; /* Highest index value for this label */
94 tree code_label
; /* Label to jump to when node matches */
95 int prob
; /* Probability of taking this case. */
96 /* Probability of reaching subtree rooted at this node */
100 typedef struct case_node case_node
;
101 typedef struct case_node
*case_node_ptr
;
103 extern basic_block
label_to_block_fn (struct function
*, tree
);
105 static bool check_unique_operand_names (tree
, tree
, tree
);
106 static char *resolve_operand_name_1 (char *, tree
, tree
, tree
);
107 static void balance_case_nodes (case_node_ptr
*, case_node_ptr
);
108 static int node_has_low_bound (case_node_ptr
, tree
);
109 static int node_has_high_bound (case_node_ptr
, tree
);
110 static int node_is_bounded (case_node_ptr
, tree
);
111 static void emit_case_nodes (rtx
, case_node_ptr
, rtx
, int, tree
);
113 /* Return the rtx-label that corresponds to a LABEL_DECL,
114 creating it if necessary. */
117 label_rtx (tree label
)
119 gcc_assert (TREE_CODE (label
) == LABEL_DECL
);
121 if (!DECL_RTL_SET_P (label
))
123 rtx r
= gen_label_rtx ();
124 SET_DECL_RTL (label
, r
);
125 if (FORCED_LABEL (label
) || DECL_NONLOCAL (label
))
126 LABEL_PRESERVE_P (r
) = 1;
129 return DECL_RTL (label
);
132 /* As above, but also put it on the forced-reference list of the
133 function that contains it. */
135 force_label_rtx (tree label
)
137 rtx ref
= label_rtx (label
);
138 tree function
= decl_function_context (label
);
140 gcc_assert (function
);
142 forced_labels
= gen_rtx_EXPR_LIST (VOIDmode
, ref
, forced_labels
);
146 /* Add an unconditional jump to LABEL as the next sequential instruction. */
149 emit_jump (rtx label
)
151 do_pending_stack_adjust ();
152 emit_jump_insn (gen_jump (label
));
156 /* Handle goto statements and the labels that they can go to. */
158 /* Specify the location in the RTL code of a label LABEL,
159 which is a LABEL_DECL tree node.
161 This is used for the kind of label that the user can jump to with a
162 goto statement, and for alternatives of a switch or case statement.
163 RTL labels generated for loops and conditionals don't go through here;
164 they are generated directly at the RTL level, by other functions below.
166 Note that this has nothing to do with defining label *names*.
167 Languages vary in how they do that and what that even means. */
170 expand_label (tree label
)
172 rtx label_r
= label_rtx (label
);
174 do_pending_stack_adjust ();
175 emit_label (label_r
);
176 if (DECL_NAME (label
))
177 LABEL_NAME (DECL_RTL (label
)) = IDENTIFIER_POINTER (DECL_NAME (label
));
179 if (DECL_NONLOCAL (label
))
181 expand_builtin_setjmp_receiver (NULL
);
182 nonlocal_goto_handler_labels
183 = gen_rtx_EXPR_LIST (VOIDmode
, label_r
,
184 nonlocal_goto_handler_labels
);
187 if (FORCED_LABEL (label
))
188 forced_labels
= gen_rtx_EXPR_LIST (VOIDmode
, label_r
, forced_labels
);
190 if (DECL_NONLOCAL (label
) || FORCED_LABEL (label
))
191 maybe_set_first_label_num (label_r
);
194 /* Parse the output constraint pointed to by *CONSTRAINT_P. It is the
195 OPERAND_NUMth output operand, indexed from zero. There are NINPUTS
196 inputs and NOUTPUTS outputs to this extended-asm. Upon return,
197 *ALLOWS_MEM will be TRUE iff the constraint allows the use of a
198 memory operand. Similarly, *ALLOWS_REG will be TRUE iff the
199 constraint allows the use of a register operand. And, *IS_INOUT
200 will be true if the operand is read-write, i.e., if it is used as
201 an input as well as an output. If *CONSTRAINT_P is not in
202 canonical form, it will be made canonical. (Note that `+' will be
203 replaced with `=' as part of this process.)
205 Returns TRUE if all went well; FALSE if an error occurred. */
208 parse_output_constraint (const char **constraint_p
, int operand_num
,
209 int ninputs
, int noutputs
, bool *allows_mem
,
210 bool *allows_reg
, bool *is_inout
)
212 const char *constraint
= *constraint_p
;
215 /* Assume the constraint doesn't allow the use of either a register
220 /* Allow the `=' or `+' to not be at the beginning of the string,
221 since it wasn't explicitly documented that way, and there is a
222 large body of code that puts it last. Swap the character to
223 the front, so as not to uglify any place else. */
224 p
= strchr (constraint
, '=');
226 p
= strchr (constraint
, '+');
228 /* If the string doesn't contain an `=', issue an error
232 error ("output operand constraint lacks %<=%>");
236 /* If the constraint begins with `+', then the operand is both read
237 from and written to. */
238 *is_inout
= (*p
== '+');
240 /* Canonicalize the output constraint so that it begins with `='. */
241 if (p
!= constraint
|| *is_inout
)
244 size_t c_len
= strlen (constraint
);
247 warning (0, "output constraint %qc for operand %d "
248 "is not at the beginning",
251 /* Make a copy of the constraint. */
252 buf
= XALLOCAVEC (char, c_len
+ 1);
253 strcpy (buf
, constraint
);
254 /* Swap the first character and the `=' or `+'. */
255 buf
[p
- constraint
] = buf
[0];
256 /* Make sure the first character is an `='. (Until we do this,
257 it might be a `+'.) */
259 /* Replace the constraint with the canonicalized string. */
260 *constraint_p
= ggc_alloc_string (buf
, c_len
);
261 constraint
= *constraint_p
;
264 /* Loop through the constraint string. */
265 for (p
= constraint
+ 1; *p
; p
+= CONSTRAINT_LEN (*p
, p
))
270 error ("operand constraint contains incorrectly positioned "
275 if (operand_num
+ 1 == ninputs
+ noutputs
)
277 error ("%<%%%> constraint used with last operand");
282 case 'V': case TARGET_MEM_CONSTRAINT
: case 'o':
286 case '?': case '!': case '*': case '&': case '#':
287 case 'E': case 'F': case 'G': case 'H':
288 case 's': case 'i': case 'n':
289 case 'I': case 'J': case 'K': case 'L': case 'M':
290 case 'N': case 'O': case 'P': case ',':
293 case '0': case '1': case '2': case '3': case '4':
294 case '5': case '6': case '7': case '8': case '9':
296 error ("matching constraint not valid in output operand");
300 /* ??? Before flow, auto inc/dec insns are not supposed to exist,
301 excepting those that expand_call created. So match memory
318 if (REG_CLASS_FROM_CONSTRAINT (*p
, p
) != NO_REGS
)
320 #ifdef EXTRA_CONSTRAINT_STR
321 else if (EXTRA_ADDRESS_CONSTRAINT (*p
, p
))
323 else if (EXTRA_MEMORY_CONSTRAINT (*p
, p
))
327 /* Otherwise we can't assume anything about the nature of
328 the constraint except that it isn't purely registers.
329 Treat it like "g" and hope for the best. */
340 /* Similar, but for input constraints. */
343 parse_input_constraint (const char **constraint_p
, int input_num
,
344 int ninputs
, int noutputs
, int ninout
,
345 const char * const * constraints
,
346 bool *allows_mem
, bool *allows_reg
)
348 const char *constraint
= *constraint_p
;
349 const char *orig_constraint
= constraint
;
350 size_t c_len
= strlen (constraint
);
352 bool saw_match
= false;
354 /* Assume the constraint doesn't allow the use of either
355 a register or memory. */
359 /* Make sure constraint has neither `=', `+', nor '&'. */
361 for (j
= 0; j
< c_len
; j
+= CONSTRAINT_LEN (constraint
[j
], constraint
+j
))
362 switch (constraint
[j
])
364 case '+': case '=': case '&':
365 if (constraint
== orig_constraint
)
367 error ("input operand constraint contains %qc", constraint
[j
]);
373 if (constraint
== orig_constraint
374 && input_num
+ 1 == ninputs
- ninout
)
376 error ("%<%%%> constraint used with last operand");
381 case 'V': case TARGET_MEM_CONSTRAINT
: case 'o':
386 case '?': case '!': case '*': case '#':
387 case 'E': case 'F': case 'G': case 'H':
388 case 's': case 'i': case 'n':
389 case 'I': case 'J': case 'K': case 'L': case 'M':
390 case 'N': case 'O': case 'P': case ',':
393 /* Whether or not a numeric constraint allows a register is
394 decided by the matching constraint, and so there is no need
395 to do anything special with them. We must handle them in
396 the default case, so that we don't unnecessarily force
397 operands to memory. */
398 case '0': case '1': case '2': case '3': case '4':
399 case '5': case '6': case '7': case '8': case '9':
406 match
= strtoul (constraint
+ j
, &end
, 10);
407 if (match
>= (unsigned long) noutputs
)
409 error ("matching constraint references invalid operand number");
413 /* Try and find the real constraint for this dup. Only do this
414 if the matching constraint is the only alternative. */
416 && (j
== 0 || (j
== 1 && constraint
[0] == '%')))
418 constraint
= constraints
[match
];
419 *constraint_p
= constraint
;
420 c_len
= strlen (constraint
);
422 /* ??? At the end of the loop, we will skip the first part of
423 the matched constraint. This assumes not only that the
424 other constraint is an output constraint, but also that
425 the '=' or '+' come first. */
429 j
= end
- constraint
;
430 /* Anticipate increment at end of loop. */
445 if (! ISALPHA (constraint
[j
]))
447 error ("invalid punctuation %qc in constraint", constraint
[j
]);
450 if (REG_CLASS_FROM_CONSTRAINT (constraint
[j
], constraint
+ j
)
453 #ifdef EXTRA_CONSTRAINT_STR
454 else if (EXTRA_ADDRESS_CONSTRAINT (constraint
[j
], constraint
+ j
))
456 else if (EXTRA_MEMORY_CONSTRAINT (constraint
[j
], constraint
+ j
))
460 /* Otherwise we can't assume anything about the nature of
461 the constraint except that it isn't purely registers.
462 Treat it like "g" and hope for the best. */
470 if (saw_match
&& !*allows_reg
)
471 warning (0, "matching constraint does not allow a register");
476 /* Return DECL iff there's an overlap between *REGS and DECL, where DECL
477 can be an asm-declared register. Called via walk_tree. */
480 decl_overlaps_hard_reg_set_p (tree
*declp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
484 const HARD_REG_SET
*const regs
= (const HARD_REG_SET
*) data
;
486 if (TREE_CODE (decl
) == VAR_DECL
)
488 if (DECL_HARD_REGISTER (decl
)
489 && REG_P (DECL_RTL (decl
))
490 && REGNO (DECL_RTL (decl
)) < FIRST_PSEUDO_REGISTER
)
492 rtx reg
= DECL_RTL (decl
);
494 if (overlaps_hard_reg_set_p (*regs
, GET_MODE (reg
), REGNO (reg
)))
499 else if (TYPE_P (decl
) || TREE_CODE (decl
) == PARM_DECL
)
504 /* If there is an overlap between *REGS and DECL, return the first overlap
507 tree_overlaps_hard_reg_set (tree decl
, HARD_REG_SET
*regs
)
509 return walk_tree (&decl
, decl_overlaps_hard_reg_set_p
, regs
, NULL
);
513 /* A subroutine of expand_asm_operands. Check that all operand names
514 are unique. Return true if so. We rely on the fact that these names
515 are identifiers, and so have been canonicalized by get_identifier,
516 so all we need are pointer comparisons. */
519 check_unique_operand_names (tree outputs
, tree inputs
, tree labels
)
521 tree i
, j
, i_name
= NULL_TREE
;
523 for (i
= outputs
; i
; i
= TREE_CHAIN (i
))
525 i_name
= TREE_PURPOSE (TREE_PURPOSE (i
));
529 for (j
= TREE_CHAIN (i
); j
; j
= TREE_CHAIN (j
))
530 if (simple_cst_equal (i_name
, TREE_PURPOSE (TREE_PURPOSE (j
))))
534 for (i
= inputs
; i
; i
= TREE_CHAIN (i
))
536 i_name
= TREE_PURPOSE (TREE_PURPOSE (i
));
540 for (j
= TREE_CHAIN (i
); j
; j
= TREE_CHAIN (j
))
541 if (simple_cst_equal (i_name
, TREE_PURPOSE (TREE_PURPOSE (j
))))
543 for (j
= outputs
; j
; j
= TREE_CHAIN (j
))
544 if (simple_cst_equal (i_name
, TREE_PURPOSE (TREE_PURPOSE (j
))))
548 for (i
= labels
; i
; i
= TREE_CHAIN (i
))
550 i_name
= TREE_PURPOSE (i
);
554 for (j
= TREE_CHAIN (i
); j
; j
= TREE_CHAIN (j
))
555 if (simple_cst_equal (i_name
, TREE_PURPOSE (j
)))
557 for (j
= inputs
; j
; j
= TREE_CHAIN (j
))
558 if (simple_cst_equal (i_name
, TREE_PURPOSE (TREE_PURPOSE (j
))))
565 error ("duplicate asm operand name %qs", TREE_STRING_POINTER (i_name
));
569 /* A subroutine of expand_asm_operands. Resolve the names of the operands
570 in *POUTPUTS and *PINPUTS to numbers, and replace the name expansions in
571 STRING and in the constraints to those numbers. */
574 resolve_asm_operand_names (tree string
, tree outputs
, tree inputs
, tree labels
)
581 check_unique_operand_names (outputs
, inputs
, labels
);
583 /* Substitute [<name>] in input constraint strings. There should be no
584 named operands in output constraints. */
585 for (t
= inputs
; t
; t
= TREE_CHAIN (t
))
587 c
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t
)));
588 if (strchr (c
, '[') != NULL
)
590 p
= buffer
= xstrdup (c
);
591 while ((p
= strchr (p
, '[')) != NULL
)
592 p
= resolve_operand_name_1 (p
, outputs
, inputs
, NULL
);
593 TREE_VALUE (TREE_PURPOSE (t
))
594 = build_string (strlen (buffer
), buffer
);
599 /* Now check for any needed substitutions in the template. */
600 c
= TREE_STRING_POINTER (string
);
601 while ((c
= strchr (c
, '%')) != NULL
)
605 else if (ISALPHA (c
[1]) && c
[2] == '[')
609 c
+= 1 + (c
[1] == '%');
616 /* OK, we need to make a copy so we can perform the substitutions.
617 Assume that we will not need extra space--we get to remove '['
618 and ']', which means we cannot have a problem until we have more
619 than 999 operands. */
620 buffer
= xstrdup (TREE_STRING_POINTER (string
));
621 p
= buffer
+ (c
- TREE_STRING_POINTER (string
));
623 while ((p
= strchr (p
, '%')) != NULL
)
627 else if (ISALPHA (p
[1]) && p
[2] == '[')
631 p
+= 1 + (p
[1] == '%');
635 p
= resolve_operand_name_1 (p
, outputs
, inputs
, labels
);
638 string
= build_string (strlen (buffer
), buffer
);
645 /* A subroutine of resolve_operand_names. P points to the '[' for a
646 potential named operand of the form [<name>]. In place, replace
647 the name and brackets with a number. Return a pointer to the
648 balance of the string after substitution. */
651 resolve_operand_name_1 (char *p
, tree outputs
, tree inputs
, tree labels
)
657 /* Collect the operand name. */
658 q
= strchr (++p
, ']');
661 error ("missing close brace for named operand");
662 return strchr (p
, '\0');
666 /* Resolve the name to a number. */
667 for (op
= 0, t
= outputs
; t
; t
= TREE_CHAIN (t
), op
++)
669 tree name
= TREE_PURPOSE (TREE_PURPOSE (t
));
670 if (name
&& strcmp (TREE_STRING_POINTER (name
), p
) == 0)
673 for (t
= inputs
; t
; t
= TREE_CHAIN (t
), op
++)
675 tree name
= TREE_PURPOSE (TREE_PURPOSE (t
));
676 if (name
&& strcmp (TREE_STRING_POINTER (name
), p
) == 0)
679 for (t
= labels
; t
; t
= TREE_CHAIN (t
), op
++)
681 tree name
= TREE_PURPOSE (t
);
682 if (name
&& strcmp (TREE_STRING_POINTER (name
), p
) == 0)
686 error ("undefined named operand %qs", identifier_to_locale (p
));
690 /* Replace the name with the number. Unfortunately, not all libraries
691 get the return value of sprintf correct, so search for the end of the
692 generated string by hand. */
693 sprintf (--p
, "%d", op
);
694 p
= strchr (p
, '\0');
696 /* Verify the no extra buffer space assumption. */
699 /* Shift the rest of the buffer down to fill the gap. */
700 memmove (p
, q
+ 1, strlen (q
+ 1) + 1);
706 /* Generate RTL to return directly from the current function.
707 (That is, we bypass any return value.) */
710 expand_naked_return (void)
714 clear_pending_stack_adjust ();
715 do_pending_stack_adjust ();
717 end_label
= naked_return_label
;
719 end_label
= naked_return_label
= gen_label_rtx ();
721 emit_jump (end_label
);
724 /* Generate code to jump to LABEL if OP0 and OP1 are equal in mode MODE. PROB
725 is the probability of jumping to LABEL. */
727 do_jump_if_equal (enum machine_mode mode
, rtx op0
, rtx op1
, rtx label
,
728 int unsignedp
, int prob
)
730 gcc_assert (prob
<= REG_BR_PROB_BASE
);
731 do_compare_rtx_and_jump (op0
, op1
, EQ
, unsignedp
, mode
,
732 NULL_RTX
, NULL_RTX
, label
, prob
);
735 /* Do the insertion of a case label into case_list. The labels are
736 fed to us in descending order from the sorted vector of case labels used
737 in the tree part of the middle end. So the list we construct is
738 sorted in ascending order.
740 LABEL is the case label to be inserted. LOW and HIGH are the bounds
741 against which the index is compared to jump to LABEL and PROB is the
742 estimated probability LABEL is reached from the switch statement. */
744 static struct case_node
*
745 add_case_node (struct case_node
*head
, tree low
, tree high
,
746 tree label
, int prob
, alloc_pool case_node_pool
)
750 gcc_checking_assert (low
);
751 gcc_checking_assert (high
&& (TREE_TYPE (low
) == TREE_TYPE (high
)));
753 /* Add this label to the chain. */
754 r
= (struct case_node
*) pool_alloc (case_node_pool
);
757 r
->code_label
= label
;
758 r
->parent
= r
->left
= NULL
;
760 r
->subtree_prob
= prob
;
765 /* Dump ROOT, a list or tree of case nodes, to file. */
768 dump_case_nodes (FILE *f
, struct case_node
*root
,
769 int indent_step
, int indent_level
)
771 HOST_WIDE_INT low
, high
;
777 dump_case_nodes (f
, root
->left
, indent_step
, indent_level
);
779 low
= tree_to_shwi (root
->low
);
780 high
= tree_to_shwi (root
->high
);
784 fprintf (f
, "%*s" HOST_WIDE_INT_PRINT_DEC
,
785 indent_step
* indent_level
, "", low
);
787 fprintf (f
, "%*s" HOST_WIDE_INT_PRINT_DEC
" ... " HOST_WIDE_INT_PRINT_DEC
,
788 indent_step
* indent_level
, "", low
, high
);
791 dump_case_nodes (f
, root
->right
, indent_step
, indent_level
);
795 #define HAVE_casesi 0
798 #ifndef HAVE_tablejump
799 #define HAVE_tablejump 0
802 /* Return the smallest number of different values for which it is best to use a
803 jump-table instead of a tree of conditional branches. */
806 case_values_threshold (void)
808 unsigned int threshold
= PARAM_VALUE (PARAM_CASE_VALUES_THRESHOLD
);
811 threshold
= targetm
.case_values_threshold ();
816 /* Return true if a switch should be expanded as a decision tree.
817 RANGE is the difference between highest and lowest case.
818 UNIQ is number of unique case node targets, not counting the default case.
819 COUNT is the number of comparisons needed, not counting the default case. */
822 expand_switch_as_decision_tree_p (tree range
,
823 unsigned int uniq ATTRIBUTE_UNUSED
,
828 /* If neither casesi or tablejump is available, or flag_jump_tables
829 over-ruled us, we really have no choice. */
830 if (!HAVE_casesi
&& !HAVE_tablejump
)
832 if (!flag_jump_tables
)
834 #ifndef ASM_OUTPUT_ADDR_DIFF_ELT
839 /* If the switch is relatively small such that the cost of one
840 indirect jump on the target are higher than the cost of a
841 decision tree, go with the decision tree.
843 If range of values is much bigger than number of values,
844 or if it is too large to represent in a HOST_WIDE_INT,
845 make a sequence of conditional branches instead of a dispatch.
847 The definition of "much bigger" depends on whether we are
848 optimizing for size or for speed. If the former, the maximum
849 ratio range/count = 3, because this was found to be the optimal
850 ratio for size on i686-pc-linux-gnu, see PR11823. The ratio
851 10 is much older, and was probably selected after an extensive
852 benchmarking investigation on numerous platforms. Or maybe it
853 just made sense to someone at some point in the history of GCC,
855 max_ratio
= optimize_insn_for_size_p () ? 3 : 10;
856 if (count
< case_values_threshold ()
857 || ! tree_fits_uhwi_p (range
)
858 || compare_tree_int (range
, max_ratio
* count
) > 0)
864 /* Generate a decision tree, switching on INDEX_EXPR and jumping to
865 one of the labels in CASE_LIST or to the DEFAULT_LABEL.
866 DEFAULT_PROB is the estimated probability that it jumps to
869 We generate a binary decision tree to select the appropriate target
870 code. This is done as follows:
872 If the index is a short or char that we do not have
873 an insn to handle comparisons directly, convert it to
874 a full integer now, rather than letting each comparison
875 generate the conversion.
877 Load the index into a register.
879 The list of cases is rearranged into a binary tree,
880 nearly optimal assuming equal probability for each case.
882 The tree is transformed into RTL, eliminating redundant
883 test conditions at the same time.
885 If program flow could reach the end of the decision tree
886 an unconditional jump to the default code is emitted.
888 The above process is unaware of the CFG. The caller has to fix up
889 the CFG itself. This is done in cfgexpand.c. */
892 emit_case_decision_tree (tree index_expr
, tree index_type
,
893 struct case_node
*case_list
, rtx default_label
,
896 rtx index
= expand_normal (index_expr
);
898 if (GET_MODE_CLASS (GET_MODE (index
)) == MODE_INT
899 && ! have_insn_for (COMPARE
, GET_MODE (index
)))
901 int unsignedp
= TYPE_UNSIGNED (index_type
);
902 enum machine_mode wider_mode
;
903 for (wider_mode
= GET_MODE (index
); wider_mode
!= VOIDmode
;
904 wider_mode
= GET_MODE_WIDER_MODE (wider_mode
))
905 if (have_insn_for (COMPARE
, wider_mode
))
907 index
= convert_to_mode (wider_mode
, index
, unsignedp
);
912 do_pending_stack_adjust ();
916 index
= copy_to_reg (index
);
917 if (TREE_CODE (index_expr
) == SSA_NAME
)
918 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (index_expr
), index
);
921 balance_case_nodes (&case_list
, NULL
);
923 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
925 int indent_step
= ceil_log2 (TYPE_PRECISION (index_type
)) + 2;
926 fprintf (dump_file
, ";; Expanding GIMPLE switch as decision tree:\n");
927 dump_case_nodes (dump_file
, case_list
, indent_step
, 0);
930 emit_case_nodes (index
, case_list
, default_label
, default_prob
, index_type
);
932 emit_jump (default_label
);
935 /* Return the sum of probabilities of outgoing edges of basic block BB. */
938 get_outgoing_edge_probs (basic_block bb
)
945 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
946 prob_sum
+= e
->probability
;
950 /* Computes the conditional probability of jumping to a target if the branch
951 instruction is executed.
952 TARGET_PROB is the estimated probability of jumping to a target relative
953 to some basic block BB.
954 BASE_PROB is the probability of reaching the branch instruction relative
955 to the same basic block BB. */
958 conditional_probability (int target_prob
, int base_prob
)
962 gcc_assert (target_prob
>= 0);
963 gcc_assert (target_prob
<= base_prob
);
964 return GCOV_COMPUTE_SCALE (target_prob
, base_prob
);
969 /* Generate a dispatch tabler, switching on INDEX_EXPR and jumping to
970 one of the labels in CASE_LIST or to the DEFAULT_LABEL.
971 MINVAL, MAXVAL, and RANGE are the extrema and range of the case
972 labels in CASE_LIST. STMT_BB is the basic block containing the statement.
974 First, a jump insn is emitted. First we try "casesi". If that
975 fails, try "tablejump". A target *must* have one of them (or both).
977 Then, a table with the target labels is emitted.
979 The process is unaware of the CFG. The caller has to fix up
980 the CFG itself. This is done in cfgexpand.c. */
983 emit_case_dispatch_table (tree index_expr
, tree index_type
,
984 struct case_node
*case_list
, rtx default_label
,
985 tree minval
, tree maxval
, tree range
,
991 rtx fallback_label
= label_rtx (case_list
->code_label
);
992 rtx table_label
= gen_label_rtx ();
993 bool has_gaps
= false;
994 edge default_edge
= stmt_bb
? EDGE_SUCC (stmt_bb
, 0) : NULL
;
995 int default_prob
= default_edge
? default_edge
->probability
: 0;
996 int base
= get_outgoing_edge_probs (stmt_bb
);
997 bool try_with_tablejump
= false;
999 int new_default_prob
= conditional_probability (default_prob
,
1002 if (! try_casesi (index_type
, index_expr
, minval
, range
,
1003 table_label
, default_label
, fallback_label
,
1006 /* Index jumptables from zero for suitable values of minval to avoid
1007 a subtraction. For the rationale see:
1008 "http://gcc.gnu.org/ml/gcc-patches/2001-10/msg01234.html". */
1009 if (optimize_insn_for_speed_p ()
1010 && compare_tree_int (minval
, 0) > 0
1011 && compare_tree_int (minval
, 3) < 0)
1013 minval
= build_int_cst (index_type
, 0);
1017 try_with_tablejump
= true;
1020 /* Get table of labels to jump to, in order of case index. */
1022 ncases
= tree_to_shwi (range
) + 1;
1023 labelvec
= XALLOCAVEC (rtx
, ncases
);
1024 memset (labelvec
, 0, ncases
* sizeof (rtx
));
1026 for (n
= case_list
; n
; n
= n
->right
)
1028 /* Compute the low and high bounds relative to the minimum
1029 value since that should fit in a HOST_WIDE_INT while the
1030 actual values may not. */
1032 = tree_to_uhwi (fold_build2 (MINUS_EXPR
, index_type
,
1034 HOST_WIDE_INT i_high
1035 = tree_to_uhwi (fold_build2 (MINUS_EXPR
, index_type
,
1039 for (i
= i_low
; i
<= i_high
; i
++)
1041 = gen_rtx_LABEL_REF (Pmode
, label_rtx (n
->code_label
));
1044 /* Fill in the gaps with the default. We may have gaps at
1045 the beginning if we tried to avoid the minval subtraction,
1046 so substitute some label even if the default label was
1047 deemed unreachable. */
1049 default_label
= fallback_label
;
1050 for (i
= 0; i
< ncases
; i
++)
1051 if (labelvec
[i
] == 0)
1054 labelvec
[i
] = gen_rtx_LABEL_REF (Pmode
, default_label
);
1059 /* There is at least one entry in the jump table that jumps
1060 to default label. The default label can either be reached
1061 through the indirect jump or the direct conditional jump
1062 before that. Split the probability of reaching the
1063 default label among these two jumps. */
1064 new_default_prob
= conditional_probability (default_prob
/2,
1067 base
-= default_prob
;
1071 base
-= default_prob
;
1076 default_edge
->probability
= default_prob
;
1078 /* We have altered the probability of the default edge. So the probabilities
1079 of all other edges need to be adjusted so that it sums up to
1080 REG_BR_PROB_BASE. */
1085 FOR_EACH_EDGE (e
, ei
, stmt_bb
->succs
)
1086 e
->probability
= GCOV_COMPUTE_SCALE (e
->probability
, base
);
1089 if (try_with_tablejump
)
1091 bool ok
= try_tablejump (index_type
, index_expr
, minval
, range
,
1092 table_label
, default_label
, new_default_prob
);
1095 /* Output the table. */
1096 emit_label (table_label
);
1098 if (CASE_VECTOR_PC_RELATIVE
|| flag_pic
)
1099 emit_jump_table_data (gen_rtx_ADDR_DIFF_VEC (CASE_VECTOR_MODE
,
1100 gen_rtx_LABEL_REF (Pmode
,
1102 gen_rtvec_v (ncases
, labelvec
),
1103 const0_rtx
, const0_rtx
));
1105 emit_jump_table_data (gen_rtx_ADDR_VEC (CASE_VECTOR_MODE
,
1106 gen_rtvec_v (ncases
, labelvec
)));
1108 /* Record no drop-through after the table. */
1112 /* Reset the aux field of all outgoing edges of basic block BB. */
1115 reset_out_edges_aux (basic_block bb
)
1119 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1123 /* Compute the number of case labels that correspond to each outgoing edge of
1124 STMT. Record this information in the aux field of the edge. */
1127 compute_cases_per_edge (gimple stmt
)
1129 basic_block bb
= gimple_bb (stmt
);
1130 reset_out_edges_aux (bb
);
1131 int ncases
= gimple_switch_num_labels (stmt
);
1132 for (int i
= ncases
- 1; i
>= 1; --i
)
1134 tree elt
= gimple_switch_label (stmt
, i
);
1135 tree lab
= CASE_LABEL (elt
);
1136 basic_block case_bb
= label_to_block_fn (cfun
, lab
);
1137 edge case_edge
= find_edge (bb
, case_bb
);
1138 case_edge
->aux
= (void *)((intptr_t)(case_edge
->aux
) + 1);
1142 /* Terminate a case (Pascal/Ada) or switch (C) statement
1143 in which ORIG_INDEX is the expression to be tested.
1144 If ORIG_TYPE is not NULL, it is the original ORIG_INDEX
1145 type as given in the source before any compiler conversions.
1146 Generate the code to test it and jump to the right place. */
1149 expand_case (gimple stmt
)
1151 tree minval
= NULL_TREE
, maxval
= NULL_TREE
, range
= NULL_TREE
;
1152 rtx default_label
= NULL_RTX
;
1153 unsigned int count
, uniq
;
1155 int ncases
= gimple_switch_num_labels (stmt
);
1156 tree index_expr
= gimple_switch_index (stmt
);
1157 tree index_type
= TREE_TYPE (index_expr
);
1159 basic_block bb
= gimple_bb (stmt
);
1161 /* A list of case labels; it is first built as a list and it may then
1162 be rearranged into a nearly balanced binary tree. */
1163 struct case_node
*case_list
= 0;
1165 /* A pool for case nodes. */
1166 alloc_pool case_node_pool
;
1168 /* An ERROR_MARK occurs for various reasons including invalid data type.
1169 ??? Can this still happen, with GIMPLE and all? */
1170 if (index_type
== error_mark_node
)
1173 /* cleanup_tree_cfg removes all SWITCH_EXPR with their index
1174 expressions being INTEGER_CST. */
1175 gcc_assert (TREE_CODE (index_expr
) != INTEGER_CST
);
1177 case_node_pool
= create_alloc_pool ("struct case_node pool",
1178 sizeof (struct case_node
),
1181 do_pending_stack_adjust ();
1183 /* Find the default case target label. */
1184 default_label
= label_rtx (CASE_LABEL (gimple_switch_default_label (stmt
)));
1185 edge default_edge
= EDGE_SUCC (bb
, 0);
1186 int default_prob
= default_edge
->probability
;
1188 /* Get upper and lower bounds of case values. */
1189 elt
= gimple_switch_label (stmt
, 1);
1190 minval
= fold_convert (index_type
, CASE_LOW (elt
));
1191 elt
= gimple_switch_label (stmt
, ncases
- 1);
1192 if (CASE_HIGH (elt
))
1193 maxval
= fold_convert (index_type
, CASE_HIGH (elt
));
1195 maxval
= fold_convert (index_type
, CASE_LOW (elt
));
1197 /* Compute span of values. */
1198 range
= fold_build2 (MINUS_EXPR
, index_type
, maxval
, minval
);
1200 /* Listify the labels queue and gather some numbers to decide
1201 how to expand this switch(). */
1204 struct pointer_set_t
*seen_labels
= pointer_set_create ();
1205 compute_cases_per_edge (stmt
);
1207 for (i
= ncases
- 1; i
>= 1; --i
)
1209 elt
= gimple_switch_label (stmt
, i
);
1210 tree low
= CASE_LOW (elt
);
1212 tree high
= CASE_HIGH (elt
);
1213 gcc_assert (! high
|| tree_int_cst_lt (low
, high
));
1214 tree lab
= CASE_LABEL (elt
);
1216 /* Count the elements.
1217 A range counts double, since it requires two compares. */
1222 /* If we have not seen this label yet, then increase the
1223 number of unique case node targets seen. */
1224 if (!pointer_set_insert (seen_labels
, lab
))
1227 /* The bounds on the case range, LOW and HIGH, have to be converted
1228 to case's index type TYPE. Note that the original type of the
1229 case index in the source code is usually "lost" during
1230 gimplification due to type promotion, but the case labels retain the
1231 original type. Make sure to drop overflow flags. */
1232 low
= fold_convert (index_type
, low
);
1233 if (TREE_OVERFLOW (low
))
1234 low
= build_int_cst_wide (index_type
,
1235 TREE_INT_CST_LOW (low
),
1236 TREE_INT_CST_HIGH (low
));
1238 /* The canonical from of a case label in GIMPLE is that a simple case
1239 has an empty CASE_HIGH. For the casesi and tablejump expanders,
1240 the back ends want simple cases to have high == low. */
1243 high
= fold_convert (index_type
, high
);
1244 if (TREE_OVERFLOW (high
))
1245 high
= build_int_cst_wide (index_type
,
1246 TREE_INT_CST_LOW (high
),
1247 TREE_INT_CST_HIGH (high
));
1249 basic_block case_bb
= label_to_block_fn (cfun
, lab
);
1250 edge case_edge
= find_edge (bb
, case_bb
);
1251 case_list
= add_case_node (
1252 case_list
, low
, high
, lab
,
1253 case_edge
->probability
/ (intptr_t)(case_edge
->aux
),
1256 pointer_set_destroy (seen_labels
);
1257 reset_out_edges_aux (bb
);
1259 /* cleanup_tree_cfg removes all SWITCH_EXPR with a single
1260 destination, such as one with a default case only.
1261 It also removes cases that are out of range for the switch
1262 type, so we should never get a zero here. */
1263 gcc_assert (count
> 0);
1265 rtx before_case
= get_last_insn ();
1267 /* Decide how to expand this switch.
1268 The two options at this point are a dispatch table (casesi or
1269 tablejump) or a decision tree. */
1271 if (expand_switch_as_decision_tree_p (range
, uniq
, count
))
1272 emit_case_decision_tree (index_expr
, index_type
,
1273 case_list
, default_label
,
1276 emit_case_dispatch_table (index_expr
, index_type
,
1277 case_list
, default_label
,
1278 minval
, maxval
, range
, bb
);
1280 reorder_insns (NEXT_INSN (before_case
), get_last_insn (), before_case
);
1283 free_alloc_pool (case_node_pool
);
1286 /* Expand the dispatch to a short decrement chain if there are few cases
1287 to dispatch to. Likewise if neither casesi nor tablejump is available,
1288 or if flag_jump_tables is set. Otherwise, expand as a casesi or a
1289 tablejump. The index mode is always the mode of integer_type_node.
1290 Trap if no case matches the index.
1292 DISPATCH_INDEX is the index expression to switch on. It should be a
1293 memory or register operand.
1295 DISPATCH_TABLE is a set of case labels. The set should be sorted in
1296 ascending order, be contiguous, starting with value 0, and contain only
1297 single-valued case labels. */
1300 expand_sjlj_dispatch_table (rtx dispatch_index
,
1301 vec
<tree
> dispatch_table
)
1303 tree index_type
= integer_type_node
;
1304 enum machine_mode index_mode
= TYPE_MODE (index_type
);
1306 int ncases
= dispatch_table
.length ();
1308 do_pending_stack_adjust ();
1309 rtx before_case
= get_last_insn ();
1311 /* Expand as a decrement-chain if there are 5 or fewer dispatch
1312 labels. This covers more than 98% of the cases in libjava,
1313 and seems to be a reasonable compromise between the "old way"
1314 of expanding as a decision tree or dispatch table vs. the "new
1315 way" with decrement chain or dispatch table. */
1316 if (dispatch_table
.length () <= 5
1317 || (!HAVE_casesi
&& !HAVE_tablejump
)
1318 || !flag_jump_tables
)
1320 /* Expand the dispatch as a decrement chain:
1322 "switch(index) {case 0: do_0; case 1: do_1; ...; case N: do_N;}"
1326 if (index == 0) do_0; else index--;
1327 if (index == 0) do_1; else index--;
1329 if (index == 0) do_N; else index--;
1331 This is more efficient than a dispatch table on most machines.
1332 The last "index--" is redundant but the code is trivially dead
1333 and will be cleaned up by later passes. */
1334 rtx index
= copy_to_mode_reg (index_mode
, dispatch_index
);
1335 rtx zero
= CONST0_RTX (index_mode
);
1336 for (int i
= 0; i
< ncases
; i
++)
1338 tree elt
= dispatch_table
[i
];
1339 rtx lab
= label_rtx (CASE_LABEL (elt
));
1340 do_jump_if_equal (index_mode
, index
, zero
, lab
, 0, -1);
1341 force_expand_binop (index_mode
, sub_optab
,
1342 index
, CONST1_RTX (index_mode
),
1343 index
, 0, OPTAB_DIRECT
);
1348 /* Similar to expand_case, but much simpler. */
1349 struct case_node
*case_list
= 0;
1350 alloc_pool case_node_pool
= create_alloc_pool ("struct sjlj_case pool",
1351 sizeof (struct case_node
),
1353 tree index_expr
= make_tree (index_type
, dispatch_index
);
1354 tree minval
= build_int_cst (index_type
, 0);
1355 tree maxval
= CASE_LOW (dispatch_table
.last ());
1356 tree range
= maxval
;
1357 rtx default_label
= gen_label_rtx ();
1359 for (int i
= ncases
- 1; i
>= 0; --i
)
1361 tree elt
= dispatch_table
[i
];
1362 tree low
= CASE_LOW (elt
);
1363 tree lab
= CASE_LABEL (elt
);
1364 case_list
= add_case_node (case_list
, low
, low
, lab
, 0, case_node_pool
);
1367 emit_case_dispatch_table (index_expr
, index_type
,
1368 case_list
, default_label
,
1369 minval
, maxval
, range
,
1370 BLOCK_FOR_INSN (before_case
));
1371 emit_label (default_label
);
1372 free_alloc_pool (case_node_pool
);
1375 /* Dispatching something not handled? Trap! */
1376 expand_builtin_trap ();
1378 reorder_insns (NEXT_INSN (before_case
), get_last_insn (), before_case
);
1384 /* Take an ordered list of case nodes
1385 and transform them into a near optimal binary tree,
1386 on the assumption that any target code selection value is as
1387 likely as any other.
1389 The transformation is performed by splitting the ordered
1390 list into two equal sections plus a pivot. The parts are
1391 then attached to the pivot as left and right branches. Each
1392 branch is then transformed recursively. */
1395 balance_case_nodes (case_node_ptr
*head
, case_node_ptr parent
)
1407 /* Count the number of entries on branch. Also count the ranges. */
1411 if (!tree_int_cst_equal (np
->low
, np
->high
))
1420 /* Split this list if it is long enough for that to help. */
1424 /* If there are just three nodes, split at the middle one. */
1426 npp
= &(*npp
)->right
;
1429 /* Find the place in the list that bisects the list's total cost,
1430 where ranges count as 2.
1431 Here I gets half the total cost. */
1432 i
= (i
+ ranges
+ 1) / 2;
1435 /* Skip nodes while their cost does not reach that amount. */
1436 if (!tree_int_cst_equal ((*npp
)->low
, (*npp
)->high
))
1441 npp
= &(*npp
)->right
;
1446 np
->parent
= parent
;
1449 /* Optimize each of the two split parts. */
1450 balance_case_nodes (&np
->left
, np
);
1451 balance_case_nodes (&np
->right
, np
);
1452 np
->subtree_prob
= np
->prob
;
1453 np
->subtree_prob
+= np
->left
->subtree_prob
;
1454 np
->subtree_prob
+= np
->right
->subtree_prob
;
1458 /* Else leave this branch as one level,
1459 but fill in `parent' fields. */
1461 np
->parent
= parent
;
1462 np
->subtree_prob
= np
->prob
;
1463 for (; np
->right
; np
= np
->right
)
1465 np
->right
->parent
= np
;
1466 (*head
)->subtree_prob
+= np
->right
->subtree_prob
;
1472 /* Search the parent sections of the case node tree
1473 to see if a test for the lower bound of NODE would be redundant.
1474 INDEX_TYPE is the type of the index expression.
1476 The instructions to generate the case decision tree are
1477 output in the same order as nodes are processed so it is
1478 known that if a parent node checks the range of the current
1479 node minus one that the current node is bounded at its lower
1480 span. Thus the test would be redundant. */
1483 node_has_low_bound (case_node_ptr node
, tree index_type
)
1486 case_node_ptr pnode
;
1488 /* If the lower bound of this node is the lowest value in the index type,
1489 we need not test it. */
1491 if (tree_int_cst_equal (node
->low
, TYPE_MIN_VALUE (index_type
)))
1494 /* If this node has a left branch, the value at the left must be less
1495 than that at this node, so it cannot be bounded at the bottom and
1496 we need not bother testing any further. */
1501 low_minus_one
= fold_build2 (MINUS_EXPR
, TREE_TYPE (node
->low
),
1503 build_int_cst (TREE_TYPE (node
->low
), 1));
1505 /* If the subtraction above overflowed, we can't verify anything.
1506 Otherwise, look for a parent that tests our value - 1. */
1508 if (! tree_int_cst_lt (low_minus_one
, node
->low
))
1511 for (pnode
= node
->parent
; pnode
; pnode
= pnode
->parent
)
1512 if (tree_int_cst_equal (low_minus_one
, pnode
->high
))
1518 /* Search the parent sections of the case node tree
1519 to see if a test for the upper bound of NODE would be redundant.
1520 INDEX_TYPE is the type of the index expression.
1522 The instructions to generate the case decision tree are
1523 output in the same order as nodes are processed so it is
1524 known that if a parent node checks the range of the current
1525 node plus one that the current node is bounded at its upper
1526 span. Thus the test would be redundant. */
1529 node_has_high_bound (case_node_ptr node
, tree index_type
)
1532 case_node_ptr pnode
;
1534 /* If there is no upper bound, obviously no test is needed. */
1536 if (TYPE_MAX_VALUE (index_type
) == NULL
)
1539 /* If the upper bound of this node is the highest value in the type
1540 of the index expression, we need not test against it. */
1542 if (tree_int_cst_equal (node
->high
, TYPE_MAX_VALUE (index_type
)))
1545 /* If this node has a right branch, the value at the right must be greater
1546 than that at this node, so it cannot be bounded at the top and
1547 we need not bother testing any further. */
1552 high_plus_one
= fold_build2 (PLUS_EXPR
, TREE_TYPE (node
->high
),
1554 build_int_cst (TREE_TYPE (node
->high
), 1));
1556 /* If the addition above overflowed, we can't verify anything.
1557 Otherwise, look for a parent that tests our value + 1. */
1559 if (! tree_int_cst_lt (node
->high
, high_plus_one
))
1562 for (pnode
= node
->parent
; pnode
; pnode
= pnode
->parent
)
1563 if (tree_int_cst_equal (high_plus_one
, pnode
->low
))
1569 /* Search the parent sections of the
1570 case node tree to see if both tests for the upper and lower
1571 bounds of NODE would be redundant. */
1574 node_is_bounded (case_node_ptr node
, tree index_type
)
1576 return (node_has_low_bound (node
, index_type
)
1577 && node_has_high_bound (node
, index_type
));
1581 /* Emit step-by-step code to select a case for the value of INDEX.
1582 The thus generated decision tree follows the form of the
1583 case-node binary tree NODE, whose nodes represent test conditions.
1584 INDEX_TYPE is the type of the index of the switch.
1586 Care is taken to prune redundant tests from the decision tree
1587 by detecting any boundary conditions already checked by
1588 emitted rtx. (See node_has_high_bound, node_has_low_bound
1589 and node_is_bounded, above.)
1591 Where the test conditions can be shown to be redundant we emit
1592 an unconditional jump to the target code. As a further
1593 optimization, the subordinates of a tree node are examined to
1594 check for bounded nodes. In this case conditional and/or
1595 unconditional jumps as a result of the boundary check for the
1596 current node are arranged to target the subordinates associated
1597 code for out of bound conditions on the current node.
1599 We can assume that when control reaches the code generated here,
1600 the index value has already been compared with the parents
1601 of this node, and determined to be on the same side of each parent
1602 as this node is. Thus, if this node tests for the value 51,
1603 and a parent tested for 52, we don't need to consider
1604 the possibility of a value greater than 51. If another parent
1605 tests for the value 50, then this node need not test anything. */
1608 emit_case_nodes (rtx index
, case_node_ptr node
, rtx default_label
,
1609 int default_prob
, tree index_type
)
1611 /* If INDEX has an unsigned type, we must make unsigned branches. */
1612 int unsignedp
= TYPE_UNSIGNED (index_type
);
1614 int prob
= node
->prob
, subtree_prob
= node
->subtree_prob
;
1615 enum machine_mode mode
= GET_MODE (index
);
1616 enum machine_mode imode
= TYPE_MODE (index_type
);
1618 /* Handle indices detected as constant during RTL expansion. */
1619 if (mode
== VOIDmode
)
1622 /* See if our parents have already tested everything for us.
1623 If they have, emit an unconditional jump for this node. */
1624 if (node_is_bounded (node
, index_type
))
1625 emit_jump (label_rtx (node
->code_label
));
1627 else if (tree_int_cst_equal (node
->low
, node
->high
))
1629 probability
= conditional_probability (prob
, subtree_prob
+ default_prob
);
1630 /* Node is single valued. First see if the index expression matches
1631 this node and then check our children, if any. */
1632 do_jump_if_equal (mode
, index
,
1633 convert_modes (mode
, imode
,
1634 expand_normal (node
->low
),
1636 label_rtx (node
->code_label
), unsignedp
, probability
);
1637 /* Since this case is taken at this point, reduce its weight from
1639 subtree_prob
-= prob
;
1640 if (node
->right
!= 0 && node
->left
!= 0)
1642 /* This node has children on both sides.
1643 Dispatch to one side or the other
1644 by comparing the index value with this node's value.
1645 If one subtree is bounded, check that one first,
1646 so we can avoid real branches in the tree. */
1648 if (node_is_bounded (node
->right
, index_type
))
1650 probability
= conditional_probability (
1652 subtree_prob
+ default_prob
);
1653 emit_cmp_and_jump_insns (index
,
1656 expand_normal (node
->high
),
1658 GT
, NULL_RTX
, mode
, unsignedp
,
1659 label_rtx (node
->right
->code_label
),
1661 emit_case_nodes (index
, node
->left
, default_label
, default_prob
,
1665 else if (node_is_bounded (node
->left
, index_type
))
1667 probability
= conditional_probability (
1669 subtree_prob
+ default_prob
);
1670 emit_cmp_and_jump_insns (index
,
1673 expand_normal (node
->high
),
1675 LT
, NULL_RTX
, mode
, unsignedp
,
1676 label_rtx (node
->left
->code_label
),
1678 emit_case_nodes (index
, node
->right
, default_label
, default_prob
, index_type
);
1681 /* If both children are single-valued cases with no
1682 children, finish up all the work. This way, we can save
1683 one ordered comparison. */
1684 else if (tree_int_cst_equal (node
->right
->low
, node
->right
->high
)
1685 && node
->right
->left
== 0
1686 && node
->right
->right
== 0
1687 && tree_int_cst_equal (node
->left
->low
, node
->left
->high
)
1688 && node
->left
->left
== 0
1689 && node
->left
->right
== 0)
1691 /* Neither node is bounded. First distinguish the two sides;
1692 then emit the code for one side at a time. */
1694 /* See if the value matches what the right hand side
1696 probability
= conditional_probability (
1698 subtree_prob
+ default_prob
);
1699 do_jump_if_equal (mode
, index
,
1700 convert_modes (mode
, imode
,
1701 expand_normal (node
->right
->low
),
1703 label_rtx (node
->right
->code_label
),
1704 unsignedp
, probability
);
1706 /* See if the value matches what the left hand side
1708 probability
= conditional_probability (
1710 subtree_prob
+ default_prob
);
1711 do_jump_if_equal (mode
, index
,
1712 convert_modes (mode
, imode
,
1713 expand_normal (node
->left
->low
),
1715 label_rtx (node
->left
->code_label
),
1716 unsignedp
, probability
);
1721 /* Neither node is bounded. First distinguish the two sides;
1722 then emit the code for one side at a time. */
1725 = build_decl (curr_insn_location (),
1726 LABEL_DECL
, NULL_TREE
, NULL_TREE
);
1728 /* The default label could be reached either through the right
1729 subtree or the left subtree. Divide the probability
1731 probability
= conditional_probability (
1732 node
->right
->subtree_prob
+ default_prob
/2,
1733 subtree_prob
+ default_prob
);
1734 /* See if the value is on the right. */
1735 emit_cmp_and_jump_insns (index
,
1738 expand_normal (node
->high
),
1740 GT
, NULL_RTX
, mode
, unsignedp
,
1741 label_rtx (test_label
),
1745 /* Value must be on the left.
1746 Handle the left-hand subtree. */
1747 emit_case_nodes (index
, node
->left
, default_label
, default_prob
, index_type
);
1748 /* If left-hand subtree does nothing,
1751 emit_jump (default_label
);
1753 /* Code branches here for the right-hand subtree. */
1754 expand_label (test_label
);
1755 emit_case_nodes (index
, node
->right
, default_label
, default_prob
, index_type
);
1759 else if (node
->right
!= 0 && node
->left
== 0)
1761 /* Here we have a right child but no left so we issue a conditional
1762 branch to default and process the right child.
1764 Omit the conditional branch to default if the right child
1765 does not have any children and is single valued; it would
1766 cost too much space to save so little time. */
1768 if (node
->right
->right
|| node
->right
->left
1769 || !tree_int_cst_equal (node
->right
->low
, node
->right
->high
))
1771 if (!node_has_low_bound (node
, index_type
))
1773 probability
= conditional_probability (
1775 subtree_prob
+ default_prob
);
1776 emit_cmp_and_jump_insns (index
,
1779 expand_normal (node
->high
),
1781 LT
, NULL_RTX
, mode
, unsignedp
,
1787 emit_case_nodes (index
, node
->right
, default_label
, default_prob
, index_type
);
1791 probability
= conditional_probability (
1792 node
->right
->subtree_prob
,
1793 subtree_prob
+ default_prob
);
1794 /* We cannot process node->right normally
1795 since we haven't ruled out the numbers less than
1796 this node's value. So handle node->right explicitly. */
1797 do_jump_if_equal (mode
, index
,
1800 expand_normal (node
->right
->low
),
1802 label_rtx (node
->right
->code_label
), unsignedp
, probability
);
1806 else if (node
->right
== 0 && node
->left
!= 0)
1808 /* Just one subtree, on the left. */
1809 if (node
->left
->left
|| node
->left
->right
1810 || !tree_int_cst_equal (node
->left
->low
, node
->left
->high
))
1812 if (!node_has_high_bound (node
, index_type
))
1814 probability
= conditional_probability (
1816 subtree_prob
+ default_prob
);
1817 emit_cmp_and_jump_insns (index
,
1820 expand_normal (node
->high
),
1822 GT
, NULL_RTX
, mode
, unsignedp
,
1828 emit_case_nodes (index
, node
->left
, default_label
,
1829 default_prob
, index_type
);
1833 probability
= conditional_probability (
1834 node
->left
->subtree_prob
,
1835 subtree_prob
+ default_prob
);
1836 /* We cannot process node->left normally
1837 since we haven't ruled out the numbers less than
1838 this node's value. So handle node->left explicitly. */
1839 do_jump_if_equal (mode
, index
,
1842 expand_normal (node
->left
->low
),
1844 label_rtx (node
->left
->code_label
), unsignedp
, probability
);
1850 /* Node is a range. These cases are very similar to those for a single
1851 value, except that we do not start by testing whether this node
1852 is the one to branch to. */
1854 if (node
->right
!= 0 && node
->left
!= 0)
1856 /* Node has subtrees on both sides.
1857 If the right-hand subtree is bounded,
1858 test for it first, since we can go straight there.
1859 Otherwise, we need to make a branch in the control structure,
1860 then handle the two subtrees. */
1861 tree test_label
= 0;
1863 if (node_is_bounded (node
->right
, index_type
))
1865 /* Right hand node is fully bounded so we can eliminate any
1866 testing and branch directly to the target code. */
1867 probability
= conditional_probability (
1868 node
->right
->subtree_prob
,
1869 subtree_prob
+ default_prob
);
1870 emit_cmp_and_jump_insns (index
,
1873 expand_normal (node
->high
),
1875 GT
, NULL_RTX
, mode
, unsignedp
,
1876 label_rtx (node
->right
->code_label
),
1881 /* Right hand node requires testing.
1882 Branch to a label where we will handle it later. */
1884 test_label
= build_decl (curr_insn_location (),
1885 LABEL_DECL
, NULL_TREE
, NULL_TREE
);
1886 probability
= conditional_probability (
1887 node
->right
->subtree_prob
+ default_prob
/2,
1888 subtree_prob
+ default_prob
);
1889 emit_cmp_and_jump_insns (index
,
1892 expand_normal (node
->high
),
1894 GT
, NULL_RTX
, mode
, unsignedp
,
1895 label_rtx (test_label
),
1900 /* Value belongs to this node or to the left-hand subtree. */
1902 probability
= conditional_probability (
1904 subtree_prob
+ default_prob
);
1905 emit_cmp_and_jump_insns (index
,
1908 expand_normal (node
->low
),
1910 GE
, NULL_RTX
, mode
, unsignedp
,
1911 label_rtx (node
->code_label
),
1914 /* Handle the left-hand subtree. */
1915 emit_case_nodes (index
, node
->left
, default_label
, default_prob
, index_type
);
1917 /* If right node had to be handled later, do that now. */
1921 /* If the left-hand subtree fell through,
1922 don't let it fall into the right-hand subtree. */
1924 emit_jump (default_label
);
1926 expand_label (test_label
);
1927 emit_case_nodes (index
, node
->right
, default_label
, default_prob
, index_type
);
1931 else if (node
->right
!= 0 && node
->left
== 0)
1933 /* Deal with values to the left of this node,
1934 if they are possible. */
1935 if (!node_has_low_bound (node
, index_type
))
1937 probability
= conditional_probability (
1939 subtree_prob
+ default_prob
);
1940 emit_cmp_and_jump_insns (index
,
1943 expand_normal (node
->low
),
1945 LT
, NULL_RTX
, mode
, unsignedp
,
1951 /* Value belongs to this node or to the right-hand subtree. */
1953 probability
= conditional_probability (
1955 subtree_prob
+ default_prob
);
1956 emit_cmp_and_jump_insns (index
,
1959 expand_normal (node
->high
),
1961 LE
, NULL_RTX
, mode
, unsignedp
,
1962 label_rtx (node
->code_label
),
1965 emit_case_nodes (index
, node
->right
, default_label
, default_prob
, index_type
);
1968 else if (node
->right
== 0 && node
->left
!= 0)
1970 /* Deal with values to the right of this node,
1971 if they are possible. */
1972 if (!node_has_high_bound (node
, index_type
))
1974 probability
= conditional_probability (
1976 subtree_prob
+ default_prob
);
1977 emit_cmp_and_jump_insns (index
,
1980 expand_normal (node
->high
),
1982 GT
, NULL_RTX
, mode
, unsignedp
,
1988 /* Value belongs to this node or to the left-hand subtree. */
1990 probability
= conditional_probability (
1992 subtree_prob
+ default_prob
);
1993 emit_cmp_and_jump_insns (index
,
1996 expand_normal (node
->low
),
1998 GE
, NULL_RTX
, mode
, unsignedp
,
1999 label_rtx (node
->code_label
),
2002 emit_case_nodes (index
, node
->left
, default_label
, default_prob
, index_type
);
2007 /* Node has no children so we check low and high bounds to remove
2008 redundant tests. Only one of the bounds can exist,
2009 since otherwise this node is bounded--a case tested already. */
2010 int high_bound
= node_has_high_bound (node
, index_type
);
2011 int low_bound
= node_has_low_bound (node
, index_type
);
2013 if (!high_bound
&& low_bound
)
2015 probability
= conditional_probability (
2017 subtree_prob
+ default_prob
);
2018 emit_cmp_and_jump_insns (index
,
2021 expand_normal (node
->high
),
2023 GT
, NULL_RTX
, mode
, unsignedp
,
2028 else if (!low_bound
&& high_bound
)
2030 probability
= conditional_probability (
2032 subtree_prob
+ default_prob
);
2033 emit_cmp_and_jump_insns (index
,
2036 expand_normal (node
->low
),
2038 LT
, NULL_RTX
, mode
, unsignedp
,
2042 else if (!low_bound
&& !high_bound
)
2044 /* Widen LOW and HIGH to the same width as INDEX. */
2045 tree type
= lang_hooks
.types
.type_for_mode (mode
, unsignedp
);
2046 tree low
= build1 (CONVERT_EXPR
, type
, node
->low
);
2047 tree high
= build1 (CONVERT_EXPR
, type
, node
->high
);
2048 rtx low_rtx
, new_index
, new_bound
;
2050 /* Instead of doing two branches, emit one unsigned branch for
2051 (index-low) > (high-low). */
2052 low_rtx
= expand_expr (low
, NULL_RTX
, mode
, EXPAND_NORMAL
);
2053 new_index
= expand_simple_binop (mode
, MINUS
, index
, low_rtx
,
2054 NULL_RTX
, unsignedp
,
2056 new_bound
= expand_expr (fold_build2 (MINUS_EXPR
, type
,
2058 NULL_RTX
, mode
, EXPAND_NORMAL
);
2060 probability
= conditional_probability (
2062 subtree_prob
+ default_prob
);
2063 emit_cmp_and_jump_insns (new_index
, new_bound
, GT
, NULL_RTX
,
2064 mode
, 1, default_label
, probability
);
2067 emit_jump (label_rtx (node
->code_label
));