]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/resource.c
Bob Manson <manson@charmed.cygnus.com>
[thirdparty/gcc.git] / gcc / resource.c
1 #include "config.h"
2 #include "rtl.h"
3 #include "hard-reg-set.h"
4 #include "system.h"
5 #include "basic-block.h"
6 #include "regs.h"
7 #include "flags.h"
8 #include "output.h"
9 #include "resource.h"
10
11 /* This structure is used to record liveness information at the targets or
12 fallthrough insns of branches. We will most likely need the information
13 at targets again, so save them in a hash table rather than recomputing them
14 each time. */
15
16 struct target_info
17 {
18 int uid; /* INSN_UID of target. */
19 struct target_info *next; /* Next info for same hash bucket. */
20 HARD_REG_SET live_regs; /* Registers live at target. */
21 int block; /* Basic block number containing target. */
22 int bb_tick; /* Generation count of basic block info. */
23 };
24
25 #define TARGET_HASH_PRIME 257
26
27 /* Indicates what resources are required at the beginning of the epilogue. */
28 static struct resources start_of_epilogue_needs;
29
30 /* Indicates what resources are required at function end. */
31 static struct resources end_of_function_needs;
32
33 /* Define the hash table itself. */
34 static struct target_info **target_hash_table = NULL;
35
36 /* For each basic block, we maintain a generation number of its basic
37 block info, which is updated each time we move an insn from the
38 target of a jump. This is the generation number indexed by block
39 number. */
40
41 static int *bb_ticks;
42
43 /* Marks registers possibly live at the current place being scanned by
44 mark_target_live_regs. Used only by next two function. */
45
46 static HARD_REG_SET current_live_regs;
47
48 /* Marks registers for which we have seen a REG_DEAD note but no assignment.
49 Also only used by the next two functions. */
50
51 static HARD_REG_SET pending_dead_regs;
52
53 /* Utility function called from mark_target_live_regs via note_stores.
54 It deadens any CLOBBERed registers and livens any SET registers. */
55
56 static void
57 update_live_status (dest, x)
58 rtx dest;
59 rtx x;
60 {
61 int first_regno, last_regno;
62 int i;
63
64 if (GET_CODE (dest) != REG
65 && (GET_CODE (dest) != SUBREG || GET_CODE (SUBREG_REG (dest)) != REG))
66 return;
67
68 if (GET_CODE (dest) == SUBREG)
69 first_regno = REGNO (SUBREG_REG (dest)) + SUBREG_WORD (dest);
70 else
71 first_regno = REGNO (dest);
72
73 last_regno = first_regno + HARD_REGNO_NREGS (first_regno, GET_MODE (dest));
74
75 if (GET_CODE (x) == CLOBBER)
76 for (i = first_regno; i < last_regno; i++)
77 CLEAR_HARD_REG_BIT (current_live_regs, i);
78 else
79 for (i = first_regno; i < last_regno; i++)
80 {
81 SET_HARD_REG_BIT (current_live_regs, i);
82 CLEAR_HARD_REG_BIT (pending_dead_regs, i);
83 }
84 }
85 /* Find the number of the basic block that starts closest to INSN. Return -1
86 if we couldn't find such a basic block. */
87
88 static int
89 find_basic_block (insn)
90 rtx insn;
91 {
92 int i;
93
94 /* Scan backwards to the previous BARRIER. Then see if we can find a
95 label that starts a basic block. Return the basic block number. */
96
97 for (insn = prev_nonnote_insn (insn);
98 insn && GET_CODE (insn) != BARRIER;
99 insn = prev_nonnote_insn (insn))
100 ;
101
102 /* The start of the function is basic block zero. */
103 if (insn == 0)
104 return 0;
105
106 /* See if any of the upcoming CODE_LABELs start a basic block. If we reach
107 anything other than a CODE_LABEL or note, we can't find this code. */
108 for (insn = next_nonnote_insn (insn);
109 insn && GET_CODE (insn) == CODE_LABEL;
110 insn = next_nonnote_insn (insn))
111 {
112 for (i = 0; i < n_basic_blocks; i++)
113 if (insn == BLOCK_HEAD (i))
114 return i;
115 }
116
117 return -1;
118 }
119 \f
120 /* Similar to next_insn, but ignores insns in the delay slots of
121 an annulled branch. */
122
123 static rtx
124 next_insn_no_annul (insn)
125 rtx insn;
126 {
127 if (insn)
128 {
129 /* If INSN is an annulled branch, skip any insns from the target
130 of the branch. */
131 if (INSN_ANNULLED_BRANCH_P (insn)
132 && NEXT_INSN (PREV_INSN (insn)) != insn)
133 while (INSN_FROM_TARGET_P (NEXT_INSN (insn)))
134 insn = NEXT_INSN (insn);
135
136 insn = NEXT_INSN (insn);
137 if (insn && GET_CODE (insn) == INSN
138 && GET_CODE (PATTERN (insn)) == SEQUENCE)
139 insn = XVECEXP (PATTERN (insn), 0, 0);
140 }
141
142 return insn;
143 }
144 \f
145 /* Given X, some rtl, and RES, a pointer to a `struct resource', mark
146 which resources are references by the insn. If INCLUDE_DELAYED_EFFECTS
147 is TRUE, resources used by the called routine will be included for
148 CALL_INSNs. */
149
150 void
151 mark_referenced_resources (x, res, include_delayed_effects)
152 register rtx x;
153 register struct resources *res;
154 register int include_delayed_effects;
155 {
156 register enum rtx_code code = GET_CODE (x);
157 register int i, j;
158 register char *format_ptr;
159
160 /* Handle leaf items for which we set resource flags. Also, special-case
161 CALL, SET and CLOBBER operators. */
162 switch (code)
163 {
164 case CONST:
165 case CONST_INT:
166 case CONST_DOUBLE:
167 case PC:
168 case SYMBOL_REF:
169 case LABEL_REF:
170 return;
171
172 case SUBREG:
173 if (GET_CODE (SUBREG_REG (x)) != REG)
174 mark_referenced_resources (SUBREG_REG (x), res, 0);
175 else
176 {
177 int regno = REGNO (SUBREG_REG (x)) + SUBREG_WORD (x);
178 int last_regno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
179 for (i = regno; i < last_regno; i++)
180 SET_HARD_REG_BIT (res->regs, i);
181 }
182 return;
183
184 case REG:
185 for (i = 0; i < HARD_REGNO_NREGS (REGNO (x), GET_MODE (x)); i++)
186 SET_HARD_REG_BIT (res->regs, REGNO (x) + i);
187 return;
188
189 case MEM:
190 /* If this memory shouldn't change, it really isn't referencing
191 memory. */
192 if (RTX_UNCHANGING_P (x))
193 res->unch_memory = 1;
194 else
195 res->memory = 1;
196 res->volatil = MEM_VOLATILE_P (x);
197
198 /* Mark registers used to access memory. */
199 mark_referenced_resources (XEXP (x, 0), res, 0);
200 return;
201
202 case CC0:
203 res->cc = 1;
204 return;
205
206 case UNSPEC_VOLATILE:
207 case ASM_INPUT:
208 /* Traditional asm's are always volatile. */
209 res->volatil = 1;
210 return;
211
212 case TRAP_IF:
213 res->volatil = 1;
214 break;
215
216 case ASM_OPERANDS:
217 res->volatil = MEM_VOLATILE_P (x);
218
219 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
220 We can not just fall through here since then we would be confused
221 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
222 traditional asms unlike their normal usage. */
223
224 for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
225 mark_referenced_resources (ASM_OPERANDS_INPUT (x, i), res, 0);
226 return;
227
228 case CALL:
229 /* The first operand will be a (MEM (xxx)) but doesn't really reference
230 memory. The second operand may be referenced, though. */
231 mark_referenced_resources (XEXP (XEXP (x, 0), 0), res, 0);
232 mark_referenced_resources (XEXP (x, 1), res, 0);
233 return;
234
235 case SET:
236 /* Usually, the first operand of SET is set, not referenced. But
237 registers used to access memory are referenced. SET_DEST is
238 also referenced if it is a ZERO_EXTRACT or SIGN_EXTRACT. */
239
240 mark_referenced_resources (SET_SRC (x), res, 0);
241
242 x = SET_DEST (x);
243 if (GET_CODE (x) == SIGN_EXTRACT || GET_CODE (x) == ZERO_EXTRACT)
244 mark_referenced_resources (x, res, 0);
245 else if (GET_CODE (x) == SUBREG)
246 x = SUBREG_REG (x);
247 if (GET_CODE (x) == MEM)
248 mark_referenced_resources (XEXP (x, 0), res, 0);
249 return;
250
251 case CLOBBER:
252 return;
253
254 case CALL_INSN:
255 if (include_delayed_effects)
256 {
257 /* A CALL references memory, the frame pointer if it exists, the
258 stack pointer, any global registers and any registers given in
259 USE insns immediately in front of the CALL.
260
261 However, we may have moved some of the parameter loading insns
262 into the delay slot of this CALL. If so, the USE's for them
263 don't count and should be skipped. */
264 rtx insn = PREV_INSN (x);
265 rtx sequence = 0;
266 int seq_size = 0;
267 rtx next = NEXT_INSN (x);
268 int i;
269
270 /* If we are part of a delay slot sequence, point at the SEQUENCE. */
271 if (NEXT_INSN (insn) != x)
272 {
273 next = NEXT_INSN (NEXT_INSN (insn));
274 sequence = PATTERN (NEXT_INSN (insn));
275 seq_size = XVECLEN (sequence, 0);
276 if (GET_CODE (sequence) != SEQUENCE)
277 abort ();
278 }
279
280 res->memory = 1;
281 SET_HARD_REG_BIT (res->regs, STACK_POINTER_REGNUM);
282 if (frame_pointer_needed)
283 {
284 SET_HARD_REG_BIT (res->regs, FRAME_POINTER_REGNUM);
285 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
286 SET_HARD_REG_BIT (res->regs, HARD_FRAME_POINTER_REGNUM);
287 #endif
288 }
289
290 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
291 if (global_regs[i])
292 SET_HARD_REG_BIT (res->regs, i);
293
294 /* Check for a NOTE_INSN_SETJMP. If it exists, then we must
295 assume that this call can need any register.
296
297 This is done to be more conservative about how we handle setjmp.
298 We assume that they both use and set all registers. Using all
299 registers ensures that a register will not be considered dead
300 just because it crosses a setjmp call. A register should be
301 considered dead only if the setjmp call returns non-zero. */
302 if (next && GET_CODE (next) == NOTE
303 && NOTE_LINE_NUMBER (next) == NOTE_INSN_SETJMP)
304 SET_HARD_REG_SET (res->regs);
305
306 {
307 rtx link;
308
309 for (link = CALL_INSN_FUNCTION_USAGE (x);
310 link;
311 link = XEXP (link, 1))
312 if (GET_CODE (XEXP (link, 0)) == USE)
313 {
314 for (i = 1; i < seq_size; i++)
315 {
316 rtx slot_pat = PATTERN (XVECEXP (sequence, 0, i));
317 if (GET_CODE (slot_pat) == SET
318 && rtx_equal_p (SET_DEST (slot_pat),
319 SET_DEST (XEXP (link, 0))))
320 break;
321 }
322 if (i >= seq_size)
323 mark_referenced_resources (SET_DEST (XEXP (link, 0)),
324 res, 0);
325 }
326 }
327 }
328
329 /* ... fall through to other INSN processing ... */
330
331 case INSN:
332 case JUMP_INSN:
333
334 #ifdef INSN_REFERENCES_ARE_DELAYED
335 if (! include_delayed_effects
336 && INSN_REFERENCES_ARE_DELAYED (x))
337 return;
338 #endif
339
340 /* No special processing, just speed up. */
341 mark_referenced_resources (PATTERN (x), res, include_delayed_effects);
342 return;
343
344 default:
345 break;
346 }
347
348 /* Process each sub-expression and flag what it needs. */
349 format_ptr = GET_RTX_FORMAT (code);
350 for (i = 0; i < GET_RTX_LENGTH (code); i++)
351 switch (*format_ptr++)
352 {
353 case 'e':
354 mark_referenced_resources (XEXP (x, i), res, include_delayed_effects);
355 break;
356
357 case 'E':
358 for (j = 0; j < XVECLEN (x, i); j++)
359 mark_referenced_resources (XVECEXP (x, i, j), res,
360 include_delayed_effects);
361 break;
362 }
363 }
364 \f
365 /* A subroutine of mark_target_live_regs. Search forward from TARGET
366 looking for registers that are set before they are used. These are dead.
367 Stop after passing a few conditional jumps, and/or a small
368 number of unconditional branches. */
369
370 static rtx
371 find_dead_or_set_registers (target, res, jump_target, jump_count, set, needed)
372 rtx target;
373 struct resources *res;
374 rtx *jump_target;
375 int jump_count;
376 struct resources set, needed;
377 {
378 HARD_REG_SET scratch;
379 rtx insn, next;
380 rtx jump_insn = 0;
381 int i;
382
383 for (insn = target; insn; insn = next)
384 {
385 rtx this_jump_insn = insn;
386
387 next = NEXT_INSN (insn);
388 switch (GET_CODE (insn))
389 {
390 case CODE_LABEL:
391 /* After a label, any pending dead registers that weren't yet
392 used can be made dead. */
393 AND_COMPL_HARD_REG_SET (pending_dead_regs, needed.regs);
394 AND_COMPL_HARD_REG_SET (res->regs, pending_dead_regs);
395 CLEAR_HARD_REG_SET (pending_dead_regs);
396
397 continue;
398
399 case BARRIER:
400 case NOTE:
401 continue;
402
403 case INSN:
404 if (GET_CODE (PATTERN (insn)) == USE)
405 {
406 /* If INSN is a USE made by update_block, we care about the
407 underlying insn. Any registers set by the underlying insn
408 are live since the insn is being done somewhere else. */
409 if (GET_RTX_CLASS (GET_CODE (XEXP (PATTERN (insn), 0))) == 'i')
410 mark_set_resources (XEXP (PATTERN (insn), 0), res, 0, 1);
411
412 /* All other USE insns are to be ignored. */
413 continue;
414 }
415 else if (GET_CODE (PATTERN (insn)) == CLOBBER)
416 continue;
417 else if (GET_CODE (PATTERN (insn)) == SEQUENCE)
418 {
419 /* An unconditional jump can be used to fill the delay slot
420 of a call, so search for a JUMP_INSN in any position. */
421 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
422 {
423 this_jump_insn = XVECEXP (PATTERN (insn), 0, i);
424 if (GET_CODE (this_jump_insn) == JUMP_INSN)
425 break;
426 }
427 }
428
429 default:
430 break;
431 }
432
433 if (GET_CODE (this_jump_insn) == JUMP_INSN)
434 {
435 if (jump_count++ < 10)
436 {
437 if (simplejump_p (this_jump_insn)
438 || GET_CODE (PATTERN (this_jump_insn)) == RETURN)
439 {
440 next = JUMP_LABEL (this_jump_insn);
441 if (jump_insn == 0)
442 {
443 jump_insn = insn;
444 if (jump_target)
445 *jump_target = JUMP_LABEL (this_jump_insn);
446 }
447 }
448 else if (condjump_p (this_jump_insn)
449 || condjump_in_parallel_p (this_jump_insn))
450 {
451 struct resources target_set, target_res;
452 struct resources fallthrough_res;
453
454 /* We can handle conditional branches here by following
455 both paths, and then IOR the results of the two paths
456 together, which will give us registers that are dead
457 on both paths. Since this is expensive, we give it
458 a much higher cost than unconditional branches. The
459 cost was chosen so that we will follow at most 1
460 conditional branch. */
461
462 jump_count += 4;
463 if (jump_count >= 10)
464 break;
465
466 mark_referenced_resources (insn, &needed, 1);
467
468 /* For an annulled branch, mark_set_resources ignores slots
469 filled by instructions from the target. This is correct
470 if the branch is not taken. Since we are following both
471 paths from the branch, we must also compute correct info
472 if the branch is taken. We do this by inverting all of
473 the INSN_FROM_TARGET_P bits, calling mark_set_resources,
474 and then inverting the INSN_FROM_TARGET_P bits again. */
475
476 if (GET_CODE (PATTERN (insn)) == SEQUENCE
477 && INSN_ANNULLED_BRANCH_P (this_jump_insn))
478 {
479 for (i = 1; i < XVECLEN (PATTERN (insn), 0); i++)
480 INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn), 0, i))
481 = ! INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn), 0, i));
482
483 target_set = set;
484 mark_set_resources (insn, &target_set, 0, 1);
485
486 for (i = 1; i < XVECLEN (PATTERN (insn), 0); i++)
487 INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn), 0, i))
488 = ! INSN_FROM_TARGET_P (XVECEXP (PATTERN (insn), 0, i));
489
490 mark_set_resources (insn, &set, 0, 1);
491 }
492 else
493 {
494 mark_set_resources (insn, &set, 0, 1);
495 target_set = set;
496 }
497
498 target_res = *res;
499 COPY_HARD_REG_SET (scratch, target_set.regs);
500 AND_COMPL_HARD_REG_SET (scratch, needed.regs);
501 AND_COMPL_HARD_REG_SET (target_res.regs, scratch);
502
503 fallthrough_res = *res;
504 COPY_HARD_REG_SET (scratch, set.regs);
505 AND_COMPL_HARD_REG_SET (scratch, needed.regs);
506 AND_COMPL_HARD_REG_SET (fallthrough_res.regs, scratch);
507
508 find_dead_or_set_registers (JUMP_LABEL (this_jump_insn),
509 &target_res, 0, jump_count,
510 target_set, needed);
511 find_dead_or_set_registers (next,
512 &fallthrough_res, 0, jump_count,
513 set, needed);
514 IOR_HARD_REG_SET (fallthrough_res.regs, target_res.regs);
515 AND_HARD_REG_SET (res->regs, fallthrough_res.regs);
516 break;
517 }
518 else
519 break;
520 }
521 else
522 {
523 /* Don't try this optimization if we expired our jump count
524 above, since that would mean there may be an infinite loop
525 in the function being compiled. */
526 jump_insn = 0;
527 break;
528 }
529 }
530
531 mark_referenced_resources (insn, &needed, 1);
532 mark_set_resources (insn, &set, 0, 1);
533
534 COPY_HARD_REG_SET (scratch, set.regs);
535 AND_COMPL_HARD_REG_SET (scratch, needed.regs);
536 AND_COMPL_HARD_REG_SET (res->regs, scratch);
537 }
538
539 return jump_insn;
540 }
541 \f
542 /* Given X, a part of an insn, and a pointer to a `struct resource',
543 RES, indicate which resources are modified by the insn. If
544 INCLUDE_DELAYED_EFFECTS is nonzero, also mark resources potentially
545 set by the called routine.
546
547 If IN_DEST is nonzero, it means we are inside a SET. Otherwise,
548 objects are being referenced instead of set.
549
550 We never mark the insn as modifying the condition code unless it explicitly
551 SETs CC0 even though this is not totally correct. The reason for this is
552 that we require a SET of CC0 to immediately precede the reference to CC0.
553 So if some other insn sets CC0 as a side-effect, we know it cannot affect
554 our computation and thus may be placed in a delay slot. */
555
556 void
557 mark_set_resources (x, res, in_dest, include_delayed_effects)
558 register rtx x;
559 register struct resources *res;
560 int in_dest;
561 int include_delayed_effects;
562 {
563 register enum rtx_code code;
564 register int i, j;
565 register char *format_ptr;
566
567 restart:
568
569 code = GET_CODE (x);
570
571 switch (code)
572 {
573 case NOTE:
574 case BARRIER:
575 case CODE_LABEL:
576 case USE:
577 case CONST_INT:
578 case CONST_DOUBLE:
579 case LABEL_REF:
580 case SYMBOL_REF:
581 case CONST:
582 case PC:
583 /* These don't set any resources. */
584 return;
585
586 case CC0:
587 if (in_dest)
588 res->cc = 1;
589 return;
590
591 case CALL_INSN:
592 /* Called routine modifies the condition code, memory, any registers
593 that aren't saved across calls, global registers and anything
594 explicitly CLOBBERed immediately after the CALL_INSN. */
595
596 if (include_delayed_effects)
597 {
598 rtx next = NEXT_INSN (x);
599 rtx prev = PREV_INSN (x);
600 rtx link;
601
602 res->cc = res->memory = 1;
603 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
604 if (call_used_regs[i] || global_regs[i])
605 SET_HARD_REG_BIT (res->regs, i);
606
607 /* If X is part of a delay slot sequence, then NEXT should be
608 the first insn after the sequence. */
609 if (NEXT_INSN (prev) != x)
610 next = NEXT_INSN (NEXT_INSN (prev));
611
612 for (link = CALL_INSN_FUNCTION_USAGE (x);
613 link; link = XEXP (link, 1))
614 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
615 mark_set_resources (SET_DEST (XEXP (link, 0)), res, 1, 0);
616
617 /* Check for a NOTE_INSN_SETJMP. If it exists, then we must
618 assume that this call can clobber any register. */
619 if (next && GET_CODE (next) == NOTE
620 && NOTE_LINE_NUMBER (next) == NOTE_INSN_SETJMP)
621 SET_HARD_REG_SET (res->regs);
622 }
623
624 /* ... and also what its RTL says it modifies, if anything. */
625
626 case JUMP_INSN:
627 case INSN:
628
629 /* An insn consisting of just a CLOBBER (or USE) is just for flow
630 and doesn't actually do anything, so we ignore it. */
631
632 #ifdef INSN_SETS_ARE_DELAYED
633 if (! include_delayed_effects
634 && INSN_SETS_ARE_DELAYED (x))
635 return;
636 #endif
637
638 x = PATTERN (x);
639 if (GET_CODE (x) != USE && GET_CODE (x) != CLOBBER)
640 goto restart;
641 return;
642
643 case SET:
644 /* If the source of a SET is a CALL, this is actually done by
645 the called routine. So only include it if we are to include the
646 effects of the calling routine. */
647
648 mark_set_resources (SET_DEST (x), res,
649 (include_delayed_effects
650 || GET_CODE (SET_SRC (x)) != CALL),
651 0);
652
653 mark_set_resources (SET_SRC (x), res, 0, 0);
654 return;
655
656 case CLOBBER:
657 mark_set_resources (XEXP (x, 0), res, 1, 0);
658 return;
659
660 case SEQUENCE:
661 for (i = 0; i < XVECLEN (x, 0); i++)
662 if (! (INSN_ANNULLED_BRANCH_P (XVECEXP (x, 0, 0))
663 && INSN_FROM_TARGET_P (XVECEXP (x, 0, i))))
664 mark_set_resources (XVECEXP (x, 0, i), res, 0,
665 include_delayed_effects);
666 return;
667
668 case POST_INC:
669 case PRE_INC:
670 case POST_DEC:
671 case PRE_DEC:
672 mark_set_resources (XEXP (x, 0), res, 1, 0);
673 return;
674
675 case ZERO_EXTRACT:
676 mark_set_resources (XEXP (x, 0), res, in_dest, 0);
677 mark_set_resources (XEXP (x, 1), res, 0, 0);
678 mark_set_resources (XEXP (x, 2), res, 0, 0);
679 return;
680
681 case MEM:
682 if (in_dest)
683 {
684 res->memory = 1;
685 res->unch_memory = RTX_UNCHANGING_P (x);
686 res->volatil = MEM_VOLATILE_P (x);
687 }
688
689 mark_set_resources (XEXP (x, 0), res, 0, 0);
690 return;
691
692 case SUBREG:
693 if (in_dest)
694 {
695 if (GET_CODE (SUBREG_REG (x)) != REG)
696 mark_set_resources (SUBREG_REG (x), res,
697 in_dest, include_delayed_effects);
698 else
699 {
700 int regno = REGNO (SUBREG_REG (x)) + SUBREG_WORD (x);
701 int last_regno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
702 for (i = regno; i < last_regno; i++)
703 SET_HARD_REG_BIT (res->regs, i);
704 }
705 }
706 return;
707
708 case REG:
709 if (in_dest)
710 for (i = 0; i < HARD_REGNO_NREGS (REGNO (x), GET_MODE (x)); i++)
711 SET_HARD_REG_BIT (res->regs, REGNO (x) + i);
712 return;
713
714 default:
715 break;
716 }
717
718 /* Process each sub-expression and flag what it needs. */
719 format_ptr = GET_RTX_FORMAT (code);
720 for (i = 0; i < GET_RTX_LENGTH (code); i++)
721 switch (*format_ptr++)
722 {
723 case 'e':
724 mark_set_resources (XEXP (x, i), res, in_dest, include_delayed_effects);
725 break;
726
727 case 'E':
728 for (j = 0; j < XVECLEN (x, i); j++)
729 mark_set_resources (XVECEXP (x, i, j), res, in_dest,
730 include_delayed_effects);
731 break;
732 }
733 }
734 \f
735 /* Set the resources that are live at TARGET.
736
737 If TARGET is zero, we refer to the end of the current function and can
738 return our precomputed value.
739
740 Otherwise, we try to find out what is live by consulting the basic block
741 information. This is tricky, because we must consider the actions of
742 reload and jump optimization, which occur after the basic block information
743 has been computed.
744
745 Accordingly, we proceed as follows::
746
747 We find the previous BARRIER and look at all immediately following labels
748 (with no intervening active insns) to see if any of them start a basic
749 block. If we hit the start of the function first, we use block 0.
750
751 Once we have found a basic block and a corresponding first insns, we can
752 accurately compute the live status from basic_block_live_regs and
753 reg_renumber. (By starting at a label following a BARRIER, we are immune
754 to actions taken by reload and jump.) Then we scan all insns between
755 that point and our target. For each CLOBBER (or for call-clobbered regs
756 when we pass a CALL_INSN), mark the appropriate registers are dead. For
757 a SET, mark them as live.
758
759 We have to be careful when using REG_DEAD notes because they are not
760 updated by such things as find_equiv_reg. So keep track of registers
761 marked as dead that haven't been assigned to, and mark them dead at the
762 next CODE_LABEL since reload and jump won't propagate values across labels.
763
764 If we cannot find the start of a basic block (should be a very rare
765 case, if it can happen at all), mark everything as potentially live.
766
767 Next, scan forward from TARGET looking for things set or clobbered
768 before they are used. These are not live.
769
770 Because we can be called many times on the same target, save our results
771 in a hash table indexed by INSN_UID. This is only done if the function
772 init_resource_info () was invoked before we are called. */
773
774 void
775 mark_target_live_regs (insns, target, res)
776 rtx insns;
777 rtx target;
778 struct resources *res;
779 {
780 int b = -1;
781 int i;
782 struct target_info *tinfo = NULL;
783 rtx insn;
784 rtx jump_insn = 0;
785 rtx jump_target;
786 HARD_REG_SET scratch;
787 struct resources set, needed;
788
789 /* Handle end of function. */
790 if (target == 0)
791 {
792 *res = end_of_function_needs;
793 return;
794 }
795
796 /* We have to assume memory is needed, but the CC isn't. */
797 res->memory = 1;
798 res->volatil = res->unch_memory = 0;
799 res->cc = 0;
800
801 /* See if we have computed this value already. */
802 if (target_hash_table != NULL)
803 {
804 for (tinfo = target_hash_table[INSN_UID (target) % TARGET_HASH_PRIME];
805 tinfo; tinfo = tinfo->next)
806 if (tinfo->uid == INSN_UID (target))
807 break;
808
809 /* Start by getting the basic block number. If we have saved
810 information, we can get it from there unless the insn at the
811 start of the basic block has been deleted. */
812 if (tinfo && tinfo->block != -1
813 && ! INSN_DELETED_P (BLOCK_HEAD (tinfo->block)))
814 b = tinfo->block;
815 }
816
817 if (b == -1)
818 b = find_basic_block (target);
819
820 if (target_hash_table != NULL)
821 {
822 if (tinfo)
823 {
824 /* If the information is up-to-date, use it. Otherwise, we will
825 update it below. */
826 if (b == tinfo->block && b != -1 && tinfo->bb_tick == bb_ticks[b])
827 {
828 COPY_HARD_REG_SET (res->regs, tinfo->live_regs);
829 return;
830 }
831 }
832 else
833 {
834 /* Allocate a place to put our results and chain it into the
835 hash table. */
836 tinfo = (struct target_info *) oballoc (sizeof (struct target_info));
837 tinfo->uid = INSN_UID (target);
838 tinfo->block = b;
839 tinfo->next = target_hash_table[INSN_UID (target) % TARGET_HASH_PRIME];
840 target_hash_table[INSN_UID (target) % TARGET_HASH_PRIME] = tinfo;
841 }
842 }
843
844 CLEAR_HARD_REG_SET (pending_dead_regs);
845
846 /* If we found a basic block, get the live registers from it and update
847 them with anything set or killed between its start and the insn before
848 TARGET. Otherwise, we must assume everything is live. */
849 if (b != -1)
850 {
851 regset regs_live = basic_block_live_at_start[b];
852 int j;
853 int regno;
854 rtx start_insn, stop_insn;
855
856 /* Compute hard regs live at start of block -- this is the real hard regs
857 marked live, plus live pseudo regs that have been renumbered to
858 hard regs. */
859
860 REG_SET_TO_HARD_REG_SET (current_live_regs, regs_live);
861
862 EXECUTE_IF_SET_IN_REG_SET
863 (regs_live, FIRST_PSEUDO_REGISTER, i,
864 {
865 if ((regno = reg_renumber[i]) >= 0)
866 for (j = regno;
867 j < regno + HARD_REGNO_NREGS (regno,
868 PSEUDO_REGNO_MODE (i));
869 j++)
870 SET_HARD_REG_BIT (current_live_regs, j);
871 });
872
873 /* Get starting and ending insn, handling the case where each might
874 be a SEQUENCE. */
875 start_insn = (b == 0 ? insns : BLOCK_HEAD (b));
876 stop_insn = target;
877
878 if (GET_CODE (start_insn) == INSN
879 && GET_CODE (PATTERN (start_insn)) == SEQUENCE)
880 start_insn = XVECEXP (PATTERN (start_insn), 0, 0);
881
882 if (GET_CODE (stop_insn) == INSN
883 && GET_CODE (PATTERN (stop_insn)) == SEQUENCE)
884 stop_insn = next_insn (PREV_INSN (stop_insn));
885
886 for (insn = start_insn; insn != stop_insn;
887 insn = next_insn_no_annul (insn))
888 {
889 rtx link;
890 rtx real_insn = insn;
891
892 /* If this insn is from the target of a branch, it isn't going to
893 be used in the sequel. If it is used in both cases, this
894 test will not be true. */
895 if (INSN_FROM_TARGET_P (insn))
896 continue;
897
898 /* If this insn is a USE made by update_block, we care about the
899 underlying insn. */
900 if (GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE
901 && GET_RTX_CLASS (GET_CODE (XEXP (PATTERN (insn), 0))) == 'i')
902 real_insn = XEXP (PATTERN (insn), 0);
903
904 if (GET_CODE (real_insn) == CALL_INSN)
905 {
906 /* CALL clobbers all call-used regs that aren't fixed except
907 sp, ap, and fp. Do this before setting the result of the
908 call live. */
909 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
910 if (call_used_regs[i]
911 && i != STACK_POINTER_REGNUM && i != FRAME_POINTER_REGNUM
912 && i != ARG_POINTER_REGNUM
913 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
914 && i != HARD_FRAME_POINTER_REGNUM
915 #endif
916 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
917 && ! (i == ARG_POINTER_REGNUM && fixed_regs[i])
918 #endif
919 #ifdef PIC_OFFSET_TABLE_REGNUM
920 && ! (i == PIC_OFFSET_TABLE_REGNUM && flag_pic)
921 #endif
922 )
923 CLEAR_HARD_REG_BIT (current_live_regs, i);
924
925 /* A CALL_INSN sets any global register live, since it may
926 have been modified by the call. */
927 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
928 if (global_regs[i])
929 SET_HARD_REG_BIT (current_live_regs, i);
930 }
931
932 /* Mark anything killed in an insn to be deadened at the next
933 label. Ignore USE insns; the only REG_DEAD notes will be for
934 parameters. But they might be early. A CALL_INSN will usually
935 clobber registers used for parameters. It isn't worth bothering
936 with the unlikely case when it won't. */
937 if ((GET_CODE (real_insn) == INSN
938 && GET_CODE (PATTERN (real_insn)) != USE
939 && GET_CODE (PATTERN (real_insn)) != CLOBBER)
940 || GET_CODE (real_insn) == JUMP_INSN
941 || GET_CODE (real_insn) == CALL_INSN)
942 {
943 for (link = REG_NOTES (real_insn); link; link = XEXP (link, 1))
944 if (REG_NOTE_KIND (link) == REG_DEAD
945 && GET_CODE (XEXP (link, 0)) == REG
946 && REGNO (XEXP (link, 0)) < FIRST_PSEUDO_REGISTER)
947 {
948 int first_regno = REGNO (XEXP (link, 0));
949 int last_regno
950 = (first_regno
951 + HARD_REGNO_NREGS (first_regno,
952 GET_MODE (XEXP (link, 0))));
953
954 for (i = first_regno; i < last_regno; i++)
955 SET_HARD_REG_BIT (pending_dead_regs, i);
956 }
957
958 note_stores (PATTERN (real_insn), update_live_status);
959
960 /* If any registers were unused after this insn, kill them.
961 These notes will always be accurate. */
962 for (link = REG_NOTES (real_insn); link; link = XEXP (link, 1))
963 if (REG_NOTE_KIND (link) == REG_UNUSED
964 && GET_CODE (XEXP (link, 0)) == REG
965 && REGNO (XEXP (link, 0)) < FIRST_PSEUDO_REGISTER)
966 {
967 int first_regno = REGNO (XEXP (link, 0));
968 int last_regno
969 = (first_regno
970 + HARD_REGNO_NREGS (first_regno,
971 GET_MODE (XEXP (link, 0))));
972
973 for (i = first_regno; i < last_regno; i++)
974 CLEAR_HARD_REG_BIT (current_live_regs, i);
975 }
976 }
977
978 else if (GET_CODE (real_insn) == CODE_LABEL)
979 {
980 /* A label clobbers the pending dead registers since neither
981 reload nor jump will propagate a value across a label. */
982 AND_COMPL_HARD_REG_SET (current_live_regs, pending_dead_regs);
983 CLEAR_HARD_REG_SET (pending_dead_regs);
984 }
985
986 /* The beginning of the epilogue corresponds to the end of the
987 RTL chain when there are no epilogue insns. Certain resources
988 are implicitly required at that point. */
989 else if (GET_CODE (real_insn) == NOTE
990 && NOTE_LINE_NUMBER (real_insn) == NOTE_INSN_EPILOGUE_BEG)
991 IOR_HARD_REG_SET (current_live_regs, start_of_epilogue_needs.regs);
992 }
993
994 COPY_HARD_REG_SET (res->regs, current_live_regs);
995 if (tinfo != NULL)
996 {
997 tinfo->block = b;
998 tinfo->bb_tick = bb_ticks[b];
999 }
1000 }
1001 else
1002 /* We didn't find the start of a basic block. Assume everything
1003 in use. This should happen only extremely rarely. */
1004 SET_HARD_REG_SET (res->regs);
1005
1006 CLEAR_RESOURCE (&set);
1007 CLEAR_RESOURCE (&needed);
1008
1009 jump_insn = find_dead_or_set_registers (target, res, &jump_target, 0,
1010 set, needed);
1011
1012 /* If we hit an unconditional branch, we have another way of finding out
1013 what is live: we can see what is live at the branch target and include
1014 anything used but not set before the branch. The only things that are
1015 live are those that are live using the above test and the test below. */
1016
1017 if (jump_insn)
1018 {
1019 struct resources new_resources;
1020 rtx stop_insn = next_active_insn (jump_insn);
1021
1022 mark_target_live_regs (insns, next_active_insn (jump_target),
1023 &new_resources);
1024 CLEAR_RESOURCE (&set);
1025 CLEAR_RESOURCE (&needed);
1026
1027 /* Include JUMP_INSN in the needed registers. */
1028 for (insn = target; insn != stop_insn; insn = next_active_insn (insn))
1029 {
1030 mark_referenced_resources (insn, &needed, 1);
1031
1032 COPY_HARD_REG_SET (scratch, needed.regs);
1033 AND_COMPL_HARD_REG_SET (scratch, set.regs);
1034 IOR_HARD_REG_SET (new_resources.regs, scratch);
1035
1036 mark_set_resources (insn, &set, 0, 1);
1037 }
1038
1039 AND_HARD_REG_SET (res->regs, new_resources.regs);
1040 }
1041
1042 if (tinfo != NULL)
1043 {
1044 COPY_HARD_REG_SET (tinfo->live_regs, res->regs);
1045 }
1046 }
1047 \f
1048 /* Initialize the resources required by mark_target_live_regs ().
1049 This should be invoked before the first call to mark_target_live_regs. */
1050
1051 void
1052 init_resource_info (epilogue_insn)
1053 rtx epilogue_insn;
1054 {
1055 int i;
1056
1057 /* Indicate what resources are required to be valid at the end of the current
1058 function. The condition code never is and memory always is. If the
1059 frame pointer is needed, it is and so is the stack pointer unless
1060 EXIT_IGNORE_STACK is non-zero. If the frame pointer is not needed, the
1061 stack pointer is. Registers used to return the function value are
1062 needed. Registers holding global variables are needed. */
1063
1064 end_of_function_needs.cc = 0;
1065 end_of_function_needs.memory = 1;
1066 end_of_function_needs.unch_memory = 0;
1067 CLEAR_HARD_REG_SET (end_of_function_needs.regs);
1068
1069 if (frame_pointer_needed)
1070 {
1071 SET_HARD_REG_BIT (end_of_function_needs.regs, FRAME_POINTER_REGNUM);
1072 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1073 SET_HARD_REG_BIT (end_of_function_needs.regs, HARD_FRAME_POINTER_REGNUM);
1074 #endif
1075 #ifdef EXIT_IGNORE_STACK
1076 if (! EXIT_IGNORE_STACK
1077 || current_function_sp_is_unchanging)
1078 #endif
1079 SET_HARD_REG_BIT (end_of_function_needs.regs, STACK_POINTER_REGNUM);
1080 }
1081 else
1082 SET_HARD_REG_BIT (end_of_function_needs.regs, STACK_POINTER_REGNUM);
1083
1084 if (current_function_return_rtx != 0)
1085 mark_referenced_resources (current_function_return_rtx,
1086 &end_of_function_needs, 1);
1087
1088 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1089 if (global_regs[i]
1090 #ifdef EPILOGUE_USES
1091 || EPILOGUE_USES (i)
1092 #endif
1093 )
1094 SET_HARD_REG_BIT (end_of_function_needs.regs, i);
1095
1096 /* The registers required to be live at the end of the function are
1097 represented in the flow information as being dead just prior to
1098 reaching the end of the function. For example, the return of a value
1099 might be represented by a USE of the return register immediately
1100 followed by an unconditional jump to the return label where the
1101 return label is the end of the RTL chain. The end of the RTL chain
1102 is then taken to mean that the return register is live.
1103
1104 This sequence is no longer maintained when epilogue instructions are
1105 added to the RTL chain. To reconstruct the original meaning, the
1106 start of the epilogue (NOTE_INSN_EPILOGUE_BEG) is regarded as the
1107 point where these registers become live (start_of_epilogue_needs).
1108 If epilogue instructions are present, the registers set by those
1109 instructions won't have been processed by flow. Thus, those
1110 registers are additionally required at the end of the RTL chain
1111 (end_of_function_needs). */
1112
1113 start_of_epilogue_needs = end_of_function_needs;
1114
1115 while ((epilogue_insn = next_nonnote_insn (epilogue_insn)))
1116 mark_set_resources (epilogue_insn, &end_of_function_needs, 0, 1);
1117
1118 /* Allocate and initialize the tables used by mark_target_live_regs. */
1119 target_hash_table
1120 = (struct target_info **) xmalloc ((TARGET_HASH_PRIME
1121 * sizeof (struct target_info *)));
1122 bzero ((char *) target_hash_table,
1123 TARGET_HASH_PRIME * sizeof (struct target_info *));
1124
1125 bb_ticks = (int *) xmalloc (n_basic_blocks * sizeof (int));
1126 bzero ((char *) bb_ticks, n_basic_blocks * sizeof (int));
1127 }
1128 \f
1129 /* Free up the resources allcated to mark_target_live_regs (). This
1130 should be invoked after the last call to mark_target_live_regs (). */
1131
1132 void
1133 free_resource_info ()
1134 {
1135 if (target_hash_table != NULL)
1136 {
1137 free (target_hash_table);
1138 target_hash_table = NULL;
1139 }
1140
1141 if (bb_ticks != NULL)
1142 {
1143 free (bb_ticks);
1144 bb_ticks = NULL;
1145 }
1146 }
1147 \f
1148 /* Clear any hashed information that we have stored for INSN. */
1149
1150 void
1151 clear_hashed_info_for_insn (insn)
1152 rtx insn;
1153 {
1154 struct target_info *tinfo;
1155
1156 if (target_hash_table != NULL)
1157 {
1158 for (tinfo = target_hash_table[INSN_UID (insn) % TARGET_HASH_PRIME];
1159 tinfo; tinfo = tinfo->next)
1160 if (tinfo->uid == INSN_UID (insn))
1161 break;
1162
1163 if (tinfo)
1164 tinfo->block = -1;
1165 }
1166 }
1167 \f
1168 /* Increment the tick count for the basic block that contains INSN. */
1169
1170 void
1171 incr_ticks_for_insn (insn)
1172 rtx insn;
1173 {
1174 int b = find_basic_block (insn);
1175
1176 if (b != -1)
1177 bb_ticks[b]++;
1178 }
1179 \f
1180 /* Add TRIAL to the set of resources used at the end of the current
1181 function. */
1182 void
1183 mark_end_of_function_resources (trial, include_delayed_effects)
1184 rtx trial;
1185 int include_delayed_effects;
1186 {
1187 mark_referenced_resources (trial, &end_of_function_needs,
1188 include_delayed_effects);
1189 }
1190 \f
1191 /* Try to find an available hard register of mode MODE at
1192 CURRENT_INSN, matching the register class in CLASS_STR. Registers
1193 that already have bits set in REG_SET will not be considered.
1194
1195 If an appropriate register is available, it will be returned and the
1196 corresponding bit(s) in REG_SET will be set; otherwise, NULL_RTX is
1197 returned. */
1198
1199 rtx
1200 find_free_register (current_insn, class_str, mode, reg_set)
1201 rtx current_insn;
1202 char *class_str;
1203 int mode;
1204 HARD_REG_SET *reg_set;
1205 {
1206 int i, j;
1207 struct resources used;
1208 unsigned char clet = class_str[0];
1209 enum reg_class class
1210 = (clet == 'r' ? GENERAL_REGS : REG_CLASS_FROM_LETTER (clet));
1211
1212 mark_target_live_regs (get_insns (), current_insn, &used);
1213
1214 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1215 {
1216 int success = 1;
1217
1218 if (! TEST_HARD_REG_BIT (reg_class_contents[class], i))
1219 continue;
1220 for (j = HARD_REGNO_NREGS (i, mode) - 1; j >= 0; j--)
1221 {
1222 if (TEST_HARD_REG_BIT (*reg_set, i + j)
1223 || TEST_HARD_REG_BIT (used.regs, i + j))
1224 {
1225 success = 0;
1226 break;
1227 }
1228 }
1229 if (success)
1230 {
1231 for (j = HARD_REGNO_NREGS (i, mode) - 1; j >= 0; j--)
1232 {
1233 SET_HARD_REG_BIT (*reg_set, i + j);
1234 }
1235 return gen_rtx_REG (mode, i);
1236 }
1237 }
1238 return NULL_RTX;
1239 }