* cppexp.c (parse_charconst): Null does not end character
[official-gcc.git] / gcc / rtlanal.c
blobd9087cd51f9bd82be964b067911298781e8a118f
1 /* Analyze RTL for C-Compiler
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "rtl.h"
27 static int rtx_addr_can_trap_p PARAMS ((rtx));
28 static void reg_set_p_1 PARAMS ((rtx, rtx, void *));
29 static void reg_set_last_1 PARAMS ((rtx, rtx, void *));
32 /* Forward declarations */
33 static int jmp_uses_reg_or_mem PARAMS ((rtx));
35 /* Bit flags that specify the machine subtype we are compiling for.
36 Bits are tested using macros TARGET_... defined in the tm.h file
37 and set by `-m...' switches. Must be defined in rtlanal.c. */
39 int target_flags;
41 /* Return 1 if the value of X is unstable
42 (would be different at a different point in the program).
43 The frame pointer, arg pointer, etc. are considered stable
44 (within one function) and so is anything marked `unchanging'. */
46 int
47 rtx_unstable_p (x)
48 rtx x;
50 register RTX_CODE code = GET_CODE (x);
51 register int i;
52 register const char *fmt;
54 if (code == MEM)
55 return ! RTX_UNCHANGING_P (x);
57 if (code == QUEUED)
58 return 1;
60 if (code == CONST || code == CONST_INT)
61 return 0;
63 if (code == REG)
64 return ! (REGNO (x) == FRAME_POINTER_REGNUM
65 || REGNO (x) == HARD_FRAME_POINTER_REGNUM
66 || REGNO (x) == ARG_POINTER_REGNUM
67 || RTX_UNCHANGING_P (x));
69 fmt = GET_RTX_FORMAT (code);
70 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
71 if (fmt[i] == 'e')
72 if (rtx_unstable_p (XEXP (x, i)))
73 return 1;
74 return 0;
77 /* Return 1 if X has a value that can vary even between two
78 executions of the program. 0 means X can be compared reliably
79 against certain constants or near-constants.
80 The frame pointer and the arg pointer are considered constant. */
82 int
83 rtx_varies_p (x)
84 rtx x;
86 register RTX_CODE code = GET_CODE (x);
87 register int i;
88 register const char *fmt;
90 switch (code)
92 case MEM:
93 case QUEUED:
94 return 1;
96 case CONST:
97 case CONST_INT:
98 case CONST_DOUBLE:
99 case SYMBOL_REF:
100 case LABEL_REF:
101 return 0;
103 case REG:
104 /* Note that we have to test for the actual rtx used for the frame
105 and arg pointers and not just the register number in case we have
106 eliminated the frame and/or arg pointer and are using it
107 for pseudos. */
108 return ! (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
109 || x == arg_pointer_rtx || x == pic_offset_table_rtx);
111 case LO_SUM:
112 /* The operand 0 of a LO_SUM is considered constant
113 (in fact is it related specifically to operand 1). */
114 return rtx_varies_p (XEXP (x, 1));
116 default:
117 break;
120 fmt = GET_RTX_FORMAT (code);
121 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
122 if (fmt[i] == 'e')
123 if (rtx_varies_p (XEXP (x, i)))
124 return 1;
125 return 0;
128 /* Return 0 if the use of X as an address in a MEM can cause a trap. */
130 static int
131 rtx_addr_can_trap_p (x)
132 register rtx x;
134 register enum rtx_code code = GET_CODE (x);
136 switch (code)
138 case SYMBOL_REF:
139 case LABEL_REF:
140 /* SYMBOL_REF is problematic due to the possible presence of
141 a #pragma weak, but to say that loads from symbols can trap is
142 *very* costly. It's not at all clear what's best here. For
143 now, we ignore the impact of #pragma weak. */
144 return 0;
146 case REG:
147 /* As in rtx_varies_p, we have to use the actual rtx, not reg number. */
148 return ! (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
149 || x == stack_pointer_rtx || x == arg_pointer_rtx);
151 case CONST:
152 return rtx_addr_can_trap_p (XEXP (x, 0));
154 case PLUS:
155 /* An address is assumed not to trap if it is an address that can't
156 trap plus a constant integer. */
157 return (rtx_addr_can_trap_p (XEXP (x, 0))
158 || GET_CODE (XEXP (x, 1)) != CONST_INT);
160 case LO_SUM:
161 return rtx_addr_can_trap_p (XEXP (x, 1));
163 default:
164 break;
167 /* If it isn't one of the case above, it can cause a trap. */
168 return 1;
171 /* Return 1 if X refers to a memory location whose address
172 cannot be compared reliably with constant addresses,
173 or if X refers to a BLKmode memory object. */
176 rtx_addr_varies_p (x)
177 rtx x;
179 register enum rtx_code code;
180 register int i;
181 register const char *fmt;
183 if (x == 0)
184 return 0;
186 code = GET_CODE (x);
187 if (code == MEM)
188 return GET_MODE (x) == BLKmode || rtx_varies_p (XEXP (x, 0));
190 fmt = GET_RTX_FORMAT (code);
191 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
192 if (fmt[i] == 'e')
194 if (rtx_addr_varies_p (XEXP (x, i)))
195 return 1;
197 else if (fmt[i] == 'E')
199 int j;
200 for (j = 0; j < XVECLEN (x, i); j++)
201 if (rtx_addr_varies_p (XVECEXP (x, i, j)))
202 return 1;
204 return 0;
207 /* Return the value of the integer term in X, if one is apparent;
208 otherwise return 0.
209 Only obvious integer terms are detected.
210 This is used in cse.c with the `related_value' field.*/
212 HOST_WIDE_INT
213 get_integer_term (x)
214 rtx x;
216 if (GET_CODE (x) == CONST)
217 x = XEXP (x, 0);
219 if (GET_CODE (x) == MINUS
220 && GET_CODE (XEXP (x, 1)) == CONST_INT)
221 return - INTVAL (XEXP (x, 1));
222 if (GET_CODE (x) == PLUS
223 && GET_CODE (XEXP (x, 1)) == CONST_INT)
224 return INTVAL (XEXP (x, 1));
225 return 0;
228 /* If X is a constant, return the value sans apparent integer term;
229 otherwise return 0.
230 Only obvious integer terms are detected. */
233 get_related_value (x)
234 rtx x;
236 if (GET_CODE (x) != CONST)
237 return 0;
238 x = XEXP (x, 0);
239 if (GET_CODE (x) == PLUS
240 && GET_CODE (XEXP (x, 1)) == CONST_INT)
241 return XEXP (x, 0);
242 else if (GET_CODE (x) == MINUS
243 && GET_CODE (XEXP (x, 1)) == CONST_INT)
244 return XEXP (x, 0);
245 return 0;
248 /* Nonzero if register REG appears somewhere within IN.
249 Also works if REG is not a register; in this case it checks
250 for a subexpression of IN that is Lisp "equal" to REG. */
253 reg_mentioned_p (reg, in)
254 register rtx reg, in;
256 register const char *fmt;
257 register int i;
258 register enum rtx_code code;
260 if (in == 0)
261 return 0;
263 if (reg == in)
264 return 1;
266 if (GET_CODE (in) == LABEL_REF)
267 return reg == XEXP (in, 0);
269 code = GET_CODE (in);
271 switch (code)
273 /* Compare registers by number. */
274 case REG:
275 return GET_CODE (reg) == REG && REGNO (in) == REGNO (reg);
277 /* These codes have no constituent expressions
278 and are unique. */
279 case SCRATCH:
280 case CC0:
281 case PC:
282 return 0;
284 case CONST_INT:
285 return GET_CODE (reg) == CONST_INT && INTVAL (in) == INTVAL (reg);
287 case CONST_DOUBLE:
288 /* These are kept unique for a given value. */
289 return 0;
291 default:
292 break;
295 if (GET_CODE (reg) == code && rtx_equal_p (reg, in))
296 return 1;
298 fmt = GET_RTX_FORMAT (code);
300 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
302 if (fmt[i] == 'E')
304 register int j;
305 for (j = XVECLEN (in, i) - 1; j >= 0; j--)
306 if (reg_mentioned_p (reg, XVECEXP (in, i, j)))
307 return 1;
309 else if (fmt[i] == 'e'
310 && reg_mentioned_p (reg, XEXP (in, i)))
311 return 1;
313 return 0;
316 /* Return 1 if in between BEG and END, exclusive of BEG and END, there is
317 no CODE_LABEL insn. */
320 no_labels_between_p (beg, end)
321 rtx beg, end;
323 register rtx p;
324 for (p = NEXT_INSN (beg); p != end; p = NEXT_INSN (p))
325 if (GET_CODE (p) == CODE_LABEL)
326 return 0;
327 return 1;
330 /* Return 1 if in between BEG and END, exclusive of BEG and END, there is
331 no JUMP_INSN insn. */
334 no_jumps_between_p (beg, end)
335 rtx beg, end;
337 register rtx p;
338 for (p = NEXT_INSN (beg); p != end; p = NEXT_INSN (p))
339 if (GET_CODE (p) == JUMP_INSN)
340 return 0;
341 return 1;
344 /* Nonzero if register REG is used in an insn between
345 FROM_INSN and TO_INSN (exclusive of those two). */
348 reg_used_between_p (reg, from_insn, to_insn)
349 rtx reg, from_insn, to_insn;
351 register rtx insn;
353 if (from_insn == to_insn)
354 return 0;
356 for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
357 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
358 && (reg_overlap_mentioned_p (reg, PATTERN (insn))
359 || (GET_CODE (insn) == CALL_INSN
360 && (find_reg_fusage (insn, USE, reg)
361 || find_reg_fusage (insn, CLOBBER, reg)))))
362 return 1;
363 return 0;
366 /* Nonzero if the old value of X, a register, is referenced in BODY. If X
367 is entirely replaced by a new value and the only use is as a SET_DEST,
368 we do not consider it a reference. */
371 reg_referenced_p (x, body)
372 rtx x;
373 rtx body;
375 int i;
377 switch (GET_CODE (body))
379 case SET:
380 if (reg_overlap_mentioned_p (x, SET_SRC (body)))
381 return 1;
383 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
384 of a REG that occupies all of the REG, the insn references X if
385 it is mentioned in the destination. */
386 if (GET_CODE (SET_DEST (body)) != CC0
387 && GET_CODE (SET_DEST (body)) != PC
388 && GET_CODE (SET_DEST (body)) != REG
389 && ! (GET_CODE (SET_DEST (body)) == SUBREG
390 && GET_CODE (SUBREG_REG (SET_DEST (body))) == REG
391 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (body))))
392 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
393 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (body)))
394 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
395 && reg_overlap_mentioned_p (x, SET_DEST (body)))
396 return 1;
397 return 0;
399 case ASM_OPERANDS:
400 for (i = ASM_OPERANDS_INPUT_LENGTH (body) - 1; i >= 0; i--)
401 if (reg_overlap_mentioned_p (x, ASM_OPERANDS_INPUT (body, i)))
402 return 1;
403 return 0;
405 case CALL:
406 case USE:
407 case IF_THEN_ELSE:
408 return reg_overlap_mentioned_p (x, body);
410 case TRAP_IF:
411 return reg_overlap_mentioned_p (x, TRAP_CONDITION (body));
413 case UNSPEC:
414 case UNSPEC_VOLATILE:
415 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
416 if (reg_overlap_mentioned_p (x, XVECEXP (body, 0, i)))
417 return 1;
418 return 0;
420 case PARALLEL:
421 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
422 if (reg_referenced_p (x, XVECEXP (body, 0, i)))
423 return 1;
424 return 0;
426 case CLOBBER:
427 if (GET_CODE (XEXP (body, 0)) == MEM)
428 if (reg_overlap_mentioned_p (x, XEXP (XEXP (body, 0), 0)))
429 return 1;
430 return 0;
432 case COND_EXEC:
433 if (reg_overlap_mentioned_p (x, COND_EXEC_TEST (body)))
434 return 1;
435 return reg_referenced_p (x, COND_EXEC_CODE (body));
437 default:
438 return 0;
442 /* Nonzero if register REG is referenced in an insn between
443 FROM_INSN and TO_INSN (exclusive of those two). Sets of REG do
444 not count. */
447 reg_referenced_between_p (reg, from_insn, to_insn)
448 rtx reg, from_insn, to_insn;
450 register rtx insn;
452 if (from_insn == to_insn)
453 return 0;
455 for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
456 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
457 && (reg_referenced_p (reg, PATTERN (insn))
458 || (GET_CODE (insn) == CALL_INSN
459 && find_reg_fusage (insn, USE, reg))))
460 return 1;
461 return 0;
464 /* Nonzero if register REG is set or clobbered in an insn between
465 FROM_INSN and TO_INSN (exclusive of those two). */
468 reg_set_between_p (reg, from_insn, to_insn)
469 rtx reg, from_insn, to_insn;
471 register rtx insn;
473 if (from_insn == to_insn)
474 return 0;
476 for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
477 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
478 && reg_set_p (reg, insn))
479 return 1;
480 return 0;
483 /* Internals of reg_set_between_p. */
485 static rtx reg_set_reg;
486 static int reg_set_flag;
488 static void
489 reg_set_p_1 (x, pat, data)
490 rtx x;
491 rtx pat ATTRIBUTE_UNUSED;
492 void *data ATTRIBUTE_UNUSED;
494 /* We don't want to return 1 if X is a MEM that contains a register
495 within REG_SET_REG. */
497 if ((GET_CODE (x) != MEM)
498 && reg_overlap_mentioned_p (reg_set_reg, x))
499 reg_set_flag = 1;
503 reg_set_p (reg, insn)
504 rtx reg, insn;
506 rtx body = insn;
508 /* We can be passed an insn or part of one. If we are passed an insn,
509 check if a side-effect of the insn clobbers REG. */
510 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
512 if (FIND_REG_INC_NOTE (insn, reg)
513 || (GET_CODE (insn) == CALL_INSN
514 /* We'd like to test call_used_regs here, but rtlanal.c can't
515 reference that variable due to its use in genattrtab. So
516 we'll just be more conservative.
518 ??? Unless we could ensure that the CALL_INSN_FUNCTION_USAGE
519 information holds all clobbered registers. */
520 && ((GET_CODE (reg) == REG
521 && REGNO (reg) < FIRST_PSEUDO_REGISTER)
522 || GET_CODE (reg) == MEM
523 || find_reg_fusage (insn, CLOBBER, reg))))
524 return 1;
526 body = PATTERN (insn);
529 reg_set_reg = reg;
530 reg_set_flag = 0;
531 note_stores (body, reg_set_p_1, NULL);
532 return reg_set_flag;
535 /* Similar to reg_set_between_p, but check all registers in X. Return 0
536 only if none of them are modified between START and END. Do not
537 consider non-registers one way or the other. */
540 regs_set_between_p (x, start, end)
541 rtx x;
542 rtx start, end;
544 enum rtx_code code = GET_CODE (x);
545 const char *fmt;
546 int i, j;
548 switch (code)
550 case CONST_INT:
551 case CONST_DOUBLE:
552 case CONST:
553 case SYMBOL_REF:
554 case LABEL_REF:
555 case PC:
556 case CC0:
557 return 0;
559 case REG:
560 return reg_set_between_p (x, start, end);
562 default:
563 break;
566 fmt = GET_RTX_FORMAT (code);
567 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
569 if (fmt[i] == 'e' && regs_set_between_p (XEXP (x, i), start, end))
570 return 1;
572 else if (fmt[i] == 'E')
573 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
574 if (regs_set_between_p (XVECEXP (x, i, j), start, end))
575 return 1;
578 return 0;
581 /* Similar to reg_set_between_p, but check all registers in X. Return 0
582 only if none of them are modified between START and END. Return 1 if
583 X contains a MEM; this routine does not perform any memory aliasing. */
586 modified_between_p (x, start, end)
587 rtx x;
588 rtx start, end;
590 enum rtx_code code = GET_CODE (x);
591 const char *fmt;
592 int i, j;
594 switch (code)
596 case CONST_INT:
597 case CONST_DOUBLE:
598 case CONST:
599 case SYMBOL_REF:
600 case LABEL_REF:
601 return 0;
603 case PC:
604 case CC0:
605 return 1;
607 case MEM:
608 /* If the memory is not constant, assume it is modified. If it is
609 constant, we still have to check the address. */
610 if (! RTX_UNCHANGING_P (x))
611 return 1;
612 break;
614 case REG:
615 return reg_set_between_p (x, start, end);
617 default:
618 break;
621 fmt = GET_RTX_FORMAT (code);
622 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
624 if (fmt[i] == 'e' && modified_between_p (XEXP (x, i), start, end))
625 return 1;
627 else if (fmt[i] == 'E')
628 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
629 if (modified_between_p (XVECEXP (x, i, j), start, end))
630 return 1;
633 return 0;
636 /* Similar to reg_set_p, but check all registers in X. Return 0 only if none
637 of them are modified in INSN. Return 1 if X contains a MEM; this routine
638 does not perform any memory aliasing. */
641 modified_in_p (x, insn)
642 rtx x;
643 rtx insn;
645 enum rtx_code code = GET_CODE (x);
646 const char *fmt;
647 int i, j;
649 switch (code)
651 case CONST_INT:
652 case CONST_DOUBLE:
653 case CONST:
654 case SYMBOL_REF:
655 case LABEL_REF:
656 return 0;
658 case PC:
659 case CC0:
660 return 1;
662 case MEM:
663 /* If the memory is not constant, assume it is modified. If it is
664 constant, we still have to check the address. */
665 if (! RTX_UNCHANGING_P (x))
666 return 1;
667 break;
669 case REG:
670 return reg_set_p (x, insn);
672 default:
673 break;
676 fmt = GET_RTX_FORMAT (code);
677 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
679 if (fmt[i] == 'e' && modified_in_p (XEXP (x, i), insn))
680 return 1;
682 else if (fmt[i] == 'E')
683 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
684 if (modified_in_p (XVECEXP (x, i, j), insn))
685 return 1;
688 return 0;
691 /* Given an INSN, return a SET expression if this insn has only a single SET.
692 It may also have CLOBBERs, USEs, or SET whose output
693 will not be used, which we ignore. */
696 single_set (insn)
697 rtx insn;
699 rtx set;
700 int i;
702 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
703 return 0;
705 if (GET_CODE (PATTERN (insn)) == SET)
706 return PATTERN (insn);
708 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
710 for (i = 0, set = 0; i < XVECLEN (PATTERN (insn), 0); i++)
712 rtx sub = XVECEXP (PATTERN (insn), 0, i);
714 switch (GET_CODE (sub))
716 case USE:
717 case CLOBBER:
718 break;
720 case SET:
721 if (! find_reg_note (insn, REG_UNUSED, SET_DEST (sub))
722 || side_effects_p (sub))
724 if (set)
725 return 0;
726 else
727 set = sub;
729 break;
731 default:
732 return 0;
735 return set;
738 return 0;
741 /* Given an INSN, return nonzero if it has more than one SET, else return
742 zero. */
745 multiple_sets (insn)
746 rtx insn;
748 int found;
749 int i;
751 /* INSN must be an insn. */
752 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
753 return 0;
755 /* Only a PARALLEL can have multiple SETs. */
756 if (GET_CODE (PATTERN (insn)) == PARALLEL)
758 for (i = 0, found = 0; i < XVECLEN (PATTERN (insn), 0); i++)
759 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
761 /* If we have already found a SET, then return now. */
762 if (found)
763 return 1;
764 else
765 found = 1;
769 /* Either zero or one SET. */
770 return 0;
773 /* Return the last thing that X was assigned from before *PINSN. If VALID_TO
774 is not NULL_RTX then verify that the object is not modified up to VALID_TO.
775 If the object was modified, if we hit a partial assignment to X, or hit a
776 CODE_LABEL first, return X. If we found an assignment, update *PINSN to
777 point to it. ALLOW_HWREG is set to 1 if hardware registers are allowed to
778 be the src. */
781 find_last_value (x, pinsn, valid_to, allow_hwreg)
782 rtx x;
783 rtx *pinsn;
784 rtx valid_to;
785 int allow_hwreg;
787 rtx p;
789 for (p = PREV_INSN (*pinsn); p && GET_CODE (p) != CODE_LABEL;
790 p = PREV_INSN (p))
791 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
793 rtx set = single_set (p);
794 rtx note = find_reg_note (p, REG_EQUAL, NULL_RTX);
796 if (set && rtx_equal_p (x, SET_DEST (set)))
798 rtx src = SET_SRC (set);
800 if (note && GET_CODE (XEXP (note, 0)) != EXPR_LIST)
801 src = XEXP (note, 0);
803 if ((valid_to == NULL_RTX
804 || ! modified_between_p (src, PREV_INSN (p), valid_to))
805 /* Reject hard registers because we don't usually want
806 to use them; we'd rather use a pseudo. */
807 && (! (GET_CODE (src) == REG
808 && REGNO (src) < FIRST_PSEUDO_REGISTER) || allow_hwreg))
810 *pinsn = p;
811 return src;
815 /* If set in non-simple way, we don't have a value. */
816 if (reg_set_p (x, p))
817 break;
820 return x;
823 /* Return nonzero if register in range [REGNO, ENDREGNO)
824 appears either explicitly or implicitly in X
825 other than being stored into.
827 References contained within the substructure at LOC do not count.
828 LOC may be zero, meaning don't ignore anything. */
831 refers_to_regno_p (regno, endregno, x, loc)
832 unsigned int regno, endregno;
833 rtx x;
834 rtx *loc;
836 int i;
837 unsigned int x_regno;
838 RTX_CODE code;
839 const char *fmt;
841 repeat:
842 /* The contents of a REG_NONNEG note is always zero, so we must come here
843 upon repeat in case the last REG_NOTE is a REG_NONNEG note. */
844 if (x == 0)
845 return 0;
847 code = GET_CODE (x);
849 switch (code)
851 case REG:
852 x_regno = REGNO (x);
854 /* If we modifying the stack, frame, or argument pointer, it will
855 clobber a virtual register. In fact, we could be more precise,
856 but it isn't worth it. */
857 if ((x_regno == STACK_POINTER_REGNUM
858 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
859 || x_regno == ARG_POINTER_REGNUM
860 #endif
861 || x_regno == FRAME_POINTER_REGNUM)
862 && regno >= FIRST_VIRTUAL_REGISTER && regno <= LAST_VIRTUAL_REGISTER)
863 return 1;
865 return (endregno > x_regno
866 && regno < x_regno + (x_regno < FIRST_PSEUDO_REGISTER
867 ? HARD_REGNO_NREGS (x_regno, GET_MODE (x))
868 : 1));
870 case SUBREG:
871 /* If this is a SUBREG of a hard reg, we can see exactly which
872 registers are being modified. Otherwise, handle normally. */
873 if (GET_CODE (SUBREG_REG (x)) == REG
874 && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER)
876 unsigned int inner_regno = REGNO (SUBREG_REG (x)) + SUBREG_WORD (x);
877 unsigned int inner_endregno
878 = inner_regno + (inner_regno < FIRST_PSEUDO_REGISTER
879 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
881 return endregno > inner_regno && regno < inner_endregno;
883 break;
885 case CLOBBER:
886 case SET:
887 if (&SET_DEST (x) != loc
888 /* Note setting a SUBREG counts as referring to the REG it is in for
889 a pseudo but not for hard registers since we can
890 treat each word individually. */
891 && ((GET_CODE (SET_DEST (x)) == SUBREG
892 && loc != &SUBREG_REG (SET_DEST (x))
893 && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG
894 && REGNO (SUBREG_REG (SET_DEST (x))) >= FIRST_PSEUDO_REGISTER
895 && refers_to_regno_p (regno, endregno,
896 SUBREG_REG (SET_DEST (x)), loc))
897 || (GET_CODE (SET_DEST (x)) != REG
898 && refers_to_regno_p (regno, endregno, SET_DEST (x), loc))))
899 return 1;
901 if (code == CLOBBER || loc == &SET_SRC (x))
902 return 0;
903 x = SET_SRC (x);
904 goto repeat;
906 default:
907 break;
910 /* X does not match, so try its subexpressions. */
912 fmt = GET_RTX_FORMAT (code);
913 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
915 if (fmt[i] == 'e' && loc != &XEXP (x, i))
917 if (i == 0)
919 x = XEXP (x, 0);
920 goto repeat;
922 else
923 if (refers_to_regno_p (regno, endregno, XEXP (x, i), loc))
924 return 1;
926 else if (fmt[i] == 'E')
928 register int j;
929 for (j = XVECLEN (x, i) - 1; j >=0; j--)
930 if (loc != &XVECEXP (x, i, j)
931 && refers_to_regno_p (regno, endregno, XVECEXP (x, i, j), loc))
932 return 1;
935 return 0;
938 /* Nonzero if modifying X will affect IN. If X is a register or a SUBREG,
939 we check if any register number in X conflicts with the relevant register
940 numbers. If X is a constant, return 0. If X is a MEM, return 1 iff IN
941 contains a MEM (we don't bother checking for memory addresses that can't
942 conflict because we expect this to be a rare case. */
945 reg_overlap_mentioned_p (x, in)
946 rtx x, in;
948 unsigned int regno, endregno;
950 /* Overly conservative. */
951 if (GET_CODE (x) == STRICT_LOW_PART)
952 x = XEXP (x, 0);
954 /* If either argument is a constant, then modifying X can not affect IN. */
955 if (CONSTANT_P (x) || CONSTANT_P (in))
956 return 0;
958 switch (GET_CODE (x))
960 case SUBREG:
961 regno = REGNO (SUBREG_REG (x));
962 if (regno < FIRST_PSEUDO_REGISTER)
963 regno += SUBREG_WORD (x);
964 goto do_reg;
966 case REG:
967 regno = REGNO (x);
968 do_reg:
969 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
970 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
971 return refers_to_regno_p (regno, endregno, in, NULL_PTR);
973 case MEM:
975 const char *fmt;
976 int i;
978 if (GET_CODE (in) == MEM)
979 return 1;
981 fmt = GET_RTX_FORMAT (GET_CODE (in));
982 for (i = GET_RTX_LENGTH (GET_CODE (in)) - 1; i >= 0; i--)
983 if (fmt[i] == 'e' && reg_overlap_mentioned_p (x, XEXP (in, i)))
984 return 1;
986 return 0;
989 case SCRATCH:
990 case PC:
991 case CC0:
992 return reg_mentioned_p (x, in);
994 case PARALLEL:
995 if (GET_MODE (x) == BLKmode)
997 register int i;
999 /* If any register in here refers to it we return true. */
1000 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1001 if (reg_overlap_mentioned_p (SET_DEST (XVECEXP (x, 0, i)), in))
1002 return 1;
1003 return 0;
1005 break;
1007 default:
1008 break;
1011 abort ();
1014 /* Used for communications between the next few functions. */
1016 static int reg_set_last_unknown;
1017 static rtx reg_set_last_value;
1018 static unsigned int reg_set_last_first_regno, reg_set_last_last_regno;
1020 /* Called via note_stores from reg_set_last. */
1022 static void
1023 reg_set_last_1 (x, pat, data)
1024 rtx x;
1025 rtx pat;
1026 void *data ATTRIBUTE_UNUSED;
1028 unsigned int first, last;
1030 /* If X is not a register, or is not one in the range we care
1031 about, ignore. */
1032 if (GET_CODE (x) != REG)
1033 return;
1035 first = REGNO (x);
1036 last = first + (first < FIRST_PSEUDO_REGISTER
1037 ? HARD_REGNO_NREGS (first, GET_MODE (x)) : 1);
1039 if (first >= reg_set_last_last_regno
1040 || last <= reg_set_last_first_regno)
1041 return;
1043 /* If this is a CLOBBER or is some complex LHS, or doesn't modify
1044 exactly the registers we care about, show we don't know the value. */
1045 if (GET_CODE (pat) == CLOBBER || SET_DEST (pat) != x
1046 || first != reg_set_last_first_regno
1047 || last != reg_set_last_last_regno)
1048 reg_set_last_unknown = 1;
1049 else
1050 reg_set_last_value = SET_SRC (pat);
1053 /* Return the last value to which REG was set prior to INSN. If we can't
1054 find it easily, return 0.
1056 We only return a REG, SUBREG, or constant because it is too hard to
1057 check if a MEM remains unchanged. */
1060 reg_set_last (x, insn)
1061 rtx x;
1062 rtx insn;
1064 rtx orig_insn = insn;
1066 reg_set_last_first_regno = REGNO (x);
1068 reg_set_last_last_regno
1069 = reg_set_last_first_regno
1070 + (reg_set_last_first_regno < FIRST_PSEUDO_REGISTER
1071 ? HARD_REGNO_NREGS (reg_set_last_first_regno, GET_MODE (x)) : 1);
1073 reg_set_last_unknown = 0;
1074 reg_set_last_value = 0;
1076 /* Scan backwards until reg_set_last_1 changed one of the above flags.
1077 Stop when we reach a label or X is a hard reg and we reach a
1078 CALL_INSN (if reg_set_last_last_regno is a hard reg).
1080 If we find a set of X, ensure that its SET_SRC remains unchanged. */
1082 /* We compare with <= here, because reg_set_last_last_regno
1083 is actually the number of the first reg *not* in X. */
1084 for (;
1085 insn && GET_CODE (insn) != CODE_LABEL
1086 && ! (GET_CODE (insn) == CALL_INSN
1087 && reg_set_last_last_regno <= FIRST_PSEUDO_REGISTER);
1088 insn = PREV_INSN (insn))
1089 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1091 note_stores (PATTERN (insn), reg_set_last_1, NULL);
1092 if (reg_set_last_unknown)
1093 return 0;
1094 else if (reg_set_last_value)
1096 if (CONSTANT_P (reg_set_last_value)
1097 || ((GET_CODE (reg_set_last_value) == REG
1098 || GET_CODE (reg_set_last_value) == SUBREG)
1099 && ! reg_set_between_p (reg_set_last_value,
1100 insn, orig_insn)))
1101 return reg_set_last_value;
1102 else
1103 return 0;
1107 return 0;
1110 /* Call FUN on each register or MEM that is stored into or clobbered by X.
1111 (X would be the pattern of an insn).
1112 FUN receives two arguments:
1113 the REG, MEM, CC0 or PC being stored in or clobbered,
1114 the SET or CLOBBER rtx that does the store.
1116 If the item being stored in or clobbered is a SUBREG of a hard register,
1117 the SUBREG will be passed. */
1119 void
1120 note_stores (x, fun, data)
1121 register rtx x;
1122 void (*fun) PARAMS ((rtx, rtx, void *));
1123 void *data;
1125 if (GET_CODE (x) == COND_EXEC)
1126 x = COND_EXEC_CODE (x);
1127 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
1129 register rtx dest = SET_DEST (x);
1130 while ((GET_CODE (dest) == SUBREG
1131 && (GET_CODE (SUBREG_REG (dest)) != REG
1132 || REGNO (SUBREG_REG (dest)) >= FIRST_PSEUDO_REGISTER))
1133 || GET_CODE (dest) == ZERO_EXTRACT
1134 || GET_CODE (dest) == SIGN_EXTRACT
1135 || GET_CODE (dest) == STRICT_LOW_PART)
1136 dest = XEXP (dest, 0);
1138 if (GET_CODE (dest) == PARALLEL
1139 && GET_MODE (dest) == BLKmode)
1141 register int i;
1142 for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
1143 (*fun) (SET_DEST (XVECEXP (dest, 0, i)), x, data);
1145 else
1146 (*fun) (dest, x, data);
1148 else if (GET_CODE (x) == PARALLEL)
1150 register int i;
1151 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1153 register rtx y = XVECEXP (x, 0, i);
1154 if (GET_CODE (y) == COND_EXEC)
1155 y = COND_EXEC_CODE (y);
1156 if (GET_CODE (y) == SET || GET_CODE (y) == CLOBBER)
1158 register rtx dest = SET_DEST (y);
1159 while ((GET_CODE (dest) == SUBREG
1160 && (GET_CODE (SUBREG_REG (dest)) != REG
1161 || (REGNO (SUBREG_REG (dest))
1162 >= FIRST_PSEUDO_REGISTER)))
1163 || GET_CODE (dest) == ZERO_EXTRACT
1164 || GET_CODE (dest) == SIGN_EXTRACT
1165 || GET_CODE (dest) == STRICT_LOW_PART)
1166 dest = XEXP (dest, 0);
1167 if (GET_CODE (dest) == PARALLEL
1168 && GET_MODE (dest) == BLKmode)
1170 register int i;
1172 for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
1173 (*fun) (SET_DEST (XVECEXP (dest, 0, i)), y, data);
1175 else
1176 (*fun) (dest, y, data);
1182 /* Return nonzero if X's old contents don't survive after INSN.
1183 This will be true if X is (cc0) or if X is a register and
1184 X dies in INSN or because INSN entirely sets X.
1186 "Entirely set" means set directly and not through a SUBREG,
1187 ZERO_EXTRACT or SIGN_EXTRACT, so no trace of the old contents remains.
1188 Likewise, REG_INC does not count.
1190 REG may be a hard or pseudo reg. Renumbering is not taken into account,
1191 but for this use that makes no difference, since regs don't overlap
1192 during their lifetimes. Therefore, this function may be used
1193 at any time after deaths have been computed (in flow.c).
1195 If REG is a hard reg that occupies multiple machine registers, this
1196 function will only return 1 if each of those registers will be replaced
1197 by INSN. */
1200 dead_or_set_p (insn, x)
1201 rtx insn;
1202 rtx x;
1204 unsigned int regno, last_regno;
1205 unsigned int i;
1207 /* Can't use cc0_rtx below since this file is used by genattrtab.c. */
1208 if (GET_CODE (x) == CC0)
1209 return 1;
1211 if (GET_CODE (x) != REG)
1212 abort ();
1214 regno = REGNO (x);
1215 last_regno = (regno >= FIRST_PSEUDO_REGISTER ? regno
1216 : regno + HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1);
1218 for (i = regno; i <= last_regno; i++)
1219 if (! dead_or_set_regno_p (insn, i))
1220 return 0;
1222 return 1;
1225 /* Utility function for dead_or_set_p to check an individual register. Also
1226 called from flow.c. */
1229 dead_or_set_regno_p (insn, test_regno)
1230 rtx insn;
1231 unsigned int test_regno;
1233 unsigned int regno, endregno;
1234 rtx link, pattern;
1236 /* See if there is a death note for something that includes
1237 TEST_REGNO. */
1238 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1240 if (REG_NOTE_KIND (link) != REG_DEAD
1241 || GET_CODE (XEXP (link, 0)) != REG)
1242 continue;
1244 regno = REGNO (XEXP (link, 0));
1245 endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1246 : regno + HARD_REGNO_NREGS (regno,
1247 GET_MODE (XEXP (link, 0))));
1249 if (test_regno >= regno && test_regno < endregno)
1250 return 1;
1253 if (GET_CODE (insn) == CALL_INSN
1254 && find_regno_fusage (insn, CLOBBER, test_regno))
1255 return 1;
1257 pattern = PATTERN (insn);
1259 if (GET_CODE (pattern) == COND_EXEC)
1260 pattern = COND_EXEC_CODE (pattern);
1262 if (GET_CODE (pattern) == SET)
1264 rtx dest = SET_DEST (PATTERN (insn));
1266 /* A value is totally replaced if it is the destination or the
1267 destination is a SUBREG of REGNO that does not change the number of
1268 words in it. */
1269 if (GET_CODE (dest) == SUBREG
1270 && (((GET_MODE_SIZE (GET_MODE (dest))
1271 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1272 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1273 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1274 dest = SUBREG_REG (dest);
1276 if (GET_CODE (dest) != REG)
1277 return 0;
1279 regno = REGNO (dest);
1280 endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1281 : regno + HARD_REGNO_NREGS (regno, GET_MODE (dest)));
1283 return (test_regno >= regno && test_regno < endregno);
1285 else if (GET_CODE (pattern) == PARALLEL)
1287 register int i;
1289 for (i = XVECLEN (pattern, 0) - 1; i >= 0; i--)
1291 rtx body = XVECEXP (pattern, 0, i);
1293 if (GET_CODE (body) == COND_EXEC)
1294 body = COND_EXEC_CODE (body);
1296 if (GET_CODE (body) == SET || GET_CODE (body) == CLOBBER)
1298 rtx dest = SET_DEST (body);
1300 if (GET_CODE (dest) == SUBREG
1301 && (((GET_MODE_SIZE (GET_MODE (dest))
1302 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1303 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1304 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1305 dest = SUBREG_REG (dest);
1307 if (GET_CODE (dest) != REG)
1308 continue;
1310 regno = REGNO (dest);
1311 endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1312 : regno + HARD_REGNO_NREGS (regno, GET_MODE (dest)));
1314 if (test_regno >= regno && test_regno < endregno)
1315 return 1;
1320 return 0;
1323 /* Return the reg-note of kind KIND in insn INSN, if there is one.
1324 If DATUM is nonzero, look for one whose datum is DATUM. */
1327 find_reg_note (insn, kind, datum)
1328 rtx insn;
1329 enum reg_note kind;
1330 rtx datum;
1332 register rtx link;
1334 /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN. */
1335 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
1336 return 0;
1338 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1339 if (REG_NOTE_KIND (link) == kind
1340 && (datum == 0 || datum == XEXP (link, 0)))
1341 return link;
1342 return 0;
1345 /* Return the reg-note of kind KIND in insn INSN which applies to register
1346 number REGNO, if any. Return 0 if there is no such reg-note. Note that
1347 the REGNO of this NOTE need not be REGNO if REGNO is a hard register;
1348 it might be the case that the note overlaps REGNO. */
1351 find_regno_note (insn, kind, regno)
1352 rtx insn;
1353 enum reg_note kind;
1354 unsigned int regno;
1356 register rtx link;
1358 /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN. */
1359 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
1360 return 0;
1362 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1363 if (REG_NOTE_KIND (link) == kind
1364 /* Verify that it is a register, so that scratch and MEM won't cause a
1365 problem here. */
1366 && GET_CODE (XEXP (link, 0)) == REG
1367 && REGNO (XEXP (link, 0)) <= regno
1368 && ((REGNO (XEXP (link, 0))
1369 + (REGNO (XEXP (link, 0)) >= FIRST_PSEUDO_REGISTER ? 1
1370 : HARD_REGNO_NREGS (REGNO (XEXP (link, 0)),
1371 GET_MODE (XEXP (link, 0)))))
1372 > regno))
1373 return link;
1374 return 0;
1377 /* Return true if DATUM, or any overlap of DATUM, of kind CODE is found
1378 in the CALL_INSN_FUNCTION_USAGE information of INSN. */
1381 find_reg_fusage (insn, code, datum)
1382 rtx insn;
1383 enum rtx_code code;
1384 rtx datum;
1386 /* If it's not a CALL_INSN, it can't possibly have a
1387 CALL_INSN_FUNCTION_USAGE field, so don't bother checking. */
1388 if (GET_CODE (insn) != CALL_INSN)
1389 return 0;
1391 if (! datum)
1392 abort();
1394 if (GET_CODE (datum) != REG)
1396 register rtx link;
1398 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1399 link;
1400 link = XEXP (link, 1))
1401 if (GET_CODE (XEXP (link, 0)) == code
1402 && rtx_equal_p (datum, SET_DEST (XEXP (link, 0))))
1403 return 1;
1405 else
1407 unsigned int regno = REGNO (datum);
1409 /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1410 to pseudo registers, so don't bother checking. */
1412 if (regno < FIRST_PSEUDO_REGISTER)
1414 unsigned int end_regno
1415 = regno + HARD_REGNO_NREGS (regno, GET_MODE (datum));
1416 unsigned int i;
1418 for (i = regno; i < end_regno; i++)
1419 if (find_regno_fusage (insn, code, i))
1420 return 1;
1424 return 0;
1427 /* Return true if REGNO, or any overlap of REGNO, of kind CODE is found
1428 in the CALL_INSN_FUNCTION_USAGE information of INSN. */
1431 find_regno_fusage (insn, code, regno)
1432 rtx insn;
1433 enum rtx_code code;
1434 unsigned int regno;
1436 register rtx link;
1438 /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1439 to pseudo registers, so don't bother checking. */
1441 if (regno >= FIRST_PSEUDO_REGISTER
1442 || GET_CODE (insn) != CALL_INSN )
1443 return 0;
1445 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
1447 unsigned int regnote;
1448 rtx op, reg;
1450 if (GET_CODE (op = XEXP (link, 0)) == code
1451 && GET_CODE (reg = XEXP (op, 0)) == REG
1452 && (regnote = REGNO (reg)) <= regno
1453 && regnote + HARD_REGNO_NREGS (regnote, GET_MODE (reg)) > regno)
1454 return 1;
1457 return 0;
1460 /* Remove register note NOTE from the REG_NOTES of INSN. */
1462 void
1463 remove_note (insn, note)
1464 register rtx insn;
1465 register rtx note;
1467 register rtx link;
1469 if (note == NULL_RTX)
1470 return;
1472 if (REG_NOTES (insn) == note)
1474 REG_NOTES (insn) = XEXP (note, 1);
1475 return;
1478 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1479 if (XEXP (link, 1) == note)
1481 XEXP (link, 1) = XEXP (note, 1);
1482 return;
1485 abort ();
1488 /* Search LISTP (an EXPR_LIST) for NODE and remove NODE from the list
1489 if it is found.
1491 A simple equality test is used to determine if NODE is on the
1492 EXPR_LIST. */
1494 void
1495 remove_node_from_expr_list (node, listp)
1496 rtx node;
1497 rtx *listp;
1499 rtx temp = *listp;
1500 rtx prev = NULL_RTX;
1502 while (temp)
1504 if (node == XEXP (temp, 0))
1506 /* Splice the node out of the list. */
1507 if (prev)
1508 XEXP (prev, 1) = XEXP (temp, 1);
1509 else
1510 *listp = XEXP (temp, 1);
1512 return;
1514 temp = XEXP (temp, 1);
1518 /* Nonzero if X contains any volatile instructions. These are instructions
1519 which may cause unpredictable machine state instructions, and thus no
1520 instructions should be moved or combined across them. This includes
1521 only volatile asms and UNSPEC_VOLATILE instructions. */
1524 volatile_insn_p (x)
1525 rtx x;
1527 register RTX_CODE code;
1529 code = GET_CODE (x);
1530 switch (code)
1532 case LABEL_REF:
1533 case SYMBOL_REF:
1534 case CONST_INT:
1535 case CONST:
1536 case CONST_DOUBLE:
1537 case CC0:
1538 case PC:
1539 case REG:
1540 case SCRATCH:
1541 case CLOBBER:
1542 case ASM_INPUT:
1543 case ADDR_VEC:
1544 case ADDR_DIFF_VEC:
1545 case CALL:
1546 case MEM:
1547 return 0;
1549 case UNSPEC_VOLATILE:
1550 /* case TRAP_IF: This isn't clear yet. */
1551 return 1;
1553 case ASM_OPERANDS:
1554 if (MEM_VOLATILE_P (x))
1555 return 1;
1557 default:
1558 break;
1561 /* Recursively scan the operands of this expression. */
1564 register const char *fmt = GET_RTX_FORMAT (code);
1565 register int i;
1567 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1569 if (fmt[i] == 'e')
1571 if (volatile_insn_p (XEXP (x, i)))
1572 return 1;
1574 else if (fmt[i] == 'E')
1576 register int j;
1577 for (j = 0; j < XVECLEN (x, i); j++)
1578 if (volatile_insn_p (XVECEXP (x, i, j)))
1579 return 1;
1583 return 0;
1586 /* Nonzero if X contains any volatile memory references
1587 UNSPEC_VOLATILE operations or volatile ASM_OPERANDS expressions. */
1590 volatile_refs_p (x)
1591 rtx x;
1593 register RTX_CODE code;
1595 code = GET_CODE (x);
1596 switch (code)
1598 case LABEL_REF:
1599 case SYMBOL_REF:
1600 case CONST_INT:
1601 case CONST:
1602 case CONST_DOUBLE:
1603 case CC0:
1604 case PC:
1605 case REG:
1606 case SCRATCH:
1607 case CLOBBER:
1608 case ASM_INPUT:
1609 case ADDR_VEC:
1610 case ADDR_DIFF_VEC:
1611 return 0;
1613 case CALL:
1614 case UNSPEC_VOLATILE:
1615 /* case TRAP_IF: This isn't clear yet. */
1616 return 1;
1618 case MEM:
1619 case ASM_OPERANDS:
1620 if (MEM_VOLATILE_P (x))
1621 return 1;
1623 default:
1624 break;
1627 /* Recursively scan the operands of this expression. */
1630 register const char *fmt = GET_RTX_FORMAT (code);
1631 register int i;
1633 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1635 if (fmt[i] == 'e')
1637 if (volatile_refs_p (XEXP (x, i)))
1638 return 1;
1640 else if (fmt[i] == 'E')
1642 register int j;
1643 for (j = 0; j < XVECLEN (x, i); j++)
1644 if (volatile_refs_p (XVECEXP (x, i, j)))
1645 return 1;
1649 return 0;
1652 /* Similar to above, except that it also rejects register pre- and post-
1653 incrementing. */
1656 side_effects_p (x)
1657 rtx x;
1659 register RTX_CODE code;
1661 code = GET_CODE (x);
1662 switch (code)
1664 case LABEL_REF:
1665 case SYMBOL_REF:
1666 case CONST_INT:
1667 case CONST:
1668 case CONST_DOUBLE:
1669 case CC0:
1670 case PC:
1671 case REG:
1672 case SCRATCH:
1673 case ASM_INPUT:
1674 case ADDR_VEC:
1675 case ADDR_DIFF_VEC:
1676 return 0;
1678 case CLOBBER:
1679 /* Reject CLOBBER with a non-VOID mode. These are made by combine.c
1680 when some combination can't be done. If we see one, don't think
1681 that we can simplify the expression. */
1682 return (GET_MODE (x) != VOIDmode);
1684 case PRE_INC:
1685 case PRE_DEC:
1686 case POST_INC:
1687 case POST_DEC:
1688 case CALL:
1689 case UNSPEC_VOLATILE:
1690 /* case TRAP_IF: This isn't clear yet. */
1691 return 1;
1693 case MEM:
1694 case ASM_OPERANDS:
1695 if (MEM_VOLATILE_P (x))
1696 return 1;
1698 default:
1699 break;
1702 /* Recursively scan the operands of this expression. */
1705 register const char *fmt = GET_RTX_FORMAT (code);
1706 register int i;
1708 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1710 if (fmt[i] == 'e')
1712 if (side_effects_p (XEXP (x, i)))
1713 return 1;
1715 else if (fmt[i] == 'E')
1717 register int j;
1718 for (j = 0; j < XVECLEN (x, i); j++)
1719 if (side_effects_p (XVECEXP (x, i, j)))
1720 return 1;
1724 return 0;
1727 /* Return nonzero if evaluating rtx X might cause a trap. */
1730 may_trap_p (x)
1731 rtx x;
1733 int i;
1734 enum rtx_code code;
1735 const char *fmt;
1737 if (x == 0)
1738 return 0;
1739 code = GET_CODE (x);
1740 switch (code)
1742 /* Handle these cases quickly. */
1743 case CONST_INT:
1744 case CONST_DOUBLE:
1745 case SYMBOL_REF:
1746 case LABEL_REF:
1747 case CONST:
1748 case PC:
1749 case CC0:
1750 case REG:
1751 case SCRATCH:
1752 return 0;
1754 /* Conditional trap can trap! */
1755 case UNSPEC_VOLATILE:
1756 case TRAP_IF:
1757 return 1;
1759 /* Memory ref can trap unless it's a static var or a stack slot. */
1760 case MEM:
1761 return rtx_addr_can_trap_p (XEXP (x, 0));
1763 /* Division by a non-constant might trap. */
1764 case DIV:
1765 case MOD:
1766 case UDIV:
1767 case UMOD:
1768 if (! CONSTANT_P (XEXP (x, 1))
1769 || GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
1770 return 1;
1771 /* This was const0_rtx, but by not using that,
1772 we can link this file into other programs. */
1773 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 0)
1774 return 1;
1775 break;
1777 case EXPR_LIST:
1778 /* An EXPR_LIST is used to represent a function call. This
1779 certainly may trap. */
1780 return 1;
1782 default:
1783 /* Any floating arithmetic may trap. */
1784 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
1785 return 1;
1788 fmt = GET_RTX_FORMAT (code);
1789 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1791 if (fmt[i] == 'e')
1793 if (may_trap_p (XEXP (x, i)))
1794 return 1;
1796 else if (fmt[i] == 'E')
1798 register int j;
1799 for (j = 0; j < XVECLEN (x, i); j++)
1800 if (may_trap_p (XVECEXP (x, i, j)))
1801 return 1;
1804 return 0;
1807 /* Return nonzero if X contains a comparison that is not either EQ or NE,
1808 i.e., an inequality. */
1811 inequality_comparisons_p (x)
1812 rtx x;
1814 register const char *fmt;
1815 register int len, i;
1816 register enum rtx_code code = GET_CODE (x);
1818 switch (code)
1820 case REG:
1821 case SCRATCH:
1822 case PC:
1823 case CC0:
1824 case CONST_INT:
1825 case CONST_DOUBLE:
1826 case CONST:
1827 case LABEL_REF:
1828 case SYMBOL_REF:
1829 return 0;
1831 case LT:
1832 case LTU:
1833 case GT:
1834 case GTU:
1835 case LE:
1836 case LEU:
1837 case GE:
1838 case GEU:
1839 return 1;
1841 default:
1842 break;
1845 len = GET_RTX_LENGTH (code);
1846 fmt = GET_RTX_FORMAT (code);
1848 for (i = 0; i < len; i++)
1850 if (fmt[i] == 'e')
1852 if (inequality_comparisons_p (XEXP (x, i)))
1853 return 1;
1855 else if (fmt[i] == 'E')
1857 register int j;
1858 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1859 if (inequality_comparisons_p (XVECEXP (x, i, j)))
1860 return 1;
1864 return 0;
1867 /* Replace any occurrence of FROM in X with TO. The function does
1868 not enter into CONST_DOUBLE for the replace.
1870 Note that copying is not done so X must not be shared unless all copies
1871 are to be modified. */
1874 replace_rtx (x, from, to)
1875 rtx x, from, to;
1877 register int i, j;
1878 register const char *fmt;
1880 /* The following prevents loops occurrence when we change MEM in
1881 CONST_DOUBLE onto the same CONST_DOUBLE. */
1882 if (x != 0 && GET_CODE (x) == CONST_DOUBLE)
1883 return x;
1885 if (x == from)
1886 return to;
1888 /* Allow this function to make replacements in EXPR_LISTs. */
1889 if (x == 0)
1890 return 0;
1892 fmt = GET_RTX_FORMAT (GET_CODE (x));
1893 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
1895 if (fmt[i] == 'e')
1896 XEXP (x, i) = replace_rtx (XEXP (x, i), from, to);
1897 else if (fmt[i] == 'E')
1898 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1899 XVECEXP (x, i, j) = replace_rtx (XVECEXP (x, i, j), from, to);
1902 return x;
1905 /* Throughout the rtx X, replace many registers according to REG_MAP.
1906 Return the replacement for X (which may be X with altered contents).
1907 REG_MAP[R] is the replacement for register R, or 0 for don't replace.
1908 NREGS is the length of REG_MAP; regs >= NREGS are not mapped.
1910 We only support REG_MAP entries of REG or SUBREG. Also, hard registers
1911 should not be mapped to pseudos or vice versa since validate_change
1912 is not called.
1914 If REPLACE_DEST is 1, replacements are also done in destinations;
1915 otherwise, only sources are replaced. */
1918 replace_regs (x, reg_map, nregs, replace_dest)
1919 rtx x;
1920 rtx *reg_map;
1921 unsigned int nregs;
1922 int replace_dest;
1924 register enum rtx_code code;
1925 register int i;
1926 register const char *fmt;
1928 if (x == 0)
1929 return x;
1931 code = GET_CODE (x);
1932 switch (code)
1934 case SCRATCH:
1935 case PC:
1936 case CC0:
1937 case CONST_INT:
1938 case CONST_DOUBLE:
1939 case CONST:
1940 case SYMBOL_REF:
1941 case LABEL_REF:
1942 return x;
1944 case REG:
1945 /* Verify that the register has an entry before trying to access it. */
1946 if (REGNO (x) < nregs && reg_map[REGNO (x)] != 0)
1948 /* SUBREGs can't be shared. Always return a copy to ensure that if
1949 this replacement occurs more than once then each instance will
1950 get distinct rtx. */
1951 if (GET_CODE (reg_map[REGNO (x)]) == SUBREG)
1952 return copy_rtx (reg_map[REGNO (x)]);
1953 return reg_map[REGNO (x)];
1955 return x;
1957 case SUBREG:
1958 /* Prevent making nested SUBREGs. */
1959 if (GET_CODE (SUBREG_REG (x)) == REG && REGNO (SUBREG_REG (x)) < nregs
1960 && reg_map[REGNO (SUBREG_REG (x))] != 0
1961 && GET_CODE (reg_map[REGNO (SUBREG_REG (x))]) == SUBREG)
1963 rtx map_val = reg_map[REGNO (SUBREG_REG (x))];
1964 rtx map_inner = SUBREG_REG (map_val);
1966 if (GET_MODE (x) == GET_MODE (map_inner))
1967 return map_inner;
1968 else
1970 /* We cannot call gen_rtx here since we may be linked with
1971 genattrtab.c. */
1972 /* Let's try clobbering the incoming SUBREG and see
1973 if this is really safe. */
1974 SUBREG_REG (x) = map_inner;
1975 SUBREG_WORD (x) += SUBREG_WORD (map_val);
1976 return x;
1977 #if 0
1978 rtx new = rtx_alloc (SUBREG);
1979 PUT_MODE (new, GET_MODE (x));
1980 SUBREG_REG (new) = map_inner;
1981 SUBREG_WORD (new) = SUBREG_WORD (x) + SUBREG_WORD (map_val);
1982 #endif
1985 break;
1987 case SET:
1988 if (replace_dest)
1989 SET_DEST (x) = replace_regs (SET_DEST (x), reg_map, nregs, 0);
1991 else if (GET_CODE (SET_DEST (x)) == MEM
1992 || GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
1993 /* Even if we are not to replace destinations, replace register if it
1994 is CONTAINED in destination (destination is memory or
1995 STRICT_LOW_PART). */
1996 XEXP (SET_DEST (x), 0) = replace_regs (XEXP (SET_DEST (x), 0),
1997 reg_map, nregs, 0);
1998 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
1999 /* Similarly, for ZERO_EXTRACT we replace all operands. */
2000 break;
2002 SET_SRC (x) = replace_regs (SET_SRC (x), reg_map, nregs, 0);
2003 return x;
2005 default:
2006 break;
2009 fmt = GET_RTX_FORMAT (code);
2010 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2012 if (fmt[i] == 'e')
2013 XEXP (x, i) = replace_regs (XEXP (x, i), reg_map, nregs, replace_dest);
2014 else if (fmt[i] == 'E')
2016 register int j;
2017 for (j = 0; j < XVECLEN (x, i); j++)
2018 XVECEXP (x, i, j) = replace_regs (XVECEXP (x, i, j), reg_map,
2019 nregs, replace_dest);
2022 return x;
2025 /* Return 1 if X, the SRC_SRC of SET of (pc) contain a REG or MEM that is
2026 not in the constant pool and not in the condition of an IF_THEN_ELSE. */
2028 static int
2029 jmp_uses_reg_or_mem (x)
2030 rtx x;
2032 enum rtx_code code = GET_CODE (x);
2033 int i, j;
2034 const char *fmt;
2036 switch (code)
2038 case CONST:
2039 case LABEL_REF:
2040 case PC:
2041 return 0;
2043 case REG:
2044 return 1;
2046 case MEM:
2047 return ! (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
2048 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)));
2050 case IF_THEN_ELSE:
2051 return (jmp_uses_reg_or_mem (XEXP (x, 1))
2052 || jmp_uses_reg_or_mem (XEXP (x, 2)));
2054 case PLUS: case MINUS: case MULT:
2055 return (jmp_uses_reg_or_mem (XEXP (x, 0))
2056 || jmp_uses_reg_or_mem (XEXP (x, 1)));
2058 default:
2059 break;
2062 fmt = GET_RTX_FORMAT (code);
2063 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2065 if (fmt[i] == 'e'
2066 && jmp_uses_reg_or_mem (XEXP (x, i)))
2067 return 1;
2069 else if (fmt[i] == 'E')
2070 for (j = 0; j < XVECLEN (x, i); j++)
2071 if (jmp_uses_reg_or_mem (XVECEXP (x, i, j)))
2072 return 1;
2075 return 0;
2078 /* Return nonzero if INSN is an indirect jump (aka computed jump).
2080 Tablejumps and casesi insns are not considered indirect jumps;
2081 we can recognize them by a (use (lael_ref)). */
2084 computed_jump_p (insn)
2085 rtx insn;
2087 int i;
2088 if (GET_CODE (insn) == JUMP_INSN)
2090 rtx pat = PATTERN (insn);
2092 if (GET_CODE (pat) == PARALLEL)
2094 int len = XVECLEN (pat, 0);
2095 int has_use_labelref = 0;
2097 for (i = len - 1; i >= 0; i--)
2098 if (GET_CODE (XVECEXP (pat, 0, i)) == USE
2099 && (GET_CODE (XEXP (XVECEXP (pat, 0, i), 0))
2100 == LABEL_REF))
2101 has_use_labelref = 1;
2103 if (! has_use_labelref)
2104 for (i = len - 1; i >= 0; i--)
2105 if (GET_CODE (XVECEXP (pat, 0, i)) == SET
2106 && SET_DEST (XVECEXP (pat, 0, i)) == pc_rtx
2107 && jmp_uses_reg_or_mem (SET_SRC (XVECEXP (pat, 0, i))))
2108 return 1;
2110 else if (GET_CODE (pat) == SET
2111 && SET_DEST (pat) == pc_rtx
2112 && jmp_uses_reg_or_mem (SET_SRC (pat)))
2113 return 1;
2115 return 0;
2118 /* Traverse X via depth-first search, calling F for each
2119 sub-expression (including X itself). F is also passed the DATA.
2120 If F returns -1, do not traverse sub-expressions, but continue
2121 traversing the rest of the tree. If F ever returns any other
2122 non-zero value, stop the traversal, and return the value returned
2123 by F. Otherwise, return 0. This function does not traverse inside
2124 tree structure that contains RTX_EXPRs, or into sub-expressions
2125 whose format code is `0' since it is not known whether or not those
2126 codes are actually RTL.
2128 This routine is very general, and could (should?) be used to
2129 implement many of the other routines in this file. */
2132 for_each_rtx (x, f, data)
2133 rtx *x;
2134 rtx_function f;
2135 void *data;
2137 int result;
2138 int length;
2139 const char* format;
2140 int i;
2142 /* Call F on X. */
2143 result = (*f)(x, data);
2144 if (result == -1)
2145 /* Do not traverse sub-expressions. */
2146 return 0;
2147 else if (result != 0)
2148 /* Stop the traversal. */
2149 return result;
2151 if (*x == NULL_RTX)
2152 /* There are no sub-expressions. */
2153 return 0;
2155 length = GET_RTX_LENGTH (GET_CODE (*x));
2156 format = GET_RTX_FORMAT (GET_CODE (*x));
2158 for (i = 0; i < length; ++i)
2160 switch (format[i])
2162 case 'e':
2163 result = for_each_rtx (&XEXP (*x, i), f, data);
2164 if (result != 0)
2165 return result;
2166 break;
2168 case 'V':
2169 case 'E':
2170 if (XVEC (*x, i) != 0)
2172 int j;
2173 for (j = 0; j < XVECLEN (*x, i); ++j)
2175 result = for_each_rtx (&XVECEXP (*x, i, j), f, data);
2176 if (result != 0)
2177 return result;
2180 break;
2182 default:
2183 /* Nothing to do. */
2184 break;
2189 return 0;
2192 /* Searches X for any reference to REGNO, returning the rtx of the
2193 reference found if any. Otherwise, returns NULL_RTX. */
2196 regno_use_in (regno, x)
2197 unsigned int regno;
2198 rtx x;
2200 register const char *fmt;
2201 int i, j;
2202 rtx tem;
2204 if (GET_CODE (x) == REG && REGNO (x) == regno)
2205 return x;
2207 fmt = GET_RTX_FORMAT (GET_CODE (x));
2208 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2210 if (fmt[i] == 'e')
2212 if ((tem = regno_use_in (regno, XEXP (x, i))))
2213 return tem;
2215 else if (fmt[i] == 'E')
2216 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2217 if ((tem = regno_use_in (regno , XVECEXP (x, i, j))))
2218 return tem;
2221 return NULL_RTX;
2225 /* Return 1 if X is an autoincrement side effect and the register is
2226 not the stack pointer. */
2228 auto_inc_p (x)
2229 rtx x;
2231 switch (GET_CODE (x))
2233 case PRE_INC:
2234 case POST_INC:
2235 case PRE_DEC:
2236 case POST_DEC:
2237 case PRE_MODIFY:
2238 case POST_MODIFY:
2239 /* There are no REG_INC notes for SP. */
2240 if (XEXP (x, 0) != stack_pointer_rtx)
2241 return 1;
2242 default:
2243 break;
2245 return 0;
2248 /* Return 1 if the sequence of instructions beginning with FROM and up
2249 to and including TO is safe to move. If NEW_TO is non-NULL, and
2250 the sequence is not already safe to move, but can be easily
2251 extended to a sequence which is safe, then NEW_TO will point to the
2252 end of the extended sequence.
2254 For now, this function only checks that the region contains whole
2255 exception regiongs, but it could be extended to check additional
2256 conditions as well. */
2259 insns_safe_to_move_p (from, to, new_to)
2260 rtx from;
2261 rtx to;
2262 rtx *new_to;
2264 int eh_region_count = 0;
2265 int past_to_p = 0;
2266 rtx r = from;
2268 /* By default, assume the end of the region will be what was
2269 suggested. */
2270 if (new_to)
2271 *new_to = to;
2273 while (r)
2275 if (GET_CODE (r) == NOTE)
2277 switch (NOTE_LINE_NUMBER (r))
2279 case NOTE_INSN_EH_REGION_BEG:
2280 ++eh_region_count;
2281 break;
2283 case NOTE_INSN_EH_REGION_END:
2284 if (eh_region_count == 0)
2285 /* This sequence of instructions contains the end of
2286 an exception region, but not he beginning. Moving
2287 it will cause chaos. */
2288 return 0;
2290 --eh_region_count;
2291 break;
2293 default:
2294 break;
2297 else if (past_to_p)
2298 /* If we've passed TO, and we see a non-note instruction, we
2299 can't extend the sequence to a movable sequence. */
2300 return 0;
2302 if (r == to)
2304 if (!new_to)
2305 /* It's OK to move the sequence if there were matched sets of
2306 exception region notes. */
2307 return eh_region_count == 0;
2309 past_to_p = 1;
2312 /* It's OK to move the sequence if there were matched sets of
2313 exception region notes. */
2314 if (past_to_p && eh_region_count == 0)
2316 *new_to = r;
2317 return 1;
2320 /* Go to the next instruction. */
2321 r = NEXT_INSN (r);
2324 return 0;