OSDN Git Service

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