Line data Source code
1 : /* Definitions for computing resource usage of specific insns.
2 : Copyright (C) 1999-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 : #include "config.h"
21 : #include "system.h"
22 : #include "coretypes.h"
23 : #include "backend.h"
24 : #include "target.h"
25 : #include "rtl.h"
26 : #include "df.h"
27 : #include "memmodel.h"
28 : #include "tm_p.h"
29 : #include "regs.h"
30 : #include "emit-rtl.h"
31 : #include "resource.h"
32 : #include "insn-attr.h"
33 : #include "function-abi.h"
34 :
35 : /* This structure is used to record liveness information at the targets or
36 : fallthrough insns of branches. We will most likely need the information
37 : at targets again, so save them in a hash table rather than recomputing them
38 : each time. */
39 :
40 : struct target_info
41 : {
42 : int uid; /* INSN_UID of target. */
43 : struct target_info *next; /* Next info for same hash bucket. */
44 : HARD_REG_SET live_regs; /* Registers live at target. */
45 : int block; /* Basic block number containing target. */
46 : int bb_tick; /* Generation count of basic block info. */
47 : };
48 :
49 : #define TARGET_HASH_PRIME 257
50 :
51 : /* Indicates what resources are required at the beginning of the epilogue. */
52 : static struct resources start_of_epilogue_needs;
53 :
54 : /* Indicates what resources are required at function end. */
55 : static struct resources end_of_function_needs;
56 :
57 : /* Define the hash table itself. */
58 : static struct target_info **target_hash_table = NULL;
59 :
60 : /* For each basic block, we maintain a generation number of its basic
61 : block info, which is updated each time we move an insn from the
62 : target of a jump. This is the generation number indexed by block
63 : number. */
64 :
65 : static int *bb_ticks;
66 :
67 : /* Marks registers possibly live at the current place being scanned by
68 : mark_target_live_regs. Also used by update_live_status. */
69 :
70 : static HARD_REG_SET current_live_regs;
71 :
72 : /* Marks registers for which we have seen a REG_DEAD note but no assignment.
73 : Also only used by the next two functions. */
74 :
75 : static HARD_REG_SET pending_dead_regs;
76 :
77 : static void update_live_status (rtx, const_rtx, void *);
78 : static int find_basic_block (rtx_insn *, int);
79 : static rtx_insn *next_insn_no_annul (rtx_insn *);
80 :
81 : /* Utility function called from mark_target_live_regs via note_stores.
82 : It deadens any CLOBBERed registers and livens any SET registers. */
83 :
84 : static void
85 0 : update_live_status (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
86 : {
87 0 : int first_regno, last_regno;
88 0 : int i;
89 :
90 0 : if (!REG_P (dest)
91 0 : && (GET_CODE (dest) != SUBREG || !REG_P (SUBREG_REG (dest))))
92 : return;
93 :
94 0 : if (GET_CODE (dest) == SUBREG)
95 : {
96 0 : first_regno = subreg_regno (dest);
97 0 : last_regno = first_regno + subreg_nregs (dest);
98 :
99 : }
100 : else
101 : {
102 0 : first_regno = REGNO (dest);
103 0 : last_regno = END_REGNO (dest);
104 : }
105 :
106 0 : if (GET_CODE (x) == CLOBBER)
107 0 : for (i = first_regno; i < last_regno; i++)
108 0 : CLEAR_HARD_REG_BIT (current_live_regs, i);
109 : else
110 0 : for (i = first_regno; i < last_regno; i++)
111 : {
112 0 : SET_HARD_REG_BIT (current_live_regs, i);
113 0 : CLEAR_HARD_REG_BIT (pending_dead_regs, i);
114 : }
115 : }
116 :
117 : /* Find the number of the basic block with correct live register
118 : information that starts closest to INSN. Return -1 if we couldn't
119 : find such a basic block or the beginning is more than
120 : SEARCH_LIMIT instructions before INSN. Use SEARCH_LIMIT = -1 for
121 : an unlimited search.
122 :
123 : The delay slot filling code destroys the control-flow graph so,
124 : instead of finding the basic block containing INSN, we search
125 : backwards toward a BARRIER where the live register information is
126 : correct. */
127 :
128 : static int
129 0 : find_basic_block (rtx_insn *insn, int search_limit)
130 : {
131 : /* Scan backwards to the previous BARRIER. Then see if we can find a
132 : label that starts a basic block. Return the basic block number. */
133 0 : for (insn = prev_nonnote_nondebug_insn (insn);
134 0 : insn && !BARRIER_P (insn) && search_limit != 0;
135 0 : insn = prev_nonnote_nondebug_insn (insn), --search_limit)
136 : ;
137 :
138 : /* The closest BARRIER is too far away. */
139 0 : if (search_limit == 0)
140 : return -1;
141 :
142 : /* The start of the function. */
143 0 : else if (insn == 0)
144 0 : return ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->index;
145 :
146 : /* See if any of the upcoming CODE_LABELs start a basic block. If we reach
147 : anything other than a CODE_LABEL or note, we can't find this code. */
148 0 : for (insn = next_nonnote_nondebug_insn (insn);
149 0 : insn && LABEL_P (insn);
150 0 : insn = next_nonnote_nondebug_insn (insn))
151 0 : if (BLOCK_FOR_INSN (insn))
152 0 : return BLOCK_FOR_INSN (insn)->index;
153 :
154 : return -1;
155 : }
156 :
157 : /* Similar to next_insn, but ignores insns in the delay slots of
158 : an annulled branch. */
159 :
160 : static rtx_insn *
161 0 : next_insn_no_annul (rtx_insn *insn)
162 : {
163 0 : if (insn)
164 : {
165 : /* If INSN is an annulled branch, skip any insns from the target
166 : of the branch. */
167 0 : if (JUMP_P (insn)
168 0 : && INSN_ANNULLED_BRANCH_P (insn)
169 0 : && NEXT_INSN (PREV_INSN (insn)) != insn)
170 : {
171 0 : rtx_insn *next = NEXT_INSN (insn);
172 :
173 0 : while ((NONJUMP_INSN_P (next) || JUMP_P (next) || CALL_P (next))
174 0 : && INSN_FROM_TARGET_P (next))
175 : {
176 0 : insn = next;
177 0 : next = NEXT_INSN (insn);
178 : }
179 : }
180 :
181 0 : insn = NEXT_INSN (insn);
182 0 : if (insn && NONJUMP_INSN_P (insn))
183 0 : if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
184 0 : insn = seq->insn (0);
185 : }
186 :
187 0 : return insn;
188 : }
189 :
190 : /* Given X, some rtl, and RES, a pointer to a `struct resource', mark
191 : which resources are referenced by the insn. If INCLUDE_DELAYED_EFFECTS
192 : is TRUE, resources used by the called routine will be included for
193 : CALL_INSNs. */
194 :
195 : void
196 0 : mark_referenced_resources (rtx x, struct resources *res,
197 : bool include_delayed_effects)
198 : {
199 0 : enum rtx_code code = GET_CODE (x);
200 0 : int i, j;
201 0 : unsigned int r;
202 0 : const char *format_ptr;
203 :
204 : /* Handle leaf items for which we set resource flags. Also, special-case
205 : CALL, SET and CLOBBER operators. */
206 0 : switch (code)
207 : {
208 : case CONST:
209 : CASE_CONST_ANY:
210 : case PC:
211 : case SYMBOL_REF:
212 : case LABEL_REF:
213 : case DEBUG_INSN:
214 : return;
215 :
216 0 : case SUBREG:
217 0 : if (!REG_P (SUBREG_REG (x)))
218 : mark_referenced_resources (SUBREG_REG (x), res, false);
219 : else
220 : {
221 0 : unsigned int regno = subreg_regno (x);
222 0 : unsigned int last_regno = regno + subreg_nregs (x);
223 :
224 0 : gcc_assert (last_regno <= FIRST_PSEUDO_REGISTER);
225 0 : for (r = regno; r < last_regno; r++)
226 0 : SET_HARD_REG_BIT (res->regs, r);
227 : }
228 : return;
229 :
230 0 : case REG:
231 0 : gcc_assert (HARD_REGISTER_P (x));
232 0 : add_to_hard_reg_set (&res->regs, GET_MODE (x), REGNO (x));
233 0 : return;
234 :
235 0 : case MEM:
236 : /* If this memory shouldn't change, it really isn't referencing
237 : memory. */
238 0 : if (! MEM_READONLY_P (x))
239 0 : res->memory = 1;
240 0 : res->volatil |= MEM_VOLATILE_P (x);
241 :
242 : /* Mark registers used to access memory. */
243 0 : mark_referenced_resources (XEXP (x, 0), res, false);
244 0 : return;
245 :
246 0 : case UNSPEC_VOLATILE:
247 0 : case TRAP_IF:
248 0 : case ASM_INPUT:
249 : /* Traditional asm's are always volatile. */
250 0 : res->volatil = 1;
251 0 : break;
252 :
253 0 : case ASM_OPERANDS:
254 0 : res->volatil |= MEM_VOLATILE_P (x);
255 :
256 : /* For all ASM_OPERANDS, we must traverse the vector of input operands.
257 : We cannot just fall through here since then we would be confused
258 : by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
259 : traditional asms unlike their normal usage. */
260 :
261 0 : for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
262 0 : mark_referenced_resources (ASM_OPERANDS_INPUT (x, i), res, false);
263 : return;
264 :
265 0 : case CALL:
266 : /* The first operand will be a (MEM (xxx)) but doesn't really reference
267 : memory. The second operand may be referenced, though. */
268 0 : mark_referenced_resources (XEXP (XEXP (x, 0), 0), res, false);
269 0 : mark_referenced_resources (XEXP (x, 1), res, false);
270 0 : return;
271 :
272 0 : case SET:
273 : /* Usually, the first operand of SET is set, not referenced. But
274 : registers used to access memory are referenced. SET_DEST is
275 : also referenced if it is a ZERO_EXTRACT. */
276 :
277 0 : mark_referenced_resources (SET_SRC (x), res, false);
278 :
279 0 : x = SET_DEST (x);
280 0 : if (GET_CODE (x) == ZERO_EXTRACT
281 0 : || GET_CODE (x) == STRICT_LOW_PART)
282 0 : mark_referenced_resources (x, res, false);
283 0 : else if (GET_CODE (x) == SUBREG)
284 0 : x = SUBREG_REG (x);
285 0 : if (MEM_P (x))
286 0 : mark_referenced_resources (XEXP (x, 0), res, false);
287 : return;
288 :
289 : case CLOBBER:
290 : return;
291 :
292 0 : case CALL_INSN:
293 0 : if (include_delayed_effects)
294 : {
295 : /* A CALL references memory, the frame pointer if it exists, the
296 : stack pointer, any global registers and any registers given in
297 : USE insns immediately in front of the CALL.
298 :
299 : However, we may have moved some of the parameter loading insns
300 : into the delay slot of this CALL. If so, the USE's for them
301 : don't count and should be skipped. */
302 0 : rtx_insn *call_insn = as_a <rtx_insn *> (x);
303 0 : rtx_insn *insn = PREV_INSN (call_insn);
304 0 : rtx_sequence *sequence = 0;
305 0 : int seq_size = 0;
306 0 : int i;
307 :
308 : /* If we are part of a delay slot sequence, point at the SEQUENCE. */
309 0 : if (NEXT_INSN (insn) != call_insn)
310 : {
311 0 : sequence = as_a <rtx_sequence *> (PATTERN (NEXT_INSN (insn)));
312 0 : seq_size = sequence->len ();
313 0 : gcc_assert (GET_CODE (sequence) == SEQUENCE);
314 : }
315 :
316 0 : res->memory = 1;
317 0 : SET_HARD_REG_BIT (res->regs, STACK_POINTER_REGNUM);
318 0 : if (frame_pointer_needed)
319 : {
320 0 : SET_HARD_REG_BIT (res->regs, FRAME_POINTER_REGNUM);
321 0 : if (!HARD_FRAME_POINTER_IS_FRAME_POINTER)
322 0 : SET_HARD_REG_BIT (res->regs, HARD_FRAME_POINTER_REGNUM);
323 : }
324 :
325 0 : for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
326 0 : if (global_regs[i])
327 0 : SET_HARD_REG_BIT (res->regs, i);
328 :
329 : /* Check for a REG_SETJMP. If it exists, then we must
330 : assume that this call can need any register.
331 :
332 : This is done to be more conservative about how we handle setjmp.
333 : We assume that they both use and set all registers. Using all
334 : registers ensures that a register will not be considered dead
335 : just because it crosses a setjmp call. A register should be
336 : considered dead only if the setjmp call returns nonzero. */
337 0 : if (find_reg_note (call_insn, REG_SETJMP, NULL))
338 0 : SET_HARD_REG_SET (res->regs);
339 :
340 0 : {
341 0 : rtx link;
342 :
343 0 : for (link = CALL_INSN_FUNCTION_USAGE (call_insn);
344 0 : link;
345 0 : link = XEXP (link, 1))
346 0 : if (GET_CODE (XEXP (link, 0)) == USE)
347 : {
348 0 : for (i = 1; i < seq_size; i++)
349 : {
350 0 : rtx slot_pat = PATTERN (sequence->insn (i));
351 0 : if (GET_CODE (slot_pat) == SET
352 0 : && rtx_equal_p (SET_DEST (slot_pat),
353 0 : XEXP (XEXP (link, 0), 0)))
354 : break;
355 : }
356 0 : if (i >= seq_size)
357 0 : mark_referenced_resources (XEXP (XEXP (link, 0), 0),
358 : res, false);
359 : }
360 : }
361 : }
362 :
363 : /* ... fall through to other INSN processing ... */
364 0 : gcc_fallthrough ();
365 :
366 0 : case INSN:
367 0 : case JUMP_INSN:
368 :
369 0 : if (GET_CODE (PATTERN (x)) == COND_EXEC)
370 : /* In addition to the usual references, also consider all outputs
371 : as referenced, to compensate for mark_set_resources treating
372 : them as killed. This is similar to ZERO_EXTRACT / STRICT_LOW_PART
373 : handling, except that we got a partial incidence instead of a partial
374 : width. */
375 0 : mark_set_resources (x, res, 0,
376 : include_delayed_effects
377 : ? MARK_SRC_DEST_CALL : MARK_SRC_DEST);
378 :
379 0 : if (! include_delayed_effects
380 : && INSN_REFERENCES_ARE_DELAYED (as_a <rtx_insn *> (x)))
381 : return;
382 :
383 : /* No special processing, just speed up. */
384 0 : mark_referenced_resources (PATTERN (x), res, include_delayed_effects);
385 0 : return;
386 :
387 : default:
388 : break;
389 : }
390 :
391 : /* Process each sub-expression and flag what it needs. */
392 0 : format_ptr = GET_RTX_FORMAT (code);
393 0 : for (i = 0; i < GET_RTX_LENGTH (code); i++)
394 0 : switch (*format_ptr++)
395 : {
396 0 : case 'e':
397 0 : mark_referenced_resources (XEXP (x, i), res, include_delayed_effects);
398 0 : break;
399 :
400 : case 'E':
401 0 : for (j = 0; j < XVECLEN (x, i); j++)
402 0 : mark_referenced_resources (XVECEXP (x, i, j), res,
403 : include_delayed_effects);
404 : break;
405 : }
406 : }
407 :
408 : /* Given X, a part of an insn, and a pointer to a `struct resource',
409 : RES, indicate which resources are modified by the insn. If
410 : MARK_TYPE is MARK_SRC_DEST_CALL, also mark resources potentially
411 : set by the called routine.
412 :
413 : If IN_DEST is nonzero, it means we are inside a SET. Otherwise,
414 : objects are being referenced instead of set. */
415 :
416 : void
417 0 : mark_set_resources (rtx x, struct resources *res, int in_dest,
418 : enum mark_resource_type mark_type)
419 : {
420 0 : enum rtx_code code;
421 0 : int i, j;
422 0 : unsigned int r;
423 0 : const char *format_ptr;
424 :
425 0 : restart:
426 :
427 0 : code = GET_CODE (x);
428 :
429 0 : switch (code)
430 : {
431 : case NOTE:
432 : case BARRIER:
433 : case CODE_LABEL:
434 : case USE:
435 : CASE_CONST_ANY:
436 : case LABEL_REF:
437 : case SYMBOL_REF:
438 : case CONST:
439 : case PC:
440 : case DEBUG_INSN:
441 : /* These don't set any resources. */
442 : return;
443 :
444 0 : case CALL_INSN:
445 : /* Called routine modifies the condition code, memory, any registers
446 : that aren't saved across calls, global registers and anything
447 : explicitly CLOBBERed immediately after the CALL_INSN. */
448 :
449 0 : if (mark_type == MARK_SRC_DEST_CALL)
450 : {
451 0 : rtx_call_insn *call_insn = as_a <rtx_call_insn *> (x);
452 0 : rtx link;
453 :
454 0 : res->cc = res->memory = 1;
455 :
456 0 : res->regs |= insn_callee_abi (call_insn).full_reg_clobbers ();
457 :
458 0 : for (link = CALL_INSN_FUNCTION_USAGE (call_insn);
459 0 : link; link = XEXP (link, 1))
460 0 : if (GET_CODE (XEXP (link, 0)) == CLOBBER)
461 0 : mark_set_resources (SET_DEST (XEXP (link, 0)), res, 1,
462 : MARK_SRC_DEST);
463 :
464 : /* Check for a REG_SETJMP. If it exists, then we must
465 : assume that this call can clobber any register. */
466 0 : if (find_reg_note (call_insn, REG_SETJMP, NULL))
467 0 : SET_HARD_REG_SET (res->regs);
468 : }
469 :
470 : /* ... and also what its RTL says it modifies, if anything. */
471 0 : gcc_fallthrough ();
472 :
473 0 : case JUMP_INSN:
474 0 : case INSN:
475 :
476 : /* An insn consisting of just a CLOBBER (or USE) is just for flow
477 : and doesn't actually do anything, so we ignore it. */
478 :
479 0 : if (mark_type != MARK_SRC_DEST_CALL
480 : && INSN_SETS_ARE_DELAYED (as_a <rtx_insn *> (x)))
481 : return;
482 :
483 0 : x = PATTERN (x);
484 0 : if (GET_CODE (x) != USE && GET_CODE (x) != CLOBBER)
485 0 : goto restart;
486 : return;
487 :
488 0 : case SET:
489 : /* If the source of a SET is a CALL, this is actually done by
490 : the called routine. So only include it if we are to include the
491 : effects of the calling routine. */
492 :
493 0 : mark_set_resources (SET_DEST (x), res,
494 : (mark_type == MARK_SRC_DEST_CALL
495 0 : || GET_CODE (SET_SRC (x)) != CALL),
496 : mark_type);
497 :
498 0 : mark_set_resources (SET_SRC (x), res, 0, MARK_SRC_DEST);
499 0 : return;
500 :
501 0 : case CLOBBER:
502 0 : mark_set_resources (XEXP (x, 0), res, 1, MARK_SRC_DEST);
503 0 : return;
504 :
505 0 : case SEQUENCE:
506 0 : {
507 0 : rtx_sequence *seq = as_a <rtx_sequence *> (x);
508 0 : rtx control = seq->element (0);
509 0 : bool annul_p = JUMP_P (control) && INSN_ANNULLED_BRANCH_P (control);
510 :
511 0 : mark_set_resources (control, res, 0, mark_type);
512 0 : for (i = seq->len () - 1; i >= 0; --i)
513 : {
514 0 : rtx_insn *elt = seq->insn (i);
515 0 : if (!annul_p && INSN_FROM_TARGET_P (elt))
516 0 : mark_set_resources (elt, res, 0, mark_type);
517 : }
518 : }
519 : return;
520 :
521 0 : case POST_INC:
522 0 : case PRE_INC:
523 0 : case POST_DEC:
524 0 : case PRE_DEC:
525 0 : mark_set_resources (XEXP (x, 0), res, 1, MARK_SRC_DEST);
526 0 : return;
527 :
528 0 : case PRE_MODIFY:
529 0 : case POST_MODIFY:
530 0 : mark_set_resources (XEXP (x, 0), res, 1, MARK_SRC_DEST);
531 0 : mark_set_resources (XEXP (XEXP (x, 1), 0), res, 0, MARK_SRC_DEST);
532 0 : mark_set_resources (XEXP (XEXP (x, 1), 1), res, 0, MARK_SRC_DEST);
533 0 : return;
534 :
535 0 : case SIGN_EXTRACT:
536 0 : case ZERO_EXTRACT:
537 0 : mark_set_resources (XEXP (x, 0), res, in_dest, MARK_SRC_DEST);
538 0 : mark_set_resources (XEXP (x, 1), res, 0, MARK_SRC_DEST);
539 0 : mark_set_resources (XEXP (x, 2), res, 0, MARK_SRC_DEST);
540 0 : return;
541 :
542 0 : case MEM:
543 0 : if (in_dest)
544 : {
545 0 : res->memory = 1;
546 0 : res->volatil |= MEM_VOLATILE_P (x);
547 : }
548 :
549 0 : mark_set_resources (XEXP (x, 0), res, 0, MARK_SRC_DEST);
550 0 : return;
551 :
552 0 : case SUBREG:
553 0 : if (in_dest)
554 : {
555 0 : if (!REG_P (SUBREG_REG (x)))
556 : mark_set_resources (SUBREG_REG (x), res, in_dest, mark_type);
557 : else
558 : {
559 0 : unsigned int regno = subreg_regno (x);
560 0 : unsigned int last_regno = regno + subreg_nregs (x);
561 :
562 0 : gcc_assert (last_regno <= FIRST_PSEUDO_REGISTER);
563 0 : for (r = regno; r < last_regno; r++)
564 0 : SET_HARD_REG_BIT (res->regs, r);
565 : }
566 : }
567 : return;
568 :
569 0 : case REG:
570 0 : if (in_dest)
571 : {
572 0 : gcc_assert (HARD_REGISTER_P (x));
573 0 : add_to_hard_reg_set (&res->regs, GET_MODE (x), REGNO (x));
574 : }
575 : return;
576 :
577 0 : case UNSPEC_VOLATILE:
578 0 : case ASM_INPUT:
579 : /* Traditional asm's are always volatile. */
580 0 : res->volatil = 1;
581 0 : return;
582 :
583 0 : case TRAP_IF:
584 0 : res->volatil = 1;
585 0 : break;
586 :
587 0 : case ASM_OPERANDS:
588 0 : res->volatil |= MEM_VOLATILE_P (x);
589 :
590 : /* For all ASM_OPERANDS, we must traverse the vector of input operands.
591 : We cannot just fall through here since then we would be confused
592 : by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
593 : traditional asms unlike their normal usage. */
594 :
595 0 : for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
596 0 : mark_set_resources (ASM_OPERANDS_INPUT (x, i), res, in_dest,
597 : MARK_SRC_DEST);
598 : return;
599 :
600 : default:
601 : break;
602 : }
603 :
604 : /* Process each sub-expression and flag what it needs. */
605 0 : format_ptr = GET_RTX_FORMAT (code);
606 0 : for (i = 0; i < GET_RTX_LENGTH (code); i++)
607 0 : switch (*format_ptr++)
608 : {
609 0 : case 'e':
610 0 : mark_set_resources (XEXP (x, i), res, in_dest, mark_type);
611 0 : break;
612 :
613 : case 'E':
614 0 : for (j = 0; j < XVECLEN (x, i); j++)
615 0 : mark_set_resources (XVECEXP (x, i, j), res, in_dest, mark_type);
616 : break;
617 : }
618 : }
619 :
620 : /* Return TRUE if INSN is a return, possibly with a filled delay slot. */
621 :
622 : static bool
623 0 : return_insn_p (const rtx_insn *insn)
624 : {
625 0 : if (JUMP_P (insn) && ANY_RETURN_P (PATTERN (insn)))
626 : return true;
627 :
628 0 : rtx_sequence *seq;
629 0 : if (NONJUMP_INSN_P (insn)
630 0 : && (seq = dyn_cast <rtx_sequence *> (PATTERN (insn))))
631 0 : return return_insn_p (seq->insn (0));
632 :
633 : return false;
634 : }
635 :
636 : /* Set the resources that are live at TARGET.
637 :
638 : If TARGET is zero, we refer to the end of the current function and can
639 : return our precomputed value.
640 :
641 : Otherwise, we try to find out what is live by consulting the basic block
642 : information. This is tricky, because we must consider the actions of
643 : reload and jump optimization, which occur after the basic block information
644 : has been computed.
645 :
646 : Accordingly, we proceed as follows::
647 :
648 : We find the previous BARRIER and look at all immediately following labels
649 : (with no intervening active insns) to see if any of them start a basic
650 : block. If we hit the start of the function first, we use block 0.
651 :
652 : Once we have found a basic block and a corresponding first insn, we can
653 : accurately compute the live status (by starting at a label following a
654 : BARRIER, we are immune to actions taken by reload and jump.) Then we
655 : scan all insns between that point and our target. For each CLOBBER (or
656 : for call-clobbered regs when we pass a CALL_INSN), mark the appropriate
657 : registers are dead. For a SET, mark them as live.
658 :
659 : We have to be careful when using REG_DEAD notes because they are not
660 : updated by such things as find_equiv_reg. So keep track of registers
661 : marked as dead that haven't been assigned to, and mark them dead at the
662 : next CODE_LABEL since reload and jump won't propagate values across labels.
663 :
664 : If we cannot find the start of a basic block (should be a very rare
665 : case, if it can happen at all), mark everything as potentially live.
666 :
667 : Because we can be called many times on the same target, save our results
668 : in a hash table indexed by INSN_UID. This is only done if the function
669 : init_resource_info () was invoked before we are called. */
670 :
671 : void
672 0 : mark_target_live_regs (rtx_insn *insns, rtx target_maybe_return, struct resources *res)
673 : {
674 0 : int b = -1;
675 0 : unsigned int i;
676 0 : struct target_info *tinfo = NULL;
677 0 : rtx_insn *insn;
678 :
679 : /* Handle end of function. */
680 0 : if (target_maybe_return == 0 || ANY_RETURN_P (target_maybe_return))
681 : {
682 0 : *res = end_of_function_needs;
683 0 : return;
684 : }
685 :
686 : /* We've handled the case of RETURN/SIMPLE_RETURN; we should now have an
687 : instruction. */
688 0 : rtx_insn *target = as_a <rtx_insn *> (target_maybe_return);
689 :
690 : /* Handle return insn. */
691 0 : if (return_insn_p (target))
692 : {
693 0 : *res = end_of_function_needs;
694 0 : mark_referenced_resources (target, res, false);
695 0 : return;
696 : }
697 :
698 : /* We have to assume memory is needed, but the CC isn't. */
699 0 : res->memory = 1;
700 0 : res->volatil = 0;
701 0 : res->cc = 0;
702 :
703 : /* See if we have computed this value already. */
704 0 : if (target_hash_table != NULL)
705 : {
706 0 : for (tinfo = target_hash_table[INSN_UID (target) % TARGET_HASH_PRIME];
707 0 : tinfo; tinfo = tinfo->next)
708 0 : if (tinfo->uid == INSN_UID (target))
709 : break;
710 :
711 : /* Start by getting the basic block number. If we have saved
712 : information, we can get it from there unless the insn at the
713 : start of the basic block has been deleted. */
714 0 : if (tinfo && tinfo->block != -1
715 0 : && ! BB_HEAD (BASIC_BLOCK_FOR_FN (cfun, tinfo->block))->deleted ())
716 : b = tinfo->block;
717 : }
718 :
719 : if (b == -1)
720 0 : b = find_basic_block (target, param_max_delay_slot_live_search);
721 :
722 0 : if (target_hash_table != NULL)
723 : {
724 0 : if (tinfo)
725 : {
726 : /* If the information is up-to-date, use it. Otherwise, we will
727 : update it below. */
728 0 : if (b == tinfo->block && b != -1 && tinfo->bb_tick == bb_ticks[b])
729 : {
730 0 : res->regs = tinfo->live_regs;
731 0 : return;
732 : }
733 : }
734 : else
735 : {
736 : /* Allocate a place to put our results and chain it into the
737 : hash table. */
738 0 : tinfo = XNEW (struct target_info);
739 0 : tinfo->uid = INSN_UID (target);
740 0 : tinfo->block = b;
741 0 : tinfo->next
742 0 : = target_hash_table[INSN_UID (target) % TARGET_HASH_PRIME];
743 0 : target_hash_table[INSN_UID (target) % TARGET_HASH_PRIME] = tinfo;
744 : }
745 : }
746 :
747 0 : CLEAR_HARD_REG_SET (pending_dead_regs);
748 :
749 : /* If we found a basic block, get the live registers from it and update
750 : them with anything set or killed between its start and the insn before
751 : TARGET; this custom life analysis is really about registers so we need
752 : to use the LR problem. Otherwise, we must assume everything is live. */
753 0 : if (b != -1)
754 : {
755 0 : regset regs_live = DF_LR_IN (BASIC_BLOCK_FOR_FN (cfun, b));
756 0 : rtx_insn *start_insn, *stop_insn;
757 0 : df_ref def;
758 :
759 : /* Compute hard regs live at start of block. */
760 0 : REG_SET_TO_HARD_REG_SET (current_live_regs, regs_live);
761 0 : FOR_EACH_ARTIFICIAL_DEF (def, b)
762 0 : if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
763 0 : SET_HARD_REG_BIT (current_live_regs, DF_REF_REGNO (def));
764 :
765 : /* Get starting and ending insn, handling the case where each might
766 : be a SEQUENCE. */
767 0 : start_insn = (b == ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->index ?
768 0 : insns : BB_HEAD (BASIC_BLOCK_FOR_FN (cfun, b)));
769 0 : stop_insn = target;
770 :
771 0 : if (NONJUMP_INSN_P (start_insn))
772 0 : if (auto *seq = dyn_cast <rtx_sequence *> (PATTERN (start_insn)))
773 0 : start_insn = seq->insn (0);
774 :
775 0 : if (NONJUMP_INSN_P (stop_insn)
776 0 : && is_a <rtx_sequence *> (PATTERN (stop_insn)))
777 0 : stop_insn = next_insn (PREV_INSN (stop_insn));
778 :
779 0 : for (insn = start_insn; insn != stop_insn;
780 0 : insn = next_insn_no_annul (insn))
781 : {
782 0 : rtx link;
783 0 : rtx_insn *real_insn = insn;
784 0 : enum rtx_code code = GET_CODE (insn);
785 :
786 0 : if (DEBUG_INSN_P (insn))
787 0 : continue;
788 :
789 : /* If this insn is from the target of a branch, it isn't going to
790 : be used in the sequel. If it is used in both cases, this
791 : test will not be true. */
792 0 : if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
793 0 : && INSN_FROM_TARGET_P (insn))
794 0 : continue;
795 :
796 : /* If this insn is a USE made by update_block, we care about the
797 : underlying insn. */
798 0 : if (code == INSN
799 0 : && GET_CODE (PATTERN (insn)) == USE
800 0 : && INSN_P (XEXP (PATTERN (insn), 0)))
801 0 : real_insn = as_a <rtx_insn *> (XEXP (PATTERN (insn), 0));
802 :
803 0 : if (auto call = dyn_cast<rtx_call_insn *> (real_insn))
804 : {
805 : /* Values in call-clobbered registers survive a COND_EXEC CALL
806 : if that is not executed; this matters for resource use because
807 : they may be used by a complementarily (or more strictly)
808 : predicated instruction, or if the CALL is NORETURN. */
809 0 : if (GET_CODE (PATTERN (call)) != COND_EXEC)
810 : {
811 0 : HARD_REG_SET regs_invalidated_by_this_call
812 0 : = insn_callee_abi (call).full_reg_clobbers ();
813 : /* CALL clobbers all call-used regs that aren't fixed except
814 : sp, ap, and fp. Do this before setting the result of the
815 : call live. */
816 0 : current_live_regs &= ~regs_invalidated_by_this_call;
817 : }
818 :
819 : /* A CALL_INSN sets any global register live, since it may
820 : have been modified by the call. */
821 0 : for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
822 0 : if (global_regs[i])
823 0 : SET_HARD_REG_BIT (current_live_regs, i);
824 : }
825 :
826 : /* Mark anything killed in an insn to be deadened at the next
827 : label. Ignore USE insns; the only REG_DEAD notes will be for
828 : parameters. But they might be early. A CALL_INSN will usually
829 : clobber registers used for parameters. It isn't worth bothering
830 : with the unlikely case when it won't. */
831 0 : if ((NONJUMP_INSN_P (real_insn)
832 0 : && GET_CODE (PATTERN (real_insn)) != USE
833 0 : && GET_CODE (PATTERN (real_insn)) != CLOBBER)
834 0 : || JUMP_P (real_insn)
835 0 : || CALL_P (real_insn))
836 : {
837 0 : for (link = REG_NOTES (real_insn); link; link = XEXP (link, 1))
838 0 : if (REG_NOTE_KIND (link) == REG_DEAD
839 0 : && REG_P (XEXP (link, 0))
840 0 : && REGNO (XEXP (link, 0)) < FIRST_PSEUDO_REGISTER)
841 0 : add_to_hard_reg_set (&pending_dead_regs,
842 0 : GET_MODE (XEXP (link, 0)),
843 0 : REGNO (XEXP (link, 0)));
844 :
845 0 : note_stores (real_insn, update_live_status, NULL);
846 :
847 : /* If any registers were unused after this insn, kill them.
848 : These notes will always be accurate. */
849 0 : for (link = REG_NOTES (real_insn); link; link = XEXP (link, 1))
850 0 : if (REG_NOTE_KIND (link) == REG_UNUSED
851 0 : && REG_P (XEXP (link, 0))
852 0 : && REGNO (XEXP (link, 0)) < FIRST_PSEUDO_REGISTER)
853 0 : remove_from_hard_reg_set (¤t_live_regs,
854 0 : GET_MODE (XEXP (link, 0)),
855 0 : REGNO (XEXP (link, 0)));
856 : }
857 :
858 0 : else if (LABEL_P (real_insn))
859 : {
860 0 : basic_block bb;
861 :
862 : /* A label clobbers the pending dead registers since neither
863 : reload nor jump will propagate a value across a label. */
864 0 : current_live_regs &= ~pending_dead_regs;
865 0 : CLEAR_HARD_REG_SET (pending_dead_regs);
866 :
867 : /* We must conservatively assume that all registers that used
868 : to be live here still are. The fallthrough edge may have
869 : left a live register uninitialized. */
870 0 : bb = BLOCK_FOR_INSN (real_insn);
871 0 : if (bb)
872 : {
873 : HARD_REG_SET extra_live;
874 :
875 0 : REG_SET_TO_HARD_REG_SET (extra_live, DF_LR_IN (bb));
876 0 : current_live_regs |= extra_live;
877 : }
878 : }
879 :
880 : /* The beginning of the epilogue corresponds to the end of the
881 : RTL chain when there are no epilogue insns. Certain resources
882 : are implicitly required at that point. */
883 0 : else if (NOTE_P (real_insn)
884 0 : && NOTE_KIND (real_insn) == NOTE_INSN_EPILOGUE_BEG)
885 0 : current_live_regs |= start_of_epilogue_needs.regs;
886 : }
887 :
888 0 : res->regs = current_live_regs;
889 0 : if (tinfo != NULL)
890 : {
891 0 : tinfo->block = b;
892 0 : tinfo->bb_tick = bb_ticks[b];
893 : }
894 : }
895 : else
896 : /* We didn't find the start of a basic block. Assume everything
897 : in use. This should happen only extremely rarely. */
898 0 : SET_HARD_REG_SET (res->regs);
899 :
900 0 : if (tinfo != NULL)
901 0 : tinfo->live_regs = res->regs;
902 : }
903 :
904 : /* Initialize the resources required by mark_target_live_regs ().
905 : This should be invoked before the first call to mark_target_live_regs. */
906 :
907 : void
908 0 : init_resource_info (rtx_insn *epilogue_insn)
909 : {
910 0 : int i;
911 0 : basic_block bb;
912 :
913 : /* Indicate what resources are required to be valid at the end of the current
914 : function. The condition code never is and memory always is.
915 : The stack pointer is needed unless EXIT_IGNORE_STACK is true
916 : and there is an epilogue that restores the original stack pointer
917 : from the frame pointer. Registers used to return the function value
918 : are needed. Registers holding global variables are needed. */
919 :
920 0 : end_of_function_needs.cc = 0;
921 0 : end_of_function_needs.memory = 1;
922 0 : CLEAR_HARD_REG_SET (end_of_function_needs.regs);
923 :
924 0 : if (frame_pointer_needed)
925 : {
926 0 : SET_HARD_REG_BIT (end_of_function_needs.regs, FRAME_POINTER_REGNUM);
927 0 : if (!HARD_FRAME_POINTER_IS_FRAME_POINTER)
928 0 : SET_HARD_REG_BIT (end_of_function_needs.regs,
929 : HARD_FRAME_POINTER_REGNUM);
930 : }
931 0 : if (!(frame_pointer_needed
932 : && EXIT_IGNORE_STACK
933 0 : && epilogue_insn
934 0 : && !crtl->sp_is_unchanging))
935 0 : SET_HARD_REG_BIT (end_of_function_needs.regs, STACK_POINTER_REGNUM);
936 :
937 0 : if (crtl->return_rtx != 0)
938 0 : mark_referenced_resources (crtl->return_rtx,
939 : &end_of_function_needs, true);
940 :
941 0 : for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
942 0 : if (global_regs[i] || df_epilogue_uses_p (i))
943 0 : SET_HARD_REG_BIT (end_of_function_needs.regs, i);
944 :
945 : /* The registers required to be live at the end of the function are
946 : represented in the flow information as being dead just prior to
947 : reaching the end of the function. For example, the return of a value
948 : might be represented by a USE of the return register immediately
949 : followed by an unconditional jump to the return label where the
950 : return label is the end of the RTL chain. The end of the RTL chain
951 : is then taken to mean that the return register is live.
952 :
953 : This sequence is no longer maintained when epilogue instructions are
954 : added to the RTL chain. To reconstruct the original meaning, the
955 : start of the epilogue (NOTE_INSN_EPILOGUE_BEG) is regarded as the
956 : point where these registers become live (start_of_epilogue_needs).
957 : If epilogue instructions are present, the registers set by those
958 : instructions won't have been processed by flow. Thus, those
959 : registers are additionally required at the end of the RTL chain
960 : (end_of_function_needs). */
961 :
962 0 : start_of_epilogue_needs = end_of_function_needs;
963 :
964 0 : while ((epilogue_insn = next_nonnote_insn (epilogue_insn)))
965 : {
966 0 : mark_set_resources (epilogue_insn, &end_of_function_needs, 0,
967 : MARK_SRC_DEST_CALL);
968 0 : if (return_insn_p (epilogue_insn))
969 : break;
970 : }
971 :
972 : /* Filter-out the flags register from those additionally required
973 : registers. */
974 0 : if (targetm.flags_regnum != INVALID_REGNUM)
975 0 : CLEAR_HARD_REG_BIT (end_of_function_needs.regs, targetm.flags_regnum);
976 :
977 : /* Allocate and initialize the tables used by mark_target_live_regs. */
978 0 : target_hash_table = XCNEWVEC (struct target_info *, TARGET_HASH_PRIME);
979 0 : bb_ticks = XCNEWVEC (int, last_basic_block_for_fn (cfun));
980 :
981 : /* Set the BLOCK_FOR_INSN of each label that starts a basic block. */
982 0 : FOR_EACH_BB_FN (bb, cfun)
983 0 : if (LABEL_P (BB_HEAD (bb)))
984 0 : BLOCK_FOR_INSN (BB_HEAD (bb)) = bb;
985 0 : }
986 :
987 : /* Free up the resources allocated to mark_target_live_regs (). This
988 : should be invoked after the last call to mark_target_live_regs (). */
989 :
990 : void
991 0 : free_resource_info (void)
992 : {
993 0 : basic_block bb;
994 :
995 0 : if (target_hash_table != NULL)
996 : {
997 : int i;
998 :
999 0 : for (i = 0; i < TARGET_HASH_PRIME; ++i)
1000 : {
1001 0 : struct target_info *ti = target_hash_table[i];
1002 :
1003 0 : while (ti)
1004 : {
1005 0 : struct target_info *next = ti->next;
1006 0 : free (ti);
1007 0 : ti = next;
1008 : }
1009 : }
1010 :
1011 0 : free (target_hash_table);
1012 0 : target_hash_table = NULL;
1013 : }
1014 :
1015 0 : if (bb_ticks != NULL)
1016 : {
1017 0 : free (bb_ticks);
1018 0 : bb_ticks = NULL;
1019 : }
1020 :
1021 0 : FOR_EACH_BB_FN (bb, cfun)
1022 0 : if (LABEL_P (BB_HEAD (bb)))
1023 0 : BLOCK_FOR_INSN (BB_HEAD (bb)) = NULL;
1024 0 : }
1025 :
1026 : /* Clear any hashed information that we have stored for INSN. */
1027 :
1028 : void
1029 0 : clear_hashed_info_for_insn (rtx_insn *insn)
1030 : {
1031 0 : struct target_info *tinfo;
1032 :
1033 0 : if (target_hash_table != NULL)
1034 : {
1035 0 : for (tinfo = target_hash_table[INSN_UID (insn) % TARGET_HASH_PRIME];
1036 0 : tinfo; tinfo = tinfo->next)
1037 0 : if (tinfo->uid == INSN_UID (insn))
1038 : break;
1039 :
1040 0 : if (tinfo)
1041 0 : tinfo->block = -1;
1042 : }
1043 0 : }
1044 :
1045 : /* Clear any hashed information that we have stored for instructions
1046 : between INSN and the next BARRIER that follow a JUMP or a LABEL. */
1047 :
1048 : void
1049 0 : clear_hashed_info_until_next_barrier (rtx_insn *insn)
1050 : {
1051 0 : while (insn && !BARRIER_P (insn))
1052 : {
1053 0 : if (JUMP_P (insn) || LABEL_P (insn))
1054 : {
1055 0 : rtx_insn *next = next_active_insn (insn);
1056 0 : if (next)
1057 0 : clear_hashed_info_for_insn (next);
1058 : }
1059 :
1060 0 : insn = next_nonnote_insn (insn);
1061 : }
1062 0 : }
1063 :
1064 : /* Increment the tick count for the basic block that contains INSN. */
1065 :
1066 : void
1067 0 : incr_ticks_for_insn (rtx_insn *insn)
1068 : {
1069 0 : int b = find_basic_block (insn, param_max_delay_slot_live_search);
1070 :
1071 0 : if (b != -1)
1072 0 : bb_ticks[b]++;
1073 0 : }
1074 :
1075 : /* Add TRIAL to the set of resources used at the end of the current
1076 : function. */
1077 : void
1078 0 : mark_end_of_function_resources (rtx trial, bool include_delayed_effects)
1079 : {
1080 0 : mark_referenced_resources (trial, &end_of_function_needs,
1081 : include_delayed_effects);
1082 0 : }
|