OSDN Git Service

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