c++: Implement for namespace statics CWG 2867 - Order of initialization for structure...
[official-gcc.git] / gcc / resource.cc
blobaa22692e4e636a4621ea74432d459b7e36e336d4
1 /* Definitions for computing resource usage of specific insns.
2 Copyright (C) 1999-2025 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
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.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #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"
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. */
40 struct target_info
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. */
49 #define TARGET_HASH_PRIME 257
51 /* Indicates what resources are required at the beginning of the epilogue. */
52 static struct resources start_of_epilogue_needs;
54 /* Indicates what resources are required at function end. */
55 static struct resources end_of_function_needs;
57 /* Define the hash table itself. */
58 static struct target_info **target_hash_table = NULL;
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. */
65 static int *bb_ticks;
67 /* Marks registers possibly live at the current place being scanned by
68 mark_target_live_regs. Also used by update_live_status. */
70 static HARD_REG_SET current_live_regs;
72 /* Marks registers for which we have seen a REG_DEAD note but no assignment.
73 Also only used by the next two functions. */
75 static HARD_REG_SET pending_dead_regs;
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 *);
81 /* Utility function called from mark_target_live_regs via note_stores.
82 It deadens any CLOBBERed registers and livens any SET registers. */
84 static void
85 update_live_status (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
87 int first_regno, last_regno;
88 int i;
90 if (!REG_P (dest)
91 && (GET_CODE (dest) != SUBREG || !REG_P (SUBREG_REG (dest))))
92 return;
94 if (GET_CODE (dest) == SUBREG)
96 first_regno = subreg_regno (dest);
97 last_regno = first_regno + subreg_nregs (dest);
100 else
102 first_regno = REGNO (dest);
103 last_regno = END_REGNO (dest);
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++)
112 SET_HARD_REG_BIT (current_live_regs, i);
113 CLEAR_HARD_REG_BIT (pending_dead_regs, i);
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.
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. */
128 static int
129 find_basic_block (rtx_insn *insn, int search_limit)
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)
138 /* The closest BARRIER is too far away. */
139 if (search_limit == 0)
140 return -1;
142 /* The start of the function. */
143 else if (insn == 0)
144 return ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->index;
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;
154 return -1;
157 /* Similar to next_insn, but ignores insns in the delay slots of
158 an annulled branch. */
160 static rtx_insn *
161 next_insn_no_annul (rtx_insn *insn)
163 if (insn)
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)
171 rtx_insn *next = NEXT_INSN (insn);
173 while ((NONJUMP_INSN_P (next) || JUMP_P (next) || CALL_P (next))
174 && INSN_FROM_TARGET_P (next))
176 insn = next;
177 next = NEXT_INSN (insn);
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);
187 return insn;
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. */
195 void
196 mark_referenced_resources (rtx x, struct resources *res,
197 bool include_delayed_effects)
199 enum rtx_code code = GET_CODE (x);
200 int i, j;
201 unsigned int r;
202 const char *format_ptr;
204 /* Handle leaf items for which we set resource flags. Also, special-case
205 CALL, SET and CLOBBER operators. */
206 switch (code)
208 case CONST:
209 CASE_CONST_ANY:
210 case PC:
211 case SYMBOL_REF:
212 case LABEL_REF:
213 case DEBUG_INSN:
214 return;
216 case SUBREG:
217 if (!REG_P (SUBREG_REG (x)))
218 mark_referenced_resources (SUBREG_REG (x), res, false);
219 else
221 unsigned int regno = subreg_regno (x);
222 unsigned int last_regno = regno + subreg_nregs (x);
224 gcc_assert (last_regno <= FIRST_PSEUDO_REGISTER);
225 for (r = regno; r < last_regno; r++)
226 SET_HARD_REG_BIT (res->regs, r);
228 return;
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;
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);
242 /* Mark registers used to access memory. */
243 mark_referenced_resources (XEXP (x, 0), res, false);
244 return;
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;
253 case ASM_OPERANDS:
254 res->volatil |= MEM_VOLATILE_P (x);
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. */
261 for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
262 mark_referenced_resources (ASM_OPERANDS_INPUT (x, i), res, false);
263 return;
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;
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. */
277 mark_referenced_resources (SET_SRC (x), res, false);
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;
289 case CLOBBER:
290 return;
292 case CALL_INSN:
293 if (include_delayed_effects)
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.
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;
307 /* If we are part of a delay slot sequence, point at the SEQUENCE. */
308 if (NEXT_INSN (insn) != x)
310 sequence = as_a <rtx_sequence *> (PATTERN (NEXT_INSN (insn)));
311 seq_size = sequence->len ();
312 gcc_assert (GET_CODE (sequence) == SEQUENCE);
315 res->memory = 1;
316 SET_HARD_REG_BIT (res->regs, STACK_POINTER_REGNUM);
317 if (frame_pointer_needed)
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);
324 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
325 if (global_regs[i])
326 SET_HARD_REG_BIT (res->regs, i);
328 /* Check for a REG_SETJMP. If it exists, then we must
329 assume that this call can need any register.
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);
340 rtx link;
342 for (link = CALL_INSN_FUNCTION_USAGE (x);
343 link;
344 link = XEXP (link, 1))
345 if (GET_CODE (XEXP (link, 0)) == USE)
347 for (i = 1; i < seq_size; i++)
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;
355 if (i >= seq_size)
356 mark_referenced_resources (XEXP (XEXP (link, 0), 0),
357 res, false);
362 /* ... fall through to other INSN processing ... */
363 gcc_fallthrough ();
365 case INSN:
366 case JUMP_INSN:
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);
378 if (! include_delayed_effects
379 && INSN_REFERENCES_ARE_DELAYED (as_a <rtx_insn *> (x)))
380 return;
382 /* No special processing, just speed up. */
383 mark_referenced_resources (PATTERN (x), res, include_delayed_effects);
384 return;
386 default:
387 break;
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++)
395 case 'e':
396 mark_referenced_resources (XEXP (x, i), res, include_delayed_effects);
397 break;
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;
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.
412 If IN_DEST is nonzero, it means we are inside a SET. Otherwise,
413 objects are being referenced instead of set. */
415 void
416 mark_set_resources (rtx x, struct resources *res, int in_dest,
417 enum mark_resource_type mark_type)
419 enum rtx_code code;
420 int i, j;
421 unsigned int r;
422 const char *format_ptr;
424 restart:
426 code = GET_CODE (x);
428 switch (code)
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;
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. */
448 if (mark_type == MARK_SRC_DEST_CALL)
450 rtx_call_insn *call_insn = as_a <rtx_call_insn *> (x);
451 rtx link;
453 res->cc = res->memory = 1;
455 res->regs |= insn_callee_abi (call_insn).full_reg_clobbers ();
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);
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);
469 /* ... and also what its RTL says it modifies, if anything. */
470 gcc_fallthrough ();
472 case JUMP_INSN:
473 case INSN:
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. */
478 if (mark_type != MARK_SRC_DEST_CALL
479 && INSN_SETS_ARE_DELAYED (as_a <rtx_insn *> (x)))
480 return;
482 x = PATTERN (x);
483 if (GET_CODE (x) != USE && GET_CODE (x) != CLOBBER)
484 goto restart;
485 return;
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. */
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);
497 mark_set_resources (SET_SRC (x), res, 0, MARK_SRC_DEST);
498 return;
500 case CLOBBER:
501 mark_set_resources (XEXP (x, 0), res, 1, MARK_SRC_DEST);
502 return;
504 case SEQUENCE:
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);
510 mark_set_resources (control, res, 0, mark_type);
511 for (i = seq->len () - 1; i >= 0; --i)
513 rtx elt = seq->element (i);
514 if (!annul_p && INSN_FROM_TARGET_P (elt))
515 mark_set_resources (elt, res, 0, mark_type);
518 return;
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;
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;
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;
541 case MEM:
542 if (in_dest)
544 res->memory = 1;
545 res->volatil |= MEM_VOLATILE_P (x);
548 mark_set_resources (XEXP (x, 0), res, 0, MARK_SRC_DEST);
549 return;
551 case SUBREG:
552 if (in_dest)
554 if (!REG_P (SUBREG_REG (x)))
555 mark_set_resources (SUBREG_REG (x), res, in_dest, mark_type);
556 else
558 unsigned int regno = subreg_regno (x);
559 unsigned int last_regno = regno + subreg_nregs (x);
561 gcc_assert (last_regno <= FIRST_PSEUDO_REGISTER);
562 for (r = regno; r < last_regno; r++)
563 SET_HARD_REG_BIT (res->regs, r);
566 return;
568 case REG:
569 if (in_dest)
571 gcc_assert (HARD_REGISTER_P (x));
572 add_to_hard_reg_set (&res->regs, GET_MODE (x), REGNO (x));
574 return;
576 case UNSPEC_VOLATILE:
577 case ASM_INPUT:
578 /* Traditional asm's are always volatile. */
579 res->volatil = 1;
580 return;
582 case TRAP_IF:
583 res->volatil = 1;
584 break;
586 case ASM_OPERANDS:
587 res->volatil |= MEM_VOLATILE_P (x);
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. */
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;
599 default:
600 break;
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++)
608 case 'e':
609 mark_set_resources (XEXP (x, i), res, in_dest, mark_type);
610 break;
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;
619 /* Return TRUE if INSN is a return, possibly with a filled delay slot. */
621 static bool
622 return_insn_p (const_rtx insn)
624 if (JUMP_P (insn) && ANY_RETURN_P (PATTERN (insn)))
625 return true;
627 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
628 return return_insn_p (XVECEXP (PATTERN (insn), 0, 0));
630 return false;
633 /* Set the resources that are live at TARGET.
635 If TARGET is zero, we refer to the end of the current function and can
636 return our precomputed value.
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.
643 Accordingly, we proceed as follows::
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.
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.
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.
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.
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. */
668 void
669 mark_target_live_regs (rtx_insn *insns, rtx target_maybe_return, struct resources *res)
671 int b = -1;
672 unsigned int i;
673 struct target_info *tinfo = NULL;
674 rtx_insn *insn;
676 /* Handle end of function. */
677 if (target_maybe_return == 0 || ANY_RETURN_P (target_maybe_return))
679 *res = end_of_function_needs;
680 return;
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);
687 /* Handle return insn. */
688 if (return_insn_p (target))
690 *res = end_of_function_needs;
691 mark_referenced_resources (target, res, false);
692 return;
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;
700 /* See if we have computed this value already. */
701 if (target_hash_table != NULL)
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;
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;
716 if (b == -1)
717 b = find_basic_block (target, param_max_delay_slot_live_search);
719 if (target_hash_table != NULL)
721 if (tinfo)
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])
727 res->regs = tinfo->live_regs;
728 return;
731 else
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;
744 CLEAR_HARD_REG_SET (pending_dead_regs);
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)
752 regset regs_live = DF_LR_IN (BASIC_BLOCK_FOR_FN (cfun, b));
753 rtx_insn *start_insn, *stop_insn;
754 df_ref def;
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));
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;
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);
772 if (NONJUMP_INSN_P (stop_insn)
773 && GET_CODE (PATTERN (stop_insn)) == SEQUENCE)
774 stop_insn = next_insn (PREV_INSN (stop_insn));
776 for (insn = start_insn; insn != stop_insn;
777 insn = next_insn_no_annul (insn))
779 rtx link;
780 rtx_insn *real_insn = insn;
781 enum rtx_code code = GET_CODE (insn);
783 if (DEBUG_INSN_P (insn))
784 continue;
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;
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));
800 if (CALL_P (real_insn))
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)
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;
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);
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))
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)));
842 note_stores (real_insn, update_live_status, NULL);
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)));
855 else if (LABEL_P (real_insn))
857 basic_block bb;
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);
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)
870 HARD_REG_SET extra_live;
872 REG_SET_TO_HARD_REG_SET (extra_live, DF_LR_IN (bb));
873 current_live_regs |= extra_live;
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;
885 res->regs = current_live_regs;
886 if (tinfo != NULL)
888 tinfo->block = b;
889 tinfo->bb_tick = bb_ticks[b];
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);
897 if (tinfo != NULL)
898 tinfo->live_regs = res->regs;
901 /* Initialize the resources required by mark_target_live_regs ().
902 This should be invoked before the first call to mark_target_live_regs. */
904 void
905 init_resource_info (rtx_insn *epilogue_insn)
907 int i;
908 basic_block bb;
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. */
917 end_of_function_needs.cc = 0;
918 end_of_function_needs.memory = 1;
919 CLEAR_HARD_REG_SET (end_of_function_needs.regs);
921 if (frame_pointer_needed)
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);
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);
934 if (crtl->return_rtx != 0)
935 mark_referenced_resources (crtl->return_rtx,
936 &end_of_function_needs, true);
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);
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.
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). */
959 start_of_epilogue_needs = end_of_function_needs;
961 while ((epilogue_insn = next_nonnote_insn (epilogue_insn)))
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;
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);
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));
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;
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 (). */
987 void
988 free_resource_info (void)
990 basic_block bb;
992 if (target_hash_table != NULL)
994 int i;
996 for (i = 0; i < TARGET_HASH_PRIME; ++i)
998 struct target_info *ti = target_hash_table[i];
1000 while (ti)
1002 struct target_info *next = ti->next;
1003 free (ti);
1004 ti = next;
1008 free (target_hash_table);
1009 target_hash_table = NULL;
1012 if (bb_ticks != NULL)
1014 free (bb_ticks);
1015 bb_ticks = NULL;
1018 FOR_EACH_BB_FN (bb, cfun)
1019 if (LABEL_P (BB_HEAD (bb)))
1020 BLOCK_FOR_INSN (BB_HEAD (bb)) = NULL;
1023 /* Clear any hashed information that we have stored for INSN. */
1025 void
1026 clear_hashed_info_for_insn (rtx_insn *insn)
1028 struct target_info *tinfo;
1030 if (target_hash_table != NULL)
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;
1037 if (tinfo)
1038 tinfo->block = -1;
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. */
1045 void
1046 clear_hashed_info_until_next_barrier (rtx_insn *insn)
1048 while (insn && !BARRIER_P (insn))
1050 if (JUMP_P (insn) || LABEL_P (insn))
1052 rtx_insn *next = next_active_insn (insn);
1053 if (next)
1054 clear_hashed_info_for_insn (next);
1057 insn = next_nonnote_insn (insn);
1061 /* Increment the tick count for the basic block that contains INSN. */
1063 void
1064 incr_ticks_for_insn (rtx_insn *insn)
1066 int b = find_basic_block (insn, param_max_delay_slot_live_search);
1068 if (b != -1)
1069 bb_ticks[b]++;
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)
1077 mark_referenced_resources (trial, &end_of_function_needs,
1078 include_delayed_effects);