rtlanal.c revision 52284
141120Sjdp/* Analyze RTL for C-Compiler
2103976Spst   Copyright (C) 1987, 88, 92-98, 1999 Free Software Foundation, Inc.
341120Sjdp
441120SjdpThis file is part of GNU CC.
541120Sjdp
641120SjdpGNU CC is free software; you can redistribute it and/or modify
741120Sjdpit under the terms of the GNU General Public License as published by
841120Sjdpthe Free Software Foundation; either version 2, or (at your option)
941120Sjdpany later version.
1041120Sjdp
1141120SjdpGNU CC is distributed in the hope that it will be useful,
1241120Sjdpbut WITHOUT ANY WARRANTY; without even the implied warranty of
1341120SjdpMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1441120SjdpGNU General Public License for more details.
1541120Sjdp
1641120SjdpYou should have received a copy of the GNU General Public License
1741120Sjdpalong with GNU CC; see the file COPYING.  If not, write to
1841120Sjdpthe Free Software Foundation, 59 Temple Place - Suite 330,
1941120SjdpBoston, MA 02111-1307, USA.  */
2041120Sjdp
2141120Sjdp
2241120Sjdp#include "config.h"
2341120Sjdp#include "system.h"
2441120Sjdp#include "rtl.h"
2541120Sjdp
2641120Sjdpstatic int rtx_addr_can_trap_p	PROTO((rtx));
2784222Sdillonstatic void reg_set_p_1		PROTO((rtx, rtx));
2884222Sdillonstatic void reg_set_last_1	PROTO((rtx, rtx));
2984222Sdillon
3041120Sjdp
3141120Sjdp/* Forward declarations */
3241120Sjdpstatic int jmp_uses_reg_or_mem		PROTO((rtx));
3341120Sjdp
3441120Sjdp/* Bit flags that specify the machine subtype we are compiling for.
3541120Sjdp   Bits are tested using macros TARGET_... defined in the tm.h file
3641120Sjdp   and set by `-m...' switches.  Must be defined in rtlanal.c.  */
3741120Sjdp
3841120Sjdpint target_flags;
3941120Sjdp
4041120Sjdp/* Return 1 if the value of X is unstable
4141120Sjdp   (would be different at a different point in the program).
4241120Sjdp   The frame pointer, arg pointer, etc. are considered stable
4341120Sjdp   (within one function) and so is anything marked `unchanging'.  */
4441120Sjdp
4541120Sjdpint
4641120Sjdprtx_unstable_p (x)
4741120Sjdp     rtx x;
4841120Sjdp{
4941120Sjdp  register RTX_CODE code = GET_CODE (x);
5041120Sjdp  register int i;
5141120Sjdp  register char *fmt;
5241120Sjdp
5341120Sjdp  if (code == MEM)
54103976Spst    return ! RTX_UNCHANGING_P (x);
5541120Sjdp
5641120Sjdp  if (code == QUEUED)
5741120Sjdp    return 1;
5841120Sjdp
5941120Sjdp  if (code == CONST || code == CONST_INT)
6041120Sjdp    return 0;
6141120Sjdp
6241120Sjdp  if (code == REG)
6341120Sjdp    return ! (REGNO (x) == FRAME_POINTER_REGNUM
6441120Sjdp	      || REGNO (x) == HARD_FRAME_POINTER_REGNUM
6541120Sjdp	      || REGNO (x) == ARG_POINTER_REGNUM
66103976Spst	      || RTX_UNCHANGING_P (x));
67103976Spst
6841120Sjdp  fmt = GET_RTX_FORMAT (code);
6941120Sjdp  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7041120Sjdp    if (fmt[i] == 'e')
7141120Sjdp      if (rtx_unstable_p (XEXP (x, i)))
7241120Sjdp	return 1;
7341120Sjdp  return 0;
7441120Sjdp}
7541120Sjdp
7641120Sjdp/* Return 1 if X has a value that can vary even between two
7741120Sjdp   executions of the program.  0 means X can be compared reliably
7841120Sjdp   against certain constants or near-constants.
79103976Spst   The frame pointer and the arg pointer are considered constant.  */
80103976Spst
8141120Sjdpint
8241120Sjdprtx_varies_p (x)
8341120Sjdp     rtx x;
8441120Sjdp{
8541120Sjdp  register RTX_CODE code = GET_CODE (x);
8641120Sjdp  register int i;
8741120Sjdp  register char *fmt;
8841120Sjdp
8941120Sjdp  switch (code)
9041120Sjdp    {
9141120Sjdp    case MEM:
9241120Sjdp    case QUEUED:
9341120Sjdp      return 1;
9441120Sjdp
9541120Sjdp    case CONST:
9641120Sjdp    case CONST_INT:
9741120Sjdp    case CONST_DOUBLE:
9841120Sjdp    case SYMBOL_REF:
9941120Sjdp    case LABEL_REF:
10041120Sjdp      return 0;
10141120Sjdp
10241120Sjdp    case REG:
10341120Sjdp      /* Note that we have to test for the actual rtx used for the frame
10441120Sjdp	 and arg pointers and not just the register number in case we have
10541120Sjdp	 eliminated the frame and/or arg pointer and are using it
10641120Sjdp	 for pseudos.  */
10741120Sjdp      return ! (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
10841120Sjdp		|| x == arg_pointer_rtx || x == pic_offset_table_rtx);
10941120Sjdp
11041120Sjdp    case LO_SUM:
11141120Sjdp      /* The operand 0 of a LO_SUM is considered constant
11241120Sjdp	 (in fact is it related specifically to operand 1).  */
11341120Sjdp      return rtx_varies_p (XEXP (x, 1));
11441120Sjdp
11541120Sjdp    default:
11641120Sjdp      break;
11741120Sjdp    }
11841120Sjdp
11941120Sjdp  fmt = GET_RTX_FORMAT (code);
12041120Sjdp  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12141120Sjdp    if (fmt[i] == 'e')
12241120Sjdp      if (rtx_varies_p (XEXP (x, i)))
12341120Sjdp	return 1;
12441120Sjdp  return 0;
12541120Sjdp}
12641120Sjdp
12741120Sjdp/* Return 0 if the use of X as an address in a MEM can cause a trap.  */
12841120Sjdp
12941120Sjdpstatic int
13041120Sjdprtx_addr_can_trap_p (x)
13141120Sjdp     register rtx x;
13241120Sjdp{
13341120Sjdp  register enum rtx_code code = GET_CODE (x);
13441120Sjdp
13541120Sjdp  switch (code)
13641120Sjdp    {
13741120Sjdp    case SYMBOL_REF:
13841120Sjdp    case LABEL_REF:
13941120Sjdp      /* SYMBOL_REF is problematic due to the possible presence of
14041120Sjdp	 a #pragma weak, but to say that loads from symbols can trap is
14141120Sjdp	 *very* costly.  It's not at all clear what's best here.  For
14241120Sjdp	 now, we ignore the impact of #pragma weak.  */
143103976Spst      return 0;
14441120Sjdp
145103976Spst    case REG:
14641120Sjdp      /* As in rtx_varies_p, we have to use the actual rtx, not reg number.  */
147103976Spst      return ! (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
148103976Spst		|| x == stack_pointer_rtx || x == arg_pointer_rtx);
149103976Spst
150103976Spst    case CONST:
151103976Spst      return rtx_addr_can_trap_p (XEXP (x, 0));
152103976Spst
15341120Sjdp    case PLUS:
154103976Spst      /* An address is assumed not to trap if it is an address that can't
155103976Spst	 trap plus a constant integer.  */
156103976Spst      return (rtx_addr_can_trap_p (XEXP (x, 0))
157103976Spst	      || GET_CODE (XEXP (x, 1)) != CONST_INT);
158103976Spst
159103976Spst    case LO_SUM:
16041120Sjdp      return rtx_addr_can_trap_p (XEXP (x, 1));
161103976Spst
162103976Spst    default:
16341120Sjdp      break;
164103976Spst    }
165103976Spst
16641120Sjdp  /* If it isn't one of the case above, it can cause a trap.  */
167103976Spst  return 1;
168103976Spst}
169103976Spst
170103976Spst/* Return 1 if X refers to a memory location whose address
17141120Sjdp   cannot be compared reliably with constant addresses,
172103976Spst   or if X refers to a BLKmode memory object.  */
17341120Sjdp
174103976Spstint
175103976Spstrtx_addr_varies_p (x)
17641120Sjdp     rtx x;
177103976Spst{
178103976Spst  register enum rtx_code code;
179103976Spst  register int i;
180103976Spst  register char *fmt;
181103976Spst
182103976Spst  if (x == 0)
183103976Spst    return 0;
184103976Spst
185103976Spst  code = GET_CODE (x);
186103976Spst  if (code == MEM)
187103976Spst    return GET_MODE (x) == BLKmode || rtx_varies_p (XEXP (x, 0));
188103976Spst
189103976Spst  fmt = GET_RTX_FORMAT (code);
190103976Spst  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
191103976Spst    if (fmt[i] == 'e')
192103976Spst      {
193103976Spst	if (rtx_addr_varies_p (XEXP (x, i)))
194103976Spst	  return 1;
195103976Spst      }
196103976Spst    else if (fmt[i] == 'E')
197103976Spst      {
198103976Spst	int j;
199103976Spst	for (j = 0; j < XVECLEN (x, i); j++)
200103976Spst	  if (rtx_addr_varies_p (XVECEXP (x, i, j)))
201103976Spst	    return 1;
202103976Spst      }
203103976Spst  return 0;
204103976Spst}
205103976Spst
206103976Spst/* Return the value of the integer term in X, if one is apparent;
207103976Spst   otherwise return 0.
208103976Spst   Only obvious integer terms are detected.
209103976Spst   This is used in cse.c with the `related_value' field.*/
21041120Sjdp
211103976SpstHOST_WIDE_INT
212103976Spstget_integer_term (x)
21341120Sjdp     rtx x;
214200399Ssyrinx{
215200399Ssyrinx  if (GET_CODE (x) == CONST)
21641120Sjdp    x = XEXP (x, 0);
217103976Spst
218103976Spst  if (GET_CODE (x) == MINUS
219103976Spst      && GET_CODE (XEXP (x, 1)) == CONST_INT)
22041120Sjdp    return - INTVAL (XEXP (x, 1));
221103976Spst  if (GET_CODE (x) == PLUS
22241120Sjdp      && GET_CODE (XEXP (x, 1)) == CONST_INT)
22341120Sjdp    return INTVAL (XEXP (x, 1));
224103976Spst  return 0;
22541120Sjdp}
22641120Sjdp
22741120Sjdp/* If X is a constant, return the value sans apparent integer term;
22841120Sjdp   otherwise return 0.
22941120Sjdp   Only obvious integer terms are detected.  */
23041120Sjdp
23141120Sjdprtx
23241120Sjdpget_related_value (x)
23341120Sjdp     rtx x;
23441120Sjdp{
23541120Sjdp  if (GET_CODE (x) != CONST)
23641120Sjdp    return 0;
23741120Sjdp  x = XEXP (x, 0);
23841120Sjdp  if (GET_CODE (x) == PLUS
23941120Sjdp      && GET_CODE (XEXP (x, 1)) == CONST_INT)
24041120Sjdp    return XEXP (x, 0);
24141120Sjdp  else if (GET_CODE (x) == MINUS
24241120Sjdp	   && GET_CODE (XEXP (x, 1)) == CONST_INT)
24341120Sjdp    return XEXP (x, 0);
24441120Sjdp  return 0;
24541120Sjdp}
24641120Sjdp
24741120Sjdp/* Nonzero if register REG appears somewhere within IN.
24841120Sjdp   Also works if REG is not a register; in this case it checks
24941120Sjdp   for a subexpression of IN that is Lisp "equal" to REG.  */
25041120Sjdp
25141120Sjdpint
25241120Sjdpreg_mentioned_p (reg, in)
25341120Sjdp     register rtx reg, in;
25441120Sjdp{
25541120Sjdp  register char *fmt;
25641120Sjdp  register int i;
25741120Sjdp  register enum rtx_code code;
25841120Sjdp
25941120Sjdp  if (in == 0)
26041120Sjdp    return 0;
261141918Sstefanf
26241120Sjdp  if (reg == in)
26341120Sjdp    return 1;
26441120Sjdp
26541120Sjdp  if (GET_CODE (in) == LABEL_REF)
26641120Sjdp    return reg == XEXP (in, 0);
26741120Sjdp
26841120Sjdp  code = GET_CODE (in);
26941120Sjdp
27041120Sjdp  switch (code)
27141120Sjdp    {
27241120Sjdp      /* Compare registers by number.  */
27341120Sjdp    case REG:
27441120Sjdp      return GET_CODE (reg) == REG && REGNO (in) == REGNO (reg);
27541120Sjdp
27641120Sjdp      /* These codes have no constituent expressions
27741120Sjdp	 and are unique.  */
27841120Sjdp    case SCRATCH:
27941120Sjdp    case CC0:
28041120Sjdp    case PC:
28141120Sjdp      return 0;
28241120Sjdp
28341120Sjdp    case CONST_INT:
28441120Sjdp      return GET_CODE (reg) == CONST_INT && INTVAL (in) == INTVAL (reg);
28541120Sjdp
28641120Sjdp    case CONST_DOUBLE:
28741120Sjdp      /* These are kept unique for a given value.  */
28841120Sjdp      return 0;
28941120Sjdp
29041120Sjdp    default:
29141120Sjdp      break;
29241120Sjdp    }
29341120Sjdp
29441120Sjdp  if (GET_CODE (reg) == code && rtx_equal_p (reg, in))
29541120Sjdp    return 1;
29641120Sjdp
29741120Sjdp  fmt = GET_RTX_FORMAT (code);
29841120Sjdp
29941120Sjdp  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
30041120Sjdp    {
30141120Sjdp      if (fmt[i] == 'E')
30241120Sjdp	{
30341120Sjdp	  register int j;
30441120Sjdp	  for (j = XVECLEN (in, i) - 1; j >= 0; j--)
30541120Sjdp	    if (reg_mentioned_p (reg, XVECEXP (in, i, j)))
30641120Sjdp	      return 1;
30741120Sjdp	}
30841120Sjdp      else if (fmt[i] == 'e'
30941120Sjdp	       && reg_mentioned_p (reg, XEXP (in, i)))
31041120Sjdp	return 1;
31141120Sjdp    }
31241120Sjdp  return 0;
31341120Sjdp}
31441120Sjdp
31541120Sjdp/* Return 1 if in between BEG and END, exclusive of BEG and END, there is
31641120Sjdp   no CODE_LABEL insn.  */
31741120Sjdp
31841120Sjdpint
31941120Sjdpno_labels_between_p (beg, end)
32041120Sjdp     rtx beg, end;
32141120Sjdp{
32241120Sjdp  register rtx p;
32341120Sjdp  for (p = NEXT_INSN (beg); p != end; p = NEXT_INSN (p))
32441120Sjdp    if (GET_CODE (p) == CODE_LABEL)
32541120Sjdp      return 0;
32641120Sjdp  return 1;
32741120Sjdp}
32841120Sjdp
32941120Sjdp/* Return 1 if in between BEG and END, exclusive of BEG and END, there is
33041120Sjdp   no JUMP_INSN insn.  */
33141120Sjdp
33241120Sjdpint
33341120Sjdpno_jumps_between_p (beg, end)
33441120Sjdp     rtx beg, end;
33541120Sjdp{
33641120Sjdp  register rtx p;
33741120Sjdp  for (p = NEXT_INSN (beg); p != end; p = NEXT_INSN (p))
33841120Sjdp    if (GET_CODE (p) == JUMP_INSN)
33941120Sjdp      return 0;
34041120Sjdp  return 1;
34141120Sjdp}
34241120Sjdp
34341120Sjdp/* Nonzero if register REG is used in an insn between
34441120Sjdp   FROM_INSN and TO_INSN (exclusive of those two).  */
34541120Sjdp
34641120Sjdpint
34741120Sjdpreg_used_between_p (reg, from_insn, to_insn)
34841120Sjdp     rtx reg, from_insn, to_insn;
34941120Sjdp{
35041120Sjdp  register rtx insn;
35141120Sjdp
35241120Sjdp  if (from_insn == to_insn)
35341120Sjdp    return 0;
35441120Sjdp
35541120Sjdp  for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
35641120Sjdp    if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
35741120Sjdp	&& (reg_overlap_mentioned_p (reg, PATTERN (insn))
35841120Sjdp	   || (GET_CODE (insn) == CALL_INSN
35941120Sjdp	      && (find_reg_fusage (insn, USE, reg)
36041120Sjdp		  || find_reg_fusage (insn, CLOBBER, reg)))))
36141120Sjdp      return 1;
36241120Sjdp  return 0;
36341120Sjdp}
36441120Sjdp
36541120Sjdp/* Nonzero if the old value of X, a register, is referenced in BODY.  If X
36641120Sjdp   is entirely replaced by a new value and the only use is as a SET_DEST,
36741120Sjdp   we do not consider it a reference.  */
36841120Sjdp
36941120Sjdpint
37041120Sjdpreg_referenced_p (x, body)
37141120Sjdp     rtx x;
37241120Sjdp     rtx body;
37341120Sjdp{
37441120Sjdp  int i;
37541120Sjdp
37641120Sjdp  switch (GET_CODE (body))
37741120Sjdp    {
37841120Sjdp    case SET:
37941120Sjdp      if (reg_overlap_mentioned_p (x, SET_SRC (body)))
38041120Sjdp	return 1;
38141120Sjdp
38241120Sjdp      /* If the destination is anything other than CC0, PC, a REG or a SUBREG
38341120Sjdp	 of a REG that occupies all of the REG, the insn references X if
38441120Sjdp	 it is mentioned in the destination.  */
38541120Sjdp      if (GET_CODE (SET_DEST (body)) != CC0
38641120Sjdp	  && GET_CODE (SET_DEST (body)) != PC
38741120Sjdp	  && GET_CODE (SET_DEST (body)) != REG
38841120Sjdp	  && ! (GET_CODE (SET_DEST (body)) == SUBREG
38941120Sjdp		&& GET_CODE (SUBREG_REG (SET_DEST (body))) == REG
39041120Sjdp		&& (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (body))))
39141120Sjdp		      + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
39241120Sjdp		    == ((GET_MODE_SIZE (GET_MODE (SET_DEST (body)))
39341120Sjdp			 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
39441120Sjdp	  && reg_overlap_mentioned_p (x, SET_DEST (body)))
39541120Sjdp	return 1;
39641120Sjdp      return 0;
39741120Sjdp
39841120Sjdp    case ASM_OPERANDS:
39941120Sjdp      for (i = ASM_OPERANDS_INPUT_LENGTH (body) - 1; i >= 0; i--)
40041120Sjdp	if (reg_overlap_mentioned_p (x, ASM_OPERANDS_INPUT (body, i)))
40141120Sjdp	  return 1;
40241120Sjdp      return 0;
40341120Sjdp
40441120Sjdp    case CALL:
40541120Sjdp    case USE:
40641120Sjdp      return reg_overlap_mentioned_p (x, body);
40741120Sjdp
40841120Sjdp    case TRAP_IF:
40941120Sjdp      return reg_overlap_mentioned_p (x, TRAP_CONDITION (body));
41041120Sjdp
41141120Sjdp    case UNSPEC:
41241120Sjdp    case UNSPEC_VOLATILE:
41341120Sjdp    case PARALLEL:
41441120Sjdp      for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
41541120Sjdp	if (reg_referenced_p (x, XVECEXP (body, 0, i)))
41641120Sjdp	  return 1;
41741120Sjdp      return 0;
41841120Sjdp
41941120Sjdp    default:
42041120Sjdp      return 0;
42141120Sjdp    }
42241120Sjdp}
42341120Sjdp
42441120Sjdp/* Nonzero if register REG is referenced in an insn between
42541120Sjdp   FROM_INSN and TO_INSN (exclusive of those two).  Sets of REG do
42641120Sjdp   not count.  */
42741120Sjdp
42841120Sjdpint
42941120Sjdpreg_referenced_between_p (reg, from_insn, to_insn)
43041120Sjdp     rtx reg, from_insn, to_insn;
43141120Sjdp{
43241120Sjdp  register rtx insn;
43341120Sjdp
43441120Sjdp  if (from_insn == to_insn)
43541120Sjdp    return 0;
43641120Sjdp
43741120Sjdp  for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
43841120Sjdp    if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
43941120Sjdp	&& (reg_referenced_p (reg, PATTERN (insn))
44041120Sjdp	   || (GET_CODE (insn) == CALL_INSN
44141120Sjdp	      && find_reg_fusage (insn, USE, reg))))
44241120Sjdp      return 1;
44341120Sjdp  return 0;
44441120Sjdp}
445103976Spst
446103976Spst/* Nonzero if register REG is set or clobbered in an insn between
447103976Spst   FROM_INSN and TO_INSN (exclusive of those two).  */
448103976Spst
449103976Spstint
450103976Spstreg_set_between_p (reg, from_insn, to_insn)
451103976Spst     rtx reg, from_insn, to_insn;
452103976Spst{
45341120Sjdp  register rtx insn;
45441120Sjdp
45541120Sjdp  if (from_insn == to_insn)
45641120Sjdp    return 0;
45741120Sjdp
45841120Sjdp  for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
459103976Spst    if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
460103976Spst	&& reg_set_p (reg, insn))
46141120Sjdp      return 1;
46241120Sjdp  return 0;
463103976Spst}
464103976Spst
465103976Spst/* Internals of reg_set_between_p.  */
46641120Sjdp
46741120Sjdpstatic rtx reg_set_reg;
46841120Sjdpstatic int reg_set_flag;
46941120Sjdp
47041120Sjdpstatic void
47141120Sjdpreg_set_p_1 (x, pat)
47241120Sjdp     rtx x;
47341120Sjdp     rtx pat ATTRIBUTE_UNUSED;
47441120Sjdp{
47541120Sjdp  /* We don't want to return 1 if X is a MEM that contains a register
47641120Sjdp     within REG_SET_REG.  */
47741120Sjdp
47841120Sjdp  if ((GET_CODE (x) != MEM)
47941120Sjdp      && reg_overlap_mentioned_p (reg_set_reg, x))
48041120Sjdp    reg_set_flag = 1;
48141120Sjdp}
48241120Sjdp
48341120Sjdpint
48441120Sjdpreg_set_p (reg, insn)
48541120Sjdp     rtx reg, insn;
48641120Sjdp{
48741120Sjdp  rtx body = insn;
48841120Sjdp
48941120Sjdp  /* We can be passed an insn or part of one.  If we are passed an insn,
49041120Sjdp     check if a side-effect of the insn clobbers REG.  */
49141120Sjdp  if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
49241120Sjdp    {
49341120Sjdp      if (FIND_REG_INC_NOTE (insn, reg)
49441120Sjdp	  || (GET_CODE (insn) == CALL_INSN
49541120Sjdp	      /* We'd like to test call_used_regs here, but rtlanal.c can't
49641120Sjdp		 reference that variable due to its use in genattrtab.  So
49741120Sjdp		 we'll just be more conservative.
49841120Sjdp
49941120Sjdp		 ??? Unless we could ensure that the CALL_INSN_FUNCTION_USAGE
50041120Sjdp		 information holds all clobbered registers.  */
50141120Sjdp	      && ((GET_CODE (reg) == REG
50241120Sjdp		   && REGNO (reg) < FIRST_PSEUDO_REGISTER)
50341120Sjdp		  || GET_CODE (reg) == MEM
50441120Sjdp		  || find_reg_fusage (insn, CLOBBER, reg))))
50541120Sjdp	return 1;
50641120Sjdp
50741120Sjdp      body = PATTERN (insn);
50841120Sjdp    }
50941120Sjdp
51041120Sjdp  reg_set_reg = reg;
51141120Sjdp  reg_set_flag = 0;
51241120Sjdp  note_stores (body, reg_set_p_1);
51341120Sjdp  return reg_set_flag;
51441120Sjdp}
51541120Sjdp
51641120Sjdp/* Similar to reg_set_between_p, but check all registers in X.  Return 0
51741120Sjdp   only if none of them are modified between START and END.  Do not
51841120Sjdp   consider non-registers one way or the other.  */
51941120Sjdp
52041120Sjdpint
52141120Sjdpregs_set_between_p (x, start, end)
52241120Sjdp     rtx x;
52341120Sjdp     rtx start, end;
52441120Sjdp{
52541120Sjdp  enum rtx_code code = GET_CODE (x);
52641120Sjdp  char *fmt;
52741120Sjdp  int i, j;
52841120Sjdp
52941120Sjdp  switch (code)
53041120Sjdp    {
53141120Sjdp    case CONST_INT:
53241120Sjdp    case CONST_DOUBLE:
53341120Sjdp    case CONST:
53441120Sjdp    case SYMBOL_REF:
53541120Sjdp    case LABEL_REF:
53641120Sjdp    case PC:
53741120Sjdp    case CC0:
53841120Sjdp      return 0;
53941120Sjdp
54041120Sjdp    case REG:
54141120Sjdp      return reg_set_between_p (x, start, end);
54241120Sjdp
54341120Sjdp    default:
54441120Sjdp      break;
54541120Sjdp    }
54641120Sjdp
54741120Sjdp  fmt = GET_RTX_FORMAT (code);
54841120Sjdp  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
54941120Sjdp    {
550103976Spst      if (fmt[i] == 'e' && regs_set_between_p (XEXP (x, i), start, end))
55141120Sjdp	return 1;
55241120Sjdp
55341120Sjdp      else if (fmt[i] == 'E')
55441120Sjdp	for (j = XVECLEN (x, i) - 1; j >= 0; j--)
55541120Sjdp	  if (regs_set_between_p (XVECEXP (x, i, j), start, end))
55641120Sjdp	    return 1;
55741120Sjdp    }
55841120Sjdp
55941120Sjdp  return 0;
56041120Sjdp}
56141120Sjdp
56241120Sjdp/* Similar to reg_set_between_p, but check all registers in X.  Return 0
56341120Sjdp   only if none of them are modified between START and END.  Return 1 if
56441120Sjdp   X contains a MEM; this routine does not perform any memory aliasing.  */
565103976Spst
566103976Spstint
567103976Spstmodified_between_p (x, start, end)
56841120Sjdp     rtx x;
56941120Sjdp     rtx start, end;
57041120Sjdp{
57141120Sjdp  enum rtx_code code = GET_CODE (x);
572103976Spst  char *fmt;
573103976Spst  int i, j;
57441120Sjdp
57541120Sjdp  switch (code)
57641120Sjdp    {
577103976Spst    case CONST_INT:
578103976Spst    case CONST_DOUBLE:
579103976Spst    case CONST:
58041120Sjdp    case SYMBOL_REF:
58141120Sjdp    case LABEL_REF:
58241120Sjdp      return 0;
58341120Sjdp
58441120Sjdp    case PC:
58541120Sjdp    case CC0:
58641120Sjdp      return 1;
58741120Sjdp
58841120Sjdp    case MEM:
58941120Sjdp      /* If the memory is not constant, assume it is modified.  If it is
59041120Sjdp	 constant, we still have to check the address.  */
59141120Sjdp      if (! RTX_UNCHANGING_P (x))
59241120Sjdp	return 1;
59341120Sjdp      break;
59441120Sjdp
59541120Sjdp    case REG:
59641120Sjdp      return reg_set_between_p (x, start, end);
59741120Sjdp
59841120Sjdp    default:
59941120Sjdp      break;
60041120Sjdp    }
60141120Sjdp
60241120Sjdp  fmt = GET_RTX_FORMAT (code);
60341120Sjdp  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
60441120Sjdp    {
60541120Sjdp      if (fmt[i] == 'e' && modified_between_p (XEXP (x, i), start, end))
60641120Sjdp	return 1;
60741120Sjdp
60841120Sjdp      if (fmt[i] == 'E')
60941120Sjdp	for (j = XVECLEN (x, i) - 1; j >= 0; j--)
61041120Sjdp	  if (modified_between_p (XVECEXP (x, i, j), start, end))
61141120Sjdp	    return 1;
61241120Sjdp    }
61341120Sjdp
61441120Sjdp  return 0;
61541120Sjdp}
61641120Sjdp
61741120Sjdp/* Similar to reg_set_p, but check all registers in X.  Return 0 only if none
61841120Sjdp   of them are modified in INSN.  Return 1 if X contains a MEM; this routine
61941120Sjdp   does not perform any memory aliasing.  */
62041120Sjdp
62141120Sjdpint
62241120Sjdpmodified_in_p (x, insn)
62341120Sjdp     rtx x;
62441120Sjdp     rtx insn;
62541120Sjdp{
62641120Sjdp  enum rtx_code code = GET_CODE (x);
62741120Sjdp  char *fmt;
62841120Sjdp  int i, j;
62941120Sjdp
630103976Spst  switch (code)
631103976Spst    {
632103976Spst    case CONST_INT:
63341120Sjdp    case CONST_DOUBLE:
63441120Sjdp    case CONST:
63541120Sjdp    case SYMBOL_REF:
63641120Sjdp    case LABEL_REF:
63741120Sjdp      return 0;
63841120Sjdp
63941120Sjdp    case PC:
64041120Sjdp    case CC0:
64141120Sjdp      return 1;
64241120Sjdp
64341120Sjdp    case MEM:
64441120Sjdp      /* If the memory is not constant, assume it is modified.  If it is
64541120Sjdp	 constant, we still have to check the address.  */
64641120Sjdp      if (! RTX_UNCHANGING_P (x))
64741120Sjdp	return 1;
64841120Sjdp      break;
64941120Sjdp
65041120Sjdp    case REG:
65141120Sjdp      return reg_set_p (x, insn);
65241120Sjdp
65341120Sjdp    default:
65441120Sjdp      break;
65541120Sjdp    }
65641120Sjdp
65741120Sjdp  fmt = GET_RTX_FORMAT (code);
65841120Sjdp  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
65941120Sjdp    {
66041120Sjdp      if (fmt[i] == 'e' && modified_in_p (XEXP (x, i), insn))
66141120Sjdp	return 1;
66241120Sjdp
66341120Sjdp      if (fmt[i] == 'E')
66441120Sjdp	for (j = XVECLEN (x, i) - 1; j >= 0; j--)
66541120Sjdp	  if (modified_in_p (XVECEXP (x, i, j), insn))
66641120Sjdp	    return 1;
66741120Sjdp    }
66841120Sjdp
66941120Sjdp  return 0;
67041120Sjdp}
67141120Sjdp
67241120Sjdp/* Given an INSN, return a SET expression if this insn has only a single SET.
67341120Sjdp   It may also have CLOBBERs, USEs, or SET whose output
67441120Sjdp   will not be used, which we ignore.  */
67541120Sjdp
67641120Sjdprtx
67741120Sjdpsingle_set (insn)
67841120Sjdp     rtx insn;
67941120Sjdp{
68041120Sjdp  rtx set;
68141120Sjdp  int i;
68241120Sjdp
68341120Sjdp  if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
68441120Sjdp    return 0;
68541120Sjdp
68641120Sjdp  if (GET_CODE (PATTERN (insn)) == SET)
68741120Sjdp    return PATTERN (insn);
68841120Sjdp
68941120Sjdp  else if (GET_CODE (PATTERN (insn)) == PARALLEL)
69041120Sjdp    {
69141120Sjdp      for (i = 0, set = 0; i < XVECLEN (PATTERN (insn), 0); i++)
69241120Sjdp	if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET
69341120Sjdp	    && (! find_reg_note (insn, REG_UNUSED,
69441120Sjdp				 SET_DEST (XVECEXP (PATTERN (insn), 0, i)))
69541120Sjdp		|| side_effects_p (XVECEXP (PATTERN (insn), 0, i))))
69641120Sjdp	  {
69741120Sjdp	    if (set)
69841120Sjdp	      return 0;
69941120Sjdp	    else
70041120Sjdp	      set = XVECEXP (PATTERN (insn), 0, i);
70141120Sjdp	  }
70241120Sjdp      return set;
70341120Sjdp    }
70441120Sjdp
70541120Sjdp  return 0;
70641120Sjdp}
70741120Sjdp
70841120Sjdp/* Given an INSN, return nonzero if it has more than one SET, else return
70941120Sjdp   zero.  */
71041120Sjdp
71141120Sjdpint
71241120Sjdpmultiple_sets (insn)
71341120Sjdp     rtx insn;
71441120Sjdp{
71541120Sjdp  int found;
71641120Sjdp  int i;
71741120Sjdp
71841120Sjdp  /* INSN must be an insn.  */
71941120Sjdp  if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
72041120Sjdp    return 0;
72141120Sjdp
72241120Sjdp  /* Only a PARALLEL can have multiple SETs.  */
72341120Sjdp  if (GET_CODE (PATTERN (insn)) == PARALLEL)
72441120Sjdp    {
72541120Sjdp      for (i = 0, found = 0; i < XVECLEN (PATTERN (insn), 0); i++)
72641120Sjdp	if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
72741120Sjdp	  {
72841120Sjdp	    /* If we have already found a SET, then return now.  */
72941120Sjdp	    if (found)
73041120Sjdp	      return 1;
73141120Sjdp	    else
73241120Sjdp	      found = 1;
73341120Sjdp	  }
73441120Sjdp    }
73541120Sjdp
73641120Sjdp  /* Either zero or one SET.  */
73741120Sjdp  return 0;
73841120Sjdp}
73941120Sjdp
74041120Sjdp/* Return the last thing that X was assigned from before *PINSN.  Verify that
74141120Sjdp   the object is not modified up to VALID_TO.  If it was, if we hit
74241120Sjdp   a partial assignment to X, or hit a CODE_LABEL first, return X.  If we
74341120Sjdp   found an assignment, update *PINSN to point to it.
74441120Sjdp   ALLOW_HWREG is set to 1 if hardware registers are allowed to be the src.  */
74541120Sjdp
74641120Sjdprtx
74741120Sjdpfind_last_value (x, pinsn, valid_to, allow_hwreg)
74841120Sjdp     rtx x;
74941120Sjdp     rtx *pinsn;
75041120Sjdp     rtx valid_to;
75141120Sjdp     int allow_hwreg;
75241120Sjdp{
75341120Sjdp  rtx p;
75441120Sjdp
75541120Sjdp  for (p = PREV_INSN (*pinsn); p && GET_CODE (p) != CODE_LABEL;
75641120Sjdp       p = PREV_INSN (p))
75741120Sjdp    if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
75841120Sjdp      {
75941120Sjdp	rtx set = single_set (p);
76041120Sjdp	rtx note = find_reg_note (p, REG_EQUAL, NULL_RTX);
76141120Sjdp
76241120Sjdp	if (set && rtx_equal_p (x, SET_DEST (set)))
76341120Sjdp	  {
76441120Sjdp	    rtx src = SET_SRC (set);
76541120Sjdp
76641120Sjdp	    if (note && GET_CODE (XEXP (note, 0)) != EXPR_LIST)
76741120Sjdp	      src = XEXP (note, 0);
76841120Sjdp
76941120Sjdp	    if (! modified_between_p (src, PREV_INSN (p), valid_to)
77056141Sjdp		/* Reject hard registers because we don't usually want
77141120Sjdp		   to use them; we'd rather use a pseudo.  */
77241120Sjdp		&& (! (GET_CODE (src) == REG
77341120Sjdp		      && REGNO (src) < FIRST_PSEUDO_REGISTER) || allow_hwreg))
77441120Sjdp	      {
77541120Sjdp		*pinsn = p;
77641120Sjdp		return src;
77741120Sjdp	      }
77841120Sjdp	  }
77941120Sjdp
78041120Sjdp	/* If set in non-simple way, we don't have a value.  */
78141120Sjdp	if (reg_set_p (x, p))
78241120Sjdp	  break;
78341120Sjdp      }
78441120Sjdp
78541120Sjdp  return x;
78641120Sjdp}
78741120Sjdp
78841120Sjdp/* Return nonzero if register in range [REGNO, ENDREGNO)
78941120Sjdp   appears either explicitly or implicitly in X
79041120Sjdp   other than being stored into.
79141120Sjdp
79241120Sjdp   References contained within the substructure at LOC do not count.
79341120Sjdp   LOC may be zero, meaning don't ignore anything.  */
79441120Sjdp
79541120Sjdpint
79641120Sjdprefers_to_regno_p (regno, endregno, x, loc)
79741120Sjdp     int regno, endregno;
79841120Sjdp     rtx x;
79941120Sjdp     rtx *loc;
800103976Spst{
80141120Sjdp  register int i;
80241120Sjdp  register RTX_CODE code;
80341120Sjdp  register char *fmt;
80441120Sjdp
80541120Sjdp repeat:
80641120Sjdp  /* The contents of a REG_NONNEG note is always zero, so we must come here
80741120Sjdp     upon repeat in case the last REG_NOTE is a REG_NONNEG note.  */
80841120Sjdp  if (x == 0)
80941120Sjdp    return 0;
81041120Sjdp
81141120Sjdp  code = GET_CODE (x);
81241120Sjdp
81341120Sjdp  switch (code)
814103976Spst    {
815103976Spst    case REG:
816103976Spst      i = REGNO (x);
817103976Spst
818103976Spst      /* If we modifying the stack, frame, or argument pointer, it will
81941120Sjdp	 clobber a virtual register.  In fact, we could be more precise,
82041120Sjdp	 but it isn't worth it.  */
82141120Sjdp      if ((i == STACK_POINTER_REGNUM
82241120Sjdp#if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
82341120Sjdp	   || i == ARG_POINTER_REGNUM
82441120Sjdp#endif
82541120Sjdp	   || i == FRAME_POINTER_REGNUM)
82641120Sjdp	  && regno >= FIRST_VIRTUAL_REGISTER && regno <= LAST_VIRTUAL_REGISTER)
82741120Sjdp	return 1;
82841120Sjdp
82941120Sjdp      return (endregno > i
83041120Sjdp	      && regno < i + (i < FIRST_PSEUDO_REGISTER
83141120Sjdp			      ? HARD_REGNO_NREGS (i, GET_MODE (x))
83241120Sjdp			      : 1));
83341120Sjdp
83441120Sjdp    case SUBREG:
83541120Sjdp      /* If this is a SUBREG of a hard reg, we can see exactly which
83641120Sjdp	 registers are being modified.  Otherwise, handle normally.  */
83741120Sjdp      if (GET_CODE (SUBREG_REG (x)) == REG
83841120Sjdp	  && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER)
83941120Sjdp	{
84041120Sjdp	  int inner_regno = REGNO (SUBREG_REG (x)) + SUBREG_WORD (x);
84141120Sjdp	  int inner_endregno
84241120Sjdp	    = inner_regno + (inner_regno < FIRST_PSEUDO_REGISTER
84365222Sache			     ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
84441120Sjdp
84541120Sjdp	  return endregno > inner_regno && regno < inner_endregno;
84641120Sjdp	}
84741120Sjdp      break;
84841120Sjdp
84941120Sjdp    case CLOBBER:
85041120Sjdp    case SET:
85141120Sjdp      if (&SET_DEST (x) != loc
85241120Sjdp	  /* Note setting a SUBREG counts as referring to the REG it is in for
85341120Sjdp	     a pseudo but not for hard registers since we can
85441120Sjdp	     treat each word individually.  */
85541120Sjdp	  && ((GET_CODE (SET_DEST (x)) == SUBREG
85641120Sjdp	       && loc != &SUBREG_REG (SET_DEST (x))
857103976Spst	       && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG
85841120Sjdp	       && REGNO (SUBREG_REG (SET_DEST (x))) >= FIRST_PSEUDO_REGISTER
85941120Sjdp	       && refers_to_regno_p (regno, endregno,
86041120Sjdp				     SUBREG_REG (SET_DEST (x)), loc))
86141120Sjdp	      || (GET_CODE (SET_DEST (x)) != REG
86241120Sjdp		  && refers_to_regno_p (regno, endregno, SET_DEST (x), loc))))
86341120Sjdp	return 1;
86441120Sjdp
86541120Sjdp      if (code == CLOBBER || loc == &SET_SRC (x))
86641120Sjdp	return 0;
86741120Sjdp      x = SET_SRC (x);
86841120Sjdp      goto repeat;
86941120Sjdp
87041120Sjdp    default:
87141120Sjdp      break;
87241120Sjdp    }
87341120Sjdp
87441120Sjdp  /* X does not match, so try its subexpressions.  */
87541120Sjdp
87641120Sjdp  fmt = GET_RTX_FORMAT (code);
87741120Sjdp  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
87841120Sjdp    {
87941120Sjdp      if (fmt[i] == 'e' && loc != &XEXP (x, i))
88041120Sjdp	{
88141120Sjdp	  if (i == 0)
88241120Sjdp	    {
88341120Sjdp	      x = XEXP (x, 0);
88441120Sjdp	      goto repeat;
88541120Sjdp	    }
88641120Sjdp	  else
88741120Sjdp	    if (refers_to_regno_p (regno, endregno, XEXP (x, i), loc))
88841120Sjdp	      return 1;
88965222Sache	}
89065222Sache      else if (fmt[i] == 'E')
89165222Sache	{
89241120Sjdp	  register int j;
89341120Sjdp	  for (j = XVECLEN (x, i) - 1; j >=0; j--)
89441120Sjdp	    if (loc != &XVECEXP (x, i, j)
89541120Sjdp		&& refers_to_regno_p (regno, endregno, XVECEXP (x, i, j), loc))
89641120Sjdp	      return 1;
89741120Sjdp	}
89841120Sjdp    }
89941120Sjdp  return 0;
90041120Sjdp}
90141120Sjdp
90241120Sjdp/* Nonzero if modifying X will affect IN.  If X is a register or a SUBREG,
90341120Sjdp   we check if any register number in X conflicts with the relevant register
90441120Sjdp   numbers.  If X is a constant, return 0.  If X is a MEM, return 1 iff IN
90541120Sjdp   contains a MEM (we don't bother checking for memory addresses that can't
90641120Sjdp   conflict because we expect this to be a rare case.  */
90741120Sjdp
90841120Sjdpint
90941120Sjdpreg_overlap_mentioned_p (x, in)
91041120Sjdp     rtx x, in;
91141120Sjdp{
91241120Sjdp  int regno, endregno;
91341120Sjdp
91441120Sjdp  /* Overly conservative.  */
91541120Sjdp  if (GET_CODE (x) == STRICT_LOW_PART)
91641120Sjdp    x = XEXP (x, 0);
91741120Sjdp
91841120Sjdp  /* If either argument is a constant, then modifying X can not affect IN.  */
91941120Sjdp  if (CONSTANT_P (x) || CONSTANT_P (in))
92041120Sjdp    return 0;
92141120Sjdp  else if (GET_CODE (x) == SUBREG)
92241120Sjdp    {
92341120Sjdp      regno = REGNO (SUBREG_REG (x));
92441120Sjdp      if (regno < FIRST_PSEUDO_REGISTER)
92541120Sjdp	regno += SUBREG_WORD (x);
92641120Sjdp    }
92741120Sjdp  else if (GET_CODE (x) == REG)
92841120Sjdp    regno = REGNO (x);
92941120Sjdp  else if (GET_CODE (x) == MEM)
93041120Sjdp    {
93141120Sjdp      char *fmt;
93241120Sjdp      int i;
93341120Sjdp
93441120Sjdp      if (GET_CODE (in) == MEM)
93541120Sjdp	return 1;
93641120Sjdp
93741120Sjdp      fmt = GET_RTX_FORMAT (GET_CODE (in));
93841120Sjdp
93941120Sjdp      for (i = GET_RTX_LENGTH (GET_CODE (in)) - 1; i >= 0; i--)
94041120Sjdp	if (fmt[i] == 'e' && reg_overlap_mentioned_p (x, XEXP (in, i)))
94141120Sjdp	  return 1;
94241120Sjdp
94341120Sjdp      return 0;
94441120Sjdp    }
945103976Spst  else if (GET_CODE (x) == SCRATCH || GET_CODE (x) == PC
94641120Sjdp	   || GET_CODE (x) == CC0)
947103976Spst    return reg_mentioned_p (x, in);
94841120Sjdp  else if (GET_CODE (x) == PARALLEL
94941120Sjdp	   && GET_MODE (x) == BLKmode)
95041120Sjdp    {
95141120Sjdp      register int i;
95241120Sjdp
953103976Spst      /* If any register in here refers to it
954103976Spst	 we return true.  */
955103976Spst      for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
956103976Spst	if (reg_overlap_mentioned_p (SET_DEST (XVECEXP (x, 0, i)), in))
957103976Spst	  return 1;
958103976Spst      return 0;
959103976Spst    }
960103976Spst  else
961103976Spst    abort ();
962103976Spst
963103976Spst  endregno = regno + (regno < FIRST_PSEUDO_REGISTER
964103976Spst		      ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
965103976Spst
966103976Spst  return refers_to_regno_p (regno, endregno, in, NULL_PTR);
967103976Spst}
968103976Spst
969103976Spst/* Used for communications between the next few functions.  */
970103976Spst
971103976Spststatic int reg_set_last_unknown;
972200399Ssyrinxstatic rtx reg_set_last_value;
973200399Ssyrinxstatic int reg_set_last_first_regno, reg_set_last_last_regno;
974200399Ssyrinx
975200399Ssyrinx/* Called via note_stores from reg_set_last.  */
976200399Ssyrinx
977200399Ssyrinxstatic void
978200399Ssyrinxreg_set_last_1 (x, pat)
979200399Ssyrinx     rtx x;
980200399Ssyrinx     rtx pat;
981200399Ssyrinx{
982200399Ssyrinx  int first, last;
983200399Ssyrinx
984200399Ssyrinx  /* If X is not a register, or is not one in the range we care
985200399Ssyrinx     about, ignore.  */
986200399Ssyrinx  if (GET_CODE (x) != REG)
987200399Ssyrinx    return;
988200399Ssyrinx
989103976Spst  first = REGNO (x);
990103976Spst  last = first + (first < FIRST_PSEUDO_REGISTER
991103976Spst		  ? HARD_REGNO_NREGS (first, GET_MODE (x)) : 1);
992103976Spst
993103976Spst  if (first >= reg_set_last_last_regno
994103976Spst      || last <= reg_set_last_first_regno)
995103976Spst    return;
996103976Spst
997103976Spst  /* If this is a CLOBBER or is some complex LHS, or doesn't modify
998103976Spst     exactly the registers we care about, show we don't know the value.  */
999103976Spst  if (GET_CODE (pat) == CLOBBER || SET_DEST (pat) != x
1000103976Spst      || first != reg_set_last_first_regno
1001103976Spst      || last != reg_set_last_last_regno)
100241120Sjdp    reg_set_last_unknown = 1;
100341120Sjdp  else
100441120Sjdp    reg_set_last_value = SET_SRC (pat);
100541120Sjdp}
100641120Sjdp
100741120Sjdp/* Return the last value to which REG was set prior to INSN.  If we can't
1008103976Spst   find it easily, return 0.
1009103976Spst
101041120Sjdp   We only return a REG, SUBREG, or constant because it is too hard to
101141120Sjdp   check if a MEM remains unchanged.  */
101241120Sjdp
101341120Sjdprtx
101441120Sjdpreg_set_last (x, insn)
101541120Sjdp     rtx x;
101641120Sjdp     rtx insn;
101741120Sjdp{
101841120Sjdp  rtx orig_insn = insn;
101941120Sjdp
102041120Sjdp  reg_set_last_first_regno = REGNO (x);
1021103976Spst
102241120Sjdp  reg_set_last_last_regno
102341120Sjdp    = reg_set_last_first_regno
102441120Sjdp      + (reg_set_last_first_regno < FIRST_PSEUDO_REGISTER
102541120Sjdp	 ? HARD_REGNO_NREGS (reg_set_last_first_regno, GET_MODE (x)) : 1);
102641120Sjdp
102741120Sjdp  reg_set_last_unknown = 0;
102841120Sjdp  reg_set_last_value = 0;
102941120Sjdp
103041120Sjdp  /* Scan backwards until reg_set_last_1 changed one of the above flags.
103141120Sjdp     Stop when we reach a label or X is a hard reg and we reach a
1032103976Spst     CALL_INSN (if reg_set_last_last_regno is a hard reg).
103341120Sjdp
103441120Sjdp     If we find a set of X, ensure that its SET_SRC remains unchanged.  */
103541120Sjdp
103641120Sjdp  /* We compare with <= here, because reg_set_last_last_regno
103741120Sjdp     is actually the number of the first reg *not* in X.  */
103841120Sjdp  for (;
103941120Sjdp       insn && GET_CODE (insn) != CODE_LABEL
104041120Sjdp       && ! (GET_CODE (insn) == CALL_INSN
104141120Sjdp	     && reg_set_last_last_regno <= FIRST_PSEUDO_REGISTER);
104241120Sjdp       insn = PREV_INSN (insn))
104341120Sjdp    if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
104441120Sjdp      {
104541120Sjdp	note_stores (PATTERN (insn), reg_set_last_1);
1046103976Spst	if (reg_set_last_unknown)
1047103976Spst	  return 0;
1048103976Spst	else if (reg_set_last_value)
1049103976Spst	  {
105041120Sjdp	    if (CONSTANT_P (reg_set_last_value)
105141120Sjdp		|| ((GET_CODE (reg_set_last_value) == REG
105241120Sjdp		     || GET_CODE (reg_set_last_value) == SUBREG)
105341120Sjdp		    && ! reg_set_between_p (reg_set_last_value,
105441120Sjdp					    insn, orig_insn)))
105541120Sjdp	      return reg_set_last_value;
105641120Sjdp	    else
105741120Sjdp	      return 0;
105841120Sjdp	  }
105941120Sjdp      }
106041120Sjdp
106141120Sjdp  return 0;
1062103976Spst}
1063103976Spst
1064103976Spst/* This is 1 until after the rtl generation pass.  */
106541120Sjdpint rtx_equal_function_value_matters;
106641120Sjdp
106741120Sjdp/* Return 1 if X and Y are identical-looking rtx's.
106841120Sjdp   This is the Lisp function EQUAL for rtx arguments.  */
106941120Sjdp
107041120Sjdpint
107141120Sjdprtx_equal_p (x, y)
107241120Sjdp     rtx x, y;
107341120Sjdp{
107441120Sjdp  register int i;
107541120Sjdp  register int j;
107641120Sjdp  register enum rtx_code code;
107741120Sjdp  register char *fmt;
107841120Sjdp
107941120Sjdp  if (x == y)
108041120Sjdp    return 1;
108141120Sjdp  if (x == 0 || y == 0)
108241120Sjdp    return 0;
108341120Sjdp
108441120Sjdp  code = GET_CODE (x);
108541120Sjdp  /* Rtx's of different codes cannot be equal.  */
108641120Sjdp  if (code != GET_CODE (y))
108741120Sjdp    return 0;
108841120Sjdp
108941120Sjdp  /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
109041120Sjdp     (REG:SI x) and (REG:HI x) are NOT equivalent.  */
109141120Sjdp
109241120Sjdp  if (GET_MODE (x) != GET_MODE (y))
109341120Sjdp    return 0;
109441120Sjdp
1095103976Spst  /* REG, LABEL_REF, and SYMBOL_REF can be compared nonrecursively.  */
1096103976Spst
109741120Sjdp  if (code == REG)
109841120Sjdp    /* Until rtl generation is complete, don't consider a reference to the
109941120Sjdp       return register of the current function the same as the return from a
110041120Sjdp       called function.  This eases the job of function integration.  Once the
110141120Sjdp       distinction is no longer needed, they can be considered equivalent.  */
110241120Sjdp    return (REGNO (x) == REGNO (y)
110341120Sjdp	    && (! rtx_equal_function_value_matters
110441120Sjdp		|| REG_FUNCTION_VALUE_P (x) == REG_FUNCTION_VALUE_P (y)));
110541120Sjdp  else if (code == LABEL_REF)
110641120Sjdp    return XEXP (x, 0) == XEXP (y, 0);
110741120Sjdp  else if (code == SYMBOL_REF)
110841120Sjdp    return XSTR (x, 0) == XSTR (y, 0);
110941120Sjdp  else if (code == SCRATCH || code == CONST_DOUBLE)
1110103976Spst    return 0;
1111103976Spst
1112103976Spst  /* Compare the elements.  If any pair of corresponding elements
1113103976Spst     fail to match, return 0 for the whole things.  */
1114103976Spst
1115103976Spst  fmt = GET_RTX_FORMAT (code);
1116103976Spst  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1117103976Spst    {
1118103976Spst      switch (fmt[i])
1119103976Spst	{
1120103976Spst	case 'w':
1121103976Spst	  if (XWINT (x, i) != XWINT (y, i))
1122103976Spst	    return 0;
1123103976Spst	  break;
1124103976Spst
1125103976Spst	case 'n':
1126103976Spst	case 'i':
1127103976Spst	  if (XINT (x, i) != XINT (y, i))
1128103976Spst	    return 0;
1129103976Spst	  break;
1130103976Spst
1131103976Spst	case 'V':
1132103976Spst	case 'E':
1133103976Spst	  /* Two vectors must have the same length.  */
1134103976Spst	  if (XVECLEN (x, i) != XVECLEN (y, i))
1135103976Spst	    return 0;
1136103976Spst
1137103976Spst	  /* And the corresponding elements must match.  */
1138103976Spst	  for (j = 0; j < XVECLEN (x, i); j++)
1139103976Spst	    if (rtx_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)) == 0)
1140103976Spst	      return 0;
1141103976Spst	  break;
1142103976Spst
1143103976Spst	case 'e':
1144103976Spst	  if (rtx_equal_p (XEXP (x, i), XEXP (y, i)) == 0)
1145103976Spst	    return 0;
1146103976Spst	  break;
1147103976Spst
1148103976Spst	case 'S':
1149103976Spst	case 's':
1150103976Spst	  if (strcmp (XSTR (x, i), XSTR (y, i)))
1151103976Spst	    return 0;
1152103976Spst	  break;
1153103976Spst
1154103976Spst	case 'u':
1155103976Spst	  /* These are just backpointers, so they don't matter.  */
1156103976Spst	  break;
1157103976Spst
1158103976Spst	case '0':
1159103976Spst	  break;
1160103976Spst
1161103976Spst	  /* It is believed that rtx's at this level will never
1162103976Spst	     contain anything but integers and other rtx's,
1163103976Spst	     except for within LABEL_REFs and SYMBOL_REFs.  */
1164103976Spst	default:
1165103976Spst	  abort ();
1166103976Spst	}
1167103976Spst    }
1168103976Spst  return 1;
1169103976Spst}
1170103976Spst
1171103976Spst/* Call FUN on each register or MEM that is stored into or clobbered by X.
1172103976Spst   (X would be the pattern of an insn).
1173103976Spst   FUN receives two arguments:
1174103976Spst     the REG, MEM, CC0 or PC being stored in or clobbered,
1175103976Spst     the SET or CLOBBER rtx that does the store.
1176103976Spst
1177103976Spst  If the item being stored in or clobbered is a SUBREG of a hard register,
1178103976Spst  the SUBREG will be passed.  */
1179103976Spst
1180200399Ssyrinxvoid
1181200399Ssyrinxnote_stores (x, fun)
1182200399Ssyrinx     register rtx x;
1183200399Ssyrinx     void (*fun) PROTO ((rtx, rtx));
1184200399Ssyrinx{
1185200399Ssyrinx  if ((GET_CODE (x) == SET || GET_CODE (x) == CLOBBER))
1186200399Ssyrinx    {
1187200399Ssyrinx      register rtx dest = SET_DEST (x);
1188200399Ssyrinx      while ((GET_CODE (dest) == SUBREG
1189200399Ssyrinx	      && (GET_CODE (SUBREG_REG (dest)) != REG
1190200399Ssyrinx		  || REGNO (SUBREG_REG (dest)) >= FIRST_PSEUDO_REGISTER))
1191200399Ssyrinx	     || GET_CODE (dest) == ZERO_EXTRACT
1192200399Ssyrinx	     || GET_CODE (dest) == SIGN_EXTRACT
1193200399Ssyrinx	     || GET_CODE (dest) == STRICT_LOW_PART)
1194200399Ssyrinx	dest = XEXP (dest, 0);
1195200399Ssyrinx
1196200399Ssyrinx      if (GET_CODE (dest) == PARALLEL
1197200399Ssyrinx	  && GET_MODE (dest) == BLKmode)
1198200399Ssyrinx	{
1199200399Ssyrinx	  register int i;
1200200399Ssyrinx	  for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
1201200399Ssyrinx	    (*fun) (SET_DEST (XVECEXP (dest, 0, i)), x);
1202200399Ssyrinx	}
1203200399Ssyrinx      else
1204200399Ssyrinx	(*fun) (dest, x);
1205200399Ssyrinx    }
1206200399Ssyrinx  else if (GET_CODE (x) == PARALLEL)
1207200399Ssyrinx    {
1208200399Ssyrinx      register int i;
1209200399Ssyrinx      for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1210200399Ssyrinx	{
1211200399Ssyrinx	  register rtx y = XVECEXP (x, 0, i);
1212200399Ssyrinx	  if (GET_CODE (y) == SET || GET_CODE (y) == CLOBBER)
1213200399Ssyrinx	    {
1214200399Ssyrinx	      register rtx dest = SET_DEST (y);
1215200399Ssyrinx	      while ((GET_CODE (dest) == SUBREG
1216200399Ssyrinx		      && (GET_CODE (SUBREG_REG (dest)) != REG
1217200399Ssyrinx			  || (REGNO (SUBREG_REG (dest))
1218200399Ssyrinx			      >= FIRST_PSEUDO_REGISTER)))
1219200399Ssyrinx		     || GET_CODE (dest) == ZERO_EXTRACT
1220200399Ssyrinx		     || GET_CODE (dest) == SIGN_EXTRACT
1221200399Ssyrinx		     || GET_CODE (dest) == STRICT_LOW_PART)
1222200399Ssyrinx		dest = XEXP (dest, 0);
122341120Sjdp	      if (GET_CODE (dest) == PARALLEL
122441120Sjdp		  && GET_MODE (dest) == BLKmode)
122541120Sjdp		{
122641120Sjdp		  register int i;
122741120Sjdp		  for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
122841120Sjdp		    (*fun) (SET_DEST (XVECEXP (dest, 0, i)), y);
122941120Sjdp		}
123041120Sjdp	      else
123141120Sjdp		(*fun) (dest, y);
123241120Sjdp	    }
123341120Sjdp	}
123441120Sjdp    }
123541120Sjdp}
123641120Sjdp
123741120Sjdp/* Return nonzero if X's old contents don't survive after INSN.
123841120Sjdp   This will be true if X is (cc0) or if X is a register and
123941120Sjdp   X dies in INSN or because INSN entirely sets X.
124041120Sjdp
124141120Sjdp   "Entirely set" means set directly and not through a SUBREG,
124241120Sjdp   ZERO_EXTRACT or SIGN_EXTRACT, so no trace of the old contents remains.
124341120Sjdp   Likewise, REG_INC does not count.
124441120Sjdp
124541120Sjdp   REG may be a hard or pseudo reg.  Renumbering is not taken into account,
124641120Sjdp   but for this use that makes no difference, since regs don't overlap
124741120Sjdp   during their lifetimes.  Therefore, this function may be used
124841120Sjdp   at any time after deaths have been computed (in flow.c).
124941120Sjdp
125041120Sjdp   If REG is a hard reg that occupies multiple machine registers, this
125141120Sjdp   function will only return 1 if each of those registers will be replaced
125241120Sjdp   by INSN.  */
125341120Sjdp
125441120Sjdpint
125541120Sjdpdead_or_set_p (insn, x)
125641120Sjdp     rtx insn;
125741120Sjdp     rtx x;
125841120Sjdp{
125941120Sjdp  register int regno, last_regno;
126041120Sjdp  register int i;
126141120Sjdp
126241120Sjdp  /* Can't use cc0_rtx below since this file is used by genattrtab.c.  */
1263103976Spst  if (GET_CODE (x) == CC0)
1264103976Spst    return 1;
1265103976Spst
1266103976Spst  if (GET_CODE (x) != REG)
1267103976Spst    abort ();
1268103976Spst
1269103976Spst  regno = REGNO (x);
1270103976Spst  last_regno = (regno >= FIRST_PSEUDO_REGISTER ? regno
1271103976Spst		: regno + HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1);
1272103976Spst
1273103976Spst  for (i = regno; i <= last_regno; i++)
1274103976Spst    if (! dead_or_set_regno_p (insn, i))
1275103976Spst      return 0;
1276103976Spst
1277103976Spst  return 1;
1278103976Spst}
1279103976Spst
1280103976Spst/* Utility function for dead_or_set_p to check an individual register.  Also
1281103976Spst   called from flow.c.  */
1282103976Spst
1283103976Spstint
1284103976Spstdead_or_set_regno_p (insn, test_regno)
1285103976Spst     rtx insn;
1286103976Spst     int test_regno;
1287103976Spst{
1288103976Spst  int regno, endregno;
1289103976Spst  rtx link;
1290103976Spst
1291103976Spst  /* See if there is a death note for something that includes
1292103976Spst     TEST_REGNO.  */
1293103976Spst  for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1294103976Spst    {
1295103976Spst      if (REG_NOTE_KIND (link) != REG_DEAD
1296103976Spst	  || GET_CODE (XEXP (link, 0)) != REG)
1297103976Spst	continue;
1298103976Spst
1299103976Spst      regno = REGNO (XEXP (link, 0));
1300103976Spst      endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1301103976Spst		  : regno + HARD_REGNO_NREGS (regno,
1302103976Spst					      GET_MODE (XEXP (link, 0))));
1303103976Spst
1304103976Spst      if (test_regno >= regno && test_regno < endregno)
1305103976Spst	return 1;
1306103976Spst    }
1307103976Spst
1308103976Spst  if (GET_CODE (insn) == CALL_INSN
1309103976Spst      && find_regno_fusage (insn, CLOBBER, test_regno))
1310103976Spst    return 1;
1311103976Spst
1312103976Spst  if (GET_CODE (PATTERN (insn)) == SET)
1313103976Spst    {
1314103976Spst      rtx dest = SET_DEST (PATTERN (insn));
1315103976Spst
1316103976Spst      /* A value is totally replaced if it is the destination or the
1317103976Spst	 destination is a SUBREG of REGNO that does not change the number of
1318103976Spst	 words in it.  */
1319103976Spst      if (GET_CODE (dest) == SUBREG
1320103976Spst	  && (((GET_MODE_SIZE (GET_MODE (dest))
1321103976Spst		+ UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1322103976Spst	      == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1323103976Spst		   + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1324103976Spst	dest = SUBREG_REG (dest);
1325103976Spst
1326103976Spst      if (GET_CODE (dest) != REG)
1327103976Spst	return 0;
1328199802Sattilio
1329199802Sattilio      regno = REGNO (dest);
1330199802Sattilio      endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1331199802Sattilio		  : regno + HARD_REGNO_NREGS (regno, GET_MODE (dest)));
1332202751Semaste
1333103976Spst      return (test_regno >= regno && test_regno < endregno);
1334199802Sattilio    }
1335103976Spst  else if (GET_CODE (PATTERN (insn)) == PARALLEL)
1336103976Spst    {
1337103976Spst      register int i;
1338103976Spst
1339103976Spst      for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1340103976Spst	{
1341103976Spst	  rtx body = XVECEXP (PATTERN (insn), 0, i);
1342103976Spst
1343103976Spst	  if (GET_CODE (body) == SET || GET_CODE (body) == CLOBBER)
1344103976Spst	    {
1345103976Spst	      rtx dest = SET_DEST (body);
1346103976Spst
1347103976Spst	      if (GET_CODE (dest) == SUBREG
1348103976Spst		  && (((GET_MODE_SIZE (GET_MODE (dest))
1349103976Spst			+ UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1350103976Spst		      == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1351103976Spst			   + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1352103976Spst		dest = SUBREG_REG (dest);
1353103976Spst
1354103976Spst	      if (GET_CODE (dest) != REG)
1355103976Spst		continue;
1356103976Spst
1357103976Spst	      regno = REGNO (dest);
1358103976Spst	      endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1359103976Spst			  : regno + HARD_REGNO_NREGS (regno, GET_MODE (dest)));
1360103976Spst
136141120Sjdp	      if (test_regno >= regno && test_regno < endregno)
136241120Sjdp		return 1;
136341120Sjdp	    }
136441120Sjdp	}
136541120Sjdp    }
136641120Sjdp
136741120Sjdp  return 0;
136841120Sjdp}
136941120Sjdp
137041120Sjdp/* Return the reg-note of kind KIND in insn INSN, if there is one.
137141120Sjdp   If DATUM is nonzero, look for one whose datum is DATUM.  */
137241120Sjdp
137341120Sjdprtx
137441120Sjdpfind_reg_note (insn, kind, datum)
137541120Sjdp     rtx insn;
137641120Sjdp     enum reg_note kind;
137741120Sjdp     rtx datum;
137841120Sjdp{
137941120Sjdp  register rtx link;
138041120Sjdp
138141120Sjdp  /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN.  */
138241120Sjdp  if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
138341120Sjdp    return 0;
138441120Sjdp
138541120Sjdp  for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1386    if (REG_NOTE_KIND (link) == kind
1387	&& (datum == 0 || datum == XEXP (link, 0)))
1388      return link;
1389  return 0;
1390}
1391
1392/* Return the reg-note of kind KIND in insn INSN which applies to register
1393   number REGNO, if any.  Return 0 if there is no such reg-note.  Note that
1394   the REGNO of this NOTE need not be REGNO if REGNO is a hard register;
1395   it might be the case that the note overlaps REGNO.  */
1396
1397rtx
1398find_regno_note (insn, kind, regno)
1399     rtx insn;
1400     enum reg_note kind;
1401     int regno;
1402{
1403  register rtx link;
1404
1405  /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN.  */
1406  if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
1407    return 0;
1408
1409  for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1410    if (REG_NOTE_KIND (link) == kind
1411	/* Verify that it is a register, so that scratch and MEM won't cause a
1412	   problem here.  */
1413	&& GET_CODE (XEXP (link, 0)) == REG
1414	&& REGNO (XEXP (link, 0)) <= regno
1415	&& ((REGNO (XEXP (link, 0))
1416	     + (REGNO (XEXP (link, 0)) >= FIRST_PSEUDO_REGISTER ? 1
1417		: HARD_REGNO_NREGS (REGNO (XEXP (link, 0)),
1418				    GET_MODE (XEXP (link, 0)))))
1419	    > regno))
1420      return link;
1421  return 0;
1422}
1423
1424/* Return true if DATUM, or any overlap of DATUM, of kind CODE is found
1425   in the CALL_INSN_FUNCTION_USAGE information of INSN.  */
1426
1427int
1428find_reg_fusage (insn, code, datum)
1429     rtx insn;
1430     enum rtx_code code;
1431     rtx datum;
1432{
1433  /* If it's not a CALL_INSN, it can't possibly have a
1434     CALL_INSN_FUNCTION_USAGE field, so don't bother checking.  */
1435  if (GET_CODE (insn) != CALL_INSN)
1436    return 0;
1437
1438  if (! datum)
1439    abort();
1440
1441  if (GET_CODE (datum) != REG)
1442    {
1443      register rtx link;
1444
1445      for (link = CALL_INSN_FUNCTION_USAGE (insn);
1446           link;
1447	   link = XEXP (link, 1))
1448        if (GET_CODE (XEXP (link, 0)) == code
1449	    && rtx_equal_p (datum, SET_DEST (XEXP (link, 0))))
1450          return 1;
1451    }
1452  else
1453    {
1454      register int regno = REGNO (datum);
1455
1456      /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1457	 to pseudo registers, so don't bother checking.  */
1458
1459      if (regno < FIRST_PSEUDO_REGISTER)
1460        {
1461	  int end_regno = regno + HARD_REGNO_NREGS (regno, GET_MODE (datum));
1462	  int i;
1463
1464	  for (i = regno; i < end_regno; i++)
1465	    if (find_regno_fusage (insn, code, i))
1466	      return 1;
1467        }
1468    }
1469
1470  return 0;
1471}
1472
1473/* Return true if REGNO, or any overlap of REGNO, of kind CODE is found
1474   in the CALL_INSN_FUNCTION_USAGE information of INSN.  */
1475
1476int
1477find_regno_fusage (insn, code, regno)
1478     rtx insn;
1479     enum rtx_code code;
1480     int regno;
1481{
1482  register rtx link;
1483
1484  /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1485     to pseudo registers, so don't bother checking.  */
1486
1487  if (regno >= FIRST_PSEUDO_REGISTER
1488      || GET_CODE (insn) != CALL_INSN )
1489    return 0;
1490
1491  for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
1492   {
1493    register int regnote;
1494    register rtx op;
1495
1496    if (GET_CODE (op = XEXP (link, 0)) == code
1497	&& GET_CODE (SET_DEST (op)) == REG
1498	&& (regnote = REGNO (SET_DEST (op))) <= regno
1499	&& regnote
1500		+ HARD_REGNO_NREGS (regnote, GET_MODE (SET_DEST (op)))
1501	    > regno)
1502      return 1;
1503   }
1504
1505  return 0;
1506}
1507
1508/* Remove register note NOTE from the REG_NOTES of INSN.  */
1509
1510void
1511remove_note (insn, note)
1512     register rtx note;
1513     register rtx insn;
1514{
1515  register rtx link;
1516
1517  if (REG_NOTES (insn) == note)
1518    {
1519      REG_NOTES (insn) = XEXP (note, 1);
1520      return;
1521    }
1522
1523  for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1524    if (XEXP (link, 1) == note)
1525      {
1526	XEXP (link, 1) = XEXP (note, 1);
1527	return;
1528      }
1529
1530  abort ();
1531}
1532
1533/* Search LISTP (an EXPR_LIST) for NODE and remove NODE from the list
1534   if it is found.
1535
1536   A simple equality test is used to determine if NODE is on the
1537   EXPR_LIST.  */
1538
1539void
1540remove_node_from_expr_list (node, listp)
1541     rtx node;
1542     rtx *listp;
1543{
1544  rtx temp = *listp;
1545  rtx prev = NULL_RTX;
1546
1547  while (temp)
1548    {
1549      if (node == XEXP (temp, 0))
1550	{
1551	  /* Splice the node out of the list.  */
1552	  if (prev)
1553	    XEXP (prev, 1) = XEXP (temp, 1);
1554	  else
1555	    *listp = XEXP (temp, 1);
1556
1557	  return;
1558	}
1559      temp = XEXP (temp, 1);
1560    }
1561}
1562
1563/* Nonzero if X contains any volatile instructions.  These are instructions
1564   which may cause unpredictable machine state instructions, and thus no
1565   instructions should be moved or combined across them.  This includes
1566   only volatile asms and UNSPEC_VOLATILE instructions.  */
1567
1568int
1569volatile_insn_p (x)
1570     rtx x;
1571{
1572  register RTX_CODE code;
1573
1574  code = GET_CODE (x);
1575  switch (code)
1576    {
1577    case LABEL_REF:
1578    case SYMBOL_REF:
1579    case CONST_INT:
1580    case CONST:
1581    case CONST_DOUBLE:
1582    case CC0:
1583    case PC:
1584    case REG:
1585    case SCRATCH:
1586    case CLOBBER:
1587    case ASM_INPUT:
1588    case ADDR_VEC:
1589    case ADDR_DIFF_VEC:
1590    case CALL:
1591    case MEM:
1592      return 0;
1593
1594    case UNSPEC_VOLATILE:
1595 /* case TRAP_IF: This isn't clear yet.  */
1596      return 1;
1597
1598    case ASM_OPERANDS:
1599      if (MEM_VOLATILE_P (x))
1600	return 1;
1601
1602    default:
1603      break;
1604    }
1605
1606  /* Recursively scan the operands of this expression.  */
1607
1608  {
1609    register char *fmt = GET_RTX_FORMAT (code);
1610    register int i;
1611
1612    for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1613      {
1614	if (fmt[i] == 'e')
1615	  {
1616	    if (volatile_insn_p (XEXP (x, i)))
1617	      return 1;
1618	  }
1619	if (fmt[i] == 'E')
1620	  {
1621	    register int j;
1622	    for (j = 0; j < XVECLEN (x, i); j++)
1623	      if (volatile_insn_p (XVECEXP (x, i, j)))
1624		return 1;
1625	  }
1626      }
1627  }
1628  return 0;
1629}
1630
1631/* Nonzero if X contains any volatile memory references
1632   UNSPEC_VOLATILE operations or volatile ASM_OPERANDS expressions.  */
1633
1634int
1635volatile_refs_p (x)
1636     rtx x;
1637{
1638  register RTX_CODE code;
1639
1640  code = GET_CODE (x);
1641  switch (code)
1642    {
1643    case LABEL_REF:
1644    case SYMBOL_REF:
1645    case CONST_INT:
1646    case CONST:
1647    case CONST_DOUBLE:
1648    case CC0:
1649    case PC:
1650    case REG:
1651    case SCRATCH:
1652    case CLOBBER:
1653    case ASM_INPUT:
1654    case ADDR_VEC:
1655    case ADDR_DIFF_VEC:
1656      return 0;
1657
1658    case CALL:
1659    case UNSPEC_VOLATILE:
1660 /* case TRAP_IF: This isn't clear yet.  */
1661      return 1;
1662
1663    case MEM:
1664    case ASM_OPERANDS:
1665      if (MEM_VOLATILE_P (x))
1666	return 1;
1667
1668    default:
1669      break;
1670    }
1671
1672  /* Recursively scan the operands of this expression.  */
1673
1674  {
1675    register char *fmt = GET_RTX_FORMAT (code);
1676    register int i;
1677
1678    for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1679      {
1680	if (fmt[i] == 'e')
1681	  {
1682	    if (volatile_refs_p (XEXP (x, i)))
1683	      return 1;
1684	  }
1685	if (fmt[i] == 'E')
1686	  {
1687	    register int j;
1688	    for (j = 0; j < XVECLEN (x, i); j++)
1689	      if (volatile_refs_p (XVECEXP (x, i, j)))
1690		return 1;
1691	  }
1692      }
1693  }
1694  return 0;
1695}
1696
1697/* Similar to above, except that it also rejects register pre- and post-
1698   incrementing.  */
1699
1700int
1701side_effects_p (x)
1702     rtx x;
1703{
1704  register RTX_CODE code;
1705
1706  code = GET_CODE (x);
1707  switch (code)
1708    {
1709    case LABEL_REF:
1710    case SYMBOL_REF:
1711    case CONST_INT:
1712    case CONST:
1713    case CONST_DOUBLE:
1714    case CC0:
1715    case PC:
1716    case REG:
1717    case SCRATCH:
1718    case ASM_INPUT:
1719    case ADDR_VEC:
1720    case ADDR_DIFF_VEC:
1721      return 0;
1722
1723    case CLOBBER:
1724      /* Reject CLOBBER with a non-VOID mode.  These are made by combine.c
1725	 when some combination can't be done.  If we see one, don't think
1726	 that we can simplify the expression.  */
1727      return (GET_MODE (x) != VOIDmode);
1728
1729    case PRE_INC:
1730    case PRE_DEC:
1731    case POST_INC:
1732    case POST_DEC:
1733    case CALL:
1734    case UNSPEC_VOLATILE:
1735 /* case TRAP_IF: This isn't clear yet.  */
1736      return 1;
1737
1738    case MEM:
1739    case ASM_OPERANDS:
1740      if (MEM_VOLATILE_P (x))
1741	return 1;
1742
1743    default:
1744      break;
1745    }
1746
1747  /* Recursively scan the operands of this expression.  */
1748
1749  {
1750    register char *fmt = GET_RTX_FORMAT (code);
1751    register int i;
1752
1753    for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1754      {
1755	if (fmt[i] == 'e')
1756	  {
1757	    if (side_effects_p (XEXP (x, i)))
1758	      return 1;
1759	  }
1760	if (fmt[i] == 'E')
1761	  {
1762	    register int j;
1763	    for (j = 0; j < XVECLEN (x, i); j++)
1764	      if (side_effects_p (XVECEXP (x, i, j)))
1765		return 1;
1766	  }
1767      }
1768  }
1769  return 0;
1770}
1771
1772/* Return nonzero if evaluating rtx X might cause a trap.  */
1773
1774int
1775may_trap_p (x)
1776     rtx x;
1777{
1778  int i;
1779  enum rtx_code code;
1780  char *fmt;
1781
1782  if (x == 0)
1783    return 0;
1784  code = GET_CODE (x);
1785  switch (code)
1786    {
1787      /* Handle these cases quickly.  */
1788    case CONST_INT:
1789    case CONST_DOUBLE:
1790    case SYMBOL_REF:
1791    case LABEL_REF:
1792    case CONST:
1793    case PC:
1794    case CC0:
1795    case REG:
1796    case SCRATCH:
1797      return 0;
1798
1799      /* Conditional trap can trap!  */
1800    case UNSPEC_VOLATILE:
1801    case TRAP_IF:
1802      return 1;
1803
1804      /* Memory ref can trap unless it's a static var or a stack slot.  */
1805    case MEM:
1806      return rtx_addr_can_trap_p (XEXP (x, 0));
1807
1808      /* Division by a non-constant might trap.  */
1809    case DIV:
1810    case MOD:
1811    case UDIV:
1812    case UMOD:
1813      if (! CONSTANT_P (XEXP (x, 1))
1814	  || GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
1815	return 1;
1816      /* This was const0_rtx, but by not using that,
1817	 we can link this file into other programs.  */
1818      if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 0)
1819	return 1;
1820      break;
1821
1822    case EXPR_LIST:
1823      /* An EXPR_LIST is used to represent a function call.  This
1824	 certainly may trap.  */
1825      return 1;
1826
1827    default:
1828      /* Any floating arithmetic may trap.  */
1829      if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
1830	return 1;
1831    }
1832
1833  fmt = GET_RTX_FORMAT (code);
1834  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1835    {
1836      if (fmt[i] == 'e')
1837	{
1838	  if (may_trap_p (XEXP (x, i)))
1839	    return 1;
1840	}
1841      else if (fmt[i] == 'E')
1842	{
1843	  register int j;
1844	  for (j = 0; j < XVECLEN (x, i); j++)
1845	    if (may_trap_p (XVECEXP (x, i, j)))
1846	      return 1;
1847	}
1848    }
1849  return 0;
1850}
1851
1852/* Return nonzero if X contains a comparison that is not either EQ or NE,
1853   i.e., an inequality.  */
1854
1855int
1856inequality_comparisons_p (x)
1857     rtx x;
1858{
1859  register char *fmt;
1860  register int len, i;
1861  register enum rtx_code code = GET_CODE (x);
1862
1863  switch (code)
1864    {
1865    case REG:
1866    case SCRATCH:
1867    case PC:
1868    case CC0:
1869    case CONST_INT:
1870    case CONST_DOUBLE:
1871    case CONST:
1872    case LABEL_REF:
1873    case SYMBOL_REF:
1874      return 0;
1875
1876    case LT:
1877    case LTU:
1878    case GT:
1879    case GTU:
1880    case LE:
1881    case LEU:
1882    case GE:
1883    case GEU:
1884      return 1;
1885
1886    default:
1887      break;
1888    }
1889
1890  len = GET_RTX_LENGTH (code);
1891  fmt = GET_RTX_FORMAT (code);
1892
1893  for (i = 0; i < len; i++)
1894    {
1895      if (fmt[i] == 'e')
1896	{
1897	  if (inequality_comparisons_p (XEXP (x, i)))
1898	    return 1;
1899	}
1900      else if (fmt[i] == 'E')
1901	{
1902	  register int j;
1903	  for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1904	    if (inequality_comparisons_p (XVECEXP (x, i, j)))
1905	      return 1;
1906	}
1907    }
1908
1909  return 0;
1910}
1911
1912/* Replace any occurrence of FROM in X with TO.  The function does
1913   not enter into CONST_DOUBLE for the replace.
1914
1915   Note that copying is not done so X must not be shared unless all copies
1916   are to be modified.  */
1917
1918rtx
1919replace_rtx (x, from, to)
1920     rtx x, from, to;
1921{
1922  register int i, j;
1923  register char *fmt;
1924
1925  /* The following prevents loops occurrence when we change MEM in
1926     CONST_DOUBLE onto the same CONST_DOUBLE. */
1927  if (x != 0 && GET_CODE (x) == CONST_DOUBLE)
1928    return x;
1929
1930  if (x == from)
1931    return to;
1932
1933  /* Allow this function to make replacements in EXPR_LISTs.  */
1934  if (x == 0)
1935    return 0;
1936
1937  fmt = GET_RTX_FORMAT (GET_CODE (x));
1938  for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
1939    {
1940      if (fmt[i] == 'e')
1941	XEXP (x, i) = replace_rtx (XEXP (x, i), from, to);
1942      else if (fmt[i] == 'E')
1943	for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1944	  XVECEXP (x, i, j) = replace_rtx (XVECEXP (x, i, j), from, to);
1945    }
1946
1947  return x;
1948}
1949
1950/* Throughout the rtx X, replace many registers according to REG_MAP.
1951   Return the replacement for X (which may be X with altered contents).
1952   REG_MAP[R] is the replacement for register R, or 0 for don't replace.
1953   NREGS is the length of REG_MAP; regs >= NREGS are not mapped.
1954
1955   We only support REG_MAP entries of REG or SUBREG.  Also, hard registers
1956   should not be mapped to pseudos or vice versa since validate_change
1957   is not called.
1958
1959   If REPLACE_DEST is 1, replacements are also done in destinations;
1960   otherwise, only sources are replaced.  */
1961
1962rtx
1963replace_regs (x, reg_map, nregs, replace_dest)
1964     rtx x;
1965     rtx *reg_map;
1966     int nregs;
1967     int replace_dest;
1968{
1969  register enum rtx_code code;
1970  register int i;
1971  register char *fmt;
1972
1973  if (x == 0)
1974    return x;
1975
1976  code = GET_CODE (x);
1977  switch (code)
1978    {
1979    case SCRATCH:
1980    case PC:
1981    case CC0:
1982    case CONST_INT:
1983    case CONST_DOUBLE:
1984    case CONST:
1985    case SYMBOL_REF:
1986    case LABEL_REF:
1987      return x;
1988
1989    case REG:
1990      /* Verify that the register has an entry before trying to access it.  */
1991      if (REGNO (x) < nregs && reg_map[REGNO (x)] != 0)
1992	{
1993	  /* SUBREGs can't be shared.  Always return a copy to ensure that if
1994	     this replacement occurs more than once then each instance will
1995	     get distinct rtx.  */
1996	  if (GET_CODE (reg_map[REGNO (x)]) == SUBREG)
1997	    return copy_rtx (reg_map[REGNO (x)]);
1998	  return reg_map[REGNO (x)];
1999	}
2000      return x;
2001
2002    case SUBREG:
2003      /* Prevent making nested SUBREGs.  */
2004      if (GET_CODE (SUBREG_REG (x)) == REG && REGNO (SUBREG_REG (x)) < nregs
2005	  && reg_map[REGNO (SUBREG_REG (x))] != 0
2006	  && GET_CODE (reg_map[REGNO (SUBREG_REG (x))]) == SUBREG)
2007	{
2008	  rtx map_val = reg_map[REGNO (SUBREG_REG (x))];
2009	  rtx map_inner = SUBREG_REG (map_val);
2010
2011	  if (GET_MODE (x) == GET_MODE (map_inner))
2012	    return map_inner;
2013	  else
2014	    {
2015	      /* We cannot call gen_rtx here since we may be linked with
2016		 genattrtab.c.  */
2017	      /* Let's try clobbering the incoming SUBREG and see
2018		 if this is really safe.  */
2019	      SUBREG_REG (x) = map_inner;
2020	      SUBREG_WORD (x) += SUBREG_WORD (map_val);
2021	      return x;
2022#if 0
2023	      rtx new = rtx_alloc (SUBREG);
2024	      PUT_MODE (new, GET_MODE (x));
2025	      SUBREG_REG (new) = map_inner;
2026	      SUBREG_WORD (new) = SUBREG_WORD (x) + SUBREG_WORD (map_val);
2027#endif
2028	    }
2029	}
2030      break;
2031
2032    case SET:
2033      if (replace_dest)
2034	SET_DEST (x) = replace_regs (SET_DEST (x), reg_map, nregs, 0);
2035
2036      else if (GET_CODE (SET_DEST (x)) == MEM
2037	       || GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
2038	/* Even if we are not to replace destinations, replace register if it
2039	   is CONTAINED in destination (destination is memory or
2040	   STRICT_LOW_PART).  */
2041	XEXP (SET_DEST (x), 0) = replace_regs (XEXP (SET_DEST (x), 0),
2042					       reg_map, nregs, 0);
2043      else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
2044	/* Similarly, for ZERO_EXTRACT we replace all operands.  */
2045	break;
2046
2047      SET_SRC (x) = replace_regs (SET_SRC (x), reg_map, nregs, 0);
2048      return x;
2049
2050    default:
2051      break;
2052    }
2053
2054  fmt = GET_RTX_FORMAT (code);
2055  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2056    {
2057      if (fmt[i] == 'e')
2058	XEXP (x, i) = replace_regs (XEXP (x, i), reg_map, nregs, replace_dest);
2059      if (fmt[i] == 'E')
2060	{
2061	  register int j;
2062	  for (j = 0; j < XVECLEN (x, i); j++)
2063	    XVECEXP (x, i, j) = replace_regs (XVECEXP (x, i, j), reg_map,
2064					      nregs, replace_dest);
2065	}
2066    }
2067  return x;
2068}
2069
2070/* Return 1 if X, the SRC_SRC of  SET of (pc) contain a REG or MEM that is
2071   not in the constant pool and not in the condition of an IF_THEN_ELSE.  */
2072
2073static int
2074jmp_uses_reg_or_mem (x)
2075     rtx x;
2076{
2077  enum rtx_code code = GET_CODE (x);
2078  int i, j;
2079  char *fmt;
2080
2081  switch (code)
2082    {
2083    case CONST:
2084    case LABEL_REF:
2085    case PC:
2086      return 0;
2087
2088    case REG:
2089      return 1;
2090
2091    case MEM:
2092      return ! (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
2093		&& CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)));
2094
2095    case IF_THEN_ELSE:
2096      return (jmp_uses_reg_or_mem (XEXP (x, 1))
2097	      || jmp_uses_reg_or_mem (XEXP (x, 2)));
2098
2099    case PLUS:  case MINUS:  case MULT:
2100      return (jmp_uses_reg_or_mem (XEXP (x, 0))
2101	      || jmp_uses_reg_or_mem (XEXP (x, 1)));
2102
2103    default:
2104      break;
2105    }
2106
2107  fmt = GET_RTX_FORMAT (code);
2108  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2109    {
2110      if (fmt[i] == 'e'
2111	  && jmp_uses_reg_or_mem (XEXP (x, i)))
2112	return 1;
2113
2114      if (fmt[i] == 'E')
2115	for (j = 0; j < XVECLEN (x, i); j++)
2116	  if (jmp_uses_reg_or_mem (XVECEXP (x, i, j)))
2117	    return 1;
2118    }
2119
2120  return 0;
2121}
2122
2123/* Return nonzero if INSN is an indirect jump (aka computed jump).
2124
2125   Tablejumps and casesi insns are not considered indirect jumps;
2126   we can recognize them by a (use (lael_ref)).  */
2127
2128int
2129computed_jump_p (insn)
2130     rtx insn;
2131{
2132  int i;
2133  if (GET_CODE (insn) == JUMP_INSN)
2134    {
2135      rtx pat = PATTERN (insn);
2136
2137      if (GET_CODE (pat) == PARALLEL)
2138	{
2139	  int len = XVECLEN (pat, 0);
2140	  int has_use_labelref = 0;
2141
2142	  for (i = len - 1; i >= 0; i--)
2143	    if (GET_CODE (XVECEXP (pat, 0, i)) == USE
2144		&& (GET_CODE (XEXP (XVECEXP (pat, 0, i), 0))
2145		    == LABEL_REF))
2146	      has_use_labelref = 1;
2147
2148	  if (! has_use_labelref)
2149	    for (i = len - 1; i >= 0; i--)
2150	      if (GET_CODE (XVECEXP (pat, 0, i)) == SET
2151		  && SET_DEST (XVECEXP (pat, 0, i)) == pc_rtx
2152		  && jmp_uses_reg_or_mem (SET_SRC (XVECEXP (pat, 0, i))))
2153		return 1;
2154	}
2155      else if (GET_CODE (pat) == SET
2156	       && SET_DEST (pat) == pc_rtx
2157	       && jmp_uses_reg_or_mem (SET_SRC (pat)))
2158	return 1;
2159    }
2160  return 0;
2161}
2162
2163/* Traverse X via depth-first search, calling F for each
2164   sub-expression (including X itself).  F is also passed the DATA.
2165   If F returns -1, do not traverse sub-expressions, but continue
2166   traversing the rest of the tree.  If F ever returns any other
2167   non-zero value, stop the traversal, and return the value returned
2168   by F.  Otherwise, return 0.  This function does not traverse inside
2169   tree structure that contains RTX_EXPRs, or into sub-expressions
2170   whose format code is `0' since it is not known whether or not those
2171   codes are actually RTL.
2172
2173   This routine is very general, and could (should?) be used to
2174   implement many of the other routines in this file.  */
2175
2176int
2177for_each_rtx (x, f, data)
2178     rtx *x;
2179     rtx_function f;
2180     void *data;
2181{
2182  int result;
2183  int length;
2184  char* format;
2185  int i;
2186
2187  /* Call F on X.  */
2188  result = (*f)(x, data);
2189  if (result == -1)
2190    /* Do not traverse sub-expressions.  */
2191    return 0;
2192  else if (result != 0)
2193    /* Stop the traversal.  */
2194    return result;
2195
2196  if (*x == NULL_RTX)
2197    /* There are no sub-expressions.  */
2198    return 0;
2199
2200  length = GET_RTX_LENGTH (GET_CODE (*x));
2201  format = GET_RTX_FORMAT (GET_CODE (*x));
2202
2203  for (i = 0; i < length; ++i)
2204    {
2205      switch (format[i])
2206	{
2207	case 'e':
2208	  result = for_each_rtx (&XEXP (*x, i), f, data);
2209	  if (result != 0)
2210	    return result;
2211	  break;
2212
2213	case 'V':
2214	case 'E':
2215	  if (XVEC (*x, i) != 0)
2216	    {
2217	      int j;
2218	      for (j = 0; j < XVECLEN (*x, i); ++j)
2219		{
2220		  result = for_each_rtx (&XVECEXP (*x, i, j), f, data);
2221		  if (result != 0)
2222		    return result;
2223		}
2224	    }
2225	  break;
2226
2227	default:
2228	  /* Nothing to do.  */
2229	  break;
2230	}
2231
2232    }
2233
2234  return 0;
2235}
2236
2237/* Searches X for any reference to REGNO, returning the rtx of the
2238   reference found if any.  Otherwise, returns NULL_RTX.  */
2239
2240rtx
2241regno_use_in (regno, x)
2242     int regno;
2243     rtx x;
2244{
2245  register char *fmt;
2246  int i, j;
2247  rtx tem;
2248
2249  if (GET_CODE (x) == REG && REGNO (x) == regno)
2250    return x;
2251
2252  fmt = GET_RTX_FORMAT (GET_CODE (x));
2253  for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2254    {
2255      if (fmt[i] == 'e')
2256	{
2257	  if ((tem = regno_use_in (regno, XEXP (x, i))))
2258	    return tem;
2259	}
2260      else if (fmt[i] == 'E')
2261	for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2262	  if ((tem = regno_use_in (regno , XVECEXP (x, i, j))))
2263	    return tem;
2264    }
2265
2266  return NULL_RTX;
2267}
2268
2269
2270/* Return 1 if X is an autoincrement side effect and the register is
2271   not the stack pointer.  */
2272int
2273auto_inc_p (x)
2274     rtx x;
2275{
2276  switch (GET_CODE (x))
2277    {
2278    case PRE_INC:
2279    case POST_INC:
2280    case PRE_DEC:
2281    case POST_DEC:
2282    case PRE_MODIFY:
2283    case POST_MODIFY:
2284      /* There are no REG_INC notes for SP.  */
2285      if (XEXP (x, 0) != stack_pointer_rtx)
2286	return 1;
2287    default:
2288      break;
2289    }
2290  return 0;
2291}
2292