OSDN Git Service

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