Debian Codegraph @

gcc/resource.cc

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