1 /* Expands front end tree to back end RTL for GCC
2 Copyright (C) 1987-2018 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"
40 #include "pretty-print.h"
41 #include "diagnostic-core.h"
43 #include "fold-const.h"
45 #include "stor-layout.h"
50 #include "langhooks.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.
67 Switch statements are expanded in jump table form.
71 struct simple_case_node
73 simple_case_node (tree low
, tree high
, tree code_label
):
74 m_low (low
), m_high (high
), m_code_label (code_label
)
77 /* Lowest index value for this label. */
79 /* Highest index value for this label. */
81 /* Label to jump to when node matches. */
85 static bool check_unique_operand_names (tree
, tree
, tree
);
86 static char *resolve_operand_name_1 (char *, tree
, tree
, tree
);
88 /* Return the rtx-label that corresponds to a LABEL_DECL,
89 creating it if necessary. */
92 label_rtx (tree label
)
94 gcc_assert (TREE_CODE (label
) == LABEL_DECL
);
96 if (!DECL_RTL_SET_P (label
))
98 rtx_code_label
*r
= gen_label_rtx ();
99 SET_DECL_RTL (label
, r
);
100 if (FORCED_LABEL (label
) || DECL_NONLOCAL (label
))
101 LABEL_PRESERVE_P (r
) = 1;
104 return as_a
<rtx_insn
*> (DECL_RTL (label
));
107 /* As above, but also put it on the forced-reference list of the
108 function that contains it. */
110 force_label_rtx (tree label
)
112 rtx_insn
*ref
= label_rtx (label
);
113 tree function
= decl_function_context (label
);
115 gcc_assert (function
);
117 vec_safe_push (forced_labels
, ref
);
121 /* As label_rtx, but ensures (in check build), that returned value is
122 an existing label (i.e. rtx with code CODE_LABEL). */
124 jump_target_rtx (tree label
)
126 return as_a
<rtx_code_label
*> (label_rtx (label
));
129 /* Add an unconditional jump to LABEL as the next sequential instruction. */
132 emit_jump (rtx label
)
134 do_pending_stack_adjust ();
135 emit_jump_insn (targetm
.gen_jump (label
));
139 /* Handle goto statements and the labels that they can go to. */
141 /* Specify the location in the RTL code of a label LABEL,
142 which is a LABEL_DECL tree node.
144 This is used for the kind of label that the user can jump to with a
145 goto statement, and for alternatives of a switch or case statement.
146 RTL labels generated for loops and conditionals don't go through here;
147 they are generated directly at the RTL level, by other functions below.
149 Note that this has nothing to do with defining label *names*.
150 Languages vary in how they do that and what that even means. */
153 expand_label (tree label
)
155 rtx_code_label
*label_r
= jump_target_rtx (label
);
157 do_pending_stack_adjust ();
158 emit_label (label_r
);
159 if (DECL_NAME (label
))
160 LABEL_NAME (DECL_RTL (label
)) = IDENTIFIER_POINTER (DECL_NAME (label
));
162 if (DECL_NONLOCAL (label
))
164 expand_builtin_setjmp_receiver (NULL
);
165 nonlocal_goto_handler_labels
166 = gen_rtx_INSN_LIST (VOIDmode
, label_r
,
167 nonlocal_goto_handler_labels
);
170 if (FORCED_LABEL (label
))
171 vec_safe_push
<rtx_insn
*> (forced_labels
, label_r
);
173 if (DECL_NONLOCAL (label
) || FORCED_LABEL (label
))
174 maybe_set_first_label_num (label_r
);
177 /* Parse the output constraint pointed to by *CONSTRAINT_P. It is the
178 OPERAND_NUMth output operand, indexed from zero. There are NINPUTS
179 inputs and NOUTPUTS outputs to this extended-asm. Upon return,
180 *ALLOWS_MEM will be TRUE iff the constraint allows the use of a
181 memory operand. Similarly, *ALLOWS_REG will be TRUE iff the
182 constraint allows the use of a register operand. And, *IS_INOUT
183 will be true if the operand is read-write, i.e., if it is used as
184 an input as well as an output. If *CONSTRAINT_P is not in
185 canonical form, it will be made canonical. (Note that `+' will be
186 replaced with `=' as part of this process.)
188 Returns TRUE if all went well; FALSE if an error occurred. */
191 parse_output_constraint (const char **constraint_p
, int operand_num
,
192 int ninputs
, int noutputs
, bool *allows_mem
,
193 bool *allows_reg
, bool *is_inout
)
195 const char *constraint
= *constraint_p
;
198 /* Assume the constraint doesn't allow the use of either a register
203 /* Allow the `=' or `+' to not be at the beginning of the string,
204 since it wasn't explicitly documented that way, and there is a
205 large body of code that puts it last. Swap the character to
206 the front, so as not to uglify any place else. */
207 p
= strchr (constraint
, '=');
209 p
= strchr (constraint
, '+');
211 /* If the string doesn't contain an `=', issue an error
215 error ("output operand constraint lacks %<=%>");
219 /* If the constraint begins with `+', then the operand is both read
220 from and written to. */
221 *is_inout
= (*p
== '+');
223 /* Canonicalize the output constraint so that it begins with `='. */
224 if (p
!= constraint
|| *is_inout
)
227 size_t c_len
= strlen (constraint
);
230 warning (0, "output constraint %qc for operand %d "
231 "is not at the beginning",
234 /* Make a copy of the constraint. */
235 buf
= XALLOCAVEC (char, c_len
+ 1);
236 strcpy (buf
, constraint
);
237 /* Swap the first character and the `=' or `+'. */
238 buf
[p
- constraint
] = buf
[0];
239 /* Make sure the first character is an `='. (Until we do this,
240 it might be a `+'.) */
242 /* Replace the constraint with the canonicalized string. */
243 *constraint_p
= ggc_alloc_string (buf
, c_len
);
244 constraint
= *constraint_p
;
247 /* Loop through the constraint string. */
248 for (p
= constraint
+ 1; *p
; )
254 error ("operand constraint contains incorrectly positioned "
259 if (operand_num
+ 1 == ninputs
+ noutputs
)
261 error ("%<%%%> constraint used with last operand");
266 case '?': case '!': case '*': case '&': case '#':
268 case 'E': case 'F': case 'G': case 'H':
269 case 's': case 'i': case 'n':
270 case 'I': case 'J': case 'K': case 'L': case 'M':
271 case 'N': case 'O': case 'P': case ',':
274 case '0': case '1': case '2': case '3': case '4':
275 case '5': case '6': case '7': case '8': case '9':
277 error ("matching constraint not valid in output operand");
281 /* ??? Before flow, auto inc/dec insns are not supposed to exist,
282 excepting those that expand_call created. So match memory
295 enum constraint_num cn
= lookup_constraint (p
);
296 if (reg_class_for_constraint (cn
) != NO_REGS
297 || insn_extra_address_constraint (cn
))
299 else if (insn_extra_memory_constraint (cn
))
302 insn_extra_constraint_allows_reg_mem (cn
, allows_reg
, allows_mem
);
306 for (size_t len
= CONSTRAINT_LEN (*p
, p
); len
; len
--, p
++)
314 /* Similar, but for input constraints. */
317 parse_input_constraint (const char **constraint_p
, int input_num
,
318 int ninputs
, int noutputs
, int ninout
,
319 const char * const * constraints
,
320 bool *allows_mem
, bool *allows_reg
)
322 const char *constraint
= *constraint_p
;
323 const char *orig_constraint
= constraint
;
324 size_t c_len
= strlen (constraint
);
326 bool saw_match
= false;
328 /* Assume the constraint doesn't allow the use of either
329 a register or memory. */
333 /* Make sure constraint has neither `=', `+', nor '&'. */
335 for (j
= 0; j
< c_len
; j
+= CONSTRAINT_LEN (constraint
[j
], constraint
+j
))
336 switch (constraint
[j
])
338 case '+': case '=': case '&':
339 if (constraint
== orig_constraint
)
341 error ("input operand constraint contains %qc", constraint
[j
]);
347 if (constraint
== orig_constraint
348 && input_num
+ 1 == ninputs
- ninout
)
350 error ("%<%%%> constraint used with last operand");
356 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 /* Whether or not a numeric constraint allows a register is
365 decided by the matching constraint, and so there is no need
366 to do anything special with them. We must handle them in
367 the default case, so that we don't unnecessarily force
368 operands to memory. */
369 case '0': case '1': case '2': case '3': case '4':
370 case '5': case '6': case '7': case '8': case '9':
377 match
= strtoul (constraint
+ j
, &end
, 10);
378 if (match
>= (unsigned long) noutputs
)
380 error ("matching constraint references invalid operand number");
384 /* Try and find the real constraint for this dup. Only do this
385 if the matching constraint is the only alternative. */
387 && (j
== 0 || (j
== 1 && constraint
[0] == '%')))
389 constraint
= constraints
[match
];
390 *constraint_p
= constraint
;
391 c_len
= strlen (constraint
);
393 /* ??? At the end of the loop, we will skip the first part of
394 the matched constraint. This assumes not only that the
395 other constraint is an output constraint, but also that
396 the '=' or '+' come first. */
400 j
= end
- constraint
;
401 /* Anticipate increment at end of loop. */
412 if (! ISALPHA (constraint
[j
]))
414 error ("invalid punctuation %qc in constraint", constraint
[j
]);
417 enum constraint_num cn
= lookup_constraint (constraint
+ j
);
418 if (reg_class_for_constraint (cn
) != NO_REGS
419 || insn_extra_address_constraint (cn
))
421 else if (insn_extra_memory_constraint (cn
)
422 || insn_extra_special_memory_constraint (cn
))
425 insn_extra_constraint_allows_reg_mem (cn
, allows_reg
, allows_mem
);
429 if (saw_match
&& !*allows_reg
)
430 warning (0, "matching constraint does not allow a register");
435 /* Return DECL iff there's an overlap between *REGS and DECL, where DECL
436 can be an asm-declared register. Called via walk_tree. */
439 decl_overlaps_hard_reg_set_p (tree
*declp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
443 const HARD_REG_SET
*const regs
= (const HARD_REG_SET
*) data
;
447 if (DECL_HARD_REGISTER (decl
)
448 && REG_P (DECL_RTL (decl
))
449 && REGNO (DECL_RTL (decl
)) < FIRST_PSEUDO_REGISTER
)
451 rtx reg
= DECL_RTL (decl
);
453 if (overlaps_hard_reg_set_p (*regs
, GET_MODE (reg
), REGNO (reg
)))
458 else if (TYPE_P (decl
) || TREE_CODE (decl
) == PARM_DECL
)
463 /* If there is an overlap between *REGS and DECL, return the first overlap
466 tree_overlaps_hard_reg_set (tree decl
, HARD_REG_SET
*regs
)
468 return walk_tree (&decl
, decl_overlaps_hard_reg_set_p
, regs
, NULL
);
472 /* A subroutine of expand_asm_operands. Check that all operand names
473 are unique. Return true if so. We rely on the fact that these names
474 are identifiers, and so have been canonicalized by get_identifier,
475 so all we need are pointer comparisons. */
478 check_unique_operand_names (tree outputs
, tree inputs
, tree labels
)
480 tree i
, j
, i_name
= NULL_TREE
;
482 for (i
= outputs
; i
; i
= TREE_CHAIN (i
))
484 i_name
= TREE_PURPOSE (TREE_PURPOSE (i
));
488 for (j
= TREE_CHAIN (i
); j
; j
= TREE_CHAIN (j
))
489 if (simple_cst_equal (i_name
, TREE_PURPOSE (TREE_PURPOSE (j
))))
493 for (i
= inputs
; i
; i
= TREE_CHAIN (i
))
495 i_name
= TREE_PURPOSE (TREE_PURPOSE (i
));
499 for (j
= TREE_CHAIN (i
); j
; j
= TREE_CHAIN (j
))
500 if (simple_cst_equal (i_name
, TREE_PURPOSE (TREE_PURPOSE (j
))))
502 for (j
= outputs
; j
; j
= TREE_CHAIN (j
))
503 if (simple_cst_equal (i_name
, TREE_PURPOSE (TREE_PURPOSE (j
))))
507 for (i
= labels
; i
; i
= TREE_CHAIN (i
))
509 i_name
= TREE_PURPOSE (i
);
513 for (j
= TREE_CHAIN (i
); j
; j
= TREE_CHAIN (j
))
514 if (simple_cst_equal (i_name
, TREE_PURPOSE (j
)))
516 for (j
= inputs
; j
; j
= TREE_CHAIN (j
))
517 if (simple_cst_equal (i_name
, TREE_PURPOSE (TREE_PURPOSE (j
))))
524 error ("duplicate asm operand name %qs", TREE_STRING_POINTER (i_name
));
528 /* Resolve the names of the operands in *POUTPUTS and *PINPUTS to numbers,
529 and replace the name expansions in STRING and in the constraints to
530 those numbers. This is generally done in the front end while creating
531 the ASM_EXPR generic tree that eventually becomes the GIMPLE_ASM. */
534 resolve_asm_operand_names (tree string
, tree outputs
, tree inputs
, tree labels
)
541 check_unique_operand_names (outputs
, inputs
, labels
);
543 /* Substitute [<name>] in input constraint strings. There should be no
544 named operands in output constraints. */
545 for (t
= inputs
; t
; t
= TREE_CHAIN (t
))
547 c
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t
)));
548 if (strchr (c
, '[') != NULL
)
550 p
= buffer
= xstrdup (c
);
551 while ((p
= strchr (p
, '[')) != NULL
)
552 p
= resolve_operand_name_1 (p
, outputs
, inputs
, NULL
);
553 TREE_VALUE (TREE_PURPOSE (t
))
554 = build_string (strlen (buffer
), buffer
);
559 /* Now check for any needed substitutions in the template. */
560 c
= TREE_STRING_POINTER (string
);
561 while ((c
= strchr (c
, '%')) != NULL
)
565 else if (ISALPHA (c
[1]) && c
[2] == '[')
569 c
+= 1 + (c
[1] == '%');
576 /* OK, we need to make a copy so we can perform the substitutions.
577 Assume that we will not need extra space--we get to remove '['
578 and ']', which means we cannot have a problem until we have more
579 than 999 operands. */
580 buffer
= xstrdup (TREE_STRING_POINTER (string
));
581 p
= buffer
+ (c
- TREE_STRING_POINTER (string
));
583 while ((p
= strchr (p
, '%')) != NULL
)
587 else if (ISALPHA (p
[1]) && p
[2] == '[')
591 p
+= 1 + (p
[1] == '%');
595 p
= resolve_operand_name_1 (p
, outputs
, inputs
, labels
);
598 string
= build_string (strlen (buffer
), buffer
);
605 /* A subroutine of resolve_operand_names. P points to the '[' for a
606 potential named operand of the form [<name>]. In place, replace
607 the name and brackets with a number. Return a pointer to the
608 balance of the string after substitution. */
611 resolve_operand_name_1 (char *p
, tree outputs
, tree inputs
, tree labels
)
617 /* Collect the operand name. */
618 q
= strchr (++p
, ']');
621 error ("missing close brace for named operand");
622 return strchr (p
, '\0');
626 /* Resolve the name to a number. */
627 for (op
= 0, t
= outputs
; t
; t
= TREE_CHAIN (t
), op
++)
629 tree name
= TREE_PURPOSE (TREE_PURPOSE (t
));
630 if (name
&& strcmp (TREE_STRING_POINTER (name
), p
) == 0)
633 for (t
= inputs
; t
; t
= TREE_CHAIN (t
), op
++)
635 tree name
= TREE_PURPOSE (TREE_PURPOSE (t
));
636 if (name
&& strcmp (TREE_STRING_POINTER (name
), p
) == 0)
639 for (t
= labels
; t
; t
= TREE_CHAIN (t
), op
++)
641 tree name
= TREE_PURPOSE (t
);
642 if (name
&& strcmp (TREE_STRING_POINTER (name
), p
) == 0)
646 error ("undefined named operand %qs", identifier_to_locale (p
));
650 /* Replace the name with the number. Unfortunately, not all libraries
651 get the return value of sprintf correct, so search for the end of the
652 generated string by hand. */
653 sprintf (--p
, "%d", op
);
654 p
= strchr (p
, '\0');
656 /* Verify the no extra buffer space assumption. */
659 /* Shift the rest of the buffer down to fill the gap. */
660 memmove (p
, q
+ 1, strlen (q
+ 1) + 1);
666 /* Generate RTL to return directly from the current function.
667 (That is, we bypass any return value.) */
670 expand_naked_return (void)
672 rtx_code_label
*end_label
;
674 clear_pending_stack_adjust ();
675 do_pending_stack_adjust ();
677 end_label
= naked_return_label
;
679 end_label
= naked_return_label
= gen_label_rtx ();
681 emit_jump (end_label
);
684 /* Generate code to jump to LABEL if OP0 and OP1 are equal in mode MODE. PROB
685 is the probability of jumping to LABEL. */
687 do_jump_if_equal (machine_mode mode
, rtx op0
, rtx op1
, rtx_code_label
*label
,
688 int unsignedp
, profile_probability prob
)
690 do_compare_rtx_and_jump (op0
, op1
, EQ
, unsignedp
, mode
,
691 NULL_RTX
, NULL
, label
, prob
);
694 /* Return the sum of probabilities of outgoing edges of basic block BB. */
696 static profile_probability
697 get_outgoing_edge_probs (basic_block bb
)
701 profile_probability prob_sum
= profile_probability::never ();
703 return profile_probability::never ();
704 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
705 prob_sum
+= e
->probability
;
709 /* Computes the conditional probability of jumping to a target if the branch
710 instruction is executed.
711 TARGET_PROB is the estimated probability of jumping to a target relative
712 to some basic block BB.
713 BASE_PROB is the probability of reaching the branch instruction relative
714 to the same basic block BB. */
716 static inline profile_probability
717 conditional_probability (profile_probability target_prob
,
718 profile_probability base_prob
)
720 return target_prob
/ base_prob
;
723 /* Generate a dispatch tabler, switching on INDEX_EXPR and jumping to
724 one of the labels in CASE_LIST or to the DEFAULT_LABEL.
725 MINVAL, MAXVAL, and RANGE are the extrema and range of the case
726 labels in CASE_LIST. STMT_BB is the basic block containing the statement.
728 First, a jump insn is emitted. First we try "casesi". If that
729 fails, try "tablejump". A target *must* have one of them (or both).
731 Then, a table with the target labels is emitted.
733 The process is unaware of the CFG. The caller has to fix up
734 the CFG itself. This is done in cfgexpand.c. */
737 emit_case_dispatch_table (tree index_expr
, tree index_type
,
738 auto_vec
<simple_case_node
> &case_list
,
740 edge default_edge
, tree minval
, tree maxval
,
741 tree range
, basic_block stmt_bb
)
745 rtx_insn
*fallback_label
= label_rtx (case_list
[0].m_code_label
);
746 rtx_code_label
*table_label
= gen_label_rtx ();
747 bool has_gaps
= false;
748 profile_probability default_prob
= default_edge
? default_edge
->probability
749 : profile_probability::never ();
750 profile_probability base
= get_outgoing_edge_probs (stmt_bb
);
751 bool try_with_tablejump
= false;
753 profile_probability new_default_prob
= conditional_probability (default_prob
,
756 if (! try_casesi (index_type
, index_expr
, minval
, range
,
757 table_label
, default_label
, fallback_label
,
760 /* Index jumptables from zero for suitable values of minval to avoid
761 a subtraction. For the rationale see:
762 "http://gcc.gnu.org/ml/gcc-patches/2001-10/msg01234.html". */
763 if (optimize_insn_for_speed_p ()
764 && compare_tree_int (minval
, 0) > 0
765 && compare_tree_int (minval
, 3) < 0)
767 minval
= build_int_cst (index_type
, 0);
771 try_with_tablejump
= true;
774 /* Get table of labels to jump to, in order of case index. */
776 ncases
= tree_to_shwi (range
) + 1;
777 labelvec
= XALLOCAVEC (rtx
, ncases
);
778 memset (labelvec
, 0, ncases
* sizeof (rtx
));
780 for (unsigned j
= 0; j
< case_list
.length (); j
++)
782 simple_case_node
*n
= &case_list
[j
];
783 /* Compute the low and high bounds relative to the minimum
784 value since that should fit in a HOST_WIDE_INT while the
785 actual values may not. */
787 = tree_to_uhwi (fold_build2 (MINUS_EXPR
, index_type
,
790 = tree_to_uhwi (fold_build2 (MINUS_EXPR
, index_type
,
794 for (i
= i_low
; i
<= i_high
; i
++)
796 = gen_rtx_LABEL_REF (Pmode
, label_rtx (n
->m_code_label
));
799 /* The dispatch table may contain gaps, including at the beginning of
800 the table if we tried to avoid the minval subtraction. We fill the
801 dispatch table slots associated with the gaps with the default case label.
802 However, in the event the default case is unreachable, we then use
803 any label from one of the case statements. */
804 rtx gap_label
= (default_label
) ? default_label
: fallback_label
;
806 for (i
= 0; i
< ncases
; i
++)
807 if (labelvec
[i
] == 0)
810 labelvec
[i
] = gen_rtx_LABEL_REF (Pmode
, gap_label
);
813 if (has_gaps
&& default_label
)
815 /* There is at least one entry in the jump table that jumps
816 to default label. The default label can either be reached
817 through the indirect jump or the direct conditional jump
818 before that. Split the probability of reaching the
819 default label among these two jumps. */
821 = conditional_probability (default_prob
.apply_scale (1, 2), base
);
822 default_prob
= default_prob
.apply_scale (1, 2);
823 base
-= default_prob
;
827 base
-= default_prob
;
828 default_prob
= profile_probability::never ();
832 default_edge
->probability
= default_prob
;
834 /* We have altered the probability of the default edge. So the probabilities
835 of all other edges need to be adjusted so that it sums up to
837 if (base
> profile_probability::never ())
841 FOR_EACH_EDGE (e
, ei
, stmt_bb
->succs
)
842 e
->probability
/= base
;
845 if (try_with_tablejump
)
847 bool ok
= try_tablejump (index_type
, index_expr
, minval
, range
,
848 table_label
, default_label
, new_default_prob
);
851 /* Output the table. */
852 emit_label (table_label
);
854 if (CASE_VECTOR_PC_RELATIVE
855 || (flag_pic
&& targetm
.asm_out
.generate_pic_addr_diff_vec ()))
856 emit_jump_table_data (gen_rtx_ADDR_DIFF_VEC (CASE_VECTOR_MODE
,
857 gen_rtx_LABEL_REF (Pmode
,
859 gen_rtvec_v (ncases
, labelvec
),
860 const0_rtx
, const0_rtx
));
862 emit_jump_table_data (gen_rtx_ADDR_VEC (CASE_VECTOR_MODE
,
863 gen_rtvec_v (ncases
, labelvec
)));
865 /* Record no drop-through after the table. */
869 /* Terminate a case Ada or switch (C) statement
870 in which ORIG_INDEX is the expression to be tested.
871 If ORIG_TYPE is not NULL, it is the original ORIG_INDEX
872 type as given in the source before any compiler conversions.
873 Generate the code to test it and jump to the right place. */
876 expand_case (gswitch
*stmt
)
878 tree minval
= NULL_TREE
, maxval
= NULL_TREE
, range
= NULL_TREE
;
879 rtx_code_label
*default_label
;
882 int ncases
= gimple_switch_num_labels (stmt
);
883 tree index_expr
= gimple_switch_index (stmt
);
884 tree index_type
= TREE_TYPE (index_expr
);
886 basic_block bb
= gimple_bb (stmt
);
888 auto_vec
<simple_case_node
> case_list
;
890 /* An ERROR_MARK occurs for various reasons including invalid data type.
891 ??? Can this still happen, with GIMPLE and all? */
892 if (index_type
== error_mark_node
)
895 /* cleanup_tree_cfg removes all SWITCH_EXPR with their index
896 expressions being INTEGER_CST. */
897 gcc_assert (TREE_CODE (index_expr
) != INTEGER_CST
);
899 /* Optimization of switch statements with only one label has already
900 occurred, so we should never see them at this point. */
901 gcc_assert (ncases
> 1);
903 do_pending_stack_adjust ();
905 /* Find the default case target label. */
906 tree default_lab
= CASE_LABEL (gimple_switch_default_label (stmt
));
907 default_label
= jump_target_rtx (default_lab
);
908 basic_block default_bb
= label_to_block (cfun
, default_lab
);
909 edge default_edge
= find_edge (bb
, default_bb
);
911 /* Get upper and lower bounds of case values. */
912 elt
= gimple_switch_label (stmt
, 1);
913 minval
= fold_convert (index_type
, CASE_LOW (elt
));
914 elt
= gimple_switch_label (stmt
, ncases
- 1);
916 maxval
= fold_convert (index_type
, CASE_HIGH (elt
));
918 maxval
= fold_convert (index_type
, CASE_LOW (elt
));
920 /* Compute span of values. */
921 range
= fold_build2 (MINUS_EXPR
, index_type
, maxval
, minval
);
923 /* Listify the labels queue and gather some numbers to decide
924 how to expand this switch(). */
927 for (i
= ncases
- 1; i
>= 1; --i
)
929 elt
= gimple_switch_label (stmt
, i
);
930 tree low
= CASE_LOW (elt
);
932 tree high
= CASE_HIGH (elt
);
933 gcc_assert (! high
|| tree_int_cst_lt (low
, high
));
934 tree lab
= CASE_LABEL (elt
);
936 /* Count the elements.
937 A range counts double, since it requires two compares. */
942 /* The bounds on the case range, LOW and HIGH, have to be converted
943 to case's index type TYPE. Note that the original type of the
944 case index in the source code is usually "lost" during
945 gimplification due to type promotion, but the case labels retain the
946 original type. Make sure to drop overflow flags. */
947 low
= fold_convert (index_type
, low
);
948 if (TREE_OVERFLOW (low
))
949 low
= wide_int_to_tree (index_type
, wi::to_wide (low
));
951 /* The canonical from of a case label in GIMPLE is that a simple case
952 has an empty CASE_HIGH. For the casesi and tablejump expanders,
953 the back ends want simple cases to have high == low. */
956 high
= fold_convert (index_type
, high
);
957 if (TREE_OVERFLOW (high
))
958 high
= wide_int_to_tree (index_type
, wi::to_wide (high
));
960 case_list
.safe_push (simple_case_node (low
, high
, lab
));
963 /* cleanup_tree_cfg removes all SWITCH_EXPR with a single
964 destination, such as one with a default case only.
965 It also removes cases that are out of range for the switch
966 type, so we should never get a zero here. */
967 gcc_assert (count
> 0);
969 rtx_insn
*before_case
= get_last_insn ();
971 /* Decide how to expand this switch.
972 The two options at this point are a dispatch table (casesi or
973 tablejump) or a decision tree. */
976 /* If the default case is unreachable, then set default_label to NULL
977 so that we omit the range check when generating the dispatch table.
978 We also remove the edge to the unreachable default case. The block
979 itself will be automatically removed later. */
980 if (EDGE_COUNT (default_edge
->dest
->succs
) == 0
981 && gimple_seq_unreachable_p (bb_seq (default_edge
->dest
)))
983 default_label
= NULL
;
984 remove_edge (default_edge
);
987 emit_case_dispatch_table (index_expr
, index_type
,
988 case_list
, default_label
, default_edge
,
989 minval
, maxval
, range
, bb
);
992 reorder_insns (NEXT_INSN (before_case
), get_last_insn (), before_case
);
997 /* Expand the dispatch to a short decrement chain if there are few cases
998 to dispatch to. Likewise if neither casesi nor tablejump is available,
999 or if flag_jump_tables is set. Otherwise, expand as a casesi or a
1000 tablejump. The index mode is always the mode of integer_type_node.
1001 Trap if no case matches the index.
1003 DISPATCH_INDEX is the index expression to switch on. It should be a
1004 memory or register operand.
1006 DISPATCH_TABLE is a set of case labels. The set should be sorted in
1007 ascending order, be contiguous, starting with value 0, and contain only
1008 single-valued case labels. */
1011 expand_sjlj_dispatch_table (rtx dispatch_index
,
1012 vec
<tree
> dispatch_table
)
1014 tree index_type
= integer_type_node
;
1015 machine_mode index_mode
= TYPE_MODE (index_type
);
1017 int ncases
= dispatch_table
.length ();
1019 do_pending_stack_adjust ();
1020 rtx_insn
*before_case
= get_last_insn ();
1022 /* Expand as a decrement-chain if there are 5 or fewer dispatch
1023 labels. This covers more than 98% of the cases in libjava,
1024 and seems to be a reasonable compromise between the "old way"
1025 of expanding as a decision tree or dispatch table vs. the "new
1026 way" with decrement chain or dispatch table. */
1027 if (dispatch_table
.length () <= 5
1028 || (!targetm
.have_casesi () && !targetm
.have_tablejump ())
1029 || !flag_jump_tables
)
1031 /* Expand the dispatch as a decrement chain:
1033 "switch(index) {case 0: do_0; case 1: do_1; ...; case N: do_N;}"
1037 if (index == 0) do_0; else index--;
1038 if (index == 0) do_1; else index--;
1040 if (index == 0) do_N; else index--;
1042 This is more efficient than a dispatch table on most machines.
1043 The last "index--" is redundant but the code is trivially dead
1044 and will be cleaned up by later passes. */
1045 rtx index
= copy_to_mode_reg (index_mode
, dispatch_index
);
1046 rtx zero
= CONST0_RTX (index_mode
);
1047 for (int i
= 0; i
< ncases
; i
++)
1049 tree elt
= dispatch_table
[i
];
1050 rtx_code_label
*lab
= jump_target_rtx (CASE_LABEL (elt
));
1051 do_jump_if_equal (index_mode
, index
, zero
, lab
, 0,
1052 profile_probability::uninitialized ());
1053 force_expand_binop (index_mode
, sub_optab
,
1054 index
, CONST1_RTX (index_mode
),
1055 index
, 0, OPTAB_DIRECT
);
1060 /* Similar to expand_case, but much simpler. */
1061 auto_vec
<simple_case_node
> case_list
;
1062 tree index_expr
= make_tree (index_type
, dispatch_index
);
1063 tree minval
= build_int_cst (index_type
, 0);
1064 tree maxval
= CASE_LOW (dispatch_table
.last ());
1065 tree range
= maxval
;
1066 rtx_code_label
*default_label
= gen_label_rtx ();
1068 for (int i
= ncases
- 1; i
>= 0; --i
)
1070 tree elt
= dispatch_table
[i
];
1071 tree high
= CASE_HIGH (elt
);
1072 if (high
== NULL_TREE
)
1073 high
= CASE_LOW (elt
);
1074 case_list
.safe_push (simple_case_node (CASE_LOW (elt
), high
,
1078 emit_case_dispatch_table (index_expr
, index_type
,
1079 case_list
, default_label
, NULL
,
1080 minval
, maxval
, range
,
1081 BLOCK_FOR_INSN (before_case
));
1082 emit_label (default_label
);
1085 /* Dispatching something not handled? Trap! */
1086 expand_builtin_trap ();
1088 reorder_insns (NEXT_INSN (before_case
), get_last_insn (), before_case
);