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