OSDN Git Service

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