OSDN Git Service

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