OSDN Git Service

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