OSDN Git Service

* rtlanal.c (rtx_unstable_p): Use CONSTANT_P.
[pf3gnuchains/gcc-fork.git] / gcc / rtlanal.c
1 /* Analyze RTL for C-Compiler
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000 Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22
23 #include "config.h"
24 #include "system.h"
25 #include "rtl.h"
26
27 static int rtx_addr_can_trap_p  PARAMS ((rtx));
28 static void reg_set_p_1         PARAMS ((rtx, rtx, void *));
29 static void insn_dependent_p_1  PARAMS ((rtx, rtx, void *));
30 static void reg_set_last_1      PARAMS ((rtx, rtx, void *));
31
32
33 /* Forward declarations */
34 static int jmp_uses_reg_or_mem          PARAMS ((rtx));
35
36 /* Bit flags that specify the machine subtype we are compiling for.
37    Bits are tested using macros TARGET_... defined in the tm.h file
38    and set by `-m...' switches.  Must be defined in rtlanal.c.  */
39
40 int target_flags;
41 \f
42 /* Return 1 if the value of X is unstable
43    (would be different at a different point in the program).
44    The frame pointer, arg pointer, etc. are considered stable
45    (within one function) and so is anything marked `unchanging'.  */
46
47 int
48 rtx_unstable_p (x)
49      rtx x;
50 {
51   register RTX_CODE code = GET_CODE (x);
52   register int i;
53   register const char *fmt;
54
55   if (code == MEM)
56     return ! RTX_UNCHANGING_P (x);
57
58   if (code == QUEUED)
59     return 1;
60
61   if (CONSTANT_P (x))
62     return 0;
63
64   if (code == REG)
65     return ! (REGNO (x) == FRAME_POINTER_REGNUM
66               || REGNO (x) == HARD_FRAME_POINTER_REGNUM
67               || REGNO (x) == ARG_POINTER_REGNUM
68               || RTX_UNCHANGING_P (x));
69
70   fmt = GET_RTX_FORMAT (code);
71   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
72     if (fmt[i] == 'e')
73       {
74         if (rtx_unstable_p (XEXP (x, i)))
75           return 1;
76       }
77     else if (fmt[i] == 'E')
78       {
79         int j;
80         for (j = 0; j < XVECLEN (x, i); j++)
81           if (rtx_unstable_p (XVECEXP (x, i, j)))
82             return 1;
83       }
84
85   return 0;
86 }
87
88 /* Return 1 if X has a value that can vary even between two
89    executions of the program.  0 means X can be compared reliably
90    against certain constants or near-constants.
91    The frame pointer and the arg pointer are considered constant.  */
92
93 int
94 rtx_varies_p (x)
95      rtx x;
96 {
97   register RTX_CODE code = GET_CODE (x);
98   register int i;
99   register const char *fmt;
100
101   switch (code)
102     {
103     case MEM:
104     case QUEUED:
105       return 1;
106
107     case CONST:
108     case CONST_INT:
109     case CONST_DOUBLE:
110     case SYMBOL_REF:
111     case LABEL_REF:
112       return 0;
113
114     case REG:
115       /* Note that we have to test for the actual rtx used for the frame
116          and arg pointers and not just the register number in case we have
117          eliminated the frame and/or arg pointer and are using it
118          for pseudos.  */
119       return ! (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
120                 || x == arg_pointer_rtx || x == pic_offset_table_rtx);
121
122     case LO_SUM:
123       /* The operand 0 of a LO_SUM is considered constant
124          (in fact is it related specifically to operand 1).  */
125       return rtx_varies_p (XEXP (x, 1));
126       
127     default:
128       break;
129     }
130
131   fmt = GET_RTX_FORMAT (code);
132   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
133     if (fmt[i] == 'e')
134       {
135         if (rtx_varies_p (XEXP (x, i)))
136           return 1;
137       }
138     else if (fmt[i] == 'E')
139       {
140         int j;
141         for (j = 0; j < XVECLEN (x, i); j++)
142           if (rtx_varies_p (XVECEXP (x, i, j)))
143             return 1;
144       }
145
146   return 0;
147 }
148
149 /* Return 0 if the use of X as an address in a MEM can cause a trap.  */
150
151 static int
152 rtx_addr_can_trap_p (x)
153      register rtx x;
154 {
155   register enum rtx_code code = GET_CODE (x);
156
157   switch (code)
158     {
159     case SYMBOL_REF:
160     case LABEL_REF:
161       /* SYMBOL_REF is problematic due to the possible presence of
162          a #pragma weak, but to say that loads from symbols can trap is
163          *very* costly.  It's not at all clear what's best here.  For
164          now, we ignore the impact of #pragma weak.  */
165       return 0;
166
167     case REG:
168       /* As in rtx_varies_p, we have to use the actual rtx, not reg number.  */
169       return ! (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
170                 || x == stack_pointer_rtx || x == arg_pointer_rtx);
171
172     case CONST:
173       return rtx_addr_can_trap_p (XEXP (x, 0));
174
175     case PLUS:
176       /* An address is assumed not to trap if it is an address that can't
177          trap plus a constant integer.  */
178       return (rtx_addr_can_trap_p (XEXP (x, 0))
179               || GET_CODE (XEXP (x, 1)) != CONST_INT);
180
181     case LO_SUM:
182       return rtx_addr_can_trap_p (XEXP (x, 1));
183       
184     default:
185       break;
186     }
187
188   /* If it isn't one of the case above, it can cause a trap.  */
189   return 1;
190 }
191
192 /* Return 1 if X refers to a memory location whose address 
193    cannot be compared reliably with constant addresses,
194    or if X refers to a BLKmode memory object.  */
195
196 int
197 rtx_addr_varies_p (x)
198      rtx x;
199 {
200   register enum rtx_code code;
201   register int i;
202   register const char *fmt;
203
204   if (x == 0)
205     return 0;
206
207   code = GET_CODE (x);
208   if (code == MEM)
209     return GET_MODE (x) == BLKmode || rtx_varies_p (XEXP (x, 0));
210
211   fmt = GET_RTX_FORMAT (code);
212   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
213     if (fmt[i] == 'e')
214       {
215         if (rtx_addr_varies_p (XEXP (x, i)))
216           return 1;
217       }
218     else if (fmt[i] == 'E')
219       {
220         int j;
221         for (j = 0; j < XVECLEN (x, i); j++)
222           if (rtx_addr_varies_p (XVECEXP (x, i, j)))
223             return 1;
224       }
225   return 0;
226 }
227 \f
228 /* Return the value of the integer term in X, if one is apparent;
229    otherwise return 0.
230    Only obvious integer terms are detected.
231    This is used in cse.c with the `related_value' field.*/
232
233 HOST_WIDE_INT
234 get_integer_term (x)
235      rtx x;
236 {
237   if (GET_CODE (x) == CONST)
238     x = XEXP (x, 0);
239
240   if (GET_CODE (x) == MINUS
241       && GET_CODE (XEXP (x, 1)) == CONST_INT)
242     return - INTVAL (XEXP (x, 1));
243   if (GET_CODE (x) == PLUS
244       && GET_CODE (XEXP (x, 1)) == CONST_INT)
245     return INTVAL (XEXP (x, 1));
246   return 0;
247 }
248
249 /* If X is a constant, return the value sans apparent integer term;
250    otherwise return 0.
251    Only obvious integer terms are detected.  */
252
253 rtx
254 get_related_value (x)
255      rtx x;
256 {
257   if (GET_CODE (x) != CONST)
258     return 0;
259   x = XEXP (x, 0);
260   if (GET_CODE (x) == PLUS
261       && GET_CODE (XEXP (x, 1)) == CONST_INT)
262     return XEXP (x, 0);
263   else if (GET_CODE (x) == MINUS
264            && GET_CODE (XEXP (x, 1)) == CONST_INT)
265     return XEXP (x, 0);
266   return 0;
267 }
268 \f
269 /* Return the number of places FIND appears within X.  If COUNT_DEST is
270    zero, we do not count occurrences inside the destination of a SET.  */
271
272 int
273 count_occurrences (x, find, count_dest)
274      rtx x, find;
275      int count_dest;
276 {
277   int i, j;
278   enum rtx_code code;
279   const char *format_ptr;
280   int count;
281
282   if (x == find)
283     return 1;
284
285   code = GET_CODE (x);
286
287   switch (code)
288     {
289     case REG:
290     case CONST_INT:
291     case CONST_DOUBLE:
292     case SYMBOL_REF:
293     case CODE_LABEL:
294     case PC:
295     case CC0:
296       return 0;
297
298     case MEM:
299       if (GET_CODE (find) == MEM && rtx_equal_p (x, find))
300         return 1;
301       break;
302
303     case SET:
304       if (SET_DEST (x) == find && ! count_dest)
305         return count_occurrences (SET_SRC (x), find, count_dest);
306       break;
307
308     default:
309       break;
310     }
311
312   format_ptr = GET_RTX_FORMAT (code);
313   count = 0;
314
315   for (i = 0; i < GET_RTX_LENGTH (code); i++)
316     {
317       switch (*format_ptr++)
318         {
319         case 'e':
320           count += count_occurrences (XEXP (x, i), find, count_dest);
321           break;
322
323         case 'E':
324           for (j = 0; j < XVECLEN (x, i); j++)
325             count += count_occurrences (XVECEXP (x, i, j), find, count_dest);
326           break;
327         }
328     }
329   return count;
330 }
331 \f
332 /* Nonzero if register REG appears somewhere within IN.
333    Also works if REG is not a register; in this case it checks
334    for a subexpression of IN that is Lisp "equal" to REG.  */
335
336 int
337 reg_mentioned_p (reg, in)
338      register rtx reg, in;
339 {
340   register const char *fmt;
341   register int i;
342   register enum rtx_code code;
343
344   if (in == 0)
345     return 0;
346
347   if (reg == in)
348     return 1;
349
350   if (GET_CODE (in) == LABEL_REF)
351     return reg == XEXP (in, 0);
352
353   code = GET_CODE (in);
354
355   switch (code)
356     {
357       /* Compare registers by number.  */
358     case REG:
359       return GET_CODE (reg) == REG && REGNO (in) == REGNO (reg);
360
361       /* These codes have no constituent expressions
362          and are unique.  */
363     case SCRATCH:
364     case CC0:
365     case PC:
366       return 0;
367
368     case CONST_INT:
369       return GET_CODE (reg) == CONST_INT && INTVAL (in) == INTVAL (reg);
370       
371     case CONST_DOUBLE:
372       /* These are kept unique for a given value.  */
373       return 0;
374       
375     default:
376       break;
377     }
378
379   if (GET_CODE (reg) == code && rtx_equal_p (reg, in))
380     return 1;
381
382   fmt = GET_RTX_FORMAT (code);
383
384   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
385     {
386       if (fmt[i] == 'E')
387         {
388           register int j;
389           for (j = XVECLEN (in, i) - 1; j >= 0; j--)
390             if (reg_mentioned_p (reg, XVECEXP (in, i, j)))
391               return 1;
392         }
393       else if (fmt[i] == 'e'
394                && reg_mentioned_p (reg, XEXP (in, i)))
395         return 1;
396     }
397   return 0;
398 }
399 \f
400 /* Return 1 if in between BEG and END, exclusive of BEG and END, there is
401    no CODE_LABEL insn.  */
402
403 int
404 no_labels_between_p (beg, end)
405      rtx beg, end;
406 {
407   register rtx p;
408   for (p = NEXT_INSN (beg); p != end; p = NEXT_INSN (p))
409     if (GET_CODE (p) == CODE_LABEL)
410       return 0;
411   return 1;
412 }
413
414 /* Return 1 if in between BEG and END, exclusive of BEG and END, there is
415    no JUMP_INSN insn.  */
416
417 int
418 no_jumps_between_p (beg, end)
419      rtx beg, end;
420 {
421   register rtx p;
422   for (p = NEXT_INSN (beg); p != end; p = NEXT_INSN (p))
423     if (GET_CODE (p) == JUMP_INSN)
424       return 0;
425   return 1;
426 }
427
428 /* Nonzero if register REG is used in an insn between
429    FROM_INSN and TO_INSN (exclusive of those two).  */
430
431 int
432 reg_used_between_p (reg, from_insn, to_insn)
433      rtx reg, from_insn, to_insn;
434 {
435   register rtx insn;
436
437   if (from_insn == to_insn)
438     return 0;
439
440   for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
441     if (INSN_P (insn)
442         && (reg_overlap_mentioned_p (reg, PATTERN (insn))
443            || (GET_CODE (insn) == CALL_INSN
444               && (find_reg_fusage (insn, USE, reg)
445                   || find_reg_fusage (insn, CLOBBER, reg)))))
446       return 1;
447   return 0;
448 }
449 \f
450 /* Nonzero if the old value of X, a register, is referenced in BODY.  If X
451    is entirely replaced by a new value and the only use is as a SET_DEST,
452    we do not consider it a reference.  */
453
454 int
455 reg_referenced_p (x, body)
456      rtx x;
457      rtx body;
458 {
459   int i;
460
461   switch (GET_CODE (body))
462     {
463     case SET:
464       if (reg_overlap_mentioned_p (x, SET_SRC (body)))
465         return 1;
466
467       /* If the destination is anything other than CC0, PC, a REG or a SUBREG
468          of a REG that occupies all of the REG, the insn references X if
469          it is mentioned in the destination.  */
470       if (GET_CODE (SET_DEST (body)) != CC0
471           && GET_CODE (SET_DEST (body)) != PC
472           && GET_CODE (SET_DEST (body)) != REG
473           && ! (GET_CODE (SET_DEST (body)) == SUBREG
474                 && GET_CODE (SUBREG_REG (SET_DEST (body))) == REG
475                 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (body))))
476                       + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
477                     == ((GET_MODE_SIZE (GET_MODE (SET_DEST (body)))
478                          + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
479           && reg_overlap_mentioned_p (x, SET_DEST (body)))
480         return 1;
481       return 0;
482
483     case ASM_OPERANDS:
484       for (i = ASM_OPERANDS_INPUT_LENGTH (body) - 1; i >= 0; i--)
485         if (reg_overlap_mentioned_p (x, ASM_OPERANDS_INPUT (body, i)))
486           return 1;
487       return 0;
488
489     case CALL:
490     case USE:
491     case IF_THEN_ELSE:
492       return reg_overlap_mentioned_p (x, body);
493
494     case TRAP_IF:
495       return reg_overlap_mentioned_p (x, TRAP_CONDITION (body));
496
497     case UNSPEC:
498     case UNSPEC_VOLATILE:
499       for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
500         if (reg_overlap_mentioned_p (x, XVECEXP (body, 0, i)))
501           return 1;
502       return 0;
503
504     case PARALLEL:
505       for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
506         if (reg_referenced_p (x, XVECEXP (body, 0, i)))
507           return 1;
508       return 0;
509       
510     case CLOBBER:
511       if (GET_CODE (XEXP (body, 0)) == MEM)
512         if (reg_overlap_mentioned_p (x, XEXP (XEXP (body, 0), 0)))
513           return 1;
514       return 0;
515
516     case COND_EXEC:
517       if (reg_overlap_mentioned_p (x, COND_EXEC_TEST (body)))
518         return 1;
519       return reg_referenced_p (x, COND_EXEC_CODE (body));
520
521     default:
522       return 0;
523     }
524 }
525
526 /* Nonzero if register REG is referenced in an insn between
527    FROM_INSN and TO_INSN (exclusive of those two).  Sets of REG do
528    not count.  */
529
530 int
531 reg_referenced_between_p (reg, from_insn, to_insn)
532      rtx reg, from_insn, to_insn;
533 {
534   register rtx insn;
535
536   if (from_insn == to_insn)
537     return 0;
538
539   for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
540     if (INSN_P (insn)
541         && (reg_referenced_p (reg, PATTERN (insn))
542            || (GET_CODE (insn) == CALL_INSN
543               && find_reg_fusage (insn, USE, reg))))
544       return 1;
545   return 0;
546 }
547 \f
548 /* Nonzero if register REG is set or clobbered in an insn between
549    FROM_INSN and TO_INSN (exclusive of those two).  */
550
551 int
552 reg_set_between_p (reg, from_insn, to_insn)
553      rtx reg, from_insn, to_insn;
554 {
555   register rtx insn;
556
557   if (from_insn == to_insn)
558     return 0;
559
560   for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
561     if (INSN_P (insn) && reg_set_p (reg, insn))
562       return 1;
563   return 0;
564 }
565
566 /* Internals of reg_set_between_p.  */
567
568 static rtx reg_set_reg;
569 static int reg_set_flag;
570
571 static void
572 reg_set_p_1 (x, pat, data)
573      rtx x;
574      rtx pat ATTRIBUTE_UNUSED;
575      void *data ATTRIBUTE_UNUSED;
576 {
577   /* We don't want to return 1 if X is a MEM that contains a register
578      within REG_SET_REG.  */
579
580   if ((GET_CODE (x) != MEM)
581       && reg_overlap_mentioned_p (reg_set_reg, x))
582     reg_set_flag = 1;
583 }
584
585 int
586 reg_set_p (reg, insn)
587      rtx reg, insn;
588 {
589   rtx body = insn;
590
591   /* We can be passed an insn or part of one.  If we are passed an insn,
592      check if a side-effect of the insn clobbers REG.  */
593   if (INSN_P (insn))
594     {
595       if (FIND_REG_INC_NOTE (insn, reg)
596           || (GET_CODE (insn) == CALL_INSN
597               /* We'd like to test call_used_regs here, but rtlanal.c can't
598                  reference that variable due to its use in genattrtab.  So
599                  we'll just be more conservative.
600
601                  ??? Unless we could ensure that the CALL_INSN_FUNCTION_USAGE
602                  information holds all clobbered registers.  */
603               && ((GET_CODE (reg) == REG
604                    && REGNO (reg) < FIRST_PSEUDO_REGISTER)
605                   || GET_CODE (reg) == MEM
606                   || find_reg_fusage (insn, CLOBBER, reg))))
607         return 1;
608
609       body = PATTERN (insn);
610     }
611
612   reg_set_reg = reg;
613   reg_set_flag = 0;
614   note_stores (body, reg_set_p_1, NULL);
615   return reg_set_flag;
616 }
617
618 /* Similar to reg_set_between_p, but check all registers in X.  Return 0
619    only if none of them are modified between START and END.  Do not
620    consider non-registers one way or the other.  */
621
622 int
623 regs_set_between_p (x, start, end)
624      rtx x;
625      rtx start, end;
626 {
627   enum rtx_code code = GET_CODE (x);
628   const char *fmt;
629   int i, j;
630
631   switch (code)
632     {
633     case CONST_INT:
634     case CONST_DOUBLE:
635     case CONST:
636     case SYMBOL_REF:
637     case LABEL_REF:
638     case PC:
639     case CC0:
640       return 0;
641
642     case REG:
643       return reg_set_between_p (x, start, end);
644       
645     default:
646       break;
647     }
648
649   fmt = GET_RTX_FORMAT (code);
650   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
651     {
652       if (fmt[i] == 'e' && regs_set_between_p (XEXP (x, i), start, end))
653         return 1;
654
655       else if (fmt[i] == 'E')
656         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
657           if (regs_set_between_p (XVECEXP (x, i, j), start, end))
658             return 1;
659     }
660
661   return 0;
662 }
663
664 /* Similar to reg_set_between_p, but check all registers in X.  Return 0
665    only if none of them are modified between START and END.  Return 1 if
666    X contains a MEM; this routine does not perform any memory aliasing.  */
667
668 int
669 modified_between_p (x, start, end)
670      rtx x;
671      rtx start, end;
672 {
673   enum rtx_code code = GET_CODE (x);
674   const char *fmt;
675   int i, j;
676
677   switch (code)
678     {
679     case CONST_INT:
680     case CONST_DOUBLE:
681     case CONST:
682     case SYMBOL_REF:
683     case LABEL_REF:
684       return 0;
685
686     case PC:
687     case CC0:
688       return 1;
689
690     case MEM:
691       /* If the memory is not constant, assume it is modified.  If it is
692          constant, we still have to check the address.  */
693       if (! RTX_UNCHANGING_P (x))
694         return 1;
695       break;
696
697     case REG:
698       return reg_set_between_p (x, start, end);
699       
700     default:
701       break;
702     }
703
704   fmt = GET_RTX_FORMAT (code);
705   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
706     {
707       if (fmt[i] == 'e' && modified_between_p (XEXP (x, i), start, end))
708         return 1;
709
710       else if (fmt[i] == 'E')
711         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
712           if (modified_between_p (XVECEXP (x, i, j), start, end))
713             return 1;
714     }
715
716   return 0;
717 }
718
719 /* Similar to reg_set_p, but check all registers in X.  Return 0 only if none
720    of them are modified in INSN.  Return 1 if X contains a MEM; this routine
721    does not perform any memory aliasing.  */
722
723 int
724 modified_in_p (x, insn)
725      rtx x;
726      rtx insn;
727 {
728   enum rtx_code code = GET_CODE (x);
729   const char *fmt;
730   int i, j;
731
732   switch (code)
733     {
734     case CONST_INT:
735     case CONST_DOUBLE:
736     case CONST:
737     case SYMBOL_REF:
738     case LABEL_REF:
739       return 0;
740
741     case PC:
742     case CC0:
743       return 1;
744
745     case MEM:
746       /* If the memory is not constant, assume it is modified.  If it is
747          constant, we still have to check the address.  */
748       if (! RTX_UNCHANGING_P (x))
749         return 1;
750       break;
751
752     case REG:
753       return reg_set_p (x, insn);
754
755     default:
756       break;
757     }
758
759   fmt = GET_RTX_FORMAT (code);
760   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
761     {
762       if (fmt[i] == 'e' && modified_in_p (XEXP (x, i), insn))
763         return 1;
764
765       else if (fmt[i] == 'E')
766         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
767           if (modified_in_p (XVECEXP (x, i, j), insn))
768             return 1;
769     }
770
771   return 0;
772 }
773
774 /* Return true if anything in insn X is (anti,output,true) dependent on
775    anything in insn Y.  */
776
777 int
778 insn_dependent_p (x, y)
779      rtx x, y;
780 {
781   rtx tmp;
782
783   if (! INSN_P (x) || ! INSN_P (y))
784     abort ();
785
786   tmp = PATTERN (y);
787   note_stores (PATTERN (x), insn_dependent_p_1, &tmp);
788   if (tmp == NULL_RTX)
789     return 1;
790
791   tmp = PATTERN (x);
792   note_stores (PATTERN (y), insn_dependent_p_1, &tmp);
793   if (tmp == NULL_RTX)
794     return 1;
795
796   return 0;
797 }
798
799 /* A helper routine for insn_dependent_p called through note_stores.  */
800
801 static void
802 insn_dependent_p_1 (x, pat, data)
803      rtx x;
804      rtx pat ATTRIBUTE_UNUSED;
805      void *data;
806 {
807   rtx * pinsn = (rtx *) data;
808
809   if (*pinsn && reg_mentioned_p (x, *pinsn))
810     *pinsn = NULL_RTX;
811 }
812 \f
813 /* Given an INSN, return a SET expression if this insn has only a single SET.
814    It may also have CLOBBERs, USEs, or SET whose output
815    will not be used, which we ignore.  */
816
817 rtx
818 single_set (insn)
819      rtx insn;
820 {
821   rtx set;
822   int i;
823   
824   if (! INSN_P (insn))
825     return 0;
826
827   if (GET_CODE (PATTERN (insn)) == SET)
828     return PATTERN (insn);
829   
830   else if (GET_CODE (PATTERN (insn)) == PARALLEL)
831     {
832       for (i = 0, set = 0; i < XVECLEN (PATTERN (insn), 0); i++)
833         {
834           rtx sub = XVECEXP (PATTERN (insn), 0, i);
835
836           switch (GET_CODE (sub))
837             {
838             case USE:
839             case CLOBBER:
840               break;
841
842             case SET:
843               if (! find_reg_note (insn, REG_UNUSED, SET_DEST (sub))
844                   || side_effects_p (sub))
845                 {
846                   if (set)
847                     return 0;
848                   else
849                     set = sub;
850                 }
851               break;
852
853             default:
854               return 0;
855             }
856         }
857       return set;
858     }
859   
860   return 0;
861 }
862
863 /* Given an INSN, return nonzero if it has more than one SET, else return
864    zero.  */
865
866 int
867 multiple_sets (insn)
868      rtx insn;
869 {
870   int found;
871   int i;
872   
873   /* INSN must be an insn.  */
874   if (! INSN_P (insn))
875     return 0;
876
877   /* Only a PARALLEL can have multiple SETs.  */
878   if (GET_CODE (PATTERN (insn)) == PARALLEL)
879     {
880       for (i = 0, found = 0; i < XVECLEN (PATTERN (insn), 0); i++)
881         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
882           {
883             /* If we have already found a SET, then return now.  */
884             if (found)
885               return 1;
886             else
887               found = 1;
888           }
889     }
890   
891   /* Either zero or one SET.  */
892   return 0;
893 }
894 \f
895 /* Return the last thing that X was assigned from before *PINSN.  If VALID_TO
896    is not NULL_RTX then verify that the object is not modified up to VALID_TO.
897    If the object was modified, if we hit a partial assignment to X, or hit a
898    CODE_LABEL first, return X.  If we found an assignment, update *PINSN to
899    point to it.  ALLOW_HWREG is set to 1 if hardware registers are allowed to
900    be the src.  */
901
902 rtx
903 find_last_value (x, pinsn, valid_to, allow_hwreg)
904      rtx x;
905      rtx *pinsn;
906      rtx valid_to;
907      int allow_hwreg;
908 {
909   rtx p;
910
911   for (p = PREV_INSN (*pinsn); p && GET_CODE (p) != CODE_LABEL;
912        p = PREV_INSN (p))
913     if (INSN_P (p))
914       {
915         rtx set = single_set (p);
916         rtx note = find_reg_note (p, REG_EQUAL, NULL_RTX);
917
918         if (set && rtx_equal_p (x, SET_DEST (set)))
919           {
920             rtx src = SET_SRC (set);
921
922             if (note && GET_CODE (XEXP (note, 0)) != EXPR_LIST)
923               src = XEXP (note, 0);
924
925             if ((valid_to == NULL_RTX
926                  || ! modified_between_p (src, PREV_INSN (p), valid_to))
927                 /* Reject hard registers because we don't usually want
928                    to use them; we'd rather use a pseudo.  */
929                 && (! (GET_CODE (src) == REG
930                       && REGNO (src) < FIRST_PSEUDO_REGISTER) || allow_hwreg))
931               {
932                 *pinsn = p;
933                 return src;
934               }
935           }
936           
937         /* If set in non-simple way, we don't have a value.  */
938         if (reg_set_p (x, p))
939           break;
940       }
941
942   return x;
943 }     
944 \f
945 /* Return nonzero if register in range [REGNO, ENDREGNO)
946    appears either explicitly or implicitly in X
947    other than being stored into.
948
949    References contained within the substructure at LOC do not count.
950    LOC may be zero, meaning don't ignore anything.  */
951
952 int
953 refers_to_regno_p (regno, endregno, x, loc)
954      unsigned int regno, endregno;
955      rtx x;
956      rtx *loc;
957 {
958   int i;
959   unsigned int x_regno;
960   RTX_CODE code;
961   const char *fmt;
962
963  repeat:
964   /* The contents of a REG_NONNEG note is always zero, so we must come here
965      upon repeat in case the last REG_NOTE is a REG_NONNEG note.  */
966   if (x == 0)
967     return 0;
968
969   code = GET_CODE (x);
970
971   switch (code)
972     {
973     case REG:
974       x_regno = REGNO (x);
975
976       /* If we modifying the stack, frame, or argument pointer, it will
977          clobber a virtual register.  In fact, we could be more precise,
978          but it isn't worth it.  */
979       if ((x_regno == STACK_POINTER_REGNUM
980 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
981            || x_regno == ARG_POINTER_REGNUM
982 #endif
983            || x_regno == FRAME_POINTER_REGNUM)
984           && regno >= FIRST_VIRTUAL_REGISTER && regno <= LAST_VIRTUAL_REGISTER)
985         return 1;
986
987       return (endregno > x_regno
988               && regno < x_regno + (x_regno < FIRST_PSEUDO_REGISTER 
989                                     ? HARD_REGNO_NREGS (x_regno, GET_MODE (x))
990                               : 1));
991
992     case SUBREG:
993       /* If this is a SUBREG of a hard reg, we can see exactly which
994          registers are being modified.  Otherwise, handle normally.  */
995       if (GET_CODE (SUBREG_REG (x)) == REG
996           && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER)
997         {
998           unsigned int inner_regno = REGNO (SUBREG_REG (x)) + SUBREG_WORD (x);
999           unsigned int inner_endregno
1000             = inner_regno + (inner_regno < FIRST_PSEUDO_REGISTER
1001                              ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
1002
1003           return endregno > inner_regno && regno < inner_endregno;
1004         }
1005       break;
1006
1007     case CLOBBER:
1008     case SET:
1009       if (&SET_DEST (x) != loc
1010           /* Note setting a SUBREG counts as referring to the REG it is in for
1011              a pseudo but not for hard registers since we can
1012              treat each word individually.  */
1013           && ((GET_CODE (SET_DEST (x)) == SUBREG
1014                && loc != &SUBREG_REG (SET_DEST (x))
1015                && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG
1016                && REGNO (SUBREG_REG (SET_DEST (x))) >= FIRST_PSEUDO_REGISTER
1017                && refers_to_regno_p (regno, endregno,
1018                                      SUBREG_REG (SET_DEST (x)), loc))
1019               || (GET_CODE (SET_DEST (x)) != REG
1020                   && refers_to_regno_p (regno, endregno, SET_DEST (x), loc))))
1021         return 1;
1022
1023       if (code == CLOBBER || loc == &SET_SRC (x))
1024         return 0;
1025       x = SET_SRC (x);
1026       goto repeat;
1027
1028     default:
1029       break;
1030     }
1031
1032   /* X does not match, so try its subexpressions.  */
1033
1034   fmt = GET_RTX_FORMAT (code);
1035   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1036     {
1037       if (fmt[i] == 'e' && loc != &XEXP (x, i))
1038         {
1039           if (i == 0)
1040             {
1041               x = XEXP (x, 0);
1042               goto repeat;
1043             }
1044           else
1045             if (refers_to_regno_p (regno, endregno, XEXP (x, i), loc))
1046               return 1;
1047         }
1048       else if (fmt[i] == 'E')
1049         {
1050           register int j;
1051           for (j = XVECLEN (x, i) - 1; j >=0; j--)
1052             if (loc != &XVECEXP (x, i, j)
1053                 && refers_to_regno_p (regno, endregno, XVECEXP (x, i, j), loc))
1054               return 1;
1055         }
1056     }
1057   return 0;
1058 }
1059
1060 /* Nonzero if modifying X will affect IN.  If X is a register or a SUBREG,
1061    we check if any register number in X conflicts with the relevant register
1062    numbers.  If X is a constant, return 0.  If X is a MEM, return 1 iff IN
1063    contains a MEM (we don't bother checking for memory addresses that can't
1064    conflict because we expect this to be a rare case.  */
1065
1066 int
1067 reg_overlap_mentioned_p (x, in)
1068      rtx x, in;
1069 {
1070   unsigned int regno, endregno;
1071
1072   /* Overly conservative.  */
1073   if (GET_CODE (x) == STRICT_LOW_PART)
1074     x = XEXP (x, 0);
1075
1076   /* If either argument is a constant, then modifying X can not affect IN.  */
1077   if (CONSTANT_P (x) || CONSTANT_P (in))
1078     return 0;
1079
1080   switch (GET_CODE (x))
1081     {
1082     case SUBREG:
1083       regno = REGNO (SUBREG_REG (x));
1084       if (regno < FIRST_PSEUDO_REGISTER)
1085         regno += SUBREG_WORD (x);
1086       goto do_reg;
1087
1088     case REG:
1089       regno = REGNO (x);
1090     do_reg:
1091       endregno = regno + (regno < FIRST_PSEUDO_REGISTER
1092                           ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
1093       return refers_to_regno_p (regno, endregno, in, NULL_PTR);
1094
1095     case MEM:
1096       {
1097         const char *fmt;
1098         int i;
1099
1100         if (GET_CODE (in) == MEM)
1101           return 1;
1102
1103         fmt = GET_RTX_FORMAT (GET_CODE (in));
1104         for (i = GET_RTX_LENGTH (GET_CODE (in)) - 1; i >= 0; i--)
1105           if (fmt[i] == 'e' && reg_overlap_mentioned_p (x, XEXP (in, i)))
1106             return 1;
1107
1108         return 0;
1109       }
1110
1111     case SCRATCH:
1112     case PC:
1113     case CC0:
1114       return reg_mentioned_p (x, in);
1115
1116     case PARALLEL:
1117       {
1118         int i, n;
1119
1120         /* Check for a NULL entry, used to indicate that the parameter goes
1121            both on the stack and in registers.  */
1122         if (XEXP (XVECEXP (x, 0, 0), 0))
1123           i = 0;
1124         else
1125           i = 1;
1126
1127         /* If any register in here refers to it we return true.  */
1128         for (n = XVECLEN (x, 0); i < n; ++i)
1129           if (reg_overlap_mentioned_p (XEXP (XVECEXP (x, 0, i), 0), in))
1130             return 1;
1131         return 0;
1132       }
1133
1134     default:
1135       break;
1136     }
1137
1138   abort ();
1139 }
1140 \f
1141 /* Used for communications between the next few functions.  */
1142
1143 static int reg_set_last_unknown;
1144 static rtx reg_set_last_value;
1145 static unsigned int reg_set_last_first_regno, reg_set_last_last_regno;
1146
1147 /* Called via note_stores from reg_set_last.  */
1148
1149 static void
1150 reg_set_last_1 (x, pat, data)
1151      rtx x;
1152      rtx pat;
1153      void *data ATTRIBUTE_UNUSED;
1154 {
1155   unsigned int first, last;
1156
1157   /* If X is not a register, or is not one in the range we care
1158      about, ignore.  */
1159   if (GET_CODE (x) != REG)
1160     return;
1161
1162   first = REGNO (x);
1163   last = first + (first < FIRST_PSEUDO_REGISTER
1164                   ? HARD_REGNO_NREGS (first, GET_MODE (x)) : 1);
1165
1166   if (first >= reg_set_last_last_regno
1167       || last <= reg_set_last_first_regno)
1168     return;
1169
1170   /* If this is a CLOBBER or is some complex LHS, or doesn't modify
1171      exactly the registers we care about, show we don't know the value.  */
1172   if (GET_CODE (pat) == CLOBBER || SET_DEST (pat) != x
1173       || first != reg_set_last_first_regno
1174       || last != reg_set_last_last_regno)
1175     reg_set_last_unknown = 1;
1176   else
1177     reg_set_last_value = SET_SRC (pat);
1178 }
1179
1180 /* Return the last value to which REG was set prior to INSN.  If we can't
1181    find it easily, return 0.
1182
1183    We only return a REG, SUBREG, or constant because it is too hard to
1184    check if a MEM remains unchanged.  */
1185
1186 rtx
1187 reg_set_last (x, insn)
1188      rtx x;
1189      rtx insn;
1190 {
1191   rtx orig_insn = insn;
1192
1193   reg_set_last_first_regno = REGNO (x);
1194
1195   reg_set_last_last_regno
1196     = reg_set_last_first_regno
1197       + (reg_set_last_first_regno < FIRST_PSEUDO_REGISTER
1198          ? HARD_REGNO_NREGS (reg_set_last_first_regno, GET_MODE (x)) : 1);
1199
1200   reg_set_last_unknown = 0;
1201   reg_set_last_value = 0;
1202
1203   /* Scan backwards until reg_set_last_1 changed one of the above flags.
1204      Stop when we reach a label or X is a hard reg and we reach a
1205      CALL_INSN (if reg_set_last_last_regno is a hard reg).
1206
1207      If we find a set of X, ensure that its SET_SRC remains unchanged.  */
1208
1209   /* We compare with <= here, because reg_set_last_last_regno
1210      is actually the number of the first reg *not* in X.  */
1211   for (;
1212        insn && GET_CODE (insn) != CODE_LABEL
1213        && ! (GET_CODE (insn) == CALL_INSN
1214              && reg_set_last_last_regno <= FIRST_PSEUDO_REGISTER);
1215        insn = PREV_INSN (insn))
1216     if (INSN_P (insn))
1217       {
1218         note_stores (PATTERN (insn), reg_set_last_1, NULL);
1219         if (reg_set_last_unknown)
1220           return 0;
1221         else if (reg_set_last_value)
1222           {
1223             if (CONSTANT_P (reg_set_last_value)
1224                 || ((GET_CODE (reg_set_last_value) == REG
1225                      || GET_CODE (reg_set_last_value) == SUBREG)
1226                     && ! reg_set_between_p (reg_set_last_value,
1227                                             insn, orig_insn)))
1228               return reg_set_last_value;
1229             else
1230               return 0;
1231           }
1232       }
1233
1234   return 0;
1235 }
1236 \f
1237 /* Call FUN on each register or MEM that is stored into or clobbered by X.
1238    (X would be the pattern of an insn).
1239    FUN receives two arguments:
1240      the REG, MEM, CC0 or PC being stored in or clobbered,
1241      the SET or CLOBBER rtx that does the store.
1242
1243   If the item being stored in or clobbered is a SUBREG of a hard register,
1244   the SUBREG will be passed.  */
1245      
1246 void
1247 note_stores (x, fun, data)
1248      register rtx x;
1249      void (*fun) PARAMS ((rtx, rtx, void *));
1250      void *data;
1251 {
1252   if (GET_CODE (x) == COND_EXEC)
1253     x = COND_EXEC_CODE (x);
1254   if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
1255     {
1256       register rtx dest = SET_DEST (x);
1257       while ((GET_CODE (dest) == SUBREG
1258               && (GET_CODE (SUBREG_REG (dest)) != REG
1259                   || REGNO (SUBREG_REG (dest)) >= FIRST_PSEUDO_REGISTER))
1260              || GET_CODE (dest) == ZERO_EXTRACT
1261              || GET_CODE (dest) == SIGN_EXTRACT
1262              || GET_CODE (dest) == STRICT_LOW_PART)
1263         dest = XEXP (dest, 0);
1264
1265       if (GET_CODE (dest) == PARALLEL
1266           && GET_MODE (dest) == BLKmode)
1267         {
1268           register int i;
1269           for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
1270             (*fun) (SET_DEST (XVECEXP (dest, 0, i)), x, data);
1271         }
1272       else
1273         (*fun) (dest, x, data);
1274     }
1275   else if (GET_CODE (x) == PARALLEL)
1276     {
1277       register int i;
1278       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1279         {
1280           register rtx y = XVECEXP (x, 0, i);
1281           if (GET_CODE (y) == COND_EXEC)
1282             y = COND_EXEC_CODE (y);
1283           if (GET_CODE (y) == SET || GET_CODE (y) == CLOBBER)
1284             {
1285               register rtx dest = SET_DEST (y);
1286               while ((GET_CODE (dest) == SUBREG
1287                       && (GET_CODE (SUBREG_REG (dest)) != REG
1288                           || (REGNO (SUBREG_REG (dest))
1289                               >= FIRST_PSEUDO_REGISTER)))
1290                      || GET_CODE (dest) == ZERO_EXTRACT
1291                      || GET_CODE (dest) == SIGN_EXTRACT
1292                      || GET_CODE (dest) == STRICT_LOW_PART)
1293                 dest = XEXP (dest, 0);
1294               if (GET_CODE (dest) == PARALLEL
1295                   && GET_MODE (dest) == BLKmode)
1296                 {
1297                   register int i;
1298
1299                   for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
1300                     (*fun) (SET_DEST (XVECEXP (dest, 0, i)), y, data);
1301                 }
1302               else
1303                 (*fun) (dest, y, data);
1304             }
1305         }
1306     }
1307 }
1308 \f
1309 /* Return nonzero if X's old contents don't survive after INSN.
1310    This will be true if X is (cc0) or if X is a register and
1311    X dies in INSN or because INSN entirely sets X.
1312
1313    "Entirely set" means set directly and not through a SUBREG,
1314    ZERO_EXTRACT or SIGN_EXTRACT, so no trace of the old contents remains.
1315    Likewise, REG_INC does not count.
1316
1317    REG may be a hard or pseudo reg.  Renumbering is not taken into account,
1318    but for this use that makes no difference, since regs don't overlap
1319    during their lifetimes.  Therefore, this function may be used
1320    at any time after deaths have been computed (in flow.c).
1321
1322    If REG is a hard reg that occupies multiple machine registers, this
1323    function will only return 1 if each of those registers will be replaced
1324    by INSN.  */
1325
1326 int
1327 dead_or_set_p (insn, x)
1328      rtx insn;
1329      rtx x;
1330 {
1331   unsigned int regno, last_regno;
1332   unsigned int i;
1333
1334   /* Can't use cc0_rtx below since this file is used by genattrtab.c.  */
1335   if (GET_CODE (x) == CC0)
1336     return 1;
1337
1338   if (GET_CODE (x) != REG)
1339     abort ();
1340
1341   regno = REGNO (x);
1342   last_regno = (regno >= FIRST_PSEUDO_REGISTER ? regno
1343                 : regno + HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1);
1344
1345   for (i = regno; i <= last_regno; i++)
1346     if (! dead_or_set_regno_p (insn, i))
1347       return 0;
1348
1349   return 1;
1350 }
1351
1352 /* Utility function for dead_or_set_p to check an individual register.  Also
1353    called from flow.c.  */
1354
1355 int
1356 dead_or_set_regno_p (insn, test_regno)
1357      rtx insn;
1358      unsigned int test_regno;
1359 {
1360   unsigned int regno, endregno;
1361   rtx pattern;
1362
1363   /* See if there is a death note for something that includes TEST_REGNO.  */
1364   if (find_regno_note (insn, REG_DEAD, test_regno))
1365     return 1;
1366
1367   if (GET_CODE (insn) == CALL_INSN
1368       && find_regno_fusage (insn, CLOBBER, test_regno))
1369     return 1;
1370
1371   pattern = PATTERN (insn);
1372
1373   if (GET_CODE (pattern) == COND_EXEC)
1374     pattern = COND_EXEC_CODE (pattern);
1375
1376   if (GET_CODE (pattern) == SET)
1377     {
1378       rtx dest = SET_DEST (PATTERN (insn));
1379  
1380       /* A value is totally replaced if it is the destination or the
1381          destination is a SUBREG of REGNO that does not change the number of
1382          words in it.  */
1383       if (GET_CODE (dest) == SUBREG
1384           && (((GET_MODE_SIZE (GET_MODE (dest))
1385                 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1386               == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1387                    + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1388         dest = SUBREG_REG (dest);
1389
1390       if (GET_CODE (dest) != REG)
1391         return 0;
1392
1393       regno = REGNO (dest);
1394       endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1395                   : regno + HARD_REGNO_NREGS (regno, GET_MODE (dest)));
1396
1397       return (test_regno >= regno && test_regno < endregno);
1398     }
1399   else if (GET_CODE (pattern) == PARALLEL)
1400     {
1401       register int i;
1402
1403       for (i = XVECLEN (pattern, 0) - 1; i >= 0; i--)
1404         {
1405           rtx body = XVECEXP (pattern, 0, i);
1406
1407           if (GET_CODE (body) == COND_EXEC)
1408             body = COND_EXEC_CODE (body);
1409
1410           if (GET_CODE (body) == SET || GET_CODE (body) == CLOBBER)
1411             {
1412               rtx dest = SET_DEST (body);
1413
1414               if (GET_CODE (dest) == SUBREG
1415                   && (((GET_MODE_SIZE (GET_MODE (dest))
1416                         + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1417                       == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1418                            + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1419                 dest = SUBREG_REG (dest);
1420
1421               if (GET_CODE (dest) != REG)
1422                 continue;
1423
1424               regno = REGNO (dest);
1425               endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1426                           : regno + HARD_REGNO_NREGS (regno, GET_MODE (dest)));
1427
1428               if (test_regno >= regno && test_regno < endregno)
1429                 return 1;
1430             }
1431         }
1432     }
1433
1434   return 0;
1435 }
1436
1437 /* Return the reg-note of kind KIND in insn INSN, if there is one.
1438    If DATUM is nonzero, look for one whose datum is DATUM.  */
1439
1440 rtx
1441 find_reg_note (insn, kind, datum)
1442      rtx insn;
1443      enum reg_note kind;
1444      rtx datum;
1445 {
1446   register rtx link;
1447
1448   /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN.  */
1449   if (! INSN_P (insn))
1450     return 0;
1451
1452   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1453     if (REG_NOTE_KIND (link) == kind
1454         && (datum == 0 || datum == XEXP (link, 0)))
1455       return link;
1456   return 0;
1457 }
1458
1459 /* Return the reg-note of kind KIND in insn INSN which applies to register
1460    number REGNO, if any.  Return 0 if there is no such reg-note.  Note that
1461    the REGNO of this NOTE need not be REGNO if REGNO is a hard register;
1462    it might be the case that the note overlaps REGNO.  */
1463
1464 rtx
1465 find_regno_note (insn, kind, regno)
1466      rtx insn;
1467      enum reg_note kind;
1468      unsigned int regno;
1469 {
1470   register rtx link;
1471
1472   /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN.  */
1473   if (! INSN_P (insn))
1474     return 0;
1475
1476   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1477     if (REG_NOTE_KIND (link) == kind
1478         /* Verify that it is a register, so that scratch and MEM won't cause a
1479            problem here.  */
1480         && GET_CODE (XEXP (link, 0)) == REG
1481         && REGNO (XEXP (link, 0)) <= regno
1482         && ((REGNO (XEXP (link, 0))
1483              + (REGNO (XEXP (link, 0)) >= FIRST_PSEUDO_REGISTER ? 1
1484                 : HARD_REGNO_NREGS (REGNO (XEXP (link, 0)),
1485                                     GET_MODE (XEXP (link, 0)))))
1486             > regno))
1487       return link;
1488   return 0;
1489 }
1490
1491 /* Return true if DATUM, or any overlap of DATUM, of kind CODE is found
1492    in the CALL_INSN_FUNCTION_USAGE information of INSN.  */
1493
1494 int
1495 find_reg_fusage (insn, code, datum)
1496      rtx insn;
1497      enum rtx_code code;
1498      rtx datum;
1499 {
1500   /* If it's not a CALL_INSN, it can't possibly have a
1501      CALL_INSN_FUNCTION_USAGE field, so don't bother checking.  */
1502   if (GET_CODE (insn) != CALL_INSN)
1503     return 0;
1504
1505   if (! datum)
1506     abort();
1507
1508   if (GET_CODE (datum) != REG)
1509     {
1510       register rtx link;
1511
1512       for (link = CALL_INSN_FUNCTION_USAGE (insn);
1513            link;
1514            link = XEXP (link, 1))
1515         if (GET_CODE (XEXP (link, 0)) == code
1516             && rtx_equal_p (datum, SET_DEST (XEXP (link, 0))))
1517           return 1;
1518     }
1519   else
1520     {
1521       unsigned int regno = REGNO (datum);
1522
1523       /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1524          to pseudo registers, so don't bother checking.  */
1525
1526       if (regno < FIRST_PSEUDO_REGISTER)
1527         {
1528           unsigned int end_regno
1529             = regno + HARD_REGNO_NREGS (regno, GET_MODE (datum));
1530           unsigned int i;
1531
1532           for (i = regno; i < end_regno; i++)
1533             if (find_regno_fusage (insn, code, i))
1534               return 1;
1535         }
1536     }
1537
1538   return 0;
1539 }
1540
1541 /* Return true if REGNO, or any overlap of REGNO, of kind CODE is found
1542    in the CALL_INSN_FUNCTION_USAGE information of INSN.  */
1543
1544 int
1545 find_regno_fusage (insn, code, regno)
1546      rtx insn;
1547      enum rtx_code code;
1548      unsigned int regno;
1549 {
1550   register rtx link;
1551
1552   /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1553      to pseudo registers, so don't bother checking.  */
1554
1555   if (regno >= FIRST_PSEUDO_REGISTER
1556       || GET_CODE (insn) != CALL_INSN )
1557     return 0;
1558
1559   for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
1560     {
1561       unsigned int regnote;
1562       rtx op, reg;
1563
1564       if (GET_CODE (op = XEXP (link, 0)) == code
1565           && GET_CODE (reg = XEXP (op, 0)) == REG
1566           && (regnote = REGNO (reg)) <= regno
1567           && regnote + HARD_REGNO_NREGS (regnote, GET_MODE (reg)) > regno)
1568         return 1;
1569     }
1570
1571   return 0;
1572 }
1573 \f
1574 /* Remove register note NOTE from the REG_NOTES of INSN.  */
1575
1576 void
1577 remove_note (insn, note)
1578      register rtx insn;
1579      register rtx note;
1580 {
1581   register rtx link;
1582
1583   if (note == NULL_RTX)
1584     return;
1585
1586   if (REG_NOTES (insn) == note)
1587     {
1588       REG_NOTES (insn) = XEXP (note, 1);
1589       return;
1590     }
1591
1592   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1593     if (XEXP (link, 1) == note)
1594       {
1595         XEXP (link, 1) = XEXP (note, 1);
1596         return;
1597       }
1598
1599   abort ();
1600 }
1601
1602 /* Search LISTP (an EXPR_LIST) for NODE and remove NODE from the list
1603    if it is found.
1604
1605    A simple equality test is used to determine if NODE is on the
1606    EXPR_LIST.  */
1607
1608 void
1609 remove_node_from_expr_list (node, listp)
1610      rtx node;
1611      rtx *listp;
1612 {
1613   rtx temp = *listp;
1614   rtx prev = NULL_RTX;
1615
1616   while (temp)
1617     {
1618       if (node == XEXP (temp, 0))
1619         {
1620           /* Splice the node out of the list.  */
1621           if (prev)
1622             XEXP (prev, 1) = XEXP (temp, 1);
1623           else
1624             *listp = XEXP (temp, 1);
1625
1626           return;
1627         }
1628       temp = XEXP (temp, 1);
1629     }
1630 }
1631 \f
1632 /* Nonzero if X contains any volatile instructions.  These are instructions
1633    which may cause unpredictable machine state instructions, and thus no
1634    instructions should be moved or combined across them.  This includes
1635    only volatile asms and UNSPEC_VOLATILE instructions.  */
1636
1637 int
1638 volatile_insn_p (x)
1639      rtx x;
1640 {
1641   register RTX_CODE code;
1642
1643   code = GET_CODE (x);
1644   switch (code)
1645     {
1646     case LABEL_REF:
1647     case SYMBOL_REF:
1648     case CONST_INT:
1649     case CONST:
1650     case CONST_DOUBLE:
1651     case CC0:
1652     case PC:
1653     case REG:
1654     case SCRATCH:
1655     case CLOBBER:
1656     case ASM_INPUT:
1657     case ADDR_VEC:
1658     case ADDR_DIFF_VEC:
1659     case CALL:
1660     case MEM:
1661       return 0;
1662
1663     case UNSPEC_VOLATILE:
1664  /* case TRAP_IF: This isn't clear yet.  */
1665       return 1;
1666
1667     case ASM_OPERANDS:
1668       if (MEM_VOLATILE_P (x))
1669         return 1;
1670
1671     default:
1672       break;
1673     }
1674
1675   /* Recursively scan the operands of this expression.  */
1676
1677   {
1678     register const char *fmt = GET_RTX_FORMAT (code);
1679     register int i;
1680     
1681     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1682       {
1683         if (fmt[i] == 'e')
1684           {
1685             if (volatile_insn_p (XEXP (x, i)))
1686               return 1;
1687           }
1688         else if (fmt[i] == 'E')
1689           {
1690             register int j;
1691             for (j = 0; j < XVECLEN (x, i); j++)
1692               if (volatile_insn_p (XVECEXP (x, i, j)))
1693                 return 1;
1694           }
1695       }
1696   }
1697   return 0;
1698 }
1699
1700 /* Nonzero if X contains any volatile memory references
1701    UNSPEC_VOLATILE operations or volatile ASM_OPERANDS expressions.  */
1702
1703 int
1704 volatile_refs_p (x)
1705      rtx x;
1706 {
1707   register RTX_CODE code;
1708
1709   code = GET_CODE (x);
1710   switch (code)
1711     {
1712     case LABEL_REF:
1713     case SYMBOL_REF:
1714     case CONST_INT:
1715     case CONST:
1716     case CONST_DOUBLE:
1717     case CC0:
1718     case PC:
1719     case REG:
1720     case SCRATCH:
1721     case CLOBBER:
1722     case ASM_INPUT:
1723     case ADDR_VEC:
1724     case ADDR_DIFF_VEC:
1725       return 0;
1726
1727     case CALL:
1728     case UNSPEC_VOLATILE:
1729  /* case TRAP_IF: This isn't clear yet.  */
1730       return 1;
1731
1732     case MEM:
1733     case ASM_OPERANDS:
1734       if (MEM_VOLATILE_P (x))
1735         return 1;
1736
1737     default:
1738       break;
1739     }
1740
1741   /* Recursively scan the operands of this expression.  */
1742
1743   {
1744     register const char *fmt = GET_RTX_FORMAT (code);
1745     register int i;
1746     
1747     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1748       {
1749         if (fmt[i] == 'e')
1750           {
1751             if (volatile_refs_p (XEXP (x, i)))
1752               return 1;
1753           }
1754         else if (fmt[i] == 'E')
1755           {
1756             register int j;
1757             for (j = 0; j < XVECLEN (x, i); j++)
1758               if (volatile_refs_p (XVECEXP (x, i, j)))
1759                 return 1;
1760           }
1761       }
1762   }
1763   return 0;
1764 }
1765
1766 /* Similar to above, except that it also rejects register pre- and post-
1767    incrementing.  */
1768
1769 int
1770 side_effects_p (x)
1771      rtx x;
1772 {
1773   register RTX_CODE code;
1774
1775   code = GET_CODE (x);
1776   switch (code)
1777     {
1778     case LABEL_REF:
1779     case SYMBOL_REF:
1780     case CONST_INT:
1781     case CONST:
1782     case CONST_DOUBLE:
1783     case CC0:
1784     case PC:
1785     case REG:
1786     case SCRATCH:
1787     case ASM_INPUT:
1788     case ADDR_VEC:
1789     case ADDR_DIFF_VEC:
1790       return 0;
1791
1792     case CLOBBER:
1793       /* Reject CLOBBER with a non-VOID mode.  These are made by combine.c
1794          when some combination can't be done.  If we see one, don't think
1795          that we can simplify the expression.  */
1796       return (GET_MODE (x) != VOIDmode);
1797
1798     case PRE_INC:
1799     case PRE_DEC:
1800     case POST_INC:
1801     case POST_DEC:
1802     case CALL:
1803     case UNSPEC_VOLATILE:
1804  /* case TRAP_IF: This isn't clear yet.  */
1805       return 1;
1806
1807     case MEM:
1808     case ASM_OPERANDS:
1809       if (MEM_VOLATILE_P (x))
1810         return 1;
1811
1812     default:
1813       break;
1814     }
1815
1816   /* Recursively scan the operands of this expression.  */
1817
1818   {
1819     register const char *fmt = GET_RTX_FORMAT (code);
1820     register int i;
1821     
1822     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1823       {
1824         if (fmt[i] == 'e')
1825           {
1826             if (side_effects_p (XEXP (x, i)))
1827               return 1;
1828           }
1829         else if (fmt[i] == 'E')
1830           {
1831             register int j;
1832             for (j = 0; j < XVECLEN (x, i); j++)
1833               if (side_effects_p (XVECEXP (x, i, j)))
1834                 return 1;
1835           }
1836       }
1837   }
1838   return 0;
1839 }
1840 \f
1841 /* Return nonzero if evaluating rtx X might cause a trap.  */
1842
1843 int
1844 may_trap_p (x)
1845      rtx x;
1846 {
1847   int i;
1848   enum rtx_code code;
1849   const char *fmt;
1850
1851   if (x == 0)
1852     return 0;
1853   code = GET_CODE (x);
1854   switch (code)
1855     {
1856       /* Handle these cases quickly.  */
1857     case CONST_INT:
1858     case CONST_DOUBLE:
1859     case SYMBOL_REF:
1860     case LABEL_REF:
1861     case CONST:
1862     case PC:
1863     case CC0:
1864     case REG:
1865     case SCRATCH:
1866       return 0;
1867
1868     case ASM_INPUT:
1869     case UNSPEC_VOLATILE:
1870     case TRAP_IF:
1871       return 1;
1872
1873     case ASM_OPERANDS:
1874       return MEM_VOLATILE_P (x);
1875
1876       /* Memory ref can trap unless it's a static var or a stack slot.  */
1877     case MEM:
1878       return rtx_addr_can_trap_p (XEXP (x, 0));
1879
1880       /* Division by a non-constant might trap.  */
1881     case DIV:
1882     case MOD:
1883     case UDIV:
1884     case UMOD:
1885       if (! CONSTANT_P (XEXP (x, 1))
1886           || GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
1887         return 1;
1888       /* This was const0_rtx, but by not using that,
1889          we can link this file into other programs.  */
1890       if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 0)
1891         return 1;
1892       break;
1893
1894     case EXPR_LIST:
1895       /* An EXPR_LIST is used to represent a function call.  This
1896          certainly may trap.  */
1897       return 1;
1898
1899     default:
1900       /* Any floating arithmetic may trap.  */
1901       if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
1902         return 1;
1903     }
1904
1905   fmt = GET_RTX_FORMAT (code);
1906   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1907     {
1908       if (fmt[i] == 'e')
1909         {
1910           if (may_trap_p (XEXP (x, i)))
1911             return 1;
1912         }
1913       else if (fmt[i] == 'E')
1914         {
1915           register int j;
1916           for (j = 0; j < XVECLEN (x, i); j++)
1917             if (may_trap_p (XVECEXP (x, i, j)))
1918               return 1;
1919         }
1920     }
1921   return 0;
1922 }
1923 \f
1924 /* Return nonzero if X contains a comparison that is not either EQ or NE,
1925    i.e., an inequality.  */
1926
1927 int
1928 inequality_comparisons_p (x)
1929      rtx x;
1930 {
1931   register const char *fmt;
1932   register int len, i;
1933   register enum rtx_code code = GET_CODE (x);
1934
1935   switch (code)
1936     {
1937     case REG:
1938     case SCRATCH:
1939     case PC:
1940     case CC0:
1941     case CONST_INT:
1942     case CONST_DOUBLE:
1943     case CONST:
1944     case LABEL_REF:
1945     case SYMBOL_REF:
1946       return 0;
1947
1948     case LT:
1949     case LTU:
1950     case GT:
1951     case GTU:
1952     case LE:
1953     case LEU:
1954     case GE:
1955     case GEU:
1956       return 1;
1957       
1958     default:
1959       break;
1960     }
1961
1962   len = GET_RTX_LENGTH (code);
1963   fmt = GET_RTX_FORMAT (code);
1964
1965   for (i = 0; i < len; i++)
1966     {
1967       if (fmt[i] == 'e')
1968         {
1969           if (inequality_comparisons_p (XEXP (x, i)))
1970             return 1;
1971         }
1972       else if (fmt[i] == 'E')
1973         {
1974           register int j;
1975           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1976             if (inequality_comparisons_p (XVECEXP (x, i, j)))
1977               return 1;
1978         }
1979     }
1980             
1981   return 0;
1982 }
1983 \f
1984 /* Replace any occurrence of FROM in X with TO.  The function does
1985    not enter into CONST_DOUBLE for the replace.
1986
1987    Note that copying is not done so X must not be shared unless all copies
1988    are to be modified.  */
1989
1990 rtx
1991 replace_rtx (x, from, to)
1992      rtx x, from, to;
1993 {
1994   register int i, j;
1995   register const char *fmt;
1996
1997   /* The following prevents loops occurrence when we change MEM in
1998      CONST_DOUBLE onto the same CONST_DOUBLE. */
1999   if (x != 0 && GET_CODE (x) == CONST_DOUBLE)
2000     return x;
2001
2002   if (x == from)
2003     return to;
2004
2005   /* Allow this function to make replacements in EXPR_LISTs.  */
2006   if (x == 0)
2007     return 0;
2008
2009   fmt = GET_RTX_FORMAT (GET_CODE (x));
2010   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2011     {
2012       if (fmt[i] == 'e')
2013         XEXP (x, i) = replace_rtx (XEXP (x, i), from, to);
2014       else if (fmt[i] == 'E')
2015         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2016           XVECEXP (x, i, j) = replace_rtx (XVECEXP (x, i, j), from, to);
2017     }
2018
2019   return x;
2020 }  
2021 \f
2022 /* Throughout the rtx X, replace many registers according to REG_MAP.
2023    Return the replacement for X (which may be X with altered contents).
2024    REG_MAP[R] is the replacement for register R, or 0 for don't replace.
2025    NREGS is the length of REG_MAP; regs >= NREGS are not mapped.  
2026
2027    We only support REG_MAP entries of REG or SUBREG.  Also, hard registers
2028    should not be mapped to pseudos or vice versa since validate_change
2029    is not called.
2030
2031    If REPLACE_DEST is 1, replacements are also done in destinations;
2032    otherwise, only sources are replaced.  */
2033
2034 rtx
2035 replace_regs (x, reg_map, nregs, replace_dest)
2036      rtx x;
2037      rtx *reg_map;
2038      unsigned int nregs;
2039      int replace_dest;
2040 {
2041   register enum rtx_code code;
2042   register int i;
2043   register const char *fmt;
2044
2045   if (x == 0)
2046     return x;
2047
2048   code = GET_CODE (x);
2049   switch (code)
2050     {
2051     case SCRATCH:
2052     case PC:
2053     case CC0:
2054     case CONST_INT:
2055     case CONST_DOUBLE:
2056     case CONST:
2057     case SYMBOL_REF:
2058     case LABEL_REF:
2059       return x;
2060
2061     case REG:
2062       /* Verify that the register has an entry before trying to access it.  */
2063       if (REGNO (x) < nregs && reg_map[REGNO (x)] != 0)
2064         {
2065           /* SUBREGs can't be shared.  Always return a copy to ensure that if
2066              this replacement occurs more than once then each instance will
2067              get distinct rtx.  */
2068           if (GET_CODE (reg_map[REGNO (x)]) == SUBREG)
2069             return copy_rtx (reg_map[REGNO (x)]);
2070           return reg_map[REGNO (x)];
2071         }
2072       return x;
2073
2074     case SUBREG:
2075       /* Prevent making nested SUBREGs.  */
2076       if (GET_CODE (SUBREG_REG (x)) == REG && REGNO (SUBREG_REG (x)) < nregs
2077           && reg_map[REGNO (SUBREG_REG (x))] != 0
2078           && GET_CODE (reg_map[REGNO (SUBREG_REG (x))]) == SUBREG)
2079         {
2080           rtx map_val = reg_map[REGNO (SUBREG_REG (x))];
2081           rtx map_inner = SUBREG_REG (map_val);
2082
2083           if (GET_MODE (x) == GET_MODE (map_inner))
2084             return map_inner;
2085           else
2086             {
2087               /* We cannot call gen_rtx here since we may be linked with
2088                  genattrtab.c.  */
2089               /* Let's try clobbering the incoming SUBREG and see
2090                  if this is really safe.  */
2091               SUBREG_REG (x) = map_inner;
2092               SUBREG_WORD (x) += SUBREG_WORD (map_val);
2093               return x;
2094 #if 0
2095               rtx new = rtx_alloc (SUBREG);
2096               PUT_MODE (new, GET_MODE (x));
2097               SUBREG_REG (new) = map_inner;
2098               SUBREG_WORD (new) = SUBREG_WORD (x) + SUBREG_WORD (map_val);
2099 #endif
2100             }
2101         }
2102       break;
2103
2104     case SET:
2105       if (replace_dest)
2106         SET_DEST (x) = replace_regs (SET_DEST (x), reg_map, nregs, 0);
2107
2108       else if (GET_CODE (SET_DEST (x)) == MEM
2109                || GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
2110         /* Even if we are not to replace destinations, replace register if it
2111            is CONTAINED in destination (destination is memory or
2112            STRICT_LOW_PART).  */
2113         XEXP (SET_DEST (x), 0) = replace_regs (XEXP (SET_DEST (x), 0),
2114                                                reg_map, nregs, 0);
2115       else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
2116         /* Similarly, for ZERO_EXTRACT we replace all operands.  */
2117         break;
2118
2119       SET_SRC (x) = replace_regs (SET_SRC (x), reg_map, nregs, 0);
2120       return x;
2121       
2122     default:
2123       break;
2124     }
2125
2126   fmt = GET_RTX_FORMAT (code);
2127   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2128     {
2129       if (fmt[i] == 'e')
2130         XEXP (x, i) = replace_regs (XEXP (x, i), reg_map, nregs, replace_dest);
2131       else if (fmt[i] == 'E')
2132         {
2133           register int j;
2134           for (j = 0; j < XVECLEN (x, i); j++)
2135             XVECEXP (x, i, j) = replace_regs (XVECEXP (x, i, j), reg_map,
2136                                               nregs, replace_dest);
2137         }
2138     }
2139   return x;
2140 }
2141
2142 /* Return 1 if X, the SRC_SRC of  SET of (pc) contain a REG or MEM that is
2143    not in the constant pool and not in the condition of an IF_THEN_ELSE.  */
2144
2145 static int
2146 jmp_uses_reg_or_mem (x)
2147      rtx x;
2148 {
2149   enum rtx_code code = GET_CODE (x);
2150   int i, j;
2151   const char *fmt;
2152
2153   switch (code)
2154     {
2155     case CONST:
2156     case LABEL_REF:
2157     case PC:
2158       return 0;
2159
2160     case REG:
2161       return 1;
2162
2163     case MEM:
2164       return ! (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
2165                 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)));
2166
2167     case IF_THEN_ELSE:
2168       return (jmp_uses_reg_or_mem (XEXP (x, 1))
2169               || jmp_uses_reg_or_mem (XEXP (x, 2)));
2170
2171     case PLUS:  case MINUS:  case MULT:
2172       return (jmp_uses_reg_or_mem (XEXP (x, 0))
2173               || jmp_uses_reg_or_mem (XEXP (x, 1)));
2174
2175     default:
2176       break;
2177     }
2178
2179   fmt = GET_RTX_FORMAT (code);
2180   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2181     {
2182       if (fmt[i] == 'e'
2183           && jmp_uses_reg_or_mem (XEXP (x, i)))
2184         return 1;
2185
2186       else if (fmt[i] == 'E')
2187         for (j = 0; j < XVECLEN (x, i); j++)
2188           if (jmp_uses_reg_or_mem (XVECEXP (x, i, j)))
2189             return 1;
2190     }
2191
2192   return 0;
2193 }
2194
2195 /* Return nonzero if INSN is an indirect jump (aka computed jump).
2196
2197    Tablejumps and casesi insns are not considered indirect jumps;
2198    we can recognize them by a (use (label_ref)).  */
2199
2200 int
2201 computed_jump_p (insn)
2202      rtx insn;
2203 {
2204   int i;
2205   if (GET_CODE (insn) == JUMP_INSN)
2206     {
2207       rtx pat = PATTERN (insn);
2208
2209       if (GET_CODE (pat) == PARALLEL)
2210         {
2211           int len = XVECLEN (pat, 0);
2212           int has_use_labelref = 0;
2213
2214           for (i = len - 1; i >= 0; i--)
2215             if (GET_CODE (XVECEXP (pat, 0, i)) == USE
2216                 && (GET_CODE (XEXP (XVECEXP (pat, 0, i), 0))
2217                     == LABEL_REF))
2218               has_use_labelref = 1;
2219
2220           if (! has_use_labelref)
2221             for (i = len - 1; i >= 0; i--)
2222               if (GET_CODE (XVECEXP (pat, 0, i)) == SET
2223                   && SET_DEST (XVECEXP (pat, 0, i)) == pc_rtx
2224                   && jmp_uses_reg_or_mem (SET_SRC (XVECEXP (pat, 0, i))))
2225                 return 1;
2226         }
2227       else if (GET_CODE (pat) == SET
2228                && SET_DEST (pat) == pc_rtx
2229                && jmp_uses_reg_or_mem (SET_SRC (pat)))
2230         return 1;
2231     }
2232   return 0;
2233 }
2234
2235 /* Traverse X via depth-first search, calling F for each
2236    sub-expression (including X itself).  F is also passed the DATA.
2237    If F returns -1, do not traverse sub-expressions, but continue
2238    traversing the rest of the tree.  If F ever returns any other
2239    non-zero value, stop the traversal, and return the value returned
2240    by F.  Otherwise, return 0.  This function does not traverse inside
2241    tree structure that contains RTX_EXPRs, or into sub-expressions
2242    whose format code is `0' since it is not known whether or not those
2243    codes are actually RTL.
2244
2245    This routine is very general, and could (should?) be used to
2246    implement many of the other routines in this file.  */
2247
2248 int
2249 for_each_rtx (x, f, data)
2250      rtx *x;
2251      rtx_function f;
2252      void *data;
2253 {
2254   int result;
2255   int length;
2256   const char* format;
2257   int i;
2258
2259   /* Call F on X.  */
2260   result = (*f)(x, data);
2261   if (result == -1)
2262     /* Do not traverse sub-expressions.  */
2263     return 0;
2264   else if (result != 0)
2265     /* Stop the traversal.  */
2266     return result;
2267
2268   if (*x == NULL_RTX)
2269     /* There are no sub-expressions.  */
2270     return 0;
2271
2272   length = GET_RTX_LENGTH (GET_CODE (*x));
2273   format = GET_RTX_FORMAT (GET_CODE (*x));
2274
2275   for (i = 0; i < length; ++i) 
2276     {
2277       switch (format[i]) 
2278         {
2279         case 'e':
2280           result = for_each_rtx (&XEXP (*x, i), f, data);
2281           if (result != 0)
2282             return result;
2283           break;
2284
2285         case 'V':
2286         case 'E':
2287           if (XVEC (*x, i) != 0) 
2288             {
2289               int j;
2290               for (j = 0; j < XVECLEN (*x, i); ++j)
2291                 {
2292                   result = for_each_rtx (&XVECEXP (*x, i, j), f, data);
2293                   if (result != 0)
2294                     return result;
2295                 }
2296             }
2297           break; 
2298
2299         default:
2300           /* Nothing to do.  */
2301           break;
2302         }
2303
2304     }
2305
2306   return 0;
2307 }
2308
2309 /* Searches X for any reference to REGNO, returning the rtx of the
2310    reference found if any.  Otherwise, returns NULL_RTX.  */
2311
2312 rtx
2313 regno_use_in (regno, x)
2314      unsigned int regno;
2315      rtx x;
2316 {
2317   register const char *fmt;
2318   int i, j;
2319   rtx tem;
2320
2321   if (GET_CODE (x) == REG && REGNO (x) == regno)
2322     return x;
2323
2324   fmt = GET_RTX_FORMAT (GET_CODE (x));
2325   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2326     {
2327       if (fmt[i] == 'e')
2328         {
2329           if ((tem = regno_use_in (regno, XEXP (x, i))))
2330             return tem;
2331         }
2332       else if (fmt[i] == 'E')
2333         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2334           if ((tem = regno_use_in (regno , XVECEXP (x, i, j))))
2335             return tem;
2336     }
2337
2338   return NULL_RTX;
2339 }
2340
2341
2342 /* Return 1 if X is an autoincrement side effect and the register is
2343    not the stack pointer.  */
2344 int
2345 auto_inc_p (x)
2346      rtx x;
2347 {
2348   switch (GET_CODE (x))
2349     {
2350     case PRE_INC:
2351     case POST_INC:
2352     case PRE_DEC:
2353     case POST_DEC:
2354     case PRE_MODIFY:
2355     case POST_MODIFY:
2356       /* There are no REG_INC notes for SP.  */
2357       if (XEXP (x, 0) != stack_pointer_rtx)
2358         return 1;
2359     default:
2360       break;
2361     }
2362   return 0;
2363 }
2364
2365 /* Return 1 if the sequence of instructions beginning with FROM and up
2366    to and including TO is safe to move.  If NEW_TO is non-NULL, and
2367    the sequence is not already safe to move, but can be easily
2368    extended to a sequence which is safe, then NEW_TO will point to the
2369    end of the extended sequence.  
2370  
2371    For now, this function only checks that the region contains whole
2372    exception regiongs, but it could be extended to check additional
2373    conditions as well.  */
2374
2375 int
2376 insns_safe_to_move_p (from, to, new_to)
2377      rtx from;
2378      rtx to;
2379      rtx *new_to;
2380 {
2381   int eh_region_count = 0;
2382   int past_to_p = 0;
2383   rtx r = from;
2384
2385   /* By default, assume the end of the region will be what was
2386      suggested.  */
2387   if (new_to)
2388     *new_to = to;
2389
2390   while (r)
2391     {
2392       if (GET_CODE (r) == NOTE)
2393         {
2394           switch (NOTE_LINE_NUMBER (r))
2395             {
2396             case NOTE_INSN_EH_REGION_BEG:
2397               ++eh_region_count;
2398               break;
2399
2400             case NOTE_INSN_EH_REGION_END:
2401               if (eh_region_count == 0)
2402                 /* This sequence of instructions contains the end of
2403                    an exception region, but not he beginning.  Moving
2404                    it will cause chaos.  */
2405                 return 0;
2406
2407               --eh_region_count;
2408               break;
2409
2410             default:
2411               break;
2412             }
2413         }
2414       else if (past_to_p)
2415         /* If we've passed TO, and we see a non-note instruction, we
2416            can't extend the sequence to a movable sequence.  */
2417         return 0;
2418
2419       if (r == to)
2420         {
2421           if (!new_to)
2422             /* It's OK to move the sequence if there were matched sets of
2423                exception region notes.  */
2424             return eh_region_count == 0;
2425           
2426           past_to_p = 1;
2427         }
2428
2429       /* It's OK to move the sequence if there were matched sets of
2430          exception region notes.  */
2431       if (past_to_p && eh_region_count == 0)
2432         {
2433           *new_to = r;
2434           return 1;
2435         }
2436
2437       /* Go to the next instruction.  */
2438       r = NEXT_INSN (r);
2439     }
2440   
2441   return 0;
2442 }
2443
2444 /* Return non-zero if IN contains a piece of rtl that has the address LOC */
2445 int
2446 loc_mentioned_in_p (loc, in)
2447      rtx *loc, in;
2448 {
2449   enum rtx_code code = GET_CODE (in);
2450   const char *fmt = GET_RTX_FORMAT (code);
2451   int i, j;
2452
2453   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2454     {
2455       if (loc == &in->fld[i].rtx)
2456         return 1;
2457       if (fmt[i] == 'e')
2458         {
2459           if (loc_mentioned_in_p (loc, XEXP (in, i)))
2460             return 1;
2461         }
2462       else if (fmt[i] == 'E')
2463         for (j = XVECLEN (in, i) - 1; j >= 0; j--)
2464           if (loc_mentioned_in_p (loc, XVECEXP (in, i, j)))
2465             return 1;
2466     }
2467   return 0;
2468 }