* invoke.texi: Add more options to summary list.
[official-gcc.git] / gcc / resource.c
blob17f7d034d93dcff45044b39598dc25d36eb4c6eb
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 GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "toplev.h"
24 #include "rtl.h"
25 #include "tm_p.h"
26 #include "hard-reg-set.h"
27 #include "basic-block.h"
28 #include "function.h"
29 #include "regs.h"
30 #include "flags.h"
31 #include "output.h"
32 #include "resource.h"
33 #include "except.h"
34 #include "insn-attr.h"
35 #include "params.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
40 each time. */
42 struct target_info
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
65 number. */
67 static int *bb_ticks;
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,
84 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. */
89 static void
90 update_live_status (dest, x, data)
91 rtx dest;
92 rtx x;
93 void *data ATTRIBUTE_UNUSED;
95 int first_regno, last_regno;
96 int i;
98 if (GET_CODE (dest) != REG
99 && (GET_CODE (dest) != SUBREG || GET_CODE (SUBREG_REG (dest)) != REG))
100 return;
102 if (GET_CODE (dest) == SUBREG)
103 first_regno = subreg_regno (dest);
104 else
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);
112 else
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
124 an unlimited search.
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
129 correct. */
131 static int
132 find_basic_block (insn, search_limit)
133 rtx insn;
134 int search_limit;
136 int i;
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)
147 return -1;
149 /* The start of the function is basic block zero. */
150 else if (insn == 0)
151 return 0;
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))
161 return i;
164 return -1;
167 /* Similar to next_insn, but ignores insns in the delay slots of
168 an annulled branch. */
170 static rtx
171 next_insn_no_annul (insn)
172 rtx insn;
174 if (insn)
176 /* If INSN is an annulled branch, skip any insns from the target
177 of the branch. */
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);
189 return insn;
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
195 CALL_INSNs. */
197 void
198 mark_referenced_resources (x, res, include_delayed_effects)
199 register rtx x;
200 register struct resources *res;
201 register int include_delayed_effects;
203 enum rtx_code code = GET_CODE (x);
204 int i, j;
205 unsigned int r;
206 register const char *format_ptr;
208 /* Handle leaf items for which we set resource flags. Also, special-case
209 CALL, SET and CLOBBER operators. */
210 switch (code)
212 case CONST:
213 case CONST_INT:
214 case CONST_DOUBLE:
215 case PC:
216 case SYMBOL_REF:
217 case LABEL_REF:
218 return;
220 case SUBREG:
221 if (GET_CODE (SUBREG_REG (x)) != REG)
222 mark_referenced_resources (SUBREG_REG (x), res, 0);
223 else
225 unsigned int regno = subreg_regno (x);
226 unsigned int last_regno
227 = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
229 if (last_regno > FIRST_PSEUDO_REGISTER)
230 abort ();
231 for (r = regno; r < last_regno; r++)
232 SET_HARD_REG_BIT (res->regs, r);
234 return;
236 case REG:
238 unsigned int regno = REGNO (x);
239 unsigned int last_regno
240 = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
242 if (last_regno > FIRST_PSEUDO_REGISTER)
243 abort ();
244 for (r = regno; r < last_regno; r++)
245 SET_HARD_REG_BIT (res->regs, r);
247 return;
249 case MEM:
250 /* If this memory shouldn't change, it really isn't referencing
251 memory. */
252 if (RTX_UNCHANGING_P (x))
253 res->unch_memory = 1;
254 else
255 res->memory = 1;
256 res->volatil |= MEM_VOLATILE_P (x);
258 /* Mark registers used to access memory. */
259 mark_referenced_resources (XEXP (x, 0), res, 0);
260 return;
262 case CC0:
263 res->cc = 1;
264 return;
266 case UNSPEC_VOLATILE:
267 case ASM_INPUT:
268 /* Traditional asm's are always volatile. */
269 res->volatil = 1;
270 return;
272 case TRAP_IF:
273 res->volatil = 1;
274 break;
276 case ASM_OPERANDS:
277 res->volatil |= MEM_VOLATILE_P (x);
279 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
280 We can not just fall through here since then we would be confused
281 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
282 traditional asms unlike their normal usage. */
284 for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
285 mark_referenced_resources (ASM_OPERANDS_INPUT (x, i), res, 0);
286 return;
288 case CALL:
289 /* The first operand will be a (MEM (xxx)) but doesn't really reference
290 memory. The second operand may be referenced, though. */
291 mark_referenced_resources (XEXP (XEXP (x, 0), 0), res, 0);
292 mark_referenced_resources (XEXP (x, 1), res, 0);
293 return;
295 case SET:
296 /* Usually, the first operand of SET is set, not referenced. But
297 registers used to access memory are referenced. SET_DEST is
298 also referenced if it is a ZERO_EXTRACT or SIGN_EXTRACT. */
300 mark_referenced_resources (SET_SRC (x), res, 0);
302 x = SET_DEST (x);
303 if (GET_CODE (x) == SIGN_EXTRACT
304 || GET_CODE (x) == ZERO_EXTRACT
305 || GET_CODE (x) == STRICT_LOW_PART)
306 mark_referenced_resources (x, res, 0);
307 else if (GET_CODE (x) == SUBREG)
308 x = SUBREG_REG (x);
309 if (GET_CODE (x) == MEM)
310 mark_referenced_resources (XEXP (x, 0), res, 0);
311 return;
313 case CLOBBER:
314 return;
316 case CALL_INSN:
317 if (include_delayed_effects)
319 /* A CALL references memory, the frame pointer if it exists, the
320 stack pointer, any global registers and any registers given in
321 USE insns immediately in front of the CALL.
323 However, we may have moved some of the parameter loading insns
324 into the delay slot of this CALL. If so, the USE's for them
325 don't count and should be skipped. */
326 rtx insn = PREV_INSN (x);
327 rtx sequence = 0;
328 int seq_size = 0;
329 rtx next = NEXT_INSN (x);
330 int i;
332 /* If we are part of a delay slot sequence, point at the SEQUENCE. */
333 if (NEXT_INSN (insn) != x)
335 next = NEXT_INSN (NEXT_INSN (insn));
336 sequence = PATTERN (NEXT_INSN (insn));
337 seq_size = XVECLEN (sequence, 0);
338 if (GET_CODE (sequence) != SEQUENCE)
339 abort ();
342 res->memory = 1;
343 SET_HARD_REG_BIT (res->regs, STACK_POINTER_REGNUM);
344 if (frame_pointer_needed)
346 SET_HARD_REG_BIT (res->regs, FRAME_POINTER_REGNUM);
347 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
348 SET_HARD_REG_BIT (res->regs, HARD_FRAME_POINTER_REGNUM);
349 #endif
352 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
353 if (global_regs[i])
354 SET_HARD_REG_BIT (res->regs, i);
356 /* Check for a NOTE_INSN_SETJMP. If it exists, then we must
357 assume that this call can need any register.
359 This is done to be more conservative about how we handle setjmp.
360 We assume that they both use and set all registers. Using all
361 registers ensures that a register will not be considered dead
362 just because it crosses a setjmp call. A register should be
363 considered dead only if the setjmp call returns non-zero. */
364 if (next && GET_CODE (next) == NOTE
365 && NOTE_LINE_NUMBER (next) == NOTE_INSN_SETJMP)
366 SET_HARD_REG_SET (res->regs);
369 rtx link;
371 for (link = CALL_INSN_FUNCTION_USAGE (x);
372 link;
373 link = XEXP (link, 1))
374 if (GET_CODE (XEXP (link, 0)) == USE)
376 for (i = 1; i < seq_size; i++)
378 rtx slot_pat = PATTERN (XVECEXP (sequence, 0, i));
379 if (GET_CODE (slot_pat) == SET
380 && rtx_equal_p (SET_DEST (slot_pat),
381 XEXP (XEXP (link, 0), 0)))
382 break;
384 if (i >= seq_size)
385 mark_referenced_resources (XEXP (XEXP (link, 0), 0),
386 res, 0);
391 /* ... fall through to other INSN processing ... */
393 case INSN:
394 case JUMP_INSN:
396 #ifdef INSN_REFERENCES_ARE_DELAYED
397 if (! include_delayed_effects
398 && INSN_REFERENCES_ARE_DELAYED (x))
399 return;
400 #endif
402 /* No special processing, just speed up. */
403 mark_referenced_resources (PATTERN (x), res, include_delayed_effects);
404 return;
406 default:
407 break;
410 /* Process each sub-expression and flag what it needs. */
411 format_ptr = GET_RTX_FORMAT (code);
412 for (i = 0; i < GET_RTX_LENGTH (code); i++)
413 switch (*format_ptr++)
415 case 'e':
416 mark_referenced_resources (XEXP (x, i), res, include_delayed_effects);
417 break;
419 case 'E':
420 for (j = 0; j < XVECLEN (x, i); j++)
421 mark_referenced_resources (XVECEXP (x, i, j), res,
422 include_delayed_effects);
423 break;
427 /* A subroutine of mark_target_live_regs. Search forward from TARGET
428 looking for registers that are set before they are used. These are dead.
429 Stop after passing a few conditional jumps, and/or a small
430 number of unconditional branches. */
432 static rtx
433 find_dead_or_set_registers (target, res, jump_target, jump_count, set, needed)
434 rtx target;
435 struct resources *res;
436 rtx *jump_target;
437 int jump_count;
438 struct resources set, needed;
440 HARD_REG_SET scratch;
441 rtx insn, next;
442 rtx jump_insn = 0;
443 int i;
445 for (insn = target; insn; insn = next)
447 rtx this_jump_insn = insn;
449 next = NEXT_INSN (insn);
451 /* If this instruction can throw an exception, then we don't
452 know where we might end up next. That means that we have to
453 assume that whatever we have already marked as live really is
454 live. */
455 if (can_throw_internal (insn))
456 break;
458 switch (GET_CODE (insn))
460 case CODE_LABEL:
461 /* After a label, any pending dead registers that weren't yet
462 used can be made dead. */
463 AND_COMPL_HARD_REG_SET (pending_dead_regs, needed.regs);
464 AND_COMPL_HARD_REG_SET (res->regs, pending_dead_regs);
465 CLEAR_HARD_REG_SET (pending_dead_regs);
467 continue;
469 case BARRIER:
470 case NOTE:
471 continue;
473 case INSN:
474 if (GET_CODE (PATTERN (insn)) == USE)
476 /* If INSN is a USE made by update_block, we care about the
477 underlying insn. Any registers set by the underlying insn
478 are live since the insn is being done somewhere else. */
479 if (INSN_P (XEXP (PATTERN (insn), 0)))
480 mark_set_resources (XEXP (PATTERN (insn), 0), res, 0,
481 MARK_SRC_DEST_CALL);
483 /* All other USE insns are to be ignored. */
484 continue;
486 else if (GET_CODE (PATTERN (insn)) == CLOBBER)
487 continue;
488 else if (GET_CODE (PATTERN (insn)) == SEQUENCE)
490 /* An unconditional jump can be used to fill the delay slot
491 of a call, so search for a JUMP_INSN in any position. */
492 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
494 this_jump_insn = XVECEXP (PATTERN (insn), 0, i);
495 if (GET_CODE (this_jump_insn) == JUMP_INSN)
496 break;
500 default:
501 break;
504 if (GET_CODE (this_jump_insn) == JUMP_INSN)
506 if (jump_count++ < 10)
508 if (any_uncondjump_p (this_jump_insn)
509 || GET_CODE (PATTERN (this_jump_insn)) == RETURN)
511 next = JUMP_LABEL (this_jump_insn);
512 if (jump_insn == 0)
514 jump_insn = insn;
515 if (jump_target)
516 *jump_target = JUMP_LABEL (this_jump_insn);
519 else if (any_condjump_p (this_jump_insn))
521 struct resources target_set, target_res;
522 struct resources fallthrough_res;
524 /* We can handle conditional branches here by following
525 both paths, and then IOR the results of the two paths
526 together, which will give us registers that are dead
527 on both paths. Since this is expensive, we give it
528 a much higher cost than unconditional branches. The
529 cost was chosen so that we will follow at most 1
530 conditional branch. */
532 jump_count += 4;
533 if (jump_count >= 10)
534 break;
536 mark_referenced_resources (insn, &needed, 1);
538 /* For an annulled branch, mark_set_resources ignores slots
539 filled by instructions from the target. This is correct
540 if the branch is not taken. Since we are following both
541 paths from the branch, we must also compute correct info
542 if the branch is taken. We do this by inverting all of
543 the INSN_FROM_TARGET_P bits, calling mark_set_resources,
544 and then inverting the INSN_FROM_TARGET_P bits again. */
546 if (GET_CODE (PATTERN (insn)) == SEQUENCE
547 && INSN_ANNULLED_BRANCH_P (this_jump_insn))
549 for (i = 1; i < XVECLEN (PATTERN (insn), 0); i++)
550 INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn), 0, i))
551 = ! INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn), 0, i));
553 target_set = set;
554 mark_set_resources (insn, &target_set, 0,
555 MARK_SRC_DEST_CALL);
557 for (i = 1; i < XVECLEN (PATTERN (insn), 0); i++)
558 INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn), 0, i))
559 = ! INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn), 0, i));
561 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
563 else
565 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
566 target_set = set;
569 target_res = *res;
570 COPY_HARD_REG_SET (scratch, target_set.regs);
571 AND_COMPL_HARD_REG_SET (scratch, needed.regs);
572 AND_COMPL_HARD_REG_SET (target_res.regs, scratch);
574 fallthrough_res = *res;
575 COPY_HARD_REG_SET (scratch, set.regs);
576 AND_COMPL_HARD_REG_SET (scratch, needed.regs);
577 AND_COMPL_HARD_REG_SET (fallthrough_res.regs, scratch);
579 find_dead_or_set_registers (JUMP_LABEL (this_jump_insn),
580 &target_res, 0, jump_count,
581 target_set, needed);
582 find_dead_or_set_registers (next,
583 &fallthrough_res, 0, jump_count,
584 set, needed);
585 IOR_HARD_REG_SET (fallthrough_res.regs, target_res.regs);
586 AND_HARD_REG_SET (res->regs, fallthrough_res.regs);
587 break;
589 else
590 break;
592 else
594 /* Don't try this optimization if we expired our jump count
595 above, since that would mean there may be an infinite loop
596 in the function being compiled. */
597 jump_insn = 0;
598 break;
602 mark_referenced_resources (insn, &needed, 1);
603 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
605 COPY_HARD_REG_SET (scratch, set.regs);
606 AND_COMPL_HARD_REG_SET (scratch, needed.regs);
607 AND_COMPL_HARD_REG_SET (res->regs, scratch);
610 return jump_insn;
613 /* Given X, a part of an insn, and a pointer to a `struct resource',
614 RES, indicate which resources are modified by the insn. If
615 MARK_TYPE is MARK_SRC_DEST_CALL, also mark resources potentially
616 set by the called routine. If MARK_TYPE is MARK_DEST, only mark SET_DESTs
618 If IN_DEST is nonzero, it means we are inside a SET. Otherwise,
619 objects are being referenced instead of set.
621 We never mark the insn as modifying the condition code unless it explicitly
622 SETs CC0 even though this is not totally correct. The reason for this is
623 that we require a SET of CC0 to immediately precede the reference to CC0.
624 So if some other insn sets CC0 as a side-effect, we know it cannot affect
625 our computation and thus may be placed in a delay slot. */
627 void
628 mark_set_resources (x, res, in_dest, mark_type)
629 register rtx x;
630 register struct resources *res;
631 int in_dest;
632 enum mark_resource_type mark_type;
634 enum rtx_code code;
635 int i, j;
636 unsigned int r;
637 const char *format_ptr;
639 restart:
641 code = GET_CODE (x);
643 switch (code)
645 case NOTE:
646 case BARRIER:
647 case CODE_LABEL:
648 case USE:
649 case CONST_INT:
650 case CONST_DOUBLE:
651 case LABEL_REF:
652 case SYMBOL_REF:
653 case CONST:
654 case PC:
655 /* These don't set any resources. */
656 return;
658 case CC0:
659 if (in_dest)
660 res->cc = 1;
661 return;
663 case CALL_INSN:
664 /* Called routine modifies the condition code, memory, any registers
665 that aren't saved across calls, global registers and anything
666 explicitly CLOBBERed immediately after the CALL_INSN. */
668 if (mark_type == MARK_SRC_DEST_CALL)
670 rtx next = NEXT_INSN (x);
671 rtx prev = PREV_INSN (x);
672 rtx link;
674 res->cc = res->memory = 1;
675 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
676 if (call_used_regs[r] || global_regs[r])
677 SET_HARD_REG_BIT (res->regs, r);
679 /* If X is part of a delay slot sequence, then NEXT should be
680 the first insn after the sequence. */
681 if (NEXT_INSN (prev) != x)
682 next = NEXT_INSN (NEXT_INSN (prev));
684 for (link = CALL_INSN_FUNCTION_USAGE (x);
685 link; link = XEXP (link, 1))
686 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
687 mark_set_resources (SET_DEST (XEXP (link, 0)), res, 1,
688 MARK_SRC_DEST);
690 /* Check for a NOTE_INSN_SETJMP. If it exists, then we must
691 assume that this call can clobber any register. */
692 if (next && GET_CODE (next) == NOTE
693 && NOTE_LINE_NUMBER (next) == NOTE_INSN_SETJMP)
694 SET_HARD_REG_SET (res->regs);
697 /* ... and also what its RTL says it modifies, if anything. */
699 case JUMP_INSN:
700 case INSN:
702 /* An insn consisting of just a CLOBBER (or USE) is just for flow
703 and doesn't actually do anything, so we ignore it. */
705 #ifdef INSN_SETS_ARE_DELAYED
706 if (mark_type != MARK_SRC_DEST_CALL
707 && INSN_SETS_ARE_DELAYED (x))
708 return;
709 #endif
711 x = PATTERN (x);
712 if (GET_CODE (x) != USE && GET_CODE (x) != CLOBBER)
713 goto restart;
714 return;
716 case SET:
717 /* If the source of a SET is a CALL, this is actually done by
718 the called routine. So only include it if we are to include the
719 effects of the calling routine. */
721 mark_set_resources (SET_DEST (x), res,
722 (mark_type == MARK_SRC_DEST_CALL
723 || GET_CODE (SET_SRC (x)) != CALL),
724 mark_type);
726 if (mark_type != MARK_DEST)
727 mark_set_resources (SET_SRC (x), res, 0, MARK_SRC_DEST);
728 return;
730 case CLOBBER:
731 mark_set_resources (XEXP (x, 0), res, 1, MARK_SRC_DEST);
732 return;
734 case SEQUENCE:
735 for (i = 0; i < XVECLEN (x, 0); i++)
736 if (! (INSN_ANNULLED_BRANCH_P (XVECEXP (x, 0, 0))
737 && INSN_FROM_TARGET_P (XVECEXP (x, 0, i))))
738 mark_set_resources (XVECEXP (x, 0, i), res, 0, mark_type);
739 return;
741 case POST_INC:
742 case PRE_INC:
743 case POST_DEC:
744 case PRE_DEC:
745 mark_set_resources (XEXP (x, 0), res, 1, MARK_SRC_DEST);
746 return;
748 case PRE_MODIFY:
749 case POST_MODIFY:
750 mark_set_resources (XEXP (x, 0), res, 1, MARK_SRC_DEST);
751 mark_set_resources (XEXP (XEXP (x, 1), 0), res, 0, MARK_SRC_DEST);
752 mark_set_resources (XEXP (XEXP (x, 1), 1), res, 0, MARK_SRC_DEST);
753 return;
755 case SIGN_EXTRACT:
756 case ZERO_EXTRACT:
757 if (! (mark_type == MARK_DEST && in_dest))
759 mark_set_resources (XEXP (x, 0), res, in_dest, MARK_SRC_DEST);
760 mark_set_resources (XEXP (x, 1), res, 0, MARK_SRC_DEST);
761 mark_set_resources (XEXP (x, 2), res, 0, MARK_SRC_DEST);
763 return;
765 case MEM:
766 if (in_dest)
768 res->memory = 1;
769 res->unch_memory |= RTX_UNCHANGING_P (x);
770 res->volatil |= MEM_VOLATILE_P (x);
773 mark_set_resources (XEXP (x, 0), res, 0, MARK_SRC_DEST);
774 return;
776 case SUBREG:
777 if (in_dest)
779 if (GET_CODE (SUBREG_REG (x)) != REG)
780 mark_set_resources (SUBREG_REG (x), res, in_dest, mark_type);
781 else
783 unsigned int regno = subreg_regno (x);
784 unsigned int last_regno
785 = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
787 if (last_regno > FIRST_PSEUDO_REGISTER)
788 abort ();
789 for (r = regno; r < last_regno; r++)
790 SET_HARD_REG_BIT (res->regs, r);
793 return;
795 case REG:
796 if (in_dest)
798 unsigned int regno = REGNO (x);
799 unsigned int last_regno
800 = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
802 if (last_regno > FIRST_PSEUDO_REGISTER)
803 abort ();
804 for (r = regno; r < last_regno; r++)
805 SET_HARD_REG_BIT (res->regs, r);
807 return;
809 case STRICT_LOW_PART:
810 if (! (mark_type == MARK_DEST && in_dest))
812 mark_set_resources (XEXP (x, 0), res, 0, MARK_SRC_DEST);
813 return;
816 case UNSPEC_VOLATILE:
817 case ASM_INPUT:
818 /* Traditional asm's are always volatile. */
819 res->volatil = 1;
820 return;
822 case TRAP_IF:
823 res->volatil = 1;
824 break;
826 case ASM_OPERANDS:
827 res->volatil |= MEM_VOLATILE_P (x);
829 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
830 We can not just fall through here since then we would be confused
831 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
832 traditional asms unlike their normal usage. */
834 for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
835 mark_set_resources (ASM_OPERANDS_INPUT (x, i), res, in_dest,
836 MARK_SRC_DEST);
837 return;
839 default:
840 break;
843 /* Process each sub-expression and flag what it needs. */
844 format_ptr = GET_RTX_FORMAT (code);
845 for (i = 0; i < GET_RTX_LENGTH (code); i++)
846 switch (*format_ptr++)
848 case 'e':
849 mark_set_resources (XEXP (x, i), res, in_dest, mark_type);
850 break;
852 case 'E':
853 for (j = 0; j < XVECLEN (x, i); j++)
854 mark_set_resources (XVECEXP (x, i, j), res, in_dest, mark_type);
855 break;
859 /* Set the resources that are live at TARGET.
861 If TARGET is zero, we refer to the end of the current function and can
862 return our precomputed value.
864 Otherwise, we try to find out what is live by consulting the basic block
865 information. This is tricky, because we must consider the actions of
866 reload and jump optimization, which occur after the basic block information
867 has been computed.
869 Accordingly, we proceed as follows::
871 We find the previous BARRIER and look at all immediately following labels
872 (with no intervening active insns) to see if any of them start a basic
873 block. If we hit the start of the function first, we use block 0.
875 Once we have found a basic block and a corresponding first insns, we can
876 accurately compute the live status from basic_block_live_regs and
877 reg_renumber. (By starting at a label following a BARRIER, we are immune
878 to actions taken by reload and jump.) Then we scan all insns between
879 that point and our target. For each CLOBBER (or for call-clobbered regs
880 when we pass a CALL_INSN), mark the appropriate registers are dead. For
881 a SET, mark them as live.
883 We have to be careful when using REG_DEAD notes because they are not
884 updated by such things as find_equiv_reg. So keep track of registers
885 marked as dead that haven't been assigned to, and mark them dead at the
886 next CODE_LABEL since reload and jump won't propagate values across labels.
888 If we cannot find the start of a basic block (should be a very rare
889 case, if it can happen at all), mark everything as potentially live.
891 Next, scan forward from TARGET looking for things set or clobbered
892 before they are used. These are not live.
894 Because we can be called many times on the same target, save our results
895 in a hash table indexed by INSN_UID. This is only done if the function
896 init_resource_info () was invoked before we are called. */
898 void
899 mark_target_live_regs (insns, target, res)
900 rtx insns;
901 rtx target;
902 struct resources *res;
904 int b = -1;
905 int i;
906 struct target_info *tinfo = NULL;
907 rtx insn;
908 rtx jump_insn = 0;
909 rtx jump_target;
910 HARD_REG_SET scratch;
911 struct resources set, needed;
913 /* Handle end of function. */
914 if (target == 0)
916 *res = end_of_function_needs;
917 return;
920 /* We have to assume memory is needed, but the CC isn't. */
921 res->memory = 1;
922 res->volatil = res->unch_memory = 0;
923 res->cc = 0;
925 /* See if we have computed this value already. */
926 if (target_hash_table != NULL)
928 for (tinfo = target_hash_table[INSN_UID (target) % TARGET_HASH_PRIME];
929 tinfo; tinfo = tinfo->next)
930 if (tinfo->uid == INSN_UID (target))
931 break;
933 /* Start by getting the basic block number. If we have saved
934 information, we can get it from there unless the insn at the
935 start of the basic block has been deleted. */
936 if (tinfo && tinfo->block != -1
937 && ! INSN_DELETED_P (BLOCK_HEAD (tinfo->block)))
938 b = tinfo->block;
941 if (b == -1)
942 b = find_basic_block (target, MAX_DELAY_SLOT_LIVE_SEARCH);
944 if (target_hash_table != NULL)
946 if (tinfo)
948 /* If the information is up-to-date, use it. Otherwise, we will
949 update it below. */
950 if (b == tinfo->block && b != -1 && tinfo->bb_tick == bb_ticks[b])
952 COPY_HARD_REG_SET (res->regs, tinfo->live_regs);
953 return;
956 else
958 /* Allocate a place to put our results and chain it into the
959 hash table. */
960 tinfo = (struct target_info *) xmalloc (sizeof (struct target_info));
961 tinfo->uid = INSN_UID (target);
962 tinfo->block = b;
963 tinfo->next = target_hash_table[INSN_UID (target) % TARGET_HASH_PRIME];
964 target_hash_table[INSN_UID (target) % TARGET_HASH_PRIME] = tinfo;
968 CLEAR_HARD_REG_SET (pending_dead_regs);
970 /* If we found a basic block, get the live registers from it and update
971 them with anything set or killed between its start and the insn before
972 TARGET. Otherwise, we must assume everything is live. */
973 if (b != -1)
975 regset regs_live = BASIC_BLOCK (b)->global_live_at_start;
976 unsigned int j;
977 unsigned int regno;
978 rtx start_insn, stop_insn;
980 /* Compute hard regs live at start of block -- this is the real hard regs
981 marked live, plus live pseudo regs that have been renumbered to
982 hard regs. */
984 REG_SET_TO_HARD_REG_SET (current_live_regs, regs_live);
986 EXECUTE_IF_SET_IN_REG_SET
987 (regs_live, FIRST_PSEUDO_REGISTER, i,
989 if (reg_renumber[i] >= 0)
991 regno = reg_renumber[i];
992 for (j = regno;
993 j < regno + HARD_REGNO_NREGS (regno,
994 PSEUDO_REGNO_MODE (i));
995 j++)
996 SET_HARD_REG_BIT (current_live_regs, j);
1000 /* Get starting and ending insn, handling the case where each might
1001 be a SEQUENCE. */
1002 start_insn = (b == 0 ? insns : BLOCK_HEAD (b));
1003 stop_insn = target;
1005 if (GET_CODE (start_insn) == INSN
1006 && GET_CODE (PATTERN (start_insn)) == SEQUENCE)
1007 start_insn = XVECEXP (PATTERN (start_insn), 0, 0);
1009 if (GET_CODE (stop_insn) == INSN
1010 && GET_CODE (PATTERN (stop_insn)) == SEQUENCE)
1011 stop_insn = next_insn (PREV_INSN (stop_insn));
1013 for (insn = start_insn; insn != stop_insn;
1014 insn = next_insn_no_annul (insn))
1016 rtx link;
1017 rtx real_insn = insn;
1019 /* If this insn is from the target of a branch, it isn't going to
1020 be used in the sequel. If it is used in both cases, this
1021 test will not be true. */
1022 if (INSN_FROM_TARGET_P (insn))
1023 continue;
1025 /* If this insn is a USE made by update_block, we care about the
1026 underlying insn. */
1027 if (GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE
1028 && INSN_P (XEXP (PATTERN (insn), 0)))
1029 real_insn = XEXP (PATTERN (insn), 0);
1031 if (GET_CODE (real_insn) == CALL_INSN)
1033 /* CALL clobbers all call-used regs that aren't fixed except
1034 sp, ap, and fp. Do this before setting the result of the
1035 call live. */
1036 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1037 if (call_used_regs[i]
1038 && i != STACK_POINTER_REGNUM && i != FRAME_POINTER_REGNUM
1039 && i != ARG_POINTER_REGNUM
1040 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1041 && i != HARD_FRAME_POINTER_REGNUM
1042 #endif
1043 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1044 && ! (i == ARG_POINTER_REGNUM && fixed_regs[i])
1045 #endif
1046 #if !defined (PIC_OFFSET_TABLE_REG_CALL_CLOBBERED)
1047 && ! (i == PIC_OFFSET_TABLE_REGNUM && flag_pic)
1048 #endif
1050 CLEAR_HARD_REG_BIT (current_live_regs, i);
1052 /* A CALL_INSN sets any global register live, since it may
1053 have been modified by the call. */
1054 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1055 if (global_regs[i])
1056 SET_HARD_REG_BIT (current_live_regs, i);
1059 /* Mark anything killed in an insn to be deadened at the next
1060 label. Ignore USE insns; the only REG_DEAD notes will be for
1061 parameters. But they might be early. A CALL_INSN will usually
1062 clobber registers used for parameters. It isn't worth bothering
1063 with the unlikely case when it won't. */
1064 if ((GET_CODE (real_insn) == INSN
1065 && GET_CODE (PATTERN (real_insn)) != USE
1066 && GET_CODE (PATTERN (real_insn)) != CLOBBER)
1067 || GET_CODE (real_insn) == JUMP_INSN
1068 || GET_CODE (real_insn) == CALL_INSN)
1070 for (link = REG_NOTES (real_insn); link; link = XEXP (link, 1))
1071 if (REG_NOTE_KIND (link) == REG_DEAD
1072 && GET_CODE (XEXP (link, 0)) == REG
1073 && REGNO (XEXP (link, 0)) < FIRST_PSEUDO_REGISTER)
1075 int first_regno = REGNO (XEXP (link, 0));
1076 int last_regno
1077 = (first_regno
1078 + HARD_REGNO_NREGS (first_regno,
1079 GET_MODE (XEXP (link, 0))));
1081 for (i = first_regno; i < last_regno; i++)
1082 SET_HARD_REG_BIT (pending_dead_regs, i);
1085 note_stores (PATTERN (real_insn), update_live_status, NULL);
1087 /* If any registers were unused after this insn, kill them.
1088 These notes will always be accurate. */
1089 for (link = REG_NOTES (real_insn); link; link = XEXP (link, 1))
1090 if (REG_NOTE_KIND (link) == REG_UNUSED
1091 && GET_CODE (XEXP (link, 0)) == REG
1092 && REGNO (XEXP (link, 0)) < FIRST_PSEUDO_REGISTER)
1094 int first_regno = REGNO (XEXP (link, 0));
1095 int last_regno
1096 = (first_regno
1097 + HARD_REGNO_NREGS (first_regno,
1098 GET_MODE (XEXP (link, 0))));
1100 for (i = first_regno; i < last_regno; i++)
1101 CLEAR_HARD_REG_BIT (current_live_regs, i);
1105 else if (GET_CODE (real_insn) == CODE_LABEL)
1107 /* A label clobbers the pending dead registers since neither
1108 reload nor jump will propagate a value across a label. */
1109 AND_COMPL_HARD_REG_SET (current_live_regs, pending_dead_regs);
1110 CLEAR_HARD_REG_SET (pending_dead_regs);
1113 /* The beginning of the epilogue corresponds to the end of the
1114 RTL chain when there are no epilogue insns. Certain resources
1115 are implicitly required at that point. */
1116 else if (GET_CODE (real_insn) == NOTE
1117 && NOTE_LINE_NUMBER (real_insn) == NOTE_INSN_EPILOGUE_BEG)
1118 IOR_HARD_REG_SET (current_live_regs, start_of_epilogue_needs.regs);
1121 COPY_HARD_REG_SET (res->regs, current_live_regs);
1122 if (tinfo != NULL)
1124 tinfo->block = b;
1125 tinfo->bb_tick = bb_ticks[b];
1128 else
1129 /* We didn't find the start of a basic block. Assume everything
1130 in use. This should happen only extremely rarely. */
1131 SET_HARD_REG_SET (res->regs);
1133 CLEAR_RESOURCE (&set);
1134 CLEAR_RESOURCE (&needed);
1136 jump_insn = find_dead_or_set_registers (target, res, &jump_target, 0,
1137 set, needed);
1139 /* If we hit an unconditional branch, we have another way of finding out
1140 what is live: we can see what is live at the branch target and include
1141 anything used but not set before the branch. We add the live
1142 resources found using the test below to those found until now. */
1144 if (jump_insn)
1146 struct resources new_resources;
1147 rtx stop_insn = next_active_insn (jump_insn);
1149 mark_target_live_regs (insns, next_active_insn (jump_target),
1150 &new_resources);
1151 CLEAR_RESOURCE (&set);
1152 CLEAR_RESOURCE (&needed);
1154 /* Include JUMP_INSN in the needed registers. */
1155 for (insn = target; insn != stop_insn; insn = next_active_insn (insn))
1157 mark_referenced_resources (insn, &needed, 1);
1159 COPY_HARD_REG_SET (scratch, needed.regs);
1160 AND_COMPL_HARD_REG_SET (scratch, set.regs);
1161 IOR_HARD_REG_SET (new_resources.regs, scratch);
1163 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
1166 IOR_HARD_REG_SET (res->regs, new_resources.regs);
1169 if (tinfo != NULL)
1171 COPY_HARD_REG_SET (tinfo->live_regs, res->regs);
1175 /* Initialize the resources required by mark_target_live_regs ().
1176 This should be invoked before the first call to mark_target_live_regs. */
1178 void
1179 init_resource_info (epilogue_insn)
1180 rtx epilogue_insn;
1182 int i;
1184 /* Indicate what resources are required to be valid at the end of the current
1185 function. The condition code never is and memory always is. If the
1186 frame pointer is needed, it is and so is the stack pointer unless
1187 EXIT_IGNORE_STACK is non-zero. If the frame pointer is not needed, the
1188 stack pointer is. Registers used to return the function value are
1189 needed. Registers holding global variables are needed. */
1191 end_of_function_needs.cc = 0;
1192 end_of_function_needs.memory = 1;
1193 end_of_function_needs.unch_memory = 0;
1194 CLEAR_HARD_REG_SET (end_of_function_needs.regs);
1196 if (frame_pointer_needed)
1198 SET_HARD_REG_BIT (end_of_function_needs.regs, FRAME_POINTER_REGNUM);
1199 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1200 SET_HARD_REG_BIT (end_of_function_needs.regs, HARD_FRAME_POINTER_REGNUM);
1201 #endif
1202 #ifdef EXIT_IGNORE_STACK
1203 if (! EXIT_IGNORE_STACK
1204 || current_function_sp_is_unchanging)
1205 #endif
1206 SET_HARD_REG_BIT (end_of_function_needs.regs, STACK_POINTER_REGNUM);
1208 else
1209 SET_HARD_REG_BIT (end_of_function_needs.regs, STACK_POINTER_REGNUM);
1211 if (current_function_return_rtx != 0)
1212 mark_referenced_resources (current_function_return_rtx,
1213 &end_of_function_needs, 1);
1215 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1216 if (global_regs[i]
1217 #ifdef EPILOGUE_USES
1218 || EPILOGUE_USES (i)
1219 #endif
1221 SET_HARD_REG_BIT (end_of_function_needs.regs, i);
1223 /* The registers required to be live at the end of the function are
1224 represented in the flow information as being dead just prior to
1225 reaching the end of the function. For example, the return of a value
1226 might be represented by a USE of the return register immediately
1227 followed by an unconditional jump to the return label where the
1228 return label is the end of the RTL chain. The end of the RTL chain
1229 is then taken to mean that the return register is live.
1231 This sequence is no longer maintained when epilogue instructions are
1232 added to the RTL chain. To reconstruct the original meaning, the
1233 start of the epilogue (NOTE_INSN_EPILOGUE_BEG) is regarded as the
1234 point where these registers become live (start_of_epilogue_needs).
1235 If epilogue instructions are present, the registers set by those
1236 instructions won't have been processed by flow. Thus, those
1237 registers are additionally required at the end of the RTL chain
1238 (end_of_function_needs). */
1240 start_of_epilogue_needs = end_of_function_needs;
1242 while ((epilogue_insn = next_nonnote_insn (epilogue_insn)))
1243 mark_set_resources (epilogue_insn, &end_of_function_needs, 0,
1244 MARK_SRC_DEST_CALL);
1246 /* Allocate and initialize the tables used by mark_target_live_regs. */
1247 target_hash_table = (struct target_info **)
1248 xcalloc (TARGET_HASH_PRIME, sizeof (struct target_info *));
1249 bb_ticks = (int *) xcalloc (n_basic_blocks, sizeof (int));
1252 /* Free up the resources allcated to mark_target_live_regs (). This
1253 should be invoked after the last call to mark_target_live_regs (). */
1255 void
1256 free_resource_info ()
1258 if (target_hash_table != NULL)
1260 int i;
1262 for (i = 0; i < TARGET_HASH_PRIME; ++i)
1264 struct target_info *ti = target_hash_table[i];
1266 while (ti)
1268 struct target_info *next = ti->next;
1269 free (ti);
1270 ti = next;
1274 free (target_hash_table);
1275 target_hash_table = NULL;
1278 if (bb_ticks != NULL)
1280 free (bb_ticks);
1281 bb_ticks = NULL;
1285 /* Clear any hashed information that we have stored for INSN. */
1287 void
1288 clear_hashed_info_for_insn (insn)
1289 rtx insn;
1291 struct target_info *tinfo;
1293 if (target_hash_table != NULL)
1295 for (tinfo = target_hash_table[INSN_UID (insn) % TARGET_HASH_PRIME];
1296 tinfo; tinfo = tinfo->next)
1297 if (tinfo->uid == INSN_UID (insn))
1298 break;
1300 if (tinfo)
1301 tinfo->block = -1;
1305 /* Increment the tick count for the basic block that contains INSN. */
1307 void
1308 incr_ticks_for_insn (insn)
1309 rtx insn;
1311 int b = find_basic_block (insn, MAX_DELAY_SLOT_LIVE_SEARCH);
1313 if (b != -1)
1314 bb_ticks[b]++;
1317 /* Add TRIAL to the set of resources used at the end of the current
1318 function. */
1319 void
1320 mark_end_of_function_resources (trial, include_delayed_effects)
1321 rtx trial;
1322 int include_delayed_effects;
1324 mark_referenced_resources (trial, &end_of_function_needs,
1325 include_delayed_effects);