Missed one in last change.
[official-gcc.git] / gcc / resource.c
blobf5424602f74fc1c1e2dd163de1f7538a72971794
1 /* Definitions for computing resource usage of specific insns.
2 Copyright (C) 1999, 2000, 2001, 2002 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
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 COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "toplev.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "hard-reg-set.h"
29 #include "basic-block.h"
30 #include "function.h"
31 #include "regs.h"
32 #include "flags.h"
33 #include "output.h"
34 #include "resource.h"
35 #include "except.h"
36 #include "insn-attr.h"
37 #include "params.h"
39 /* This structure is used to record liveness information at the targets or
40 fallthrough insns of branches. We will most likely need the information
41 at targets again, so save them in a hash table rather than recomputing them
42 each time. */
44 struct target_info
46 int uid; /* INSN_UID of target. */
47 struct target_info *next; /* Next info for same hash bucket. */
48 HARD_REG_SET live_regs; /* Registers live at target. */
49 int block; /* Basic block number containing target. */
50 int bb_tick; /* Generation count of basic block info. */
53 #define TARGET_HASH_PRIME 257
55 /* Indicates what resources are required at the beginning of the epilogue. */
56 static struct resources start_of_epilogue_needs;
58 /* Indicates what resources are required at function end. */
59 static struct resources end_of_function_needs;
61 /* Define the hash table itself. */
62 static struct target_info **target_hash_table = NULL;
64 /* For each basic block, we maintain a generation number of its basic
65 block info, which is updated each time we move an insn from the
66 target of a jump. This is the generation number indexed by block
67 number. */
69 static int *bb_ticks;
71 /* Marks registers possibly live at the current place being scanned by
72 mark_target_live_regs. Also used by update_live_status. */
74 static HARD_REG_SET current_live_regs;
76 /* Marks registers for which we have seen a REG_DEAD note but no assignment.
77 Also only used by the next two functions. */
79 static HARD_REG_SET pending_dead_regs;
81 static void update_live_status PARAMS ((rtx, rtx, void *));
82 static int find_basic_block PARAMS ((rtx, int));
83 static rtx next_insn_no_annul PARAMS ((rtx));
84 static rtx find_dead_or_set_registers PARAMS ((rtx, struct resources*,
85 rtx*, int, struct resources,
86 struct resources));
88 /* Utility function called from mark_target_live_regs via note_stores.
89 It deadens any CLOBBERed registers and livens any SET registers. */
91 static void
92 update_live_status (dest, x, data)
93 rtx dest;
94 rtx x;
95 void *data ATTRIBUTE_UNUSED;
97 int first_regno, last_regno;
98 int i;
100 if (GET_CODE (dest) != REG
101 && (GET_CODE (dest) != SUBREG || GET_CODE (SUBREG_REG (dest)) != REG))
102 return;
104 if (GET_CODE (dest) == SUBREG)
105 first_regno = subreg_regno (dest);
106 else
107 first_regno = REGNO (dest);
109 last_regno = first_regno + HARD_REGNO_NREGS (first_regno, GET_MODE (dest));
111 if (GET_CODE (x) == CLOBBER)
112 for (i = first_regno; i < last_regno; i++)
113 CLEAR_HARD_REG_BIT (current_live_regs, i);
114 else
115 for (i = first_regno; i < last_regno; i++)
117 SET_HARD_REG_BIT (current_live_regs, i);
118 CLEAR_HARD_REG_BIT (pending_dead_regs, i);
122 /* Find the number of the basic block with correct live register
123 information that starts closest to INSN. Return -1 if we couldn't
124 find such a basic block or the beginning is more than
125 SEARCH_LIMIT instructions before INSN. Use SEARCH_LIMIT = -1 for
126 an unlimited search.
128 The delay slot filling code destroys the control-flow graph so,
129 instead of finding the basic block containing INSN, we search
130 backwards toward a BARRIER where the live register information is
131 correct. */
133 static int
134 find_basic_block (insn, search_limit)
135 rtx insn;
136 int search_limit;
138 basic_block bb;
140 /* Scan backwards to the previous BARRIER. Then see if we can find a
141 label that starts a basic block. Return the basic block number. */
142 for (insn = prev_nonnote_insn (insn);
143 insn && GET_CODE (insn) != BARRIER && search_limit != 0;
144 insn = prev_nonnote_insn (insn), --search_limit)
147 /* The closest BARRIER is too far away. */
148 if (search_limit == 0)
149 return -1;
151 /* The start of the function. */
152 else if (insn == 0)
153 return ENTRY_BLOCK_PTR->next_bb->index;
155 /* See if any of the upcoming CODE_LABELs start a basic block. If we reach
156 anything other than a CODE_LABEL or note, we can't find this code. */
157 for (insn = next_nonnote_insn (insn);
158 insn && GET_CODE (insn) == CODE_LABEL;
159 insn = next_nonnote_insn (insn))
161 FOR_EACH_BB (bb)
162 if (insn == bb->head)
163 return bb->index;
166 return -1;
169 /* Similar to next_insn, but ignores insns in the delay slots of
170 an annulled branch. */
172 static rtx
173 next_insn_no_annul (insn)
174 rtx insn;
176 if (insn)
178 /* If INSN is an annulled branch, skip any insns from the target
179 of the branch. */
180 if ((GET_CODE (insn) == JUMP_INSN
181 || GET_CODE (insn) == CALL_INSN
182 || GET_CODE (insn) == INSN)
183 && INSN_ANNULLED_BRANCH_P (insn)
184 && NEXT_INSN (PREV_INSN (insn)) != insn)
186 rtx next = NEXT_INSN (insn);
187 enum rtx_code code = GET_CODE (next);
189 while ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
190 && INSN_FROM_TARGET_P (next))
192 insn = next;
193 next = NEXT_INSN (insn);
194 code = GET_CODE (next);
198 insn = NEXT_INSN (insn);
199 if (insn && GET_CODE (insn) == INSN
200 && GET_CODE (PATTERN (insn)) == SEQUENCE)
201 insn = XVECEXP (PATTERN (insn), 0, 0);
204 return insn;
207 /* Given X, some rtl, and RES, a pointer to a `struct resource', mark
208 which resources are referenced by the insn. If INCLUDE_DELAYED_EFFECTS
209 is TRUE, resources used by the called routine will be included for
210 CALL_INSNs. */
212 void
213 mark_referenced_resources (x, res, include_delayed_effects)
214 rtx x;
215 struct resources *res;
216 int include_delayed_effects;
218 enum rtx_code code = GET_CODE (x);
219 int i, j;
220 unsigned int r;
221 const char *format_ptr;
223 /* Handle leaf items for which we set resource flags. Also, special-case
224 CALL, SET and CLOBBER operators. */
225 switch (code)
227 case CONST:
228 case CONST_INT:
229 case CONST_DOUBLE:
230 case CONST_VECTOR:
231 case PC:
232 case SYMBOL_REF:
233 case LABEL_REF:
234 return;
236 case SUBREG:
237 if (GET_CODE (SUBREG_REG (x)) != REG)
238 mark_referenced_resources (SUBREG_REG (x), res, 0);
239 else
241 unsigned int regno = subreg_regno (x);
242 unsigned int last_regno
243 = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
245 if (last_regno > FIRST_PSEUDO_REGISTER)
246 abort ();
247 for (r = regno; r < last_regno; r++)
248 SET_HARD_REG_BIT (res->regs, r);
250 return;
252 case REG:
254 unsigned int regno = REGNO (x);
255 unsigned int last_regno
256 = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
258 if (last_regno > FIRST_PSEUDO_REGISTER)
259 abort ();
260 for (r = regno; r < last_regno; r++)
261 SET_HARD_REG_BIT (res->regs, r);
263 return;
265 case MEM:
266 /* If this memory shouldn't change, it really isn't referencing
267 memory. */
268 if (RTX_UNCHANGING_P (x))
269 res->unch_memory = 1;
270 else
271 res->memory = 1;
272 res->volatil |= MEM_VOLATILE_P (x);
274 /* Mark registers used to access memory. */
275 mark_referenced_resources (XEXP (x, 0), res, 0);
276 return;
278 case CC0:
279 res->cc = 1;
280 return;
282 case UNSPEC_VOLATILE:
283 case ASM_INPUT:
284 /* Traditional asm's are always volatile. */
285 res->volatil = 1;
286 return;
288 case TRAP_IF:
289 res->volatil = 1;
290 break;
292 case ASM_OPERANDS:
293 res->volatil |= MEM_VOLATILE_P (x);
295 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
296 We can not just fall through here since then we would be confused
297 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
298 traditional asms unlike their normal usage. */
300 for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
301 mark_referenced_resources (ASM_OPERANDS_INPUT (x, i), res, 0);
302 return;
304 case CALL:
305 /* The first operand will be a (MEM (xxx)) but doesn't really reference
306 memory. The second operand may be referenced, though. */
307 mark_referenced_resources (XEXP (XEXP (x, 0), 0), res, 0);
308 mark_referenced_resources (XEXP (x, 1), res, 0);
309 return;
311 case SET:
312 /* Usually, the first operand of SET is set, not referenced. But
313 registers used to access memory are referenced. SET_DEST is
314 also referenced if it is a ZERO_EXTRACT or SIGN_EXTRACT. */
316 mark_referenced_resources (SET_SRC (x), res, 0);
318 x = SET_DEST (x);
319 if (GET_CODE (x) == SIGN_EXTRACT
320 || GET_CODE (x) == ZERO_EXTRACT
321 || GET_CODE (x) == STRICT_LOW_PART)
322 mark_referenced_resources (x, res, 0);
323 else if (GET_CODE (x) == SUBREG)
324 x = SUBREG_REG (x);
325 if (GET_CODE (x) == MEM)
326 mark_referenced_resources (XEXP (x, 0), res, 0);
327 return;
329 case CLOBBER:
330 return;
332 case CALL_INSN:
333 if (include_delayed_effects)
335 /* A CALL references memory, the frame pointer if it exists, the
336 stack pointer, any global registers and any registers given in
337 USE insns immediately in front of the CALL.
339 However, we may have moved some of the parameter loading insns
340 into the delay slot of this CALL. If so, the USE's for them
341 don't count and should be skipped. */
342 rtx insn = PREV_INSN (x);
343 rtx sequence = 0;
344 int seq_size = 0;
345 int i;
347 /* If we are part of a delay slot sequence, point at the SEQUENCE. */
348 if (NEXT_INSN (insn) != x)
350 sequence = PATTERN (NEXT_INSN (insn));
351 seq_size = XVECLEN (sequence, 0);
352 if (GET_CODE (sequence) != SEQUENCE)
353 abort ();
356 res->memory = 1;
357 SET_HARD_REG_BIT (res->regs, STACK_POINTER_REGNUM);
358 if (frame_pointer_needed)
360 SET_HARD_REG_BIT (res->regs, FRAME_POINTER_REGNUM);
361 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
362 SET_HARD_REG_BIT (res->regs, HARD_FRAME_POINTER_REGNUM);
363 #endif
366 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
367 if (global_regs[i])
368 SET_HARD_REG_BIT (res->regs, i);
370 /* Check for a REG_SETJMP. If it exists, then we must
371 assume that this call can need any register.
373 This is done to be more conservative about how we handle setjmp.
374 We assume that they both use and set all registers. Using all
375 registers ensures that a register will not be considered dead
376 just because it crosses a setjmp call. A register should be
377 considered dead only if the setjmp call returns nonzero. */
378 if (find_reg_note (x, REG_SETJMP, NULL))
379 SET_HARD_REG_SET (res->regs);
382 rtx link;
384 for (link = CALL_INSN_FUNCTION_USAGE (x);
385 link;
386 link = XEXP (link, 1))
387 if (GET_CODE (XEXP (link, 0)) == USE)
389 for (i = 1; i < seq_size; i++)
391 rtx slot_pat = PATTERN (XVECEXP (sequence, 0, i));
392 if (GET_CODE (slot_pat) == SET
393 && rtx_equal_p (SET_DEST (slot_pat),
394 XEXP (XEXP (link, 0), 0)))
395 break;
397 if (i >= seq_size)
398 mark_referenced_resources (XEXP (XEXP (link, 0), 0),
399 res, 0);
404 /* ... fall through to other INSN processing ... */
406 case INSN:
407 case JUMP_INSN:
409 #ifdef INSN_REFERENCES_ARE_DELAYED
410 if (! include_delayed_effects
411 && INSN_REFERENCES_ARE_DELAYED (x))
412 return;
413 #endif
415 /* No special processing, just speed up. */
416 mark_referenced_resources (PATTERN (x), res, include_delayed_effects);
417 return;
419 default:
420 break;
423 /* Process each sub-expression and flag what it needs. */
424 format_ptr = GET_RTX_FORMAT (code);
425 for (i = 0; i < GET_RTX_LENGTH (code); i++)
426 switch (*format_ptr++)
428 case 'e':
429 mark_referenced_resources (XEXP (x, i), res, include_delayed_effects);
430 break;
432 case 'E':
433 for (j = 0; j < XVECLEN (x, i); j++)
434 mark_referenced_resources (XVECEXP (x, i, j), res,
435 include_delayed_effects);
436 break;
440 /* A subroutine of mark_target_live_regs. Search forward from TARGET
441 looking for registers that are set before they are used. These are dead.
442 Stop after passing a few conditional jumps, and/or a small
443 number of unconditional branches. */
445 static rtx
446 find_dead_or_set_registers (target, res, jump_target, jump_count, set, needed)
447 rtx target;
448 struct resources *res;
449 rtx *jump_target;
450 int jump_count;
451 struct resources set, needed;
453 HARD_REG_SET scratch;
454 rtx insn, next;
455 rtx jump_insn = 0;
456 int i;
458 for (insn = target; insn; insn = next)
460 rtx this_jump_insn = insn;
462 next = NEXT_INSN (insn);
464 /* If this instruction can throw an exception, then we don't
465 know where we might end up next. That means that we have to
466 assume that whatever we have already marked as live really is
467 live. */
468 if (can_throw_internal (insn))
469 break;
471 switch (GET_CODE (insn))
473 case CODE_LABEL:
474 /* After a label, any pending dead registers that weren't yet
475 used can be made dead. */
476 AND_COMPL_HARD_REG_SET (pending_dead_regs, needed.regs);
477 AND_COMPL_HARD_REG_SET (res->regs, pending_dead_regs);
478 CLEAR_HARD_REG_SET (pending_dead_regs);
480 continue;
482 case BARRIER:
483 case NOTE:
484 continue;
486 case INSN:
487 if (GET_CODE (PATTERN (insn)) == USE)
489 /* If INSN is a USE made by update_block, we care about the
490 underlying insn. Any registers set by the underlying insn
491 are live since the insn is being done somewhere else. */
492 if (INSN_P (XEXP (PATTERN (insn), 0)))
493 mark_set_resources (XEXP (PATTERN (insn), 0), res, 0,
494 MARK_SRC_DEST_CALL);
496 /* All other USE insns are to be ignored. */
497 continue;
499 else if (GET_CODE (PATTERN (insn)) == CLOBBER)
500 continue;
501 else if (GET_CODE (PATTERN (insn)) == SEQUENCE)
503 /* An unconditional jump can be used to fill the delay slot
504 of a call, so search for a JUMP_INSN in any position. */
505 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
507 this_jump_insn = XVECEXP (PATTERN (insn), 0, i);
508 if (GET_CODE (this_jump_insn) == JUMP_INSN)
509 break;
513 default:
514 break;
517 if (GET_CODE (this_jump_insn) == JUMP_INSN)
519 if (jump_count++ < 10)
521 if (any_uncondjump_p (this_jump_insn)
522 || GET_CODE (PATTERN (this_jump_insn)) == RETURN)
524 next = JUMP_LABEL (this_jump_insn);
525 if (jump_insn == 0)
527 jump_insn = insn;
528 if (jump_target)
529 *jump_target = JUMP_LABEL (this_jump_insn);
532 else if (any_condjump_p (this_jump_insn))
534 struct resources target_set, target_res;
535 struct resources fallthrough_res;
537 /* We can handle conditional branches here by following
538 both paths, and then IOR the results of the two paths
539 together, which will give us registers that are dead
540 on both paths. Since this is expensive, we give it
541 a much higher cost than unconditional branches. The
542 cost was chosen so that we will follow at most 1
543 conditional branch. */
545 jump_count += 4;
546 if (jump_count >= 10)
547 break;
549 mark_referenced_resources (insn, &needed, 1);
551 /* For an annulled branch, mark_set_resources ignores slots
552 filled by instructions from the target. This is correct
553 if the branch is not taken. Since we are following both
554 paths from the branch, we must also compute correct info
555 if the branch is taken. We do this by inverting all of
556 the INSN_FROM_TARGET_P bits, calling mark_set_resources,
557 and then inverting the INSN_FROM_TARGET_P bits again. */
559 if (GET_CODE (PATTERN (insn)) == SEQUENCE
560 && INSN_ANNULLED_BRANCH_P (this_jump_insn))
562 for (i = 1; i < XVECLEN (PATTERN (insn), 0); i++)
563 INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn), 0, i))
564 = ! INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn), 0, i));
566 target_set = set;
567 mark_set_resources (insn, &target_set, 0,
568 MARK_SRC_DEST_CALL);
570 for (i = 1; i < XVECLEN (PATTERN (insn), 0); i++)
571 INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn), 0, i))
572 = ! INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn), 0, i));
574 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
576 else
578 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
579 target_set = set;
582 target_res = *res;
583 COPY_HARD_REG_SET (scratch, target_set.regs);
584 AND_COMPL_HARD_REG_SET (scratch, needed.regs);
585 AND_COMPL_HARD_REG_SET (target_res.regs, scratch);
587 fallthrough_res = *res;
588 COPY_HARD_REG_SET (scratch, set.regs);
589 AND_COMPL_HARD_REG_SET (scratch, needed.regs);
590 AND_COMPL_HARD_REG_SET (fallthrough_res.regs, scratch);
592 find_dead_or_set_registers (JUMP_LABEL (this_jump_insn),
593 &target_res, 0, jump_count,
594 target_set, needed);
595 find_dead_or_set_registers (next,
596 &fallthrough_res, 0, jump_count,
597 set, needed);
598 IOR_HARD_REG_SET (fallthrough_res.regs, target_res.regs);
599 AND_HARD_REG_SET (res->regs, fallthrough_res.regs);
600 break;
602 else
603 break;
605 else
607 /* Don't try this optimization if we expired our jump count
608 above, since that would mean there may be an infinite loop
609 in the function being compiled. */
610 jump_insn = 0;
611 break;
615 mark_referenced_resources (insn, &needed, 1);
616 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
618 COPY_HARD_REG_SET (scratch, set.regs);
619 AND_COMPL_HARD_REG_SET (scratch, needed.regs);
620 AND_COMPL_HARD_REG_SET (res->regs, scratch);
623 return jump_insn;
626 /* Given X, a part of an insn, and a pointer to a `struct resource',
627 RES, indicate which resources are modified by the insn. If
628 MARK_TYPE is MARK_SRC_DEST_CALL, also mark resources potentially
629 set by the called routine. If MARK_TYPE is MARK_DEST, only mark SET_DESTs
631 If IN_DEST is nonzero, it means we are inside a SET. Otherwise,
632 objects are being referenced instead of set.
634 We never mark the insn as modifying the condition code unless it explicitly
635 SETs CC0 even though this is not totally correct. The reason for this is
636 that we require a SET of CC0 to immediately precede the reference to CC0.
637 So if some other insn sets CC0 as a side-effect, we know it cannot affect
638 our computation and thus may be placed in a delay slot. */
640 void
641 mark_set_resources (x, res, in_dest, mark_type)
642 rtx x;
643 struct resources *res;
644 int in_dest;
645 enum mark_resource_type mark_type;
647 enum rtx_code code;
648 int i, j;
649 unsigned int r;
650 const char *format_ptr;
652 restart:
654 code = GET_CODE (x);
656 switch (code)
658 case NOTE:
659 case BARRIER:
660 case CODE_LABEL:
661 case USE:
662 case CONST_INT:
663 case CONST_DOUBLE:
664 case CONST_VECTOR:
665 case LABEL_REF:
666 case SYMBOL_REF:
667 case CONST:
668 case PC:
669 /* These don't set any resources. */
670 return;
672 case CC0:
673 if (in_dest)
674 res->cc = 1;
675 return;
677 case CALL_INSN:
678 /* Called routine modifies the condition code, memory, any registers
679 that aren't saved across calls, global registers and anything
680 explicitly CLOBBERed immediately after the CALL_INSN. */
682 if (mark_type == MARK_SRC_DEST_CALL)
684 rtx link;
686 res->cc = res->memory = 1;
687 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
688 if (call_used_regs[r] || global_regs[r])
689 SET_HARD_REG_BIT (res->regs, r);
691 for (link = CALL_INSN_FUNCTION_USAGE (x);
692 link; link = XEXP (link, 1))
693 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
694 mark_set_resources (SET_DEST (XEXP (link, 0)), res, 1,
695 MARK_SRC_DEST);
697 /* Check for a REG_SETJMP. If it exists, then we must
698 assume that this call can clobber any register. */
699 if (find_reg_note (x, REG_SETJMP, NULL))
700 SET_HARD_REG_SET (res->regs);
703 /* ... and also what its RTL says it modifies, if anything. */
705 case JUMP_INSN:
706 case INSN:
708 /* An insn consisting of just a CLOBBER (or USE) is just for flow
709 and doesn't actually do anything, so we ignore it. */
711 #ifdef INSN_SETS_ARE_DELAYED
712 if (mark_type != MARK_SRC_DEST_CALL
713 && INSN_SETS_ARE_DELAYED (x))
714 return;
715 #endif
717 x = PATTERN (x);
718 if (GET_CODE (x) != USE && GET_CODE (x) != CLOBBER)
719 goto restart;
720 return;
722 case SET:
723 /* If the source of a SET is a CALL, this is actually done by
724 the called routine. So only include it if we are to include the
725 effects of the calling routine. */
727 mark_set_resources (SET_DEST (x), res,
728 (mark_type == MARK_SRC_DEST_CALL
729 || GET_CODE (SET_SRC (x)) != CALL),
730 mark_type);
732 if (mark_type != MARK_DEST)
733 mark_set_resources (SET_SRC (x), res, 0, MARK_SRC_DEST);
734 return;
736 case CLOBBER:
737 mark_set_resources (XEXP (x, 0), res, 1, MARK_SRC_DEST);
738 return;
740 case SEQUENCE:
741 for (i = 0; i < XVECLEN (x, 0); i++)
742 if (! (INSN_ANNULLED_BRANCH_P (XVECEXP (x, 0, 0))
743 && INSN_FROM_TARGET_P (XVECEXP (x, 0, i))))
744 mark_set_resources (XVECEXP (x, 0, i), res, 0, mark_type);
745 return;
747 case POST_INC:
748 case PRE_INC:
749 case POST_DEC:
750 case PRE_DEC:
751 mark_set_resources (XEXP (x, 0), res, 1, MARK_SRC_DEST);
752 return;
754 case PRE_MODIFY:
755 case POST_MODIFY:
756 mark_set_resources (XEXP (x, 0), res, 1, MARK_SRC_DEST);
757 mark_set_resources (XEXP (XEXP (x, 1), 0), res, 0, MARK_SRC_DEST);
758 mark_set_resources (XEXP (XEXP (x, 1), 1), res, 0, MARK_SRC_DEST);
759 return;
761 case SIGN_EXTRACT:
762 case ZERO_EXTRACT:
763 if (! (mark_type == MARK_DEST && in_dest))
765 mark_set_resources (XEXP (x, 0), res, in_dest, MARK_SRC_DEST);
766 mark_set_resources (XEXP (x, 1), res, 0, MARK_SRC_DEST);
767 mark_set_resources (XEXP (x, 2), res, 0, MARK_SRC_DEST);
769 return;
771 case MEM:
772 if (in_dest)
774 res->memory = 1;
775 res->unch_memory |= RTX_UNCHANGING_P (x);
776 res->volatil |= MEM_VOLATILE_P (x);
779 mark_set_resources (XEXP (x, 0), res, 0, MARK_SRC_DEST);
780 return;
782 case SUBREG:
783 if (in_dest)
785 if (GET_CODE (SUBREG_REG (x)) != REG)
786 mark_set_resources (SUBREG_REG (x), res, in_dest, mark_type);
787 else
789 unsigned int regno = subreg_regno (x);
790 unsigned int last_regno
791 = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
793 if (last_regno > FIRST_PSEUDO_REGISTER)
794 abort ();
795 for (r = regno; r < last_regno; r++)
796 SET_HARD_REG_BIT (res->regs, r);
799 return;
801 case REG:
802 if (in_dest)
804 unsigned int regno = REGNO (x);
805 unsigned int last_regno
806 = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
808 if (last_regno > FIRST_PSEUDO_REGISTER)
809 abort ();
810 for (r = regno; r < last_regno; r++)
811 SET_HARD_REG_BIT (res->regs, r);
813 return;
815 case STRICT_LOW_PART:
816 if (! (mark_type == MARK_DEST && in_dest))
818 mark_set_resources (XEXP (x, 0), res, 0, MARK_SRC_DEST);
819 return;
822 case UNSPEC_VOLATILE:
823 case ASM_INPUT:
824 /* Traditional asm's are always volatile. */
825 res->volatil = 1;
826 return;
828 case TRAP_IF:
829 res->volatil = 1;
830 break;
832 case ASM_OPERANDS:
833 res->volatil |= MEM_VOLATILE_P (x);
835 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
836 We can not just fall through here since then we would be confused
837 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
838 traditional asms unlike their normal usage. */
840 for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
841 mark_set_resources (ASM_OPERANDS_INPUT (x, i), res, in_dest,
842 MARK_SRC_DEST);
843 return;
845 default:
846 break;
849 /* Process each sub-expression and flag what it needs. */
850 format_ptr = GET_RTX_FORMAT (code);
851 for (i = 0; i < GET_RTX_LENGTH (code); i++)
852 switch (*format_ptr++)
854 case 'e':
855 mark_set_resources (XEXP (x, i), res, in_dest, mark_type);
856 break;
858 case 'E':
859 for (j = 0; j < XVECLEN (x, i); j++)
860 mark_set_resources (XVECEXP (x, i, j), res, in_dest, mark_type);
861 break;
865 /* Set the resources that are live at TARGET.
867 If TARGET is zero, we refer to the end of the current function and can
868 return our precomputed value.
870 Otherwise, we try to find out what is live by consulting the basic block
871 information. This is tricky, because we must consider the actions of
872 reload and jump optimization, which occur after the basic block information
873 has been computed.
875 Accordingly, we proceed as follows::
877 We find the previous BARRIER and look at all immediately following labels
878 (with no intervening active insns) to see if any of them start a basic
879 block. If we hit the start of the function first, we use block 0.
881 Once we have found a basic block and a corresponding first insns, we can
882 accurately compute the live status from basic_block_live_regs and
883 reg_renumber. (By starting at a label following a BARRIER, we are immune
884 to actions taken by reload and jump.) Then we scan all insns between
885 that point and our target. For each CLOBBER (or for call-clobbered regs
886 when we pass a CALL_INSN), mark the appropriate registers are dead. For
887 a SET, mark them as live.
889 We have to be careful when using REG_DEAD notes because they are not
890 updated by such things as find_equiv_reg. So keep track of registers
891 marked as dead that haven't been assigned to, and mark them dead at the
892 next CODE_LABEL since reload and jump won't propagate values across labels.
894 If we cannot find the start of a basic block (should be a very rare
895 case, if it can happen at all), mark everything as potentially live.
897 Next, scan forward from TARGET looking for things set or clobbered
898 before they are used. These are not live.
900 Because we can be called many times on the same target, save our results
901 in a hash table indexed by INSN_UID. This is only done if the function
902 init_resource_info () was invoked before we are called. */
904 void
905 mark_target_live_regs (insns, target, res)
906 rtx insns;
907 rtx target;
908 struct resources *res;
910 int b = -1;
911 unsigned int i;
912 struct target_info *tinfo = NULL;
913 rtx insn;
914 rtx jump_insn = 0;
915 rtx jump_target;
916 HARD_REG_SET scratch;
917 struct resources set, needed;
919 /* Handle end of function. */
920 if (target == 0)
922 *res = end_of_function_needs;
923 return;
926 /* We have to assume memory is needed, but the CC isn't. */
927 res->memory = 1;
928 res->volatil = res->unch_memory = 0;
929 res->cc = 0;
931 /* See if we have computed this value already. */
932 if (target_hash_table != NULL)
934 for (tinfo = target_hash_table[INSN_UID (target) % TARGET_HASH_PRIME];
935 tinfo; tinfo = tinfo->next)
936 if (tinfo->uid == INSN_UID (target))
937 break;
939 /* Start by getting the basic block number. If we have saved
940 information, we can get it from there unless the insn at the
941 start of the basic block has been deleted. */
942 if (tinfo && tinfo->block != -1
943 && ! INSN_DELETED_P (BLOCK_HEAD (tinfo->block)))
944 b = tinfo->block;
947 if (b == -1)
948 b = find_basic_block (target, MAX_DELAY_SLOT_LIVE_SEARCH);
950 if (target_hash_table != NULL)
952 if (tinfo)
954 /* If the information is up-to-date, use it. Otherwise, we will
955 update it below. */
956 if (b == tinfo->block && b != -1 && tinfo->bb_tick == bb_ticks[b])
958 COPY_HARD_REG_SET (res->regs, tinfo->live_regs);
959 return;
962 else
964 /* Allocate a place to put our results and chain it into the
965 hash table. */
966 tinfo = (struct target_info *) xmalloc (sizeof (struct target_info));
967 tinfo->uid = INSN_UID (target);
968 tinfo->block = b;
969 tinfo->next
970 = target_hash_table[INSN_UID (target) % TARGET_HASH_PRIME];
971 target_hash_table[INSN_UID (target) % TARGET_HASH_PRIME] = tinfo;
975 CLEAR_HARD_REG_SET (pending_dead_regs);
977 /* If we found a basic block, get the live registers from it and update
978 them with anything set or killed between its start and the insn before
979 TARGET. Otherwise, we must assume everything is live. */
980 if (b != -1)
982 regset regs_live = BASIC_BLOCK (b)->global_live_at_start;
983 unsigned int j;
984 unsigned int regno;
985 rtx start_insn, stop_insn;
987 /* Compute hard regs live at start of block -- this is the real hard regs
988 marked live, plus live pseudo regs that have been renumbered to
989 hard regs. */
991 REG_SET_TO_HARD_REG_SET (current_live_regs, regs_live);
993 EXECUTE_IF_SET_IN_REG_SET
994 (regs_live, FIRST_PSEUDO_REGISTER, i,
996 if (reg_renumber[i] >= 0)
998 regno = reg_renumber[i];
999 for (j = regno;
1000 j < regno + HARD_REGNO_NREGS (regno,
1001 PSEUDO_REGNO_MODE (i));
1002 j++)
1003 SET_HARD_REG_BIT (current_live_regs, j);
1007 /* Get starting and ending insn, handling the case where each might
1008 be a SEQUENCE. */
1009 start_insn = (b == 0 ? insns : BLOCK_HEAD (b));
1010 stop_insn = target;
1012 if (GET_CODE (start_insn) == INSN
1013 && GET_CODE (PATTERN (start_insn)) == SEQUENCE)
1014 start_insn = XVECEXP (PATTERN (start_insn), 0, 0);
1016 if (GET_CODE (stop_insn) == INSN
1017 && GET_CODE (PATTERN (stop_insn)) == SEQUENCE)
1018 stop_insn = next_insn (PREV_INSN (stop_insn));
1020 for (insn = start_insn; insn != stop_insn;
1021 insn = next_insn_no_annul (insn))
1023 rtx link;
1024 rtx real_insn = insn;
1025 enum rtx_code code = GET_CODE (insn);
1027 /* If this insn is from the target of a branch, it isn't going to
1028 be used in the sequel. If it is used in both cases, this
1029 test will not be true. */
1030 if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
1031 && INSN_FROM_TARGET_P (insn))
1032 continue;
1034 /* If this insn is a USE made by update_block, we care about the
1035 underlying insn. */
1036 if (code == INSN && GET_CODE (PATTERN (insn)) == USE
1037 && INSN_P (XEXP (PATTERN (insn), 0)))
1038 real_insn = XEXP (PATTERN (insn), 0);
1040 if (GET_CODE (real_insn) == CALL_INSN)
1042 /* CALL clobbers all call-used regs that aren't fixed except
1043 sp, ap, and fp. Do this before setting the result of the
1044 call live. */
1045 AND_COMPL_HARD_REG_SET (current_live_regs,
1046 regs_invalidated_by_call);
1048 /* A CALL_INSN sets any global register live, since it may
1049 have been modified by the call. */
1050 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1051 if (global_regs[i])
1052 SET_HARD_REG_BIT (current_live_regs, i);
1055 /* Mark anything killed in an insn to be deadened at the next
1056 label. Ignore USE insns; the only REG_DEAD notes will be for
1057 parameters. But they might be early. A CALL_INSN will usually
1058 clobber registers used for parameters. It isn't worth bothering
1059 with the unlikely case when it won't. */
1060 if ((GET_CODE (real_insn) == INSN
1061 && GET_CODE (PATTERN (real_insn)) != USE
1062 && GET_CODE (PATTERN (real_insn)) != CLOBBER)
1063 || GET_CODE (real_insn) == JUMP_INSN
1064 || GET_CODE (real_insn) == CALL_INSN)
1066 for (link = REG_NOTES (real_insn); link; link = XEXP (link, 1))
1067 if (REG_NOTE_KIND (link) == REG_DEAD
1068 && GET_CODE (XEXP (link, 0)) == REG
1069 && REGNO (XEXP (link, 0)) < FIRST_PSEUDO_REGISTER)
1071 unsigned int first_regno = REGNO (XEXP (link, 0));
1072 unsigned int last_regno
1073 = (first_regno
1074 + HARD_REGNO_NREGS (first_regno,
1075 GET_MODE (XEXP (link, 0))));
1077 for (i = first_regno; i < last_regno; i++)
1078 SET_HARD_REG_BIT (pending_dead_regs, i);
1081 note_stores (PATTERN (real_insn), update_live_status, NULL);
1083 /* If any registers were unused after this insn, kill them.
1084 These notes will always be accurate. */
1085 for (link = REG_NOTES (real_insn); link; link = XEXP (link, 1))
1086 if (REG_NOTE_KIND (link) == REG_UNUSED
1087 && GET_CODE (XEXP (link, 0)) == REG
1088 && REGNO (XEXP (link, 0)) < FIRST_PSEUDO_REGISTER)
1090 unsigned int first_regno = REGNO (XEXP (link, 0));
1091 unsigned int last_regno
1092 = (first_regno
1093 + HARD_REGNO_NREGS (first_regno,
1094 GET_MODE (XEXP (link, 0))));
1096 for (i = first_regno; i < last_regno; i++)
1097 CLEAR_HARD_REG_BIT (current_live_regs, i);
1101 else if (GET_CODE (real_insn) == CODE_LABEL)
1103 /* A label clobbers the pending dead registers since neither
1104 reload nor jump will propagate a value across a label. */
1105 AND_COMPL_HARD_REG_SET (current_live_regs, pending_dead_regs);
1106 CLEAR_HARD_REG_SET (pending_dead_regs);
1109 /* The beginning of the epilogue corresponds to the end of the
1110 RTL chain when there are no epilogue insns. Certain resources
1111 are implicitly required at that point. */
1112 else if (GET_CODE (real_insn) == NOTE
1113 && NOTE_LINE_NUMBER (real_insn) == NOTE_INSN_EPILOGUE_BEG)
1114 IOR_HARD_REG_SET (current_live_regs, start_of_epilogue_needs.regs);
1117 COPY_HARD_REG_SET (res->regs, current_live_regs);
1118 if (tinfo != NULL)
1120 tinfo->block = b;
1121 tinfo->bb_tick = bb_ticks[b];
1124 else
1125 /* We didn't find the start of a basic block. Assume everything
1126 in use. This should happen only extremely rarely. */
1127 SET_HARD_REG_SET (res->regs);
1129 CLEAR_RESOURCE (&set);
1130 CLEAR_RESOURCE (&needed);
1132 jump_insn = find_dead_or_set_registers (target, res, &jump_target, 0,
1133 set, needed);
1135 /* If we hit an unconditional branch, we have another way of finding out
1136 what is live: we can see what is live at the branch target and include
1137 anything used but not set before the branch. We add the live
1138 resources found using the test below to those found until now. */
1140 if (jump_insn)
1142 struct resources new_resources;
1143 rtx stop_insn = next_active_insn (jump_insn);
1145 mark_target_live_regs (insns, next_active_insn (jump_target),
1146 &new_resources);
1147 CLEAR_RESOURCE (&set);
1148 CLEAR_RESOURCE (&needed);
1150 /* Include JUMP_INSN in the needed registers. */
1151 for (insn = target; insn != stop_insn; insn = next_active_insn (insn))
1153 mark_referenced_resources (insn, &needed, 1);
1155 COPY_HARD_REG_SET (scratch, needed.regs);
1156 AND_COMPL_HARD_REG_SET (scratch, set.regs);
1157 IOR_HARD_REG_SET (new_resources.regs, scratch);
1159 mark_set_resources (insn, &set, 0, MARK_SRC_DEST_CALL);
1162 IOR_HARD_REG_SET (res->regs, new_resources.regs);
1165 if (tinfo != NULL)
1167 COPY_HARD_REG_SET (tinfo->live_regs, res->regs);
1171 /* Initialize the resources required by mark_target_live_regs ().
1172 This should be invoked before the first call to mark_target_live_regs. */
1174 void
1175 init_resource_info (epilogue_insn)
1176 rtx epilogue_insn;
1178 int i;
1180 /* Indicate what resources are required to be valid at the end of the current
1181 function. The condition code never is and memory always is. If the
1182 frame pointer is needed, it is and so is the stack pointer unless
1183 EXIT_IGNORE_STACK is nonzero. If the frame pointer is not needed, the
1184 stack pointer is. Registers used to return the function value are
1185 needed. Registers holding global variables are needed. */
1187 end_of_function_needs.cc = 0;
1188 end_of_function_needs.memory = 1;
1189 end_of_function_needs.unch_memory = 0;
1190 CLEAR_HARD_REG_SET (end_of_function_needs.regs);
1192 if (frame_pointer_needed)
1194 SET_HARD_REG_BIT (end_of_function_needs.regs, FRAME_POINTER_REGNUM);
1195 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1196 SET_HARD_REG_BIT (end_of_function_needs.regs, HARD_FRAME_POINTER_REGNUM);
1197 #endif
1198 #ifdef EXIT_IGNORE_STACK
1199 if (! EXIT_IGNORE_STACK
1200 || current_function_sp_is_unchanging)
1201 #endif
1202 SET_HARD_REG_BIT (end_of_function_needs.regs, STACK_POINTER_REGNUM);
1204 else
1205 SET_HARD_REG_BIT (end_of_function_needs.regs, STACK_POINTER_REGNUM);
1207 if (current_function_return_rtx != 0)
1208 mark_referenced_resources (current_function_return_rtx,
1209 &end_of_function_needs, 1);
1211 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1212 if (global_regs[i]
1213 #ifdef EPILOGUE_USES
1214 || EPILOGUE_USES (i)
1215 #endif
1217 SET_HARD_REG_BIT (end_of_function_needs.regs, i);
1219 /* The registers required to be live at the end of the function are
1220 represented in the flow information as being dead just prior to
1221 reaching the end of the function. For example, the return of a value
1222 might be represented by a USE of the return register immediately
1223 followed by an unconditional jump to the return label where the
1224 return label is the end of the RTL chain. The end of the RTL chain
1225 is then taken to mean that the return register is live.
1227 This sequence is no longer maintained when epilogue instructions are
1228 added to the RTL chain. To reconstruct the original meaning, the
1229 start of the epilogue (NOTE_INSN_EPILOGUE_BEG) is regarded as the
1230 point where these registers become live (start_of_epilogue_needs).
1231 If epilogue instructions are present, the registers set by those
1232 instructions won't have been processed by flow. Thus, those
1233 registers are additionally required at the end of the RTL chain
1234 (end_of_function_needs). */
1236 start_of_epilogue_needs = end_of_function_needs;
1238 while ((epilogue_insn = next_nonnote_insn (epilogue_insn)))
1239 mark_set_resources (epilogue_insn, &end_of_function_needs, 0,
1240 MARK_SRC_DEST_CALL);
1242 /* Allocate and initialize the tables used by mark_target_live_regs. */
1243 target_hash_table = (struct target_info **)
1244 xcalloc (TARGET_HASH_PRIME, sizeof (struct target_info *));
1245 bb_ticks = (int *) xcalloc (last_basic_block, sizeof (int));
1248 /* Free up the resources allocated to mark_target_live_regs (). This
1249 should be invoked after the last call to mark_target_live_regs (). */
1251 void
1252 free_resource_info ()
1254 if (target_hash_table != NULL)
1256 int i;
1258 for (i = 0; i < TARGET_HASH_PRIME; ++i)
1260 struct target_info *ti = target_hash_table[i];
1262 while (ti)
1264 struct target_info *next = ti->next;
1265 free (ti);
1266 ti = next;
1270 free (target_hash_table);
1271 target_hash_table = NULL;
1274 if (bb_ticks != NULL)
1276 free (bb_ticks);
1277 bb_ticks = NULL;
1281 /* Clear any hashed information that we have stored for INSN. */
1283 void
1284 clear_hashed_info_for_insn (insn)
1285 rtx insn;
1287 struct target_info *tinfo;
1289 if (target_hash_table != NULL)
1291 for (tinfo = target_hash_table[INSN_UID (insn) % TARGET_HASH_PRIME];
1292 tinfo; tinfo = tinfo->next)
1293 if (tinfo->uid == INSN_UID (insn))
1294 break;
1296 if (tinfo)
1297 tinfo->block = -1;
1301 /* Increment the tick count for the basic block that contains INSN. */
1303 void
1304 incr_ticks_for_insn (insn)
1305 rtx insn;
1307 int b = find_basic_block (insn, MAX_DELAY_SLOT_LIVE_SEARCH);
1309 if (b != -1)
1310 bb_ticks[b]++;
1313 /* Add TRIAL to the set of resources used at the end of the current
1314 function. */
1315 void
1316 mark_end_of_function_resources (trial, include_delayed_effects)
1317 rtx trial;
1318 int include_delayed_effects;
1320 mark_referenced_resources (trial, &end_of_function_needs,
1321 include_delayed_effects);