OSDN Git Service

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