1 /* Definitions for computing resource usage of specific insns.
2 Copyright (C) 1999, 2000, 2001 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 2, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 #include "hard-reg-set.h"
27 #include "basic-block.h"
34 #include "insn-attr.h"
37 /* This structure is used to record liveness information at the targets or
38 fallthrough insns of branches. We will most likely need the information
39 at targets again, so save them in a hash table rather than recomputing them
44 int uid
; /* INSN_UID of target. */
45 struct target_info
*next
; /* Next info for same hash bucket. */
46 HARD_REG_SET live_regs
; /* Registers live at target. */
47 int block
; /* Basic block number containing target. */
48 int bb_tick
; /* Generation count of basic block info. */
51 #define TARGET_HASH_PRIME 257
53 /* Indicates what resources are required at the beginning of the epilogue. */
54 static struct resources start_of_epilogue_needs
;
56 /* Indicates what resources are required at function end. */
57 static struct resources end_of_function_needs
;
59 /* Define the hash table itself. */
60 static struct target_info
**target_hash_table
= NULL
;
62 /* For each basic block, we maintain a generation number of its basic
63 block info, which is updated each time we move an insn from the
64 target of a jump. This is the generation number indexed by block
69 /* Marks registers possibly live at the current place being scanned by
70 mark_target_live_regs. Also used by update_live_status. */
72 static HARD_REG_SET current_live_regs
;
74 /* Marks registers for which we have seen a REG_DEAD note but no assignment.
75 Also only used by the next two functions. */
77 static HARD_REG_SET pending_dead_regs
;
79 static void update_live_status
PARAMS ((rtx
, rtx
, void *));
80 static int find_basic_block
PARAMS ((rtx
, int));
81 static rtx next_insn_no_annul
PARAMS ((rtx
));
82 static rtx find_dead_or_set_registers
PARAMS ((rtx
, struct resources
*,
83 rtx
*, int, struct resources
,
86 /* Utility function called from mark_target_live_regs via note_stores.
87 It deadens any CLOBBERed registers and livens any SET registers. */
90 update_live_status (dest
, x
, data
)
93 void *data ATTRIBUTE_UNUSED
;
95 int first_regno
, last_regno
;
98 if (GET_CODE (dest
) != REG
99 && (GET_CODE (dest
) != SUBREG
|| GET_CODE (SUBREG_REG (dest
)) != REG
))
102 if (GET_CODE (dest
) == SUBREG
)
103 first_regno
= subreg_regno (dest
);
105 first_regno
= REGNO (dest
);
107 last_regno
= first_regno
+ HARD_REGNO_NREGS (first_regno
, GET_MODE (dest
));
109 if (GET_CODE (x
) == CLOBBER
)
110 for (i
= first_regno
; i
< last_regno
; i
++)
111 CLEAR_HARD_REG_BIT (current_live_regs
, i
);
113 for (i
= first_regno
; i
< last_regno
; i
++)
115 SET_HARD_REG_BIT (current_live_regs
, i
);
116 CLEAR_HARD_REG_BIT (pending_dead_regs
, i
);
120 /* Find the number of the basic block with correct live register
121 information that starts closest to INSN. Return -1 if we couldn't
122 find such a basic block or the beginning is more than
123 SEARCH_LIMIT instructions before INSN. Use SEARCH_LIMIT = -1 for
126 The delay slot filling code destroys the control-flow graph so,
127 instead of finding the basic block containing INSN, we search
128 backwards toward a BARRIER where the live register information is
132 find_basic_block (insn
, search_limit
)
138 /* Scan backwards to the previous BARRIER. Then see if we can find a
139 label that starts a basic block. Return the basic block number. */
140 for (insn
= prev_nonnote_insn (insn
);
141 insn
&& GET_CODE (insn
) != BARRIER
&& search_limit
!= 0;
142 insn
= prev_nonnote_insn (insn
), --search_limit
)
145 /* The closest BARRIER is too far away. */
146 if (search_limit
== 0)
149 /* The start of the function is basic block zero. */
153 /* See if any of the upcoming CODE_LABELs start a basic block. If we reach
154 anything other than a CODE_LABEL or note, we can't find this code. */
155 for (insn
= next_nonnote_insn (insn
);
156 insn
&& GET_CODE (insn
) == CODE_LABEL
;
157 insn
= next_nonnote_insn (insn
))
159 for (i
= 0; i
< n_basic_blocks
; i
++)
160 if (insn
== BLOCK_HEAD (i
))
167 /* Similar to next_insn, but ignores insns in the delay slots of
168 an annulled branch. */
171 next_insn_no_annul (insn
)
176 /* If INSN is an annulled branch, skip any insns from the target
178 if (INSN_ANNULLED_BRANCH_P (insn
)
179 && NEXT_INSN (PREV_INSN (insn
)) != insn
)
180 while (INSN_FROM_TARGET_P (NEXT_INSN (insn
)))
181 insn
= NEXT_INSN (insn
);
183 insn
= NEXT_INSN (insn
);
184 if (insn
&& GET_CODE (insn
) == INSN
185 && GET_CODE (PATTERN (insn
)) == SEQUENCE
)
186 insn
= XVECEXP (PATTERN (insn
), 0, 0);
192 /* Given X, some rtl, and RES, a pointer to a `struct resource', mark
193 which resources are referenced by the insn. If INCLUDE_DELAYED_EFFECTS
194 is TRUE, resources used by the called routine will be included for
198 mark_referenced_resources (x
, res
, include_delayed_effects
)
200 struct resources
*res
;
201 int include_delayed_effects
;
203 enum rtx_code code
= GET_CODE (x
);
206 const char *format_ptr
;
208 /* Handle leaf items for which we set resource flags. Also, special-case
209 CALL, SET and CLOBBER operators. */
222 if (GET_CODE (SUBREG_REG (x
)) != REG
)
223 mark_referenced_resources (SUBREG_REG (x
), res
, 0);
226 unsigned int regno
= subreg_regno (x
);
227 unsigned int last_regno
228 = regno
+ HARD_REGNO_NREGS (regno
, GET_MODE (x
));
230 if (last_regno
> FIRST_PSEUDO_REGISTER
)
232 for (r
= regno
; r
< last_regno
; r
++)
233 SET_HARD_REG_BIT (res
->regs
, r
);
239 unsigned int regno
= REGNO (x
);
240 unsigned int last_regno
241 = regno
+ HARD_REGNO_NREGS (regno
, GET_MODE (x
));
243 if (last_regno
> FIRST_PSEUDO_REGISTER
)
245 for (r
= regno
; r
< last_regno
; r
++)
246 SET_HARD_REG_BIT (res
->regs
, r
);
251 /* If this memory shouldn't change, it really isn't referencing
253 if (RTX_UNCHANGING_P (x
))
254 res
->unch_memory
= 1;
257 res
->volatil
|= MEM_VOLATILE_P (x
);
259 /* Mark registers used to access memory. */
260 mark_referenced_resources (XEXP (x
, 0), res
, 0);
267 case UNSPEC_VOLATILE
:
269 /* Traditional asm's are always volatile. */
278 res
->volatil
|= MEM_VOLATILE_P (x
);
280 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
281 We can not just fall through here since then we would be confused
282 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
283 traditional asms unlike their normal usage. */
285 for (i
= 0; i
< ASM_OPERANDS_INPUT_LENGTH (x
); i
++)
286 mark_referenced_resources (ASM_OPERANDS_INPUT (x
, i
), res
, 0);
290 /* The first operand will be a (MEM (xxx)) but doesn't really reference
291 memory. The second operand may be referenced, though. */
292 mark_referenced_resources (XEXP (XEXP (x
, 0), 0), res
, 0);
293 mark_referenced_resources (XEXP (x
, 1), res
, 0);
297 /* Usually, the first operand of SET is set, not referenced. But
298 registers used to access memory are referenced. SET_DEST is
299 also referenced if it is a ZERO_EXTRACT or SIGN_EXTRACT. */
301 mark_referenced_resources (SET_SRC (x
), res
, 0);
304 if (GET_CODE (x
) == SIGN_EXTRACT
305 || GET_CODE (x
) == ZERO_EXTRACT
306 || GET_CODE (x
) == STRICT_LOW_PART
)
307 mark_referenced_resources (x
, res
, 0);
308 else if (GET_CODE (x
) == SUBREG
)
310 if (GET_CODE (x
) == MEM
)
311 mark_referenced_resources (XEXP (x
, 0), res
, 0);
318 if (include_delayed_effects
)
320 /* A CALL references memory, the frame pointer if it exists, the
321 stack pointer, any global registers and any registers given in
322 USE insns immediately in front of the CALL.
324 However, we may have moved some of the parameter loading insns
325 into the delay slot of this CALL. If so, the USE's for them
326 don't count and should be skipped. */
327 rtx insn
= PREV_INSN (x
);
332 /* If we are part of a delay slot sequence, point at the SEQUENCE. */
333 if (NEXT_INSN (insn
) != x
)
335 sequence
= PATTERN (NEXT_INSN (insn
));
336 seq_size
= XVECLEN (sequence
, 0);
337 if (GET_CODE (sequence
) != SEQUENCE
)
342 SET_HARD_REG_BIT (res
->regs
, STACK_POINTER_REGNUM
);
343 if (frame_pointer_needed
)
345 SET_HARD_REG_BIT (res
->regs
, FRAME_POINTER_REGNUM
);
346 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
347 SET_HARD_REG_BIT (res
->regs
, HARD_FRAME_POINTER_REGNUM
);
351 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
353 SET_HARD_REG_BIT (res
->regs
, i
);
355 /* Check for a REG_SETJMP. If it exists, then we must
356 assume that this call can need any register.
358 This is done to be more conservative about how we handle setjmp.
359 We assume that they both use and set all registers. Using all
360 registers ensures that a register will not be considered dead
361 just because it crosses a setjmp call. A register should be
362 considered dead only if the setjmp call returns non-zero. */
363 if (find_reg_note (x
, REG_SETJMP
, NULL
))
364 SET_HARD_REG_SET (res
->regs
);
369 for (link
= CALL_INSN_FUNCTION_USAGE (x
);
371 link
= XEXP (link
, 1))
372 if (GET_CODE (XEXP (link
, 0)) == USE
)
374 for (i
= 1; i
< seq_size
; i
++)
376 rtx slot_pat
= PATTERN (XVECEXP (sequence
, 0, i
));
377 if (GET_CODE (slot_pat
) == SET
378 && rtx_equal_p (SET_DEST (slot_pat
),
379 XEXP (XEXP (link
, 0), 0)))
383 mark_referenced_resources (XEXP (XEXP (link
, 0), 0),
389 /* ... fall through to other INSN processing ... */
394 #ifdef INSN_REFERENCES_ARE_DELAYED
395 if (! include_delayed_effects
396 && INSN_REFERENCES_ARE_DELAYED (x
))
400 /* No special processing, just speed up. */
401 mark_referenced_resources (PATTERN (x
), res
, include_delayed_effects
);
408 /* Process each sub-expression and flag what it needs. */
409 format_ptr
= GET_RTX_FORMAT (code
);
410 for (i
= 0; i
< GET_RTX_LENGTH (code
); i
++)
411 switch (*format_ptr
++)
414 mark_referenced_resources (XEXP (x
, i
), res
, include_delayed_effects
);
418 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
419 mark_referenced_resources (XVECEXP (x
, i
, j
), res
,
420 include_delayed_effects
);
425 /* A subroutine of mark_target_live_regs. Search forward from TARGET
426 looking for registers that are set before they are used. These are dead.
427 Stop after passing a few conditional jumps, and/or a small
428 number of unconditional branches. */
431 find_dead_or_set_registers (target
, res
, jump_target
, jump_count
, set
, needed
)
433 struct resources
*res
;
436 struct resources set
, needed
;
438 HARD_REG_SET scratch
;
443 for (insn
= target
; insn
; insn
= next
)
445 rtx this_jump_insn
= insn
;
447 next
= NEXT_INSN (insn
);
449 /* If this instruction can throw an exception, then we don't
450 know where we might end up next. That means that we have to
451 assume that whatever we have already marked as live really is
453 if (can_throw_internal (insn
))
456 switch (GET_CODE (insn
))
459 /* After a label, any pending dead registers that weren't yet
460 used can be made dead. */
461 AND_COMPL_HARD_REG_SET (pending_dead_regs
, needed
.regs
);
462 AND_COMPL_HARD_REG_SET (res
->regs
, pending_dead_regs
);
463 CLEAR_HARD_REG_SET (pending_dead_regs
);
472 if (GET_CODE (PATTERN (insn
)) == USE
)
474 /* If INSN is a USE made by update_block, we care about the
475 underlying insn. Any registers set by the underlying insn
476 are live since the insn is being done somewhere else. */
477 if (INSN_P (XEXP (PATTERN (insn
), 0)))
478 mark_set_resources (XEXP (PATTERN (insn
), 0), res
, 0,
481 /* All other USE insns are to be ignored. */
484 else if (GET_CODE (PATTERN (insn
)) == CLOBBER
)
486 else if (GET_CODE (PATTERN (insn
)) == SEQUENCE
)
488 /* An unconditional jump can be used to fill the delay slot
489 of a call, so search for a JUMP_INSN in any position. */
490 for (i
= 0; i
< XVECLEN (PATTERN (insn
), 0); i
++)
492 this_jump_insn
= XVECEXP (PATTERN (insn
), 0, i
);
493 if (GET_CODE (this_jump_insn
) == JUMP_INSN
)
502 if (GET_CODE (this_jump_insn
) == JUMP_INSN
)
504 if (jump_count
++ < 10)
506 if (any_uncondjump_p (this_jump_insn
)
507 || GET_CODE (PATTERN (this_jump_insn
)) == RETURN
)
509 next
= JUMP_LABEL (this_jump_insn
);
514 *jump_target
= JUMP_LABEL (this_jump_insn
);
517 else if (any_condjump_p (this_jump_insn
))
519 struct resources target_set
, target_res
;
520 struct resources fallthrough_res
;
522 /* We can handle conditional branches here by following
523 both paths, and then IOR the results of the two paths
524 together, which will give us registers that are dead
525 on both paths. Since this is expensive, we give it
526 a much higher cost than unconditional branches. The
527 cost was chosen so that we will follow at most 1
528 conditional branch. */
531 if (jump_count
>= 10)
534 mark_referenced_resources (insn
, &needed
, 1);
536 /* For an annulled branch, mark_set_resources ignores slots
537 filled by instructions from the target. This is correct
538 if the branch is not taken. Since we are following both
539 paths from the branch, we must also compute correct info
540 if the branch is taken. We do this by inverting all of
541 the INSN_FROM_TARGET_P bits, calling mark_set_resources,
542 and then inverting the INSN_FROM_TARGET_P bits again. */
544 if (GET_CODE (PATTERN (insn
)) == SEQUENCE
545 && INSN_ANNULLED_BRANCH_P (this_jump_insn
))
547 for (i
= 1; i
< XVECLEN (PATTERN (insn
), 0); i
++)
548 INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn
), 0, i
))
549 = ! INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn
), 0, i
));
552 mark_set_resources (insn
, &target_set
, 0,
555 for (i
= 1; i
< XVECLEN (PATTERN (insn
), 0); i
++)
556 INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn
), 0, i
))
557 = ! INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn
), 0, i
));
559 mark_set_resources (insn
, &set
, 0, MARK_SRC_DEST_CALL
);
563 mark_set_resources (insn
, &set
, 0, MARK_SRC_DEST_CALL
);
568 COPY_HARD_REG_SET (scratch
, target_set
.regs
);
569 AND_COMPL_HARD_REG_SET (scratch
, needed
.regs
);
570 AND_COMPL_HARD_REG_SET (target_res
.regs
, scratch
);
572 fallthrough_res
= *res
;
573 COPY_HARD_REG_SET (scratch
, set
.regs
);
574 AND_COMPL_HARD_REG_SET (scratch
, needed
.regs
);
575 AND_COMPL_HARD_REG_SET (fallthrough_res
.regs
, scratch
);
577 find_dead_or_set_registers (JUMP_LABEL (this_jump_insn
),
578 &target_res
, 0, jump_count
,
580 find_dead_or_set_registers (next
,
581 &fallthrough_res
, 0, jump_count
,
583 IOR_HARD_REG_SET (fallthrough_res
.regs
, target_res
.regs
);
584 AND_HARD_REG_SET (res
->regs
, fallthrough_res
.regs
);
592 /* Don't try this optimization if we expired our jump count
593 above, since that would mean there may be an infinite loop
594 in the function being compiled. */
600 mark_referenced_resources (insn
, &needed
, 1);
601 mark_set_resources (insn
, &set
, 0, MARK_SRC_DEST_CALL
);
603 COPY_HARD_REG_SET (scratch
, set
.regs
);
604 AND_COMPL_HARD_REG_SET (scratch
, needed
.regs
);
605 AND_COMPL_HARD_REG_SET (res
->regs
, scratch
);
611 /* Given X, a part of an insn, and a pointer to a `struct resource',
612 RES, indicate which resources are modified by the insn. If
613 MARK_TYPE is MARK_SRC_DEST_CALL, also mark resources potentially
614 set by the called routine. If MARK_TYPE is MARK_DEST, only mark SET_DESTs
616 If IN_DEST is nonzero, it means we are inside a SET. Otherwise,
617 objects are being referenced instead of set.
619 We never mark the insn as modifying the condition code unless it explicitly
620 SETs CC0 even though this is not totally correct. The reason for this is
621 that we require a SET of CC0 to immediately precede the reference to CC0.
622 So if some other insn sets CC0 as a side-effect, we know it cannot affect
623 our computation and thus may be placed in a delay slot. */
626 mark_set_resources (x
, res
, in_dest
, mark_type
)
628 struct resources
*res
;
630 enum mark_resource_type mark_type
;
635 const char *format_ptr
;
654 /* These don't set any resources. */
663 /* Called routine modifies the condition code, memory, any registers
664 that aren't saved across calls, global registers and anything
665 explicitly CLOBBERed immediately after the CALL_INSN. */
667 if (mark_type
== MARK_SRC_DEST_CALL
)
671 res
->cc
= res
->memory
= 1;
672 for (r
= 0; r
< FIRST_PSEUDO_REGISTER
; r
++)
673 if (call_used_regs
[r
] || global_regs
[r
])
674 SET_HARD_REG_BIT (res
->regs
, r
);
676 for (link
= CALL_INSN_FUNCTION_USAGE (x
);
677 link
; link
= XEXP (link
, 1))
678 if (GET_CODE (XEXP (link
, 0)) == CLOBBER
)
679 mark_set_resources (SET_DEST (XEXP (link
, 0)), res
, 1,
682 /* Check for a REG_SETJMP. If it exists, then we must
683 assume that this call can clobber any register. */
684 if (find_reg_note (x
, REG_SETJMP
, NULL
))
685 SET_HARD_REG_SET (res
->regs
);
688 /* ... and also what its RTL says it modifies, if anything. */
693 /* An insn consisting of just a CLOBBER (or USE) is just for flow
694 and doesn't actually do anything, so we ignore it. */
696 #ifdef INSN_SETS_ARE_DELAYED
697 if (mark_type
!= MARK_SRC_DEST_CALL
698 && INSN_SETS_ARE_DELAYED (x
))
703 if (GET_CODE (x
) != USE
&& GET_CODE (x
) != CLOBBER
)
708 /* If the source of a SET is a CALL, this is actually done by
709 the called routine. So only include it if we are to include the
710 effects of the calling routine. */
712 mark_set_resources (SET_DEST (x
), res
,
713 (mark_type
== MARK_SRC_DEST_CALL
714 || GET_CODE (SET_SRC (x
)) != CALL
),
717 if (mark_type
!= MARK_DEST
)
718 mark_set_resources (SET_SRC (x
), res
, 0, MARK_SRC_DEST
);
722 mark_set_resources (XEXP (x
, 0), res
, 1, MARK_SRC_DEST
);
726 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
727 if (! (INSN_ANNULLED_BRANCH_P (XVECEXP (x
, 0, 0))
728 && INSN_FROM_TARGET_P (XVECEXP (x
, 0, i
))))
729 mark_set_resources (XVECEXP (x
, 0, i
), res
, 0, mark_type
);
736 mark_set_resources (XEXP (x
, 0), res
, 1, MARK_SRC_DEST
);
741 mark_set_resources (XEXP (x
, 0), res
, 1, MARK_SRC_DEST
);
742 mark_set_resources (XEXP (XEXP (x
, 1), 0), res
, 0, MARK_SRC_DEST
);
743 mark_set_resources (XEXP (XEXP (x
, 1), 1), res
, 0, MARK_SRC_DEST
);
748 if (! (mark_type
== MARK_DEST
&& in_dest
))
750 mark_set_resources (XEXP (x
, 0), res
, in_dest
, MARK_SRC_DEST
);
751 mark_set_resources (XEXP (x
, 1), res
, 0, MARK_SRC_DEST
);
752 mark_set_resources (XEXP (x
, 2), res
, 0, MARK_SRC_DEST
);
760 res
->unch_memory
|= RTX_UNCHANGING_P (x
);
761 res
->volatil
|= MEM_VOLATILE_P (x
);
764 mark_set_resources (XEXP (x
, 0), res
, 0, MARK_SRC_DEST
);
770 if (GET_CODE (SUBREG_REG (x
)) != REG
)
771 mark_set_resources (SUBREG_REG (x
), res
, in_dest
, mark_type
);
774 unsigned int regno
= subreg_regno (x
);
775 unsigned int last_regno
776 = regno
+ HARD_REGNO_NREGS (regno
, GET_MODE (x
));
778 if (last_regno
> FIRST_PSEUDO_REGISTER
)
780 for (r
= regno
; r
< last_regno
; r
++)
781 SET_HARD_REG_BIT (res
->regs
, r
);
789 unsigned int regno
= REGNO (x
);
790 unsigned int last_regno
791 = regno
+ HARD_REGNO_NREGS (regno
, GET_MODE (x
));
793 if (last_regno
> FIRST_PSEUDO_REGISTER
)
795 for (r
= regno
; r
< last_regno
; r
++)
796 SET_HARD_REG_BIT (res
->regs
, r
);
800 case STRICT_LOW_PART
:
801 if (! (mark_type
== MARK_DEST
&& in_dest
))
803 mark_set_resources (XEXP (x
, 0), res
, 0, MARK_SRC_DEST
);
807 case UNSPEC_VOLATILE
:
809 /* Traditional asm's are always volatile. */
818 res
->volatil
|= MEM_VOLATILE_P (x
);
820 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
821 We can not just fall through here since then we would be confused
822 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
823 traditional asms unlike their normal usage. */
825 for (i
= 0; i
< ASM_OPERANDS_INPUT_LENGTH (x
); i
++)
826 mark_set_resources (ASM_OPERANDS_INPUT (x
, i
), res
, in_dest
,
834 /* Process each sub-expression and flag what it needs. */
835 format_ptr
= GET_RTX_FORMAT (code
);
836 for (i
= 0; i
< GET_RTX_LENGTH (code
); i
++)
837 switch (*format_ptr
++)
840 mark_set_resources (XEXP (x
, i
), res
, in_dest
, mark_type
);
844 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
845 mark_set_resources (XVECEXP (x
, i
, j
), res
, in_dest
, mark_type
);
850 /* Set the resources that are live at TARGET.
852 If TARGET is zero, we refer to the end of the current function and can
853 return our precomputed value.
855 Otherwise, we try to find out what is live by consulting the basic block
856 information. This is tricky, because we must consider the actions of
857 reload and jump optimization, which occur after the basic block information
860 Accordingly, we proceed as follows::
862 We find the previous BARRIER and look at all immediately following labels
863 (with no intervening active insns) to see if any of them start a basic
864 block. If we hit the start of the function first, we use block 0.
866 Once we have found a basic block and a corresponding first insns, we can
867 accurately compute the live status from basic_block_live_regs and
868 reg_renumber. (By starting at a label following a BARRIER, we are immune
869 to actions taken by reload and jump.) Then we scan all insns between
870 that point and our target. For each CLOBBER (or for call-clobbered regs
871 when we pass a CALL_INSN), mark the appropriate registers are dead. For
872 a SET, mark them as live.
874 We have to be careful when using REG_DEAD notes because they are not
875 updated by such things as find_equiv_reg. So keep track of registers
876 marked as dead that haven't been assigned to, and mark them dead at the
877 next CODE_LABEL since reload and jump won't propagate values across labels.
879 If we cannot find the start of a basic block (should be a very rare
880 case, if it can happen at all), mark everything as potentially live.
882 Next, scan forward from TARGET looking for things set or clobbered
883 before they are used. These are not live.
885 Because we can be called many times on the same target, save our results
886 in a hash table indexed by INSN_UID. This is only done if the function
887 init_resource_info () was invoked before we are called. */
890 mark_target_live_regs (insns
, target
, res
)
893 struct resources
*res
;
897 struct target_info
*tinfo
= NULL
;
901 HARD_REG_SET scratch
;
902 struct resources set
, needed
;
904 /* Handle end of function. */
907 *res
= end_of_function_needs
;
911 /* We have to assume memory is needed, but the CC isn't. */
913 res
->volatil
= res
->unch_memory
= 0;
916 /* See if we have computed this value already. */
917 if (target_hash_table
!= NULL
)
919 for (tinfo
= target_hash_table
[INSN_UID (target
) % TARGET_HASH_PRIME
];
920 tinfo
; tinfo
= tinfo
->next
)
921 if (tinfo
->uid
== INSN_UID (target
))
924 /* Start by getting the basic block number. If we have saved
925 information, we can get it from there unless the insn at the
926 start of the basic block has been deleted. */
927 if (tinfo
&& tinfo
->block
!= -1
928 && ! INSN_DELETED_P (BLOCK_HEAD (tinfo
->block
)))
933 b
= find_basic_block (target
, MAX_DELAY_SLOT_LIVE_SEARCH
);
935 if (target_hash_table
!= NULL
)
939 /* If the information is up-to-date, use it. Otherwise, we will
941 if (b
== tinfo
->block
&& b
!= -1 && tinfo
->bb_tick
== bb_ticks
[b
])
943 COPY_HARD_REG_SET (res
->regs
, tinfo
->live_regs
);
949 /* Allocate a place to put our results and chain it into the
951 tinfo
= (struct target_info
*) xmalloc (sizeof (struct target_info
));
952 tinfo
->uid
= INSN_UID (target
);
955 = target_hash_table
[INSN_UID (target
) % TARGET_HASH_PRIME
];
956 target_hash_table
[INSN_UID (target
) % TARGET_HASH_PRIME
] = tinfo
;
960 CLEAR_HARD_REG_SET (pending_dead_regs
);
962 /* If we found a basic block, get the live registers from it and update
963 them with anything set or killed between its start and the insn before
964 TARGET. Otherwise, we must assume everything is live. */
967 regset regs_live
= BASIC_BLOCK (b
)->global_live_at_start
;
970 rtx start_insn
, stop_insn
;
972 /* Compute hard regs live at start of block -- this is the real hard regs
973 marked live, plus live pseudo regs that have been renumbered to
976 REG_SET_TO_HARD_REG_SET (current_live_regs
, regs_live
);
978 EXECUTE_IF_SET_IN_REG_SET
979 (regs_live
, FIRST_PSEUDO_REGISTER
, i
,
981 if (reg_renumber
[i
] >= 0)
983 regno
= reg_renumber
[i
];
985 j
< regno
+ HARD_REGNO_NREGS (regno
,
986 PSEUDO_REGNO_MODE (i
));
988 SET_HARD_REG_BIT (current_live_regs
, j
);
992 /* Get starting and ending insn, handling the case where each might
994 start_insn
= (b
== 0 ? insns
: BLOCK_HEAD (b
));
997 if (GET_CODE (start_insn
) == INSN
998 && GET_CODE (PATTERN (start_insn
)) == SEQUENCE
)
999 start_insn
= XVECEXP (PATTERN (start_insn
), 0, 0);
1001 if (GET_CODE (stop_insn
) == INSN
1002 && GET_CODE (PATTERN (stop_insn
)) == SEQUENCE
)
1003 stop_insn
= next_insn (PREV_INSN (stop_insn
));
1005 for (insn
= start_insn
; insn
!= stop_insn
;
1006 insn
= next_insn_no_annul (insn
))
1009 rtx real_insn
= insn
;
1011 /* If this insn is from the target of a branch, it isn't going to
1012 be used in the sequel. If it is used in both cases, this
1013 test will not be true. */
1014 if (INSN_FROM_TARGET_P (insn
))
1017 /* If this insn is a USE made by update_block, we care about the
1019 if (GET_CODE (insn
) == INSN
&& GET_CODE (PATTERN (insn
)) == USE
1020 && INSN_P (XEXP (PATTERN (insn
), 0)))
1021 real_insn
= XEXP (PATTERN (insn
), 0);
1023 if (GET_CODE (real_insn
) == CALL_INSN
)
1025 /* CALL clobbers all call-used regs that aren't fixed except
1026 sp, ap, and fp. Do this before setting the result of the
1028 AND_COMPL_HARD_REG_SET (current_live_regs
,
1029 regs_invalidated_by_call
);
1031 /* A CALL_INSN sets any global register live, since it may
1032 have been modified by the call. */
1033 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
1035 SET_HARD_REG_BIT (current_live_regs
, i
);
1038 /* Mark anything killed in an insn to be deadened at the next
1039 label. Ignore USE insns; the only REG_DEAD notes will be for
1040 parameters. But they might be early. A CALL_INSN will usually
1041 clobber registers used for parameters. It isn't worth bothering
1042 with the unlikely case when it won't. */
1043 if ((GET_CODE (real_insn
) == INSN
1044 && GET_CODE (PATTERN (real_insn
)) != USE
1045 && GET_CODE (PATTERN (real_insn
)) != CLOBBER
)
1046 || GET_CODE (real_insn
) == JUMP_INSN
1047 || GET_CODE (real_insn
) == CALL_INSN
)
1049 for (link
= REG_NOTES (real_insn
); link
; link
= XEXP (link
, 1))
1050 if (REG_NOTE_KIND (link
) == REG_DEAD
1051 && GET_CODE (XEXP (link
, 0)) == REG
1052 && REGNO (XEXP (link
, 0)) < FIRST_PSEUDO_REGISTER
)
1054 unsigned int first_regno
= REGNO (XEXP (link
, 0));
1055 unsigned int last_regno
1057 + HARD_REGNO_NREGS (first_regno
,
1058 GET_MODE (XEXP (link
, 0))));
1060 for (i
= first_regno
; i
< last_regno
; i
++)
1061 SET_HARD_REG_BIT (pending_dead_regs
, i
);
1064 note_stores (PATTERN (real_insn
), update_live_status
, NULL
);
1066 /* If any registers were unused after this insn, kill them.
1067 These notes will always be accurate. */
1068 for (link
= REG_NOTES (real_insn
); link
; link
= XEXP (link
, 1))
1069 if (REG_NOTE_KIND (link
) == REG_UNUSED
1070 && GET_CODE (XEXP (link
, 0)) == REG
1071 && REGNO (XEXP (link
, 0)) < FIRST_PSEUDO_REGISTER
)
1073 unsigned int first_regno
= REGNO (XEXP (link
, 0));
1074 unsigned int last_regno
1076 + HARD_REGNO_NREGS (first_regno
,
1077 GET_MODE (XEXP (link
, 0))));
1079 for (i
= first_regno
; i
< last_regno
; i
++)
1080 CLEAR_HARD_REG_BIT (current_live_regs
, i
);
1084 else if (GET_CODE (real_insn
) == CODE_LABEL
)
1086 /* A label clobbers the pending dead registers since neither
1087 reload nor jump will propagate a value across a label. */
1088 AND_COMPL_HARD_REG_SET (current_live_regs
, pending_dead_regs
);
1089 CLEAR_HARD_REG_SET (pending_dead_regs
);
1092 /* The beginning of the epilogue corresponds to the end of the
1093 RTL chain when there are no epilogue insns. Certain resources
1094 are implicitly required at that point. */
1095 else if (GET_CODE (real_insn
) == NOTE
1096 && NOTE_LINE_NUMBER (real_insn
) == NOTE_INSN_EPILOGUE_BEG
)
1097 IOR_HARD_REG_SET (current_live_regs
, start_of_epilogue_needs
.regs
);
1100 COPY_HARD_REG_SET (res
->regs
, current_live_regs
);
1104 tinfo
->bb_tick
= bb_ticks
[b
];
1108 /* We didn't find the start of a basic block. Assume everything
1109 in use. This should happen only extremely rarely. */
1110 SET_HARD_REG_SET (res
->regs
);
1112 CLEAR_RESOURCE (&set
);
1113 CLEAR_RESOURCE (&needed
);
1115 jump_insn
= find_dead_or_set_registers (target
, res
, &jump_target
, 0,
1118 /* If we hit an unconditional branch, we have another way of finding out
1119 what is live: we can see what is live at the branch target and include
1120 anything used but not set before the branch. We add the live
1121 resources found using the test below to those found until now. */
1125 struct resources new_resources
;
1126 rtx stop_insn
= next_active_insn (jump_insn
);
1128 mark_target_live_regs (insns
, next_active_insn (jump_target
),
1130 CLEAR_RESOURCE (&set
);
1131 CLEAR_RESOURCE (&needed
);
1133 /* Include JUMP_INSN in the needed registers. */
1134 for (insn
= target
; insn
!= stop_insn
; insn
= next_active_insn (insn
))
1136 mark_referenced_resources (insn
, &needed
, 1);
1138 COPY_HARD_REG_SET (scratch
, needed
.regs
);
1139 AND_COMPL_HARD_REG_SET (scratch
, set
.regs
);
1140 IOR_HARD_REG_SET (new_resources
.regs
, scratch
);
1142 mark_set_resources (insn
, &set
, 0, MARK_SRC_DEST_CALL
);
1145 IOR_HARD_REG_SET (res
->regs
, new_resources
.regs
);
1150 COPY_HARD_REG_SET (tinfo
->live_regs
, res
->regs
);
1154 /* Initialize the resources required by mark_target_live_regs ().
1155 This should be invoked before the first call to mark_target_live_regs. */
1158 init_resource_info (epilogue_insn
)
1163 /* Indicate what resources are required to be valid at the end of the current
1164 function. The condition code never is and memory always is. If the
1165 frame pointer is needed, it is and so is the stack pointer unless
1166 EXIT_IGNORE_STACK is non-zero. If the frame pointer is not needed, the
1167 stack pointer is. Registers used to return the function value are
1168 needed. Registers holding global variables are needed. */
1170 end_of_function_needs
.cc
= 0;
1171 end_of_function_needs
.memory
= 1;
1172 end_of_function_needs
.unch_memory
= 0;
1173 CLEAR_HARD_REG_SET (end_of_function_needs
.regs
);
1175 if (frame_pointer_needed
)
1177 SET_HARD_REG_BIT (end_of_function_needs
.regs
, FRAME_POINTER_REGNUM
);
1178 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1179 SET_HARD_REG_BIT (end_of_function_needs
.regs
, HARD_FRAME_POINTER_REGNUM
);
1181 #ifdef EXIT_IGNORE_STACK
1182 if (! EXIT_IGNORE_STACK
1183 || current_function_sp_is_unchanging
)
1185 SET_HARD_REG_BIT (end_of_function_needs
.regs
, STACK_POINTER_REGNUM
);
1188 SET_HARD_REG_BIT (end_of_function_needs
.regs
, STACK_POINTER_REGNUM
);
1190 if (current_function_return_rtx
!= 0)
1191 mark_referenced_resources (current_function_return_rtx
,
1192 &end_of_function_needs
, 1);
1194 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
1196 #ifdef EPILOGUE_USES
1197 || EPILOGUE_USES (i
)
1200 SET_HARD_REG_BIT (end_of_function_needs
.regs
, i
);
1202 /* The registers required to be live at the end of the function are
1203 represented in the flow information as being dead just prior to
1204 reaching the end of the function. For example, the return of a value
1205 might be represented by a USE of the return register immediately
1206 followed by an unconditional jump to the return label where the
1207 return label is the end of the RTL chain. The end of the RTL chain
1208 is then taken to mean that the return register is live.
1210 This sequence is no longer maintained when epilogue instructions are
1211 added to the RTL chain. To reconstruct the original meaning, the
1212 start of the epilogue (NOTE_INSN_EPILOGUE_BEG) is regarded as the
1213 point where these registers become live (start_of_epilogue_needs).
1214 If epilogue instructions are present, the registers set by those
1215 instructions won't have been processed by flow. Thus, those
1216 registers are additionally required at the end of the RTL chain
1217 (end_of_function_needs). */
1219 start_of_epilogue_needs
= end_of_function_needs
;
1221 while ((epilogue_insn
= next_nonnote_insn (epilogue_insn
)))
1222 mark_set_resources (epilogue_insn
, &end_of_function_needs
, 0,
1223 MARK_SRC_DEST_CALL
);
1225 /* Allocate and initialize the tables used by mark_target_live_regs. */
1226 target_hash_table
= (struct target_info
**)
1227 xcalloc (TARGET_HASH_PRIME
, sizeof (struct target_info
*));
1228 bb_ticks
= (int *) xcalloc (n_basic_blocks
, sizeof (int));
1231 /* Free up the resources allcated to mark_target_live_regs (). This
1232 should be invoked after the last call to mark_target_live_regs (). */
1235 free_resource_info ()
1237 if (target_hash_table
!= NULL
)
1241 for (i
= 0; i
< TARGET_HASH_PRIME
; ++i
)
1243 struct target_info
*ti
= target_hash_table
[i
];
1247 struct target_info
*next
= ti
->next
;
1253 free (target_hash_table
);
1254 target_hash_table
= NULL
;
1257 if (bb_ticks
!= NULL
)
1264 /* Clear any hashed information that we have stored for INSN. */
1267 clear_hashed_info_for_insn (insn
)
1270 struct target_info
*tinfo
;
1272 if (target_hash_table
!= NULL
)
1274 for (tinfo
= target_hash_table
[INSN_UID (insn
) % TARGET_HASH_PRIME
];
1275 tinfo
; tinfo
= tinfo
->next
)
1276 if (tinfo
->uid
== INSN_UID (insn
))
1284 /* Increment the tick count for the basic block that contains INSN. */
1287 incr_ticks_for_insn (insn
)
1290 int b
= find_basic_block (insn
, MAX_DELAY_SLOT_LIVE_SEARCH
);
1296 /* Add TRIAL to the set of resources used at the end of the current
1299 mark_end_of_function_resources (trial
, include_delayed_effects
)
1301 int include_delayed_effects
;
1303 mark_referenced_resources (trial
, &end_of_function_needs
,
1304 include_delayed_effects
);