OSDN Git Service

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