OSDN Git Service

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