OSDN Git Service

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