rtlanal.c revision 72562
1290001Sglebius/* Analyze RTL for C-Compiler
2290001Sglebius   Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3290001Sglebius   2000 Free Software Foundation, Inc.
4290001Sglebius
5290001SglebiusThis file is part of GNU CC.
6290001Sglebius
7290001SglebiusGNU CC is free software; you can redistribute it and/or modify
8290001Sglebiusit under the terms of the GNU General Public License as published by
9290001Sglebiusthe Free Software Foundation; either version 2, or (at your option)
10290001Sglebiusany later version.
11290001Sglebius
12290001SglebiusGNU CC is distributed in the hope that it will be useful,
13290001Sglebiusbut WITHOUT ANY WARRANTY; without even the implied warranty of
14290001SglebiusMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15290001SglebiusGNU General Public License for more details.
16290001Sglebius
17290001SglebiusYou should have received a copy of the GNU General Public License
18290001Sglebiusalong with GNU CC; see the file COPYING.  If not, write to
19290001Sglebiusthe Free Software Foundation, 59 Temple Place - Suite 330,
20290001SglebiusBoston, MA 02111-1307, USA.  */
21290001Sglebius
22290001Sglebius
23290001Sglebius#include "config.h"
24290001Sglebius#include "system.h"
25290001Sglebius#include "rtl.h"
26290001Sglebius
27290001Sglebiusstatic int rtx_addr_can_trap_p	PROTO((rtx));
28290001Sglebiusstatic void reg_set_p_1		PROTO((rtx, rtx));
29290001Sglebiusstatic void reg_set_last_1	PROTO((rtx, rtx));
30290001Sglebius
31290001Sglebius
32290001Sglebius/* Forward declarations */
33290001Sglebiusstatic int jmp_uses_reg_or_mem		PROTO((rtx));
34290001Sglebius
35290001Sglebius/* Bit flags that specify the machine subtype we are compiling for.
36290001Sglebius   Bits are tested using macros TARGET_... defined in the tm.h file
37290001Sglebius   and set by `-m...' switches.  Must be defined in rtlanal.c.  */
38290001Sglebius
39290001Sglebiusint target_flags;
40290001Sglebius
41290001Sglebius/* Return 1 if the value of X is unstable
42290001Sglebius   (would be different at a different point in the program).
43290001Sglebius   The frame pointer, arg pointer, etc. are considered stable
44290001Sglebius   (within one function) and so is anything marked `unchanging'.  */
45290001Sglebius
46290001Sglebiusint
47290001Sglebiusrtx_unstable_p (x)
48290001Sglebius     rtx x;
49290001Sglebius{
50290001Sglebius  register RTX_CODE code = GET_CODE (x);
51290001Sglebius  register int i;
52290001Sglebius  register char *fmt;
53290001Sglebius
54290001Sglebius  if (code == MEM)
55290001Sglebius    return ! RTX_UNCHANGING_P (x);
56290001Sglebius
57290001Sglebius  if (code == QUEUED)
58290001Sglebius    return 1;
59290001Sglebius
60290001Sglebius  if (code == CONST || code == CONST_INT)
61290001Sglebius    return 0;
62290001Sglebius
63290001Sglebius  if (code == REG)
64290001Sglebius    return ! (REGNO (x) == FRAME_POINTER_REGNUM
65290001Sglebius	      || REGNO (x) == HARD_FRAME_POINTER_REGNUM
66290001Sglebius	      || REGNO (x) == ARG_POINTER_REGNUM
67290001Sglebius	      || RTX_UNCHANGING_P (x));
68290001Sglebius
69290001Sglebius  fmt = GET_RTX_FORMAT (code);
70290001Sglebius  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
71290001Sglebius    if (fmt[i] == 'e')
72290001Sglebius      if (rtx_unstable_p (XEXP (x, i)))
73290001Sglebius	return 1;
74290001Sglebius  return 0;
75290001Sglebius}
76290001Sglebius
77290001Sglebius/* Return 1 if X has a value that can vary even between two
78290001Sglebius   executions of the program.  0 means X can be compared reliably
79290001Sglebius   against certain constants or near-constants.
80290001Sglebius   The frame pointer and the arg pointer are considered constant.  */
81290001Sglebius
82290001Sglebiusint
83290001Sglebiusrtx_varies_p (x)
84290001Sglebius     rtx x;
85290001Sglebius{
86290001Sglebius  register RTX_CODE code = GET_CODE (x);
87290001Sglebius  register int i;
88290001Sglebius  register char *fmt;
89290001Sglebius
90290001Sglebius  switch (code)
91290001Sglebius    {
92290001Sglebius    case MEM:
93290001Sglebius    case QUEUED:
94290001Sglebius      return 1;
95290001Sglebius
96290001Sglebius    case CONST:
97290001Sglebius    case CONST_INT:
98290001Sglebius    case CONST_DOUBLE:
99290001Sglebius    case SYMBOL_REF:
100290001Sglebius    case LABEL_REF:
101290001Sglebius      return 0;
102290001Sglebius
103290001Sglebius    case REG:
104290001Sglebius      /* Note that we have to test for the actual rtx used for the frame
105290001Sglebius	 and arg pointers and not just the register number in case we have
106290001Sglebius	 eliminated the frame and/or arg pointer and are using it
107290001Sglebius	 for pseudos.  */
108290001Sglebius      return ! (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
109290001Sglebius		|| x == arg_pointer_rtx || x == pic_offset_table_rtx);
110290001Sglebius
111290001Sglebius    case LO_SUM:
112290001Sglebius      /* The operand 0 of a LO_SUM is considered constant
113290001Sglebius	 (in fact is it related specifically to operand 1).  */
114290001Sglebius      return rtx_varies_p (XEXP (x, 1));
115290001Sglebius
116301301Sdelphij    default:
117301301Sdelphij      break;
118301301Sdelphij    }
119290001Sglebius
120290001Sglebius  fmt = GET_RTX_FORMAT (code);
121290001Sglebius  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
122290001Sglebius    if (fmt[i] == 'e')
123290001Sglebius      if (rtx_varies_p (XEXP (x, i)))
124290001Sglebius	return 1;
125290001Sglebius  return 0;
126290001Sglebius}
127290001Sglebius
128290001Sglebius/* Return 0 if the use of X as an address in a MEM can cause a trap.  */
129290001Sglebius
130290001Sglebiusstatic int
131290001Sglebiusrtx_addr_can_trap_p (x)
132290001Sglebius     register rtx x;
133290001Sglebius{
134290001Sglebius  register enum rtx_code code = GET_CODE (x);
135290001Sglebius
136290001Sglebius  switch (code)
137290001Sglebius    {
138290001Sglebius    case SYMBOL_REF:
139290001Sglebius    case LABEL_REF:
140290001Sglebius      /* SYMBOL_REF is problematic due to the possible presence of
141290001Sglebius	 a #pragma weak, but to say that loads from symbols can trap is
142290001Sglebius	 *very* costly.  It's not at all clear what's best here.  For
143290001Sglebius	 now, we ignore the impact of #pragma weak.  */
144290001Sglebius      return 0;
145290001Sglebius
146290001Sglebius    case REG:
147290001Sglebius      /* As in rtx_varies_p, we have to use the actual rtx, not reg number.  */
148290001Sglebius      return ! (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
149290001Sglebius		|| x == stack_pointer_rtx || x == arg_pointer_rtx);
150290001Sglebius
151290001Sglebius    case CONST:
152290001Sglebius      return rtx_addr_can_trap_p (XEXP (x, 0));
153290001Sglebius
154290001Sglebius    case PLUS:
155290001Sglebius      /* An address is assumed not to trap if it is an address that can't
156290001Sglebius	 trap plus a constant integer.  */
157290001Sglebius      return (rtx_addr_can_trap_p (XEXP (x, 0))
158290001Sglebius	      || GET_CODE (XEXP (x, 1)) != CONST_INT);
159290001Sglebius
160290001Sglebius    case LO_SUM:
161290001Sglebius      return rtx_addr_can_trap_p (XEXP (x, 1));
162290001Sglebius
163290001Sglebius    default:
164290001Sglebius      break;
165290001Sglebius    }
166290001Sglebius
167290001Sglebius  /* If it isn't one of the case above, it can cause a trap.  */
168290001Sglebius  return 1;
169290001Sglebius}
170290001Sglebius
171290001Sglebius/* Return 1 if X refers to a memory location whose address
172290001Sglebius   cannot be compared reliably with constant addresses,
173290001Sglebius   or if X refers to a BLKmode memory object.  */
174290001Sglebius
175290001Sglebiusint
176290001Sglebiusrtx_addr_varies_p (x)
177290001Sglebius     rtx x;
178290001Sglebius{
179290001Sglebius  register enum rtx_code code;
180290001Sglebius  register int i;
181290001Sglebius  register char *fmt;
182290001Sglebius
183290001Sglebius  if (x == 0)
184290001Sglebius    return 0;
185290001Sglebius
186290001Sglebius  code = GET_CODE (x);
187290001Sglebius  if (code == MEM)
188290001Sglebius    return GET_MODE (x) == BLKmode || rtx_varies_p (XEXP (x, 0));
189290001Sglebius
190290001Sglebius  fmt = GET_RTX_FORMAT (code);
191290001Sglebius  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
192290001Sglebius    if (fmt[i] == 'e')
193290001Sglebius      {
194290001Sglebius	if (rtx_addr_varies_p (XEXP (x, i)))
195290001Sglebius	  return 1;
196290001Sglebius      }
197290001Sglebius    else if (fmt[i] == 'E')
198290001Sglebius      {
199290001Sglebius	int j;
200290001Sglebius	for (j = 0; j < XVECLEN (x, i); j++)
201290001Sglebius	  if (rtx_addr_varies_p (XVECEXP (x, i, j)))
202290001Sglebius	    return 1;
203290001Sglebius      }
204290001Sglebius  return 0;
205290001Sglebius}
206290001Sglebius
207290001Sglebius/* Return the value of the integer term in X, if one is apparent;
208290001Sglebius   otherwise return 0.
209290001Sglebius   Only obvious integer terms are detected.
210290001Sglebius   This is used in cse.c with the `related_value' field.*/
211290001Sglebius
212290001SglebiusHOST_WIDE_INT
213290001Sglebiusget_integer_term (x)
214290001Sglebius     rtx x;
215290001Sglebius{
216290001Sglebius  if (GET_CODE (x) == CONST)
217290001Sglebius    x = XEXP (x, 0);
218290001Sglebius
219290001Sglebius  if (GET_CODE (x) == MINUS
220290001Sglebius      && GET_CODE (XEXP (x, 1)) == CONST_INT)
221290001Sglebius    return - INTVAL (XEXP (x, 1));
222290001Sglebius  if (GET_CODE (x) == PLUS
223290001Sglebius      && GET_CODE (XEXP (x, 1)) == CONST_INT)
224290001Sglebius    return INTVAL (XEXP (x, 1));
225290001Sglebius  return 0;
226290001Sglebius}
227290001Sglebius
228290001Sglebius/* If X is a constant, return the value sans apparent integer term;
229290001Sglebius   otherwise return 0.
230290001Sglebius   Only obvious integer terms are detected.  */
231290001Sglebius
232290001Sglebiusrtx
233290001Sglebiusget_related_value (x)
234290001Sglebius     rtx x;
235290001Sglebius{
236290001Sglebius  if (GET_CODE (x) != CONST)
237290001Sglebius    return 0;
238290001Sglebius  x = XEXP (x, 0);
239290001Sglebius  if (GET_CODE (x) == PLUS
240290001Sglebius      && GET_CODE (XEXP (x, 1)) == CONST_INT)
241290001Sglebius    return XEXP (x, 0);
242290001Sglebius  else if (GET_CODE (x) == MINUS
243290001Sglebius	   && GET_CODE (XEXP (x, 1)) == CONST_INT)
244290001Sglebius    return XEXP (x, 0);
245290001Sglebius  return 0;
246290001Sglebius}
247290001Sglebius
248290001Sglebius/* Nonzero if register REG appears somewhere within IN.
249290001Sglebius   Also works if REG is not a register; in this case it checks
250290001Sglebius   for a subexpression of IN that is Lisp "equal" to REG.  */
251290001Sglebius
252290001Sglebiusint
253290001Sglebiusreg_mentioned_p (reg, in)
254290001Sglebius     register rtx reg, in;
255290001Sglebius{
256290001Sglebius  register char *fmt;
257290001Sglebius  register int i;
258290001Sglebius  register enum rtx_code code;
259290001Sglebius
260290001Sglebius  if (in == 0)
261290001Sglebius    return 0;
262290001Sglebius
263290001Sglebius  if (reg == in)
264290001Sglebius    return 1;
265290001Sglebius
266290001Sglebius  if (GET_CODE (in) == LABEL_REF)
267290001Sglebius    return reg == XEXP (in, 0);
268290001Sglebius
269290001Sglebius  code = GET_CODE (in);
270290001Sglebius
271290001Sglebius  switch (code)
272290001Sglebius    {
273290001Sglebius      /* Compare registers by number.  */
274290001Sglebius    case REG:
275290001Sglebius      return GET_CODE (reg) == REG && REGNO (in) == REGNO (reg);
276290001Sglebius
277290001Sglebius      /* These codes have no constituent expressions
278290001Sglebius	 and are unique.  */
279290001Sglebius    case SCRATCH:
280290001Sglebius    case CC0:
281290001Sglebius    case PC:
282290001Sglebius      return 0;
283290001Sglebius
284290001Sglebius    case CONST_INT:
285290001Sglebius      return GET_CODE (reg) == CONST_INT && INTVAL (in) == INTVAL (reg);
286290001Sglebius
287290001Sglebius    case CONST_DOUBLE:
288290001Sglebius      /* These are kept unique for a given value.  */
289290001Sglebius      return 0;
290290001Sglebius
291290001Sglebius    default:
292290001Sglebius      break;
293290001Sglebius    }
294290001Sglebius
295290001Sglebius  if (GET_CODE (reg) == code && rtx_equal_p (reg, in))
296290001Sglebius    return 1;
297290001Sglebius
298290001Sglebius  fmt = GET_RTX_FORMAT (code);
299290001Sglebius
300290001Sglebius  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
301290001Sglebius    {
302290001Sglebius      if (fmt[i] == 'E')
303290001Sglebius	{
304290001Sglebius	  register int j;
305290001Sglebius	  for (j = XVECLEN (in, i) - 1; j >= 0; j--)
306290001Sglebius	    if (reg_mentioned_p (reg, XVECEXP (in, i, j)))
307290001Sglebius	      return 1;
308290001Sglebius	}
309290001Sglebius      else if (fmt[i] == 'e'
310290001Sglebius	       && reg_mentioned_p (reg, XEXP (in, i)))
311290001Sglebius	return 1;
312290001Sglebius    }
313290001Sglebius  return 0;
314290001Sglebius}
315290001Sglebius
316290001Sglebius/* Return 1 if in between BEG and END, exclusive of BEG and END, there is
317290001Sglebius   no CODE_LABEL insn.  */
318290001Sglebius
319290001Sglebiusint
320290001Sglebiusno_labels_between_p (beg, end)
321290001Sglebius     rtx beg, end;
322290001Sglebius{
323290001Sglebius  register rtx p;
324290001Sglebius  for (p = NEXT_INSN (beg); p != end; p = NEXT_INSN (p))
325290001Sglebius    if (GET_CODE (p) == CODE_LABEL)
326290001Sglebius      return 0;
327290001Sglebius  return 1;
328290001Sglebius}
329290001Sglebius
330290001Sglebius/* Return 1 if in between BEG and END, exclusive of BEG and END, there is
331290001Sglebius   no JUMP_INSN insn.  */
332290001Sglebius
333290001Sglebiusint
334290001Sglebiusno_jumps_between_p (beg, end)
335290001Sglebius     rtx beg, end;
336290001Sglebius{
337290001Sglebius  register rtx p;
338290001Sglebius  for (p = NEXT_INSN (beg); p != end; p = NEXT_INSN (p))
339290001Sglebius    if (GET_CODE (p) == JUMP_INSN)
340290001Sglebius      return 0;
341290001Sglebius  return 1;
342290001Sglebius}
343290001Sglebius
344290001Sglebius/* Nonzero if register REG is used in an insn between
345290001Sglebius   FROM_INSN and TO_INSN (exclusive of those two).  */
346290001Sglebius
347290001Sglebiusint
348290001Sglebiusreg_used_between_p (reg, from_insn, to_insn)
349290001Sglebius     rtx reg, from_insn, to_insn;
350290001Sglebius{
351290001Sglebius  register rtx insn;
352290001Sglebius
353290001Sglebius  if (from_insn == to_insn)
354290001Sglebius    return 0;
355290001Sglebius
356290001Sglebius  for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
357290001Sglebius    if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
358290001Sglebius	&& (reg_overlap_mentioned_p (reg, PATTERN (insn))
359290001Sglebius	   || (GET_CODE (insn) == CALL_INSN
360290001Sglebius	      && (find_reg_fusage (insn, USE, reg)
361290001Sglebius		  || find_reg_fusage (insn, CLOBBER, reg)))))
362290001Sglebius      return 1;
363290001Sglebius  return 0;
364290001Sglebius}
365290001Sglebius
366290001Sglebius/* Nonzero if the old value of X, a register, is referenced in BODY.  If X
367290001Sglebius   is entirely replaced by a new value and the only use is as a SET_DEST,
368290001Sglebius   we do not consider it a reference.  */
369290001Sglebius
370290001Sglebiusint
371290001Sglebiusreg_referenced_p (x, body)
372290001Sglebius     rtx x;
373290001Sglebius     rtx body;
374290001Sglebius{
375290001Sglebius  int i;
376290001Sglebius
377290001Sglebius  switch (GET_CODE (body))
378290001Sglebius    {
379290001Sglebius    case SET:
380290001Sglebius      if (reg_overlap_mentioned_p (x, SET_SRC (body)))
381290001Sglebius	return 1;
382290001Sglebius
383290001Sglebius      /* If the destination is anything other than CC0, PC, a REG or a SUBREG
384290001Sglebius	 of a REG that occupies all of the REG, the insn references X if
385290001Sglebius	 it is mentioned in the destination.  */
386290001Sglebius      if (GET_CODE (SET_DEST (body)) != CC0
387290001Sglebius	  && GET_CODE (SET_DEST (body)) != PC
388290001Sglebius	  && GET_CODE (SET_DEST (body)) != REG
389290001Sglebius	  && ! (GET_CODE (SET_DEST (body)) == SUBREG
390290001Sglebius		&& GET_CODE (SUBREG_REG (SET_DEST (body))) == REG
391290001Sglebius		&& (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (body))))
392290001Sglebius		      + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
393290001Sglebius		    == ((GET_MODE_SIZE (GET_MODE (SET_DEST (body)))
394290001Sglebius			 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
395290001Sglebius	  && reg_overlap_mentioned_p (x, SET_DEST (body)))
396290001Sglebius	return 1;
397290001Sglebius      return 0;
398290001Sglebius
399290001Sglebius    case ASM_OPERANDS:
400290001Sglebius      for (i = ASM_OPERANDS_INPUT_LENGTH (body) - 1; i >= 0; i--)
401290001Sglebius	if (reg_overlap_mentioned_p (x, ASM_OPERANDS_INPUT (body, i)))
402290001Sglebius	  return 1;
403290001Sglebius      return 0;
404290001Sglebius
405290001Sglebius    case CALL:
406290001Sglebius    case USE:
407290001Sglebius      return reg_overlap_mentioned_p (x, body);
408290001Sglebius
409290001Sglebius    case TRAP_IF:
410290001Sglebius      return reg_overlap_mentioned_p (x, TRAP_CONDITION (body));
411290001Sglebius
412290001Sglebius    case UNSPEC:
413290001Sglebius    case UNSPEC_VOLATILE:
414290001Sglebius    case PARALLEL:
415290001Sglebius      for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
416290001Sglebius	if (reg_referenced_p (x, XVECEXP (body, 0, i)))
417290001Sglebius	  return 1;
418290001Sglebius      return 0;
419290001Sglebius
420290001Sglebius    default:
421290001Sglebius      return 0;
422290001Sglebius    }
423290001Sglebius}
424290001Sglebius
425290001Sglebius/* Nonzero if register REG is referenced in an insn between
426290001Sglebius   FROM_INSN and TO_INSN (exclusive of those two).  Sets of REG do
427290001Sglebius   not count.  */
428290001Sglebius
429290001Sglebiusint
430290001Sglebiusreg_referenced_between_p (reg, from_insn, to_insn)
431290001Sglebius     rtx reg, from_insn, to_insn;
432290001Sglebius{
433290001Sglebius  register rtx insn;
434290001Sglebius
435290001Sglebius  if (from_insn == to_insn)
436290001Sglebius    return 0;
437290001Sglebius
438290001Sglebius  for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
439290001Sglebius    if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
440290001Sglebius	&& (reg_referenced_p (reg, PATTERN (insn))
441290001Sglebius	   || (GET_CODE (insn) == CALL_INSN
442290001Sglebius	      && find_reg_fusage (insn, USE, reg))))
443290001Sglebius      return 1;
444290001Sglebius  return 0;
445290001Sglebius}
446290001Sglebius
447290001Sglebius/* Nonzero if register REG is set or clobbered in an insn between
448290001Sglebius   FROM_INSN and TO_INSN (exclusive of those two).  */
449290001Sglebius
450290001Sglebiusint
451290001Sglebiusreg_set_between_p (reg, from_insn, to_insn)
452290001Sglebius     rtx reg, from_insn, to_insn;
453290001Sglebius{
454290001Sglebius  register rtx insn;
455290001Sglebius
456290001Sglebius  if (from_insn == to_insn)
457290001Sglebius    return 0;
458290001Sglebius
459290001Sglebius  for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
460290001Sglebius    if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
461290001Sglebius	&& reg_set_p (reg, insn))
462290001Sglebius      return 1;
463290001Sglebius  return 0;
464290001Sglebius}
465290001Sglebius
466290001Sglebius/* Internals of reg_set_between_p.  */
467290001Sglebius
468290001Sglebiusstatic rtx reg_set_reg;
469290001Sglebiusstatic int reg_set_flag;
470290001Sglebius
471290001Sglebiusstatic void
472290001Sglebiusreg_set_p_1 (x, pat)
473290001Sglebius     rtx x;
474290001Sglebius     rtx pat ATTRIBUTE_UNUSED;
475290001Sglebius{
476290001Sglebius  /* We don't want to return 1 if X is a MEM that contains a register
477290001Sglebius     within REG_SET_REG.  */
478290001Sglebius
479290001Sglebius  if ((GET_CODE (x) != MEM)
480290001Sglebius      && reg_overlap_mentioned_p (reg_set_reg, x))
481290001Sglebius    reg_set_flag = 1;
482290001Sglebius}
483290001Sglebius
484290001Sglebiusint
485290001Sglebiusreg_set_p (reg, insn)
486290001Sglebius     rtx reg, insn;
487290001Sglebius{
488290001Sglebius  rtx body = insn;
489290001Sglebius
490290001Sglebius  /* We can be passed an insn or part of one.  If we are passed an insn,
491290001Sglebius     check if a side-effect of the insn clobbers REG.  */
492290001Sglebius  if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
493290001Sglebius    {
494290001Sglebius      if (FIND_REG_INC_NOTE (insn, reg)
495290001Sglebius	  || (GET_CODE (insn) == CALL_INSN
496290001Sglebius	      /* We'd like to test call_used_regs here, but rtlanal.c can't
497290001Sglebius		 reference that variable due to its use in genattrtab.  So
498290001Sglebius		 we'll just be more conservative.
499290001Sglebius
500290001Sglebius		 ??? Unless we could ensure that the CALL_INSN_FUNCTION_USAGE
501290001Sglebius		 information holds all clobbered registers.  */
502290001Sglebius	      && ((GET_CODE (reg) == REG
503290001Sglebius		   && REGNO (reg) < FIRST_PSEUDO_REGISTER)
504290001Sglebius		  || GET_CODE (reg) == MEM
505290001Sglebius		  || find_reg_fusage (insn, CLOBBER, reg))))
506290001Sglebius	return 1;
507290001Sglebius
508290001Sglebius      body = PATTERN (insn);
509290001Sglebius    }
510290001Sglebius
511290001Sglebius  reg_set_reg = reg;
512290001Sglebius  reg_set_flag = 0;
513290001Sglebius  note_stores (body, reg_set_p_1);
514290001Sglebius  return reg_set_flag;
515290001Sglebius}
516290001Sglebius
517290001Sglebius/* Similar to reg_set_between_p, but check all registers in X.  Return 0
518290001Sglebius   only if none of them are modified between START and END.  Do not
519290001Sglebius   consider non-registers one way or the other.  */
520290001Sglebius
521290001Sglebiusint
522290001Sglebiusregs_set_between_p (x, start, end)
523290001Sglebius     rtx x;
524290001Sglebius     rtx start, end;
525290001Sglebius{
526290001Sglebius  enum rtx_code code = GET_CODE (x);
527290001Sglebius  char *fmt;
528290001Sglebius  int i, j;
529290001Sglebius
530290001Sglebius  switch (code)
531290001Sglebius    {
532290001Sglebius    case CONST_INT:
533290001Sglebius    case CONST_DOUBLE:
534290001Sglebius    case CONST:
535290001Sglebius    case SYMBOL_REF:
536290001Sglebius    case LABEL_REF:
537290001Sglebius    case PC:
538290001Sglebius    case CC0:
539290001Sglebius      return 0;
540290001Sglebius
541290001Sglebius    case REG:
542290001Sglebius      return reg_set_between_p (x, start, end);
543290001Sglebius
544290001Sglebius    default:
545290001Sglebius      break;
546290001Sglebius    }
547290001Sglebius
548290001Sglebius  fmt = GET_RTX_FORMAT (code);
549290001Sglebius  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
550290001Sglebius    {
551290001Sglebius      if (fmt[i] == 'e' && regs_set_between_p (XEXP (x, i), start, end))
552290001Sglebius	return 1;
553290001Sglebius
554290001Sglebius      else if (fmt[i] == 'E')
555290001Sglebius	for (j = XVECLEN (x, i) - 1; j >= 0; j--)
556290001Sglebius	  if (regs_set_between_p (XVECEXP (x, i, j), start, end))
557290001Sglebius	    return 1;
558290001Sglebius    }
559290001Sglebius
560290001Sglebius  return 0;
561290001Sglebius}
562290001Sglebius
563290001Sglebius/* Similar to reg_set_between_p, but check all registers in X.  Return 0
564290001Sglebius   only if none of them are modified between START and END.  Return 1 if
565290001Sglebius   X contains a MEM; this routine does not perform any memory aliasing.  */
566290001Sglebius
567290001Sglebiusint
568290001Sglebiusmodified_between_p (x, start, end)
569290001Sglebius     rtx x;
570290001Sglebius     rtx start, end;
571290001Sglebius{
572290001Sglebius  enum rtx_code code = GET_CODE (x);
573290001Sglebius  char *fmt;
574290001Sglebius  int i, j;
575290001Sglebius
576290001Sglebius  switch (code)
577290001Sglebius    {
578290001Sglebius    case CONST_INT:
579290001Sglebius    case CONST_DOUBLE:
580290001Sglebius    case CONST:
581290001Sglebius    case SYMBOL_REF:
582290001Sglebius    case LABEL_REF:
583290001Sglebius      return 0;
584290001Sglebius
585    case PC:
586    case CC0:
587      return 1;
588
589    case MEM:
590      /* If the memory is not constant, assume it is modified.  If it is
591	 constant, we still have to check the address.  */
592      if (! RTX_UNCHANGING_P (x))
593	return 1;
594      break;
595
596    case REG:
597      return reg_set_between_p (x, start, end);
598
599    default:
600      break;
601    }
602
603  fmt = GET_RTX_FORMAT (code);
604  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
605    {
606      if (fmt[i] == 'e' && modified_between_p (XEXP (x, i), start, end))
607	return 1;
608
609      if (fmt[i] == 'E')
610	for (j = XVECLEN (x, i) - 1; j >= 0; j--)
611	  if (modified_between_p (XVECEXP (x, i, j), start, end))
612	    return 1;
613    }
614
615  return 0;
616}
617
618/* Similar to reg_set_p, but check all registers in X.  Return 0 only if none
619   of them are modified in INSN.  Return 1 if X contains a MEM; this routine
620   does not perform any memory aliasing.  */
621
622int
623modified_in_p (x, insn)
624     rtx x;
625     rtx insn;
626{
627  enum rtx_code code = GET_CODE (x);
628  char *fmt;
629  int i, j;
630
631  switch (code)
632    {
633    case CONST_INT:
634    case CONST_DOUBLE:
635    case CONST:
636    case SYMBOL_REF:
637    case LABEL_REF:
638      return 0;
639
640    case PC:
641    case CC0:
642      return 1;
643
644    case MEM:
645      /* If the memory is not constant, assume it is modified.  If it is
646	 constant, we still have to check the address.  */
647      if (! RTX_UNCHANGING_P (x))
648	return 1;
649      break;
650
651    case REG:
652      return reg_set_p (x, insn);
653
654    default:
655      break;
656    }
657
658  fmt = GET_RTX_FORMAT (code);
659  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
660    {
661      if (fmt[i] == 'e' && modified_in_p (XEXP (x, i), insn))
662	return 1;
663
664      if (fmt[i] == 'E')
665	for (j = XVECLEN (x, i) - 1; j >= 0; j--)
666	  if (modified_in_p (XVECEXP (x, i, j), insn))
667	    return 1;
668    }
669
670  return 0;
671}
672
673/* Given an INSN, return a SET expression if this insn has only a single SET.
674   It may also have CLOBBERs, USEs, or SET whose output
675   will not be used, which we ignore.  */
676
677rtx
678single_set (insn)
679     rtx insn;
680{
681  rtx set;
682  int i;
683
684  if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
685    return 0;
686
687  if (GET_CODE (PATTERN (insn)) == SET)
688    return PATTERN (insn);
689
690  else if (GET_CODE (PATTERN (insn)) == PARALLEL)
691    {
692      for (i = 0, set = 0; i < XVECLEN (PATTERN (insn), 0); i++)
693	if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET
694	    && (! find_reg_note (insn, REG_UNUSED,
695				 SET_DEST (XVECEXP (PATTERN (insn), 0, i)))
696		|| side_effects_p (XVECEXP (PATTERN (insn), 0, i))))
697	  {
698	    if (set)
699	      return 0;
700	    else
701	      set = XVECEXP (PATTERN (insn), 0, i);
702	  }
703      return set;
704    }
705
706  return 0;
707}
708
709/* Given an INSN, return nonzero if it has more than one SET, else return
710   zero.  */
711
712int
713multiple_sets (insn)
714     rtx insn;
715{
716  int found;
717  int i;
718
719  /* INSN must be an insn.  */
720  if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
721    return 0;
722
723  /* Only a PARALLEL can have multiple SETs.  */
724  if (GET_CODE (PATTERN (insn)) == PARALLEL)
725    {
726      for (i = 0, found = 0; i < XVECLEN (PATTERN (insn), 0); i++)
727	if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
728	  {
729	    /* If we have already found a SET, then return now.  */
730	    if (found)
731	      return 1;
732	    else
733	      found = 1;
734	  }
735    }
736
737  /* Either zero or one SET.  */
738  return 0;
739}
740
741/* Return the last thing that X was assigned from before *PINSN.  Verify that
742   the object is not modified up to VALID_TO.  If it was, if we hit
743   a partial assignment to X, or hit a CODE_LABEL first, return X.  If we
744   found an assignment, update *PINSN to point to it.
745   ALLOW_HWREG is set to 1 if hardware registers are allowed to be the src.  */
746
747rtx
748find_last_value (x, pinsn, valid_to, allow_hwreg)
749     rtx x;
750     rtx *pinsn;
751     rtx valid_to;
752     int allow_hwreg;
753{
754  rtx p;
755
756  for (p = PREV_INSN (*pinsn); p && GET_CODE (p) != CODE_LABEL;
757       p = PREV_INSN (p))
758    if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
759      {
760	rtx set = single_set (p);
761	rtx note = find_reg_note (p, REG_EQUAL, NULL_RTX);
762
763	if (set && rtx_equal_p (x, SET_DEST (set)))
764	  {
765	    rtx src = SET_SRC (set);
766
767	    if (note && GET_CODE (XEXP (note, 0)) != EXPR_LIST)
768	      src = XEXP (note, 0);
769
770	    if (! modified_between_p (src, PREV_INSN (p), valid_to)
771		/* Reject hard registers because we don't usually want
772		   to use them; we'd rather use a pseudo.  */
773		&& (! (GET_CODE (src) == REG
774		      && REGNO (src) < FIRST_PSEUDO_REGISTER) || allow_hwreg))
775	      {
776		*pinsn = p;
777		return src;
778	      }
779	  }
780
781	/* If set in non-simple way, we don't have a value.  */
782	if (reg_set_p (x, p))
783	  break;
784      }
785
786  return x;
787}
788
789/* Return nonzero if register in range [REGNO, ENDREGNO)
790   appears either explicitly or implicitly in X
791   other than being stored into.
792
793   References contained within the substructure at LOC do not count.
794   LOC may be zero, meaning don't ignore anything.  */
795
796int
797refers_to_regno_p (regno, endregno, x, loc)
798     int regno, endregno;
799     rtx x;
800     rtx *loc;
801{
802  register int i;
803  register RTX_CODE code;
804  register char *fmt;
805
806 repeat:
807  /* The contents of a REG_NONNEG note is always zero, so we must come here
808     upon repeat in case the last REG_NOTE is a REG_NONNEG note.  */
809  if (x == 0)
810    return 0;
811
812  code = GET_CODE (x);
813
814  switch (code)
815    {
816    case REG:
817      i = REGNO (x);
818
819      /* If we modifying the stack, frame, or argument pointer, it will
820	 clobber a virtual register.  In fact, we could be more precise,
821	 but it isn't worth it.  */
822      if ((i == STACK_POINTER_REGNUM
823#if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
824	   || i == ARG_POINTER_REGNUM
825#endif
826	   || i == FRAME_POINTER_REGNUM)
827	  && regno >= FIRST_VIRTUAL_REGISTER && regno <= LAST_VIRTUAL_REGISTER)
828	return 1;
829
830      return (endregno > i
831	      && regno < i + (i < FIRST_PSEUDO_REGISTER
832			      ? HARD_REGNO_NREGS (i, GET_MODE (x))
833			      : 1));
834
835    case SUBREG:
836      /* If this is a SUBREG of a hard reg, we can see exactly which
837	 registers are being modified.  Otherwise, handle normally.  */
838      if (GET_CODE (SUBREG_REG (x)) == REG
839	  && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER)
840	{
841	  int inner_regno = REGNO (SUBREG_REG (x)) + SUBREG_WORD (x);
842	  int inner_endregno
843	    = inner_regno + (inner_regno < FIRST_PSEUDO_REGISTER
844			     ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
845
846	  return endregno > inner_regno && regno < inner_endregno;
847	}
848      break;
849
850    case CLOBBER:
851    case SET:
852      if (&SET_DEST (x) != loc
853	  /* Note setting a SUBREG counts as referring to the REG it is in for
854	     a pseudo but not for hard registers since we can
855	     treat each word individually.  */
856	  && ((GET_CODE (SET_DEST (x)) == SUBREG
857	       && loc != &SUBREG_REG (SET_DEST (x))
858	       && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG
859	       && REGNO (SUBREG_REG (SET_DEST (x))) >= FIRST_PSEUDO_REGISTER
860	       && refers_to_regno_p (regno, endregno,
861				     SUBREG_REG (SET_DEST (x)), loc))
862	      || (GET_CODE (SET_DEST (x)) != REG
863		  && refers_to_regno_p (regno, endregno, SET_DEST (x), loc))))
864	return 1;
865
866      if (code == CLOBBER || loc == &SET_SRC (x))
867	return 0;
868      x = SET_SRC (x);
869      goto repeat;
870
871    default:
872      break;
873    }
874
875  /* X does not match, so try its subexpressions.  */
876
877  fmt = GET_RTX_FORMAT (code);
878  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
879    {
880      if (fmt[i] == 'e' && loc != &XEXP (x, i))
881	{
882	  if (i == 0)
883	    {
884	      x = XEXP (x, 0);
885	      goto repeat;
886	    }
887	  else
888	    if (refers_to_regno_p (regno, endregno, XEXP (x, i), loc))
889	      return 1;
890	}
891      else if (fmt[i] == 'E')
892	{
893	  register int j;
894	  for (j = XVECLEN (x, i) - 1; j >=0; j--)
895	    if (loc != &XVECEXP (x, i, j)
896		&& refers_to_regno_p (regno, endregno, XVECEXP (x, i, j), loc))
897	      return 1;
898	}
899    }
900  return 0;
901}
902
903/* Nonzero if modifying X will affect IN.  If X is a register or a SUBREG,
904   we check if any register number in X conflicts with the relevant register
905   numbers.  If X is a constant, return 0.  If X is a MEM, return 1 iff IN
906   contains a MEM (we don't bother checking for memory addresses that can't
907   conflict because we expect this to be a rare case.  */
908
909int
910reg_overlap_mentioned_p (x, in)
911     rtx x, in;
912{
913  int regno, endregno;
914
915  /* Overly conservative.  */
916  if (GET_CODE (x) == STRICT_LOW_PART)
917    x = XEXP (x, 0);
918
919  /* If either argument is a constant, then modifying X can not affect IN.  */
920  if (CONSTANT_P (x) || CONSTANT_P (in))
921    return 0;
922  else if (GET_CODE (x) == SUBREG)
923    {
924      regno = REGNO (SUBREG_REG (x));
925      if (regno < FIRST_PSEUDO_REGISTER)
926	regno += SUBREG_WORD (x);
927    }
928  else if (GET_CODE (x) == REG)
929    regno = REGNO (x);
930  else if (GET_CODE (x) == MEM)
931    {
932      char *fmt;
933      int i;
934
935      if (GET_CODE (in) == MEM)
936	return 1;
937
938      fmt = GET_RTX_FORMAT (GET_CODE (in));
939
940      for (i = GET_RTX_LENGTH (GET_CODE (in)) - 1; i >= 0; i--)
941	if (fmt[i] == 'e' && reg_overlap_mentioned_p (x, XEXP (in, i)))
942	  return 1;
943
944      return 0;
945    }
946  else if (GET_CODE (x) == SCRATCH || GET_CODE (x) == PC
947	   || GET_CODE (x) == CC0)
948    return reg_mentioned_p (x, in);
949  else if (GET_CODE (x) == PARALLEL
950	   && GET_MODE (x) == BLKmode)
951    {
952      register int i;
953
954      /* If any register in here refers to it
955	 we return true.  */
956      for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
957	if (reg_overlap_mentioned_p (SET_DEST (XVECEXP (x, 0, i)), in))
958	  return 1;
959      return 0;
960    }
961  else
962    abort ();
963
964  endregno = regno + (regno < FIRST_PSEUDO_REGISTER
965		      ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
966
967  return refers_to_regno_p (regno, endregno, in, NULL_PTR);
968}
969
970/* Used for communications between the next few functions.  */
971
972static int reg_set_last_unknown;
973static rtx reg_set_last_value;
974static int reg_set_last_first_regno, reg_set_last_last_regno;
975
976/* Called via note_stores from reg_set_last.  */
977
978static void
979reg_set_last_1 (x, pat)
980     rtx x;
981     rtx pat;
982{
983  int first, last;
984
985  /* If X is not a register, or is not one in the range we care
986     about, ignore.  */
987  if (GET_CODE (x) != REG)
988    return;
989
990  first = REGNO (x);
991  last = first + (first < FIRST_PSEUDO_REGISTER
992		  ? HARD_REGNO_NREGS (first, GET_MODE (x)) : 1);
993
994  if (first >= reg_set_last_last_regno
995      || last <= reg_set_last_first_regno)
996    return;
997
998  /* If this is a CLOBBER or is some complex LHS, or doesn't modify
999     exactly the registers we care about, show we don't know the value.  */
1000  if (GET_CODE (pat) == CLOBBER || SET_DEST (pat) != x
1001      || first != reg_set_last_first_regno
1002      || last != reg_set_last_last_regno)
1003    reg_set_last_unknown = 1;
1004  else
1005    reg_set_last_value = SET_SRC (pat);
1006}
1007
1008/* Return the last value to which REG was set prior to INSN.  If we can't
1009   find it easily, return 0.
1010
1011   We only return a REG, SUBREG, or constant because it is too hard to
1012   check if a MEM remains unchanged.  */
1013
1014rtx
1015reg_set_last (x, insn)
1016     rtx x;
1017     rtx insn;
1018{
1019  rtx orig_insn = insn;
1020
1021  reg_set_last_first_regno = REGNO (x);
1022
1023  reg_set_last_last_regno
1024    = reg_set_last_first_regno
1025      + (reg_set_last_first_regno < FIRST_PSEUDO_REGISTER
1026	 ? HARD_REGNO_NREGS (reg_set_last_first_regno, GET_MODE (x)) : 1);
1027
1028  reg_set_last_unknown = 0;
1029  reg_set_last_value = 0;
1030
1031  /* Scan backwards until reg_set_last_1 changed one of the above flags.
1032     Stop when we reach a label or X is a hard reg and we reach a
1033     CALL_INSN (if reg_set_last_last_regno is a hard reg).
1034
1035     If we find a set of X, ensure that its SET_SRC remains unchanged.  */
1036
1037  /* We compare with <= here, because reg_set_last_last_regno
1038     is actually the number of the first reg *not* in X.  */
1039  for (;
1040       insn && GET_CODE (insn) != CODE_LABEL
1041       && ! (GET_CODE (insn) == CALL_INSN
1042	     && reg_set_last_last_regno <= FIRST_PSEUDO_REGISTER);
1043       insn = PREV_INSN (insn))
1044    if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1045      {
1046	note_stores (PATTERN (insn), reg_set_last_1);
1047	if (reg_set_last_unknown)
1048	  return 0;
1049	else if (reg_set_last_value)
1050	  {
1051	    if (CONSTANT_P (reg_set_last_value)
1052		|| ((GET_CODE (reg_set_last_value) == REG
1053		     || GET_CODE (reg_set_last_value) == SUBREG)
1054		    && ! reg_set_between_p (reg_set_last_value,
1055					    insn, orig_insn)))
1056	      return reg_set_last_value;
1057	    else
1058	      return 0;
1059	  }
1060      }
1061
1062  return 0;
1063}
1064
1065/* This is 1 until after the rtl generation pass.  */
1066int rtx_equal_function_value_matters;
1067
1068/* Return 1 if X and Y are identical-looking rtx's.
1069   This is the Lisp function EQUAL for rtx arguments.  */
1070
1071int
1072rtx_equal_p (x, y)
1073     rtx x, y;
1074{
1075  register int i;
1076  register int j;
1077  register enum rtx_code code;
1078  register char *fmt;
1079
1080  if (x == y)
1081    return 1;
1082  if (x == 0 || y == 0)
1083    return 0;
1084
1085  code = GET_CODE (x);
1086  /* Rtx's of different codes cannot be equal.  */
1087  if (code != GET_CODE (y))
1088    return 0;
1089
1090  /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1091     (REG:SI x) and (REG:HI x) are NOT equivalent.  */
1092
1093  if (GET_MODE (x) != GET_MODE (y))
1094    return 0;
1095
1096  /* REG, LABEL_REF, and SYMBOL_REF can be compared nonrecursively.  */
1097
1098  if (code == REG)
1099    /* Until rtl generation is complete, don't consider a reference to the
1100       return register of the current function the same as the return from a
1101       called function.  This eases the job of function integration.  Once the
1102       distinction is no longer needed, they can be considered equivalent.  */
1103    return (REGNO (x) == REGNO (y)
1104	    && (! rtx_equal_function_value_matters
1105		|| REG_FUNCTION_VALUE_P (x) == REG_FUNCTION_VALUE_P (y)));
1106  else if (code == LABEL_REF)
1107    return XEXP (x, 0) == XEXP (y, 0);
1108  else if (code == SYMBOL_REF)
1109    return XSTR (x, 0) == XSTR (y, 0);
1110  else if (code == SCRATCH || code == CONST_DOUBLE)
1111    return 0;
1112
1113  /* Compare the elements.  If any pair of corresponding elements
1114     fail to match, return 0 for the whole things.  */
1115
1116  fmt = GET_RTX_FORMAT (code);
1117  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1118    {
1119      switch (fmt[i])
1120	{
1121	case 'w':
1122	  if (XWINT (x, i) != XWINT (y, i))
1123	    return 0;
1124	  break;
1125
1126	case 'n':
1127	case 'i':
1128	  if (XINT (x, i) != XINT (y, i))
1129	    return 0;
1130	  break;
1131
1132	case 'V':
1133	case 'E':
1134	  /* Two vectors must have the same length.  */
1135	  if (XVECLEN (x, i) != XVECLEN (y, i))
1136	    return 0;
1137
1138	  /* And the corresponding elements must match.  */
1139	  for (j = 0; j < XVECLEN (x, i); j++)
1140	    if (rtx_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)) == 0)
1141	      return 0;
1142	  break;
1143
1144	case 'e':
1145	  if (rtx_equal_p (XEXP (x, i), XEXP (y, i)) == 0)
1146	    return 0;
1147	  break;
1148
1149	case 'S':
1150	case 's':
1151	  if (strcmp (XSTR (x, i), XSTR (y, i)))
1152	    return 0;
1153	  break;
1154
1155	case 'u':
1156	  /* These are just backpointers, so they don't matter.  */
1157	  break;
1158
1159	case '0':
1160	  break;
1161
1162	  /* It is believed that rtx's at this level will never
1163	     contain anything but integers and other rtx's,
1164	     except for within LABEL_REFs and SYMBOL_REFs.  */
1165	default:
1166	  abort ();
1167	}
1168    }
1169  return 1;
1170}
1171
1172/* Call FUN on each register or MEM that is stored into or clobbered by X.
1173   (X would be the pattern of an insn).
1174   FUN receives two arguments:
1175     the REG, MEM, CC0 or PC being stored in or clobbered,
1176     the SET or CLOBBER rtx that does the store.
1177
1178  If the item being stored in or clobbered is a SUBREG of a hard register,
1179  the SUBREG will be passed.  */
1180
1181void
1182note_stores (x, fun)
1183     register rtx x;
1184     void (*fun) PROTO ((rtx, rtx));
1185{
1186  if ((GET_CODE (x) == SET || GET_CODE (x) == CLOBBER))
1187    {
1188      register rtx dest = SET_DEST (x);
1189      while ((GET_CODE (dest) == SUBREG
1190	      && (GET_CODE (SUBREG_REG (dest)) != REG
1191		  || REGNO (SUBREG_REG (dest)) >= FIRST_PSEUDO_REGISTER))
1192	     || GET_CODE (dest) == ZERO_EXTRACT
1193	     || GET_CODE (dest) == SIGN_EXTRACT
1194	     || GET_CODE (dest) == STRICT_LOW_PART)
1195	dest = XEXP (dest, 0);
1196
1197      if (GET_CODE (dest) == PARALLEL
1198	  && GET_MODE (dest) == BLKmode)
1199	{
1200	  register int i;
1201	  for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
1202	    (*fun) (SET_DEST (XVECEXP (dest, 0, i)), x);
1203	}
1204      else
1205	(*fun) (dest, x);
1206    }
1207  else if (GET_CODE (x) == PARALLEL)
1208    {
1209      register int i;
1210      for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1211	{
1212	  register rtx y = XVECEXP (x, 0, i);
1213	  if (GET_CODE (y) == SET || GET_CODE (y) == CLOBBER)
1214	    {
1215	      register rtx dest = SET_DEST (y);
1216	      while ((GET_CODE (dest) == SUBREG
1217		      && (GET_CODE (SUBREG_REG (dest)) != REG
1218			  || (REGNO (SUBREG_REG (dest))
1219			      >= FIRST_PSEUDO_REGISTER)))
1220		     || GET_CODE (dest) == ZERO_EXTRACT
1221		     || GET_CODE (dest) == SIGN_EXTRACT
1222		     || GET_CODE (dest) == STRICT_LOW_PART)
1223		dest = XEXP (dest, 0);
1224	      if (GET_CODE (dest) == PARALLEL
1225		  && GET_MODE (dest) == BLKmode)
1226		{
1227		  register int i;
1228		  for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
1229		    (*fun) (SET_DEST (XVECEXP (dest, 0, i)), y);
1230		}
1231	      else
1232		(*fun) (dest, y);
1233	    }
1234	}
1235    }
1236}
1237
1238/* Return nonzero if X's old contents don't survive after INSN.
1239   This will be true if X is (cc0) or if X is a register and
1240   X dies in INSN or because INSN entirely sets X.
1241
1242   "Entirely set" means set directly and not through a SUBREG,
1243   ZERO_EXTRACT or SIGN_EXTRACT, so no trace of the old contents remains.
1244   Likewise, REG_INC does not count.
1245
1246   REG may be a hard or pseudo reg.  Renumbering is not taken into account,
1247   but for this use that makes no difference, since regs don't overlap
1248   during their lifetimes.  Therefore, this function may be used
1249   at any time after deaths have been computed (in flow.c).
1250
1251   If REG is a hard reg that occupies multiple machine registers, this
1252   function will only return 1 if each of those registers will be replaced
1253   by INSN.  */
1254
1255int
1256dead_or_set_p (insn, x)
1257     rtx insn;
1258     rtx x;
1259{
1260  register int regno, last_regno;
1261  register int i;
1262
1263  /* Can't use cc0_rtx below since this file is used by genattrtab.c.  */
1264  if (GET_CODE (x) == CC0)
1265    return 1;
1266
1267  if (GET_CODE (x) != REG)
1268    abort ();
1269
1270  regno = REGNO (x);
1271  last_regno = (regno >= FIRST_PSEUDO_REGISTER ? regno
1272		: regno + HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1);
1273
1274  for (i = regno; i <= last_regno; i++)
1275    if (! dead_or_set_regno_p (insn, i))
1276      return 0;
1277
1278  return 1;
1279}
1280
1281/* Utility function for dead_or_set_p to check an individual register.  Also
1282   called from flow.c.  */
1283
1284int
1285dead_or_set_regno_p (insn, test_regno)
1286     rtx insn;
1287     int test_regno;
1288{
1289  int regno, endregno;
1290  rtx link;
1291
1292  /* See if there is a death note for something that includes
1293     TEST_REGNO.  */
1294  for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1295    {
1296      if (REG_NOTE_KIND (link) != REG_DEAD
1297	  || GET_CODE (XEXP (link, 0)) != REG)
1298	continue;
1299
1300      regno = REGNO (XEXP (link, 0));
1301      endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1302		  : regno + HARD_REGNO_NREGS (regno,
1303					      GET_MODE (XEXP (link, 0))));
1304
1305      if (test_regno >= regno && test_regno < endregno)
1306	return 1;
1307    }
1308
1309  if (GET_CODE (insn) == CALL_INSN
1310      && find_regno_fusage (insn, CLOBBER, test_regno))
1311    return 1;
1312
1313  if (GET_CODE (PATTERN (insn)) == SET)
1314    {
1315      rtx dest = SET_DEST (PATTERN (insn));
1316
1317      /* A value is totally replaced if it is the destination or the
1318	 destination is a SUBREG of REGNO that does not change the number of
1319	 words in it.  */
1320      if (GET_CODE (dest) == SUBREG
1321	  && (((GET_MODE_SIZE (GET_MODE (dest))
1322		+ UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1323	      == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1324		   + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1325	dest = SUBREG_REG (dest);
1326
1327      if (GET_CODE (dest) != REG)
1328	return 0;
1329
1330      regno = REGNO (dest);
1331      endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1332		  : regno + HARD_REGNO_NREGS (regno, GET_MODE (dest)));
1333
1334      return (test_regno >= regno && test_regno < endregno);
1335    }
1336  else if (GET_CODE (PATTERN (insn)) == PARALLEL)
1337    {
1338      register int i;
1339
1340      for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1341	{
1342	  rtx body = XVECEXP (PATTERN (insn), 0, i);
1343
1344	  if (GET_CODE (body) == SET || GET_CODE (body) == CLOBBER)
1345	    {
1346	      rtx dest = SET_DEST (body);
1347
1348	      if (GET_CODE (dest) == SUBREG
1349		  && (((GET_MODE_SIZE (GET_MODE (dest))
1350			+ UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1351		      == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1352			   + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1353		dest = SUBREG_REG (dest);
1354
1355	      if (GET_CODE (dest) != REG)
1356		continue;
1357
1358	      regno = REGNO (dest);
1359	      endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1360			  : regno + HARD_REGNO_NREGS (regno, GET_MODE (dest)));
1361
1362	      if (test_regno >= regno && test_regno < endregno)
1363		return 1;
1364	    }
1365	}
1366    }
1367
1368  return 0;
1369}
1370
1371/* Return the reg-note of kind KIND in insn INSN, if there is one.
1372   If DATUM is nonzero, look for one whose datum is DATUM.  */
1373
1374rtx
1375find_reg_note (insn, kind, datum)
1376     rtx insn;
1377     enum reg_note kind;
1378     rtx datum;
1379{
1380  register rtx link;
1381
1382  /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN.  */
1383  if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
1384    return 0;
1385
1386  for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1387    if (REG_NOTE_KIND (link) == kind
1388	&& (datum == 0 || datum == XEXP (link, 0)))
1389      return link;
1390  return 0;
1391}
1392
1393/* Return the reg-note of kind KIND in insn INSN which applies to register
1394   number REGNO, if any.  Return 0 if there is no such reg-note.  Note that
1395   the REGNO of this NOTE need not be REGNO if REGNO is a hard register;
1396   it might be the case that the note overlaps REGNO.  */
1397
1398rtx
1399find_regno_note (insn, kind, regno)
1400     rtx insn;
1401     enum reg_note kind;
1402     int regno;
1403{
1404  register rtx link;
1405
1406  /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN.  */
1407  if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
1408    return 0;
1409
1410  for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1411    if (REG_NOTE_KIND (link) == kind
1412	/* Verify that it is a register, so that scratch and MEM won't cause a
1413	   problem here.  */
1414	&& GET_CODE (XEXP (link, 0)) == REG
1415	&& REGNO (XEXP (link, 0)) <= regno
1416	&& ((REGNO (XEXP (link, 0))
1417	     + (REGNO (XEXP (link, 0)) >= FIRST_PSEUDO_REGISTER ? 1
1418		: HARD_REGNO_NREGS (REGNO (XEXP (link, 0)),
1419				    GET_MODE (XEXP (link, 0)))))
1420	    > regno))
1421      return link;
1422  return 0;
1423}
1424
1425/* Return true if DATUM, or any overlap of DATUM, of kind CODE is found
1426   in the CALL_INSN_FUNCTION_USAGE information of INSN.  */
1427
1428int
1429find_reg_fusage (insn, code, datum)
1430     rtx insn;
1431     enum rtx_code code;
1432     rtx datum;
1433{
1434  /* If it's not a CALL_INSN, it can't possibly have a
1435     CALL_INSN_FUNCTION_USAGE field, so don't bother checking.  */
1436  if (GET_CODE (insn) != CALL_INSN)
1437    return 0;
1438
1439  if (! datum)
1440    abort();
1441
1442  if (GET_CODE (datum) != REG)
1443    {
1444      register rtx link;
1445
1446      for (link = CALL_INSN_FUNCTION_USAGE (insn);
1447           link;
1448	   link = XEXP (link, 1))
1449        if (GET_CODE (XEXP (link, 0)) == code
1450	    && rtx_equal_p (datum, SET_DEST (XEXP (link, 0))))
1451          return 1;
1452    }
1453  else
1454    {
1455      register int regno = REGNO (datum);
1456
1457      /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1458	 to pseudo registers, so don't bother checking.  */
1459
1460      if (regno < FIRST_PSEUDO_REGISTER)
1461        {
1462	  int end_regno = regno + HARD_REGNO_NREGS (regno, GET_MODE (datum));
1463	  int i;
1464
1465	  for (i = regno; i < end_regno; i++)
1466	    if (find_regno_fusage (insn, code, i))
1467	      return 1;
1468        }
1469    }
1470
1471  return 0;
1472}
1473
1474/* Return true if REGNO, or any overlap of REGNO, of kind CODE is found
1475   in the CALL_INSN_FUNCTION_USAGE information of INSN.  */
1476
1477int
1478find_regno_fusage (insn, code, regno)
1479     rtx insn;
1480     enum rtx_code code;
1481     int regno;
1482{
1483  register rtx link;
1484
1485  /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1486     to pseudo registers, so don't bother checking.  */
1487
1488  if (regno >= FIRST_PSEUDO_REGISTER
1489      || GET_CODE (insn) != CALL_INSN )
1490    return 0;
1491
1492  for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
1493   {
1494    register int regnote;
1495    register rtx op;
1496
1497    if (GET_CODE (op = XEXP (link, 0)) == code
1498	&& GET_CODE (SET_DEST (op)) == REG
1499	&& (regnote = REGNO (SET_DEST (op))) <= regno
1500	&& regnote
1501		+ HARD_REGNO_NREGS (regnote, GET_MODE (SET_DEST (op)))
1502	    > regno)
1503      return 1;
1504   }
1505
1506  return 0;
1507}
1508
1509/* Remove register note NOTE from the REG_NOTES of INSN.  */
1510
1511void
1512remove_note (insn, note)
1513     register rtx note;
1514     register rtx insn;
1515{
1516  register rtx link;
1517
1518  if (REG_NOTES (insn) == note)
1519    {
1520      REG_NOTES (insn) = XEXP (note, 1);
1521      return;
1522    }
1523
1524  for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1525    if (XEXP (link, 1) == note)
1526      {
1527	XEXP (link, 1) = XEXP (note, 1);
1528	return;
1529      }
1530
1531  abort ();
1532}
1533
1534/* Search LISTP (an EXPR_LIST) for NODE and remove NODE from the list
1535   if it is found.
1536
1537   A simple equality test is used to determine if NODE is on the
1538   EXPR_LIST.  */
1539
1540void
1541remove_node_from_expr_list (node, listp)
1542     rtx node;
1543     rtx *listp;
1544{
1545  rtx temp = *listp;
1546  rtx prev = NULL_RTX;
1547
1548  while (temp)
1549    {
1550      if (node == XEXP (temp, 0))
1551	{
1552	  /* Splice the node out of the list.  */
1553	  if (prev)
1554	    XEXP (prev, 1) = XEXP (temp, 1);
1555	  else
1556	    *listp = XEXP (temp, 1);
1557
1558	  return;
1559	}
1560      temp = XEXP (temp, 1);
1561    }
1562}
1563
1564/* Nonzero if X contains any volatile instructions.  These are instructions
1565   which may cause unpredictable machine state instructions, and thus no
1566   instructions should be moved or combined across them.  This includes
1567   only volatile asms and UNSPEC_VOLATILE instructions.  */
1568
1569int
1570volatile_insn_p (x)
1571     rtx x;
1572{
1573  register RTX_CODE code;
1574
1575  code = GET_CODE (x);
1576  switch (code)
1577    {
1578    case LABEL_REF:
1579    case SYMBOL_REF:
1580    case CONST_INT:
1581    case CONST:
1582    case CONST_DOUBLE:
1583    case CC0:
1584    case PC:
1585    case REG:
1586    case SCRATCH:
1587    case CLOBBER:
1588    case ASM_INPUT:
1589    case ADDR_VEC:
1590    case ADDR_DIFF_VEC:
1591    case CALL:
1592    case MEM:
1593      return 0;
1594
1595    case UNSPEC_VOLATILE:
1596 /* case TRAP_IF: This isn't clear yet.  */
1597      return 1;
1598
1599    case ASM_OPERANDS:
1600      if (MEM_VOLATILE_P (x))
1601	return 1;
1602
1603    default:
1604      break;
1605    }
1606
1607  /* Recursively scan the operands of this expression.  */
1608
1609  {
1610    register char *fmt = GET_RTX_FORMAT (code);
1611    register int i;
1612
1613    for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1614      {
1615	if (fmt[i] == 'e')
1616	  {
1617	    if (volatile_insn_p (XEXP (x, i)))
1618	      return 1;
1619	  }
1620	if (fmt[i] == 'E')
1621	  {
1622	    register int j;
1623	    for (j = 0; j < XVECLEN (x, i); j++)
1624	      if (volatile_insn_p (XVECEXP (x, i, j)))
1625		return 1;
1626	  }
1627      }
1628  }
1629  return 0;
1630}
1631
1632/* Nonzero if X contains any volatile memory references
1633   UNSPEC_VOLATILE operations or volatile ASM_OPERANDS expressions.  */
1634
1635int
1636volatile_refs_p (x)
1637     rtx x;
1638{
1639  register RTX_CODE code;
1640
1641  code = GET_CODE (x);
1642  switch (code)
1643    {
1644    case LABEL_REF:
1645    case SYMBOL_REF:
1646    case CONST_INT:
1647    case CONST:
1648    case CONST_DOUBLE:
1649    case CC0:
1650    case PC:
1651    case REG:
1652    case SCRATCH:
1653    case CLOBBER:
1654    case ASM_INPUT:
1655    case ADDR_VEC:
1656    case ADDR_DIFF_VEC:
1657      return 0;
1658
1659    case CALL:
1660    case UNSPEC_VOLATILE:
1661 /* case TRAP_IF: This isn't clear yet.  */
1662      return 1;
1663
1664    case MEM:
1665    case ASM_OPERANDS:
1666      if (MEM_VOLATILE_P (x))
1667	return 1;
1668
1669    default:
1670      break;
1671    }
1672
1673  /* Recursively scan the operands of this expression.  */
1674
1675  {
1676    register char *fmt = GET_RTX_FORMAT (code);
1677    register int i;
1678
1679    for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1680      {
1681	if (fmt[i] == 'e')
1682	  {
1683	    if (volatile_refs_p (XEXP (x, i)))
1684	      return 1;
1685	  }
1686	if (fmt[i] == 'E')
1687	  {
1688	    register int j;
1689	    for (j = 0; j < XVECLEN (x, i); j++)
1690	      if (volatile_refs_p (XVECEXP (x, i, j)))
1691		return 1;
1692	  }
1693      }
1694  }
1695  return 0;
1696}
1697
1698/* Similar to above, except that it also rejects register pre- and post-
1699   incrementing.  */
1700
1701int
1702side_effects_p (x)
1703     rtx x;
1704{
1705  register RTX_CODE code;
1706
1707  code = GET_CODE (x);
1708  switch (code)
1709    {
1710    case LABEL_REF:
1711    case SYMBOL_REF:
1712    case CONST_INT:
1713    case CONST:
1714    case CONST_DOUBLE:
1715    case CC0:
1716    case PC:
1717    case REG:
1718    case SCRATCH:
1719    case ASM_INPUT:
1720    case ADDR_VEC:
1721    case ADDR_DIFF_VEC:
1722      return 0;
1723
1724    case CLOBBER:
1725      /* Reject CLOBBER with a non-VOID mode.  These are made by combine.c
1726	 when some combination can't be done.  If we see one, don't think
1727	 that we can simplify the expression.  */
1728      return (GET_MODE (x) != VOIDmode);
1729
1730    case PRE_INC:
1731    case PRE_DEC:
1732    case POST_INC:
1733    case POST_DEC:
1734    case CALL:
1735    case UNSPEC_VOLATILE:
1736 /* case TRAP_IF: This isn't clear yet.  */
1737      return 1;
1738
1739    case MEM:
1740    case ASM_OPERANDS:
1741      if (MEM_VOLATILE_P (x))
1742	return 1;
1743
1744    default:
1745      break;
1746    }
1747
1748  /* Recursively scan the operands of this expression.  */
1749
1750  {
1751    register char *fmt = GET_RTX_FORMAT (code);
1752    register int i;
1753
1754    for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1755      {
1756	if (fmt[i] == 'e')
1757	  {
1758	    if (side_effects_p (XEXP (x, i)))
1759	      return 1;
1760	  }
1761	if (fmt[i] == 'E')
1762	  {
1763	    register int j;
1764	    for (j = 0; j < XVECLEN (x, i); j++)
1765	      if (side_effects_p (XVECEXP (x, i, j)))
1766		return 1;
1767	  }
1768      }
1769  }
1770  return 0;
1771}
1772
1773/* Return nonzero if evaluating rtx X might cause a trap.  */
1774
1775int
1776may_trap_p (x)
1777     rtx x;
1778{
1779  int i;
1780  enum rtx_code code;
1781  char *fmt;
1782
1783  if (x == 0)
1784    return 0;
1785  code = GET_CODE (x);
1786  switch (code)
1787    {
1788      /* Handle these cases quickly.  */
1789    case CONST_INT:
1790    case CONST_DOUBLE:
1791    case SYMBOL_REF:
1792    case LABEL_REF:
1793    case CONST:
1794    case PC:
1795    case CC0:
1796    case REG:
1797    case SCRATCH:
1798      return 0;
1799
1800      /* Conditional trap can trap!  */
1801    case UNSPEC_VOLATILE:
1802    case TRAP_IF:
1803      return 1;
1804
1805      /* Memory ref can trap unless it's a static var or a stack slot.  */
1806    case MEM:
1807      return rtx_addr_can_trap_p (XEXP (x, 0));
1808
1809      /* Division by a non-constant might trap.  */
1810    case DIV:
1811    case MOD:
1812    case UDIV:
1813    case UMOD:
1814      if (! CONSTANT_P (XEXP (x, 1))
1815	  || GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
1816	return 1;
1817      /* This was const0_rtx, but by not using that,
1818	 we can link this file into other programs.  */
1819      if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 0)
1820	return 1;
1821      break;
1822
1823    case EXPR_LIST:
1824      /* An EXPR_LIST is used to represent a function call.  This
1825	 certainly may trap.  */
1826      return 1;
1827
1828    default:
1829      /* Any floating arithmetic may trap.  */
1830      if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
1831	return 1;
1832    }
1833
1834  fmt = GET_RTX_FORMAT (code);
1835  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1836    {
1837      if (fmt[i] == 'e')
1838	{
1839	  if (may_trap_p (XEXP (x, i)))
1840	    return 1;
1841	}
1842      else if (fmt[i] == 'E')
1843	{
1844	  register int j;
1845	  for (j = 0; j < XVECLEN (x, i); j++)
1846	    if (may_trap_p (XVECEXP (x, i, j)))
1847	      return 1;
1848	}
1849    }
1850  return 0;
1851}
1852
1853/* Return nonzero if X contains a comparison that is not either EQ or NE,
1854   i.e., an inequality.  */
1855
1856int
1857inequality_comparisons_p (x)
1858     rtx x;
1859{
1860  register char *fmt;
1861  register int len, i;
1862  register enum rtx_code code = GET_CODE (x);
1863
1864  switch (code)
1865    {
1866    case REG:
1867    case SCRATCH:
1868    case PC:
1869    case CC0:
1870    case CONST_INT:
1871    case CONST_DOUBLE:
1872    case CONST:
1873    case LABEL_REF:
1874    case SYMBOL_REF:
1875      return 0;
1876
1877    case LT:
1878    case LTU:
1879    case GT:
1880    case GTU:
1881    case LE:
1882    case LEU:
1883    case GE:
1884    case GEU:
1885      return 1;
1886
1887    default:
1888      break;
1889    }
1890
1891  len = GET_RTX_LENGTH (code);
1892  fmt = GET_RTX_FORMAT (code);
1893
1894  for (i = 0; i < len; i++)
1895    {
1896      if (fmt[i] == 'e')
1897	{
1898	  if (inequality_comparisons_p (XEXP (x, i)))
1899	    return 1;
1900	}
1901      else if (fmt[i] == 'E')
1902	{
1903	  register int j;
1904	  for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1905	    if (inequality_comparisons_p (XVECEXP (x, i, j)))
1906	      return 1;
1907	}
1908    }
1909
1910  return 0;
1911}
1912
1913/* Replace any occurrence of FROM in X with TO.  The function does
1914   not enter into CONST_DOUBLE for the replace.
1915
1916   Note that copying is not done so X must not be shared unless all copies
1917   are to be modified.  */
1918
1919rtx
1920replace_rtx (x, from, to)
1921     rtx x, from, to;
1922{
1923  register int i, j;
1924  register char *fmt;
1925
1926  /* The following prevents loops occurrence when we change MEM in
1927     CONST_DOUBLE onto the same CONST_DOUBLE. */
1928  if (x != 0 && GET_CODE (x) == CONST_DOUBLE)
1929    return x;
1930
1931  if (x == from)
1932    return to;
1933
1934  /* Allow this function to make replacements in EXPR_LISTs.  */
1935  if (x == 0)
1936    return 0;
1937
1938  fmt = GET_RTX_FORMAT (GET_CODE (x));
1939  for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
1940    {
1941      if (fmt[i] == 'e')
1942	XEXP (x, i) = replace_rtx (XEXP (x, i), from, to);
1943      else if (fmt[i] == 'E')
1944	for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1945	  XVECEXP (x, i, j) = replace_rtx (XVECEXP (x, i, j), from, to);
1946    }
1947
1948  return x;
1949}
1950
1951/* Throughout the rtx X, replace many registers according to REG_MAP.
1952   Return the replacement for X (which may be X with altered contents).
1953   REG_MAP[R] is the replacement for register R, or 0 for don't replace.
1954   NREGS is the length of REG_MAP; regs >= NREGS are not mapped.
1955
1956   We only support REG_MAP entries of REG or SUBREG.  Also, hard registers
1957   should not be mapped to pseudos or vice versa since validate_change
1958   is not called.
1959
1960   If REPLACE_DEST is 1, replacements are also done in destinations;
1961   otherwise, only sources are replaced.  */
1962
1963rtx
1964replace_regs (x, reg_map, nregs, replace_dest)
1965     rtx x;
1966     rtx *reg_map;
1967     int nregs;
1968     int replace_dest;
1969{
1970  register enum rtx_code code;
1971  register int i;
1972  register char *fmt;
1973
1974  if (x == 0)
1975    return x;
1976
1977  code = GET_CODE (x);
1978  switch (code)
1979    {
1980    case SCRATCH:
1981    case PC:
1982    case CC0:
1983    case CONST_INT:
1984    case CONST_DOUBLE:
1985    case CONST:
1986    case SYMBOL_REF:
1987    case LABEL_REF:
1988      return x;
1989
1990    case REG:
1991      /* Verify that the register has an entry before trying to access it.  */
1992      if (REGNO (x) < nregs && reg_map[REGNO (x)] != 0)
1993	{
1994	  /* SUBREGs can't be shared.  Always return a copy to ensure that if
1995	     this replacement occurs more than once then each instance will
1996	     get distinct rtx.  */
1997	  if (GET_CODE (reg_map[REGNO (x)]) == SUBREG)
1998	    return copy_rtx (reg_map[REGNO (x)]);
1999	  return reg_map[REGNO (x)];
2000	}
2001      return x;
2002
2003    case SUBREG:
2004      /* Prevent making nested SUBREGs.  */
2005      if (GET_CODE (SUBREG_REG (x)) == REG && REGNO (SUBREG_REG (x)) < nregs
2006	  && reg_map[REGNO (SUBREG_REG (x))] != 0
2007	  && GET_CODE (reg_map[REGNO (SUBREG_REG (x))]) == SUBREG)
2008	{
2009	  rtx map_val = reg_map[REGNO (SUBREG_REG (x))];
2010	  rtx map_inner = SUBREG_REG (map_val);
2011
2012	  if (GET_MODE (x) == GET_MODE (map_inner))
2013	    return map_inner;
2014	  else
2015	    {
2016	      /* We cannot call gen_rtx here since we may be linked with
2017		 genattrtab.c.  */
2018	      /* Let's try clobbering the incoming SUBREG and see
2019		 if this is really safe.  */
2020	      SUBREG_REG (x) = map_inner;
2021	      SUBREG_WORD (x) += SUBREG_WORD (map_val);
2022	      return x;
2023#if 0
2024	      rtx new = rtx_alloc (SUBREG);
2025	      PUT_MODE (new, GET_MODE (x));
2026	      SUBREG_REG (new) = map_inner;
2027	      SUBREG_WORD (new) = SUBREG_WORD (x) + SUBREG_WORD (map_val);
2028#endif
2029	    }
2030	}
2031      break;
2032
2033    case SET:
2034      if (replace_dest)
2035	SET_DEST (x) = replace_regs (SET_DEST (x), reg_map, nregs, 0);
2036
2037      else if (GET_CODE (SET_DEST (x)) == MEM
2038	       || GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
2039	/* Even if we are not to replace destinations, replace register if it
2040	   is CONTAINED in destination (destination is memory or
2041	   STRICT_LOW_PART).  */
2042	XEXP (SET_DEST (x), 0) = replace_regs (XEXP (SET_DEST (x), 0),
2043					       reg_map, nregs, 0);
2044      else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
2045	/* Similarly, for ZERO_EXTRACT we replace all operands.  */
2046	break;
2047
2048      SET_SRC (x) = replace_regs (SET_SRC (x), reg_map, nregs, 0);
2049      return x;
2050
2051    default:
2052      break;
2053    }
2054
2055  fmt = GET_RTX_FORMAT (code);
2056  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2057    {
2058      if (fmt[i] == 'e')
2059	XEXP (x, i) = replace_regs (XEXP (x, i), reg_map, nregs, replace_dest);
2060      if (fmt[i] == 'E')
2061	{
2062	  register int j;
2063	  for (j = 0; j < XVECLEN (x, i); j++)
2064	    XVECEXP (x, i, j) = replace_regs (XVECEXP (x, i, j), reg_map,
2065					      nregs, replace_dest);
2066	}
2067    }
2068  return x;
2069}
2070
2071/* Return 1 if X, the SRC_SRC of  SET of (pc) contain a REG or MEM that is
2072   not in the constant pool and not in the condition of an IF_THEN_ELSE.  */
2073
2074static int
2075jmp_uses_reg_or_mem (x)
2076     rtx x;
2077{
2078  enum rtx_code code = GET_CODE (x);
2079  int i, j;
2080  char *fmt;
2081
2082  switch (code)
2083    {
2084    case CONST:
2085    case LABEL_REF:
2086    case PC:
2087      return 0;
2088
2089    case REG:
2090      return 1;
2091
2092    case MEM:
2093      return ! (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
2094		&& CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)));
2095
2096    case IF_THEN_ELSE:
2097      return (jmp_uses_reg_or_mem (XEXP (x, 1))
2098	      || jmp_uses_reg_or_mem (XEXP (x, 2)));
2099
2100    case PLUS:  case MINUS:  case MULT:
2101      return (jmp_uses_reg_or_mem (XEXP (x, 0))
2102	      || jmp_uses_reg_or_mem (XEXP (x, 1)));
2103
2104    default:
2105      break;
2106    }
2107
2108  fmt = GET_RTX_FORMAT (code);
2109  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2110    {
2111      if (fmt[i] == 'e'
2112	  && jmp_uses_reg_or_mem (XEXP (x, i)))
2113	return 1;
2114
2115      if (fmt[i] == 'E')
2116	for (j = 0; j < XVECLEN (x, i); j++)
2117	  if (jmp_uses_reg_or_mem (XVECEXP (x, i, j)))
2118	    return 1;
2119    }
2120
2121  return 0;
2122}
2123
2124/* Return nonzero if INSN is an indirect jump (aka computed jump).
2125
2126   Tablejumps and casesi insns are not considered indirect jumps;
2127   we can recognize them by a (use (lael_ref)).  */
2128
2129int
2130computed_jump_p (insn)
2131     rtx insn;
2132{
2133  int i;
2134  if (GET_CODE (insn) == JUMP_INSN)
2135    {
2136      rtx pat = PATTERN (insn);
2137
2138      if (GET_CODE (pat) == PARALLEL)
2139	{
2140	  int len = XVECLEN (pat, 0);
2141	  int has_use_labelref = 0;
2142
2143	  for (i = len - 1; i >= 0; i--)
2144	    if (GET_CODE (XVECEXP (pat, 0, i)) == USE
2145		&& (GET_CODE (XEXP (XVECEXP (pat, 0, i), 0))
2146		    == LABEL_REF))
2147	      has_use_labelref = 1;
2148
2149	  if (! has_use_labelref)
2150	    for (i = len - 1; i >= 0; i--)
2151	      if (GET_CODE (XVECEXP (pat, 0, i)) == SET
2152		  && SET_DEST (XVECEXP (pat, 0, i)) == pc_rtx
2153		  && jmp_uses_reg_or_mem (SET_SRC (XVECEXP (pat, 0, i))))
2154		return 1;
2155	}
2156      else if (GET_CODE (pat) == SET
2157	       && SET_DEST (pat) == pc_rtx
2158	       && jmp_uses_reg_or_mem (SET_SRC (pat)))
2159	return 1;
2160    }
2161  return 0;
2162}
2163
2164/* Traverse X via depth-first search, calling F for each
2165   sub-expression (including X itself).  F is also passed the DATA.
2166   If F returns -1, do not traverse sub-expressions, but continue
2167   traversing the rest of the tree.  If F ever returns any other
2168   non-zero value, stop the traversal, and return the value returned
2169   by F.  Otherwise, return 0.  This function does not traverse inside
2170   tree structure that contains RTX_EXPRs, or into sub-expressions
2171   whose format code is `0' since it is not known whether or not those
2172   codes are actually RTL.
2173
2174   This routine is very general, and could (should?) be used to
2175   implement many of the other routines in this file.  */
2176
2177int
2178for_each_rtx (x, f, data)
2179     rtx *x;
2180     rtx_function f;
2181     void *data;
2182{
2183  int result;
2184  int length;
2185  char* format;
2186  int i;
2187
2188  /* Call F on X.  */
2189  result = (*f)(x, data);
2190  if (result == -1)
2191    /* Do not traverse sub-expressions.  */
2192    return 0;
2193  else if (result != 0)
2194    /* Stop the traversal.  */
2195    return result;
2196
2197  if (*x == NULL_RTX)
2198    /* There are no sub-expressions.  */
2199    return 0;
2200
2201  length = GET_RTX_LENGTH (GET_CODE (*x));
2202  format = GET_RTX_FORMAT (GET_CODE (*x));
2203
2204  for (i = 0; i < length; ++i)
2205    {
2206      switch (format[i])
2207	{
2208	case 'e':
2209	  result = for_each_rtx (&XEXP (*x, i), f, data);
2210	  if (result != 0)
2211	    return result;
2212	  break;
2213
2214	case 'V':
2215	case 'E':
2216	  if (XVEC (*x, i) != 0)
2217	    {
2218	      int j;
2219	      for (j = 0; j < XVECLEN (*x, i); ++j)
2220		{
2221		  result = for_each_rtx (&XVECEXP (*x, i, j), f, data);
2222		  if (result != 0)
2223		    return result;
2224		}
2225	    }
2226	  break;
2227
2228	default:
2229	  /* Nothing to do.  */
2230	  break;
2231	}
2232
2233    }
2234
2235  return 0;
2236}
2237
2238/* Searches X for any reference to REGNO, returning the rtx of the
2239   reference found if any.  Otherwise, returns NULL_RTX.  */
2240
2241rtx
2242regno_use_in (regno, x)
2243     int regno;
2244     rtx x;
2245{
2246  register char *fmt;
2247  int i, j;
2248  rtx tem;
2249
2250  if (GET_CODE (x) == REG && REGNO (x) == regno)
2251    return x;
2252
2253  fmt = GET_RTX_FORMAT (GET_CODE (x));
2254  for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2255    {
2256      if (fmt[i] == 'e')
2257	{
2258	  if ((tem = regno_use_in (regno, XEXP (x, i))))
2259	    return tem;
2260	}
2261      else if (fmt[i] == 'E')
2262	for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2263	  if ((tem = regno_use_in (regno , XVECEXP (x, i, j))))
2264	    return tem;
2265    }
2266
2267  return NULL_RTX;
2268}
2269
2270
2271/* Return 1 if X is an autoincrement side effect and the register is
2272   not the stack pointer.  */
2273int
2274auto_inc_p (x)
2275     rtx x;
2276{
2277  switch (GET_CODE (x))
2278    {
2279    case PRE_INC:
2280    case POST_INC:
2281    case PRE_DEC:
2282    case POST_DEC:
2283    case PRE_MODIFY:
2284    case POST_MODIFY:
2285      /* There are no REG_INC notes for SP.  */
2286      if (XEXP (x, 0) != stack_pointer_rtx)
2287	return 1;
2288    default:
2289      break;
2290    }
2291  return 0;
2292}
2293
2294/* Return 1 if the sequence of instructions beginning with FROM and up
2295   to and including TO is safe to move.  If NEW_TO is non-NULL, and
2296   the sequence is not already safe to move, but can be easily
2297   extended to a sequence which is safe, then NEW_TO will point to the
2298   end of the extended sequence.
2299
2300   For now, this function only checks that the region contains whole
2301   exception regiongs, but it could be extended to check additional
2302   conditions as well.  */
2303
2304int
2305insns_safe_to_move_p (from, to, new_to)
2306     rtx from;
2307     rtx to;
2308     rtx *new_to;
2309{
2310  int eh_region_count = 0;
2311  int past_to_p = 0;
2312  rtx r = from;
2313
2314  /* By default, assume the end of the region will be what was
2315     suggested.  */
2316  if (new_to)
2317    *new_to = to;
2318
2319  while (r)
2320    {
2321      if (GET_CODE (r) == NOTE)
2322	{
2323	  switch (NOTE_LINE_NUMBER (r))
2324	    {
2325	    case NOTE_INSN_EH_REGION_BEG:
2326	      ++eh_region_count;
2327	      break;
2328
2329	    case NOTE_INSN_EH_REGION_END:
2330	      if (eh_region_count == 0)
2331		/* This sequence of instructions contains the end of
2332		   an exception region, but not he beginning.  Moving
2333		   it will cause chaos.  */
2334		return 0;
2335
2336	      --eh_region_count;
2337	      break;
2338
2339	    default:
2340	      break;
2341	    }
2342	}
2343      else if (past_to_p)
2344	/* If we've passed TO, and we see a non-note instruction, we
2345	   can't extend the sequence to a movable sequence.  */
2346	return 0;
2347
2348      if (r == to)
2349	{
2350	  if (!new_to)
2351	    /* It's OK to move the sequence if there were matched sets of
2352	       exception region notes.  */
2353	    return eh_region_count == 0;
2354
2355	  past_to_p = 1;
2356	}
2357
2358      /* It's OK to move the sequence if there were matched sets of
2359	 exception region notes.  */
2360      if (past_to_p && eh_region_count == 0)
2361	{
2362	  *new_to = r;
2363	  return 1;
2364	}
2365
2366      /* Go to the next instruction.  */
2367      r = NEXT_INSN (r);
2368    }
2369
2370  return 0;
2371}
2372