OSDN Git Service

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