OSDN Git Service

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