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"
55 #include "pointer-set.h"
60 /* Functions and data structures for expanding case statements. */
62 /* Case label structure, used to hold info on labels within case
63 statements. We handle "range" labels; for a single-value label
64 as in C, the high and low limits are the same.
66 We start with a vector of case nodes sorted in ascending order, and
67 the default label as the last element in the vector. Before expanding
68 to RTL, we transform this vector into a list linked via the RIGHT
69 fields in the case_node struct. Nodes with higher case values are
72 Switch statements can be output in three forms. A branch table is
73 used if there are more than a few labels and the labels are dense
74 within the range between the smallest and largest case value. If a
75 branch table is used, no further manipulations are done with the case
78 The alternative to the use of a branch table is to generate a series
79 of compare and jump insns. When that is done, we use the LEFT, RIGHT,
80 and PARENT fields to hold a binary tree. Initially the tree is
81 totally unbalanced, with everything on the right. We balance the tree
82 with nodes on the left having lower case values than the parent
83 and nodes on the right having higher values. We then output the tree
86 For very small, suitable switch statements, we can generate a series
87 of simple bit test and branches instead. */
91 struct case_node
*left
; /* Left son in binary tree */
92 struct case_node
*right
; /* Right son in binary tree; also node chain */
93 struct case_node
*parent
; /* Parent of node in binary tree */
94 tree low
; /* Lowest index value for this label */
95 tree high
; /* Highest index value for this label */
96 tree code_label
; /* Label to jump to when node matches */
97 int prob
; /* Probability of taking this case. */
98 /* Probability of reaching subtree rooted at this node */
102 typedef struct case_node case_node
;
103 typedef struct case_node
*case_node_ptr
;
105 extern basic_block
label_to_block_fn (struct function
*, tree
);
107 static int n_occurrences (int, const char *);
108 static bool tree_conflicts_with_clobbers_p (tree
, HARD_REG_SET
*);
109 static void expand_nl_goto_receiver (void);
110 static bool check_operand_nalternatives (tree
, tree
);
111 static bool check_unique_operand_names (tree
, tree
, tree
);
112 static char *resolve_operand_name_1 (char *, tree
, tree
, tree
);
113 static void expand_null_return_1 (void);
114 static void expand_value_return (rtx
);
115 static void balance_case_nodes (case_node_ptr
*, case_node_ptr
);
116 static int node_has_low_bound (case_node_ptr
, tree
);
117 static int node_has_high_bound (case_node_ptr
, tree
);
118 static int node_is_bounded (case_node_ptr
, tree
);
119 static void emit_case_nodes (rtx
, case_node_ptr
, rtx
, int, tree
);
121 /* Return the rtx-label that corresponds to a LABEL_DECL,
122 creating it if necessary. */
125 label_rtx (tree label
)
127 gcc_assert (TREE_CODE (label
) == LABEL_DECL
);
129 if (!DECL_RTL_SET_P (label
))
131 rtx r
= gen_label_rtx ();
132 SET_DECL_RTL (label
, r
);
133 if (FORCED_LABEL (label
) || DECL_NONLOCAL (label
))
134 LABEL_PRESERVE_P (r
) = 1;
137 return DECL_RTL (label
);
140 /* As above, but also put it on the forced-reference list of the
141 function that contains it. */
143 force_label_rtx (tree label
)
145 rtx ref
= label_rtx (label
);
146 tree function
= decl_function_context (label
);
148 gcc_assert (function
);
150 forced_labels
= gen_rtx_EXPR_LIST (VOIDmode
, ref
, forced_labels
);
154 /* Add an unconditional jump to LABEL as the next sequential instruction. */
157 emit_jump (rtx label
)
159 do_pending_stack_adjust ();
160 emit_jump_insn (gen_jump (label
));
164 /* Emit code to jump to the address
165 specified by the pointer expression EXP. */
168 expand_computed_goto (tree exp
)
170 rtx x
= expand_normal (exp
);
172 x
= convert_memory_address (Pmode
, x
);
174 do_pending_stack_adjust ();
175 emit_indirect_jump (x
);
178 /* Handle goto statements and the labels that they can go to. */
180 /* Specify the location in the RTL code of a label LABEL,
181 which is a LABEL_DECL tree node.
183 This is used for the kind of label that the user can jump to with a
184 goto statement, and for alternatives of a switch or case statement.
185 RTL labels generated for loops and conditionals don't go through here;
186 they are generated directly at the RTL level, by other functions below.
188 Note that this has nothing to do with defining label *names*.
189 Languages vary in how they do that and what that even means. */
192 expand_label (tree label
)
194 rtx label_r
= label_rtx (label
);
196 do_pending_stack_adjust ();
197 emit_label (label_r
);
198 if (DECL_NAME (label
))
199 LABEL_NAME (DECL_RTL (label
)) = IDENTIFIER_POINTER (DECL_NAME (label
));
201 if (DECL_NONLOCAL (label
))
203 expand_nl_goto_receiver ();
204 nonlocal_goto_handler_labels
205 = gen_rtx_EXPR_LIST (VOIDmode
, label_r
,
206 nonlocal_goto_handler_labels
);
209 if (FORCED_LABEL (label
))
210 forced_labels
= gen_rtx_EXPR_LIST (VOIDmode
, label_r
, forced_labels
);
212 if (DECL_NONLOCAL (label
) || FORCED_LABEL (label
))
213 maybe_set_first_label_num (label_r
);
216 /* Generate RTL code for a `goto' statement with target label LABEL.
217 LABEL should be a LABEL_DECL tree node that was or will later be
218 defined with `expand_label'. */
221 expand_goto (tree label
)
223 #ifdef ENABLE_CHECKING
224 /* Check for a nonlocal goto to a containing function. Should have
225 gotten translated to __builtin_nonlocal_goto. */
226 tree context
= decl_function_context (label
);
227 gcc_assert (!context
|| context
== current_function_decl
);
230 emit_jump (label_rtx (label
));
233 /* Return the number of times character C occurs in string S. */
235 n_occurrences (int c
, const char *s
)
243 /* Generate RTL for an asm statement (explicit assembler code).
244 STRING is a STRING_CST node containing the assembler code text,
245 or an ADDR_EXPR containing a STRING_CST. VOL nonzero means the
246 insn is volatile; don't optimize it. */
249 expand_asm_loc (tree string
, int vol
, location_t locus
)
253 if (TREE_CODE (string
) == ADDR_EXPR
)
254 string
= TREE_OPERAND (string
, 0);
256 body
= gen_rtx_ASM_INPUT_loc (VOIDmode
,
257 ggc_strdup (TREE_STRING_POINTER (string
)),
260 MEM_VOLATILE_P (body
) = vol
;
265 /* Parse the output constraint pointed to by *CONSTRAINT_P. It is the
266 OPERAND_NUMth output operand, indexed from zero. There are NINPUTS
267 inputs and NOUTPUTS outputs to this extended-asm. Upon return,
268 *ALLOWS_MEM will be TRUE iff the constraint allows the use of a
269 memory operand. Similarly, *ALLOWS_REG will be TRUE iff the
270 constraint allows the use of a register operand. And, *IS_INOUT
271 will be true if the operand is read-write, i.e., if it is used as
272 an input as well as an output. If *CONSTRAINT_P is not in
273 canonical form, it will be made canonical. (Note that `+' will be
274 replaced with `=' as part of this process.)
276 Returns TRUE if all went well; FALSE if an error occurred. */
279 parse_output_constraint (const char **constraint_p
, int operand_num
,
280 int ninputs
, int noutputs
, bool *allows_mem
,
281 bool *allows_reg
, bool *is_inout
)
283 const char *constraint
= *constraint_p
;
286 /* Assume the constraint doesn't allow the use of either a register
291 /* Allow the `=' or `+' to not be at the beginning of the string,
292 since it wasn't explicitly documented that way, and there is a
293 large body of code that puts it last. Swap the character to
294 the front, so as not to uglify any place else. */
295 p
= strchr (constraint
, '=');
297 p
= strchr (constraint
, '+');
299 /* If the string doesn't contain an `=', issue an error
303 error ("output operand constraint lacks %<=%>");
307 /* If the constraint begins with `+', then the operand is both read
308 from and written to. */
309 *is_inout
= (*p
== '+');
311 /* Canonicalize the output constraint so that it begins with `='. */
312 if (p
!= constraint
|| *is_inout
)
315 size_t c_len
= strlen (constraint
);
318 warning (0, "output constraint %qc for operand %d "
319 "is not at the beginning",
322 /* Make a copy of the constraint. */
323 buf
= XALLOCAVEC (char, c_len
+ 1);
324 strcpy (buf
, constraint
);
325 /* Swap the first character and the `=' or `+'. */
326 buf
[p
- constraint
] = buf
[0];
327 /* Make sure the first character is an `='. (Until we do this,
328 it might be a `+'.) */
330 /* Replace the constraint with the canonicalized string. */
331 *constraint_p
= ggc_alloc_string (buf
, c_len
);
332 constraint
= *constraint_p
;
335 /* Loop through the constraint string. */
336 for (p
= constraint
+ 1; *p
; p
+= CONSTRAINT_LEN (*p
, p
))
341 error ("operand constraint contains incorrectly positioned "
346 if (operand_num
+ 1 == ninputs
+ noutputs
)
348 error ("%<%%%> constraint used with last operand");
353 case 'V': case TARGET_MEM_CONSTRAINT
: case 'o':
357 case '?': case '!': case '*': case '&': case '#':
358 case 'E': case 'F': case 'G': case 'H':
359 case 's': case 'i': case 'n':
360 case 'I': case 'J': case 'K': case 'L': case 'M':
361 case 'N': case 'O': case 'P': case ',':
364 case '0': case '1': case '2': case '3': case '4':
365 case '5': case '6': case '7': case '8': case '9':
367 error ("matching constraint not valid in output operand");
371 /* ??? Before flow, auto inc/dec insns are not supposed to exist,
372 excepting those that expand_call created. So match memory
389 if (REG_CLASS_FROM_CONSTRAINT (*p
, p
) != NO_REGS
)
391 #ifdef EXTRA_CONSTRAINT_STR
392 else if (EXTRA_ADDRESS_CONSTRAINT (*p
, p
))
394 else if (EXTRA_MEMORY_CONSTRAINT (*p
, p
))
398 /* Otherwise we can't assume anything about the nature of
399 the constraint except that it isn't purely registers.
400 Treat it like "g" and hope for the best. */
411 /* Similar, but for input constraints. */
414 parse_input_constraint (const char **constraint_p
, int input_num
,
415 int ninputs
, int noutputs
, int ninout
,
416 const char * const * constraints
,
417 bool *allows_mem
, bool *allows_reg
)
419 const char *constraint
= *constraint_p
;
420 const char *orig_constraint
= constraint
;
421 size_t c_len
= strlen (constraint
);
423 bool saw_match
= false;
425 /* Assume the constraint doesn't allow the use of either
426 a register or memory. */
430 /* Make sure constraint has neither `=', `+', nor '&'. */
432 for (j
= 0; j
< c_len
; j
+= CONSTRAINT_LEN (constraint
[j
], constraint
+j
))
433 switch (constraint
[j
])
435 case '+': case '=': case '&':
436 if (constraint
== orig_constraint
)
438 error ("input operand constraint contains %qc", constraint
[j
]);
444 if (constraint
== orig_constraint
445 && input_num
+ 1 == ninputs
- ninout
)
447 error ("%<%%%> constraint used with last operand");
452 case 'V': case TARGET_MEM_CONSTRAINT
: case 'o':
457 case '?': case '!': case '*': case '#':
458 case 'E': case 'F': case 'G': case 'H':
459 case 's': case 'i': case 'n':
460 case 'I': case 'J': case 'K': case 'L': case 'M':
461 case 'N': case 'O': case 'P': case ',':
464 /* Whether or not a numeric constraint allows a register is
465 decided by the matching constraint, and so there is no need
466 to do anything special with them. We must handle them in
467 the default case, so that we don't unnecessarily force
468 operands to memory. */
469 case '0': case '1': case '2': case '3': case '4':
470 case '5': case '6': case '7': case '8': case '9':
477 match
= strtoul (constraint
+ j
, &end
, 10);
478 if (match
>= (unsigned long) noutputs
)
480 error ("matching constraint references invalid operand number");
484 /* Try and find the real constraint for this dup. Only do this
485 if the matching constraint is the only alternative. */
487 && (j
== 0 || (j
== 1 && constraint
[0] == '%')))
489 constraint
= constraints
[match
];
490 *constraint_p
= constraint
;
491 c_len
= strlen (constraint
);
493 /* ??? At the end of the loop, we will skip the first part of
494 the matched constraint. This assumes not only that the
495 other constraint is an output constraint, but also that
496 the '=' or '+' come first. */
500 j
= end
- constraint
;
501 /* Anticipate increment at end of loop. */
516 if (! ISALPHA (constraint
[j
]))
518 error ("invalid punctuation %qc in constraint", constraint
[j
]);
521 if (REG_CLASS_FROM_CONSTRAINT (constraint
[j
], constraint
+ j
)
524 #ifdef EXTRA_CONSTRAINT_STR
525 else if (EXTRA_ADDRESS_CONSTRAINT (constraint
[j
], constraint
+ j
))
527 else if (EXTRA_MEMORY_CONSTRAINT (constraint
[j
], constraint
+ j
))
531 /* Otherwise we can't assume anything about the nature of
532 the constraint except that it isn't purely registers.
533 Treat it like "g" and hope for the best. */
541 if (saw_match
&& !*allows_reg
)
542 warning (0, "matching constraint does not allow a register");
547 /* Return DECL iff there's an overlap between *REGS and DECL, where DECL
548 can be an asm-declared register. Called via walk_tree. */
551 decl_overlaps_hard_reg_set_p (tree
*declp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
555 const HARD_REG_SET
*const regs
= (const HARD_REG_SET
*) data
;
557 if (TREE_CODE (decl
) == VAR_DECL
)
559 if (DECL_HARD_REGISTER (decl
)
560 && REG_P (DECL_RTL (decl
))
561 && REGNO (DECL_RTL (decl
)) < FIRST_PSEUDO_REGISTER
)
563 rtx reg
= DECL_RTL (decl
);
565 if (overlaps_hard_reg_set_p (*regs
, GET_MODE (reg
), REGNO (reg
)))
570 else if (TYPE_P (decl
) || TREE_CODE (decl
) == PARM_DECL
)
575 /* If there is an overlap between *REGS and DECL, return the first overlap
578 tree_overlaps_hard_reg_set (tree decl
, HARD_REG_SET
*regs
)
580 return walk_tree (&decl
, decl_overlaps_hard_reg_set_p
, regs
, NULL
);
583 /* Check for overlap between registers marked in CLOBBERED_REGS and
584 anything inappropriate in T. Emit error and return the register
585 variable definition for error, NULL_TREE for ok. */
588 tree_conflicts_with_clobbers_p (tree t
, HARD_REG_SET
*clobbered_regs
)
590 /* Conflicts between asm-declared register variables and the clobber
591 list are not allowed. */
592 tree overlap
= tree_overlaps_hard_reg_set (t
, clobbered_regs
);
596 error ("asm-specifier for variable %qE conflicts with asm clobber list",
597 DECL_NAME (overlap
));
599 /* Reset registerness to stop multiple errors emitted for a single
601 DECL_REGISTER (overlap
) = 0;
608 /* Generate RTL for an asm statement with arguments.
609 STRING is the instruction template.
610 OUTPUTS is a list of output arguments (lvalues); INPUTS a list of inputs.
611 Each output or input has an expression in the TREE_VALUE and
612 a tree list in TREE_PURPOSE which in turn contains a constraint
613 name in TREE_VALUE (or NULL_TREE) and a constraint string
615 CLOBBERS is a list of STRING_CST nodes each naming a hard register
616 that is clobbered by this insn.
618 Not all kinds of lvalue that may appear in OUTPUTS can be stored directly.
619 Some elements of OUTPUTS may be replaced with trees representing temporary
620 values. The caller should copy those temporary values to the originally
623 VOL nonzero means the insn is volatile; don't optimize it. */
626 expand_asm_operands (tree string
, tree outputs
, tree inputs
,
627 tree clobbers
, tree labels
, int vol
, location_t locus
)
629 rtvec argvec
, constraintvec
, labelvec
;
631 int ninputs
= list_length (inputs
);
632 int noutputs
= list_length (outputs
);
633 int nlabels
= list_length (labels
);
636 HARD_REG_SET clobbered_regs
;
637 int clobber_conflict_found
= 0;
641 /* Vector of RTX's of evaluated output operands. */
642 rtx
*output_rtx
= XALLOCAVEC (rtx
, noutputs
);
643 int *inout_opnum
= XALLOCAVEC (int, noutputs
);
644 rtx
*real_output_rtx
= XALLOCAVEC (rtx
, noutputs
);
645 enum machine_mode
*inout_mode
= XALLOCAVEC (enum machine_mode
, noutputs
);
646 const char **constraints
= XALLOCAVEC (const char *, noutputs
+ ninputs
);
647 int old_generating_concat_p
= generating_concat_p
;
649 /* An ASM with no outputs needs to be treated as volatile, for now. */
653 if (! check_operand_nalternatives (outputs
, inputs
))
656 string
= resolve_asm_operand_names (string
, outputs
, inputs
, labels
);
658 /* Collect constraints. */
660 for (t
= outputs
; t
; t
= TREE_CHAIN (t
), i
++)
661 constraints
[i
] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t
)));
662 for (t
= inputs
; t
; t
= TREE_CHAIN (t
), i
++)
663 constraints
[i
] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t
)));
665 /* Sometimes we wish to automatically clobber registers across an asm.
666 Case in point is when the i386 backend moved from cc0 to a hard reg --
667 maintaining source-level compatibility means automatically clobbering
668 the flags register. */
669 clobbers
= targetm
.md_asm_clobbers (outputs
, inputs
, clobbers
);
671 /* Count the number of meaningful clobbered registers, ignoring what
672 we would ignore later. */
674 CLEAR_HARD_REG_SET (clobbered_regs
);
675 for (tail
= clobbers
; tail
; tail
= TREE_CHAIN (tail
))
680 if (TREE_VALUE (tail
) == error_mark_node
)
682 regname
= TREE_STRING_POINTER (TREE_VALUE (tail
));
684 i
= decode_reg_name_and_count (regname
, &nregs
);
688 error ("unknown register name %qs in %<asm%>", regname
);
690 /* Mark clobbered registers. */
695 for (reg
= i
; reg
< i
+ nregs
; reg
++)
699 /* Clobbering the PIC register is an error. */
700 if (reg
== (int) PIC_OFFSET_TABLE_REGNUM
)
702 error ("PIC register clobbered by %qs in %<asm%>", regname
);
706 SET_HARD_REG_BIT (clobbered_regs
, reg
);
711 /* First pass over inputs and outputs checks validity and sets
712 mark_addressable if needed. */
715 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
717 tree val
= TREE_VALUE (tail
);
718 tree type
= TREE_TYPE (val
);
719 const char *constraint
;
724 /* If there's an erroneous arg, emit no insn. */
725 if (type
== error_mark_node
)
728 /* Try to parse the output constraint. If that fails, there's
729 no point in going further. */
730 constraint
= constraints
[i
];
731 if (!parse_output_constraint (&constraint
, i
, ninputs
, noutputs
,
732 &allows_mem
, &allows_reg
, &is_inout
))
739 && REG_P (DECL_RTL (val
))
740 && GET_MODE (DECL_RTL (val
)) != TYPE_MODE (type
))))
741 mark_addressable (val
);
748 if (ninputs
+ noutputs
> MAX_RECOG_OPERANDS
)
750 error ("more than %d operands in %<asm%>", MAX_RECOG_OPERANDS
);
754 for (i
= 0, tail
= inputs
; tail
; i
++, tail
= TREE_CHAIN (tail
))
756 bool allows_reg
, allows_mem
;
757 const char *constraint
;
759 /* If there's an erroneous arg, emit no insn, because the ASM_INPUT
760 would get VOIDmode and that could cause a crash in reload. */
761 if (TREE_TYPE (TREE_VALUE (tail
)) == error_mark_node
)
764 constraint
= constraints
[i
+ noutputs
];
765 if (! parse_input_constraint (&constraint
, i
, ninputs
, noutputs
, ninout
,
766 constraints
, &allows_mem
, &allows_reg
))
769 if (! allows_reg
&& allows_mem
)
770 mark_addressable (TREE_VALUE (tail
));
773 /* Second pass evaluates arguments. */
775 /* Make sure stack is consistent for asm goto. */
777 do_pending_stack_adjust ();
780 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
782 tree val
= TREE_VALUE (tail
);
783 tree type
= TREE_TYPE (val
);
790 ok
= parse_output_constraint (&constraints
[i
], i
, ninputs
,
791 noutputs
, &allows_mem
, &allows_reg
,
795 /* If an output operand is not a decl or indirect ref and our constraint
796 allows a register, make a temporary to act as an intermediate.
797 Make the asm insn write into that, then our caller will copy it to
798 the real output operand. Likewise for promoted variables. */
800 generating_concat_p
= 0;
802 real_output_rtx
[i
] = NULL_RTX
;
803 if ((TREE_CODE (val
) == INDIRECT_REF
806 && (allows_mem
|| REG_P (DECL_RTL (val
)))
807 && ! (REG_P (DECL_RTL (val
))
808 && GET_MODE (DECL_RTL (val
)) != TYPE_MODE (type
)))
812 op
= expand_expr (val
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
814 op
= validize_mem (op
);
816 if (! allows_reg
&& !MEM_P (op
))
817 error ("output number %d not directly addressable", i
);
818 if ((! allows_mem
&& MEM_P (op
))
819 || GET_CODE (op
) == CONCAT
)
821 real_output_rtx
[i
] = op
;
822 op
= gen_reg_rtx (GET_MODE (op
));
824 emit_move_insn (op
, real_output_rtx
[i
]);
829 op
= assign_temp (type
, 0, 1);
830 op
= validize_mem (op
);
831 if (!MEM_P (op
) && TREE_CODE (TREE_VALUE (tail
)) == SSA_NAME
)
832 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (TREE_VALUE (tail
)), op
);
833 TREE_VALUE (tail
) = make_tree (type
, op
);
837 generating_concat_p
= old_generating_concat_p
;
841 inout_mode
[ninout
] = TYPE_MODE (type
);
842 inout_opnum
[ninout
++] = i
;
845 if (tree_conflicts_with_clobbers_p (val
, &clobbered_regs
))
846 clobber_conflict_found
= 1;
849 /* Make vectors for the expression-rtx, constraint strings,
850 and named operands. */
852 argvec
= rtvec_alloc (ninputs
);
853 constraintvec
= rtvec_alloc (ninputs
);
854 labelvec
= rtvec_alloc (nlabels
);
856 body
= gen_rtx_ASM_OPERANDS ((noutputs
== 0 ? VOIDmode
857 : GET_MODE (output_rtx
[0])),
858 ggc_strdup (TREE_STRING_POINTER (string
)),
859 empty_string
, 0, argvec
, constraintvec
,
862 MEM_VOLATILE_P (body
) = vol
;
864 /* Eval the inputs and put them into ARGVEC.
865 Put their constraints into ASM_INPUTs and store in CONSTRAINTS. */
867 for (i
= 0, tail
= inputs
; tail
; tail
= TREE_CHAIN (tail
), ++i
)
869 bool allows_reg
, allows_mem
;
870 const char *constraint
;
875 constraint
= constraints
[i
+ noutputs
];
876 ok
= parse_input_constraint (&constraint
, i
, ninputs
, noutputs
, ninout
,
877 constraints
, &allows_mem
, &allows_reg
);
880 generating_concat_p
= 0;
882 val
= TREE_VALUE (tail
);
883 type
= TREE_TYPE (val
);
884 /* EXPAND_INITIALIZER will not generate code for valid initializer
885 constants, but will still generate code for other types of operand.
886 This is the behavior we want for constant constraints. */
887 op
= expand_expr (val
, NULL_RTX
, VOIDmode
,
888 allows_reg
? EXPAND_NORMAL
889 : allows_mem
? EXPAND_MEMORY
890 : EXPAND_INITIALIZER
);
892 /* Never pass a CONCAT to an ASM. */
893 if (GET_CODE (op
) == CONCAT
)
894 op
= force_reg (GET_MODE (op
), op
);
896 op
= validize_mem (op
);
898 if (asm_operand_ok (op
, constraint
, NULL
) <= 0)
900 if (allows_reg
&& TYPE_MODE (type
) != BLKmode
)
901 op
= force_reg (TYPE_MODE (type
), op
);
902 else if (!allows_mem
)
903 warning (0, "asm operand %d probably doesn%'t match constraints",
907 /* We won't recognize either volatile memory or memory
908 with a queued address as available a memory_operand
909 at this point. Ignore it: clearly this *is* a memory. */
915 generating_concat_p
= old_generating_concat_p
;
916 ASM_OPERANDS_INPUT (body
, i
) = op
;
918 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body
, i
)
919 = gen_rtx_ASM_INPUT (TYPE_MODE (type
),
920 ggc_strdup (constraints
[i
+ noutputs
]));
922 if (tree_conflicts_with_clobbers_p (val
, &clobbered_regs
))
923 clobber_conflict_found
= 1;
926 /* Protect all the operands from the queue now that they have all been
929 generating_concat_p
= 0;
931 /* For in-out operands, copy output rtx to input rtx. */
932 for (i
= 0; i
< ninout
; i
++)
934 int j
= inout_opnum
[i
];
937 ASM_OPERANDS_INPUT (body
, ninputs
- ninout
+ i
)
940 sprintf (buffer
, "%d", j
);
941 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body
, ninputs
- ninout
+ i
)
942 = gen_rtx_ASM_INPUT (inout_mode
[i
], ggc_strdup (buffer
));
945 /* Copy labels to the vector. */
946 for (i
= 0, tail
= labels
; i
< nlabels
; ++i
, tail
= TREE_CHAIN (tail
))
947 ASM_OPERANDS_LABEL (body
, i
)
948 = gen_rtx_LABEL_REF (Pmode
, label_rtx (TREE_VALUE (tail
)));
950 generating_concat_p
= old_generating_concat_p
;
952 /* Now, for each output, construct an rtx
953 (set OUTPUT (asm_operands INSN OUTPUTCONSTRAINT OUTPUTNUMBER
954 ARGVEC CONSTRAINTS OPNAMES))
955 If there is more than one, put them inside a PARALLEL. */
957 if (nlabels
> 0 && nclobbers
== 0)
959 gcc_assert (noutputs
== 0);
960 emit_jump_insn (body
);
962 else if (noutputs
== 0 && nclobbers
== 0)
964 /* No output operands: put in a raw ASM_OPERANDS rtx. */
967 else if (noutputs
== 1 && nclobbers
== 0)
969 ASM_OPERANDS_OUTPUT_CONSTRAINT (body
) = ggc_strdup (constraints
[0]);
970 emit_insn (gen_rtx_SET (VOIDmode
, output_rtx
[0], body
));
980 body
= gen_rtx_PARALLEL (VOIDmode
, rtvec_alloc (num
+ nclobbers
));
982 /* For each output operand, store a SET. */
983 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
986 = gen_rtx_SET (VOIDmode
,
989 (GET_MODE (output_rtx
[i
]),
990 ggc_strdup (TREE_STRING_POINTER (string
)),
991 ggc_strdup (constraints
[i
]),
992 i
, argvec
, constraintvec
, labelvec
, locus
));
994 MEM_VOLATILE_P (SET_SRC (XVECEXP (body
, 0, i
))) = vol
;
997 /* If there are no outputs (but there are some clobbers)
998 store the bare ASM_OPERANDS into the PARALLEL. */
1001 XVECEXP (body
, 0, i
++) = obody
;
1003 /* Store (clobber REG) for each clobbered register specified. */
1005 for (tail
= clobbers
; tail
; tail
= TREE_CHAIN (tail
))
1007 const char *regname
= TREE_STRING_POINTER (TREE_VALUE (tail
));
1009 int j
= decode_reg_name_and_count (regname
, &nregs
);
1014 if (j
== -3) /* `cc', which is not a register */
1017 if (j
== -4) /* `memory', don't cache memory across asm */
1019 XVECEXP (body
, 0, i
++)
1020 = gen_rtx_CLOBBER (VOIDmode
,
1023 gen_rtx_SCRATCH (VOIDmode
)));
1027 /* Ignore unknown register, error already signaled. */
1031 for (reg
= j
; reg
< j
+ nregs
; reg
++)
1033 /* Use QImode since that's guaranteed to clobber just
1035 clobbered_reg
= gen_rtx_REG (QImode
, reg
);
1037 /* Do sanity check for overlap between clobbers and
1038 respectively input and outputs that hasn't been
1039 handled. Such overlap should have been detected and
1041 if (!clobber_conflict_found
)
1045 /* We test the old body (obody) contents to avoid
1046 tripping over the under-construction body. */
1047 for (opno
= 0; opno
< noutputs
; opno
++)
1048 if (reg_overlap_mentioned_p (clobbered_reg
,
1051 ("asm clobber conflict with output operand");
1053 for (opno
= 0; opno
< ninputs
- ninout
; opno
++)
1054 if (reg_overlap_mentioned_p (clobbered_reg
,
1055 ASM_OPERANDS_INPUT (obody
,
1058 ("asm clobber conflict with input operand");
1061 XVECEXP (body
, 0, i
++)
1062 = gen_rtx_CLOBBER (VOIDmode
, clobbered_reg
);
1067 emit_jump_insn (body
);
1072 /* For any outputs that needed reloading into registers, spill them
1073 back to where they belong. */
1074 for (i
= 0; i
< noutputs
; ++i
)
1075 if (real_output_rtx
[i
])
1076 emit_move_insn (real_output_rtx
[i
], output_rtx
[i
]);
1078 crtl
->has_asm_statement
= 1;
1079 coverage_has_asm_stmt ();
1084 expand_asm_stmt (gimple stmt
)
1087 tree outputs
, tail
, t
;
1091 tree str
, out
, in
, cl
, labels
;
1092 location_t locus
= gimple_location (stmt
);
1094 /* Meh... convert the gimple asm operands into real tree lists.
1095 Eventually we should make all routines work on the vectors instead
1096 of relying on TREE_CHAIN. */
1098 n
= gimple_asm_noutputs (stmt
);
1101 t
= out
= gimple_asm_output_op (stmt
, 0);
1102 for (i
= 1; i
< n
; i
++)
1103 t
= TREE_CHAIN (t
) = gimple_asm_output_op (stmt
, i
);
1107 n
= gimple_asm_ninputs (stmt
);
1110 t
= in
= gimple_asm_input_op (stmt
, 0);
1111 for (i
= 1; i
< n
; i
++)
1112 t
= TREE_CHAIN (t
) = gimple_asm_input_op (stmt
, i
);
1116 n
= gimple_asm_nclobbers (stmt
);
1119 t
= cl
= gimple_asm_clobber_op (stmt
, 0);
1120 for (i
= 1; i
< n
; i
++)
1121 t
= TREE_CHAIN (t
) = gimple_asm_clobber_op (stmt
, i
);
1125 n
= gimple_asm_nlabels (stmt
);
1128 t
= labels
= gimple_asm_label_op (stmt
, 0);
1129 for (i
= 1; i
< n
; i
++)
1130 t
= TREE_CHAIN (t
) = gimple_asm_label_op (stmt
, i
);
1133 s
= gimple_asm_string (stmt
);
1134 str
= build_string (strlen (s
), s
);
1136 if (gimple_asm_input_p (stmt
))
1138 expand_asm_loc (str
, gimple_asm_volatile_p (stmt
), locus
);
1143 noutputs
= gimple_asm_noutputs (stmt
);
1144 /* o[I] is the place that output number I should be written. */
1145 o
= (tree
*) alloca (noutputs
* sizeof (tree
));
1147 /* Record the contents of OUTPUTS before it is modified. */
1148 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
1149 o
[i
] = TREE_VALUE (tail
);
1151 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
1152 OUTPUTS some trees for where the values were actually stored. */
1153 expand_asm_operands (str
, outputs
, in
, cl
, labels
,
1154 gimple_asm_volatile_p (stmt
), locus
);
1156 /* Copy all the intermediate outputs into the specified outputs. */
1157 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
1159 if (o
[i
] != TREE_VALUE (tail
))
1161 expand_assignment (o
[i
], TREE_VALUE (tail
), false);
1164 /* Restore the original value so that it's correct the next
1165 time we expand this function. */
1166 TREE_VALUE (tail
) = o
[i
];
1171 /* A subroutine of expand_asm_operands. Check that all operands have
1172 the same number of alternatives. Return true if so. */
1175 check_operand_nalternatives (tree outputs
, tree inputs
)
1177 if (outputs
|| inputs
)
1179 tree tmp
= TREE_PURPOSE (outputs
? outputs
: inputs
);
1181 = n_occurrences (',', TREE_STRING_POINTER (TREE_VALUE (tmp
)));
1184 if (nalternatives
+ 1 > MAX_RECOG_ALTERNATIVES
)
1186 error ("too many alternatives in %<asm%>");
1193 const char *constraint
1194 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tmp
)));
1196 if (n_occurrences (',', constraint
) != nalternatives
)
1198 error ("operand constraints for %<asm%> differ "
1199 "in number of alternatives");
1203 if (TREE_CHAIN (tmp
))
1204 tmp
= TREE_CHAIN (tmp
);
1206 tmp
= next
, next
= 0;
1213 /* A subroutine of expand_asm_operands. Check that all operand names
1214 are unique. Return true if so. We rely on the fact that these names
1215 are identifiers, and so have been canonicalized by get_identifier,
1216 so all we need are pointer comparisons. */
1219 check_unique_operand_names (tree outputs
, tree inputs
, tree labels
)
1221 tree i
, j
, i_name
= NULL_TREE
;
1223 for (i
= outputs
; i
; i
= TREE_CHAIN (i
))
1225 i_name
= TREE_PURPOSE (TREE_PURPOSE (i
));
1229 for (j
= TREE_CHAIN (i
); j
; j
= TREE_CHAIN (j
))
1230 if (simple_cst_equal (i_name
, TREE_PURPOSE (TREE_PURPOSE (j
))))
1234 for (i
= inputs
; i
; i
= TREE_CHAIN (i
))
1236 i_name
= TREE_PURPOSE (TREE_PURPOSE (i
));
1240 for (j
= TREE_CHAIN (i
); j
; j
= TREE_CHAIN (j
))
1241 if (simple_cst_equal (i_name
, TREE_PURPOSE (TREE_PURPOSE (j
))))
1243 for (j
= outputs
; j
; j
= TREE_CHAIN (j
))
1244 if (simple_cst_equal (i_name
, TREE_PURPOSE (TREE_PURPOSE (j
))))
1248 for (i
= labels
; i
; i
= TREE_CHAIN (i
))
1250 i_name
= TREE_PURPOSE (i
);
1254 for (j
= TREE_CHAIN (i
); j
; j
= TREE_CHAIN (j
))
1255 if (simple_cst_equal (i_name
, TREE_PURPOSE (j
)))
1257 for (j
= inputs
; j
; j
= TREE_CHAIN (j
))
1258 if (simple_cst_equal (i_name
, TREE_PURPOSE (TREE_PURPOSE (j
))))
1265 error ("duplicate asm operand name %qs", TREE_STRING_POINTER (i_name
));
1269 /* A subroutine of expand_asm_operands. Resolve the names of the operands
1270 in *POUTPUTS and *PINPUTS to numbers, and replace the name expansions in
1271 STRING and in the constraints to those numbers. */
1274 resolve_asm_operand_names (tree string
, tree outputs
, tree inputs
, tree labels
)
1281 check_unique_operand_names (outputs
, inputs
, labels
);
1283 /* Substitute [<name>] in input constraint strings. There should be no
1284 named operands in output constraints. */
1285 for (t
= inputs
; t
; t
= TREE_CHAIN (t
))
1287 c
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t
)));
1288 if (strchr (c
, '[') != NULL
)
1290 p
= buffer
= xstrdup (c
);
1291 while ((p
= strchr (p
, '[')) != NULL
)
1292 p
= resolve_operand_name_1 (p
, outputs
, inputs
, NULL
);
1293 TREE_VALUE (TREE_PURPOSE (t
))
1294 = build_string (strlen (buffer
), buffer
);
1299 /* Now check for any needed substitutions in the template. */
1300 c
= TREE_STRING_POINTER (string
);
1301 while ((c
= strchr (c
, '%')) != NULL
)
1305 else if (ISALPHA (c
[1]) && c
[2] == '[')
1309 c
+= 1 + (c
[1] == '%');
1316 /* OK, we need to make a copy so we can perform the substitutions.
1317 Assume that we will not need extra space--we get to remove '['
1318 and ']', which means we cannot have a problem until we have more
1319 than 999 operands. */
1320 buffer
= xstrdup (TREE_STRING_POINTER (string
));
1321 p
= buffer
+ (c
- TREE_STRING_POINTER (string
));
1323 while ((p
= strchr (p
, '%')) != NULL
)
1327 else if (ISALPHA (p
[1]) && p
[2] == '[')
1331 p
+= 1 + (p
[1] == '%');
1335 p
= resolve_operand_name_1 (p
, outputs
, inputs
, labels
);
1338 string
= build_string (strlen (buffer
), buffer
);
1345 /* A subroutine of resolve_operand_names. P points to the '[' for a
1346 potential named operand of the form [<name>]. In place, replace
1347 the name and brackets with a number. Return a pointer to the
1348 balance of the string after substitution. */
1351 resolve_operand_name_1 (char *p
, tree outputs
, tree inputs
, tree labels
)
1357 /* Collect the operand name. */
1358 q
= strchr (++p
, ']');
1361 error ("missing close brace for named operand");
1362 return strchr (p
, '\0');
1366 /* Resolve the name to a number. */
1367 for (op
= 0, t
= outputs
; t
; t
= TREE_CHAIN (t
), op
++)
1369 tree name
= TREE_PURPOSE (TREE_PURPOSE (t
));
1370 if (name
&& strcmp (TREE_STRING_POINTER (name
), p
) == 0)
1373 for (t
= inputs
; t
; t
= TREE_CHAIN (t
), op
++)
1375 tree name
= TREE_PURPOSE (TREE_PURPOSE (t
));
1376 if (name
&& strcmp (TREE_STRING_POINTER (name
), p
) == 0)
1379 for (t
= labels
; t
; t
= TREE_CHAIN (t
), op
++)
1381 tree name
= TREE_PURPOSE (t
);
1382 if (name
&& strcmp (TREE_STRING_POINTER (name
), p
) == 0)
1386 error ("undefined named operand %qs", identifier_to_locale (p
));
1390 /* Replace the name with the number. Unfortunately, not all libraries
1391 get the return value of sprintf correct, so search for the end of the
1392 generated string by hand. */
1393 sprintf (--p
, "%d", op
);
1394 p
= strchr (p
, '\0');
1396 /* Verify the no extra buffer space assumption. */
1397 gcc_assert (p
<= q
);
1399 /* Shift the rest of the buffer down to fill the gap. */
1400 memmove (p
, q
+ 1, strlen (q
+ 1) + 1);
1405 /* Generate RTL to return from the current function, with no value.
1406 (That is, we do not do anything about returning any value.) */
1409 expand_null_return (void)
1411 /* If this function was declared to return a value, but we
1412 didn't, clobber the return registers so that they are not
1413 propagated live to the rest of the function. */
1414 clobber_return_register ();
1416 expand_null_return_1 ();
1419 /* Generate RTL to return directly from the current function.
1420 (That is, we bypass any return value.) */
1423 expand_naked_return (void)
1427 clear_pending_stack_adjust ();
1428 do_pending_stack_adjust ();
1430 end_label
= naked_return_label
;
1432 end_label
= naked_return_label
= gen_label_rtx ();
1434 emit_jump (end_label
);
1437 /* Generate RTL to return from the current function, with value VAL. */
1440 expand_value_return (rtx val
)
1442 /* Copy the value to the return location unless it's already there. */
1444 tree decl
= DECL_RESULT (current_function_decl
);
1445 rtx return_reg
= DECL_RTL (decl
);
1446 if (return_reg
!= val
)
1448 tree funtype
= TREE_TYPE (current_function_decl
);
1449 tree type
= TREE_TYPE (decl
);
1450 int unsignedp
= TYPE_UNSIGNED (type
);
1451 enum machine_mode old_mode
= DECL_MODE (decl
);
1452 enum machine_mode mode
;
1453 if (DECL_BY_REFERENCE (decl
))
1454 mode
= promote_function_mode (type
, old_mode
, &unsignedp
, funtype
, 2);
1456 mode
= promote_function_mode (type
, old_mode
, &unsignedp
, funtype
, 1);
1458 if (mode
!= old_mode
)
1459 val
= convert_modes (mode
, old_mode
, val
, unsignedp
);
1461 if (GET_CODE (return_reg
) == PARALLEL
)
1462 emit_group_load (return_reg
, val
, type
, int_size_in_bytes (type
));
1464 emit_move_insn (return_reg
, val
);
1467 expand_null_return_1 ();
1470 /* Output a return with no value. */
1473 expand_null_return_1 (void)
1475 clear_pending_stack_adjust ();
1476 do_pending_stack_adjust ();
1477 emit_jump (return_label
);
1480 /* Generate RTL to evaluate the expression RETVAL and return it
1481 from the current function. */
1484 expand_return (tree retval
)
1490 /* If function wants no value, give it none. */
1491 if (TREE_CODE (TREE_TYPE (TREE_TYPE (current_function_decl
))) == VOID_TYPE
)
1493 expand_normal (retval
);
1494 expand_null_return ();
1498 if (retval
== error_mark_node
)
1500 /* Treat this like a return of no value from a function that
1502 expand_null_return ();
1505 else if ((TREE_CODE (retval
) == MODIFY_EXPR
1506 || TREE_CODE (retval
) == INIT_EXPR
)
1507 && TREE_CODE (TREE_OPERAND (retval
, 0)) == RESULT_DECL
)
1508 retval_rhs
= TREE_OPERAND (retval
, 1);
1510 retval_rhs
= retval
;
1512 result_rtl
= DECL_RTL (DECL_RESULT (current_function_decl
));
1514 /* If we are returning the RESULT_DECL, then the value has already
1515 been stored into it, so we don't have to do anything special. */
1516 if (TREE_CODE (retval_rhs
) == RESULT_DECL
)
1517 expand_value_return (result_rtl
);
1519 /* If the result is an aggregate that is being returned in one (or more)
1520 registers, load the registers here. */
1522 else if (retval_rhs
!= 0
1523 && TYPE_MODE (TREE_TYPE (retval_rhs
)) == BLKmode
1524 && REG_P (result_rtl
))
1526 val
= copy_blkmode_to_reg (GET_MODE (result_rtl
), retval_rhs
);
1529 /* Use the mode of the result value on the return register. */
1530 PUT_MODE (result_rtl
, GET_MODE (val
));
1531 expand_value_return (val
);
1534 expand_null_return ();
1536 else if (retval_rhs
!= 0
1537 && !VOID_TYPE_P (TREE_TYPE (retval_rhs
))
1538 && (REG_P (result_rtl
)
1539 || (GET_CODE (result_rtl
) == PARALLEL
)))
1541 /* Calculate the return value into a temporary (usually a pseudo
1543 tree ot
= TREE_TYPE (DECL_RESULT (current_function_decl
));
1544 tree nt
= build_qualified_type (ot
, TYPE_QUALS (ot
) | TYPE_QUAL_CONST
);
1546 val
= assign_temp (nt
, 0, 1);
1547 val
= expand_expr (retval_rhs
, val
, GET_MODE (val
), EXPAND_NORMAL
);
1548 val
= force_not_mem (val
);
1549 /* Return the calculated value. */
1550 expand_value_return (val
);
1554 /* No hard reg used; calculate value into hard return reg. */
1555 expand_expr (retval
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
1556 expand_value_return (result_rtl
);
1560 /* Emit code to restore vital registers at the beginning of a nonlocal goto
1563 expand_nl_goto_receiver (void)
1567 /* Clobber the FP when we get here, so we have to make sure it's
1568 marked as used by this function. */
1569 emit_use (hard_frame_pointer_rtx
);
1571 /* Mark the static chain as clobbered here so life information
1572 doesn't get messed up for it. */
1573 chain
= targetm
.calls
.static_chain (current_function_decl
, true);
1574 if (chain
&& REG_P (chain
))
1575 emit_clobber (chain
);
1577 #ifdef HAVE_nonlocal_goto
1578 if (! HAVE_nonlocal_goto
)
1580 /* First adjust our frame pointer to its actual value. It was
1581 previously set to the start of the virtual area corresponding to
1582 the stacked variables when we branched here and now needs to be
1583 adjusted to the actual hardware fp value.
1585 Assignments are to virtual registers are converted by
1586 instantiate_virtual_regs into the corresponding assignment
1587 to the underlying register (fp in this case) that makes
1588 the original assignment true.
1589 So the following insn will actually be
1590 decrementing fp by STARTING_FRAME_OFFSET. */
1591 emit_move_insn (virtual_stack_vars_rtx
, hard_frame_pointer_rtx
);
1593 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
1594 if (fixed_regs
[ARG_POINTER_REGNUM
])
1596 #ifdef ELIMINABLE_REGS
1597 /* If the argument pointer can be eliminated in favor of the
1598 frame pointer, we don't need to restore it. We assume here
1599 that if such an elimination is present, it can always be used.
1600 This is the case on all known machines; if we don't make this
1601 assumption, we do unnecessary saving on many machines. */
1602 static const struct elims
{const int from
, to
;} elim_regs
[] = ELIMINABLE_REGS
;
1605 for (i
= 0; i
< ARRAY_SIZE (elim_regs
); i
++)
1606 if (elim_regs
[i
].from
== ARG_POINTER_REGNUM
1607 && elim_regs
[i
].to
== HARD_FRAME_POINTER_REGNUM
)
1610 if (i
== ARRAY_SIZE (elim_regs
))
1613 /* Now restore our arg pointer from the address at which it
1614 was saved in our stack frame. */
1615 emit_move_insn (crtl
->args
.internal_arg_pointer
,
1616 copy_to_reg (get_arg_pointer_save_area ()));
1621 #ifdef HAVE_nonlocal_goto_receiver
1622 if (HAVE_nonlocal_goto_receiver
)
1623 emit_insn (gen_nonlocal_goto_receiver ());
1626 /* We must not allow the code we just generated to be reordered by
1627 scheduling. Specifically, the update of the frame pointer must
1628 happen immediately, not later. */
1629 emit_insn (gen_blockage ());
1632 /* Emit code to save the current value of stack. */
1634 expand_stack_save (void)
1638 do_pending_stack_adjust ();
1639 emit_stack_save (SAVE_BLOCK
, &ret
);
1643 /* Emit code to restore the current value of stack. */
1645 expand_stack_restore (tree var
)
1647 rtx prev
, sa
= expand_normal (var
);
1649 sa
= convert_memory_address (Pmode
, sa
);
1651 prev
= get_last_insn ();
1652 emit_stack_restore (SAVE_BLOCK
, sa
);
1653 fixup_args_size_notes (prev
, get_last_insn (), 0);
1656 /* Generate code to jump to LABEL if OP0 and OP1 are equal in mode MODE. PROB
1657 is the probability of jumping to LABEL. */
1659 do_jump_if_equal (enum machine_mode mode
, rtx op0
, rtx op1
, rtx label
,
1660 int unsignedp
, int prob
)
1662 gcc_assert (prob
<= REG_BR_PROB_BASE
);
1663 do_compare_rtx_and_jump (op0
, op1
, EQ
, unsignedp
, mode
,
1664 NULL_RTX
, NULL_RTX
, label
, prob
);
1667 /* Do the insertion of a case label into case_list. The labels are
1668 fed to us in descending order from the sorted vector of case labels used
1669 in the tree part of the middle end. So the list we construct is
1670 sorted in ascending order.
1672 LABEL is the case label to be inserted. LOW and HIGH are the bounds
1673 against which the index is compared to jump to LABEL and PROB is the
1674 estimated probability LABEL is reached from the switch statement. */
1676 static struct case_node
*
1677 add_case_node (struct case_node
*head
, tree low
, tree high
,
1678 tree label
, int prob
, alloc_pool case_node_pool
)
1680 struct case_node
*r
;
1682 gcc_checking_assert (low
);
1683 gcc_checking_assert (high
&& (TREE_TYPE (low
) == TREE_TYPE (high
)));
1685 /* Add this label to the chain. */
1686 r
= (struct case_node
*) pool_alloc (case_node_pool
);
1689 r
->code_label
= label
;
1690 r
->parent
= r
->left
= NULL
;
1692 r
->subtree_prob
= prob
;
1697 /* Dump ROOT, a list or tree of case nodes, to file. */
1700 dump_case_nodes (FILE *f
, struct case_node
*root
,
1701 int indent_step
, int indent_level
)
1703 HOST_WIDE_INT low
, high
;
1709 dump_case_nodes (f
, root
->left
, indent_step
, indent_level
);
1711 low
= tree_low_cst (root
->low
, 0);
1712 high
= tree_low_cst (root
->high
, 0);
1716 fprintf(f
, "%*s" HOST_WIDE_INT_PRINT_DEC
,
1717 indent_step
* indent_level
, "", low
);
1719 fprintf(f
, "%*s" HOST_WIDE_INT_PRINT_DEC
" ... " HOST_WIDE_INT_PRINT_DEC
,
1720 indent_step
* indent_level
, "", low
, high
);
1723 dump_case_nodes (f
, root
->right
, indent_step
, indent_level
);
1727 #define HAVE_casesi 0
1730 #ifndef HAVE_tablejump
1731 #define HAVE_tablejump 0
1734 /* Return the smallest number of different values for which it is best to use a
1735 jump-table instead of a tree of conditional branches. */
1738 case_values_threshold (void)
1740 unsigned int threshold
= PARAM_VALUE (PARAM_CASE_VALUES_THRESHOLD
);
1743 threshold
= targetm
.case_values_threshold ();
1748 /* Return true if a switch should be expanded as a decision tree.
1749 RANGE is the difference between highest and lowest case.
1750 UNIQ is number of unique case node targets, not counting the default case.
1751 COUNT is the number of comparisons needed, not counting the default case. */
1754 expand_switch_as_decision_tree_p (tree range
,
1755 unsigned int uniq ATTRIBUTE_UNUSED
,
1760 /* If neither casesi or tablejump is available, or flag_jump_tables
1761 over-ruled us, we really have no choice. */
1762 if (!HAVE_casesi
&& !HAVE_tablejump
)
1764 if (!flag_jump_tables
)
1767 /* If the switch is relatively small such that the cost of one
1768 indirect jump on the target are higher than the cost of a
1769 decision tree, go with the decision tree.
1771 If range of values is much bigger than number of values,
1772 or if it is too large to represent in a HOST_WIDE_INT,
1773 make a sequence of conditional branches instead of a dispatch.
1775 The definition of "much bigger" depends on whether we are
1776 optimizing for size or for speed. If the former, the maximum
1777 ratio range/count = 3, because this was found to be the optimal
1778 ratio for size on i686-pc-linux-gnu, see PR11823. The ratio
1779 10 is much older, and was probably selected after an extensive
1780 benchmarking investigation on numerous platforms. Or maybe it
1781 just made sense to someone at some point in the history of GCC,
1783 max_ratio
= optimize_insn_for_size_p () ? 3 : 10;
1784 if (count
< case_values_threshold ()
1785 || ! host_integerp (range
, /*pos=*/1)
1786 || compare_tree_int (range
, max_ratio
* count
) > 0)
1792 /* Generate a decision tree, switching on INDEX_EXPR and jumping to
1793 one of the labels in CASE_LIST or to the DEFAULT_LABEL.
1794 DEFAULT_PROB is the estimated probability that it jumps to
1797 We generate a binary decision tree to select the appropriate target
1798 code. This is done as follows:
1800 If the index is a short or char that we do not have
1801 an insn to handle comparisons directly, convert it to
1802 a full integer now, rather than letting each comparison
1803 generate the conversion.
1805 Load the index into a register.
1807 The list of cases is rearranged into a binary tree,
1808 nearly optimal assuming equal probability for each case.
1810 The tree is transformed into RTL, eliminating redundant
1811 test conditions at the same time.
1813 If program flow could reach the end of the decision tree
1814 an unconditional jump to the default code is emitted.
1816 The above process is unaware of the CFG. The caller has to fix up
1817 the CFG itself. This is done in cfgexpand.c. */
1820 emit_case_decision_tree (tree index_expr
, tree index_type
,
1821 struct case_node
*case_list
, rtx default_label
,
1824 rtx index
= expand_normal (index_expr
);
1826 if (GET_MODE_CLASS (GET_MODE (index
)) == MODE_INT
1827 && ! have_insn_for (COMPARE
, GET_MODE (index
)))
1829 int unsignedp
= TYPE_UNSIGNED (index_type
);
1830 enum machine_mode wider_mode
;
1831 for (wider_mode
= GET_MODE (index
); wider_mode
!= VOIDmode
;
1832 wider_mode
= GET_MODE_WIDER_MODE (wider_mode
))
1833 if (have_insn_for (COMPARE
, wider_mode
))
1835 index
= convert_to_mode (wider_mode
, index
, unsignedp
);
1840 do_pending_stack_adjust ();
1844 index
= copy_to_reg (index
);
1845 if (TREE_CODE (index_expr
) == SSA_NAME
)
1846 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (index_expr
), index
);
1849 balance_case_nodes (&case_list
, NULL
);
1851 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1853 int indent_step
= ceil_log2 (TYPE_PRECISION (index_type
)) + 2;
1854 fprintf (dump_file
, ";; Expanding GIMPLE switch as decision tree:\n");
1855 dump_case_nodes (dump_file
, case_list
, indent_step
, 0);
1858 emit_case_nodes (index
, case_list
, default_label
, default_prob
, index_type
);
1860 emit_jump (default_label
);
1863 /* Return the sum of probabilities of outgoing edges of basic block BB. */
1866 get_outgoing_edge_probs (basic_block bb
)
1873 FOR_EACH_EDGE(e
, ei
, bb
->succs
)
1874 prob_sum
+= e
->probability
;
1878 /* Computes the conditional probability of jumping to a target if the branch
1879 instruction is executed.
1880 TARGET_PROB is the estimated probability of jumping to a target relative
1881 to some basic block BB.
1882 BASE_PROB is the probability of reaching the branch instruction relative
1883 to the same basic block BB. */
1886 conditional_probability (int target_prob
, int base_prob
)
1890 gcc_assert (target_prob
>= 0);
1891 gcc_assert (target_prob
<= base_prob
);
1892 return RDIV (target_prob
* REG_BR_PROB_BASE
, base_prob
);
1897 /* Generate a dispatch tabler, switching on INDEX_EXPR and jumping to
1898 one of the labels in CASE_LIST or to the DEFAULT_LABEL.
1899 MINVAL, MAXVAL, and RANGE are the extrema and range of the case
1900 labels in CASE_LIST. STMT_BB is the basic block containing the statement.
1902 First, a jump insn is emitted. First we try "casesi". If that
1903 fails, try "tablejump". A target *must* have one of them (or both).
1905 Then, a table with the target labels is emitted.
1907 The process is unaware of the CFG. The caller has to fix up
1908 the CFG itself. This is done in cfgexpand.c. */
1911 emit_case_dispatch_table (tree index_expr
, tree index_type
,
1912 struct case_node
*case_list
, rtx default_label
,
1913 tree minval
, tree maxval
, tree range
,
1914 basic_block stmt_bb
)
1917 struct case_node
*n
;
1919 rtx fallback_label
= label_rtx (case_list
->code_label
);
1920 rtx table_label
= gen_label_rtx ();
1921 bool has_gaps
= false;
1922 edge default_edge
= stmt_bb
? EDGE_SUCC(stmt_bb
, 0) : NULL
;
1923 int default_prob
= default_edge
? default_edge
->probability
: 0;
1924 int base
= get_outgoing_edge_probs (stmt_bb
);
1925 bool try_with_tablejump
= false;
1927 int new_default_prob
= conditional_probability (default_prob
,
1930 if (! try_casesi (index_type
, index_expr
, minval
, range
,
1931 table_label
, default_label
, fallback_label
,
1934 /* Index jumptables from zero for suitable values of minval to avoid
1935 a subtraction. For the rationale see:
1936 "http://gcc.gnu.org/ml/gcc-patches/2001-10/msg01234.html". */
1937 if (optimize_insn_for_speed_p ()
1938 && compare_tree_int (minval
, 0) > 0
1939 && compare_tree_int (minval
, 3) < 0)
1941 minval
= build_int_cst (index_type
, 0);
1945 try_with_tablejump
= true;
1948 /* Get table of labels to jump to, in order of case index. */
1950 ncases
= tree_low_cst (range
, 0) + 1;
1951 labelvec
= XALLOCAVEC (rtx
, ncases
);
1952 memset (labelvec
, 0, ncases
* sizeof (rtx
));
1954 for (n
= case_list
; n
; n
= n
->right
)
1956 /* Compute the low and high bounds relative to the minimum
1957 value since that should fit in a HOST_WIDE_INT while the
1958 actual values may not. */
1960 = tree_low_cst (fold_build2 (MINUS_EXPR
, index_type
,
1961 n
->low
, minval
), 1);
1962 HOST_WIDE_INT i_high
1963 = tree_low_cst (fold_build2 (MINUS_EXPR
, index_type
,
1964 n
->high
, minval
), 1);
1967 for (i
= i_low
; i
<= i_high
; i
++)
1969 = gen_rtx_LABEL_REF (Pmode
, label_rtx (n
->code_label
));
1972 /* Fill in the gaps with the default. We may have gaps at
1973 the beginning if we tried to avoid the minval subtraction,
1974 so substitute some label even if the default label was
1975 deemed unreachable. */
1977 default_label
= fallback_label
;
1978 for (i
= 0; i
< ncases
; i
++)
1979 if (labelvec
[i
] == 0)
1982 labelvec
[i
] = gen_rtx_LABEL_REF (Pmode
, default_label
);
1987 /* There is at least one entry in the jump table that jumps
1988 to default label. The default label can either be reached
1989 through the indirect jump or the direct conditional jump
1990 before that. Split the probability of reaching the
1991 default label among these two jumps. */
1992 new_default_prob
= conditional_probability (default_prob
/2,
1995 base
-= default_prob
;
1999 base
-= default_prob
;
2004 default_edge
->probability
= default_prob
;
2006 /* We have altered the probability of the default edge. So the probabilities
2007 of all other edges need to be adjusted so that it sums up to
2008 REG_BR_PROB_BASE. */
2013 FOR_EACH_EDGE (e
, ei
, stmt_bb
->succs
)
2014 e
->probability
= RDIV (e
->probability
* REG_BR_PROB_BASE
, base
);
2017 if (try_with_tablejump
)
2019 bool ok
= try_tablejump (index_type
, index_expr
, minval
, range
,
2020 table_label
, default_label
, new_default_prob
);
2023 /* Output the table. */
2024 emit_label (table_label
);
2026 if (CASE_VECTOR_PC_RELATIVE
|| flag_pic
)
2027 emit_jump_insn (gen_rtx_ADDR_DIFF_VEC (CASE_VECTOR_MODE
,
2028 gen_rtx_LABEL_REF (Pmode
, table_label
),
2029 gen_rtvec_v (ncases
, labelvec
),
2030 const0_rtx
, const0_rtx
));
2032 emit_jump_insn (gen_rtx_ADDR_VEC (CASE_VECTOR_MODE
,
2033 gen_rtvec_v (ncases
, labelvec
)));
2035 /* Record no drop-through after the table. */
2039 /* Reset the aux field of all outgoing edges of basic block BB. */
2042 reset_out_edges_aux (basic_block bb
)
2046 FOR_EACH_EDGE(e
, ei
, bb
->succs
)
2050 /* Compute the number of case labels that correspond to each outgoing edge of
2051 STMT. Record this information in the aux field of the edge. */
2054 compute_cases_per_edge (gimple stmt
)
2056 basic_block bb
= gimple_bb (stmt
);
2057 reset_out_edges_aux (bb
);
2058 int ncases
= gimple_switch_num_labels (stmt
);
2059 for (int i
= ncases
- 1; i
>= 1; --i
)
2061 tree elt
= gimple_switch_label (stmt
, i
);
2062 tree lab
= CASE_LABEL (elt
);
2063 basic_block case_bb
= label_to_block_fn (cfun
, lab
);
2064 edge case_edge
= find_edge (bb
, case_bb
);
2065 case_edge
->aux
= (void *)((intptr_t)(case_edge
->aux
) + 1);
2069 /* Terminate a case (Pascal/Ada) or switch (C) statement
2070 in which ORIG_INDEX is the expression to be tested.
2071 If ORIG_TYPE is not NULL, it is the original ORIG_INDEX
2072 type as given in the source before any compiler conversions.
2073 Generate the code to test it and jump to the right place. */
2076 expand_case (gimple stmt
)
2078 tree minval
= NULL_TREE
, maxval
= NULL_TREE
, range
= NULL_TREE
;
2079 rtx default_label
= NULL_RTX
;
2080 unsigned int count
, uniq
;
2082 int ncases
= gimple_switch_num_labels (stmt
);
2083 tree index_expr
= gimple_switch_index (stmt
);
2084 tree index_type
= TREE_TYPE (index_expr
);
2086 basic_block bb
= gimple_bb (stmt
);
2088 /* A list of case labels; it is first built as a list and it may then
2089 be rearranged into a nearly balanced binary tree. */
2090 struct case_node
*case_list
= 0;
2092 /* A pool for case nodes. */
2093 alloc_pool case_node_pool
;
2095 /* An ERROR_MARK occurs for various reasons including invalid data type.
2096 ??? Can this still happen, with GIMPLE and all? */
2097 if (index_type
== error_mark_node
)
2100 /* cleanup_tree_cfg removes all SWITCH_EXPR with their index
2101 expressions being INTEGER_CST. */
2102 gcc_assert (TREE_CODE (index_expr
) != INTEGER_CST
);
2104 case_node_pool
= create_alloc_pool ("struct case_node pool",
2105 sizeof (struct case_node
),
2108 do_pending_stack_adjust ();
2110 /* Find the default case target label. */
2111 default_label
= label_rtx (CASE_LABEL (gimple_switch_default_label (stmt
)));
2112 edge default_edge
= EDGE_SUCC(bb
, 0);
2113 int default_prob
= default_edge
->probability
;
2115 /* Get upper and lower bounds of case values. */
2116 elt
= gimple_switch_label (stmt
, 1);
2117 minval
= fold_convert (index_type
, CASE_LOW (elt
));
2118 elt
= gimple_switch_label (stmt
, ncases
- 1);
2119 if (CASE_HIGH (elt
))
2120 maxval
= fold_convert (index_type
, CASE_HIGH (elt
));
2122 maxval
= fold_convert (index_type
, CASE_LOW (elt
));
2124 /* Compute span of values. */
2125 range
= fold_build2 (MINUS_EXPR
, index_type
, maxval
, minval
);
2127 /* Listify the labels queue and gather some numbers to decide
2128 how to expand this switch(). */
2131 struct pointer_set_t
*seen_labels
= pointer_set_create ();
2132 compute_cases_per_edge (stmt
);
2134 for (i
= ncases
- 1; i
>= 1; --i
)
2136 elt
= gimple_switch_label (stmt
, i
);
2137 tree low
= CASE_LOW (elt
);
2139 tree high
= CASE_HIGH (elt
);
2140 gcc_assert (! high
|| tree_int_cst_lt (low
, high
));
2141 tree lab
= CASE_LABEL (elt
);
2143 /* Count the elements.
2144 A range counts double, since it requires two compares. */
2149 /* If we have not seen this label yet, then increase the
2150 number of unique case node targets seen. */
2151 if (!pointer_set_insert (seen_labels
, lab
))
2154 /* The bounds on the case range, LOW and HIGH, have to be converted
2155 to case's index type TYPE. Note that the original type of the
2156 case index in the source code is usually "lost" during
2157 gimplification due to type promotion, but the case labels retain the
2158 original type. Make sure to drop overflow flags. */
2159 low
= fold_convert (index_type
, low
);
2160 if (TREE_OVERFLOW (low
))
2161 low
= build_int_cst_wide (index_type
,
2162 TREE_INT_CST_LOW (low
),
2163 TREE_INT_CST_HIGH (low
));
2165 /* The canonical from of a case label in GIMPLE is that a simple case
2166 has an empty CASE_HIGH. For the casesi and tablejump expanders,
2167 the back ends want simple cases to have high == low. */
2170 high
= fold_convert (index_type
, high
);
2171 if (TREE_OVERFLOW (high
))
2172 high
= build_int_cst_wide (index_type
,
2173 TREE_INT_CST_LOW (high
),
2174 TREE_INT_CST_HIGH (high
));
2176 basic_block case_bb
= label_to_block_fn (cfun
, lab
);
2177 edge case_edge
= find_edge (bb
, case_bb
);
2178 case_list
= add_case_node (
2179 case_list
, low
, high
, lab
,
2180 case_edge
->probability
/ (intptr_t)(case_edge
->aux
),
2183 pointer_set_destroy (seen_labels
);
2184 reset_out_edges_aux (bb
);
2186 /* cleanup_tree_cfg removes all SWITCH_EXPR with a single
2187 destination, such as one with a default case only.
2188 It also removes cases that are out of range for the switch
2189 type, so we should never get a zero here. */
2190 gcc_assert (count
> 0);
2192 rtx before_case
= get_last_insn ();
2194 /* Decide how to expand this switch.
2195 The two options at this point are a dispatch table (casesi or
2196 tablejump) or a decision tree. */
2198 if (expand_switch_as_decision_tree_p (range
, uniq
, count
))
2199 emit_case_decision_tree (index_expr
, index_type
,
2200 case_list
, default_label
,
2203 emit_case_dispatch_table (index_expr
, index_type
,
2204 case_list
, default_label
,
2205 minval
, maxval
, range
, bb
);
2207 reorder_insns (NEXT_INSN (before_case
), get_last_insn (), before_case
);
2210 free_alloc_pool (case_node_pool
);
2213 /* Expand the dispatch to a short decrement chain if there are few cases
2214 to dispatch to. Likewise if neither casesi nor tablejump is available,
2215 or if flag_jump_tables is set. Otherwise, expand as a casesi or a
2216 tablejump. The index mode is always the mode of integer_type_node.
2217 Trap if no case matches the index.
2219 DISPATCH_INDEX is the index expression to switch on. It should be a
2220 memory or register operand.
2222 DISPATCH_TABLE is a set of case labels. The set should be sorted in
2223 ascending order, be contiguous, starting with value 0, and contain only
2224 single-valued case labels. */
2227 expand_sjlj_dispatch_table (rtx dispatch_index
,
2228 vec
<tree
> dispatch_table
)
2230 tree index_type
= integer_type_node
;
2231 enum machine_mode index_mode
= TYPE_MODE (index_type
);
2233 int ncases
= dispatch_table
.length ();
2235 do_pending_stack_adjust ();
2236 rtx before_case
= get_last_insn ();
2238 /* Expand as a decrement-chain if there are 5 or fewer dispatch
2239 labels. This covers more than 98% of the cases in libjava,
2240 and seems to be a reasonable compromise between the "old way"
2241 of expanding as a decision tree or dispatch table vs. the "new
2242 way" with decrement chain or dispatch table. */
2243 if (dispatch_table
.length () <= 5
2244 || (!HAVE_casesi
&& !HAVE_tablejump
)
2245 || !flag_jump_tables
)
2247 /* Expand the dispatch as a decrement chain:
2249 "switch(index) {case 0: do_0; case 1: do_1; ...; case N: do_N;}"
2253 if (index == 0) do_0; else index--;
2254 if (index == 0) do_1; else index--;
2256 if (index == 0) do_N; else index--;
2258 This is more efficient than a dispatch table on most machines.
2259 The last "index--" is redundant but the code is trivially dead
2260 and will be cleaned up by later passes. */
2261 rtx index
= copy_to_mode_reg (index_mode
, dispatch_index
);
2262 rtx zero
= CONST0_RTX (index_mode
);
2263 for (int i
= 0; i
< ncases
; i
++)
2265 tree elt
= dispatch_table
[i
];
2266 rtx lab
= label_rtx (CASE_LABEL (elt
));
2267 do_jump_if_equal (index_mode
, index
, zero
, lab
, 0, -1);
2268 force_expand_binop (index_mode
, sub_optab
,
2269 index
, CONST1_RTX (index_mode
),
2270 index
, 0, OPTAB_DIRECT
);
2275 /* Similar to expand_case, but much simpler. */
2276 struct case_node
*case_list
= 0;
2277 alloc_pool case_node_pool
= create_alloc_pool ("struct sjlj_case pool",
2278 sizeof (struct case_node
),
2280 tree index_expr
= make_tree (index_type
, dispatch_index
);
2281 tree minval
= build_int_cst (index_type
, 0);
2282 tree maxval
= CASE_LOW (dispatch_table
.last ());
2283 tree range
= maxval
;
2284 rtx default_label
= gen_label_rtx ();
2286 for (int i
= ncases
- 1; i
>= 0; --i
)
2288 tree elt
= dispatch_table
[i
];
2289 tree low
= CASE_LOW (elt
);
2290 tree lab
= CASE_LABEL (elt
);
2291 case_list
= add_case_node (case_list
, low
, low
, lab
, 0, case_node_pool
);
2294 emit_case_dispatch_table (index_expr
, index_type
,
2295 case_list
, default_label
,
2296 minval
, maxval
, range
,
2297 BLOCK_FOR_INSN (before_case
));
2298 emit_label (default_label
);
2299 free_alloc_pool (case_node_pool
);
2302 /* Dispatching something not handled? Trap! */
2303 expand_builtin_trap ();
2305 reorder_insns (NEXT_INSN (before_case
), get_last_insn (), before_case
);
2311 /* Take an ordered list of case nodes
2312 and transform them into a near optimal binary tree,
2313 on the assumption that any target code selection value is as
2314 likely as any other.
2316 The transformation is performed by splitting the ordered
2317 list into two equal sections plus a pivot. The parts are
2318 then attached to the pivot as left and right branches. Each
2319 branch is then transformed recursively. */
2322 balance_case_nodes (case_node_ptr
*head
, case_node_ptr parent
)
2334 /* Count the number of entries on branch. Also count the ranges. */
2338 if (!tree_int_cst_equal (np
->low
, np
->high
))
2347 /* Split this list if it is long enough for that to help. */
2351 /* If there are just three nodes, split at the middle one. */
2353 npp
= &(*npp
)->right
;
2356 /* Find the place in the list that bisects the list's total cost,
2357 where ranges count as 2.
2358 Here I gets half the total cost. */
2359 i
= (i
+ ranges
+ 1) / 2;
2362 /* Skip nodes while their cost does not reach that amount. */
2363 if (!tree_int_cst_equal ((*npp
)->low
, (*npp
)->high
))
2368 npp
= &(*npp
)->right
;
2373 np
->parent
= parent
;
2376 /* Optimize each of the two split parts. */
2377 balance_case_nodes (&np
->left
, np
);
2378 balance_case_nodes (&np
->right
, np
);
2379 np
->subtree_prob
= np
->prob
;
2380 np
->subtree_prob
+= np
->left
->subtree_prob
;
2381 np
->subtree_prob
+= np
->right
->subtree_prob
;
2385 /* Else leave this branch as one level,
2386 but fill in `parent' fields. */
2388 np
->parent
= parent
;
2389 np
->subtree_prob
= np
->prob
;
2390 for (; np
->right
; np
= np
->right
)
2392 np
->right
->parent
= np
;
2393 (*head
)->subtree_prob
+= np
->right
->subtree_prob
;
2399 /* Search the parent sections of the case node tree
2400 to see if a test for the lower bound of NODE would be redundant.
2401 INDEX_TYPE is the type of the index expression.
2403 The instructions to generate the case decision tree are
2404 output in the same order as nodes are processed so it is
2405 known that if a parent node checks the range of the current
2406 node minus one that the current node is bounded at its lower
2407 span. Thus the test would be redundant. */
2410 node_has_low_bound (case_node_ptr node
, tree index_type
)
2413 case_node_ptr pnode
;
2415 /* If the lower bound of this node is the lowest value in the index type,
2416 we need not test it. */
2418 if (tree_int_cst_equal (node
->low
, TYPE_MIN_VALUE (index_type
)))
2421 /* If this node has a left branch, the value at the left must be less
2422 than that at this node, so it cannot be bounded at the bottom and
2423 we need not bother testing any further. */
2428 low_minus_one
= fold_build2 (MINUS_EXPR
, TREE_TYPE (node
->low
),
2430 build_int_cst (TREE_TYPE (node
->low
), 1));
2432 /* If the subtraction above overflowed, we can't verify anything.
2433 Otherwise, look for a parent that tests our value - 1. */
2435 if (! tree_int_cst_lt (low_minus_one
, node
->low
))
2438 for (pnode
= node
->parent
; pnode
; pnode
= pnode
->parent
)
2439 if (tree_int_cst_equal (low_minus_one
, pnode
->high
))
2445 /* Search the parent sections of the case node tree
2446 to see if a test for the upper bound of NODE would be redundant.
2447 INDEX_TYPE is the type of the index expression.
2449 The instructions to generate the case decision tree are
2450 output in the same order as nodes are processed so it is
2451 known that if a parent node checks the range of the current
2452 node plus one that the current node is bounded at its upper
2453 span. Thus the test would be redundant. */
2456 node_has_high_bound (case_node_ptr node
, tree index_type
)
2459 case_node_ptr pnode
;
2461 /* If there is no upper bound, obviously no test is needed. */
2463 if (TYPE_MAX_VALUE (index_type
) == NULL
)
2466 /* If the upper bound of this node is the highest value in the type
2467 of the index expression, we need not test against it. */
2469 if (tree_int_cst_equal (node
->high
, TYPE_MAX_VALUE (index_type
)))
2472 /* If this node has a right branch, the value at the right must be greater
2473 than that at this node, so it cannot be bounded at the top and
2474 we need not bother testing any further. */
2479 high_plus_one
= fold_build2 (PLUS_EXPR
, TREE_TYPE (node
->high
),
2481 build_int_cst (TREE_TYPE (node
->high
), 1));
2483 /* If the addition above overflowed, we can't verify anything.
2484 Otherwise, look for a parent that tests our value + 1. */
2486 if (! tree_int_cst_lt (node
->high
, high_plus_one
))
2489 for (pnode
= node
->parent
; pnode
; pnode
= pnode
->parent
)
2490 if (tree_int_cst_equal (high_plus_one
, pnode
->low
))
2496 /* Search the parent sections of the
2497 case node tree to see if both tests for the upper and lower
2498 bounds of NODE would be redundant. */
2501 node_is_bounded (case_node_ptr node
, tree index_type
)
2503 return (node_has_low_bound (node
, index_type
)
2504 && node_has_high_bound (node
, index_type
));
2508 /* Emit step-by-step code to select a case for the value of INDEX.
2509 The thus generated decision tree follows the form of the
2510 case-node binary tree NODE, whose nodes represent test conditions.
2511 INDEX_TYPE is the type of the index of the switch.
2513 Care is taken to prune redundant tests from the decision tree
2514 by detecting any boundary conditions already checked by
2515 emitted rtx. (See node_has_high_bound, node_has_low_bound
2516 and node_is_bounded, above.)
2518 Where the test conditions can be shown to be redundant we emit
2519 an unconditional jump to the target code. As a further
2520 optimization, the subordinates of a tree node are examined to
2521 check for bounded nodes. In this case conditional and/or
2522 unconditional jumps as a result of the boundary check for the
2523 current node are arranged to target the subordinates associated
2524 code for out of bound conditions on the current node.
2526 We can assume that when control reaches the code generated here,
2527 the index value has already been compared with the parents
2528 of this node, and determined to be on the same side of each parent
2529 as this node is. Thus, if this node tests for the value 51,
2530 and a parent tested for 52, we don't need to consider
2531 the possibility of a value greater than 51. If another parent
2532 tests for the value 50, then this node need not test anything. */
2535 emit_case_nodes (rtx index
, case_node_ptr node
, rtx default_label
,
2536 int default_prob
, tree index_type
)
2538 /* If INDEX has an unsigned type, we must make unsigned branches. */
2539 int unsignedp
= TYPE_UNSIGNED (index_type
);
2541 int prob
= node
->prob
, subtree_prob
= node
->subtree_prob
;
2542 enum machine_mode mode
= GET_MODE (index
);
2543 enum machine_mode imode
= TYPE_MODE (index_type
);
2545 /* Handle indices detected as constant during RTL expansion. */
2546 if (mode
== VOIDmode
)
2549 /* See if our parents have already tested everything for us.
2550 If they have, emit an unconditional jump for this node. */
2551 if (node_is_bounded (node
, index_type
))
2552 emit_jump (label_rtx (node
->code_label
));
2554 else if (tree_int_cst_equal (node
->low
, node
->high
))
2556 probability
= conditional_probability (prob
, subtree_prob
+ default_prob
);
2557 /* Node is single valued. First see if the index expression matches
2558 this node and then check our children, if any. */
2559 do_jump_if_equal (mode
, index
,
2560 convert_modes (mode
, imode
,
2561 expand_normal (node
->low
),
2563 label_rtx (node
->code_label
), unsignedp
, probability
);
2564 /* Since this case is taken at this point, reduce its weight from
2566 subtree_prob
-= prob
;
2567 if (node
->right
!= 0 && node
->left
!= 0)
2569 /* This node has children on both sides.
2570 Dispatch to one side or the other
2571 by comparing the index value with this node's value.
2572 If one subtree is bounded, check that one first,
2573 so we can avoid real branches in the tree. */
2575 if (node_is_bounded (node
->right
, index_type
))
2577 probability
= conditional_probability (
2579 subtree_prob
+ default_prob
);
2580 emit_cmp_and_jump_insns (index
,
2583 expand_normal (node
->high
),
2585 GT
, NULL_RTX
, mode
, unsignedp
,
2586 label_rtx (node
->right
->code_label
),
2588 emit_case_nodes (index
, node
->left
, default_label
, default_prob
,
2592 else if (node_is_bounded (node
->left
, index_type
))
2594 probability
= conditional_probability (
2596 subtree_prob
+ default_prob
);
2597 emit_cmp_and_jump_insns (index
,
2600 expand_normal (node
->high
),
2602 LT
, NULL_RTX
, mode
, unsignedp
,
2603 label_rtx (node
->left
->code_label
),
2605 emit_case_nodes (index
, node
->right
, default_label
, default_prob
, index_type
);
2608 /* If both children are single-valued cases with no
2609 children, finish up all the work. This way, we can save
2610 one ordered comparison. */
2611 else if (tree_int_cst_equal (node
->right
->low
, node
->right
->high
)
2612 && node
->right
->left
== 0
2613 && node
->right
->right
== 0
2614 && tree_int_cst_equal (node
->left
->low
, node
->left
->high
)
2615 && node
->left
->left
== 0
2616 && node
->left
->right
== 0)
2618 /* Neither node is bounded. First distinguish the two sides;
2619 then emit the code for one side at a time. */
2621 /* See if the value matches what the right hand side
2623 probability
= conditional_probability (
2625 subtree_prob
+ default_prob
);
2626 do_jump_if_equal (mode
, index
,
2627 convert_modes (mode
, imode
,
2628 expand_normal (node
->right
->low
),
2630 label_rtx (node
->right
->code_label
),
2631 unsignedp
, probability
);
2633 /* See if the value matches what the left hand side
2635 probability
= conditional_probability (
2637 subtree_prob
+ default_prob
);
2638 do_jump_if_equal (mode
, index
,
2639 convert_modes (mode
, imode
,
2640 expand_normal (node
->left
->low
),
2642 label_rtx (node
->left
->code_label
),
2643 unsignedp
, probability
);
2648 /* Neither node is bounded. First distinguish the two sides;
2649 then emit the code for one side at a time. */
2652 = build_decl (curr_insn_location (),
2653 LABEL_DECL
, NULL_TREE
, NULL_TREE
);
2655 /* The default label could be reached either through the right
2656 subtree or the left subtree. Divide the probability
2658 probability
= conditional_probability (
2659 node
->right
->subtree_prob
+ default_prob
/2,
2660 subtree_prob
+ default_prob
);
2661 /* See if the value is on the right. */
2662 emit_cmp_and_jump_insns (index
,
2665 expand_normal (node
->high
),
2667 GT
, NULL_RTX
, mode
, unsignedp
,
2668 label_rtx (test_label
),
2672 /* Value must be on the left.
2673 Handle the left-hand subtree. */
2674 emit_case_nodes (index
, node
->left
, default_label
, default_prob
, index_type
);
2675 /* If left-hand subtree does nothing,
2678 emit_jump (default_label
);
2680 /* Code branches here for the right-hand subtree. */
2681 expand_label (test_label
);
2682 emit_case_nodes (index
, node
->right
, default_label
, default_prob
, index_type
);
2686 else if (node
->right
!= 0 && node
->left
== 0)
2688 /* Here we have a right child but no left so we issue a conditional
2689 branch to default and process the right child.
2691 Omit the conditional branch to default if the right child
2692 does not have any children and is single valued; it would
2693 cost too much space to save so little time. */
2695 if (node
->right
->right
|| node
->right
->left
2696 || !tree_int_cst_equal (node
->right
->low
, node
->right
->high
))
2698 if (!node_has_low_bound (node
, index_type
))
2700 probability
= conditional_probability (
2702 subtree_prob
+ default_prob
);
2703 emit_cmp_and_jump_insns (index
,
2706 expand_normal (node
->high
),
2708 LT
, NULL_RTX
, mode
, unsignedp
,
2714 emit_case_nodes (index
, node
->right
, default_label
, default_prob
, index_type
);
2718 probability
= conditional_probability (
2719 node
->right
->subtree_prob
,
2720 subtree_prob
+ default_prob
);
2721 /* We cannot process node->right normally
2722 since we haven't ruled out the numbers less than
2723 this node's value. So handle node->right explicitly. */
2724 do_jump_if_equal (mode
, index
,
2727 expand_normal (node
->right
->low
),
2729 label_rtx (node
->right
->code_label
), unsignedp
, probability
);
2733 else if (node
->right
== 0 && node
->left
!= 0)
2735 /* Just one subtree, on the left. */
2736 if (node
->left
->left
|| node
->left
->right
2737 || !tree_int_cst_equal (node
->left
->low
, node
->left
->high
))
2739 if (!node_has_high_bound (node
, index_type
))
2741 probability
= conditional_probability (
2743 subtree_prob
+ default_prob
);
2744 emit_cmp_and_jump_insns (index
,
2747 expand_normal (node
->high
),
2749 GT
, NULL_RTX
, mode
, unsignedp
,
2755 emit_case_nodes (index
, node
->left
, default_label
,
2756 default_prob
, index_type
);
2760 probability
= conditional_probability (
2761 node
->left
->subtree_prob
,
2762 subtree_prob
+ default_prob
);
2763 /* We cannot process node->left normally
2764 since we haven't ruled out the numbers less than
2765 this node's value. So handle node->left explicitly. */
2766 do_jump_if_equal (mode
, index
,
2769 expand_normal (node
->left
->low
),
2771 label_rtx (node
->left
->code_label
), unsignedp
, probability
);
2777 /* Node is a range. These cases are very similar to those for a single
2778 value, except that we do not start by testing whether this node
2779 is the one to branch to. */
2781 if (node
->right
!= 0 && node
->left
!= 0)
2783 /* Node has subtrees on both sides.
2784 If the right-hand subtree is bounded,
2785 test for it first, since we can go straight there.
2786 Otherwise, we need to make a branch in the control structure,
2787 then handle the two subtrees. */
2788 tree test_label
= 0;
2790 if (node_is_bounded (node
->right
, index_type
))
2792 /* Right hand node is fully bounded so we can eliminate any
2793 testing and branch directly to the target code. */
2794 probability
= conditional_probability (
2795 node
->right
->subtree_prob
,
2796 subtree_prob
+ default_prob
);
2797 emit_cmp_and_jump_insns (index
,
2800 expand_normal (node
->high
),
2802 GT
, NULL_RTX
, mode
, unsignedp
,
2803 label_rtx (node
->right
->code_label
),
2808 /* Right hand node requires testing.
2809 Branch to a label where we will handle it later. */
2811 test_label
= build_decl (curr_insn_location (),
2812 LABEL_DECL
, NULL_TREE
, NULL_TREE
);
2813 probability
= conditional_probability (
2814 node
->right
->subtree_prob
+ default_prob
/2,
2815 subtree_prob
+ default_prob
);
2816 emit_cmp_and_jump_insns (index
,
2819 expand_normal (node
->high
),
2821 GT
, NULL_RTX
, mode
, unsignedp
,
2822 label_rtx (test_label
),
2827 /* Value belongs to this node or to the left-hand subtree. */
2829 probability
= conditional_probability (
2831 subtree_prob
+ default_prob
);
2832 emit_cmp_and_jump_insns (index
,
2835 expand_normal (node
->low
),
2837 GE
, NULL_RTX
, mode
, unsignedp
,
2838 label_rtx (node
->code_label
),
2841 /* Handle the left-hand subtree. */
2842 emit_case_nodes (index
, node
->left
, default_label
, default_prob
, index_type
);
2844 /* If right node had to be handled later, do that now. */
2848 /* If the left-hand subtree fell through,
2849 don't let it fall into the right-hand subtree. */
2851 emit_jump (default_label
);
2853 expand_label (test_label
);
2854 emit_case_nodes (index
, node
->right
, default_label
, default_prob
, index_type
);
2858 else if (node
->right
!= 0 && node
->left
== 0)
2860 /* Deal with values to the left of this node,
2861 if they are possible. */
2862 if (!node_has_low_bound (node
, index_type
))
2864 probability
= conditional_probability (
2866 subtree_prob
+ default_prob
);
2867 emit_cmp_and_jump_insns (index
,
2870 expand_normal (node
->low
),
2872 LT
, NULL_RTX
, mode
, unsignedp
,
2878 /* Value belongs to this node or to the right-hand subtree. */
2880 probability
= conditional_probability (
2882 subtree_prob
+ default_prob
);
2883 emit_cmp_and_jump_insns (index
,
2886 expand_normal (node
->high
),
2888 LE
, NULL_RTX
, mode
, unsignedp
,
2889 label_rtx (node
->code_label
),
2892 emit_case_nodes (index
, node
->right
, default_label
, default_prob
, index_type
);
2895 else if (node
->right
== 0 && node
->left
!= 0)
2897 /* Deal with values to the right of this node,
2898 if they are possible. */
2899 if (!node_has_high_bound (node
, index_type
))
2901 probability
= conditional_probability (
2903 subtree_prob
+ default_prob
);
2904 emit_cmp_and_jump_insns (index
,
2907 expand_normal (node
->high
),
2909 GT
, NULL_RTX
, mode
, unsignedp
,
2915 /* Value belongs to this node or to the left-hand subtree. */
2917 probability
= conditional_probability (
2919 subtree_prob
+ default_prob
);
2920 emit_cmp_and_jump_insns (index
,
2923 expand_normal (node
->low
),
2925 GE
, NULL_RTX
, mode
, unsignedp
,
2926 label_rtx (node
->code_label
),
2929 emit_case_nodes (index
, node
->left
, default_label
, default_prob
, index_type
);
2934 /* Node has no children so we check low and high bounds to remove
2935 redundant tests. Only one of the bounds can exist,
2936 since otherwise this node is bounded--a case tested already. */
2937 int high_bound
= node_has_high_bound (node
, index_type
);
2938 int low_bound
= node_has_low_bound (node
, index_type
);
2940 if (!high_bound
&& low_bound
)
2942 probability
= conditional_probability (
2944 subtree_prob
+ default_prob
);
2945 emit_cmp_and_jump_insns (index
,
2948 expand_normal (node
->high
),
2950 GT
, NULL_RTX
, mode
, unsignedp
,
2955 else if (!low_bound
&& high_bound
)
2957 probability
= conditional_probability (
2959 subtree_prob
+ default_prob
);
2960 emit_cmp_and_jump_insns (index
,
2963 expand_normal (node
->low
),
2965 LT
, NULL_RTX
, mode
, unsignedp
,
2969 else if (!low_bound
&& !high_bound
)
2971 /* Widen LOW and HIGH to the same width as INDEX. */
2972 tree type
= lang_hooks
.types
.type_for_mode (mode
, unsignedp
);
2973 tree low
= build1 (CONVERT_EXPR
, type
, node
->low
);
2974 tree high
= build1 (CONVERT_EXPR
, type
, node
->high
);
2975 rtx low_rtx
, new_index
, new_bound
;
2977 /* Instead of doing two branches, emit one unsigned branch for
2978 (index-low) > (high-low). */
2979 low_rtx
= expand_expr (low
, NULL_RTX
, mode
, EXPAND_NORMAL
);
2980 new_index
= expand_simple_binop (mode
, MINUS
, index
, low_rtx
,
2981 NULL_RTX
, unsignedp
,
2983 new_bound
= expand_expr (fold_build2 (MINUS_EXPR
, type
,
2985 NULL_RTX
, mode
, EXPAND_NORMAL
);
2987 probability
= conditional_probability (
2989 subtree_prob
+ default_prob
);
2990 emit_cmp_and_jump_insns (new_index
, new_bound
, GT
, NULL_RTX
,
2991 mode
, 1, default_label
, probability
);
2994 emit_jump (label_rtx (node
->code_label
));