OSDN Git Service

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