OSDN Git Service

[pf3gnuchains/gcc-fork.git] / gcc / rtlanal.c
1 /* Analyze RTL for C-Compiler
2    Copyright (C) 1987, 88, 92-97, 1998 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21
22 #include "config.h"
23 #include "system.h"
24 #include "rtl.h"
25
26 static int rtx_addr_can_trap_p  PROTO((rtx));
27 static void reg_set_p_1         PROTO((rtx, rtx));
28 static void reg_set_last_1      PROTO((rtx, rtx));
29
30
31 /* Forward declarations */
32 static int jmp_uses_reg_or_mem          PROTO((rtx));
33
34 /* Bit flags that specify the machine subtype we are compiling for.
35    Bits are tested using macros TARGET_... defined in the tm.h file
36    and set by `-m...' switches.  Must be defined in rtlanal.c.  */
37
38 int target_flags;
39 \f
40 /* Return 1 if the value of X is unstable
41    (would be different at a different point in the program).
42    The frame pointer, arg pointer, etc. are considered stable
43    (within one function) and so is anything marked `unchanging'.  */
44
45 int
46 rtx_unstable_p (x)
47      rtx x;
48 {
49   register RTX_CODE code = GET_CODE (x);
50   register int i;
51   register char *fmt;
52
53   if (code == MEM)
54     return ! RTX_UNCHANGING_P (x);
55
56   if (code == QUEUED)
57     return 1;
58
59   if (code == CONST || code == CONST_INT)
60     return 0;
61
62   if (code == REG)
63     return ! (REGNO (x) == FRAME_POINTER_REGNUM
64               || REGNO (x) == HARD_FRAME_POINTER_REGNUM
65               || REGNO (x) == ARG_POINTER_REGNUM
66               || RTX_UNCHANGING_P (x));
67
68   fmt = GET_RTX_FORMAT (code);
69   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
70     if (fmt[i] == 'e')
71       if (rtx_unstable_p (XEXP (x, i)))
72         return 1;
73   return 0;
74 }
75
76 /* Return 1 if X has a value that can vary even between two
77    executions of the program.  0 means X can be compared reliably
78    against certain constants or near-constants.
79    The frame pointer and the arg pointer are considered constant.  */
80
81 int
82 rtx_varies_p (x)
83      rtx x;
84 {
85   register RTX_CODE code = GET_CODE (x);
86   register int i;
87   register char *fmt;
88
89   switch (code)
90     {
91     case MEM:
92     case QUEUED:
93       return 1;
94
95     case CONST:
96     case CONST_INT:
97     case CONST_DOUBLE:
98     case SYMBOL_REF:
99     case LABEL_REF:
100       return 0;
101
102     case REG:
103       /* Note that we have to test for the actual rtx used for the frame
104          and arg pointers and not just the register number in case we have
105          eliminated the frame and/or arg pointer and are using it
106          for pseudos.  */
107       return ! (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
108                 || x == arg_pointer_rtx || x == pic_offset_table_rtx);
109
110     case LO_SUM:
111       /* The operand 0 of a LO_SUM is considered constant
112          (in fact is it related specifically to operand 1).  */
113       return rtx_varies_p (XEXP (x, 1));
114       
115     default:
116       break;
117     }
118
119   fmt = GET_RTX_FORMAT (code);
120   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
121     if (fmt[i] == 'e')
122       if (rtx_varies_p (XEXP (x, i)))
123         return 1;
124   return 0;
125 }
126
127 /* Return 0 if the use of X as an address in a MEM can cause a trap.  */
128
129 static int
130 rtx_addr_can_trap_p (x)
131      register rtx x;
132 {
133   register enum rtx_code code = GET_CODE (x);
134
135   switch (code)
136     {
137     case SYMBOL_REF:
138     case LABEL_REF:
139       /* SYMBOL_REF is problematic due to the possible presence of
140          a #pragma weak, but to say that loads from symbols can trap is
141          *very* costly.  It's not at all clear what's best here.  For
142          now, we ignore the impact of #pragma weak.  */
143       return 0;
144
145     case REG:
146       /* As in rtx_varies_p, we have to use the actual rtx, not reg number.  */
147       return ! (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
148                 || x == stack_pointer_rtx || x == arg_pointer_rtx);
149
150     case CONST:
151       return rtx_addr_can_trap_p (XEXP (x, 0));
152
153     case PLUS:
154       /* An address is assumed not to trap if it is an address that can't
155          trap plus a constant integer.  */
156       return (rtx_addr_can_trap_p (XEXP (x, 0))
157               || GET_CODE (XEXP (x, 1)) != CONST_INT);
158
159     case LO_SUM:
160       return rtx_addr_can_trap_p (XEXP (x, 1));
161       
162     default:
163       break;
164     }
165
166   /* If it isn't one of the case above, it can cause a trap.  */
167   return 1;
168 }
169
170 /* Return 1 if X refers to a memory location whose address 
171    cannot be compared reliably with constant addresses,
172    or if X refers to a BLKmode memory object.  */
173
174 int
175 rtx_addr_varies_p (x)
176      rtx x;
177 {
178   register enum rtx_code code;
179   register int i;
180   register char *fmt;
181
182   if (x == 0)
183     return 0;
184
185   code = GET_CODE (x);
186   if (code == MEM)
187     return GET_MODE (x) == BLKmode || rtx_varies_p (XEXP (x, 0));
188
189   fmt = GET_RTX_FORMAT (code);
190   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
191     if (fmt[i] == 'e')
192       {
193         if (rtx_addr_varies_p (XEXP (x, i)))
194           return 1;
195       }
196     else if (fmt[i] == 'E')
197       {
198         int j;
199         for (j = 0; j < XVECLEN (x, i); j++)
200           if (rtx_addr_varies_p (XVECEXP (x, i, j)))
201             return 1;
202       }
203   return 0;
204 }
205 \f
206 /* Return the value of the integer term in X, if one is apparent;
207    otherwise return 0.
208    Only obvious integer terms are detected.
209    This is used in cse.c with the `related_value' field.*/
210
211 HOST_WIDE_INT
212 get_integer_term (x)
213      rtx x;
214 {
215   if (GET_CODE (x) == CONST)
216     x = XEXP (x, 0);
217
218   if (GET_CODE (x) == MINUS
219       && GET_CODE (XEXP (x, 1)) == CONST_INT)
220     return - INTVAL (XEXP (x, 1));
221   if (GET_CODE (x) == PLUS
222       && GET_CODE (XEXP (x, 1)) == CONST_INT)
223     return INTVAL (XEXP (x, 1));
224   return 0;
225 }
226
227 /* If X is a constant, return the value sans apparent integer term;
228    otherwise return 0.
229    Only obvious integer terms are detected.  */
230
231 rtx
232 get_related_value (x)
233      rtx x;
234 {
235   if (GET_CODE (x) != CONST)
236     return 0;
237   x = XEXP (x, 0);
238   if (GET_CODE (x) == PLUS
239       && GET_CODE (XEXP (x, 1)) == CONST_INT)
240     return XEXP (x, 0);
241   else if (GET_CODE (x) == MINUS
242            && GET_CODE (XEXP (x, 1)) == CONST_INT)
243     return XEXP (x, 0);
244   return 0;
245 }
246 \f
247 /* Nonzero if register REG appears somewhere within IN.
248    Also works if REG is not a register; in this case it checks
249    for a subexpression of IN that is Lisp "equal" to REG.  */
250
251 int
252 reg_mentioned_p (reg, in)
253      register rtx reg, in;
254 {
255   register char *fmt;
256   register int i;
257   register enum rtx_code code;
258
259   if (in == 0)
260     return 0;
261
262   if (reg == in)
263     return 1;
264
265   if (GET_CODE (in) == LABEL_REF)
266     return reg == XEXP (in, 0);
267
268   code = GET_CODE (in);
269
270   switch (code)
271     {
272       /* Compare registers by number.  */
273     case REG:
274       return GET_CODE (reg) == REG && REGNO (in) == REGNO (reg);
275
276       /* These codes have no constituent expressions
277          and are unique.  */
278     case SCRATCH:
279     case CC0:
280     case PC:
281       return 0;
282
283     case CONST_INT:
284       return GET_CODE (reg) == CONST_INT && INTVAL (in) == INTVAL (reg);
285       
286     case CONST_DOUBLE:
287       /* These are kept unique for a given value.  */
288       return 0;
289       
290     default:
291       break;
292     }
293
294   if (GET_CODE (reg) == code && rtx_equal_p (reg, in))
295     return 1;
296
297   fmt = GET_RTX_FORMAT (code);
298
299   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
300     {
301       if (fmt[i] == 'E')
302         {
303           register int j;
304           for (j = XVECLEN (in, i) - 1; j >= 0; j--)
305             if (reg_mentioned_p (reg, XVECEXP (in, i, j)))
306               return 1;
307         }
308       else if (fmt[i] == 'e'
309                && reg_mentioned_p (reg, XEXP (in, i)))
310         return 1;
311     }
312   return 0;
313 }
314 \f
315 /* Return 1 if in between BEG and END, exclusive of BEG and END, there is
316    no CODE_LABEL insn.  */
317
318 int
319 no_labels_between_p (beg, end)
320      rtx beg, end;
321 {
322   register rtx p;
323   for (p = NEXT_INSN (beg); p != end; p = NEXT_INSN (p))
324     if (GET_CODE (p) == CODE_LABEL)
325       return 0;
326   return 1;
327 }
328
329 /* Nonzero if register REG is used in an insn between
330    FROM_INSN and TO_INSN (exclusive of those two).  */
331
332 int
333 reg_used_between_p (reg, from_insn, to_insn)
334      rtx reg, from_insn, to_insn;
335 {
336   register rtx insn;
337
338   if (from_insn == to_insn)
339     return 0;
340
341   for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
342     if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
343         && (reg_overlap_mentioned_p (reg, PATTERN (insn))
344            || (GET_CODE (insn) == CALL_INSN
345               && (find_reg_fusage (insn, USE, reg)
346                   || find_reg_fusage (insn, CLOBBER, reg)))))
347       return 1;
348   return 0;
349 }
350 \f
351 /* Nonzero if the old value of X, a register, is referenced in BODY.  If X
352    is entirely replaced by a new value and the only use is as a SET_DEST,
353    we do not consider it a reference.  */
354
355 int
356 reg_referenced_p (x, body)
357      rtx x;
358      rtx body;
359 {
360   int i;
361
362   switch (GET_CODE (body))
363     {
364     case SET:
365       if (reg_overlap_mentioned_p (x, SET_SRC (body)))
366         return 1;
367
368       /* If the destination is anything other than CC0, PC, a REG or a SUBREG
369          of a REG that occupies all of the REG, the insn references X if
370          it is mentioned in the destination.  */
371       if (GET_CODE (SET_DEST (body)) != CC0
372           && GET_CODE (SET_DEST (body)) != PC
373           && GET_CODE (SET_DEST (body)) != REG
374           && ! (GET_CODE (SET_DEST (body)) == SUBREG
375                 && GET_CODE (SUBREG_REG (SET_DEST (body))) == REG
376                 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (body))))
377                       + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
378                     == ((GET_MODE_SIZE (GET_MODE (SET_DEST (body)))
379                          + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
380           && reg_overlap_mentioned_p (x, SET_DEST (body)))
381         return 1;
382       return 0;
383
384     case ASM_OPERANDS:
385       for (i = ASM_OPERANDS_INPUT_LENGTH (body) - 1; i >= 0; i--)
386         if (reg_overlap_mentioned_p (x, ASM_OPERANDS_INPUT (body, i)))
387           return 1;
388       return 0;
389
390     case CALL:
391     case USE:
392       return reg_overlap_mentioned_p (x, body);
393
394     case TRAP_IF:
395       return reg_overlap_mentioned_p (x, TRAP_CONDITION (body));
396
397     case UNSPEC:
398     case UNSPEC_VOLATILE:
399     case PARALLEL:
400       for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
401         if (reg_referenced_p (x, XVECEXP (body, 0, i)))
402           return 1;
403       return 0;
404       
405     default:
406       return 0;
407     }
408 }
409
410 /* Nonzero if register REG is referenced in an insn between
411    FROM_INSN and TO_INSN (exclusive of those two).  Sets of REG do
412    not count.  */
413
414 int
415 reg_referenced_between_p (reg, from_insn, to_insn)
416      rtx reg, from_insn, to_insn;
417 {
418   register rtx insn;
419
420   if (from_insn == to_insn)
421     return 0;
422
423   for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
424     if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
425         && (reg_referenced_p (reg, PATTERN (insn))
426            || (GET_CODE (insn) == CALL_INSN
427               && find_reg_fusage (insn, USE, reg))))
428       return 1;
429   return 0;
430 }
431 \f
432 /* Nonzero if register REG is set or clobbered in an insn between
433    FROM_INSN and TO_INSN (exclusive of those two).  */
434
435 int
436 reg_set_between_p (reg, from_insn, to_insn)
437      rtx reg, from_insn, to_insn;
438 {
439   register rtx insn;
440
441   if (from_insn == to_insn)
442     return 0;
443
444   for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
445     if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
446         && reg_set_p (reg, insn))
447       return 1;
448   return 0;
449 }
450
451 /* Internals of reg_set_between_p.  */
452
453 static rtx reg_set_reg;
454 static int reg_set_flag;
455
456 static void
457 reg_set_p_1 (x, pat)
458      rtx x;
459      rtx pat ATTRIBUTE_UNUSED;
460 {
461   /* We don't want to return 1 if X is a MEM that contains a register
462      within REG_SET_REG.  */
463
464   if ((GET_CODE (x) != MEM)
465       && reg_overlap_mentioned_p (reg_set_reg, x))
466     reg_set_flag = 1;
467 }
468
469 int
470 reg_set_p (reg, insn)
471      rtx reg, insn;
472 {
473   rtx body = insn;
474
475   /* We can be passed an insn or part of one.  If we are passed an insn,
476      check if a side-effect of the insn clobbers REG.  */
477   if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
478     {
479       if (FIND_REG_INC_NOTE (insn, reg)
480           || (GET_CODE (insn) == CALL_INSN
481               /* We'd like to test call_used_regs here, but rtlanal.c can't
482                  reference that variable due to its use in genattrtab.  So
483                  we'll just be more conservative.
484
485                  ??? Unless we could ensure that the CALL_INSN_FUNCTION_USAGE
486                  information holds all clobbered registers.  */
487               && ((GET_CODE (reg) == REG
488                    && REGNO (reg) < FIRST_PSEUDO_REGISTER)
489                   || GET_CODE (reg) == MEM
490                   || find_reg_fusage (insn, CLOBBER, reg))))
491         return 1;
492
493       body = PATTERN (insn);
494     }
495
496   reg_set_reg = reg;
497   reg_set_flag = 0;
498   note_stores (body, reg_set_p_1);
499   return reg_set_flag;
500 }
501
502 /* Similar to reg_set_between_p, but check all registers in X.  Return 0
503    only if none of them are modified between START and END.  Do not
504    consider non-registers one way or the other.  */
505
506 int
507 regs_set_between_p (x, start, end)
508      rtx x;
509      rtx start, end;
510 {
511   enum rtx_code code = GET_CODE (x);
512   char *fmt;
513   int i, j;
514
515   switch (code)
516     {
517     case CONST_INT:
518     case CONST_DOUBLE:
519     case CONST:
520     case SYMBOL_REF:
521     case LABEL_REF:
522     case PC:
523     case CC0:
524       return 0;
525
526     case REG:
527       return reg_set_between_p (x, start, end);
528       
529     default:
530       break;
531     }
532
533   fmt = GET_RTX_FORMAT (code);
534   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
535     {
536       if (fmt[i] == 'e' && regs_set_between_p (XEXP (x, i), start, end))
537         return 1;
538
539       else if (fmt[i] == 'E')
540         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
541           if (regs_set_between_p (XVECEXP (x, i, j), start, end))
542             return 1;
543     }
544
545   return 0;
546 }
547
548 /* Similar to reg_set_between_p, but check all registers in X.  Return 0
549    only if none of them are modified between START and END.  Return 1 if
550    X contains a MEM; this routine does not perform any memory aliasing.  */
551
552 int
553 modified_between_p (x, start, end)
554      rtx x;
555      rtx start, end;
556 {
557   enum rtx_code code = GET_CODE (x);
558   char *fmt;
559   int i, j;
560
561   switch (code)
562     {
563     case CONST_INT:
564     case CONST_DOUBLE:
565     case CONST:
566     case SYMBOL_REF:
567     case LABEL_REF:
568       return 0;
569
570     case PC:
571     case CC0:
572       return 1;
573
574     case MEM:
575       /* If the memory is not constant, assume it is modified.  If it is
576          constant, we still have to check the address.  */
577       if (! RTX_UNCHANGING_P (x))
578         return 1;
579       break;
580
581     case REG:
582       return reg_set_between_p (x, start, end);
583       
584     default:
585       break;
586     }
587
588   fmt = GET_RTX_FORMAT (code);
589   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
590     {
591       if (fmt[i] == 'e' && modified_between_p (XEXP (x, i), start, end))
592         return 1;
593
594       if (fmt[i] == 'E')
595         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
596           if (modified_between_p (XVECEXP (x, i, j), start, end))
597             return 1;
598     }
599
600   return 0;
601 }
602
603 /* Similar to reg_set_p, but check all registers in X.  Return 0 only if none
604    of them are modified in INSN.  Return 1 if X contains a MEM; this routine
605    does not perform any memory aliasing.  */
606
607 int
608 modified_in_p (x, insn)
609      rtx x;
610      rtx insn;
611 {
612   enum rtx_code code = GET_CODE (x);
613   char *fmt;
614   int i, j;
615
616   switch (code)
617     {
618     case CONST_INT:
619     case CONST_DOUBLE:
620     case CONST:
621     case SYMBOL_REF:
622     case LABEL_REF:
623       return 0;
624
625     case PC:
626     case CC0:
627       return 1;
628
629     case MEM:
630       /* If the memory is not constant, assume it is modified.  If it is
631          constant, we still have to check the address.  */
632       if (! RTX_UNCHANGING_P (x))
633         return 1;
634       break;
635
636     case REG:
637       return reg_set_p (x, insn);
638
639     default:
640       break;
641     }
642
643   fmt = GET_RTX_FORMAT (code);
644   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
645     {
646       if (fmt[i] == 'e' && modified_in_p (XEXP (x, i), insn))
647         return 1;
648
649       if (fmt[i] == 'E')
650         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
651           if (modified_in_p (XVECEXP (x, i, j), insn))
652             return 1;
653     }
654
655   return 0;
656 }
657 \f
658 /* Given an INSN, return a SET expression if this insn has only a single SET.
659    It may also have CLOBBERs, USEs, or SET whose output
660    will not be used, which we ignore.  */
661
662 rtx
663 single_set (insn)
664      rtx insn;
665 {
666   rtx set;
667   int i;
668   
669   if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
670     return 0;
671
672   if (GET_CODE (PATTERN (insn)) == SET)
673     return PATTERN (insn);
674   
675   else if (GET_CODE (PATTERN (insn)) == PARALLEL)
676     {
677       for (i = 0, set = 0; i < XVECLEN (PATTERN (insn), 0); i++)
678         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET
679             && (! find_reg_note (insn, REG_UNUSED,
680                                  SET_DEST (XVECEXP (PATTERN (insn), 0, i)))
681                 || side_effects_p (XVECEXP (PATTERN (insn), 0, i))))
682           {
683             if (set)
684               return 0;
685             else
686               set = XVECEXP (PATTERN (insn), 0, i);
687           }
688       return set;
689     }
690   
691   return 0;
692 }
693 \f
694 /* Return the last thing that X was assigned from before *PINSN.  Verify that
695    the object is not modified up to VALID_TO.  If it was, if we hit
696    a partial assignment to X, or hit a CODE_LABEL first, return X.  If we
697    found an assignment, update *PINSN to point to it.  */
698
699 rtx
700 find_last_value (x, pinsn, valid_to)
701      rtx x;
702      rtx *pinsn;
703      rtx valid_to;
704 {
705   rtx p;
706
707   for (p = PREV_INSN (*pinsn); p && GET_CODE (p) != CODE_LABEL;
708        p = PREV_INSN (p))
709     if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
710       {
711         rtx set = single_set (p);
712         rtx note = find_reg_note (p, REG_EQUAL, NULL_RTX);
713
714         if (set && rtx_equal_p (x, SET_DEST (set)))
715           {
716             rtx src = SET_SRC (set);
717
718             if (note && GET_CODE (XEXP (note, 0)) != EXPR_LIST)
719               src = XEXP (note, 0);
720
721             if (! modified_between_p (src, PREV_INSN (p), valid_to)
722                 /* Reject hard registers because we don't usually want
723                    to use them; we'd rather use a pseudo.  */
724                 && ! (GET_CODE (src) == REG
725                       && REGNO (src) < FIRST_PSEUDO_REGISTER))
726               {
727                 *pinsn = p;
728                 return src;
729               }
730           }
731           
732         /* If set in non-simple way, we don't have a value.  */
733         if (reg_set_p (x, p))
734           break;
735       }
736
737   return x;
738 }     
739 \f
740 /* Return nonzero if register in range [REGNO, ENDREGNO)
741    appears either explicitly or implicitly in X
742    other than being stored into.
743
744    References contained within the substructure at LOC do not count.
745    LOC may be zero, meaning don't ignore anything.  */
746
747 int
748 refers_to_regno_p (regno, endregno, x, loc)
749      int regno, endregno;
750      rtx x;
751      rtx *loc;
752 {
753   register int i;
754   register RTX_CODE code;
755   register char *fmt;
756
757  repeat:
758   /* The contents of a REG_NONNEG note is always zero, so we must come here
759      upon repeat in case the last REG_NOTE is a REG_NONNEG note.  */
760   if (x == 0)
761     return 0;
762
763   code = GET_CODE (x);
764
765   switch (code)
766     {
767     case REG:
768       i = REGNO (x);
769
770       /* If we modifying the stack, frame, or argument pointer, it will
771          clobber a virtual register.  In fact, we could be more precise,
772          but it isn't worth it.  */
773       if ((i == STACK_POINTER_REGNUM
774 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
775            || i == ARG_POINTER_REGNUM
776 #endif
777            || i == FRAME_POINTER_REGNUM)
778           && regno >= FIRST_VIRTUAL_REGISTER && regno <= LAST_VIRTUAL_REGISTER)
779         return 1;
780
781       return (endregno > i
782               && regno < i + (i < FIRST_PSEUDO_REGISTER 
783                               ? HARD_REGNO_NREGS (i, GET_MODE (x))
784                               : 1));
785
786     case SUBREG:
787       /* If this is a SUBREG of a hard reg, we can see exactly which
788          registers are being modified.  Otherwise, handle normally.  */
789       if (GET_CODE (SUBREG_REG (x)) == REG
790           && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER)
791         {
792           int inner_regno = REGNO (SUBREG_REG (x)) + SUBREG_WORD (x);
793           int inner_endregno
794             = inner_regno + (inner_regno < FIRST_PSEUDO_REGISTER
795                              ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
796
797           return endregno > inner_regno && regno < inner_endregno;
798         }
799       break;
800
801     case CLOBBER:
802     case SET:
803       if (&SET_DEST (x) != loc
804           /* Note setting a SUBREG counts as referring to the REG it is in for
805              a pseudo but not for hard registers since we can
806              treat each word individually.  */
807           && ((GET_CODE (SET_DEST (x)) == SUBREG
808                && loc != &SUBREG_REG (SET_DEST (x))
809                && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG
810                && REGNO (SUBREG_REG (SET_DEST (x))) >= FIRST_PSEUDO_REGISTER
811                && refers_to_regno_p (regno, endregno,
812                                      SUBREG_REG (SET_DEST (x)), loc))
813               || (GET_CODE (SET_DEST (x)) != REG
814                   && refers_to_regno_p (regno, endregno, SET_DEST (x), loc))))
815         return 1;
816
817       if (code == CLOBBER || loc == &SET_SRC (x))
818         return 0;
819       x = SET_SRC (x);
820       goto repeat;
821
822     default:
823       break;
824     }
825
826   /* X does not match, so try its subexpressions.  */
827
828   fmt = GET_RTX_FORMAT (code);
829   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
830     {
831       if (fmt[i] == 'e' && loc != &XEXP (x, i))
832         {
833           if (i == 0)
834             {
835               x = XEXP (x, 0);
836               goto repeat;
837             }
838           else
839             if (refers_to_regno_p (regno, endregno, XEXP (x, i), loc))
840               return 1;
841         }
842       else if (fmt[i] == 'E')
843         {
844           register int j;
845           for (j = XVECLEN (x, i) - 1; j >=0; j--)
846             if (loc != &XVECEXP (x, i, j)
847                 && refers_to_regno_p (regno, endregno, XVECEXP (x, i, j), loc))
848               return 1;
849         }
850     }
851   return 0;
852 }
853
854 /* Nonzero if modifying X will affect IN.  If X is a register or a SUBREG,
855    we check if any register number in X conflicts with the relevant register
856    numbers.  If X is a constant, return 0.  If X is a MEM, return 1 iff IN
857    contains a MEM (we don't bother checking for memory addresses that can't
858    conflict because we expect this to be a rare case.  */
859
860 int
861 reg_overlap_mentioned_p (x, in)
862      rtx x, in;
863 {
864   int regno, endregno;
865
866   /* Overly conservative.  */
867   if (GET_CODE (x) == STRICT_LOW_PART)
868     x = XEXP (x, 0);
869
870   /* If either argument is a constant, then modifying X can not affect IN.  */
871   if (CONSTANT_P (x) || CONSTANT_P (in))
872     return 0;
873   else if (GET_CODE (x) == SUBREG)
874     {
875       regno = REGNO (SUBREG_REG (x));
876       if (regno < FIRST_PSEUDO_REGISTER)
877         regno += SUBREG_WORD (x);
878     }
879   else if (GET_CODE (x) == REG)
880     regno = REGNO (x);
881   else if (GET_CODE (x) == MEM)
882     {
883       char *fmt;
884       int i;
885
886       if (GET_CODE (in) == MEM)
887         return 1;
888
889       fmt = GET_RTX_FORMAT (GET_CODE (in));
890
891       for (i = GET_RTX_LENGTH (GET_CODE (in)) - 1; i >= 0; i--)
892         if (fmt[i] == 'e' && reg_overlap_mentioned_p (x, XEXP (in, i)))
893           return 1;
894
895       return 0;
896     }
897   else if (GET_CODE (x) == SCRATCH || GET_CODE (x) == PC
898            || GET_CODE (x) == CC0)
899     return reg_mentioned_p (x, in);
900   else if (GET_CODE (x) == PARALLEL
901            && GET_MODE (x) == BLKmode)
902     {
903       register int i;
904
905       /* If any register in here refers to it
906          we return true.  */
907       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
908         if (reg_overlap_mentioned_p (SET_DEST (XVECEXP (x, 0, i)), in))
909           return 1;
910       return 0;
911     }
912   else
913     abort ();
914
915   endregno = regno + (regno < FIRST_PSEUDO_REGISTER
916                       ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
917
918   return refers_to_regno_p (regno, endregno, in, NULL_PTR);
919 }
920 \f
921 /* Used for communications between the next few functions.  */
922
923 static int reg_set_last_unknown;
924 static rtx reg_set_last_value;
925 static int reg_set_last_first_regno, reg_set_last_last_regno;
926
927 /* Called via note_stores from reg_set_last.  */
928
929 static void
930 reg_set_last_1 (x, pat)
931      rtx x;
932      rtx pat;
933 {
934   int first, last;
935
936   /* If X is not a register, or is not one in the range we care
937      about, ignore.  */
938   if (GET_CODE (x) != REG)
939     return;
940
941   first = REGNO (x);
942   last = first + (first < FIRST_PSEUDO_REGISTER
943                   ? HARD_REGNO_NREGS (first, GET_MODE (x)) : 1);
944
945   if (first >= reg_set_last_last_regno
946       || last <= reg_set_last_first_regno)
947     return;
948
949   /* If this is a CLOBBER or is some complex LHS, or doesn't modify
950      exactly the registers we care about, show we don't know the value.  */
951   if (GET_CODE (pat) == CLOBBER || SET_DEST (pat) != x
952       || first != reg_set_last_first_regno
953       || last != reg_set_last_last_regno)
954     reg_set_last_unknown = 1;
955   else
956     reg_set_last_value = SET_SRC (pat);
957 }
958
959 /* Return the last value to which REG was set prior to INSN.  If we can't
960    find it easily, return 0.
961
962    We only return a REG, SUBREG, or constant because it is too hard to
963    check if a MEM remains unchanged.  */
964
965 rtx
966 reg_set_last (x, insn)
967      rtx x;
968      rtx insn;
969 {
970   rtx orig_insn = insn;
971
972   reg_set_last_first_regno = REGNO (x);
973
974   reg_set_last_last_regno
975     = reg_set_last_first_regno
976       + (reg_set_last_first_regno < FIRST_PSEUDO_REGISTER
977          ? HARD_REGNO_NREGS (reg_set_last_first_regno, GET_MODE (x)) : 1);
978
979   reg_set_last_unknown = 0;
980   reg_set_last_value = 0;
981
982   /* Scan backwards until reg_set_last_1 changed one of the above flags.
983      Stop when we reach a label or X is a hard reg and we reach a
984      CALL_INSN (if reg_set_last_last_regno is a hard reg).
985
986      If we find a set of X, ensure that its SET_SRC remains unchanged.  */
987
988   /* We compare with <= here, because reg_set_last_last_regno
989      is actually the number of the first reg *not* in X.  */
990   for (;
991        insn && GET_CODE (insn) != CODE_LABEL
992        && ! (GET_CODE (insn) == CALL_INSN
993              && reg_set_last_last_regno <= FIRST_PSEUDO_REGISTER);
994        insn = PREV_INSN (insn))
995     if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
996       {
997         note_stores (PATTERN (insn), reg_set_last_1);
998         if (reg_set_last_unknown)
999           return 0;
1000         else if (reg_set_last_value)
1001           {
1002             if (CONSTANT_P (reg_set_last_value)
1003                 || ((GET_CODE (reg_set_last_value) == REG
1004                      || GET_CODE (reg_set_last_value) == SUBREG)
1005                     && ! reg_set_between_p (reg_set_last_value,
1006                                             insn, orig_insn)))
1007               return reg_set_last_value;
1008             else
1009               return 0;
1010           }
1011       }
1012
1013   return 0;
1014 }
1015 \f
1016 /* This is 1 until after the rtl generation pass.  */
1017 int rtx_equal_function_value_matters;
1018
1019 /* Return 1 if X and Y are identical-looking rtx's.
1020    This is the Lisp function EQUAL for rtx arguments.  */
1021
1022 int
1023 rtx_equal_p (x, y)
1024      rtx x, y;
1025 {
1026   register int i;
1027   register int j;
1028   register enum rtx_code code;
1029   register char *fmt;
1030
1031   if (x == y)
1032     return 1;
1033   if (x == 0 || y == 0)
1034     return 0;
1035
1036   code = GET_CODE (x);
1037   /* Rtx's of different codes cannot be equal.  */
1038   if (code != GET_CODE (y))
1039     return 0;
1040
1041   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1042      (REG:SI x) and (REG:HI x) are NOT equivalent.  */
1043
1044   if (GET_MODE (x) != GET_MODE (y))
1045     return 0;
1046
1047   /* REG, LABEL_REF, and SYMBOL_REF can be compared nonrecursively.  */
1048
1049   if (code == REG)
1050     /* Until rtl generation is complete, don't consider a reference to the
1051        return register of the current function the same as the return from a
1052        called function.  This eases the job of function integration.  Once the
1053        distinction is no longer needed, they can be considered equivalent.  */
1054     return (REGNO (x) == REGNO (y)
1055             && (! rtx_equal_function_value_matters
1056                 || REG_FUNCTION_VALUE_P (x) == REG_FUNCTION_VALUE_P (y)));
1057   else if (code == LABEL_REF)
1058     return XEXP (x, 0) == XEXP (y, 0);
1059   else if (code == SYMBOL_REF)
1060     return XSTR (x, 0) == XSTR (y, 0);
1061   else if (code == SCRATCH || code == CONST_DOUBLE)
1062     return 0;
1063
1064   /* Compare the elements.  If any pair of corresponding elements
1065      fail to match, return 0 for the whole things.  */
1066
1067   fmt = GET_RTX_FORMAT (code);
1068   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1069     {
1070       switch (fmt[i])
1071         {
1072         case 'w':
1073           if (XWINT (x, i) != XWINT (y, i))
1074             return 0;
1075           break;
1076
1077         case 'n':
1078         case 'i':
1079           if (XINT (x, i) != XINT (y, i))
1080             return 0;
1081           break;
1082
1083         case 'V':
1084         case 'E':
1085           /* Two vectors must have the same length.  */
1086           if (XVECLEN (x, i) != XVECLEN (y, i))
1087             return 0;
1088
1089           /* And the corresponding elements must match.  */
1090           for (j = 0; j < XVECLEN (x, i); j++)
1091             if (rtx_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)) == 0)
1092               return 0;
1093           break;
1094
1095         case 'e':
1096           if (rtx_equal_p (XEXP (x, i), XEXP (y, i)) == 0)
1097             return 0;
1098           break;
1099
1100         case 'S':
1101         case 's':
1102           if (strcmp (XSTR (x, i), XSTR (y, i)))
1103             return 0;
1104           break;
1105
1106         case 'u':
1107           /* These are just backpointers, so they don't matter.  */
1108           break;
1109
1110         case '0':
1111           break;
1112
1113           /* It is believed that rtx's at this level will never
1114              contain anything but integers and other rtx's,
1115              except for within LABEL_REFs and SYMBOL_REFs.  */
1116         default:
1117           abort ();
1118         }
1119     }
1120   return 1;
1121 }
1122 \f
1123 /* Call FUN on each register or MEM that is stored into or clobbered by X.
1124    (X would be the pattern of an insn).
1125    FUN receives two arguments:
1126      the REG, MEM, CC0 or PC being stored in or clobbered,
1127      the SET or CLOBBER rtx that does the store.
1128
1129   If the item being stored in or clobbered is a SUBREG of a hard register,
1130   the SUBREG will be passed.  */
1131      
1132 void
1133 note_stores (x, fun)
1134      register rtx x;
1135      void (*fun) ();
1136 {
1137   if ((GET_CODE (x) == SET || GET_CODE (x) == CLOBBER))
1138     {
1139       register rtx dest = SET_DEST (x);
1140       while ((GET_CODE (dest) == SUBREG
1141               && (GET_CODE (SUBREG_REG (dest)) != REG
1142                   || REGNO (SUBREG_REG (dest)) >= FIRST_PSEUDO_REGISTER))
1143              || GET_CODE (dest) == ZERO_EXTRACT
1144              || GET_CODE (dest) == SIGN_EXTRACT
1145              || GET_CODE (dest) == STRICT_LOW_PART)
1146         dest = XEXP (dest, 0);
1147
1148       if (GET_CODE (dest) == PARALLEL
1149           && GET_MODE (dest) == BLKmode)
1150         {
1151           register int i;
1152           for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
1153             (*fun) (SET_DEST (XVECEXP (dest, 0, i)), x);
1154         }
1155       else
1156         (*fun) (dest, x);
1157     }
1158   else if (GET_CODE (x) == PARALLEL)
1159     {
1160       register int i;
1161       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1162         {
1163           register rtx y = XVECEXP (x, 0, i);
1164           if (GET_CODE (y) == SET || GET_CODE (y) == CLOBBER)
1165             {
1166               register rtx dest = SET_DEST (y);
1167               while ((GET_CODE (dest) == SUBREG
1168                       && (GET_CODE (SUBREG_REG (dest)) != REG
1169                           || (REGNO (SUBREG_REG (dest))
1170                               >= FIRST_PSEUDO_REGISTER)))
1171                      || GET_CODE (dest) == ZERO_EXTRACT
1172                      || GET_CODE (dest) == SIGN_EXTRACT
1173                      || GET_CODE (dest) == STRICT_LOW_PART)
1174                 dest = XEXP (dest, 0);
1175               if (GET_CODE (dest) == PARALLEL
1176                   && GET_MODE (dest) == BLKmode)
1177                 {
1178                   register int i;
1179                   for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
1180                     (*fun) (SET_DEST (XVECEXP (dest, 0, i)), y);
1181                 }
1182               else
1183                 (*fun) (dest, y);
1184             }
1185         }
1186     }
1187 }
1188 \f
1189 /* Return nonzero if X's old contents don't survive after INSN.
1190    This will be true if X is (cc0) or if X is a register and
1191    X dies in INSN or because INSN entirely sets X.
1192
1193    "Entirely set" means set directly and not through a SUBREG,
1194    ZERO_EXTRACT or SIGN_EXTRACT, so no trace of the old contents remains.
1195    Likewise, REG_INC does not count.
1196
1197    REG may be a hard or pseudo reg.  Renumbering is not taken into account,
1198    but for this use that makes no difference, since regs don't overlap
1199    during their lifetimes.  Therefore, this function may be used
1200    at any time after deaths have been computed (in flow.c).
1201
1202    If REG is a hard reg that occupies multiple machine registers, this
1203    function will only return 1 if each of those registers will be replaced
1204    by INSN.  */
1205
1206 int
1207 dead_or_set_p (insn, x)
1208      rtx insn;
1209      rtx x;
1210 {
1211   register int regno, last_regno;
1212   register int i;
1213
1214   /* Can't use cc0_rtx below since this file is used by genattrtab.c.  */
1215   if (GET_CODE (x) == CC0)
1216     return 1;
1217
1218   if (GET_CODE (x) != REG)
1219     abort ();
1220
1221   regno = REGNO (x);
1222   last_regno = (regno >= FIRST_PSEUDO_REGISTER ? regno
1223                 : regno + HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1);
1224
1225   for (i = regno; i <= last_regno; i++)
1226     if (! dead_or_set_regno_p (insn, i))
1227       return 0;
1228
1229   return 1;
1230 }
1231
1232 /* Utility function for dead_or_set_p to check an individual register.  Also
1233    called from flow.c.  */
1234
1235 int
1236 dead_or_set_regno_p (insn, test_regno)
1237      rtx insn;
1238      int test_regno;
1239 {
1240   int regno, endregno;
1241   rtx link;
1242
1243   /* See if there is a death note for something that includes
1244      TEST_REGNO.  */
1245   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1246     {
1247       if (REG_NOTE_KIND (link) != REG_DEAD
1248           || GET_CODE (XEXP (link, 0)) != REG)
1249         continue;
1250
1251       regno = REGNO (XEXP (link, 0));
1252       endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1253                   : regno + HARD_REGNO_NREGS (regno,
1254                                               GET_MODE (XEXP (link, 0))));
1255
1256       if (test_regno >= regno && test_regno < endregno)
1257         return 1;
1258     }
1259
1260   if (GET_CODE (insn) == CALL_INSN
1261       && find_regno_fusage (insn, CLOBBER, test_regno))
1262     return 1;
1263
1264   if (GET_CODE (PATTERN (insn)) == SET)
1265     {
1266       rtx dest = SET_DEST (PATTERN (insn));
1267  
1268       /* A value is totally replaced if it is the destination or the
1269          destination is a SUBREG of REGNO that does not change the number of
1270          words in it.  */
1271       if (GET_CODE (dest) == SUBREG
1272           && (((GET_MODE_SIZE (GET_MODE (dest))
1273                 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1274               == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1275                    + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1276         dest = SUBREG_REG (dest);
1277
1278       if (GET_CODE (dest) != REG)
1279         return 0;
1280
1281       regno = REGNO (dest);
1282       endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1283                   : regno + HARD_REGNO_NREGS (regno, GET_MODE (dest)));
1284
1285       return (test_regno >= regno && test_regno < endregno);
1286     }
1287   else if (GET_CODE (PATTERN (insn)) == PARALLEL)
1288     {
1289       register int i;
1290
1291       for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1292         {
1293           rtx body = XVECEXP (PATTERN (insn), 0, i);
1294
1295           if (GET_CODE (body) == SET || GET_CODE (body) == CLOBBER)
1296             {
1297               rtx dest = SET_DEST (body);
1298
1299               if (GET_CODE (dest) == SUBREG
1300                   && (((GET_MODE_SIZE (GET_MODE (dest))
1301                         + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1302                       == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1303                            + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1304                 dest = SUBREG_REG (dest);
1305
1306               if (GET_CODE (dest) != REG)
1307                 continue;
1308
1309               regno = REGNO (dest);
1310               endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1311                           : regno + HARD_REGNO_NREGS (regno, GET_MODE (dest)));
1312
1313               if (test_regno >= regno && test_regno < endregno)
1314                 return 1;
1315             }
1316         }
1317     }
1318
1319   return 0;
1320 }
1321
1322 /* Return the reg-note of kind KIND in insn INSN, if there is one.
1323    If DATUM is nonzero, look for one whose datum is DATUM.  */
1324
1325 rtx
1326 find_reg_note (insn, kind, datum)
1327      rtx insn;
1328      enum reg_note kind;
1329      rtx datum;
1330 {
1331   register rtx link;
1332
1333   /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN.  */
1334   if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
1335     return 0;
1336
1337   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1338     if (REG_NOTE_KIND (link) == kind
1339         && (datum == 0 || datum == XEXP (link, 0)))
1340       return link;
1341   return 0;
1342 }
1343
1344 /* Return the reg-note of kind KIND in insn INSN which applies to register
1345    number REGNO, if any.  Return 0 if there is no such reg-note.  Note that
1346    the REGNO of this NOTE need not be REGNO if REGNO is a hard register;
1347    it might be the case that the note overlaps REGNO.  */
1348
1349 rtx
1350 find_regno_note (insn, kind, regno)
1351      rtx insn;
1352      enum reg_note kind;
1353      int regno;
1354 {
1355   register rtx link;
1356
1357   /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN.  */
1358   if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
1359     return 0;
1360
1361   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1362     if (REG_NOTE_KIND (link) == kind
1363         /* Verify that it is a register, so that scratch and MEM won't cause a
1364            problem here.  */
1365         && GET_CODE (XEXP (link, 0)) == REG
1366         && REGNO (XEXP (link, 0)) <= regno
1367         && ((REGNO (XEXP (link, 0))
1368              + (REGNO (XEXP (link, 0)) >= FIRST_PSEUDO_REGISTER ? 1
1369                 : HARD_REGNO_NREGS (REGNO (XEXP (link, 0)),
1370                                     GET_MODE (XEXP (link, 0)))))
1371             > regno))
1372       return link;
1373   return 0;
1374 }
1375
1376 /* Return true if DATUM, or any overlap of DATUM, of kind CODE is found
1377    in the CALL_INSN_FUNCTION_USAGE information of INSN.  */
1378
1379 int
1380 find_reg_fusage (insn, code, datum)
1381      rtx insn;
1382      enum rtx_code code;
1383      rtx datum;
1384 {
1385   /* If it's not a CALL_INSN, it can't possibly have a
1386      CALL_INSN_FUNCTION_USAGE field, so don't bother checking.  */
1387   if (GET_CODE (insn) != CALL_INSN)
1388     return 0;
1389
1390   if (! datum)
1391     abort();
1392
1393   if (GET_CODE (datum) != REG)
1394     {
1395       register rtx link;
1396
1397       for (link = CALL_INSN_FUNCTION_USAGE (insn);
1398            link;
1399            link = XEXP (link, 1))
1400         if (GET_CODE (XEXP (link, 0)) == code
1401             && rtx_equal_p (datum, SET_DEST (XEXP (link, 0))))
1402           return 1;
1403     }
1404   else
1405     {
1406       register int regno = REGNO (datum);
1407
1408       /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1409          to pseudo registers, so don't bother checking.  */
1410
1411       if (regno < FIRST_PSEUDO_REGISTER)
1412         {
1413           int end_regno = regno + HARD_REGNO_NREGS (regno, GET_MODE (datum));
1414           int i;
1415
1416           for (i = regno; i < end_regno; i++)
1417             if (find_regno_fusage (insn, code, i))
1418               return 1;
1419         }
1420     }
1421
1422   return 0;
1423 }
1424
1425 /* Return true if REGNO, or any overlap of REGNO, of kind CODE is found
1426    in the CALL_INSN_FUNCTION_USAGE information of INSN.  */
1427
1428 int
1429 find_regno_fusage (insn, code, regno)
1430      rtx insn;
1431      enum rtx_code code;
1432      int regno;
1433 {
1434   register rtx link;
1435
1436   /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1437      to pseudo registers, so don't bother checking.  */
1438
1439   if (regno >= FIRST_PSEUDO_REGISTER
1440       || GET_CODE (insn) != CALL_INSN )
1441     return 0;
1442
1443   for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
1444    {
1445     register int regnote;
1446     register rtx op;
1447
1448     if (GET_CODE (op = XEXP (link, 0)) == code
1449         && GET_CODE (SET_DEST (op)) == REG
1450         && (regnote = REGNO (SET_DEST (op))) <= regno
1451         && regnote
1452                 + HARD_REGNO_NREGS (regnote, GET_MODE (SET_DEST (op)))
1453             > regno)
1454       return 1;
1455    }
1456
1457   return 0;
1458 }
1459 \f
1460 /* Remove register note NOTE from the REG_NOTES of INSN.  */
1461
1462 void
1463 remove_note (insn, note)
1464      register rtx note;
1465      register rtx insn;
1466 {
1467   register rtx link;
1468
1469   if (REG_NOTES (insn) == note)
1470     {
1471       REG_NOTES (insn) = XEXP (note, 1);
1472       return;
1473     }
1474
1475   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1476     if (XEXP (link, 1) == note)
1477       {
1478         XEXP (link, 1) = XEXP (note, 1);
1479         return;
1480       }
1481
1482   abort ();
1483 }
1484 \f
1485 /* Nonzero if X contains any volatile instructions.  These are instructions
1486    which may cause unpredictable machine state instructions, and thus no
1487    instructions should be moved or combined across them.  This includes
1488    only volatile asms and UNSPEC_VOLATILE instructions.  */
1489
1490 int
1491 volatile_insn_p (x)
1492      rtx x;
1493 {
1494   register RTX_CODE code;
1495
1496   code = GET_CODE (x);
1497   switch (code)
1498     {
1499     case LABEL_REF:
1500     case SYMBOL_REF:
1501     case CONST_INT:
1502     case CONST:
1503     case CONST_DOUBLE:
1504     case CC0:
1505     case PC:
1506     case REG:
1507     case SCRATCH:
1508     case CLOBBER:
1509     case ASM_INPUT:
1510     case ADDR_VEC:
1511     case ADDR_DIFF_VEC:
1512     case CALL:
1513     case MEM:
1514       return 0;
1515
1516     case UNSPEC_VOLATILE:
1517  /* case TRAP_IF: This isn't clear yet.  */
1518       return 1;
1519
1520     case ASM_OPERANDS:
1521       if (MEM_VOLATILE_P (x))
1522         return 1;
1523
1524     default:
1525       break;
1526     }
1527
1528   /* Recursively scan the operands of this expression.  */
1529
1530   {
1531     register char *fmt = GET_RTX_FORMAT (code);
1532     register int i;
1533     
1534     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1535       {
1536         if (fmt[i] == 'e')
1537           {
1538             if (volatile_insn_p (XEXP (x, i)))
1539               return 1;
1540           }
1541         if (fmt[i] == 'E')
1542           {
1543             register int j;
1544             for (j = 0; j < XVECLEN (x, i); j++)
1545               if (volatile_insn_p (XVECEXP (x, i, j)))
1546                 return 1;
1547           }
1548       }
1549   }
1550   return 0;
1551 }
1552
1553 /* Nonzero if X contains any volatile memory references
1554    UNSPEC_VOLATILE operations or volatile ASM_OPERANDS expressions.  */
1555
1556 int
1557 volatile_refs_p (x)
1558      rtx x;
1559 {
1560   register RTX_CODE code;
1561
1562   code = GET_CODE (x);
1563   switch (code)
1564     {
1565     case LABEL_REF:
1566     case SYMBOL_REF:
1567     case CONST_INT:
1568     case CONST:
1569     case CONST_DOUBLE:
1570     case CC0:
1571     case PC:
1572     case REG:
1573     case SCRATCH:
1574     case CLOBBER:
1575     case ASM_INPUT:
1576     case ADDR_VEC:
1577     case ADDR_DIFF_VEC:
1578       return 0;
1579
1580     case CALL:
1581     case UNSPEC_VOLATILE:
1582  /* case TRAP_IF: This isn't clear yet.  */
1583       return 1;
1584
1585     case MEM:
1586     case ASM_OPERANDS:
1587       if (MEM_VOLATILE_P (x))
1588         return 1;
1589
1590     default:
1591       break;
1592     }
1593
1594   /* Recursively scan the operands of this expression.  */
1595
1596   {
1597     register char *fmt = GET_RTX_FORMAT (code);
1598     register int i;
1599     
1600     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1601       {
1602         if (fmt[i] == 'e')
1603           {
1604             if (volatile_refs_p (XEXP (x, i)))
1605               return 1;
1606           }
1607         if (fmt[i] == 'E')
1608           {
1609             register int j;
1610             for (j = 0; j < XVECLEN (x, i); j++)
1611               if (volatile_refs_p (XVECEXP (x, i, j)))
1612                 return 1;
1613           }
1614       }
1615   }
1616   return 0;
1617 }
1618
1619 /* Similar to above, except that it also rejects register pre- and post-
1620    incrementing.  */
1621
1622 int
1623 side_effects_p (x)
1624      rtx x;
1625 {
1626   register RTX_CODE code;
1627
1628   code = GET_CODE (x);
1629   switch (code)
1630     {
1631     case LABEL_REF:
1632     case SYMBOL_REF:
1633     case CONST_INT:
1634     case CONST:
1635     case CONST_DOUBLE:
1636     case CC0:
1637     case PC:
1638     case REG:
1639     case SCRATCH:
1640     case ASM_INPUT:
1641     case ADDR_VEC:
1642     case ADDR_DIFF_VEC:
1643       return 0;
1644
1645     case CLOBBER:
1646       /* Reject CLOBBER with a non-VOID mode.  These are made by combine.c
1647          when some combination can't be done.  If we see one, don't think
1648          that we can simplify the expression.  */
1649       return (GET_MODE (x) != VOIDmode);
1650
1651     case PRE_INC:
1652     case PRE_DEC:
1653     case POST_INC:
1654     case POST_DEC:
1655     case CALL:
1656     case UNSPEC_VOLATILE:
1657  /* case TRAP_IF: This isn't clear yet.  */
1658       return 1;
1659
1660     case MEM:
1661     case ASM_OPERANDS:
1662       if (MEM_VOLATILE_P (x))
1663         return 1;
1664
1665     default:
1666       break;
1667     }
1668
1669   /* Recursively scan the operands of this expression.  */
1670
1671   {
1672     register char *fmt = GET_RTX_FORMAT (code);
1673     register int i;
1674     
1675     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1676       {
1677         if (fmt[i] == 'e')
1678           {
1679             if (side_effects_p (XEXP (x, i)))
1680               return 1;
1681           }
1682         if (fmt[i] == 'E')
1683           {
1684             register int j;
1685             for (j = 0; j < XVECLEN (x, i); j++)
1686               if (side_effects_p (XVECEXP (x, i, j)))
1687                 return 1;
1688           }
1689       }
1690   }
1691   return 0;
1692 }
1693 \f
1694 /* Return nonzero if evaluating rtx X might cause a trap.  */
1695
1696 int
1697 may_trap_p (x)
1698      rtx x;
1699 {
1700   int i;
1701   enum rtx_code code;
1702   char *fmt;
1703
1704   if (x == 0)
1705     return 0;
1706   code = GET_CODE (x);
1707   switch (code)
1708     {
1709       /* Handle these cases quickly.  */
1710     case CONST_INT:
1711     case CONST_DOUBLE:
1712     case SYMBOL_REF:
1713     case LABEL_REF:
1714     case CONST:
1715     case PC:
1716     case CC0:
1717     case REG:
1718     case SCRATCH:
1719       return 0;
1720
1721       /* Conditional trap can trap!  */
1722     case UNSPEC_VOLATILE:
1723     case TRAP_IF:
1724       return 1;
1725
1726       /* Memory ref can trap unless it's a static var or a stack slot.  */
1727     case MEM:
1728       return rtx_addr_can_trap_p (XEXP (x, 0));
1729
1730       /* Division by a non-constant might trap.  */
1731     case DIV:
1732     case MOD:
1733     case UDIV:
1734     case UMOD:
1735       if (! CONSTANT_P (XEXP (x, 1))
1736           || GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
1737         return 1;
1738       /* This was const0_rtx, but by not using that,
1739          we can link this file into other programs.  */
1740       if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 0)
1741         return 1;
1742       break;
1743
1744     case EXPR_LIST:
1745       /* An EXPR_LIST is used to represent a function call.  This
1746          certainly may trap.  */
1747       return 1;
1748
1749     default:
1750       /* Any floating arithmetic may trap.  */
1751       if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
1752         return 1;
1753     }
1754
1755   fmt = GET_RTX_FORMAT (code);
1756   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1757     {
1758       if (fmt[i] == 'e')
1759         {
1760           if (may_trap_p (XEXP (x, i)))
1761             return 1;
1762         }
1763       else if (fmt[i] == 'E')
1764         {
1765           register int j;
1766           for (j = 0; j < XVECLEN (x, i); j++)
1767             if (may_trap_p (XVECEXP (x, i, j)))
1768               return 1;
1769         }
1770     }
1771   return 0;
1772 }
1773 \f
1774 /* Return nonzero if X contains a comparison that is not either EQ or NE,
1775    i.e., an inequality.  */
1776
1777 int
1778 inequality_comparisons_p (x)
1779      rtx x;
1780 {
1781   register char *fmt;
1782   register int len, i;
1783   register enum rtx_code code = GET_CODE (x);
1784
1785   switch (code)
1786     {
1787     case REG:
1788     case SCRATCH:
1789     case PC:
1790     case CC0:
1791     case CONST_INT:
1792     case CONST_DOUBLE:
1793     case CONST:
1794     case LABEL_REF:
1795     case SYMBOL_REF:
1796       return 0;
1797
1798     case LT:
1799     case LTU:
1800     case GT:
1801     case GTU:
1802     case LE:
1803     case LEU:
1804     case GE:
1805     case GEU:
1806       return 1;
1807       
1808     default:
1809       break;
1810     }
1811
1812   len = GET_RTX_LENGTH (code);
1813   fmt = GET_RTX_FORMAT (code);
1814
1815   for (i = 0; i < len; i++)
1816     {
1817       if (fmt[i] == 'e')
1818         {
1819           if (inequality_comparisons_p (XEXP (x, i)))
1820             return 1;
1821         }
1822       else if (fmt[i] == 'E')
1823         {
1824           register int j;
1825           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1826             if (inequality_comparisons_p (XVECEXP (x, i, j)))
1827               return 1;
1828         }
1829     }
1830             
1831   return 0;
1832 }
1833 \f
1834 /* Replace any occurrence of FROM in X with TO.  The function does
1835    not enter into CONST_DOUBLE for the replace.
1836
1837    Note that copying is not done so X must not be shared unless all copies
1838    are to be modified.  */
1839
1840 rtx
1841 replace_rtx (x, from, to)
1842      rtx x, from, to;
1843 {
1844   register int i, j;
1845   register char *fmt;
1846
1847   /* The following prevents loops occurrence when we change MEM in
1848      CONST_DOUBLE onto the same CONST_DOUBLE. */
1849   if (x != 0 && GET_CODE (x) == CONST_DOUBLE)
1850     return x;
1851
1852   if (x == from)
1853     return to;
1854
1855   /* Allow this function to make replacements in EXPR_LISTs.  */
1856   if (x == 0)
1857     return 0;
1858
1859   fmt = GET_RTX_FORMAT (GET_CODE (x));
1860   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
1861     {
1862       if (fmt[i] == 'e')
1863         XEXP (x, i) = replace_rtx (XEXP (x, i), from, to);
1864       else if (fmt[i] == 'E')
1865         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1866           XVECEXP (x, i, j) = replace_rtx (XVECEXP (x, i, j), from, to);
1867     }
1868
1869   return x;
1870 }  
1871 \f
1872 /* Throughout the rtx X, replace many registers according to REG_MAP.
1873    Return the replacement for X (which may be X with altered contents).
1874    REG_MAP[R] is the replacement for register R, or 0 for don't replace.
1875    NREGS is the length of REG_MAP; regs >= NREGS are not mapped.  
1876
1877    We only support REG_MAP entries of REG or SUBREG.  Also, hard registers
1878    should not be mapped to pseudos or vice versa since validate_change
1879    is not called.
1880
1881    If REPLACE_DEST is 1, replacements are also done in destinations;
1882    otherwise, only sources are replaced.  */
1883
1884 rtx
1885 replace_regs (x, reg_map, nregs, replace_dest)
1886      rtx x;
1887      rtx *reg_map;
1888      int nregs;
1889      int replace_dest;
1890 {
1891   register enum rtx_code code;
1892   register int i;
1893   register char *fmt;
1894
1895   if (x == 0)
1896     return x;
1897
1898   code = GET_CODE (x);
1899   switch (code)
1900     {
1901     case SCRATCH:
1902     case PC:
1903     case CC0:
1904     case CONST_INT:
1905     case CONST_DOUBLE:
1906     case CONST:
1907     case SYMBOL_REF:
1908     case LABEL_REF:
1909       return x;
1910
1911     case REG:
1912       /* Verify that the register has an entry before trying to access it.  */
1913       if (REGNO (x) < nregs && reg_map[REGNO (x)] != 0)
1914         {
1915           /* SUBREGs can't be shared.  Always return a copy to ensure that if
1916              this replacement occurs more than once then each instance will
1917              get distinct rtx.  */
1918           if (GET_CODE (reg_map[REGNO (x)]) == SUBREG)
1919             return copy_rtx (reg_map[REGNO (x)]);
1920           return reg_map[REGNO (x)];
1921         }
1922       return x;
1923
1924     case SUBREG:
1925       /* Prevent making nested SUBREGs.  */
1926       if (GET_CODE (SUBREG_REG (x)) == REG && REGNO (SUBREG_REG (x)) < nregs
1927           && reg_map[REGNO (SUBREG_REG (x))] != 0
1928           && GET_CODE (reg_map[REGNO (SUBREG_REG (x))]) == SUBREG)
1929         {
1930           rtx map_val = reg_map[REGNO (SUBREG_REG (x))];
1931           rtx map_inner = SUBREG_REG (map_val);
1932
1933           if (GET_MODE (x) == GET_MODE (map_inner))
1934             return map_inner;
1935           else
1936             {
1937               /* We cannot call gen_rtx here since we may be linked with
1938                  genattrtab.c.  */
1939               /* Let's try clobbering the incoming SUBREG and see
1940                  if this is really safe.  */
1941               SUBREG_REG (x) = map_inner;
1942               SUBREG_WORD (x) += SUBREG_WORD (map_val);
1943               return x;
1944 #if 0
1945               rtx new = rtx_alloc (SUBREG);
1946               PUT_MODE (new, GET_MODE (x));
1947               SUBREG_REG (new) = map_inner;
1948               SUBREG_WORD (new) = SUBREG_WORD (x) + SUBREG_WORD (map_val);
1949 #endif
1950             }
1951         }
1952       break;
1953
1954     case SET:
1955       if (replace_dest)
1956         SET_DEST (x) = replace_regs (SET_DEST (x), reg_map, nregs, 0);
1957
1958       else if (GET_CODE (SET_DEST (x)) == MEM
1959                || GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
1960         /* Even if we are not to replace destinations, replace register if it
1961            is CONTAINED in destination (destination is memory or
1962            STRICT_LOW_PART).  */
1963         XEXP (SET_DEST (x), 0) = replace_regs (XEXP (SET_DEST (x), 0),
1964                                                reg_map, nregs, 0);
1965       else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
1966         /* Similarly, for ZERO_EXTRACT we replace all operands.  */
1967         break;
1968
1969       SET_SRC (x) = replace_regs (SET_SRC (x), reg_map, nregs, 0);
1970       return x;
1971       
1972     default:
1973       break;
1974     }
1975
1976   fmt = GET_RTX_FORMAT (code);
1977   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1978     {
1979       if (fmt[i] == 'e')
1980         XEXP (x, i) = replace_regs (XEXP (x, i), reg_map, nregs, replace_dest);
1981       if (fmt[i] == 'E')
1982         {
1983           register int j;
1984           for (j = 0; j < XVECLEN (x, i); j++)
1985             XVECEXP (x, i, j) = replace_regs (XVECEXP (x, i, j), reg_map,
1986                                               nregs, replace_dest);
1987         }
1988     }
1989   return x;
1990 }
1991
1992 /* Return 1 if X, the SRC_SRC of  SET of (pc) contain a REG or MEM that is
1993    not in the constant pool and not in the condition of an IF_THEN_ELSE.  */
1994
1995 static int
1996 jmp_uses_reg_or_mem (x)
1997      rtx x;
1998 {
1999   enum rtx_code code = GET_CODE (x);
2000   int i, j;
2001   char *fmt;
2002
2003   switch (code)
2004     {
2005     case CONST:
2006     case LABEL_REF:
2007     case PC:
2008       return 0;
2009
2010     case REG:
2011       return 1;
2012
2013     case MEM:
2014       return ! (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
2015                 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)));
2016
2017     case IF_THEN_ELSE:
2018       return (jmp_uses_reg_or_mem (XEXP (x, 1))
2019               || jmp_uses_reg_or_mem (XEXP (x, 2)));
2020
2021     case PLUS:  case MINUS:  case MULT:
2022       return (jmp_uses_reg_or_mem (XEXP (x, 0))
2023               || jmp_uses_reg_or_mem (XEXP (x, 1)));
2024
2025     default:
2026       break;
2027     }
2028
2029   fmt = GET_RTX_FORMAT (code);
2030   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2031     {
2032       if (fmt[i] == 'e'
2033           && jmp_uses_reg_or_mem (XEXP (x, i)))
2034         return 1;
2035
2036       if (fmt[i] == 'E')
2037         for (j = 0; j < XVECLEN (x, i); j++)
2038           if (jmp_uses_reg_or_mem (XVECEXP (x, i, j)))
2039             return 1;
2040     }
2041
2042   return 0;
2043 }
2044
2045 /* Return nonzero if INSN is an indirect jump (aka computed jump).
2046
2047    Tablejumps and casesi insns are not considered indirect jumps;
2048    we can recognize them by a (use (lael_ref)).  */
2049
2050 int
2051 computed_jump_p (insn)
2052      rtx insn;
2053 {
2054   int i;
2055   if (GET_CODE (insn) == JUMP_INSN)
2056     {
2057       rtx pat = PATTERN (insn);
2058
2059       if (GET_CODE (pat) == PARALLEL)
2060         {
2061           int len = XVECLEN (pat, 0);
2062           int has_use_labelref = 0;
2063
2064           for (i = len - 1; i >= 0; i--)
2065             if (GET_CODE (XVECEXP (pat, 0, i)) == USE
2066                 && (GET_CODE (XEXP (XVECEXP (pat, 0, i), 0))
2067                     == LABEL_REF))
2068               has_use_labelref = 1;
2069
2070           if (! has_use_labelref)
2071             for (i = len - 1; i >= 0; i--)
2072               if (GET_CODE (XVECEXP (pat, 0, i)) == SET
2073                   && SET_DEST (XVECEXP (pat, 0, i)) == pc_rtx
2074                   && jmp_uses_reg_or_mem (SET_SRC (XVECEXP (pat, 0, i))))
2075                 return 1;
2076         }
2077       else if (GET_CODE (pat) == SET
2078                && SET_DEST (pat) == pc_rtx
2079                && jmp_uses_reg_or_mem (SET_SRC (pat)))
2080         return 1;
2081     }
2082   return 0;
2083 }
2084
2085 /* Traverse X via depth-first search, calling F for each
2086    sub-expression (including X itself).  F is also passed the DATA.
2087    If F returns -1, do not traverse sub-expressions, but continue
2088    traversing the rest of the tree.  If F ever returns any other
2089    non-zero value, stop the traversal, and return the value returned
2090    by F.  Otherwise, return 0.  This function does not traverse inside
2091    tree structure that contains RTX_EXPRs, or into sub-expressions
2092    whose format code is `0' since it is not known whether or not those
2093    codes are actually RTL.
2094
2095    This routine is very general, and could (should?) be used to
2096    implement many of the other routines in this file.  */
2097
2098 int
2099 for_each_rtx (x, f, data)
2100      rtx* x;
2101      rtx_function f;
2102      void* data;
2103 {
2104   int result;
2105   int length;
2106   char* format;
2107   int i;
2108
2109   /* Call F on X.  */
2110   result = (*f)(x, data);
2111   if (result == -1)
2112     /* Do not traverse sub-expressions.  */
2113     return 0;
2114   else if (result != 0)
2115     /* Stop the traversal.  */
2116     return result;
2117
2118   if (*x == NULL_RTX)
2119     /* There are no sub-expressions.  */
2120     return 0;
2121
2122   length = GET_RTX_LENGTH (GET_CODE (*x));
2123   format = GET_RTX_FORMAT (GET_CODE (*x));
2124
2125   for (i = 0; i < length; ++i) 
2126     {
2127       switch (format[i]) 
2128         {
2129         case 'e':
2130           result = for_each_rtx (&XEXP (*x, i), f, data);
2131           if (result != 0)
2132             return result;
2133           break;
2134
2135         case 'V':
2136         case 'E':
2137           if (XVEC (*x, i) != 0) 
2138             {
2139               int j;
2140               for (j = 0; j < XVECLEN (*x, i); ++j)
2141                 {
2142                   result = for_each_rtx (&XVECEXP (*x, i, j), f, data);
2143                   if (result != 0)
2144                     return result;
2145                 }
2146             }
2147           break; 
2148
2149         default:
2150           /* Nothing to do.  */
2151           break;
2152         }
2153
2154     }
2155
2156   return 0;
2157 }