Line data Source code
1 : /* Expands front end tree to back end RTL for GCC
2 : Copyright (C) 1987-2026 Free Software Foundation, Inc.
3 :
4 : This file is part of GCC.
5 :
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
9 : version.
10 :
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
14 : for more details.
15 :
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/>. */
19 :
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.cc.
22 : The functions whose names start with `expand_' are called by the
23 : expander to generate RTL instructions for various kinds of constructs. */
24 :
25 : #include "config.h"
26 : #include "system.h"
27 : #include "coretypes.h"
28 : #include "backend.h"
29 : #include "target.h"
30 : #include "rtl.h"
31 : #include "tree.h"
32 : #include "gimple.h"
33 : #include "cfghooks.h"
34 : #include "predict.h"
35 : #include "memmodel.h"
36 : #include "tm_p.h"
37 : #include "optabs.h"
38 : #include "regs.h"
39 : #include "emit-rtl.h"
40 : #include "pretty-print.h"
41 : #include "diagnostic-core.h"
42 : #include "output.h"
43 : #include "gimplify_reg_info.h"
44 :
45 : #include "fold-const.h"
46 : #include "varasm.h"
47 : #include "stor-layout.h"
48 : #include "dojump.h"
49 : #include "explow.h"
50 : #include "stmt.h"
51 : #include "expr.h"
52 : #include "langhooks.h"
53 : #include "cfganal.h"
54 : #include "tree-cfg.h"
55 : #include "dumpfile.h"
56 : #include "builtins.h"
57 : #include "cfgexpand.h"
58 :
59 : #if CHECKING_P
60 : #include "selftest.h"
61 : #endif
62 :
63 :
64 : /* Functions and data structures for expanding case statements. */
65 :
66 : /* Case label structure, used to hold info on labels within case
67 : statements. We handle "range" labels; for a single-value label
68 : as in C, the high and low limits are the same.
69 :
70 : We start with a vector of case nodes sorted in ascending order, and
71 : the default label as the last element in the vector.
72 :
73 : Switch statements are expanded in jump table form.
74 :
75 : */
76 :
77 : class simple_case_node
78 : {
79 : public:
80 83121 : simple_case_node (tree low, tree high, tree code_label):
81 83121 : m_low (low), m_high (high), m_code_label (code_label)
82 : {}
83 :
84 : /* Lowest index value for this label. */
85 : tree m_low;
86 : /* Highest index value for this label. */
87 : tree m_high;
88 : /* Label to jump to when node matches. */
89 : tree m_code_label;
90 : };
91 :
92 : static bool check_unique_operand_names (tree, tree, tree);
93 :
94 : /* Return the rtx-label that corresponds to a LABEL_DECL,
95 : creating it if necessary. */
96 :
97 : rtx_insn *
98 2740419 : label_rtx (tree label)
99 : {
100 2740419 : gcc_assert (TREE_CODE (label) == LABEL_DECL);
101 :
102 2740419 : if (!DECL_RTL_SET_P (label))
103 : {
104 666412 : rtx_code_label *r = gen_label_rtx ();
105 666412 : SET_DECL_RTL (label, r);
106 666412 : if (FORCED_LABEL (label) || DECL_NONLOCAL (label))
107 27192 : LABEL_PRESERVE_P (r) = 1;
108 : }
109 :
110 2740419 : return as_a <rtx_insn *> (DECL_RTL (label));
111 : }
112 :
113 : /* As above, but also put it on the forced-reference list of the
114 : function that contains it. */
115 : rtx_insn *
116 0 : force_label_rtx (tree label)
117 : {
118 0 : rtx_insn *ref = label_rtx (label);
119 0 : tree function = decl_function_context (label);
120 :
121 0 : gcc_assert (function);
122 :
123 0 : vec_safe_push (forced_labels, ref);
124 0 : return ref;
125 : }
126 :
127 : /* As label_rtx, but ensures (in check build), that returned value is
128 : an existing label (i.e. rtx with code CODE_LABEL). */
129 : rtx_code_label *
130 911402 : jump_target_rtx (tree label)
131 : {
132 911402 : return as_a <rtx_code_label *> (label_rtx (label));
133 : }
134 :
135 : /* Add an unconditional jump to LABEL as the next sequential instruction. */
136 :
137 : void
138 3246288 : emit_jump (rtx_code_label *label)
139 : {
140 3246288 : do_pending_stack_adjust ();
141 3246288 : emit_jump_insn (targetm.gen_jump (label));
142 3246288 : emit_barrier ();
143 3246288 : }
144 :
145 : /* Handle goto statements and the labels that they can go to. */
146 :
147 : /* Specify the location in the RTL code of a label LABEL,
148 : which is a LABEL_DECL tree node.
149 :
150 : This is used for the kind of label that the user can jump to with a
151 : goto statement, and for alternatives of a switch or case statement.
152 : RTL labels generated for loops and conditionals don't go through here;
153 : they are generated directly at the RTL level, by other functions below.
154 :
155 : Note that this has nothing to do with defining label *names*.
156 : Languages vary in how they do that and what that even means. */
157 :
158 : void
159 666412 : expand_label (tree label)
160 : {
161 666412 : rtx_code_label *label_r = jump_target_rtx (label);
162 :
163 666412 : do_pending_stack_adjust ();
164 666412 : emit_label (label_r);
165 666412 : if (DECL_NAME (label))
166 122290 : LABEL_NAME (DECL_RTL (label)) = IDENTIFIER_POINTER (DECL_NAME (label));
167 :
168 666412 : if (DECL_NONLOCAL (label))
169 : {
170 525 : expand_builtin_setjmp_receiver (NULL);
171 525 : nonlocal_goto_handler_labels
172 525 : = gen_rtx_INSN_LIST (VOIDmode, label_r,
173 525 : nonlocal_goto_handler_labels);
174 : }
175 :
176 666412 : if (FORCED_LABEL (label))
177 26667 : vec_safe_push<rtx_insn *> (forced_labels, label_r);
178 :
179 1332299 : if (DECL_NONLOCAL (label) || FORCED_LABEL (label))
180 27192 : maybe_set_first_label_num (label_r);
181 666412 : }
182 :
183 : /* Parse a hard register constraint and return its number or -1 in case of an
184 : error. BEGIN should point to a string of the form `{regname}`. For the
185 : sake of simplicity assume that a register name is not longer than 31
186 : characters, if not error out. */
187 :
188 : int
189 1439 : decode_hard_reg_constraint (const char *begin)
190 : {
191 1439 : if (*begin != '{')
192 : return -1;
193 1439 : ++begin;
194 1439 : const char *end = begin;
195 5756 : while (*end != '}' && *end != '\0')
196 4317 : ++end;
197 1439 : if (*end != '}' || end == begin)
198 : return -1;
199 1437 : ptrdiff_t len = end - begin;
200 1437 : if (len >= 31)
201 : return -1;
202 1437 : char regname[32];
203 1437 : memcpy (regname, begin, len);
204 1437 : regname[len] = '\0';
205 1437 : int regno = decode_reg_name (regname);
206 1437 : return regno;
207 : }
208 :
209 : static bool
210 0 : eliminable_regno_p (int regnum)
211 : {
212 0 : static const struct
213 : {
214 : const int from;
215 : const int to;
216 : } eliminables[] = ELIMINABLE_REGS;
217 0 : for (size_t i = 0; i < ARRAY_SIZE (eliminables); i++)
218 0 : if (regnum == eliminables[i].from)
219 : return true;
220 : return false;
221 : }
222 :
223 : /* Perform a similar check as done in make_decl_rtl(). */
224 :
225 : static bool
226 189 : hardreg_ok_p (int reg_number, machine_mode mode, int operand_num)
227 : {
228 189 : if (mode == BLKmode)
229 1 : error ("data type isn%'t suitable for register %s of operand %i",
230 : reg_names[reg_number], operand_num);
231 188 : else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
232 0 : error ("register %s for operand %i cannot be accessed"
233 : " by the current target", reg_names[reg_number], operand_num);
234 188 : else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
235 0 : error ("register %s for operand %i is not general enough"
236 : " to be used as a register variable", reg_names[reg_number], operand_num);
237 188 : else if (!targetm.hard_regno_mode_ok (reg_number, mode))
238 0 : error ("register %s for operand %i isn%'t suitable for data type",
239 : reg_names[reg_number], operand_num);
240 188 : else if (reg_number != HARD_FRAME_POINTER_REGNUM
241 188 : && (reg_number == FRAME_POINTER_REGNUM
242 : #ifdef RETURN_ADDRESS_POINTER_REGNUM
243 : || reg_number == RETURN_ADDRESS_POINTER_REGNUM
244 : #endif
245 188 : || reg_number == ARG_POINTER_REGNUM)
246 188 : && eliminable_regno_p (reg_number))
247 0 : error ("register for operand %i is an internal GCC "
248 : "implementation detail", operand_num);
249 : else
250 : return true;
251 : return false;
252 : }
253 :
254 : /* Parse the output constraint pointed to by *CONSTRAINT_P. It is the
255 : OPERAND_NUMth output operand, indexed from zero. There are NINPUTS
256 : inputs and NOUTPUTS outputs to this extended-asm. Upon return,
257 : *ALLOWS_MEM will be TRUE iff the constraint allows the use of a
258 : memory operand. Similarly, *ALLOWS_REG will be TRUE iff the
259 : constraint allows the use of a register operand. And, *IS_INOUT
260 : will be true if the operand is read-write, i.e., if it is used as
261 : an input as well as an output. If *CONSTRAINT_P is not in
262 : canonical form, it will be made canonical. (Note that `+' will be
263 : replaced with `=' as part of this process.)
264 :
265 : Returns TRUE if all went well; FALSE if an error occurred. */
266 :
267 : bool
268 35597282 : parse_output_constraint (const char **constraint_p, int operand_num,
269 : int ninputs, int noutputs, bool *allows_mem,
270 : bool *allows_reg, bool *is_inout,
271 : gimplify_reg_info *reg_info)
272 : {
273 35597282 : const char *constraint = *constraint_p;
274 35597282 : const char *p;
275 :
276 : /* Assume the constraint doesn't allow the use of either a register
277 : or memory. */
278 35597282 : *allows_mem = false;
279 35597282 : *allows_reg = false;
280 :
281 : /* Allow the `=' or `+' to not be at the beginning of the string,
282 : since it wasn't explicitly documented that way, and there is a
283 : large body of code that puts it last. Swap the character to
284 : the front, so as not to uglify any place else. */
285 35597282 : p = strchr (constraint, '=');
286 35597282 : if (!p)
287 17489 : p = strchr (constraint, '+');
288 :
289 : /* If the string doesn't contain an `=', issue an error
290 : message. */
291 17489 : if (!p)
292 : {
293 9 : error ("output operand constraint lacks %<=%>");
294 9 : return false;
295 : }
296 :
297 : /* If the constraint begins with `+', then the operand is both read
298 : from and written to. */
299 35597273 : *is_inout = (*p == '+');
300 :
301 : /* Canonicalize the output constraint so that it begins with `='. */
302 35597273 : if (p != constraint || *is_inout)
303 : {
304 17488 : char *buf;
305 17488 : size_t c_len = strlen (constraint);
306 :
307 17488 : if (p != constraint)
308 8 : warning (0, "output constraint %qc for operand %d "
309 : "is not at the beginning",
310 8 : *p, operand_num);
311 :
312 : /* Make a copy of the constraint. */
313 17488 : buf = XALLOCAVEC (char, c_len + 1);
314 17488 : strcpy (buf, constraint);
315 : /* Swap the first character and the `=' or `+'. */
316 17488 : buf[p - constraint] = buf[0];
317 : /* Make sure the first character is an `='. (Until we do this,
318 : it might be a `+'.) */
319 17488 : buf[0] = '=';
320 : /* Replace the constraint with the canonicalized string. */
321 17488 : *constraint_p = ggc_alloc_string (buf, c_len);
322 17488 : constraint = *constraint_p;
323 : }
324 :
325 35597273 : unsigned int alt = 0;
326 35597273 : bool early_clobbered = false;
327 : /* Currently, multiple hard register constraints in one alternative are not
328 : supported. A combination of hard register constraints and regular
329 : register constraints is also not supported. */
330 35597273 : bool alt_has_hard_reg_cstr = false;
331 35597273 : bool alt_has_reg_cstr = false;
332 :
333 : /* Loop through the constraint string. */
334 72021969 : for (p = constraint + 1; *p; )
335 : {
336 36424715 : switch (*p)
337 : {
338 0 : case '+':
339 0 : case '=':
340 0 : error ("operand constraint contains incorrectly positioned "
341 : "%<+%> or %<=%>");
342 0 : return false;
343 :
344 8 : case '%':
345 8 : if (operand_num + 1 == ninputs + noutputs)
346 : {
347 0 : error ("%<%%%> constraint used with last operand");
348 0 : return false;
349 : }
350 : break;
351 :
352 : case '&':
353 36424696 : early_clobbered = true;
354 : break;
355 :
356 : case '?': case '!': case '*': case '#':
357 : 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 '-':
362 : break;
363 :
364 86807 : case ',':
365 86807 : ++alt;
366 86807 : early_clobbered = false;
367 86807 : alt_has_hard_reg_cstr = false;
368 86807 : alt_has_reg_cstr = false;
369 86807 : break;
370 :
371 0 : case '0': case '1': case '2': case '3': case '4':
372 0 : case '5': case '6': case '7': case '8': case '9':
373 0 : case '[':
374 0 : error ("matching constraint not valid in output operand");
375 0 : return false;
376 :
377 8 : case ':':
378 8 : error ("%<:%> constraint used for output operand");
379 8 : return false;
380 :
381 0 : case '<': case '>':
382 : /* ??? Before flow, auto inc/dec insns are not supposed to exist,
383 : excepting those that expand_call created. So match memory
384 : and hope. */
385 0 : *allows_mem = true;
386 0 : break;
387 :
388 2731180 : case 'g': case 'X':
389 2731180 : *allows_reg = true;
390 2731180 : *allows_mem = true;
391 2731180 : break;
392 :
393 25736 : case '{':
394 25736 : {
395 25736 : if (!targetm.lra_p ())
396 : {
397 0 : error ("hard register constraints are only supported while using LRA");
398 0 : return false;
399 : }
400 25736 : if (reg_info)
401 : {
402 103 : if (alt_has_reg_cstr)
403 : {
404 1 : error (
405 : "hard register constraints and regular register "
406 : "constraints in one alternative are not supported");
407 1 : return false;
408 : }
409 102 : if (alt_has_hard_reg_cstr)
410 : {
411 2 : error (
412 : "multiple hard register constraints in one "
413 : "alternative are not supported");
414 2 : return false;
415 : }
416 100 : alt_has_hard_reg_cstr = true;
417 100 : int regno = decode_hard_reg_constraint (p);
418 100 : if (regno < 0)
419 : {
420 0 : error ("invalid output constraint: %s", p);
421 0 : return false;
422 : }
423 100 : int overlap_regno = reg_info->test_alt_output (alt, regno);
424 100 : if (overlap_regno < 0)
425 99 : overlap_regno = reg_info->test_reg_asm_output (regno);
426 99 : if (overlap_regno >= 0)
427 : {
428 1 : error ("multiple outputs to hard register: %s",
429 : reg_names[overlap_regno]);
430 1 : return false;
431 : }
432 99 : reg_info->set_output (alt, regno);
433 99 : if (early_clobbered)
434 16 : reg_info->set_early_clobbered (alt, operand_num, regno);
435 99 : if (reg_info->is_clobbered (regno))
436 : {
437 1 : error ("hard register constraint for output %i conflicts "
438 : "with %<asm%> clobber list", operand_num);
439 1 : return false;
440 : }
441 98 : if (VAR_P (reg_info->operand)
442 98 : && DECL_HARD_REGISTER (reg_info->operand))
443 : {
444 2 : tree id = DECL_ASSEMBLER_NAME (reg_info->operand);
445 2 : const char *asmspec = IDENTIFIER_POINTER (id) + 1;
446 2 : int regno_op = decode_reg_name (asmspec);
447 2 : if (regno != regno_op)
448 : {
449 1 : error ("constraint and register %<asm%> for output "
450 : "operand %i are unsatisfiable", operand_num);
451 1 : return false;
452 : }
453 : }
454 97 : machine_mode mode = TYPE_MODE (TREE_TYPE (reg_info->operand));
455 97 : if (!hardreg_ok_p (regno, mode, operand_num))
456 : return false;
457 : }
458 25730 : *allows_reg = true;
459 25730 : break;
460 : }
461 :
462 33033809 : default:
463 33033809 : if (!ISALPHA (*p))
464 : break;
465 33014250 : enum constraint_num cn = lookup_constraint (p);
466 33014250 : if (reg_class_for_constraint (cn) != NO_REGS
467 33014250 : || insn_extra_address_constraint (cn))
468 : {
469 30224850 : if (alt_has_hard_reg_cstr)
470 : {
471 1 : error (
472 : "hard register constraints and regular register "
473 : "constraints in one alternative are not supported");
474 1 : return false;
475 : }
476 30224849 : alt_has_reg_cstr = true;
477 30224849 : *allows_reg = true;
478 : }
479 : else if (insn_extra_memory_constraint (cn))
480 2781165 : *allows_mem = true;
481 : else
482 : insn_extra_constraint_allows_reg_mem (cn, allows_reg, allows_mem);
483 55986 : if (reg_info && *allows_reg
484 53882 : && VAR_P (reg_info->operand)
485 33047928 : && DECL_HARD_REGISTER (reg_info->operand))
486 : {
487 954 : tree id = DECL_ASSEMBLER_NAME (reg_info->operand);
488 954 : const char *asmspec = IDENTIFIER_POINTER (id) + 1;
489 954 : int regno = decode_reg_name (asmspec);
490 954 : if (regno < 0)
491 : {
492 4 : location_t loc = DECL_SOURCE_LOCATION (reg_info->operand);
493 4 : error_at (loc, "invalid register name for %q+D",
494 : reg_info->operand);
495 4 : return false;
496 : }
497 950 : int overlap_regno = reg_info->test_alt_output (alt, regno);
498 950 : if (overlap_regno >= 0)
499 : {
500 0 : error ("multiple outputs to hard register: %s",
501 : reg_names[overlap_regno]);
502 0 : return false;
503 : }
504 950 : reg_info->set_reg_asm_output (regno);
505 950 : if (early_clobbered)
506 10 : reg_info->set_early_clobbered (alt, operand_num, regno);
507 : }
508 : break;
509 : }
510 :
511 72967296 : for (size_t len = CONSTRAINT_LEN (*p, p); len; len--, p++)
512 36543047 : if (*p == '\0')
513 : break;
514 : }
515 :
516 : return true;
517 : }
518 :
519 : /* Similar, but for input constraints. */
520 :
521 : bool
522 23679132 : parse_input_constraint (const char **constraint_p, int input_num,
523 : int ninputs, int noutputs, int ninout,
524 : const char * const * constraints,
525 : bool *allows_mem, bool *allows_reg,
526 : gimplify_reg_info *reg_info)
527 : {
528 23679132 : const char *constraint = *constraint_p;
529 23679132 : const char *orig_constraint = constraint;
530 23679132 : size_t c_len = strlen (constraint);
531 23679132 : size_t j;
532 23679132 : bool saw_match = false;
533 23679132 : bool at_checked = false;
534 :
535 : /* Assume the constraint doesn't allow the use of either
536 : a register or memory. */
537 23679132 : *allows_mem = false;
538 23679132 : *allows_reg = false;
539 :
540 23679132 : bool alt_has_hard_reg_cstr = false;
541 23679132 : bool alt_has_reg_cstr = false;
542 :
543 : /* Make sure constraint has neither `=', `+', nor '&'. */
544 :
545 23679132 : unsigned int alt = 0;
546 23679132 : unsigned long match = 0;
547 :
548 37179848 : repeat:
549 76074400 : for (j = 0; j < c_len; j += CONSTRAINT_LEN (constraint[j], constraint+j))
550 52395327 : switch (constraint[j])
551 : {
552 13975730 : case '+': case '=': case '&':
553 13975730 : if (constraint == orig_constraint)
554 : {
555 0 : error ("input operand constraint contains %qc", constraint[j]);
556 0 : return false;
557 : }
558 : break;
559 :
560 377958 : case '%':
561 377958 : if (constraint == orig_constraint
562 377958 : && input_num + 1 == ninputs - ninout)
563 : {
564 0 : error ("%<%%%> constraint used with last operand");
565 0 : return false;
566 : }
567 : break;
568 :
569 : case '<': case '>':
570 : case '?': case '!': case '*': case '#':
571 : case '$': case '^':
572 : case 'E': case 'F': case 'G': case 'H':
573 : case 's': case 'i': case 'n':
574 : case 'I': case 'J': case 'K': case 'L': case 'M':
575 : case 'N': case 'O': case 'P': case '-':
576 : break;
577 :
578 166 : case ':':
579 : /* Verify that if : is used, it is just ":" or say ":,:" but not
580 : mixed with other constraints or say ",:,," etc. */
581 166 : if (!at_checked)
582 : {
583 312 : for (size_t k = 0; k < c_len; ++k)
584 336 : if (constraint[k] != ((k & 1) ? ',' : ':') || (c_len & 1) == 0)
585 : {
586 20 : error ("%<:%> constraint mixed with other constraints");
587 20 : return false;
588 : }
589 : at_checked = true;
590 : }
591 : break;
592 :
593 84505 : case ',':
594 84505 : ++alt;
595 84505 : alt_has_hard_reg_cstr = false;
596 84505 : alt_has_reg_cstr = false;
597 84505 : break;
598 :
599 : /* Whether or not a numeric constraint allows a register is
600 : decided by the matching constraint, and so there is no need
601 : to do anything special with them. We must handle them in
602 : the default case, so that we don't unnecessarily force
603 : operands to memory. */
604 13576039 : case '0': case '1': case '2': case '3': case '4':
605 13576039 : case '5': case '6': case '7': case '8': case '9':
606 13576039 : {
607 13576039 : char *end;
608 :
609 13576039 : saw_match = true;
610 :
611 13576039 : match = strtoul (constraint + j, &end, 10);
612 13576039 : if (match >= (unsigned long) noutputs)
613 : {
614 4 : error ("matching constraint references invalid operand number");
615 4 : return false;
616 : }
617 :
618 : /* Try and find the real constraint for this dup. Only do this
619 : if the matching constraint is the only alternative. */
620 13576035 : if (*end == '\0'
621 13512023 : && (j == 0 || (j == 1 && constraint[0] == '%')))
622 : {
623 13500716 : constraint = constraints[match];
624 13500716 : *constraint_p = constraint;
625 13500716 : c_len = strlen (constraint);
626 13500716 : goto repeat;
627 : }
628 : else
629 75319 : j = end - constraint;
630 : /* Anticipate increment at end of loop. */
631 75319 : j--;
632 : }
633 : /* Fall through. */
634 :
635 3690945 : case 'g': case 'X':
636 3690945 : *allows_reg = true;
637 3690945 : *allows_mem = true;
638 3690945 : break;
639 :
640 20950 : case '{':
641 20950 : {
642 20950 : if (!targetm.lra_p ())
643 : {
644 0 : error ("hard register constraints are only supported while using LRA");
645 0 : return false;
646 : }
647 20950 : if (reg_info)
648 : {
649 110 : if (alt_has_reg_cstr)
650 : {
651 1 : error (
652 : "hard register constraints and regular register "
653 : "constraints in one alternative are not supported");
654 1 : return false;
655 : }
656 109 : if (alt_has_hard_reg_cstr)
657 : {
658 2 : error (
659 : "multiple hard register constraints in one "
660 : "alternative are not supported");
661 2 : return false;
662 : }
663 107 : alt_has_hard_reg_cstr = true;
664 107 : int regno = decode_hard_reg_constraint (constraint + j);
665 107 : if (regno < 0)
666 : {
667 3 : error ("invalid input constraint: %s", constraint + j);
668 3 : return false;
669 : }
670 104 : int overlap_regno = reg_info->test_alt_input (alt, regno);
671 104 : if (overlap_regno < 0)
672 99 : overlap_regno = reg_info->test_reg_asm_input (regno);
673 99 : if (overlap_regno >= 0)
674 : {
675 6 : error ("multiple inputs to hard register: %s",
676 : reg_names[overlap_regno]);
677 6 : return false;
678 : }
679 98 : reg_info->set_input (alt, regno);
680 98 : if (reg_info->is_clobbered (regno))
681 : {
682 1 : error ("hard register constraint for input %i conflicts "
683 : "with %<asm%> clobber list", input_num);
684 1 : return false;
685 : }
686 97 : if (constraint == orig_constraint
687 97 : && reg_info->test_early_clobbered_alt (alt, regno))
688 : {
689 3 : error ("invalid hard register usage between earlyclobber "
690 : "operand and input operand");
691 3 : return false;
692 : }
693 94 : if (VAR_P (reg_info->operand)
694 94 : && DECL_HARD_REGISTER (reg_info->operand))
695 : {
696 4 : tree id = DECL_ASSEMBLER_NAME (reg_info->operand);
697 4 : const char *asmspec = IDENTIFIER_POINTER (id) + 1;
698 4 : int regno_op = decode_reg_name (asmspec);
699 4 : if (regno != regno_op)
700 : {
701 2 : error ("constraint and register %<asm%> for input "
702 : "operand %i are unsatisfiable", input_num);
703 2 : return false;
704 : }
705 : }
706 92 : machine_mode mode = TYPE_MODE (TREE_TYPE (reg_info->operand));
707 92 : if (!hardreg_ok_p (regno, mode, input_num))
708 : return false;
709 : }
710 20931 : *allows_reg = true;
711 20931 : break;
712 : }
713 :
714 20087952 : default:
715 20087952 : if (! ISALPHA (constraint[j]))
716 : {
717 5 : error ("invalid punctuation %qc in constraint", constraint[j]);
718 5 : return false;
719 : }
720 20087947 : enum constraint_num cn = lookup_constraint (constraint + j);
721 20087947 : if (reg_class_for_constraint (cn) != NO_REGS
722 20087947 : || insn_extra_address_constraint (cn))
723 : {
724 16465736 : if (alt_has_hard_reg_cstr)
725 : {
726 1 : error (
727 : "hard register constraints and regular register "
728 : "constraints in one alternative are not supported");
729 1 : return false;
730 : }
731 16465735 : alt_has_reg_cstr = true;
732 16465735 : *allows_reg = true;
733 : }
734 : else if (insn_extra_memory_constraint (cn)
735 : || insn_extra_special_memory_constraint (cn)
736 : || insn_extra_relaxed_memory_constraint (cn))
737 3387203 : *allows_mem = true;
738 : else
739 : insn_extra_constraint_allows_reg_mem (cn, allows_reg, allows_mem);
740 35625 : if (reg_info && *allows_reg
741 33167 : && VAR_P (reg_info->operand)
742 20097434 : && DECL_HARD_REGISTER (reg_info->operand))
743 : {
744 878 : tree id = DECL_ASSEMBLER_NAME (reg_info->operand);
745 878 : const char *asmspec = IDENTIFIER_POINTER (id) + 1;
746 878 : int regno = decode_reg_name (asmspec);
747 878 : if (regno < 0)
748 : {
749 5 : location_t loc = DECL_SOURCE_LOCATION (reg_info->operand);
750 5 : error_at (loc, "invalid register name for %q+D",
751 : reg_info->operand);
752 5 : return false;
753 : }
754 873 : int overlap_regno = reg_info->test_alt_input (alt, regno);
755 873 : if (overlap_regno >= 0)
756 : {
757 1 : error ("multiple inputs to hard register: %s",
758 : reg_names[overlap_regno]);
759 1 : return false;
760 : }
761 872 : reg_info->set_reg_asm_input (regno);
762 872 : if ((constraint == orig_constraint
763 200 : && reg_info->test_early_clobbered_alt (alt, regno))
764 1070 : || (constraint != orig_constraint
765 672 : && reg_info->is_early_clobbered_in_any_output_unequal
766 672 : (match, regno)))
767 : {
768 4 : error ("invalid hard register usage between earlyclobber "
769 : "operand and input operand");
770 4 : return false;
771 : }
772 : }
773 : break;
774 : }
775 :
776 23679073 : if (saw_match && !*allows_reg)
777 4 : warning (0, "matching constraint does not allow a register");
778 :
779 : return true;
780 : }
781 :
782 : /* Return DECL iff there's an overlap between *REGS and DECL, where DECL
783 : can be an asm-declared register. Called via walk_tree. */
784 :
785 : static tree
786 161418 : decl_overlaps_hard_reg_set_p (tree *declp, int *walk_subtrees ATTRIBUTE_UNUSED,
787 : void *data)
788 : {
789 161418 : tree decl = *declp;
790 161418 : const HARD_REG_SET *const regs = (const HARD_REG_SET *) data;
791 :
792 161418 : if (VAR_P (decl))
793 : {
794 21269 : if (DECL_HARD_REGISTER (decl)
795 1865 : && REG_P (DECL_RTL (decl))
796 23134 : && REGNO (DECL_RTL (decl)) < FIRST_PSEUDO_REGISTER)
797 : {
798 1865 : rtx reg = DECL_RTL (decl);
799 :
800 1865 : if (overlaps_hard_reg_set_p (*regs, GET_MODE (reg), REGNO (reg)))
801 21 : return decl;
802 : }
803 161418 : walk_subtrees = 0;
804 : }
805 : else if (TYPE_P (decl) || TREE_CODE (decl) == PARM_DECL)
806 : walk_subtrees = 0;
807 : return NULL_TREE;
808 : }
809 :
810 : /* If there is an overlap between *REGS and DECL, return the first overlap
811 : found. */
812 : tree
813 138513 : tree_overlaps_hard_reg_set (tree decl, HARD_REG_SET *regs)
814 : {
815 138513 : return walk_tree (&decl, decl_overlaps_hard_reg_set_p, regs, NULL);
816 : }
817 :
818 :
819 : /* A subroutine of expand_asm_operands. Check that all operand names
820 : are unique. Return true if so. We rely on the fact that these names
821 : are identifiers, and so have been canonicalized by get_identifier,
822 : so all we need are pointer comparisons. */
823 :
824 : static bool
825 208910 : check_unique_operand_names (tree outputs, tree inputs, tree labels)
826 : {
827 208910 : tree i, j, i_name = NULL_TREE;
828 :
829 581175 : for (i = outputs; i ; i = TREE_CHAIN (i))
830 : {
831 372265 : i_name = TREE_PURPOSE (TREE_PURPOSE (i));
832 372265 : if (! i_name)
833 371819 : continue;
834 :
835 2365 : for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
836 1919 : if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
837 0 : goto failure;
838 : }
839 :
840 594740 : for (i = inputs; i ; i = TREE_CHAIN (i))
841 : {
842 385835 : i_name = TREE_PURPOSE (TREE_PURPOSE (i));
843 385835 : if (! i_name)
844 385205 : continue;
845 :
846 1178 : for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
847 548 : if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
848 0 : goto failure;
849 1076 : for (j = outputs; j ; j = TREE_CHAIN (j))
850 451 : if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
851 5 : goto failure;
852 : }
853 :
854 209882 : for (i = labels; i ; i = TREE_CHAIN (i))
855 : {
856 985 : i_name = TREE_PURPOSE (i);
857 985 : if (! i_name)
858 0 : continue;
859 :
860 2355 : for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
861 1374 : if (simple_cst_equal (i_name, TREE_PURPOSE (j)))
862 4 : goto failure;
863 1342 : for (j = inputs; j ; j = TREE_CHAIN (j))
864 365 : if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
865 4 : goto failure;
866 : }
867 :
868 : return true;
869 :
870 13 : failure:
871 13 : error ("duplicate %<asm%> operand name %qs", TREE_STRING_POINTER (i_name));
872 13 : return false;
873 : }
874 :
875 : /* Find the operand named by [START, END), including in/out operands and
876 : labels in the numbering. */
877 :
878 : static int
879 1381 : asm_operand_name_number (const char *start, const char *end,
880 : tree outputs, tree inputs, tree labels)
881 : {
882 1381 : string_slice operand_name (start, end);
883 1381 : int op = 0;
884 1381 : int op_inout = 0;
885 :
886 : /* Outputs come first. Count read/write outputs as we go: each one
887 : also creates an implicit input, which affects the label numbers. */
888 2914 : for (tree t = outputs; t; t = TREE_CHAIN (t), op++)
889 : {
890 1799 : tree name = TREE_PURPOSE (TREE_PURPOSE (t));
891 3109 : if (name && operand_name == TREE_STRING_POINTER (name))
892 266 : return op;
893 1533 : tree constraint = TREE_VALUE (TREE_PURPOSE (t));
894 3066 : if (constraint && strchr (TREE_STRING_POINTER (constraint), '+'))
895 68 : op_inout++;
896 : }
897 : /* Explicit inputs follow the outputs, before the implicit inputs. */
898 2304 : for (tree t = inputs; t; t = TREE_CHAIN (t), op++)
899 : {
900 2190 : tree name = TREE_PURPOSE (TREE_PURPOSE (t));
901 3834 : if (name && operand_name == TREE_STRING_POINTER (name))
902 1001 : return op;
903 : }
904 : /* Labels follow all inputs, including those from read/write outputs.
905 : Their names are stored directly in TREE_PURPOSE. */
906 114 : op += op_inout;
907 123 : for (tree t = labels; t; t = TREE_CHAIN (t), op++)
908 : {
909 120 : tree name = TREE_PURPOSE (t);
910 240 : if (name && operand_name == TREE_STRING_POINTER (name))
911 111 : return op;
912 : }
913 :
914 : /* Only the diagnostic needs a null-terminated copy of the name. */
915 3 : char *name = xstrndup (start, end - start);
916 3 : error ("undefined named operand %qs",
917 : identifier_to_locale (name));
918 3 : free (name);
919 3 : return 0;
920 : }
921 :
922 : /* Append OPNUM in decimal form to OB. */
923 :
924 : static void
925 1381 : asm_obstack_grow_opnum (struct obstack *ob, int opnum)
926 : {
927 1381 : char number[HOST_BITS_PER_INT * 3 / 10 + 3];
928 1381 : int n = sprintf (number, "%d", opnum);
929 1381 : obstack_grow (ob, number, n);
930 1381 : }
931 :
932 : /* Resolve names in an asm template and its input constraints. This is
933 : normally done while the front end builds the ASM_EXPR. */
934 :
935 : tree
936 208910 : resolve_asm_operand_names (tree string, tree outputs, tree inputs, tree labels)
937 : {
938 208910 : check_unique_operand_names (outputs, inputs, labels);
939 :
940 : /* Substitute names in input constraints, removing their brackets. */
941 803655 : for (tree t = inputs; t; t = TREE_CHAIN (t))
942 : {
943 385835 : const char *input = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
944 385835 : if (!strchr (input, '['))
945 385652 : continue;
946 :
947 183 : struct obstack ob;
948 183 : gcc_obstack_init (&ob);
949 183 : const char *p = input;
950 366 : while (const char *open = strchr (p, '['))
951 : {
952 183 : obstack_grow (&ob, p, open - p);
953 183 : const char *close = strchr (open + 1, ']');
954 183 : if (!close)
955 : {
956 0 : error ("missing close brace for named operand");
957 0 : p = open;
958 0 : break;
959 : }
960 183 : asm_obstack_grow_opnum (&ob,
961 : asm_operand_name_number (open + 1, close,
962 : outputs, inputs,
963 : nullptr));
964 183 : p = close + 1;
965 183 : }
966 183 : obstack_grow (&ob, p, strlen (p));
967 183 : TREE_VALUE (TREE_PURPOSE (t))
968 183 : = build_string (obstack_object_size (&ob), (char *) obstack_base (&ob));
969 183 : obstack_free (&ob, nullptr);
970 : }
971 :
972 208910 : const char *input = TREE_STRING_POINTER (string);
973 208910 : if (!strchr (input, '%'))
974 : return string;
975 :
976 5748 : struct obstack ob;
977 5748 : gcc_obstack_init (&ob);
978 5748 : const char *p = input;
979 33153 : while (const char *percent = strchr (p, '%'))
980 : {
981 27405 : obstack_grow (&ob, p, percent - p);
982 27405 : p = percent;
983 27405 : const char *open = p + 1;
984 27405 : if (ISALPHA (*open))
985 : {
986 2857 : ++open;
987 2857 : if (ISALPHA (*open))
988 1429 : ++open;
989 : }
990 27405 : if (*open != '[')
991 : {
992 26191 : int len = 1 + (p[1] == '%');
993 26191 : obstack_grow (&ob, p, len);
994 26191 : p += len;
995 26191 : continue;
996 26191 : }
997 :
998 1214 : bool remove_brackets = open != p + 1;
999 1214 : const char *name_start = open + 1;
1000 1214 : const char *close = strchr (name_start, ']');
1001 1214 : if (!close)
1002 : {
1003 0 : error ("missing close brace for named operand");
1004 0 : break;
1005 : }
1006 1214 : if (!remove_brackets)
1007 : {
1008 227 : if (const char *colon
1009 227 : = (const char *) memchr (name_start, ':', close - name_start))
1010 : {
1011 : /* Validate modifiers even in asms that are optimized away. */
1012 24 : string_slice name (name_start, colon);
1013 24 : if (targetm.asm_out.print_operand_modifier (name, true)
1014 : < PRINT_OPERAND_MODIFIER_BASE)
1015 0 : error ("invalid operand modifier %qB", &name);
1016 24 : name_start = colon + 1;
1017 : }
1018 : /* Symbolic operand names cannot start with a digit. Leave
1019 : numbered operands and lists for final output to parse. */
1020 227 : if (ISDIGIT (*name_start))
1021 : {
1022 16 : obstack_grow (&ob, p, close + 1 - p);
1023 16 : p = close + 1;
1024 16 : continue;
1025 16 : }
1026 : }
1027 : /* Preserve brackets in %[name] and %[modifier:name], so following
1028 : literal digits cannot become part of the operand number. */
1029 1198 : obstack_grow (&ob, p, name_start - p - remove_brackets);
1030 1198 : asm_obstack_grow_opnum (&ob,
1031 : asm_operand_name_number (name_start, close,
1032 : outputs, inputs,
1033 : labels));
1034 1198 : p = close + remove_brackets;
1035 : }
1036 5748 : obstack_grow (&ob, p, strlen (p));
1037 5748 : string = build_string (obstack_object_size (&ob), (char *) obstack_base (&ob));
1038 5748 : obstack_free (&ob, nullptr);
1039 5748 : return string;
1040 : }
1041 :
1042 : #if CHECKING_P
1043 :
1044 : namespace selftest {
1045 :
1046 : static tree
1047 52 : build_asm_operand (const char *name, const char *constraint)
1048 : {
1049 52 : tree name_tree = name ? build_string (strlen (name), name) : NULL_TREE;
1050 52 : tree constraint_tree = build_string (strlen (constraint), constraint);
1051 52 : return tree_cons (tree_cons (name_tree, constraint_tree, NULL_TREE),
1052 52 : integer_zero_node, NULL_TREE);
1053 : }
1054 :
1055 : static tree
1056 4 : build_asm_label (const char *name)
1057 : {
1058 4 : return tree_cons (build_string (strlen (name), name), integer_zero_node,
1059 4 : NULL_TREE);
1060 : }
1061 :
1062 : /* Keep these tests independent of the target's modifier namespace. */
1063 :
1064 : static int
1065 24 : test_asm_operand_modifier (string_slice name, bool user_p)
1066 : {
1067 24 : ASSERT_TRUE (user_p);
1068 24 : return name == "w" || name == "wide" ? PRINT_OPERAND_MODIFIER_BASE : 0;
1069 : }
1070 :
1071 : static void
1072 4 : test_resolve_asm_operand_names ()
1073 : {
1074 4 : tree outputs = build_asm_operand ("out", "=r");
1075 4 : tree inputs = build_asm_operand ("in", "[out]");
1076 4 : tree labels = build_asm_label ("lab");
1077 4 : auto saved_hook = targetm.asm_out.print_operand_modifier;
1078 4 : targetm.asm_out.print_operand_modifier = test_asm_operand_modifier;
1079 :
1080 4 : const char *tests[][2] = {
1081 : { "%[out] %w[out] %cc[in] %l[lab]", "%[0] %w0 %cc1 %l2" },
1082 : { "%[out]0 %[in]1", "%[0]0 %[1]1" },
1083 : { "%[10] %[0]", "%[10] %[0]" },
1084 : { "%[w:out] %[w:in]", "%[w:0] %[w:1]" },
1085 : { "%[wide:out] %[wide:0,1]", "%[wide:0] %[wide:0,1]" },
1086 : { "%%[out] %%[w:in] %[w:0,1]", "%%[out] %%[w:in] %[w:0,1]" }
1087 : };
1088 28 : for (const auto &test : tests)
1089 : {
1090 24 : tree string = build_string (strlen (test[0]), test[0]);
1091 24 : tree result = resolve_asm_operand_names (string, outputs, inputs, labels);
1092 24 : ASSERT_STREQ (test[1], TREE_STRING_POINTER (result));
1093 : }
1094 4 : ASSERT_STREQ ("0", TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (inputs))));
1095 :
1096 : /* Unlike the old %[name] syntax, %[modifier:name] can grow when a
1097 : short name is replaced by a two-digit operand number. */
1098 4 : tree many_outputs = build_asm_operand ("x", "=r");
1099 48 : for (int i = 0; i < 10; ++i)
1100 40 : many_outputs = chainon (build_asm_operand (nullptr, "=r"), many_outputs);
1101 4 : const char *templ = "%[w:x]";
1102 4 : tree string = build_string (strlen (templ), templ);
1103 4 : tree result = resolve_asm_operand_names (string, many_outputs, nullptr,
1104 : nullptr);
1105 4 : ASSERT_STREQ ("%[w:10]", TREE_STRING_POINTER (result));
1106 :
1107 4 : TREE_VALUE (TREE_PURPOSE (outputs)) = build_string (2, "+r");
1108 4 : templ = "%l[lab]";
1109 4 : string = build_string (strlen (templ), templ);
1110 4 : result = resolve_asm_operand_names (string, outputs, inputs, labels);
1111 4 : ASSERT_STREQ ("%l3", TREE_STRING_POINTER (result));
1112 4 : targetm.asm_out.print_operand_modifier = saved_hook;
1113 4 : }
1114 :
1115 : void
1116 4 : stmt_cc_tests ()
1117 : {
1118 4 : test_resolve_asm_operand_names ();
1119 4 : }
1120 :
1121 : } // namespace selftest
1122 :
1123 : #endif /* CHECKING_P */
1124 :
1125 :
1126 : /* Generate RTL to return directly from the current function.
1127 : (That is, we bypass any return value.) */
1128 :
1129 : void
1130 379 : expand_naked_return (void)
1131 : {
1132 379 : rtx_code_label *end_label;
1133 :
1134 379 : clear_pending_stack_adjust ();
1135 379 : do_pending_stack_adjust ();
1136 :
1137 379 : end_label = naked_return_label;
1138 379 : if (end_label == 0)
1139 379 : end_label = naked_return_label = gen_label_rtx ();
1140 :
1141 379 : emit_jump (end_label);
1142 379 : }
1143 :
1144 : /* Generate code to jump to LABEL if OP0 and OP1 are equal in mode MODE. PROB
1145 : is the probability of jumping to LABEL. */
1146 : static void
1147 0 : do_jump_if_equal (machine_mode mode, rtx op0, rtx op1, rtx_code_label *label,
1148 : int unsignedp, profile_probability prob)
1149 : {
1150 0 : do_compare_rtx_and_jump (op0, op1, EQ, unsignedp, mode,
1151 : NULL_RTX, NULL, label, prob);
1152 0 : }
1153 :
1154 : /* Return the sum of probabilities of outgoing edges of basic block BB. */
1155 :
1156 : static profile_probability
1157 7659 : get_outgoing_edge_probs (basic_block bb)
1158 : {
1159 7659 : edge e;
1160 7659 : edge_iterator ei;
1161 7659 : profile_probability prob_sum = profile_probability::never ();
1162 7659 : if (!bb)
1163 0 : return profile_probability::never ();
1164 92217 : FOR_EACH_EDGE (e, ei, bb->succs)
1165 84558 : prob_sum += e->probability;
1166 7659 : return prob_sum;
1167 : }
1168 :
1169 : /* Computes the conditional probability of jumping to a target if the branch
1170 : instruction is executed.
1171 : TARGET_PROB is the estimated probability of jumping to a target relative
1172 : to some basic block BB.
1173 : BASE_PROB is the probability of reaching the branch instruction relative
1174 : to the same basic block BB. */
1175 :
1176 : static inline profile_probability
1177 13749 : conditional_probability (profile_probability target_prob,
1178 : profile_probability base_prob)
1179 : {
1180 13749 : return target_prob / base_prob;
1181 : }
1182 :
1183 : /* Generate a dispatch tabler, switching on INDEX_EXPR and jumping to
1184 : one of the labels in CASE_LIST or to the DEFAULT_LABEL.
1185 : MINVAL, MAXVAL, and RANGE are the extrema and range of the case
1186 : labels in CASE_LIST. STMT_BB is the basic block containing the statement.
1187 :
1188 : First, a jump insn is emitted. First we try "casesi". If that
1189 : fails, try "tablejump". A target *must* have one of them (or both).
1190 :
1191 : Then, a table with the target labels is emitted.
1192 :
1193 : The process is unaware of the CFG. The caller has to fix up
1194 : the CFG itself. This is done in cfgexpand.cc. */
1195 :
1196 : static void
1197 7659 : emit_case_dispatch_table (tree index_expr, tree index_type,
1198 : auto_vec<simple_case_node> &case_list,
1199 : rtx_code_label *default_label,
1200 : edge default_edge, tree minval, tree maxval,
1201 : tree range, basic_block stmt_bb)
1202 : {
1203 7659 : int i, ncases;
1204 7659 : auto_vec<rtx> labelvec;
1205 7659 : rtx_insn *fallback_label = label_rtx (case_list[0].m_code_label);
1206 7659 : rtx_code_label *table_label = gen_label_rtx ();
1207 7659 : bool has_gaps = false;
1208 7659 : profile_probability default_prob = default_edge ? default_edge->probability
1209 7659 : : profile_probability::never ();
1210 7659 : profile_probability base = get_outgoing_edge_probs (stmt_bb);
1211 7659 : bool try_with_tablejump = false;
1212 :
1213 7659 : profile_probability new_default_prob = conditional_probability (default_prob,
1214 : base);
1215 :
1216 7659 : if (! try_casesi (index_type, index_expr, minval, range,
1217 : table_label, default_label, fallback_label,
1218 : new_default_prob))
1219 : {
1220 : /* Index jumptables from zero for suitable values of minval to avoid
1221 : a subtraction. For the rationale see:
1222 : "http://gcc.gnu.org/ml/gcc-patches/2001-10/msg01234.html". */
1223 7659 : if (optimize_insn_for_speed_p ()
1224 7376 : && compare_tree_int (minval, 0) > 0
1225 12813 : && compare_tree_int (minval, 3) < 0)
1226 : {
1227 2215 : minval = build_int_cst (index_type, 0);
1228 2215 : range = maxval;
1229 2215 : has_gaps = true;
1230 : }
1231 : try_with_tablejump = true;
1232 : }
1233 :
1234 : /* Get table of labels to jump to, in order of case index. */
1235 :
1236 7659 : ncases = tree_to_shwi (range) + 1;
1237 7659 : labelvec.safe_grow_cleared (ncases);
1238 :
1239 98439 : for (unsigned j = 0; j < case_list.length (); j++)
1240 : {
1241 83121 : simple_case_node *n = &case_list[j];
1242 : /* Compute the low and high bounds relative to the minimum
1243 : value since that should fit in a HOST_WIDE_INT while the
1244 : actual values may not. */
1245 83121 : HOST_WIDE_INT i_low
1246 83121 : = tree_to_uhwi (fold_build2 (MINUS_EXPR, index_type,
1247 83121 : n->m_low, minval));
1248 83121 : HOST_WIDE_INT i_high
1249 83121 : = tree_to_uhwi (fold_build2 (MINUS_EXPR, index_type,
1250 83121 : n->m_high, minval));
1251 83121 : HOST_WIDE_INT i;
1252 :
1253 174630 : for (i = i_low; i <= i_high; i ++)
1254 274527 : labelvec[i]
1255 103923 : = gen_rtx_LABEL_REF (Pmode, label_rtx (n->m_code_label));
1256 : }
1257 :
1258 : /* The dispatch table may contain gaps, including at the beginning of
1259 : the table if we tried to avoid the minval subtraction. We fill the
1260 : dispatch table slots associated with the gaps with the default case label.
1261 : However, in the event the default case is unreachable, we then use
1262 : any label from one of the case statements. */
1263 7659 : rtx_insn *gap_label = (default_label) ? default_label : fallback_label;
1264 :
1265 179717 : for (i = 0; i < ncases; i++)
1266 172058 : if (labelvec[i] == 0)
1267 : {
1268 80549 : has_gaps = true;
1269 93725 : labelvec[i] = gen_rtx_LABEL_REF (Pmode, gap_label);
1270 : }
1271 :
1272 7659 : if (has_gaps && default_label)
1273 : {
1274 : /* There is at least one entry in the jump table that jumps
1275 : to default label. The default label can either be reached
1276 : through the indirect jump or the direct conditional jump
1277 : before that. Split the probability of reaching the
1278 : default label among these two jumps. */
1279 6090 : new_default_prob = conditional_probability (default_prob / 2, base);
1280 6090 : default_prob /= 2;
1281 6090 : base -= default_prob;
1282 : }
1283 : else
1284 : {
1285 1569 : base -= default_prob;
1286 1569 : default_prob = profile_probability::never ();
1287 : }
1288 :
1289 7659 : if (default_edge)
1290 7583 : default_edge->probability = default_prob;
1291 :
1292 : /* We have altered the probability of the default edge. So the probabilities
1293 : of all other edges need to be adjusted so that it sums up to
1294 : REG_BR_PROB_BASE. */
1295 7659 : if (base > profile_probability::never ())
1296 : {
1297 7655 : edge e;
1298 7655 : edge_iterator ei;
1299 92081 : FOR_EACH_EDGE (e, ei, stmt_bb->succs)
1300 84426 : e->probability /= base;
1301 : }
1302 :
1303 7659 : if (try_with_tablejump)
1304 : {
1305 7659 : bool ok = try_tablejump (index_type, index_expr, minval, range,
1306 : table_label, default_label, new_default_prob);
1307 7659 : gcc_assert (ok);
1308 : }
1309 : /* Output the table. */
1310 7659 : emit_label (table_label);
1311 :
1312 7659 : if (CASE_VECTOR_PC_RELATIVE
1313 7659 : || (flag_pic && targetm.asm_out.generate_pic_addr_diff_vec ()))
1314 4198 : emit_jump_table_data (gen_rtx_ADDR_DIFF_VEC (CASE_VECTOR_MODE,
1315 : gen_rtx_LABEL_REF (Pmode,
1316 : table_label),
1317 : gen_rtvec_v (ncases, labelvec.address ()),
1318 : const0_rtx, const0_rtx));
1319 : else
1320 13403 : emit_jump_table_data (gen_rtx_ADDR_VEC (CASE_VECTOR_MODE,
1321 : gen_rtvec_v (ncases, labelvec.address ())));
1322 :
1323 : /* Record no drop-through after the table. */
1324 7659 : emit_barrier ();
1325 7659 : }
1326 :
1327 : /* Terminate a case Ada or switch (C) statement
1328 : in which ORIG_INDEX is the expression to be tested.
1329 : If ORIG_TYPE is not NULL, it is the original ORIG_INDEX
1330 : type as given in the source before any compiler conversions.
1331 : Generate the code to test it and jump to the right place. */
1332 :
1333 : void
1334 7659 : expand_case (gswitch *stmt)
1335 : {
1336 7659 : tree minval = NULL_TREE, maxval = NULL_TREE, range = NULL_TREE;
1337 7659 : rtx_code_label *default_label;
1338 7659 : unsigned int count;
1339 7659 : int i;
1340 7659 : int ncases = gimple_switch_num_labels (stmt);
1341 7659 : tree index_expr = gimple_switch_index (stmt);
1342 7659 : tree index_type = TREE_TYPE (index_expr);
1343 7659 : tree elt;
1344 7659 : basic_block bb = gimple_bb (stmt);
1345 7659 : gimple *def_stmt;
1346 :
1347 7659 : auto_vec<simple_case_node> case_list;
1348 :
1349 : /* An ERROR_MARK occurs for various reasons including invalid data type.
1350 : ??? Can this still happen, with GIMPLE and all? */
1351 7659 : if (index_type == error_mark_node)
1352 0 : return;
1353 :
1354 : /* cleanup_tree_cfg removes all SWITCH_EXPR with their index
1355 : expressions being INTEGER_CST. */
1356 7659 : gcc_assert (TREE_CODE (index_expr) != INTEGER_CST);
1357 :
1358 : /* Optimization of switch statements with only one label has already
1359 : occurred, so we should never see them at this point. */
1360 7659 : gcc_assert (ncases > 1);
1361 :
1362 7659 : do_pending_stack_adjust ();
1363 :
1364 : /* Find the default case target label. */
1365 7659 : tree default_lab = CASE_LABEL (gimple_switch_default_label (stmt));
1366 7659 : default_label = jump_target_rtx (default_lab);
1367 7659 : basic_block default_bb = label_to_block (cfun, default_lab);
1368 7659 : edge default_edge = find_edge (bb, default_bb);
1369 :
1370 : /* Get upper and lower bounds of case values. */
1371 7659 : elt = gimple_switch_label (stmt, 1);
1372 7659 : minval = fold_convert (index_type, CASE_LOW (elt));
1373 7659 : elt = gimple_switch_label (stmt, ncases - 1);
1374 7659 : if (CASE_HIGH (elt))
1375 491 : maxval = fold_convert (index_type, CASE_HIGH (elt));
1376 : else
1377 7168 : maxval = fold_convert (index_type, CASE_LOW (elt));
1378 :
1379 : /* Try to narrow the index type if it's larger than a word.
1380 : That is mainly for -O0 where an equivalent optimization
1381 : done by forward propagation is not run and is aimed at
1382 : avoiding a call to a comparison routine of libgcc. */
1383 7659 : if (TYPE_PRECISION (index_type) > BITS_PER_WORD
1384 6 : && TREE_CODE (index_expr) == SSA_NAME
1385 6 : && (def_stmt = SSA_NAME_DEF_STMT (index_expr))
1386 6 : && is_gimple_assign (def_stmt)
1387 7662 : && gimple_assign_rhs_code (def_stmt) == NOP_EXPR)
1388 : {
1389 0 : tree inner_index_expr = gimple_assign_rhs1 (def_stmt);
1390 0 : tree inner_index_type = TREE_TYPE (inner_index_expr);
1391 :
1392 0 : if (INTEGRAL_TYPE_P (inner_index_type)
1393 0 : && TYPE_PRECISION (inner_index_type) <= BITS_PER_WORD
1394 0 : && int_fits_type_p (minval, inner_index_type)
1395 0 : && int_fits_type_p (maxval, inner_index_type))
1396 : {
1397 0 : index_expr = inner_index_expr;
1398 0 : index_type = inner_index_type;
1399 0 : minval = fold_convert (index_type, minval);
1400 0 : maxval = fold_convert (index_type, maxval);
1401 : }
1402 : }
1403 :
1404 : /* Compute span of values. */
1405 7659 : range = fold_build2 (MINUS_EXPR, index_type, maxval, minval);
1406 :
1407 : /* Listify the labels queue and gather some numbers to decide
1408 : how to expand this switch(). */
1409 7659 : count = 0;
1410 :
1411 98439 : for (i = ncases - 1; i >= 1; --i)
1412 : {
1413 83121 : elt = gimple_switch_label (stmt, i);
1414 83121 : tree low = CASE_LOW (elt);
1415 83121 : gcc_assert (low);
1416 83121 : tree high = CASE_HIGH (elt);
1417 83121 : gcc_assert (! high || tree_int_cst_lt (low, high));
1418 83121 : tree lab = CASE_LABEL (elt);
1419 :
1420 : /* Count the elements.
1421 : A range counts double, since it requires two compares. */
1422 83121 : count++;
1423 83121 : if (high)
1424 3017 : count++;
1425 :
1426 : /* The bounds on the case range, LOW and HIGH, have to be converted
1427 : to case's index type TYPE. Note that the original type of the
1428 : case index in the source code is usually "lost" during
1429 : gimplification due to type promotion, but the case labels retain the
1430 : original type. Make sure to drop overflow flags. */
1431 83121 : low = fold_convert (index_type, low);
1432 83121 : if (TREE_OVERFLOW (low))
1433 0 : low = wide_int_to_tree (index_type, wi::to_wide (low));
1434 :
1435 : /* The canonical from of a case label in GIMPLE is that a simple case
1436 : has an empty CASE_HIGH. For the casesi and tablejump expanders,
1437 : the back ends want simple cases to have high == low. */
1438 83121 : if (! high)
1439 80104 : high = low;
1440 83121 : high = fold_convert (index_type, high);
1441 83121 : if (TREE_OVERFLOW (high))
1442 0 : high = wide_int_to_tree (index_type, wi::to_wide (high));
1443 :
1444 83121 : case_list.safe_push (simple_case_node (low, high, lab));
1445 : }
1446 :
1447 : /* cleanup_tree_cfg removes all SWITCH_EXPR with a single
1448 : destination, such as one with a default case only.
1449 : It also removes cases that are out of range for the switch
1450 : type, so we should never get a zero here. */
1451 7659 : gcc_assert (count > 0);
1452 :
1453 7659 : rtx_insn *before_case = get_last_insn ();
1454 :
1455 : /* If the default case is unreachable, then set default_label to NULL
1456 : so that we omit the range check when generating the dispatch table.
1457 : We also remove the edge to the unreachable default case. The block
1458 : itself will be automatically removed later. */
1459 7659 : if (EDGE_COUNT (default_edge->dest->succs) == 0
1460 8796 : && gimple_seq_unreachable_p (bb_seq (default_edge->dest)))
1461 : {
1462 76 : default_label = NULL;
1463 76 : expand_remove_edge (default_edge);
1464 76 : default_edge = NULL;
1465 : }
1466 :
1467 7659 : emit_case_dispatch_table (index_expr, index_type,
1468 : case_list, default_label, default_edge,
1469 : minval, maxval, range, bb);
1470 :
1471 7659 : reorder_insns (NEXT_INSN (before_case), get_last_insn (), before_case);
1472 :
1473 7659 : free_temp_slots ();
1474 7659 : }
1475 :
1476 : /* Expand the dispatch to a short decrement chain if there are few cases
1477 : to dispatch to. Likewise if neither casesi nor tablejump is available,
1478 : or if flag_jump_tables is set. Otherwise, expand as a casesi or a
1479 : tablejump. The index mode is always the mode of integer_type_node.
1480 : Trap if no case matches the index.
1481 :
1482 : DISPATCH_INDEX is the index expression to switch on. It should be a
1483 : memory or register operand.
1484 :
1485 : DISPATCH_TABLE is a set of case labels. The set should be sorted in
1486 : ascending order, be contiguous, starting with value 0, and contain only
1487 : single-valued case labels. */
1488 :
1489 : void
1490 0 : expand_sjlj_dispatch_table (rtx dispatch_index,
1491 : vec<tree> dispatch_table)
1492 : {
1493 0 : tree index_type = integer_type_node;
1494 0 : machine_mode index_mode = TYPE_MODE (index_type);
1495 :
1496 0 : int ncases = dispatch_table.length ();
1497 :
1498 0 : do_pending_stack_adjust ();
1499 0 : rtx_insn *before_case = get_last_insn ();
1500 :
1501 : /* Expand as a decrement-chain if there are 5 or fewer dispatch
1502 : labels. This covers more than 98% of the cases in libjava,
1503 : and seems to be a reasonable compromise between the "old way"
1504 : of expanding as a decision tree or dispatch table vs. the "new
1505 : way" with decrement chain or dispatch table. */
1506 0 : if (dispatch_table.length () <= 5
1507 0 : || (!targetm.have_casesi () && !targetm.have_tablejump ())
1508 0 : || !flag_jump_tables)
1509 : {
1510 : /* Expand the dispatch as a decrement chain:
1511 :
1512 : "switch(index) {case 0: do_0; case 1: do_1; ...; case N: do_N;}"
1513 :
1514 : ==>
1515 :
1516 : if (index == 0) do_0; else index--;
1517 : if (index == 0) do_1; else index--;
1518 : ...
1519 : if (index == 0) do_N; else index--;
1520 :
1521 : This is more efficient than a dispatch table on most machines.
1522 : The last "index--" is redundant but the code is trivially dead
1523 : and will be cleaned up by later passes. */
1524 0 : rtx index = copy_to_mode_reg (index_mode, dispatch_index);
1525 0 : rtx zero = CONST0_RTX (index_mode);
1526 0 : for (int i = 0; i < ncases; i++)
1527 : {
1528 0 : tree elt = dispatch_table[i];
1529 0 : rtx_code_label *lab = jump_target_rtx (CASE_LABEL (elt));
1530 0 : do_jump_if_equal (index_mode, index, zero, lab, 0,
1531 : profile_probability::uninitialized ());
1532 0 : force_expand_binop (index_mode, sub_optab,
1533 : index, CONST1_RTX (index_mode),
1534 : index, 0, OPTAB_DIRECT);
1535 : }
1536 : }
1537 : else
1538 : {
1539 : /* Similar to expand_case, but much simpler. */
1540 0 : auto_vec<simple_case_node> case_list;
1541 0 : tree index_expr = make_tree (index_type, dispatch_index);
1542 0 : tree minval = build_int_cst (index_type, 0);
1543 0 : tree maxval = CASE_LOW (dispatch_table.last ());
1544 0 : tree range = maxval;
1545 0 : rtx_code_label *default_label = gen_label_rtx ();
1546 :
1547 0 : for (int i = ncases - 1; i >= 0; --i)
1548 : {
1549 0 : tree elt = dispatch_table[i];
1550 0 : tree high = CASE_HIGH (elt);
1551 0 : if (high == NULL_TREE)
1552 0 : high = CASE_LOW (elt);
1553 0 : case_list.safe_push (simple_case_node (CASE_LOW (elt), high,
1554 0 : CASE_LABEL (elt)));
1555 : }
1556 :
1557 0 : emit_case_dispatch_table (index_expr, index_type,
1558 : case_list, default_label, NULL,
1559 : minval, maxval, range,
1560 0 : BLOCK_FOR_INSN (before_case));
1561 0 : emit_label (default_label);
1562 0 : }
1563 :
1564 : /* Dispatching something not handled? Trap! */
1565 0 : expand_builtin_trap ();
1566 :
1567 0 : reorder_insns (NEXT_INSN (before_case), get_last_insn (), before_case);
1568 :
1569 0 : free_temp_slots ();
1570 0 : }
1571 :
1572 :
|