OSDN Git Service

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