OSDN Git Service

* varasm.c (asm_output_bss): Add prototype.
[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, pat;
459 {
460   /* We don't want to return 1 if X is a MEM that contains a register
461      within REG_SET_REG.  */
462
463   if ((GET_CODE (x) != MEM)
464       && reg_overlap_mentioned_p (reg_set_reg, x))
465     reg_set_flag = 1;
466 }
467
468 int
469 reg_set_p (reg, insn)
470      rtx reg, insn;
471 {
472   rtx body = insn;
473
474   /* We can be passed an insn or part of one.  If we are passed an insn,
475      check if a side-effect of the insn clobbers REG.  */
476   if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
477     {
478       if (FIND_REG_INC_NOTE (insn, reg)
479           || (GET_CODE (insn) == CALL_INSN
480               /* We'd like to test call_used_regs here, but rtlanal.c can't
481                  reference that variable due to its use in genattrtab.  So
482                  we'll just be more conservative.
483
484                  ??? Unless we could ensure that the CALL_INSN_FUNCTION_USAGE
485                  information holds all clobbered registers.  */
486               && ((GET_CODE (reg) == REG
487                    && REGNO (reg) < FIRST_PSEUDO_REGISTER)
488                   || GET_CODE (reg) == MEM
489                   || find_reg_fusage (insn, CLOBBER, reg))))
490         return 1;
491
492       body = PATTERN (insn);
493     }
494
495   reg_set_reg = reg;
496   reg_set_flag = 0;
497   note_stores (body, reg_set_p_1);
498   return reg_set_flag;
499 }
500
501 /* Similar to reg_set_between_p, but check all registers in X.  Return 0
502    only if none of them are modified between START and END.  Return 1 if
503    X contains a MEM; this routine does not perform any memory aliasing.  */
504
505 int
506 modified_between_p (x, start, end)
507      rtx x;
508      rtx start, end;
509 {
510   enum rtx_code code = GET_CODE (x);
511   char *fmt;
512   int i, j;
513
514   switch (code)
515     {
516     case CONST_INT:
517     case CONST_DOUBLE:
518     case CONST:
519     case SYMBOL_REF:
520     case LABEL_REF:
521       return 0;
522
523     case PC:
524     case CC0:
525       return 1;
526
527     case MEM:
528       /* If the memory is not constant, assume it is modified.  If it is
529          constant, we still have to check the address.  */
530       if (! RTX_UNCHANGING_P (x))
531         return 1;
532       break;
533
534     case REG:
535       return reg_set_between_p (x, start, end);
536       
537     default:
538       break;
539     }
540
541   fmt = GET_RTX_FORMAT (code);
542   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
543     {
544       if (fmt[i] == 'e' && modified_between_p (XEXP (x, i), start, end))
545         return 1;
546
547       if (fmt[i] == 'E')
548         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
549           if (modified_between_p (XVECEXP (x, i, j), start, end))
550             return 1;
551     }
552
553   return 0;
554 }
555
556 /* Similar to reg_set_p, but check all registers in X.  Return 0 only if none
557    of them are modified in INSN.  Return 1 if X contains a MEM; this routine
558    does not perform any memory aliasing.  */
559
560 int
561 modified_in_p (x, insn)
562      rtx x;
563      rtx insn;
564 {
565   enum rtx_code code = GET_CODE (x);
566   char *fmt;
567   int i, j;
568
569   switch (code)
570     {
571     case CONST_INT:
572     case CONST_DOUBLE:
573     case CONST:
574     case SYMBOL_REF:
575     case LABEL_REF:
576       return 0;
577
578     case PC:
579     case CC0:
580       return 1;
581
582     case MEM:
583       /* If the memory is not constant, assume it is modified.  If it is
584          constant, we still have to check the address.  */
585       if (! RTX_UNCHANGING_P (x))
586         return 1;
587       break;
588
589     case REG:
590       return reg_set_p (x, insn);
591
592     default:
593       break;
594     }
595
596   fmt = GET_RTX_FORMAT (code);
597   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
598     {
599       if (fmt[i] == 'e' && modified_in_p (XEXP (x, i), insn))
600         return 1;
601
602       if (fmt[i] == 'E')
603         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
604           if (modified_in_p (XVECEXP (x, i, j), insn))
605             return 1;
606     }
607
608   return 0;
609 }
610 \f
611 /* Given an INSN, return a SET expression if this insn has only a single SET.
612    It may also have CLOBBERs, USEs, or SET whose output
613    will not be used, which we ignore.  */
614
615 rtx
616 single_set (insn)
617      rtx insn;
618 {
619   rtx set;
620   int i;
621   
622   if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
623     return 0;
624
625   if (GET_CODE (PATTERN (insn)) == SET)
626     return PATTERN (insn);
627   
628   else if (GET_CODE (PATTERN (insn)) == PARALLEL)
629     {
630       for (i = 0, set = 0; i < XVECLEN (PATTERN (insn), 0); i++)
631         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET
632             && (! find_reg_note (insn, REG_UNUSED,
633                                  SET_DEST (XVECEXP (PATTERN (insn), 0, i)))
634                 || side_effects_p (XVECEXP (PATTERN (insn), 0, i))))
635           {
636             if (set)
637               return 0;
638             else
639               set = XVECEXP (PATTERN (insn), 0, i);
640           }
641       return set;
642     }
643   
644   return 0;
645 }
646 \f
647 /* Return the last thing that X was assigned from before *PINSN.  Verify that
648    the object is not modified up to VALID_TO.  If it was, if we hit
649    a partial assignment to X, or hit a CODE_LABEL first, return X.  If we
650    found an assignment, update *PINSN to point to it.  */
651
652 rtx
653 find_last_value (x, pinsn, valid_to)
654      rtx x;
655      rtx *pinsn;
656      rtx valid_to;
657 {
658   rtx p;
659
660   for (p = PREV_INSN (*pinsn); p && GET_CODE (p) != CODE_LABEL;
661        p = PREV_INSN (p))
662     if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
663       {
664         rtx set = single_set (p);
665         rtx note = find_reg_note (p, REG_EQUAL, NULL_RTX);
666
667         if (set && rtx_equal_p (x, SET_DEST (set)))
668           {
669             rtx src = SET_SRC (set);
670
671             if (note && GET_CODE (XEXP (note, 0)) != EXPR_LIST)
672               src = XEXP (note, 0);
673
674             if (! modified_between_p (src, PREV_INSN (p), valid_to)
675                 /* Reject hard registers because we don't usually want
676                    to use them; we'd rather use a pseudo.  */
677                 && ! (GET_CODE (src) == REG
678                       && REGNO (src) < FIRST_PSEUDO_REGISTER))
679               {
680                 *pinsn = p;
681                 return src;
682               }
683           }
684           
685         /* If set in non-simple way, we don't have a value.  */
686         if (reg_set_p (x, p))
687           break;
688       }
689
690   return x;
691 }     
692 \f
693 /* Return nonzero if register in range [REGNO, ENDREGNO)
694    appears either explicitly or implicitly in X
695    other than being stored into.
696
697    References contained within the substructure at LOC do not count.
698    LOC may be zero, meaning don't ignore anything.  */
699
700 int
701 refers_to_regno_p (regno, endregno, x, loc)
702      int regno, endregno;
703      rtx x;
704      rtx *loc;
705 {
706   register int i;
707   register RTX_CODE code;
708   register char *fmt;
709
710  repeat:
711   /* The contents of a REG_NONNEG note is always zero, so we must come here
712      upon repeat in case the last REG_NOTE is a REG_NONNEG note.  */
713   if (x == 0)
714     return 0;
715
716   code = GET_CODE (x);
717
718   switch (code)
719     {
720     case REG:
721       i = REGNO (x);
722
723       /* If we modifying the stack, frame, or argument pointer, it will
724          clobber a virtual register.  In fact, we could be more precise,
725          but it isn't worth it.  */
726       if ((i == STACK_POINTER_REGNUM
727 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
728            || i == ARG_POINTER_REGNUM
729 #endif
730            || i == FRAME_POINTER_REGNUM)
731           && regno >= FIRST_VIRTUAL_REGISTER && regno <= LAST_VIRTUAL_REGISTER)
732         return 1;
733
734       return (endregno > i
735               && regno < i + (i < FIRST_PSEUDO_REGISTER 
736                               ? HARD_REGNO_NREGS (i, GET_MODE (x))
737                               : 1));
738
739     case SUBREG:
740       /* If this is a SUBREG of a hard reg, we can see exactly which
741          registers are being modified.  Otherwise, handle normally.  */
742       if (GET_CODE (SUBREG_REG (x)) == REG
743           && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER)
744         {
745           int inner_regno = REGNO (SUBREG_REG (x)) + SUBREG_WORD (x);
746           int inner_endregno
747             = inner_regno + (inner_regno < FIRST_PSEUDO_REGISTER
748                              ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
749
750           return endregno > inner_regno && regno < inner_endregno;
751         }
752       break;
753
754     case CLOBBER:
755     case SET:
756       if (&SET_DEST (x) != loc
757           /* Note setting a SUBREG counts as referring to the REG it is in for
758              a pseudo but not for hard registers since we can
759              treat each word individually.  */
760           && ((GET_CODE (SET_DEST (x)) == SUBREG
761                && loc != &SUBREG_REG (SET_DEST (x))
762                && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG
763                && REGNO (SUBREG_REG (SET_DEST (x))) >= FIRST_PSEUDO_REGISTER
764                && refers_to_regno_p (regno, endregno,
765                                      SUBREG_REG (SET_DEST (x)), loc))
766               || (GET_CODE (SET_DEST (x)) != REG
767                   && refers_to_regno_p (regno, endregno, SET_DEST (x), loc))))
768         return 1;
769
770       if (code == CLOBBER || loc == &SET_SRC (x))
771         return 0;
772       x = SET_SRC (x);
773       goto repeat;
774
775     default:
776       break;
777     }
778
779   /* X does not match, so try its subexpressions.  */
780
781   fmt = GET_RTX_FORMAT (code);
782   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
783     {
784       if (fmt[i] == 'e' && loc != &XEXP (x, i))
785         {
786           if (i == 0)
787             {
788               x = XEXP (x, 0);
789               goto repeat;
790             }
791           else
792             if (refers_to_regno_p (regno, endregno, XEXP (x, i), loc))
793               return 1;
794         }
795       else if (fmt[i] == 'E')
796         {
797           register int j;
798           for (j = XVECLEN (x, i) - 1; j >=0; j--)
799             if (loc != &XVECEXP (x, i, j)
800                 && refers_to_regno_p (regno, endregno, XVECEXP (x, i, j), loc))
801               return 1;
802         }
803     }
804   return 0;
805 }
806
807 /* Nonzero if modifying X will affect IN.  If X is a register or a SUBREG,
808    we check if any register number in X conflicts with the relevant register
809    numbers.  If X is a constant, return 0.  If X is a MEM, return 1 iff IN
810    contains a MEM (we don't bother checking for memory addresses that can't
811    conflict because we expect this to be a rare case.  */
812
813 int
814 reg_overlap_mentioned_p (x, in)
815      rtx x, in;
816 {
817   int regno, endregno;
818
819   if (GET_CODE (x) == SUBREG)
820     {
821       regno = REGNO (SUBREG_REG (x));
822       if (regno < FIRST_PSEUDO_REGISTER)
823         regno += SUBREG_WORD (x);
824     }
825   else if (GET_CODE (x) == REG)
826     regno = REGNO (x);
827   else if (CONSTANT_P (x))
828     return 0;
829   else if (GET_CODE (x) == MEM)
830     {
831       char *fmt;
832       int i;
833
834       if (GET_CODE (in) == MEM)
835         return 1;
836
837       fmt = GET_RTX_FORMAT (GET_CODE (in));
838
839       for (i = GET_RTX_LENGTH (GET_CODE (in)) - 1; i >= 0; i--)
840         if (fmt[i] == 'e' && reg_overlap_mentioned_p (x, XEXP (in, i)))
841           return 1;
842
843       return 0;
844     }
845   else if (GET_CODE (x) == SCRATCH || GET_CODE (x) == PC
846            || GET_CODE (x) == CC0)
847     return reg_mentioned_p (x, in);
848   else
849     abort ();
850
851   endregno = regno + (regno < FIRST_PSEUDO_REGISTER
852                       ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
853
854   return refers_to_regno_p (regno, endregno, in, NULL_PTR);
855 }
856 \f
857 /* Used for communications between the next few functions.  */
858
859 static int reg_set_last_unknown;
860 static rtx reg_set_last_value;
861 static int reg_set_last_first_regno, reg_set_last_last_regno;
862
863 /* Called via note_stores from reg_set_last.  */
864
865 static void
866 reg_set_last_1 (x, pat)
867      rtx x;
868      rtx pat;
869 {
870   int first, last;
871
872   /* If X is not a register, or is not one in the range we care
873      about, ignore.  */
874   if (GET_CODE (x) != REG)
875     return;
876
877   first = REGNO (x);
878   last = first + (first < FIRST_PSEUDO_REGISTER
879                   ? HARD_REGNO_NREGS (first, GET_MODE (x)) : 1);
880
881   if (first >= reg_set_last_last_regno
882       || last <= reg_set_last_first_regno)
883     return;
884
885   /* If this is a CLOBBER or is some complex LHS, or doesn't modify
886      exactly the registers we care about, show we don't know the value.  */
887   if (GET_CODE (pat) == CLOBBER || SET_DEST (pat) != x
888       || first != reg_set_last_first_regno
889       || last != reg_set_last_last_regno)
890     reg_set_last_unknown = 1;
891   else
892     reg_set_last_value = SET_SRC (pat);
893 }
894
895 /* Return the last value to which REG was set prior to INSN.  If we can't
896    find it easily, return 0.
897
898    We only return a REG, SUBREG, or constant because it is too hard to
899    check if a MEM remains unchanged.  */
900
901 rtx
902 reg_set_last (x, insn)
903      rtx x;
904      rtx insn;
905 {
906   rtx orig_insn = insn;
907
908   reg_set_last_first_regno = REGNO (x);
909
910   reg_set_last_last_regno
911     = reg_set_last_first_regno
912       + (reg_set_last_first_regno < FIRST_PSEUDO_REGISTER
913          ? HARD_REGNO_NREGS (reg_set_last_first_regno, GET_MODE (x)) : 1);
914
915   reg_set_last_unknown = 0;
916   reg_set_last_value = 0;
917
918   /* Scan backwards until reg_set_last_1 changed one of the above flags.
919      Stop when we reach a label or X is a hard reg and we reach a
920      CALL_INSN (if reg_set_last_last_regno is a hard reg).
921
922      If we find a set of X, ensure that its SET_SRC remains unchanged.  */
923
924   /* We compare with <= here, because reg_set_last_last_regno
925      is actually the number of the first reg *not* in X.  */
926   for (;
927        insn && GET_CODE (insn) != CODE_LABEL
928        && ! (GET_CODE (insn) == CALL_INSN
929              && reg_set_last_last_regno <= FIRST_PSEUDO_REGISTER);
930        insn = PREV_INSN (insn))
931     if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
932       {
933         note_stores (PATTERN (insn), reg_set_last_1);
934         if (reg_set_last_unknown)
935           return 0;
936         else if (reg_set_last_value)
937           {
938             if (CONSTANT_P (reg_set_last_value)
939                 || ((GET_CODE (reg_set_last_value) == REG
940                      || GET_CODE (reg_set_last_value) == SUBREG)
941                     && ! reg_set_between_p (reg_set_last_value,
942                                             insn, orig_insn)))
943               return reg_set_last_value;
944             else
945               return 0;
946           }
947       }
948
949   return 0;
950 }
951 \f
952 /* This is 1 until after the rtl generation pass.  */
953 int rtx_equal_function_value_matters;
954
955 /* Return 1 if X and Y are identical-looking rtx's.
956    This is the Lisp function EQUAL for rtx arguments.  */
957
958 int
959 rtx_equal_p (x, y)
960      rtx x, y;
961 {
962   register int i;
963   register int j;
964   register enum rtx_code code;
965   register char *fmt;
966
967   if (x == y)
968     return 1;
969   if (x == 0 || y == 0)
970     return 0;
971
972   code = GET_CODE (x);
973   /* Rtx's of different codes cannot be equal.  */
974   if (code != GET_CODE (y))
975     return 0;
976
977   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
978      (REG:SI x) and (REG:HI x) are NOT equivalent.  */
979
980   if (GET_MODE (x) != GET_MODE (y))
981     return 0;
982
983   /* REG, LABEL_REF, and SYMBOL_REF can be compared nonrecursively.  */
984
985   if (code == REG)
986     /* Until rtl generation is complete, don't consider a reference to the
987        return register of the current function the same as the return from a
988        called function.  This eases the job of function integration.  Once the
989        distinction is no longer needed, they can be considered equivalent.  */
990     return (REGNO (x) == REGNO (y)
991             && (! rtx_equal_function_value_matters
992                 || REG_FUNCTION_VALUE_P (x) == REG_FUNCTION_VALUE_P (y)));
993   else if (code == LABEL_REF)
994     return XEXP (x, 0) == XEXP (y, 0);
995   else if (code == SYMBOL_REF)
996     return XSTR (x, 0) == XSTR (y, 0);
997   else if (code == SCRATCH || code == CONST_DOUBLE)
998     return 0;
999
1000   /* Compare the elements.  If any pair of corresponding elements
1001      fail to match, return 0 for the whole things.  */
1002
1003   fmt = GET_RTX_FORMAT (code);
1004   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1005     {
1006       switch (fmt[i])
1007         {
1008         case 'w':
1009           if (XWINT (x, i) != XWINT (y, i))
1010             return 0;
1011           break;
1012
1013         case 'n':
1014         case 'i':
1015           if (XINT (x, i) != XINT (y, i))
1016             return 0;
1017           break;
1018
1019         case 'V':
1020         case 'E':
1021           /* Two vectors must have the same length.  */
1022           if (XVECLEN (x, i) != XVECLEN (y, i))
1023             return 0;
1024
1025           /* And the corresponding elements must match.  */
1026           for (j = 0; j < XVECLEN (x, i); j++)
1027             if (rtx_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)) == 0)
1028               return 0;
1029           break;
1030
1031         case 'e':
1032           if (rtx_equal_p (XEXP (x, i), XEXP (y, i)) == 0)
1033             return 0;
1034           break;
1035
1036         case 'S':
1037         case 's':
1038           if (strcmp (XSTR (x, i), XSTR (y, i)))
1039             return 0;
1040           break;
1041
1042         case 'u':
1043           /* These are just backpointers, so they don't matter.  */
1044           break;
1045
1046         case '0':
1047           break;
1048
1049           /* It is believed that rtx's at this level will never
1050              contain anything but integers and other rtx's,
1051              except for within LABEL_REFs and SYMBOL_REFs.  */
1052         default:
1053           abort ();
1054         }
1055     }
1056   return 1;
1057 }
1058 \f
1059 /* Call FUN on each register or MEM that is stored into or clobbered by X.
1060    (X would be the pattern of an insn).
1061    FUN receives two arguments:
1062      the REG, MEM, CC0 or PC being stored in or clobbered,
1063      the SET or CLOBBER rtx that does the store.
1064
1065   If the item being stored in or clobbered is a SUBREG of a hard register,
1066   the SUBREG will be passed.  */
1067      
1068 void
1069 note_stores (x, fun)
1070      register rtx x;
1071      void (*fun) ();
1072 {
1073   if ((GET_CODE (x) == SET || GET_CODE (x) == CLOBBER))
1074     {
1075       register rtx dest = SET_DEST (x);
1076       while ((GET_CODE (dest) == SUBREG
1077               && (GET_CODE (SUBREG_REG (dest)) != REG
1078                   || REGNO (SUBREG_REG (dest)) >= FIRST_PSEUDO_REGISTER))
1079              || GET_CODE (dest) == ZERO_EXTRACT
1080              || GET_CODE (dest) == SIGN_EXTRACT
1081              || GET_CODE (dest) == STRICT_LOW_PART)
1082         dest = XEXP (dest, 0);
1083       (*fun) (dest, x);
1084     }
1085   else if (GET_CODE (x) == PARALLEL)
1086     {
1087       register int i;
1088       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1089         {
1090           register rtx y = XVECEXP (x, 0, i);
1091           if (GET_CODE (y) == SET || GET_CODE (y) == CLOBBER)
1092             {
1093               register rtx dest = SET_DEST (y);
1094               while ((GET_CODE (dest) == SUBREG
1095                       && (GET_CODE (SUBREG_REG (dest)) != REG
1096                           || (REGNO (SUBREG_REG (dest))
1097                               >= FIRST_PSEUDO_REGISTER)))
1098                      || GET_CODE (dest) == ZERO_EXTRACT
1099                      || GET_CODE (dest) == SIGN_EXTRACT
1100                      || GET_CODE (dest) == STRICT_LOW_PART)
1101                 dest = XEXP (dest, 0);
1102               (*fun) (dest, y);
1103             }
1104         }
1105     }
1106 }
1107 \f
1108 /* Return nonzero if X's old contents don't survive after INSN.
1109    This will be true if X is (cc0) or if X is a register and
1110    X dies in INSN or because INSN entirely sets X.
1111
1112    "Entirely set" means set directly and not through a SUBREG,
1113    ZERO_EXTRACT or SIGN_EXTRACT, so no trace of the old contents remains.
1114    Likewise, REG_INC does not count.
1115
1116    REG may be a hard or pseudo reg.  Renumbering is not taken into account,
1117    but for this use that makes no difference, since regs don't overlap
1118    during their lifetimes.  Therefore, this function may be used
1119    at any time after deaths have been computed (in flow.c).
1120
1121    If REG is a hard reg that occupies multiple machine registers, this
1122    function will only return 1 if each of those registers will be replaced
1123    by INSN.  */
1124
1125 int
1126 dead_or_set_p (insn, x)
1127      rtx insn;
1128      rtx x;
1129 {
1130   register int regno, last_regno;
1131   register int i;
1132
1133   /* Can't use cc0_rtx below since this file is used by genattrtab.c.  */
1134   if (GET_CODE (x) == CC0)
1135     return 1;
1136
1137   if (GET_CODE (x) != REG)
1138     abort ();
1139
1140   regno = REGNO (x);
1141   last_regno = (regno >= FIRST_PSEUDO_REGISTER ? regno
1142                 : regno + HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1);
1143
1144   for (i = regno; i <= last_regno; i++)
1145     if (! dead_or_set_regno_p (insn, i))
1146       return 0;
1147
1148   return 1;
1149 }
1150
1151 /* Utility function for dead_or_set_p to check an individual register.  Also
1152    called from flow.c.  */
1153
1154 int
1155 dead_or_set_regno_p (insn, test_regno)
1156      rtx insn;
1157      int test_regno;
1158 {
1159   int regno, endregno;
1160   rtx link;
1161
1162   /* REG_READ notes are not normally maintained after reload, so we
1163      ignore them if the are invalid.  */
1164   if (! reload_completed
1165 #ifdef PRESERVE_DEATH_INFO_REGNO_P
1166       || PRESERVE_DEATH_INFO_REGNO_P (test_regno)
1167 #endif
1168       )
1169     {
1170       /* See if there is a death note for something that includes
1171          TEST_REGNO.  */
1172       for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1173         {
1174           if (REG_NOTE_KIND (link) != REG_DEAD
1175               || GET_CODE (XEXP (link, 0)) != REG)
1176             continue;
1177
1178           regno = REGNO (XEXP (link, 0));
1179           endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1180                       : regno + HARD_REGNO_NREGS (regno,
1181                                                   GET_MODE (XEXP (link, 0))));
1182
1183           if (test_regno >= regno && test_regno < endregno)
1184             return 1;
1185         }
1186     }
1187
1188   if (GET_CODE (insn) == CALL_INSN
1189       && find_regno_fusage (insn, CLOBBER, test_regno))
1190     return 1;
1191
1192   if (GET_CODE (PATTERN (insn)) == SET)
1193     {
1194       rtx dest = SET_DEST (PATTERN (insn));
1195  
1196       /* A value is totally replaced if it is the destination or the
1197          destination is a SUBREG of REGNO that does not change the number of
1198          words in it.  */
1199      if (GET_CODE (dest) == SUBREG
1200           && (((GET_MODE_SIZE (GET_MODE (dest))
1201                 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1202               == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1203                    + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1204         dest = SUBREG_REG (dest);
1205
1206       if (GET_CODE (dest) != REG)
1207         return 0;
1208
1209       regno = REGNO (dest);
1210       endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1211                   : regno + HARD_REGNO_NREGS (regno, GET_MODE (dest)));
1212
1213       return (test_regno >= regno && test_regno < endregno);
1214     }
1215   else if (GET_CODE (PATTERN (insn)) == PARALLEL)
1216     {
1217       register int i;
1218
1219       for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1220         {
1221           rtx body = XVECEXP (PATTERN (insn), 0, i);
1222
1223           if (GET_CODE (body) == SET || GET_CODE (body) == CLOBBER)
1224             {
1225               rtx dest = SET_DEST (body);
1226
1227               if (GET_CODE (dest) == SUBREG
1228                   && (((GET_MODE_SIZE (GET_MODE (dest))
1229                         + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1230                       == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1231                            + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1232                 dest = SUBREG_REG (dest);
1233
1234               if (GET_CODE (dest) != REG)
1235                 continue;
1236
1237               regno = REGNO (dest);
1238               endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1239                           : regno + HARD_REGNO_NREGS (regno, GET_MODE (dest)));
1240
1241               if (test_regno >= regno && test_regno < endregno)
1242                 return 1;
1243             }
1244         }
1245     }
1246
1247   return 0;
1248 }
1249
1250 /* Return the reg-note of kind KIND in insn INSN, if there is one.
1251    If DATUM is nonzero, look for one whose datum is DATUM.  */
1252
1253 rtx
1254 find_reg_note (insn, kind, datum)
1255      rtx insn;
1256      enum reg_note kind;
1257      rtx datum;
1258 {
1259   register rtx link;
1260
1261   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1262     if (REG_NOTE_KIND (link) == kind
1263         && (datum == 0 || datum == XEXP (link, 0)))
1264       return link;
1265   return 0;
1266 }
1267
1268 /* Return the reg-note of kind KIND in insn INSN which applies to register
1269    number REGNO, if any.  Return 0 if there is no such reg-note.  Note that
1270    the REGNO of this NOTE need not be REGNO if REGNO is a hard register;
1271    it might be the case that the note overlaps REGNO.  */
1272
1273 rtx
1274 find_regno_note (insn, kind, regno)
1275      rtx insn;
1276      enum reg_note kind;
1277      int regno;
1278 {
1279   register rtx link;
1280
1281   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1282     if (REG_NOTE_KIND (link) == kind
1283         /* Verify that it is a register, so that scratch and MEM won't cause a
1284            problem here.  */
1285         && GET_CODE (XEXP (link, 0)) == REG
1286         && REGNO (XEXP (link, 0)) <= regno
1287         && ((REGNO (XEXP (link, 0))
1288              + (REGNO (XEXP (link, 0)) >= FIRST_PSEUDO_REGISTER ? 1
1289                 : HARD_REGNO_NREGS (REGNO (XEXP (link, 0)),
1290                                     GET_MODE (XEXP (link, 0)))))
1291             > regno))
1292       return link;
1293   return 0;
1294 }
1295
1296 /* Return true if DATUM, or any overlap of DATUM, of kind CODE is found
1297    in the CALL_INSN_FUNCTION_USAGE information of INSN.  */
1298
1299 int
1300 find_reg_fusage (insn, code, datum)
1301      rtx insn;
1302      enum rtx_code code;
1303      rtx datum;
1304 {
1305   /* If it's not a CALL_INSN, it can't possibly have a
1306      CALL_INSN_FUNCTION_USAGE field, so don't bother checking.  */
1307   if (GET_CODE (insn) != CALL_INSN)
1308     return 0;
1309
1310   if (! datum)
1311     abort();
1312
1313   if (GET_CODE (datum) != REG)
1314     {
1315       register rtx link;
1316
1317       for (link = CALL_INSN_FUNCTION_USAGE (insn);
1318            link;
1319            link = XEXP (link, 1))
1320         if (GET_CODE (XEXP (link, 0)) == code
1321             && rtx_equal_p (datum, SET_DEST (XEXP (link, 0))))
1322           return 1;
1323     }
1324   else
1325     {
1326       register int regno = REGNO (datum);
1327
1328       /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1329          to pseudo registers, so don't bother checking.  */
1330
1331       if (regno < FIRST_PSEUDO_REGISTER)
1332         {
1333           int end_regno = regno + HARD_REGNO_NREGS (regno, GET_MODE (datum));
1334           int i;
1335
1336           for (i = regno; i < end_regno; i++)
1337             if (find_regno_fusage (insn, code, i))
1338               return 1;
1339         }
1340     }
1341
1342   return 0;
1343 }
1344
1345 /* Return true if REGNO, or any overlap of REGNO, of kind CODE is found
1346    in the CALL_INSN_FUNCTION_USAGE information of INSN.  */
1347
1348 int
1349 find_regno_fusage (insn, code, regno)
1350      rtx insn;
1351      enum rtx_code code;
1352      int regno;
1353 {
1354   register rtx link;
1355
1356   /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1357      to pseudo registers, so don't bother checking.  */
1358
1359   if (regno >= FIRST_PSEUDO_REGISTER
1360       || GET_CODE (insn) != CALL_INSN )
1361     return 0;
1362
1363   for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
1364    {
1365     register int regnote;
1366     register rtx op;
1367
1368     if (GET_CODE (op = XEXP (link, 0)) == code
1369         && GET_CODE (SET_DEST (op)) == REG
1370         && (regnote = REGNO (SET_DEST (op))) <= regno
1371         && regnote
1372                 + HARD_REGNO_NREGS (regnote, GET_MODE (SET_DEST (op)))
1373             > regno)
1374       return 1;
1375    }
1376
1377   return 0;
1378 }
1379 \f
1380 /* Remove register note NOTE from the REG_NOTES of INSN.  */
1381
1382 void
1383 remove_note (insn, note)
1384      register rtx note;
1385      register rtx insn;
1386 {
1387   register rtx link;
1388
1389   if (REG_NOTES (insn) == note)
1390     {
1391       REG_NOTES (insn) = XEXP (note, 1);
1392       return;
1393     }
1394
1395   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1396     if (XEXP (link, 1) == note)
1397       {
1398         XEXP (link, 1) = XEXP (note, 1);
1399         return;
1400       }
1401
1402   abort ();
1403 }
1404 \f
1405 /* Nonzero if X contains any volatile instructions.  These are instructions
1406    which may cause unpredictable machine state instructions, and thus no
1407    instructions should be moved or combined across them.  This includes
1408    only volatile asms and UNSPEC_VOLATILE instructions.  */
1409
1410 int
1411 volatile_insn_p (x)
1412      rtx x;
1413 {
1414   register RTX_CODE code;
1415
1416   code = GET_CODE (x);
1417   switch (code)
1418     {
1419     case LABEL_REF:
1420     case SYMBOL_REF:
1421     case CONST_INT:
1422     case CONST:
1423     case CONST_DOUBLE:
1424     case CC0:
1425     case PC:
1426     case REG:
1427     case SCRATCH:
1428     case CLOBBER:
1429     case ASM_INPUT:
1430     case ADDR_VEC:
1431     case ADDR_DIFF_VEC:
1432     case CALL:
1433     case MEM:
1434       return 0;
1435
1436     case UNSPEC_VOLATILE:
1437  /* case TRAP_IF: This isn't clear yet.  */
1438       return 1;
1439
1440     case ASM_OPERANDS:
1441       if (MEM_VOLATILE_P (x))
1442         return 1;
1443
1444     default:
1445       break;
1446     }
1447
1448   /* Recursively scan the operands of this expression.  */
1449
1450   {
1451     register char *fmt = GET_RTX_FORMAT (code);
1452     register int i;
1453     
1454     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1455       {
1456         if (fmt[i] == 'e')
1457           {
1458             if (volatile_insn_p (XEXP (x, i)))
1459               return 1;
1460           }
1461         if (fmt[i] == 'E')
1462           {
1463             register int j;
1464             for (j = 0; j < XVECLEN (x, i); j++)
1465               if (volatile_insn_p (XVECEXP (x, i, j)))
1466                 return 1;
1467           }
1468       }
1469   }
1470   return 0;
1471 }
1472
1473 /* Nonzero if X contains any volatile memory references
1474    UNSPEC_VOLATILE operations or volatile ASM_OPERANDS expressions.  */
1475
1476 int
1477 volatile_refs_p (x)
1478      rtx x;
1479 {
1480   register RTX_CODE code;
1481
1482   code = GET_CODE (x);
1483   switch (code)
1484     {
1485     case LABEL_REF:
1486     case SYMBOL_REF:
1487     case CONST_INT:
1488     case CONST:
1489     case CONST_DOUBLE:
1490     case CC0:
1491     case PC:
1492     case REG:
1493     case SCRATCH:
1494     case CLOBBER:
1495     case ASM_INPUT:
1496     case ADDR_VEC:
1497     case ADDR_DIFF_VEC:
1498       return 0;
1499
1500     case CALL:
1501     case UNSPEC_VOLATILE:
1502  /* case TRAP_IF: This isn't clear yet.  */
1503       return 1;
1504
1505     case MEM:
1506     case ASM_OPERANDS:
1507       if (MEM_VOLATILE_P (x))
1508         return 1;
1509
1510     default:
1511       break;
1512     }
1513
1514   /* Recursively scan the operands of this expression.  */
1515
1516   {
1517     register char *fmt = GET_RTX_FORMAT (code);
1518     register int i;
1519     
1520     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1521       {
1522         if (fmt[i] == 'e')
1523           {
1524             if (volatile_refs_p (XEXP (x, i)))
1525               return 1;
1526           }
1527         if (fmt[i] == 'E')
1528           {
1529             register int j;
1530             for (j = 0; j < XVECLEN (x, i); j++)
1531               if (volatile_refs_p (XVECEXP (x, i, j)))
1532                 return 1;
1533           }
1534       }
1535   }
1536   return 0;
1537 }
1538
1539 /* Similar to above, except that it also rejects register pre- and post-
1540    incrementing.  */
1541
1542 int
1543 side_effects_p (x)
1544      rtx x;
1545 {
1546   register RTX_CODE code;
1547
1548   code = GET_CODE (x);
1549   switch (code)
1550     {
1551     case LABEL_REF:
1552     case SYMBOL_REF:
1553     case CONST_INT:
1554     case CONST:
1555     case CONST_DOUBLE:
1556     case CC0:
1557     case PC:
1558     case REG:
1559     case SCRATCH:
1560     case ASM_INPUT:
1561     case ADDR_VEC:
1562     case ADDR_DIFF_VEC:
1563       return 0;
1564
1565     case CLOBBER:
1566       /* Reject CLOBBER with a non-VOID mode.  These are made by combine.c
1567          when some combination can't be done.  If we see one, don't think
1568          that we can simplify the expression.  */
1569       return (GET_MODE (x) != VOIDmode);
1570
1571     case PRE_INC:
1572     case PRE_DEC:
1573     case POST_INC:
1574     case POST_DEC:
1575     case CALL:
1576     case UNSPEC_VOLATILE:
1577  /* case TRAP_IF: This isn't clear yet.  */
1578       return 1;
1579
1580     case MEM:
1581     case ASM_OPERANDS:
1582       if (MEM_VOLATILE_P (x))
1583         return 1;
1584
1585     default:
1586       break;
1587     }
1588
1589   /* Recursively scan the operands of this expression.  */
1590
1591   {
1592     register char *fmt = GET_RTX_FORMAT (code);
1593     register int i;
1594     
1595     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1596       {
1597         if (fmt[i] == 'e')
1598           {
1599             if (side_effects_p (XEXP (x, i)))
1600               return 1;
1601           }
1602         if (fmt[i] == 'E')
1603           {
1604             register int j;
1605             for (j = 0; j < XVECLEN (x, i); j++)
1606               if (side_effects_p (XVECEXP (x, i, j)))
1607                 return 1;
1608           }
1609       }
1610   }
1611   return 0;
1612 }
1613 \f
1614 /* Return nonzero if evaluating rtx X might cause a trap.  */
1615
1616 int
1617 may_trap_p (x)
1618      rtx x;
1619 {
1620   int i;
1621   enum rtx_code code;
1622   char *fmt;
1623
1624   if (x == 0)
1625     return 0;
1626   code = GET_CODE (x);
1627   switch (code)
1628     {
1629       /* Handle these cases quickly.  */
1630     case CONST_INT:
1631     case CONST_DOUBLE:
1632     case SYMBOL_REF:
1633     case LABEL_REF:
1634     case CONST:
1635     case PC:
1636     case CC0:
1637     case REG:
1638     case SCRATCH:
1639       return 0;
1640
1641       /* Conditional trap can trap!  */
1642     case UNSPEC_VOLATILE:
1643     case TRAP_IF:
1644       return 1;
1645
1646       /* Memory ref can trap unless it's a static var or a stack slot.  */
1647     case MEM:
1648       return rtx_addr_can_trap_p (XEXP (x, 0));
1649
1650       /* Division by a non-constant might trap.  */
1651     case DIV:
1652     case MOD:
1653     case UDIV:
1654     case UMOD:
1655       if (! CONSTANT_P (XEXP (x, 1))
1656           || GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
1657         return 1;
1658       /* This was const0_rtx, but by not using that,
1659          we can link this file into other programs.  */
1660       if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 0)
1661         return 1;
1662       break;
1663
1664     case EXPR_LIST:
1665       /* An EXPR_LIST is used to represent a function call.  This
1666          certainly may trap.  */
1667       return 1;
1668
1669     default:
1670       /* Any floating arithmetic may trap.  */
1671       if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
1672         return 1;
1673     }
1674
1675   fmt = GET_RTX_FORMAT (code);
1676   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1677     {
1678       if (fmt[i] == 'e')
1679         {
1680           if (may_trap_p (XEXP (x, i)))
1681             return 1;
1682         }
1683       else if (fmt[i] == 'E')
1684         {
1685           register int j;
1686           for (j = 0; j < XVECLEN (x, i); j++)
1687             if (may_trap_p (XVECEXP (x, i, j)))
1688               return 1;
1689         }
1690     }
1691   return 0;
1692 }
1693 \f
1694 /* Return nonzero if X contains a comparison that is not either EQ or NE,
1695    i.e., an inequality.  */
1696
1697 int
1698 inequality_comparisons_p (x)
1699      rtx x;
1700 {
1701   register char *fmt;
1702   register int len, i;
1703   register enum rtx_code code = GET_CODE (x);
1704
1705   switch (code)
1706     {
1707     case REG:
1708     case SCRATCH:
1709     case PC:
1710     case CC0:
1711     case CONST_INT:
1712     case CONST_DOUBLE:
1713     case CONST:
1714     case LABEL_REF:
1715     case SYMBOL_REF:
1716       return 0;
1717
1718     case LT:
1719     case LTU:
1720     case GT:
1721     case GTU:
1722     case LE:
1723     case LEU:
1724     case GE:
1725     case GEU:
1726       return 1;
1727       
1728     default:
1729       break;
1730     }
1731
1732   len = GET_RTX_LENGTH (code);
1733   fmt = GET_RTX_FORMAT (code);
1734
1735   for (i = 0; i < len; i++)
1736     {
1737       if (fmt[i] == 'e')
1738         {
1739           if (inequality_comparisons_p (XEXP (x, i)))
1740             return 1;
1741         }
1742       else if (fmt[i] == 'E')
1743         {
1744           register int j;
1745           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1746             if (inequality_comparisons_p (XVECEXP (x, i, j)))
1747               return 1;
1748         }
1749     }
1750             
1751   return 0;
1752 }
1753 \f
1754 /* Replace any occurrence of FROM in X with TO.
1755
1756    Note that copying is not done so X must not be shared unless all copies
1757    are to be modified.  */
1758
1759 rtx
1760 replace_rtx (x, from, to)
1761      rtx x, from, to;
1762 {
1763   register int i, j;
1764   register char *fmt;
1765
1766   if (x == from)
1767     return to;
1768
1769   /* Allow this function to make replacements in EXPR_LISTs.  */
1770   if (x == 0)
1771     return 0;
1772
1773   fmt = GET_RTX_FORMAT (GET_CODE (x));
1774   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
1775     {
1776       if (fmt[i] == 'e')
1777         XEXP (x, i) = replace_rtx (XEXP (x, i), from, to);
1778       else if (fmt[i] == 'E')
1779         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1780           XVECEXP (x, i, j) = replace_rtx (XVECEXP (x, i, j), from, to);
1781     }
1782
1783   return x;
1784 }  
1785 \f
1786 /* Throughout the rtx X, replace many registers according to REG_MAP.
1787    Return the replacement for X (which may be X with altered contents).
1788    REG_MAP[R] is the replacement for register R, or 0 for don't replace.
1789    NREGS is the length of REG_MAP; regs >= NREGS are not mapped.  
1790
1791    We only support REG_MAP entries of REG or SUBREG.  Also, hard registers
1792    should not be mapped to pseudos or vice versa since validate_change
1793    is not called.
1794
1795    If REPLACE_DEST is 1, replacements are also done in destinations;
1796    otherwise, only sources are replaced.  */
1797
1798 rtx
1799 replace_regs (x, reg_map, nregs, replace_dest)
1800      rtx x;
1801      rtx *reg_map;
1802      int nregs;
1803      int replace_dest;
1804 {
1805   register enum rtx_code code;
1806   register int i;
1807   register char *fmt;
1808
1809   if (x == 0)
1810     return x;
1811
1812   code = GET_CODE (x);
1813   switch (code)
1814     {
1815     case SCRATCH:
1816     case PC:
1817     case CC0:
1818     case CONST_INT:
1819     case CONST_DOUBLE:
1820     case CONST:
1821     case SYMBOL_REF:
1822     case LABEL_REF:
1823       return x;
1824
1825     case REG:
1826       /* Verify that the register has an entry before trying to access it.  */
1827       if (REGNO (x) < nregs && reg_map[REGNO (x)] != 0)
1828         {
1829           /* SUBREGs can't be shared.  Always return a copy to ensure that if
1830              this replacement occurs more than once then each instance will
1831              get distinct rtx.  */
1832           if (GET_CODE (reg_map[REGNO (x)]) == SUBREG)
1833             return copy_rtx (reg_map[REGNO (x)]);
1834           return reg_map[REGNO (x)];
1835         }
1836       return x;
1837
1838     case SUBREG:
1839       /* Prevent making nested SUBREGs.  */
1840       if (GET_CODE (SUBREG_REG (x)) == REG && REGNO (SUBREG_REG (x)) < nregs
1841           && reg_map[REGNO (SUBREG_REG (x))] != 0
1842           && GET_CODE (reg_map[REGNO (SUBREG_REG (x))]) == SUBREG)
1843         {
1844           rtx map_val = reg_map[REGNO (SUBREG_REG (x))];
1845           rtx map_inner = SUBREG_REG (map_val);
1846
1847           if (GET_MODE (x) == GET_MODE (map_inner))
1848             return map_inner;
1849           else
1850             {
1851               /* We cannot call gen_rtx here since we may be linked with
1852                  genattrtab.c.  */
1853               /* Let's try clobbering the incoming SUBREG and see
1854                  if this is really safe.  */
1855               SUBREG_REG (x) = map_inner;
1856               SUBREG_WORD (x) += SUBREG_WORD (map_val);
1857               return x;
1858 #if 0
1859               rtx new = rtx_alloc (SUBREG);
1860               PUT_MODE (new, GET_MODE (x));
1861               SUBREG_REG (new) = map_inner;
1862               SUBREG_WORD (new) = SUBREG_WORD (x) + SUBREG_WORD (map_val);
1863 #endif
1864             }
1865         }
1866       break;
1867
1868     case SET:
1869       if (replace_dest)
1870         SET_DEST (x) = replace_regs (SET_DEST (x), reg_map, nregs, 0);
1871
1872       else if (GET_CODE (SET_DEST (x)) == MEM
1873                || GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
1874         /* Even if we are not to replace destinations, replace register if it
1875            is CONTAINED in destination (destination is memory or
1876            STRICT_LOW_PART).  */
1877         XEXP (SET_DEST (x), 0) = replace_regs (XEXP (SET_DEST (x), 0),
1878                                                reg_map, nregs, 0);
1879       else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
1880         /* Similarly, for ZERO_EXTRACT we replace all operands.  */
1881         break;
1882
1883       SET_SRC (x) = replace_regs (SET_SRC (x), reg_map, nregs, 0);
1884       return x;
1885       
1886     default:
1887       break;
1888     }
1889
1890   fmt = GET_RTX_FORMAT (code);
1891   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1892     {
1893       if (fmt[i] == 'e')
1894         XEXP (x, i) = replace_regs (XEXP (x, i), reg_map, nregs, replace_dest);
1895       if (fmt[i] == 'E')
1896         {
1897           register int j;
1898           for (j = 0; j < XVECLEN (x, i); j++)
1899             XVECEXP (x, i, j) = replace_regs (XVECEXP (x, i, j), reg_map,
1900                                               nregs, replace_dest);
1901         }
1902     }
1903   return x;
1904 }
1905
1906 /* Return 1 if X, the SRC_SRC of  SET of (pc) contain a REG or MEM that is
1907    not in the constant pool and not in the condition of an IF_THEN_ELSE.  */
1908
1909 static int
1910 jmp_uses_reg_or_mem (x)
1911      rtx x;
1912 {
1913   enum rtx_code code = GET_CODE (x);
1914   int i, j;
1915   char *fmt;
1916
1917   switch (code)
1918     {
1919     case CONST:
1920     case LABEL_REF:
1921     case PC:
1922       return 0;
1923
1924     case REG:
1925       return 1;
1926
1927     case MEM:
1928       return ! (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1929                 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)));
1930
1931     case IF_THEN_ELSE:
1932       return (jmp_uses_reg_or_mem (XEXP (x, 1))
1933               || jmp_uses_reg_or_mem (XEXP (x, 2)));
1934
1935     case PLUS:  case MINUS:  case MULT:
1936       return (jmp_uses_reg_or_mem (XEXP (x, 0))
1937               || jmp_uses_reg_or_mem (XEXP (x, 1)));
1938
1939     default:
1940       break;
1941     }
1942
1943   fmt = GET_RTX_FORMAT (code);
1944   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1945     {
1946       if (fmt[i] == 'e'
1947           && jmp_uses_reg_or_mem (XEXP (x, i)))
1948         return 1;
1949
1950       if (fmt[i] == 'E')
1951         for (j = 0; j < XVECLEN (x, i); j++)
1952           if (jmp_uses_reg_or_mem (XVECEXP (x, i, j)))
1953             return 1;
1954     }
1955
1956   return 0;
1957 }
1958
1959 /* Return nonzero if INSN is an indirect jump (aka computed jump).
1960
1961    Tablejumps and casesi insns are not considered indirect jumps;
1962    we can recognize them by a (use (lael_ref)).  */
1963
1964 int
1965 computed_jump_p (insn)
1966      rtx insn;
1967 {
1968   int i;
1969   if (GET_CODE (insn) == JUMP_INSN)
1970     {
1971       rtx pat = PATTERN (insn);
1972
1973       if (GET_CODE (pat) == PARALLEL)
1974         {
1975           int len = XVECLEN (pat, 0);
1976           int has_use_labelref = 0;
1977
1978           for (i = len - 1; i >= 0; i--)
1979             if (GET_CODE (XVECEXP (pat, 0, i)) == USE
1980                 && (GET_CODE (XEXP (XVECEXP (pat, 0, i), 0))
1981                     == LABEL_REF))
1982               has_use_labelref = 1;
1983
1984           if (! has_use_labelref)
1985             for (i = len - 1; i >= 0; i--)
1986               if (GET_CODE (XVECEXP (pat, 0, i)) == SET
1987                   && SET_DEST (XVECEXP (pat, 0, i)) == pc_rtx
1988                   && jmp_uses_reg_or_mem (SET_SRC (XVECEXP (pat, 0, i))))
1989                 return 1;
1990         }
1991       else if (GET_CODE (pat) == SET
1992                && SET_DEST (pat) == pc_rtx
1993                && jmp_uses_reg_or_mem (SET_SRC (pat)))
1994         return 1;
1995     }
1996   return 0;
1997 }